kuratowski_subgraph.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/boyer_myrvold_planar_test.hpp>
  16. #include <boost/graph/is_kuratowski_subgraph.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 K_6 (complete graph on 6 vertices), which
  29. // contains both Kuratowski subgraphs as minors.
  30. graph g(6);
  31. add_edge(0,1,g);
  32. add_edge(0,2,g);
  33. add_edge(0,3,g);
  34. add_edge(0,4,g);
  35. add_edge(0,5,g);
  36. add_edge(1,2,g);
  37. add_edge(1,3,g);
  38. add_edge(1,4,g);
  39. add_edge(1,5,g);
  40. add_edge(2,3,g);
  41. add_edge(2,4,g);
  42. add_edge(2,5,g);
  43. add_edge(3,4,g);
  44. add_edge(3,5,g);
  45. add_edge(4,5,g);
  46. // Initialize the interior edge index
  47. property_map<graph, edge_index_t>::type e_index = get(edge_index, g);
  48. graph_traits<graph>::edges_size_type edge_count = 0;
  49. graph_traits<graph>::edge_iterator ei, ei_end;
  50. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  51. put(e_index, *ei, edge_count++);
  52. // Test for planarity - we know it is not planar, we just want to
  53. // compute the kuratowski subgraph as a side-effect
  54. typedef std::vector< graph_traits<graph>::edge_descriptor >
  55. kuratowski_edges_t;
  56. kuratowski_edges_t kuratowski_edges;
  57. if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  58. boyer_myrvold_params::kuratowski_subgraph =
  59. std::back_inserter(kuratowski_edges)
  60. )
  61. )
  62. std::cout << "Input graph is planar" << std::endl;
  63. else
  64. {
  65. std::cout << "Input graph is not planar" << std::endl;
  66. std::cout << "Edges in the Kuratowski subgraph: ";
  67. kuratowski_edges_t::iterator ki, ki_end;
  68. ki_end = kuratowski_edges.end();
  69. for(ki = kuratowski_edges.begin(); ki != ki_end; ++ki)
  70. {
  71. std::cout << *ki << " ";
  72. }
  73. std::cout << std::endl;
  74. std::cout << "Is a kuratowski subgraph? ";
  75. if (is_kuratowski_subgraph
  76. (g, kuratowski_edges.begin(), kuratowski_edges.end())
  77. )
  78. std::cout << "Yes." << std::endl;
  79. else
  80. std::cout << "No." << std::endl;
  81. }
  82. return 0;
  83. }