ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StrongComponents.h

The strongly connected components of an oriented graph by one visit in depth, beyond the book, whose visits and components by the algorithm of Kosaraju are in Visit.h. More...

Files

file  ADS/graph/StrongComponents.h
 The strongly connected components of an oriented graph by one visit in depth, beyond the book, whose visits and components by the algorithm of Kosaraju are in Visit.h [see Visit.h, which includes this header]:

Functions

template<Graph G>
std::vector< int > ADS::sccTarjan (const G &g)
 the strongly connected components of scc() by one visit in depth, with no transposed graph, numbered as there
template<Graph G>
std::vector< int > ADS::sccPearce (const G &g)
 the strongly connected components of sccTarjan() with one integer and one bit per node in place of three integers, numbered as scc() does

Detailed description

The strongly connected components of an oriented graph by one visit in depth, beyond the book, whose visits and components by the algorithm of Kosaraju are in Visit.h.

The definitions, the invariants and the costs of the area are in Theory of graphs.

Function documentation

◆ sccPearce()

template<Graph G>
std::vector< int > ADS::sccPearce ( const G & g)

the strongly connected components of sccTarjan() with one integer and one bit per node in place of three integers, numbered as scc() does

In pseudocode (Pearce's algorithm, beyond the book): \( \mathit{rindex}[ u ] \) is the order of discovery of \( u \) until a node of a lower one is reached, which it then takes; a closed component takes the number \( c \), which counts down from \( N - 1 \) (\( N \) the bound()) and is never below a number in use, since \( i \) gives back the numbers of the nodes closed. A test of \( \mathit{rindex} \) alone thus tells a node of the stack from a closed one (see Visits).

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sccPearce}( G ) \cr 2 & \quad i \gets 1;\ c \gets N - 1;\ S \gets \text{an empty stack}; \ \mathit{rindex}[ u ] \gets 0\ \forall u \cr 3 & \quad \textbf{foreach}\ r \in V\ \textbf{with}\ \mathit{rindex}[ r ] = 0\ \textbf{do}\ \mathrm{visit}( r ) \cr 4 & \quad \textbf{return}\ \mathit{rindex}[ u ] - c\ \forall u \cr 5 & \textbf{procedure}\ \mathrm{visit}( u ) \cr 6 & \quad \mathit{root}[ u ] \gets \textbf{true};\ \mathit{rindex}[ u ] \gets i;\ i \gets i + 1 \cr 7 & \quad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 8 & \qquad \textbf{if}\ \mathit{rindex}[ v ] = 0\ \textbf{then} \ \mathrm{visit}( v ) \cr 9 & \qquad \textbf{if}\ \mathit{rindex}[ v ] < \mathit{rindex}[ u ] \ \textbf{then}\ \mathit{rindex}[ u ] \gets \mathit{rindex}[ v ]; \ \mathit{root}[ u ] \gets \textbf{false} \cr 10 & \quad \textbf{if not}\ \mathit{root}[ u ]\ \textbf{then} \ \mathrm{push}( S , u );\ \textbf{return} \cr 11 & \quad i \gets i - 1 \cr 12 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{and} \ \mathit{rindex}[ u ] \le \mathit{rindex}[ \mathrm{top}( S ) ] \ \textbf{do} \cr 13 & \qquad \mathit{rindex}[ \mathrm{pop}( S ) ] \gets c;\ i \gets i - 1 \cr 14 & \quad \mathit{rindex}[ u ] \gets c;\ c \gets c - 1 \end{array}} \]

Postcondition
each node has a component, and an edge from the component \( C \) to \( C' \ne C \) gives \( C < C' \) (audit)

◆ sccTarjan()

template<Graph G>
std::vector< int > ADS::sccTarjan ( const G & g)

the strongly connected components of scc() by one visit in depth, with no transposed graph, numbered as there

In pseudocode (Tarjan's algorithm, beyond the book): \( \mathit{low}[ u ] \) is the least order of discovery of a node of the stack \( S \) that the subtree of \( u \) reaches by one more edge, and \( u \) is the first node of its component if and only if \( \mathit{low}[ u ] = \mathit{dt}[ u ] \) when \( u \) finishes (see Visits); the recursion is kept on an explicit stack.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sccTarjan}( G ) \cr 2 & \quad t \gets 0;\ k \gets 0;\ S \gets \text{an empty stack}; \ \mathit{dt}[ u ] \gets 0 , C[ u ] \gets 0\ \forall u \cr 3 & \quad \textbf{foreach}\ r \in V\ \textbf{with}\ \mathit{dt}[ r ] = 0\ \textbf{do}\ \mathrm{visit}( r ) \cr 4 & \quad \textbf{return}\ C\ \text{with each } c \text{ replaced by } k + 1 - c \cr 5 & \textbf{procedure}\ \mathrm{visit}( u ) \cr 6 & \quad t \gets t + 1;\ \mathit{dt}[ u ] \gets t;\ \mathit{low}[ u ] \gets t;\ \mathrm{push}( S , u ) \cr 7 & \quad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 8 & \qquad \textbf{if}\ \mathit{dt}[ v ] = 0\ \textbf{then} \ \mathrm{visit}( v );\ \mathit{low}[ u ] \gets \min( \mathit{low}[ u ] , \mathit{low}[ v ] ) \cr 9 & \qquad \textbf{else if}\ C[ v ] = 0\ \textbf{then}\ \mathit{low}[ u ] \gets \min( \mathit{low}[ u ] , \mathit{dt}[ v ] )\quad \triangleright\ v \in S \cr 10 & \quad \textbf{if}\ \mathit{low}[ u ] = \mathit{dt}[ u ] \ \textbf{then} \cr 11 & \qquad k \gets k + 1 \cr 12 & \qquad \textbf{repeat}\ v \gets \mathrm{pop}( S );\ C[ v ] \gets k \ \textbf{until}\ v = u \end{array}} \]

Postcondition
each node has a component, and an edge from the component \( C \) to \( C' \ne C \) gives \( C < C' \) (audit)