epoc32/include/stdapis/boost/graph/isomorphism.hpp
branchSymbian2
changeset 2 2fe1408b6811
equal deleted inserted replaced
1:666f914201fb 2:2fe1408b6811
       
     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 
       
    24 namespace 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& G1) 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         {
       
   137           std::vector<invar1_value> invar1_array;
       
   138           BGL_FORALL_VERTICES_T(v, G1, Graph1)
       
   139             invar1_array.push_back(invariant1(v));
       
   140           sort(invar1_array);
       
   141         
       
   142           std::vector<invar2_value> invar2_array;
       
   143           BGL_FORALL_VERTICES_T(v, G2, Graph2)
       
   144             invar2_array.push_back(invariant2(v));
       
   145           sort(invar2_array);
       
   146           if (! equal(invar1_array, invar2_array))
       
   147             return false;
       
   148         }
       
   149         
       
   150         std::vector<vertex1_t> V_mult;
       
   151         BGL_FORALL_VERTICES_T(v, G1, Graph1)
       
   152           V_mult.push_back(v);
       
   153         {
       
   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]));
       
   158         }
       
   159         
       
   160         std::vector<default_color_type> color_vec(num_vertices(G1));
       
   161         safe_iterator_property_map<std::vector<default_color_type>::iterator,
       
   162                                    IndexMap1
       
   163 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
       
   164                                    , default_color_type, default_color_type&
       
   165 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
       
   166                                    >
       
   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);
       
   174           }
       
   175         }
       
   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(),
       
   179                                                   dfs_num_vec.size(), 
       
   180                                                   index_map1
       
   181 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
       
   182                                                   , dfs_num_vec.front()
       
   183 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
       
   184                                                   );
       
   185         size_type n = 0;
       
   186         for (vertex_iter v = dfs_vertices.begin(); v != dfs_vertices.end(); ++v)
       
   187           dfs_num[*v] = n++;
       
   188         
       
   189         sort(ordered_edges, edge_cmp(G1, dfs_num));
       
   190         
       
   191     
       
   192         int dfs_num_k = -1;
       
   193         return this->match(ordered_edges.begin(), dfs_num_k);
       
   194       }
       
   195     
       
   196     private:
       
   197       bool match(edge_iter iter, int dfs_num_k)
       
   198       {
       
   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) {
       
   205                 f[kp1] = u;
       
   206                 in_S[u] = true;
       
   207                 num_edges_on_k = 0;
       
   208                 
       
   209                 if (match(iter, dfs_num_k + 1))
       
   210 #if 0
       
   211                     // dwa 2003/7/11 -- this *HAS* to be a bug!
       
   212                     ;
       
   213 #endif 
       
   214                     return true;
       
   215                     
       
   216                 in_S[u] = false;
       
   217               }
       
   218             }
       
   219                
       
   220           }
       
   221           else if (dfs_num[j] > dfs_num_k) {
       
   222             vertex1_t k = dfs_vertices[dfs_num_k];
       
   223             num_edges_on_k -= 
       
   224               count_if(adjacent_vertices(f[k], G2), make_indirect_pmap(in_S));
       
   225                 
       
   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]);
       
   229             }
       
   230                 
       
   231             if (num_edges_on_k != 0)
       
   232               return false;
       
   233             BGL_FORALL_ADJ_T(f[i], v, G2, Graph2)
       
   234               if (invariant2(v) == invariant1(j) && in_S[v] == false) {
       
   235                 f[j] = v;
       
   236                 in_S[v] = true;
       
   237                 num_edges_on_k = 1;
       
   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))
       
   241                   return true;
       
   242                 in_S[v] = false;
       
   243               }
       
   244                 
       
   245                 
       
   246           }
       
   247           else {
       
   248             if (contains(adjacent_vertices(f[i], G2), f[j])) {
       
   249               ++num_edges_on_k;
       
   250               if (match(next(iter), dfs_num_k))
       
   251                 return true;
       
   252             }
       
   253                 
       
   254           }
       
   255         } else 
       
   256           return true;
       
   257         return false;
       
   258       }
       
   259     
       
   260     };
       
   261 
       
   262     
       
   263     template <typename Graph, typename InDegreeMap>
       
   264     void compute_in_degree(const Graph& g, InDegreeMap in_degree_map)
       
   265     {
       
   266       BGL_FORALL_VERTICES_T(v, g, Graph)
       
   267         put(in_degree_map, v, 0);
       
   268 
       
   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);
       
   272     }
       
   273 
       
   274   } // namespace detail
       
   275 
       
   276 
       
   277   template <typename InDegreeMap, typename Graph>
       
   278   class degree_vertex_invariant
       
   279   {
       
   280     typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
       
   281     typedef typename graph_traits<Graph>::degree_size_type size_type;
       
   282   public:
       
   283     typedef vertex_t argument_type;
       
   284     typedef size_type result_type;
       
   285 
       
   286     degree_vertex_invariant(const InDegreeMap& in_degree_map, const Graph& g)
       
   287       : m_in_degree_map(in_degree_map), m_g(g) { }
       
   288 
       
   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);
       
   292     }
       
   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);
       
   296     }
       
   297   private:
       
   298     InDegreeMap m_in_degree_map;
       
   299     const Graph& m_g;
       
   300   };
       
   301 
       
   302 
       
   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)
       
   310 
       
   311   {
       
   312     // Graph requirements
       
   313     function_requires< VertexListGraphConcept<Graph1> >();
       
   314     function_requires< EdgeListGraphConcept<Graph1> >();
       
   315     function_requires< VertexListGraphConcept<Graph2> >();
       
   316     function_requires< BidirectionalGraphConcept<Graph2> >();
       
   317     
       
   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;
       
   321     
       
   322     // Vertex invariant requirement
       
   323     function_requires< AdaptableUnaryFunctionConcept<Invariant1,
       
   324       size_type, vertex1_t> >();
       
   325     function_requires< AdaptableUnaryFunctionConcept<Invariant2,
       
   326       size_type, vertex2_t> >();
       
   327     
       
   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));
       
   332     
       
   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));
       
   336     
       
   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));
       
   340     
       
   341     if (num_vertices(G1) != num_vertices(G2))
       
   342       return false;
       
   343     if (num_vertices(G1) == 0 && num_vertices(G2) == 0)
       
   344       return true;
       
   345     
       
   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();
       
   351   }
       
   352 
       
   353 
       
   354   namespace detail {
       
   355   
       
   356     template <typename Graph1, typename Graph2, 
       
   357       typename IsoMapping, 
       
   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)
       
   363     {
       
   364       std::vector<std::size_t> in_degree1_vec(num_vertices(G1));
       
   365       typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,
       
   366                                          IndexMap1
       
   367 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
       
   368                                          , std::size_t, std::size_t&
       
   369 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
       
   370                                          > InDeg1;
       
   371       InDeg1 in_degree1(in_degree1_vec.begin(), in_degree1_vec.size(), index_map1);
       
   372       compute_in_degree(G1, in_degree1);
       
   373 
       
   374       std::vector<std::size_t> in_degree2_vec(num_vertices(G2));
       
   375       typedef safe_iterator_property_map<std::vector<std::size_t>::iterator, 
       
   376                                          IndexMap2
       
   377 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
       
   378                                          , std::size_t, std::size_t&
       
   379 #endif /* BOOST_NO_STD_ITERATOR_TRAITS */
       
   380                                          > InDeg2;
       
   381       InDeg2 in_degree2(in_degree2_vec.begin(), in_degree2_vec.size(), index_map2);
       
   382       compute_in_degree(G2, in_degree2);
       
   383 
       
   384       degree_vertex_invariant<InDeg1, Graph1> invariant1(in_degree1, G1);
       
   385       degree_vertex_invariant<InDeg2, Graph2> invariant2(in_degree2, G2);
       
   386 
       
   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
       
   392                          );  
       
   393     }  
       
   394    
       
   395   } // namespace detail
       
   396 
       
   397 
       
   398   // Named parameter interface
       
   399   template <typename Graph1, typename Graph2, class P, class T, class R>
       
   400   bool isomorphism(const Graph1& g1,
       
   401                    const Graph2& g2,
       
   402                    const bgl_named_params<P,T,R>& params)
       
   403   {
       
   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
       
   408       (g1, g2, 
       
   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),
       
   415        params
       
   416        );
       
   417   }
       
   418 
       
   419   // All defaults interface
       
   420   template <typename Graph1, typename Graph2>
       
   421   bool isomorphism(const Graph1& g1, const Graph2& g2)
       
   422   {
       
   423     return isomorphism(g1, g2,
       
   424                        bgl_named_params<int, buffer_param_t>(0));// bogus named param
       
   425   }
       
   426 
       
   427 
       
   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)
       
   435   {
       
   436 #if 0
       
   437     // problematic for filtered_graph!
       
   438     if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
       
   439       return false;
       
   440 #endif
       
   441   
       
   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))) {
       
   449           found_edge = true;
       
   450         }
       
   451       }
       
   452     
       
   453       if (!found_edge)
       
   454         return false;
       
   455     }
       
   456   
       
   457     return true;
       
   458   }
       
   459 
       
   460 } // namespace boost
       
   461 
       
   462 #ifdef BOOST_ISO_INCLUDED_ITER_MACROS
       
   463 #undef BOOST_ISO_INCLUDED_ITER_MACROS
       
   464 #include <boost/graph/iteration_macros_undef.hpp>
       
   465 #endif
       
   466 
       
   467 #endif // BOOST_GRAPH_ISOMORPHISM_HPP