210 likes | 345 Views
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License . CS 312: Algorithm Analysis. Lecture #16: Strongly Connected Components. Slides by: Eric Ringger, adapting figures from Dasgupta et al. Objectives. Understand how to linearize a DAG
E N D
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. CS 312: Algorithm Analysis Lecture #16: Strongly Connected Components Slides by: Eric Ringger, adapting figures from Dasgupta et al.
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
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)
Sources, Sinks, Linearization DFS Search Forest: Linearized DAG: Analysis: 0. Define order; 1. DFS; 2. Read off post-order values in reverse order
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?
Many Kinds of Connectedness Graphs Undirected Directed Edge Edge Vertex Vertex Strongly connected components Similar to biconnected components Connected components Biconnected components
Connectivity in Directed Graphs • Two nodes u and v of a directed graph are connectediff 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
Example Meta-graph:
Meta-Graph Property: Every directed graph is a DAG of its strongly connected components.
How would you find SCCs? • 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 …
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.
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”
Reverse Graph GR Metagraph of GR G To find a sink, identify a source SCC of the reverse graph GR!
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.
Step 1 • Reverse the graph to produce GR • Necessary to do the reversal explicitly? • Run depth-first search on GR
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
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?
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