⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isomorphism-impl.w

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 W
📖 第 1 页 / 共 3 页
字号:
\documentclass[11pt]{report}%\input{defs}\usepackage{math}\usepackage{jweb}\usepackage{lgrind}\usepackage{times}\usepackage{fullpage}\usepackage{graphicx}\newif\ifpdf\ifx\pdfoutput\undefined   \pdffalse\else   \pdfoutput=1   \pdftrue\fi\ifpdf  \usepackage[              pdftex,              colorlinks=true, %change to true for the electronic version              linkcolor=blue,filecolor=blue,pagecolor=blue,urlcolor=blue              ]{hyperref}\fi\ifpdf  \newcommand{\stlconcept}[1]{\href{http://www.sgi.com/tech/stl/#1.html}{{\small \textsf{#1}}}}  \newcommand{\bglconcept}[1]{\href{http://www.boost.org/libs/graph/doc/#1.html}{{\small \textsf{#1}}}}  \newcommand{\pmconcept}[1]{\href{http://www.boost.org/libs/property_map/#1.html}{{\small \textsf{#1}}}}  \newcommand{\myhyperref}[2]{\hyperref[#1]{#2}}  \newcommand{\vizfig}[2]{\begin{figure}[htbp]\centerline{\includegraphics*{#1.pdf}}\caption{#2}\label{fig:#1}\end{figure}}\else  \newcommand{\myhyperref}[2]{#2}  \newcommand{\bglconcept}[1]{{\small \textsf{#1}}}  \newcommand{\pmconcept}[1]{{\small \textsf{#1}}}  \newcommand{\stlconcept}[1]{{\small \textsf{#1}}}  \newcommand{\vizfig}[2]{\begin{figure}[htbp]\centerline{\includegraphics*{#1.eps}}\caption{#2}\label{fig:#1}\end{figure}}\fi\newcommand{\code}[1]{{\small{\em \textbf{#1}}}}% jweb -np isomorphism-impl.w; dot -Tps out.dot -o out.eps; dot -Tps in.dot -o in.eps; latex isomorphism-impl.tex; dvips isomorphism-impl.dvi -o isomorphism-impl.ps\setlength\overfullrule{5pt}\tolerance=10000\sloppy\hfuzz=10pt\makeindex\newcommand{\isomorphic}{\cong}\begin{document}\title{An Implementation of Isomorphism Testing}\author{Jeremy G. Siek}\maketitle\section{Introduction}This paper documents the implementation of the \code{isomorphism()}function of the Boost Graph Library.  The implementation was by JeremySiek with algorithmic improvements and test code from Douglas Gregor.The \code{isomorphism()} function answers the question, ``are thesetwo graphs equal?''  By \emph{equal}, we mean the two graphs have thesame structure---the vertices and edges are connected in the sameway. The mathematical name for this kind of equality is\emph{isomorphic}.An \emph{isomorphism} is a one-to-one mapping of the vertices in onegraph to the vertices of another graph such that adjacency ispreserved. Another words, given graphs $G_{1} = (V_{1},E_{1})$ and$G_{2} = (V_{2},E_{2})$, an isomorphism is a function $f$ such thatfor all pairs of vertices $a,b$ in $V_{1}$, edge $(a,b)$ is in $E_{1}$if and only if edge $(f(a),f(b))$ is in $E_{2}$.Both graphs must be the same size, so let $N = |V_1| = |V_2|$. Thegraph $G_1$ is \emph{isomorphic} to $G_2$ if an isomorphism existsbetween the two graphs, which we denote by $G_1 \isomorphic G_2$.In the following discussion we will need to use several notions fromgraph theory. The graph $G_s=(V_s,E_s)$ is a \emph{subgraph} of graph$G=(V,E)$ if $V_s \subseteq V$ and $E_s \subseteq E$.  An\emph{induced subgraph}, denoted by $G[V_s]$, of a graph $G=(V,E)$consists of the vertices in $V_s$, which is a subset of $V$, and everyedge $(u,v)$ in $E$ such that both $u$ and $v$ are in $V_s$.  We usethe notation $E[V_s]$ to mean the edges in $G[V_s]$.In some places we express a function as a set of pairs, so the set $f= \{ \pair{a_1}{b_1}, \ldots, \pair{a_n}{b_n} \}$means $f(a_i) = b_i$ for $i=1,\ldots,n$.\section{Exhaustive Backtracking Search}The algorithm used by the \code{isomorphism()} function is, atfirst approximation, an exhaustive search implemented viabacktracking.  The backtracking algorithm is a recursive function. Ateach stage we will try to extend the match that we have found so far.So suppose that we have already determined that some subgraph of $G_1$is isomorphic to a subgraph of $G_2$.  We then try to add a vertex toeach subgraph such that the new subgraphs are still isomorphic to oneanother. At some point we may hit a dead end---there are no verticesthat can be added to extend the isomorphic subgraphs. We thenbacktrack to previous smaller matching subgraphs, and try extendingwith a different vertex choice. The process ends by either finding acomplete mapping between $G_1$ and $G_2$ and return true, or byexhausting all possibilities and returning false.We are going to consider the vertices of $G_1$ in a specific order(more about this later), so assume that the vertices of $G_1$ arelabeled $1,\ldots,N$ according to the order that we plan to add themto the subgraph.  Let $G_1[k]$ denote the subgraph of $G_1$ induced bythe first $k$ vertices, with $G_1[0]$ being an empty graph. At eachstage of the recursion we start with an isomorphism $f_{k-1}$ between$G_1[k-1]$ and a subgraph of $G_2$, which we denote by $G_2[S]$, so$G_1[k-1] \isomorphic G_2[S]$. The vertex set $S$ is the subset of$V_2$ that corresponds via $f_{k-1}$ to the first $k-1$ vertices in$G_1$. We try to extend the isomorphism by finding a vertex $v \in V_2- S$ that matches with vertex $k$. If a matching vertex is found, wehave a new isomorphism $f_k$ with $G_1[k] \isomorphic G_2[S \union \{v \}]$.\begin{tabbing}IS\=O\=M\=O\=RPH($k$, $S$, $f_{k-1}$) $\equiv$ \\\>\textbf{if} ($k = |V_1|+1$) \\\>\>\textbf{return} true \\\>\textbf{for} each vertex $v \in V_2 - S$ \\\>\>\textbf{if} (MATCH($k$, $v$)) \\\>\>\>$f_k = f_{k-1} \union \pair{k}{v}$ \\\>\>\>ISOMORPH($k+1$, $S \union \{ v \}$, $f_k$)\\\>\>\textbf{else}\\\>\>\>\textbf{return} false \\\\ISOMORPH($0$, $G_1$, $\emptyset$, $G_2$)\end{tabbing}The basic idea of the match operation is to check whether $G_1[k]$ isisomorphic to $G_2[S \union \{ v \}]$. We already know that $G_1[k-1]\isomorphic G_2[S]$ with the mapping $f_{k-1}$, so all we need to dois verify that the edges in $E_1[k] - E_1[k-1]$ connect vertices thatcorrespond to the vertices connected by the edges in $E_2[S \union \{v \}] - E_2[S]$. The edges in $E_1[k] - E_1[k-1]$ are all theout-edges $(k,j)$ and in-edges $(j,k)$ of $k$ where $j$ is less thanor equal to $k$ according to the ordering.  The edges in $E_2[S \union\{ v \}] - E_2[S]$ consists of all the out-edges $(v,u)$ andin-edges $(u,v)$ of $v$ where $u \in S$.\begin{tabbing}M\=ATCH($k$, $v$) $\equiv$ \\\>$out \leftarrow \forall (k,j) \in E_1[k] - E_1[k-1] \Big( (v,f(j)) \in E_2[S \union \{ v \}] - E_2[S] \Big)$ \\\>$in \leftarrow \forall (j,k) \in E_1[k] - E_1[k-1] \Big( (f(j),v) \in E_2[S \union \{ v \}] - E_2[S] \Big)$ \\\>\textbf{return} $out \Land in$ \end{tabbing}The problem with the exhaustive backtracking algorithm is that thereare $N!$ possible vertex mappings, and $N!$ gets very large as $N$increases, so we need to prune the search space. We use the pruningtechniques described in\cite{deo77:_new_algo_digraph_isomorph,fortin96:_isomorph,reingold77:_combin_algo}that originated in\cite{sussenguth65:_isomorphism,unger64:_isomorphism}.\section{Vertex Invariants}\label{sec:vertex-invariants}One way to reduce the search space is through the use of \emph{vertexinvariants}. The idea is to compute a number for each vertex $i(v)$such that $i(v) = i(v')$ if there exists some isomorphism $f$ where$f(v) = v'$. Then when we look for a match to some vertex $v$, we onlyneed to consider those vertices that have the same vertex invariantnumber. The number of vertices in a graph with the same vertexinvariant number $i$ is called the \emph{invariant multiplicity} for$i$.  In this implementation, by default we use the out-degree of thevertex as the vertex invariant, though the user can also supply thereown invariant function. The ability of the invariant function to prunethe search space varies widely with the type of graph.As a first check to rule out graphs that have no possibility ofmatching, one can create a list of computed vertex invariant numbersfor the vertices in each graph, sort the two lists, and then comparethem.  If the two lists are different then the two graphs are notisomorphic.  If the two lists are the same then the two graphs may beisomorphic.Also, we extend the MATCH operation to use the vertex invariants tohelp rule out vertices.\begin{tabbing}M\=A\=T\=C\=H-INVAR($k$, $v$) $\equiv$ \\\>$out \leftarrow \forall (k,j) \in E_1[k] - E_1[k-1] \Big( (v,f(j)) \in E_2[S \union \{ v \}] - E_2[S] \Land i(v) = i(k) \Big)$ \\\>$in \leftarrow \forall (j,k) \in E_1[k] - E_1[k-1] \Big( (f(j),v) \in E_2[S \union \{ v \}] - E_2[S] \Land i(v) = i(k) \Big)$ \\\>\textbf{return} $out \Land in$ \end{tabbing}\section{Vertex Order}A good choice of the labeling for the vertices (which determines theorder in which the subgraph $G_1[k]$ is grown) can also reduce thesearch space. In the following we discuss two labeling heuristics.\subsection{Most Constrained First}Consider the most constrained vertices first.  That is, examinelower-degree vertices before higher-degree vertices. This reduces thesearch space because it chops off a trunk before the trunk has achance to blossom out. We can generalize this to use vertexinvariants. We examine vertices with low invariant multiplicitybefore examining vertices with high invariant multiplicity.\subsection{Adjacent First}The MATCH operation only considers edges when the other vertex alreadyhas a mapping defined. This means that the MATCH operation can onlyweed out vertices that are adjacent to vertices that have already beenmatched. Therefore, when choosing the next vertex to examine, it isdesirable to choose one that is adjacent a vertex already in $S_1$.\subsection{DFS Order, Starting with Lowest Multiplicity}For this implementation, we combine the above two heuristics in thefollowing way. To implement the ``adjacent first'' heuristic we applyDFS to the graph, and use the DFS discovery order as our vertexorder. To comply with the ``most constrained first'' heuristic weorder the roots of our DFS trees by invariant multiplicity.\section{Implementation}The following is the public interface for the \code{isomorphism}function. The input to the function is the two graphs $G_1$ and $G_2$,mappings from the vertices in the graphs to integers (in the range$[0,|V|)$), and a vertex invariant function object. The output of thefunction is an isomorphism $f$ if there is one. The \code{isomorphism}function returns true if the graphs are isomorphic and falseotherwise. The requirements on type template parameters are describedbelow in the section ``Concept checking''.@d Isomorphism Function Interface@{template <typename Graph1, typename Graph2,           typename IndexMapping,           typename VertexInvariant1, typename VertexInvariant2,          typename IndexMap1, typename IndexMap2>bool isomorphism(const Graph1& g1, const Graph2& g2,                  IndexMapping f,                  VertexInvariant1 invariant1, VertexInvariant2 invariant2,                 IndexMap1 index_map1, IndexMap2 index_map2)@}The main outline of the \code{isomorphism} function is asfollows. Most of the steps in this function are for setting up thevertex ordering, first ordering the vertices by invariant multiplicityand then by DFS order. The last step is the call to the\code{isomorph} function which starts the backtracking search.@d Isomorphism Function Body@{{  @<Some type definitions and iterator declarations@>  @<Concept checking@>  @<Quick return with false if $|V_1| \neq |V_2|$@>  @<Compute vertex invariants@>  @<Quick return if the graph's invariants do not match@>  @<Compute invariant multiplicity@>  @<Sort vertices by invariant multiplicity@>  @<Order the vertices by DFS discover time@>  @<Order the edges by DFS discover time@>  @<Invoke recursive \code{isomorph} function@>}@}There are some types that will be used throughout the function, whichwe create shortened names for here. We will also need vertexiterators for \code{g1} and \code{g2} in several places, so we definethem here.@d Some type definitions and iterator declarations@{typedef typename graph_traits<Graph1>::vertex_descriptor vertex1_t;typedef typename graph_traits<Graph2>::vertex_descriptor vertex2_t;typedef typename graph_traits<Graph1>::vertices_size_type size_type;typename graph_traits<Graph1>::vertex_iterator i1, i1_end;typename graph_traits<Graph2>::vertex_iterator i2, i2_end;@}We use the Boost Concept Checking Library to make sure that the typearguments to the function fulfill there requirements. The\code{Graph1} type must be a \bglconcept{VertexListGraph} and a\bglconcept{EdgeListGraph}. The \code{Graph2} type must be a\bglconcept{VertexListGraph} and a\bglconcept{BidirectionalGraph}. The \code{IndexMapping} type thatrepresents the isomorphism $f$ must be a\pmconcept{ReadWritePropertyMap} that maps from vertices in $G_1$ tovertices in $G_2$. The two other index maps are\pmconcept{ReadablePropertyMap}s from vertices in $G_1$ and $G_2$ tounsigned integers.@d Concept checking@{// Graph requirementsfunction_requires< VertexListGraphConcept<Graph1> >();function_requires< EdgeListGraphConcept<Graph1> >();function_requires< VertexListGraphConcept<Graph2> >();function_requires< BidirectionalGraphConcept<Graph2> >();// Property map requirementsfunction_requires< ReadWritePropertyMapConcept<IndexMapping, vertex1_t> >();typedef typename property_traits<IndexMapping>::value_type IndexMappingValue;BOOST_STATIC_ASSERT((is_same<IndexMappingValue, vertex2_t>::value));function_requires< ReadablePropertyMapConcept<IndexMap1, vertex1_t> >();typedef typename property_traits<IndexMap1>::value_type IndexMap1Value;BOOST_STATIC_ASSERT((is_convertible<IndexMap1Value, size_type>::value));function_requires< ReadablePropertyMapConcept<IndexMap2, vertex2_t> >();typedef typename property_traits<IndexMap2>::value_type IndexMap2Value;BOOST_STATIC_ASSERT((is_convertible<IndexMap2Value, size_type>::value));@}\noindent If there are no vertices in either graph, then they are triviallyisomorphic.@d Quick return with false if $|V_1| \neq |V_2|$@{if (num_vertices(g1) != num_vertices(g2))  return false;@}\subsection{Ordering by Vertex Invariant Multiplicity}The user can supply the vertex invariant functions as a\stlconcept{AdaptableUnaryFunction} (with the addition of the\code{max} function) in the \code{invariant1} and \code{invariant2}parameters. We also define a default which uses the out-degree andin-degree of a vertex. The following is the definition of the functionobject for the default vertex invariant. User-defined vertex invariantfunction objects should follow the same pattern.@d Degree vertex invariant@{template <typename InDegreeMap, typename Graph>class degree_vertex_invariant{public:  typedef typename graph_traits<Graph>::vertex_descriptor argument_type;  typedef typename graph_traits<Graph>::degree_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) { }  result_type operator()(argument_type 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  result_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;};@}Since the invariant function may be expensive to compute, wepre-compute the invariant numbers for every vertex in the twographs. The variables \code{invar1} and \code{invar2} are propertymaps for accessing the stored invariants, which are described next.@d Compute vertex invariants@{@<Setup storage for vertex invariants@>for (tie(i1, i1_end) = vertices(g1); i1 != i1_end; ++i1)  invar1[*i1] = invariant1(*i1);for (tie(i2, i2_end) = vertices(g2); i2 != i2_end; ++i2)  invar2[*i2] = invariant2(*i2);@}\noindent We store the invariants in two vectors, indexed by the vertex indicesof the two graphs. We then create property maps for accessing thesetwo vectors in a more convenient fashion (they go directly from vertexto invariant, instead of vertex to index to invariant).@d Setup storage for vertex invariants@{typedef typename VertexInvariant1::result_type InvarValue1;typedef typename VertexInvariant2::result_type InvarValue2;typedef std::vector<InvarValue1> invar_vec1_t;typedef std::vector<InvarValue2> invar_vec2_t;invar_vec1_t invar1_vec(num_vertices(g1));invar_vec2_t invar2_vec(num_vertices(g2));typedef typename invar_vec1_t::iterator vec1_iter;typedef typename invar_vec2_t::iterator vec2_iter;iterator_property_map<vec1_iter, IndexMap1, InvarValue1, InvarValue1&>  invar1(invar1_vec.begin(), index_map1);iterator_property_map<vec2_iter, IndexMap2, InvarValue2, InvarValue2&>  invar2(invar2_vec.begin(), index_map2);@}

⌨️ 快捷键说明

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