Finding Strongly Connected Components

Similar documents
Design and Analysis of Algorithms

Strongly Connected Components

Fundamental Graph Algorithms Part Three

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Lecture 10: Strongly Connected Components, Biconnected Graphs

Graph Representation

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Graph Representations and Traversal

Lecture 22 Tuesday, April 10

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph Algorithms Using Depth First Search

6. Lecture notes on matroid intersection

1 Connected components in undirected graphs

CS483 Design and Analysis of Algorithms

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

Algorithm Design and Analysis

DFS & STRONGLY CONNECTED COMPONENTS

Inf 2B: Graphs II - Applications of DFS

Binary Heaps in Dynamic Arrays

Fundamental Graph Algorithms Part Four

Dynamic Arrays and Amortized Analysis

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CS 473: Algorithms. Chandra Chekuri 3228 Siebel Center. Fall University of Illinois, Urbana-Champaign

Announcements. HW3 is graded. Average is 81%

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Graph Algorithms. Definition

Number Theory and Graph Theory

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Lecture 8: The Traveling Salesman Problem

Lecture 3: Graphs and flows

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

Algorithms Activity 6: Applications of BFS

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Algorithm Design and Analysis

Priority Queues and Binary Heaps

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Copyright 2000, Kevin Wayne 1

5. Lecture notes on matroid intersection

Jessica Su (some parts copied from CLRS / last quarter s notes)

Lecture 15: The subspace topology, Closed sets

Depth First Search (DFS)

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

implementing the breadth-first search algorithm implementing the depth-first search algorithm

On the Relationships between Zero Forcing Numbers and Certain Graph Coverings

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Lecture 10 Graph algorithms: testing graph properties

Graph theory - solutions to problem set 1

Problem Set 2 Solutions

Chapter 22. Elementary Graph Algorithms

CS261: Problem Set #1

Algebra of Sets (Mathematics & Logic A)

Lecture 10. Finding strongly connected components

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

These notes present some properties of chordal graphs, a set of undirected graphs that are important for undirected graphical models.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

22.3 Depth First Search

1. Lecture notes on bipartite matching

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Strongly Connected Components. Andreas Klappenecker

Matchings in Graphs. Definition 1 Let G = (V, E) be a graph. M E is called as a matching of G if v V we have {e M : v is incident on e E} 1.

Basic Graph Algorithms

CSE 421 Applications of DFS(?) Topological sort

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

The complement of PATH is in NL

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Module 11. Directed Graphs. Contents

Answers to specimen paper questions. Most of the answers below go into rather more detail than is really needed. Please let me know of any mistakes.

Planarity: dual graphs

Matching Theory. Figure 1: Is this graph bipartite?

Lecture 6: Faces, Facets

2. CONNECTIVITY Connectivity

Problem Set 2 Solutions

15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017

Graph representation

Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets

Graph. Vertex. edge. Directed Graph. Undirected Graph

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

Chapter 6. Planar Orientations. 6.1 Numberings of Digraphs

arxiv: v1 [math.co] 3 Apr 2016

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

3 No-Wait Job Shops with Variable Processing Times

MAL 376: Graph Algorithms I Semester Lecture 1: July 24

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Ma/CS 6b Class 4: Matchings in General Graphs

Advanced Combinatorial Optimization September 17, Lecture 3. Sketch some results regarding ear-decompositions and factor-critical graphs.

W4231: Analysis of Algorithms

CS612 Algorithms for Electronic Design Automation

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Homework No. 4 Answers

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Fundamental Properties of Graphs

1 Variations of the Traveling Salesman Problem

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Transcription:

Yufei Tao ITEE University of Queensland

We just can t get enough of the beautiful algorithm of DFS! In this lecture, we will use it to solve a problem finding strongly connected components that seems to be rather difficult at first glance. As you probably have guessed, the algorithm is once again very simple, and runs DFS only twice.

Strongly Connected Component Let G = (V, E) be a directed graph. A strongly connected component (SCC) of G is a subset S of V such that For any two vertices u, v S, it must hold that: There is a path from u to v. There is a path from v to u. S is maximal in the sense that we cannot put any more vertex into S without violating the above property.

Example Consider the following graph: a c b e d k f g l i j h {a, b, c} is an SCC. {a, b, c, d} is not an SCC. {d, e, f, k, l} is not an SCC (because we can still add vertex g). {e, d, f, k, l, g} is an SCC.

SCCs are Disjoint Theorem: Suppose that S 1 and S 2 are both SCCs of G. Then, S 1 S 2 =. Proof: Assume that there is a vertex v in both S 1 and S 2. Then, for any vertex u 1 S 1 and any vertex u 2 S 2 : There is a path from u 1 to u 2 : we can first go from u 1 to v within S 1, and then from v to u 2 within S 2. Likewise, there is also a path from u 2 to u 1. Hence, neither S 1 nor S 2 is maximal, contradicting the fact that they are SCCs.

The Problem of Finding SCCs Given a directed graph G = (V, E), the goal of the finding strongly connected components problem is to divide V into disjoint subsets, each of which is an SCC.

Example a c b e d k f g l i j h The goal is to output the following 4 SCCs: {a, b, c}, {d, e, f, g, k, l}, {h, i}, and {j}.

Algorithm Step 1: Obtain the reverse graph G R by reversing the directions of all the edges in G.

Example a c b e d k f g l a b i c e d k f g l i j h j h Input graph Reverse graph

Algorithm Step 2: Perform DFS on G R, and obtain the sequence L R that the vertices in G R turn black (i.e., whenever a vertex is popped out of the stack, append it to L R ). Obtain L as the reverse order of L R.

Example Reverse graph G R : a c b e d k f g l i j h We may perform DFS starting from any vertex. When a restart is needed, we may do so from any vertex that is still white. The following is a possible order that the vertices are discovered: f, l, k, e, j, d, g, i, h, a, b, c. The corresponding turn-black sequence is L R = (k, l, j, h, i, g, d, e, f, c, b, a). Hence, L = (a, b, c, f, e, d, g, i, h, j, k, l).

Algorithm Step 3: Perform DFS on the original graph G by obeying the following rules: Rule 1: Start the DFS at the first vertex of L. Rule 2: Whenever a restart is needed, start from the first vertex of L that is still white. Output the vertices in each DFS-tree as an SCC.

Example From the last step, we have L = (a, b, c, f, e, d, g, i, h, j, k, l). The original graph G: a c b e d k f g l i j h Start DFS from a, which finishes after discovering {a, c, b}. Restart from f, which finishes after discovering {f, k, l, d, e, g} Restart from i, which finishes after discovering {i, h} Restart from j, which finishes after discovering {j} The DFS returns 4 DFS-trees, whose vertex sets are shown as above. Each vertex set constitutes an SCC.

Time Analysis Steps 1 and 2 obviously require only O( V + E ) time. Regarding Step 3, the DFS itself takes O( V + E ) time, but we still need to discuss the time to implement Rule 2. Namely, whenever DFS needs a restart, how do we find the first white vertex in L efficiently? This will be left as an exercise where you will be asked to do so in O( V ) total time. Hence, the overall execution time is O( V + E ).

Next, we will prove that the algorithm is correct. Once again, the correctness is due to the remarkable properties of DFS.

SCC Graph Let G be the input directed graph, with SCCs S 1, S 2,..., S t for some t 1. Let us define a SCC graph G SCC as follows: Each vertex in G SCC is a distinct SCC in G. Consider two vertices (a.k.a. SCCs) S i and S j (1 i, j t). G SCC has an edge from S i to S j if and only if i j, and There is a path in G from a vertex in S i to a vertex in S j.

Example S 2 S 1 a c b e d k f g l S 3 i SCC Graph S 1 S 2 S 3 j h S 4 S 4

SCC Graph Lemma: G SCC is a DAG. Proof: Suppose that there is a cycle in G SCC, which must involve at least 2 SCCs say S i, S j as no vertex in G SCC has an edge to itself. Then, any vertex in S i is reachable from any vertex in S j, and vice versa. This violates the fact that S i, S j are SCCs (violating maximality).

SCC Graph Define an SCC as a sink SCC if it has no outgoing edge in G SCC. Lemma: There must be at least one sink SCC in G SCC. Proof: Since G SCC is a DAG, it admits a topological order. The last vertex of the topological order cannot have any outgoing edges.

Example S 2 S 1 a c b e d k f g l S 3 i SCC Graph S 1 S 2 S 3 j h S 4 S 4 S 1 is a sink vertex.

DFS in a Sink SCC Lemma: Let S be a sink SCC of G SCC. Suppose that we perform a DFS starting from any vertex in S. Then the first DFS-tree output must include all and only the vertices in S. Proof: Let v S be the starting vertex of DFS. By the white path theorem of DFS, the DFS-tree must include all the vertices that v can reach. These are exactly the vertices in S.

Example S 2 S 1 a c b e d k f g l S 3 i SCC Graph S 1 S 2 S 3 j h S 4 S 4 Performing DFS from any vertex in S 1 will discover S 1 as the first SCC.

Finding SCCs The Strategy The previous lemma suggests the following strategy for finding all the SCCs: 1. Performing DFS from any vertex in a sink SCC S. 2. Delete all the vertices of S from G, as well as their edges. 3. Accordingly, delete S from G SCC, as well as its edges. 4. Repeat from Step 1, until G is empty.

Example After deleting S 1, we have: S 2 e d k f g l S 3 i SCC Graph S 2 S 3 j h S 4 S 4 Now, S 2 becomes the sink SCC. Performing DFS from any vertex in S 2 discovers S 2 as the second SCC.

Example After deleting S 2, we have: SCC Graph j h S 3 i S 4 S 3 S 4 Now, S 3 becomes the sink SCC. Performing DFS from any vertex in S 3 discovers S 3 as the third SCC.

Example After deleting S 3, we have: SCC Graph j S 4 S 4 Now, S 4 becomes the sink SCC. Performing DFS from any vertex in S 4 discovers S 4 as the last SCC.

Next, we will show that this is exactly the strategy taken by our algorithm. In particular, we resort to the ordering L to correctly identify the sequence of sink SCCs!

A Property of the Ordering L Lemma: Let S 1, S 2 be SCCs such that there is a path from S 1 to S 2 in G SCC. In the ordering of L, the earliest vertex in S 2 must come before the earliest vertex in S 1. Proof: Left to you as an exercise.

Example S 2 S 1 a c b e d k f g l S 3 i SCC Graph S 1 S 2 S 3 j h S 4 S 4 Recall that we obtained earlier L = (a, b, c, f, e, d, g, i, h, j, k, l). The red vertices a, f, i, j are, respectively, the earliest vertex in L of S 1, S 2, S 3, and S 4.

This essentially completes the proof of the correctness of our SCC algorithm. You may want to ask: but we never delete any vertices from G! In fact, we did, as far as DFS is concerned. To see this, recall that DFS colors all the done vertices black. These vertices are never touched again, and hence, effectively deleted.