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/graph/depth_first_search.hpp>
15 #include <boost/utility.hpp>
16 #include <boost/detail/algorithm.hpp>
17 #include <boost/pending/indirect_cmp.hpp> // for make_indirect_pmap
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>
28 template <typename Graph1, typename Graph2, typename IsoMapping,
29 typename Invariant1, typename Invariant2,
30 typename IndexMap1, typename IndexMap2>
31 class isomorphism_algo
33 typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_t;
34 typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
35 typedef typename graph_traits<Graph1>::edge_descriptor edge1_t;
36 typedef typename graph_traits<Graph1>::vertices_size_type size_type;
37 typedef typename Invariant1::result_type invar1_value;
38 typedef typename Invariant2::result_type invar2_value;
43 Invariant1 invariant1;
44 Invariant2 invariant2;
45 std::size_t max_invariant;
49 std::vector<vertex1_t> dfs_vertices;
50 typedef typename std::vector<vertex1_t>::iterator vertex_iter;
51 std::vector<int> dfs_num_vec;
52 typedef safe_iterator_property_map<typename std::vector<int>::iterator,
54 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
56 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
59 std::vector<edge1_t> ordered_edges;
60 typedef typename std::vector<edge1_t>::iterator edge_iter;
62 std::vector<char> in_S_vec;
63 typedef safe_iterator_property_map<typename std::vector<char>::iterator,
65 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
67 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
73 friend struct compare_multiplicity;
74 struct compare_multiplicity
76 compare_multiplicity(Invariant1 invariant1, size_type* multiplicity)
77 : invariant1(invariant1), multiplicity(multiplicity) { }
78 bool operator()(const vertex1_t& x, const vertex1_t& y) const {
79 return multiplicity[invariant1(x)] < multiplicity[invariant1(y)];
81 Invariant1 invariant1;
82 size_type* multiplicity;
85 struct record_dfs_order : default_dfs_visitor
87 record_dfs_order(std::vector<vertex1_t>& v, std::vector<edge1_t>& e)
88 : vertices(v), edges(e) { }
90 void discover_vertex(vertex1_t v, const Graph1&) const {
91 vertices.push_back(v);
93 void examine_edge(edge1_t e, const Graph1& G1) const {
96 std::vector<vertex1_t>& vertices;
97 std::vector<edge1_t>& edges;
101 edge_cmp(const Graph1& G1, DFSNumMap dfs_num)
102 : G1(G1), dfs_num(dfs_num) { }
103 bool operator()(const edge1_t& e1, const edge1_t& e2) const {
105 int u1 = dfs_num[source(e1,G1)], v1 = dfs_num[target(e1,G1)];
106 int u2 = dfs_num[source(e2,G1)], v2 = dfs_num[target(e2,G1)];
107 int m1 = (max)(u1, v1);
108 int m2 = (max)(u2, v2);
109 // lexicographical comparison
110 return std::make_pair(m1, std::make_pair(u1, v1))
111 < std::make_pair(m2, std::make_pair(u2, v2));
118 isomorphism_algo(const Graph1& G1, const Graph2& G2, IsoMapping f,
119 Invariant1 invariant1, Invariant2 invariant2, std::size_t max_invariant,
120 IndexMap1 index_map1, IndexMap2 index_map2)
121 : G1(G1), G2(G2), f(f), invariant1(invariant1), invariant2(invariant2),
122 max_invariant(max_invariant),
123 index_map1(index_map1), index_map2(index_map2)
125 in_S_vec.resize(num_vertices(G1));
126 in_S = make_safe_iterator_property_map
127 (in_S_vec.begin(), in_S_vec.size(), index_map2
128 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
130 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
134 bool test_isomorphism()
137 std::vector<invar1_value> invar1_array;
138 BGL_FORALL_VERTICES_T(v, G1, Graph1)
139 invar1_array.push_back(invariant1(v));
142 std::vector<invar2_value> invar2_array;
143 BGL_FORALL_VERTICES_T(v, G2, Graph2)
144 invar2_array.push_back(invariant2(v));
146 if (! equal(invar1_array, invar2_array))
150 std::vector<vertex1_t> V_mult;
151 BGL_FORALL_VERTICES_T(v, G1, Graph1)
154 std::vector<size_type> multiplicity(max_invariant, 0);
155 BGL_FORALL_VERTICES_T(v, G1, Graph1)
156 ++multiplicity[invariant1(v)];
157 sort(V_mult, compare_multiplicity(invariant1, &multiplicity[0]));
160 std::vector<default_color_type> color_vec(num_vertices(G1));
161 safe_iterator_property_map<std::vector<default_color_type>::iterator,
163 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
164 , default_color_type, default_color_type&
165 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
167 color_map(color_vec.begin(), color_vec.size(), index_map1);
168 record_dfs_order dfs_visitor(dfs_vertices, ordered_edges);
169 typedef color_traits<default_color_type> Color;
170 for (vertex_iter u = V_mult.begin(); u != V_mult.end(); ++u) {
171 if (color_map[*u] == Color::white()) {
172 dfs_visitor.start_vertex(*u, G1);
173 depth_first_visit(G1, *u, dfs_visitor, color_map);
176 // Create the dfs_num array and dfs_num_map
177 dfs_num_vec.resize(num_vertices(G1));
178 dfs_num = make_safe_iterator_property_map(dfs_num_vec.begin(),
181 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
182 , dfs_num_vec.front()
183 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
186 for (vertex_iter v = dfs_vertices.begin(); v != dfs_vertices.end(); ++v)
189 sort(ordered_edges, edge_cmp(G1, dfs_num));
193 return this->match(ordered_edges.begin(), dfs_num_k);
197 bool match(edge_iter iter, int dfs_num_k)
199 if (iter != ordered_edges.end()) {
200 vertex1_t i = source(*iter, G1), j = target(*iter, G2);
201 if (dfs_num[i] > dfs_num_k) {
202 vertex1_t kp1 = dfs_vertices[dfs_num_k + 1];
203 BGL_FORALL_VERTICES_T(u, G2, Graph2) {
204 if (invariant1(kp1) == invariant2(u) && in_S[u] == false) {
209 if (match(iter, dfs_num_k + 1))
211 // dwa 2003/7/11 -- this *HAS* to be a bug!
221 else if (dfs_num[j] > dfs_num_k) {
222 vertex1_t k = dfs_vertices[dfs_num_k];
224 count_if(adjacent_vertices(f[k], G2), make_indirect_pmap(in_S));
226 for (int jj = 0; jj < dfs_num_k; ++jj) {
227 vertex1_t j = dfs_vertices[jj];
228 num_edges_on_k -= count(adjacent_vertices(f[j], G2), f[k]);
231 if (num_edges_on_k != 0)
233 BGL_FORALL_ADJ_T(f[i], v, G2, Graph2)
234 if (invariant2(v) == invariant1(j) && in_S[v] == false) {
238 BOOST_USING_STD_MAX();
239 int next_k = max BOOST_PREVENT_MACRO_SUBSTITUTION(dfs_num_k, max BOOST_PREVENT_MACRO_SUBSTITUTION(dfs_num[i], dfs_num[j]));
240 if (match(next(iter), next_k))
248 if (contains(adjacent_vertices(f[i], G2), f[j])) {
250 if (match(next(iter), dfs_num_k))
263 template <typename Graph, typename InDegreeMap>
264 void compute_in_degree(const Graph& g, InDegreeMap in_degree_map)
266 BGL_FORALL_VERTICES_T(v, g, Graph)
267 put(in_degree_map, v, 0);
269 BGL_FORALL_VERTICES_T(u, g, Graph)
270 BGL_FORALL_ADJ_T(u, v, g, Graph)
271 put(in_degree_map, v, get(in_degree_map, v) + 1);
274 } // namespace detail
277 template <typename InDegreeMap, typename Graph>
278 class degree_vertex_invariant
280 typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
281 typedef typename graph_traits<Graph>::degree_size_type size_type;
283 typedef vertex_t argument_type;
284 typedef size_type result_type;
286 degree_vertex_invariant(const InDegreeMap& in_degree_map, const Graph& g)
287 : m_in_degree_map(in_degree_map), m_g(g) { }
289 size_type operator()(vertex_t v) const {
290 return (num_vertices(m_g) + 1) * out_degree(v, m_g)
291 + get(m_in_degree_map, v);
293 // The largest possible vertex invariant number
294 size_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const {
295 return num_vertices(m_g) * num_vertices(m_g) + num_vertices(m_g);
298 InDegreeMap m_in_degree_map;
303 template <typename Graph1, typename Graph2, typename IsoMapping,
304 typename Invariant1, typename Invariant2,
305 typename IndexMap1, typename IndexMap2>
306 bool isomorphism(const Graph1& G1, const Graph2& G2, IsoMapping f,
307 Invariant1 invariant1, Invariant2 invariant2,
308 std::size_t max_invariant,
309 IndexMap1 index_map1, IndexMap2 index_map2)
312 // Graph requirements
313 function_requires< VertexListGraphConcept<Graph1> >();
314 function_requires< EdgeListGraphConcept<Graph1> >();
315 function_requires< VertexListGraphConcept<Graph2> >();
316 function_requires< BidirectionalGraphConcept<Graph2> >();
318 typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_t;
319 typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
320 typedef typename graph_traits<Graph1>::vertices_size_type size_type;
322 // Vertex invariant requirement
323 function_requires< AdaptableUnaryFunctionConcept<Invariant1,
324 size_type, vertex1_t> >();
325 function_requires< AdaptableUnaryFunctionConcept<Invariant2,
326 size_type, vertex2_t> >();
328 // Property map requirements
329 function_requires< ReadWritePropertyMapConcept<IsoMapping, vertex1_t> >();
330 typedef typename property_traits<IsoMapping>::value_type IsoMappingValue;
331 BOOST_STATIC_ASSERT((is_same<IsoMappingValue, vertex2_t>::value));
333 function_requires< ReadablePropertyMapConcept<IndexMap1, vertex1_t> >();
334 typedef typename property_traits<IndexMap1>::value_type IndexMap1Value;
335 BOOST_STATIC_ASSERT((is_convertible<IndexMap1Value, size_type>::value));
337 function_requires< ReadablePropertyMapConcept<IndexMap2, vertex2_t> >();
338 typedef typename property_traits<IndexMap2>::value_type IndexMap2Value;
339 BOOST_STATIC_ASSERT((is_convertible<IndexMap2Value, size_type>::value));
341 if (num_vertices(G1) != num_vertices(G2))
343 if (num_vertices(G1) == 0 && num_vertices(G2) == 0)
346 detail::isomorphism_algo<Graph1, Graph2, IsoMapping, Invariant1,
347 Invariant2, IndexMap1, IndexMap2>
348 algo(G1, G2, f, invariant1, invariant2, max_invariant,
349 index_map1, index_map2);
350 return algo.test_isomorphism();
356 template <typename Graph1, typename Graph2,
358 typename IndexMap1, typename IndexMap2,
359 typename P, typename T, typename R>
360 bool isomorphism_impl(const Graph1& G1, const Graph2& G2,
361 IsoMapping f, IndexMap1 index_map1, IndexMap2 index_map2,
362 const bgl_named_params<P,T,R>& params)
364 std::vector<std::size_t> in_degree1_vec(num_vertices(G1));
365 typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,
367 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
368 , std::size_t, std::size_t&
369 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
371 InDeg1 in_degree1(in_degree1_vec.begin(), in_degree1_vec.size(), index_map1);
372 compute_in_degree(G1, in_degree1);
374 std::vector<std::size_t> in_degree2_vec(num_vertices(G2));
375 typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,
377 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
378 , std::size_t, std::size_t&
379 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
381 InDeg2 in_degree2(in_degree2_vec.begin(), in_degree2_vec.size(), index_map2);
382 compute_in_degree(G2, in_degree2);
384 degree_vertex_invariant<InDeg1, Graph1> invariant1(in_degree1, G1);
385 degree_vertex_invariant<InDeg2, Graph2> invariant2(in_degree2, G2);
387 return isomorphism(G1, G2, f,
388 choose_param(get_param(params, vertex_invariant1_t()), invariant1),
389 choose_param(get_param(params, vertex_invariant2_t()), invariant2),
390 choose_param(get_param(params, vertex_max_invariant_t()), (invariant2.max)()),
391 index_map1, index_map2
395 } // namespace detail
398 // Named parameter interface
399 template <typename Graph1, typename Graph2, class P, class T, class R>
400 bool isomorphism(const Graph1& g1,
402 const bgl_named_params<P,T,R>& params)
404 typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
405 typename std::vector<vertex2_t>::size_type n = num_vertices(g1);
406 std::vector<vertex2_t> f(n);
407 return detail::isomorphism_impl
409 choose_param(get_param(params, vertex_isomorphism_t()),
410 make_safe_iterator_property_map(f.begin(), f.size(),
411 choose_const_pmap(get_param(params, vertex_index1),
412 g1, vertex_index), vertex2_t())),
413 choose_const_pmap(get_param(params, vertex_index1), g1, vertex_index),
414 choose_const_pmap(get_param(params, vertex_index2), g2, vertex_index),
419 // All defaults interface
420 template <typename Graph1, typename Graph2>
421 bool isomorphism(const Graph1& g1, const Graph2& g2)
423 return isomorphism(g1, g2,
424 bgl_named_params<int, buffer_param_t>(0));// bogus named param
428 // Verify that the given mapping iso_map from the vertices of g1 to the
429 // vertices of g2 describes an isomorphism.
430 // Note: this could be made much faster by specializing based on the graph
431 // concepts modeled, but since we're verifying an O(n^(lg n)) algorithm,
432 // O(n^4) won't hurt us.
433 template<typename Graph1, typename Graph2, typename IsoMap>
434 inline bool verify_isomorphism(const Graph1& g1, const Graph2& g2, IsoMap iso_map)
437 // problematic for filtered_graph!
438 if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
442 for (typename graph_traits<Graph1>::edge_iterator e1 = edges(g1).first;
443 e1 != edges(g1).second; ++e1) {
444 bool found_edge = false;
445 for (typename graph_traits<Graph2>::edge_iterator e2 = edges(g2).first;
446 e2 != edges(g2).second && !found_edge; ++e2) {
447 if (source(*e2, g2) == get(iso_map, source(*e1, g1)) &&
448 target(*e2, g2) == get(iso_map, target(*e1, g1))) {
462 #ifdef BOOST_ISO_INCLUDED_ITER_MACROS
463 #undef BOOST_ISO_INCLUDED_ITER_MACROS
464 #include <boost/graph/iteration_macros_undef.hpp>
467 #endif // BOOST_GRAPH_ISOMORPHISM_HPP