26
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng

Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng

Embed Size (px)

Citation preview

Lecture 16:DFS, DAG, and Strongly Connected Components

Shang-Hua Teng

Directed Acyclic Graphs

• A directed acyclic graph or DAG is a directed graph with no directed cycles:

DFS and DAGs

• Theorem: a directed graph G is acyclic iff a DFS of G yields no back edges:– => if G is acyclic, will be no back edges

• Trivial: a back edge implies a cycle

– <= if no back edges, G is acyclic• Proof by contradiction: G has a cycle a back edge

– Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle

– When v discovered, whole cycle is white– Must visit everything reachable from v before returning from

DFS-Visit()– So path from uv is graygray, thus (u, v) is a back edge

Topological Sort

• Topological sort of a DAG:– Linear ordering of all vertices in graph G such that

vertex u comes before vertex v if edge (u, v) G

• Real-world application: Scheduling a dependent graph, find a feasible course plan for university studies

A Topological Sort AlgorithmTopological-Sort(){

1. Call DFS to compute finish time f[v] for each vertex

2. As each vertex is finished, insert it onto the front of a linked list

3. Return the linked list of vertices}

• Time: O(V+E)• Correctness: need to prove that

(u,v) G f[u]>f[v]

Correctness of Topological Sort

• Lemma: (u,v) G f[u] > f[v]– When (u,v) is explored, u is gray, consider the

following cases:1. v is gray (u,v) is back edge. Can’t happen, if G is a

DAG.2. v if white v becomes descendent of u f[v]

< f[u] (since must finish v before backtracking and finishing u)

3. v is black v already finished f[v] < f[u]

Our Algorithm for Topological Sorting is correct

Strongly Connected Directed graphs

• Every pair of vertices are reachable from each other

a

b

d

c

e

f

g

Strongly-ConnectedGraph G is strongly connected if, for every u

and v in V, there is some path from u to v and some path from v to u.

Strongly Connected

Not Strongly Connected

Strongly-Connected Components

A strongly connected component of a graph is a maximal subset of nodes (along with their associated edges) that is strongly connected. Nodes share a strongly connected component if they are inter-reachable.

Strongly Connected Components

a

b

d

c

e

f

g { a , c , g }

{ f , d , e , b }

Reduced Component Graph of Strongly Connected Components

a

b

d

c

e

f

g{ a , c , g }

{ f , d , e , b }

• Component graph GSCC=(VSCC, ESCC): one vertex for each component– (u, v) ESCC if there exists at least one directed

edge from the corresponding components

Strongly Connected Components

Graph of Strongly Connected Components

• Theorem: the Component graph GSCC=(VSCC, ESCC) is a DAG– Each component is maximal in the sense that no

other vertices can be added to it. If GSCC=(VSCC, ESCC) is not a DAG, then one can merge components on along a circle of GSCC

• Therefore, GSCC has a topological ordering

Finding Strongly-Connected Components

• Input: A directed graph G = (V,E)

• Output: a partition of V into disjoint sets so that each set defines a strongly connected component of G

• How should we compute the partition?

Graph of Strongly Connected Components

• Recall: Theorem: the Component graph GSCC=(VSCC, ESCC) is a DAG– Each component is maximal in the sense that no

other vertices can be added to it. If GSCC=(VSCC, ESCC) is not a DAG, then one can merge components on along a circle of GSCC

• Therefore, GSCC has a topological ordering

DFS on G Topological Sort GSCC=(VSCC, ESCC)

• Let U be a subset of V

• If we output U in VSCC in the decreasing order of f[U], then we topologically sort GSCC

• Lemma: Let U and U’ be distinct strongly connected component, suppose there is an edge (u,v) in E where u in U and v in U’. Then f[U] > f[U’]

][max)(

][min)(

ufUf

udUd

Uu

Uu

Proof of the Lemma

Lemma: Let U and U’ be distinct strongly connected component, suppose there is an edge (u,v) in E where u in U and v in U’. Then f[U] > f[U’]

Proof: Two cases1. d[U] < d[U’], say x in U is the first vertex

2. d[U’] < d[U], say y is the first, but U is not reachable from y

'Uvux

Transpose of a Digraph

Transpose of G = (V,E):

GT=(V, ET), where ET={(u, v): (v, u) E}

If G is a DAG then GT is also a DAG

If we print the topological order of G in the reverse order, then it is a topological order of GT

Strongly-Connected ComponentsStrongly-Connected-Components(G)

1. call DFS(G) to compute finishing times f[u] for each vertex u.

2. compute GT

3. call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u]

4. output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component.

The graph GT is the transpose of G, which is visualized by reversing the arrows on the digraph.

Strong Components: example

a

d c

b a

d c

b

after step 1

a4

b3

c2

d1a4

d1 c2

b3

Graph Gr

a4

c2

b3

d1

df spanning forest for Gr

Runtime

Lines 1 and 3 are (E+V) due to DFS

Line 2 involves creating an adjacency list or matrix, and it is also O(E+V)

Line 4 is constant time

So, SCC(G) is (E+V)

Strongly-Connected Components

DFS on G, starting at c.

node ad=13f=14

node bd=11f=16

node cd=1f=10

node dd=8f=9

node ed=12f=15

node fd=3f=4

node gd=2f=7

node hd=5f=6

DFS on GT

node ad=13f=14

node bd=11f=16

node cd=1f=10

node dd=8f=9

node ed=12f=15

node fd=3f=4

node gd=2f=7

node hd=5f=6

node ad=2f=5=b

node bd=1f=6

=NIL

node cd=7f=10=NIL

node dd=8f=9=c

node ed=3f=4=a

node fd=12f=13=g

node gd=11f=14=NIL

node hd=15f=16=NIL

DFS on GT

This is GT, labeled after running DFS(GT). In order of decreasing finishing time, process the nodes in this order: b e a c d g h f.

GT:

node ad=2f=5=b

node bd=1f=6

=NIL

node cd=7f=10=NIL

node dd=8f=9=c

node ed=3f=4=a

node fd=12f=13=g

node gd=11f=14=NIL

node hd=15f=16=NIL

Strongly-Connected ComponentsThese are the 4 trees that result, yielding the strongly connected

components. Finally, merge the nodes of any given tree into a super-node, and draw

links between them, showing the resultant acyclic component graph.

a

b c

d

e

f

g h

a b c d

e f g h

abe cd

fg h

Component Graph