Presentation is loading. Please wait.

Presentation is loading. Please wait.

Knapsack Problem An Example of Dynamic Programming.

Similar presentations


Presentation on theme: "Knapsack Problem An Example of Dynamic Programming."— Presentation transcript:

1 Knapsack Problem An Example of Dynamic Programming

2 Problem Definition A small knapsack of capacity M N types of items of varying size and value Find the combination of items to maximize the total value

3 Example NameABCDE Size34789 Value45101113 What is the best combination for size 17?

4 Strategy: calculate all sizes up to M for (j = 1; j <= N; j++) { for (i = 1; i <= M; i++) { if (i >= size[j] && cost[i] < cost[i-size[j]]+val[j]) { cost[i] = cost[i-size[j]]+val[j]; best[i] = j; }

5 k34567891011121314151617 cost[k]44488812 16 20 best[k]AAAAAAAAAAAAAAA cost[k]4558910121314161718202122 best[k]ABBABBABBABBABB cost[k]455810 121415161820 2224 best[k]ABBACBACCACCACC cost[k]45581011121415161820212224 best[k]ABBACDACCACCDCC cost[k]45581011131415171820212324 best[k]ABBACDECCECCDEC


Download ppt "Knapsack Problem An Example of Dynamic Programming."

Similar presentations


Ads by Google