Lecture 16 Strongly Connected Components

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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
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.,
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Tirgul 8 Graph algorithms: Strongly connected components.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
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.
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.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Graphs.
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.
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
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
 2004 SDU 1 Lecture5-Strongly Connected Components.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Undirected versus Directed Graphs
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.
CSC317 Graph algorithms Why bother?
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
Topological Sort (an application of DFS)
CS200: Algorithm Analysis
Graph Algorithms Using Depth First Search
CSCE 411 Design and Analysis of Algorithms
Planarity Testing.
Many slides here are based on E. Demaine , D. Luebke slides
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
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)
Graph Representation (23.1/22.1)
Graph Algorithms What is a graph? V - vertices E µ V x V - edges
Basic Graph Algorithms
Analysis of Algorithms CS 477/677
Topological Sort (an application of DFS)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Trevor Brown DC 2338, Office hour M3-4pm
Trevor Brown DC 2338, Office hour M3-4pm
Text Book: Introduction to algorithms By C L R S
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Lecture 16 Strongly Connected Components CS473-Algorithms I Lecture 16 Strongly Connected Components CS 473 Lecture 16

Strongly Connected Components Definition: a strongly connected component (SCC) of a directed graph G(V,E) is a maximal set of vertices U V such that For each u,v U we have both u  v and v  u i.e., u and v are mutually reachable from each other (u  v) Let GT(V,ET) be the transpose of G(V,E) where ET {(u,v): (u,v)  E} i.e., ET consists of edges of G with their directions reversed Constructing GT from G takes O(V+E) time (adjacency list rep) Note: G and GT have the same SCCs (u  v in G u  v in GT) CS 473 Lecture 16

Strongly Connected Components Algorithm Run DFS(G) to compute finishing times for all uV Compute GT Call DFS(GT) processing vertices in main loop in decreasing f[u] computed in Step (1) Output vertices of each DFT in DFF of Step (3) as a separate SCC CS 473 Lecture 16

Strongly Connected Components Lemma 1: no path between a pair of vertices in the same SCC, ever leaves the SCC Proof: let u and v be in the same SCC  u  v let w be on some path u w  v  u w but v  u   a path w v  u  w u therefore u and w are in the same SCC u SCC w v QED CS 473 Lecture 16

SCC: Example CS 473 Lecture 16

SCC: Example Run DFS(G) to compute finishing times for all uV CS 473 Lecture 16

SCC: Example Run DFS(G) to compute finishing times for all uV CS 473 Lecture 16

SCC: Example Run DFS(G) to compute finishing times for all uV CS 473 Lecture 16

SCC: Example b, e, a, c, d, g, h, f  Vertices sorted according to the finishing times: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example Compute GT CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (3) Call DFS(GT) processing vertices in main loop in decreasing f[u] order: b, e, a, c, d, g, h, f  CS 473 Lecture 16

SCC: Example (4) Output vertices of each DFT in DFF as a separate SCC Cc{c,d} Cb{b,a,e} Cg{g,f} Ch{h} CS 473 Lecture 16

SCC: Example Cc Cb Cg Ch CS 473 Lecture 16

Strongly Connected Components Thrm 1: in any DFS, all vertices in the same SCC are placed in the same DFT Proof: let r be the first vertex discovered in SCC Sr because r is first, color[x]WHITE xSr{r} at time d[r] So all vertices are WHITE on each r  x path xSr{r} since these paths never leave Sr Hence each vertex in Sr{r}becomes a descendent of r (White-path Thrm) G at time d[r] r W W W Sr QED CS 473 Lecture 16

Notation for the Rest of This Lecture d[u] and f[u] refer to those values computed by DFS(G) at step (1) u  v refers to G not GT Definition: forefather  (u) of vertex u  (u)  That vertex w such that u  w and f[u] is maximized  (u)  u possible because u  u  f[u]  f[ (u)] CS 473 Lecture 16

Strongly Connected Components Lemma 2:  ( (u))   (u) Proof try to show that f[ ( (u))]  f[ (u)] : For any u,v V; u  v  Rv  Ru  f[ (v)]  f[ (u)] So, u   (u)  f[ ( (u))]  f[ (u)] Due to definition of  (u) we have f[ ( (u))]  f[ (u)] Therefore f[ ( (u))]  f[ (u)] QED Note: f[x]  f[y]  x  y (same vertex) Rv {w: v w} u v  v  (u) CS 473 Lecture 16

Strongly Connected Components Properties of forefather: Every vertex in an SCC has the same forefather which is in the SCC Forefather of an SCC is the representative vertex of the SCC In the DFS of G, forefather of an SCC is the first vertex discovered in the SCC last vertex finished in the SCC CS 473 Lecture 16

Strongly Connected Components THM2: (u) of any u  V in any DFS of G is an ancestor of u PROOF: Trivial if (u)  u. If (u)  u, consider color of (u) at time d[u] (u) is GRAY: (u) is an ancestor of u  proving the theorem (u) is BLACK: f [(u)] < f [u]  contradiction to def. of (u) (u) is WHITE:  2 cases according to colors of intermediate vertices on p(u, (u)) Path p(u, (u)) at time d[u]: CS 473 Lecture 16

Strongly Connected Components Case 1: every intermediate vertex xi  p(u, (u)) is WHITE (u) becomes a descendant of u (WP-THM)  f [(u)] < f [u]  contradiction Case 2:  some non-WHITE intermediate vertices on p(u, (u)) Let xt be the last non-WHITE vertex on p(u, (u))  u, x1, x2,…, xr, (u) Then, xt must be GRAY since BLACK-to-WHITE edge (xt, xt+1) cannot exist But then, p(xt, (u))   xt+1, xt+2,…, xr, (u) is a white path  (u) is a descendant of xt (by white-path theorem) f [xt] > f [(u)] contradicting our choice for (u) Q.E.D. CS 473 Lecture 16

Strongly Connected Components C1: in any DFS of G  (V, E) vertices u and (u) lie in the same SCC, u  V PROOF: u  (u) (by definition) and (u)  u since (u) is an ancestor of u (by THM2) THM3: two vertices u,v V lie in the same SCC  (u) = (v) in a DFS of G  (V, E) PROOF: let u and v be in the same SCC Cuv  u  v CS 473 Lecture 16

Strongly Connected Components w: v  w  u  w and w: u  w  v  w, i.e., every vertex reachable from u is reachable from v and vice-versa So, w  (u)  w  (v) and w  (v)  w  (u) by definition of forefather PROOF: Let (u)  (v)  wCw  uCw by C1 and vCw by C1 By THM3: SCCs are sets of vertices with the same forefather By THM2 and parenthesis THM: A forefather is the first vertex discovered and the last vertex finished in its SCC CS 473 Lecture 16

SCC: Why do we Run DFS on GT? Consider r  V with largest finishing time computed by DFS on G r must be a forefather by definition since r  r and f [r] is maximum in V Cr  ?: Cr  vertices in r’s SCC  {u in V: (u)  r}  Cr  {u  V: u  r and f [x]  f [r] x  Ru} where Ru {v  V: u  v}  Cr  {u  V: u  r} since f [r] is maximum  Cr  RrT  {u  V: r  u in GT}  reachability set of r in GT i.e., Cr  those vertices reachable from r in GT Thus DFS-VISIT(GT, r) identifies all vertices in Cr and blackens them CS 473 Lecture 16

SCC: Why do we Run DFS on GT? BFS(GT, r) can also be used to identify Cr Then, DFS on GT continues with DFS-VISIT(GT, r ) where f [r ] > f [w] w  V Cr r must be a forefather by definition since r  r and f [r ] is maximum in V Cr CS 473 Lecture 16

SCC: Why do we Run DFS on GT? Hence by similar reasoning DFS-VISIT(GT, r ) identifies Cr Impossible since otherwise r, w  Cr  r, w would have been blackened Thus, each DFS-VISIT(GT, x) in DFS(GT) identifies an SCC Cx with   x CS 473 Lecture 16