[Lemon-commits] Alpar Juttner: Merge #597
Lemon HG
hg at lemon.cs.elte.hu
Wed Oct 17 23:21:03 CEST 2018
details: http://lemon.cs.elte.hu/hg/lemon/rev/959d78f3fe0e
changeset: 1415:959d78f3fe0e
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Wed Oct 17 22:56:43 2018 +0200
description:
Merge #597
diffstat:
doc/references.bib | 11 +
lemon/bits/vf2_internals.h | 47 ++
lemon/vf2.h | 675 +++++++++++++++++-----------------
lemon/vf2pp.h | 860 +++++++++++++++++++++++++++++++++++++++++++++
test/vf2_test.cc | 339 +++++++++++------
5 files changed, 1470 insertions(+), 462 deletions(-)
diffs (truncated from 2322 to 300 lines):
diff --git a/doc/references.bib b/doc/references.bib
--- a/doc/references.bib
+++ b/doc/references.bib
@@ -43,6 +43,17 @@
pages = {67--118}
}
+ at article{VF2PP,
+ author = {Alp\'ar J\"uttner and P\'eter Madarasi},
+ title = {{VF2++} — An improved subgraph isomorphism algorithm},
+ journal = {Discrete Applied Mathematics},
+ year = {2018},
+ volume = {242},
+ pages = {69--81},
+ url = {https://doi.org/10.1016/j.dam.2018.02.018}
+}
+
+
%%%%% Other libraries %%%%%%
diff --git a/lemon/bits/vf2_internals.h b/lemon/bits/vf2_internals.h
new file mode 100644
--- /dev/null
+++ b/lemon/bits/vf2_internals.h
@@ -0,0 +1,47 @@
+/* -*- mode: C++; indent-tabs-mode: nil; -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library.
+ *
+ * Copyright (C) 2015-2017
+ * EMAXA Kutato-fejleszto Kft. (EMAXA Research Ltd.)
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#ifndef VF2_INTERNALS_H
+#define VF2_INTERNALS_H
+
+
+///\ingroup graph_properties
+///\file
+///\brief Mapping types for graph matching algorithms.
+
+namespace lemon {
+ ///\ingroup graph_isomorphism
+ ///The \ref Vf2 "VF2" algorithm is capable of finding different kind of
+ ///graph embeddings, this enum specifies their types.
+ ///
+ ///See \ref graph_isomorphism for a more detailed description.
+ enum MappingType {
+ /// Subgraph isomorphism
+ SUBGRAPH = 0,
+ /// Induced subgraph isomorphism
+ INDUCED = 1,
+ /// Graph isomorphism
+ ///
+ /// If the two graphs have the same number of nodes, than it is
+ /// equivalent to \ref INDUCED, and if they also have the same
+ /// number of edges, then it is also equivalent to \ref SUBGRAPH.
+ ///
+ /// However, using this setting is faster than the other two options.
+ ISOMORPH = 2
+ };
+}
+#endif
diff --git a/lemon/vf2.h b/lemon/vf2.h
--- a/lemon/vf2.h
+++ b/lemon/vf2.h
@@ -2,7 +2,7 @@
*
* This file is a part of LEMON, a generic C++ optimization library.
*
- * Copyright (C) 2015
+ * Copyright (C) 2015-2017
* EMAXA Kutato-fejleszto Kft. (EMAXA Research Ltd.)
*
* Permission to use, modify and distribute this software is granted
@@ -24,97 +24,50 @@
#include <lemon/core.h>
#include <lemon/concepts/graph.h>
-#include <lemon/dfs.h>
#include <lemon/bfs.h>
+#include <lemon/bits/vf2_internals.h>
#include <vector>
-#include <set>
-namespace lemon
-{
- namespace bits
- {
- namespace vf2
- {
- class AlwaysEq
- {
+namespace lemon {
+ namespace bits {
+ namespace vf2 {
+
+ class AlwaysEq {
public:
template<class T1, class T2>
- bool operator()(T1, T2) const
- {
+ bool operator()(T1&, T2&) const {
return true;
}
};
template<class M1, class M2>
- class MapEq
- {
+ class MapEq{
const M1 &_m1;
const M2 &_m2;
public:
- MapEq(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
- bool operator()(typename M1::Key k1, typename M2::Key k2) const
- {
+ MapEq(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) { }
+ bool operator()(typename M1::Key k1, typename M2::Key k2) const {
return _m1[k1] == _m2[k2];
}
};
template <class G>
- class DfsLeaveOrder : public DfsVisitor<G>
- {
- const G &_g;
- std::vector<typename G::Node> &_order;
- int i;
- public:
- DfsLeaveOrder(const G &g, std::vector<typename G::Node> &order)
- : i(countNodes(g)), _g(g), _order(order)
- {}
- void leave(const typename G::Node &node)
- {
- _order[--i]=node;
- }
- };
-
- template <class G>
- class BfsLeaveOrder : public BfsVisitor<G>
- {
+ class BfsLeaveOrder : public BfsVisitor<G> {
int i;
const G &_g;
std::vector<typename G::Node> &_order;
public:
BfsLeaveOrder(const G &g, std::vector<typename G::Node> &order)
- : i(0), _g(g), _order(order)
- {}
- void process(const typename G::Node &node)
- {
+ : i(0), _g(g), _order(order){
+ }
+ void process(const typename G::Node &node) {
_order[i++]=node;
}
};
}
}
- ///Graph mapping types.
-
- ///\ingroup graph_isomorphism
- ///The \ref Vf2 "VF2" algorithm is capable of finding different kind of
- ///embeddings, this enum specifies its type.
- ///
- ///See \ref graph_isomorphism for a more detailed description.
- enum Vf2MappingType {
- /// Subgraph isomorphism
- SUBGRAPH = 0,
- /// Induced subgraph isomorphism
- INDUCED = 1,
- /// Graph isomorphism
-
- /// If the two graph has the same number of nodes, than it is
- /// equivalent to \ref INDUCED, and if they also have the same
- /// number of edges, then it is also equivalent to \ref SUBGRAPH.
- ///
- /// However, using this setting is faster than the other two
- /// options.
- ISOMORPH = 2
- };
///%VF2 algorithm class.
@@ -124,271 +77,252 @@
///
///There is also a \ref vf2() "function-type interface" called \ref vf2()
///for the %VF2 algorithm, which is probably more convenient in most
- ///use-cases.
+ ///use cases.
///
///\tparam G1 The type of the graph to be embedded.
- ///The default type is \ref ListDigraph.
+ ///The default type is \ref ListGraph.
///\tparam G2 The type of the graph g1 will be embedded into.
- ///The default type is \ref ListDigraph.
+ ///The default type is \ref ListGraph.
///\tparam M The type of the NodeMap storing the mapping.
///By default, it is G1::NodeMap<G2::Node>
///\tparam NEQ A bool-valued binary functor determinining whether a node is
- ///mappable to another. By default it is an always true operator.
+ ///mappable to another. By default, it is an always-true operator.
///
///\sa vf2()
#ifdef DOXYGEN
template<class G1, class G2, class M, class NEQ >
#else
- template<class G1=ListDigraph,
- class G2=ListDigraph,
+ template<class G1 = ListGraph,
+ class G2 = ListGraph,
class M = typename G1::template NodeMap<G2::Node>,
class NEQ = bits::vf2::AlwaysEq >
#endif
- class Vf2
- {
- //Current depth in the DFS tree.
- int _depth;
+ class Vf2 {
+ //The graph to be embedded
+ const G1 &_g1;
+
+ //The graph into which g1 is to be embedded
+ const G2 &_g2;
+
//Functor with bool operator()(G1::Node,G2::Node), which returns 1
- //if and only if the 2 nodes are equivalent.
+ //if and only if the two nodes are equivalent
NEQ _nEq;
+ //Current depth in the search tree
+ int _depth;
+
+ //The current mapping. _mapping[v1]=v2 iff v1 has been mapped to v2,
+ //where v1 is a node of G1 and v2 is a node of G2
+ M &_mapping;
+
+ //_order[i] is the node of g1 for which a pair is searched if depth=i
+ std::vector<typename G1::Node> _order;
+
+ //_conn[v2] = number of covered neighbours of v2
typename G2::template NodeMap<int> _conn;
- //Current mapping. We index it by the nodes of g1, and match[v] is
- //a node of g2.
- M &_mapping;
- //order[i] is the node of g1, for which we find a pair if depth=i
- std::vector<typename G1::Node> order;
- //currEdgeIts[i] is an edge iterator, witch is last used in the ith
- //depth to find a pair for order[i].
- std::vector<typename G2::IncEdgeIt> currEdgeIts;
- //The small graph.
- const G1 &_g1;
- //The big graph.
- const G2 &_g2;
- //lookup tables for cut the searchtree
- typename G1::template NodeMap<int> rNew1t,rInOut1t;
- Vf2MappingType _mapping_type;
+ //_currEdgeIts[i] is the last used edge iterator in the i-th
+ //depth to find a pair for node _order[i]
+ std::vector<typename G2::IncEdgeIt> _currEdgeIts;
+
+ //lookup tables for cutting the searchtree
+ typename G1::template NodeMap<int> _rNew1t, _rInOut1t;
+
+ MappingType _mapping_type;
+
+ bool _deallocMappingAfterUse;
//cut the search tree
- template<Vf2MappingType MT>
- bool cut(const typename G1::Node n1,const typename G2::Node n2) const
- {
+ template<MappingType MT>
+ bool cut(const typename G1::Node n1,const typename G2::Node n2) const {
int rNew2=0,rInOut2=0;
- for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
- {
- const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
- if(_conn[currNode]>0)
- ++rInOut2;
- else if(MT!=SUBGRAPH&&_conn[currNode]==0)
- ++rNew2;
- }
- switch(MT)
- {
- case INDUCED:
More information about the Lemon-commits
mailing list