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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -