Incremental Connected Components
A family of functions and classes for computing connected components of an undirected graph incrementally using disjoint-sets.
Complexity: O(V + E * alpha(E, V))
Defined in: <boost/graph/incremental_components.hpp>
Example
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/incremental_components.hpp>
#include <boost/pending/disjoint_sets.hpp>
int main() {
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
Graph g(5);
std::vector<std::size_t> rank(5, 0);
std::vector<std::size_t> parent(5);
boost::disjoint_sets<std::size_t*, std::size_t*> ds(rank.data(), parent.data());
for (std::size_t i = 0; i < 5; ++i) ds.make_set(i);
auto add_and_report = [&](int u, int v) {
boost::add_edge(u, v, g);
ds.union_set(static_cast<std::size_t>(u), static_cast<std::size_t>(v));
// Count components
std::size_t count = 0;
for (std::size_t i = 0; i < 5; ++i)
if (ds.find_set(i) == i) ++count;
std::cout << " After adding " << u << "-" << v << ": " << count << " components\n";
};
std::cout << "Incremental connected components:\n";
add_and_report(0, 1);
add_and_report(2, 3);
add_and_report(1, 2);
add_and_report(3, 4);
}
Incremental connected components:
After adding 0-1: 4 components
After adding 2-3: 3 components
After adding 1-2: 2 components
After adding 3-4: 1 components
initialize_incremental_components
template <class VertexListGraph,
class DisjointSets>
void initialize_incremental_components(
VertexListGraph& G,
DisjointSets& ds);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Vertex List Graph. |
IN/OUT |
|
A disjoint-sets data structure. Must provide a |
incremental_components
Complexity: O(E)
template <class EdgeListGraph,
class DisjointSets>
void incremental_components(
EdgeListGraph& g,
DisjointSets& ds);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
An undirected graph. The graph type must be a model of Edge List Graph. |
IN/OUT |
|
A disjoint-sets data structure. Must provide a |
same_component
Complexity: O(alpha(E,V))
template <class Vertex,
class DisjointSet>
bool same_component(
Vertex u,
Vertex v,
DisjointSet& ds);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
The first vertex. |
IN |
|
The second vertex. |
IN |
|
A disjoint-sets data structure. Must provide a |
component_index
component_index<Index>
The component_index class provides an STL container-like view for the components of the graph. Each component is a container-like object, and access is provided via the operator[]. A component_index object is initialized with the parents property in the disjoint-sets calculated from the incremental_components() function. Optionally, a vertex to index property map is passed in (identity_property_map is used by default).
| Member | Description |
|---|---|
|
The type for a component index (same as |
|
Returns the number of components in the graph. |
|
Iterators used to traverse the available component indices [0 to |
|
Returns an iterator at the start of the component indices (0). |
|
Returns an iterator past the end of the component indices ( |
|
Returns a pair of iterators for the component at |
Description
This section describes a family of functions and classes that work together to calculate the connected components of an undirected graph. The algorithm used here is based on the disjoint-sets (fast union-find) data structure [5,22] which is a good method to use for situations where the graph is growing (edges are being added) and the connected components information needs to be updated repeatedly. This method does not cover the situation where edges are both added and removed from the graph, hence it is called incremental [42] (and not fully dynamic). The disjoint-sets class is described in Section Disjoint Sets.
The following five operations are the primary functions that you will use to calculate and maintain the connected components. The objects used here are a graph g, a disjoint-sets structure ds, and vertices u and v.
-
initialize_incremental_components(g, ds)— Basic initialization of the disjoint-sets structure. Each vertex in the graphgis in its own set. -
incremental_components(g, ds)— The connected components are calculated based on the edges in the graphgand the information is embedded inds. -
ds.find_set(v)— Extracts the component information for vertexvfrom the disjoint-sets. -
ds.union_set(u, v)— Update the disjoint-sets structure when edge (u,v) is added to the graph.
initialize_incremental_components prepares the disjoint-sets data structure for the incremental connected components algorithm by making each vertex in the graph a member of its own component (or set).
incremental_components calculates the connected components of the graph, embedding the results in the disjoint-sets data structure.
same_component determines whether u and v are in the same component.