mcgregor_common_subgraphs.hpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. //=======================================================================
  2. // Copyright 2009 Trustees of Indiana University.
  3. // Authors: Michael Hansen, Andrew Lumsdaine
  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. #ifndef BOOST_GRAPH_MCGREGOR_COMMON_SUBGRAPHS_HPP
  10. #define BOOST_GRAPH_MCGREGOR_COMMON_SUBGRAPHS_HPP
  11. #include <algorithm>
  12. #include <vector>
  13. #include <stack>
  14. #include <boost/make_shared.hpp>
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/filtered_graph.hpp>
  17. #include <boost/graph/graph_utility.hpp>
  18. #include <boost/graph/iteration_macros.hpp>
  19. #include <boost/graph/properties.hpp>
  20. #include <boost/property_map/shared_array_property_map.hpp>
  21. namespace boost
  22. {
  23. namespace detail
  24. {
  25. // Traits associated with common subgraphs, used mainly to keep a
  26. // consistent type for the correspondence maps.
  27. template < typename GraphFirst, typename GraphSecond,
  28. typename VertexIndexMapFirst, typename VertexIndexMapSecond >
  29. struct mcgregor_common_subgraph_traits
  30. {
  31. typedef typename graph_traits< GraphFirst >::vertex_descriptor
  32. vertex_first_type;
  33. typedef typename graph_traits< GraphSecond >::vertex_descriptor
  34. vertex_second_type;
  35. typedef shared_array_property_map< vertex_second_type,
  36. VertexIndexMapFirst >
  37. correspondence_map_first_to_second_type;
  38. typedef shared_array_property_map< vertex_first_type,
  39. VertexIndexMapSecond >
  40. correspondence_map_second_to_first_type;
  41. };
  42. } // namespace detail
  43. // ==========================================================================
  44. // Binary function object that returns true if the values for item1
  45. // in property_map1 and item2 in property_map2 are equivalent.
  46. template < typename PropertyMapFirst, typename PropertyMapSecond >
  47. struct property_map_equivalent
  48. {
  49. property_map_equivalent(const PropertyMapFirst property_map1,
  50. const PropertyMapSecond property_map2)
  51. : m_property_map1(property_map1), m_property_map2(property_map2)
  52. {
  53. }
  54. template < typename ItemFirst, typename ItemSecond >
  55. bool operator()(const ItemFirst item1, const ItemSecond item2)
  56. {
  57. return (get(m_property_map1, item1) == get(m_property_map2, item2));
  58. }
  59. private:
  60. const PropertyMapFirst m_property_map1;
  61. const PropertyMapSecond m_property_map2;
  62. };
  63. // Returns a property_map_equivalent object that compares the values
  64. // of property_map1 and property_map2.
  65. template < typename PropertyMapFirst, typename PropertyMapSecond >
  66. property_map_equivalent< PropertyMapFirst, PropertyMapSecond >
  67. make_property_map_equivalent(
  68. const PropertyMapFirst property_map1, const PropertyMapSecond property_map2)
  69. {
  70. return (property_map_equivalent< PropertyMapFirst, PropertyMapSecond >(
  71. property_map1, property_map2));
  72. }
  73. // Binary function object that always returns true. Used when
  74. // vertices or edges are always equivalent (i.e. have no labels).
  75. struct always_equivalent
  76. {
  77. template < typename ItemFirst, typename ItemSecond >
  78. bool operator()(const ItemFirst&, const ItemSecond&)
  79. {
  80. return (true);
  81. }
  82. };
  83. // ==========================================================================
  84. namespace detail
  85. {
  86. // Return true if new_vertex1 and new_vertex2 can extend the
  87. // subgraph represented by correspondence_map_1_to_2 and
  88. // correspondence_map_2_to_1. The vertices_equivalent and
  89. // edges_equivalent predicates are used to test vertex and edge
  90. // equivalency between the two graphs.
  91. template < typename GraphFirst, typename GraphSecond,
  92. typename CorrespondenceMapFirstToSecond,
  93. typename CorrespondenceMapSecondToFirst,
  94. typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate >
  95. bool can_extend_graph(const GraphFirst& graph1, const GraphSecond& graph2,
  96. CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  97. CorrespondenceMapSecondToFirst /*correspondence_map_2_to_1*/,
  98. typename graph_traits< GraphFirst >::vertices_size_type subgraph_size,
  99. typename graph_traits< GraphFirst >::vertex_descriptor new_vertex1,
  100. typename graph_traits< GraphSecond >::vertex_descriptor new_vertex2,
  101. EdgeEquivalencePredicate edges_equivalent,
  102. VertexEquivalencePredicate vertices_equivalent,
  103. bool only_connected_subgraphs)
  104. {
  105. typedef typename graph_traits< GraphSecond >::vertex_descriptor
  106. VertexSecond;
  107. typedef typename graph_traits< GraphFirst >::edge_descriptor EdgeFirst;
  108. typedef
  109. typename graph_traits< GraphSecond >::edge_descriptor EdgeSecond;
  110. // Check vertex equality
  111. if (!vertices_equivalent(new_vertex1, new_vertex2))
  112. {
  113. return (false);
  114. }
  115. // Vertices match and graph is empty, so we can extend the subgraph
  116. if (subgraph_size == 0)
  117. {
  118. return (true);
  119. }
  120. bool has_one_edge = false;
  121. // Verify edges with existing sub-graph
  122. BGL_FORALL_VERTICES_T(existing_vertex1, graph1, GraphFirst)
  123. {
  124. VertexSecond existing_vertex2
  125. = get(correspondence_map_1_to_2, existing_vertex1);
  126. // Skip unassociated vertices
  127. if (existing_vertex2 == graph_traits< GraphSecond >::null_vertex())
  128. {
  129. continue;
  130. }
  131. // NOTE: This will not work with parallel edges, since the
  132. // first matching edge is always chosen.
  133. EdgeFirst edge_to_new1, edge_from_new1;
  134. bool edge_to_new_exists1 = false, edge_from_new_exists1 = false;
  135. EdgeSecond edge_to_new2, edge_from_new2;
  136. bool edge_to_new_exists2 = false, edge_from_new_exists2 = false;
  137. // Search for edge from existing to new vertex (graph1)
  138. BGL_FORALL_OUTEDGES_T(existing_vertex1, edge1, graph1, GraphFirst)
  139. {
  140. if (target(edge1, graph1) == new_vertex1)
  141. {
  142. edge_to_new1 = edge1;
  143. edge_to_new_exists1 = true;
  144. break;
  145. }
  146. }
  147. // Search for edge from existing to new vertex (graph2)
  148. BGL_FORALL_OUTEDGES_T(existing_vertex2, edge2, graph2, GraphSecond)
  149. {
  150. if (target(edge2, graph2) == new_vertex2)
  151. {
  152. edge_to_new2 = edge2;
  153. edge_to_new_exists2 = true;
  154. break;
  155. }
  156. }
  157. // Make sure edges from existing to new vertices are equivalent
  158. if ((edge_to_new_exists1 != edge_to_new_exists2)
  159. || ((edge_to_new_exists1 && edge_to_new_exists2)
  160. && !edges_equivalent(edge_to_new1, edge_to_new2)))
  161. {
  162. return (false);
  163. }
  164. bool is_undirected1 = is_undirected(graph1),
  165. is_undirected2 = is_undirected(graph2);
  166. if (is_undirected1 && is_undirected2)
  167. {
  168. // Edge in both graphs exists and both graphs are undirected
  169. if (edge_to_new_exists1 && edge_to_new_exists2)
  170. {
  171. has_one_edge = true;
  172. }
  173. continue;
  174. }
  175. else
  176. {
  177. if (!is_undirected1)
  178. {
  179. // Search for edge from new to existing vertex (graph1)
  180. BGL_FORALL_OUTEDGES_T(
  181. new_vertex1, edge1, graph1, GraphFirst)
  182. {
  183. if (target(edge1, graph1) == existing_vertex1)
  184. {
  185. edge_from_new1 = edge1;
  186. edge_from_new_exists1 = true;
  187. break;
  188. }
  189. }
  190. }
  191. if (!is_undirected2)
  192. {
  193. // Search for edge from new to existing vertex (graph2)
  194. BGL_FORALL_OUTEDGES_T(
  195. new_vertex2, edge2, graph2, GraphSecond)
  196. {
  197. if (target(edge2, graph2) == existing_vertex2)
  198. {
  199. edge_from_new2 = edge2;
  200. edge_from_new_exists2 = true;
  201. break;
  202. }
  203. }
  204. }
  205. // Make sure edges from new to existing vertices are equivalent
  206. if ((edge_from_new_exists1 != edge_from_new_exists2)
  207. || ((edge_from_new_exists1 && edge_from_new_exists2)
  208. && !edges_equivalent(edge_from_new1, edge_from_new2)))
  209. {
  210. return (false);
  211. }
  212. if ((edge_from_new_exists1 && edge_from_new_exists2)
  213. || (edge_to_new_exists1 && edge_to_new_exists2))
  214. {
  215. has_one_edge = true;
  216. }
  217. } // else
  218. } // BGL_FORALL_VERTICES_T
  219. // Make sure new vertices are connected to the existing subgraph
  220. if (only_connected_subgraphs && !has_one_edge)
  221. {
  222. return (false);
  223. }
  224. return (true);
  225. }
  226. // Recursive method that does a depth-first search in the space of
  227. // potential subgraphs. At each level, every new vertex pair from
  228. // both graphs is tested to see if it can extend the current
  229. // subgraph. If so, the subgraph is output to subgraph_callback
  230. // in the form of two correspondence maps (one for each graph).
  231. // Returning false from subgraph_callback will terminate the
  232. // search. Function returns true if the entire search space was
  233. // explored.
  234. template < typename GraphFirst, typename GraphSecond,
  235. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  236. typename CorrespondenceMapFirstToSecond,
  237. typename CorrespondenceMapSecondToFirst, typename VertexStackFirst,
  238. typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
  239. typename SubGraphInternalCallback >
  240. bool mcgregor_common_subgraphs_internal(const GraphFirst& graph1,
  241. const GraphSecond& graph2, const VertexIndexMapFirst& vindex_map1,
  242. const VertexIndexMapSecond& vindex_map2,
  243. CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  244. CorrespondenceMapSecondToFirst correspondence_map_2_to_1,
  245. VertexStackFirst& vertex_stack1,
  246. EdgeEquivalencePredicate edges_equivalent,
  247. VertexEquivalencePredicate vertices_equivalent,
  248. bool only_connected_subgraphs,
  249. SubGraphInternalCallback subgraph_callback)
  250. {
  251. typedef
  252. typename graph_traits< GraphFirst >::vertex_descriptor VertexFirst;
  253. typedef typename graph_traits< GraphSecond >::vertex_descriptor
  254. VertexSecond;
  255. typedef typename graph_traits< GraphFirst >::vertices_size_type
  256. VertexSizeFirst;
  257. // Get iterators for vertices from both graphs
  258. typename graph_traits< GraphFirst >::vertex_iterator vertex1_iter,
  259. vertex1_end;
  260. typename graph_traits< GraphSecond >::vertex_iterator vertex2_begin,
  261. vertex2_end, vertex2_iter;
  262. boost::tie(vertex1_iter, vertex1_end) = vertices(graph1);
  263. boost::tie(vertex2_begin, vertex2_end) = vertices(graph2);
  264. vertex2_iter = vertex2_begin;
  265. // Iterate until all vertices have been visited
  266. BGL_FORALL_VERTICES_T(new_vertex1, graph1, GraphFirst)
  267. {
  268. VertexSecond existing_vertex2
  269. = get(correspondence_map_1_to_2, new_vertex1);
  270. // Skip already matched vertices in first graph
  271. if (existing_vertex2 != graph_traits< GraphSecond >::null_vertex())
  272. {
  273. continue;
  274. }
  275. BGL_FORALL_VERTICES_T(new_vertex2, graph2, GraphSecond)
  276. {
  277. VertexFirst existing_vertex1
  278. = get(correspondence_map_2_to_1, new_vertex2);
  279. // Skip already matched vertices in second graph
  280. if (existing_vertex1
  281. != graph_traits< GraphFirst >::null_vertex())
  282. {
  283. continue;
  284. }
  285. // Check if current sub-graph can be extended with the matched
  286. // vertex pair
  287. if (can_extend_graph(graph1, graph2, correspondence_map_1_to_2,
  288. correspondence_map_2_to_1,
  289. (VertexSizeFirst)vertex_stack1.size(), new_vertex1,
  290. new_vertex2, edges_equivalent, vertices_equivalent,
  291. only_connected_subgraphs))
  292. {
  293. // Keep track of old graph size for restoring later
  294. VertexSizeFirst old_graph_size
  295. = (VertexSizeFirst)vertex_stack1.size(),
  296. new_graph_size = old_graph_size + 1;
  297. // Extend subgraph
  298. put(correspondence_map_1_to_2, new_vertex1, new_vertex2);
  299. put(correspondence_map_2_to_1, new_vertex2, new_vertex1);
  300. vertex_stack1.push(new_vertex1);
  301. // Returning false from the callback will cancel iteration
  302. if (!subgraph_callback(correspondence_map_1_to_2,
  303. correspondence_map_2_to_1, new_graph_size))
  304. {
  305. return (false);
  306. }
  307. // Depth-first search into the state space of possible
  308. // sub-graphs
  309. bool continue_iteration
  310. = mcgregor_common_subgraphs_internal(graph1, graph2,
  311. vindex_map1, vindex_map2, correspondence_map_1_to_2,
  312. correspondence_map_2_to_1, vertex_stack1,
  313. edges_equivalent, vertices_equivalent,
  314. only_connected_subgraphs, subgraph_callback);
  315. if (!continue_iteration)
  316. {
  317. return (false);
  318. }
  319. // Restore previous state
  320. if (vertex_stack1.size() > old_graph_size)
  321. {
  322. VertexFirst stack_vertex1 = vertex_stack1.top();
  323. VertexSecond stack_vertex2
  324. = get(correspondence_map_1_to_2, stack_vertex1);
  325. // Contract subgraph
  326. put(correspondence_map_1_to_2, stack_vertex1,
  327. graph_traits< GraphSecond >::null_vertex());
  328. put(correspondence_map_2_to_1, stack_vertex2,
  329. graph_traits< GraphFirst >::null_vertex());
  330. vertex_stack1.pop();
  331. }
  332. } // if can_extend_graph
  333. } // BGL_FORALL_VERTICES_T (graph2)
  334. } // BGL_FORALL_VERTICES_T (graph1)
  335. return (true);
  336. }
  337. // Internal method that initializes blank correspondence maps and
  338. // a vertex stack for use in mcgregor_common_subgraphs_internal.
  339. template < typename GraphFirst, typename GraphSecond,
  340. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  341. typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
  342. typename SubGraphInternalCallback >
  343. inline void mcgregor_common_subgraphs_internal_init(
  344. const GraphFirst& graph1, const GraphSecond& graph2,
  345. const VertexIndexMapFirst vindex_map1,
  346. const VertexIndexMapSecond vindex_map2,
  347. EdgeEquivalencePredicate edges_equivalent,
  348. VertexEquivalencePredicate vertices_equivalent,
  349. bool only_connected_subgraphs,
  350. SubGraphInternalCallback subgraph_callback)
  351. {
  352. typedef mcgregor_common_subgraph_traits< GraphFirst, GraphSecond,
  353. VertexIndexMapFirst, VertexIndexMapSecond >
  354. SubGraphTraits;
  355. typename SubGraphTraits::correspondence_map_first_to_second_type
  356. correspondence_map_1_to_2(num_vertices(graph1), vindex_map1);
  357. BGL_FORALL_VERTICES_T(vertex1, graph1, GraphFirst)
  358. {
  359. put(correspondence_map_1_to_2, vertex1,
  360. graph_traits< GraphSecond >::null_vertex());
  361. }
  362. typename SubGraphTraits::correspondence_map_second_to_first_type
  363. correspondence_map_2_to_1(num_vertices(graph2), vindex_map2);
  364. BGL_FORALL_VERTICES_T(vertex2, graph2, GraphSecond)
  365. {
  366. put(correspondence_map_2_to_1, vertex2,
  367. graph_traits< GraphFirst >::null_vertex());
  368. }
  369. typedef
  370. typename graph_traits< GraphFirst >::vertex_descriptor VertexFirst;
  371. std::stack< VertexFirst > vertex_stack1;
  372. mcgregor_common_subgraphs_internal(graph1, graph2, vindex_map1,
  373. vindex_map2, correspondence_map_1_to_2, correspondence_map_2_to_1,
  374. vertex_stack1, edges_equivalent, vertices_equivalent,
  375. only_connected_subgraphs, subgraph_callback);
  376. }
  377. } // namespace detail
  378. // ==========================================================================
  379. // Enumerates all common subgraphs present in graph1 and graph2.
  380. // Continues until the search space has been fully explored or false
  381. // is returned from user_callback.
  382. template < typename GraphFirst, typename GraphSecond,
  383. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  384. typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
  385. typename SubGraphCallback >
  386. void mcgregor_common_subgraphs(const GraphFirst& graph1,
  387. const GraphSecond& graph2, const VertexIndexMapFirst vindex_map1,
  388. const VertexIndexMapSecond vindex_map2,
  389. EdgeEquivalencePredicate edges_equivalent,
  390. VertexEquivalencePredicate vertices_equivalent,
  391. bool only_connected_subgraphs, SubGraphCallback user_callback)
  392. {
  393. detail::mcgregor_common_subgraphs_internal_init(graph1, graph2, vindex_map1,
  394. vindex_map2, edges_equivalent, vertices_equivalent,
  395. only_connected_subgraphs, user_callback);
  396. }
  397. // Variant of mcgregor_common_subgraphs with all default parameters
  398. template < typename GraphFirst, typename GraphSecond,
  399. typename SubGraphCallback >
  400. void mcgregor_common_subgraphs(const GraphFirst& graph1,
  401. const GraphSecond& graph2, bool only_connected_subgraphs,
  402. SubGraphCallback user_callback)
  403. {
  404. detail::mcgregor_common_subgraphs_internal_init(graph1, graph2,
  405. get(vertex_index, graph1), get(vertex_index, graph2),
  406. always_equivalent(), always_equivalent(), only_connected_subgraphs,
  407. user_callback);
  408. }
  409. // Named parameter variant of mcgregor_common_subgraphs
  410. template < typename GraphFirst, typename GraphSecond, typename SubGraphCallback,
  411. typename Param, typename Tag, typename Rest >
  412. void mcgregor_common_subgraphs(const GraphFirst& graph1,
  413. const GraphSecond& graph2, bool only_connected_subgraphs,
  414. SubGraphCallback user_callback,
  415. const bgl_named_params< Param, Tag, Rest >& params)
  416. {
  417. detail::mcgregor_common_subgraphs_internal_init(graph1, graph2,
  418. choose_const_pmap(
  419. get_param(params, vertex_index1), graph1, vertex_index),
  420. choose_const_pmap(
  421. get_param(params, vertex_index2), graph2, vertex_index),
  422. choose_param(
  423. get_param(params, edges_equivalent_t()), always_equivalent()),
  424. choose_param(
  425. get_param(params, vertices_equivalent_t()), always_equivalent()),
  426. only_connected_subgraphs, user_callback);
  427. }
  428. // ==========================================================================
  429. namespace detail
  430. {
  431. // Binary function object that intercepts subgraphs from
  432. // mcgregor_common_subgraphs_internal and maintains a cache of
  433. // unique subgraphs. The user callback is invoked for each unique
  434. // subgraph.
  435. template < typename GraphFirst, typename GraphSecond,
  436. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  437. typename SubGraphCallback >
  438. struct unique_subgraph_interceptor
  439. {
  440. typedef typename graph_traits< GraphFirst >::vertices_size_type
  441. VertexSizeFirst;
  442. typedef mcgregor_common_subgraph_traits< GraphFirst, GraphSecond,
  443. VertexIndexMapFirst, VertexIndexMapSecond >
  444. SubGraphTraits;
  445. typedef typename SubGraphTraits::correspondence_map_first_to_second_type
  446. CachedCorrespondenceMapFirstToSecond;
  447. typedef typename SubGraphTraits::correspondence_map_second_to_first_type
  448. CachedCorrespondenceMapSecondToFirst;
  449. typedef std::pair< VertexSizeFirst,
  450. std::pair< CachedCorrespondenceMapFirstToSecond,
  451. CachedCorrespondenceMapSecondToFirst > >
  452. SubGraph;
  453. typedef std::vector< SubGraph > SubGraphList;
  454. unique_subgraph_interceptor(const GraphFirst& graph1,
  455. const GraphSecond& graph2, const VertexIndexMapFirst vindex_map1,
  456. const VertexIndexMapSecond vindex_map2,
  457. SubGraphCallback user_callback)
  458. : m_graph1(graph1)
  459. , m_graph2(graph2)
  460. , m_vindex_map1(vindex_map1)
  461. , m_vindex_map2(vindex_map2)
  462. , m_subgraphs(make_shared< SubGraphList >())
  463. , m_user_callback(user_callback)
  464. {
  465. }
  466. template < typename CorrespondenceMapFirstToSecond,
  467. typename CorrespondenceMapSecondToFirst >
  468. bool operator()(
  469. CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  470. CorrespondenceMapSecondToFirst correspondence_map_2_to_1,
  471. VertexSizeFirst subgraph_size)
  472. {
  473. for (typename SubGraphList::const_iterator subgraph_iter
  474. = m_subgraphs->begin();
  475. subgraph_iter != m_subgraphs->end(); ++subgraph_iter)
  476. {
  477. SubGraph subgraph_cached = *subgraph_iter;
  478. // Compare subgraph sizes
  479. if (subgraph_size != subgraph_cached.first)
  480. {
  481. continue;
  482. }
  483. if (!are_property_maps_different(correspondence_map_1_to_2,
  484. subgraph_cached.second.first, m_graph1))
  485. {
  486. // New subgraph is a duplicate
  487. return (true);
  488. }
  489. }
  490. // Subgraph is unique, so make a cached copy
  491. CachedCorrespondenceMapFirstToSecond new_subgraph_1_to_2
  492. = CachedCorrespondenceMapFirstToSecond(
  493. num_vertices(m_graph1), m_vindex_map1);
  494. CachedCorrespondenceMapSecondToFirst new_subgraph_2_to_1
  495. = CorrespondenceMapSecondToFirst(
  496. num_vertices(m_graph2), m_vindex_map2);
  497. BGL_FORALL_VERTICES_T(vertex1, m_graph1, GraphFirst)
  498. {
  499. put(new_subgraph_1_to_2, vertex1,
  500. get(correspondence_map_1_to_2, vertex1));
  501. }
  502. BGL_FORALL_VERTICES_T(vertex2, m_graph2, GraphFirst)
  503. {
  504. put(new_subgraph_2_to_1, vertex2,
  505. get(correspondence_map_2_to_1, vertex2));
  506. }
  507. m_subgraphs->push_back(std::make_pair(subgraph_size,
  508. std::make_pair(new_subgraph_1_to_2, new_subgraph_2_to_1)));
  509. return (m_user_callback(correspondence_map_1_to_2,
  510. correspondence_map_2_to_1, subgraph_size));
  511. }
  512. private:
  513. const GraphFirst& m_graph1;
  514. const GraphFirst& m_graph2;
  515. const VertexIndexMapFirst m_vindex_map1;
  516. const VertexIndexMapSecond m_vindex_map2;
  517. shared_ptr< SubGraphList > m_subgraphs;
  518. SubGraphCallback m_user_callback;
  519. };
  520. } // namespace detail
  521. // Enumerates all unique common subgraphs between graph1 and graph2.
  522. // The user callback is invoked for each unique subgraph as they are
  523. // discovered.
  524. template < typename GraphFirst, typename GraphSecond,
  525. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  526. typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
  527. typename SubGraphCallback >
  528. void mcgregor_common_subgraphs_unique(const GraphFirst& graph1,
  529. const GraphSecond& graph2, const VertexIndexMapFirst vindex_map1,
  530. const VertexIndexMapSecond vindex_map2,
  531. EdgeEquivalencePredicate edges_equivalent,
  532. VertexEquivalencePredicate vertices_equivalent,
  533. bool only_connected_subgraphs, SubGraphCallback user_callback)
  534. {
  535. detail::unique_subgraph_interceptor< GraphFirst, GraphSecond,
  536. VertexIndexMapFirst, VertexIndexMapSecond, SubGraphCallback >
  537. unique_callback(
  538. graph1, graph2, vindex_map1, vindex_map2, user_callback);
  539. detail::mcgregor_common_subgraphs_internal_init(graph1, graph2, vindex_map1,
  540. vindex_map2, edges_equivalent, vertices_equivalent,
  541. only_connected_subgraphs, unique_callback);
  542. }
  543. // Variant of mcgregor_common_subgraphs_unique with all default
  544. // parameters.
  545. template < typename GraphFirst, typename GraphSecond,
  546. typename SubGraphCallback >
  547. void mcgregor_common_subgraphs_unique(const GraphFirst& graph1,
  548. const GraphSecond& graph2, bool only_connected_subgraphs,
  549. SubGraphCallback user_callback)
  550. {
  551. mcgregor_common_subgraphs_unique(graph1, graph2, get(vertex_index, graph1),
  552. get(vertex_index, graph2), always_equivalent(), always_equivalent(),
  553. only_connected_subgraphs, user_callback);
  554. }
  555. // Named parameter variant of mcgregor_common_subgraphs_unique
  556. template < typename GraphFirst, typename GraphSecond, typename SubGraphCallback,
  557. typename Param, typename Tag, typename Rest >
  558. void mcgregor_common_subgraphs_unique(const GraphFirst& graph1,
  559. const GraphSecond& graph2, bool only_connected_subgraphs,
  560. SubGraphCallback user_callback,
  561. const bgl_named_params< Param, Tag, Rest >& params)
  562. {
  563. mcgregor_common_subgraphs_unique(graph1, graph2,
  564. choose_const_pmap(
  565. get_param(params, vertex_index1), graph1, vertex_index),
  566. choose_const_pmap(
  567. get_param(params, vertex_index2), graph2, vertex_index),
  568. choose_param(
  569. get_param(params, edges_equivalent_t()), always_equivalent()),
  570. choose_param(
  571. get_param(params, vertices_equivalent_t()), always_equivalent()),
  572. only_connected_subgraphs, user_callback);
  573. }
  574. // ==========================================================================
  575. namespace detail
  576. {
  577. // Binary function object that intercepts subgraphs from
  578. // mcgregor_common_subgraphs_internal and maintains a cache of the
  579. // largest subgraphs.
  580. template < typename GraphFirst, typename GraphSecond,
  581. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  582. typename SubGraphCallback >
  583. struct maximum_subgraph_interceptor
  584. {
  585. typedef typename graph_traits< GraphFirst >::vertices_size_type
  586. VertexSizeFirst;
  587. typedef mcgregor_common_subgraph_traits< GraphFirst, GraphSecond,
  588. VertexIndexMapFirst, VertexIndexMapSecond >
  589. SubGraphTraits;
  590. typedef typename SubGraphTraits::correspondence_map_first_to_second_type
  591. CachedCorrespondenceMapFirstToSecond;
  592. typedef typename SubGraphTraits::correspondence_map_second_to_first_type
  593. CachedCorrespondenceMapSecondToFirst;
  594. typedef std::pair< VertexSizeFirst,
  595. std::pair< CachedCorrespondenceMapFirstToSecond,
  596. CachedCorrespondenceMapSecondToFirst > >
  597. SubGraph;
  598. typedef std::vector< SubGraph > SubGraphList;
  599. maximum_subgraph_interceptor(const GraphFirst& graph1,
  600. const GraphSecond& graph2, const VertexIndexMapFirst vindex_map1,
  601. const VertexIndexMapSecond vindex_map2,
  602. SubGraphCallback user_callback)
  603. : m_graph1(graph1)
  604. , m_graph2(graph2)
  605. , m_vindex_map1(vindex_map1)
  606. , m_vindex_map2(vindex_map2)
  607. , m_subgraphs(make_shared< SubGraphList >())
  608. , m_largest_size_so_far(make_shared< VertexSizeFirst >(0))
  609. , m_user_callback(user_callback)
  610. {
  611. }
  612. template < typename CorrespondenceMapFirstToSecond,
  613. typename CorrespondenceMapSecondToFirst >
  614. bool operator()(
  615. CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  616. CorrespondenceMapSecondToFirst correspondence_map_2_to_1,
  617. VertexSizeFirst subgraph_size)
  618. {
  619. if (subgraph_size > *m_largest_size_so_far)
  620. {
  621. m_subgraphs->clear();
  622. *m_largest_size_so_far = subgraph_size;
  623. }
  624. if (subgraph_size == *m_largest_size_so_far)
  625. {
  626. // Make a cached copy
  627. CachedCorrespondenceMapFirstToSecond new_subgraph_1_to_2
  628. = CachedCorrespondenceMapFirstToSecond(
  629. num_vertices(m_graph1), m_vindex_map1);
  630. CachedCorrespondenceMapSecondToFirst new_subgraph_2_to_1
  631. = CachedCorrespondenceMapSecondToFirst(
  632. num_vertices(m_graph2), m_vindex_map2);
  633. BGL_FORALL_VERTICES_T(vertex1, m_graph1, GraphFirst)
  634. {
  635. put(new_subgraph_1_to_2, vertex1,
  636. get(correspondence_map_1_to_2, vertex1));
  637. }
  638. BGL_FORALL_VERTICES_T(vertex2, m_graph2, GraphFirst)
  639. {
  640. put(new_subgraph_2_to_1, vertex2,
  641. get(correspondence_map_2_to_1, vertex2));
  642. }
  643. m_subgraphs->push_back(std::make_pair(subgraph_size,
  644. std::make_pair(new_subgraph_1_to_2, new_subgraph_2_to_1)));
  645. }
  646. return (true);
  647. }
  648. void output_subgraphs()
  649. {
  650. for (typename SubGraphList::const_iterator subgraph_iter
  651. = m_subgraphs->begin();
  652. subgraph_iter != m_subgraphs->end(); ++subgraph_iter)
  653. {
  654. SubGraph subgraph_cached = *subgraph_iter;
  655. m_user_callback(subgraph_cached.second.first,
  656. subgraph_cached.second.second, subgraph_cached.first);
  657. }
  658. }
  659. private:
  660. const GraphFirst& m_graph1;
  661. const GraphFirst& m_graph2;
  662. const VertexIndexMapFirst m_vindex_map1;
  663. const VertexIndexMapSecond m_vindex_map2;
  664. shared_ptr< SubGraphList > m_subgraphs;
  665. shared_ptr< VertexSizeFirst > m_largest_size_so_far;
  666. SubGraphCallback m_user_callback;
  667. };
  668. } // namespace detail
  669. // Enumerates the largest common subgraphs found between graph1
  670. // and graph2. Note that the ENTIRE search space is explored before
  671. // user_callback is actually invoked.
  672. template < typename GraphFirst, typename GraphSecond,
  673. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  674. typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
  675. typename SubGraphCallback >
  676. void mcgregor_common_subgraphs_maximum(const GraphFirst& graph1,
  677. const GraphSecond& graph2, const VertexIndexMapFirst vindex_map1,
  678. const VertexIndexMapSecond vindex_map2,
  679. EdgeEquivalencePredicate edges_equivalent,
  680. VertexEquivalencePredicate vertices_equivalent,
  681. bool only_connected_subgraphs, SubGraphCallback user_callback)
  682. {
  683. detail::maximum_subgraph_interceptor< GraphFirst, GraphSecond,
  684. VertexIndexMapFirst, VertexIndexMapSecond, SubGraphCallback >
  685. max_interceptor(
  686. graph1, graph2, vindex_map1, vindex_map2, user_callback);
  687. detail::mcgregor_common_subgraphs_internal_init(graph1, graph2, vindex_map1,
  688. vindex_map2, edges_equivalent, vertices_equivalent,
  689. only_connected_subgraphs, max_interceptor);
  690. // Only output the largest subgraphs
  691. max_interceptor.output_subgraphs();
  692. }
  693. // Variant of mcgregor_common_subgraphs_maximum with all default
  694. // parameters.
  695. template < typename GraphFirst, typename GraphSecond,
  696. typename SubGraphCallback >
  697. void mcgregor_common_subgraphs_maximum(const GraphFirst& graph1,
  698. const GraphSecond& graph2, bool only_connected_subgraphs,
  699. SubGraphCallback user_callback)
  700. {
  701. mcgregor_common_subgraphs_maximum(graph1, graph2, get(vertex_index, graph1),
  702. get(vertex_index, graph2), always_equivalent(), always_equivalent(),
  703. only_connected_subgraphs, user_callback);
  704. }
  705. // Named parameter variant of mcgregor_common_subgraphs_maximum
  706. template < typename GraphFirst, typename GraphSecond, typename SubGraphCallback,
  707. typename Param, typename Tag, typename Rest >
  708. void mcgregor_common_subgraphs_maximum(const GraphFirst& graph1,
  709. const GraphSecond& graph2, bool only_connected_subgraphs,
  710. SubGraphCallback user_callback,
  711. const bgl_named_params< Param, Tag, Rest >& params)
  712. {
  713. mcgregor_common_subgraphs_maximum(graph1, graph2,
  714. choose_const_pmap(
  715. get_param(params, vertex_index1), graph1, vertex_index),
  716. choose_const_pmap(
  717. get_param(params, vertex_index2), graph2, vertex_index),
  718. choose_param(
  719. get_param(params, edges_equivalent_t()), always_equivalent()),
  720. choose_param(
  721. get_param(params, vertices_equivalent_t()), always_equivalent()),
  722. only_connected_subgraphs, user_callback);
  723. }
  724. // ==========================================================================
  725. namespace detail
  726. {
  727. // Binary function object that intercepts subgraphs from
  728. // mcgregor_common_subgraphs_internal and maintains a cache of the
  729. // largest, unique subgraphs.
  730. template < typename GraphFirst, typename GraphSecond,
  731. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  732. typename SubGraphCallback >
  733. struct unique_maximum_subgraph_interceptor
  734. {
  735. typedef typename graph_traits< GraphFirst >::vertices_size_type
  736. VertexSizeFirst;
  737. typedef mcgregor_common_subgraph_traits< GraphFirst, GraphSecond,
  738. VertexIndexMapFirst, VertexIndexMapSecond >
  739. SubGraphTraits;
  740. typedef typename SubGraphTraits::correspondence_map_first_to_second_type
  741. CachedCorrespondenceMapFirstToSecond;
  742. typedef typename SubGraphTraits::correspondence_map_second_to_first_type
  743. CachedCorrespondenceMapSecondToFirst;
  744. typedef std::pair< VertexSizeFirst,
  745. std::pair< CachedCorrespondenceMapFirstToSecond,
  746. CachedCorrespondenceMapSecondToFirst > >
  747. SubGraph;
  748. typedef std::vector< SubGraph > SubGraphList;
  749. unique_maximum_subgraph_interceptor(const GraphFirst& graph1,
  750. const GraphSecond& graph2, const VertexIndexMapFirst vindex_map1,
  751. const VertexIndexMapSecond vindex_map2,
  752. SubGraphCallback user_callback)
  753. : m_graph1(graph1)
  754. , m_graph2(graph2)
  755. , m_vindex_map1(vindex_map1)
  756. , m_vindex_map2(vindex_map2)
  757. , m_subgraphs(make_shared< SubGraphList >())
  758. , m_largest_size_so_far(make_shared< VertexSizeFirst >(0))
  759. , m_user_callback(user_callback)
  760. {
  761. }
  762. template < typename CorrespondenceMapFirstToSecond,
  763. typename CorrespondenceMapSecondToFirst >
  764. bool operator()(
  765. CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  766. CorrespondenceMapSecondToFirst correspondence_map_2_to_1,
  767. VertexSizeFirst subgraph_size)
  768. {
  769. if (subgraph_size > *m_largest_size_so_far)
  770. {
  771. m_subgraphs->clear();
  772. *m_largest_size_so_far = subgraph_size;
  773. }
  774. if (subgraph_size == *m_largest_size_so_far)
  775. {
  776. // Check if subgraph is unique
  777. for (typename SubGraphList::const_iterator subgraph_iter
  778. = m_subgraphs->begin();
  779. subgraph_iter != m_subgraphs->end(); ++subgraph_iter)
  780. {
  781. SubGraph subgraph_cached = *subgraph_iter;
  782. if (!are_property_maps_different(correspondence_map_1_to_2,
  783. subgraph_cached.second.first, m_graph1))
  784. {
  785. // New subgraph is a duplicate
  786. return (true);
  787. }
  788. }
  789. // Subgraph is unique, so make a cached copy
  790. CachedCorrespondenceMapFirstToSecond new_subgraph_1_to_2
  791. = CachedCorrespondenceMapFirstToSecond(
  792. num_vertices(m_graph1), m_vindex_map1);
  793. CachedCorrespondenceMapSecondToFirst new_subgraph_2_to_1
  794. = CachedCorrespondenceMapSecondToFirst(
  795. num_vertices(m_graph2), m_vindex_map2);
  796. BGL_FORALL_VERTICES_T(vertex1, m_graph1, GraphFirst)
  797. {
  798. put(new_subgraph_1_to_2, vertex1,
  799. get(correspondence_map_1_to_2, vertex1));
  800. }
  801. BGL_FORALL_VERTICES_T(vertex2, m_graph2, GraphFirst)
  802. {
  803. put(new_subgraph_2_to_1, vertex2,
  804. get(correspondence_map_2_to_1, vertex2));
  805. }
  806. m_subgraphs->push_back(std::make_pair(subgraph_size,
  807. std::make_pair(new_subgraph_1_to_2, new_subgraph_2_to_1)));
  808. }
  809. return (true);
  810. }
  811. void output_subgraphs()
  812. {
  813. for (typename SubGraphList::const_iterator subgraph_iter
  814. = m_subgraphs->begin();
  815. subgraph_iter != m_subgraphs->end(); ++subgraph_iter)
  816. {
  817. SubGraph subgraph_cached = *subgraph_iter;
  818. m_user_callback(subgraph_cached.second.first,
  819. subgraph_cached.second.second, subgraph_cached.first);
  820. }
  821. }
  822. private:
  823. const GraphFirst& m_graph1;
  824. const GraphFirst& m_graph2;
  825. const VertexIndexMapFirst m_vindex_map1;
  826. const VertexIndexMapSecond m_vindex_map2;
  827. shared_ptr< SubGraphList > m_subgraphs;
  828. shared_ptr< VertexSizeFirst > m_largest_size_so_far;
  829. SubGraphCallback m_user_callback;
  830. };
  831. } // namespace detail
  832. // Enumerates the largest, unique common subgraphs found between
  833. // graph1 and graph2. Note that the ENTIRE search space is explored
  834. // before user_callback is actually invoked.
  835. template < typename GraphFirst, typename GraphSecond,
  836. typename VertexIndexMapFirst, typename VertexIndexMapSecond,
  837. typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
  838. typename SubGraphCallback >
  839. void mcgregor_common_subgraphs_maximum_unique(const GraphFirst& graph1,
  840. const GraphSecond& graph2, const VertexIndexMapFirst vindex_map1,
  841. const VertexIndexMapSecond vindex_map2,
  842. EdgeEquivalencePredicate edges_equivalent,
  843. VertexEquivalencePredicate vertices_equivalent,
  844. bool only_connected_subgraphs, SubGraphCallback user_callback)
  845. {
  846. detail::unique_maximum_subgraph_interceptor< GraphFirst, GraphSecond,
  847. VertexIndexMapFirst, VertexIndexMapSecond, SubGraphCallback >
  848. unique_max_interceptor(
  849. graph1, graph2, vindex_map1, vindex_map2, user_callback);
  850. detail::mcgregor_common_subgraphs_internal_init(graph1, graph2, vindex_map1,
  851. vindex_map2, edges_equivalent, vertices_equivalent,
  852. only_connected_subgraphs, unique_max_interceptor);
  853. // Only output the largest, unique subgraphs
  854. unique_max_interceptor.output_subgraphs();
  855. }
  856. // Variant of mcgregor_common_subgraphs_maximum_unique with all default
  857. // parameters
  858. template < typename GraphFirst, typename GraphSecond,
  859. typename SubGraphCallback >
  860. void mcgregor_common_subgraphs_maximum_unique(const GraphFirst& graph1,
  861. const GraphSecond& graph2, bool only_connected_subgraphs,
  862. SubGraphCallback user_callback)
  863. {
  864. mcgregor_common_subgraphs_maximum_unique(graph1, graph2,
  865. get(vertex_index, graph1), get(vertex_index, graph2),
  866. always_equivalent(), always_equivalent(), only_connected_subgraphs,
  867. user_callback);
  868. }
  869. // Named parameter variant of
  870. // mcgregor_common_subgraphs_maximum_unique
  871. template < typename GraphFirst, typename GraphSecond, typename SubGraphCallback,
  872. typename Param, typename Tag, typename Rest >
  873. void mcgregor_common_subgraphs_maximum_unique(const GraphFirst& graph1,
  874. const GraphSecond& graph2, bool only_connected_subgraphs,
  875. SubGraphCallback user_callback,
  876. const bgl_named_params< Param, Tag, Rest >& params)
  877. {
  878. mcgregor_common_subgraphs_maximum_unique(graph1, graph2,
  879. choose_const_pmap(
  880. get_param(params, vertex_index1), graph1, vertex_index),
  881. choose_const_pmap(
  882. get_param(params, vertex_index2), graph2, vertex_index),
  883. choose_param(
  884. get_param(params, edges_equivalent_t()), always_equivalent()),
  885. choose_param(
  886. get_param(params, vertices_equivalent_t()), always_equivalent()),
  887. only_connected_subgraphs, user_callback);
  888. }
  889. // ==========================================================================
  890. // Fills a membership map (vertex -> bool) using the information
  891. // present in correspondence_map_1_to_2. Every vertex in a
  892. // membership map will have a true value only if it is not
  893. // associated with a null vertex in the correspondence map.
  894. template < typename GraphSecond, typename GraphFirst,
  895. typename CorrespondenceMapFirstToSecond, typename MembershipMapFirst >
  896. void fill_membership_map(const GraphFirst& graph1,
  897. const CorrespondenceMapFirstToSecond correspondence_map_1_to_2,
  898. MembershipMapFirst membership_map1)
  899. {
  900. BGL_FORALL_VERTICES_T(vertex1, graph1, GraphFirst)
  901. {
  902. put(membership_map1, vertex1,
  903. get(correspondence_map_1_to_2, vertex1)
  904. != graph_traits< GraphSecond >::null_vertex());
  905. }
  906. }
  907. // Traits associated with a membership map filtered graph. Provided
  908. // for convenience to access graph and vertex filter types.
  909. template < typename Graph, typename MembershipMap >
  910. struct membership_filtered_graph_traits
  911. {
  912. typedef property_map_filter< MembershipMap > vertex_filter_type;
  913. typedef filtered_graph< Graph, keep_all, vertex_filter_type > graph_type;
  914. };
  915. // Returns a filtered sub-graph of graph whose edge and vertex
  916. // inclusion is dictated by membership_map.
  917. template < typename Graph, typename MembershipMap >
  918. typename membership_filtered_graph_traits< Graph, MembershipMap >::graph_type
  919. make_membership_filtered_graph(
  920. const Graph& graph, MembershipMap& membership_map)
  921. {
  922. typedef membership_filtered_graph_traits< Graph, MembershipMap > MFGTraits;
  923. typedef typename MFGTraits::graph_type MembershipFilteredGraph;
  924. typename MFGTraits::vertex_filter_type v_filter(membership_map);
  925. return (MembershipFilteredGraph(graph, keep_all(), v_filter));
  926. }
  927. } // namespace boost
  928. #endif // BOOST_GRAPH_MCGREGOR_COMMON_SUBGRAPHS_HPP