canonical_ordering.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //=======================================================================
  2. // Copyright 2007 Aaron Windsor
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <iostream>
  9. #include <boost/graph/adjacency_list.hpp>
  10. #include <boost/graph/properties.hpp>
  11. #include <boost/graph/graph_traits.hpp>
  12. #include <boost/property_map/property_map.hpp>
  13. #include <boost/ref.hpp>
  14. #include <vector>
  15. #include <boost/graph/planar_canonical_ordering.hpp>
  16. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  17. using namespace boost;
  18. int main(int argc, char** argv)
  19. {
  20. typedef adjacency_list
  21. < vecS,
  22. vecS,
  23. undirectedS,
  24. property<vertex_index_t, int>,
  25. property<edge_index_t, int>
  26. >
  27. graph;
  28. // Create a maximal planar graph on 6 vertices
  29. graph g(6);
  30. add_edge(0,1,g);
  31. add_edge(1,2,g);
  32. add_edge(2,3,g);
  33. add_edge(3,4,g);
  34. add_edge(4,5,g);
  35. add_edge(5,0,g);
  36. add_edge(0,2,g);
  37. add_edge(0,3,g);
  38. add_edge(0,4,g);
  39. add_edge(1,3,g);
  40. add_edge(1,4,g);
  41. add_edge(1,5,g);
  42. // Initialize the interior edge index
  43. property_map<graph, edge_index_t>::type e_index = get(edge_index, g);
  44. graph_traits<graph>::edges_size_type edge_count = 0;
  45. graph_traits<graph>::edge_iterator ei, ei_end;
  46. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  47. put(e_index, *ei, edge_count++);
  48. // Test for planarity - we know it is planar, we just want to
  49. // compute the planar embedding as a side-effect
  50. typedef std::vector< graph_traits<graph>::edge_descriptor > vec_t;
  51. std::vector<vec_t> embedding(num_vertices(g));
  52. if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  53. boyer_myrvold_params::embedding =
  54. make_iterator_property_map(
  55. embedding.begin(), get(vertex_index, g))
  56. )
  57. )
  58. std::cout << "Input graph is planar" << std::endl;
  59. else
  60. std::cout << "Input graph is not planar" << std::endl;
  61. typedef std::vector<graph_traits<graph>::vertex_descriptor>
  62. ordering_storage_t;
  63. ordering_storage_t ordering;
  64. planar_canonical_ordering(g,
  65. make_iterator_property_map(
  66. embedding.begin(), get(vertex_index, g)),
  67. std::back_inserter(ordering));
  68. ordering_storage_t::iterator oi, oi_end;
  69. oi_end = ordering.end();
  70. std::cout << "The planar canonical ordering is: ";
  71. for(oi = ordering.begin(); oi != oi_end; ++oi)
  72. std::cout << *oi << " ";
  73. std::cout << std::endl;
  74. return 0;
  75. }