Greedy algorithm for knapsack in java -
i trying write simple greedy algorithm knapsack problem. inputs 2 parallel arrays. 1 array contains value of item , other array contains weights.
the greedy algorithm i’m trying write go follows: check item has highest value , put in knapsack. set value of item zero. check again item has highest value , put in knapsack if knapsack can still hold it. if can hold it, set value again 0 (after you’ve put in) , start looking highest value again. if knapsack cannot hold anymore end program.
i know there lot of better greedy algorithms out there seems me pretty simple 1 , think manage this. problem have go through entire values array find maximum value. when i’ve found put in knapsack , set value zero. problem have go in loop find new highest value item , put in knapsack. don’t know how go doing this.
i writing in java.
first of all, solve knapsack greedily using highest ratio value/weight instead of value.
secondly, not need search maximum in each step if sort list of items once , go through step step.
Comments
Post a Comment