mirror of
https://github.com/boostorg/graph.git
synced 2026-01-26 06:32:17 +00:00
194 lines
5.5 KiB
HTML
194 lines
5.5 KiB
HTML
<HTML>
|
|
<!--
|
|
-- Copyright (c) Jeremy Siek 2000
|
|
--
|
|
-- Permission to use, copy, modify, distribute and sell this software
|
|
-- and its documentation for any purpose is hereby granted without fee,
|
|
-- provided that the above copyright notice appears in all copies and
|
|
-- that both that copyright notice and this permission notice appear
|
|
-- in supporting documentation. Jeremy Siek makes no
|
|
-- representations about the suitability of this software for any
|
|
-- purpose. It is provided "as is" without express or implied warranty.
|
|
-->
|
|
<Head>
|
|
<Title>Boost Graph Library: Connected Components</Title>
|
|
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
|
|
ALINK="#ff0000">
|
|
<IMG SRC="../../../c++boost.gif"
|
|
ALT="C++ Boost" width="277" height="86">
|
|
|
|
<BR Clear>
|
|
|
|
|
|
<H1>
|
|
<A NAME="sec:connected-components"></A><A NAME="sec:strongly-connected-components"></A>
|
|
<TT>connected_components</TT>
|
|
</H1>
|
|
|
|
<P>
|
|
<DIV ALIGN="left">
|
|
<TABLE CELLPADDING=3 border>
|
|
<TR><TH ALIGN="LEFT"><B>Graphs:</B></TH>
|
|
<TD ALIGN="LEFT">see below</TD>
|
|
</TR>
|
|
<TR><TH ALIGN="LEFT"><B>Properties:</B></TH>
|
|
<TD ALIGN="LEFT">components, color</TD>
|
|
</TR>
|
|
<TR><TH ALIGN="LEFT"><B>Complexity:</B></TH>
|
|
<TD ALIGN="LEFT"><i>O(V + E)</i></TD>
|
|
</TR>
|
|
</TABLE>
|
|
</DIV>
|
|
|
|
<P>
|
|
<PRE>
|
|
(2)
|
|
template <class VertexListGraph, class ComponentMap, class ColorMap>
|
|
typename property_traits<ComponentMap>::value_type
|
|
connected_components(VertexListGraph& G, ComponentMap comp, ColorMap color);
|
|
|
|
(2)
|
|
template <class VertexListGraph,
|
|
class ComponentMap, class ColorMap, class DFSVisitor>
|
|
typename property_traits<ComponentMap>::value_type
|
|
connected_components(VertexListGraph& G, ComponentMap comp, ColorMap color,
|
|
DFSVisitor v);
|
|
</PRE>
|
|
|
|
<P>
|
|
The <TT>connected_components()</TT> functions compute the connected
|
|
components of an undirected graph using a DFS-based approach. If the
|
|
connected-components are to be calculated over and over while a graph
|
|
is changing the disjoint-set based approach of function
|
|
<TT>dynamic_connected_components()</TT> is faster. For ``static''
|
|
graphs this DFS-based approach is faster [<A
|
|
HREF="bibliography.html#clr90">8</A>].
|
|
</LI>
|
|
</UL>
|
|
|
|
<P>
|
|
The output of the algorithm is recorded in the component property
|
|
map <TT>comp</TT>, which will contain numbers giving the component ID
|
|
assigned to each vertex. The number of components is the return value
|
|
of the function.
|
|
|
|
<P>
|
|
The algorithm requires the use of several property maps: color,
|
|
discover time, and finish time. There are several versions of this
|
|
algorithm to accommodate whether you wish to use interior or exterior
|
|
property maps.
|
|
|
|
<P>
|
|
|
|
<H3>Where Defined</H3>
|
|
|
|
<P>
|
|
<a href="../../../boost/graph/connected_components.hpp"><TT>boost/graph/connected_components.hpp</TT></a>
|
|
|
|
<P>
|
|
|
|
<H3>Definitions</H3>
|
|
|
|
<P>
|
|
A <b><I>connected component</I></b> of an undirected graph is a set of
|
|
vertices that are all reachable from each other.
|
|
|
|
<P>
|
|
|
|
<H3>Requirements on Types</H3>
|
|
|
|
<P>
|
|
|
|
<UL>
|
|
<LI>The graph type must be a model of <a
|
|
href="./VertexListGraph.html">VertexListGraph</a>.
|
|
|
|
</LI>
|
|
<LI>The <TT>ComponentMap</TT> type must be a model of <a
|
|
href="../../property_map/WritablePropertyMap.html">WritablePropertyMap</a>. The
|
|
value type of the <TT>ComponentMap</TT> property map should be
|
|
an integer type, preferably the same as the <TT>size_type</TT> of
|
|
the graph. The key type should be the graph's vertex descriptor
|
|
type.
|
|
</LI>
|
|
<LI>The <TT>ColorMap</TT> map must be a <a
|
|
href="../../property_map/ReadWritePropertyMap.html">ReadWritePropertyMap</a>
|
|
and the graph's vertex descriptor type should be usable as the
|
|
map's key type. The value type of the map must be a
|
|
model of <I>ColorValue</I>.
|
|
</LI>
|
|
</UL>
|
|
|
|
<P>
|
|
|
|
<H3>Complexity</H3>
|
|
|
|
<P>
|
|
The time complexity for the connected components algorithm is also
|
|
<i>O(V + E)</i>.
|
|
|
|
<P>
|
|
|
|
<h3>See Also</h3>
|
|
|
|
<a href="./strong_components.html"><tt>strong_components()</tt></a>
|
|
and <a href="./incremental_components.html"><tt>incremental_components()</tt></a>
|
|
|
|
<H3>Example</H3>
|
|
|
|
<P>
|
|
Calculating the connected components of an undirected graph. The
|
|
complete source is in file <a
|
|
href="../example/connected_components.cpp"><tt>examples/connected_components.cpp</tt></a>.
|
|
|
|
<P>
|
|
<PRE>
|
|
typedef discover_time_property< finish_time_property
|
|
< color_property<> > > VertexProperty;
|
|
typedef adjacency_list <vecS, vecS, undirectedS, VertexProperty> Graph;
|
|
typedef graph_traits<Graph>::vertex_descriptor Vertex;
|
|
|
|
const int N = 6;
|
|
Graph G(N);
|
|
add_edge(0, 1, G);
|
|
add_edge(1, 4, G);
|
|
add_edge(4, 0, G);
|
|
add_edge(2, 5, G);
|
|
|
|
std::vector<int> c(num_vertices(G));
|
|
int num = connected_components(G, c.begin(),
|
|
get_color_map(G), null_visitor());
|
|
|
|
cout << endl;
|
|
std::vector<int>::iterator i;
|
|
cout << "Total number of components: " << num << endl;
|
|
for (i = c.begin(); i != c.end(); ++i)
|
|
cout << "Vertex " << i - c.begin()
|
|
<< " is in component " << *i << endl;
|
|
cout << endl;
|
|
</PRE>
|
|
The output is:
|
|
<PRE>
|
|
Total number of components: 3
|
|
Vertex 0 is in component 1
|
|
Vertex 1 is in component 1
|
|
Vertex 2 is in component 2
|
|
Vertex 3 is in component 3
|
|
Vertex 4 is in component 1
|
|
Vertex 5 is in component 2
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
<br>
|
|
<HR>
|
|
<TABLE>
|
|
<TR valign=top>
|
|
<TD nowrap>Copyright © 2000</TD><TD>
|
|
<A HREF="../../../people/jeremy_siek.htm">Jeremy Siek</A>, Univ.of Notre Dame (<A HREF="mailto:jsiek@lsc.nd.edu">jsiek@lsc.nd.edu</A>)
|
|
</TD></TR></TABLE>
|
|
|
|
</BODY>
|
|
</HTML>
|