570 likes | 1.08k Views
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.
E N D
Find Strongly Connected Components Using Kosaraju’s Algorithm And Tarjan’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. • Complexity of Kosaraju’sAlgorithm. • How ’s Tarjan’s Algorithm works. • Complexity of Tarjan’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:
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 B C D CD E F G ABE FG
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: • If G contains both (u, u’) and (v’, v), then C and C’ is reachable from each other. • Because vertices in a SCC set is reachable from each other, every vertex in C and C’ will be reachable from each other. • Thus, C and C’ are not distinct SCC => contradiction.
Property(II) of strongly connected components • Graph G and its transpose GT have exactly the same SCCs. A B C D E A B C D E
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.
Kosaraju’s Algorithm • Make use of the second property of SCC. • Kosaraju(G) • Run DFS(G) to compute finishing times u.f for each vertex u. • Compute GT. • Run DFS(GT), choose the vertices with the decreasing order of u.f calculated in step 1. • Put every vertex visited during each DFS iteration into current SCC set. • When current DFS iteration comes to an end, put current SCC set into SCC list, and start next DFS iteration from another unvisited vertex. • After all vertices are visited, we get a complete SCC list.
Kosaraju’s Algorithm • Run DFS(G). A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm • Compute GT. A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {B} Present SCC list: NULL • Run DFS(GT). • Start from B A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {B, A} Present SCC list: NULL • Run DFS(GT). A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {B, A, E} Present SCC list: {B, A, E} • Run DFS(GT). A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {C} Present SCC list: {B, A, E} • Run DFS(GT). • Choose C. A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {C, D} Present SCC list: {B, A, E}, {C, D} • Run DFS(GT). A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {G} Present SCC list: {B, A, E}, {C, D} • Run DFS(GT). • Choose G. A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {G, F} Present SCC list: {B, A, E}, {C, D}, {G, F} • Run DFS(GT). A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
Kosaraju’s Algorithm Present SCC set: {H} Present SCC list: {B, A, E}, {C, D}, {G, F}, {H} • Run DFS(GT). • Finish. A B C D 13/14 11/16 1/10 8/9 12/15 2/7 5/6 3/4 E F G H
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.
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.
Complexity of Kosaraju’s Algorithm • DFS x 2, G transpose x 1 • Analysis: • Do DFS(G) first time. => Θ(V+E) / Θ(V^2) • Transpose G. => Θ(E) / 0 • Do DFS(GT) and produce SCC sets. => Θ(V+E) / Θ(V^2) • Using adjacency list: Θ(V+E) • Using adjacency matrix: Θ(V^2)
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 funcitonTarjan(v). • One time of DFS may produce multiple SCC sets. • Some parameters: • 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
Tarjan’s Algorithm • Tarjan(v) • Set both v.index, v.low to a global variable “t”. • ++t • Push v into the stack S. • Traverse every vertex directly reachable from v. • If vertex w is unvisited, run Tarjan(w). Then set v.low = min{v.low, w.low}. • If vertex w is visited, v.low = min{v.low, w.low} • After the loop above, we can get the index of the oldest ancestor reachable from v (a.k.a. v.low). • If (v.low == v.index), v is the root of the SCC. • Pop the items in stack out until v is popped out. • Those vertices compose a SCC.
Tarjan’s Algorithm SCC list: NULL A B C Stack E F G D
Tarjan’s Algorithm 1/1 SCC list: NULL A B C A Discover A, put A in the stack. Stack E F G D
Tarjan’s Algorithm 1/1 SCC list: NULL A 2/2 B Discover B, put B in the stack. B C A Stack E F G D
Tarjan’s Algorithm 1/1 SCC list: NULL A A is a visited ancestor, so we change B.low to 1 2/1 B B C A Stack E F G D
Tarjan’s Algorithm 1/1 SCC list: NULL A 2/1 B Discover C, put C in the stack. C B C A 3/3 Stack E F G D
Tarjan’s Algorithm 1/1 SCC list: NULL A 2/1 B D Discover D, put Din the stack. C B C A 3/3 Stack E F G 4/4 D
Tarjan’s Algorithm 1/1 SCC list: NULL A B is a visited ancestor, so we change D.low to 1. Then trace back one level and change C.low to 1. 2/1 B D C B C A 3/1 Stack E F G 4/1 D
Tarjan’s Algorithm 1/1 SCC list: NULL A 2/1 Discover E, put E in the stack. E B D C B C A 3/1 Stack E F G 4/1 5/5 D
Tarjan’s Algorithm 1/1 SCC list: NULL A Discover F, put F in the stack. F 2/1 E B D C B C A 3/1 Stack E F G 4/1 5/5 6/6 D
Tarjan’s Algorithm 1/1 SCC list: NULL A F 2/1 E B D C B C A 3/1 E is a visited ancestor, so we change F.low to 5. Stack E F G 4/1 5/5 6/5 D
Tarjan’s Algorithm 1/1 SCC list: NULL A Discover G, put Gin the stack. G F 2/1 E B D C B C A 3/1 Stack E F G 4/1 5/5 6/5 7/7 D
Tarjan’s Algorithm 1/1 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. G F 2/1 E B D C B C A 3/1 Stack E F G 4/1 5/5 6/5 7/7 D
Tarjan’s Algorithm 1/1 SCC list: {G} A Pop until G. (7==7) We got a SCC set: {G} G F 2/1 E B D C B C A 3/1 Stack E F G 4/1 5/5 6/5 7/7 D
Tarjan’s Algorithm 1/1 SCC list: {G}, {E, F} A F Pop until E. (5==5) We got a SCC set: {E, F} 2/1 E B D C B C A 3/1 Stack E F G 4/1 5/5 6/5 7/7 D
Tarjan’s Algorithm 1/1 SCC list: {G}, {E, F}, {A, B, C, D} A 2/1 B D C Pop until A. (1==1) We got a SCC set: {A, B, C, D} B C A 3/1 Stack E F G 4/1 5/5 6/5 7/7 D
Complexity of Tarjan’s Algorithm • Total operation: DFS x 1 • Analysis: • Discover every vertex and initialize v.index, v.low. => Θ(V) • Traverse every edge. => Θ(E) / Θ(V^2) • Pop items out and build SCC sets. => Θ(V) • Using adjacency list: Θ(V+E) • Using adjacency matrix: Θ(V^2)