//======================================================================= // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek // // 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) //======================================================================= #include #include #include #include #include #include #include #include #include /* This example shows how to use the disjoint set data structure to compute the connected components of an undirected, changing graph. Sample output: An undirected graph: 0 <--> 1 4 1 <--> 0 4 2 <--> 5 3 <--> 4 <--> 1 0 5 <--> 2 representative[0] = 1 representative[1] = 1 representative[2] = 5 representative[3] = 3 representative[4] = 1 representative[5] = 5 component 0 contains: 4 1 0 component 1 contains: 3 component 2 contains: 5 2 */ using namespace std; int main(int , char* []) { using namespace boost; typedef adjacency_list Graph; typedef graph_traits::vertex_descriptor Vertex; typedef graph_traits::vertices_size_type size_type; const int N = 6; Graph G(N); std::vector rank(num_vertices(G)); std::vector parent(num_vertices(G)); typedef size_type* Rank; typedef Vertex* Parent; disjoint_sets ds(&rank[0], &parent[0]); initialize_incremental_components(G, ds); incremental_components(G, ds); graph_traits::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 << "An undirected graph:" << endl; print_graph(G, get(vertex_index, G)); cout << endl; graph_traits::vertex_iterator i,end; for (boost::tie(i, end) = vertices(G); i != end; ++i) cout << "representative[" << *i << "] = " << ds.find_set(*i) << endl;; cout << endl; typedef component_index Components; Components components(&parent[0], &parent[0] + parent.size()); for (Components::size_type c = 0; c < components.size(); ++c) { cout << "component " << c << " contains: "; Components::value_type::iterator j = components[c].begin(), jend = components[c].end(); for ( ; j != jend; ++j) cout << *j << " "; cout << endl; } return 0; }