1 | //======================================================================= |
---|
2 | // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. |
---|
3 | // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek |
---|
4 | // Doug Gregor, D. Kevin McGrath |
---|
5 | // |
---|
6 | // This file is part of the Boost Graph Library |
---|
7 | // |
---|
8 | // You should have received a copy of the License Agreement for the |
---|
9 | // Boost Graph Library along with the software; see the file LICENSE. |
---|
10 | // If not, contact Office of Research, University of Notre Dame, Notre |
---|
11 | // Dame, IN 46556. |
---|
12 | // |
---|
13 | // Permission to modify the code and to distribute modified code is |
---|
14 | // granted, provided the text of this NOTICE is retained, a notice that |
---|
15 | // the code was modified is included with the above COPYRIGHT NOTICE and |
---|
16 | // with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE |
---|
17 | // file is distributed with the modified code. |
---|
18 | // |
---|
19 | // LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. |
---|
20 | // By way of example, but not limitation, Licensor MAKES NO |
---|
21 | // REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY |
---|
22 | // PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS |
---|
23 | // OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS |
---|
24 | // OR OTHER RIGHTS. |
---|
25 | //======================================================================= |
---|
26 | |
---|
27 | #include <boost/config.hpp> |
---|
28 | #include <vector> |
---|
29 | #include <iostream> |
---|
30 | #include <boost/graph/adjacency_list.hpp> |
---|
31 | #include <boost/graph/king_ordering.hpp> |
---|
32 | #include <boost/graph/properties.hpp> |
---|
33 | #include <boost/graph/bandwidth.hpp> |
---|
34 | |
---|
35 | /* |
---|
36 | Sample Output |
---|
37 | original bandwidth: 8 |
---|
38 | Reverse Cuthill-McKee ordering starting at: 6 |
---|
39 | 8 3 0 9 2 5 1 4 7 6 |
---|
40 | bandwidth: 4 |
---|
41 | Reverse Cuthill-McKee ordering starting at: 0 |
---|
42 | 9 1 4 6 7 2 8 5 3 0 |
---|
43 | bandwidth: 4 |
---|
44 | Reverse Cuthill-McKee ordering: |
---|
45 | 0 8 5 7 3 6 4 2 1 9 |
---|
46 | bandwidth: 4 |
---|
47 | */ |
---|
48 | int main(int , char* []) |
---|
49 | { |
---|
50 | using namespace boost; |
---|
51 | using namespace std; |
---|
52 | typedef adjacency_list<vecS, vecS, undirectedS, |
---|
53 | property<vertex_color_t, default_color_type, |
---|
54 | property<vertex_degree_t,int> > > Graph; |
---|
55 | typedef graph_traits<Graph>::vertex_descriptor Vertex; |
---|
56 | typedef graph_traits<Graph>::vertices_size_type size_type; |
---|
57 | |
---|
58 | typedef std::pair<std::size_t, std::size_t> Pair; |
---|
59 | Pair edges[14] = { Pair(0,3), //a-d |
---|
60 | Pair(0,5), //a-f |
---|
61 | Pair(1,2), //b-c |
---|
62 | Pair(1,4), //b-e |
---|
63 | Pair(1,6), //b-g |
---|
64 | Pair(1,9), //b-j |
---|
65 | Pair(2,3), //c-d |
---|
66 | Pair(2,4), //c-e |
---|
67 | Pair(3,5), //d-f |
---|
68 | Pair(3,8), //d-i |
---|
69 | Pair(4,6), //e-g |
---|
70 | Pair(5,6), //f-g |
---|
71 | Pair(5,7), //f-h |
---|
72 | Pair(6,7) }; //g-h |
---|
73 | |
---|
74 | Graph G(10); |
---|
75 | for (int i = 0; i < 14; ++i) |
---|
76 | add_edge(edges[i].first, edges[i].second, G); |
---|
77 | |
---|
78 | graph_traits<Graph>::vertex_iterator ui, ui_end; |
---|
79 | |
---|
80 | property_map<Graph,vertex_degree_t>::type deg = get(vertex_degree, G); |
---|
81 | for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui) |
---|
82 | deg[*ui] = degree(*ui, G); |
---|
83 | |
---|
84 | property_map<Graph, vertex_index_t>::type |
---|
85 | index_map = get(vertex_index, G); |
---|
86 | |
---|
87 | std::cout << "original bandwidth: " << bandwidth(G) << std::endl; |
---|
88 | |
---|
89 | std::vector<Vertex> inv_perm(num_vertices(G)); |
---|
90 | std::vector<size_type> perm(num_vertices(G)); |
---|
91 | { |
---|
92 | Vertex s = vertex(6, G); |
---|
93 | //king_ordering |
---|
94 | king_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G), |
---|
95 | get(vertex_degree, G)); |
---|
96 | cout << "King ordering starting at: " << s << endl; |
---|
97 | cout << " "; |
---|
98 | for (std::vector<Vertex>::const_iterator i = inv_perm.begin(); |
---|
99 | i != inv_perm.end(); ++i) |
---|
100 | cout << index_map[*i] << " "; |
---|
101 | cout << endl; |
---|
102 | |
---|
103 | for (size_type c = 0; c != inv_perm.size(); ++c) |
---|
104 | perm[index_map[inv_perm[c]]] = c; |
---|
105 | std::cout << " bandwidth: " |
---|
106 | << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0])) |
---|
107 | << std::endl; |
---|
108 | } |
---|
109 | { |
---|
110 | Vertex s = vertex(0, G); |
---|
111 | //king_ordering |
---|
112 | king_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G), |
---|
113 | get(vertex_degree, G)); |
---|
114 | cout << "King ordering starting at: " << s << endl; |
---|
115 | cout << " "; |
---|
116 | for (std::vector<Vertex>::const_iterator i=inv_perm.begin(); |
---|
117 | i != inv_perm.end(); ++i) |
---|
118 | cout << index_map[*i] << " "; |
---|
119 | cout << endl; |
---|
120 | |
---|
121 | for (size_type c = 0; c != inv_perm.size(); ++c) |
---|
122 | perm[index_map[inv_perm[c]]] = c; |
---|
123 | std::cout << " bandwidth: " |
---|
124 | << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0])) |
---|
125 | << std::endl; |
---|
126 | } |
---|
127 | |
---|
128 | { |
---|
129 | //king_ordering |
---|
130 | king_ordering(G, inv_perm.rbegin(), get(vertex_color, G), |
---|
131 | make_degree_map(G)); |
---|
132 | |
---|
133 | cout << "King ordering:" << endl; |
---|
134 | cout << " "; |
---|
135 | for (std::vector<Vertex>::const_iterator i=inv_perm.begin(); |
---|
136 | i != inv_perm.end(); ++i) |
---|
137 | cout << index_map[*i] << " "; |
---|
138 | cout << endl; |
---|
139 | |
---|
140 | for (size_type c = 0; c != inv_perm.size(); ++c) |
---|
141 | perm[index_map[inv_perm[c]]] = c; |
---|
142 | std::cout << " bandwidth: " |
---|
143 | << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0])) |
---|
144 | << std::endl; |
---|
145 | } |
---|
146 | return 0; |
---|
147 | } |
---|