3 Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
5 Distributed under the Boost Software License, Version 1.0.
6 (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
10 <Title>Boost Graph Library: Incremental Connected Components
</Title>
11 <style type=
"text/css">
15 border-left-style: groove;
16 border-left-width: 1px;
23 <BODY BGCOLOR=
"#ffffff" LINK=
"#0000ee" TEXT=
"#000000" VLINK=
"#551a8b"
25 <IMG SRC=
"../../../boost.png"
26 ALT=
"C++ Boost" width=
"277" height=
"86">
30 <H1>Incremental Connected Components
</H1>
33 This section describes a family of functions and classes that work
34 together to calculate the connected components of an undirected graph.
35 The algorithm used here is based on the disjoint-sets (fast
36 union-find) data structure
[
<A
37 HREF=
"bibliography.html#clr90">8</A>,
<A
38 HREF=
"bibliography.html#tarjan83:_data_struct_network_algo">27</A>]
39 which is a good method to use for situations where the graph is
40 growing (edges are being added) and the connected components
41 information needs to be updated repeatedly. This method does not cover
42 the situation where edges are both added and removed from the graph,
43 hence it is called
<b><i>incremental
</i></b><a
44 href=
"bibliography.html#eppstein97:dynamic_graph">[
42]
</a> (and not
45 fully dynamic). The disjoint-sets class is described in Section
<A
46 HREF=
"../../disjoint_sets/disjoint_sets.html">Disjoint Sets
</A>.
49 The following five operations are the primary functions that you will
50 use to calculate and maintain the connected components. The objects
51 used here are a graph
<TT>g
</TT>, a disjoint-sets structure
<TT>ds
</TT>,
52 and vertices
<TT>u
</TT> and
<TT>v
</TT>.
57 <LI><TT>initialize_incremental_components(g, ds)
</TT>
59 Basic initialization of the disjoint-sets structure. Each
60 vertex in the graph
<TT>g
</TT> is in its own set.
62 <LI><TT>incremental_components(g, ds)
</TT>
64 The connected components are calculated based on the edges in the graph
65 <TT>g
</TT> and the information is embedded in
<TT>ds
</TT>.
67 <LI><TT>ds.find_set(v)
</TT>
69 Extracts the component information for vertex
<TT>v
</TT> from the
72 <LI><TT>ds.union_set(u, v)
</TT>
74 Update the disjoint-sets structure when edge
<i>(u,v)
</i> is added to the graph.
83 The time complexity for the whole process is
<i>O(V + E
84 alpha(E,V))
</i> where
<i>E
</i> is the total number of edges in the
85 graph (by the end of the process) and
<i>V
</i> is the number of
86 vertices.
<i>alpha
</i> is the inverse of Ackermann's function which
87 has explosive recursively exponential growth. Therefore its inverse
88 function grows
<I>very
</I> slowly. For all practical purposes
89 <i>alpha(m,n) <=
4</i> which means the time complexity is only
90 slightly larger than
<i>O(V + E)
</i>.
97 Maintain the connected components of a graph while adding edges using
98 the disjoint-sets data structure. The full source code for this
99 example can be found in
<a
100 href=
"../example/incremental_components.cpp"><TT>examples/incremental_components.cpp
</TT></a>.
104 using namespace boost;
106 int main(int argc, char* argv[])
108 typedef adjacency_list
<vecS, vecS, undirectedS
> Graph;
109 typedef graph_traits
<Graph>::vertex_descriptor Vertex;
110 typedef graph_traits
<Graph>::vertices_size_type VertexIndex;
112 const int VERTEX_COUNT =
6;
113 Graph graph(VERTEX_COUNT);
115 std::vector
<VertexIndex> rank(num_vertices(graph));
116 std::vector
<Vertex> parent(num_vertices(graph));
118 typedef VertexIndex* Rank;
119 typedef Vertex* Parent;
121 disjoint_sets
<Rank, Parent
> ds(&rank[
0], &parent[
0]);
123 initialize_incremental_components(graph, ds);
124 incremental_components(graph, ds);
126 graph_traits
<Graph>::edge_descriptor edge;
129 boost::tie(edge, flag) = add_edge(
0,
1, graph);
132 boost::tie(edge, flag) = add_edge(
1,
4, graph);
135 boost::tie(edge, flag) = add_edge(
4,
0, graph);
138 boost::tie(edge, flag) = add_edge(
2,
5, graph);
141 std::cout <<
"An undirected graph:" << std::endl;
142 print_graph(graph, get(boost::vertex_index, graph));
143 std::cout << std::endl;
145 BOOST_FOREACH(Vertex current_vertex, vertices(graph)) {
146 std::cout <<
"representative[" << current_vertex <<
"] = " <<
147 ds.find_set(current_vertex) << std::endl;
150 std::cout << std::endl;
152 typedef component_index
<VertexIndex> Components;
154 // NOTE: Because we're using vecS for the graph type, we're
155 // effectively using identity_property_map for a vertex index map.
156 // If we were to use listS instead, the index map would need to be
157 // explicity passed to the component_index constructor.
158 Components components(parent.begin(), parent.end());
160 // Iterate through the component indices
161 BOOST_FOREACH(VertexIndex current_index, components) {
162 std::cout <<
"component " << current_index <<
" contains: ";
164 // Iterate through the child vertex indices for [current_index]
165 BOOST_FOREACH(VertexIndex child_index,
166 components[current_index]) {
167 std::cout << child_index <<
" ";
170 std::cout << std::endl;
181 <H2><A NAME=
"sec:initialize-incremental-components"></A>
182 <TT>initialize_incremental_components
</TT>
187 <TABLE CELLPADDING=
3 border
>
188 <TR><TH ALIGN=
"LEFT"><B>Graphs:
</B></TH>
189 <TD ALIGN=
"LEFT">undirected
</TD>
191 <TR><TH ALIGN=
"LEFT"><B>Properties:
</B></TH>
192 <TD ALIGN=
"LEFT">rank, parent (in disjoint-sets)
</TD>
194 <TR><TH ALIGN=
"LEFT"><B>Complexity:
</B></TH>
202 template
<class VertexListGraph, class DisjointSets
>
203 void initialize_incremental_components(VertexListGraph
& G, DisjointSets
& ds)
207 This prepares the disjoint-sets data structure for the incremental
208 connected components algorithm by making each vertex in the graph a
209 member of its own component (or set).
213 <H3>Where Defined
</H3>
216 <a href=
"../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp
</TT></a>
222 <H2><A NAME=
"sec:incremental-components"></A>
223 <TT>incremental_components
</TT>
228 <TABLE CELLPADDING=
3 border
>
229 <TR><TH ALIGN=
"LEFT"><B>Graphs:
</B></TH>
230 <TD ALIGN=
"LEFT">undirected
</TD>
232 <TR><TH ALIGN=
"LEFT"><B>Properties:
</B></TH>
233 <TD ALIGN=
"LEFT">rank, parent (in disjoint-sets)
</TD>
235 <TR><TH ALIGN=
"LEFT"><B>Complexity:
</B></TH>
236 <TD ALIGN=
"LEFT"><i>O(E)
</i></TD>
243 template
<class EdgeListGraph, class DisjointSets
>
244 void incremental_components(EdgeListGraph
& g, DisjointSets
& ds)
248 This function calculates the connected components of the graph,
249 embedding the results in the disjoint-sets data structure.
253 <H3>Where Defined
</H3>
256 <a href=
"../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp
</TT></a>
260 <H3>Requirements on Types
</H3>
265 <LI>The graph type must be a model of
<a href=
"./EdgeListGraph.html">EdgeListGraph
</a>.
273 <H2><A NAME=
"sec:same-component">
274 <TT>same_component
</TT></A>
279 <TABLE CELLPADDING=
3 border
>
280 <TR><TH ALIGN=
"LEFT"><B>Properties:
</B></TH>
281 <TD ALIGN=
"LEFT">rank, parent (in disjoint-sets)
</TD>
283 <TR><TH ALIGN=
"LEFT"><B>Complexity:
</B></TH>
284 <TD ALIGN=
"LEFT"><i>O(alpha(E,V))
</i></TD>
291 template
<class Vertex, class DisjointSet
>
292 bool same_component(Vertex u, Vertex v, DisjointSet
& ds)
296 This function determines whether
<TT>u
</TT> and
<TT>v
</TT> are in the same
301 <H3>Where Defined
</H3>
304 <a href=
"../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp
</TT></a>
308 <H3>Requirements on Types
</H3>
313 <LI><TT>Vertex
</TT> must be compatible with the rank and parent
314 property maps of the
<TT>DisjointSets
</TT> data structure.
322 <H2><A NAME=
"sec:component-index"></A>
323 <TT>component_index
</TT>
328 component_index
<Index
>
332 The
<tt>component_index
</tt> class provides an STL
333 container-like view for the components of the graph. Each component is
334 a container-like object, and access is provided via
335 the
<TT>operator[]
</TT>. A
<TT>component_index
</TT> object is
336 initialized with the parents property in the disjoint-sets calculated
337 from the
<TT>incremental_components()
</TT> function. Optionally, a
338 vertex -
> index property map is passed in
339 (
<tt>identity_property_map
</tt> is used by default).
343 <H3>Where Defined
</H3>
346 <a href=
"../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp
</TT></a>
357 <th>Member
</th> <th>Description
</th>
361 <td><tt>value_type/size_type
</tt></td>
363 The type for a component index (same as
<tt>Index
</tt>).
368 <td><tt>size_type size()
</tt></td>
370 Returns the number of components in the graph.
376 <td><tt>iterator/const_iterator
</tt></td>
378 Iterators used to traverse the available component indices [
0 to
<tt>size()
</tt>).
383 <td><tt>iterator begin() const
</tt></td>
385 Returns an iterator at the start of the component indices (
0).
390 <td><tt>iterator end() const
</tt></td>
392 Returns an iterator past the end of the component indices (
<tt>size()
</tt>).
397 <td><tt>std::pair
<component_iterator, component_iterator
> operator[size_type index] const
</tt></td>
399 Returns a pair of iterators for the component at
<tt>index
</tt> where
<tt>index
</tt> is in [
0,
<tt>size()
</tt>).
409 <TD nowrap
>Copyright
© 2000-
2001</TD><TD>
410 <A HREF=
"http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek
</A>,
411 Indiana University (
<A
412 HREF=
"mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu
</A>)
<br>
413 <A HREF=
"http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee
</A>, Indiana University (
<A HREF=
"mailto:llee@cs.indiana.edu">llee@cs.indiana.edu
</A>)
<br>
414 <A HREF=
"http://www.osl.iu.edu/~lums">Andrew Lumsdaine
</A>,
415 Indiana University (
<A
416 HREF=
"mailto:lums@osl.iu.edu">lums@osl.iu.edu
</A>)