Dynamic Programming

Post on 13-Jun-2015

681 views 0 download

Tags:

Transcript of Dynamic Programming

Dynamic Programming

Kasun Ranga Wijeweera

(Email: krw19870829@gmail.com)

Introduction• In divide and conquer principle, large problem is solved by

breaking it up into smaller problems which can be solved independently

• In dynamic programming, this principle is carried to an extreme

• When we do not know exactly which smaller problems to solve, we simply solve them all, then store the answers away to be used later in solving larger problems

Two Difficulties• It may not always be possible to combine the solutions of

smaller problems to form the solution of a larger one• The number of small problems to solve may be unacceptably

large

Knapsack Problem• A thief robbing a safe finds it filled with N types of items of

varying size and value• But has only a knapsack of capacity M to carry the goods• The knapsack problem is to find the combination of items

which the thief should choose for his knapsack in order to maximize the total value of all the items he takes

Example• Capacity of the knapsack = 17• Then the thief can take five A’s for a total take of 20, or he can

fill 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 9

Value 4 5 10 11 13

Name A B C D E

Dynamic Programming Solution• We calculate the best combination for all knapsack sizes up to

M• It turns out that we can perform this calculation very

efficiently by doing things in an appropriate order

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;

}

}

Details• cost[i] is the highest value that can be achieved with a

knapsack of capacity i• best[i] is the last item that was added to achieve that maximum• cost[0] = 0

i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

cost[i] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

best[i]

Contents of the Optimal Knapsack• The actual contents of the optimal knapsack can be computed

with the aid of the best array• By definition, best[M] is included• The remaining contents are the same as for the optimal

knapsack of size M – size[best[M]]• Therefore best[M – size[best[M]]] is included, and so forth

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

Conclusions• The dynamic programming solution to the knapsack problem

takes time proportional to N * M• The knapsack problem is easily solved if M is not large• But the running time can become unacceptable for large

capabilities• This method does not work at all if M and the sizes or values

are, for example, real numbers instead of integers

Reference

Any Questions?

Thank You!