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