vf2_sub_graph_iso.hpp 47 KB

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