Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Reachability as Transitive Closure
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Discussion #35 Chapter 7, Section 5.5 1/15 Discussion #35 Dijkstra’s Algorithm.
Discussion #36 Spanning Trees
1 Graphs: shortest paths & Minimum Spanning Tree(MST) Fundamental Data Structures and Algorithms Ananda Guna April 8, 2003.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Algorithms All pairs shortest path
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 10.
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
Graph Definitions and Applications. Graphs: Very Useful Abstractions Graphs apply to many diverse areas: social sciences, linguistics, physical sciences,
Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Lecture 16. Shortest Path Algorithms
Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths.
Introduction to Algorithms Jiafen Liu Sept
Nattee Niparnan. Dijkstra’s Algorithm Graph with Length.
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
Chapter 10 Graph Theory Eulerian Cycle and the property of graph theory 10.3 The important property of graph theory and its representation 10.4.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
All-Pairs Shortest Paths
Properties and Applications of Depth-First Search Trees and Forests
15.082J & 6.855J & ESD.78J September 30, 2010 The Label Correcting Algorithm.
EMIS 8374 Shortest Path Trees Updated 11 February 2008 Slide 1.
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Graphs Reference:
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Breadth-First Search (BFS)
Shortest Paths.
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
Spanning Trees.
Algorithm Analysis Fall 2017 CS 4306/03
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Parallel Graph Algorithms
Shortest Path Graph represents highway system Edges have weights
Graph Definitions and Applications
Lecture 7 All-Pairs Shortest Paths
Shortest Paths.
Basic Graph Algorithms
CS 583 Analysis of Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
All pairs shortest path problem
Algorithms (2IL15) – Lecture 7
Algorithms: Design and Analysis
Shortest Paths.
Parallel Graph Algorithms
Directed Graphs (Part II)
Presentation transcript:

Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms

Discussion #34 2/17 Topics Faster reachability algorithms Warshall’s algorithm Weighted graphs Floyd’s algorithm

Discussion #34 3/17 Warshall’s Algorithm A more efficient way to compute reachability. Algorithm: 2. for (k=1 to n) for (i=1 to n) for (j=1 to n) if (a ij = 0) a ij  a ik  a kj Clearly more efficient than our straightforward reachability algorithm O(n 3 ) < O(n 4 ); but does it work? 1. Initialize adjacency matrix A: (For an edge from i to j, a ij is set to 1; otherwise, 0.)

Discussion #34 4/17 Warshall’s Algorithm: Intuitive Idea We’ll see that after k outer loops, a ij = 1 iff there is a path from i to j directly or through one or more nodes in {1, 2, …, k} k=6: adds (2,5), (3,5), (1,5), (4,5), (2,2) k is called a pivot 1 k=1: a 41  a 13 adds (4,3) k=2: a 32  a 26 adds (3,6), a 62  a 26 adds (6,6) k=3: a 13  a 32 adds (1,2), a 43  a 32 adds (4,2), a 43  a 36 adds (4,6), a 13  a 36 adds (1,6) k=4 and k=5 add nothing (either no in or no out) for (k=1 to n) for (i=1 to n) for (j=1 to n) if (a ij = 0) a ij  a ik  a kj

Discussion #34 5/17 Warshall’s Algorithm Works Are you convinced? –If not, how can you become convinced? –Prove it  even a sketch helps What should you prove? –Terminates: clear (can map each loop to a descending sequence on a well-founded poset) –Inductive proof based on the loop invariant, which is the key observation about Warshall’s algorithm. W(k): After k outer loops, a ij = 1 iff there is a path from i to j directly or through {1, 2, …, k}. Proof: –Basis: k = 0. After 0 outer loops, a ij = 1 iff there is a path from i to j directly (by definition of an adjacency matrix). –Induction: Show W(k)  W(k+1).

Form of Proof for Warshall’s Algorithm W(k): After k outer loops, a ij = 1 iff there is a path from i to j directly or through {1, 2, …, k}. For the induction part, we must prove: W(k)  W(k+1). Let P be “a ij = 1”, Q be “there is a path from i to j directly or through {1, 2, …, k}”, and R be “there is a path from i to j directly or through {1, 2, …, k+1}”. Then what we have to prove is that after k outer loops: (P  Q)  (P  R)  (P  Q)  ((P  R)  (R  P))A  B  (A  B)  (B  A)  ((P  Q)  (P  R))  ((P  Q)  (R  P))A  B  C  (A  B)  (A  C)  ((P  Q)  P  R)  ((P  Q)  R  P)A  (B  C)  A  B  C Thus, we do two proofs. For the first, we can choose to start by assuming P. For the second, we can choose to start by assuming R. For both proofs, we can also assume the induction hypotheses (P  Q). If we wish, we can also, prove by contradiction, and for the first we choose to do so and thus also assume  R. Discussion #34 6/17

Discussion #34 7/17 Induction Proof: (P  Q)  P  R) Assume a ij = 1 and, by way of contradiction, assume that there does not exist a path from i to j directly or indirectly through {1, 2, …, k, k+1}. There are two cases to consider. –Case 1: a ij = 1 in the initialization. But then there is a path from i to j directly (as established in the basis)  a contradiction. –Case 2: a ij = 0 in the initialization. But then in some iteration, say the q th, 1  q  k+1, a ij becomes 1. Hence, in the q th iteration a iq = 1 and a qj = 1, and thus there is a path from i to j through {1, 2, …, k, k+1}  a contradiction.

Discussion #34 8/17 Induction Proof: (P  Q)  R  P) Assume there is a path from i to j directly or through {1, 2, …, k, k+1}. (We need to show that a ij = 1.) There are two cases to consider. –Case 1: k+1 is not on the path from i to j. Then the path from i to j is direct or through {1, 2, …, k}. Thus by the induction hypothesis, a ij = 1. –Case 2: k+1 is on the path from i to j. But, now there is a path from i to k+1 directly or through {1, 2, …, k} and thus by the induction hypothesis, a i(k+1) = 1. Similarly, there is a path from k+1 to j directly or through {1, 2, …, k} and thus by the induction hypothesis a (k+1)j = 1. Thus, in the k+1 st loop a ij = 1 since a ij = a i(k+1)  a (k+1)j.

Discussion #34 9/17 Shortest Distance in a Weighted Graph Minimum Dist[1,4] = 6 Minimum Dist[3,2] = 

Discussion #34 10/17 Floyd’s Algorithm Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j]  8  2  02  6 3   40  5  0 Initial Dist[i,j] = O(n 3 ) The initial matrix has the edge weights or infinity. Variation of Warshall’s Algorithm: Pivot on node k

Discussion #34 11/17 Floyd’s Algorithm (continued…) Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j]  8  2  02  6 3   40  5  0 k=1: nothing k=2: 1  3:3, 1  5:7 Pivot k:  02  6 3   40  5  0 k=1k=2

Discussion #34 12/  02  6 3   40  5  0 k=2 Floyd’s Algorithm (continued…) Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] k=1: nothing k=2: 1  3:3, 1  5:7 k=3: 1  4:1  3+3  4:6 1  5:4 2  4:5 2  5:3 4  5:5 4  4:0 (not changed) Pivot k:     0 k=3

Discussion #34 13/     0 k=3 Floyd’s Algorithm (continued…) Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] k=5: nothing k=1: nothing k=2: 1  3:3, 1  5:7 k=3: 1  4:1  3+3  4:6 1  5:4 2  4:5 2  5:3 4  5:5 4  4:0 (not changed) k=4: no changes Pivot k:     0 k=4

Discussion #34 14/17 Proof that Floyd’s Algorithm Works Termination: Clear (can map each loop to a descending sequence on a well-founded poset) Loop Invariant –What does the algorithm do? –After k outer loops, each Dist[i,j] is as short or shorter than any other path from i to j whose intermediate nodes are in {1, …, k}. Proof (why does it work?): –Basis: k=0, no intermediate nodes; initialized to the shortest path with no intermediate nodes. –Induction: We need to prove S(k)  S(k+1), where S(k) is the loop invariant.

Discussion #34 15/17 Induction Part of the Proof Assume after k iterations, each Dist[i,j] is as short or shorter than any other path from i to j whose intermediate nodes are in {1, …, k}. There are two cases for the k+1 st iteration. –Case 1: Dist[i,j] > Dist[i,k+1] + Dist[k+1,j]. The new distance is Dist[i,k+1] + Dist[k+1,j], which is shorter than the previous distance, and thus by the induction hypothesis is as short or shorter than any other path from i to j whose intermediate nodes are in {1, …, k+1}. –Case 2: Dist[i,j]  Dist[i,k+1] + Dist[k+1,j]. The current path is as short or shorter and left unchanged, and thus by the induction hypothesis is as short or shorter than any other path from i to j whose intermediate nodes are in {1, …, k+1}.

Discussion #34 16/17 Notes on Warshall’s and Floyd’s Algorithms Warshall’s and Floyd’s algorithms do not produce paths. –Can keep track of paths –Update as the algorithms execute Warshall’s and Floyd’s algorithms only work for directed graphs. –For undirected graphs, replace each edge with two directed edges –Then, run algorithms as given.

Discussion #34 17/17 Notes on Warshall’s and Floyd’s Algorithms With DFS, cycle detection can be done in O(m), O(n 2 ) in the worst case. This beats Warshall’s algorithm, O(n 3 )—i.e. run algorithm and check to see if a 1 is on the diagonal. With DFS, reachability can be done in O(m), O(n 2 ) in the worst, which beats Warshall’s O(n 3 ), but Warshall’s tests reachability for all pairs.