Dynamic Programming

934 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
934
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
61
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dynamic Programming

  1. 1. Dynamic ProgrammingKasun Ranga Wijeweera(Email: krw19870829@gmail.com)
  2. 2. Introduction• In divide and conquer principle, large problem is solved bybreaking it up into smaller problems which can be solvedindependently• In dynamic programming, this principle is carried to anextreme• When we do not know exactly which smaller problems tosolve, we simply solve them all, then store the answers awayto be used later in solving larger problems
  3. 3. Two Difficulties• It may not always be possible to combine the solutions ofsmaller problems to form the solution of a larger one• The number of small problems to solve may be unacceptablylarge
  4. 4. Knapsack Problem• A thief robbing a safe finds it filled with N types of items ofvarying size and value• But has only a knapsack of capacity M to carry the goods• The knapsack problem is to find the combination of itemswhich the thief should choose for his knapsack in order tomaximize the total value of all the items he takes
  5. 5. Example• Capacity of the knapsack = 17• Then the thief can take five A’s for a total take of 20, or he canfill up his knapsack with a D and an E for a total take of 24• He can try many other combinations• But which will maximize his total take?Size 3 4 7 8 9Value 4 5 10 11 13Name A B C D E
  6. 6. Dynamic Programming Solution• We calculate the best combination for all knapsack sizes up toM• It turns out that we can perform this calculation veryefficiently by doing things in an appropriate order
  7. 7. The programfor (j = 1; j <= N; j++){for (i = 1; i <= M; i++)if( i >= size[j] )if( cost[i] < cost[i – size[j]) + val[j] ){cost[i] = cost[i – size[j]] + val[j];best[i] = j;}}
  8. 8. Details• cost[i] is the highest value that can be achieved with aknapsack of capacity i• best[i] is the last item that was added to achieve that maximum• cost[0] = 0i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17cost[i] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0best[i]
  9. 9. Contents of the Optimal Knapsack• The actual contents of the optimal knapsack can be computedwith the aid of the best array• By definition, best[M] is included• The remaining contents are the same as for the optimalknapsack of size M – size[best[M]]• Therefore best[M – size[best[M]]] is included, and so forth
  10. 10. For Our Example• First, best[17] = C• Then we find another type-C item at size 10• Then a type-A item at size 3
  11. 11. Conclusions• The dynamic programming solution to the knapsack problemtakes time proportional to N * M• The knapsack problem is easily solved if M is not large• But the running time can become unacceptable for largecapabilities• This method does not work at all if M and the sizes or valuesare, for example, real numbers instead of integers
  12. 12. Reference
  13. 13. Any Questions?
  14. 14. Thank You!

×