39
FIND STRONGLY CONNECTED COMPONENTS USING KOSARAJU’S ALGORITHM AND TARJAN’S ALGORITHM National Chiao Tung University Department of Electronics Engineering

Find Strongly Connected C omponents Using K osaraju’s Algorithm And T arjan’s Algorithm

  • Upload
    bevis

  • View
    53

  • Download
    0

Embed Size (px)

DESCRIPTION

Find Strongly Connected C omponents Using K osaraju’s Algorithm And T arjan’s Algorithm. National Chiao Tung University Department of Electronics Engineering. Outline. What is strongly connected components (SCC) ? 2 properties of SCC. How Kosaraju’s Algorithm works. - PowerPoint PPT Presentation

Citation preview

Page 1: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

FIND STRONGLY CONNECTED COMPONENTS USING KOSARAJU’S ALGORITHM AND TARJAN’S ALGORITHMNational Chiao Tung UniversityDepartment of Electronics Engineering

Page 2: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

OUTLINE

What is strongly connected components (SCC) ?

2 properties of SCC. How Kosaraju’s Algorithm works. Complexity of Kosaraju’s Algorithm. How ’s Tarjan’s Algorithm works. Complexity of Tarjan’s Algorithm.

Page 3: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

WHAT IS STRONGLY CONNECTED COMPONENTS? Definition: A strongly connected components

(SCC) of a directed graph G = (V, E) is a maximal set of vertices C V, such that every vertices in C is reachable from each other.

Not strongly connected: Strongly connected:

Page 4: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

PROPERTY(I) OF STRONGLY CONNECTED COMPONENTS If we compress each SCC set in the graph G

to a single vertex, G will become a DAG.

A

E

B

F G

C D

ABECD

FG

Page 5: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

PROPERTY(I) OF STRONGLY CONNECTED COMPONENTS The compressed SCC graph is a DAG. Lemma 22.13: Let C and C’ be distinct SCCs in

directed graph G. Let u, v C; u’, v’ C’. If G contains a path (u, u’), then G cannot also contain a path (v’, v).

Proof: 1. If G contains both (u, u’) and (v’, v), then C and

C’ is reachable from each other. 2. Because vertices in a SCC set is reachable from

each other, every vertex in C and C’ will be reachable from each other.

3. Thus, C and C’ are not distinct SCC => contradiction.

Page 6: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

PROPERTY(II) OF STRONGLY CONNECTED COMPONENTS Graph G and its transpose GT have exactly

the same SCCs.

A

E

B C D

A

E

B C D

Page 7: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

PROPERTY(II) OF STRONGLY CONNECTED COMPONENTS Informal proof. A SCC set → vertices within it construct a

cycle. Reverse the edges of a cycle → still a cycle. According to property I, we can transform

graph G in to a DAG. Reverse the edges of a DAG → still a DAG. So the SCC sets of G is the same as GT ’s.

Page 8: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Make use of the second property of SCC. Kosaraju(G)1. Run DFS(G) to compute finishing times u.f

for each vertex u.2. Compute GT.3. Run DFS(GT), choose the vertices with the

decreasing order of u.f calculated in step 1.4. Put every vertex visited during each DFS

iteration into current SCC set. 5. When current DFS iteration comes to an end,

put current SCC set into SCC list, and start next DFS iteration from another unvisited vertex.

6. After all vertices are visited, we get a complete SCC list.

Page 9: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(G).

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Page 10: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Compute GT.

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Page 11: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT). Start from B

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {B}Present SCC list: NULL

Page 12: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT).

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {B, A}Present SCC list: NULL

Page 13: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT).

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {B, A, E}Present SCC list: {B, A, E}

Page 14: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT). Choose C.

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {C}Present SCC list: {B, A, E}

Page 15: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT).

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {C, D}Present SCC list: {B, A, E}, {C, D}

Page 16: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT). Choose G.

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {G}Present SCC list: {B, A, E}, {C, D}

Page 17: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT).

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {G, F}Present SCC list: {B, A, E}, {C, D}, {G, F}

Page 18: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

KOSARAJU’S ALGORITHM Run DFS(GT). Finish.

13/14 8/911/1

6 1/10

12/15 3/4 2/7 5/6

A B C

E F

D

HG

Present SCC set: {H}Present SCC list: {B, A, E}, {C, D}, {G, F}, {H}

Page 19: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

WHY KOSARAJU’S ALGORITHM WORKS? According to Corollary 22.15: Each edge in

the transpose of G that goes between different SCC goes from a vertex with an earlier finishing time to one with a later finishing time.

According to property I, we can compress the graph into a DAG.

So, if we choose the vertex in the transpose of G to start DFS in the order of decreasing finishing time, we are actually visiting the DAG of SCC in the reverse topological order.

Page 20: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

WHY KOSARAJU’S ALGORITHM WORKS? Because there is a two-way path between

any pair of vertices in a SCC. We can traverse all the vertices in a SCC by

DFS in any order. Because we visit the DAG of SCC in the

reverse topological order. No matter how we traverse all vertices within

a SCC by DFS, we won’t accidentally go into other SCC. The vertices we can visit by one time of DFS are constrained to the same SCC set.

Remember to delete the whole SCC set from the graph once it is visited.

Page 21: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

COMPLEXITY OF KOSARAJU’S ALGORITHM DFS x 2, G transpose x 1 Analysis: 1. Do DFS(G) first time. => Θ(V+E) / Θ(V^2)2. Transpose G. => Θ(E) / 03. Do DFS(GT) and produce SCC sets. =>

Θ(V+E) / Θ(V^2) Using adjacency list: Θ(V+E) Using adjacency matrix: Θ(V^2)

Page 22: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM Modified DFS. Every vertex in the SCC can be the root. Traverse every vertex, if a vertex v is

unvisited, then it is the root of a new SCC. We call the funciton Tarjan(v).

One time of DFS may produce multiple SCC sets.

Some parameters: 1. v.index is the time stamp we discover vertex

v; v.low is the index of oldest ancestor v can reach.1/1

2/2

3/2

4/2

Page 23: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM Tarjan(v)1. Set both v.index, v.low to a global variable “t”.2. ++t3. Push v into the stack S. 4. Traverse every vertex directly reachable from v.

a. If vertex w is unvisited, run Tarjan(w). Then set v.low = min{v.low, w.low}.

b. If vertex w is visited, v.low = min{v.low, w.low}5. After the loop above, we can get the index of the

oldest ancestor reachable from v (a.k.a. v.low). 6. If (v.low == v.index), v is the root of the SCC. 7. Pop the items in stack out until v is popped out. 8. Those vertices compose a SCC.

Page 24: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

A

B

C

D

E F G

Stack

SCC list: NULL

Page 25: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1 A

B

C

D

E F G

Stack

SCC list: NULL

A Discover A, put A in the stack.

Page 26: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/2

A

B

C

D

E F G

Stack

SCC list: NULL

ADiscover B, put B in the stack. B

Page 27: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

A

B

C

D

E F G

Stack

SCC list: NULL

A

A is a visited ancestor, so we change B.low to 1

B

Page 28: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/3

A

B

C

D

E F G

Stack

SCC list: NULL

A

Discover C, put C in the stack. BC

Page 29: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/3

4/4

A

B

C

D

E F G

Stack

SCC list: NULL

A

Discover D, put D in the stack.

BCD

Page 30: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1

A

B

C

D

E F G

Stack

SCC list: NULL

ABCD

B is a visited ancestor, so we change D.low to 1. Then trace back one level and change C.low to 1.

Page 31: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5

A

B

C

D

E F G

Stack

SCC list: NULL

A

Discover E, put E in the stack.

BCDE

Page 32: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5 6/6

A

B

C

D

E F G

Stack

SCC list: NULL

A

Discover F, put F in the stack.

BCDEF

Page 33: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5 6/5

A

B

C

D

E F G

Stack

SCC list: NULL

ABCDEF

E is a visited ancestor, so we change F.low to 5.

Page 34: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5 6/5 7/7

A

B

C

D

E F G

Stack

SCC list: NULL

A

Discover G, put G in the stack.

BCDEFG

Page 35: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5 6/5 7/7

A

B

C

D

E F G

Stack

SCC list: NULL

A

Pop items out until v.index == v.low. That is, pop until the root vertex of present SCC is popped out.

BCDEFG

Page 36: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5 6/5 7/7

A

B

C

D

E F G

Stack

SCC list: {G}

A

Pop until G. (7==7)We got a SCC set: {G}

BCDEFG

Page 37: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5 6/5 7/7

A

B

C

D

E F G

Stack

SCC list: {G}, {E, F}

A

Pop until E. (5==5)We got a SCC set: {E, F}

BCDEF

Page 38: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

TARJAN’S ALGORITHM

1/1

2/1

3/1

4/1 5/5 6/5 7/7

A

B

C

D

E F G

Stack

SCC list: {G}, {E, F}, {A, B, C, D}

A

Pop until A. (1==1)We got a SCC set: {A, B, C, D}

BCD

Page 39: Find Strongly Connected  C omponents Using  K osaraju’s  Algorithm And  T arjan’s  Algorithm

COMPLEXITY OF TARJAN’S ALGORITHM Total operation: DFS x 1 Analysis: 1. Discover every vertex and initialize v.index,

v.low. => Θ(V)2. Traverse every edge. => Θ(E) / Θ(V^2)3. Pop items out and build SCC sets. => Θ(V) Using adjacency list: Θ(V+E) Using adjacency matrix: Θ(V^2)