Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

RAIK 283: Data Structures & Algorithms

Dynamic Programming
Warshall’s and Floyd’sAlgorithm

Dr. Ying Lu
ylu@cse.unl.edu

Design and Analysis of Algorithms - Chapter 8 1


RAIK 283: Data Structures & Algorithms
 Giving credit where credit is due:
• Most of the lecture notes are based on the slides from
the Textbook’s companion website
– http://www.aw-bc.com/info/levitin

• Some of the notes are based on slides created by Dr.


Philippe Laborie, ILOG and Mark Llewellyn,
University of Central Florida.

• I have modified many of their slides and added new


slides.

Design and Analysis of Algorithms - Chapter 8 2


Examples of dynamic programming algorithms
• Coin-row problem

• Computing binomial coefficients

• Longest Common Subsequence (LCS)

•Warshall’s algorithm for transitive closure

• Floyd’s algorithm for all-pairs shortest paths

•Some instances of difficult discrete optimization problems:


•0-1 knapsack

3
Warshall’s algorithm: transitive closure
• Computes the transitive closure of a relation
• (Alternatively: all paths in a directed graph)

• Example of transitive closure:

3 3
1 1

4 4 0 0 1 0
2 0 0 1 0 2
1 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0
0 1 0 0 1 1 1 1

Design and Analysis of Algorithms - Chapter 8 4


Warshall’s algorithm
• Main idea: a path exists between two vertices i, j, iff
•there is an edge from i to j; or

•there is a path from i to j going through intermediate


vertices which are drawn from set {vertex 1}; or

•there is a path from i to j going through intermediate


vertices which are drawn from set {vertex 1, 2}; or

•…

Design and Analysis of Algorithms - Chapter 8 5


Warshall’s algorithm
• Main idea: a path exists between two vertices i, j, iff
•there is a path from i to j going through intermediate
vertices which are drawn from set {vertex 1, 2, … k-1}; or

•there is a path from i to j going through intermediate


vertices which are drawn from set {vertex 1, 2, … k}; or

•...

•there is a path from i to j going through any of the other


vertices

Design and Analysis of Algorithms - Chapter 8 6


Warshall’s algorithm

 Idea: dynamic programming


• Let V={1, …, n} and for k≤n, Vk={1, …, k}
• For any pair of vertices i, jV, identify all paths from i to j
whose intermediate vertices are all drawn from Vk: Pijk={p1, p2,
…}, if Pijk then Rk[i, j]=1 Vk

i P1 j
p2

• For any pair of vertices i, j: Rn[i, j], that is Rn


• Starting with R0=A, the adjacency matrix, how to get R1  …
 Rk-1  Rk  …  Rn
Design and Analysis of Algorithms - Chapter 8 7
Warshall’s algorithm

 Idea: dynamic programming


• pPijk: p is a path from i to j with all intermediate vertices
in Vk
• If k is not on p, then p is also a path from i to j with all
intermediate vertices in Vk-1: pPijk-1

k Vk
Vk-1
i p j

Design and Analysis of Algorithms - Chapter 8 8


Warshall’s algorithm

 Idea: dynamic programming


• pPijk: p is a path from i to j with all intermediate vertices
in Vk
• If k is on p, then we break down p into p1 and p2
– What are P1 and P2?

p
k Vk
p1
Vk-1 p2
i j

Design and Analysis of Algorithms - Chapter 8 9


Warshall’s algorithm

 Idea: dynamic programming


• pPijk: p is a path from i to j with all intermediate vertices
in Vk
• If k is on p, then we break down p into p1 and p2 where
– p1 is a path from i to k with all intermediate vertices in Vk-1
p all intermediate vertices in Vk-1
– p2 is a path from k to j with
k Vk
p1
Vk-1 p2
i j

Design and Analysis of Algorithms - Chapter 8 10


Warshall’s algorithm

• In the kth stage determine if a path exists between two vertices


i, j using just vertices among 1, …, k

{
R(k)[i,j] =
R(k-1)[i,j]
or
(path using just 1, …, k-1)

(R(k-1)[i,k] and R(k-1)[k,j]) (path from i to k


and from k to j
k using just 1, …, k-1)
i
kth stage

j
Design and Analysis of Algorithms - Chapter 8 11
Warshall’s algorithm
3
3 1
1

2 4
2 4 3 R2
R1 1 0 0 1 0
R0
0 0 1 0 0 0 1 0 1 0 1 1
1 0 0 1 1 0 1 1 0 0 0 0
2 4 1 1 1 1
0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0

3 R4
3 R3 1
1 0 0 1 0 0 0 1 0
1 0 1 1 1 1 1 1
0 0 0 0 4 0 0 0 0
2 1 1 1 1
2 4 1 1 1 1

Design and Analysis of Algorithms - Chapter 8 12


Warshall’s algorithm

R0 = A R1 R2
0 0 1 0 0 0 1 0 0 0 1 0
1 0 0 1 1 0 1 1 1 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 1 1 1 1

R3 R4
0 0 1 0 0 0 1 0
1 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1

Design and Analysis of Algorithms - Chapter 8 13


In-class exercises
 8.4.1
Apply Warshall’s algorithm to find the transitive closure of
the digraph defined by the following adjacency matrix

0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0

Design and Analysis of Algorithms - Chapter 8 14


In-class exercises
 Problem 2
a. What is the time efficiency of Warshall’s algorithm?

Design and Analysis of Algorithms - Chapter 8 15


In-class exercises
 Problem 2
a. What is the time efficiency of Warshall’s algorithm?

b. How to solve this “finding all paths in a directed graph”


problem by a traversal-based algorithm (BFS-based or
DFS-based)?

Design and Analysis of Algorithms - Chapter 8 16


In-class exercises
 Problem 2
a. What is the time efficiency of Warshall’s algorithm?

b. How to solve this “finding all paths in a directed graph”


problem by a traversal-based algorithm (BFS-based or
DFS-based)?

c. Explain why the time efficiency of Warshall’s algorithm


is inferior to that of traversal-based algorithm for sparse
graphs represented by their adjacency lists.

Design and Analysis of Algorithms - Chapter 8 17


Floyd’s algorithm: all pairs shortest paths
• In a weighted graph, find shortest paths between every pair of
vertices

•Same idea: construct solution through series of matrices D(0),


D(1), … using an initial subset of the vertices as intermediaries.

•In D(k), dij(k): weight of the shortest path from ui to uj with all
intermediate vertices in an initial subset {u1, u2, … uk}
4 3
1
1
• Example: 6
1 5

2 4
3
Design and Analysis of Algorithms - Chapter 8 18
Floyd’s algorithm: all pairs shortest paths

 Idea: dynamic programming


• Let V={u1,…,un} and for k≤n, Vk={u1,…,uk}
• To construct D(k) , we need to get dijk
• For any pair of vertices ui, ujV, consider all paths from ui
to uj whose intermediate vertices are all drawn from Vk
and find p the shortest path among them, weight of p is dijk

Vk

ui p uj

Design and Analysis of Algorithms - Chapter 8 19


Floyd’s algorithm: all pairs shortest paths

 Idea: dynamic programming


• Let V={u1,…,un} and for k≤n, Vk={u1,…,uk}
• To construct D(k) , we need to get dijk
• For any pair of vertices ui, ujV, consider all paths from ui
to uj whose intermediate vertices are all drawn from Vk
and find p the shortest path among them, weight of p is dijk
• If uk is not on p, then what is dijk equal to?
Vk

ui p uj

Design and Analysis of Algorithms - Chapter 8 20


Floyd’s algorithm: all pairs shortest paths

 Idea: dynamic programming


• If uk is not on p, then a shortest path from ui to uj with all
intermediate vertices in Vk-1 is also a shortest path in Vk ,
i.e., dij(k) = dij(k-1).

• If uk is on p, then we break down p into p1 and p2


– What are p1, p2?

Design and Analysis of Algorithms - Chapter 8 21


Floyd’s algorithm: all pairs shortest paths

 Idea: dynamic programming


• If uk is not in p, then a shortest path from ui to uj with all
intermediate vertices in Vk-1 is also a shortest path in Vk ,
i.e., dij(k) = dij(k-1).

• If uk is in p, then we break down p into p1 and p2 where


– p1 is the shortest path from ui to uk with all intermediate
vertices in Vk-1
– p2 is the shortest path from uk to uj with all intermediate
vertices in Vk-1
– i.e., dij(k) = dik(k-1)+ dkj(k-1).
Design and Analysis of Algorithms - Chapter 8 22
Floyd’s algorithm: all pairs shortest paths
 Idea: dynamic programming
• Construct matrices D(0), D(1), … D(k-1), D(k) … D(n)

• dij(k): weight of the shortest path from ui to uj with all


intermediate vertices in Vk

• dij(0)=wij

• dij(k)=min (dij(k-1), dik(k-1)+ dkj(k-1)) for k≥1

Design and Analysis of Algorithms - Chapter 8 23


Floyd’s algorithm: all pairs shortest paths

FLOYD(G)
for i,j in [1..n]
d[i,j]=w(ui,uj)
for k in [1..n]
for i in [1..n]
for j in [1..n]
d[i,j]=min(d[i,j],d[i,k]+d[k,j])

Time complexity: O(n3)

Design and Analysis of Algorithms - Chapter 8 24


In-Class Exercises
 Apply the Floyd’s algorithm to the following problem
instance:

 D(0) = 0 3 
2 0 
7 0 1
6 0

 How to enhance Floyd’s algorithm to actually output the


shortest path from node i to node j?

Design and Analysis of Algorithms - Chapter 8 25


Enhanced Floyd’s Algorithm

FloydEnhanced(w[1..n, 1..n])
ShortestPath(i, j, p[1..n, 1..n])
for i,j in [1..n]
k = p[i, j]
d[i,j]=w(ui,uj)
if k  0
for i in [1..n] ShortestPath(i, k)
for j in [1..n] print(k)
p[i,j]=0 ShortestPath(k, j)
for k in [1..n]
for i in [1..n]
for j in [1..n]
if d[i,j] >d[i,k]+d[k,j]
d[i,j]=d[i,k]+d[k,j]
p[i,j] = k

Design and Analysis of Algorithms - Chapter 8 26


In-Class Exercises
 Apply the enhanced Floyd’s algorithm, which finds the
shortest paths and their lengths, to the following problem
instance:
0 3 
 D(0) = 2 0 
7 0 1
6 0

Design and Analysis of Algorithms - Chapter 8 27


Dynamic programming
 Dynamic programming is a technique for solving problems
with overlapping subproblems. It suggests solving each
smaller subproblem once and recording the results in a
table from which a solution to the original problem can be
then obtained.

Design and Analysis of Algorithms - Chapter 8 28


Dynamic programming
 Dynamic programming is a technique for solving problems
with overlapping subproblems. It suggests solving each
smaller subproblem once and recording the results in a
table from which a solution to the original problem can be
then obtained.
 What are the overlapping subproblems in Floyd’s
algorithm?
FLOYD(G)
for i,j in [1..n]
d[i,j]=w(ui,uj)
for k in [1..n]
for i in [1..n]
for j in [1..n]
d[i,j]=min(d[i,j],d[i,k]+d[k,j])
Design and Analysis of Algorithms - Chapter 8 29
Floyd’s Algorithm

dij(k)=min (dij(k-1), dik(k-1)+ dkj(k-1)) for k≥1

p
k Vk
p1
Vk-1 p2
i p j

solutions for smaller subproblems  solution for a larger


subproblem
Floyd’s Algorithm

dij(k)=min (dij(k-1), dik(k-1)+ dkj(k-1)) for k≥1


dil(k)=min (dil(k-1), dik(k-1)+ dkl(k-1)) for k≥1
p
k Vk
p1
Vk-1 p2
i j
p3

l
solution for a smaller subproblem is used for getting solutions for
multiple bigger subproblems
In-class exercise
 What does dynamic programming have in common with
divide-and-conquer? What is a principal difference
between them?

Design and Analysis of Algorithms - Chapter 8 32

You might also like