1 // Copyright (C) 2001 Jeremy Siek, Douglas Gregor, Brian Osman
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
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>
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>
32 template < typename Graph1, typename Graph2, typename IsoMapping,
33 typename Invariant1, typename Invariant2, typename IndexMap1,
35 class isomorphism_algo
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;
47 Invariant1 invariant1;
48 Invariant2 invariant2;
49 std::size_t max_invariant;
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
61 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
65 std::vector< edge1_t > ordered_edges;
66 typedef typename std::vector< edge1_t >::iterator edge_iter;
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
74 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
81 friend struct compare_multiplicity;
82 struct compare_multiplicity
84 compare_multiplicity(Invariant1 invariant1, size_type* multiplicity)
85 : invariant1(invariant1), multiplicity(multiplicity)
88 bool operator()(const vertex1_t& x, const vertex1_t& y) const
90 return multiplicity[invariant1(x)]
91 < multiplicity[invariant1(y)];
93 Invariant1 invariant1;
94 size_type* multiplicity;
97 struct record_dfs_order : default_dfs_visitor
100 std::vector< vertex1_t >& v, std::vector< edge1_t >& e)
101 : vertices(v), edges(e)
105 void discover_vertex(vertex1_t v, const Graph1&) const
107 vertices.push_back(v);
109 void examine_edge(edge1_t e, const Graph1&) const
113 std::vector< vertex1_t >& vertices;
114 std::vector< edge1_t >& edges;
119 edge_cmp(const Graph1& G1, DFSNumMap dfs_num)
120 : G1(G1), dfs_num(dfs_num)
123 bool operator()(const edge1_t& e1, const edge1_t& e2) const
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));
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)
146 , invariant1(invariant1)
147 , invariant2(invariant2)
148 , max_invariant(max_invariant)
149 , index_map1(index_map1)
150 , index_map2(index_map2)
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
158 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
162 bool test_isomorphism()
165 BGL_FORALL_VERTICES_T(v, G1, Graph1)
166 f[v] = graph_traits< Graph2 >::null_vertex();
169 std::vector< invar1_value > invar1_array;
170 BGL_FORALL_VERTICES_T(v, G1, Graph1)
171 invar1_array.push_back(invariant1(v));
174 std::vector< invar2_value > invar2_array;
175 BGL_FORALL_VERTICES_T(v, G2, Graph2)
176 invar2_array.push_back(invariant2(v));
178 if (!equal(invar1_array, invar2_array))
182 std::vector< vertex1_t > V_mult;
183 BGL_FORALL_VERTICES_T(v, G1, Graph1)
186 std::vector< size_type > multiplicity(max_invariant, 0);
187 BGL_FORALL_VERTICES_T(v, G1, Graph1)
188 ++multiplicity.at(invariant1(v));
190 V_mult, compare_multiplicity(invariant1, &multiplicity[0]));
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
198 default_color_type, default_color_type&
199 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
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)
206 if (color_map[*u] == Color::white())
208 dfs_visitor.start_vertex(*u, G1);
209 depth_first_visit(G1, *u, dfs_visitor, color_map);
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
219 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
222 for (vertex_iter v = dfs_vertices.begin(); v != dfs_vertices.end();
226 sort(ordered_edges, edge_cmp(G1, dfs_num));
229 return this->match(ordered_edges.begin(), dfs_num_k);
233 struct match_continuation
241 typedef typename graph_traits< Graph2 >::vertex_iterator
243 std::pair< vertex_iterator, vertex_iterator > G2_verts;
244 typedef typename graph_traits< Graph2 >::adjacency_iterator
246 std::pair< adjacency_iterator, adjacency_iterator > fi_adj;
251 bool match(edge_iter iter, int dfs_num_k)
253 std::vector< match_continuation > k;
254 typedef typename graph_traits< Graph2 >::vertex_iterator
256 std::pair< vertex_iterator, vertex_iterator > G2_verts(
258 typedef typename graph_traits< Graph2 >::adjacency_iterator
260 std::pair< adjacency_iterator, adjacency_iterator > fi_adj;
264 if (iter != ordered_edges.end())
266 i = source(*iter, G1);
267 j = target(*iter, G1);
268 if (dfs_num[i] > dfs_num_k)
270 G2_verts = vertices(G2);
271 while (G2_verts.first != G2_verts.second)
274 vertex2_t u = *G2_verts.first;
275 vertex1_t kp1 = dfs_vertices[dfs_num_k + 1];
276 if (invariant1(kp1) == invariant2(u)
284 match_continuation new_k;
285 new_k.position = match_continuation::
287 new_k.G2_verts = G2_verts;
289 new_k.dfs_num_k = dfs_num_k;
300 else if (dfs_num[j] > dfs_num_k)
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));
307 for (int jj = 0; jj < dfs_num_k; ++jj)
309 vertex1_t j = dfs_vertices[jj];
311 -= count(adjacent_vertices(f[j], G2), f[vk]);
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)
321 vertex2_t v = *fi_adj.first;
322 if (invariant2(v) == invariant1(j)
328 BOOST_USING_STD_MAX();
330 = max BOOST_PREVENT_MACRO_SUBSTITUTION(
332 max BOOST_PREVENT_MACRO_SUBSTITUTION(
333 dfs_num[i], dfs_num[j]));
334 match_continuation new_k;
336 = match_continuation::pos_fi_adj_loop;
337 new_k.fi_adj = fi_adj;
339 new_k.dfs_num_k = dfs_num_k;
352 if (container_contains(adjacent_vertices(f[i], G2), f[j]))
355 match_continuation new_k;
356 new_k.position = match_continuation::pos_dfs_num;
364 goto return_point_true;
365 goto return_point_false;
374 const match_continuation& this_k = k.back();
375 switch (this_k.position)
377 case match_continuation::pos_G2_vertex_loop:
379 G2_verts = this_k.G2_verts;
381 dfs_num_k = this_k.dfs_num_k;
383 in_S[*G2_verts.first] = false;
384 i = source(*iter, G1);
385 j = target(*iter, G1);
388 case match_continuation::pos_fi_adj_loop:
390 fi_adj = this_k.fi_adj;
392 dfs_num_k = this_k.dfs_num_k;
394 in_S[*fi_adj.first] = false;
395 i = source(*iter, G1);
396 j = target(*iter, G1);
399 case match_continuation::pos_dfs_num:
402 goto return_point_false;
406 BOOST_ASSERT(!"Bad position");
418 template < typename Graph, typename InDegreeMap >
419 void compute_in_degree(const Graph& g, InDegreeMap in_degree_map)
421 BGL_FORALL_VERTICES_T(v, g, Graph)
422 put(in_degree_map, v, 0);
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);
429 } // namespace detail
431 template < typename InDegreeMap, typename Graph > class degree_vertex_invariant
433 typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
434 typedef typename graph_traits< Graph >::degree_size_type size_type;
437 typedef vertex_t argument_type;
438 typedef size_type result_type;
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)
446 BGL_FORALL_VERTICES_T(v, g, Graph)
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));
455 size_type operator()(vertex_t v) const
457 return (m_max_vertex_in_degree + 1) * out_degree(v, m_g)
458 + get(m_in_degree_map, v);
460 // The largest possible vertex invariant number
461 size_type max BOOST_PREVENT_MACRO_SUBSTITUTION() const
463 return (m_max_vertex_in_degree + 1) * (m_max_vertex_out_degree + 1);
467 InDegreeMap m_in_degree_map;
468 size_type m_max_vertex_in_degree;
469 size_type m_max_vertex_out_degree;
473 // Count actual number of vertices, even in filtered graphs.
474 template < typename Graph > size_t count_vertices(const Graph& g)
477 BGL_FORALL_VERTICES_T(v, g, Graph)
485 template < typename Graph1, typename Graph2, typename IsoMapping,
486 typename Invariant1, typename Invariant2, typename IndexMap1,
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)
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> ));
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;
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 >));
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));
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));
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));
523 if (count_vertices(G1) != count_vertices(G2))
525 if (count_vertices(G1) == 0 && count_vertices(G2) == 0)
528 detail::isomorphism_algo< Graph1, Graph2, IsoMapping, Invariant1,
529 Invariant2, IndexMap1, IndexMap2 >
530 algo(G1, G2, f, invariant1, invariant2, max_invariant, index_map1,
532 return algo.test_isomorphism();
538 template < typename Graph1, typename Graph2, typename IsoMapping,
539 typename IndexMap1, typename IndexMap2, typename P, typename T,
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)
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
550 std::size_t, std::size_t&
551 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
555 in_degree1_vec.begin(), in_degree1_vec.size(), index_map1);
556 compute_in_degree(G1, in_degree1);
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
563 std::size_t, std::size_t&
564 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
568 in_degree2_vec.begin(), in_degree2_vec.size(), index_map2);
569 compute_in_degree(G2, in_degree2);
571 degree_vertex_invariant< InDeg1, Graph1 > invariant1(in_degree1, G1);
572 degree_vertex_invariant< InDeg2, Graph2 > invariant2(in_degree2, G2);
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()),
579 index_map1, index_map2);
582 template < typename G, typename Index > struct make_degree_invariant
586 make_degree_invariant(const G& g, const Index& index)
590 typedef typename boost::graph_traits< G >::degree_size_type
592 typedef shared_array_property_map< degree_size_type, Index >
594 typedef degree_vertex_invariant< prop_map_type, G > result_type;
595 result_type operator()() const
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);
604 } // namespace detail
610 template < typename Graph1, typename Graph2 > struct isomorphism_impl
612 typedef bool result_type;
613 typedef result_type type;
614 template < typename ArgPack >
615 bool operator()(const Graph1& g1, const Graph2& g2,
616 const ArgPack& arg_pack) const
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
633 typename std::vector< vertex2_t >::size_type n
634 = (typename std::vector< vertex2_t >::size_type)
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,
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);
662 BOOST_GRAPH_MAKE_FORWARDING_FUNCTION(isomorphism, 2, 6)
665 // Named parameter interface
666 BOOST_GRAPH_MAKE_OLD_STYLE_PARAMETER_FUNCTION(isomorphism, 2)
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 >
674 inline bool verify_isomorphism(
675 const Graph1& g1, const Graph2& g2, IsoMap iso_map)
678 // problematic for filtered_graph!
679 if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
683 BGL_FORALL_EDGES_T(e1, g1, Graph1)
685 bool found_edge = false;
686 BGL_FORALL_EDGES_T(e2, g2, Graph2)
688 if (source(e2, g2) == get(iso_map, source(e1, g1))
689 && target(e2, g2) == get(iso_map, target(e1, g1)))
704 #ifdef BOOST_ISO_INCLUDED_ITER_MACROS
705 #undef BOOST_ISO_INCLUDED_ITER_MACROS
706 #include <boost/graph/iteration_macros_undef.hpp>
709 #endif // BOOST_GRAPH_ISOMORPHISM_HPP