mcgregor_subgraphs_test.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <cmath>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <vector>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/random.hpp>
  16. #include <boost/graph/adjacency_list.hpp>
  17. #include <boost/graph/filtered_graph.hpp>
  18. #include <boost/graph/graphviz.hpp>
  19. #include <boost/graph/isomorphism.hpp>
  20. #include <boost/graph/iteration_macros.hpp>
  21. #include <boost/graph/random.hpp>
  22. #include <boost/graph/mcgregor_common_subgraphs.hpp>
  23. #include <boost/property_map/shared_array_property_map.hpp>
  24. #include <boost/test/minimal.hpp>
  25. bool was_common_subgraph_found = false, output_graphs = false;
  26. std::vector<std::string> simple_subgraph_list;
  27. // Callback that compares incoming graphs to the supplied common
  28. // subgraph.
  29. template <typename Graph>
  30. struct test_callback {
  31. test_callback(Graph& common_subgraph,
  32. const Graph& graph1,
  33. const Graph& graph2) :
  34. m_graph1(graph1),
  35. m_graph2(graph2),
  36. m_common_subgraph(common_subgraph) { }
  37. template <typename CorrespondenceMapFirstToSecond,
  38. typename CorrespondenceMapSecondToFirst>
  39. bool operator()(CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  40. CorrespondenceMapSecondToFirst correspondence_map_2_to_1,
  41. typename boost::graph_traits<Graph>::vertices_size_type subgraph_size) {
  42. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  43. typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
  44. typedef std::pair<Edge, bool> EdgeInfo;
  45. typedef typename boost::property_map<Graph, boost::vertex_index_t>::type VertexIndexMap;
  46. typedef typename boost::property_map<Graph, boost::vertex_name_t>::type VertexNameMap;
  47. typedef typename boost::property_map<Graph, boost::edge_name_t>::type EdgeNameMap;
  48. if (subgraph_size != num_vertices(m_common_subgraph)) {
  49. return (true);
  50. }
  51. // Fill membership maps for both graphs
  52. typedef boost::shared_array_property_map<bool, VertexIndexMap> MembershipMap;
  53. MembershipMap membership_map1(num_vertices(m_graph1),
  54. get(boost::vertex_index, m_graph1));
  55. MembershipMap membership_map2(num_vertices(m_graph2),
  56. get(boost::vertex_index, m_graph2));
  57. boost::fill_membership_map<Graph>(m_graph1, correspondence_map_1_to_2, membership_map1);
  58. boost::fill_membership_map<Graph>(m_graph2, correspondence_map_2_to_1, membership_map2);
  59. // Generate filtered graphs using membership maps
  60. typedef typename boost::membership_filtered_graph_traits<Graph, MembershipMap>::graph_type
  61. MembershipFilteredGraph;
  62. MembershipFilteredGraph subgraph1 =
  63. boost::make_membership_filtered_graph(m_graph1, membership_map1);
  64. MembershipFilteredGraph subgraph2 =
  65. boost::make_membership_filtered_graph(m_graph2, membership_map2);
  66. VertexIndexMap vindex_map1 = get(boost::vertex_index, subgraph1);
  67. VertexIndexMap vindex_map2 = get(boost::vertex_index, subgraph2);
  68. VertexNameMap vname_map_common = get(boost::vertex_name, m_common_subgraph);
  69. VertexNameMap vname_map1 = get(boost::vertex_name, subgraph1);
  70. VertexNameMap vname_map2 = get(boost::vertex_name, subgraph2);
  71. EdgeNameMap ename_map_common = get(boost::edge_name, m_common_subgraph);
  72. EdgeNameMap ename_map1 = get(boost::edge_name, subgraph1);
  73. EdgeNameMap ename_map2 = get(boost::edge_name, subgraph2);
  74. // Verify that subgraph1 matches the supplied common subgraph
  75. BGL_FORALL_VERTICES_T(vertex1, subgraph1, MembershipFilteredGraph) {
  76. Vertex vertex_common = vertex(get(vindex_map1, vertex1), m_common_subgraph);
  77. // Match vertex names
  78. if (get(vname_map_common, vertex_common) != get(vname_map1, vertex1)) {
  79. // Keep looking
  80. return (true);
  81. }
  82. BGL_FORALL_VERTICES_T(vertex1_2, subgraph1, MembershipFilteredGraph) {
  83. Vertex vertex_common2 = vertex(get(vindex_map1, vertex1_2), m_common_subgraph);
  84. EdgeInfo edge_common = edge(vertex_common, vertex_common2, m_common_subgraph);
  85. EdgeInfo edge1 = edge(vertex1, vertex1_2, subgraph1);
  86. if ((edge_common.second != edge1.second) ||
  87. ((edge_common.second && edge1.second) &&
  88. (get(ename_map_common, edge_common.first) != get(ename_map1, edge1.first)))) {
  89. // Keep looking
  90. return (true);
  91. }
  92. }
  93. } // BGL_FORALL_VERTICES_T (subgraph1)
  94. // Verify that subgraph2 matches the supplied common subgraph
  95. BGL_FORALL_VERTICES_T(vertex2, subgraph2, MembershipFilteredGraph) {
  96. Vertex vertex_common = vertex(get(vindex_map2, vertex2), m_common_subgraph);
  97. // Match vertex names
  98. if (get(vname_map_common, vertex_common) != get(vname_map2, vertex2)) {
  99. // Keep looking
  100. return (true);
  101. }
  102. BGL_FORALL_VERTICES_T(vertex2_2, subgraph2, MembershipFilteredGraph) {
  103. Vertex vertex_common2 = vertex(get(vindex_map2, vertex2_2), m_common_subgraph);
  104. EdgeInfo edge_common = edge(vertex_common, vertex_common2, m_common_subgraph);
  105. EdgeInfo edge2 = edge(vertex2, vertex2_2, subgraph2);
  106. if ((edge_common.second != edge2.second) ||
  107. ((edge_common.second && edge2.second) &&
  108. (get(ename_map_common, edge_common.first) != get(ename_map2, edge2.first)))) {
  109. // Keep looking
  110. return (true);
  111. }
  112. }
  113. } // BGL_FORALL_VERTICES_T (subgraph2)
  114. // Check isomorphism just to be thorough
  115. if (verify_isomorphism(subgraph1, subgraph2, correspondence_map_1_to_2)) {
  116. was_common_subgraph_found = true;
  117. if (output_graphs) {
  118. std::fstream file_subgraph("found_common_subgraph.dot", std::fstream::out);
  119. write_graphviz(file_subgraph, subgraph1,
  120. make_label_writer(get(boost::vertex_name, m_graph1)),
  121. make_label_writer(get(boost::edge_name, m_graph1)));
  122. }
  123. // Stop iterating
  124. return (false);
  125. }
  126. // Keep looking
  127. return (true);
  128. }
  129. private:
  130. const Graph& m_graph1, m_graph2;
  131. Graph& m_common_subgraph;
  132. };
  133. template <typename Graph>
  134. struct simple_callback {
  135. simple_callback(const Graph& graph1) :
  136. m_graph1(graph1) { }
  137. template <typename CorrespondenceMapFirstToSecond,
  138. typename CorrespondenceMapSecondToFirst>
  139. bool operator()(CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  140. CorrespondenceMapSecondToFirst /*correspondence_map_2_to_1*/,
  141. typename boost::graph_traits<Graph>::vertices_size_type /*subgraph_size*/) {
  142. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  143. std::stringstream subgraph_string;
  144. BGL_FORALL_VERTICES_T(vertex1, m_graph1, Graph) {
  145. Vertex vertex2 = get(correspondence_map_1_to_2, vertex1);
  146. if (vertex2 != boost::graph_traits<Graph>::null_vertex()) {
  147. subgraph_string << vertex1 << "," << vertex2 << " ";
  148. }
  149. }
  150. simple_subgraph_list.push_back(subgraph_string.str());
  151. return (true);
  152. }
  153. private:
  154. const Graph& m_graph1;
  155. };
  156. template <typename Graph,
  157. typename RandomNumberGenerator,
  158. typename VertexNameMap,
  159. typename EdgeNameMap>
  160. void add_random_vertices(Graph& graph, RandomNumberGenerator& generator,
  161. int vertices_to_create, int max_edges_per_vertex,
  162. VertexNameMap vname_map, EdgeNameMap ename_map) {
  163. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  164. typedef std::vector<Vertex> VertexList;
  165. VertexList new_vertices;
  166. for (int v_index = 0; v_index < vertices_to_create; ++v_index) {
  167. Vertex new_vertex = add_vertex(graph);
  168. put(vname_map, new_vertex, generator());
  169. new_vertices.push_back(new_vertex);
  170. }
  171. // Add edges for every new vertex. Care is taken to avoid parallel
  172. // edges.
  173. for (typename VertexList::const_iterator v_iter = new_vertices.begin();
  174. v_iter != new_vertices.end(); ++v_iter) {
  175. Vertex source_vertex = *v_iter;
  176. int edges_for_vertex = (std::min)((int)(generator() % max_edges_per_vertex) + 1,
  177. (int)num_vertices(graph));
  178. while (edges_for_vertex > 0) {
  179. Vertex target_vertex = random_vertex(graph, generator);
  180. if (source_vertex == target_vertex) {
  181. continue;
  182. }
  183. BGL_FORALL_OUTEDGES_T(source_vertex, edge, graph, Graph) {
  184. if (target(edge, graph) == target_vertex) {
  185. continue;
  186. }
  187. }
  188. put(ename_map, add_edge(source_vertex, target_vertex, graph).first,
  189. generator());
  190. edges_for_vertex--;
  191. }
  192. }
  193. }
  194. bool has_subgraph_string(std::string set_string) {
  195. return (std::find(simple_subgraph_list.begin(), simple_subgraph_list.end(),
  196. set_string) != simple_subgraph_list.end());
  197. }
  198. int test_main (int argc, char *argv[]) {
  199. int vertices_to_create = 10;
  200. int max_edges_per_vertex = 2;
  201. std::size_t random_seed = time(0);
  202. if (argc > 1) {
  203. vertices_to_create = boost::lexical_cast<int>(argv[1]);
  204. }
  205. if (argc > 2) {
  206. max_edges_per_vertex = boost::lexical_cast<int>(argv[2]);
  207. }
  208. if (argc > 3) {
  209. output_graphs = boost::lexical_cast<bool>(argv[3]);
  210. }
  211. if (argc > 4) {
  212. random_seed = boost::lexical_cast<std::size_t>(argv[4]);
  213. }
  214. boost::minstd_rand generator(random_seed);
  215. // Using a vecS graph here so that we don't have to mess around with
  216. // a vertex index map; it will be implicit.
  217. typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS,
  218. boost::property<boost::vertex_name_t, unsigned int,
  219. boost::property<boost::vertex_index_t, unsigned int> >,
  220. boost::property<boost::edge_name_t, unsigned int> > Graph;
  221. typedef boost::property_map<Graph, boost::vertex_name_t>::type VertexNameMap;
  222. typedef boost::property_map<Graph, boost::edge_name_t>::type EdgeNameMap;
  223. // Generate a random common subgraph and then add random vertices
  224. // and edges to the two parent graphs.
  225. Graph common_subgraph, graph1, graph2;
  226. VertexNameMap vname_map_common = get(boost::vertex_name, common_subgraph);
  227. VertexNameMap vname_map1 = get(boost::vertex_name, graph1);
  228. VertexNameMap vname_map2 = get(boost::vertex_name, graph2);
  229. EdgeNameMap ename_map_common = get(boost::edge_name, common_subgraph);
  230. EdgeNameMap ename_map1 = get(boost::edge_name, graph1);
  231. EdgeNameMap ename_map2 = get(boost::edge_name, graph2);
  232. for (int vindex = 0; vindex < vertices_to_create; ++vindex) {
  233. put(vname_map_common, add_vertex(common_subgraph), generator());
  234. }
  235. BGL_FORALL_VERTICES(source_vertex, common_subgraph, Graph) {
  236. BGL_FORALL_VERTICES(target_vertex, common_subgraph, Graph) {
  237. if (source_vertex != target_vertex) {
  238. put(ename_map_common,
  239. add_edge(source_vertex, target_vertex, common_subgraph).first,
  240. generator());
  241. }
  242. }
  243. }
  244. boost::randomize_property<boost::vertex_name_t>(common_subgraph, generator);
  245. boost::randomize_property<boost::edge_name_t>(common_subgraph, generator);
  246. boost::copy_graph(common_subgraph, graph1);
  247. boost::copy_graph(common_subgraph, graph2);
  248. // Randomly add vertices and edges to graph1 and graph2.
  249. add_random_vertices(graph1, generator, vertices_to_create,
  250. max_edges_per_vertex, vname_map1, ename_map1);
  251. add_random_vertices(graph2, generator, vertices_to_create,
  252. max_edges_per_vertex, vname_map2, ename_map2);
  253. if (output_graphs) {
  254. std::fstream file_graph1("graph1.dot", std::fstream::out),
  255. file_graph2("graph2.dot", std::fstream::out),
  256. file_common_subgraph("expected_common_subgraph.dot", std::fstream::out);
  257. write_graphviz(file_graph1, graph1,
  258. make_label_writer(vname_map1),
  259. make_label_writer(ename_map1));
  260. write_graphviz(file_graph2, graph2,
  261. make_label_writer(vname_map2),
  262. make_label_writer(ename_map2));
  263. write_graphviz(file_common_subgraph, common_subgraph,
  264. make_label_writer(get(boost::vertex_name, common_subgraph)),
  265. make_label_writer(get(boost::edge_name, common_subgraph)));
  266. }
  267. std::cout << "Searching for common subgraph of size " <<
  268. num_vertices(common_subgraph) << std::endl;
  269. test_callback<Graph> user_callback(common_subgraph, graph1, graph2);
  270. boost::mcgregor_common_subgraphs(graph1, graph2, true, user_callback,
  271. boost::edges_equivalent(boost::make_property_map_equivalent(ename_map1, ename_map2)).
  272. vertices_equivalent(boost::make_property_map_equivalent(vname_map1, vname_map2)));
  273. BOOST_CHECK(was_common_subgraph_found);
  274. // Test maximum and unique variants on known graphs
  275. Graph graph_simple1, graph_simple2;
  276. simple_callback<Graph> user_callback_simple(graph_simple1);
  277. VertexNameMap vname_map_simple1 = get(boost::vertex_name, graph_simple1);
  278. VertexNameMap vname_map_simple2 = get(boost::vertex_name, graph_simple2);
  279. put(vname_map_simple1, add_vertex(graph_simple1), 1);
  280. put(vname_map_simple1, add_vertex(graph_simple1), 2);
  281. put(vname_map_simple1, add_vertex(graph_simple1), 3);
  282. add_edge(0, 1, graph_simple1);
  283. add_edge(0, 2, graph_simple1);
  284. add_edge(1, 2, graph_simple1);
  285. put(vname_map_simple2, add_vertex(graph_simple2), 1);
  286. put(vname_map_simple2, add_vertex(graph_simple2), 2);
  287. put(vname_map_simple2, add_vertex(graph_simple2), 3);
  288. put(vname_map_simple2, add_vertex(graph_simple2), 4);
  289. add_edge(0, 1, graph_simple2);
  290. add_edge(0, 2, graph_simple2);
  291. add_edge(1, 2, graph_simple2);
  292. add_edge(1, 3, graph_simple2);
  293. // Unique subgraphs
  294. std::cout << "Searching for unique subgraphs" << std::endl;
  295. boost::mcgregor_common_subgraphs_unique(graph_simple1, graph_simple2,
  296. true, user_callback_simple,
  297. boost::vertices_equivalent(boost::make_property_map_equivalent(vname_map_simple1, vname_map_simple2)));
  298. BOOST_CHECK(has_subgraph_string("0,0 1,1 "));
  299. BOOST_CHECK(has_subgraph_string("0,0 1,1 2,2 "));
  300. BOOST_CHECK(has_subgraph_string("0,0 2,2 "));
  301. BOOST_CHECK(has_subgraph_string("1,1 2,2 "));
  302. if (output_graphs) {
  303. std::copy(simple_subgraph_list.begin(), simple_subgraph_list.end(),
  304. std::ostream_iterator<std::string>(std::cout, "\n"));
  305. std::cout << std::endl;
  306. }
  307. simple_subgraph_list.clear();
  308. // Maximum subgraphs
  309. std::cout << "Searching for maximum subgraphs" << std::endl;
  310. boost::mcgregor_common_subgraphs_maximum(graph_simple1, graph_simple2,
  311. true, user_callback_simple,
  312. boost::vertices_equivalent(boost::make_property_map_equivalent(vname_map_simple1, vname_map_simple2)));
  313. BOOST_CHECK(has_subgraph_string("0,0 1,1 2,2 "));
  314. if (output_graphs) {
  315. std::copy(simple_subgraph_list.begin(), simple_subgraph_list.end(),
  316. std::ostream_iterator<std::string>(std::cout, "\n"));
  317. std::cout << std::endl;
  318. }
  319. simple_subgraph_list.clear();
  320. // Maximum, unique subgraphs
  321. std::cout << "Searching for maximum unique subgraphs" << std::endl;
  322. boost::mcgregor_common_subgraphs_maximum_unique(graph_simple1, graph_simple2,
  323. true, user_callback_simple,
  324. boost::vertices_equivalent(boost::make_property_map_equivalent(vname_map_simple1, vname_map_simple2)));
  325. BOOST_CHECK(simple_subgraph_list.size() == 1);
  326. BOOST_CHECK(has_subgraph_string("0,0 1,1 2,2 "));
  327. if (output_graphs) {
  328. std::copy(simple_subgraph_list.begin(), simple_subgraph_list.end(),
  329. std::ostream_iterator<std::string>(std::cout, "\n"));
  330. std::cout << std::endl;
  331. }
  332. return 0;
  333. }