jacobi.template
Loading...
Searching...
No Matches
cuthill_mckee_ordering.cpp
Go to the documentation of this file.
1//=======================================================================
2// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
4//
5// This file is part of the Boost Graph Library
6//
7// You should have received a copy of the License Agreement for the
8// Boost Graph Library along with the software; see the file LICENSE.
9// If not, contact Office of Research, University of Notre Dame, Notre
10// Dame, IN 46556.
11//
12// Permission to modify the code and to distribute modified code is
13// granted, provided the text of this NOTICE is retained, a notice that
14// the code was modified is included with the above COPYRIGHT NOTICE and
15// with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
16// file is distributed with the modified code.
17//
18// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
19// By way of example, but not limitation, Licensor MAKES NO
20// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
21// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
22// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
23// OR OTHER RIGHTS.
24
25// Modyfied 2019 by Gundolf Haase, University of Graz
26//=======================================================================
28
29#include <boost/config.hpp>
30#include <boost/graph/adjacency_list.hpp>
31#include <boost/graph/bandwidth.hpp>
32#include <boost/graph/cuthill_mckee_ordering.hpp>
33#include <boost/graph/properties.hpp>
34#include <iostream>
35#include <vector>
36
37/*
38 Sample Output
39 original bandwidth: 8
40 Reverse Cuthill-McKee ordering starting at: 6
41 8 3 0 9 2 5 1 4 7 6
42 bandwidth: 4
43 Reverse Cuthill-McKee ordering starting at: 0
44 9 1 4 6 7 2 8 5 3 0
45 bandwidth: 4
46 Reverse Cuthill-McKee ordering:
47 0 8 5 7 3 6 4 2 1 9
48 bandwidth: 4
49 */
50 /*
51int main(int, char *[])
52{
53 using namespace boost;
54 using namespace std;
55 typedef adjacency_list<vecS, vecS, undirectedS,
56 property<vertex_color_t, default_color_type,
57 property<vertex_degree_t, int> > > Graph;
58 typedef graph_traits<Graph>::vertex_descriptor Vertex;
59 typedef graph_traits<Graph>::vertices_size_type size_type;
60
61 typedef std::pair<std::size_t, std::size_t> Pair;
62 Pair edges[14] = { Pair(0, 3), //a-d
63 Pair(0, 5), //a-f
64 Pair(1, 2), //b-c
65 Pair(1, 4), //b-e
66 Pair(1, 6), //b-g
67 Pair(1, 9), //b-j
68 Pair(2, 3), //c-d
69 Pair(2, 4), //c-e
70 Pair(3, 5), //d-f
71 Pair(3, 8), //d-i
72 Pair(4, 6), //e-g
73 Pair(5, 6), //f-g
74 Pair(5, 7), //f-h
75 Pair(6, 7)
76 }; //g-h
77
78 Graph G(10);
79 for (int i = 0; i < 14; ++i)
80 add_edge(edges[i].first, edges[i].second, G);
81
82 graph_traits<Graph>::vertex_iterator ui, ui_end;
83
84 property_map<Graph, vertex_degree_t>::type deg = get(vertex_degree, G);
85 for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
86 deg[*ui] = degree(*ui, G);
87
88 property_map<Graph, vertex_index_t>::type
89 index_map = get(vertex_index, G);
90
91 std::cout << "original bandwidth: " << bandwidth(G) << std::endl;
92
93 std::vector<Vertex> inv_perm(num_vertices(G));
94 std::vector<size_type> perm(num_vertices(G));
95 {
96 Vertex s = vertex(6, G);
97 //reverse cuthill_mckee_ordering
98 cuthill_mckee_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G),
99 get(vertex_degree, G));
100 cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl;
101 cout << " ";
102 for (std::vector<Vertex>::const_iterator i = inv_perm.begin();
103 i != inv_perm.end(); ++i)
104 cout << index_map[*i] << " ";
105 cout << endl;
106
107 for (size_type c = 0; c != inv_perm.size(); ++c)
108 perm[index_map[inv_perm[c]]] = c;
109 std::cout << " bandwidth: "
110 << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
111 << std::endl;
112 }
113 {
114 Vertex s = vertex(0, G);
115 //reverse cuthill_mckee_ordering
116 cuthill_mckee_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G),
117 get(vertex_degree, G));
118 cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl;
119 cout << " ";
120 for (std::vector<Vertex>::const_iterator i = inv_perm.begin();
121 i != inv_perm.end(); ++i)
122 cout << index_map[*i] << " ";
123 cout << endl;
124
125 for (size_type c = 0; c != inv_perm.size(); ++c)
126 perm[index_map[inv_perm[c]]] = c;
127 std::cout << " bandwidth: "
128 << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
129 << std::endl;
130 }
131
132 {
133 //reverse cuthill_mckee_ordering
134 cuthill_mckee_ordering(G, inv_perm.rbegin(), get(vertex_color, G),
135 make_degree_map(G));
136
137 cout << "Reverse Cuthill-McKee ordering:" << endl;
138 cout << " ";
139 for (std::vector<Vertex>::const_iterator i = inv_perm.begin();
140 i != inv_perm.end(); ++i)
141 cout << index_map[*i] << " ";
142 cout << endl;
143
144 for (size_type c = 0; c != inv_perm.size(); ++c)
145 perm[index_map[inv_perm[c]]] = c;
146 std::cout << " bandwidth: "
147 << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
148 << std::endl;
149 }
150 return 0;
151}
152*/
153
154
155// ------------- Modifications by Gundolf Haase
156// std::vector<int> _edges; //!< edges of mesh (vertices ordered ascending)
157
158using namespace boost;
159using namespace std;
160typedef adjacency_list<vecS, vecS, undirectedS,
161 property<vertex_color_t, default_color_type,
162 property<vertex_degree_t, int> > > Graph;
163typedef graph_traits<Graph>::vertex_descriptor Vertex;
164typedef graph_traits<Graph>::vertices_size_type size_type;
165
166typedef std::pair<std::size_t, std::size_t> Pair;
167
168vector<int> cuthill_mckee_reordering(vector<int> const &_edges)
169{
170 size_t const nnodes = *max_element(cbegin(_edges), cend(_edges)) + 1;
171 cout << "NNODES = " << nnodes << endl;
172 //size_t const nedges = _edges.size()/2;
173
174 Graph G(nnodes);
175 for (size_t i = 0; i < _edges.size(); i+=2)
176 add_edge(_edges[i], _edges[i+1], G);
177
178 graph_traits<Graph>::vertex_iterator ui, ui_end;
179
180 property_map<Graph, vertex_degree_t>::type deg = get(vertex_degree, G);
181 for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
182 deg[*ui] = degree(*ui, G);
183
184 property_map<Graph, vertex_index_t>::type
185 index_map = get(vertex_index, G);
186
187 std::cout << "original bandwidth: " << bandwidth(G) << std::endl;
188
189 std::vector<Vertex> inv_perm(num_vertices(G));
190 //std::vector<size_type> perm(num_vertices(G));
191 std::vector<int> perm(num_vertices(G));
192
193 {
194 Vertex s = vertex(nnodes/2, G);
195 //reverse cuthill_mckee_ordering
196 cuthill_mckee_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G),
197 get(vertex_degree, G));
198 //cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl;
199 //cout << " ";
200 //for (std::vector<Vertex>::const_iterator i = inv_perm.begin(); i != inv_perm.end(); ++i)
201 //cout << index_map[*i] << " ";
202 //cout << endl;
203
204 for (size_type c = 0; c != inv_perm.size(); ++c)
205 perm[index_map[inv_perm[c]]] = c;
206 std::cout << "improved bandwidth: "
207 << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
208 << std::endl;
209 }
210
211 assert(perm.size()==nnodes);
212
213 return perm;
214}
215
216// ------------- end Modifications
217
218
adjacency_list< vecS, vecS, undirectedS, property< vertex_color_t, default_color_type, property< vertex_degree_t, int > > > Graph
std::pair< std::size_t, std::size_t > Pair
graph_traits< Graph >::vertices_size_type size_type
graph_traits< Graph >::vertex_descriptor Vertex
vector< int > cuthill_mckee_reordering(vector< int > const &_edges)
id c()