LU-30 Dynamic Programming, Warshal, Floyd
LU-30 Dynamic Programming, Warshal, Floyd
Dynamic Programming
• Dynamic Programming is a general algorithm design
technique for solving problems defined by or formulated as
recurrences with overlapping subinstances.
• Optimal Substructure
• Overlapping Subproblems
• Memorization
Elements of Dynamic Programming
(Contd.,)
Elements of Dynamic Programming
(Contd.,)
Elements of Dynamic Programming
(Contd.,)
Elements of Dynamic Programming
(Contd.,)
Elements of Dynamic Programming
(Contd.,)
Example 1: Fibonacci numbers
Definition of Fibonacci numbers:
F(n)
F(n-1) + F(n-2)
Example: 4 3
1
1
6
1 5
4
2 3
13
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
i
D(k-1)[k,j]
D(k-1)[i,j]
j
14
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
16