isomorphism.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // Copyright (C) 2001 Jeremy Siek, Douglas Gregor, Brian Osman
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_ISOMORPHISM_HPP
  7. #define BOOST_GRAPH_ISOMORPHISM_HPP
  8. #include <utility>
  9. #include <vector>
  10. #include <iterator>
  11. #include <algorithm>
  12. #include <boost/config.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/smart_ptr.hpp>
  15. #include <boost/graph/depth_first_search.hpp>
  16. #include <boost/detail/algorithm.hpp>
  17. #include <boost/pending/indirect_cmp.hpp> // for make_indirect_pmap
  18. #include <boost/concept/assert.hpp>
  19. #ifndef BOOST_GRAPH_ITERATION_MACROS_HPP
  20. #define BOOST_ISO_INCLUDED_ITER_MACROS // local macro, see bottom of file
  21. #include <boost/graph/iteration_macros.hpp>
  22. #endif
  23. namespace boost {
  24. namespace detail {
  25. template <typename Graph1, typename Graph2, typename IsoMapping,
  26. typename Invariant1, typename Invariant2,
  27. typename IndexMap1, typename IndexMap2>
  28. class isomorphism_algo
  29. {
  30. typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_t;
  31. typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
  32. typedef typename graph_traits<Graph1>::edge_descriptor edge1_t;
  33. typedef typename graph_traits<Graph1>::vertices_size_type size_type;
  34. typedef typename Invariant1::result_type invar1_value;
  35. typedef typename Invariant2::result_type invar2_value;
  36. const Graph1& G1;
  37. const Graph2& G2;
  38. IsoMapping f;
  39. Invariant1 invariant1;
  40. Invariant2 invariant2;
  41. std::size_t max_invariant;
  42. IndexMap1 index_map1;
  43. IndexMap2 index_map2;
  44. std::vector<vertex1_t> dfs_vertices;
  45. typedef typename std::vector<vertex1_t>::iterator vertex_iter;
  46. std::vector<int> dfs_num_vec;
  47. typedef safe_iterator_property_map<typename std::vector<int>::iterator,
  48. IndexMap1
  49. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  50. , int, int&
  51. #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
  52. > DFSNumMap;
  53. DFSNumMap dfs_num;
  54. std::vector<edge1_t> ordered_edges;
  55. typedef typename std::vector<edge1_t>::iterator edge_iter;
  56. std::vector<char> in_S_vec;
  57. typedef safe_iterator_property_map<typename std::vector<char>::iterator,
  58. IndexMap2
  59. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  60. , char, char&
  61. #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
  62. > InSMap;
  63. InSMap in_S;
  64. int num_edges_on_k;
  65. friend struct compare_multiplicity;
  66. struct compare_multiplicity
  67. {
  68. compare_multiplicity(Invariant1 invariant1, size_type* multiplicity)
  69. : invariant1(invariant1), multiplicity(multiplicity) { }
  70. bool operator()(const vertex1_t& x, const vertex1_t& y) const {
  71. return multiplicity[invariant1(x)] < multiplicity[invariant1(y)];
  72. }
  73. Invariant1 invariant1;
  74. size_type* multiplicity;
  75. };
  76. struct record_dfs_order : default_dfs_visitor
  77. {
  78. record_dfs_order(std::vector<vertex1_t>& v, std::vector<edge1_t>& e)
  79. : vertices(v), edges(e) { }
  80. void discover_vertex(vertex1_t v, const Graph1&) const {
  81. vertices.push_back(v);
  82. }
  83. void examine_edge(edge1_t e, const Graph1&) const {
  84. edges.push_back(e);
  85. }
  86. std::vector<vertex1_t>& vertices;
  87. std::vector<edge1_t>& edges;
  88. };
  89. struct edge_cmp {
  90. edge_cmp(const Graph1& G1, DFSNumMap dfs_num)
  91. : G1(G1), dfs_num(dfs_num) { }
  92. bool operator()(const edge1_t& e1, const edge1_t& e2) const {
  93. using namespace std;
  94. int u1 = dfs_num[source(e1,G1)], v1 = dfs_num[target(e1,G1)];
  95. int u2 = dfs_num[source(e2,G1)], v2 = dfs_num[target(e2,G1)];
  96. int m1 = (max)(u1, v1);
  97. int m2 = (max)(u2, v2);
  98. // lexicographical comparison
  99. return std::make_pair(m1, std::make_pair(u1, v1))
  100. < std::make_pair(m2, std::make_pair(u2, v2));
  101. }
  102. const Graph1& G1;
  103. DFSNumMap dfs_num;
  104. };
  105. public:
  106. isomorphism_algo(const Graph1& G1, const Graph2& G2, IsoMapping f,
  107. Invariant1 invariant1, Invariant2 invariant2, std::size_t max_invariant,
  108. IndexMap1 index_map1, IndexMap2 index_map2)
  109. : G1(G1), G2(G2), f(f), invariant1(invariant1), invariant2(invariant2),
  110. max_invariant(max_invariant),
  111. index_map1(index_map1), index_map2(index_map2)
  112. {
  113. in_S_vec.resize(num_vertices(G1));
  114. in_S = make_safe_iterator_property_map
  115. (in_S_vec.begin(), in_S_vec.size(), index_map2
  116. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  117. , in_S_vec.front()
  118. #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
  119. );
  120. }
  121. bool test_isomorphism()
  122. {
  123. // reset isomapping
  124. BGL_FORALL_VERTICES_T(v, G1, Graph1)
  125. f[v] = graph_traits<Graph2>::null_vertex();
  126. {
  127. std::vector<invar1_value> invar1_array;
  128. BGL_FORALL_VERTICES_T(v, G1, Graph1)
  129. invar1_array.push_back(invariant1(v));
  130. sort(invar1_array);
  131. std::vector<invar2_value> invar2_array;
  132. BGL_FORALL_VERTICES_T(v, G2, Graph2)
  133. invar2_array.push_back(invariant2(v));
  134. sort(invar2_array);
  135. if (! equal(invar1_array, invar2_array))
  136. return false;
  137. }
  138. std::vector<vertex1_t> V_mult;
  139. BGL_FORALL_VERTICES_T(v, G1, Graph1)
  140. V_mult.push_back(v);
  141. {
  142. std::vector<size_type> multiplicity(max_invariant, 0);
  143. BGL_FORALL_VERTICES_T(v, G1, Graph1)
  144. ++multiplicity.at(invariant1(v));
  145. sort(V_mult, compare_multiplicity(invariant1, &multiplicity[0]));
  146. }
  147. std::vector<default_color_type> color_vec(num_vertices(G1));
  148. safe_iterator_property_map<std::vector<default_color_type>::iterator,
  149. IndexMap1
  150. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  151. , default_color_type, default_color_type&
  152. #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
  153. >
  154. color_map(color_vec.begin(), color_vec.size(), index_map1);
  155. record_dfs_order dfs_visitor(dfs_vertices, ordered_edges);
  156. typedef color_traits<default_color_type> Color;
  157. for (vertex_iter u = V_mult.begin(); u != V_mult.end(); ++u) {
  158. if (color_map[*u] == Color::white()) {
  159. dfs_visitor.start_vertex(*u, G1);
  160. depth_first_visit(G1, *u, dfs_visitor, color_map);
  161. }
  162. }
  163. // Create the dfs_num array and dfs_num_map
  164. dfs_num_vec.resize(num_vertices(G1));
  165. dfs_num = make_safe_iterator_property_map(dfs_num_vec.begin(),
  166. dfs_num_vec.size(),
  167. index_map1
  168. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  169. , dfs_num_vec.front()
  170. #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
  171. );
  172. size_type n = 0;
  173. for (vertex_iter v = dfs_vertices.begin(); v != dfs_vertices.end(); ++v)
  174. dfs_num[*v] = n++;
  175. sort(ordered_edges, edge_cmp(G1, dfs_num));
  176. int dfs_num_k = -1;
  177. return this->match(ordered_edges.begin(), dfs_num_k);
  178. }
  179. private:
  180. struct match_continuation {
  181. enum {pos_G2_vertex_loop, pos_fi_adj_loop, pos_dfs_num} position;
  182. typedef typename graph_traits<Graph2>::vertex_iterator vertex_iterator;
  183. std::pair<vertex_iterator, vertex_iterator> G2_verts;
  184. typedef typename graph_traits<Graph2>::adjacency_iterator adjacency_iterator;
  185. std::pair<adjacency_iterator, adjacency_iterator> fi_adj;
  186. edge_iter iter;
  187. int dfs_num_k;
  188. };
  189. bool match(edge_iter iter, int dfs_num_k)
  190. {
  191. std::vector<match_continuation> k;
  192. typedef typename graph_traits<Graph2>::vertex_iterator vertex_iterator;
  193. std::pair<vertex_iterator, vertex_iterator> G2_verts(vertices(G2));
  194. typedef typename graph_traits<Graph2>::adjacency_iterator adjacency_iterator;
  195. std::pair<adjacency_iterator, adjacency_iterator> fi_adj;
  196. vertex1_t i, j;
  197. recur:
  198. if (iter != ordered_edges.end()) {
  199. i = source(*iter, G1);
  200. j = target(*iter, G1);
  201. if (dfs_num[i] > dfs_num_k) {
  202. G2_verts = vertices(G2);
  203. while (G2_verts.first != G2_verts.second) {
  204. {
  205. vertex2_t u = *G2_verts.first;
  206. vertex1_t kp1 = dfs_vertices[dfs_num_k + 1];
  207. if (invariant1(kp1) == invariant2(u) && in_S[u] == false) {
  208. {
  209. f[kp1] = u;
  210. in_S[u] = true;
  211. num_edges_on_k = 0;
  212. match_continuation new_k;
  213. new_k.position = match_continuation::pos_G2_vertex_loop;
  214. new_k.G2_verts = G2_verts;
  215. new_k.iter = iter;
  216. new_k.dfs_num_k = dfs_num_k;
  217. k.push_back(new_k);
  218. ++dfs_num_k;
  219. goto recur;
  220. }
  221. }
  222. }
  223. G2_loop_k: ++G2_verts.first;
  224. }
  225. }
  226. else if (dfs_num[j] > dfs_num_k) {
  227. {
  228. vertex1_t vk = dfs_vertices[dfs_num_k];
  229. num_edges_on_k -=
  230. count_if(adjacent_vertices(f[vk], G2), make_indirect_pmap(in_S));
  231. for (int jj = 0; jj < dfs_num_k; ++jj) {
  232. vertex1_t j = dfs_vertices[jj];
  233. num_edges_on_k -= count(adjacent_vertices(f[j], G2), f[vk]);
  234. }
  235. }
  236. if (num_edges_on_k != 0)
  237. goto return_point_false;
  238. fi_adj = adjacent_vertices(f[i], G2);
  239. while (fi_adj.first != fi_adj.second) {
  240. {
  241. vertex2_t v = *fi_adj.first;
  242. if (invariant2(v) == invariant1(j) && in_S[v] == false) {
  243. f[j] = v;
  244. in_S[v] = true;
  245. num_edges_on_k = 1;
  246. BOOST_USING_STD_MAX();
  247. int next_k = max BOOST_PREVENT_MACRO_SUBSTITUTION(dfs_num_k, max BOOST_PREVENT_MACRO_SUBSTITUTION(dfs_num[i], dfs_num[j]));
  248. match_continuation new_k;
  249. new_k.position = match_continuation::pos_fi_adj_loop;
  250. new_k.fi_adj = fi_adj;
  251. new_k.iter = iter;
  252. new_k.dfs_num_k = dfs_num_k;
  253. ++iter;
  254. dfs_num_k = next_k;
  255. k.push_back(new_k);
  256. goto recur;
  257. }
  258. }
  259. fi_adj_loop_k:++fi_adj.first;
  260. }
  261. }
  262. else {
  263. if (container_contains(adjacent_vertices(f[i], G2), f[j])) {
  264. ++num_edges_on_k;
  265. match_continuation new_k;
  266. new_k.position = match_continuation::pos_dfs_num;
  267. k.push_back(new_k);
  268. ++iter;
  269. goto recur;
  270. }
  271. }
  272. } else
  273. goto return_point_true;
  274. goto return_point_false;
  275. {
  276. return_point_true: return true;
  277. return_point_false:
  278. if (k.empty()) return false;
  279. const match_continuation& this_k = k.back();
  280. switch (this_k.position) {
  281. case match_continuation::pos_G2_vertex_loop: {G2_verts = this_k.G2_verts; iter = this_k.iter; dfs_num_k = this_k.dfs_num_k; k.pop_back(); in_S[*G2_verts.first] = false; i = source(*iter, G1); j = target(*iter, G1); goto G2_loop_k;}
  282. case match_continuation::pos_fi_adj_loop: {fi_adj = this_k.fi_adj; iter = this_k.iter; dfs_num_k = this_k.dfs_num_k; k.pop_back(); in_S[*fi_adj.first] = false; i = source(*iter, G1); j = target(*iter, G1); goto fi_adj_loop_k;}
  283. case match_continuation::pos_dfs_num: {k.pop_back(); goto return_point_false;}
  284. default: {
  285. BOOST_ASSERT(!"Bad position");
  286. #ifdef UNDER_CE
  287. exit(-1);
  288. #else
  289. abort();
  290. #endif
  291. }
  292. }
  293. }
  294. }
  295. };
  296. template <typename Graph, typename InDegreeMap>
  297. void compute_in_degree(const Graph& g, InDegreeMap in_degree_map)
  298. {
  299. BGL_FORALL_VERTICES_T(v, g, Graph)
  300. put(in_degree_map, v, 0);
  301. BGL_FORALL_VERTICES_T(u, g, Graph)
  302. BGL_FORALL_ADJ_T(u, v, g, Graph)
  303. put(in_degree_map, v, get(in_degree_map, v) + 1);
  304. }
  305. } // namespace detail
  306. template <typename InDegreeMap, typename Graph>
  307. class degree_vertex_invariant
  308. {
  309. typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
  310. typedef typename graph_traits<Graph>::degree_size_type size_type;
  311. public:
  312. typedef vertex_t argument_type;
  313. typedef size_type result_type;
  314. degree_vertex_invariant(const InDegreeMap& in_degree_map, const Graph& g)
  315. : m_in_degree_map(in_degree_map),
  316. m_max_vertex_in_degree(0),
  317. m_max_vertex_out_degree(0),
  318. m_g(g) {
  319. BGL_FORALL_VERTICES_T(v, g, Graph) {
  320. m_max_vertex_in_degree =
  321. (std::max)(m_max_vertex_in_degree, get(m_in_degree_map, v));
  322. m_max_vertex_out_degree =
  323. (std::max)(m_max_vertex_out_degree, out_degree(v, g));
  324. }
  325. }
  326. size_type operator()(vertex_t v) const {
  327. return (m_max_vertex_in_degree + 1) * out_degree(v, m_g)
  328. + get(m_in_degree_map, v);
  329. }
  330. // The largest possible vertex invariant number
  331. size_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const {
  332. return (m_max_vertex_in_degree + 1) * (m_max_vertex_out_degree + 1);
  333. }
  334. private:
  335. InDegreeMap m_in_degree_map;
  336. size_type m_max_vertex_in_degree;
  337. size_type m_max_vertex_out_degree;
  338. const Graph& m_g;
  339. };
  340. // Count actual number of vertices, even in filtered graphs.
  341. template <typename Graph>
  342. size_t count_vertices(const Graph& g)
  343. {
  344. size_t n = 0;
  345. BGL_FORALL_VERTICES_T(v, g, Graph) {(void)v; ++n;}
  346. return n;
  347. }
  348. template <typename Graph1, typename Graph2, typename IsoMapping,
  349. typename Invariant1, typename Invariant2,
  350. typename IndexMap1, typename IndexMap2>
  351. bool isomorphism(const Graph1& G1, const Graph2& G2, IsoMapping f,
  352. Invariant1 invariant1, Invariant2 invariant2,
  353. std::size_t max_invariant,
  354. IndexMap1 index_map1, IndexMap2 index_map2)
  355. {
  356. // Graph requirements
  357. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph1> ));
  358. BOOST_CONCEPT_ASSERT(( EdgeListGraphConcept<Graph1> ));
  359. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph2> ));
  360. //BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<Graph2> ));
  361. typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_t;
  362. typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
  363. typedef typename graph_traits<Graph1>::vertices_size_type size_type;
  364. // Vertex invariant requirement
  365. BOOST_CONCEPT_ASSERT(( AdaptableUnaryFunctionConcept<Invariant1,
  366. size_type, vertex1_t> ));
  367. BOOST_CONCEPT_ASSERT(( AdaptableUnaryFunctionConcept<Invariant2,
  368. size_type, vertex2_t> ));
  369. // Property map requirements
  370. BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<IsoMapping, vertex1_t> ));
  371. typedef typename property_traits<IsoMapping>::value_type IsoMappingValue;
  372. BOOST_STATIC_ASSERT((is_convertible<IsoMappingValue, vertex2_t>::value));
  373. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMap1, vertex1_t> ));
  374. typedef typename property_traits<IndexMap1>::value_type IndexMap1Value;
  375. BOOST_STATIC_ASSERT((is_convertible<IndexMap1Value, size_type>::value));
  376. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<IndexMap2, vertex2_t> ));
  377. typedef typename property_traits<IndexMap2>::value_type IndexMap2Value;
  378. BOOST_STATIC_ASSERT((is_convertible<IndexMap2Value, size_type>::value));
  379. if (count_vertices(G1) != count_vertices(G2))
  380. return false;
  381. if (count_vertices(G1) == 0 && count_vertices(G2) == 0)
  382. return true;
  383. detail::isomorphism_algo<Graph1, Graph2, IsoMapping, Invariant1,
  384. Invariant2, IndexMap1, IndexMap2>
  385. algo(G1, G2, f, invariant1, invariant2, max_invariant,
  386. index_map1, index_map2);
  387. return algo.test_isomorphism();
  388. }
  389. namespace detail {
  390. template <typename Graph1, typename Graph2,
  391. typename IsoMapping,
  392. typename IndexMap1, typename IndexMap2,
  393. typename P, typename T, typename R>
  394. bool isomorphism_impl(const Graph1& G1, const Graph2& G2,
  395. IsoMapping f, IndexMap1 index_map1, IndexMap2 index_map2,
  396. const bgl_named_params<P,T,R>& params)
  397. {
  398. std::vector<std::size_t> in_degree1_vec(num_vertices(G1));
  399. typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,
  400. IndexMap1
  401. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  402. , std::size_t, std::size_t&
  403. #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
  404. > InDeg1;
  405. InDeg1 in_degree1(in_degree1_vec.begin(), in_degree1_vec.size(), index_map1);
  406. compute_in_degree(G1, in_degree1);
  407. std::vector<std::size_t> in_degree2_vec(num_vertices(G2));
  408. typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,
  409. IndexMap2
  410. #ifdef BOOST_NO_STD_ITERATOR_TRAITS
  411. , std::size_t, std::size_t&
  412. #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
  413. > InDeg2;
  414. InDeg2 in_degree2(in_degree2_vec.begin(), in_degree2_vec.size(), index_map2);
  415. compute_in_degree(G2, in_degree2);
  416. degree_vertex_invariant<InDeg1, Graph1> invariant1(in_degree1, G1);
  417. degree_vertex_invariant<InDeg2, Graph2> invariant2(in_degree2, G2);
  418. return isomorphism(G1, G2, f,
  419. choose_param(get_param(params, vertex_invariant1_t()), invariant1),
  420. choose_param(get_param(params, vertex_invariant2_t()), invariant2),
  421. choose_param(get_param(params, vertex_max_invariant_t()), (invariant2.max)()),
  422. index_map1, index_map2
  423. );
  424. }
  425. template <typename G, typename Index>
  426. struct make_degree_invariant {
  427. const G& g;
  428. const Index& index;
  429. make_degree_invariant(const G& g, const Index& index): g(g), index(index) {}
  430. typedef typename boost::graph_traits<G>::degree_size_type degree_size_type;
  431. typedef shared_array_property_map<degree_size_type, Index> prop_map_type;
  432. typedef degree_vertex_invariant<prop_map_type, G> result_type;
  433. result_type operator()() const {
  434. prop_map_type pm = make_shared_array_property_map(num_vertices(g), degree_size_type(), index);
  435. compute_in_degree(g, pm);
  436. return result_type(pm, g);
  437. }
  438. };
  439. } // namespace detail
  440. namespace graph {
  441. namespace detail {
  442. template <typename Graph1, typename Graph2>
  443. struct isomorphism_impl {
  444. typedef bool result_type;
  445. typedef result_type type;
  446. template <typename ArgPack>
  447. bool operator()(const Graph1& g1, const Graph2& g2, const ArgPack& arg_pack) const {
  448. using namespace boost::graph::keywords;
  449. typedef typename boost::detail::override_const_property_result<ArgPack, tag::vertex_index1_map, boost::vertex_index_t, Graph1>::type index1_map_type;
  450. typedef typename boost::detail::override_const_property_result<ArgPack, tag::vertex_index2_map, boost::vertex_index_t, Graph2>::type index2_map_type;
  451. index1_map_type index1_map = boost::detail::override_const_property(arg_pack, _vertex_index1_map, g1, boost::vertex_index);
  452. index2_map_type index2_map = boost::detail::override_const_property(arg_pack, _vertex_index2_map, g2, boost::vertex_index);
  453. typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
  454. typename std::vector<vertex2_t>::size_type n = (typename std::vector<vertex2_t>::size_type)num_vertices(g1);
  455. std::vector<vertex2_t> f(n);
  456. typename boost::parameter::lazy_binding<
  457. ArgPack,
  458. tag::vertex_invariant1,
  459. boost::detail::make_degree_invariant<Graph1, index1_map_type> >::type
  460. invariant1 =
  461. arg_pack[_vertex_invariant1 || boost::detail::make_degree_invariant<Graph1, index1_map_type>(g1, index1_map)];
  462. typename boost::parameter::lazy_binding<
  463. ArgPack,
  464. tag::vertex_invariant2,
  465. boost::detail::make_degree_invariant<Graph2, index2_map_type> >::type
  466. invariant2 =
  467. arg_pack[_vertex_invariant2 || boost::detail::make_degree_invariant<Graph2, index2_map_type>(g2, index2_map)];
  468. return boost::isomorphism
  469. (g1, g2,
  470. choose_param(arg_pack[_isomorphism_map | boost::param_not_found()],
  471. make_shared_array_property_map(num_vertices(g1), vertex2_t(), index1_map)),
  472. invariant1,
  473. invariant2,
  474. arg_pack[_vertex_max_invariant | (invariant2.max)()],
  475. index1_map,
  476. index2_map);
  477. }
  478. };
  479. }
  480. BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(isomorphism, 2, 6)
  481. }
  482. // Named parameter interface
  483. BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(isomorphism, 2)
  484. // Verify that the given mapping iso_map from the vertices of g1 to the
  485. // vertices of g2 describes an isomorphism.
  486. // Note: this could be made much faster by specializing based on the graph
  487. // concepts modeled, but since we're verifying an O(n^(lg n)) algorithm,
  488. // O(n^4) won't hurt us.
  489. template<typename Graph1, typename Graph2, typename IsoMap>
  490. inline bool verify_isomorphism(const Graph1& g1, const Graph2& g2, IsoMap iso_map)
  491. {
  492. #if 0
  493. // problematic for filtered_graph!
  494. if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
  495. return false;
  496. #endif
  497. BGL_FORALL_EDGES_T(e1, g1, Graph1) {
  498. bool found_edge = false;
  499. BGL_FORALL_EDGES_T(e2, g2, Graph2) {
  500. if (source(e2, g2) == get(iso_map, source(e1, g1)) &&
  501. target(e2, g2) == get(iso_map, target(e1, g1))) {
  502. found_edge = true;
  503. }
  504. }
  505. if (!found_edge)
  506. return false;
  507. }
  508. return true;
  509. }
  510. } // namespace boost
  511. #ifdef BOOST_ISO_INCLUDED_ITER_MACROS
  512. #undef BOOST_ISO_INCLUDED_ITER_MACROS
  513. #include <boost/graph/iteration_macros_undef.hpp>
  514. #endif
  515. #endif // BOOST_GRAPH_ISOMORPHISM_HPP