king_ordering.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. #include <boost/config.hpp>
  11. #include <vector>
  12. #include <iostream>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/graph/king_ordering.hpp>
  15. #include <boost/graph/properties.hpp>
  16. #include <boost/graph/bandwidth.hpp>
  17. /*
  18. Sample Output
  19. original bandwidth: 8
  20. Reverse Cuthill-McKee ordering starting at: 6
  21. 8 3 0 9 2 5 1 4 7 6
  22. bandwidth: 4
  23. Reverse Cuthill-McKee ordering starting at: 0
  24. 9 1 4 6 7 2 8 5 3 0
  25. bandwidth: 4
  26. Reverse Cuthill-McKee ordering:
  27. 0 8 5 7 3 6 4 2 1 9
  28. bandwidth: 4
  29. */
  30. int main(int , char* [])
  31. {
  32. using namespace boost;
  33. using namespace std;
  34. typedef adjacency_list<vecS, vecS, undirectedS,
  35. property<vertex_color_t, default_color_type,
  36. property<vertex_degree_t,int> > > Graph;
  37. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  38. typedef graph_traits<Graph>::vertices_size_type size_type;
  39. typedef std::pair<std::size_t, std::size_t> Pair;
  40. Pair edges[14] = { Pair(0,3), //a-d
  41. Pair(0,5), //a-f
  42. Pair(1,2), //b-c
  43. Pair(1,4), //b-e
  44. Pair(1,6), //b-g
  45. Pair(1,9), //b-j
  46. Pair(2,3), //c-d
  47. Pair(2,4), //c-e
  48. Pair(3,5), //d-f
  49. Pair(3,8), //d-i
  50. Pair(4,6), //e-g
  51. Pair(5,6), //f-g
  52. Pair(5,7), //f-h
  53. Pair(6,7) }; //g-h
  54. Graph G(10);
  55. for (int i = 0; i < 14; ++i)
  56. add_edge(edges[i].first, edges[i].second, G);
  57. graph_traits<Graph>::vertex_iterator ui, ui_end;
  58. property_map<Graph,vertex_degree_t>::type deg = get(vertex_degree, G);
  59. for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
  60. deg[*ui] = degree(*ui, G);
  61. property_map<Graph, vertex_index_t>::type
  62. index_map = get(vertex_index, G);
  63. std::cout << "original bandwidth: " << bandwidth(G) << std::endl;
  64. std::vector<Vertex> inv_perm(num_vertices(G));
  65. std::vector<size_type> perm(num_vertices(G));
  66. {
  67. Vertex s = vertex(6, G);
  68. //king_ordering
  69. king_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G),
  70. get(vertex_degree, G), get(vertex_index, G));
  71. cout << "King ordering starting at: " << s << endl;
  72. cout << " ";
  73. for (std::vector<Vertex>::const_iterator i = inv_perm.begin();
  74. i != inv_perm.end(); ++i)
  75. cout << index_map[*i] << " ";
  76. cout << endl;
  77. for (size_type c = 0; c != inv_perm.size(); ++c)
  78. perm[index_map[inv_perm[c]]] = c;
  79. std::cout << " bandwidth: "
  80. << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  81. << std::endl;
  82. }
  83. {
  84. Vertex s = vertex(0, G);
  85. //king_ordering
  86. king_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G),
  87. get(vertex_degree, G), get(vertex_index, G));
  88. cout << "King ordering starting at: " << s << endl;
  89. cout << " ";
  90. for (std::vector<Vertex>::const_iterator i=inv_perm.begin();
  91. i != inv_perm.end(); ++i)
  92. cout << index_map[*i] << " ";
  93. cout << endl;
  94. for (size_type c = 0; c != inv_perm.size(); ++c)
  95. perm[index_map[inv_perm[c]]] = c;
  96. std::cout << " bandwidth: "
  97. << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  98. << std::endl;
  99. }
  100. {
  101. //king_ordering
  102. king_ordering(G, inv_perm.rbegin(), get(vertex_color, G),
  103. make_degree_map(G), get(vertex_index, G));
  104. cout << "King ordering:" << endl;
  105. cout << " ";
  106. for (std::vector<Vertex>::const_iterator i=inv_perm.begin();
  107. i != inv_perm.end(); ++i)
  108. cout << index_map[*i] << " ";
  109. cout << endl;
  110. for (size_type c = 0; c != inv_perm.size(); ++c)
  111. perm[index_map[inv_perm[c]]] = c;
  112. std::cout << " bandwidth: "
  113. << bandwidth(G, make_iterator_property_map(&perm[0], index_map, perm[0]))
  114. << std::endl;
  115. }
  116. return 0;
  117. }