Dynamic Programming
Dynamic Programming
• Dynamic Programming is a general algorithm design technique for
solving problems defined by or formulated as recurrences with
overlapping subinstances.
• Invented by American mathematician Richard Bellman in the 1950s
to solve optimization problems.
• “Programming” here means “planning”
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.
Elements of Dynamic Programming
•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)
• F(0) = 0
• F(1) = 1
Example 1: Fibonacci numbers (cont.)
Computing the nth
Fibonacci number recursively (top-down):
F(n)
F(n-1) + F(n-2)
F(n-2) + F(n-3) F(n-3) + F(n-4)
...
Other examples of DP algorithms
• Computing a binomial coefficient
• Constructing an optimal binary search tree
• Warshall’s algorithm for transitive closure
• Floyd’s algorithm for all-pairs shortest paths
• Some difficult discrete optimization problems:
- knapsack
- traveling salesman
Warshall’s Algorithm: Transitive Closure
• Computes the transitive closure of a relation
• Alternatively: existence of all nontrivial paths in a digraph
• Example of transitive closure:
3
4
2
1
0 0 1 0
1 0 0 1
0 0 0 0
0 1 0 0
0 0 1 0
1 1 1 1
0 0 0 0
1 1 1 1
14
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
4
2
1
3
4
2
1
3
4
2
1
3
4
2
1
R(0)
0 0 1 0
1 0 0 1
0 0 0 0
0 1 0 0
R(1)
0 0 1 0
1 0 1 1
0 0 0 0
0 1 0 0
R(2)
0 0 1 0
1 0 1 1
0 0 0 0
1 1 1 1
R(3)
0 0 1 0
1 0 1 1
0 0 0 0
1 1 1 1
R(4)
0 0 1 0
1 1 1 1
0 0 0 0
1 1 1 1
3
4
2
1
15
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
R(k-1)
[i,j] (path using just 1 ,…,k-1)
R(k)
[i,j] = or
R(k-1)
[i,k] and R(k-1)
[k,j] (path from i to k and from k to i
using just 1 ,…,k-1)
i
j
k
{
16
Warshall’s Algorithm (matrix generation)
Recurrence relating elements R(k)
to elements of R(k-1)
is:
R(k)
[i,j] = R(k-1)
[i,j] or (R(k-1)
[i,k] and R(k-1)
[k,j])
It implies the following rules for generating R(k)
from R(k-1)
:
Rule 1 If an element in row i and column j is 1 in R(k-1)
,
it remains 1 in R(k)
Rule 2 If an element in row i and column j is 0 in R(k-1)
,
it has to be changed to 1 in R(k)
if and only if
the element in its row i and column k and the element
in its column j and row k are both 1’s in R(k-1)
17
Warshall’s Algorithm (example)
3
4
2
1 0 0 1 0
1 0 0 1
0 0 0 0
0 1 0 0
R(0) =
0 0 1 0
1 0 1 1
0 0 0 0
0 1 0 0
R(1) =
0 0 1 0
1 0 1 1
0 0 0 0
1 1 1 1
R(2) =
0 0 1 0
1 0 1 1
0 0 0 0
1 1 1 1
R(3) =
0 0 1 0
1 1 1 1
0 0 0 0
1 1 1 1
R(4) =
18
Warshall’s Algorithm (pseudocode and analysis)
Time efficiency: Θ(n3
)
Space efficiency: Matrices can be written over their predecessors
19
Floyd’s Algorithm: All pairs shortest paths
Problem: In a weighted (di)graph, find shortest paths between
every pair of vertices
Same idea: construct solution through series of matrices D(0)
, …,
D (n)
using increasing subsets of the vertices allowed
as intermediate
Example:
3
4
2
1
4
1
6
1
5
3
20
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]}
i
j
k
D(k-1)
[i,j]
D(k-1)
[i,k]
D(k-1)
[k,j]
21
Floyd’s Algorithm (example)
0 ∞ 3 ∞
2 0 ∞ ∞
∞ 7 0 1
6 ∞ ∞ 0
D(0) =
0 ∞ 3 ∞
2 0 5 ∞
∞ 7 0 1
6 ∞ 9 0
D(1) =
0 ∞ 3 ∞
2 0 5 ∞
9 7 0 1
6 ∞ 9 0
D(2) =
0 10 3 4
2 0 5 6
9 7 0 1
6 16 9 0
D(3) =
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
D(4) =
3
1
3
2
6 7
4
1 2
D(3): 3 to 1 not allowing 4=9. D(4): 3 to 1 with allowing 4=7
22
Floyd’s Algorithm (pseudocode and analysis)
Time efficiency: Θ(n3
)
Space efficiency: Matrices can be written over their predecessors
Note: Shortest paths themselves can be found, too

LU_30_Dynamic_Programming_Warshal_Floyd_1712140744434.pptx

  • 1.
  • 2.
    Dynamic Programming • DynamicProgramming is a general algorithm design technique for solving problems defined by or formulated as recurrences with overlapping subinstances. • Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems. • “Programming” here means “planning”
  • 3.
    Dynamic Programming- Mainidea: • 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.
  • 4.
    Elements of DynamicProgramming •Optimal Substructure •Overlapping Subproblems •Memorization
  • 5.
    Elements of DynamicProgramming (Contd.,)
  • 6.
    Elements of DynamicProgramming (Contd.,)
  • 7.
    Elements of DynamicProgramming (Contd.,)
  • 8.
    Elements of DynamicProgramming (Contd.,)
  • 9.
    Elements of DynamicProgramming (Contd.,)
  • 10.
    Example 1: Fibonaccinumbers Definition of Fibonacci numbers: • F(n) = F(n-1) + F(n-2) • F(0) = 0 • F(1) = 1
  • 11.
    Example 1: Fibonaccinumbers (cont.) Computing the nth Fibonacci number recursively (top-down): F(n) F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4) ...
  • 12.
    Other examples ofDP algorithms • Computing a binomial coefficient • Constructing an optimal binary search tree • Warshall’s algorithm for transitive closure • Floyd’s algorithm for all-pairs shortest paths • Some difficult discrete optimization problems: - knapsack - traveling salesman
  • 13.
    Warshall’s Algorithm: TransitiveClosure • Computes the transitive closure of a relation • Alternatively: existence of all nontrivial paths in a digraph • Example of transitive closure: 3 4 2 1 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1
  • 14.
    14 Warshall’s Algorithm Constructs transitiveclosure 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 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 R(0) 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 R(1) 0 0 1 0 1 0 1 1 0 0 0 0 0 1 0 0 R(2) 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R(3) 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R(4) 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 3 4 2 1
  • 15.
    15 Warshall’s Algorithm (recurrence) Onthe 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 R(k-1) [i,j] (path using just 1 ,…,k-1) R(k) [i,j] = or R(k-1) [i,k] and R(k-1) [k,j] (path from i to k and from k to i using just 1 ,…,k-1) i j k {
  • 16.
    16 Warshall’s Algorithm (matrixgeneration) Recurrence relating elements R(k) to elements of R(k-1) is: R(k) [i,j] = R(k-1) [i,j] or (R(k-1) [i,k] and R(k-1) [k,j]) It implies the following rules for generating R(k) from R(k-1) : Rule 1 If an element in row i and column j is 1 in R(k-1) , it remains 1 in R(k) Rule 2 If an element in row i and column j is 0 in R(k-1) , it has to be changed to 1 in R(k) if and only if the element in its row i and column k and the element in its column j and row k are both 1’s in R(k-1)
  • 17.
    17 Warshall’s Algorithm (example) 3 4 2 10 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 R(0) = 0 0 1 0 1 0 1 1 0 0 0 0 0 1 0 0 R(1) = 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R(2) = 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 R(3) = 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 R(4) =
  • 18.
    18 Warshall’s Algorithm (pseudocodeand analysis) Time efficiency: Θ(n3 ) Space efficiency: Matrices can be written over their predecessors
  • 19.
    19 Floyd’s Algorithm: Allpairs shortest paths Problem: In a weighted (di)graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matrices D(0) , …, D (n) using increasing subsets of the vertices allowed as intermediate Example: 3 4 2 1 4 1 6 1 5 3
  • 20.
    20 Floyd’s Algorithm (matrixgeneration) 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]} i j k D(k-1) [i,j] D(k-1) [i,k] D(k-1) [k,j]
  • 21.
    21 Floyd’s Algorithm (example) 0∞ 3 ∞ 2 0 ∞ ∞ ∞ 7 0 1 6 ∞ ∞ 0 D(0) = 0 ∞ 3 ∞ 2 0 5 ∞ ∞ 7 0 1 6 ∞ 9 0 D(1) = 0 ∞ 3 ∞ 2 0 5 ∞ 9 7 0 1 6 ∞ 9 0 D(2) = 0 10 3 4 2 0 5 6 9 7 0 1 6 16 9 0 D(3) = 0 10 3 4 2 0 5 6 7 7 0 1 6 16 9 0 D(4) = 3 1 3 2 6 7 4 1 2 D(3): 3 to 1 not allowing 4=9. D(4): 3 to 1 with allowing 4=7
  • 22.
    22 Floyd’s Algorithm (pseudocodeand analysis) Time efficiency: Θ(n3 ) Space efficiency: Matrices can be written over their predecessors Note: Shortest paths themselves can be found, too