0% found this document useful (0 votes)
2 views

LU-30 Dynamic Programming, Warshal, Floyd

Dynamic Programming is an algorithm design technique for solving problems with overlapping subproblems, introduced by Richard Bellman in the 1950s. It involves setting up recurrences, solving smaller instances once, and recording solutions in a table to extract the final solution. Examples include Fibonacci numbers, binomial coefficients, and algorithms like Floyd's for finding shortest paths in graphs.

Uploaded by

akashkalimuthu4
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LU-30 Dynamic Programming, Warshal, Floyd

Dynamic Programming is an algorithm design technique for solving problems with overlapping subproblems, introduced by Richard Bellman in the 1950s. It involves setting up recurrences, solving smaller instances once, and recording solutions in a table to extract the final solution. Examples include Fibonacci numbers, binomial coefficients, and algorithms like Floyd's for finding shortest paths in graphs.

Uploaded by

akashkalimuthu4
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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
Floyd’s Algorithm: All pairs shortest paths
Problem: In a weighted (di)graph, find shortest
paths between every pair of vertices

Idea: Construct solution through series of matrices


D(0), …, D (n) using increasing subsets of the vertices
allowed as intermediate

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)[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
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

D(3): 3 to 1 not allowing 4=9. D(4): 3 to 1 with allowing 4=7


15
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

16

You might also like