Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures & Algorithms
Design and Analysis of Algorithms - Chapter 82 b Giving credit where credit is due: Most of the lecture notes are based on the slides from the Textbook’s companion websiteMost of the lecture notes are based on the slides from the Textbook’s companion website – Some of the notes are based on slides created by Dr. Philippe Laborie, ILOG and Mark Llewellyn, University of Central Florida.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.I have modified many of their slides and added new slides. RAIK 283: Data Structures & Algorithms
3 Examples of dynamic programming algorithms Coin-row problem Coin-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 paths Floyd’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
Design and Analysis of Algorithms - Chapter 84 Warshall’s algorithm: transitive closure Computes the transitive closure of a relation Computes 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:
Design and Analysis of Algorithms - Chapter 85 Warshall’s algorithm Main idea: a path exists between two vertices i, j, iff Main 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 vertices which are drawn from set {vertex 1}; orthere 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}; orthere 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 86 Warshall’s algorithm Main idea: a path exists between two vertices i, j, iff 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}; orthere 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}; orthere 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 verticesthere is a path from i to j going through any of the other vertices
Design and Analysis of Algorithms - Chapter 87 b Idea: dynamic programming Let V={1, …, n} and for k≤n, V k ={1, …, k}Let V={1, …, n} and for k≤n, V k ={1, …, k} For any pair of vertices i, j V, identify all paths from i to j whose intermediate vertices are all drawn from V k : P ij k ={p1, p2, …}, if P ij k then R k [i, j]=1For any pair of vertices i, j V, identify all paths from i to j whose intermediate vertices are all drawn from V k : P ij k ={p1, p2, …}, if P ij k then R k [i, j]=1 For any pair of vertices i, j: R n [i, j], that is R nFor any pair of vertices i, j: R n [i, j], that is R n Starting with R 0 =A, the adjacency matrix, how to get R 1 … R k-1 R k … R nStarting with R 0 =A, the adjacency matrix, how to get R 1 … R k-1 R k … R n ij P1 VkVk Warshall’s algorithm p2
Design and Analysis of Algorithms - Chapter 88 b Idea: dynamic programming p P ij k : p is a path from i to j with all intermediate vertices in V kp P ij k : p is a path from i to j with all intermediate vertices in V k If k is not on p, then p is also a path from i to j with all intermediate vertices in V k-1 : p P ij k-1If k is not on p, then p is also a path from i to j with all intermediate vertices in V k-1 : p P ij k-1 Warshall’s algorithm ij p V k-1 VkVk k
Design and Analysis of Algorithms - Chapter 89 b Idea: dynamic programming p P ij k : p is a path from i to j with all intermediate vertices in V kp P ij k : p is a path from i to j with all intermediate vertices in V k If k is on p, then we break down p into p 1 and p 2If k is on p, then we break down p into p 1 and p 2 –What are P 1 and P 2 ? Warshall’s algorithm ij V k-1 p VkVk k p1 p2
Design and Analysis of Algorithms - Chapter 810 b Idea: dynamic programming p P ij k : p is a path from i to j with all intermediate vertices in V kp P ij k : p is a path from i to j with all intermediate vertices in V k If k is on p, then we break down p into p 1 and p 2 whereIf k is on p, then we break down p into p 1 and p 2 where –p 1 is a path from i to k with all intermediate vertices in V k-1 –p 2 is a path from k to j with all intermediate vertices in V k-1 Warshall’s algorithm ij V k-1 p VkVk k p1 p2
Design and Analysis of Algorithms - Chapter 811 Warshall’s algorithm In the k th stage determine if a path exists between two vertices i, j using just vertices among 1, …, k In the k th stage determine if a path exists between two vertices i, j using just vertices among 1, …, k R (k-1) [i,j] (path using just 1, …, k-1) R (k) [i,j] = or R (k) [i,j] = or (R (k-1) [i,k] and R (k-1) [k,j]) (path from i to k (R (k-1) [i,k] and R (k-1) [k,j]) (path from i to k and from k to j and from k to j using just 1, …, k-1) using just 1, …, k-1) i j k k th stage {
Design and Analysis of Algorithms - Chapter 812 Warshall’s algorithm R R R R R
Design and Analysis of Algorithms - Chapter 813 Warshall’s algorithm R 0 = A R R R R
Design and Analysis of Algorithms - Chapter 814 In-class exercises b Apply Warshall’s algorithm to find the transitive closure of the digraph defined by the following adjacency matrix
In-class exercises b Problem 2 a. What is the time efficiency of Warshall’s algorithm? Design and Analysis of Algorithms - Chapter 815
In-class exercises b 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 816
In-class exercises b 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 817
Design and Analysis of Algorithms - Chapter 818 Floyd’s algorithm: all pairs shortest paths In a weighted graph, find shortest paths between every pair of vertices 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.Same idea: construct solution through series of matrices D (0), D (1), … using an initial subset of the vertices as intermediaries. In D (k), d ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in an initial subset {u 1, u 2, … u k }In D (k), d ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in an initial subset {u 1, u 2, … u k } Example: Example:
Design and Analysis of Algorithms - Chapter 819 b Idea: dynamic programming Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k }Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k } To construct D (k), we need to get d ij kTo construct D (k), we need to get d ij k For any pair of vertices u i, u j V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij kFor any pair of vertices u i, u j V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij k uiui ujuj p VkVk Floyd’s algorithm: all pairs shortest paths
Design and Analysis of Algorithms - Chapter 820 b Idea: dynamic programming Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k }Let V={u 1,…,u n } and for k≤n, V k ={u 1,…,u k } To construct D (k), we need to get d ij kTo construct D (k), we need to get d ij k For any pair of vertices u i, u j V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij kFor any pair of vertices u i, u j V, consider all paths from u i to u j whose intermediate vertices are all drawn from V k and find p the shortest path among them, weight of p is d ij k If u k is not on p, then what is d ij k equal to?If u k is not on p, then what is d ij k equal to? uiui ujuj p VkVk Floyd’s algorithm: all pairs shortest paths
Design and Analysis of Algorithms - Chapter 821 b Idea: dynamic programming If u k is not on p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1).If u k is not on p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1). If u k is on p, then we break down p into p 1 and p 2If u k is on p, then we break down p into p 1 and p 2 –What are p1, p2? Floyd’s algorithm: all pairs shortest paths
Design and Analysis of Algorithms - Chapter 822 b Idea: dynamic programming If u k is not in p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1).If u k is not in p, then a shortest path from u i to u j with all intermediate vertices in V k-1 is also a shortest path in V k, i.e., d ij (k) = d ij (k-1). If u k is in p, then we break down p into p 1 and p 2 whereIf u k is in p, then we break down p into p 1 and p 2 where –p 1 is the shortest path from u i to u k with all intermediate vertices in V k-1 –p 2 is the shortest path from u k to u j with all intermediate vertices in V k-1 –i.e., d ij (k) = d ik (k-1) + d kj (k-1). Floyd’s algorithm: all pairs shortest paths
Design and Analysis of Algorithms - Chapter 823 b Idea: dynamic programming Construct matrices D (0), D (1), … D (k-1), D (k) … D (n)Construct matrices D (0), D (1), … D (k-1), D (k) … D (n) d ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in V kd ij (k) : weight of the shortest path from u i to u j with all intermediate vertices in V k d ij (0) =w ijd ij (0) =w ij d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1 Floyd’s algorithm: all pairs shortest paths
Design and Analysis of Algorithms - Chapter 824 FLOYD(G) for i,j in [1..n] d[i,j]=w(u i,u j ) 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(n 3 ) Floyd’s algorithm: all pairs shortest paths
Design and Analysis of Algorithms - Chapter 825 In-Class Exercises u Apply the Floyd’s algorithm to the following problem instance: u D (0) = 0 3 2 0 0 b How to enhance Floyd’s algorithm to actually output the shortest path from node i to node j?
Enhanced Floyd’s Algorithm Design and Analysis of Algorithms - Chapter 826 FloydEnhanced(w[1..n, 1..n]) for i,j in [1..n] d[i,j]=w(u i,u j ) 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)
Design and Analysis of Algorithms - Chapter 827 In-Class Exercises u Apply the enhanced Floyd’s algorithm, which finds the shortest paths and their lengths, to the following problem instance: u D (0) = 0 3 2 0 0
Dynamic programming b 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 828
Dynamic programming b 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. b What are the overlapping subproblems in Floyd’s algorithm? Design and Analysis of Algorithms - Chapter 829 FLOYD(G) for i,j in [1..n] d[i,j]=w(u i,u j ) 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])
Floyd’s Algorithm ij V k-1 p VkVk k p1 p2 p d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1 solutions for smaller subproblems solution for a larger subproblem
Floyd’s Algorithm ij V k-1 p VkVk k p1 p2 d ij (k) =min (d ij (k-1), d ik (k-1) + d kj (k-1) ) for k≥1 d il (k) =min (d il (k-1), d ik (k-1) + d kl (k-1) ) for k≥1 solution for a smaller subproblem is used for getting solutions for multiple bigger subproblems l p3
Design and Analysis of Algorithms - Chapter 832 In-class exercise b What does dynamic programming have in common with divide-and-conquer? What is a principal difference between them?