strong_components
Computes the strongly connected components of a directed graph using Tarjan’s algorithm based on DFS.
Complexity: O(V + E)
Defined in: <boost/graph/strong_components.hpp>
Example
// Strongly Connected Components example
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/strong_components.hpp>
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>;
int main() {
Graph g{5};
// Cycle: 0->1->2->0, plus 2->3->4
boost::add_edge(0, 1, g); boost::add_edge(1, 2, g); boost::add_edge(2, 0, g);
boost::add_edge(2, 3, g); boost::add_edge(3, 4, g);
std::vector<int> comp(boost::num_vertices(g));
int n = boost::strong_components(g,
boost::make_iterator_property_map(comp.begin(), boost::get(boost::vertex_index, g)));
std::cout << "Number of strongly connected components: " << n << "\n";
for (std::size_t v = 0; v < comp.size(); ++v) {
std::cout << " Vertex " << v << " -> component " << comp[v] << "\n";
}
}
Number of strongly connected components: 3
Vertex 0 -> component 2
Vertex 1 -> component 2
Vertex 2 -> component 2
Vertex 3 -> component 1
Vertex 4 -> component 0
(1) Named parameter version
template <class Graph, class ComponentMap,
class P, class T, class R>
typename property_traits<ComponentMap>::value_type
strong_components(
Graph& g, ComponentMap comp,
const bgl_named_params<P, T, R>& params = all defaults);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed graph. The graph type must be a model of Vertex List Graph and Incidence Graph. |
OUT |
|
The algorithm computes how many connected components are in the graph, and assigning each component an integer label. The algorithm then records which component each vertex in the graph belongs to by recording the component number in the component property map. The |
- Named Parameters
| Direction | Parameter | Description |
|---|---|---|
UTIL |
|
This is used by the algorithm to record the candidate root vertex for each vertex. By the end of the algorithm, there is a single root vertex for each component and |
UTIL |
|
This is used by the algorithm to keep track of the DFS ordering of the vertices. The |
UTIL |
|
This is used by the algorithm to keep track of its progress through the graph. The type |
IN |
|
This maps each vertex to an integer in the range |
(2) Positional version
template <class Graph, class ComponentMap>
typename property_traits<ComponentMap>::value_type
strong_components(const Graph& g, ComponentMap comp);
Equivalent to (1) with every named parameter left at its default. Uses get(vertex_index, g) for the index map and internally allocated maps for the root / discover-time / color work arrays.
| Direction | Parameter | Description |
|---|---|---|
IN |
|
Same concept requirements as (1). |
OUT |
|
Same as (1). |
(3) kosaraju_strong_components
template <class Graph, class DFSVisitor, class ComponentsMap,
class DiscoverTime, class FinishTime, class ColorMap>
typename property_traits<ComponentsMap>::value_type
kosaraju_strong_components(
Graph& G, ComponentsMap c,
FinishTime finish_time, ColorMap color);
Alternate implementation using Kosaraju’s two-DFS-forest algorithm (CLR / Aho-Hopcroft-Ullman). Slower than Tarjan’s by a constant factor, uses more memory, and requires the graph to model Mutable Graph.
The template has six parameters but the function signature only
uses four — DFSVisitor and DiscoverTime are declared yet neither
deducible from the function arguments nor referenced in the body. Calling
this function therefore requires explicit template argument specification,
and there are no uses of kosaraju_strong_components anywhere in the
Boost tree itself. Prefer overloads (1) or (2) above.
|
| Direction | Parameter | Description |
|---|---|---|
IN/OUT |
|
A directed graph modelling Mutable Graph. The algorithm temporarily transposes the graph internally. |
OUT |
|
Per-vertex component index. Same requirements as |
UTIL |
|
Read/Write property map. Stores DFS finish timestamps used to order the second pass. |
UTIL |
|
Read/Write property map with |
Description
The strong_components() function computes the strongly connected components of a directed graph using
Tarjan’s algorithm based on DFS [36].
A strongly connected component of a directed graph G=(V,E) is a maximal set of vertices U which is in V such that for every pair of vertices u and v in U, we have both a path from u to v and path from v to u. That is to say that u and v are reachable from each other.
The output of the algorithm is recorded in the component property map comp, which will contain numbers giving the component ID assigned to each vertex. The number of components is the return value of the function.
Returns
The number of strongly connected components, as a value of type property_traits<ComponentMap>::value_type.