[12] | 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 <algorithm> |
---|
| 12 | #include <utility> |
---|
| 13 | #include <boost/graph/adjacency_list.hpp> |
---|
| 14 | #include <boost/pending/disjoint_sets.hpp> |
---|
| 15 | #include <boost/graph/incremental_components.hpp> |
---|
| 16 | |
---|
| 17 | int |
---|
| 18 | main(int, char *[]) |
---|
| 19 | { |
---|
| 20 | using namespace boost; |
---|
| 21 | // Create a graph |
---|
| 22 | typedef adjacency_list < vecS, vecS, undirectedS > Graph; |
---|
| 23 | typedef graph_traits < Graph >::vertex_descriptor Vertex; |
---|
| 24 | const int N = 6; |
---|
| 25 | Graph G(N); |
---|
| 26 | add_edge(0, 1, G); |
---|
| 27 | add_edge(1, 4, G); |
---|
| 28 | // create the disjoint-sets object, which requires rank and parent vertex properties |
---|
| 29 | std::vector < Vertex > rank(num_vertices(G)); |
---|
| 30 | std::vector < Vertex > parent(num_vertices(G)); |
---|
| 31 | typedef graph_traits<Graph>::vertices_size_type* Rank; |
---|
| 32 | typedef Vertex* Parent; |
---|
| 33 | disjoint_sets < Rank, Parent > ds(&rank[0], &parent[0]); |
---|
| 34 | |
---|
| 35 | // determine the connected components, storing the results in the disjoint-sets object |
---|
| 36 | initialize_incremental_components(G, ds); |
---|
| 37 | incremental_components(G, ds); |
---|
| 38 | |
---|
| 39 | // Add a couple more edges and update the disjoint-sets |
---|
| 40 | graph_traits < Graph >::edge_descriptor e; |
---|
| 41 | bool flag; |
---|
| 42 | tie(e, flag) = add_edge(4, 0, G); |
---|
| 43 | ds.union_set(4, 0); |
---|
| 44 | tie(e, flag) = add_edge(2, 5, G); |
---|
| 45 | ds.union_set(2, 5); |
---|
| 46 | |
---|
| 47 | graph_traits < Graph >::vertex_iterator iter, end; |
---|
| 48 | for (tie(iter, end) = vertices(G); iter != end; ++iter) |
---|
| 49 | std::cout << "representative[" << *iter << "] = " << |
---|
| 50 | ds.find_set(*iter) << std::endl;; |
---|
| 51 | std::cout << std::endl; |
---|
| 52 | |
---|
| 53 | typedef component_index < unsigned int >Components; |
---|
| 54 | Components components(parent.begin(), parent.end()); |
---|
| 55 | for (Components::size_type i = 0; i < components.size(); ++i) { |
---|
| 56 | std::cout << "component " << i << " contains: "; |
---|
| 57 | for (Components::value_type::iterator j = components[i].begin(); |
---|
| 58 | j != components[i].end(); ++j) |
---|
| 59 | std::cout << *j << " "; |
---|
| 60 | std::cout << std::endl; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | return EXIT_SUCCESS; |
---|
| 64 | } |
---|