U3 - Dynamic Programming
U3 - Dynamic Programming
PROGRAMMING
-BHASKAR H S
Dynamic Programming
Dynamic Programming is a general algorithm design
technique for solving problems defined by recurrences with
overlapping subproblems
• Main idea:
- set up a recurrence relating a solution to a larger
instance to solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Examples of DP algorithms
3 3
1 1
0 0 1 0 0 0 1 0
1 0 0 1 1 1 1 1
2 4 0 0 0 0 4 0 0 0 0
0 1 0 0
2 1 1 1 1
Warshall’s Algorithm
Constructs transitive closure T as the last matrix in the sequence
of n-by-n matrices R(0), … , R(k), … , R(n) where
R(k)[i,j] = 1 iff there is nontrivial path from i to j with only first k
vertices allowed as intermediate
Note that R(0) = A (adjacency matrix), R(n) = T (transitive closure)
3 3 3 3 3
1 1 1 1 1
4 4 4 2 4 4
2 2 2 2
{
allowed as intermediate
j
Warshall’s Algorithm (matrix
generation)
Recurrence relating elements R(k) to elements of
R(k-1) is:
0 0 1 0 0 0 1 0 0 0 1 0
1 0 1 1 1 0 1 1 1 1 1 1
R(2) = 0 0 0 0 R(3) = 0 0 0 0 R(4) = 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
Warshall’s Algorithm (pseudocode and analysis)
2 4
3
Floyd’s Algorithm (matrix
generation)
On the k-th iteration, the algorithm determines
shortest paths between every pair of vertices i, j
that use only vertices among 1,…,k as
intermediate
D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]}
D(k-1)[i,k]
k
i
D(k-1)[k,j]
D(k-1)[i,j]
j
Floyd’s Algorithm (example)
1
2 2 0 ∞ 3 ∞ 0 ∞ 3 ∞
3 6 7 2 0 ∞ ∞ 2 0 5 ∞
D(0) = ∞ 7 0 1 D(1) = ∞ 7 0 1
3
1
4 6 ∞ ∞ 0 6 ∞ 9 0
0 ∞ 3 ∞ 0 10 3 4 0 10 3 4
2 0 5 ∞ 2 0 5 6 2 0 5 6
D(2) = 9 7 0 1 D(3) = 9 7 0 1 D(4) = 7 7 0 1
6 ∞ 9 0 6 16 9 0 6 16 9 0
Recursive solution?
What is smaller problem?
How to use solution to smaller in solution to larger
Table?
Order to solve?
Initial conditions?
Knapsack Problem by DP (example)
Example: Knapsack of capacity W = 5
item weight value
1 2 $12
2 1 $10
3 3 $20
4 2 $15 capacity j
0 1 2 3 4 5
0 0 0 0 0 0 0
w1 = 1
2, v1= 012
w2 = 2
1, v2= 010
3 0
w3 = 3, v3= 20
4 0
w4 = 2, v4= 15
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … w n
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the
knapsack
w1 = 2, v1= 12
w2 = 1, v2= 10
w3 = 3, v3= 20
w4 = 2, v4= 15
Computing Binomial
Coefficient
A K-combination of an n-Set or in Simple nCk
We have learnt a method of computing this Binomial
Coefficient by the formula
=