Chapter Four
Chapter Four
Dynamic
Programming
Introduction to Dynamic
Programming
• Dynamic programming, like the divide-and-conquer method, solves
problems by combining the solutions to subproblems. (“Programming” in
this context refers to a tabular method, not to writing computer code.)
• Dynamic programming applies when the subproblems overlap—that is,
when subproblems share subsubproblems.
• In this context, a divide-and-conquer algorithm does more work than
necessary, repeatedly solving the common subsubproblems.
• A dynamic-programming algorithm solves each subsubproblem just once
and then saves its answer in a table, thereby avoiding the work of
recomputing the answer every time it solves each subsubproblem
• We typically apply dynamic programming to optimization problems.
• Such problems can have many possible solutions. Each solution has a
value, and we wish to find a solution with the optimal (minimum or
maximum) value.
• We call such a solution an optimal solution to the problem, as
opposed to the optimal solution, since there may be several solutions
that achieve the optimal value.
• When developing a dynamic-programming algorithm, we follow a
sequence of four steps:
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution, typically in a bottom-up fashion.
4. Construct an optimal solution from computed information.
All pairs Shortest Path – Floyd-
Warshall Algorithm
Psudocode
• k=1,i=2,j=3
• k=2,i=4,j=1
• k=2,i=4,j=3
• k=3,i=1,j=4
• k=3,i=2,j=4
• k=4,i=1,j=2
• What is the difference between Floyd-Warshall & Dijkstra algorithm
• Apply Dijkstra on the above question
Minimum Coin Change Problem
• You are given an array of coins with varying denominations (infinite)
and an integer sum representing the total amount of money; you
must return the fewest coins required to make up that sum; if that
sum cannot be constructed, return -1.
• Given
• Let the values of the matrix be a[n][m]
• Coins[z].
Initialize a[i][0] 0
For i=0 to coins.length()-1
For j=0 to w
If conis [i]>j then
a[i][j] a[i-1][j]
Else
A[i][j] min(a[i-1][j],1+a[i][j-cons[i])
End if
Example
• Given coins of 1,5,6,9 denomination what is the minimum amount of coin denomination needed to
make sumj
equals to 10.
w 0 1 2 3 4 5 6 7 8 9 10
i Coins
1 0 1 2 3 4 5 6 7 8 9 10
5 0 1 2 3 4 1 2 3 4 5 2
6 0 1 2 3 4 1 1 2 3 4 2
9 0 1 2 3 4 1 1 2 3 1 2
• The minimum number of coins needed are 2 and we can use (5,5)
Number of ways to make Coin
change
• You are given an array of coins with varying denominations (infinite)
and an integer sum representing the total amount of money; you
must return the total number of ways to make the change of the
given amount using the given coins given.
• Given
• Let the values of the matrix be a[n][m]
• Coins[z].
Initialize a[i][0] 1
For i=0 to coins.length()-1
For j=0 to w
If conis [i]>j then
a[i][j] a[i-1][j]
Else
A[i][j] a[i-1][j]+a[i][j-cons[i])
End if
Example
• Given coins of 2,3,5,10 denomination what is the minimum amount of
coin denomination needed to make sum equals to 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
3 1 0 1 1 1 1 2 1 2 2 2 2 3 2 3 3
5 1 0 1 1 1 2 2 2 3 3 4 4 5 5 6 7
10 1 0 1 1 1 2 2 2 3 3 5 4 6 6 7 9
0/1 Knapsack
• Here knapsack is like a container or a bag.
• Suppose we have given some items which have some weights or
profits.
• We have to put some items in the knapsack in such a way total value
produces a maximum profit.
• For example, the weight of the container is 20 kg.
• We have to select the items in such a way that the sum of the weight of items
should be either smaller than or equal to the weight of the container, and the
profit should be maximum.
• The 0/1 knapsack problem means that the items are either
completely or no items are filled in a knapsack.
• For example, we have two items having weights 2kg and 3kg,
respectively. If we pick the 2kg item then we cannot pick 1kg item
from the 2kg item (item is not divisible); we have to pick the 2kg item
completely.
• This is a 0/1 knapsack problem in which either we pick the item
completely or we will pick that item.
• The 0/1 knapsack problem is solved by the dynamic programming.
Example
Reading Assignment
Fractional
Pseudocode
knapsack
Write the What is the
pseudocode for pseudocode for
0/1 knapsack fractional
problem knapsack
Support your
algorithm with
example
Introduction to Probabilistic Algorithms - Parallel
Algorithms