warshall's and floyd's algorithms

32
Design and Analysis of Algorithms - Chapter 8 1 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu [email protected] RAIK 283: Data Structures & RAIK 283: Data Structures & Algorithms Algorithms

Upload: phungxuyen

Post on 01-Jan-2017

238 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 1

Dynamic ProgrammingWarshall’s and Floyd’sAlgorithm

Dr. Ying [email protected]

RAIK 283: Data Structures & RAIK 283: Data Structures & AlgorithmsAlgorithms

Page 2: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 2

Giving credit where credit is due:Giving credit where credit is due:• Most of the lecture notes are based on the slides from Most of the lecture notes are based on the slides from

the Textbook’s companion websitethe Textbook’s companion website– http://www.aw-bc.com/info/levitinhttp://www.aw-bc.com/info/levitin

• Some of the notes are based on slides created by Dr. Some of the notes are based on slides created by Dr. Philippe Laborie, ILOG and Mark Llewellyn, Philippe Laborie, ILOG and Mark Llewellyn, University of Central Florida. University of Central Florida.

• I have modified many of their slides and added new I have modified many of their slides and added new slides.slides.

RAIK 283: Data Structures & RAIK 283: Data Structures & AlgorithmsAlgorithms

Page 3: Warshall's and Floyd's Algorithms

3

Examples of dynamic Examples of dynamic programming algorithmsprogramming algorithms

• Coin-row problemCoin-row problem

• Computing binomial coefficients Computing binomial coefficients

• Longest Common Subsequence (LCS) Longest Common Subsequence (LCS)

•Warshall’s algorithm for transitive closureWarshall’s algorithm for transitive closure

• Floyd’s algorithm for all-pairs shortest pathsFloyd’s algorithm for all-pairs shortest paths

•Some instances of difficult discrete optimization problems:Some instances of difficult discrete optimization problems:•0-1 knapsack0-1 knapsack

Page 4: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 4

Warshall’s algorithm: Warshall’s algorithm: transitive closuretransitive closure

• Computes the transitive closure of a relationComputes the transitive closure of a relation• (Alternatively: all paths in a directed graph)(Alternatively: all paths in a directed graph)

• Example of transitive closure:Example of transitive closure:

3

42

1

0 0 1 01 0 0 10 0 0 00 1 0 0

0 0 1 01 1 11 1 10 0 0 011 1 1 11 1

3

42

1

Page 5: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 5

Warshall’s algorithmWarshall’s algorithm• Main idea: a path exists between two vertices i, j, iffMain idea: a path exists between two vertices i, j, iff

•there is an edge from i to j; orthere is an edge from i to j; or

•there is a path from i to j going through intermediate there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1}; orvertices which are drawn from set {vertex 1}; or

•there is a path from i to j going through intermediate there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2}; orvertices which are drawn from set {vertex 1, 2}; or

•……

Page 6: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 6

Warshall’s algorithmWarshall’s algorithm• Main idea: a path exists between two vertices i, j, iffMain idea: a path exists between two vertices i, j, iff

•there is a path from i to j going through intermediate there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2, … k-1}; orvertices which are drawn from set {vertex 1, 2, … k-1}; or

•there is a path from i to j going through intermediate there is a path from i to j going through intermediate vertices which are drawn from set {vertex 1, 2, … k}; orvertices which are drawn from set {vertex 1, 2, … k}; or

•......

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

Page 7: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 7

Idea: dynamic programmingIdea: dynamic programming• Let V={1, …, n} and for k≤n, VLet V={1, …, n} and for k≤n, Vkk={1, …, k}={1, …, k}• For any pair of vertices i, jFor any pair of vertices i, jV, identify all paths from i to j V, identify all paths from i to j

whose intermediate vertices are all drawn from Vwhose intermediate vertices are all drawn from Vkk: P: Pijijkk={p1, p2, ={p1, p2,

…}, if P…}, if Pijijkk then R then Rkk[i, j]=1[i, j]=1

• For any pair of vertices i, j: RFor any pair of vertices i, j: Rnn[i, j], that is R[i, j], that is Rnn

• Starting with RStarting with R00=A, the adjacency matrix, how to get R=A, the adjacency matrix, how to get R1 1 … … RRk-1k-1 R Rk k … … R Rnn

i jP1

Vk

Warshall’s algorithmWarshall’s algorithm

p2

Page 8: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 8

Idea: dynamic programmingIdea: dynamic programming• ppPPijij

kk: p is a path from i to j with all intermediate vertices : p is a path from i to j with all intermediate vertices in Vin Vkk

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

k-1 k-1

Warshall’s algorithmWarshall’s algorithm

i jpVk-1

Vkk

Page 9: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 9

Idea: dynamic programmingIdea: dynamic programming• ppPPijij

kk: p is a path from i to j with all intermediate vertices : p is a path from i to j with all intermediate vertices in Vin Vkk

• If k is on p, then we break down p into pIf k is on p, then we break down p into p11 and p and p22 – What are PWhat are P11 and P and P22??

Warshall’s algorithmWarshall’s algorithm

i jVk-1

pVk

kp1 p2

Page 10: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 10

Idea: dynamic programmingIdea: dynamic programming• ppPPijij

kk: p is a path from i to j with all intermediate vertices : p is a path from i to j with all intermediate vertices in Vin Vkk

• If k is on p, then we break down p into pIf k is on p, then we break down p into p11 and p and p22 where where– pp11 is a path from i to k with all intermediate vertices in V is a path from i to k with all intermediate vertices in Vk-1k-1

– pp22 is a path from k to j with all intermediate vertices in V is a path from k to j with all intermediate vertices in Vk-1k-1

Warshall’s algorithmWarshall’s algorithm

i jVk-1

pVk

kp1 p2

Page 11: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 11

Warshall’s algorithmWarshall’s algorithm

• In theIn the k kthth stage determine if a path exists between two vertices stage determine if a path exists between two vertices i, j i, j using just vertices among 1, …, using just vertices among 1, …, k k

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

((RR(k-1)(k-1)[[i,ki,k] and ] and RR(k-1)(k-1)[[k,jk,j]) (path from ]) (path from i i to to kk and from and from kk to to jj using just 1, …, using just 1, …, k-k-1)1)

i

j

k

kth stage

{

Page 12: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 12

Warshall’s algorithmWarshall’s algorithm

3

42

1

3

42

13

42

1

R0

0 0 1 01 0 0 10 0 0 00 1 0 0

R1

0 0 1 01 0 11 10 0 0 00 1 0 0

R2

0 0 1 01 0 1 10 0 0 01 1 1 1 11 1

R3

0 0 1 01 0 1 10 0 0 01 1 1 1

R4

0 0 1 01 11 1 10 0 0 01 1 1 1

3

42

1

3

42

1

Page 13: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 13

Warshall’s algorithmWarshall’s algorithm

R0 = A0 0 1 01 0 0 10 0 0 00 1 0 0

R1

0 0 1 01 0 11 10 0 0 00 1 0 0

R2

0 0 1 01 0 1 10 0 0 01 1 1 1 11 1

R3

0 0 1 01 0 1 10 0 0 01 1 1 1

R4

0 0 1 01 11 1 10 0 0 01 1 1 1

Page 14: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 14

In-class exercisesIn-class exercises 8.4.18.4.1

Apply Warshall’s algorithm to find the transitive closure of Apply Warshall’s algorithm to find the transitive closure of the digraph defined by the following adjacency matrixthe digraph defined by the following adjacency matrix

0 1 0 00 0 1 00 0 0 10 0 0 0

Page 15: Warshall's and Floyd's Algorithms

In-class exercisesIn-class exercises Problem 2 Problem 2

a. What is the time efficiency of Warshalla. What is the time efficiency of Warshall’’s algorithm?s algorithm?

Design and Analysis of Algorithms - Chapter 8 15

Page 16: Warshall's and Floyd's Algorithms

In-class exercisesIn-class exercises Problem 2 Problem 2

a. What is the time efficiency of Warshalla. What is the time efficiency of Warshall’’s algorithm?s algorithm?

b. How to solve this b. How to solve this ““finding finding all paths in a directed graph” all paths in a directed graph” problem by a traversal-based algorithm (BFS-based or problem by a traversal-based algorithm (BFS-based or DFS-based)? DFS-based)?

Design and Analysis of Algorithms - Chapter 8 16

Page 17: Warshall's and Floyd's Algorithms

In-class exercisesIn-class exercises Problem 2 Problem 2

a. What is the time efficiency of Warshalla. What is the time efficiency of Warshall’’s algorithm?s algorithm?

b. How to solve this b. How to solve this ““finding finding all paths in a directed graph” all paths in a directed graph” problem by a traversal-based algorithm (BFS-based or problem by a traversal-based algorithm (BFS-based or DFS-based)? DFS-based)?

c. c. Explain why the time efficiency of WarshallExplain why the time efficiency of Warshall’’s algorithm s algorithm is inferior to that of traversal-based algorithm for sparse is inferior to that of traversal-based algorithm for sparse graphs represented by their adjacency lists. graphs represented by their adjacency lists.

Design and Analysis of Algorithms - Chapter 8 17

Page 18: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 18

Floyd’s algorithm: all pairs Floyd’s algorithm: all pairs shortest pathsshortest paths

• In a weighted graph, find shortest paths between every pair of In a weighted graph, find shortest paths between every pair of verticesvertices

•Same idea: construct solution through series of matrices DSame idea: construct solution through series of matrices D(0)(0), , DD(1)(1), … using an initial subset of the vertices as intermediaries. , … using an initial subset of the vertices as intermediaries.

•In DIn D(k)(k), d, dijij(k)(k): weight of the shortest path from u: weight of the shortest path from uii to u to ujj with all with all

intermediate vertices in an initial subset {uintermediate vertices in an initial subset {u11, u, u22, … u, … ukk}}

• Example:Example:

3

42

14

16

1

5

3

Page 19: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 19

Idea: dynamic programmingIdea: dynamic programming• Let V={uLet V={u11,…,u,…,unn} and for k≤n, V} and for k≤n, Vkk={u={u11,…,u,…,ukk}}• To construct DTo construct D(k)(k)

, we need to get d, we need to get dijijkk

• For any pair of vertices uFor any pair of vertices uii, u, ujjV, consider all paths from uV, consider all paths from uii to uto ujj whose intermediate vertices are all drawn from V whose intermediate vertices are all drawn from Vkk and find p the shortest path among them, weight of p is dand find p the shortest path among them, weight of p is dijij

kk

ui ujp

Vk

Floyd’s algorithm: all pairs Floyd’s algorithm: all pairs shortest pathsshortest paths

Page 20: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 20

Idea: dynamic programmingIdea: dynamic programming• Let V={uLet V={u11,…,u,…,unn} and for k≤n, V} and for k≤n, Vkk={u={u11,…,u,…,ukk}}• To construct DTo construct D(k)(k)

, we need to get d, we need to get dijijkk

• For any pair of vertices uFor any pair of vertices uii, u, ujjV, consider all paths from uV, consider all paths from uii to uto ujj whose intermediate vertices are all drawn from V whose intermediate vertices are all drawn from Vkk and find p the shortest path among them, weight of p is dand find p the shortest path among them, weight of p is dijij

kk

• If uIf ukk is not on p, then what is d is not on p, then what is dijijk k equal to? equal to?

ui ujp

Vk

Floyd’s algorithm: all pairs Floyd’s algorithm: all pairs shortest pathsshortest paths

Page 21: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 21

Idea: dynamic programmingIdea: dynamic programming• If uIf ukk is not on p, then a shortest path from u is not on p, then a shortest path from uii to u to ujj with all with all

intermediate vertices in Vintermediate vertices in Vk-1k-1 is also a shortest path in V is also a shortest path in Vk k , , i.e., di.e., dijij

(k)(k) = d = dijij(k-1)(k-1). .

• If uIf ukk is on p, then we break down p into p is on p, then we break down p into p11 and p and p22

– What are p1, p2?What are p1, p2?

Floyd’s algorithm: all pairs Floyd’s algorithm: all pairs shortest pathsshortest paths

Page 22: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 22

Idea: dynamic programmingIdea: dynamic programming• If uIf ukk is not in p, then a shortest path from u is not in p, then a shortest path from uii to u to ujj with all with all

intermediate vertices in Vintermediate vertices in Vk-1k-1 is also a shortest path in V is also a shortest path in Vk k , , i.e., di.e., dijij

(k)(k) = d = dijij(k-1)(k-1). .

• If uIf ukk is in p, then we break down p into p is in p, then we break down p into p11 and p and p22 where where– pp11 is the shortest path from u is the shortest path from uii to u to ukk with all intermediate with all intermediate

vertices in Vvertices in Vk-1k-1

– pp22 is the shortest path from u is the shortest path from ukk to u to ujj with all intermediate with all intermediate vertices in Vvertices in Vk-1k-1

– i.e., di.e., dijij(k)(k) = d = dikik

(k-1)(k-1)+ d+ dkjkj(k-1)(k-1)..

Floyd’s algorithm: all pairs Floyd’s algorithm: all pairs shortest pathsshortest paths

Page 23: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 23

Idea: dynamic programmingIdea: dynamic programming• Construct matrices DConstruct matrices D(0)(0), D, D(1)(1), … D, … D(k-1)(k-1), D, D(k)(k) … D … D(n)(n)

• ddijij(k)(k): weight of the shortest path from u: weight of the shortest path from uii to u to ujj with all with all

intermediate vertices in Vintermediate vertices in Vkk

• ddijij(0)(0)=w=wijij

• ddijij(k)(k)=min (d=min (dijij

(k-1)(k-1), d, dikik(k-1)(k-1)+ d+ dkjkj

(k-1)(k-1)) for k) for k≥≥11

Floyd’s algorithm: all pairs Floyd’s algorithm: all pairs shortest pathsshortest paths

Page 24: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 24

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)

Floyd’s algorithm: all pairs Floyd’s algorithm: all pairs shortest pathsshortest paths

Page 25: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 25

In-Class ExercisesIn-Class Exercises Apply the Floyd’s algorithm to the following problem Apply the Floyd’s algorithm to the following problem

instance:instance:

DD(0) (0) = = 0 3 2 0 7 0 16 0

How to enhance FloydHow to enhance Floyd’’s algorithm to actually output the s algorithm to actually output the shortest path from node i to node j?shortest path from node i to node j?

Page 26: Warshall's and Floyd's Algorithms

Enhanced FloydEnhanced Floyd’’s Algorithm s Algorithm

Design and Analysis of Algorithms - Chapter 8 26

FloydEnhanced(w[1..n, 1..n]) for i,j in [1..n] d[i,j]=w(ui,uj) for i in [1..n] for j in [1..n] p[i,j]=0 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

ShortestPath(i, j, p[1..n, 1..n]) k = p[i, j] if k 0 ShortestPath(i, k) print(k) ShortestPath(k, j)

Page 27: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 27

In-Class ExercisesIn-Class Exercises Apply the enhanced Floyd’s algorithm, which finds the Apply the enhanced Floyd’s algorithm, which finds the

shortest paths and their lengths, to the following problem shortest paths and their lengths, to the following problem instance:instance:

DD(0) (0) = = 0 3 2 0 7 0 16 0

Page 28: Warshall's and Floyd's Algorithms

Dynamic programming Dynamic programming Dynamic programming is a technique for solving problems Dynamic programming is a technique for solving problems

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

Design and Analysis of Algorithms - Chapter 8 28

Page 29: Warshall's and Floyd's Algorithms

Dynamic programming Dynamic programming Dynamic programming is a technique for solving problems Dynamic programming is a technique for solving problems

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

What are the overlapping subproblems in FloydWhat are the overlapping subproblems in Floyd’’s s algorithm?algorithm?

Design and Analysis of Algorithms - Chapter 8 29

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

Page 30: Warshall's and Floyd's Algorithms

Floyd’s AlgorithmFloyd’s Algorithm

i jVk-1

pVk

kp1 p2p

dij(k)=min (dij

(k-1), dik(k-1)+ dkj

(k-1)) for k≥1

solutions for smaller subproblems solution for a larger subproblem

Page 31: Warshall's and Floyd's Algorithms

Floyd’s AlgorithmFloyd’s Algorithm

i jVk-1

pVk

kp1 p2

dij(k)=min (dij

(k-1), dik(k-1)+ dkj

(k-1)) for k≥1dil

(k)=min (dil(k-1), dik

(k-1)+ dkl(k-1)) for k≥1

solution for a smaller subproblem is used for getting solutions for multiple bigger subproblems

l

p3

Page 32: Warshall's and Floyd's Algorithms

Design and Analysis of Algorithms - Chapter 8 32

In-class exerciseIn-class exercise What does dynamic programming have in common with What does dynamic programming have in common with

divide-and-conquer? What is a principal difference divide-and-conquer? What is a principal difference between them?between them?