lemon/dfs.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 17 Nov 2008 15:41:15 +0000
changeset 379 1bab3a47be88
parent 313 64f8f7cc6168
child 405 6b9057cdcd8b
child 906 e24922c56bc2
permissions -rw-r--r--
Port Elevator from svn -r3516 (#174)
- the unify script hes also been applied
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@100
     4
 *
alpar@100
     5
 * Copyright (C) 2003-2008
alpar@100
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100
     8
 *
alpar@100
     9
 * Permission to use, modify and distribute this software is granted
alpar@100
    10
 * provided that this copyright notice appears in all copies. For
alpar@100
    11
 * precise terms see the accompanying LICENSE file.
alpar@100
    12
 *
alpar@100
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@100
    14
 * express or implied, and with no claim as to its suitability for any
alpar@100
    15
 * purpose.
alpar@100
    16
 *
alpar@100
    17
 */
alpar@100
    18
alpar@100
    19
#ifndef LEMON_DFS_H
alpar@100
    20
#define LEMON_DFS_H
alpar@100
    21
alpar@100
    22
///\ingroup search
alpar@100
    23
///\file
kpeter@244
    24
///\brief DFS algorithm.
alpar@100
    25
alpar@100
    26
#include <lemon/list_graph.h>
alpar@100
    27
#include <lemon/bits/path_dump.h>
deba@220
    28
#include <lemon/core.h>
alpar@100
    29
#include <lemon/error.h>
alpar@100
    30
#include <lemon/maps.h>
kpeter@278
    31
#include <lemon/path.h>
alpar@100
    32
alpar@100
    33
namespace lemon {
alpar@100
    34
alpar@100
    35
  ///Default traits class of Dfs class.
alpar@100
    36
alpar@100
    37
  ///Default traits class of Dfs class.
kpeter@157
    38
  ///\tparam GR Digraph type.
alpar@100
    39
  template<class GR>
alpar@100
    40
  struct DfsDefaultTraits
alpar@100
    41
  {
kpeter@244
    42
    ///The type of the digraph the algorithm runs on.
alpar@100
    43
    typedef GR Digraph;
kpeter@244
    44
kpeter@244
    45
    ///\brief The type of the map that stores the predecessor
alpar@100
    46
    ///arcs of the %DFS paths.
alpar@209
    47
    ///
kpeter@244
    48
    ///The type of the map that stores the predecessor
alpar@100
    49
    ///arcs of the %DFS paths.
alpar@100
    50
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
kpeter@244
    51
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@301
    52
    ///Instantiates a PredMap.
alpar@209
    53
kpeter@301
    54
    ///This function instantiates a PredMap.
kpeter@244
    55
    ///\param g is the digraph, to which we would like to define the
kpeter@301
    56
    ///PredMap.
kpeter@244
    57
    static PredMap *createPredMap(const Digraph &g)
alpar@100
    58
    {
kpeter@244
    59
      return new PredMap(g);
alpar@100
    60
    }
alpar@100
    61
alpar@100
    62
    ///The type of the map that indicates which nodes are processed.
alpar@209
    63
alpar@100
    64
    ///The type of the map that indicates which nodes are processed.
alpar@100
    65
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
    66
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@301
    67
    ///Instantiates a ProcessedMap.
alpar@209
    68
kpeter@301
    69
    ///This function instantiates a ProcessedMap.
alpar@100
    70
    ///\param g is the digraph, to which
kpeter@301
    71
    ///we would like to define the ProcessedMap
alpar@100
    72
#ifdef DOXYGEN
kpeter@244
    73
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
    74
#else
kpeter@244
    75
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
    76
#endif
alpar@100
    77
    {
alpar@100
    78
      return new ProcessedMap();
alpar@100
    79
    }
kpeter@244
    80
alpar@100
    81
    ///The type of the map that indicates which nodes are reached.
alpar@209
    82
alpar@100
    83
    ///The type of the map that indicates which nodes are reached.
kpeter@244
    84
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
    85
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
kpeter@301
    86
    ///Instantiates a ReachedMap.
alpar@209
    87
kpeter@301
    88
    ///This function instantiates a ReachedMap.
kpeter@244
    89
    ///\param g is the digraph, to which
kpeter@301
    90
    ///we would like to define the ReachedMap.
kpeter@244
    91
    static ReachedMap *createReachedMap(const Digraph &g)
alpar@100
    92
    {
kpeter@244
    93
      return new ReachedMap(g);
alpar@100
    94
    }
alpar@209
    95
kpeter@244
    96
    ///The type of the map that stores the distances of the nodes.
kpeter@244
    97
kpeter@244
    98
    ///The type of the map that stores the distances of the nodes.
alpar@100
    99
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   100
    typedef typename Digraph::template NodeMap<int> DistMap;
kpeter@301
   101
    ///Instantiates a DistMap.
alpar@209
   102
kpeter@301
   103
    ///This function instantiates a DistMap.
kpeter@244
   104
    ///\param g is the digraph, to which we would like to define the
kpeter@301
   105
    ///DistMap.
kpeter@244
   106
    static DistMap *createDistMap(const Digraph &g)
alpar@100
   107
    {
kpeter@244
   108
      return new DistMap(g);
alpar@100
   109
    }
alpar@100
   110
  };
alpar@209
   111
alpar@100
   112
  ///%DFS algorithm class.
alpar@209
   113
alpar@100
   114
  ///\ingroup search
alpar@100
   115
  ///This class provides an efficient implementation of the %DFS algorithm.
alpar@100
   116
  ///
kpeter@278
   117
  ///There is also a \ref dfs() "function-type interface" for the DFS
kpeter@244
   118
  ///algorithm, which is convenient in the simplier cases and it can be
kpeter@244
   119
  ///used easier.
kpeter@244
   120
  ///
kpeter@244
   121
  ///\tparam GR The type of the digraph the algorithm runs on.
kpeter@244
   122
  ///The default value is \ref ListDigraph. The value of GR is not used
kpeter@244
   123
  ///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits.
kpeter@157
   124
  ///\tparam TR Traits class to set various data types used by the algorithm.
alpar@100
   125
  ///The default traits class is
alpar@100
   126
  ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
alpar@100
   127
  ///See \ref DfsDefaultTraits for the documentation of
alpar@100
   128
  ///a Dfs traits class.
alpar@100
   129
#ifdef DOXYGEN
alpar@100
   130
  template <typename GR,
alpar@209
   131
            typename TR>
alpar@100
   132
#else
alpar@100
   133
  template <typename GR=ListDigraph,
alpar@209
   134
            typename TR=DfsDefaultTraits<GR> >
alpar@100
   135
#endif
alpar@100
   136
  class Dfs {
alpar@100
   137
  public:
alpar@100
   138
kpeter@244
   139
    ///The type of the digraph the algorithm runs on.
kpeter@244
   140
    typedef typename TR::Digraph Digraph;
kpeter@244
   141
kpeter@244
   142
    ///\brief The type of the map that stores the predecessor arcs of the
kpeter@244
   143
    ///DFS paths.
kpeter@244
   144
    typedef typename TR::PredMap PredMap;
kpeter@244
   145
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   146
    typedef typename TR::DistMap DistMap;
kpeter@244
   147
    ///The type of the map that indicates which nodes are reached.
kpeter@244
   148
    typedef typename TR::ReachedMap ReachedMap;
kpeter@244
   149
    ///The type of the map that indicates which nodes are processed.
kpeter@244
   150
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@244
   151
    ///The type of the paths.
kpeter@244
   152
    typedef PredMapPath<Digraph, PredMap> Path;
kpeter@244
   153
kpeter@244
   154
    ///The traits class.
alpar@100
   155
    typedef TR Traits;
kpeter@244
   156
kpeter@244
   157
  private:
kpeter@244
   158
alpar@100
   159
    typedef typename Digraph::Node Node;
alpar@100
   160
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   161
    typedef typename Digraph::Arc Arc;
alpar@100
   162
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@209
   163
kpeter@244
   164
    //Pointer to the underlying digraph.
alpar@100
   165
    const Digraph *G;
kpeter@244
   166
    //Pointer to the map of predecessor arcs.
alpar@100
   167
    PredMap *_pred;
kpeter@244
   168
    //Indicates if _pred is locally allocated (true) or not.
alpar@100
   169
    bool local_pred;
kpeter@244
   170
    //Pointer to the map of distances.
alpar@100
   171
    DistMap *_dist;
kpeter@244
   172
    //Indicates if _dist is locally allocated (true) or not.
alpar@100
   173
    bool local_dist;
kpeter@244
   174
    //Pointer to the map of reached status of the nodes.
alpar@100
   175
    ReachedMap *_reached;
kpeter@244
   176
    //Indicates if _reached is locally allocated (true) or not.
alpar@100
   177
    bool local_reached;
kpeter@244
   178
    //Pointer to the map of processed status of the nodes.
alpar@100
   179
    ProcessedMap *_processed;
kpeter@244
   180
    //Indicates if _processed is locally allocated (true) or not.
alpar@100
   181
    bool local_processed;
alpar@100
   182
alpar@100
   183
    std::vector<typename Digraph::OutArcIt> _stack;
alpar@100
   184
    int _stack_head;
alpar@100
   185
alpar@280
   186
    //Creates the maps if necessary.
alpar@209
   187
    void create_maps()
alpar@100
   188
    {
alpar@100
   189
      if(!_pred) {
alpar@209
   190
        local_pred = true;
alpar@209
   191
        _pred = Traits::createPredMap(*G);
alpar@100
   192
      }
alpar@100
   193
      if(!_dist) {
alpar@209
   194
        local_dist = true;
alpar@209
   195
        _dist = Traits::createDistMap(*G);
alpar@100
   196
      }
alpar@100
   197
      if(!_reached) {
alpar@209
   198
        local_reached = true;
alpar@209
   199
        _reached = Traits::createReachedMap(*G);
alpar@100
   200
      }
alpar@100
   201
      if(!_processed) {
alpar@209
   202
        local_processed = true;
alpar@209
   203
        _processed = Traits::createProcessedMap(*G);
alpar@100
   204
      }
alpar@100
   205
    }
alpar@100
   206
alpar@100
   207
  protected:
alpar@100
   208
alpar@100
   209
    Dfs() {}
alpar@209
   210
alpar@100
   211
  public:
alpar@100
   212
alpar@100
   213
    typedef Dfs Create;
alpar@100
   214
alpar@100
   215
    ///\name Named template parameters
alpar@100
   216
alpar@100
   217
    ///@{
alpar@100
   218
alpar@100
   219
    template <class T>
kpeter@257
   220
    struct SetPredMapTraits : public Traits {
alpar@100
   221
      typedef T PredMap;
kpeter@244
   222
      static PredMap *createPredMap(const Digraph &)
alpar@100
   223
      {
deba@290
   224
        LEMON_ASSERT(false, "PredMap is not initialized");
deba@290
   225
        return 0; // ignore warnings
alpar@100
   226
      }
alpar@100
   227
    };
alpar@100
   228
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   229
    ///PredMap type.
alpar@100
   230
    ///
kpeter@244
   231
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   232
    ///PredMap type.
alpar@100
   233
    template <class T>
kpeter@257
   234
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
kpeter@257
   235
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
alpar@100
   236
    };
alpar@209
   237
alpar@100
   238
    template <class T>
kpeter@257
   239
    struct SetDistMapTraits : public Traits {
alpar@100
   240
      typedef T DistMap;
alpar@209
   241
      static DistMap *createDistMap(const Digraph &)
alpar@100
   242
      {
deba@290
   243
        LEMON_ASSERT(false, "DistMap is not initialized");
deba@290
   244
        return 0; // ignore warnings
alpar@100
   245
      }
alpar@100
   246
    };
alpar@100
   247
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   248
    ///DistMap type.
alpar@100
   249
    ///
kpeter@244
   250
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   251
    ///DistMap type.
alpar@100
   252
    template <class T>
kpeter@257
   253
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
kpeter@257
   254
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
alpar@100
   255
    };
alpar@209
   256
alpar@100
   257
    template <class T>
kpeter@257
   258
    struct SetReachedMapTraits : public Traits {
alpar@100
   259
      typedef T ReachedMap;
alpar@209
   260
      static ReachedMap *createReachedMap(const Digraph &)
alpar@100
   261
      {
deba@290
   262
        LEMON_ASSERT(false, "ReachedMap is not initialized");
deba@290
   263
        return 0; // ignore warnings
alpar@100
   264
      }
alpar@100
   265
    };
alpar@100
   266
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   267
    ///ReachedMap type.
alpar@100
   268
    ///
kpeter@244
   269
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   270
    ///ReachedMap type.
alpar@100
   271
    template <class T>
kpeter@257
   272
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
kpeter@257
   273
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
alpar@100
   274
    };
alpar@100
   275
alpar@100
   276
    template <class T>
kpeter@257
   277
    struct SetProcessedMapTraits : public Traits {
alpar@100
   278
      typedef T ProcessedMap;
alpar@209
   279
      static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   280
      {
deba@290
   281
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
deba@290
   282
        return 0; // ignore warnings
alpar@100
   283
      }
alpar@100
   284
    };
alpar@100
   285
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   286
    ///ProcessedMap type.
alpar@100
   287
    ///
kpeter@244
   288
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   289
    ///ProcessedMap type.
alpar@100
   290
    template <class T>
kpeter@257
   291
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
kpeter@257
   292
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
alpar@100
   293
    };
alpar@209
   294
kpeter@257
   295
    struct SetStandardProcessedMapTraits : public Traits {
alpar@100
   296
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
kpeter@244
   297
      static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   298
      {
kpeter@244
   299
        return new ProcessedMap(g);
alpar@100
   300
      }
alpar@100
   301
    };
kpeter@244
   302
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@301
   303
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
alpar@100
   304
    ///
kpeter@244
   305
    ///\ref named-templ-param "Named parameter" for setting
kpeter@301
   306
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
kpeter@244
   307
    ///If you don't set it explicitly, it will be automatically allocated.
kpeter@257
   308
    struct SetStandardProcessedMap :
kpeter@257
   309
      public Dfs< Digraph, SetStandardProcessedMapTraits > {
kpeter@257
   310
      typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
alpar@100
   311
    };
alpar@209
   312
alpar@100
   313
    ///@}
alpar@100
   314
alpar@209
   315
  public:
alpar@209
   316
alpar@100
   317
    ///Constructor.
alpar@209
   318
kpeter@244
   319
    ///Constructor.
kpeter@244
   320
    ///\param g The digraph the algorithm runs on.
kpeter@244
   321
    Dfs(const Digraph &g) :
kpeter@244
   322
      G(&g),
alpar@100
   323
      _pred(NULL), local_pred(false),
alpar@100
   324
      _dist(NULL), local_dist(false),
alpar@100
   325
      _reached(NULL), local_reached(false),
alpar@100
   326
      _processed(NULL), local_processed(false)
alpar@100
   327
    { }
alpar@209
   328
alpar@100
   329
    ///Destructor.
alpar@209
   330
    ~Dfs()
alpar@100
   331
    {
alpar@100
   332
      if(local_pred) delete _pred;
alpar@100
   333
      if(local_dist) delete _dist;
alpar@100
   334
      if(local_reached) delete _reached;
alpar@100
   335
      if(local_processed) delete _processed;
alpar@100
   336
    }
alpar@100
   337
kpeter@244
   338
    ///Sets the map that stores the predecessor arcs.
alpar@100
   339
kpeter@244
   340
    ///Sets the map that stores the predecessor arcs.
alpar@100
   341
    ///If you don't use this function before calling \ref run(),
kpeter@244
   342
    ///it will allocate one. The destructor deallocates this
alpar@100
   343
    ///automatically allocated map, of course.
alpar@100
   344
    ///\return <tt> (*this) </tt>
alpar@209
   345
    Dfs &predMap(PredMap &m)
alpar@100
   346
    {
alpar@100
   347
      if(local_pred) {
alpar@209
   348
        delete _pred;
alpar@209
   349
        local_pred=false;
alpar@100
   350
      }
alpar@100
   351
      _pred = &m;
alpar@100
   352
      return *this;
alpar@100
   353
    }
alpar@100
   354
kpeter@244
   355
    ///Sets the map that indicates which nodes are reached.
alpar@100
   356
kpeter@244
   357
    ///Sets the map that indicates which nodes are reached.
alpar@100
   358
    ///If you don't use this function before calling \ref run(),
kpeter@244
   359
    ///it will allocate one. The destructor deallocates this
kpeter@244
   360
    ///automatically allocated map, of course.
kpeter@244
   361
    ///\return <tt> (*this) </tt>
kpeter@244
   362
    Dfs &reachedMap(ReachedMap &m)
kpeter@244
   363
    {
kpeter@244
   364
      if(local_reached) {
kpeter@244
   365
        delete _reached;
kpeter@244
   366
        local_reached=false;
kpeter@244
   367
      }
kpeter@244
   368
      _reached = &m;
kpeter@244
   369
      return *this;
kpeter@244
   370
    }
kpeter@244
   371
kpeter@244
   372
    ///Sets the map that indicates which nodes are processed.
kpeter@244
   373
kpeter@244
   374
    ///Sets the map that indicates which nodes are processed.
kpeter@244
   375
    ///If you don't use this function before calling \ref run(),
kpeter@244
   376
    ///it will allocate one. The destructor deallocates this
kpeter@244
   377
    ///automatically allocated map, of course.
kpeter@244
   378
    ///\return <tt> (*this) </tt>
kpeter@244
   379
    Dfs &processedMap(ProcessedMap &m)
kpeter@244
   380
    {
kpeter@244
   381
      if(local_processed) {
kpeter@244
   382
        delete _processed;
kpeter@244
   383
        local_processed=false;
kpeter@244
   384
      }
kpeter@244
   385
      _processed = &m;
kpeter@244
   386
      return *this;
kpeter@244
   387
    }
kpeter@244
   388
kpeter@244
   389
    ///Sets the map that stores the distances of the nodes.
kpeter@244
   390
kpeter@244
   391
    ///Sets the map that stores the distances of the nodes calculated by
kpeter@244
   392
    ///the algorithm.
kpeter@244
   393
    ///If you don't use this function before calling \ref run(),
kpeter@244
   394
    ///it will allocate one. The destructor deallocates this
alpar@100
   395
    ///automatically allocated map, of course.
alpar@100
   396
    ///\return <tt> (*this) </tt>
alpar@209
   397
    Dfs &distMap(DistMap &m)
alpar@100
   398
    {
alpar@100
   399
      if(local_dist) {
alpar@209
   400
        delete _dist;
alpar@209
   401
        local_dist=false;
alpar@100
   402
      }
alpar@100
   403
      _dist = &m;
alpar@100
   404
      return *this;
alpar@100
   405
    }
alpar@100
   406
kpeter@244
   407
  public:
alpar@100
   408
alpar@100
   409
    ///\name Execution control
alpar@100
   410
    ///The simplest way to execute the algorithm is to use
kpeter@244
   411
    ///one of the member functions called \ref lemon::Dfs::run() "run()".
alpar@100
   412
    ///\n
kpeter@244
   413
    ///If you need more control on the execution, first you must call
kpeter@244
   414
    ///\ref lemon::Dfs::init() "init()", then you can add a source node
kpeter@244
   415
    ///with \ref lemon::Dfs::addSource() "addSource()".
kpeter@244
   416
    ///Finally \ref lemon::Dfs::start() "start()" will perform the
kpeter@244
   417
    ///actual path computation.
alpar@100
   418
alpar@100
   419
    ///@{
alpar@100
   420
alpar@100
   421
    ///Initializes the internal data structures.
alpar@100
   422
alpar@100
   423
    ///Initializes the internal data structures.
alpar@100
   424
    ///
alpar@100
   425
    void init()
alpar@100
   426
    {
alpar@100
   427
      create_maps();
alpar@100
   428
      _stack.resize(countNodes(*G));
alpar@100
   429
      _stack_head=-1;
alpar@100
   430
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@209
   431
        _pred->set(u,INVALID);
alpar@209
   432
        _reached->set(u,false);
alpar@209
   433
        _processed->set(u,false);
alpar@100
   434
      }
alpar@100
   435
    }
alpar@209
   436
alpar@100
   437
    ///Adds a new source node.
alpar@100
   438
alpar@100
   439
    ///Adds a new source node to the set of nodes to be processed.
alpar@100
   440
    ///
kpeter@244
   441
    ///\pre The stack must be empty. (Otherwise the algorithm gives
kpeter@244
   442
    ///false results.)
kpeter@244
   443
    ///
kpeter@244
   444
    ///\warning Distances will be wrong (or at least strange) in case of
kpeter@244
   445
    ///multiple sources.
alpar@100
   446
    void addSource(Node s)
alpar@100
   447
    {
kpeter@244
   448
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
alpar@100
   449
      if(!(*_reached)[s])
alpar@209
   450
        {
alpar@209
   451
          _reached->set(s,true);
alpar@209
   452
          _pred->set(s,INVALID);
alpar@209
   453
          OutArcIt e(*G,s);
alpar@209
   454
          if(e!=INVALID) {
alpar@209
   455
            _stack[++_stack_head]=e;
alpar@209
   456
            _dist->set(s,_stack_head);
alpar@209
   457
          }
alpar@209
   458
          else {
alpar@209
   459
            _processed->set(s,true);
alpar@209
   460
            _dist->set(s,0);
alpar@209
   461
          }
alpar@209
   462
        }
alpar@100
   463
    }
alpar@209
   464
alpar@100
   465
    ///Processes the next arc.
alpar@100
   466
alpar@100
   467
    ///Processes the next arc.
alpar@100
   468
    ///
alpar@100
   469
    ///\return The processed arc.
alpar@100
   470
    ///
kpeter@244
   471
    ///\pre The stack must not be empty.
alpar@100
   472
    Arc processNextArc()
alpar@209
   473
    {
alpar@100
   474
      Node m;
alpar@100
   475
      Arc e=_stack[_stack_head];
alpar@100
   476
      if(!(*_reached)[m=G->target(e)]) {
alpar@209
   477
        _pred->set(m,e);
alpar@209
   478
        _reached->set(m,true);
alpar@209
   479
        ++_stack_head;
alpar@209
   480
        _stack[_stack_head] = OutArcIt(*G, m);
alpar@209
   481
        _dist->set(m,_stack_head);
alpar@100
   482
      }
alpar@100
   483
      else {
alpar@209
   484
        m=G->source(e);
alpar@209
   485
        ++_stack[_stack_head];
alpar@100
   486
      }
alpar@100
   487
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
alpar@209
   488
        _processed->set(m,true);
alpar@209
   489
        --_stack_head;
alpar@209
   490
        if(_stack_head>=0) {
alpar@209
   491
          m=G->source(_stack[_stack_head]);
alpar@209
   492
          ++_stack[_stack_head];
alpar@209
   493
        }
alpar@100
   494
      }
alpar@100
   495
      return e;
alpar@100
   496
    }
kpeter@244
   497
alpar@100
   498
    ///Next arc to be processed.
alpar@100
   499
alpar@100
   500
    ///Next arc to be processed.
alpar@100
   501
    ///
kpeter@244
   502
    ///\return The next arc to be processed or \c INVALID if the stack
kpeter@244
   503
    ///is empty.
kpeter@244
   504
    OutArcIt nextArc() const
alpar@209
   505
    {
alpar@100
   506
      return _stack_head>=0?_stack[_stack_head]:INVALID;
alpar@100
   507
    }
alpar@100
   508
alpar@100
   509
    ///\brief Returns \c false if there are nodes
kpeter@244
   510
    ///to be processed.
alpar@100
   511
    ///
alpar@100
   512
    ///Returns \c false if there are nodes
kpeter@244
   513
    ///to be processed in the queue (stack).
kpeter@244
   514
    bool emptyQueue() const { return _stack_head<0; }
kpeter@244
   515
alpar@100
   516
    ///Returns the number of the nodes to be processed.
alpar@209
   517
kpeter@244
   518
    ///Returns the number of the nodes to be processed in the queue (stack).
kpeter@244
   519
    int queueSize() const { return _stack_head+1; }
alpar@209
   520
alpar@100
   521
    ///Executes the algorithm.
alpar@100
   522
alpar@100
   523
    ///Executes the algorithm.
alpar@100
   524
    ///
kpeter@244
   525
    ///This method runs the %DFS algorithm from the root node
kpeter@244
   526
    ///in order to compute the DFS path to each node.
alpar@100
   527
    ///
kpeter@244
   528
    /// The algorithm computes
kpeter@244
   529
    ///- the %DFS tree,
kpeter@244
   530
    ///- the distance of each node from the root in the %DFS tree.
alpar@100
   531
    ///
kpeter@244
   532
    ///\pre init() must be called and a root node should be
kpeter@244
   533
    ///added with addSource() before using this function.
kpeter@244
   534
    ///
kpeter@244
   535
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
kpeter@244
   536
    ///\code
kpeter@244
   537
    ///  while ( !d.emptyQueue() ) {
kpeter@244
   538
    ///    d.processNextArc();
kpeter@244
   539
    ///  }
kpeter@244
   540
    ///\endcode
alpar@100
   541
    void start()
alpar@100
   542
    {
alpar@100
   543
      while ( !emptyQueue() ) processNextArc();
alpar@100
   544
    }
alpar@209
   545
kpeter@244
   546
    ///Executes the algorithm until the given target node is reached.
alpar@100
   547
kpeter@244
   548
    ///Executes the algorithm until the given target node is reached.
alpar@100
   549
    ///
kpeter@244
   550
    ///This method runs the %DFS algorithm from the root node
kpeter@286
   551
    ///in order to compute the DFS path to \c t.
alpar@100
   552
    ///
kpeter@244
   553
    ///The algorithm computes
kpeter@286
   554
    ///- the %DFS path to \c t,
kpeter@286
   555
    ///- the distance of \c t from the root in the %DFS tree.
alpar@100
   556
    ///
kpeter@244
   557
    ///\pre init() must be called and a root node should be
kpeter@244
   558
    ///added with addSource() before using this function.
kpeter@286
   559
    void start(Node t)
alpar@100
   560
    {
kpeter@286
   561
      while ( !emptyQueue() && G->target(_stack[_stack_head])!=t )
alpar@209
   562
        processNextArc();
alpar@100
   563
    }
alpar@209
   564
alpar@100
   565
    ///Executes the algorithm until a condition is met.
alpar@100
   566
alpar@100
   567
    ///Executes the algorithm until a condition is met.
alpar@100
   568
    ///
kpeter@244
   569
    ///This method runs the %DFS algorithm from the root node
kpeter@244
   570
    ///until an arc \c a with <tt>am[a]</tt> true is found.
alpar@100
   571
    ///
kpeter@244
   572
    ///\param am A \c bool (or convertible) arc map. The algorithm
kpeter@244
   573
    ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
alpar@100
   574
    ///
kpeter@244
   575
    ///\return The reached arc \c a with <tt>am[a]</tt> true or
alpar@100
   576
    ///\c INVALID if no such arc was found.
alpar@100
   577
    ///
kpeter@244
   578
    ///\pre init() must be called and a root node should be
kpeter@244
   579
    ///added with addSource() before using this function.
kpeter@244
   580
    ///
kpeter@244
   581
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
alpar@100
   582
    ///not a node map.
kpeter@244
   583
    template<class ArcBoolMap>
kpeter@244
   584
    Arc start(const ArcBoolMap &am)
alpar@100
   585
    {
kpeter@244
   586
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
alpar@100
   587
        processNextArc();
alpar@100
   588
      return emptyQueue() ? INVALID : _stack[_stack_head];
alpar@100
   589
    }
alpar@100
   590
kpeter@286
   591
    ///Runs the algorithm from the given source node.
alpar@209
   592
kpeter@244
   593
    ///This method runs the %DFS algorithm from node \c s
kpeter@244
   594
    ///in order to compute the DFS path to each node.
alpar@100
   595
    ///
kpeter@244
   596
    ///The algorithm computes
kpeter@244
   597
    ///- the %DFS tree,
kpeter@244
   598
    ///- the distance of each node from the root in the %DFS tree.
kpeter@244
   599
    ///
kpeter@244
   600
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
alpar@100
   601
    ///\code
alpar@100
   602
    ///  d.init();
kpeter@244
   603
    ///  d.addSource(s);
kpeter@244
   604
    ///  d.start();
kpeter@244
   605
    ///\endcode
kpeter@244
   606
    void run(Node s) {
kpeter@244
   607
      init();
kpeter@244
   608
      addSource(s);
kpeter@244
   609
      start();
kpeter@244
   610
    }
kpeter@244
   611
kpeter@244
   612
    ///Finds the %DFS path between \c s and \c t.
kpeter@244
   613
kpeter@244
   614
    ///This method runs the %DFS algorithm from node \c s
kpeter@286
   615
    ///in order to compute the DFS path to node \c t
kpeter@286
   616
    ///(it stops searching when \c t is processed)
kpeter@244
   617
    ///
kpeter@286
   618
    ///\return \c true if \c t is reachable form \c s.
kpeter@244
   619
    ///
kpeter@244
   620
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
kpeter@244
   621
    ///just a shortcut of the following code.
kpeter@244
   622
    ///\code
kpeter@244
   623
    ///  d.init();
kpeter@244
   624
    ///  d.addSource(s);
kpeter@244
   625
    ///  d.start(t);
kpeter@244
   626
    ///\endcode
kpeter@286
   627
    bool run(Node s,Node t) {
kpeter@244
   628
      init();
kpeter@244
   629
      addSource(s);
kpeter@244
   630
      start(t);
kpeter@286
   631
      return reached(t);
kpeter@244
   632
    }
kpeter@244
   633
kpeter@244
   634
    ///Runs the algorithm to visit all nodes in the digraph.
kpeter@244
   635
kpeter@244
   636
    ///This method runs the %DFS algorithm in order to compute the
kpeter@244
   637
    ///%DFS path to each node.
kpeter@244
   638
    ///
kpeter@244
   639
    ///The algorithm computes
kpeter@244
   640
    ///- the %DFS tree,
kpeter@244
   641
    ///- the distance of each node from the root in the %DFS tree.
kpeter@244
   642
    ///
kpeter@244
   643
    ///\note <tt>d.run()</tt> is just a shortcut of the following code.
kpeter@244
   644
    ///\code
kpeter@244
   645
    ///  d.init();
kpeter@244
   646
    ///  for (NodeIt n(digraph); n != INVALID; ++n) {
kpeter@244
   647
    ///    if (!d.reached(n)) {
kpeter@244
   648
    ///      d.addSource(n);
alpar@100
   649
    ///      d.start();
alpar@100
   650
    ///    }
alpar@100
   651
    ///  }
alpar@100
   652
    ///\endcode
alpar@100
   653
    void run() {
alpar@100
   654
      init();
alpar@100
   655
      for (NodeIt it(*G); it != INVALID; ++it) {
alpar@100
   656
        if (!reached(it)) {
alpar@100
   657
          addSource(it);
alpar@100
   658
          start();
alpar@100
   659
        }
alpar@100
   660
      }
alpar@100
   661
    }
alpar@100
   662
alpar@100
   663
    ///@}
alpar@100
   664
alpar@100
   665
    ///\name Query Functions
alpar@100
   666
    ///The result of the %DFS algorithm can be obtained using these
alpar@100
   667
    ///functions.\n
kpeter@244
   668
    ///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start()
kpeter@244
   669
    ///"start()" must be called before using them.
alpar@209
   670
alpar@100
   671
    ///@{
alpar@100
   672
kpeter@244
   673
    ///The DFS path to a node.
alpar@100
   674
kpeter@244
   675
    ///Returns the DFS path to a node.
kpeter@244
   676
    ///
kpeter@244
   677
    ///\warning \c t should be reachable from the root.
kpeter@244
   678
    ///
kpeter@244
   679
    ///\pre Either \ref run() or \ref start() must be called before
kpeter@244
   680
    ///using this function.
kpeter@244
   681
    Path path(Node t) const { return Path(*G, *_pred, t); }
alpar@209
   682
kpeter@244
   683
    ///The distance of a node from the root.
alpar@100
   684
kpeter@244
   685
    ///Returns the distance of a node from the root.
kpeter@244
   686
    ///
kpeter@244
   687
    ///\warning If node \c v is not reachable from the root, then
kpeter@244
   688
    ///the return value of this function is undefined.
kpeter@244
   689
    ///
kpeter@244
   690
    ///\pre Either \ref run() or \ref start() must be called before
kpeter@244
   691
    ///using this function.
alpar@100
   692
    int dist(Node v) const { return (*_dist)[v]; }
alpar@100
   693
kpeter@244
   694
    ///Returns the 'previous arc' of the %DFS tree for a node.
alpar@100
   695
kpeter@244
   696
    ///This function returns the 'previous arc' of the %DFS tree for the
kpeter@244
   697
    ///node \c v, i.e. it returns the last arc of a %DFS path from the
kpeter@244
   698
    ///root to \c v. It is \c INVALID
kpeter@244
   699
    ///if \c v is not reachable from the root(s) or if \c v is a root.
kpeter@244
   700
    ///
kpeter@244
   701
    ///The %DFS tree used here is equal to the %DFS tree used in
alpar@100
   702
    ///\ref predNode().
kpeter@244
   703
    ///
alpar@100
   704
    ///\pre Either \ref run() or \ref start() must be called before using
alpar@100
   705
    ///this function.
alpar@100
   706
    Arc predArc(Node v) const { return (*_pred)[v];}
alpar@100
   707
alpar@100
   708
    ///Returns the 'previous node' of the %DFS tree.
alpar@100
   709
kpeter@244
   710
    ///This function returns the 'previous node' of the %DFS
kpeter@244
   711
    ///tree for the node \c v, i.e. it returns the last but one node
kpeter@244
   712
    ///from a %DFS path from the root to \c v. It is \c INVALID
kpeter@244
   713
    ///if \c v is not reachable from the root(s) or if \c v is a root.
kpeter@244
   714
    ///
kpeter@244
   715
    ///The %DFS tree used here is equal to the %DFS tree used in
kpeter@244
   716
    ///\ref predArc().
kpeter@244
   717
    ///
alpar@100
   718
    ///\pre Either \ref run() or \ref start() must be called before
alpar@100
   719
    ///using this function.
alpar@100
   720
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@209
   721
                                  G->source((*_pred)[v]); }
alpar@209
   722
kpeter@244
   723
    ///\brief Returns a const reference to the node map that stores the
kpeter@244
   724
    ///distances of the nodes.
kpeter@244
   725
    ///
kpeter@244
   726
    ///Returns a const reference to the node map that stores the
kpeter@244
   727
    ///distances of the nodes calculated by the algorithm.
kpeter@244
   728
    ///
kpeter@244
   729
    ///\pre Either \ref run() or \ref init()
kpeter@244
   730
    ///must be called before using this function.
alpar@100
   731
    const DistMap &distMap() const { return *_dist;}
alpar@209
   732
kpeter@244
   733
    ///\brief Returns a const reference to the node map that stores the
kpeter@244
   734
    ///predecessor arcs.
kpeter@244
   735
    ///
kpeter@244
   736
    ///Returns a const reference to the node map that stores the predecessor
kpeter@244
   737
    ///arcs, which form the DFS tree.
kpeter@244
   738
    ///
alpar@100
   739
    ///\pre Either \ref run() or \ref init()
alpar@100
   740
    ///must be called before using this function.
alpar@100
   741
    const PredMap &predMap() const { return *_pred;}
alpar@209
   742
kpeter@244
   743
    ///Checks if a node is reachable from the root(s).
alpar@100
   744
alpar@100
   745
    ///Returns \c true if \c v is reachable from the root(s).
alpar@100
   746
    ///\pre Either \ref run() or \ref start()
alpar@100
   747
    ///must be called before using this function.
kpeter@244
   748
    bool reached(Node v) const { return (*_reached)[v]; }
alpar@209
   749
alpar@100
   750
    ///@}
alpar@100
   751
  };
alpar@100
   752
kpeter@244
   753
  ///Default traits class of dfs() function.
alpar@100
   754
kpeter@244
   755
  ///Default traits class of dfs() function.
kpeter@157
   756
  ///\tparam GR Digraph type.
alpar@100
   757
  template<class GR>
alpar@100
   758
  struct DfsWizardDefaultTraits
alpar@100
   759
  {
kpeter@244
   760
    ///The type of the digraph the algorithm runs on.
alpar@100
   761
    typedef GR Digraph;
kpeter@244
   762
kpeter@244
   763
    ///\brief The type of the map that stores the predecessor
alpar@100
   764
    ///arcs of the %DFS paths.
alpar@209
   765
    ///
kpeter@244
   766
    ///The type of the map that stores the predecessor
alpar@100
   767
    ///arcs of the %DFS paths.
alpar@100
   768
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   769
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@301
   770
    ///Instantiates a PredMap.
alpar@209
   771
kpeter@301
   772
    ///This function instantiates a PredMap.
kpeter@244
   773
    ///\param g is the digraph, to which we would like to define the
kpeter@301
   774
    ///PredMap.
kpeter@244
   775
    static PredMap *createPredMap(const Digraph &g)
alpar@100
   776
    {
kpeter@278
   777
      return new PredMap(g);
alpar@100
   778
    }
alpar@100
   779
alpar@100
   780
    ///The type of the map that indicates which nodes are processed.
alpar@209
   781
alpar@100
   782
    ///The type of the map that indicates which nodes are processed.
alpar@100
   783
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   784
    ///By default it is a NullMap.
alpar@100
   785
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@301
   786
    ///Instantiates a ProcessedMap.
alpar@209
   787
kpeter@301
   788
    ///This function instantiates a ProcessedMap.
alpar@100
   789
    ///\param g is the digraph, to which
kpeter@301
   790
    ///we would like to define the ProcessedMap.
alpar@100
   791
#ifdef DOXYGEN
kpeter@244
   792
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   793
#else
kpeter@244
   794
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   795
#endif
alpar@100
   796
    {
alpar@100
   797
      return new ProcessedMap();
alpar@100
   798
    }
kpeter@244
   799
alpar@100
   800
    ///The type of the map that indicates which nodes are reached.
alpar@209
   801
alpar@100
   802
    ///The type of the map that indicates which nodes are reached.
kpeter@244
   803
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
   804
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
kpeter@301
   805
    ///Instantiates a ReachedMap.
alpar@209
   806
kpeter@301
   807
    ///This function instantiates a ReachedMap.
kpeter@244
   808
    ///\param g is the digraph, to which
kpeter@301
   809
    ///we would like to define the ReachedMap.
kpeter@244
   810
    static ReachedMap *createReachedMap(const Digraph &g)
alpar@100
   811
    {
kpeter@244
   812
      return new ReachedMap(g);
alpar@100
   813
    }
alpar@209
   814
kpeter@244
   815
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   816
kpeter@244
   817
    ///The type of the map that stores the distances of the nodes.
alpar@100
   818
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   819
    typedef typename Digraph::template NodeMap<int> DistMap;
kpeter@301
   820
    ///Instantiates a DistMap.
alpar@209
   821
kpeter@301
   822
    ///This function instantiates a DistMap.
alpar@210
   823
    ///\param g is the digraph, to which we would like to define
kpeter@301
   824
    ///the DistMap
kpeter@244
   825
    static DistMap *createDistMap(const Digraph &g)
alpar@100
   826
    {
kpeter@278
   827
      return new DistMap(g);
alpar@100
   828
    }
kpeter@278
   829
kpeter@278
   830
    ///The type of the DFS paths.
kpeter@278
   831
kpeter@278
   832
    ///The type of the DFS paths.
kpeter@278
   833
    ///It must meet the \ref concepts::Path "Path" concept.
kpeter@278
   834
    typedef lemon::Path<Digraph> Path;
alpar@100
   835
  };
alpar@209
   836
kpeter@313
   837
  /// Default traits class used by DfsWizard
alpar@100
   838
alpar@100
   839
  /// To make it easier to use Dfs algorithm
kpeter@244
   840
  /// we have created a wizard class.
alpar@100
   841
  /// This \ref DfsWizard class needs default traits,
kpeter@244
   842
  /// as well as the \ref Dfs class.
alpar@100
   843
  /// The \ref DfsWizardBase is a class to be the default traits of the
alpar@100
   844
  /// \ref DfsWizard class.
alpar@100
   845
  template<class GR>
alpar@100
   846
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
alpar@100
   847
  {
alpar@100
   848
alpar@100
   849
    typedef DfsWizardDefaultTraits<GR> Base;
alpar@100
   850
  protected:
kpeter@244
   851
    //The type of the nodes in the digraph.
alpar@100
   852
    typedef typename Base::Digraph::Node Node;
alpar@100
   853
kpeter@244
   854
    //Pointer to the digraph the algorithm runs on.
alpar@100
   855
    void *_g;
kpeter@244
   856
    //Pointer to the map of reached nodes.
alpar@100
   857
    void *_reached;
kpeter@244
   858
    //Pointer to the map of processed nodes.
alpar@100
   859
    void *_processed;
kpeter@244
   860
    //Pointer to the map of predecessors arcs.
alpar@100
   861
    void *_pred;
kpeter@244
   862
    //Pointer to the map of distances.
alpar@100
   863
    void *_dist;
kpeter@278
   864
    //Pointer to the DFS path to the target node.
kpeter@278
   865
    void *_path;
kpeter@278
   866
    //Pointer to the distance of the target node.
kpeter@278
   867
    int *_di;
alpar@209
   868
alpar@100
   869
    public:
alpar@100
   870
    /// Constructor.
alpar@209
   871
alpar@100
   872
    /// This constructor does not require parameters, therefore it initiates
kpeter@278
   873
    /// all of the attributes to \c 0.
alpar@100
   874
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
kpeter@278
   875
                      _dist(0), _path(0), _di(0) {}
alpar@100
   876
alpar@100
   877
    /// Constructor.
alpar@209
   878
kpeter@278
   879
    /// This constructor requires one parameter,
kpeter@278
   880
    /// others are initiated to \c 0.
kpeter@244
   881
    /// \param g The digraph the algorithm runs on.
kpeter@278
   882
    DfsWizardBase(const GR &g) :
alpar@209
   883
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
kpeter@278
   884
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
alpar@100
   885
alpar@100
   886
  };
alpar@209
   887
kpeter@278
   888
  /// Auxiliary class for the function-type interface of DFS algorithm.
alpar@100
   889
kpeter@278
   890
  /// This auxiliary class is created to implement the
kpeter@278
   891
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
kpeter@278
   892
  /// It does not have own \ref run() method, it uses the functions
kpeter@278
   893
  /// and features of the plain \ref Dfs.
alpar@100
   894
  ///
kpeter@278
   895
  /// This class should only be used through the \ref dfs() function,
kpeter@278
   896
  /// which makes it easier to use the algorithm.
alpar@100
   897
  template<class TR>
alpar@100
   898
  class DfsWizard : public TR
alpar@100
   899
  {
alpar@100
   900
    typedef TR Base;
alpar@100
   901
kpeter@244
   902
    ///The type of the digraph the algorithm runs on.
alpar@100
   903
    typedef typename TR::Digraph Digraph;
kpeter@244
   904
alpar@100
   905
    typedef typename Digraph::Node Node;
alpar@100
   906
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   907
    typedef typename Digraph::Arc Arc;
alpar@100
   908
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@209
   909
kpeter@244
   910
    ///\brief The type of the map that stores the predecessor
kpeter@278
   911
    ///arcs of the DFS paths.
kpeter@244
   912
    typedef typename TR::PredMap PredMap;
kpeter@244
   913
    ///\brief The type of the map that stores the distances of the nodes.
kpeter@244
   914
    typedef typename TR::DistMap DistMap;
kpeter@244
   915
    ///\brief The type of the map that indicates which nodes are reached.
alpar@100
   916
    typedef typename TR::ReachedMap ReachedMap;
kpeter@244
   917
    ///\brief The type of the map that indicates which nodes are processed.
alpar@100
   918
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@278
   919
    ///The type of the DFS paths
kpeter@278
   920
    typedef typename TR::Path Path;
alpar@100
   921
alpar@100
   922
  public:
kpeter@244
   923
alpar@100
   924
    /// Constructor.
alpar@100
   925
    DfsWizard() : TR() {}
alpar@100
   926
alpar@100
   927
    /// Constructor that requires parameters.
alpar@100
   928
alpar@100
   929
    /// Constructor that requires parameters.
alpar@100
   930
    /// These parameters will be the default values for the traits class.
kpeter@278
   931
    /// \param g The digraph the algorithm runs on.
kpeter@278
   932
    DfsWizard(const Digraph &g) :
kpeter@278
   933
      TR(g) {}
alpar@100
   934
alpar@100
   935
    ///Copy constructor
alpar@100
   936
    DfsWizard(const TR &b) : TR(b) {}
alpar@100
   937
alpar@100
   938
    ~DfsWizard() {}
alpar@100
   939
kpeter@278
   940
    ///Runs DFS algorithm from the given source node.
alpar@209
   941
kpeter@278
   942
    ///This method runs DFS algorithm from node \c s
kpeter@278
   943
    ///in order to compute the DFS path to each node.
kpeter@278
   944
    void run(Node s)
kpeter@278
   945
    {
kpeter@278
   946
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
kpeter@278
   947
      if (Base::_pred)
kpeter@278
   948
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
   949
      if (Base::_dist)
kpeter@278
   950
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
   951
      if (Base::_reached)
kpeter@278
   952
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
kpeter@278
   953
      if (Base::_processed)
kpeter@278
   954
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
   955
      if (s!=INVALID)
kpeter@278
   956
        alg.run(s);
kpeter@278
   957
      else
kpeter@278
   958
        alg.run();
kpeter@278
   959
    }
kpeter@278
   960
kpeter@278
   961
    ///Finds the DFS path between \c s and \c t.
kpeter@278
   962
kpeter@278
   963
    ///This method runs DFS algorithm from node \c s
kpeter@278
   964
    ///in order to compute the DFS path to node \c t
kpeter@278
   965
    ///(it stops searching when \c t is processed).
kpeter@278
   966
    ///
kpeter@278
   967
    ///\return \c true if \c t is reachable form \c s.
kpeter@278
   968
    bool run(Node s, Node t)
kpeter@278
   969
    {
kpeter@278
   970
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
kpeter@278
   971
      if (Base::_pred)
kpeter@278
   972
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
   973
      if (Base::_dist)
kpeter@278
   974
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
   975
      if (Base::_reached)
kpeter@278
   976
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
kpeter@278
   977
      if (Base::_processed)
kpeter@278
   978
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
   979
      alg.run(s,t);
kpeter@278
   980
      if (Base::_path)
kpeter@278
   981
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
kpeter@278
   982
      if (Base::_di)
kpeter@278
   983
        *Base::_di = alg.dist(t);
kpeter@278
   984
      return alg.reached(t);
kpeter@278
   985
      }
kpeter@278
   986
kpeter@278
   987
    ///Runs DFS algorithm to visit all nodes in the digraph.
kpeter@278
   988
kpeter@278
   989
    ///This method runs DFS algorithm in order to compute
kpeter@278
   990
    ///the DFS path to each node.
alpar@100
   991
    void run()
alpar@100
   992
    {
kpeter@278
   993
      run(INVALID);
kpeter@244
   994
    }
kpeter@244
   995
alpar@100
   996
    template<class T>
kpeter@257
   997
    struct SetPredMapBase : public Base {
alpar@100
   998
      typedef T PredMap;
alpar@100
   999
      static PredMap *createPredMap(const Digraph &) { return 0; };
kpeter@257
  1000
      SetPredMapBase(const TR &b) : TR(b) {}
alpar@100
  1001
    };
kpeter@278
  1002
    ///\brief \ref named-func-param "Named parameter"
kpeter@301
  1003
    ///for setting PredMap object.
alpar@100
  1004
    ///
kpeter@278
  1005
    ///\ref named-func-param "Named parameter"
kpeter@301
  1006
    ///for setting PredMap object.
alpar@100
  1007
    template<class T>
kpeter@257
  1008
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
alpar@100
  1009
    {
alpar@100
  1010
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1011
      return DfsWizard<SetPredMapBase<T> >(*this);
alpar@100
  1012
    }
alpar@209
  1013
alpar@100
  1014
    template<class T>
kpeter@257
  1015
    struct SetReachedMapBase : public Base {
alpar@100
  1016
      typedef T ReachedMap;
alpar@100
  1017
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
kpeter@257
  1018
      SetReachedMapBase(const TR &b) : TR(b) {}
alpar@100
  1019
    };
kpeter@278
  1020
    ///\brief \ref named-func-param "Named parameter"
kpeter@301
  1021
    ///for setting ReachedMap object.
alpar@100
  1022
    ///
kpeter@278
  1023
    /// \ref named-func-param "Named parameter"
kpeter@301
  1024
    ///for setting ReachedMap object.
alpar@100
  1025
    template<class T>
kpeter@257
  1026
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
alpar@100
  1027
    {
deba@158
  1028
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1029
      return DfsWizard<SetReachedMapBase<T> >(*this);
alpar@100
  1030
    }
alpar@209
  1031
alpar@100
  1032
    template<class T>
kpeter@278
  1033
    struct SetDistMapBase : public Base {
kpeter@278
  1034
      typedef T DistMap;
kpeter@278
  1035
      static DistMap *createDistMap(const Digraph &) { return 0; };
kpeter@278
  1036
      SetDistMapBase(const TR &b) : TR(b) {}
kpeter@278
  1037
    };
kpeter@278
  1038
    ///\brief \ref named-func-param "Named parameter"
kpeter@301
  1039
    ///for setting DistMap object.
kpeter@278
  1040
    ///
kpeter@278
  1041
    /// \ref named-func-param "Named parameter"
kpeter@301
  1042
    ///for setting DistMap object.
kpeter@278
  1043
    template<class T>
kpeter@278
  1044
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
kpeter@278
  1045
    {
kpeter@278
  1046
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1047
      return DfsWizard<SetDistMapBase<T> >(*this);
kpeter@278
  1048
    }
kpeter@278
  1049
kpeter@278
  1050
    template<class T>
kpeter@257
  1051
    struct SetProcessedMapBase : public Base {
alpar@100
  1052
      typedef T ProcessedMap;
alpar@100
  1053
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
kpeter@257
  1054
      SetProcessedMapBase(const TR &b) : TR(b) {}
alpar@100
  1055
    };
kpeter@278
  1056
    ///\brief \ref named-func-param "Named parameter"
kpeter@301
  1057
    ///for setting ProcessedMap object.
alpar@100
  1058
    ///
kpeter@278
  1059
    /// \ref named-func-param "Named parameter"
kpeter@301
  1060
    ///for setting ProcessedMap object.
alpar@100
  1061
    template<class T>
kpeter@257
  1062
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
alpar@100
  1063
    {
deba@158
  1064
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1065
      return DfsWizard<SetProcessedMapBase<T> >(*this);
alpar@100
  1066
    }
alpar@209
  1067
alpar@100
  1068
    template<class T>
kpeter@278
  1069
    struct SetPathBase : public Base {
kpeter@278
  1070
      typedef T Path;
kpeter@278
  1071
      SetPathBase(const TR &b) : TR(b) {}
alpar@100
  1072
    };
kpeter@278
  1073
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1074
    ///for getting the DFS path to the target node.
alpar@100
  1075
    ///
kpeter@278
  1076
    ///\ref named-func-param "Named parameter"
kpeter@278
  1077
    ///for getting the DFS path to the target node.
alpar@100
  1078
    template<class T>
kpeter@278
  1079
    DfsWizard<SetPathBase<T> > path(const T &t)
alpar@100
  1080
    {
kpeter@278
  1081
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1082
      return DfsWizard<SetPathBase<T> >(*this);
kpeter@278
  1083
    }
kpeter@278
  1084
kpeter@278
  1085
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1086
    ///for getting the distance of the target node.
kpeter@278
  1087
    ///
kpeter@278
  1088
    ///\ref named-func-param "Named parameter"
kpeter@278
  1089
    ///for getting the distance of the target node.
kpeter@278
  1090
    DfsWizard dist(const int &d)
kpeter@278
  1091
    {
kpeter@278
  1092
      Base::_di=const_cast<int*>(&d);
kpeter@278
  1093
      return *this;
alpar@100
  1094
    }
alpar@209
  1095
alpar@100
  1096
  };
alpar@209
  1097
kpeter@278
  1098
  ///Function-type interface for DFS algorithm.
alpar@100
  1099
alpar@100
  1100
  ///\ingroup search
kpeter@278
  1101
  ///Function-type interface for DFS algorithm.
alpar@100
  1102
  ///
kpeter@278
  1103
  ///This function also has several \ref named-func-param "named parameters",
alpar@100
  1104
  ///they are declared as the members of class \ref DfsWizard.
kpeter@278
  1105
  ///The following examples show how to use these parameters.
alpar@100
  1106
  ///\code
kpeter@278
  1107
  ///  // Compute the DFS tree
kpeter@278
  1108
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
kpeter@278
  1109
  ///
kpeter@278
  1110
  ///  // Compute the DFS path from s to t
kpeter@278
  1111
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
alpar@100
  1112
  ///\endcode
kpeter@278
  1113
alpar@100
  1114
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
alpar@100
  1115
  ///to the end of the parameter list.
alpar@100
  1116
  ///\sa DfsWizard
alpar@100
  1117
  ///\sa Dfs
alpar@100
  1118
  template<class GR>
alpar@100
  1119
  DfsWizard<DfsWizardBase<GR> >
kpeter@278
  1120
  dfs(const GR &digraph)
alpar@100
  1121
  {
kpeter@278
  1122
    return DfsWizard<DfsWizardBase<GR> >(digraph);
alpar@100
  1123
  }
alpar@100
  1124
alpar@100
  1125
#ifdef DOXYGEN
kpeter@244
  1126
  /// \brief Visitor class for DFS.
alpar@209
  1127
  ///
kpeter@244
  1128
  /// This class defines the interface of the DfsVisit events, and
kpeter@244
  1129
  /// it could be the base of a real visitor class.
alpar@100
  1130
  template <typename _Digraph>
alpar@100
  1131
  struct DfsVisitor {
alpar@100
  1132
    typedef _Digraph Digraph;
alpar@100
  1133
    typedef typename Digraph::Arc Arc;
alpar@100
  1134
    typedef typename Digraph::Node Node;
kpeter@244
  1135
    /// \brief Called for the source node of the DFS.
alpar@209
  1136
    ///
kpeter@244
  1137
    /// This function is called for the source node of the DFS.
kpeter@244
  1138
    void start(const Node& node) {}
kpeter@244
  1139
    /// \brief Called when the source node is leaved.
kpeter@244
  1140
    ///
kpeter@244
  1141
    /// This function is called when the source node is leaved.
kpeter@244
  1142
    void stop(const Node& node) {}
kpeter@244
  1143
    /// \brief Called when a node is reached first time.
kpeter@244
  1144
    ///
kpeter@244
  1145
    /// This function is called when a node is reached first time.
kpeter@244
  1146
    void reach(const Node& node) {}
kpeter@244
  1147
    /// \brief Called when an arc reaches a new node.
kpeter@244
  1148
    ///
kpeter@244
  1149
    /// This function is called when the DFS finds an arc whose target node
kpeter@244
  1150
    /// is not reached yet.
alpar@100
  1151
    void discover(const Arc& arc) {}
kpeter@244
  1152
    /// \brief Called when an arc is examined but its target node is
alpar@100
  1153
    /// already discovered.
alpar@209
  1154
    ///
kpeter@244
  1155
    /// This function is called when an arc is examined but its target node is
alpar@100
  1156
    /// already discovered.
alpar@100
  1157
    void examine(const Arc& arc) {}
kpeter@244
  1158
    /// \brief Called when the DFS steps back from a node.
alpar@209
  1159
    ///
kpeter@244
  1160
    /// This function is called when the DFS steps back from a node.
kpeter@244
  1161
    void leave(const Node& node) {}
kpeter@244
  1162
    /// \brief Called when the DFS steps back on an arc.
alpar@209
  1163
    ///
kpeter@244
  1164
    /// This function is called when the DFS steps back on an arc.
kpeter@244
  1165
    void backtrack(const Arc& arc) {}
alpar@100
  1166
  };
alpar@100
  1167
#else
alpar@100
  1168
  template <typename _Digraph>
alpar@100
  1169
  struct DfsVisitor {
alpar@100
  1170
    typedef _Digraph Digraph;
alpar@100
  1171
    typedef typename Digraph::Arc Arc;
alpar@100
  1172
    typedef typename Digraph::Node Node;
alpar@100
  1173
    void start(const Node&) {}
alpar@100
  1174
    void stop(const Node&) {}
kpeter@244
  1175
    void reach(const Node&) {}
kpeter@244
  1176
    void discover(const Arc&) {}
kpeter@244
  1177
    void examine(const Arc&) {}
kpeter@244
  1178
    void leave(const Node&) {}
kpeter@244
  1179
    void backtrack(const Arc&) {}
alpar@100
  1180
alpar@100
  1181
    template <typename _Visitor>
alpar@100
  1182
    struct Constraints {
alpar@100
  1183
      void constraints() {
alpar@209
  1184
        Arc arc;
alpar@209
  1185
        Node node;
alpar@209
  1186
        visitor.start(node);
alpar@209
  1187
        visitor.stop(arc);
kpeter@244
  1188
        visitor.reach(node);
kpeter@244
  1189
        visitor.discover(arc);
kpeter@244
  1190
        visitor.examine(arc);
kpeter@244
  1191
        visitor.leave(node);
kpeter@244
  1192
        visitor.backtrack(arc);
alpar@100
  1193
      }
alpar@100
  1194
      _Visitor& visitor;
alpar@100
  1195
    };
alpar@100
  1196
  };
alpar@100
  1197
#endif
alpar@100
  1198
alpar@100
  1199
  /// \brief Default traits class of DfsVisit class.
alpar@100
  1200
  ///
alpar@100
  1201
  /// Default traits class of DfsVisit class.
kpeter@244
  1202
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
alpar@100
  1203
  template<class _Digraph>
alpar@100
  1204
  struct DfsVisitDefaultTraits {
alpar@100
  1205
kpeter@244
  1206
    /// \brief The type of the digraph the algorithm runs on.
alpar@100
  1207
    typedef _Digraph Digraph;
alpar@100
  1208
alpar@100
  1209
    /// \brief The type of the map that indicates which nodes are reached.
alpar@209
  1210
    ///
alpar@100
  1211
    /// The type of the map that indicates which nodes are reached.
kpeter@244
  1212
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
  1213
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
alpar@100
  1214
kpeter@301
  1215
    /// \brief Instantiates a ReachedMap.
alpar@100
  1216
    ///
kpeter@301
  1217
    /// This function instantiates a ReachedMap.
alpar@100
  1218
    /// \param digraph is the digraph, to which
kpeter@301
  1219
    /// we would like to define the ReachedMap.
alpar@100
  1220
    static ReachedMap *createReachedMap(const Digraph &digraph) {
alpar@100
  1221
      return new ReachedMap(digraph);
alpar@100
  1222
    }
alpar@100
  1223
alpar@100
  1224
  };
alpar@209
  1225
alpar@100
  1226
  /// \ingroup search
kpeter@244
  1227
  ///
kpeter@244
  1228
  /// \brief %DFS algorithm class with visitor interface.
kpeter@244
  1229
  ///
alpar@100
  1230
  /// This class provides an efficient implementation of the %DFS algorithm
alpar@100
  1231
  /// with visitor interface.
alpar@100
  1232
  ///
alpar@100
  1233
  /// The %DfsVisit class provides an alternative interface to the Dfs
alpar@100
  1234
  /// class. It works with callback mechanism, the DfsVisit object calls
kpeter@244
  1235
  /// the member functions of the \c Visitor class on every DFS event.
alpar@100
  1236
  ///
kpeter@252
  1237
  /// This interface of the DFS algorithm should be used in special cases
kpeter@252
  1238
  /// when extra actions have to be performed in connection with certain
kpeter@252
  1239
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
kpeter@252
  1240
  /// instead.
kpeter@252
  1241
  ///
kpeter@244
  1242
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
alpar@210
  1243
  /// The default value is
kpeter@244
  1244
  /// \ref ListDigraph. The value of _Digraph is not used directly by
kpeter@244
  1245
  /// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits.
kpeter@244
  1246
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
kpeter@244
  1247
  /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which
kpeter@244
  1248
  /// does not observe the DFS events. If you want to observe the DFS
kpeter@244
  1249
  /// events, you should implement your own visitor class.
alpar@209
  1250
  /// \tparam _Traits Traits class to set various data types used by the
alpar@100
  1251
  /// algorithm. The default traits class is
alpar@100
  1252
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
alpar@100
  1253
  /// See \ref DfsVisitDefaultTraits for the documentation of
kpeter@244
  1254
  /// a DFS visit traits class.
alpar@100
  1255
#ifdef DOXYGEN
alpar@100
  1256
  template <typename _Digraph, typename _Visitor, typename _Traits>
alpar@100
  1257
#else
alpar@100
  1258
  template <typename _Digraph = ListDigraph,
alpar@209
  1259
            typename _Visitor = DfsVisitor<_Digraph>,
deba@288
  1260
            typename _Traits = DfsVisitDefaultTraits<_Digraph> >
alpar@100
  1261
#endif
alpar@100
  1262
  class DfsVisit {
alpar@100
  1263
  public:
alpar@209
  1264
kpeter@244
  1265
    ///The traits class.
alpar@100
  1266
    typedef _Traits Traits;
alpar@100
  1267
kpeter@244
  1268
    ///The type of the digraph the algorithm runs on.
alpar@100
  1269
    typedef typename Traits::Digraph Digraph;
alpar@100
  1270
kpeter@244
  1271
    ///The visitor type used by the algorithm.
alpar@100
  1272
    typedef _Visitor Visitor;
alpar@100
  1273
kpeter@244
  1274
    ///The type of the map that indicates which nodes are reached.
alpar@100
  1275
    typedef typename Traits::ReachedMap ReachedMap;
alpar@100
  1276
alpar@100
  1277
  private:
alpar@100
  1278
alpar@100
  1279
    typedef typename Digraph::Node Node;
alpar@100
  1280
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
  1281
    typedef typename Digraph::Arc Arc;
alpar@100
  1282
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
  1283
kpeter@244
  1284
    //Pointer to the underlying digraph.
alpar@100
  1285
    const Digraph *_digraph;
kpeter@244
  1286
    //Pointer to the visitor object.
alpar@100
  1287
    Visitor *_visitor;
kpeter@244
  1288
    //Pointer to the map of reached status of the nodes.
alpar@100
  1289
    ReachedMap *_reached;
kpeter@244
  1290
    //Indicates if _reached is locally allocated (true) or not.
alpar@100
  1291
    bool local_reached;
alpar@100
  1292
alpar@100
  1293
    std::vector<typename Digraph::Arc> _stack;
alpar@100
  1294
    int _stack_head;
alpar@100
  1295
alpar@280
  1296
    //Creates the maps if necessary.
alpar@100
  1297
    void create_maps() {
alpar@100
  1298
      if(!_reached) {
alpar@209
  1299
        local_reached = true;
alpar@209
  1300
        _reached = Traits::createReachedMap(*_digraph);
alpar@100
  1301
      }
alpar@100
  1302
    }
alpar@100
  1303
alpar@100
  1304
  protected:
alpar@100
  1305
alpar@100
  1306
    DfsVisit() {}
alpar@209
  1307
alpar@100
  1308
  public:
alpar@100
  1309
alpar@100
  1310
    typedef DfsVisit Create;
alpar@100
  1311
alpar@100
  1312
    /// \name Named template parameters
alpar@100
  1313
alpar@100
  1314
    ///@{
alpar@100
  1315
    template <class T>
kpeter@257
  1316
    struct SetReachedMapTraits : public Traits {
alpar@100
  1317
      typedef T ReachedMap;
alpar@100
  1318
      static ReachedMap *createReachedMap(const Digraph &digraph) {
deba@290
  1319
        LEMON_ASSERT(false, "ReachedMap is not initialized");
deba@290
  1320
        return 0; // ignore warnings
alpar@100
  1321
      }
alpar@100
  1322
    };
alpar@209
  1323
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@244
  1324
    /// ReachedMap type.
alpar@100
  1325
    ///
kpeter@244
  1326
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
alpar@100
  1327
    template <class T>
kpeter@257
  1328
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
kpeter@257
  1329
                                            SetReachedMapTraits<T> > {
kpeter@257
  1330
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
alpar@100
  1331
    };
alpar@100
  1332
    ///@}
alpar@100
  1333
alpar@209
  1334
  public:
alpar@209
  1335
alpar@100
  1336
    /// \brief Constructor.
alpar@100
  1337
    ///
alpar@100
  1338
    /// Constructor.
alpar@100
  1339
    ///
kpeter@244
  1340
    /// \param digraph The digraph the algorithm runs on.
kpeter@244
  1341
    /// \param visitor The visitor object of the algorithm.
alpar@209
  1342
    DfsVisit(const Digraph& digraph, Visitor& visitor)
alpar@100
  1343
      : _digraph(&digraph), _visitor(&visitor),
alpar@209
  1344
        _reached(0), local_reached(false) {}
alpar@209
  1345
alpar@100
  1346
    /// \brief Destructor.
alpar@100
  1347
    ~DfsVisit() {
alpar@100
  1348
      if(local_reached) delete _reached;
alpar@100
  1349
    }
alpar@100
  1350
kpeter@244
  1351
    /// \brief Sets the map that indicates which nodes are reached.
alpar@100
  1352
    ///
kpeter@244
  1353
    /// Sets the map that indicates which nodes are reached.
alpar@100
  1354
    /// If you don't use this function before calling \ref run(),
kpeter@244
  1355
    /// it will allocate one. The destructor deallocates this
alpar@100
  1356
    /// automatically allocated map, of course.
alpar@100
  1357
    /// \return <tt> (*this) </tt>
alpar@100
  1358
    DfsVisit &reachedMap(ReachedMap &m) {
alpar@100
  1359
      if(local_reached) {
alpar@209
  1360
        delete _reached;
alpar@209
  1361
        local_reached=false;
alpar@100
  1362
      }
alpar@100
  1363
      _reached = &m;
alpar@100
  1364
      return *this;
alpar@100
  1365
    }
alpar@100
  1366
alpar@100
  1367
  public:
kpeter@244
  1368
alpar@100
  1369
    /// \name Execution control
alpar@100
  1370
    /// The simplest way to execute the algorithm is to use
kpeter@244
  1371
    /// one of the member functions called \ref lemon::DfsVisit::run()
kpeter@244
  1372
    /// "run()".
alpar@100
  1373
    /// \n
kpeter@244
  1374
    /// If you need more control on the execution, first you must call
kpeter@244
  1375
    /// \ref lemon::DfsVisit::init() "init()", then you can add several
kpeter@244
  1376
    /// source nodes with \ref lemon::DfsVisit::addSource() "addSource()".
kpeter@244
  1377
    /// Finally \ref lemon::DfsVisit::start() "start()" will perform the
kpeter@244
  1378
    /// actual path computation.
alpar@100
  1379
alpar@100
  1380
    /// @{
kpeter@244
  1381
alpar@100
  1382
    /// \brief Initializes the internal data structures.
alpar@100
  1383
    ///
alpar@100
  1384
    /// Initializes the internal data structures.
alpar@100
  1385
    void init() {
alpar@100
  1386
      create_maps();
alpar@100
  1387
      _stack.resize(countNodes(*_digraph));
alpar@100
  1388
      _stack_head = -1;
alpar@100
  1389
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
alpar@209
  1390
        _reached->set(u, false);
alpar@100
  1391
      }
alpar@100
  1392
    }
alpar@209
  1393
kpeter@244
  1394
    ///Adds a new source node.
kpeter@244
  1395
kpeter@244
  1396
    ///Adds a new source node to the set of nodes to be processed.
alpar@100
  1397
    ///
kpeter@244
  1398
    ///\pre The stack must be empty. (Otherwise the algorithm gives
kpeter@244
  1399
    ///false results.)
kpeter@244
  1400
    ///
kpeter@244
  1401
    ///\warning Distances will be wrong (or at least strange) in case of
kpeter@244
  1402
    ///multiple sources.
kpeter@244
  1403
    void addSource(Node s)
kpeter@244
  1404
    {
kpeter@244
  1405
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
alpar@100
  1406
      if(!(*_reached)[s]) {
alpar@209
  1407
          _reached->set(s,true);
alpar@209
  1408
          _visitor->start(s);
alpar@209
  1409
          _visitor->reach(s);
alpar@209
  1410
          Arc e;
alpar@209
  1411
          _digraph->firstOut(e, s);
alpar@209
  1412
          if (e != INVALID) {
alpar@209
  1413
            _stack[++_stack_head] = e;
alpar@209
  1414
          } else {
alpar@209
  1415
            _visitor->leave(s);
alpar@209
  1416
          }
alpar@209
  1417
        }
alpar@100
  1418
    }
alpar@209
  1419
alpar@100
  1420
    /// \brief Processes the next arc.
alpar@100
  1421
    ///
alpar@100
  1422
    /// Processes the next arc.
alpar@100
  1423
    ///
alpar@100
  1424
    /// \return The processed arc.
alpar@100
  1425
    ///
kpeter@244
  1426
    /// \pre The stack must not be empty.
alpar@209
  1427
    Arc processNextArc() {
alpar@100
  1428
      Arc e = _stack[_stack_head];
alpar@100
  1429
      Node m = _digraph->target(e);
alpar@100
  1430
      if(!(*_reached)[m]) {
alpar@209
  1431
        _visitor->discover(e);
alpar@209
  1432
        _visitor->reach(m);
alpar@209
  1433
        _reached->set(m, true);
alpar@209
  1434
        _digraph->firstOut(_stack[++_stack_head], m);
alpar@100
  1435
      } else {
alpar@209
  1436
        _visitor->examine(e);
alpar@209
  1437
        m = _digraph->source(e);
alpar@209
  1438
        _digraph->nextOut(_stack[_stack_head]);
alpar@100
  1439
      }
alpar@100
  1440
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
alpar@209
  1441
        _visitor->leave(m);
alpar@209
  1442
        --_stack_head;
alpar@209
  1443
        if (_stack_head >= 0) {
alpar@209
  1444
          _visitor->backtrack(_stack[_stack_head]);
alpar@209
  1445
          m = _digraph->source(_stack[_stack_head]);
alpar@209
  1446
          _digraph->nextOut(_stack[_stack_head]);
alpar@209
  1447
        } else {
alpar@209
  1448
          _visitor->stop(m);
alpar@209
  1449
        }
alpar@100
  1450
      }
alpar@100
  1451
      return e;
alpar@100
  1452
    }
alpar@100
  1453
alpar@100
  1454
    /// \brief Next arc to be processed.
alpar@100
  1455
    ///
alpar@100
  1456
    /// Next arc to be processed.
alpar@100
  1457
    ///
alpar@100
  1458
    /// \return The next arc to be processed or INVALID if the stack is
alpar@100
  1459
    /// empty.
kpeter@244
  1460
    Arc nextArc() const {
alpar@100
  1461
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
alpar@100
  1462
    }
alpar@100
  1463
alpar@100
  1464
    /// \brief Returns \c false if there are nodes
kpeter@244
  1465
    /// to be processed.
alpar@100
  1466
    ///
alpar@100
  1467
    /// Returns \c false if there are nodes
kpeter@244
  1468
    /// to be processed in the queue (stack).
kpeter@244
  1469
    bool emptyQueue() const { return _stack_head < 0; }
alpar@100
  1470
alpar@100
  1471
    /// \brief Returns the number of the nodes to be processed.
alpar@100
  1472
    ///
kpeter@244
  1473
    /// Returns the number of the nodes to be processed in the queue (stack).
kpeter@244
  1474
    int queueSize() const { return _stack_head + 1; }
alpar@209
  1475
alpar@100
  1476
    /// \brief Executes the algorithm.
alpar@100
  1477
    ///
alpar@100
  1478
    /// Executes the algorithm.
alpar@100
  1479
    ///
kpeter@244
  1480
    /// This method runs the %DFS algorithm from the root node
kpeter@244
  1481
    /// in order to compute the %DFS path to each node.
kpeter@244
  1482
    ///
kpeter@244
  1483
    /// The algorithm computes
kpeter@244
  1484
    /// - the %DFS tree,
kpeter@244
  1485
    /// - the distance of each node from the root in the %DFS tree.
kpeter@244
  1486
    ///
kpeter@244
  1487
    /// \pre init() must be called and a root node should be
kpeter@244
  1488
    /// added with addSource() before using this function.
kpeter@244
  1489
    ///
kpeter@244
  1490
    /// \note <tt>d.start()</tt> is just a shortcut of the following code.
kpeter@244
  1491
    /// \code
kpeter@244
  1492
    ///   while ( !d.emptyQueue() ) {
kpeter@244
  1493
    ///     d.processNextArc();
kpeter@244
  1494
    ///   }
kpeter@244
  1495
    /// \endcode
alpar@100
  1496
    void start() {
alpar@100
  1497
      while ( !emptyQueue() ) processNextArc();
alpar@100
  1498
    }
alpar@209
  1499
kpeter@244
  1500
    /// \brief Executes the algorithm until the given target node is reached.
alpar@100
  1501
    ///
kpeter@244
  1502
    /// Executes the algorithm until the given target node is reached.
alpar@100
  1503
    ///
kpeter@244
  1504
    /// This method runs the %DFS algorithm from the root node
kpeter@286
  1505
    /// in order to compute the DFS path to \c t.
kpeter@244
  1506
    ///
kpeter@244
  1507
    /// The algorithm computes
kpeter@286
  1508
    /// - the %DFS path to \c t,
kpeter@286
  1509
    /// - the distance of \c t from the root in the %DFS tree.
kpeter@244
  1510
    ///
kpeter@244
  1511
    /// \pre init() must be called and a root node should be added
alpar@100
  1512
    /// with addSource() before using this function.
kpeter@286
  1513
    void start(Node t) {
kpeter@286
  1514
      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
alpar@209
  1515
        processNextArc();
alpar@100
  1516
    }
alpar@209
  1517
alpar@100
  1518
    /// \brief Executes the algorithm until a condition is met.
alpar@100
  1519
    ///
alpar@100
  1520
    /// Executes the algorithm until a condition is met.
alpar@100
  1521
    ///
kpeter@244
  1522
    /// This method runs the %DFS algorithm from the root node
kpeter@244
  1523
    /// until an arc \c a with <tt>am[a]</tt> true is found.
kpeter@244
  1524
    ///
kpeter@244
  1525
    /// \param am A \c bool (or convertible) arc map. The algorithm
kpeter@244
  1526
    /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
kpeter@244
  1527
    ///
kpeter@244
  1528
    /// \return The reached arc \c a with <tt>am[a]</tt> true or
kpeter@244
  1529
    /// \c INVALID if no such arc was found.
kpeter@244
  1530
    ///
kpeter@244
  1531
    /// \pre init() must be called and a root node should be added
alpar@100
  1532
    /// with addSource() before using this function.
alpar@100
  1533
    ///
kpeter@244
  1534
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
alpar@100
  1535
    /// not a node map.
kpeter@244
  1536
    template <typename AM>
kpeter@244
  1537
    Arc start(const AM &am) {
kpeter@244
  1538
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
alpar@100
  1539
        processNextArc();
alpar@100
  1540
      return emptyQueue() ? INVALID : _stack[_stack_head];
alpar@100
  1541
    }
alpar@100
  1542
kpeter@286
  1543
    /// \brief Runs the algorithm from the given source node.
alpar@100
  1544
    ///
kpeter@244
  1545
    /// This method runs the %DFS algorithm from node \c s.
kpeter@244
  1546
    /// in order to compute the DFS path to each node.
kpeter@244
  1547
    ///
kpeter@244
  1548
    /// The algorithm computes
kpeter@244
  1549
    /// - the %DFS tree,
kpeter@244
  1550
    /// - the distance of each node from the root in the %DFS tree.
kpeter@244
  1551
    ///
kpeter@244
  1552
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
alpar@100
  1553
    ///\code
alpar@100
  1554
    ///   d.init();
alpar@100
  1555
    ///   d.addSource(s);
alpar@100
  1556
    ///   d.start();
alpar@100
  1557
    ///\endcode
alpar@100
  1558
    void run(Node s) {
alpar@100
  1559
      init();
alpar@100
  1560
      addSource(s);
alpar@100
  1561
      start();
alpar@100
  1562
    }
alpar@100
  1563
kpeter@244
  1564
    /// \brief Finds the %DFS path between \c s and \c t.
kpeter@244
  1565
kpeter@244
  1566
    /// This method runs the %DFS algorithm from node \c s
kpeter@286
  1567
    /// in order to compute the DFS path to node \c t
kpeter@286
  1568
    /// (it stops searching when \c t is processed).
kpeter@244
  1569
    ///
kpeter@286
  1570
    /// \return \c true if \c t is reachable form \c s.
kpeter@244
  1571
    ///
kpeter@244
  1572
    /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
kpeter@244
  1573
    /// just a shortcut of the following code.
kpeter@244
  1574
    ///\code
kpeter@244
  1575
    ///   d.init();
kpeter@244
  1576
    ///   d.addSource(s);
kpeter@244
  1577
    ///   d.start(t);
kpeter@244
  1578
    ///\endcode
kpeter@286
  1579
    bool run(Node s,Node t) {
kpeter@244
  1580
      init();
kpeter@244
  1581
      addSource(s);
kpeter@244
  1582
      start(t);
kpeter@286
  1583
      return reached(t);
kpeter@244
  1584
    }
kpeter@244
  1585
kpeter@244
  1586
    /// \brief Runs the algorithm to visit all nodes in the digraph.
alpar@209
  1587
alpar@100
  1588
    /// This method runs the %DFS algorithm in order to
kpeter@244
  1589
    /// compute the %DFS path to each node.
alpar@100
  1590
    ///
kpeter@244
  1591
    /// The algorithm computes
kpeter@244
  1592
    /// - the %DFS tree,
kpeter@244
  1593
    /// - the distance of each node from the root in the %DFS tree.
kpeter@244
  1594
    ///
kpeter@244
  1595
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
alpar@100
  1596
    ///\code
kpeter@244
  1597
    ///   d.init();
kpeter@244
  1598
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
kpeter@244
  1599
    ///     if (!d.reached(n)) {
kpeter@244
  1600
    ///       d.addSource(n);
kpeter@244
  1601
    ///       d.start();
kpeter@244
  1602
    ///     }
kpeter@244
  1603
    ///   }
alpar@100
  1604
    ///\endcode
alpar@100
  1605
    void run() {
alpar@100
  1606
      init();
alpar@100
  1607
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
alpar@100
  1608
        if (!reached(it)) {
alpar@100
  1609
          addSource(it);
alpar@100
  1610
          start();
alpar@100
  1611
        }
alpar@100
  1612
      }
alpar@100
  1613
    }
kpeter@244
  1614
alpar@100
  1615
    ///@}
alpar@100
  1616
alpar@100
  1617
    /// \name Query Functions
alpar@100
  1618
    /// The result of the %DFS algorithm can be obtained using these
alpar@100
  1619
    /// functions.\n
kpeter@244
  1620
    /// Either \ref lemon::DfsVisit::run() "run()" or
kpeter@244
  1621
    /// \ref lemon::DfsVisit::start() "start()" must be called before
kpeter@244
  1622
    /// using them.
alpar@100
  1623
    ///@{
kpeter@244
  1624
kpeter@244
  1625
    /// \brief Checks if a node is reachable from the root(s).
alpar@100
  1626
    ///
alpar@100
  1627
    /// Returns \c true if \c v is reachable from the root(s).
alpar@100
  1628
    /// \pre Either \ref run() or \ref start()
alpar@100
  1629
    /// must be called before using this function.
alpar@100
  1630
    bool reached(Node v) { return (*_reached)[v]; }
kpeter@244
  1631
alpar@100
  1632
    ///@}
kpeter@244
  1633
alpar@100
  1634
  };
alpar@100
  1635
alpar@100
  1636
} //END OF NAMESPACE LEMON
alpar@100
  1637
alpar@100
  1638
#endif