Chap 6 - DynamicProgramming(Binomial-AllShotestPath)
Chap 6 - DynamicProgramming(Binomial-AllShotestPath)
Applications of DP technique:
(ii) Binomial
(ii) Shortest Path.
Dr. H. Bahig 2
Input • Given n and k.
Output • C(n,k).
Dr. H. Bahig 3
Binomial Coefficients
• (x + y)2 = x2 + 2xy + y2, coefficients are 1,2,1
• (x + y)3 = x3 + 3x2y + 3xy2 + y3, coefficients are 1,3,3,1
• (x + y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4,
coefficients are 1,4,6,4,1
• (x + y)5 = x5 + 5x4y + 10x3y2 + 10x2y3 + 5xy4 + y5,
coefficients are 1,5,10,10,5,1
• The n+1 coefficients can be computed for (x + y)n according
to the formula c(n, i) = n! / (i! * (n – i)!)
for each of i = 0..n
• The repeated computation of all the factorials gets to be
expensive.
4 Dr. H. Bahig
Main Idea
Complexity Analysis
4 3
Examples 1
1
6
1 5
4
2 3
i
D(k-1)[k,j]
D(k-1)[i,j]
j
All pairs shortest paths
Main Idea
Construct solution through series of matrices D(0), …, D (n) using
increasing subsets of the vertices allowed as intermediate.
i
D(k-1)[k,j]
D(k-1)[i,j]
j
Floyd’s Algorithm (example)
2
1 2 0 ∞ 3 ∞ 0 ∞ 3 ∞
6 7 2 0 ∞ ∞ 2 0 5 ∞
3 ∞ 7 0 1 ∞ 7 0 1
D(0) =
D(1) =
6 ∞ ∞ 0 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
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
2
2 4 2 2
1 2 1 2
3 6 7
3 6 7 3 6 7
1 1 3
4 1 3 3 1 4
Algorithm
Complexity Analysis
Time efficiency: Θ(n3)
Space efficiency: Matrices can be written over their predecessors