madarasip@1350: /* -*- mode: C++; indent-tabs-mode: nil; -*- madarasip@1350: * madarasip@1350: * This file is a part of LEMON, a generic C++ optimization library. madarasip@1350: * madarasip@1350: * Copyright (C) 2015 madarasip@1350: * EMAXA Kutato-fejleszto Kft. (EMAXA Research Ltd.) madarasip@1350: * madarasip@1350: * Permission to use, modify and distribute this software is granted madarasip@1350: * provided that this copyright notice appears in all copies. For madarasip@1350: * precise terms see the accompanying LICENSE file. madarasip@1350: * madarasip@1350: * This software is provided "AS IS" with no warranty of any kind, madarasip@1350: * express or implied, and with no claim as to its suitability for any madarasip@1350: * purpose. madarasip@1350: * madarasip@1350: */ madarasip@1350: madarasip@1350: #ifndef LEMON_VF2_H madarasip@1350: #define LEMON_VF2_H madarasip@1350: alpar@1351: ///\ingroup graph_properties alpar@1351: ///\file alpar@1351: ///\brief VF2 algorithm \cite cordella2004sub. alpar@1351: madarasip@1350: #include madarasip@1350: #include madarasip@1350: #include madarasip@1350: #include madarasip@1350: madarasip@1350: #include madarasip@1350: #include madarasip@1350: madarasip@1350: namespace lemon madarasip@1350: { madarasip@1350: namespace bits madarasip@1350: { madarasip@1350: namespace vf2 madarasip@1350: { madarasip@1350: class AlwaysEq madarasip@1350: { madarasip@1350: public: madarasip@1350: template madarasip@1350: bool operator()(T1, T2) const madarasip@1350: { madarasip@1350: return true; madarasip@1350: } madarasip@1350: }; madarasip@1350: madarasip@1350: template madarasip@1350: class MapEq madarasip@1350: { madarasip@1350: const M1 &_m1; madarasip@1350: const M2 &_m2; madarasip@1350: public: madarasip@1350: MapEq(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} madarasip@1350: bool operator()(typename M1::Key k1, typename M2::Key k2) const madarasip@1350: { madarasip@1350: return _m1[k1] == _m2[k2]; madarasip@1350: } madarasip@1350: }; madarasip@1350: madarasip@1350: template madarasip@1350: class DfsLeaveOrder : public DfsVisitor madarasip@1350: { madarasip@1350: const G &_g; madarasip@1350: std::vector &_order; madarasip@1350: int i; madarasip@1350: public: madarasip@1350: DfsLeaveOrder(const G &g, std::vector &order) madarasip@1350: : i(countNodes(g)), _g(g), _order(order) madarasip@1350: {} madarasip@1350: void leave(const typename G::Node &node) madarasip@1350: { madarasip@1350: _order[--i]=node; madarasip@1350: } madarasip@1350: }; madarasip@1350: madarasip@1350: template madarasip@1350: class BfsLeaveOrder : public BfsVisitor madarasip@1350: { madarasip@1350: int i; madarasip@1350: const G &_g; madarasip@1350: std::vector &_order; madarasip@1350: public: madarasip@1350: BfsLeaveOrder(const G &g, std::vector &order) madarasip@1350: : i(0), _g(g), _order(order) madarasip@1350: {} madarasip@1350: void process(const typename G::Node &node) madarasip@1350: { madarasip@1350: _order[i++]=node; madarasip@1350: } madarasip@1350: }; madarasip@1350: } madarasip@1350: } madarasip@1350: alpar@1351: ///Graph mapping types. alpar@1351: alpar@1351: ///\ingroup graph_isomorphism alpar@1351: ///The \ref Vf2 "VF2" algorithm is capable of finding different kind of alpar@1351: ///embeddings, this enum specifies its type. alpar@1351: /// alpar@1351: ///See \ref graph_isomorphism for a more detailed description. alpar@1351: enum Vf2MappingType { alpar@1351: /// Subgraph isomorphism madarasip@1350: SUBGRAPH = 0, alpar@1351: /// Induced subgraph isomorphism madarasip@1350: INDUCED = 1, alpar@1351: /// Graph isomorphism alpar@1351: alpar@1351: /// If the two graph has the same number of nodes, than it is alpar@1351: /// equivalent to \ref INDUCED, and if they also have the same alpar@1351: /// number of edges, then it is also equivalent to \ref SUBGRAPH. alpar@1351: /// alpar@1351: /// However, using this setting is faster than the other two alpar@1351: /// options. madarasip@1350: ISOMORPH = 2 madarasip@1350: }; madarasip@1350: alpar@1351: ///%VF2 algorithm class. alpar@1351: alpar@1351: ///\ingroup graph_isomorphism This class provides an efficient alpar@1351: ///implementation of the %VF2 algorithm \cite cordella2004sub alpar@1351: ///for variants of the (Sub)graph Isomorphism problem. alpar@1351: /// alpar@1351: ///There is also a \ref vf2() "function-type interface" called \ref vf2() alpar@1351: ///for the %VF2 algorithm, which is probably more convenient in most alpar@1351: ///use-cases. alpar@1351: /// alpar@1351: ///\tparam G1 The type of the graph to be embedded. alpar@1351: ///The default type is \ref ListDigraph. alpar@1351: ///\tparam G2 The type of the graph g1 will be embedded into. alpar@1351: ///The default type is \ref ListDigraph. alpar@1351: ///\tparam M The type of the NodeMap storing the mapping. alpar@1351: ///By default, it is G1::NodeMap alpar@1351: ///\tparam NEQ A bool-valued binary functor determinining whether a node is alpar@1351: ///mappable to another. By default it is an always true operator. alpar@1351: /// alpar@1351: ///\sa vf2() alpar@1351: #ifdef DOXYGEN alpar@1351: template alpar@1351: #else alpar@1351: template, alpar@1351: class NEQ = bits::vf2::AlwaysEq > alpar@1351: #endif madarasip@1350: class Vf2 madarasip@1350: { madarasip@1350: //Current depth in the DFS tree. madarasip@1350: int _depth; madarasip@1350: //Functor with bool operator()(G1::Node,G2::Node), which returns 1 madarasip@1350: //if and only if the 2 nodes are equivalent. alpar@1351: NEQ _nEq; madarasip@1350: madarasip@1350: typename G2::template NodeMap _conn; alpar@1351: //Current mapping. We index it by the nodes of g1, and match[v] is madarasip@1350: //a node of g2. alpar@1351: M &_mapping; madarasip@1350: //order[i] is the node of g1, for which we find a pair if depth=i madarasip@1350: std::vector order; madarasip@1350: //currEdgeIts[i] is an edge iterator, witch is last used in the ith madarasip@1350: //depth to find a pair for order[i]. madarasip@1350: std::vector currEdgeIts; madarasip@1350: //The small graph. madarasip@1350: const G1 &_g1; madarasip@1350: //The big graph. madarasip@1350: const G2 &_g2; madarasip@1350: //lookup tables for cut the searchtree madarasip@1350: typename G1::template NodeMap rNew1t,rInOut1t; madarasip@1350: alpar@1351: Vf2MappingType _mapping_type; madarasip@1350: madarasip@1350: //cut the search tree alpar@1351: template madarasip@1350: bool cut(const typename G1::Node n1,const typename G2::Node n2) const madarasip@1350: { madarasip@1350: int rNew2=0,rInOut2=0; madarasip@1350: for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) madarasip@1350: { madarasip@1350: const typename G2::Node currNode=_g2.oppositeNode(n2,e2); madarasip@1350: if(_conn[currNode]>0) madarasip@1350: ++rInOut2; madarasip@1350: else if(MT!=SUBGRAPH&&_conn[currNode]==0) madarasip@1350: ++rNew2; madarasip@1350: } madarasip@1350: switch(MT) madarasip@1350: { madarasip@1350: case INDUCED: madarasip@1350: return rInOut1t[n1]<=rInOut2&&rNew1t[n1]<=rNew2; madarasip@1350: case SUBGRAPH: madarasip@1350: return rInOut1t[n1]<=rInOut2; madarasip@1350: case ISOMORPH: madarasip@1350: return rInOut1t[n1]==rInOut2&&rNew1t[n1]==rNew2; madarasip@1350: default: madarasip@1350: return false; madarasip@1350: } madarasip@1350: } madarasip@1350: alpar@1351: template madarasip@1350: bool feas(const typename G1::Node n1,const typename G2::Node n2) madarasip@1350: { madarasip@1350: if(!_nEq(n1,n2)) madarasip@1350: return 0; madarasip@1350: madarasip@1350: for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1) madarasip@1350: { madarasip@1350: const typename G1::Node currNode=_g1.oppositeNode(n1,e1); alpar@1351: if(_mapping[currNode]!=INVALID) alpar@1351: --_conn[_mapping[currNode]]; madarasip@1350: } madarasip@1350: bool isIso=1; madarasip@1350: for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) madarasip@1350: { madarasip@1350: const typename G2::Node currNode=_g2.oppositeNode(n2,e2); madarasip@1350: if(_conn[currNode]<-1) madarasip@1350: ++_conn[currNode]; madarasip@1350: else if(MT!=SUBGRAPH&&_conn[currNode]==-1) madarasip@1350: { madarasip@1350: isIso=0; madarasip@1350: break; madarasip@1350: } madarasip@1350: } madarasip@1350: madarasip@1350: for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1) madarasip@1350: { madarasip@1350: const typename G1::Node currNode=_g1.oppositeNode(n1,e1); alpar@1351: if(_mapping[currNode]!=INVALID&&_conn[_mapping[currNode]]!=-1) madarasip@1350: { madarasip@1350: switch(MT) madarasip@1350: { madarasip@1350: case INDUCED: madarasip@1350: case ISOMORPH: madarasip@1350: isIso=0; madarasip@1350: break; madarasip@1350: case SUBGRAPH: alpar@1351: if(_conn[_mapping[currNode]]<-1) madarasip@1350: isIso=0; madarasip@1350: break; madarasip@1350: } alpar@1351: _conn[_mapping[currNode]]=-1; madarasip@1350: } madarasip@1350: } madarasip@1350: return isIso&&cut(n1,n2); madarasip@1350: } madarasip@1350: madarasip@1350: void addPair(const typename G1::Node n1,const typename G2::Node n2) madarasip@1350: { madarasip@1350: _conn[n2]=-1; alpar@1351: _mapping.set(n1,n2); madarasip@1350: for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) madarasip@1350: if(_conn[_g2.oppositeNode(n2,e2)]!=-1) madarasip@1350: ++_conn[_g2.oppositeNode(n2,e2)]; madarasip@1350: } madarasip@1350: madarasip@1350: void subPair(const typename G1::Node n1,const typename G2::Node n2) madarasip@1350: { madarasip@1350: _conn[n2]=0; alpar@1351: _mapping.set(n1,INVALID); madarasip@1350: for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) madarasip@1350: { madarasip@1350: const typename G2::Node currNode=_g2.oppositeNode(n2,e2); madarasip@1350: if(_conn[currNode]>0) madarasip@1350: --_conn[currNode]; madarasip@1350: else if(_conn[currNode]==-1) madarasip@1350: ++_conn[n2]; madarasip@1350: } madarasip@1350: } madarasip@1350: madarasip@1350: void setOrder()//we will find pairs for the nodes of g1 in this order madarasip@1350: { madarasip@1350: // bits::vf2::DfsLeaveOrder v(_g1,order); madarasip@1350: // DfsVisit >dfs(_g1, v); madarasip@1350: // dfs.run(); madarasip@1350: madarasip@1350: //it is more efficient in practice than DFS madarasip@1350: bits::vf2::BfsLeaveOrder v(_g1,order); madarasip@1350: BfsVisit >bfs(_g1, v); madarasip@1350: bfs.run(); madarasip@1350: } madarasip@1350: alpar@1351: template madarasip@1350: bool extMatch() madarasip@1350: { madarasip@1350: while(_depth>=0) madarasip@1350: { madarasip@1350: //there are not nodes in g1, which has not pair in g2. madarasip@1350: if(_depth==static_cast(order.size())) madarasip@1350: { madarasip@1350: --_depth; madarasip@1350: return true; madarasip@1350: } madarasip@1350: //the node of g2, which neighbours are the candidates for madarasip@1350: //the pair of order[_depth] madarasip@1350: typename G2::Node currPNode; madarasip@1350: if(currEdgeIts[_depth]==INVALID) madarasip@1350: { madarasip@1350: typename G1::IncEdgeIt fstMatchedE(_g1,order[_depth]); alpar@1351: //if _mapping[order[_depth]]!=INVALID, we dont use madarasip@1350: //fstMatchedE alpar@1351: if(_mapping[order[_depth]]==INVALID) madarasip@1350: for(; fstMatchedE!=INVALID && alpar@1351: _mapping[_g1.oppositeNode(order[_depth], madarasip@1350: fstMatchedE)]==INVALID; madarasip@1350: ++fstMatchedE) ; //find fstMatchedE alpar@1351: if(fstMatchedE==INVALID||_mapping[order[_depth]]!=INVALID) madarasip@1350: { madarasip@1350: //We did not find an covered neighbour, this means madarasip@1350: //the graph is not connected(or _depth==0). Every madarasip@1350: //uncovered(and there are some other properties due madarasip@1350: //to the spec. problem types) node of g2 is madarasip@1350: //candidate. We can read the iterator of the last madarasip@1350: //tryed node from the match if it is not the first madarasip@1350: //try(match[order[_depth]]!=INVALID) madarasip@1350: typename G2::NodeIt n2(_g2); madarasip@1350: //if its not the first try alpar@1351: if(_mapping[order[_depth]]!=INVALID) madarasip@1350: { alpar@1351: n2=++typename G2::NodeIt(_g2,_mapping[order[_depth]]); alpar@1351: subPair(order[_depth],_mapping[order[_depth]]); madarasip@1350: } madarasip@1350: for(; n2!=INVALID; ++n2) madarasip@1350: if(MT!=SUBGRAPH&&_conn[n2]==0) madarasip@1350: { madarasip@1350: if(feas(order[_depth],n2)) madarasip@1350: break; madarasip@1350: } madarasip@1350: else if(MT==SUBGRAPH&&_conn[n2]>=0) madarasip@1350: if(feas(order[_depth],n2)) madarasip@1350: break; madarasip@1350: // n2 is the next candidate madarasip@1350: if(n2!=INVALID) madarasip@1350: { madarasip@1350: addPair(order[_depth],n2); madarasip@1350: ++_depth; madarasip@1350: } madarasip@1350: else // there is no more candidate madarasip@1350: --_depth; madarasip@1350: continue; madarasip@1350: } madarasip@1350: else madarasip@1350: { alpar@1351: currPNode=_mapping[_g1.oppositeNode(order[_depth], alpar@1351: fstMatchedE)]; madarasip@1350: currEdgeIts[_depth]=typename G2::IncEdgeIt(_g2,currPNode); madarasip@1350: } madarasip@1350: } madarasip@1350: else madarasip@1350: { alpar@1351: currPNode=_g2.oppositeNode(_mapping[order[_depth]], madarasip@1350: currEdgeIts[_depth]); alpar@1351: subPair(order[_depth],_mapping[order[_depth]]); madarasip@1350: ++currEdgeIts[_depth]; madarasip@1350: } madarasip@1350: for(; currEdgeIts[_depth]!=INVALID; ++currEdgeIts[_depth]) madarasip@1350: { madarasip@1350: const typename G2::Node currNode = madarasip@1350: _g2.oppositeNode(currPNode, currEdgeIts[_depth]); madarasip@1350: //if currNode is uncovered madarasip@1350: if(_conn[currNode]>0&&feas(order[_depth],currNode)) madarasip@1350: { madarasip@1350: addPair(order[_depth],currNode); madarasip@1350: break; madarasip@1350: } madarasip@1350: } madarasip@1350: currEdgeIts[_depth]==INVALID?--_depth:++_depth; madarasip@1350: } madarasip@1350: return false; madarasip@1350: } madarasip@1350: madarasip@1350: //calc. the lookup table for cut the searchtree madarasip@1350: void setRNew1tRInOut1t() madarasip@1350: { madarasip@1350: typename G1::template NodeMap tmp(_g1,0); madarasip@1350: for(unsigned int i=0; i0) madarasip@1350: ++rInOut1t[order[i]]; madarasip@1350: else if(tmp[currNode]==0) madarasip@1350: ++rNew1t[order[i]]; madarasip@1350: } madarasip@1350: for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1) madarasip@1350: { madarasip@1350: const typename G1::Node currNode=_g1.oppositeNode(order[i],e1); madarasip@1350: if(tmp[currNode]!=-1) madarasip@1350: ++tmp[currNode]; madarasip@1350: } madarasip@1350: } madarasip@1350: } madarasip@1350: public: alpar@1351: ///Constructor alpar@1351: alpar@1351: ///Constructor alpar@1351: alpar@1351: ///\param g1 The graph to be embedded into \e g2. alpar@1351: ///\param g2 The graph \e g1 will be embedded into. alpar@1351: ///\param m \ref concepts::ReadWriteMap "read-write" NodeMap alpar@1351: ///storing the found mapping. alpar@1351: ///\param neq A bool-valued binary functor determinining whether a node is alpar@1351: ///mappable to another. By default it is an always true operator. alpar@1351: Vf2(const G1 &g1, const G2 &g2,M &m, const NEQ &neq = NEQ() ) : alpar@1351: _nEq(neq), _conn(g2,0), _mapping(m), order(countNodes(g1)), madarasip@1350: currEdgeIts(countNodes(g1),INVALID), _g1(g1), _g2(g2), rNew1t(g1,0), madarasip@1350: rInOut1t(g1,0), _mapping_type(SUBGRAPH) madarasip@1350: { madarasip@1350: _depth=0; madarasip@1350: setOrder(); madarasip@1350: setRNew1tRInOut1t(); alpar@1352: for(typename G1::NodeIt n(g1);n!=INVALID;++n) alpar@1352: m[n]=INVALID; madarasip@1350: } madarasip@1350: alpar@1351: ///Returns the current mapping type madarasip@1350: alpar@1351: ///Returns the current mapping type alpar@1351: /// alpar@1351: Vf2MappingType mappingType() const { return _mapping_type; } alpar@1351: ///Sets mapping type alpar@1351: alpar@1351: ///Sets mapping type. alpar@1351: /// alpar@1351: ///The mapping type is set to \ref SUBGRAPH by default. alpar@1351: /// alpar@1353: ///\sa See \ref Vf2MappingType for the possible values. alpar@1351: void mappingType(Vf2MappingType m_type) { _mapping_type = m_type; } alpar@1351: kpeter@1366: ///Finds a mapping alpar@1351: alpar@1351: ///It finds a mapping between from g1 into g2 according to the mapping alpar@1351: ///type set by \ref mappingType(Vf2MappingType) "mappingType()". alpar@1351: /// alpar@1351: ///By subsequent calls, it returns all possible mappings one-by-one. alpar@1351: /// alpar@1351: ///\retval true if a mapping is found. alpar@1351: ///\retval false if there is no (more) mapping. madarasip@1350: bool find() madarasip@1350: { madarasip@1350: switch(_mapping_type) madarasip@1350: { madarasip@1350: case SUBGRAPH: madarasip@1350: return extMatch(); madarasip@1350: case INDUCED: madarasip@1350: return extMatch(); madarasip@1350: case ISOMORPH: madarasip@1350: return extMatch(); madarasip@1350: default: madarasip@1350: return false; madarasip@1350: } madarasip@1350: } madarasip@1350: }; madarasip@1350: madarasip@1350: template madarasip@1350: class Vf2WizardBase madarasip@1350: { madarasip@1350: protected: madarasip@1350: typedef G1 Graph1; madarasip@1350: typedef G2 Graph2; madarasip@1350: madarasip@1350: const G1 &_g1; madarasip@1350: const G2 &_g2; madarasip@1350: alpar@1351: Vf2MappingType _mapping_type; madarasip@1350: madarasip@1350: typedef typename G1::template NodeMap Mapping; madarasip@1350: bool _local_mapping; madarasip@1350: void *_mapping; madarasip@1350: void createMapping() madarasip@1350: { madarasip@1350: _mapping = new Mapping(_g1); madarasip@1350: } madarasip@1350: madarasip@1350: typedef bits::vf2::AlwaysEq NodeEq; madarasip@1350: NodeEq _node_eq; madarasip@1350: madarasip@1350: Vf2WizardBase(const G1 &g1,const G2 &g2) madarasip@1350: : _g1(g1), _g2(g2), _mapping_type(SUBGRAPH), _local_mapping(true) {} madarasip@1350: }; madarasip@1350: alpar@1351: /// Auxiliary class for the function-type interface of %VF2 algorithm. alpar@1351: alpar@1351: /// This auxiliary class implements the named parameters of alpar@1351: /// \ref vf2() "function-type interface" of \ref Vf2 algorithm. alpar@1351: /// alpar@1351: /// \warning This class should only be used through the function \ref vf2(). alpar@1351: /// alpar@1351: /// \tparam TR The traits class that defines various types used by the alpar@1351: /// algorithm. madarasip@1350: template madarasip@1350: class Vf2Wizard : public TR madarasip@1350: { madarasip@1350: typedef TR Base; madarasip@1350: typedef typename TR::Graph1 Graph1; madarasip@1350: typedef typename TR::Graph2 Graph2; madarasip@1350: madarasip@1350: typedef typename TR::Mapping Mapping; madarasip@1350: typedef typename TR::NodeEq NodeEq; madarasip@1350: madarasip@1350: using TR::_g1; madarasip@1350: using TR::_g2; madarasip@1350: using TR::_mapping_type; madarasip@1350: using TR::_mapping; madarasip@1350: using TR::_node_eq; madarasip@1350: madarasip@1350: public: alpar@1351: ///Constructor madarasip@1350: Vf2Wizard(const Graph1 &g1,const Graph2 &g2) : Base(g1,g2) {} madarasip@1350: madarasip@1350: ///Copy constructor madarasip@1350: Vf2Wizard(const Base &b) : Base(b) {} madarasip@1350: madarasip@1350: madarasip@1350: template madarasip@1350: struct SetMappingBase : public Base { madarasip@1350: typedef T Mapping; madarasip@1350: SetMappingBase(const Base &b) : Base(b) {} madarasip@1350: }; madarasip@1350: madarasip@1350: ///\brief \ref named-templ-param "Named parameter" for setting madarasip@1350: ///the mapping. madarasip@1350: /// madarasip@1350: ///\ref named-templ-param "Named parameter" function for setting madarasip@1350: ///the map that stores the found embedding. madarasip@1350: template madarasip@1350: Vf2Wizard< SetMappingBase > mapping(const T &t) madarasip@1350: { madarasip@1350: Base::_mapping=reinterpret_cast(const_cast(&t)); madarasip@1350: Base::_local_mapping = false; madarasip@1350: return Vf2Wizard >(*this); madarasip@1350: } madarasip@1350: madarasip@1350: template madarasip@1350: struct SetNodeEqBase : public Base { madarasip@1350: typedef NE NodeEq; madarasip@1350: NodeEq _node_eq; madarasip@1350: SetNodeEqBase(const Base &b, const NE &node_eq) madarasip@1350: : Base(b), _node_eq(node_eq) {} madarasip@1350: }; madarasip@1350: madarasip@1350: ///\brief \ref named-templ-param "Named parameter" for setting madarasip@1350: ///the node equivalence relation. madarasip@1350: /// madarasip@1350: ///\ref named-templ-param "Named parameter" function for setting madarasip@1350: ///the equivalence relation between the nodes. alpar@1351: /// alpar@1351: ///\param node_eq A bool-valued binary functor determinining alpar@1351: ///whether a node is mappable to another. By default it is an alpar@1351: ///always true operator. madarasip@1350: template madarasip@1350: Vf2Wizard< SetNodeEqBase > nodeEq(const T &node_eq) madarasip@1350: { madarasip@1350: return Vf2Wizard >(SetNodeEqBase(*this,node_eq)); madarasip@1350: } madarasip@1350: madarasip@1350: ///\brief \ref named-templ-param "Named parameter" for setting madarasip@1350: ///the node labels. madarasip@1350: /// madarasip@1350: ///\ref named-templ-param "Named parameter" function for setting madarasip@1350: ///the node labels defining equivalence relation between them. alpar@1351: /// alpar@1353: ///\param m1 It is arbitrary \ref concepts::ReadMap "readable node map" alpar@1353: ///of g1. alpar@1353: ///\param m2 It is arbitrary \ref concepts::ReadMap "readable node map" alpar@1353: ///of g2. alpar@1351: /// alpar@1351: ///The value type of these maps must be equal comparable. madarasip@1350: template madarasip@1350: Vf2Wizard< SetNodeEqBase > > madarasip@1350: nodeLabels(const M1 &m1,const M2 &m2) madarasip@1350: { madarasip@1350: return nodeEq(bits::vf2::MapEq(m1,m2)); madarasip@1350: } madarasip@1350: alpar@1351: ///\brief \ref named-templ-param "Named parameter" for setting alpar@1351: ///the mapping type. alpar@1351: /// alpar@1351: ///\ref named-templ-param "Named parameter" for setting alpar@1351: ///the mapping type. alpar@1351: /// alpar@1351: ///The mapping type is set to \ref SUBGRAPH by default. alpar@1351: /// alpar@1353: ///\sa See \ref Vf2MappingType for the possible values. alpar@1351: Vf2Wizard &mappingType(Vf2MappingType m_type) madarasip@1350: { madarasip@1350: _mapping_type = m_type; madarasip@1350: return *this; madarasip@1350: } madarasip@1350: alpar@1351: ///\brief \ref named-templ-param "Named parameter" for setting alpar@1351: ///the mapping type to \ref INDUCED. alpar@1351: /// alpar@1351: ///\ref named-templ-param "Named parameter" for setting alpar@1351: ///the mapping type to \ref INDUCED. madarasip@1350: Vf2Wizard &induced() madarasip@1350: { madarasip@1350: _mapping_type = INDUCED; madarasip@1350: return *this; madarasip@1350: } madarasip@1350: alpar@1351: ///\brief \ref named-templ-param "Named parameter" for setting alpar@1351: ///the mapping type to \ref ISOMORPH. alpar@1351: /// alpar@1351: ///\ref named-templ-param "Named parameter" for setting alpar@1351: ///the mapping type to \ref ISOMORPH. madarasip@1350: Vf2Wizard &iso() madarasip@1350: { madarasip@1350: _mapping_type = ISOMORPH; madarasip@1350: return *this; madarasip@1350: } madarasip@1350: alpar@1351: ///Runs VF2 algorithm. alpar@1351: alpar@1351: ///This method runs VF2 algorithm. alpar@1351: /// alpar@1351: ///\retval true if a mapping is found. alpar@1351: ///\retval false if there is no (more) mapping. madarasip@1350: bool run() madarasip@1350: { madarasip@1350: if(Base::_local_mapping) madarasip@1350: Base::createMapping(); madarasip@1350: madarasip@1350: Vf2 madarasip@1350: alg(_g1, _g2, *reinterpret_cast(_mapping), _node_eq); madarasip@1350: madarasip@1350: alg.mappingType(_mapping_type); madarasip@1350: madarasip@1350: bool ret = alg.find(); madarasip@1350: madarasip@1350: if(Base::_local_mapping) madarasip@1350: delete reinterpret_cast(_mapping); madarasip@1350: madarasip@1350: return ret; madarasip@1350: } madarasip@1350: }; madarasip@1350: alpar@1351: ///Function-type interface for VF2 algorithm. alpar@1351: alpar@1351: /// \ingroup graph_isomorphism alpar@1351: ///Function-type interface for VF2 algorithm \cite cordella2004sub. alpar@1351: /// alpar@1351: ///This function has several \ref named-func-param "named parameters" alpar@1351: ///declared as the members of class \ref Vf2Wizard. alpar@1351: ///The following examples show how to use these parameters. alpar@1351: ///\code alpar@1351: /// // Find an embedding of graph g into graph h alpar@1351: /// ListGraph::NodeMap m(g); alpar@1351: /// vf2(g,h).mapping(m).run(); alpar@1351: /// alpar@1351: /// // Check whether graphs g and h are isomorphic alpar@1351: /// bool is_iso = vf2(g,h).iso().run(); alpar@1351: ///\endcode alpar@1351: ///\warning Don't forget to put the \ref Vf2Wizard::run() "run()" alpar@1351: ///to the end of the expression. alpar@1351: ///\sa Vf2Wizard alpar@1351: ///\sa Vf2 madarasip@1350: template madarasip@1350: Vf2Wizard > vf2(const G1 &g1, const G2 &g2) madarasip@1350: { madarasip@1350: return Vf2Wizard >(g1,g2); madarasip@1350: } madarasip@1350: madarasip@1350: } madarasip@1350: madarasip@1350: #endif