1 | //======================================================================= |
---|
2 | // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. |
---|
3 | // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek |
---|
4 | // |
---|
5 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
6 | // accompanying file LICENSE_1_0.txt or copy at |
---|
7 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | //======================================================================= |
---|
9 | #include <boost/config.hpp> |
---|
10 | #include <iostream> |
---|
11 | #include <vector> |
---|
12 | #include <algorithm> |
---|
13 | #include <utility> |
---|
14 | #include <boost/graph/graph_utility.hpp> |
---|
15 | #include <boost/graph/adjacency_list.hpp> |
---|
16 | #include <boost/pending/disjoint_sets.hpp> |
---|
17 | #include <boost/graph/incremental_components.hpp> |
---|
18 | |
---|
19 | /* |
---|
20 | |
---|
21 | This example shows how to use the disjoint set data structure |
---|
22 | to compute the connected components of an undirected, changing |
---|
23 | graph. |
---|
24 | |
---|
25 | Sample output: |
---|
26 | |
---|
27 | An undirected graph: |
---|
28 | 0 <--> 1 4 |
---|
29 | 1 <--> 0 4 |
---|
30 | 2 <--> 5 |
---|
31 | 3 <--> |
---|
32 | 4 <--> 1 0 |
---|
33 | 5 <--> 2 |
---|
34 | |
---|
35 | representative[0] = 1 |
---|
36 | representative[1] = 1 |
---|
37 | representative[2] = 5 |
---|
38 | representative[3] = 3 |
---|
39 | representative[4] = 1 |
---|
40 | representative[5] = 5 |
---|
41 | |
---|
42 | component 0 contains: 4 1 0 |
---|
43 | component 1 contains: 3 |
---|
44 | component 2 contains: 5 2 |
---|
45 | |
---|
46 | */ |
---|
47 | |
---|
48 | using namespace std; |
---|
49 | |
---|
50 | int main(int , char* []) |
---|
51 | { |
---|
52 | using namespace boost; |
---|
53 | typedef adjacency_list <vecS, vecS, undirectedS> Graph; |
---|
54 | typedef graph_traits<Graph>::vertex_descriptor Vertex; |
---|
55 | typedef graph_traits<Graph>::vertices_size_type size_type; |
---|
56 | |
---|
57 | const int N = 6; |
---|
58 | Graph G(N); |
---|
59 | |
---|
60 | std::vector<size_type> rank(num_vertices(G)); |
---|
61 | std::vector<Vertex> parent(num_vertices(G)); |
---|
62 | typedef size_type* Rank; |
---|
63 | typedef Vertex* Parent; |
---|
64 | disjoint_sets<Rank, Parent> ds(&rank[0], &parent[0]); |
---|
65 | |
---|
66 | initialize_incremental_components(G, ds); |
---|
67 | incremental_components(G, ds); |
---|
68 | |
---|
69 | graph_traits<Graph>::edge_descriptor e; |
---|
70 | bool flag; |
---|
71 | boost::tie(e,flag) = add_edge(0, 1, G); |
---|
72 | ds.union_set(0,1); |
---|
73 | |
---|
74 | boost::tie(e,flag) = add_edge(1, 4, G); |
---|
75 | ds.union_set(1,4); |
---|
76 | |
---|
77 | boost::tie(e,flag) = add_edge(4, 0, G); |
---|
78 | ds.union_set(4,0); |
---|
79 | |
---|
80 | boost::tie(e,flag) = add_edge(2, 5, G); |
---|
81 | ds.union_set(2,5); |
---|
82 | |
---|
83 | cout << "An undirected graph:" << endl; |
---|
84 | print_graph(G, get(vertex_index, G)); |
---|
85 | cout << endl; |
---|
86 | |
---|
87 | graph_traits<Graph>::vertex_iterator i,end; |
---|
88 | for (boost::tie(i, end) = vertices(G); i != end; ++i) |
---|
89 | cout << "representative[" << *i << "] = " << |
---|
90 | ds.find_set(*i) << endl;; |
---|
91 | cout << endl; |
---|
92 | |
---|
93 | typedef component_index<unsigned int> Components; |
---|
94 | Components components(&parent[0], &parent[0] + parent.size()); |
---|
95 | |
---|
96 | for (Components::size_type c = 0; c < components.size(); ++c) { |
---|
97 | cout << "component " << c << " contains: "; |
---|
98 | Components::value_type::iterator |
---|
99 | j = components[c].begin(), |
---|
100 | jend = components[c].end(); |
---|
101 | for ( ; j != jend; ++j) |
---|
102 | cout << *j << " "; |
---|
103 | cout << endl; |
---|
104 | } |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|