📄 isomorphism-impl-v2.w
字号:
@{
std::vector<vertex1_t> dfs_vertices;
typedef std::vector<vertex1_t>::iterator vertex_iter;
std::vector<size_type> dfs_number_vec;
safe_iterator_property_map<typename std::vector<size_type>::iterator, IndexMap1>
dfs_number;
std::vector<ordered_edge> ordered_edges;
typedef std::vector<ordered_edge>::iterator edge_iter;
std::vector<vertex1_t> f_inv_vec;
safe_iterator_property_map<typename std::vector<vertex1_t>::iterator,
IndexMap2> f_inv;
std::vector<char> f_assigned_vec;
safe_iterator_property_map<typename std::vector<char>::iterator,
IndexMap1> f_assigned;
std::vector<char> f_inv_assigned_vec;
safe_iterator_property_map<typename std::vector<char>::iterator,
IndexMap2> f_inv_assigned;
int num_edges_incident_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,
IndexMap1 index_map1, IndexMap2 index_map2)
: G1(G1), G2(G2), f(f), invariant1(invariant1), invariant2(invariant2),
max_invariant(max_invariant),
index_map1(index_map1), index_map2(index_map2)
{
f_assigned_vec.resize(num_vertices(G1));
f_assigned = make_safe_iterator_property_map
(f_assigned_vec.begin(), f_assigned_vec.size(), index_map1);
f_inv_vec.resize(num_vertices(G1));
f_inv = make_safe_iterator_property_map
(f_inv_vec.begin(), f_inv_vec.size(), index_map2);
f_inv_assigned_vec.resize(num_vertices(G1));
f_inv_assigned = make_safe_iterator_property_map
(f_inv_assigned_vec.begin(), f_inv_assigned_vec.size(), index_map2);
}
@}
@d Degree vertex invariant functor
@{
template <typename InDegreeMap, typename Graph>
class degree_vertex_invariant
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
typedef typename graph_traits<Graph>::degree_size_type size_type;
public:
typedef vertex_t argument_type;
typedef size_type result_type;
degree_vertex_invariant(const InDegreeMap& in_degree_map, const Graph& g)
: m_in_degree_map(in_degree_map), m_g(g) { }
size_type operator()(vertex_t v) const {
return (num_vertices(m_g) + 1) * out_degree(v, m_g)
+ get(m_in_degree_map, v);
}
// The largest possible vertex invariant number
size_type max() const {
return num_vertices(m_g) * num_vertices(m_g) + num_vertices(m_g);
}
private:
InDegreeMap m_in_degree_map;
const Graph& m_g;
};
@}
ficticiuos edges for the DFS tree roots
Use \code{ordered\_edge} instead of \code{edge1\_t} so that we can create ficticious
edges for the DFS tree roots.
@d Ordered edge class
@{
struct ordered_edge {
ordered_edge(int s, int t) : source(s), target(t) { }
bool operator<(const ordered_edge& e) const {
using namespace std;
int m1 = max(source, target);
int m2 = max(e.source, e.target);
// lexicographical comparison of (m1,source,target) and (m2,e.source,e.target)
return make_pair(m1, make_pair(source, target)) < make_pair(m2, make_pair(e.source, e.target));
}
int source;
int target;
int k_num;
};
@}
\subsection{Recursive Match Function}
@d $v$ is a DFS tree root
@{
// Try all possible mappings
BGL_FORALL_VERTICES_T(y, G2, Graph2) {
if (invariant1(v) == invariant2(y) && f_inv_assigned[y] == false) {
f[v] = y; f_assigned[v] = true;
f_inv[y] = v; f_inv_assigned[y] = true;
num_edges_incident_on_k = 0;
if (match(next(iter)))
return true;
f_assigned[v] = false;
f_inv_assigned[y] = false;
}
}
@}
Growing the subgraph.
@d $v$ is an unmatched vertex, $(u,v)$ is a tree edge
@{
@<Count out-edges of $f(k)$ in $G_2[S]$@>
@<Count in-edges of $f(k)$ in $G_2[S]$@>
if (num_edges_incident_on_k != 0)
return false;
@<Assign $v$ to some vertex in $V_2 - S$@>
@}
@d Count out-edges of $f(k)$ in $G_2[S]$
@{
BGL_FORALL_ADJACENT_T(f[k], w, G2, Graph2)
if (f_inv_assigned[w] == true)
--num_edges_incident_on_k;
@}
@d Count in-edges of $f(k)$ in $G_2[S]$
@{
for (std::size_t jj = 0; jj < k_num; ++jj) {
vertex1_t j = dfs_vertices[jj];
BGL_FORALL_ADJACENT_T(f[j], w, G2, Graph2)
if (w == f[k])
--num_edges_incident_on_k;
}
@}
@d Assign $v$ to some vertex in $V_2 - S$
@{
BGL_FORALL_ADJACENT_T(f[u], y, G2, Graph2)
if (invariant1(v) == invariant2(y) && f_inv_assigned[y] == false) {
f[v] = y; f_assigned[v] = true;
f_inv[y] = v; f_inv_assigned[y] = true;
num_edges_incident_on_k = 1;
if (match(next(iter)))
return true;
f_assigned[v] = false;
f_inv_assigned[y] = false;
}
@}
@d Check to see if there is an edge in $G_2$ to match $(u,v)$
@{
bool verify = false;
assert(f_assigned[u] == true);
BGL_FORALL_ADJACENT_T(f[u], y, G2, Graph2) {
if (y == f[v]) {
verify = true;
break;
}
}
if (verify == true) {
++num_edges_incident_on_k;
if (match(next(iter)))
return true;
}
@}
@o isomorphism-v2.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/tuple/tuple.hpp>
namespace 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_ADJACENT_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 {
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);
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()),
index_map1, index_map2
);
}
} // namespace detail
// Named parameter interface
template <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 interface
template <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 (num_vertices(g1) != num_vertices(g2) || num_edges(g1) != num_edges(g2))
return false;
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -