Ticket #6242: isomorphism_fixed.hpp

File isomorphism_fixed.hpp, 17.8 KB (added by dgleich@…, 11 years ago)
Line 
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/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
18
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
24namespace boost {
25
26 namespace detail {
27
28 template <typename Graph1, typename Graph2, typename IsoMapping,
29 typename Invariant1, typename Invariant2,
30 typename IndexMap1, typename IndexMap2>
31 class isomorphism_algo
32 {
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;
39
40 const Graph1& G1;
41 const Graph2& G2;
42 IsoMapping f;
43 Invariant1 invariant1;
44 Invariant2 invariant2;
45 std::size_t max_invariant;
46 IndexMap1 index_map1;
47 IndexMap2 index_map2;
48
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,
53 IndexMap1
54#ifdef BOOST_NO_STD_ITERATOR_TRAITS
55 , int, int&
56#endif /* BOOST_NO_STD_ITERATOR_TRAITS */
57 > DFSNumMap;
58 DFSNumMap dfs_num;
59 std::vector<edge1_t> ordered_edges;
60 typedef typename std::vector<edge1_t>::iterator edge_iter;
61
62 std::vector<char> in_S_vec;
63 typedef safe_iterator_property_map<typename std::vector<char>::iterator,
64 IndexMap2
65#ifdef BOOST_NO_STD_ITERATOR_TRAITS
66 , char, char&
67#endif /* BOOST_NO_STD_ITERATOR_TRAITS */
68 > InSMap;
69 InSMap in_S;
70
71 int num_edges_on_k;
72
73 friend struct compare_multiplicity;
74 struct compare_multiplicity
75 {
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)];
80 }
81 Invariant1 invariant1;
82 size_type* multiplicity;
83 };
84
85 struct record_dfs_order : default_dfs_visitor
86 {
87 record_dfs_order(std::vector<vertex1_t>& v, std::vector<edge1_t>& e)
88 : vertices(v), edges(e) { }
89
90 void discover_vertex(vertex1_t v, const Graph1&) const {
91 vertices.push_back(v);
92 }
93 void examine_edge(edge1_t e, const Graph1&) const {
94 edges.push_back(e);
95 }
96 std::vector<vertex1_t>& vertices;
97 std::vector<edge1_t>& edges;
98 };
99
100 struct edge_cmp {
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 {
104 using namespace std;
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));
112 }
113 const Graph1& G1;
114 DFSNumMap dfs_num;
115 };
116
117 public:
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)
124 {
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
129 , in_S_vec.front()
130#endif /* BOOST_NO_STD_ITERATOR_TRAITS */
131 );
132 }
133
134 bool test_isomorphism()
135 {
136 // reset isomapping
137 BGL_FORALL_VERTICES_T(v, G1, Graph1)
138 f[v] = graph_traits<Graph2>::null_vertex();
139
140 {
141 std::vector<invar1_value> invar1_array;
142 BGL_FORALL_VERTICES_T(v, G1, Graph1)
143 invar1_array.push_back(invariant1(v));
144 sort(invar1_array);
145
146 std::vector<invar2_value> invar2_array;
147 BGL_FORALL_VERTICES_T(v, G2, Graph2)
148 invar2_array.push_back(invariant2(v));
149 sort(invar2_array);
150 if (! equal(invar1_array, invar2_array))
151 return false;
152 }
153
154 std::vector<vertex1_t> V_mult;
155 BGL_FORALL_VERTICES_T(v, G1, Graph1)
156 V_mult.push_back(v);
157 {
158 std::vector<size_type> multiplicity(max_invariant, 0);
159 BGL_FORALL_VERTICES_T(v, G1, Graph1)
160 ++multiplicity[invariant1(v)];
161 sort(V_mult, compare_multiplicity(invariant1, &multiplicity[0]));
162 }
163
164 std::vector<default_color_type> color_vec(num_vertices(G1));
165 safe_iterator_property_map<std::vector<default_color_type>::iterator,
166 IndexMap1
167#ifdef BOOST_NO_STD_ITERATOR_TRAITS
168 , default_color_type, default_color_type&
169#endif /* BOOST_NO_STD_ITERATOR_TRAITS */
170 >
171 color_map(color_vec.begin(), color_vec.size(), index_map1);
172 record_dfs_order dfs_visitor(dfs_vertices, ordered_edges);
173 typedef color_traits<default_color_type> Color;
174 for (vertex_iter u = V_mult.begin(); u != V_mult.end(); ++u) {
175 if (color_map[*u] == Color::white()) {
176 dfs_visitor.start_vertex(*u, G1);
177 depth_first_visit(G1, *u, dfs_visitor, color_map);
178 }
179 }
180 // Create the dfs_num array and dfs_num_map
181 dfs_num_vec.resize(num_vertices(G1));
182 dfs_num = make_safe_iterator_property_map(dfs_num_vec.begin(),
183 dfs_num_vec.size(),
184 index_map1
185#ifdef BOOST_NO_STD_ITERATOR_TRAITS
186 , dfs_num_vec.front()
187#endif /* BOOST_NO_STD_ITERATOR_TRAITS */
188 );
189 size_type n = 0;
190 for (vertex_iter v = dfs_vertices.begin(); v != dfs_vertices.end(); ++v)
191 dfs_num[*v] = n++;
192
193 sort(ordered_edges, edge_cmp(G1, dfs_num));
194
195
196 int dfs_num_k = -1;
197 return this->match(ordered_edges.begin(), dfs_num_k);
198 }
199
200 private:
201 bool match(edge_iter iter, int dfs_num_k)
202 {
203 if (iter != ordered_edges.end()) {
204 vertex1_t i = source(*iter, G1), j = target(*iter, G2);
205 if (dfs_num[i] > dfs_num_k) {
206 vertex1_t kp1 = dfs_vertices[dfs_num_k + 1];
207 BGL_FORALL_VERTICES_T(u, G2, Graph2) {
208 if (invariant1(kp1) == invariant2(u) && in_S[u] == false) {
209 f[kp1] = u;
210 in_S[u] = true;
211 num_edges_on_k = 0;
212
213 if (match(iter, dfs_num_k + 1))
214#if 0
215 // dwa 2003/7/11 -- this *HAS* to be a bug!
216 ;
217#endif
218 return true;
219
220 in_S[u] = false;
221 }
222 }
223
224 }
225 else if (dfs_num[j] > dfs_num_k) {
226 vertex1_t k = dfs_vertices[dfs_num_k];
227 num_edges_on_k -=
228 count_if(adjacent_vertices(f[k], G2), make_indirect_pmap(in_S));
229
230 for (int jj = 0; jj < dfs_num_k; ++jj) {
231 vertex1_t j = dfs_vertices[jj];
232 num_edges_on_k -= count(adjacent_vertices(f[j], G2), f[k]);
233 }
234
235 if (num_edges_on_k != 0)
236 return false;
237 BGL_FORALL_ADJ_T(f[i], v, G2, Graph2)
238 if (invariant2(v) == invariant1(j) && in_S[v] == false) {
239 f[j] = v;
240 in_S[v] = true;
241 num_edges_on_k = 1;
242 BOOST_USING_STD_MAX();
243 int next_k = max BOOST_PREVENT_MACRO_SUBSTITUTION(dfs_num_k, max BOOST_PREVENT_MACRO_SUBSTITUTION(dfs_num[i], dfs_num[j]));
244 if (match(boost::next(iter), next_k))
245 return true;
246 in_S[v] = false;
247 }
248
249
250 }
251 else {
252 if (container_contains(adjacent_vertices(f[i], G2), f[j])) {
253 ++num_edges_on_k;
254 if (match(boost::next(iter), dfs_num_k))
255 return true;
256 }
257
258 }
259 } else
260 return true;
261 return false;
262 }
263
264 };
265
266
267 template <typename Graph, typename InDegreeMap>
268 void compute_in_degree(const Graph& g, InDegreeMap in_degree_map)
269 {
270 BGL_FORALL_VERTICES_T(v, g, Graph)
271 put(in_degree_map, v, 0);
272
273 BGL_FORALL_VERTICES_T(u, g, Graph)
274 BGL_FORALL_ADJ_T(u, v, g, Graph)
275 put(in_degree_map, v, get(in_degree_map, v) + 1);
276 }
277
278 } // namespace detail
279
280
281 template <typename InDegreeMap, typename Graph>
282 class degree_vertex_invariant
283 {
284 typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
285 typedef typename graph_traits<Graph>::degree_size_type size_type;
286 public:
287 typedef vertex_t argument_type;
288 typedef size_type result_type;
289
290 degree_vertex_invariant(const InDegreeMap& in_degree_map, const Graph& g)
291 : m_in_degree_map(in_degree_map),
292 m_max_vertex_in_degree(0),
293 m_max_vertex_out_degree(0),
294 m_g(g) {
295 BGL_FORALL_VERTICES_T(v, g, Graph) {
296 m_max_vertex_in_degree =
297 (std::max)(m_max_vertex_in_degree, get(m_in_degree_map, v));
298 m_max_vertex_out_degree =
299 (std::max)(m_max_vertex_out_degree, out_degree(v, g));
300 }
301 }
302
303 size_type operator()(vertex_t v) const {
304 return (m_max_vertex_in_degree + 1) * out_degree(v, m_g)
305 + get(m_in_degree_map, v);
306 }
307 // The largest possible vertex invariant number
308 size_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const {
309 return (m_max_vertex_in_degree + 2) * m_max_vertex_out_degree + 1;
310 }
311 private:
312 InDegreeMap m_in_degree_map;
313 size_type m_max_vertex_in_degree;
314 size_type m_max_vertex_out_degree;
315 const Graph& m_g;
316 };
317
318
319 template <typename Graph1, typename Graph2, typename IsoMapping,
320 typename Invariant1, typename Invariant2,
321 typename IndexMap1, typename IndexMap2>
322 bool isomorphism(const Graph1& G1, const Graph2& G2, IsoMapping f,
323 Invariant1 invariant1, Invariant2 invariant2,
324 std::size_t max_invariant,
325 IndexMap1 index_map1, IndexMap2 index_map2)
326
327 {
328 // Graph requirements
329 function_requires< VertexListGraphConcept<Graph1> >();
330 function_requires< EdgeListGraphConcept<Graph1> >();
331 function_requires< VertexListGraphConcept<Graph2> >();
332 function_requires< BidirectionalGraphConcept<Graph2> >();
333
334 typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_t;
335 typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
336 typedef typename graph_traits<Graph1>::vertices_size_type size_type;
337
338 // Vertex invariant requirement
339 function_requires< AdaptableUnaryFunctionConcept<Invariant1,
340 size_type, vertex1_t> >();
341 function_requires< AdaptableUnaryFunctionConcept<Invariant2,
342 size_type, vertex2_t> >();
343
344 // Property map requirements
345 function_requires< ReadWritePropertyMapConcept<IsoMapping, vertex1_t> >();
346 typedef typename property_traits<IsoMapping>::value_type IsoMappingValue;
347 BOOST_STATIC_ASSERT((is_same<IsoMappingValue, vertex2_t>::value));
348
349 function_requires< ReadablePropertyMapConcept<IndexMap1, vertex1_t> >();
350 typedef typename property_traits<IndexMap1>::value_type IndexMap1Value;
351 BOOST_STATIC_ASSERT((is_convertible<IndexMap1Value, size_type>::value));
352
353 function_requires< ReadablePropertyMapConcept<IndexMap2, vertex2_t> >();
354 typedef typename property_traits<IndexMap2>::value_type IndexMap2Value;
355 BOOST_STATIC_ASSERT((is_convertible<IndexMap2Value, size_type>::value));
356
357 if (num_vertices(G1) != num_vertices(G2))
358 return false;
359 if (num_vertices(G1) == 0 && num_vertices(G2) == 0)
360 return true;
361
362 detail::isomorphism_algo<Graph1, Graph2, IsoMapping, Invariant1,
363 Invariant2, IndexMap1, IndexMap2>
364 algo(G1, G2, f, invariant1, invariant2, max_invariant,
365 index_map1, index_map2);
366 return algo.test_isomorphism();
367 }
368
369
370 namespace detail {
371
372 template <typename Graph1, typename Graph2,
373 typename IsoMapping,
374 typename IndexMap1, typename IndexMap2,
375 typename P, typename T, typename R>
376 bool isomorphism_impl(const Graph1& G1, const Graph2& G2,
377 IsoMapping f, IndexMap1 index_map1, IndexMap2 index_map2,
378 const bgl_named_params<P,T,R>& params)
379 {
380 std::vector<std::size_t> in_degree1_vec(num_vertices(G1));
381 typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,
382 IndexMap1
383#ifdef BOOST_NO_STD_ITERATOR_TRAITS
384 , std::size_t, std::size_t&
385#endif /* BOOST_NO_STD_ITERATOR_TRAITS */
386 > InDeg1;
387 InDeg1 in_degree1(in_degree1_vec.begin(), in_degree1_vec.size(), index_map1);
388 compute_in_degree(G1, in_degree1);
389
390 std::vector<std::size_t> in_degree2_vec(num_vertices(G2));
391 typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,
392 IndexMap2
393#ifdef BOOST_NO_STD_ITERATOR_TRAITS
394 , std::size_t, std::size_t&
395#endif /* BOOST_NO_STD_ITERATOR_TRAITS */
396 > InDeg2;
397 InDeg2 in_degree2(in_degree2_vec.begin(), in_degree2_vec.size(), index_map2);
398 compute_in_degree(G2, in_degree2);
399
400 degree_vertex_invariant<InDeg1, Graph1> invariant1(in_degree1, G1);
401 degree_vertex_invariant<InDeg2, Graph2> invariant2(in_degree2, G2);
402
403 return isomorphism(G1, G2, f,
404 choose_param(get_param(params, vertex_invariant1_t()), invariant1),
405 choose_param(get_param(params, vertex_invariant2_t()), invariant2),
406 choose_param(get_param(params, vertex_max_invariant_t()), (invariant2.max)()),
407 index_map1, index_map2
408 );
409 }
410
411 } // namespace detail
412
413
414 // Named parameter interface
415 template <typename Graph1, typename Graph2, class P, class T, class R>
416 bool isomorphism(const Graph1& g1,
417 const Graph2& g2,
418 const bgl_named_params<P,T,R>& params)
419 {
420 typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;
421 typename std::vector<vertex2_t>::size_type n = num_vertices(g1);
422 std::vector<vertex2_t> f(n);
423 return detail::isomorphism_impl
424 (g1, g2,
425 choose_param(get_param(params, vertex_isomorphism_t()),
426 make_safe_iterator_property_map(f.begin(), f.size(),
427 choose_const_pmap(get_param(params, vertex_index1),
428 g1, vertex_index), vertex2_t())),
429 choose_const_pmap(get_param(params, vertex_index1), g1, vertex_index),
430 choose_const_pmap(get_param(params, vertex_index2), g2, vertex_index),
431 params
432 );
433 }
434
435 // All defaults interface
436 template <typename Graph1, typename Graph2>
437 bool isomorphism(const Graph1& g1, const Graph2& g2)
438 {
439 return isomorphism(g1, g2,
440 bgl_named_params<int, buffer_param_t>(0));// bogus named param
441 }
442
443
444 // Verify that the given mapping iso_map from the vertices of g1 to the
445 // vertices of g2 describes an isomorphism.
446 // Note: this could be made much faster by specializing based on the graph
447 // concepts modeled, but since we're verifying an O(n^(lg n)) algorithm,
448 // O(n^4) won't hurt us.
449 template<typename Graph1, typename Graph2, typename IsoMap>
450 inline bool verify_isomorphism(const Graph1& g1, const Graph2& g2, IsoMap iso_map)
451 {
452#if 0
453 // problematic for filtered_graph!
454 if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
455 return false;
456#endif
457
458 BGL_FORALL_EDGES_T(e1, g1, Graph1) {
459 bool found_edge = false;
460 BGL_FORALL_EDGES_T(e2, g2, Graph2) {
461 if (source(e2, g2) == get(iso_map, source(e1, g1)) &&
462 target(e2, g2) == get(iso_map, target(e1, g1))) {
463 found_edge = true;
464 }
465 }
466
467 if (!found_edge)
468 return false;
469 }
470
471 return true;
472 }
473
474} // namespace boost
475
476#ifdef BOOST_ISO_INCLUDED_ITER_MACROS
477#undef BOOST_ISO_INCLUDED_ITER_MACROS
478#include <boost/graph/iteration_macros_undef.hpp>
479#endif
480
481#endif // BOOST_GRAPH_ISOMORPHISM_HPP