Unit 3 Dynamic Programming
Unit 3 Dynamic Programming
Dynamic Programming
• Dynamic Programming is an algorithm design
method that can be used when the solution to a
problem may be viewed as the result of a
sequence of decisions
Dynamic Programming
Dynamic Programming
• 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
Some difficult discrete optimization problems:
1. knapsack
2. Chain Matrix Multiplication
3. Constructing an optimal binary search tree
4. Floyd’s algorithm for all-pairs shortest paths
5. Bellman Ford Algorithm
6. Traveling salesman Problem
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … v n
a knapsack of integer capacity W
find most valuable subset of the items that fit into the knapsack
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
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the knapsack
A1*A2*A3=2 possibility
First (A1*A2)*A3 second A1*(A2*A3)
2*3 3*4 4*2 36
2*3*4+0=24
2*4 4*2
2*4*2=16
Total no of mul=no of mul
for 24+16 =40
Second possibility
Matrix Chain Multiplication
A1 A2 A3
2*3 3*4 4*2
do d1 d1 d2 d2 d3
(A1*A2)*A3
2*3*4 0
c[1,2]=24 c[3 3]=0
24+0+16=40
C[1,2]+ c[3,3]+ d0*d2*d3
Cost of Cost of
A1*A2 A3
24+0+16=40
2* 4* 2
do d2 d3
C[1,2]+c[3, 3]+d0+d2+d3
i k K+1 j i-1 k j
Matrix Chain Multiplication
A1 A2 A3
2*3 3*4 4*2
do d1 d1 d2 d2 d3
A1*(A2*A3)
0+24+12=36
C[1,1] c[2,3] 2* 3* 2
C[1,1] c[2, 3] d0 d1 d3
I k k+1 j i-1 k j
Possibility
A1*(A2*(A3*A4))
A1*((A2*A3)*A4))
(A1*A2)*(A3*A4)
((A1*A2)*A3)*A4
(A1*(A2*A3))*A4
i=1 j=4
1<=k<4 k=1,2,3
C[1,4]=min k=1 {c[1,1]+c[2,4]+d0*d1*d4}
k=2 {c[1,2]+c[3,4]+d0*d2*d4}
k=3 {c[1,3]+c[4,4]+d0*d3*d4}
2<=k<4 k=2,3
C[2,4]=min k=2 {c[2,3]+c[4,4]+d1*d2*d4}
k=3 {c[2,3]+c[4,4]+d1*d3*d4}
1
Matrix
2 3
Chain
4
Multiplication
1 2 3 4
1 0 24 1 0 1
2 0 2 0
3 0 3 0
4 0 4 0
for for
whi ret
do do
le urn
ret whi
if
urn le
3 comparisons in
if worst case
4 comparisons in
worst case
Binary Search Tree Containing A Symbol
Table
• An optimal binary search tree for the identifier set a1, …, an is one that
minimize the above equation over all possible binary search trees for
this identifier set. Since all searches must terminate either successfully
or unsuccessfully, we have
Binary Search Tree With Three Identifiers
Example
w
if do
hil
e
w
if
if do hil
e w
hil
do
e
w (b) (c)
(a) hil do
e
w
do hil
e
if if
(d) (e)
Cost of Binary Search Tree In The Example
Tree c is optimal.
Determine Optimal Binary Search Tree
• If Tij is an optimal binary search tree for ai+1, …, aj, and rij
=k, then i< k <j. Tij has two subtrees L and R. L contains
ai+1, …, ak-1, and R contains ak+1, …, aj. So the cost cij of Tij is
4 w44=1
c44=0
r44=0
1, a2, a3, a4) = (do, if return,
Example
i,j 0 1 2 3 4
0 w00=2 w01=8
2, p3, p4)=(3,3,1,1) c00=0 C01=8
r00=0 r01=1
1, q2, q3, q4)=(2,3,1,1,1)
1 w11=3 w12=
0, and rii=0, 0 ≤ i ≤ 4. c11=0 c12
0]+p1+q1 r11=0 r12=
2 w22=1 w23=
+pj+qj c22=0 c23=
+p1+q1 r22=0 r23=
=8 3 w33=1 w34=
-1}+c[k,j]}+w[i,j] i<k<j c33=0 c34=
r33=0 r34=
=1
c[00]+c[11}+w[01] 4 w44=1
0+0}+8 c44=0
r44=0
, a2, a3, a4) = (do, if return,
Example
i,j 0 1 2 3 4
0 w00=2 w01=8
p3, p4)=(3,3,1,1) c00=0 C01=8
, q2, q3, q4)=(2,3,1,1,1) r00=0 r01=1
0, and rii=0, 0 ≤ i ≤ 4. 1 w11=3 w12=7
c11=0 C12=7
]+p1+q1 r11=0 r12=2
0 w00=2 w01=8
p3, p4)=(3,3,1,1) c00=0 C01=8
, q2, q3, q4)=(2,3,1,1,1) r00=0 r01=1
0, and rii=0, 0 ≤ i ≤ 4. 1 w11=3 w12=7
c11=0 C12=7
]+p1+q1 r11=0 r12=2
0 w00=2 w01=8
2, p3, p4)=(3,3,1,1) c00=0 C01=8
r00=0 r01=1
1, q2, q3, q4)=(2,3,1,1,1)
1 w11=3 w12=7
0, and rii=0, 0 ≤ i ≤ 4. c11=0 C12=7
0]+p1+q1 r11=0 r12=2
2 w22=1 w23=3
+pj+qj c22=0 c23=3
p4+q4 r22=0 r23=3
=3 3 w33=1 w34=3
-1}+c[k,j]}+w[i,j] i<k<j c33=0 c34=3
r33=0 r34=4
k=4
3]+c[44]}+w[34] 4 w44=1
0}+3 c44=0
r44=0
a2, a3, a4) = (do, if return, while).
Example
i,j 0 1 2 3 4
3, p4)=(3,3,1,1)
0 w00=2 w01=8 w02=12
q2, q3, q4)=(2,3,1,1,1) c00=0 C01=8 C02=19
and rii=0, 0 ≤ i ≤ 4. r00=0 r01=1 r02=1
p1+q1 1 w11=3 w12=7 w13=
c11=0 C12=7 C13=
+qj r11=0 r12=2 r13=
2 w22=1 w23=3 w24=
q2 c22=0 c23=3 C24=
r22=0 r23=3 r24=
+c[k,j]}+w[i,j] i<k<=j 3 w33=1 w34=3
c33=0 c34=3
2 r33=0 r34=4
0]+c[12}+w[02]
4 w44=1
1]+c[22]}+w[02] c44=0
r44=0
}+12 =19
]}+12 =20
a2, a3, a4) = (do, if return, while).
Example
i,j 0 1 2 3 4
3, p4)=(3,3,1,1)
0 w00=2 w01=8 w02=12
q2, q3, q4)=(2,3,1,1,1) c00=0 C01=8 C02=19
and rii=0, 0 ≤ i ≤ 4. r00=0 r01=1 r02=1
p1+q1 1 w11=3 w12=7 w13=9
c11=0 C12=7 C13=12
+qj r11=0 r12=2 r13=2
2 w22=1 w23=3 w24=
q3 c22=0 c23=3 C24=
r22=0 r23=3 r24=
+c[k,j]}+w[i,j] i<k<=j 3 w33=1 w34=3
c33=0 c34=3
3 r33=0 r34=4
1]+c[23}+w[13]
4 w44=1
2]+c[33]}+w[13] c44=0
r44=0
}+9 =12
]}+9 =16
a2, a3, a4) = (do, if return, while).
Example
i,j 0 1 2 3 4
p3, p4)=(3,3,1,1) 0 w00=2 w01=8 w02=12
q2, q3, q4)=(2,3,1,1,1) c00=0 C01=8 C02=19
, and rii=0, 0 ≤ i ≤ 4. r00=0 r01=1 r02=1
+p1+q1 1 w11=3 w12=7 w13=9
c11=0 C12=7 C13=12
r11=0 r12=2 r13=2
pj+qj
2 w22=1 w23=3 w24=5
+q4 c22=0 c23=3 C24=8
r22=0 r23=3 r24=3
}+c[k,j]}+w[i,j] i<k<=j 3 w33=1 w34=3
,4 c33=0 c34=3
r33=0 r34=4
22]+c[34}+w[24]
23]+c[44]}+w[24] 4 w44=1
c44=0
r44=0
3}+5 =8
0]}+5 =8
2, a3, a4) = (do, if return, while).
Example
i,j 0 1 2 3 4
, p4)=(3,3,1,1)
, q3, q4)=(2,3,1,1,1) 0 w00=2 w01=8 w02=12 w03=14
c00=0 C01=8 C02=19 c03=25
nd rii=0, 0 ≤ i ≤ 4.
r00=0 r01=1 r02=1 r03=2
1+q1
1 w11=3 w12=7 w13=9 w14=
c11=0 C12=7 C13=12 c14=
qj
r11=0 r12=2 r13=2 r14=
0 1 2 3 4 r01=1
w00=2 w11=3 w22=1 w33=1 w44=1 i=0 j=1 k=1
c00=0 c11=0 c22=0 c33=0 c44=0 LR=r[I,k-1]=r[00]=
0
RR=r[kj]=r[11] =0
r00=0 r11=0 r22=0 r33=0 r44=0
w01=8 w12=7 w23=3 w34=3
a1
1 c01=8 c12=7 c23=3 c34=3
r01=1 r12=2 r23=3 r34=4
w02=12 w13=9 w24=5
2 c02=19 c13=12 c24=8
r02=1 r13=2 r24=3 Weight of the tree= 16
w03=14 w14=11
3 c03=25 c14=19
r03=2 r14=2
w04=16
4 c04=32
r04=2
TSP BY DP
For a subset of cities S Є {1, 2, 3, ... , n} that
includes 1, and j Є S,
let C(S, j) be the length of the shortest path visiting
each node in S
exactly once, starting at 1 and ending at j.
When |S| > 1, we define C(S, 1) = ∝ since the path
cannot start and end at 1.
Now, let express C(S, j) in terms of smaller sub-
problems.
We need to start at 1 and end at j. We should select
the next city in such a way that
C(S,j)=minC(S−{j},i)+d(i,j)
where i∈S and i≠j c(S,j)=minC(s−{j},i)+d(i,j)
where i∈S and i≠j
TSP BY DP
TSP BY DP
TSP BY DP
TSP BY DP
TSP BY DP
TSP BY DP
The minimum cost path is 35.
Start from cost {1, {2, 3, 4}, 1}, we get the minimum value for d [1, 2]. When s = 3, select
the path from 1 to 2 (cost is 10) then go backwards. When s = 2, we get the minimum
value for d [4, 2]. Select the path from 2 to 4 (cost is 10) then go backwards.
When s = 1, we get the minimum value for d [4, 3]. Selecting path 4 to 3 (cost is 9), then
we shall go to then go to s = Φ step. We get the minimum value for d [3, 1] (cost is 6).
Computation Complexity of Optimal Binary
Search Tree
• To evaluate the optimal binary tree we need to compute cij for (j-
i)=1, 2, …,n in that order. When j-i=m, there are n-m+1 cij’s to
compute.
• The computation of each cij’s can be computed in time O(m).
• The total time for all cij’s with j-i=m is therefore O(nm-m2). The
total time to evaluate all the cij’s and rij’s is
Since total number of BSTs with n nodes is given by C(2n,n)/(n+1), which grows
exponentially, brute force is hopeless.
74
Analysis DP for Optimal BST Problem
Time efficiency: Θ(n3) but can be reduced to Θ(n2) by taking
advantage of monotonicity of entries in the
root table, i.e., R[i,j] is always in the range
between R[i,j-1] and R[i+1,j]
Space efficiency: Θ(n2)
75
Warshall’s Algorithm: Transitive Closure
3 3
1 1
0 0 1 0 0 0 1 0
1 0 0 1 1 1 1 1
4 4 0 0 0 0
2 0 0 0 0 2
0 1 0 0 1 1 1 1
76
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
77
Warshall’s Algorithm (recurrence)
On the k-th iteration, the algorithm determines for every pair of vertices i, j if a path exists from i
and j with just vertices 1,…,k allowed as intermediate
{
and from k to i
using just 1 ,…,k-1)
78
Warshall’s Algorithm (matrix generation)
Recurrence relating elements R(k) to elements of R(k-1) is:
79
Warshall’s Algorithm (example)
3 0 0 1 0 0 0 1 0
1 1 0 0 1 1 0 1 1
0 0 0 0 0 0 0 0
R(0) =
0 1 0 0 R(1) =
0 1 0 0
2 4
0 0 1 0 0 0 1 0 0 0 1 0
1 0 1 1 1 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
R(2) =
1 1 1 1 R(3) =
1 1 1 1 R(4) =
1 1 1 1
80
Application of Warshall’s algorithm
Warshall’s Algorithm (pseudocode and analysis)
82
Floyd’s Algorithm: All pairs shortest paths
SKIP
Problem: In a weighted (di)graph, find shortest paths between
every pair of vertices
Example:
4 3
1
1
6
1 5
4
2 3
83
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-1)[i,k]
k
D(k-1)[k,j]
D(k-1)[i,j]
84
Floyd’s Algorithm (example)
2
1 2 0 ∞ 3 ∞2 0 0 ∞ 3 ∞
∞ ∞ 2 0 5 ∞
3 6 7 ∞ 7 0 1 ∞ 7 0 1
D(0) =
6 ∞ ∞ 0 D(1) =
6 ∞ 9 0
3 1 4
0 ∞ 3 ∞ 0 10 3 4 0 10 3 4
2 0 5 ∞ 2 0 5 6 2 0 5 6
9 7 0 1 9 7 0 1 7 7 0 1
D(2) =
6 ∞ 9 0 D(3) =
6 16 9 0 D(4) =
6 16 9 0
85
Floyd’s Algorithm (pseudocode and analysis)
86
Application of Floyd’s algorithm
Dijkstra’s algorithm
Dijkstra’s algorithm
Dijkstra’s algorithm
prev keeps track of
the shortest path
Dijkstra’s algorithm
Dijkstra’s algorithm
Dijkstra’s algorithm
Single source shortest paths
• All of the shortest path algorithms we’ll look
at today are call “single source shortest
paths” algorithms
• Why?
3
B D
3
1
1 2
A
1
C E
4
3
B D
3
1
1 2
A
1
C E
4
Heap
A 0
B
3
C
B D D
3
E
0 1
1 2
A
1
C E
4
Heap
B
C
3
D
B D E
3
0 1
1 2
A
1
C E
4
Heap
B
C
3
D
B D E
3
0 1
1 2
A
1
C E
4
Heap
C 1
B
3
D
B D E
3
0 1
1 2
A
1
1
C E
4
Heap
C 1
B
3
D
B D E
3
0 1
1 2
A
1
1
C E
4
Heap
C 1
B 3
3
3
D
B D E
3
0 1
1 2
A
1
1
C E
4
Heap
C 1
B 3
3
3
D
B D E
3
0 1
1 2
A
1
1
C E
4
Heap
B 3
D
3
3
E
B D
3
0 1
1 2
A
1
1
C E
4
Heap
B 3
D
3
3
E
B D
3
0 1
1 2
A
1
1
C E
4
Heap
B 3
D
3
3
E
B D
3
0 1
1 2
A
1
1
C E
4
Heap
B 2
D
2
3
E
B D
3
0 1
1 2
A
1
1
C E
4
Heap
B 2
D
2
3
E
B D
3
0 1
1 2
A
1
1
C E
4
Heap
B 2
E 5
2
3
D
B D
3
0 1
1 2
A
1
1
C E 5
4
Heap
B 2
E 5
2
3
D
B D
3
0 1
2
A 1
Frontier?
1
1
C E 5
4
Heap
B 2
E 5
2
3
D
B D
3
0 1 All nodes reachable
1 2
A from starting node
1
1 within a given distance
C E 5
4
Heap
E 3
D 5
2 5
3
B D
3
0 1
1 2
A
1
1
C E 3
4
Heap
D 5
2 5
3
B D
3
0 1
1 2
A
1
1
C E 3
4
Heap
2 5
3
B D
3
0 1
1 2
A
1
1
C E 3
4
Heap
2 3
5
B D
0 1
1
A
1
1
C E 3
Is Dijkstra’s algorithm correct?
• Invariant:
Is Dijkstra’s algorithm correct?
• Invariant: For every vertex removed from the heap,
dist[v] is the actual shortest distance from s to v
Is Dijkstra’s algorithm correct?
• Invariant: For every vertex removed from the
heap, dist[v] is the actual shortest distance
from s to v
– The only time a vertex gets visited is when the
distance from s to that vertex is smaller than the
distance to any remaining vertex
– Therefore, there cannot be any other path that
hasn’t been visited already that would result in a
shorter path
Running time?
Running time?
1 call to MakeHeap
Running time?
|V| iterations
Running time?
|V| calls
Running time?
O(|E|) calls
Running time?
• Depends on the heap implementation
1 MakeHeap |V| ExtractMin |E| DecreaseKey Total
Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)
Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)
Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)
Fib heap O(|V|) O(|V| log |V|) O(|E|) O(|V| log |V| + |E|)
What about Dijkstra’s on…?
1
1 B D
5
A 10 -10
C E
What about Dijkstra’s on…?
1
1 B D
5
A 10 -10
C E
What about Dijkstra’s on…?
Dijkstra’s algorithm only works
for positive edge weights
1
1 B D
5
A 10
C E
Bounding the distance
• Another invariant: For each vertex v, dist[v] is
an upper bound on the actual shortest distance
– start of at
– only update the value if we find a shorter distance
• An update procedure
s p1 p2 p3 pk v
dist [v] min{dist [v], dist [u ] w(u , v)}
s p1 p2 p3 pk v
dist [v] min{dist [v], dist [u ] w(u , v)}
s p1 p2 p3 pk v
correct
dist [v] min{dist [v], dist [u ] w(u , v)}
s p1 p2 p3 pk v
correct correct
dist [v] min{dist [v], dist [u ] w(u , v)}
s p1 p2 p3 pk v
correct correct
dist [v] min{dist [v], dist [u ] w(u , v)}
s p1 p2 p3 pk v
dist [v] min{dist [v], dist [u ] w(u , v)}
s p1 p2 p3 pk v
correct correct
dist [v] min{dist [v], dist [u ] w(u , v)}
s p1 p2 p3 pk v
s p1 p2 p3 pk v
s p1 p2 p3 pk v
s p1 p2 p3 pk v
1
1 B D
5
A 10 -10
C E
3
Bellman-Ford algorithm
Bellman-Ford algorithm
S
10
A How many edges is
the shortest path
8 1
from s to:
G
-4
B A:
2
1 1
-2
F C
3
-1
E D
-1
Bellman-Ford algorithm
S
10
A How many edges is
the shortest path
8 1
from s to:
G
-4
B A: 3
2
1 1
-2
F C
3
-1
E D
-1
Bellman-Ford algorithm
S
10
A How many edges is
the shortest path
8 1
from s to:
G
-4
B A: 3
2
1 1
-2 B:
F C
3
-1
E D
-1
Bellman-Ford algorithm
S
10
A How many edges is
the shortest path
8 1
from s to:
G
-4
B A: 3
2
1 1
-2 B: 5
F C
3
-1
E D
-1
Bellman-Ford algorithm
S
10
A How many edges is
the shortest path
8 1
from s to:
G
-4
B A: 3
2
1 1
-2 B: 5
F C
3
-1
E D D:
-1
Bellman-Ford algorithm
S
10
A How many edges is
the shortest path
8 1
from s to:
G
-4
B A: 3
2
1 1
-2 B: 5
F C
3
-1
E D D: 7
-1
Bellman-Ford algorithm
0
10
S A Iteration: 0
8 1
G
-4
B
2
1 1
-2
F C
3
-1
E D
-1
Bellman-Ford algorithm
0 10
10
S A Iteration: 1
8 1
8
G
-4
B
2
1 1
-2
F C
3
-1
E D
-1
Bellman-Ford algorithm
0 10
10
S A Iteration: 2
8 1
8
G
-4
B
2
1 1
-2
9 F C
3
-1
E D
-1
12
Bellman-Ford algorithm
0 5
10
S A Iteration: 3
8 1
10
8
G
-4
B A has the correct
2 distance and path
1 1
-2
9 F C
3
-1
E D
-1
8
Bellman-Ford algorithm
0 5
10
S A Iteration: 4
8 1
6
8
G
-4
B
2
1 1
-2 11
9 F C
3
-1
E D
-1
7
Bellman-Ford algorithm
0 5
10
S A Iteration: 5
8 1
5
8
G
-4
B B has the correct
2 distance and path
1 1
-2 7
9 F C
3
-1
E D
-1
7 14
Bellman-Ford algorithm
0 5
10
S A Iteration: 6
8 1
5
8
G
-4
B
2
1 1
-2 6
9 F C
3
-1
E D
-1
7 10
Bellman-Ford algorithm
0 5
10
S A Iteration: 7
8 1
5
8
G
-4
B D (and all other
2 nodes) have the
1 1 correct distance
and path
-2 6
9 F C
3
-1
E D
-1
7 9
Correctness of Bellman-Ford
• Loop invariant:
Correctness of Bellman-Ford
• Loop invariant: After iteration i, all vertices with shortest
paths from s of length i edges or less have correct
distances
Runtime of Bellman-Ford
O(|V| |E|)
Runtime of Bellman-Ford