[#sec:incremental-components]
= 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

[source,cpp]
----
include::example$algorithms/connected_components/incremental.cpp[]
----

[,text]
----
include::example$algorithms/connected_components/incremental.txt[]
----

'''

[#sec:initialize-incremental-components]
=== initialize_incremental_components

[source,cpp]
----
template <class VertexListGraph,
          class DisjointSets>
void initialize_incremental_components(
    VertexListGraph& G,
    DisjointSets& ds);
----

[cols="1,2,5"]
|===
| Direction | Parameter | Description

| IN | `VertexListGraph& G` | An undirected graph. The graph type must be a model of xref:concepts/VertexListGraph.adoc[Vertex List Graph].
| IN/OUT | `DisjointSets& ds` | A disjoint-sets data structure. Must provide a `make_set` operation. The rank and parent properties are embedded in this structure.
|===

'''

=== incremental_components

*Complexity:* _O(E)_

[source,cpp]
----
template <class EdgeListGraph,
          class DisjointSets>
void incremental_components(
    EdgeListGraph& g,
    DisjointSets& ds);
----

[cols="1,2,5"]
|===
| Direction | Parameter | Description

| IN | `EdgeListGraph& g` | An undirected graph. The graph type must be a model of xref:concepts/EdgeListGraph.adoc[Edge List Graph].
| IN/OUT | `DisjointSets& ds` | A disjoint-sets data structure. Must provide a `union_set` operation. The rank and parent properties are embedded in this structure.
|===

'''

[#sec:same-component]
=== same_component

*Complexity:* _O(alpha(E,V))_

[source,cpp]
----
template <class Vertex,
          class DisjointSet>
bool same_component(
    Vertex u,
    Vertex v,
    DisjointSet& ds);
----

[cols="1,2,5"]
|===
| Direction | Parameter | Description

| IN | `Vertex u` | The first vertex. `Vertex` must be compatible with the rank and parent property maps of the `DisjointSet` data structure.
| IN | `Vertex v` | The second vertex.
| IN | `DisjointSet& ds` | A disjoint-sets data structure. Must provide a `find_set` operation.
|===

'''

[#sec:component-index]
=== component_index

[source,cpp]
----
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).

[cols="1,3"]
|===
| Member | Description

| `value_type` / `size_type` | The type for a component index (same as `Index`).
| `size_type size()` | Returns the number of components in the graph.
| `iterator` / `const_iterator` | Iterators used to traverse the available component indices [0 to `size()`).
| `iterator begin() const` | Returns an iterator at the start of the component indices (0).
| `iterator end() const` | Returns an iterator past the end of the component indices (`size()`).
| `std::pair<component_iterator, component_iterator> operator[](size_type index) const` | Returns a pair of iterators for the component at `index` where `index` is in [0, `size()`).
|===

== 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
https://en.wikipedia.org/wiki/Disjoint-set_data_structure[disjoint-sets (fast union-find) data structure] [xref:about/bibliography.adoc#clr90[5],xref:about/bibliography.adoc#tarjan83:_data_struct_network_algo[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_* xref:about/bibliography.adoc#eppstein97:dynamic_graph[[42]] (and not fully dynamic). The disjoint-sets class is described in Section xref:algorithms/utility/disjoint_sets.adoc[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 graph `g` is in its own set.
* `incremental_components(g, ds)` -- The connected components are calculated based on the edges in the graph `g` and the information is embedded in `ds`.
* `ds.find_set(v)` -- Extracts the component information for vertex `v` from 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.

