vf2_sub_graph_iso.hpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. //=======================================================================
  2. // Copyright (C) 2012 Flavio De Lorenzi (fdlorenzi@gmail.com)
  3. // Copyright (C) 2013 Jakob Lykke Andersen, University of Southern Denmark (jlandersen@imada.sdu.dk)
  4. //
  5. // The algorithm implemented here is derived from original ideas by
  6. // Pasquale Foggia and colaborators. For further information see
  7. // e.g. Cordella et al. 2001, 2004.
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //=======================================================================
  13. // Revision History:
  14. // 8 April 2013: Fixed a typo in vf2_print_callback. (Flavio De Lorenzi)
  15. #ifndef BOOST_VF2_SUB_GRAPH_ISO_HPP
  16. #define BOOST_VF2_SUB_GRAPH_ISO_HPP
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <iterator>
  20. #include <vector>
  21. #include <utility>
  22. #include <boost/assert.hpp>
  23. #include <boost/concept/assert.hpp>
  24. #include <boost/concept_check.hpp>
  25. #include <boost/graph/graph_utility.hpp>
  26. #include <boost/graph/graph_traits.hpp>
  27. #include <boost/graph/mcgregor_common_subgraphs.hpp> // for always_equivalent
  28. #include <boost/graph/named_function_params.hpp>
  29. #include <boost/type_traits/has_less.hpp>
  30. #include <boost/mpl/int.hpp>
  31. #include <boost/range/algorithm/sort.hpp>
  32. #include <boost/tuple/tuple.hpp>
  33. #include <boost/utility/enable_if.hpp>
  34. #ifndef BOOST_GRAPH_ITERATION_MACROS_HPP
  35. #define BOOST_ISO_INCLUDED_ITER_MACROS // local macro, see bottom of file
  36. #include <boost/graph/iteration_macros.hpp>
  37. #endif
  38. namespace boost {
  39. // Default print_callback
  40. template <typename Graph1,
  41. typename Graph2>
  42. struct vf2_print_callback {
  43. vf2_print_callback(const Graph1& graph1, const Graph2& graph2)
  44. : graph1_(graph1), graph2_(graph2) {}
  45. template <typename CorrespondenceMap1To2,
  46. typename CorrespondenceMap2To1>
  47. bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) const {
  48. // Print (sub)graph isomorphism map
  49. BGL_FORALL_VERTICES_T(v, graph1_, Graph1)
  50. std::cout << '(' << get(vertex_index_t(), graph1_, v) << ", "
  51. << get(vertex_index_t(), graph2_, get(f, v)) << ") ";
  52. std::cout << std::endl;
  53. return true;
  54. }
  55. private:
  56. const Graph1& graph1_;
  57. const Graph2& graph2_;
  58. };
  59. namespace detail {
  60. // State associated with a single graph (graph_this)
  61. template<typename GraphThis,
  62. typename GraphOther,
  63. typename IndexMapThis,
  64. typename IndexMapOther>
  65. class base_state {
  66. typedef typename graph_traits<GraphThis>::vertex_descriptor vertex_this_type;
  67. typedef typename graph_traits<GraphOther>::vertex_descriptor vertex_other_type;
  68. typedef typename graph_traits<GraphThis>::vertices_size_type size_type;
  69. const GraphThis& graph_this_;
  70. const GraphOther& graph_other_;
  71. IndexMapThis index_map_this_;
  72. IndexMapOther index_map_other_;
  73. std::vector<vertex_other_type> core_vec_;
  74. typedef iterator_property_map<typename std::vector<vertex_other_type>::iterator,
  75. IndexMapThis, vertex_other_type,
  76. vertex_other_type&> core_map_type;
  77. core_map_type core_;
  78. std::vector<size_type> in_vec_, out_vec_;
  79. typedef iterator_property_map<typename std::vector<size_type>::iterator,
  80. IndexMapThis, size_type, size_type&> in_out_map_type;
  81. in_out_map_type in_, out_;
  82. size_type term_in_count_, term_out_count_, term_both_count_, core_count_;
  83. // Forbidden
  84. base_state(const base_state&);
  85. base_state& operator=(const base_state&);
  86. public:
  87. base_state(const GraphThis& graph_this, const GraphOther& graph_other,
  88. IndexMapThis index_map_this, IndexMapOther index_map_other)
  89. : graph_this_(graph_this), graph_other_(graph_other),
  90. index_map_this_(index_map_this), index_map_other_(index_map_other),
  91. term_in_count_(0), term_out_count_(0), term_both_count_(0), core_count_(0) {
  92. core_vec_.resize(num_vertices(graph_this_), graph_traits<GraphOther>::null_vertex());
  93. core_ = make_iterator_property_map(core_vec_.begin(), index_map_this_);
  94. in_vec_.resize(num_vertices(graph_this_), 0);
  95. in_ = make_iterator_property_map(in_vec_.begin(), index_map_this_);
  96. out_vec_.resize(num_vertices(graph_this_), 0);
  97. out_ = make_iterator_property_map(out_vec_.begin(), index_map_this_);
  98. }
  99. // Adds a vertex pair to the state of graph graph_this
  100. void push(const vertex_this_type& v_this, const vertex_other_type& v_other) {
  101. ++core_count_;
  102. put(core_, v_this, v_other);
  103. if (!get(in_, v_this)) {
  104. put(in_, v_this, core_count_);
  105. ++term_in_count_;
  106. if (get(out_, v_this))
  107. ++term_both_count_;
  108. }
  109. if (!get(out_, v_this)) {
  110. put(out_, v_this, core_count_);
  111. ++term_out_count_;
  112. if (get(in_, v_this))
  113. ++term_both_count_;
  114. }
  115. BGL_FORALL_INEDGES_T(v_this, e, graph_this_, GraphThis) {
  116. vertex_this_type w = source(e, graph_this_);
  117. if (!get(in_, w)) {
  118. put(in_, w, core_count_);
  119. ++term_in_count_;
  120. if (get(out_, w))
  121. ++term_both_count_;
  122. }
  123. }
  124. BGL_FORALL_OUTEDGES_T(v_this, e, graph_this_, GraphThis) {
  125. vertex_this_type w = target(e, graph_this_);
  126. if (!get(out_, w)) {
  127. put(out_, w, core_count_);
  128. ++term_out_count_;
  129. if (get(in_, w))
  130. ++term_both_count_;
  131. }
  132. }
  133. }
  134. // Removes vertex pair from state of graph_this
  135. void pop(const vertex_this_type& v_this, const vertex_other_type&) {
  136. if (!core_count_) return;
  137. if (get(in_, v_this) == core_count_) {
  138. put(in_, v_this, 0);
  139. --term_in_count_;
  140. if (get(out_, v_this))
  141. --term_both_count_;
  142. }
  143. BGL_FORALL_INEDGES_T(v_this, e, graph_this_, GraphThis) {
  144. vertex_this_type w = source(e, graph_this_);
  145. if (get(in_, w) == core_count_) {
  146. put(in_, w, 0);
  147. --term_in_count_;
  148. if (get(out_, w))
  149. --term_both_count_;
  150. }
  151. }
  152. if (get(out_, v_this) == core_count_) {
  153. put(out_, v_this, 0);
  154. --term_out_count_;
  155. if (get(in_, v_this))
  156. --term_both_count_;
  157. }
  158. BGL_FORALL_OUTEDGES_T(v_this, e, graph_this_, GraphThis) {
  159. vertex_this_type w = target(e, graph_this_);
  160. if (get(out_, w) == core_count_) {
  161. put(out_, w, 0);
  162. --term_out_count_;
  163. if (get(in_, w))
  164. --term_both_count_;
  165. }
  166. }
  167. put(core_, v_this, graph_traits<GraphOther>::null_vertex());
  168. --core_count_;
  169. }
  170. // Returns true if the in-terminal set is not empty
  171. bool term_in() const {
  172. return core_count_ < term_in_count_ ;
  173. }
  174. // Returns true if vertex belongs to the in-terminal set
  175. bool term_in(const vertex_this_type& v) const {
  176. return (get(in_, v) > 0) &&
  177. (get(core_, v) == graph_traits<GraphOther>::null_vertex());
  178. }
  179. // Returns true if the out-terminal set is not empty
  180. bool term_out() const {
  181. return core_count_ < term_out_count_;
  182. }
  183. // Returns true if vertex belongs to the out-terminal set
  184. bool term_out(const vertex_this_type& v) const {
  185. return (get(out_, v) > 0) &&
  186. (get(core_, v) == graph_traits<GraphOther>::null_vertex());
  187. }
  188. // Returns true of both (in- and out-terminal) sets are not empty
  189. bool term_both() const {
  190. return core_count_ < term_both_count_;
  191. }
  192. // Returns true if vertex belongs to both (in- and out-terminal) sets
  193. bool term_both(const vertex_this_type& v) const {
  194. return (get(in_, v) > 0) && (get(out_, v) > 0) &&
  195. (get(core_, v) == graph_traits<GraphOther>::null_vertex());
  196. }
  197. // Returns true if vertex belongs to the core map, i.e. it is in the
  198. // present mapping
  199. bool in_core(const vertex_this_type& v) const {
  200. return get(core_, v) != graph_traits<GraphOther>::null_vertex();
  201. }
  202. // Returns the number of vertices in the mapping
  203. size_type count() const {
  204. return core_count_;
  205. }
  206. // Returns the image (in graph_other) of vertex v (in graph_this)
  207. vertex_other_type core(const vertex_this_type& v) const {
  208. return get(core_, v);
  209. }
  210. // Returns the mapping
  211. core_map_type get_map() const {
  212. return core_;
  213. }
  214. // Returns the "time" (or depth) when vertex was added to the in-terminal set
  215. size_type in_depth(const vertex_this_type& v) const {
  216. return get(in_, v);
  217. }
  218. // Returns the "time" (or depth) when vertex was added to the out-terminal set
  219. size_type out_depth(const vertex_this_type& v) const {
  220. return get(out_, v);
  221. }
  222. // Returns the terminal set counts
  223. boost::tuple<size_type, size_type, size_type>
  224. term_set() const {
  225. return boost::make_tuple(term_in_count_, term_out_count_,
  226. term_both_count_);
  227. }
  228. };
  229. // Function object that checks whether a valid edge
  230. // exists. For multi-graphs matched edges are excluded
  231. template <typename Graph, typename Enable = void>
  232. struct equivalent_edge_exists {
  233. typedef typename boost::graph_traits<Graph>::edge_descriptor edge_type;
  234. BOOST_CONCEPT_ASSERT(( LessThanComparable<edge_type> ));
  235. template<typename EdgePredicate>
  236. bool operator()(typename graph_traits<Graph>::vertex_descriptor s,
  237. typename graph_traits<Graph>::vertex_descriptor t,
  238. EdgePredicate is_valid_edge, const Graph& g) {
  239. BGL_FORALL_OUTEDGES_T(s, e, g, Graph) {
  240. if ((target(e, g) == t) && is_valid_edge(e) &&
  241. (matched_edges_.find(e) == matched_edges_.end())) {
  242. matched_edges_.insert(e);
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. private:
  249. std::set<edge_type> matched_edges_;
  250. };
  251. template <typename Graph>
  252. struct equivalent_edge_exists<Graph, typename boost::disable_if<is_multigraph<Graph> >::type> {
  253. template<typename EdgePredicate>
  254. bool operator()(typename graph_traits<Graph>::vertex_descriptor s,
  255. typename graph_traits<Graph>::vertex_descriptor t,
  256. EdgePredicate is_valid_edge, const Graph& g) {
  257. typename graph_traits<Graph>::edge_descriptor e;
  258. bool found;
  259. boost::tie(e, found) = edge(s, t, g);
  260. if (!found)
  261. return false;
  262. else if (is_valid_edge(e))
  263. return true;
  264. return false;
  265. }
  266. };
  267. // Generates a predicate for edge e1 given a binary predicate and a
  268. // fixed edge e2
  269. template <typename Graph1,
  270. typename Graph2,
  271. typename EdgeEquivalencePredicate>
  272. struct edge1_predicate {
  273. edge1_predicate(EdgeEquivalencePredicate edge_comp,
  274. typename graph_traits<Graph2>::edge_descriptor e2)
  275. : edge_comp_(edge_comp), e2_(e2) {}
  276. bool operator()(typename graph_traits<Graph1>::edge_descriptor e1) {
  277. return edge_comp_(e1, e2_);
  278. }
  279. EdgeEquivalencePredicate edge_comp_;
  280. typename graph_traits<Graph2>::edge_descriptor e2_;
  281. };
  282. // Generates a predicate for edge e2 given given a binary predicate and a
  283. // fixed edge e1
  284. template <typename Graph1,
  285. typename Graph2,
  286. typename EdgeEquivalencePredicate>
  287. struct edge2_predicate {
  288. edge2_predicate(EdgeEquivalencePredicate edge_comp,
  289. typename graph_traits<Graph1>::edge_descriptor e1)
  290. : edge_comp_(edge_comp), e1_(e1) {}
  291. bool operator()(typename graph_traits<Graph2>::edge_descriptor e2) {
  292. return edge_comp_(e1_, e2);
  293. }
  294. EdgeEquivalencePredicate edge_comp_;
  295. typename graph_traits<Graph1>::edge_descriptor e1_;
  296. };
  297. enum problem_selector {subgraph_mono, subgraph_iso, isomorphism };
  298. // The actual state associated with both graphs
  299. template<typename Graph1,
  300. typename Graph2,
  301. typename IndexMap1,
  302. typename IndexMap2,
  303. typename EdgeEquivalencePredicate,
  304. typename VertexEquivalencePredicate,
  305. typename SubGraphIsoMapCallback,
  306. problem_selector problem_selection>
  307. class state {
  308. typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_type;
  309. typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_type;
  310. typedef typename graph_traits<Graph1>::edge_descriptor edge1_type;
  311. typedef typename graph_traits<Graph2>::edge_descriptor edge2_type;
  312. typedef typename graph_traits<Graph1>::vertices_size_type graph1_size_type;
  313. typedef typename graph_traits<Graph2>::vertices_size_type graph2_size_type;
  314. const Graph1& graph1_;
  315. const Graph2& graph2_;
  316. IndexMap1 index_map1_;
  317. EdgeEquivalencePredicate edge_comp_;
  318. VertexEquivalencePredicate vertex_comp_;
  319. base_state<Graph1, Graph2, IndexMap1, IndexMap2> state1_;
  320. base_state<Graph2, Graph1, IndexMap2, IndexMap1> state2_;
  321. // Three helper functions used in Feasibility and Valid functions to test
  322. // terminal set counts when testing for:
  323. // - graph sub-graph monomorphism, or
  324. inline bool comp_term_sets(graph1_size_type a,
  325. graph2_size_type b,
  326. boost::mpl::int_<subgraph_mono>) const {
  327. return a <= b;
  328. }
  329. // - graph sub-graph isomorphism, or
  330. inline bool comp_term_sets(graph1_size_type a,
  331. graph2_size_type b,
  332. boost::mpl::int_<subgraph_iso>) const {
  333. return a <= b;
  334. }
  335. // - graph isomorphism
  336. inline bool comp_term_sets(graph1_size_type a,
  337. graph2_size_type b,
  338. boost::mpl::int_<isomorphism>) const {
  339. return a == b;
  340. }
  341. // Forbidden
  342. state(const state&);
  343. state& operator=(const state&);
  344. public:
  345. state(const Graph1& graph1, const Graph2& graph2,
  346. IndexMap1 index_map1, IndexMap2 index_map2,
  347. EdgeEquivalencePredicate edge_comp,
  348. VertexEquivalencePredicate vertex_comp)
  349. : graph1_(graph1), graph2_(graph2),
  350. index_map1_(index_map1),
  351. edge_comp_(edge_comp), vertex_comp_(vertex_comp),
  352. state1_(graph1, graph2, index_map1, index_map2),
  353. state2_(graph2, graph1, index_map2, index_map1) {}
  354. // Add vertex pair to the state
  355. void push(const vertex1_type& v, const vertex2_type& w) {
  356. state1_.push(v, w);
  357. state2_.push(w, v);
  358. }
  359. // Remove vertex pair from state
  360. void pop(const vertex1_type& v, const vertex2_type&) {
  361. vertex2_type w = state1_.core(v);
  362. state1_.pop(v, w);
  363. state2_.pop(w, v);
  364. }
  365. // Checks the feasibility of a new vertex pair
  366. bool feasible(const vertex1_type& v_new, const vertex2_type& w_new) {
  367. if (!vertex_comp_(v_new, w_new)) return false;
  368. // graph1
  369. graph1_size_type term_in1_count = 0, term_out1_count = 0, rest1_count = 0;
  370. {
  371. equivalent_edge_exists<Graph2> edge2_exists;
  372. BGL_FORALL_INEDGES_T(v_new, e1, graph1_, Graph1) {
  373. vertex1_type v = source(e1, graph1_);
  374. if (state1_.in_core(v) || (v == v_new)) {
  375. vertex2_type w = w_new;
  376. if (v != v_new)
  377. w = state1_.core(v);
  378. if (!edge2_exists(w, w_new,
  379. edge2_predicate<Graph1, Graph2, EdgeEquivalencePredicate>(edge_comp_, e1),
  380. graph2_))
  381. return false;
  382. } else {
  383. if (0 < state1_.in_depth(v))
  384. ++term_in1_count;
  385. if (0 < state1_.out_depth(v))
  386. ++term_out1_count;
  387. if ((state1_.in_depth(v) == 0) && (state1_.out_depth(v) == 0))
  388. ++rest1_count;
  389. }
  390. }
  391. }
  392. {
  393. equivalent_edge_exists<Graph2> edge2_exists;
  394. BGL_FORALL_OUTEDGES_T(v_new, e1, graph1_, Graph1) {
  395. vertex1_type v = target(e1, graph1_);
  396. if (state1_.in_core(v) || (v == v_new)) {
  397. vertex2_type w = w_new;
  398. if (v != v_new)
  399. w = state1_.core(v);
  400. if (!edge2_exists(w_new, w,
  401. edge2_predicate<Graph1, Graph2, EdgeEquivalencePredicate>(edge_comp_, e1),
  402. graph2_))
  403. return false;
  404. } else {
  405. if (0 < state1_.in_depth(v))
  406. ++term_in1_count;
  407. if (0 < state1_.out_depth(v))
  408. ++term_out1_count;
  409. if ((state1_.in_depth(v) == 0) && (state1_.out_depth(v) == 0))
  410. ++rest1_count;
  411. }
  412. }
  413. }
  414. // graph2
  415. graph2_size_type term_out2_count = 0, term_in2_count = 0, rest2_count = 0;
  416. {
  417. equivalent_edge_exists<Graph1> edge1_exists;
  418. BGL_FORALL_INEDGES_T(w_new, e2, graph2_, Graph2) {
  419. vertex2_type w = source(e2, graph2_);
  420. if (state2_.in_core(w) || (w == w_new)) {
  421. if (problem_selection != subgraph_mono) {
  422. vertex1_type v = v_new;
  423. if (w != w_new)
  424. v = state2_.core(w);
  425. if (!edge1_exists(v, v_new,
  426. edge1_predicate<Graph1, Graph2, EdgeEquivalencePredicate>(edge_comp_, e2),
  427. graph1_))
  428. return false;
  429. }
  430. } else {
  431. if (0 < state2_.in_depth(w))
  432. ++term_in2_count;
  433. if (0 < state2_.out_depth(w))
  434. ++term_out2_count;
  435. if ((state2_.in_depth(w) == 0) && (state2_.out_depth(w) == 0))
  436. ++rest2_count;
  437. }
  438. }
  439. }
  440. {
  441. equivalent_edge_exists<Graph1> edge1_exists;
  442. BGL_FORALL_OUTEDGES_T(w_new, e2, graph2_, Graph2) {
  443. vertex2_type w = target(e2, graph2_);
  444. if (state2_.in_core(w) || (w == w_new)) {
  445. if (problem_selection != subgraph_mono) {
  446. vertex1_type v = v_new;
  447. if (w != w_new)
  448. v = state2_.core(w);
  449. if (!edge1_exists(v_new, v,
  450. edge1_predicate<Graph1, Graph2, EdgeEquivalencePredicate>(edge_comp_, e2),
  451. graph1_))
  452. return false;
  453. }
  454. } else {
  455. if (0 < state2_.in_depth(w))
  456. ++term_in2_count;
  457. if (0 < state2_.out_depth(w))
  458. ++term_out2_count;
  459. if ((state2_.in_depth(w) == 0) && (state2_.out_depth(w) == 0))
  460. ++rest2_count;
  461. }
  462. }
  463. }
  464. if (problem_selection != subgraph_mono) { // subgraph_iso and isomorphism
  465. return comp_term_sets(term_in1_count, term_in2_count,
  466. boost::mpl::int_<problem_selection>()) &&
  467. comp_term_sets(term_out1_count, term_out2_count,
  468. boost::mpl::int_<problem_selection>()) &&
  469. comp_term_sets(rest1_count, rest2_count,
  470. boost::mpl::int_<problem_selection>());
  471. } else { // subgraph_mono
  472. return comp_term_sets(term_in1_count, term_in2_count,
  473. boost::mpl::int_<problem_selection>()) &&
  474. comp_term_sets(term_out1_count, term_out2_count,
  475. boost::mpl::int_<problem_selection>()) &&
  476. comp_term_sets(term_in1_count + term_out1_count + rest1_count,
  477. term_in2_count + term_out2_count + rest2_count,
  478. boost::mpl::int_<problem_selection>());
  479. }
  480. }
  481. // Returns true if vertex v in graph1 is a possible candidate to
  482. // be added to the current state
  483. bool possible_candidate1(const vertex1_type& v) const {
  484. if (state1_.term_both() && state2_.term_both())
  485. return state1_.term_both(v);
  486. else if (state1_.term_out() && state2_.term_out())
  487. return state1_.term_out(v);
  488. else if (state1_.term_in() && state2_.term_in())
  489. return state1_.term_in(v);
  490. else
  491. return !state1_.in_core(v);
  492. }
  493. // Returns true if vertex w in graph2 is a possible candidate to
  494. // be added to the current state
  495. bool possible_candidate2(const vertex2_type& w) const {
  496. if (state1_.term_both() && state2_.term_both())
  497. return state2_.term_both(w);
  498. else if (state1_.term_out() && state2_.term_out())
  499. return state2_.term_out(w);
  500. else if (state1_.term_in() && state2_.term_in())
  501. return state2_.term_in(w);
  502. else
  503. return !state2_.in_core(w);
  504. }
  505. // Returns true if a mapping was found
  506. bool success() const {
  507. return state1_.count() == num_vertices(graph1_);
  508. }
  509. // Returns true if a state is valid
  510. bool valid() const {
  511. boost::tuple<graph1_size_type, graph1_size_type, graph1_size_type> term1;
  512. boost::tuple<graph2_size_type, graph2_size_type, graph2_size_type> term2;
  513. term1 = state1_.term_set();
  514. term2 = state2_.term_set();
  515. return comp_term_sets(boost::get<0>(term1), boost::get<0>(term2),
  516. boost::mpl::int_<problem_selection>()) &&
  517. comp_term_sets(boost::get<1>(term1), boost::get<1>(term2),
  518. boost::mpl::int_<problem_selection>()) &&
  519. comp_term_sets(boost::get<2>(term1), boost::get<2>(term2),
  520. boost::mpl::int_<problem_selection>());
  521. }
  522. // Calls the user_callback with a graph (sub)graph mapping
  523. bool call_back(SubGraphIsoMapCallback user_callback) const {
  524. return user_callback(state1_.get_map(), state2_.get_map());
  525. }
  526. };
  527. // Data structure to keep info used for back tracking during
  528. // matching process
  529. template<typename Graph1,
  530. typename Graph2,
  531. typename VertexOrder1>
  532. struct vf2_match_continuation {
  533. typename VertexOrder1::const_iterator graph1_verts_iter;
  534. typename graph_traits<Graph2>::vertex_iterator graph2_verts_iter;
  535. };
  536. // Non-recursive method that explores state space using a depth-first
  537. // search strategy. At each depth possible pairs candidate are compute
  538. // and tested for feasibility to extend the mapping. If a complete
  539. // mapping is found, the mapping is output to user_callback in the form
  540. // of a correspondence map (graph1 to graph2). Returning false from the
  541. // user_callback will terminate the search. Function match will return
  542. // true if the entire search space was explored.
  543. template<typename Graph1,
  544. typename Graph2,
  545. typename IndexMap1,
  546. typename IndexMap2,
  547. typename VertexOrder1,
  548. typename EdgeEquivalencePredicate,
  549. typename VertexEquivalencePredicate,
  550. typename SubGraphIsoMapCallback,
  551. problem_selector problem_selection>
  552. bool match(const Graph1& graph1, const Graph2& graph2,
  553. SubGraphIsoMapCallback user_callback, const VertexOrder1& vertex_order1,
  554. state<Graph1, Graph2, IndexMap1, IndexMap2,
  555. EdgeEquivalencePredicate, VertexEquivalencePredicate,
  556. SubGraphIsoMapCallback, problem_selection>& s) {
  557. typename VertexOrder1::const_iterator graph1_verts_iter;
  558. typedef typename graph_traits<Graph2>::vertex_iterator vertex2_iterator_type;
  559. vertex2_iterator_type graph2_verts_iter, graph2_verts_iter_end;
  560. typedef vf2_match_continuation<Graph1, Graph2, VertexOrder1> match_continuation_type;
  561. std::vector<match_continuation_type> k;
  562. recur:
  563. if (s.success()) {
  564. if (!s.call_back(user_callback))
  565. return false;
  566. goto back_track;
  567. }
  568. if (!s.valid())
  569. goto back_track;
  570. graph1_verts_iter = vertex_order1.begin();
  571. while (graph1_verts_iter != vertex_order1.end() &&
  572. !s.possible_candidate1(*graph1_verts_iter)) {
  573. ++graph1_verts_iter;
  574. }
  575. boost::tie(graph2_verts_iter, graph2_verts_iter_end) = vertices(graph2);
  576. while (graph2_verts_iter != graph2_verts_iter_end) {
  577. if (s.possible_candidate2(*graph2_verts_iter)) {
  578. if (s.feasible(*graph1_verts_iter, *graph2_verts_iter)) {
  579. match_continuation_type kk;
  580. kk.graph1_verts_iter = graph1_verts_iter;
  581. kk.graph2_verts_iter = graph2_verts_iter;
  582. k.push_back(kk);
  583. s.push(*graph1_verts_iter, *graph2_verts_iter);
  584. goto recur;
  585. }
  586. }
  587. graph2_loop: ++graph2_verts_iter;
  588. }
  589. back_track:
  590. if (k.empty())
  591. return true;
  592. const match_continuation_type kk = k.back();
  593. graph1_verts_iter = kk.graph1_verts_iter;
  594. graph2_verts_iter = kk.graph2_verts_iter;
  595. k.pop_back();
  596. s.pop(*graph1_verts_iter, *graph2_verts_iter);
  597. goto graph2_loop;
  598. }
  599. // Used to sort nodes by in/out degrees
  600. template<typename Graph>
  601. struct vertex_in_out_degree_cmp {
  602. typedef typename graph_traits<Graph>::vertex_descriptor vertex_type;
  603. vertex_in_out_degree_cmp(const Graph& graph)
  604. : graph_(graph) {}
  605. bool operator()(const vertex_type& v, const vertex_type& w) const {
  606. // lexicographical comparison
  607. return std::make_pair(in_degree(v, graph_), out_degree(v, graph_)) <
  608. std::make_pair(in_degree(w, graph_), out_degree(w, graph_));
  609. }
  610. const Graph& graph_;
  611. };
  612. // Used to sort nodes by multiplicity of in/out degrees
  613. template<typename Graph,
  614. typename FrequencyMap>
  615. struct vertex_frequency_degree_cmp {
  616. typedef typename graph_traits<Graph>::vertex_descriptor vertex_type;
  617. vertex_frequency_degree_cmp(const Graph& graph, FrequencyMap freq)
  618. : graph_(graph), freq_(freq) {}
  619. bool operator()(const vertex_type& v, const vertex_type& w) const {
  620. // lexicographical comparison
  621. return std::make_pair(freq_[v], in_degree(v, graph_)+out_degree(v, graph_)) <
  622. std::make_pair(freq_[w], in_degree(w, graph_)+out_degree(w, graph_));
  623. }
  624. const Graph& graph_;
  625. FrequencyMap freq_;
  626. };
  627. // Sorts vertices of a graph by multiplicity of in/out degrees
  628. template<typename Graph,
  629. typename IndexMap,
  630. typename VertexOrder>
  631. void sort_vertices(const Graph& graph, IndexMap index_map, VertexOrder& order) {
  632. typedef typename graph_traits<Graph>::vertices_size_type size_type;
  633. boost::range::sort(order, vertex_in_out_degree_cmp<Graph>(graph));
  634. std::vector<size_type> freq_vec(num_vertices(graph), 0);
  635. typedef iterator_property_map<typename std::vector<size_type>::iterator,
  636. IndexMap, size_type, size_type&> frequency_map_type;
  637. frequency_map_type freq = make_iterator_property_map(freq_vec.begin(), index_map);
  638. typedef typename VertexOrder::iterator order_iterator;
  639. for (order_iterator order_iter = order.begin(); order_iter != order.end(); ) {
  640. size_type count = 0;
  641. for (order_iterator count_iter = order_iter;
  642. (count_iter != order.end()) &&
  643. (in_degree(*order_iter, graph) == in_degree(*count_iter, graph)) &&
  644. (out_degree(*order_iter, graph) == out_degree(*count_iter, graph));
  645. ++count_iter)
  646. ++count;
  647. for (size_type i = 0; i < count; ++i) {
  648. freq[*order_iter] = count;
  649. ++order_iter;
  650. }
  651. }
  652. boost::range::sort(order, vertex_frequency_degree_cmp<Graph, frequency_map_type>(graph, freq));
  653. }
  654. // Enumerates all graph sub-graph mono-/iso-morphism mappings between graphs
  655. // graph_small and graph_large. Continues until user_callback returns true or the
  656. // search space has been fully explored.
  657. template <problem_selector problem_selection,
  658. typename GraphSmall,
  659. typename GraphLarge,
  660. typename IndexMapSmall,
  661. typename IndexMapLarge,
  662. typename VertexOrderSmall,
  663. typename EdgeEquivalencePredicate,
  664. typename VertexEquivalencePredicate,
  665. typename SubGraphIsoMapCallback>
  666. bool vf2_subgraph_morphism(const GraphSmall& graph_small, const GraphLarge& graph_large,
  667. SubGraphIsoMapCallback user_callback,
  668. IndexMapSmall index_map_small, IndexMapLarge index_map_large,
  669. const VertexOrderSmall& vertex_order_small,
  670. EdgeEquivalencePredicate edge_comp,
  671. VertexEquivalencePredicate vertex_comp) {
  672. // Graph requirements
  673. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<GraphSmall> ));
  674. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<GraphSmall> ));
  675. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<GraphSmall> ));
  676. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<GraphSmall> ));
  677. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<GraphLarge> ));
  678. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<GraphLarge> ));
  679. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<GraphLarge> ));
  680. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<GraphLarge> ));
  681. typedef typename graph_traits<GraphSmall>::vertex_descriptor vertex_small_type;
  682. typedef typename graph_traits<GraphLarge>::vertex_descriptor vertex_large_type;
  683. typedef typename graph_traits<GraphSmall>::vertices_size_type size_type_small;
  684. typedef typename graph_traits<GraphLarge>::vertices_size_type size_type_large;
  685. // Property map requirements
  686. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMapSmall, vertex_small_type> ));
  687. typedef typename property_traits<IndexMapSmall>::value_type IndexMapSmallValue;
  688. BOOST_STATIC_ASSERT(( is_convertible<IndexMapSmallValue, size_type_small>::value ));
  689. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMapLarge, vertex_large_type> ));
  690. typedef typename property_traits<IndexMapLarge>::value_type IndexMapLargeValue;
  691. BOOST_STATIC_ASSERT(( is_convertible<IndexMapLargeValue, size_type_large>::value ));
  692. // Edge & vertex requirements
  693. typedef typename graph_traits<GraphSmall>::edge_descriptor edge_small_type;
  694. typedef typename graph_traits<GraphLarge>::edge_descriptor edge_large_type;
  695. BOOST_CONCEPT_ASSERT(( BinaryPredicateConcept<EdgeEquivalencePredicate,
  696. edge_small_type, edge_large_type> ));
  697. BOOST_CONCEPT_ASSERT(( BinaryPredicateConcept<VertexEquivalencePredicate,
  698. vertex_small_type, vertex_large_type> ));
  699. // Vertex order requirements
  700. BOOST_CONCEPT_ASSERT(( ContainerConcept<VertexOrderSmall> ));
  701. typedef typename VertexOrderSmall::value_type order_value_type;
  702. BOOST_STATIC_ASSERT(( is_same<vertex_small_type, order_value_type>::value ));
  703. BOOST_ASSERT( num_vertices(graph_small) == vertex_order_small.size() );
  704. if (num_vertices(graph_small) > num_vertices(graph_large))
  705. return false;
  706. typename graph_traits<GraphSmall>::edges_size_type num_edges_small = num_edges(graph_small);
  707. typename graph_traits<GraphLarge>::edges_size_type num_edges_large = num_edges(graph_large);
  708. // Double the number of edges for undirected graphs: each edge counts as
  709. // in-edge and out-edge
  710. if (is_undirected(graph_small)) num_edges_small *= 2;
  711. if (is_undirected(graph_large)) num_edges_large *= 2;
  712. if (num_edges_small > num_edges_large)
  713. return false;
  714. if ((num_vertices(graph_small) == 0) && (num_vertices(graph_large) == 0))
  715. return true;
  716. detail::state<GraphSmall, GraphLarge, IndexMapSmall, IndexMapLarge,
  717. EdgeEquivalencePredicate, VertexEquivalencePredicate,
  718. SubGraphIsoMapCallback, problem_selection>
  719. s(graph_small, graph_large, index_map_small, index_map_large, edge_comp, vertex_comp);
  720. return detail::match(graph_small, graph_large, user_callback, vertex_order_small, s);
  721. }
  722. } // namespace detail
  723. // Returns vertex order (vertices sorted by multiplicity of in/out degrees)
  724. template<typename Graph>
  725. std::vector<typename graph_traits<Graph>::vertex_descriptor>
  726. vertex_order_by_mult(const Graph& graph) {
  727. std::vector<typename graph_traits<Graph>::vertex_descriptor> vertex_order;
  728. std::copy(vertices(graph).first, vertices(graph).second, std::back_inserter(vertex_order));
  729. detail::sort_vertices(graph, get(vertex_index, graph), vertex_order);
  730. return vertex_order;
  731. }
  732. // Enumerates all graph sub-graph monomorphism mappings between graphs
  733. // graph_small and graph_large. Continues until user_callback returns true or the
  734. // search space has been fully explored.
  735. template <typename GraphSmall,
  736. typename GraphLarge,
  737. typename IndexMapSmall,
  738. typename IndexMapLarge,
  739. typename VertexOrderSmall,
  740. typename EdgeEquivalencePredicate,
  741. typename VertexEquivalencePredicate,
  742. typename SubGraphIsoMapCallback>
  743. bool vf2_subgraph_mono(const GraphSmall& graph_small, const GraphLarge& graph_large,
  744. SubGraphIsoMapCallback user_callback,
  745. IndexMapSmall index_map_small, IndexMapLarge index_map_large,
  746. const VertexOrderSmall& vertex_order_small,
  747. EdgeEquivalencePredicate edge_comp,
  748. VertexEquivalencePredicate vertex_comp) {
  749. return detail::vf2_subgraph_morphism<detail::subgraph_mono>
  750. (graph_small, graph_large,
  751. user_callback,
  752. index_map_small, index_map_large,
  753. vertex_order_small,
  754. edge_comp,
  755. vertex_comp);
  756. }
  757. // All default interface for vf2_subgraph_iso
  758. template <typename GraphSmall,
  759. typename GraphLarge,
  760. typename SubGraphIsoMapCallback>
  761. bool vf2_subgraph_mono(const GraphSmall& graph_small, const GraphLarge& graph_large,
  762. SubGraphIsoMapCallback user_callback) {
  763. return vf2_subgraph_mono(graph_small, graph_large, user_callback,
  764. get(vertex_index, graph_small), get(vertex_index, graph_large),
  765. vertex_order_by_mult(graph_small),
  766. always_equivalent(), always_equivalent());
  767. }
  768. // Named parameter interface of vf2_subgraph_iso
  769. template <typename GraphSmall,
  770. typename GraphLarge,
  771. typename VertexOrderSmall,
  772. typename SubGraphIsoMapCallback,
  773. typename Param,
  774. typename Tag,
  775. typename Rest>
  776. bool vf2_subgraph_mono(const GraphSmall& graph_small, const GraphLarge& graph_large,
  777. SubGraphIsoMapCallback user_callback,
  778. const VertexOrderSmall& vertex_order_small,
  779. const bgl_named_params<Param, Tag, Rest>& params) {
  780. return vf2_subgraph_mono(graph_small, graph_large, user_callback,
  781. choose_const_pmap(get_param(params, vertex_index1),
  782. graph_small, vertex_index),
  783. choose_const_pmap(get_param(params, vertex_index2),
  784. graph_large, vertex_index),
  785. vertex_order_small,
  786. choose_param(get_param(params, edges_equivalent_t()),
  787. always_equivalent()),
  788. choose_param(get_param(params, vertices_equivalent_t()),
  789. always_equivalent())
  790. );
  791. }
  792. // Enumerates all graph sub-graph isomorphism mappings between graphs
  793. // graph_small and graph_large. Continues until user_callback returns true or the
  794. // search space has been fully explored.
  795. template <typename GraphSmall,
  796. typename GraphLarge,
  797. typename IndexMapSmall,
  798. typename IndexMapLarge,
  799. typename VertexOrderSmall,
  800. typename EdgeEquivalencePredicate,
  801. typename VertexEquivalencePredicate,
  802. typename SubGraphIsoMapCallback>
  803. bool vf2_subgraph_iso(const GraphSmall& graph_small, const GraphLarge& graph_large,
  804. SubGraphIsoMapCallback user_callback,
  805. IndexMapSmall index_map_small, IndexMapLarge index_map_large,
  806. const VertexOrderSmall& vertex_order_small,
  807. EdgeEquivalencePredicate edge_comp,
  808. VertexEquivalencePredicate vertex_comp) {
  809. return detail::vf2_subgraph_morphism<detail::subgraph_iso>
  810. (graph_small, graph_large,
  811. user_callback,
  812. index_map_small, index_map_large,
  813. vertex_order_small,
  814. edge_comp,
  815. vertex_comp);
  816. }
  817. // All default interface for vf2_subgraph_iso
  818. template <typename GraphSmall,
  819. typename GraphLarge,
  820. typename SubGraphIsoMapCallback>
  821. bool vf2_subgraph_iso(const GraphSmall& graph_small, const GraphLarge& graph_large,
  822. SubGraphIsoMapCallback user_callback) {
  823. return vf2_subgraph_iso(graph_small, graph_large, user_callback,
  824. get(vertex_index, graph_small), get(vertex_index, graph_large),
  825. vertex_order_by_mult(graph_small),
  826. always_equivalent(), always_equivalent());
  827. }
  828. // Named parameter interface of vf2_subgraph_iso
  829. template <typename GraphSmall,
  830. typename GraphLarge,
  831. typename VertexOrderSmall,
  832. typename SubGraphIsoMapCallback,
  833. typename Param,
  834. typename Tag,
  835. typename Rest>
  836. bool vf2_subgraph_iso(const GraphSmall& graph_small, const GraphLarge& graph_large,
  837. SubGraphIsoMapCallback user_callback,
  838. const VertexOrderSmall& vertex_order_small,
  839. const bgl_named_params<Param, Tag, Rest>& params) {
  840. return vf2_subgraph_iso(graph_small, graph_large, user_callback,
  841. choose_const_pmap(get_param(params, vertex_index1),
  842. graph_small, vertex_index),
  843. choose_const_pmap(get_param(params, vertex_index2),
  844. graph_large, vertex_index),
  845. vertex_order_small,
  846. choose_param(get_param(params, edges_equivalent_t()),
  847. always_equivalent()),
  848. choose_param(get_param(params, vertices_equivalent_t()),
  849. always_equivalent())
  850. );
  851. }
  852. // Enumerates all isomorphism mappings between graphs graph1_ and graph2_.
  853. // Continues until user_callback returns true or the search space has been
  854. // fully explored.
  855. template <typename Graph1,
  856. typename Graph2,
  857. typename IndexMap1,
  858. typename IndexMap2,
  859. typename VertexOrder1,
  860. typename EdgeEquivalencePredicate,
  861. typename VertexEquivalencePredicate,
  862. typename GraphIsoMapCallback>
  863. bool vf2_graph_iso(const Graph1& graph1, const Graph2& graph2,
  864. GraphIsoMapCallback user_callback,
  865. IndexMap1 index_map1, IndexMap2 index_map2,
  866. const VertexOrder1& vertex_order1,
  867. EdgeEquivalencePredicate edge_comp,
  868. VertexEquivalencePredicate vertex_comp) {
  869. // Graph requirements
  870. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<Graph1> ));
  871. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph1> ));
  872. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<Graph1> ));
  873. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<Graph1> ));
  874. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<Graph2> ));
  875. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph2> ));
  876. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<Graph2> ));
  877. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<Graph2> ));
  878. typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_type;
  879. typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_type;
  880. typedef typename graph_traits<Graph1>::vertices_size_type size_type1;
  881. typedef typename graph_traits<Graph2>::vertices_size_type size_type2;
  882. // Property map requirements
  883. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMap1, vertex1_type> ));
  884. typedef typename property_traits<IndexMap1>::value_type IndexMap1Value;
  885. BOOST_STATIC_ASSERT(( is_convertible<IndexMap1Value, size_type1>::value ));
  886. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMap2, vertex2_type> ));
  887. typedef typename property_traits<IndexMap2>::value_type IndexMap2Value;
  888. BOOST_STATIC_ASSERT(( is_convertible<IndexMap2Value, size_type2>::value ));
  889. // Edge & vertex requirements
  890. typedef typename graph_traits<Graph1>::edge_descriptor edge1_type;
  891. typedef typename graph_traits<Graph2>::edge_descriptor edge2_type;
  892. BOOST_CONCEPT_ASSERT(( BinaryPredicateConcept<EdgeEquivalencePredicate,
  893. edge1_type, edge2_type> ));
  894. BOOST_CONCEPT_ASSERT(( BinaryPredicateConcept<VertexEquivalencePredicate,
  895. vertex1_type, vertex2_type> ));
  896. // Vertex order requirements
  897. BOOST_CONCEPT_ASSERT(( ContainerConcept<VertexOrder1> ));
  898. typedef typename VertexOrder1::value_type order_value_type;
  899. BOOST_STATIC_ASSERT(( is_same<vertex1_type, order_value_type>::value ));
  900. BOOST_ASSERT( num_vertices(graph1) == vertex_order1.size() );
  901. if (num_vertices(graph1) != num_vertices(graph2))
  902. return false;
  903. typename graph_traits<Graph1>::edges_size_type num_edges1 = num_edges(graph1);
  904. typename graph_traits<Graph2>::edges_size_type num_edges2 = num_edges(graph2);
  905. // Double the number of edges for undirected graphs: each edge counts as
  906. // in-edge and out-edge
  907. if (is_undirected(graph1)) num_edges1 *= 2;
  908. if (is_undirected(graph2)) num_edges2 *= 2;
  909. if (num_edges1 != num_edges2)
  910. return false;
  911. if ((num_vertices(graph1) == 0) && (num_vertices(graph2) == 0))
  912. return true;
  913. detail::state<Graph1, Graph2, IndexMap1, IndexMap2,
  914. EdgeEquivalencePredicate, VertexEquivalencePredicate,
  915. GraphIsoMapCallback, detail::isomorphism>
  916. s(graph1, graph2, index_map1, index_map2, edge_comp, vertex_comp);
  917. return detail::match(graph1, graph2, user_callback, vertex_order1, s);
  918. }
  919. // All default interface for vf2_graph_iso
  920. template <typename Graph1,
  921. typename Graph2,
  922. typename GraphIsoMapCallback>
  923. bool vf2_graph_iso(const Graph1& graph1, const Graph2& graph2,
  924. GraphIsoMapCallback user_callback) {
  925. return vf2_graph_iso(graph1, graph2, user_callback,
  926. get(vertex_index, graph1), get(vertex_index, graph2),
  927. vertex_order_by_mult(graph1),
  928. always_equivalent(), always_equivalent());
  929. }
  930. // Named parameter interface of vf2_graph_iso
  931. template <typename Graph1,
  932. typename Graph2,
  933. typename VertexOrder1,
  934. typename GraphIsoMapCallback,
  935. typename Param,
  936. typename Tag,
  937. typename Rest>
  938. bool vf2_graph_iso(const Graph1& graph1, const Graph2& graph2,
  939. GraphIsoMapCallback user_callback,
  940. const VertexOrder1& vertex_order1,
  941. const bgl_named_params<Param, Tag, Rest>& params) {
  942. return vf2_graph_iso(graph1, graph2, user_callback,
  943. choose_const_pmap(get_param(params, vertex_index1),
  944. graph1, vertex_index),
  945. choose_const_pmap(get_param(params, vertex_index2),
  946. graph2, vertex_index),
  947. vertex_order1,
  948. choose_param(get_param(params, edges_equivalent_t()),
  949. always_equivalent()),
  950. choose_param(get_param(params, vertices_equivalent_t()),
  951. always_equivalent())
  952. );
  953. }
  954. // Verifies a graph (sub)graph isomorphism map
  955. template<typename Graph1,
  956. typename Graph2,
  957. typename CorresponenceMap1To2,
  958. typename EdgeEquivalencePredicate,
  959. typename VertexEquivalencePredicate>
  960. inline bool verify_vf2_subgraph_iso(const Graph1& graph1, const Graph2& graph2,
  961. const CorresponenceMap1To2 f,
  962. EdgeEquivalencePredicate edge_comp,
  963. VertexEquivalencePredicate vertex_comp) {
  964. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<Graph1> ));
  965. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<Graph2> ));
  966. detail::equivalent_edge_exists<Graph2> edge2_exists;
  967. BGL_FORALL_EDGES_T(e1, graph1, Graph1) {
  968. typename graph_traits<Graph1>::vertex_descriptor s1, t1;
  969. typename graph_traits<Graph2>::vertex_descriptor s2, t2;
  970. s1 = source(e1, graph1); t1 = target(e1, graph1);
  971. s2 = get(f, s1); t2 = get(f, t1);
  972. if (!vertex_comp(s1, s2) || !vertex_comp(t1, t2))
  973. return false;
  974. typename graph_traits<Graph2>::edge_descriptor e2;
  975. if (!edge2_exists(s2, t2,
  976. detail::edge2_predicate<Graph1, Graph2, EdgeEquivalencePredicate>(edge_comp, e1),
  977. graph2))
  978. return false;
  979. }
  980. return true;
  981. }
  982. // Variant of verify_subgraph_iso with all default parameters
  983. template<typename Graph1,
  984. typename Graph2,
  985. typename CorresponenceMap1To2>
  986. inline bool verify_vf2_subgraph_iso(const Graph1& graph1, const Graph2& graph2,
  987. const CorresponenceMap1To2 f) {
  988. return verify_vf2_subgraph_iso(graph1, graph2, f,
  989. always_equivalent(), always_equivalent());
  990. }
  991. } // namespace boost
  992. #ifdef BOOST_ISO_INCLUDED_ITER_MACROS
  993. #undef BOOST_ISO_INCLUDED_ITER_MACROS
  994. #include <boost/graph/iteration_macros_undef.hpp>
  995. #endif
  996. #endif // BOOST_VF2_SUB_GRAPH_ISO_HPP