isomorphism-impl-v3.w

来自「Boost provides free peer-reviewed portab」· W 代码 · 共 988 行 · 第 1/3 页

W
988 行
字号
but we want the roots of the DFS tree's to be ordered by invariantmultiplicity.  Therefore we implement the outer-loop of the DFS hereand then call \code{depth\_\-first\_\-visit} to handle the recursiveportion of the DFS. The \code{record\_dfs\_order} adapts the DFS torecord the ordering, storing the results in in the\code{dfs\_vertices} and \code{ordered\_edges} arrays. We then createthe \code{dfs\_num} array which provides a mapping from vertex to DFSnumber.@d Order vertices and edges by DFS@{std::vector<default_color_type> color_vec(num_vertices(G1));safe_iterator_property_map<std::vector<default_color_type>::iterator, IndexMap1>     color_map(color_vec.begin(), color_vec.size(), index_map1);record_dfs_order dfs_visitor(dfs_vertices, ordered_edges);typedef color_traits<default_color_type> Color;for (vertex_iter u = V_mult.begin(); u != V_mult.end(); ++u) {    if (color_map[*u] == Color::white()) {        dfs_visitor.start_vertex(*u, G1);        depth_first_visit(G1, *u, dfs_visitor, color_map);    }}// Create the dfs_num array and dfs_num_mapdfs_num_vec.resize(num_vertices(G1));dfs_num = make_safe_iterator_property_map(dfs_num_vec.begin(),                          dfs_num_vec.size(), index_map1);size_type n = 0;for (vertex_iter v = dfs_vertices.begin(); v != dfs_vertices.end(); ++v)    dfs_num[*v] = n++;@}\noindent The definition of the \code{record\_dfs\_order} visitorclass is as follows.@d DFS visitor to record vertex and edge order@{struct record_dfs_order : default_dfs_visitor{    record_dfs_order(std::vector<vertex1_t>& v, std::vector<edge1_t>& e)         : vertices(v), edges(e) { }    void discover_vertex(vertex1_t v, const Graph1&) const {        vertices.push_back(v);    }    void examine_edge(edge1_t e, const Graph1& G1) const {        edges.push_back(e);    }    std::vector<vertex1_t>& vertices;    std::vector<edge1_t>& edges;};@}The final stage of the setup is to reorder the edges so that all edgesbelonging to $G_1[k]$ appear before any edges not in $G_1[k]$, for$k=1,...,n$.@d Sort edges according to vertex DFS order@{sort(ordered_edges, edge_cmp(G1, dfs_num));@}\noindent The edge comparison function object is defined as follows.@d Edge comparison predicate@{struct edge_cmp {    edge_cmp(const Graph1& G1, DFSNumMap dfs_num)        : G1(G1), dfs_num(dfs_num) { }    bool operator()(const edge1_t& e1, const edge1_t& e2) const {        using namespace std;        vertex1_t u1 = dfs_num[source(e1,G1)], v1 = dfs_num[target(e1,G1)];        vertex1_t u2 = dfs_num[source(e2,G1)], v2 = dfs_num[target(e2,G1)];        int m1 = max(u1, v1);        int m2 = max(u2, v2);        // lexicographical comparison         return make_pair(m1, make_pair(u1, v1))             < make_pair(m2, make_pair(u2, v2));    }    const Graph1& G1;    DFSNumMap dfs_num;};@}\section{Appendix}@d Typedefs for commonly used types@{typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_t;typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;typedef typename graph_traits<Graph1>::edge_descriptor edge1_t;typedef typename graph_traits<Graph2>::edge_descriptor edge2_t;typedef typename graph_traits<Graph1>::vertices_size_type size_type;typedef typename Invariant1::result_type invar1_value;typedef typename Invariant2::result_type invar2_value;@}@d Data members for the parameters@{const Graph1& G1;const Graph2& G2;IsoMapping f;Invariant1 invariant1;Invariant2 invariant2;std::size_t max_invariant;EdgeCompare edge_compare;IndexMap1 index_map1;IndexMap2 index_map2;@}@d Internal data structures@{std::vector<vertex1_t> dfs_vertices;typedef typename std::vector<vertex1_t>::iterator vertex_iter;std::vector<int> dfs_num_vec;typedef safe_iterator_property_map<typename std::vector<int>::iterator,   IndexMap1> DFSNumMap;DFSNumMap dfs_num;std::vector<edge1_t> ordered_edges;typedef typename std::vector<edge1_t>::iterator edge_iter;std::vector<char> in_S_vec;typedef safe_iterator_property_map<typename std::vector<char>::iterator,    IndexMap2> InSMap;InSMap in_S;int num_edges_on_k;@}@d Isomorphism algorithm constructor@{isomorphism_algo(const Graph1& G1, const Graph2& G2, IsoMapping f,                 Invariant1 invariant1, Invariant2 invariant2,                  std::size_t max_invariant,                 EdgeCompare edge_compare,                 IndexMap1 index_map1, IndexMap2 index_map2)    : G1(G1), G2(G2), f(f), invariant1(invariant1), invariant2(invariant2),      max_invariant(max_invariant), edge_compare(edge_compare),      index_map1(index_map1), index_map2(index_map2){    in_S_vec.resize(num_vertices(G1));    in_S = make_safe_iterator_property_map        (in_S_vec.begin(), in_S_vec.size(), index_map2);}@}@o isomorphism.hpp@{// Copyright (C) 2001 Jeremy Siek, Douglas Gregor, Brian Osman//// Permission to copy, use, sell and distribute this software is granted// provided this copyright notice appears in all copies.// Permission to modify the code and to distribute modified code is granted// provided this copyright notice appears in all copies, and a notice// that the code was modified is included with the copyright notice.//// This software is provided "as is" without express or implied warranty,// and with no claim as to its suitability for any purpose.#ifndef BOOST_GRAPH_ISOMORPHISM_HPP#define BOOST_GRAPH_ISOMORPHISM_HPP#include <utility>#include <vector>#include <iterator>#include <algorithm>#include <boost/graph/iteration_macros.hpp>#include <boost/graph/depth_first_search.hpp>#include <boost/utility.hpp>#include <boost/detail/algorithm.hpp>#include <boost/pending/indirect_cmp.hpp> // for make_indirect_pmapnamespace boost {namespace detail {@<Isomorphism algorithm class@>    template <typename Graph, typename InDegreeMap>void compute_in_degree(const Graph& g, InDegreeMap in_degree_map){    BGL_FORALL_VERTICES_T(v, g, Graph)        put(in_degree_map, v, 0);    BGL_FORALL_VERTICES_T(u, g, Graph)      BGL_FORALL_ADJ_T(u, v, g, Graph)        put(in_degree_map, v, get(in_degree_map, v) + 1);}} // namespace detail@<Degree vertex invariant functor@>@<Isomorphism function interface@>@<Isomorphism function body@>namespace detail {struct default_edge_compare {   template <typename Edge1, typename Edge2>   bool operator()(Edge1 e1, Edge2 e2) const { return true; }};  template <typename Graph1, typename Graph2,           typename IsoMapping,           typename IndexMap1, typename IndexMap2,          typename P, typename T, typename R>bool isomorphism_impl(const Graph1& G1, const Graph2& G2,                       IsoMapping f, IndexMap1 index_map1, IndexMap2 index_map2,                      const bgl_named_params<P,T,R>& params){  std::vector<std::size_t> in_degree1_vec(num_vertices(G1));  typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,     IndexMap1> InDeg1;  InDeg1 in_degree1(in_degree1_vec.begin(), in_degree1_vec.size(), index_map1);  compute_in_degree(G1, in_degree1);  std::vector<std::size_t> in_degree2_vec(num_vertices(G2));  typedef safe_iterator_property_map<std::vector<std::size_t>::iterator,     IndexMap2> InDeg2;  InDeg2 in_degree2(in_degree2_vec.begin(), in_degree2_vec.size(), index_map2);  compute_in_degree(G2, in_degree2);  degree_vertex_invariant<InDeg1, Graph1> invariant1(in_degree1, G1);  degree_vertex_invariant<InDeg2, Graph2> invariant2(in_degree2, G2);  default_edge_compare edge_cmp;  return isomorphism(G1, G2, f,        choose_param(get_param(params, vertex_invariant1_t()), invariant1),        choose_param(get_param(params, vertex_invariant2_t()), invariant2),        choose_param(get_param(params, vertex_max_invariant_t()),                      invariant2.max()),        choose_param(get_param(params, edge_compare_t()), edge_cmp),        index_map1, index_map2        );  }     } // namespace detail// Named parameter interfacetemplate <typename Graph1, typename Graph2, class P, class T, class R>bool isomorphism(const Graph1& g1,                 const Graph2& g2,                 const bgl_named_params<P,T,R>& params){  typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;  typename std::vector<vertex2_t>::size_type n = num_vertices(g1);  std::vector<vertex2_t> f(n);  return detail::isomorphism_impl    (g1, g2,      choose_param(get_param(params, vertex_isomorphism_t()),          make_safe_iterator_property_map(f.begin(), f.size(),                  choose_const_pmap(get_param(params, vertex_index1),                                    g1, vertex_index), vertex2_t())),     choose_const_pmap(get_param(params, vertex_index1), g1, vertex_index),     choose_const_pmap(get_param(params, vertex_index2), g2, vertex_index),     params     );}// All defaults interfacetemplate <typename Graph1, typename Graph2>bool isomorphism(const Graph1& g1, const Graph2& g2){  return isomorphism(g1, g2,    bgl_named_params<int, buffer_param_t>(0));// bogus named param}// Verify that the given mapping iso_map from the vertices of g1 to the// vertices of g2 describes an isomorphism.// Note: this could be made much faster by specializing based on the graph// concepts modeled, but since we're verifying an O(n^(lg n)) algorithm,// O(n^4) won't hurt us.template<typename Graph1, typename Graph2, typename IsoMap>inline bool verify_isomorphism(const Graph1& g1, const Graph2& g2, IsoMap iso_map){#if 0    // problematic for filtered_graph!  if (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))    return false;#endif    for (typename graph_traits<Graph1>::edge_iterator e1 = edges(g1).first;       e1 != edges(g1).second; ++e1) {    bool found_edge = false;    for (typename graph_traits<Graph2>::edge_iterator e2 = edges(g2).first;         e2 != edges(g2).second && !found_edge; ++e2) {      if (source(*e2, g2) == get(iso_map, source(*e1, g1)) &&          target(*e2, g2) == get(iso_map, target(*e1, g1))) {        found_edge = true;      }    }        if (!found_edge)      return false;  }    return true;}} // namespace boost#include <boost/graph/iteration_macros_undef.hpp>#endif // BOOST_GRAPH_ISOMORPHISM_HPP@}\bibliographystyle{abbrv}\bibliography{ggcl}\end{document}% LocalWords:  Isomorphism Siek isomorphism adjacency subgraph subgraphs OM DFS% LocalWords:  ISOMORPH Invariants invariants typename IsoMapping bool const% LocalWords:  VertexInvariant VertexIndexMap iterator typedef VertexG Idx num% LocalWords:  InvarValue struct invar vec iter tmp_matches mult inserter permute ui% LocalWords:  dfs cmp isomorph VertexIter edge_iter_t IndexMap desc RPH ATCH pre% LocalWords:  iterators VertexListGraph EdgeListGraph BidirectionalGraph tmp% LocalWords:  ReadWritePropertyMap VertexListGraphConcept EdgeListGraphConcept% LocalWords:  BidirectionalGraphConcept ReadWritePropertyMapConcept indices ei% LocalWords:  IsoMappingValue ReadablePropertyMapConcept namespace InvarFun% LocalWords:  MultMap vip inline bitset typedefs fj hpp ifndef adaptor params% LocalWords:  bgl param pmap endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?