8/26/2023 0 Comments Knapsack problem using openglThe actual method in Java can fit in very few lines of code. Note that is called twice: once with a capacity of 20 (because we didn't take 11) and once with a capacity of 9 (because with did take 11).ġ1 not taken, calling with a capacity of 20Ĩ taken, calling with a capacity of 12 (20 - 8)ħ taken, calling with a capacity of 5 (12 - 7)Ħ not taken, calling with a capacity of 5 I did on purpose show the call to with a capacity of 20, which gives a result of 20 (8 + 7 + 5). In this case, it could look like this: Calling 11 8 7 6 5 with cap: 20 Sometimes it helps to print what the recursive calls may look like. When you don't take the item, you remove if from you list but you do not decrease the capacity. ![]() When you take the item, you remove it from your list and you decrease the capacity by the weight of the item. The idea, given the problem you stated (which specifies we must use recursion) is simple: for each item that you can take, see if it's better to take it or not.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |