1//=======================================================================
2// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
3//
4// Distributed under the Boost Software License, Version 1.0. (See
5// accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7//=======================================================================
8#include <boost/config.hpp>
9#include <iostream>
10#include <vector>
11#include <boost/graph/connected_components.hpp>
12#include <boost/graph/adjacency_list.hpp>
13
14int main()
15{
16 using namespace boost;
17 typedef adjacency_list< vecS, vecS, undirectedS > Graph;
18
19 const int N = 6;
20 Graph G(N);
21 add_edge(u: 0, v: 1, g_&: G);
22 add_edge(u: 1, v: 4, g_&: G);
23 add_edge(u: 4, v: 0, g_&: G);
24 add_edge(u: 2, v: 5, g_&: G);
25
26 std::vector< int > c(num_vertices(g_: G));
27 int num = connected_components(
28 g: G, c: make_iterator_property_map(iter: c.begin(), id: get(p: vertex_index, g&: G), c[0]));
29
30 std::cout << std::endl;
31 std::vector< int >::iterator i;
32 std::cout << "Total number of components: " << num << std::endl;
33 for (i = c.begin(); i != c.end(); ++i)
34 std::cout << "Vertex " << i - c.begin() << " is in component " << *i
35 << std::endl;
36 std::cout << std::endl;
37 return EXIT_SUCCESS;
38}
39

source code of boost/libs/graph/example/connected-components.cpp