Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
1 Strongly connected components. 2 Definition: the strongly connected components (SCC) C 1, …, C k of a directed graph G = (V,E) are the largest disjoint.
CSC401 – Analysis of Algorithms Lecture Notes 14 Graph Biconnectivity
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graphs – Depth First Search ORD DFW SFO LAX
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Algorithms and Data Structures
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Introduction to Graph Theory
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Graph Algorithms.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Chapter 22: Elementary Graph Algorithms
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
 2004 SDU 1 Lecture5-Strongly Connected Components.
5. Biconnected Components of A Graph If one city’s airport is closed by bad weather, can you still fly between any other pair of cities? If one computer.
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.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
Introduction to Algorithms
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Lecture 16 Strongly Connected Components
Graph Algorithms Using Depth First Search
CSCE 411 Design and Analysis of Algorithms
Graph Algorithms – 2 DAGs Topological order
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Applications of DFS Topological sort (for directed acyclic graph)
Basic Graph Algorithms
CS 583 Analysis of Algorithms
Richard Anderson Lecture 5 Graph Theory
Presentation transcript:

Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5 in the textbook (pp 552—557).

Data Structures, Spring 2006 © L. Joskowicz 2 Connected components Find the largest components (sub-graphs) such that there is a path from any vertex to any other vertex. Applications: networking, communications. Undirected graphs: apply BFS/DFS (inner function) from a vertex, and mark vertices as visited. Upon termination, repeat for every unvisited vertex. Directed graphs: strongly connected components, not just connected: a path from u to v AND from v to u, which are not necessarily the same!

Data Structures, Spring 2006 © L. Joskowicz 3 Example: strongly connected components d bfeac g h

Data Structures, Spring 2006 © L. Joskowicz 4 Example: strongly connected components d bfeac g h

Data Structures, Spring 2006 © L. Joskowicz 5 Strongly connected components Definition: the strongly connected components (SCC) C 1, …, C k of a directed graph G = (V,E) are the largest disjoint sub-graphs (no common vertices or edges) such that for any two vertices u and v in C i, there is a path from u to v and from v to u. Equivalence classes of the binary relation path(u,v) denoted by u ~ v. The relation is not symmetric! Goal: compute the strongly connected components of G in time linear in the graph size Θ(|V|+|E|).

Data Structures, Spring 2006 © L. Joskowicz 6 Strongly connected components graph Definition: the SCC graph G ~ = (V ~,E ~ ) of the graph G = (V,E) is as follows: –V ~ = {C 1, …, C k }. Each SCC is a vertex. –E ~ = {(C i,C j )| i≠j and (x,y)  E, where x  C i and y  C j }. A directed edge between components corresponds to a directed edge between them from any of their vertices. G ~ is a directed acyclic graph (no directed cycles)! Definition: the transpose graph G T = (V,E T ) of the graph G = (V,E) is G with its edge directions reversed: E T = {(u,v)| (v,u)  E}.

Data Structures, Spring 2006 © L. Joskowicz 7 Example: SCC graph C4C4 C3C3 C1C1 C2C2 d b f e a c g h

Data Structures, Spring 2006 © L. Joskowicz 8 Example: transpose graph G T d b f e a c g h d b f e a c g h G GTGT

Data Structures, Spring 2006 © L. Joskowicz 9 SCC algorithm Idea: compute the SCC graph G ~ = (V ~,E ~ ) with two DFS, one for G and one for its transpose G T, visiting the vertices in reverse order. SCC(G) 1. DFS(G) to compute finishing times f [v],  v  V 2. Compute G T 3.DFS(G T ) in the order of decreasing f [v] 4.Output the vertices of each tree in the DFS forest as a separate SCC.

Data Structures, Spring 2006 © L. Joskowicz 10 Example: computing SCC (1) 1/6 d bfeac g h 2/5 3/4 8/13 11/12 7/16 14/15 9/10

Data Structures, Spring 2006 © L. Joskowicz 11 Example: computing SCC (2) d bfeac g h /6 d bfeac g h 2/5 3/4 8/13 11/12 7/16 14/15 9/10

Data Structures, Spring 2006 © L. Joskowicz 12 Example: computing SCC (3) d bfeac g h d b f e a c g h 3/64/5 2/7 1/8

Data Structures, Spring 2006 © L. Joskowicz 13 Example: computing SCC (4) d b fe ac g h b e a c 1/2

Data Structures, Spring 2006 © L. Joskowicz 14 Example: computing SCC (5) d b f ea c g h b e a 1/2

Data Structures, Spring 2006 © L. Joskowicz 15 Example: computing SCC (6) 5 b e 1/42/3 d b f e ac g h

Data Structures, Spring 2006 © L. Joskowicz 16 Example: computing SCC (2) d b f e a c g h ’’ C3C3 C4C4 C1C1 C2C Labeled transpose graph G T 4’

Data Structures, Spring 2006 © L. Joskowicz 17 Proof of correctness: SCC (1) Lemma 1: Let C and C’ be two distinct SCC of G = (V,E), let u,v  C and u’,v’  C’. If there is a path from u to u’, then there cannot be a path from v’ to v. Definition: the start and finishing times of a set of vertices U  V is: d[U] = min u  U {d [u]} f [U] = max u  U {f [u]}

Data Structures, Spring 2006 © L. Joskowicz 18 Proof of correctness: SCC (2) Lemma 2: Let C and C’ be two distinct SCC of G, and let (u,v)  E where and u  C and v  C’. Then, f [C] > f [C’]. Proof: there are two cases, depending on which strongly connected component, C or C’ is discovered first: 1. C was discovered before C’: d(C) < d(C’) 2. C was discovered after C’: d(C) > d(C’)

Data Structures, Spring 2006 © L. Joskowicz 19 Example: finishing times d bfeac g h 1/6 d bfeac g h 2/5 3/4 8/13 11/12 7/16 14/15 9/10 f [C 3 ] = 6 f [C 4 ] = 5 f [C 2 ] = 15 f [C 1 ] = 16

Data Structures, Spring 2006 © L. Joskowicz 20 Example: finishing times d bfeac g h 1/6 d bfeac g h 2/5 3/4 8/13 11/12 7/16 14/15 9/10 f [C 3 ] = 6 f [C 4 ] = 5 f [C 2 ] = 15 f [C 1 ] = 16

Data Structures, Spring 2006 © L. Joskowicz 21 Proof of correctness: SCC (3) 1. d(C) < d(C’): C discovered before C’ Let x be the first vertex discovered in C. There is a path in G from x to each vertex of C which has not yet been discovered. Because (u,v)  E, for any vertex w  C’, there is also a path at time d[x] from x to w in G consisting only of unvisited vertices: x  u  v  w. Thus, all vertices in C and C’ become descendants of x in the depth-first tree. Therefore, f [x] = f [C] > f [C’].

Data Structures, Spring 2006 © L. Joskowicz 22 Proof of correctness: SCC (4) 2. d(C) > d(C’): C discovered after C’ Let y be the first vertex discovered in C’. At time d[y], all vertices in C’ are unvisited. There is a path in G from y to each vertex of C’ which has only vertices not yet discovered. Thus, all vertices in C’ will become descendants of y in the depth-first tree, and so f [y] = f [C’]. At time d[y], all vertices in C are unvisited. Since there is an edge (u,v) from C to C’, there cannot, by Lemma 1, be a path from C’ to C. Hence, no vertex in C is reachable from y.

Data Structures, Spring 2006 © L. Joskowicz 23 Proof of correctness: SCC (5) 2. d(C) > d(C’) At time f [y], therefore, all vertices in C are unvisited. Thus, no vertex in C is reachable from y. At time f [y], therefore, all vertices in C are still unvisited. Thus, for anuy vertex w in C: f [w] > f [y]  f [C] > f [C’].

Data Structures, Spring 2006 © L. Joskowicz 24 Proof of correctness: SCC (6) Corollary: for edge (u,v)  E T, and u  C and v’  C’ f [C] < f [C’] This provides shows to what happens during the second DFS. The algorithm starts at x with the SCC C whose finishing time f [C] is maximum. Since there are no vertices in G T from C to any other SCC, the search from x will not visit any other component! Once all the vertices have been visited, a new SCC is constructed as above.

Data Structures, Spring 2006 © L. Joskowicz 25 Proof of correctness: SCC (7) Theorem: The SCC algorithm computes the strongly connected components of a directed graph G. Proof: by induction on the number of depth-first trees found in the DFS of G T : the vertices of each tree form a SCC. The first k trees produced by the algorithm are SCC. Basis: for k = 0, this is trivially true. Inductive step: The first k trees produced by the algorithm are SCC. Consider the (k+1) st tree rooted at u in SCC C. By the lemma, f [u] = f [C] > f [C’] for SCC C’ that has not yet been visited.

Data Structures, Spring 2006 © L. Joskowicz 26 Proof of correctness: SCC (8) When u is visited, all the vertices v in its SCC have not been visited. Therefore, all vertices v are descendants of u in the depth-first tree. By the inductive hypothesis, and the corollary, any edges in G T that leave C must be in SCC that have already been visited. Thus, no vertex in any SCC other than C will be a descendant of u during the depth first search of G T. Thus, the vertices of the depth-first search tree in G T that is rooted at u form exactly one connected component.

Data Structures, Spring 2006 © L. Joskowicz 27 Uses of the SCC graph Articulation: a vertex whose removal disconnects G. Bridge: an edge whose removal disconnects G. Euler tour: a cycle that traverses all edges of G exactly once (vertices can be visited more than once) All can be computed in O(|E|) on the SCC. d bf eac g h C1C1 C2C2 C4C4 C3C3