mcgregor_subgraphs_example.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //=======================================================================
  2. // Copyright 2009 Trustees of Indiana University.
  3. // Authors: Michael Hansen
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #include <fstream>
  10. #include <iostream>
  11. #include <string>
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/graph/filtered_graph.hpp>
  15. #include <boost/graph/graph_utility.hpp>
  16. #include <boost/graph/iteration_macros.hpp>
  17. #include <boost/graph/mcgregor_common_subgraphs.hpp>
  18. #include <boost/property_map/shared_array_property_map.hpp>
  19. using namespace boost;
  20. // Callback that looks for the first common subgraph whose size
  21. // matches the user's preference.
  22. template <typename Graph>
  23. struct example_callback {
  24. typedef typename graph_traits<Graph>::vertices_size_type VertexSizeFirst;
  25. example_callback(const Graph& graph1) :
  26. m_graph1(graph1) { }
  27. template <typename CorrespondenceMapFirstToSecond,
  28. typename CorrespondenceMapSecondToFirst>
  29. bool operator()(CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  30. CorrespondenceMapSecondToFirst correspondence_map_2_to_1,
  31. VertexSizeFirst subgraph_size) {
  32. // Fill membership map for first graph
  33. typedef typename property_map<Graph, vertex_index_t>::type VertexIndexMap;
  34. typedef shared_array_property_map<bool, VertexIndexMap> MembershipMap;
  35. MembershipMap membership_map1(num_vertices(m_graph1),
  36. get(vertex_index, m_graph1));
  37. fill_membership_map<Graph>(m_graph1, correspondence_map_1_to_2, membership_map1);
  38. // Generate filtered graphs using membership map
  39. typedef typename membership_filtered_graph_traits<Graph, MembershipMap>::graph_type
  40. MembershipFilteredGraph;
  41. MembershipFilteredGraph subgraph1 =
  42. make_membership_filtered_graph(m_graph1, membership_map1);
  43. // Print the graph out to the console
  44. std::cout << "Found common subgraph (size " << subgraph_size << ")" << std::endl;
  45. print_graph(subgraph1);
  46. std::cout << std::endl;
  47. // Explore the entire space
  48. return (true);
  49. }
  50. private:
  51. const Graph& m_graph1;
  52. VertexSizeFirst m_max_subgraph_size;
  53. };
  54. int main (int argc, char *argv[]) {
  55. // Using a vecS graph here so that we don't have to mess around with
  56. // a vertex index map; it will be implicit.
  57. typedef adjacency_list<listS, vecS, directedS,
  58. property<vertex_name_t, unsigned int,
  59. property<vertex_index_t, unsigned int> >,
  60. property<edge_name_t, unsigned int> > Graph;
  61. typedef property_map<Graph, vertex_name_t>::type VertexNameMap;
  62. // Test maximum and unique variants on known graphs
  63. Graph graph_simple1, graph_simple2;
  64. example_callback<Graph> user_callback(graph_simple1);
  65. VertexNameMap vname_map_simple1 = get(vertex_name, graph_simple1);
  66. VertexNameMap vname_map_simple2 = get(vertex_name, graph_simple2);
  67. // Graph that looks like a triangle
  68. put(vname_map_simple1, add_vertex(graph_simple1), 1);
  69. put(vname_map_simple1, add_vertex(graph_simple1), 2);
  70. put(vname_map_simple1, add_vertex(graph_simple1), 3);
  71. add_edge(0, 1, graph_simple1);
  72. add_edge(0, 2, graph_simple1);
  73. add_edge(1, 2, graph_simple1);
  74. std::cout << "First graph:" << std::endl;
  75. print_graph(graph_simple1);
  76. std::cout << std::endl;
  77. // Triangle with an extra vertex
  78. put(vname_map_simple2, add_vertex(graph_simple2), 1);
  79. put(vname_map_simple2, add_vertex(graph_simple2), 2);
  80. put(vname_map_simple2, add_vertex(graph_simple2), 3);
  81. put(vname_map_simple2, add_vertex(graph_simple2), 4);
  82. add_edge(0, 1, graph_simple2);
  83. add_edge(0, 2, graph_simple2);
  84. add_edge(1, 2, graph_simple2);
  85. add_edge(1, 3, graph_simple2);
  86. std::cout << "Second graph:" << std::endl;
  87. print_graph(graph_simple2);
  88. std::cout << std::endl;
  89. // All subgraphs
  90. std::cout << "mcgregor_common_subgraphs:" << std::endl;
  91. mcgregor_common_subgraphs
  92. (graph_simple1, graph_simple2, true, user_callback,
  93. vertices_equivalent(make_property_map_equivalent(vname_map_simple1, vname_map_simple2)));
  94. std::cout << std::endl;
  95. // Unique subgraphs
  96. std::cout << "mcgregor_common_subgraphs_unique:" << std::endl;
  97. mcgregor_common_subgraphs_unique
  98. (graph_simple1, graph_simple2, true, user_callback,
  99. vertices_equivalent(make_property_map_equivalent(vname_map_simple1, vname_map_simple2)));
  100. std::cout << std::endl;
  101. // Maximum subgraphs
  102. std::cout << "mcgregor_common_subgraphs_maximum:" << std::endl;
  103. mcgregor_common_subgraphs_maximum
  104. (graph_simple1, graph_simple2, true, user_callback,
  105. vertices_equivalent(make_property_map_equivalent(vname_map_simple1, vname_map_simple2)));
  106. std::cout << std::endl;
  107. // Maximum, unique subgraphs
  108. std::cout << "mcgregor_common_subgraphs_maximum_unique:" << std::endl;
  109. mcgregor_common_subgraphs_maximum_unique
  110. (graph_simple1, graph_simple2, true, user_callback,
  111. vertices_equivalent(make_property_map_equivalent(vname_map_simple1, vname_map_simple2)));
  112. return 0;
  113. }