sloan_ordering.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. //=======================================================================
  3. // Copyright 2002 Marc Wintermantel (wintermantel@imes.mavt.ethz.ch)
  4. // ETH Zurich, Center of Structure Technologies
  5. // (https://web.archive.org/web/20050307090307/http://www.structures.ethz.ch/)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //=======================================================================
  11. //
  12. #include <boost/config.hpp>
  13. #include <vector>
  14. #include <iostream>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/sloan_ordering.hpp>
  17. #include <boost/graph/properties.hpp>
  18. #include <boost/graph/bandwidth.hpp>
  19. #include <boost/graph/profile.hpp>
  20. #include <boost/graph/wavefront.hpp>
  21. using std::cout;
  22. using std::endl;
  23. /*
  24. Sample Output
  25. #####################################
  26. ### First light of sloan-ordering ###
  27. #####################################
  28. original bandwidth: 8
  29. original profile: 42
  30. original max_wavefront: 7
  31. original aver_wavefront: 4.2
  32. original rms_wavefront: 4.58258
  33. Starting vertex: 0
  34. Pseudoperipheral vertex: 9
  35. Pseudoperipheral radius: 4
  36. Sloan ordering starting at: 0
  37. 0 8 3 7 5 2 4 6 1 9
  38. bandwidth: 4
  39. profile: 28
  40. max_wavefront: 4
  41. aver_wavefront: 2.8
  42. rms_wavefront: 2.93258
  43. Sloan ordering without a start-vertex:
  44. 8 0 3 7 5 2 4 6 1 9
  45. bandwidth: 4
  46. profile: 27
  47. max_wavefront: 4
  48. aver_wavefront: 2.7
  49. rms_wavefront: 2.84605
  50. ###############################
  51. ### sloan-ordering finished ###
  52. ###############################
  53. */
  54. int main(int , char* [])
  55. {
  56. cout << endl;
  57. cout << "#####################################" << endl;
  58. cout << "### First light of sloan-ordering ###" << endl;
  59. cout << "#####################################" << endl << endl;
  60. using namespace boost;
  61. using namespace std;
  62. //Defining the graph type
  63. typedef adjacency_list<
  64. setS,
  65. vecS,
  66. undirectedS,
  67. property<
  68. vertex_color_t,
  69. default_color_type,
  70. property<
  71. vertex_degree_t,
  72. int,
  73. property<
  74. vertex_priority_t,
  75. double > > > > Graph;
  76. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  77. typedef graph_traits<Graph>::vertices_size_type size_type;
  78. typedef std::pair<std::size_t, std::size_t> Pair;
  79. Pair edges[14] = { Pair(0,3), //a-d
  80. Pair(0,5), //a-f
  81. Pair(1,2), //b-c
  82. Pair(1,4), //b-e
  83. Pair(1,6), //b-g
  84. Pair(1,9), //b-j
  85. Pair(2,3), //c-d
  86. Pair(2,4), //c-e
  87. Pair(3,5), //d-f
  88. Pair(3,8), //d-i
  89. Pair(4,6), //e-g
  90. Pair(5,6), //f-g
  91. Pair(5,7), //f-h
  92. Pair(6,7) }; //g-h
  93. //Creating a graph and adding the edges from above into it
  94. Graph G(10);
  95. for (int i = 0; i < 14; ++i)
  96. add_edge(edges[i].first, edges[i].second, G);
  97. //Creating two iterators over the vertices
  98. graph_traits<Graph>::vertex_iterator ui, ui_end;
  99. //Creating a property_map with the degrees of the degrees of each vertex
  100. property_map<Graph,vertex_degree_t>::type deg = get(vertex_degree, G);
  101. for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
  102. deg[*ui] = degree(*ui, G);
  103. //Creating a property_map for the indices of a vertex
  104. property_map<Graph, vertex_index_t>::type index_map = get(vertex_index, G);
  105. std::cout << "original bandwidth: " << bandwidth(G) << std::endl;
  106. std::cout << "original profile: " << profile(G) << std::endl;
  107. std::cout << "original max_wavefront: " << max_wavefront(G) << std::endl;
  108. std::cout << "original aver_wavefront: " << aver_wavefront(G) << std::endl;
  109. std::cout << "original rms_wavefront: " << rms_wavefront(G) << std::endl;
  110. //Creating a vector of vertices
  111. std::vector<Vertex> sloan_order(num_vertices(G));
  112. //Creating a vector of size_type
  113. std::vector<size_type> perm(num_vertices(G));
  114. {
  115. //Setting the start node
  116. Vertex s = vertex(0, G);
  117. int ecc; //defining a variable for the pseudoperipheral radius
  118. //Calculating the pseudoeperipheral node and radius
  119. Vertex e = pseudo_peripheral_pair(G, s, ecc, get(vertex_color, G), get(vertex_degree, G) );
  120. cout << endl;
  121. cout << "Starting vertex: " << s << endl;
  122. cout << "Pseudoperipheral vertex: " << e << endl;
  123. cout << "Pseudoperipheral radius: " << ecc << endl << endl;
  124. //Sloan ordering
  125. sloan_ordering(G, s, e, sloan_order.begin(), get(vertex_color, G),
  126. get(vertex_degree, G), get(vertex_priority, G));
  127. cout << "Sloan ordering starting at: " << s << endl;
  128. cout << " ";
  129. for (std::vector<Vertex>::const_iterator i = sloan_order.begin();
  130. i != sloan_order.end(); ++i)
  131. cout << index_map[*i] << " ";
  132. cout << endl;
  133. for (size_type c = 0; c != sloan_order.size(); ++c)
  134. perm[index_map[sloan_order[c]]] = c;
  135. std::cout << " bandwidth: "
  136. << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  137. << std::endl;
  138. std::cout << " profile: "
  139. << profile(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  140. << std::endl;
  141. std::cout << " max_wavefront: "
  142. << max_wavefront(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  143. << std::endl;
  144. std::cout << " aver_wavefront: "
  145. << aver_wavefront(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  146. << std::endl;
  147. std::cout << " rms_wavefront: "
  148. << rms_wavefront(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  149. << std::endl;
  150. }
  151. /////////////////////////////////////////////////
  152. //Version including finding a good starting point
  153. /////////////////////////////////////////////////
  154. {
  155. //sloan_ordering
  156. sloan_ordering(G, sloan_order.begin(),
  157. get(vertex_color, G),
  158. make_degree_map(G),
  159. get(vertex_priority, G) );
  160. cout << endl << "Sloan ordering without a start-vertex:" << endl;
  161. cout << " ";
  162. for (std::vector<Vertex>::const_iterator i=sloan_order.begin();
  163. i != sloan_order.end(); ++i)
  164. cout << index_map[*i] << " ";
  165. cout << endl;
  166. for (size_type c = 0; c != sloan_order.size(); ++c)
  167. perm[index_map[sloan_order[c]]] = c;
  168. std::cout << " bandwidth: "
  169. << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  170. << std::endl;
  171. std::cout << " profile: "
  172. << profile(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  173. << std::endl;
  174. std::cout << " max_wavefront: "
  175. << max_wavefront(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  176. << std::endl;
  177. std::cout << " aver_wavefront: "
  178. << aver_wavefront(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  179. << std::endl;
  180. std::cout << " rms_wavefront: "
  181. << rms_wavefront(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  182. << std::endl;
  183. }
  184. cout << endl;
  185. cout << "###############################" << endl;
  186. cout << "### sloan-ordering finished ###" << endl;
  187. cout << "###############################" << endl << endl;
  188. return 0;
  189. }