0% found this document useful (0 votes)
31 views

Dynamic Programming: We'd Like To Have "Generic" Algorithmic Paradigms For Solving Problems

Dynamic programming is an algorithmic paradigm that is appropriate when problems exhibit recursive subproblems that are not independent. It works by breaking down a problem into smaller subproblems, solving each subproblem only once and storing the results in a table for future reference. This avoids recomputing results repeatedly. For the problem of making change using coins of different denominations, dynamic programming works by defining a table C where C[p] stores the minimum number of coins needed to make change for p cents. It fills the table in a bottom-up manner by trying all coin denominations at each value p and taking the minimum. The overall runtime is O(nk) where n is the amount in cents and k is the number of coin denomin

Uploaded by

kamalesh123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Dynamic Programming: We'd Like To Have "Generic" Algorithmic Paradigms For Solving Problems

Dynamic programming is an algorithmic paradigm that is appropriate when problems exhibit recursive subproblems that are not independent. It works by breaking down a problem into smaller subproblems, solving each subproblem only once and storing the results in a table for future reference. This avoids recomputing results repeatedly. For the problem of making change using coins of different denominations, dynamic programming works by defining a table C where C[p] stores the minimum number of coins needed to make change for p cents. It fills the table in a bottom-up manner by trying all coin denominations at each value p and taking the minimum. The overall runtime is O(nk) where n is the amount in cents and k is the number of coin denomin

Uploaded by

kamalesh123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Dynamic Programming

Wed like to have generic algorithmic paradigms for solving problems


Example:

Divide and conquer

Break problem into independent subproblems


Recursively solve subproblems (subproblems are smaller instances of
main problem)
Combine solutions

Examples:
Mergesort,
Quicksort,
Strassens algorithm
...
Dynamic Programming: Appropriate when you have recursive subproblems that are not independent

Example: Making Change


Problem:

A country has coins with denominations


1 = d1 < d2 < < dk .

You want to make change for n cents, using the smallest number of coins.
Example: U.S. coins
d1 = 1 d2 = 5 d3 = 10 d4 = 25

Change for 37 cents 1 quarter, 1 dime, 2 pennies.


What is the algorithm?

Change in another system


Suppose
d1 = 1 d2 = 4 d3 = 5 d4 = 10
Change for 7 cents 5,1,1
Change for 8 cents 4,4
What can we do?

Change in another system


Suppose
d1 = 1 d2 = 4 d3 = 5 d4 = 10
Change for 7 cents 5,1,1
Change for 8 cents 4,4
What can we do?
The answer is counterintuitive. To make change for n cents, we are going
to figure out how to make change for every value x < n first. We then build
up the solution out of the solution for smaller values.

Solution
We will only concentrate on computing the number of coins. We will
later recreate the solution.
Let C[p] be the minimum number of coins needed to make change for
p cents.
Let x be the value of the first coin used in the optimal solution.
Then C[p] = 1 + C[p x] .

Problem:

We dont know x.

Solution
We will only concentrate on computing the number of coins. We will
later recreate the solution.
Let C[p] be the minimum number of coins needed to make change for
p cents.
Let x be the value of the first coin used in the optimal solution.
Then C[p] = 1 + C[p x] .

Problem:
Answer:

We dont know x.
We will try all possible x and take the minimum.

C[p] =

mini:dip{C[p di] + 1} if p > 0


0 if p = 0

Example: penny, nickel, dime

C[p] =

1
2
3
4
5
6

mini:dip{C[p di] + 1} if p > 0


0 if p = 0

Change(p)
if (p < 0)
then return
elseif (p = 0)
then return 0
else
return 1 + min{Change(p 1), Change(p 5), Change(p 10)}

What is the running time?

(dont do analysis here)

Dynamic Programming Algorithm

1
2
3
4
5
6
7
8
9
10
11
12

DP-Change(n)
C[< 0] =
C[0] = 0
for p = 2 to n
do min =
for i = 1 to k
do if (p di)
then if (C[p di]) + 1 < min)
then min = C[p di] + 1
coin = i
C[p] = min
S[p] = coin

Running Time:

O(nk)

Dynamic Programming
Used when:
Optimal substructure
Overlapping subproblems

Methodology
Characterize structure of optimal solution
Recursively define value of optimal solution
Compute in a bottom-up manner

You might also like