44 Dynamic Programming
44 Dynamic Programming
Algorithm types
Counting coins
The greedy method would not work if we did not have 5 coins
At each step, just choose the largest coin that does not overshoot the
desired amount: 31=25
For 63 cents, the greedy method gives six coins (25+25+10+1+1+1), but
we can do it with three (21+21+21)
How can we find the minimum number of coins for any given
coin set?
3
A simple solution
To make K cents:
If there is a K-cent coin, then that one coin is the minimum
Otherwise, for each value i < K,
Another solution
Idea: Solve first for one cent, then two cents, then three cents,
etc., up to the desired amount
Example
To make 6, try
Etc.
Example: Quicksort
Example: Mergesort
Example: Binary search
14
If either subset, n1 or n2, can be made with fewer coins, then clearly
N can be made with fewer coins, hence solution was not optimal
15
Example: In US coinage,
16
B
1
17
He has to select the items to steal in order to maximize the value of his
loot, but cannot exceed 25 pounds
18
Comments
These solutions to simpler problems are then used to compute the solution
to more complex problems
Only problems that satisfy the principle of optimality are suitable for
dynamic programming solutions
The End
20