1.07k likes | 1.08k Views
This algorithm finds the strongly connected components in a directed graph using the Kosaraju-Sharir algorithm.
E N D
Advanced algorithms strongly connected components algorithms, Euler trail, Hamiltonian path Jiří Vyskočil, Radek Mařík 2013
Connected component b C(a)=C(b)={a,b} c a C(c) =C(d) =C(e)={c,d,e} d e A connected component of graphG =(V,E ) with regard to vertex vis a set C(v ) = {u∈V | there exists a pathin G fromutov }. In other words: If a graph is disconnected, then parts from which is composed from and that are themselves connected, are called connected components.
Strongly Connected Components[silně souvislé komponenty] a b c d e f g h A directed graph G =(V,E )is called strongly connected if there is a path in each direction between every couple of vertices in the graph. The strongly connected components of a directed graph G are its maximal strongly connected subgraphs. SCC(v ) = {u∈V | there exists a pathin G fromutov and a pathin G fromvtou}
Kosaraju-SharirAlgorithm input: graph G = (V, E ) output: set of strongly connected components (sets of vertices) • S = empty stack; • whileS does not contain all vertices do Choose an arbitrary vertex v not in S; DFS-Walk’(v ) and each time that DFS finishes expanding a vertex u, push u onto S; • Reverse the directions of all arcs to obtain the transpose graph; • whileS is nonempty do v = pop(S); ifv is UNVISITED then DFS-Walk(v ); The set of visited vertices will give the strongly connected component containing v;
DFS-Walk • input:Graph G. • procedure DFS-Walk(Vertex u ) { • state[u ] = OPEN; d[u ] = ++time; • for each Vertexvin succ(u ) • if (state[v ] == UNVISITED) then{p[v ] = u; DFS-Walk(v ); } • state[u ] = CLOSED; f[u ] = ++time; • } • procedure DFS-Walk’(Vertex u ) { • state[u ] = OPEN; d[u ] = ++time; • for each Vertexvin succ(u ) • if (state[v ] == UNVISITED) then{p[v ] = u; DFS-Walk’(v ); } • state[u ] = CLOSED; f[u ] = ++time; push u to S; • } • output: arrayppointing to predecessor vertex, array dwith times of vertex opening and array fwith time of vertex closing.
DFS-Walk - optimized • input:Graph G. • procedure DFS-Walk(Vertex u ) { • state[u ] = OPEN; • for each Vertexvin succ(u ) • if (state[v ] == UNVISITED) thenDFS-Walk(v ); • state[u ] = CLOSED; • } • procedure DFS-Walk’(Vertex u ) { • state[u ] = OPEN; • for each Vertexvin succ(u ) • if (state[v ] == UNVISITED) thenDFS-Walk’(v ); • state[u ] = CLOSED; push u to S; • }
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED
Kosaraju-SharirAlgorithm a b c d e f g h OPEN CLOSED UNVISITED