Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 312: Algorithm Analysis Lecture #16: Strongly Connected Components This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.

Similar presentations


Presentation on theme: "CS 312: Algorithm Analysis Lecture #16: Strongly Connected Components This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported."— Presentation transcript:

1 CS 312: Algorithm Analysis Lecture #16: Strongly Connected Components This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative Commons Attribution-Share Alike 3.0 Unported License Slides by: Eric Ringger, adapting figures from Dasgupta et al.

2 Objectives  Understand how to linearize a DAG  Introduce the idea of connectedness among vertices in Directed Graphs  Introduce the algorithm for finding Strongly Connected Components

3 Cycles  How do you detect a cycle in a directed graph?  Property: A directed graph has a cycle if and only if its depth-first search reveals a back edge.  Otherwise: a Directed Acyclic Graph (DAG)

4 Sources, Sinks, Linearization DFS Search Forest: Linearized DAG: 0. Define order; 1. DFS; 2. Read off post-order values in reverse order Analysis:

5 Sources, Sinks, Linearization DFS Search Forest: Linearized DAG: 0. Define order; 1. DFS; 2. Read off post-order values in reverse order Analysis:

6 Sources, Sinks, Linearization  Property: In a DAG, every edge leads to a vertex with a smaller post number.  Property: Every DAG has at least one source (a node with 0 in-degree).  Property: Every DAG has at least one sink (a node with 0 out-degree).  How to prove these?

7 Vertex Many Kinds of Connectedness Graphs Undirected Directed Edge Vertex Edge Connected components Biconnected components Strongly connected components Similar to biconnected components

8 Connectivity in Directed Graphs  Two nodes u and v of a directed graph are connected iff there is a path from u to v and a path from v to u.  Each vertex v is connected to itself.  Defines an equivalence relation!  This relation partitions V into disjoint sets that we call strongly connected components.  Equivalence classes under the above relation

9 Example Meta-graph:

10 Example Meta-graph:

11 Meta-Graph

12 Property: Every directed graph is a DAG of its strongly connected components.

13 How would you find SCCs?

14  Idea 1: run DFS, identify back-edges to find cycles, post-process  Idea 2: run DFS, identify back-edges to find cycles, merge cycles  How to analyze?  We’re going to go take a different direction …

15 Finding Strongly Connected Components  To understand the algorithm for finding SCCs, you need to understand the following:  Pre and post numbering in DFS  Meta-graph created by representing each SCC with a single meta-node.  The meta-graph is a DAG and could therefore be linearized using DFS.

16 Finding SCCs  Goal: Given a directed graph, find all of the SCCs  Big Idea:  A: Find a (vertex in a) sink SCC  B: Work back to find the other SCCs  How do you find a sink SCC?  Finding a sink (node) in a DAG is easy  But what about when there are cycles?  Problem A seems hard, so solve problem A’ first:  Finding a node in a source SCC of a directed graph could be easier.  A’: find a source SCC on the “reverse graph”

17 Reverse Graph G To find a sink, identify a source SCC of the reverse graph G R ! GRGR Metagraph of G R

18 Insights  Property: The node that receives the highest post number in a depth-first search must lie in a source strongly connected component.  Property: If C and C’ are strongly connected components, and there is an edge from a node in C to a node in C’, then the highest post number in C is bigger than the highest post number in C’.  Consequence: The strongly connected components can be linearized by arranging them in decreasing order of their post numbers.

19 Step 1  Reverse the graph to produce G R  Necessary to do the reversal explicitly?  Run depth-first search on G R

20 Problem 3.4 (ii)

21

22

23 Step 2  How do we continue once a vertex belonging to the sink component has been discovered?  Recall: The strongly connected components can be linearized by arranging them in decreasing order of their post numbers.  Run DFS on G, keeping track of connected components  Process the vertices beginning and breaking ties using the decreasing order of their post numbers from step 1  Can neglect keeping pre and post numbers this time  Can neglect back-, cross-, and forward- edges this time as well

24

25

26 3 Questions  Is it correct?  Relies on the correctness of the properties we identified earlier  How long does it take?  2 DFS passes  O(||V|| + ||E||) – again!  Can we do better?

27 Assignment  HW #11:  Practice using the SCC-finding algorithm  Study for Midterm (Study Guide)  Testing Center: Tuesday-Thurs (10-12 July)  One page of notes hand written or typed by you  Read 4.1-4.7  Shortest Paths  Breadth First Search  Dijkstra’s Algorithm


Download ppt "CS 312: Algorithm Analysis Lecture #16: Strongly Connected Components This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported."

Similar presentations


Ads by Google