CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms Professor Henry Carter Fall 2016
Recap Space-time tradeoffs allow for faster algorithms at the cost of space complexity overhead Dynamic programming achieves this by saving the result of overlapping subproblems Can be executed bottom-up or top-down (using memory functions) 2
Digraphs Revisited Recall: directed graphs New feature: edge weights Applications: scheduling, process flow, revision history 3
Transitive Closure Is node b reachable from node a? Consider all pairs (a,b) Store results in an n x n matrix of {0,1} 4
Example Graph A B a 0 1 0 0 a 1 1 1 1 b 0 0 0 1 b 1 1 1 1 C D c 0 0 0 0 d 1 0 1 0 c 0 0 0 0 d 1 1 1 1 5
BFS/DFS Approach The transitive closure from a to any other node can be found through graph traversal Repeating for all nodes yields the complete closure matrix How could we apply dynamic programming? 6
Warshall s Algorithm Construct transitive closure using a series of matrices Matrix k considers paths through G traversing nodes 1,,k If a path exists from i to k and from k to j, mark a path from i to j R 0 is the paths between each vertex with no intermediate vertices (i.e., the adjacency matrix) 7
Rule for Changing 0 to 1 k j k 1 i 1 0 8
Example Application a 0 1 0 0 b 0 0 0 1 c 0 0 0 0 d 1 0 1 0 9
Example Application a 0 1 0 0 b 0 0 0 1 c 0 0 0 0 d 1 0 1 0 10
Example Application a 0 1 0 0 b 0 0 0 1 c 0 0 0 0 d 1 1 1 0 11
Example Application a 0 1 0 1 b 0 0 0 1 c 0 0 0 0 d 1 1 1 1 12
Example Application a 0 1 0 1 b 0 0 0 1 c 0 0 0 0 d 1 1 1 1 13
Example Application a 1 1 1 1 b 1 1 1 1 c 0 0 0 0 d 1 1 1 1 14
Algorithm Warshall(A[1,...,n,1,...,n]) input : The adjacency matrix A with n vertices. output: The transitive closure of the digraph. R (0) A for k 1 to n do for i 1 to n do for j 1 to n do R (k) [i, j] R (k 1) [i, j] or (R (k 1) [i, k] and R (k 1) [k, j]) end end end return R (n) 15
Speeding Things Up More efficient inner loop Treat rows as bit strings and apply boolean operations simultaneously Combine matrices into one 16
All-Pairs Shortest Paths Given a weighted digraph, find the shortest path from a to b Solve for all pairs (a,b) Stored in an n x n integer distance matrix 17
Example Graph A 2 B a 0 3 a 0 10 3 4 3 C 7 1 6 D b 2 0 c 7 0 1 d 6 0 b 2 0 5 6 c 7 7 0 1 d 6 16 9 0 18
Floyd s Algorithm Construct the distance matrix using a series of matrices Matrix k considers paths through G that traverse any vertex numbered 1,,k If there is a path from i to k and from k to j, we compare it to the current shortest path and (possibly) update the distance R0 is the distance between each node with no intermediate vertices (i.e., the edge weight matrix) 19
Rule for Updating Distance k j k 3 i 2 20
Example Application a 0 3 b 2 0 c 7 0 1 d 6 0 21
Example Application a 0 3 b 2 0 c 7 0 1 d 6 0 22
Example Application a 0 3 b 2 0 5 c 7 0 1 d 6 9 0 23
Example Application a 0 3 b 2 0 5 c 9 7 0 1 d 6 9 0 24
Example Application a 0 10 3 4 b 2 0 5 6 c 9 7 0 1 d 6 16 9 0 25
Example Application a 0 10 3 4 b 2 0 5 6 c 7 7 0 1 d 6 16 9 0 26
Algorithm Floyd(W [1,...,n,1,...,n]) input : The weight matrix W. output: The distance matrix of the shortest paths lengths. D W for k 1 to n do for i 1 to n do for j 1 to n do D[i, j] min(d[i, j],d[i, k]+d[k, j]) end end end return D 27
Practice Apply Warshall s algorithm to the following adjacency matrix: 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 28
Recap Transitive closure and all-pairs shortest paths problems Warshall s and Floyd s algorithms use dynamic programming to store intermediate results in a series of matrices We will revisit shortest-paths in the next chapter 29
Next Time... Levitin Chapter 9.1-9.2 Remember, you need to read it BEFORE you come to class! Homework: 8.4: 1, 2, 3, 6, 7 30