make_maximal_planar_test.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <boost/graph/adjacency_list.hpp>
  9. #include <boost/graph/properties.hpp>
  10. #include <boost/graph/make_maximal_planar.hpp>
  11. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  12. #include <boost/property_map/property_map.hpp>
  13. #include <boost/property_map/vector_property_map.hpp>
  14. #include <boost/test/minimal.hpp>
  15. using namespace boost;
  16. template <typename Graph>
  17. void reset_edge_index(Graph& g)
  18. {
  19. typename property_map<Graph, edge_index_t>::type index = get(edge_index, g);
  20. typename graph_traits<Graph>::edge_iterator ei, ei_end;
  21. typename graph_traits<Graph>::edges_size_type cnt = 0;
  22. for(boost::tie(ei,ei_end) = edges(g); ei != ei_end; ++ei)
  23. put(index, *ei, cnt++);
  24. }
  25. template <typename Graph>
  26. void make_cycle(Graph& g, int size)
  27. {
  28. typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
  29. vertex_t first_vertex = add_vertex(g);
  30. vertex_t prev_vertex = first_vertex;
  31. for(int i = 1; i < size; ++i)
  32. {
  33. vertex_t curr_vertex = add_vertex(g);
  34. add_edge(curr_vertex, prev_vertex, g);
  35. prev_vertex = curr_vertex;
  36. }
  37. add_edge(first_vertex, prev_vertex, g);
  38. }
  39. struct UpdateVertexIndex
  40. {
  41. template <typename Graph>
  42. void update(Graph& g)
  43. {
  44. typename property_map<Graph, vertex_index_t>::type index = get(vertex_index, g);
  45. typename graph_traits<Graph>::vertex_iterator vi, vi_end;
  46. typename graph_traits<Graph>::vertices_size_type cnt = 0;
  47. for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
  48. put(index, *vi, cnt++);
  49. }
  50. };
  51. struct NoVertexIndexUpdater
  52. {
  53. template <typename Graph> void update(Graph&) {}
  54. };
  55. template <typename Graph, typename VertexIndexUpdater>
  56. void test_cycle(VertexIndexUpdater vertex_index_updater, int size)
  57. {
  58. Graph g;
  59. make_cycle(g, size);
  60. vertex_index_updater.update(g);
  61. reset_edge_index(g);
  62. typedef std::vector< typename graph_traits<Graph>::edge_descriptor > edge_vector_t;
  63. typedef std::vector< edge_vector_t > embedding_storage_t;
  64. typedef iterator_property_map
  65. < typename embedding_storage_t::iterator,
  66. typename property_map<Graph, vertex_index_t>::type
  67. > embedding_t;
  68. embedding_storage_t embedding_storage(num_vertices(g));
  69. embedding_t embedding(embedding_storage.begin(), get(vertex_index, g));
  70. typename graph_traits<Graph>::vertex_iterator vi, vi_end;
  71. for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
  72. std::copy(out_edges(*vi,g).first, out_edges(*vi,g).second, std::back_inserter(embedding[*vi]));
  73. BOOST_CHECK(boyer_myrvold_planarity_test(g));
  74. make_maximal_planar(g, embedding);
  75. reset_edge_index(g);
  76. // A graph is maximal planar exactly when it's both
  77. // planar and has 3 * num_vertices(g) - 6 edges.
  78. BOOST_CHECK(num_edges(g) == 3 * num_vertices(g) - 6);
  79. BOOST_CHECK(boyer_myrvold_planarity_test(g));
  80. }
  81. int test_main(int, char* [])
  82. {
  83. typedef adjacency_list
  84. <vecS,
  85. vecS,
  86. undirectedS,
  87. property<vertex_index_t, int>,
  88. property<edge_index_t, int>
  89. >
  90. VVgraph_t;
  91. typedef adjacency_list
  92. <vecS,
  93. listS,
  94. undirectedS,
  95. property<vertex_index_t, int>,
  96. property<edge_index_t, int>
  97. >
  98. VLgraph_t;
  99. typedef adjacency_list
  100. <listS,
  101. vecS,
  102. undirectedS,
  103. property<vertex_index_t, int>,
  104. property<edge_index_t, int>
  105. >
  106. LVgraph_t;
  107. typedef adjacency_list
  108. <listS,
  109. listS,
  110. undirectedS,
  111. property<vertex_index_t, int>,
  112. property<edge_index_t, int>
  113. >
  114. LLgraph_t;
  115. typedef adjacency_list
  116. <setS,
  117. setS,
  118. undirectedS,
  119. property<vertex_index_t, int>,
  120. property<edge_index_t, int>
  121. >
  122. SSgraph_t;
  123. test_cycle<VVgraph_t>(NoVertexIndexUpdater(), 10);
  124. test_cycle<VVgraph_t>(NoVertexIndexUpdater(), 50);
  125. test_cycle<VLgraph_t>(UpdateVertexIndex(), 3);
  126. test_cycle<VLgraph_t>(UpdateVertexIndex(), 30);
  127. test_cycle<LVgraph_t>(NoVertexIndexUpdater(), 15);
  128. test_cycle<LVgraph_t>(NoVertexIndexUpdater(), 45);
  129. test_cycle<LLgraph_t>(UpdateVertexIndex(), 8);
  130. test_cycle<LLgraph_t>(UpdateVertexIndex(), 19);
  131. test_cycle<SSgraph_t>(UpdateVertexIndex(), 13);
  132. test_cycle<SSgraph_t>(UpdateVertexIndex(), 20);
  133. return 0;
  134. }