• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #include <utility>
10 #include <vector>
11 #include <iterator>
12 #include <algorithm>
13 #include <boost/config.hpp>
14 #include <boost/assert.hpp>
15 #include <boost/smart_ptr.hpp>
16 #include <boost/graph/depth_first_search.hpp>
17 #include <boost/detail/algorithm.hpp>
18 #include <boost/pending/indirect_cmp.hpp> // for make_indirect_pmap
19 #include <boost/concept/assert.hpp>
20 
21 #ifndef BOOST_GRAPH_ITERATION_MACROS_HPP
22 #define BOOST_ISO_INCLUDED_ITER_MACROS // local macro, see bottom of file
23 #include <boost/graph/iteration_macros.hpp>
24 #endif
25 
26 namespace boost
27 {
28 
29 namespace detail
30 {
31 
32     template < typename Graph1, typename Graph2, typename IsoMapping,
33         typename Invariant1, typename Invariant2, typename IndexMap1,
34         typename IndexMap2 >
35     class isomorphism_algo
36     {
37         typedef typename graph_traits< Graph1 >::vertex_descriptor vertex1_t;
38         typedef typename graph_traits< Graph2 >::vertex_descriptor vertex2_t;
39         typedef typename graph_traits< Graph1 >::edge_descriptor edge1_t;
40         typedef typename graph_traits< Graph1 >::vertices_size_type size_type;
41         typedef typename Invariant1::result_type invar1_value;
42         typedef typename Invariant2::result_type invar2_value;
43 
44         const Graph1& G1;
45         const Graph2& G2;
46         IsoMapping f;
47         Invariant1 invariant1;
48         Invariant2 invariant2;
49         std::size_t max_invariant;
50         IndexMap1 index_map1;
51         IndexMap2 index_map2;
52 
53         std::vector< vertex1_t > dfs_vertices;
54         typedef typename std::vector< vertex1_t >::iterator vertex_iter;
55         std::vector< int > dfs_num_vec;
56         typedef safe_iterator_property_map<
57             typename std::vector< int >::iterator, IndexMap1
58 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
59             ,
60             int, int&
61 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
62             >
63             DFSNumMap;
64         DFSNumMap dfs_num;
65         std::vector< edge1_t > ordered_edges;
66         typedef typename std::vector< edge1_t >::iterator edge_iter;
67 
68         std::vector< char > in_S_vec;
69         typedef safe_iterator_property_map<
70             typename std::vector< char >::iterator, IndexMap2
71 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
72             ,
73             char, char&
74 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
75             >
76             InSMap;
77         InSMap in_S;
78 
79         int num_edges_on_k;
80 
81         friend struct compare_multiplicity;
82         struct compare_multiplicity
83         {
compare_multiplicityboost::detail::isomorphism_algo::compare_multiplicity84             compare_multiplicity(Invariant1 invariant1, size_type* multiplicity)
85             : invariant1(invariant1), multiplicity(multiplicity)
86             {
87             }
operator ()boost::detail::isomorphism_algo::compare_multiplicity88             bool operator()(const vertex1_t& x, const vertex1_t& y) const
89             {
90                 return multiplicity[invariant1(x)]
91                     < multiplicity[invariant1(y)];
92             }
93             Invariant1 invariant1;
94             size_type* multiplicity;
95         };
96 
97         struct record_dfs_order : default_dfs_visitor
98         {
record_dfs_orderboost::detail::isomorphism_algo::record_dfs_order99             record_dfs_order(
100                 std::vector< vertex1_t >& v, std::vector< edge1_t >& e)
101             : vertices(v), edges(e)
102             {
103             }
104 
discover_vertexboost::detail::isomorphism_algo::record_dfs_order105             void discover_vertex(vertex1_t v, const Graph1&) const
106             {
107                 vertices.push_back(v);
108             }
examine_edgeboost::detail::isomorphism_algo::record_dfs_order109             void examine_edge(edge1_t e, const Graph1&) const
110             {
111                 edges.push_back(e);
112             }
113             std::vector< vertex1_t >& vertices;
114             std::vector< edge1_t >& edges;
115         };
116 
117         struct edge_cmp
118         {
edge_cmpboost::detail::isomorphism_algo::edge_cmp119             edge_cmp(const Graph1& G1, DFSNumMap dfs_num)
120             : G1(G1), dfs_num(dfs_num)
121             {
122             }
operator ()boost::detail::isomorphism_algo::edge_cmp123             bool operator()(const edge1_t& e1, const edge1_t& e2) const
124             {
125                 using namespace std;
126                 int u1 = dfs_num[source(e1, G1)], v1 = dfs_num[target(e1, G1)];
127                 int u2 = dfs_num[source(e2, G1)], v2 = dfs_num[target(e2, G1)];
128                 int m1 = (max)(u1, v1);
129                 int m2 = (max)(u2, v2);
130                 // lexicographical comparison
131                 return std::make_pair(m1, std::make_pair(u1, v1))
132                     < std::make_pair(m2, std::make_pair(u2, v2));
133             }
134             const Graph1& G1;
135             DFSNumMap dfs_num;
136         };
137 
138     public:
isomorphism_algo(const Graph1 & G1,const Graph2 & G2,IsoMapping f,Invariant1 invariant1,Invariant2 invariant2,std::size_t max_invariant,IndexMap1 index_map1,IndexMap2 index_map2)139         isomorphism_algo(const Graph1& G1, const Graph2& G2, IsoMapping f,
140             Invariant1 invariant1, Invariant2 invariant2,
141             std::size_t max_invariant, IndexMap1 index_map1,
142             IndexMap2 index_map2)
143         : G1(G1)
144         , G2(G2)
145         , f(f)
146         , invariant1(invariant1)
147         , invariant2(invariant2)
148         , max_invariant(max_invariant)
149         , index_map1(index_map1)
150         , index_map2(index_map2)
151         {
152             in_S_vec.resize(num_vertices(G1));
153             in_S = make_safe_iterator_property_map(
154                 in_S_vec.begin(), in_S_vec.size(), index_map2
155 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
156                 ,
157                 in_S_vec.front()
158 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
159             );
160         }
161 
test_isomorphism()162         bool test_isomorphism()
163         {
164             // reset isomapping
165             BGL_FORALL_VERTICES_T(v, G1, Graph1)
166             f[v] = graph_traits< Graph2 >::null_vertex();
167 
168             {
169                 std::vector< invar1_value > invar1_array;
170                 BGL_FORALL_VERTICES_T(v, G1, Graph1)
171                 invar1_array.push_back(invariant1(v));
172                 sort(invar1_array);
173 
174                 std::vector< invar2_value > invar2_array;
175                 BGL_FORALL_VERTICES_T(v, G2, Graph2)
176                 invar2_array.push_back(invariant2(v));
177                 sort(invar2_array);
178                 if (!equal(invar1_array, invar2_array))
179                     return false;
180             }
181 
182             std::vector< vertex1_t > V_mult;
183             BGL_FORALL_VERTICES_T(v, G1, Graph1)
184             V_mult.push_back(v);
185             {
186                 std::vector< size_type > multiplicity(max_invariant, 0);
187                 BGL_FORALL_VERTICES_T(v, G1, Graph1)
188                 ++multiplicity.at(invariant1(v));
189                 sort(
190                     V_mult, compare_multiplicity(invariant1, &multiplicity[0]));
191             }
192 
193             std::vector< default_color_type > color_vec(num_vertices(G1));
194             safe_iterator_property_map<
195                 std::vector< default_color_type >::iterator, IndexMap1
196 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
197                 ,
198                 default_color_type, default_color_type&
199 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
200                 >
201                 color_map(color_vec.begin(), color_vec.size(), index_map1);
202             record_dfs_order dfs_visitor(dfs_vertices, ordered_edges);
203             typedef color_traits< default_color_type > Color;
204             for (vertex_iter u = V_mult.begin(); u != V_mult.end(); ++u)
205             {
206                 if (color_map[*u] == Color::white())
207                 {
208                     dfs_visitor.start_vertex(*u, G1);
209                     depth_first_visit(G1, *u, dfs_visitor, color_map);
210                 }
211             }
212             // Create the dfs_num array and dfs_num_map
213             dfs_num_vec.resize(num_vertices(G1));
214             dfs_num = make_safe_iterator_property_map(
215                 dfs_num_vec.begin(), dfs_num_vec.size(), index_map1
216 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
217                 ,
218                 dfs_num_vec.front()
219 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
220             );
221             size_type n = 0;
222             for (vertex_iter v = dfs_vertices.begin(); v != dfs_vertices.end();
223                  ++v)
224                 dfs_num[*v] = n++;
225 
226             sort(ordered_edges, edge_cmp(G1, dfs_num));
227 
228             int dfs_num_k = -1;
229             return this->match(ordered_edges.begin(), dfs_num_k);
230         }
231 
232     private:
233         struct match_continuation
234         {
235             enum
236             {
237                 pos_G2_vertex_loop,
238                 pos_fi_adj_loop,
239                 pos_dfs_num
240             } position;
241             typedef typename graph_traits< Graph2 >::vertex_iterator
242                 vertex_iterator;
243             std::pair< vertex_iterator, vertex_iterator > G2_verts;
244             typedef typename graph_traits< Graph2 >::adjacency_iterator
245                 adjacency_iterator;
246             std::pair< adjacency_iterator, adjacency_iterator > fi_adj;
247             edge_iter iter;
248             int dfs_num_k;
249         };
250 
match(edge_iter iter,int dfs_num_k)251         bool match(edge_iter iter, int dfs_num_k)
252         {
253             std::vector< match_continuation > k;
254             typedef typename graph_traits< Graph2 >::vertex_iterator
255                 vertex_iterator;
256             std::pair< vertex_iterator, vertex_iterator > G2_verts(
257                 vertices(G2));
258             typedef typename graph_traits< Graph2 >::adjacency_iterator
259                 adjacency_iterator;
260             std::pair< adjacency_iterator, adjacency_iterator > fi_adj;
261             vertex1_t i, j;
262 
263         recur:
264             if (iter != ordered_edges.end())
265             {
266                 i = source(*iter, G1);
267                 j = target(*iter, G1);
268                 if (dfs_num[i] > dfs_num_k)
269                 {
270                     G2_verts = vertices(G2);
271                     while (G2_verts.first != G2_verts.second)
272                     {
273                         {
274                             vertex2_t u = *G2_verts.first;
275                             vertex1_t kp1 = dfs_vertices[dfs_num_k + 1];
276                             if (invariant1(kp1) == invariant2(u)
277                                 && in_S[u] == false)
278                             {
279                                 {
280                                     f[kp1] = u;
281                                     in_S[u] = true;
282                                     num_edges_on_k = 0;
283 
284                                     match_continuation new_k;
285                                     new_k.position = match_continuation::
286                                         pos_G2_vertex_loop;
287                                     new_k.G2_verts = G2_verts;
288                                     new_k.iter = iter;
289                                     new_k.dfs_num_k = dfs_num_k;
290                                     k.push_back(new_k);
291                                     ++dfs_num_k;
292                                     goto recur;
293                                 }
294                             }
295                         }
296                     G2_loop_k:
297                         ++G2_verts.first;
298                     }
299                 }
300                 else if (dfs_num[j] > dfs_num_k)
301                 {
302                     {
303                         vertex1_t vk = dfs_vertices[dfs_num_k];
304                         num_edges_on_k -= count_if(adjacent_vertices(f[vk], G2),
305                             make_indirect_pmap(in_S));
306 
307                         for (int jj = 0; jj < dfs_num_k; ++jj)
308                         {
309                             vertex1_t j = dfs_vertices[jj];
310                             num_edges_on_k
311                                 -= count(adjacent_vertices(f[j], G2), f[vk]);
312                         }
313                     }
314 
315                     if (num_edges_on_k != 0)
316                         goto return_point_false;
317                     fi_adj = adjacent_vertices(f[i], G2);
318                     while (fi_adj.first != fi_adj.second)
319                     {
320                         {
321                             vertex2_t v = *fi_adj.first;
322                             if (invariant2(v) == invariant1(j)
323                                 && in_S[v] == false)
324                             {
325                                 f[j] = v;
326                                 in_S[v] = true;
327                                 num_edges_on_k = 1;
328                                 BOOST_USING_STD_MAX();
329                                 int next_k
330                                     = max BOOST_PREVENT_MACRO_SUBSTITUTION(
331                                         dfs_num_k,
332                                         max BOOST_PREVENT_MACRO_SUBSTITUTION(
333                                             dfs_num[i], dfs_num[j]));
334                                 match_continuation new_k;
335                                 new_k.position
336                                     = match_continuation::pos_fi_adj_loop;
337                                 new_k.fi_adj = fi_adj;
338                                 new_k.iter = iter;
339                                 new_k.dfs_num_k = dfs_num_k;
340                                 ++iter;
341                                 dfs_num_k = next_k;
342                                 k.push_back(new_k);
343                                 goto recur;
344                             }
345                         }
346                     fi_adj_loop_k:
347                         ++fi_adj.first;
348                     }
349                 }
350                 else
351                 {
352                     if (container_contains(adjacent_vertices(f[i], G2), f[j]))
353                     {
354                         ++num_edges_on_k;
355                         match_continuation new_k;
356                         new_k.position = match_continuation::pos_dfs_num;
357                         k.push_back(new_k);
358                         ++iter;
359                         goto recur;
360                     }
361                 }
362             }
363             else
364                 goto return_point_true;
365             goto return_point_false;
366 
367             {
368             return_point_true:
369                 return true;
370 
371             return_point_false:
372                 if (k.empty())
373                     return false;
374                 const match_continuation& this_k = k.back();
375                 switch (this_k.position)
376                 {
377                 case match_continuation::pos_G2_vertex_loop:
378                 {
379                     G2_verts = this_k.G2_verts;
380                     iter = this_k.iter;
381                     dfs_num_k = this_k.dfs_num_k;
382                     k.pop_back();
383                     in_S[*G2_verts.first] = false;
384                     i = source(*iter, G1);
385                     j = target(*iter, G1);
386                     goto G2_loop_k;
387                 }
388                 case match_continuation::pos_fi_adj_loop:
389                 {
390                     fi_adj = this_k.fi_adj;
391                     iter = this_k.iter;
392                     dfs_num_k = this_k.dfs_num_k;
393                     k.pop_back();
394                     in_S[*fi_adj.first] = false;
395                     i = source(*iter, G1);
396                     j = target(*iter, G1);
397                     goto fi_adj_loop_k;
398                 }
399                 case match_continuation::pos_dfs_num:
400                 {
401                     k.pop_back();
402                     goto return_point_false;
403                 }
404                 default:
405                 {
406                     BOOST_ASSERT(!"Bad position");
407 #ifdef UNDER_CE
408                     exit(-1);
409 #else
410                     abort();
411 #endif
412                 }
413                 }
414             }
415         }
416     };
417 
418     template < typename Graph, typename InDegreeMap >
compute_in_degree(const Graph & g,InDegreeMap in_degree_map)419     void compute_in_degree(const Graph& g, InDegreeMap in_degree_map)
420     {
421         BGL_FORALL_VERTICES_T(v, g, Graph)
422         put(in_degree_map, v, 0);
423 
424         BGL_FORALL_VERTICES_T(u, g, Graph)
425         BGL_FORALL_ADJ_T(u, v, g, Graph)
426         put(in_degree_map, v, get(in_degree_map, v) + 1);
427     }
428 
429 } // namespace detail
430 
431 template < typename InDegreeMap, typename Graph > class degree_vertex_invariant
432 {
433     typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
434     typedef typename graph_traits< Graph >::degree_size_type size_type;
435 
436 public:
437     typedef vertex_t argument_type;
438     typedef size_type result_type;
439 
degree_vertex_invariant(const InDegreeMap & in_degree_map,const Graph & g)440     degree_vertex_invariant(const InDegreeMap& in_degree_map, const Graph& g)
441     : m_in_degree_map(in_degree_map)
442     , m_max_vertex_in_degree(0)
443     , m_max_vertex_out_degree(0)
444     , m_g(g)
445     {
446         BGL_FORALL_VERTICES_T(v, g, Graph)
447         {
448             m_max_vertex_in_degree
449                 = (std::max)(m_max_vertex_in_degree, get(m_in_degree_map, v));
450             m_max_vertex_out_degree
451                 = (std::max)(m_max_vertex_out_degree, out_degree(v, g));
452         }
453     }
454 
operator ()(vertex_t v) const455     size_type operator()(vertex_t v) const
456     {
457         return (m_max_vertex_in_degree + 1) * out_degree(v, m_g)
458             + get(m_in_degree_map, v);
459     }
460     // The largest possible vertex invariant number
BOOST_PREVENT_MACRO_SUBSTITUTION() const461     size_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
462     {
463         return (m_max_vertex_in_degree + 1) * (m_max_vertex_out_degree + 1);
464     }
465 
466 private:
467     InDegreeMap m_in_degree_map;
468     size_type m_max_vertex_in_degree;
469     size_type m_max_vertex_out_degree;
470     const Graph& m_g;
471 };
472 
473 // Count actual number of vertices, even in filtered graphs.
count_vertices(const Graph & g)474 template < typename Graph > size_t count_vertices(const Graph& g)
475 {
476     size_t n = 0;
477     BGL_FORALL_VERTICES_T(v, g, Graph)
478     {
479         (void)v;
480         ++n;
481     }
482     return n;
483 }
484 
485 template < typename Graph1, typename Graph2, typename IsoMapping,
486     typename Invariant1, typename Invariant2, typename IndexMap1,
487     typename IndexMap2 >
isomorphism(const Graph1 & G1,const Graph2 & G2,IsoMapping f,Invariant1 invariant1,Invariant2 invariant2,std::size_t max_invariant,IndexMap1 index_map1,IndexMap2 index_map2)488 bool isomorphism(const Graph1& G1, const Graph2& G2, IsoMapping f,
489     Invariant1 invariant1, Invariant2 invariant2, std::size_t max_invariant,
490     IndexMap1 index_map1, IndexMap2 index_map2)
491 
492 {
493     // Graph requirements
494     BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph1 >));
495     BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< Graph1 >));
496     BOOST_CONCEPT_ASSERT((VertexListGraphConcept< Graph2 >));
497     // BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept<Graph2> ));
498 
499     typedef typename graph_traits< Graph1 >::vertex_descriptor vertex1_t;
500     typedef typename graph_traits< Graph2 >::vertex_descriptor vertex2_t;
501     typedef typename graph_traits< Graph1 >::vertices_size_type size_type;
502 
503     // Vertex invariant requirement
504     BOOST_CONCEPT_ASSERT(
505         (AdaptableUnaryFunctionConcept< Invariant1, size_type, vertex1_t >));
506     BOOST_CONCEPT_ASSERT(
507         (AdaptableUnaryFunctionConcept< Invariant2, size_type, vertex2_t >));
508 
509     // Property map requirements
510     BOOST_CONCEPT_ASSERT(
511         (ReadWritePropertyMapConcept< IsoMapping, vertex1_t >));
512     typedef typename property_traits< IsoMapping >::value_type IsoMappingValue;
513     BOOST_STATIC_ASSERT((is_convertible< IsoMappingValue, vertex2_t >::value));
514 
515     BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< IndexMap1, vertex1_t >));
516     typedef typename property_traits< IndexMap1 >::value_type IndexMap1Value;
517     BOOST_STATIC_ASSERT((is_convertible< IndexMap1Value, size_type >::value));
518 
519     BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept< IndexMap2, vertex2_t >));
520     typedef typename property_traits< IndexMap2 >::value_type IndexMap2Value;
521     BOOST_STATIC_ASSERT((is_convertible< IndexMap2Value, size_type >::value));
522 
523     if (count_vertices(G1) != count_vertices(G2))
524         return false;
525     if (count_vertices(G1) == 0 && count_vertices(G2) == 0)
526         return true;
527 
528     detail::isomorphism_algo< Graph1, Graph2, IsoMapping, Invariant1,
529         Invariant2, IndexMap1, IndexMap2 >
530         algo(G1, G2, f, invariant1, invariant2, max_invariant, index_map1,
531             index_map2);
532     return algo.test_isomorphism();
533 }
534 
535 namespace detail
536 {
537 
538     template < typename Graph1, typename Graph2, typename IsoMapping,
539         typename IndexMap1, typename IndexMap2, typename P, typename T,
540         typename R >
isomorphism_impl(const Graph1 & G1,const Graph2 & G2,IsoMapping f,IndexMap1 index_map1,IndexMap2 index_map2,const bgl_named_params<P,T,R> & params)541     bool isomorphism_impl(const Graph1& G1, const Graph2& G2, IsoMapping f,
542         IndexMap1 index_map1, IndexMap2 index_map2,
543         const bgl_named_params< P, T, R >& params)
544     {
545         std::vector< std::size_t > in_degree1_vec(num_vertices(G1));
546         typedef safe_iterator_property_map<
547             std::vector< std::size_t >::iterator, IndexMap1
548 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
549             ,
550             std::size_t, std::size_t&
551 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
552             >
553             InDeg1;
554         InDeg1 in_degree1(
555             in_degree1_vec.begin(), in_degree1_vec.size(), index_map1);
556         compute_in_degree(G1, in_degree1);
557 
558         std::vector< std::size_t > in_degree2_vec(num_vertices(G2));
559         typedef safe_iterator_property_map<
560             std::vector< std::size_t >::iterator, IndexMap2
561 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
562             ,
563             std::size_t, std::size_t&
564 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
565             >
566             InDeg2;
567         InDeg2 in_degree2(
568             in_degree2_vec.begin(), in_degree2_vec.size(), index_map2);
569         compute_in_degree(G2, in_degree2);
570 
571         degree_vertex_invariant< InDeg1, Graph1 > invariant1(in_degree1, G1);
572         degree_vertex_invariant< InDeg2, Graph2 > invariant2(in_degree2, G2);
573 
574         return isomorphism(G1, G2, f,
575             choose_param(get_param(params, vertex_invariant1_t()), invariant1),
576             choose_param(get_param(params, vertex_invariant2_t()), invariant2),
577             choose_param(get_param(params, vertex_max_invariant_t()),
578                 (invariant2.max)()),
579             index_map1, index_map2);
580     }
581 
582     template < typename G, typename Index > struct make_degree_invariant
583     {
584         const G& g;
585         const Index& index;
make_degree_invariantboost::detail::make_degree_invariant586         make_degree_invariant(const G& g, const Index& index)
587         : g(g), index(index)
588         {
589         }
590         typedef typename boost::graph_traits< G >::degree_size_type
591             degree_size_type;
592         typedef shared_array_property_map< degree_size_type, Index >
593             prop_map_type;
594         typedef degree_vertex_invariant< prop_map_type, G > result_type;
operator ()boost::detail::make_degree_invariant595         result_type operator()() const
596         {
597             prop_map_type pm = make_shared_array_property_map(
598                 num_vertices(g), degree_size_type(), index);
599             compute_in_degree(g, pm);
600             return result_type(pm, g);
601         }
602     };
603 
604 } // namespace detail
605 
606 namespace graph
607 {
608     namespace detail
609     {
610         template < typename Graph1, typename Graph2 > struct isomorphism_impl
611         {
612             typedef bool result_type;
613             typedef result_type type;
614             template < typename ArgPack >
operator ()boost::graph::detail::isomorphism_impl615             bool operator()(const Graph1& g1, const Graph2& g2,
616                 const ArgPack& arg_pack) const
617             {
618                 using namespace boost::graph::keywords;
619                 typedef typename boost::detail::override_const_property_result<
620                     ArgPack, tag::vertex_index1_map, boost::vertex_index_t,
621                     Graph1 >::type index1_map_type;
622                 typedef typename boost::detail::override_const_property_result<
623                     ArgPack, tag::vertex_index2_map, boost::vertex_index_t,
624                     Graph2 >::type index2_map_type;
625                 index1_map_type index1_map
626                     = boost::detail::override_const_property(
627                         arg_pack, _vertex_index1_map, g1, boost::vertex_index);
628                 index2_map_type index2_map
629                     = boost::detail::override_const_property(
630                         arg_pack, _vertex_index2_map, g2, boost::vertex_index);
631                 typedef typename graph_traits< Graph2 >::vertex_descriptor
632                     vertex2_t;
633                 typename std::vector< vertex2_t >::size_type n
634                     = (typename std::vector< vertex2_t >::size_type)
635                         num_vertices(g1);
636                 std::vector< vertex2_t > f(n);
637                 typename boost::parameter::lazy_binding< ArgPack,
638                     tag::vertex_invariant1,
639                     boost::detail::make_degree_invariant< Graph1,
640                         index1_map_type > >::type invariant1
641                     = arg_pack[_vertex_invariant1
642                         || boost::detail::make_degree_invariant< Graph1,
643                             index1_map_type >(g1, index1_map)];
644                 typename boost::parameter::lazy_binding< ArgPack,
645                     tag::vertex_invariant2,
646                     boost::detail::make_degree_invariant< Graph2,
647                         index2_map_type > >::type invariant2
648                     = arg_pack[_vertex_invariant2
649                         || boost::detail::make_degree_invariant< Graph2,
650                             index2_map_type >(g2, index2_map)];
651                 return boost::isomorphism(g1, g2,
652                     choose_param(
653                         arg_pack[_isomorphism_map | boost::param_not_found()],
654                         make_shared_array_property_map(
655                             num_vertices(g1), vertex2_t(), index1_map)),
656                     invariant1, invariant2,
657                     arg_pack[_vertex_max_invariant | (invariant2.max)()],
658                     index1_map, index2_map);
659             }
660         };
661     }
662     BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(isomorphism, 2, 6)
663 }
664 
665 // Named parameter interface
666 BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(isomorphism, 2)
667 
668 // Verify that the given mapping iso_map from the vertices of g1 to the
669 // vertices of g2 describes an isomorphism.
670 // Note: this could be made much faster by specializing based on the graph
671 // concepts modeled, but since we're verifying an O(n^(lg n)) algorithm,
672 // O(n^4) won't hurt us.
673 template < typename Graph1, typename Graph2, typename IsoMap >
verify_isomorphism(const Graph1 & g1,const Graph2 & g2,IsoMap iso_map)674 inline bool verify_isomorphism(
675     const Graph1& g1, const Graph2& g2, IsoMap iso_map)
676 {
677 #if 0
678     // problematic for filtered_graph!
679     if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
680       return false;
681 #endif
682 
683     BGL_FORALL_EDGES_T(e1, g1, Graph1)
684     {
685         bool found_edge = false;
686         BGL_FORALL_EDGES_T(e2, g2, Graph2)
687         {
688             if (source(e2, g2) == get(iso_map, source(e1, g1))
689                 && target(e2, g2) == get(iso_map, target(e1, g1)))
690             {
691                 found_edge = true;
692             }
693         }
694 
695         if (!found_edge)
696             return false;
697     }
698 
699     return true;
700 }
701 
702 } // namespace boost
703 
704 #ifdef BOOST_ISO_INCLUDED_ITER_MACROS
705 #undef BOOST_ISO_INCLUDED_ITER_MACROS
706 #include <boost/graph/iteration_macros_undef.hpp>
707 #endif
708 
709 #endif // BOOST_GRAPH_ISOMORPHISM_HPP
710