1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
6 * EMAXA Kutato-fejleszto Kft. (EMAXA Research Ltd.)
8 * Permission to use, modify and distribute this software is granted
9 * provided that this copyright notice appears in all copies. For
10 * precise terms see the accompanying LICENSE file.
12 * This software is provided "AS IS" with no warranty of any kind,
13 * express or implied, and with no claim as to its suitability for any
21 ///\ingroup graph_properties
23 ///\brief VF2 algorithm \cite cordella2004sub.
25 #include <lemon/core.h>
26 #include <lemon/concepts/graph.h>
27 #include <lemon/dfs.h>
28 #include <lemon/bfs.h>
42 template<class T1, class T2>
43 bool operator()(T1, T2) const
49 template<class M1, class M2>
55 MapEq(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
56 bool operator()(typename M1::Key k1, typename M2::Key k2) const
58 return _m1[k1] == _m2[k2];
63 class DfsLeaveOrder : public DfsVisitor<G>
66 std::vector<typename G::Node> &_order;
69 DfsLeaveOrder(const G &g, std::vector<typename G::Node> &order)
70 : i(countNodes(g)), _g(g), _order(order)
72 void leave(const typename G::Node &node)
79 class BfsLeaveOrder : public BfsVisitor<G>
83 std::vector<typename G::Node> &_order;
85 BfsLeaveOrder(const G &g, std::vector<typename G::Node> &order)
86 : i(0), _g(g), _order(order)
88 void process(const typename G::Node &node)
96 ///Graph mapping types.
98 ///\ingroup graph_isomorphism
99 ///The \ref Vf2 "VF2" algorithm is capable of finding different kind of
100 ///embeddings, this enum specifies its type.
102 ///See \ref graph_isomorphism for a more detailed description.
103 enum Vf2MappingType {
104 /// Subgraph isomorphism
106 /// Induced subgraph isomorphism
108 /// Graph isomorphism
110 /// If the two graph has the same number of nodes, than it is
111 /// equivalent to \ref INDUCED, and if they also have the same
112 /// number of edges, then it is also equivalent to \ref SUBGRAPH.
114 /// However, using this setting is faster than the other two
119 ///%VF2 algorithm class.
121 ///\ingroup graph_isomorphism This class provides an efficient
122 ///implementation of the %VF2 algorithm \cite cordella2004sub
123 ///for variants of the (Sub)graph Isomorphism problem.
125 ///There is also a \ref vf2() "function-type interface" called \ref vf2()
126 ///for the %VF2 algorithm, which is probably more convenient in most
129 ///\tparam G1 The type of the graph to be embedded.
130 ///The default type is \ref ListDigraph.
131 ///\tparam G2 The type of the graph g1 will be embedded into.
132 ///The default type is \ref ListDigraph.
133 ///\tparam M The type of the NodeMap storing the mapping.
134 ///By default, it is G1::NodeMap<G2::Node>
135 ///\tparam NEQ A bool-valued binary functor determinining whether a node is
136 ///mappable to another. By default it is an always true operator.
140 template<class G1, class G2, class M, class NEQ >
142 template<class G1=ListDigraph,
143 class G2=ListDigraph,
144 class M = typename G1::template NodeMap<G2::Node>,
145 class NEQ = bits::vf2::AlwaysEq >
149 //Current depth in the DFS tree.
151 //Functor with bool operator()(G1::Node,G2::Node), which returns 1
152 //if and only if the 2 nodes are equivalent.
155 typename G2::template NodeMap<int> _conn;
156 //Current mapping. We index it by the nodes of g1, and match[v] is
159 //order[i] is the node of g1, for which we find a pair if depth=i
160 std::vector<typename G1::Node> order;
161 //currEdgeIts[i] is an edge iterator, witch is last used in the ith
162 //depth to find a pair for order[i].
163 std::vector<typename G2::IncEdgeIt> currEdgeIts;
168 //lookup tables for cut the searchtree
169 typename G1::template NodeMap<int> rNew1t,rInOut1t;
171 Vf2MappingType _mapping_type;
173 //cut the search tree
174 template<Vf2MappingType MT>
175 bool cut(const typename G1::Node n1,const typename G2::Node n2) const
177 int rNew2=0,rInOut2=0;
178 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
180 const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
181 if(_conn[currNode]>0)
183 else if(MT!=SUBGRAPH&&_conn[currNode]==0)
189 return rInOut1t[n1]<=rInOut2&&rNew1t[n1]<=rNew2;
191 return rInOut1t[n1]<=rInOut2;
193 return rInOut1t[n1]==rInOut2&&rNew1t[n1]==rNew2;
199 template<Vf2MappingType MT>
200 bool feas(const typename G1::Node n1,const typename G2::Node n2)
205 for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1)
207 const typename G1::Node currNode=_g1.oppositeNode(n1,e1);
208 if(_mapping[currNode]!=INVALID)
209 --_conn[_mapping[currNode]];
212 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
214 const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
215 if(_conn[currNode]<-1)
217 else if(MT!=SUBGRAPH&&_conn[currNode]==-1)
224 for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1)
226 const typename G1::Node currNode=_g1.oppositeNode(n1,e1);
227 if(_mapping[currNode]!=INVALID&&_conn[_mapping[currNode]]!=-1)
236 if(_conn[_mapping[currNode]]<-1)
240 _conn[_mapping[currNode]]=-1;
243 return isIso&&cut<MT>(n1,n2);
246 void addPair(const typename G1::Node n1,const typename G2::Node n2)
250 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
251 if(_conn[_g2.oppositeNode(n2,e2)]!=-1)
252 ++_conn[_g2.oppositeNode(n2,e2)];
255 void subPair(const typename G1::Node n1,const typename G2::Node n2)
258 _mapping.set(n1,INVALID);
259 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
261 const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
262 if(_conn[currNode]>0)
264 else if(_conn[currNode]==-1)
269 void setOrder()//we will find pairs for the nodes of g1 in this order
271 // bits::vf2::DfsLeaveOrder<G1> v(_g1,order);
272 // DfsVisit<G1,bits::vf2::DfsLeaveOrder<G1> >dfs(_g1, v);
275 //it is more efficient in practice than DFS
276 bits::vf2::BfsLeaveOrder<G1> v(_g1,order);
277 BfsVisit<G1,bits::vf2::BfsLeaveOrder<G1> >bfs(_g1, v);
281 template<Vf2MappingType MT>
286 //there are not nodes in g1, which has not pair in g2.
287 if(_depth==static_cast<int>(order.size()))
292 //the node of g2, which neighbours are the candidates for
293 //the pair of order[_depth]
294 typename G2::Node currPNode;
295 if(currEdgeIts[_depth]==INVALID)
297 typename G1::IncEdgeIt fstMatchedE(_g1,order[_depth]);
298 //if _mapping[order[_depth]]!=INVALID, we dont use
300 if(_mapping[order[_depth]]==INVALID)
301 for(; fstMatchedE!=INVALID &&
302 _mapping[_g1.oppositeNode(order[_depth],
303 fstMatchedE)]==INVALID;
304 ++fstMatchedE) ; //find fstMatchedE
305 if(fstMatchedE==INVALID||_mapping[order[_depth]]!=INVALID)
307 //We did not find an covered neighbour, this means
308 //the graph is not connected(or _depth==0). Every
309 //uncovered(and there are some other properties due
310 //to the spec. problem types) node of g2 is
311 //candidate. We can read the iterator of the last
312 //tryed node from the match if it is not the first
313 //try(match[order[_depth]]!=INVALID)
314 typename G2::NodeIt n2(_g2);
315 //if its not the first try
316 if(_mapping[order[_depth]]!=INVALID)
318 n2=++typename G2::NodeIt(_g2,_mapping[order[_depth]]);
319 subPair(order[_depth],_mapping[order[_depth]]);
321 for(; n2!=INVALID; ++n2)
322 if(MT!=SUBGRAPH&&_conn[n2]==0)
324 if(feas<MT>(order[_depth],n2))
327 else if(MT==SUBGRAPH&&_conn[n2]>=0)
328 if(feas<MT>(order[_depth],n2))
330 // n2 is the next candidate
333 addPair(order[_depth],n2);
336 else // there is no more candidate
342 currPNode=_mapping[_g1.oppositeNode(order[_depth],
344 currEdgeIts[_depth]=typename G2::IncEdgeIt(_g2,currPNode);
349 currPNode=_g2.oppositeNode(_mapping[order[_depth]],
350 currEdgeIts[_depth]);
351 subPair(order[_depth],_mapping[order[_depth]]);
352 ++currEdgeIts[_depth];
354 for(; currEdgeIts[_depth]!=INVALID; ++currEdgeIts[_depth])
356 const typename G2::Node currNode =
357 _g2.oppositeNode(currPNode, currEdgeIts[_depth]);
358 //if currNode is uncovered
359 if(_conn[currNode]>0&&feas<MT>(order[_depth],currNode))
361 addPair(order[_depth],currNode);
365 currEdgeIts[_depth]==INVALID?--_depth:++_depth;
370 //calc. the lookup table for cut the searchtree
371 void setRNew1tRInOut1t()
373 typename G1::template NodeMap<int> tmp(_g1,0);
374 for(unsigned int i=0; i<order.size(); ++i)
377 for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1)
379 const typename G1::Node currNode=_g1.oppositeNode(order[i],e1);
381 ++rInOut1t[order[i]];
382 else if(tmp[currNode]==0)
385 for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1)
387 const typename G1::Node currNode=_g1.oppositeNode(order[i],e1);
388 if(tmp[currNode]!=-1)
398 ///\param g1 The graph to be embedded into \e g2.
399 ///\param g2 The graph \e g1 will be embedded into.
400 ///\param m \ref concepts::ReadWriteMap "read-write" NodeMap
401 ///storing the found mapping.
402 ///\param neq A bool-valued binary functor determinining whether a node is
403 ///mappable to another. By default it is an always true operator.
404 Vf2(const G1 &g1, const G2 &g2,M &m, const NEQ &neq = NEQ() ) :
405 _nEq(neq), _conn(g2,0), _mapping(m), order(countNodes(g1)),
406 currEdgeIts(countNodes(g1),INVALID), _g1(g1), _g2(g2), rNew1t(g1,0),
407 rInOut1t(g1,0), _mapping_type(SUBGRAPH)
412 for(typename G1::NodeIt n(g1);n!=INVALID;++n)
416 ///Returns the current mapping type
418 ///Returns the current mapping type
420 Vf2MappingType mappingType() const { return _mapping_type; }
423 ///Sets mapping type.
425 ///The mapping type is set to \ref SUBGRAPH by default.
427 ///\sa See \ref Vf2MappingType for the possible values.
428 void mappingType(Vf2MappingType m_type) { _mapping_type = m_type; }
432 ///It finds a mapping between from g1 into g2 according to the mapping
433 ///type set by \ref mappingType(Vf2MappingType) "mappingType()".
435 ///By subsequent calls, it returns all possible mappings one-by-one.
437 ///\retval true if a mapping is found.
438 ///\retval false if there is no (more) mapping.
441 switch(_mapping_type)
444 return extMatch<SUBGRAPH>();
446 return extMatch<INDUCED>();
448 return extMatch<ISOMORPH>();
455 template<class G1, class G2>
465 Vf2MappingType _mapping_type;
467 typedef typename G1::template NodeMap<typename G2::Node> Mapping;
472 _mapping = new Mapping(_g1);
475 typedef bits::vf2::AlwaysEq NodeEq;
478 Vf2WizardBase(const G1 &g1,const G2 &g2)
479 : _g1(g1), _g2(g2), _mapping_type(SUBGRAPH), _local_mapping(true) {}
482 /// Auxiliary class for the function-type interface of %VF2 algorithm.
484 /// This auxiliary class implements the named parameters of
485 /// \ref vf2() "function-type interface" of \ref Vf2 algorithm.
487 /// \warning This class should only be used through the function \ref vf2().
489 /// \tparam TR The traits class that defines various types used by the
492 class Vf2Wizard : public TR
495 typedef typename TR::Graph1 Graph1;
496 typedef typename TR::Graph2 Graph2;
498 typedef typename TR::Mapping Mapping;
499 typedef typename TR::NodeEq NodeEq;
503 using TR::_mapping_type;
509 Vf2Wizard(const Graph1 &g1,const Graph2 &g2) : Base(g1,g2) {}
512 Vf2Wizard(const Base &b) : Base(b) {}
516 struct SetMappingBase : public Base {
518 SetMappingBase(const Base &b) : Base(b) {}
521 ///\brief \ref named-templ-param "Named parameter" for setting
524 ///\ref named-templ-param "Named parameter" function for setting
525 ///the map that stores the found embedding.
527 Vf2Wizard< SetMappingBase<T> > mapping(const T &t)
529 Base::_mapping=reinterpret_cast<void*>(const_cast<T*>(&t));
530 Base::_local_mapping = false;
531 return Vf2Wizard<SetMappingBase<T> >(*this);
535 struct SetNodeEqBase : public Base {
538 SetNodeEqBase(const Base &b, const NE &node_eq)
539 : Base(b), _node_eq(node_eq) {}
542 ///\brief \ref named-templ-param "Named parameter" for setting
543 ///the node equivalence relation.
545 ///\ref named-templ-param "Named parameter" function for setting
546 ///the equivalence relation between the nodes.
548 ///\param node_eq A bool-valued binary functor determinining
549 ///whether a node is mappable to another. By default it is an
550 ///always true operator.
552 Vf2Wizard< SetNodeEqBase<T> > nodeEq(const T &node_eq)
554 return Vf2Wizard<SetNodeEqBase<T> >(SetNodeEqBase<T>(*this,node_eq));
557 ///\brief \ref named-templ-param "Named parameter" for setting
560 ///\ref named-templ-param "Named parameter" function for setting
561 ///the node labels defining equivalence relation between them.
563 ///\param m1 It is arbitrary \ref concepts::ReadMap "readable node map"
565 ///\param m2 It is arbitrary \ref concepts::ReadMap "readable node map"
568 ///The value type of these maps must be equal comparable.
569 template<class M1, class M2>
570 Vf2Wizard< SetNodeEqBase<bits::vf2::MapEq<M1,M2> > >
571 nodeLabels(const M1 &m1,const M2 &m2)
573 return nodeEq(bits::vf2::MapEq<M1,M2>(m1,m2));
576 ///\brief \ref named-templ-param "Named parameter" for setting
579 ///\ref named-templ-param "Named parameter" for setting
582 ///The mapping type is set to \ref SUBGRAPH by default.
584 ///\sa See \ref Vf2MappingType for the possible values.
585 Vf2Wizard<Base> &mappingType(Vf2MappingType m_type)
587 _mapping_type = m_type;
591 ///\brief \ref named-templ-param "Named parameter" for setting
592 ///the mapping type to \ref INDUCED.
594 ///\ref named-templ-param "Named parameter" for setting
595 ///the mapping type to \ref INDUCED.
596 Vf2Wizard<Base> &induced()
598 _mapping_type = INDUCED;
602 ///\brief \ref named-templ-param "Named parameter" for setting
603 ///the mapping type to \ref ISOMORPH.
605 ///\ref named-templ-param "Named parameter" for setting
606 ///the mapping type to \ref ISOMORPH.
607 Vf2Wizard<Base> &iso()
609 _mapping_type = ISOMORPH;
613 ///Runs VF2 algorithm.
615 ///This method runs VF2 algorithm.
617 ///\retval true if a mapping is found.
618 ///\retval false if there is no (more) mapping.
621 if(Base::_local_mapping)
622 Base::createMapping();
624 Vf2<Graph1, Graph2, Mapping, NodeEq >
625 alg(_g1, _g2, *reinterpret_cast<Mapping*>(_mapping), _node_eq);
627 alg.mappingType(_mapping_type);
629 bool ret = alg.find();
631 if(Base::_local_mapping)
632 delete reinterpret_cast<Mapping*>(_mapping);
638 ///Function-type interface for VF2 algorithm.
640 /// \ingroup graph_isomorphism
641 ///Function-type interface for VF2 algorithm \cite cordella2004sub.
643 ///This function has several \ref named-func-param "named parameters"
644 ///declared as the members of class \ref Vf2Wizard.
645 ///The following examples show how to use these parameters.
647 /// // Find an embedding of graph g into graph h
648 /// ListGraph::NodeMap<ListGraph::Node> m(g);
649 /// vf2(g,h).mapping(m).run();
651 /// // Check whether graphs g and h are isomorphic
652 /// bool is_iso = vf2(g,h).iso().run();
654 ///\warning Don't forget to put the \ref Vf2Wizard::run() "run()"
655 ///to the end of the expression.
658 template<class G1, class G2>
659 Vf2Wizard<Vf2WizardBase<G1,G2> > vf2(const G1 &g1, const G2 &g2)
661 return Vf2Wizard<Vf2WizardBase<G1,G2> >(g1,g2);