vf2_sub_graph_iso.hpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. core_vec_(num_vertices(graph_this_), graph_traits<GraphOther>::null_vertex()),
  92. core_(core_vec_.begin(), index_map_this_),
  93. in_vec_(num_vertices(graph_this_), 0),
  94. out_vec_(num_vertices(graph_this_), 0),
  95. in_(in_vec_.begin(), index_map_this_),
  96. out_(out_vec_.begin(), index_map_this_),
  97. term_in_count_(0), term_out_count_(0), term_both_count_(0), core_count_(0) {
  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. bool found_match = false;
  563. recur:
  564. if (s.success()) {
  565. if (!s.call_back(user_callback))
  566. return true;
  567. found_match = true;
  568. goto back_track;
  569. }
  570. if (!s.valid())
  571. goto back_track;
  572. graph1_verts_iter = vertex_order1.begin();
  573. while (graph1_verts_iter != vertex_order1.end() &&
  574. !s.possible_candidate1(*graph1_verts_iter)) {
  575. ++graph1_verts_iter;
  576. }
  577. boost::tie(graph2_verts_iter, graph2_verts_iter_end) = vertices(graph2);
  578. while (graph2_verts_iter != graph2_verts_iter_end) {
  579. if (s.possible_candidate2(*graph2_verts_iter)) {
  580. if (s.feasible(*graph1_verts_iter, *graph2_verts_iter)) {
  581. match_continuation_type kk;
  582. kk.graph1_verts_iter = graph1_verts_iter;
  583. kk.graph2_verts_iter = graph2_verts_iter;
  584. k.push_back(kk);
  585. s.push(*graph1_verts_iter, *graph2_verts_iter);
  586. goto recur;
  587. }
  588. }
  589. graph2_loop: ++graph2_verts_iter;
  590. }
  591. back_track:
  592. if (k.empty())
  593. return found_match;
  594. const match_continuation_type kk = k.back();
  595. graph1_verts_iter = kk.graph1_verts_iter;
  596. graph2_verts_iter = kk.graph2_verts_iter;
  597. k.pop_back();
  598. s.pop(*graph1_verts_iter, *graph2_verts_iter);
  599. goto graph2_loop;
  600. }
  601. // Used to sort nodes by in/out degrees
  602. template<typename Graph>
  603. struct vertex_in_out_degree_cmp {
  604. typedef typename graph_traits<Graph>::vertex_descriptor vertex_type;
  605. vertex_in_out_degree_cmp(const Graph& graph)
  606. : graph_(graph) {}
  607. bool operator()(const vertex_type& v, const vertex_type& w) const {
  608. // lexicographical comparison
  609. return std::make_pair(in_degree(v, graph_), out_degree(v, graph_)) <
  610. std::make_pair(in_degree(w, graph_), out_degree(w, graph_));
  611. }
  612. const Graph& graph_;
  613. };
  614. // Used to sort nodes by multiplicity of in/out degrees
  615. template<typename Graph,
  616. typename FrequencyMap>
  617. struct vertex_frequency_degree_cmp {
  618. typedef typename graph_traits<Graph>::vertex_descriptor vertex_type;
  619. vertex_frequency_degree_cmp(const Graph& graph, FrequencyMap freq)
  620. : graph_(graph), freq_(freq) {}
  621. bool operator()(const vertex_type& v, const vertex_type& w) const {
  622. // lexicographical comparison
  623. return std::make_pair(freq_[v], in_degree(v, graph_)+out_degree(v, graph_)) <
  624. std::make_pair(freq_[w], in_degree(w, graph_)+out_degree(w, graph_));
  625. }
  626. const Graph& graph_;
  627. FrequencyMap freq_;
  628. };
  629. // Sorts vertices of a graph by multiplicity of in/out degrees
  630. template<typename Graph,
  631. typename IndexMap,
  632. typename VertexOrder>
  633. void sort_vertices(const Graph& graph, IndexMap index_map, VertexOrder& order) {
  634. typedef typename graph_traits<Graph>::vertices_size_type size_type;
  635. boost::range::sort(order, vertex_in_out_degree_cmp<Graph>(graph));
  636. std::vector<size_type> freq_vec(num_vertices(graph), 0);
  637. typedef iterator_property_map<typename std::vector<size_type>::iterator,
  638. IndexMap, size_type, size_type&> frequency_map_type;
  639. frequency_map_type freq = make_iterator_property_map(freq_vec.begin(), index_map);
  640. typedef typename VertexOrder::iterator order_iterator;
  641. for (order_iterator order_iter = order.begin(); order_iter != order.end(); ) {
  642. size_type count = 0;
  643. for (order_iterator count_iter = order_iter;
  644. (count_iter != order.end()) &&
  645. (in_degree(*order_iter, graph) == in_degree(*count_iter, graph)) &&
  646. (out_degree(*order_iter, graph) == out_degree(*count_iter, graph));
  647. ++count_iter)
  648. ++count;
  649. for (size_type i = 0; i < count; ++i) {
  650. freq[*order_iter] = count;
  651. ++order_iter;
  652. }
  653. }
  654. boost::range::sort(order, vertex_frequency_degree_cmp<Graph, frequency_map_type>(graph, freq));
  655. }
  656. // Enumerates all graph sub-graph mono-/iso-morphism mappings between graphs
  657. // graph_small and graph_large. Continues until user_callback returns true or the
  658. // search space has been fully explored.
  659. template <problem_selector problem_selection,
  660. typename GraphSmall,
  661. typename GraphLarge,
  662. typename IndexMapSmall,
  663. typename IndexMapLarge,
  664. typename VertexOrderSmall,
  665. typename EdgeEquivalencePredicate,
  666. typename VertexEquivalencePredicate,
  667. typename SubGraphIsoMapCallback>
  668. bool vf2_subgraph_morphism(const GraphSmall& graph_small, const GraphLarge& graph_large,
  669. SubGraphIsoMapCallback user_callback,
  670. IndexMapSmall index_map_small, IndexMapLarge index_map_large,
  671. const VertexOrderSmall& vertex_order_small,
  672. EdgeEquivalencePredicate edge_comp,
  673. VertexEquivalencePredicate vertex_comp) {
  674. // Graph requirements
  675. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<GraphSmall> ));
  676. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<GraphSmall> ));
  677. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<GraphSmall> ));
  678. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<GraphSmall> ));
  679. BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<GraphLarge> ));
  680. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<GraphLarge> ));
  681. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<GraphLarge> ));
  682. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<GraphLarge> ));
  683. typedef typename graph_traits<GraphSmall>::vertex_descriptor vertex_small_type;
  684. typedef typename graph_traits<GraphLarge>::vertex_descriptor vertex_large_type;
  685. typedef typename graph_traits<GraphSmall>::vertices_size_type size_type_small;
  686. typedef typename graph_traits<GraphLarge>::vertices_size_type size_type_large;
  687. // Property map requirements
  688. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMapSmall, vertex_small_type> ));
  689. typedef typename property_traits<IndexMapSmall>::value_type IndexMapSmallValue;
  690. BOOST_STATIC_ASSERT(( is_convertible<IndexMapSmallValue, size_type_small>::value ));
  691. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMapLarge, vertex_large_type> ));
  692. typedef typename property_traits<IndexMapLarge>::value_type IndexMapLargeValue;
  693. BOOST_STATIC_ASSERT(( is_convertible<IndexMapLargeValue, size_type_large>::value ));
  694. // Edge & vertex requirements
  695. typedef typename graph_traits<GraphSmall>::edge_descriptor edge_small_type;
  696. typedef typename graph_traits<GraphLarge>::edge_descriptor edge_large_type;
  697. BOOST_CONCEPT_ASSERT(( BinaryPredicateConcept<EdgeEquivalencePredicate,
  698. edge_small_type, edge_large_type> ));
  699. BOOST_CONCEPT_ASSERT(( BinaryPredicateConcept<VertexEquivalencePredicate,
  700. vertex_small_type, vertex_large_type> ));
  701. // Vertex order requirements
  702. BOOST_CONCEPT_ASSERT(( ContainerConcept<VertexOrderSmall> ));
  703. typedef typename VertexOrderSmall::value_type order_value_type;
  704. BOOST_STATIC_ASSERT(( is_same<vertex_small_type, order_value_type>::value ));
  705. BOOST_ASSERT( num_vertices(graph_small) == vertex_order_small.size() );
  706. if (num_vertices(graph_small) > num_vertices(graph_large))
  707. return false;
  708. typename graph_traits<GraphSmall>::edges_size_type num_edges_small = num_edges(graph_small);
  709. typename graph_traits<GraphLarge>::edges_size_type num_edges_large = num_edges(graph_large);
  710. // Double the number of edges for undirected graphs: each edge counts as
  711. // in-edge and out-edge
  712. if (is_undirected(graph_small)) num_edges_small *= 2;
  713. if (is_undirected(graph_large)) num_edges_large *= 2;
  714. if (num_edges_small > num_edges_large)
  715. return false;
  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. detail::state<Graph1, Graph2, IndexMap1, IndexMap2,
  912. EdgeEquivalencePredicate, VertexEquivalencePredicate,
  913. GraphIsoMapCallback, detail::isomorphism>
  914. s(graph1, graph2, index_map1, index_map2, edge_comp, vertex_comp);
  915. return detail::match(graph1, graph2, user_callback, vertex_order1, s);
  916. }
  917. // All default interface for vf2_graph_iso
  918. template <typename Graph1,
  919. typename Graph2,
  920. typename GraphIsoMapCallback>
  921. bool vf2_graph_iso(const Graph1& graph1, const Graph2& graph2,
  922. GraphIsoMapCallback user_callback) {
  923. return vf2_graph_iso(graph1, graph2, user_callback,
  924. get(vertex_index, graph1), get(vertex_index, graph2),
  925. vertex_order_by_mult(graph1),
  926. always_equivalent(), always_equivalent());
  927. }
  928. // Named parameter interface of vf2_graph_iso
  929. template <typename Graph1,
  930. typename Graph2,
  931. typename VertexOrder1,
  932. typename GraphIsoMapCallback,
  933. typename Param,
  934. typename Tag,
  935. typename Rest>
  936. bool vf2_graph_iso(const Graph1& graph1, const Graph2& graph2,
  937. GraphIsoMapCallback user_callback,
  938. const VertexOrder1& vertex_order1,
  939. const bgl_named_params<Param, Tag, Rest>& params) {
  940. return vf2_graph_iso(graph1, graph2, user_callback,
  941. choose_const_pmap(get_param(params, vertex_index1),
  942. graph1, vertex_index),
  943. choose_const_pmap(get_param(params, vertex_index2),
  944. graph2, vertex_index),
  945. vertex_order1,
  946. choose_param(get_param(params, edges_equivalent_t()),
  947. always_equivalent()),
  948. choose_param(get_param(params, vertices_equivalent_t()),
  949. always_equivalent())
  950. );
  951. }
  952. // Verifies a graph (sub)graph isomorphism map
  953. template<typename Graph1,
  954. typename Graph2,
  955. typename CorresponenceMap1To2,
  956. typename EdgeEquivalencePredicate,
  957. typename VertexEquivalencePredicate>
  958. inline bool verify_vf2_subgraph_iso(const Graph1& graph1, const Graph2& graph2,
  959. const CorresponenceMap1To2 f,
  960. EdgeEquivalencePredicate edge_comp,
  961. VertexEquivalencePredicate vertex_comp) {
  962. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<Graph1> ));
  963. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<Graph2> ));
  964. detail::equivalent_edge_exists<Graph2> edge2_exists;
  965. BGL_FORALL_EDGES_T(e1, graph1, Graph1) {
  966. typename graph_traits<Graph1>::vertex_descriptor s1, t1;
  967. typename graph_traits<Graph2>::vertex_descriptor s2, t2;
  968. s1 = source(e1, graph1); t1 = target(e1, graph1);
  969. s2 = get(f, s1); t2 = get(f, t1);
  970. if (!vertex_comp(s1, s2) || !vertex_comp(t1, t2))
  971. return false;
  972. typename graph_traits<Graph2>::edge_descriptor e2;
  973. if (!edge2_exists(s2, t2,
  974. detail::edge2_predicate<Graph1, Graph2, EdgeEquivalencePredicate>(edge_comp, e1),
  975. graph2))
  976. return false;
  977. }
  978. return true;
  979. }
  980. // Variant of verify_subgraph_iso with all default parameters
  981. template<typename Graph1,
  982. typename Graph2,
  983. typename CorresponenceMap1To2>
  984. inline bool verify_vf2_subgraph_iso(const Graph1& graph1, const Graph2& graph2,
  985. const CorresponenceMap1To2 f) {
  986. return verify_vf2_subgraph_iso(graph1, graph2, f,
  987. always_equivalent(), always_equivalent());
  988. }
  989. } // namespace boost
  990. #ifdef BOOST_ISO_INCLUDED_ITER_MACROS
  991. #undef BOOST_ISO_INCLUDED_ITER_MACROS
  992. #include <boost/graph/iteration_macros_undef.hpp>
  993. #endif
  994. #endif // BOOST_VF2_SUB_GRAPH_ISO_HPP