1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2015-2017
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 Plus Plus algorithm.
25 #include <lemon/core.h>
26 #include <lemon/concepts/graph.h>
27 #include <lemon/dfs.h>
28 #include <lemon/bfs.h>
29 #include <lemon/bits/vf2_internals.h>
42 class DfsLeaveOrder : public DfsVisitor<G> {
45 std::vector<typename G::Node> &_order;
47 DfsLeaveOrder(const G &g, std::vector<typename G::Node> &order)
48 : i(countNodes(g)), _g(g), _order(order) {
50 void leave(const typename G::Node &node) {
56 class BfsLeaveOrder : public BfsVisitor<G> {
59 std::vector<typename G::Node> &_order;
61 BfsLeaveOrder(const G &g, std::vector<typename G::Node> &order) { }
62 void process(const typename G::Node &node) {
70 ///%VF2 Plus Plus algorithm class.
72 ///\ingroup graph_isomorphism This class provides an efficient
73 ///implementation of the %VF2 Plus Plus algorithm
74 ///for variants of the (Sub)graph Isomorphism problem.
76 ///There is also a \ref vf2pp() "function-type interface" called
77 ///\ref vf2pp() for the %VF2 Plus Plus algorithm, which is probably
78 ///more convenient in most use cases.
80 ///\tparam G1 The type of the graph to be embedded.
81 ///The default type is \ref ListDigraph.
82 ///\tparam G2 The type of the graph g1 will be embedded into.
83 ///The default type is \ref ListDigraph.
84 ///\tparam M The type of the NodeMap storing the mapping.
85 ///By default, it is G1::NodeMap<G2::Node>
86 ///\tparam M1 The type of the NodeMap storing the integer node labels of G1.
87 ///The labels must be the numbers {0,1,2,..,K-1}, where K is the number of
88 ///different labels. By default, it is G1::NodeMap<int>.
89 ///\tparam M2 The type of the NodeMap storing the integer node labels of G2.
90 ///The labels must be the numbers {0,1,2,..,K-1}, where K is the number of
91 ///different labels. By default, it is G2::NodeMap<int>.
95 template<class G1, class G2, class M, class M1, class M2 >
97 template<class G1=ListDigraph,
99 class M = typename G1::template NodeMap<G2::Node>,
100 class M1 = typename G1::template NodeMap<int>,
101 class M2 = typename G2::template NodeMap<int> >
104 //Current depth in the search tree.
107 //_conn[v2] = number of covered neighbours of v2
108 typename G2::template NodeMap<int> _conn;
110 //The current mapping. _mapping[v1]=v2 iff v1 has been mapped to v2,
111 //where v1 is a node of G1 and v2 is a node of G2
114 //order[i] is a node of g1 for which a pair is searched if depth=i
115 std::vector<typename G1::Node> order;
117 //currEdgeIts[i] is the last used edge iterator in the i-th
118 //depth to find a pair for node order[i]
119 std::vector<typename G2::IncEdgeIt> currEdgeIts;
121 //The graph to be embedded
124 //The graph into which g1 is to be embedded
127 //rNewLabels1[v] is a pair of form
128 //(label; num. of uncov. nodes with such label and no covered neighbours)
129 typename G1::template NodeMap<std::vector<std::pair<int,int> > >
132 //rInOutLabels1[v] is the number of covered neighbours of v for each label
133 //in form (label,number of such labels)
134 typename G1::template NodeMap<std::vector<std::pair<int,int> > >
137 //_intLabels1[v]==i means that node v has label i in _g1
138 //(i is in {0,1,2,..,K-1}, where K is the number of diff. labels)
141 //_intLabels2[v]==i means that node v has label i in _g2
142 //(i is in {0,1,2,..,K-1}, where K is the number of diff. labels)
148 //lookup tables for manipulating with label class cardinalities
149 //(after use they have to be reset to 0..0)
150 std::vector<int> labelTmp1,labelTmp2;
152 MappingType _mapping_type;
154 //indicates whether the mapping or the labels must be deleted in the destructor
155 bool _deallocMappingAfterUse,_deallocLabelsAfterUse;
158 //improved cutting function
159 template<MappingType MT>
160 bool cutByLabels(const typename G1::Node n1,const typename G2::Node n2) {
161 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) {
162 const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
163 if(_conn[currNode]>0)
164 --labelTmp1[_intLabels2[currNode]];
165 else if(MT!=SUBGRAPH&&_conn[currNode]==0)
166 --labelTmp2[_intLabels2[currNode]];
171 for(unsigned int i = 0; i < rInOutLabels1[n1].size(); ++i)
172 labelTmp1[rInOutLabels1[n1][i].first]+=rInOutLabels1[n1][i].second;
175 for(unsigned int i = 0; i < rNewLabels1[n1].size(); ++i)
176 labelTmp2[rNewLabels1[n1][i].first]+=rNewLabels1[n1][i].second;
180 for(unsigned int i = 0; i < rInOutLabels1[n1].size(); ++i)
181 if(labelTmp1[rInOutLabels1[n1][i].first]>0) {
186 for(unsigned int i = 0; i < rNewLabels1[n1].size(); ++i)
187 if(labelTmp2[rNewLabels1[n1][i].first]>0) {
193 for(unsigned int i = 0; i < rInOutLabels1[n1].size(); ++i)
194 if(labelTmp1[rInOutLabels1[n1][i].first]>0) {
200 for(unsigned int i = 0; i < rInOutLabels1[n1].size(); ++i)
201 if(labelTmp1[rInOutLabels1[n1][i].first]!=0) {
206 for(unsigned int i = 0; i < rNewLabels1[n1].size(); ++i)
207 if(labelTmp2[rNewLabels1[n1][i].first]!=0) {
215 for(unsigned int i = 0; i < rInOutLabels1[n1].size(); ++i)
216 labelTmp1[rInOutLabels1[n1][i].first]=0;
219 for(unsigned int i = 0; i < rNewLabels1[n1].size(); ++i)
220 labelTmp2[rNewLabels1[n1][i].first]=0;
223 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) {
224 const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
225 labelTmp1[_intLabels2[currNode]]=0;
226 if(MT!=SUBGRAPH&&_conn[currNode]==0)
227 labelTmp2[_intLabels2[currNode]]=0;
234 //try to exclude the matching of n1 and n2
235 template<MappingType MT>
236 bool feas(const typename G1::Node n1,const typename G2::Node n2) {
237 if(_intLabels1[n1]!=_intLabels2[n2])
240 for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1) {
241 const typename G1::Node& currNode=_g1.oppositeNode(n1,e1);
242 if(_mapping[currNode]!=INVALID)
243 --_conn[_mapping[currNode]];
247 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) {
248 int& connCurrNode = _conn[_g2.oppositeNode(n2,e2)];
251 else if(MT!=SUBGRAPH&&connCurrNode==-1) {
258 for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1) {
259 const typename G2::Node& currNodePair =
260 _mapping[_g1.oppositeNode(n1,e1)];
261 int& connCurrNodePair=_conn[currNodePair];
262 if(currNodePair!=INVALID&&connCurrNodePair!=-1) {
269 if(connCurrNodePair<-1)
277 for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1) {
278 const typename G2::Node currNode=_mapping[_g1.oppositeNode(n1,e1)];
279 if(currNode!=INVALID/*&&_conn[currNode]!=-1*/)
283 return isIso&&cutByLabels<MT>(n1,n2);
287 void addPair(const typename G1::Node n1,const typename G2::Node n2) {
290 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2) {
291 int& currConn = _conn[_g2.oppositeNode(n2,e2)];
297 //removes mapping of n1 to n2
298 void subPair(const typename G1::Node n1,const typename G2::Node n2) {
300 _mapping.set(n1,INVALID);
301 for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2){
302 int& currConn = _conn[_g2.oppositeNode(n2,e2)];
305 else if(currConn==-1)
310 void processBFSLevel(typename G1::Node source,unsigned int& orderIndex,
311 typename G1::template NodeMap<int>& dm1,
312 typename G1::template NodeMap<bool>& added) {
313 order[orderIndex]=source;
316 unsigned int endPosOfLevel=orderIndex,
317 startPosOfLevel=orderIndex,
318 lastAdded=orderIndex;
320 typename G1::template NodeMap<int> currConn(_g1,0);
322 while(orderIndex<=lastAdded){
323 typename G1::Node currNode = order[orderIndex];
324 for(typename G1::IncEdgeIt e(_g1,currNode); e!=INVALID; ++e) {
325 typename G1::Node n = _g1.oppositeNode(currNode,e);
327 order[++lastAdded]=n;
331 if(orderIndex>endPosOfLevel){
332 for(unsigned int j = startPosOfLevel; j <= endPosOfLevel; ++j) {
334 for(unsigned int i = j+1; i <= endPosOfLevel; ++i)
335 if(currConn[order[i]]>currConn[order[minInd]]||
336 (currConn[order[i]]==currConn[order[minInd]]&&
337 (dm1[order[i]]>dm1[order[minInd]]||
338 (dm1[order[i]]==dm1[order[minInd]]&&
339 labelTmp1[_intLabels1[order[minInd]]]>
340 labelTmp1[_intLabels1[order[i]]]))))
343 --labelTmp1[_intLabels1[order[minInd]]];
344 for(typename G1::IncEdgeIt e(_g1,order[minInd]); e!=INVALID; ++e)
345 ++currConn[_g1.oppositeNode(order[minInd],e)];
346 std::swap(order[j],order[minInd]);
348 startPosOfLevel=endPosOfLevel+1;
349 endPosOfLevel=lastAdded;
356 //we will find pairs for the nodes of g1 in this order
358 for(typename G2::NodeIt n2(_g2); n2!=INVALID; ++n2)
359 ++labelTmp1[_intLabels2[n2]];
361 typename G1::template NodeMap<int> dm1(_g1,0);
362 for(typename G1::EdgeIt e(_g1); e!=INVALID; ++e) {
367 typename G1::template NodeMap<bool> added(_g1,0);
368 unsigned int orderIndex=0;
370 for(typename G1::NodeIt n(_g1); n!=INVALID;) {
372 typename G1::Node minNode = n;
373 for(typename G1::NodeIt n1(_g1,minNode); n1!=INVALID; ++n1)
375 (labelTmp1[_intLabels1[minNode]]>
376 labelTmp1[_intLabels1[n1]]||(dm1[minNode]<dm1[n1]&&
377 labelTmp1[_intLabels1[minNode]]==
378 labelTmp1[_intLabels1[n1]])))
380 processBFSLevel(minNode,orderIndex,dm1,added);
385 for(unsigned int i = 0; i < labelTmp1.size(); ++i)
390 template<MappingType MT>
393 if(_depth==static_cast<int>(order.size())) {
394 //all nodes of g1 are mapped to nodes of g2
398 typename G1::Node& nodeOfDepth = order[_depth];
399 const typename G2::Node& pairOfNodeOfDepth = _mapping[nodeOfDepth];
400 typename G2::IncEdgeIt &edgeItOfDepth = currEdgeIts[_depth];
401 //the node of g2 whose neighbours are the candidates for
402 //the pair of order[_depth]
403 typename G2::Node currPNode;
404 if(edgeItOfDepth==INVALID){
405 typename G1::IncEdgeIt fstMatchedE(_g1,nodeOfDepth);
406 //if _mapping[order[_depth]]!=INVALID, we don't need fstMatchedE
407 if(pairOfNodeOfDepth==INVALID) {
408 for(; fstMatchedE!=INVALID &&
409 _mapping[_g1.oppositeNode(nodeOfDepth,
410 fstMatchedE)]==INVALID;
411 ++fstMatchedE); //find fstMatchedE, it could be preprocessed
413 if(fstMatchedE==INVALID||pairOfNodeOfDepth!=INVALID) {
414 //We found no covered neighbours, this means that
415 //the graph is not connected (or _depth==0). Each
416 //uncovered (and there are some other properties due
417 //to the spec. problem types) node of g2 is
418 //candidate. We can read the iterator of the last
419 //tried node from the match if it is not the first
420 //try (match[nodeOfDepth]!=INVALID)
421 typename G2::NodeIt n2(_g2);
422 //if it's not the first try
423 if(pairOfNodeOfDepth!=INVALID) {
424 n2=++typename G2::NodeIt(_g2,pairOfNodeOfDepth);
425 subPair(nodeOfDepth,pairOfNodeOfDepth);
427 for(; n2!=INVALID; ++n2)
429 if(_conn[n2]==0&&feas<MT>(nodeOfDepth,n2))
432 else if(_conn[n2]>=0&&feas<MT>(nodeOfDepth,n2))
434 // n2 is the next candidate
436 addPair(nodeOfDepth,n2);
439 else // there are no more candidates
444 currPNode=_mapping[_g1.oppositeNode(nodeOfDepth,
446 edgeItOfDepth=typename G2::IncEdgeIt(_g2,currPNode);
450 currPNode=_g2.oppositeNode(pairOfNodeOfDepth,
452 subPair(nodeOfDepth,pairOfNodeOfDepth);
455 for(; edgeItOfDepth!=INVALID; ++edgeItOfDepth) {
456 const typename G2::Node currNode =
457 _g2.oppositeNode(currPNode, edgeItOfDepth);
458 if(_conn[currNode]>0&&feas<MT>(nodeOfDepth,currNode)) {
459 addPair(nodeOfDepth,currNode);
463 edgeItOfDepth==INVALID?--_depth:++_depth;
468 //calculate the lookup table for cutting the search tree
469 void setRNew1tRInOut1t(){
470 typename G1::template NodeMap<int> tmp(_g1,0);
471 for(unsigned int i=0; i<order.size(); ++i) {
473 for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1) {
474 const typename G1::Node currNode=_g1.oppositeNode(order[i],e1);
476 ++labelTmp1[_intLabels1[currNode]];
477 else if(tmp[currNode]==0)
478 ++labelTmp2[_intLabels1[currNode]];
480 //labelTmp1[i]=number of neightbours with label i in set rInOut
481 //labelTmp2[i]=number of neightbours with label i in set rNew
482 for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1) {
483 const int& currIntLabel = _intLabels1[_g1.oppositeNode(order[i],e1)];
484 if(labelTmp1[currIntLabel]>0) {
485 rInOutLabels1[order[i]]
486 .push_back(std::make_pair(currIntLabel,
487 labelTmp1[currIntLabel]));
488 labelTmp1[currIntLabel]=0;
490 else if(labelTmp2[currIntLabel]>0) {
491 rNewLabels1[order[i]].
492 push_back(std::make_pair(currIntLabel,labelTmp2[currIntLabel]));
493 labelTmp2[currIntLabel]=0;
497 for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1) {
498 int& tmpCurrNode=tmp[_g1.oppositeNode(order[i],e1)];
505 int getMaxLabel() const{
507 for(typename G1::NodeIt n1(_g1); n1!=INVALID; ++n1) {
508 const int& currIntLabel = _intLabels1[n1];
512 for(typename G2::NodeIt n2(_g2); n2!=INVALID; ++n2) {
513 const int& currIntLabel = _intLabels2[n2];
524 ///\param g1 The graph to be embedded.
525 ///\param g2 The graph \e g1 will be embedded into.
526 ///\param m The type of the NodeMap storing the mapping.
527 ///By default, it is G1::NodeMap<G2::Node>
528 ///\param intLabel1 The NodeMap storing the integer node labels of G1.
529 ///The labels must be the numbers {0,1,2,..,K-1}, where K is the number of
531 ///\param intLabel1 The NodeMap storing the integer node labels of G2.
532 ///The labels must be the numbers {0,1,2,..,K-1}, where K is the number of
534 Vf2pp(const G1 &g1, const G2 &g2,M &m, M1 &intLabels1, M2 &intLabels2) :
535 _depth(0), _conn(g2,0), _mapping(m), order(countNodes(g1),INVALID),
536 currEdgeIts(countNodes(g1),INVALID), _g1(g1), _g2(g2), rNewLabels1(_g1),
537 rInOutLabels1(_g1), _intLabels1(intLabels1) ,_intLabels2(intLabels2),
538 maxLabel(getMaxLabel()), labelTmp1(maxLabel+1),labelTmp2(maxLabel+1),
539 _mapping_type(SUBGRAPH), _deallocMappingAfterUse(0),
540 _deallocLabelsAfterUse(0)
546 for(typename G1::NodeIt n(g1);n!=INVALID;++n)
556 if(_deallocMappingAfterUse)
558 if(_deallocLabelsAfterUse) {
564 ///Returns the current mapping type.
566 ///Returns the current mapping type.
568 MappingType mappingType() const
570 return _mapping_type;
573 ///Sets the mapping type
575 ///Sets the mapping type.
577 ///The mapping type is set to \ref SUBGRAPH by default.
579 ///\sa See \ref MappingType for the possible values.
580 void mappingType(MappingType m_type)
582 _mapping_type = m_type;
587 ///This method finds a mapping from g1 into g2 according to the mapping
588 ///type set by \ref mappingType(MappingType) "mappingType()".
590 ///By subsequent calls, it returns all possible mappings one-by-one.
592 ///\retval true if a mapping is found.
593 ///\retval false if there is no (more) mapping.
596 switch(_mapping_type)
599 return extMatch<SUBGRAPH>();
601 return extMatch<INDUCED>();
603 return extMatch<ISOMORPH>();
610 template<typename G1, typename G2>
611 class Vf2ppWizardBase {
619 MappingType _mapping_type;
621 typedef typename G1::template NodeMap<typename G2::Node> Mapping;
624 void createMapping() {
625 _mapping = new Mapping(_g1);
628 bool _local_nodeLabels;
629 typedef typename G1::template NodeMap<int> NodeLabels1;
630 typedef typename G2::template NodeMap<int> NodeLabels2;
631 void *_nodeLabels1, *_nodeLabels2;
632 void createNodeLabels() {
633 _nodeLabels1 = new NodeLabels1(_g1,0);
634 _nodeLabels2 = new NodeLabels2(_g2,0);
637 Vf2ppWizardBase(const G1 &g1,const G2 &g2)
638 : _g1(g1), _g2(g2), _mapping_type(SUBGRAPH),
639 _local_mapping(1), _local_nodeLabels(1) { }
643 /// \brief Auxiliary class for the function-type interface of %VF2
644 /// Plus Plus algorithm.
646 /// This auxiliary class implements the named parameters of
647 /// \ref vf2pp() "function-type interface" of \ref Vf2pp algorithm.
649 /// \warning This class is not to be used directly.
651 /// \tparam TR The traits class that defines various types used by the
653 template<typename TR>
654 class Vf2ppWizard : public TR {
656 typedef typename TR::Graph1 Graph1;
657 typedef typename TR::Graph2 Graph2;
658 typedef typename TR::Mapping Mapping;
659 typedef typename TR::NodeLabels1 NodeLabels1;
660 typedef typename TR::NodeLabels2 NodeLabels2;
664 using TR::_mapping_type;
666 using TR::_nodeLabels1;
667 using TR::_nodeLabels2;
671 Vf2ppWizard(const Graph1 &g1,const Graph2 &g2) : Base(g1,g2) {}
674 Vf2ppWizard(const Base &b) : Base(b) {}
678 struct SetMappingBase : public Base {
680 SetMappingBase(const Base &b) : Base(b) {}
683 ///\brief \ref named-templ-param "Named parameter" for setting
686 ///\ref named-templ-param "Named parameter" function for setting
687 ///the map that stores the found embedding.
689 Vf2ppWizard< SetMappingBase<T> > mapping(const T &t) {
690 Base::_mapping=reinterpret_cast<void*>(const_cast<T*>(&t));
691 Base::_local_mapping = 0;
692 return Vf2ppWizard<SetMappingBase<T> >(*this);
695 template<typename NL1, typename NL2>
696 struct SetNodeLabelsBase : public Base {
697 typedef NL1 NodeLabels1;
698 typedef NL2 NodeLabels2;
699 SetNodeLabelsBase(const Base &b) : Base(b) { }
702 ///\brief \ref named-templ-param "Named parameter" for setting the
705 ///\ref named-templ-param "Named parameter" function for setting
708 ///\param nodeLabels1 A \ref concepts::ReadMap "readable node map"
709 ///of g1 with integer values. In case of K different labels, the labels
710 ///must be the numbers {0,1,..,K-1}.
711 ///\param nodeLabels2 A \ref concepts::ReadMap "readable node map"
712 ///of g2 with integer values. In case of K different labels, the labels
713 ///must be the numbers {0,1,..,K-1}.
714 template<typename NL1, typename NL2>
715 Vf2ppWizard< SetNodeLabelsBase<NL1,NL2> >
716 nodeLabels(const NL1 &nodeLabels1, const NL2 &nodeLabels2) {
717 Base::_local_nodeLabels = 0;
719 reinterpret_cast<void*>(const_cast<NL1*>(&nodeLabels1));
721 reinterpret_cast<void*>(const_cast<NL2*>(&nodeLabels2));
722 return Vf2ppWizard<SetNodeLabelsBase<NL1,NL2> >
723 (SetNodeLabelsBase<NL1,NL2>(*this));
727 ///\brief \ref named-templ-param "Named parameter" for setting
730 ///\ref named-templ-param "Named parameter" for setting
733 ///The mapping type is set to \ref SUBGRAPH by default.
735 ///\sa See \ref MappingType for the possible values.
736 Vf2ppWizard<Base> &mappingType(MappingType m_type) {
737 _mapping_type = m_type;
741 ///\brief \ref named-templ-param "Named parameter" for setting
742 ///the mapping type to \ref INDUCED.
744 ///\ref named-templ-param "Named parameter" for setting
745 ///the mapping type to \ref INDUCED.
746 Vf2ppWizard<Base> &induced() {
747 _mapping_type = INDUCED;
751 ///\brief \ref named-templ-param "Named parameter" for setting
752 ///the mapping type to \ref ISOMORPH.
754 ///\ref named-templ-param "Named parameter" for setting
755 ///the mapping type to \ref ISOMORPH.
756 Vf2ppWizard<Base> &iso() {
757 _mapping_type = ISOMORPH;
761 ///Runs the %VF2 Plus Plus algorithm.
763 ///This method runs the VF2 Plus Plus algorithm.
765 ///\retval true if a mapping is found.
766 ///\retval false if there is no mapping.
768 if(Base::_local_mapping)
769 Base::createMapping();
770 if(Base::_local_nodeLabels)
771 Base::createNodeLabels();
773 Vf2pp<Graph1, Graph2, Mapping, NodeLabels1, NodeLabels2 >
774 alg(_g1, _g2, *reinterpret_cast<Mapping*>(_mapping),
775 *reinterpret_cast<NodeLabels1*>(_nodeLabels1),
776 *reinterpret_cast<NodeLabels2*>(_nodeLabels2));
778 alg.mappingType(_mapping_type);
780 const bool ret = alg.find();
782 if(Base::_local_nodeLabels) {
783 delete reinterpret_cast<NodeLabels1*>(_nodeLabels1);
784 delete reinterpret_cast<NodeLabels2*>(_nodeLabels2);
786 if(Base::_local_mapping)
787 delete reinterpret_cast<Mapping*>(_mapping);
792 ///Get a pointer to the generated Vf2pp object.
794 ///Gives a pointer to the generated Vf2pp object.
796 ///\return Pointer to the generated Vf2pp object.
797 ///\warning Don't forget to delete the referred Vf2pp object after use.
798 Vf2pp<Graph1, Graph2, Mapping, NodeLabels1, NodeLabels2 >*
799 getPtrToVf2ppObject(){
800 if(Base::_local_mapping)
801 Base::createMapping();
802 if(Base::_local_nodeLabels)
803 Base::createNodeLabels();
805 Vf2pp<Graph1, Graph2, Mapping, NodeLabels1, NodeLabels2 >* ptr =
806 new Vf2pp<Graph1, Graph2, Mapping, NodeLabels1, NodeLabels2>
807 (_g1, _g2, *reinterpret_cast<Mapping*>(_mapping),
808 *reinterpret_cast<NodeLabels1*>(_nodeLabels1),
809 *reinterpret_cast<NodeLabels2*>(_nodeLabels2));
810 ptr->mappingType(_mapping_type);
811 if(Base::_local_mapping)
812 ptr->_deallocMappingAfterUse=true;
813 if(Base::_local_nodeLabels)
814 ptr->_deallocLabelMapsAfterUse=true;
819 ///Counts the number of mappings.
821 ///This method counts the number of mappings.
823 /// \return The number of mappings.
825 if(Base::_local_mapping)
826 Base::createMapping();
827 if(Base::_local_nodeLabels)
828 Base::createNodeLabels();
830 Vf2pp<Graph1, Graph2, Mapping, NodeLabels1, NodeLabels2>
831 alg(_g1, _g2, *reinterpret_cast<Mapping*>(_mapping),
832 *reinterpret_cast<NodeLabels1*>(_nodeLabels1),
833 *reinterpret_cast<NodeLabels2*>(_nodeLabels2));
835 alg.mappingType(_mapping_type);
841 if(Base::_local_nodeLabels) {
842 delete reinterpret_cast<NodeLabels1*>(_nodeLabels1);
843 delete reinterpret_cast<NodeLabels2*>(_nodeLabels2);
845 if(Base::_local_mapping)
846 delete reinterpret_cast<Mapping*>(_mapping);
853 ///Function-type interface for VF2 Plus Plus algorithm.
855 /// \ingroup graph_isomorphism
856 ///Function-type interface for VF2 Plus Plus algorithm.
858 ///This function has several \ref named-func-param "named parameters"
859 ///declared as the members of class \ref Vf2ppWizard.
860 ///The following examples show how to use these parameters.
862 /// ListGraph::NodeMap<ListGraph::Node> m(g);
863 /// // Find an embedding of graph g1 into graph g2
864 /// vf2pp(g1,g2).mapping(m).run();
866 /// // Check whether graphs g1 and g2 are isomorphic
867 /// bool is_iso = vf2pp(g1,g2).iso().run();
869 /// // Count the number of isomorphisms
870 /// int num_isos = vf2pp(g1,g2).iso().count();
872 /// // Iterate through all the induced subgraph mappings
873 /// // of graph g1 into g2 using the labels c1 and c2
874 /// auto* myVf2pp = vf2pp(g1,g2).mapping(m).nodeLabels(c1,c2)
875 /// .induced().getPtrToVf2Object();
876 /// while(myVf2pp->find()){
877 /// //process the current mapping m
881 ///\warning Don't forget to put the \ref Vf2ppWizard::run() "run()",
882 ///\ref Vf2ppWizard::count() "count()" or
883 ///the \ref Vf2ppWizard::getPtrToVf2ppObject() "getPtrToVf2ppObject()"
884 ///to the end of the expression.
887 template<class G1, class G2>
888 Vf2ppWizard<Vf2ppWizardBase<G1,G2> > vf2pp(const G1 &g1, const G2 &g2) {
889 return Vf2ppWizard<Vf2ppWizardBase<G1,G2> >(g1,g2);