File: incremental_components.html

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (421 lines) | stat: -rw-r--r-- 10,467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<HTML>
<!--
  -- Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
  --
  -- Distributed under the Boost Software License, Version 1.0.
  -- (See accompanying file LICENSE_1_0.txt or copy at
  -- http://www.boost.org/LICENSE_1_0.txt)
  -->
<Head>
<Title>Boost Graph Library: Incremental Connected Components</Title>
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 
        ALINK="#ff0000"> 
<IMG SRC="../../../boost.png" 
     ALT="C++ Boost" width="277" height="86"> 

<BR Clear>

<H1>Incremental Connected Components</H1>

<P>
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&nbsp;[<A
HREF="bibliography.html#clr90">8</A>,<A
HREF="bibliography.html#tarjan83:_data_struct_network_algo">27</A>]
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 <b><i>incremental</i></b><a
href="bibliography.html#eppstein97:dynamic_graph">[42]</a> (and not
fully dynamic). The disjoint-sets class is described in Section <A
HREF="../../disjoint_sets/disjoint_sets.html">Disjoint Sets</A>.

<P>
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 <TT>g</TT>, a disjoint-sets structure <TT>ds</TT>,
and vertices <TT>u</TT> and <TT>v</TT>.

<P>

<UL>
<LI><TT>initialize_incremental_components(g, ds)</TT> 
<BR>
Basic initialization of the disjoint-sets structure. Each
    vertex in the graph <TT>g</TT> is in its own set.
</LI>
<LI><TT>incremental_components(g, ds)</TT> 
<BR>
The connected components are calculated based on the edges in the graph
    <TT>g</TT> and the information is embedded in <TT>ds</TT>.
</LI>
<LI><TT>ds.find_set(v)</TT> 
<BR>
Extracts the component information for vertex <TT>v</TT> from the
    disjoint-sets.
</LI>
<LI><TT>ds.union_set(u, v)</TT> 
<BR>
Update the disjoint-sets structure when edge <i>(u,v)</i> is added to the graph.
</LI>
</UL>

<P>

<H3>Complexity</H3>

<P>
The time complexity for the whole process is <i>O(V + E
alpha(E,V))</i> where <i>E</i> is the total number of edges in the
graph (by the end of the process) and <i>V</i> is the number of
vertices.  <i>alpha</i> is the inverse of Ackermann's function which
has explosive recursively exponential growth. Therefore its inverse
function grows <I>very</I> slowly. For all practical purposes
<i>alpha(m,n) <= 4</i> which means the time complexity is only
slightly larger than <i>O(V + E)</i>.

<P>

<H3>Example</H3>

<P>
Maintain the connected components of a graph while adding edges using
the disjoint-sets data structure. The full source code for this
example can be found in <a
href="../example/incremental_components.cpp"><TT>examples/incremental_components.cpp</TT></a>.

<P>
<PRE>
typedef adjacency_list &lt;vecS, vecS, undirectedS&gt; Graph;
typedef graph_traits&lt;Graph&gt;::vertex_descriptor Vertex;
typedef graph_traits&lt;Graph&gt;::vertices_size_type size_type;

const int N = 6;
Graph G(N);

std::vector&lt;size_type&gt; rank(num_vertices(G));
std::vector&lt;Vertex&gt; parent(num_vertices(G));
typedef size_type* Rank;
typedef Vertex* Parent;
disjoint_sets&lt;Rank, Parent&gt;  ds(&amp;rank[0], &amp;parent[0]);

initialize_incremental_components(G, ds);
incremental_components(G, ds);

graph_traits&lt;Graph&gt;::edge_descriptor e;
bool flag;
boost::tie(e,flag) = add_edge(0, 1, G);
ds.union_set(0,1);

boost::tie(e,flag) = add_edge(1, 4, G);
ds.union_set(1,4);

boost::tie(e,flag) = add_edge(4, 0, G);
ds.union_set(4,0);

boost::tie(e,flag) = add_edge(2, 5, G);
ds.union_set(2,5);

cout &lt;&lt; &quot;An undirected graph:&quot; &lt;&lt; endl;
print_graph(G, get(vertex_index, G));
cout &lt;&lt; endl;

graph_traits&lt;Graph&gt;::vertex_iterator i,end;
for (boost::tie(i, end) = vertices(G); i != end; ++i)
  cout &lt;&lt; &quot;representative[&quot; &lt;&lt; *i &lt;&lt; &quot;] = &quot; &lt;&lt; 
    ds.find_set(*i) &lt;&lt; endl;;
cout &lt;&lt; endl;

typedef component_index&lt;unsigned int&gt; Components;
Components components(&amp;parent[0], &amp;parent[0] + parent.size());

for (Components::size_type c = 0; c &lt; components.size(); ++c) {
  cout &lt;&lt; &quot;component &quot; &lt;&lt; c &lt;&lt; &quot; contains: &quot;;
  Components::value_type::iterator
    j = components[c].begin(),
    jend = components[c].end();
  for ( ; j != jend; ++j)
    cout &lt;&lt; *j &lt;&lt; &quot; &quot;;
  cout &lt;&lt; endl;
}
</PRE>

<P>
<hr>
<p>

<H2><A NAME="sec:initialize-incremental-components"></A>
<TT>initialize_incremental_components</TT>
</H2>

<P>
<DIV ALIGN="left">
<TABLE CELLPADDING=3 border>
<TR><TH ALIGN="LEFT"><B>Graphs:</B></TH>
<TD ALIGN="LEFT">undirected</TD>
</TR>
<TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
<TD ALIGN="LEFT">rank, parent (in disjoint-sets)</TD>
</TR>
<TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
<TD></TD>
</TR>
</TABLE>
</DIV>

<P>
<PRE>
template &lt;class VertexListGraph, class DisjointSets&gt; 
void initialize_incremental_components(VertexListGraph&amp; G, DisjointSets&amp; ds)
</PRE>

<P>
This 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).

<P>

<H3>Where Defined</H3>

<P>
<a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>

<p>
<hr>
<P>

<H2><A NAME="sec:incremental-components"></A>
<TT>incremental_components</TT>
</H2>

<P>
<DIV ALIGN="left">
<TABLE CELLPADDING=3 border>
<TR><TH ALIGN="LEFT"><B>Graphs:</B></TH>
<TD ALIGN="LEFT">undirected</TD>
</TR>
<TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
<TD ALIGN="LEFT">rank, parent (in disjoint-sets)</TD>
</TR>
<TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
<TD ALIGN="LEFT"><i>O(E)</i></TD>
</TR>
</TABLE>
</DIV>

<p>
<PRE>
template &lt;class EdgeListGraph, class DisjointSets&gt;
void incremental_components(EdgeListGraph&amp; g, DisjointSets&amp; ds)
</PRE>

<P>
This function calculates the connected components of the graph,
embedding the results in the disjoint-sets data structure.

<P>

<H3>Where Defined</H3>

<P>
<a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>

<P>

<H3>Requirements on Types</H3>

<P>

<UL>
<LI>The graph type must be a model of <a href="./EdgeListGraph.html">EdgeListGraph</a>.
</LI>
</UL>

<P>
<hr>
<p>

<H2><A NAME="sec:same-component">
<TT>same_component</TT></A>
</H2>

<P>
<DIV ALIGN="left">
<TABLE CELLPADDING=3 border>
<TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
<TD ALIGN="LEFT">rank, parent (in disjoint-sets)</TD>
</TR>
<TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
<TD ALIGN="LEFT"><i>O(alpha(E,V))</i></TD>
</TR>
</TABLE>
</DIV>

<P>
<PRE>
template &lt;class Vertex, class DisjointSet&gt;
bool same_component(Vertex u, Vertex v, DisjointSet&amp; ds)
</PRE>

<P>
This function determines whether <TT>u</TT> and <TT>v</TT> are in the same
component.

<P>

<H3>Where Defined</H3>

<P>
<a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>

<P>

<H3>Requirements on Types</H3>

<P>

<UL>
<LI><TT>Vertex</TT> must be compatible with the rank and parent
    property maps of the <TT>DisjointSets</TT> data structure.
</LI>
</UL>

<P>
<hr>
<p>

<H2><A NAME="sec:component-index"></A>
<TT>component_index</TT>
</H2>

<p>
<PRE>
component_index&lt;Index&gt;
</PRE>

<P>
The is a class that provides an STL container-like view for the
components of the graph. Each component is a container-like object,
and the <TT>component_index</TT> object provides access to the
component objects via <TT>operator[]</TT>. A <TT>component_index</TT>
object is initialized with the parents property in the disjoint-sets
calculated from the <TT>incremental_components()</TT> function.

<P>

<H3>Where Defined</H3>

<P>
<a href="../../../boost/graph/incremental_components.hpp"><TT>boost/graph/incremental_components.hpp</TT></a>

<P>

<H3>Members</H3>

<P>
 
<table border>

<tr>
<th>Member</th> <th>Description</th>
</tr>

<tr>
<td><tt>size_type</tt></td>
<td>The type used for representing the number of components.</td>
</tr>


<tr>
<td><tt>value_type</tt></td>
<td>
The type for a component object. The component type has the following members.
</td>
</tr>
 
<tr>
<td><tt>value_type::value_type</tt></td>
<td>
The value type of a component object is a vertex ID.
</td>
</tr>

<tr>
<td><tt>value_type::iterator</tt></td>
<td>
This iterator can be used to traverse all of the vertices
in the component. This iterator dereferences to give a vertex ID.
</td>
</tr>

<tr>
<td><tt>value_type::const_iterator</tt></td>
<td>
The const iterator.
</td>
</tr>

<tr>
<td><tt>value_type::iterator value_type::begin() const</tt></td>
<td>
Return an iterator pointing to the first vertex in the component.
</td>
</tr>

<tr>
<td><tt>value_type::iterator value_type::end() const</tt></td>
<td>
Return an iterator pointing past the end of the last vertex in the
component.
</td>
<tr>

<tr>
<td>
<tt>
template &lt;class ComponentsContainer&gt;
component_index(const ComponentsContainer&amp; c)
</tt>
</td>
<td>
Construct the <TT>component_index</TT> using the information
from the components container <TT>c</TT> which was the result
of executing <TT>connected_components_on_edgelist</TT>.
</td>
</tr>

<tr>
<td><tt>value_type operator[](size_type i)</tt></td>
<td>
Returns the <TT>i</TT>th component in the graph.
</td>
</tr>

<tr>
<td><tt>size_type component_index::size()</tt></td>
<td>
Returns the number of components in the graph.
</td>

</table> 

<br>
<HR>
<TABLE>
<TR valign=top>
<TD nowrap>Copyright &copy 2000-2001</TD><TD>
<A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
Indiana University (<A
HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
<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>
<A HREF=http://www.osl.iu.edu/~lums>Andrew Lumsdaine</A>,
Indiana University (<A
HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
</TD></TR></TABLE>

</BODY>
</HTML>