lemon/vf2.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 16 Nov 2015 08:46:42 +0100
changeset 1156 f51c01a1b88e
parent 1152 abc24245d276
child 1186 3feba0ea1bda
permissions -rw-r--r--
Remove unnecessary test include from lemon/vf2.h (#603)
madarasip@1141
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
madarasip@1141
     2
 *
madarasip@1141
     3
 * This file is a part of LEMON, a generic C++ optimization library.
madarasip@1141
     4
 *
madarasip@1141
     5
 * Copyright (C) 2015
madarasip@1141
     6
 * EMAXA Kutato-fejleszto Kft. (EMAXA Research Ltd.)
madarasip@1141
     7
 *
madarasip@1141
     8
 * Permission to use, modify and distribute this software is granted
madarasip@1141
     9
 * provided that this copyright notice appears in all copies. For
madarasip@1141
    10
 * precise terms see the accompanying LICENSE file.
madarasip@1141
    11
 *
madarasip@1141
    12
 * This software is provided "AS IS" with no warranty of any kind,
madarasip@1141
    13
 * express or implied, and with no claim as to its suitability for any
madarasip@1141
    14
 * purpose.
madarasip@1141
    15
 *
madarasip@1141
    16
 */
madarasip@1141
    17
madarasip@1141
    18
#ifndef LEMON_VF2_H
madarasip@1141
    19
#define LEMON_VF2_H
madarasip@1141
    20
alpar@1142
    21
///\ingroup graph_properties
alpar@1142
    22
///\file
alpar@1142
    23
///\brief VF2 algorithm \cite cordella2004sub.
alpar@1142
    24
madarasip@1141
    25
#include <lemon/core.h>
madarasip@1141
    26
#include <lemon/concepts/graph.h>
madarasip@1141
    27
#include <lemon/dfs.h>
madarasip@1141
    28
#include <lemon/bfs.h>
madarasip@1141
    29
madarasip@1141
    30
#include <vector>
madarasip@1141
    31
#include <set>
madarasip@1141
    32
madarasip@1141
    33
namespace lemon
madarasip@1141
    34
{
madarasip@1141
    35
  namespace bits
madarasip@1141
    36
  {
madarasip@1141
    37
    namespace vf2
madarasip@1141
    38
    {
madarasip@1141
    39
      class AlwaysEq
madarasip@1141
    40
      {
madarasip@1141
    41
      public:
madarasip@1141
    42
        template<class T1, class T2>
madarasip@1141
    43
        bool operator()(T1, T2) const
madarasip@1141
    44
        {
madarasip@1141
    45
          return true;
madarasip@1141
    46
        }
madarasip@1141
    47
      };
madarasip@1141
    48
madarasip@1141
    49
      template<class M1, class M2>
madarasip@1141
    50
      class MapEq
madarasip@1141
    51
      {
madarasip@1141
    52
        const M1 &_m1;
madarasip@1141
    53
        const M2 &_m2;
madarasip@1141
    54
      public:
madarasip@1141
    55
        MapEq(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
madarasip@1141
    56
        bool operator()(typename M1::Key k1, typename M2::Key k2) const
madarasip@1141
    57
        {
madarasip@1141
    58
          return _m1[k1] == _m2[k2];
madarasip@1141
    59
        }
madarasip@1141
    60
      };
madarasip@1141
    61
madarasip@1141
    62
      template <class G>
madarasip@1141
    63
      class DfsLeaveOrder : public DfsVisitor<G>
madarasip@1141
    64
      {
madarasip@1141
    65
        const G &_g;
madarasip@1141
    66
        std::vector<typename G::Node> &_order;
madarasip@1141
    67
        int i;
madarasip@1141
    68
      public:
madarasip@1141
    69
        DfsLeaveOrder(const G &g, std::vector<typename G::Node> &order)
madarasip@1141
    70
          : i(countNodes(g)), _g(g), _order(order)
madarasip@1141
    71
        {}
madarasip@1141
    72
        void leave(const typename G::Node &node)
madarasip@1141
    73
        {
madarasip@1141
    74
          _order[--i]=node;
madarasip@1141
    75
        }
madarasip@1141
    76
      };
madarasip@1141
    77
madarasip@1141
    78
      template <class G>
madarasip@1141
    79
      class BfsLeaveOrder : public BfsVisitor<G>
madarasip@1141
    80
      {
madarasip@1141
    81
        int i;
madarasip@1141
    82
        const G &_g;
madarasip@1141
    83
        std::vector<typename G::Node> &_order;
madarasip@1141
    84
      public:
madarasip@1141
    85
        BfsLeaveOrder(const G &g, std::vector<typename G::Node> &order)
madarasip@1141
    86
          : i(0), _g(g), _order(order)
madarasip@1141
    87
        {}
madarasip@1141
    88
        void process(const typename G::Node &node)
madarasip@1141
    89
        {
madarasip@1141
    90
          _order[i++]=node;
madarasip@1141
    91
        }
madarasip@1141
    92
      };
madarasip@1141
    93
    }
madarasip@1141
    94
  }
madarasip@1141
    95
alpar@1142
    96
  ///Graph mapping types.
alpar@1142
    97
alpar@1142
    98
  ///\ingroup graph_isomorphism
alpar@1142
    99
  ///The \ref Vf2 "VF2" algorithm is capable of finding different kind of
alpar@1142
   100
  ///embeddings, this enum specifies its type.
alpar@1142
   101
  ///
alpar@1142
   102
  ///See \ref graph_isomorphism for a more detailed description.
alpar@1142
   103
  enum Vf2MappingType {
alpar@1142
   104
    /// Subgraph isomorphism
madarasip@1141
   105
    SUBGRAPH = 0,
alpar@1142
   106
    /// Induced subgraph isomorphism
madarasip@1141
   107
    INDUCED = 1,
alpar@1142
   108
    /// Graph isomorphism
alpar@1142
   109
alpar@1142
   110
    /// If the two graph has the same number of nodes, than it is
alpar@1142
   111
    /// equivalent to \ref INDUCED, and if they also have the same
alpar@1142
   112
    /// number of edges, then it is also equivalent to \ref SUBGRAPH.
alpar@1142
   113
    ///
alpar@1142
   114
    /// However, using this setting is faster than the other two
alpar@1142
   115
    /// options.
madarasip@1141
   116
    ISOMORPH = 2
madarasip@1141
   117
  };
madarasip@1141
   118
alpar@1142
   119
  ///%VF2 algorithm class.
alpar@1142
   120
alpar@1142
   121
  ///\ingroup graph_isomorphism This class provides an efficient
alpar@1142
   122
  ///implementation of the %VF2 algorithm \cite cordella2004sub
alpar@1142
   123
  ///for variants of the (Sub)graph Isomorphism problem.
alpar@1142
   124
  ///
alpar@1142
   125
  ///There is also a \ref vf2() "function-type interface" called \ref vf2()
alpar@1142
   126
  ///for the %VF2 algorithm, which is probably more convenient in most
alpar@1142
   127
  ///use-cases.
alpar@1142
   128
  ///
alpar@1142
   129
  ///\tparam G1 The type of the graph to be embedded.
alpar@1142
   130
  ///The default type is \ref ListDigraph.
alpar@1142
   131
  ///\tparam G2 The type of the graph g1 will be embedded into.
alpar@1142
   132
  ///The default type is \ref ListDigraph.
alpar@1142
   133
  ///\tparam M The type of the NodeMap storing the mapping.
alpar@1142
   134
  ///By default, it is G1::NodeMap<G2::Node>
alpar@1142
   135
  ///\tparam NEQ A bool-valued binary functor determinining whether a node is
alpar@1142
   136
  ///mappable to another. By default it is an always true operator.
alpar@1142
   137
  ///
alpar@1142
   138
  ///\sa vf2()
alpar@1142
   139
#ifdef DOXYGEN
alpar@1142
   140
  template<class G1, class G2, class M, class NEQ >
alpar@1142
   141
#else
alpar@1142
   142
  template<class G1=ListDigraph,
alpar@1142
   143
           class G2=ListDigraph,
alpar@1142
   144
           class M = typename G1::template NodeMap<G2::Node>,
alpar@1142
   145
           class NEQ = bits::vf2::AlwaysEq >
alpar@1142
   146
#endif
madarasip@1141
   147
  class Vf2
madarasip@1141
   148
  {
madarasip@1141
   149
    //Current depth in the DFS tree.
madarasip@1141
   150
    int _depth;
madarasip@1141
   151
    //Functor with bool operator()(G1::Node,G2::Node), which returns 1
madarasip@1141
   152
    //if and only if the 2 nodes are equivalent.
alpar@1142
   153
    NEQ _nEq;
madarasip@1141
   154
madarasip@1141
   155
    typename G2::template NodeMap<int> _conn;
alpar@1142
   156
    //Current mapping. We index it by the nodes of g1, and match[v] is
madarasip@1141
   157
    //a node of g2.
alpar@1142
   158
    M &_mapping;
madarasip@1141
   159
    //order[i] is the node of g1, for which we find a pair if depth=i
madarasip@1141
   160
    std::vector<typename G1::Node> order;
madarasip@1141
   161
    //currEdgeIts[i] is an edge iterator, witch is last used in the ith
madarasip@1141
   162
    //depth to find a pair for order[i].
madarasip@1141
   163
    std::vector<typename G2::IncEdgeIt> currEdgeIts;
madarasip@1141
   164
    //The small graph.
madarasip@1141
   165
    const G1 &_g1;
madarasip@1141
   166
    //The big graph.
madarasip@1141
   167
    const G2 &_g2;
madarasip@1141
   168
    //lookup tables for cut the searchtree
madarasip@1141
   169
    typename G1::template NodeMap<int> rNew1t,rInOut1t;
madarasip@1141
   170
alpar@1142
   171
    Vf2MappingType _mapping_type;
madarasip@1141
   172
madarasip@1141
   173
    //cut the search tree
alpar@1142
   174
    template<Vf2MappingType MT>
madarasip@1141
   175
    bool cut(const typename G1::Node n1,const typename G2::Node n2) const
madarasip@1141
   176
    {
madarasip@1141
   177
      int rNew2=0,rInOut2=0;
madarasip@1141
   178
      for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
madarasip@1141
   179
        {
madarasip@1141
   180
          const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
madarasip@1141
   181
          if(_conn[currNode]>0)
madarasip@1141
   182
            ++rInOut2;
madarasip@1141
   183
          else if(MT!=SUBGRAPH&&_conn[currNode]==0)
madarasip@1141
   184
            ++rNew2;
madarasip@1141
   185
        }
madarasip@1141
   186
      switch(MT)
madarasip@1141
   187
        {
madarasip@1141
   188
        case INDUCED:
madarasip@1141
   189
          return rInOut1t[n1]<=rInOut2&&rNew1t[n1]<=rNew2;
madarasip@1141
   190
        case SUBGRAPH:
madarasip@1141
   191
          return rInOut1t[n1]<=rInOut2;
madarasip@1141
   192
        case ISOMORPH:
madarasip@1141
   193
          return rInOut1t[n1]==rInOut2&&rNew1t[n1]==rNew2;
madarasip@1141
   194
        default:
madarasip@1141
   195
          return false;
madarasip@1141
   196
        }
madarasip@1141
   197
    }
madarasip@1141
   198
alpar@1142
   199
    template<Vf2MappingType MT>
madarasip@1141
   200
    bool feas(const typename G1::Node n1,const typename G2::Node n2)
madarasip@1141
   201
    {
madarasip@1141
   202
      if(!_nEq(n1,n2))
madarasip@1141
   203
        return 0;
madarasip@1141
   204
madarasip@1141
   205
      for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1)
madarasip@1141
   206
        {
madarasip@1141
   207
          const typename G1::Node currNode=_g1.oppositeNode(n1,e1);
alpar@1142
   208
          if(_mapping[currNode]!=INVALID)
alpar@1142
   209
            --_conn[_mapping[currNode]];
madarasip@1141
   210
        }
madarasip@1141
   211
      bool isIso=1;
madarasip@1141
   212
      for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
madarasip@1141
   213
        {
madarasip@1141
   214
          const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
madarasip@1141
   215
          if(_conn[currNode]<-1)
madarasip@1141
   216
            ++_conn[currNode];
madarasip@1141
   217
          else if(MT!=SUBGRAPH&&_conn[currNode]==-1)
madarasip@1141
   218
            {
madarasip@1141
   219
              isIso=0;
madarasip@1141
   220
              break;
madarasip@1141
   221
            }
madarasip@1141
   222
        }
madarasip@1141
   223
madarasip@1141
   224
      for(typename G1::IncEdgeIt e1(_g1,n1); e1!=INVALID; ++e1)
madarasip@1141
   225
        {
madarasip@1141
   226
          const typename G1::Node currNode=_g1.oppositeNode(n1,e1);
alpar@1142
   227
          if(_mapping[currNode]!=INVALID&&_conn[_mapping[currNode]]!=-1)
madarasip@1141
   228
            {
madarasip@1141
   229
              switch(MT)
madarasip@1141
   230
                {
madarasip@1141
   231
                case INDUCED:
madarasip@1141
   232
                case ISOMORPH:
madarasip@1141
   233
                  isIso=0;
madarasip@1141
   234
                  break;
madarasip@1141
   235
                case SUBGRAPH:
alpar@1142
   236
                  if(_conn[_mapping[currNode]]<-1)
madarasip@1141
   237
                    isIso=0;
madarasip@1141
   238
                  break;
madarasip@1141
   239
                }
alpar@1142
   240
              _conn[_mapping[currNode]]=-1;
madarasip@1141
   241
            }
madarasip@1141
   242
        }
madarasip@1141
   243
      return isIso&&cut<MT>(n1,n2);
madarasip@1141
   244
    }
madarasip@1141
   245
madarasip@1141
   246
    void addPair(const typename G1::Node n1,const typename G2::Node n2)
madarasip@1141
   247
    {
madarasip@1141
   248
      _conn[n2]=-1;
alpar@1142
   249
      _mapping.set(n1,n2);
madarasip@1141
   250
      for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
madarasip@1141
   251
        if(_conn[_g2.oppositeNode(n2,e2)]!=-1)
madarasip@1141
   252
          ++_conn[_g2.oppositeNode(n2,e2)];
madarasip@1141
   253
    }
madarasip@1141
   254
madarasip@1141
   255
    void subPair(const typename G1::Node n1,const typename G2::Node n2)
madarasip@1141
   256
    {
madarasip@1141
   257
      _conn[n2]=0;
alpar@1142
   258
      _mapping.set(n1,INVALID);
madarasip@1141
   259
      for(typename G2::IncEdgeIt e2(_g2,n2); e2!=INVALID; ++e2)
madarasip@1141
   260
        {
madarasip@1141
   261
          const typename G2::Node currNode=_g2.oppositeNode(n2,e2);
madarasip@1141
   262
          if(_conn[currNode]>0)
madarasip@1141
   263
            --_conn[currNode];
madarasip@1141
   264
          else if(_conn[currNode]==-1)
madarasip@1141
   265
            ++_conn[n2];
madarasip@1141
   266
        }
madarasip@1141
   267
    }
madarasip@1141
   268
madarasip@1141
   269
    void setOrder()//we will find pairs for the nodes of g1 in this order
madarasip@1141
   270
    {
madarasip@1141
   271
      // bits::vf2::DfsLeaveOrder<G1> v(_g1,order);
madarasip@1141
   272
      //   DfsVisit<G1,bits::vf2::DfsLeaveOrder<G1> >dfs(_g1, v);
madarasip@1141
   273
      //   dfs.run();
madarasip@1141
   274
madarasip@1141
   275
      //it is more efficient in practice than DFS
madarasip@1141
   276
      bits::vf2::BfsLeaveOrder<G1> v(_g1,order);
madarasip@1141
   277
      BfsVisit<G1,bits::vf2::BfsLeaveOrder<G1> >bfs(_g1, v);
madarasip@1141
   278
      bfs.run();
madarasip@1141
   279
    }
madarasip@1141
   280
alpar@1142
   281
    template<Vf2MappingType MT>
madarasip@1141
   282
    bool extMatch()
madarasip@1141
   283
    {
madarasip@1141
   284
      while(_depth>=0)
madarasip@1141
   285
        {
madarasip@1141
   286
          //there are not nodes in g1, which has not pair in g2.
madarasip@1141
   287
          if(_depth==static_cast<int>(order.size()))
madarasip@1141
   288
            {
madarasip@1141
   289
              --_depth;
madarasip@1141
   290
              return true;
madarasip@1141
   291
            }
madarasip@1141
   292
          //the node of g2, which neighbours are the candidates for
madarasip@1141
   293
          //the pair of order[_depth]
madarasip@1141
   294
          typename G2::Node currPNode;
madarasip@1141
   295
          if(currEdgeIts[_depth]==INVALID)
madarasip@1141
   296
            {
madarasip@1141
   297
              typename G1::IncEdgeIt fstMatchedE(_g1,order[_depth]);
alpar@1142
   298
              //if _mapping[order[_depth]]!=INVALID, we dont use
madarasip@1141
   299
              //fstMatchedE
alpar@1142
   300
              if(_mapping[order[_depth]]==INVALID)
madarasip@1141
   301
                for(; fstMatchedE!=INVALID &&
alpar@1142
   302
                      _mapping[_g1.oppositeNode(order[_depth],
madarasip@1141
   303
                                              fstMatchedE)]==INVALID;
madarasip@1141
   304
                    ++fstMatchedE) ; //find fstMatchedE
alpar@1142
   305
              if(fstMatchedE==INVALID||_mapping[order[_depth]]!=INVALID)
madarasip@1141
   306
                {
madarasip@1141
   307
                  //We did not find an covered neighbour, this means
madarasip@1141
   308
                  //the graph is not connected(or _depth==0).  Every
madarasip@1141
   309
                  //uncovered(and there are some other properties due
madarasip@1141
   310
                  //to the spec. problem types) node of g2 is
madarasip@1141
   311
                  //candidate.  We can read the iterator of the last
madarasip@1141
   312
                  //tryed node from the match if it is not the first
madarasip@1141
   313
                  //try(match[order[_depth]]!=INVALID)
madarasip@1141
   314
                  typename G2::NodeIt n2(_g2);
madarasip@1141
   315
                  //if its not the first try
alpar@1142
   316
                  if(_mapping[order[_depth]]!=INVALID)
madarasip@1141
   317
                    {
alpar@1142
   318
                      n2=++typename G2::NodeIt(_g2,_mapping[order[_depth]]);
alpar@1142
   319
                      subPair(order[_depth],_mapping[order[_depth]]);
madarasip@1141
   320
                    }
madarasip@1141
   321
                  for(; n2!=INVALID; ++n2)
madarasip@1141
   322
                    if(MT!=SUBGRAPH&&_conn[n2]==0)
madarasip@1141
   323
                      {
madarasip@1141
   324
                        if(feas<MT>(order[_depth],n2))
madarasip@1141
   325
                          break;
madarasip@1141
   326
                      }
madarasip@1141
   327
                    else if(MT==SUBGRAPH&&_conn[n2]>=0)
madarasip@1141
   328
                      if(feas<MT>(order[_depth],n2))
madarasip@1141
   329
                        break;
madarasip@1141
   330
                  // n2 is the next candidate
madarasip@1141
   331
                  if(n2!=INVALID)
madarasip@1141
   332
                    {
madarasip@1141
   333
                      addPair(order[_depth],n2);
madarasip@1141
   334
                      ++_depth;
madarasip@1141
   335
                    }
madarasip@1141
   336
                  else // there is no more candidate
madarasip@1141
   337
                    --_depth;
madarasip@1141
   338
                  continue;
madarasip@1141
   339
                }
madarasip@1141
   340
              else
madarasip@1141
   341
                {
alpar@1142
   342
                  currPNode=_mapping[_g1.oppositeNode(order[_depth],
alpar@1142
   343
                                                      fstMatchedE)];
madarasip@1141
   344
                  currEdgeIts[_depth]=typename G2::IncEdgeIt(_g2,currPNode);
madarasip@1141
   345
                }
madarasip@1141
   346
            }
madarasip@1141
   347
          else
madarasip@1141
   348
            {
alpar@1142
   349
              currPNode=_g2.oppositeNode(_mapping[order[_depth]],
madarasip@1141
   350
                                         currEdgeIts[_depth]);
alpar@1142
   351
              subPair(order[_depth],_mapping[order[_depth]]);
madarasip@1141
   352
              ++currEdgeIts[_depth];
madarasip@1141
   353
            }
madarasip@1141
   354
          for(; currEdgeIts[_depth]!=INVALID; ++currEdgeIts[_depth])
madarasip@1141
   355
            {
madarasip@1141
   356
              const typename G2::Node currNode =
madarasip@1141
   357
                _g2.oppositeNode(currPNode, currEdgeIts[_depth]);
madarasip@1141
   358
              //if currNode is uncovered
madarasip@1141
   359
              if(_conn[currNode]>0&&feas<MT>(order[_depth],currNode))
madarasip@1141
   360
                {
madarasip@1141
   361
                  addPair(order[_depth],currNode);
madarasip@1141
   362
                  break;
madarasip@1141
   363
                }
madarasip@1141
   364
            }
madarasip@1141
   365
          currEdgeIts[_depth]==INVALID?--_depth:++_depth;
madarasip@1141
   366
        }
madarasip@1141
   367
      return false;
madarasip@1141
   368
    }
madarasip@1141
   369
madarasip@1141
   370
    //calc. the lookup table for cut the searchtree
madarasip@1141
   371
    void setRNew1tRInOut1t()
madarasip@1141
   372
    {
madarasip@1141
   373
      typename G1::template NodeMap<int> tmp(_g1,0);
madarasip@1141
   374
      for(unsigned int i=0; i<order.size(); ++i)
madarasip@1141
   375
        {
madarasip@1141
   376
          tmp[order[i]]=-1;
madarasip@1141
   377
          for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1)
madarasip@1141
   378
            {
madarasip@1141
   379
              const typename G1::Node currNode=_g1.oppositeNode(order[i],e1);
madarasip@1141
   380
              if(tmp[currNode]>0)
madarasip@1141
   381
                ++rInOut1t[order[i]];
madarasip@1141
   382
              else if(tmp[currNode]==0)
madarasip@1141
   383
                ++rNew1t[order[i]];
madarasip@1141
   384
            }
madarasip@1141
   385
          for(typename G1::IncEdgeIt e1(_g1,order[i]); e1!=INVALID; ++e1)
madarasip@1141
   386
            {
madarasip@1141
   387
              const typename G1::Node currNode=_g1.oppositeNode(order[i],e1);
madarasip@1141
   388
              if(tmp[currNode]!=-1)
madarasip@1141
   389
                ++tmp[currNode];
madarasip@1141
   390
            }
madarasip@1141
   391
        }
madarasip@1141
   392
    }
madarasip@1141
   393
  public:
alpar@1142
   394
    ///Constructor
alpar@1142
   395
alpar@1142
   396
    ///Constructor
alpar@1142
   397
alpar@1142
   398
    ///\param g1 The graph to be embedded into \e g2.
alpar@1142
   399
    ///\param g2 The graph \e g1 will be embedded into.
alpar@1142
   400
    ///\param m \ref concepts::ReadWriteMap "read-write" NodeMap
alpar@1142
   401
    ///storing the found mapping.
alpar@1142
   402
    ///\param neq A bool-valued binary functor determinining whether a node is
alpar@1142
   403
    ///mappable to another. By default it is an always true operator.
alpar@1142
   404
    Vf2(const G1 &g1, const G2 &g2,M &m, const NEQ &neq = NEQ() ) :
alpar@1142
   405
      _nEq(neq), _conn(g2,0), _mapping(m), order(countNodes(g1)),
madarasip@1141
   406
      currEdgeIts(countNodes(g1),INVALID), _g1(g1), _g2(g2), rNew1t(g1,0),
madarasip@1141
   407
      rInOut1t(g1,0), _mapping_type(SUBGRAPH)
madarasip@1141
   408
    {
madarasip@1141
   409
      _depth=0;
madarasip@1141
   410
      setOrder();
madarasip@1141
   411
      setRNew1tRInOut1t();
alpar@1143
   412
      for(typename G1::NodeIt n(g1);n!=INVALID;++n)
alpar@1143
   413
        m[n]=INVALID;
madarasip@1141
   414
    }
madarasip@1141
   415
alpar@1142
   416
    ///Returns the current mapping type
madarasip@1141
   417
alpar@1142
   418
    ///Returns the current mapping type
alpar@1142
   419
    ///
alpar@1142
   420
    Vf2MappingType mappingType() const { return _mapping_type; }
alpar@1142
   421
    ///Sets mapping type
alpar@1142
   422
alpar@1142
   423
    ///Sets mapping type.
alpar@1142
   424
    ///
alpar@1142
   425
    ///The mapping type is set to \ref SUBGRAPH by default.
alpar@1142
   426
    ///
alpar@1144
   427
    ///\sa See \ref Vf2MappingType for the possible values.
alpar@1142
   428
    void mappingType(Vf2MappingType m_type) { _mapping_type = m_type; }
alpar@1142
   429
kpeter@1152
   430
    ///Finds a mapping
alpar@1142
   431
alpar@1142
   432
    ///It finds a mapping between from g1 into g2 according to the mapping
alpar@1142
   433
    ///type set by \ref mappingType(Vf2MappingType) "mappingType()".
alpar@1142
   434
    ///
alpar@1142
   435
    ///By subsequent calls, it returns all possible mappings one-by-one.
alpar@1142
   436
    ///
alpar@1142
   437
    ///\retval true if a mapping is found.
alpar@1142
   438
    ///\retval false if there is no (more) mapping.
madarasip@1141
   439
    bool find()
madarasip@1141
   440
    {
madarasip@1141
   441
      switch(_mapping_type)
madarasip@1141
   442
        {
madarasip@1141
   443
        case SUBGRAPH:
madarasip@1141
   444
          return extMatch<SUBGRAPH>();
madarasip@1141
   445
        case INDUCED:
madarasip@1141
   446
          return extMatch<INDUCED>();
madarasip@1141
   447
        case ISOMORPH:
madarasip@1141
   448
          return extMatch<ISOMORPH>();
madarasip@1141
   449
        default:
madarasip@1141
   450
          return false;
madarasip@1141
   451
        }
madarasip@1141
   452
    }
madarasip@1141
   453
  };
madarasip@1141
   454
madarasip@1141
   455
  template<class G1, class G2>
madarasip@1141
   456
  class Vf2WizardBase
madarasip@1141
   457
  {
madarasip@1141
   458
  protected:
madarasip@1141
   459
    typedef G1 Graph1;
madarasip@1141
   460
    typedef G2 Graph2;
madarasip@1141
   461
madarasip@1141
   462
    const G1 &_g1;
madarasip@1141
   463
    const G2 &_g2;
madarasip@1141
   464
alpar@1142
   465
    Vf2MappingType _mapping_type;
madarasip@1141
   466
madarasip@1141
   467
    typedef typename G1::template NodeMap<typename G2::Node> Mapping;
madarasip@1141
   468
    bool _local_mapping;
madarasip@1141
   469
    void *_mapping;
madarasip@1141
   470
    void createMapping()
madarasip@1141
   471
    {
madarasip@1141
   472
      _mapping = new Mapping(_g1);
madarasip@1141
   473
    }
madarasip@1141
   474
madarasip@1141
   475
    typedef bits::vf2::AlwaysEq NodeEq;
madarasip@1141
   476
    NodeEq _node_eq;
madarasip@1141
   477
madarasip@1141
   478
    Vf2WizardBase(const G1 &g1,const G2 &g2)
madarasip@1141
   479
      : _g1(g1), _g2(g2), _mapping_type(SUBGRAPH), _local_mapping(true) {}
madarasip@1141
   480
  };
madarasip@1141
   481
alpar@1142
   482
  /// Auxiliary class for the function-type interface of %VF2 algorithm.
alpar@1142
   483
alpar@1142
   484
  /// This auxiliary class implements the named parameters of
alpar@1142
   485
  /// \ref vf2() "function-type interface" of \ref Vf2 algorithm.
alpar@1142
   486
  ///
alpar@1142
   487
  /// \warning This class should only be used through the function \ref vf2().
alpar@1142
   488
  ///
alpar@1142
   489
  /// \tparam TR The traits class that defines various types used by the
alpar@1142
   490
  /// algorithm.
madarasip@1141
   491
  template<class TR>
madarasip@1141
   492
  class Vf2Wizard : public TR
madarasip@1141
   493
  {
madarasip@1141
   494
    typedef TR Base;
madarasip@1141
   495
    typedef typename TR::Graph1 Graph1;
madarasip@1141
   496
    typedef typename TR::Graph2 Graph2;
madarasip@1141
   497
madarasip@1141
   498
    typedef typename TR::Mapping Mapping;
madarasip@1141
   499
    typedef typename TR::NodeEq NodeEq;
madarasip@1141
   500
madarasip@1141
   501
    using TR::_g1;
madarasip@1141
   502
    using TR::_g2;
madarasip@1141
   503
    using TR::_mapping_type;
madarasip@1141
   504
    using TR::_mapping;
madarasip@1141
   505
    using TR::_node_eq;
madarasip@1141
   506
madarasip@1141
   507
  public:
alpar@1142
   508
    ///Constructor
madarasip@1141
   509
    Vf2Wizard(const Graph1 &g1,const Graph2 &g2) : Base(g1,g2) {}
madarasip@1141
   510
madarasip@1141
   511
    ///Copy constructor
madarasip@1141
   512
    Vf2Wizard(const Base &b) : Base(b) {}
madarasip@1141
   513
madarasip@1141
   514
madarasip@1141
   515
    template<class T>
madarasip@1141
   516
    struct SetMappingBase : public Base {
madarasip@1141
   517
      typedef T Mapping;
madarasip@1141
   518
      SetMappingBase(const Base &b) : Base(b) {}
madarasip@1141
   519
    };
madarasip@1141
   520
madarasip@1141
   521
    ///\brief \ref named-templ-param "Named parameter" for setting
madarasip@1141
   522
    ///the mapping.
madarasip@1141
   523
    ///
madarasip@1141
   524
    ///\ref named-templ-param "Named parameter" function for setting
madarasip@1141
   525
    ///the map that stores the found embedding.
madarasip@1141
   526
    template<class T>
madarasip@1141
   527
    Vf2Wizard< SetMappingBase<T> > mapping(const T &t)
madarasip@1141
   528
    {
madarasip@1141
   529
      Base::_mapping=reinterpret_cast<void*>(const_cast<T*>(&t));
madarasip@1141
   530
      Base::_local_mapping = false;
madarasip@1141
   531
      return Vf2Wizard<SetMappingBase<T> >(*this);
madarasip@1141
   532
    }
madarasip@1141
   533
madarasip@1141
   534
    template<class NE>
madarasip@1141
   535
    struct SetNodeEqBase : public Base {
madarasip@1141
   536
      typedef NE NodeEq;
madarasip@1141
   537
      NodeEq _node_eq;
madarasip@1141
   538
      SetNodeEqBase(const Base &b, const NE &node_eq)
madarasip@1141
   539
        : Base(b), _node_eq(node_eq) {}
madarasip@1141
   540
    };
madarasip@1141
   541
madarasip@1141
   542
    ///\brief \ref named-templ-param "Named parameter" for setting
madarasip@1141
   543
    ///the node equivalence relation.
madarasip@1141
   544
    ///
madarasip@1141
   545
    ///\ref named-templ-param "Named parameter" function for setting
madarasip@1141
   546
    ///the equivalence relation between the nodes.
alpar@1142
   547
    ///
alpar@1142
   548
    ///\param node_eq A bool-valued binary functor determinining
alpar@1142
   549
    ///whether a node is mappable to another. By default it is an
alpar@1142
   550
    ///always true operator.
madarasip@1141
   551
    template<class T>
madarasip@1141
   552
    Vf2Wizard< SetNodeEqBase<T> > nodeEq(const T &node_eq)
madarasip@1141
   553
    {
madarasip@1141
   554
      return Vf2Wizard<SetNodeEqBase<T> >(SetNodeEqBase<T>(*this,node_eq));
madarasip@1141
   555
    }
madarasip@1141
   556
madarasip@1141
   557
    ///\brief \ref named-templ-param "Named parameter" for setting
madarasip@1141
   558
    ///the node labels.
madarasip@1141
   559
    ///
madarasip@1141
   560
    ///\ref named-templ-param "Named parameter" function for setting
madarasip@1141
   561
    ///the node labels defining equivalence relation between them.
alpar@1142
   562
    ///
alpar@1144
   563
    ///\param m1 It is arbitrary \ref concepts::ReadMap "readable node map"
alpar@1144
   564
    ///of g1.
alpar@1144
   565
    ///\param m2 It is arbitrary \ref concepts::ReadMap "readable node map"
alpar@1144
   566
    ///of g2.
alpar@1142
   567
    ///
alpar@1142
   568
    ///The value type of these maps must be equal comparable.
madarasip@1141
   569
    template<class M1, class M2>
madarasip@1141
   570
    Vf2Wizard< SetNodeEqBase<bits::vf2::MapEq<M1,M2> > >
madarasip@1141
   571
    nodeLabels(const M1 &m1,const M2 &m2)
madarasip@1141
   572
    {
madarasip@1141
   573
      return nodeEq(bits::vf2::MapEq<M1,M2>(m1,m2));
madarasip@1141
   574
    }
madarasip@1141
   575
alpar@1142
   576
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@1142
   577
    ///the mapping type.
alpar@1142
   578
    ///
alpar@1142
   579
    ///\ref named-templ-param "Named parameter" for setting
alpar@1142
   580
    ///the mapping type.
alpar@1142
   581
    ///
alpar@1142
   582
    ///The mapping type is set to \ref SUBGRAPH by default.
alpar@1142
   583
    ///
alpar@1144
   584
    ///\sa See \ref Vf2MappingType for the possible values.
alpar@1142
   585
    Vf2Wizard<Base> &mappingType(Vf2MappingType m_type)
madarasip@1141
   586
    {
madarasip@1141
   587
      _mapping_type = m_type;
madarasip@1141
   588
      return *this;
madarasip@1141
   589
    }
madarasip@1141
   590
alpar@1142
   591
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@1142
   592
    ///the mapping type to \ref INDUCED.
alpar@1142
   593
    ///
alpar@1142
   594
    ///\ref named-templ-param "Named parameter" for setting
alpar@1142
   595
    ///the mapping type to \ref INDUCED.
madarasip@1141
   596
    Vf2Wizard<Base> &induced()
madarasip@1141
   597
    {
madarasip@1141
   598
      _mapping_type = INDUCED;
madarasip@1141
   599
      return *this;
madarasip@1141
   600
    }
madarasip@1141
   601
alpar@1142
   602
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@1142
   603
    ///the mapping type to \ref ISOMORPH.
alpar@1142
   604
    ///
alpar@1142
   605
    ///\ref named-templ-param "Named parameter" for setting
alpar@1142
   606
    ///the mapping type to \ref ISOMORPH.
madarasip@1141
   607
    Vf2Wizard<Base> &iso()
madarasip@1141
   608
    {
madarasip@1141
   609
      _mapping_type = ISOMORPH;
madarasip@1141
   610
      return *this;
madarasip@1141
   611
    }
madarasip@1141
   612
alpar@1142
   613
    ///Runs VF2 algorithm.
alpar@1142
   614
alpar@1142
   615
    ///This method runs VF2 algorithm.
alpar@1142
   616
    ///
alpar@1142
   617
    ///\retval true if a mapping is found.
alpar@1142
   618
    ///\retval false if there is no (more) mapping.
madarasip@1141
   619
    bool run()
madarasip@1141
   620
    {
madarasip@1141
   621
      if(Base::_local_mapping)
madarasip@1141
   622
        Base::createMapping();
madarasip@1141
   623
madarasip@1141
   624
      Vf2<Graph1, Graph2, Mapping, NodeEq >
madarasip@1141
   625
        alg(_g1, _g2, *reinterpret_cast<Mapping*>(_mapping), _node_eq);
madarasip@1141
   626
madarasip@1141
   627
      alg.mappingType(_mapping_type);
madarasip@1141
   628
madarasip@1141
   629
      bool ret = alg.find();
madarasip@1141
   630
madarasip@1141
   631
      if(Base::_local_mapping)
madarasip@1141
   632
        delete reinterpret_cast<Mapping*>(_mapping);
madarasip@1141
   633
madarasip@1141
   634
      return ret;
madarasip@1141
   635
    }
madarasip@1141
   636
  };
madarasip@1141
   637
alpar@1142
   638
  ///Function-type interface for VF2 algorithm.
alpar@1142
   639
alpar@1142
   640
  /// \ingroup graph_isomorphism
alpar@1142
   641
  ///Function-type interface for VF2 algorithm \cite cordella2004sub.
alpar@1142
   642
  ///
alpar@1142
   643
  ///This function has several \ref named-func-param "named parameters"
alpar@1142
   644
  ///declared as the members of class \ref Vf2Wizard.
alpar@1142
   645
  ///The following examples show how to use these parameters.
alpar@1142
   646
  ///\code
alpar@1142
   647
  ///  // Find an embedding of graph g into graph h
alpar@1142
   648
  ///  ListGraph::NodeMap<ListGraph::Node> m(g);
alpar@1142
   649
  ///  vf2(g,h).mapping(m).run();
alpar@1142
   650
  ///
alpar@1142
   651
  ///  // Check whether graphs g and h are isomorphic
alpar@1142
   652
  ///  bool is_iso = vf2(g,h).iso().run();
alpar@1142
   653
  ///\endcode
alpar@1142
   654
  ///\warning Don't forget to put the \ref Vf2Wizard::run() "run()"
alpar@1142
   655
  ///to the end of the expression.
alpar@1142
   656
  ///\sa Vf2Wizard
alpar@1142
   657
  ///\sa Vf2
madarasip@1141
   658
  template<class G1, class G2>
madarasip@1141
   659
  Vf2Wizard<Vf2WizardBase<G1,G2> > vf2(const G1 &g1, const G2 &g2)
madarasip@1141
   660
  {
madarasip@1141
   661
    return Vf2Wizard<Vf2WizardBase<G1,G2> >(g1,g2);
madarasip@1141
   662
  }
madarasip@1141
   663
madarasip@1141
   664
}
madarasip@1141
   665
madarasip@1141
   666
#endif