lemon/dfs.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 10 Oct 2009 08:18:46 +0200
changeset 755 134852d7fb0a
parent 716 f47b6c94577e
parent 713 4ac30454f1c1
child 786 e20173729589
child 787 c2230649a493
permissions -rw-r--r--
Insert citations into the doc (#184)

- Add general citations to modules.
- Add specific citations for max flow and min cost flow algorithms.
- Add citations for the supported LP and MIP solvers.
- Extend the main page.
- Replace inproceedings entries with the journal versions.
- Add a new bibtex entry about network simplex.
- Remove unwanted entries.
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@440
     5
 * Copyright (C) 2003-2009
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.
kpeter@716
    50
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@244
    51
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@492
    52
    ///Instantiates a \c PredMap.
alpar@209
    53
kpeter@492
    54
    ///This function instantiates a \ref PredMap.
kpeter@244
    55
    ///\param g is the digraph, to which we would like to define the
kpeter@492
    56
    ///\ref 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.
kpeter@716
    65
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@716
    66
    ///By default it is a NullMap.
alpar@100
    67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@492
    68
    ///Instantiates a \c ProcessedMap.
alpar@209
    69
kpeter@492
    70
    ///This function instantiates a \ref ProcessedMap.
alpar@100
    71
    ///\param g is the digraph, to which
kpeter@492
    72
    ///we would like to define the \ref ProcessedMap.
alpar@100
    73
#ifdef DOXYGEN
kpeter@244
    74
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
    75
#else
kpeter@244
    76
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
    77
#endif
alpar@100
    78
    {
alpar@100
    79
      return new ProcessedMap();
alpar@100
    80
    }
kpeter@244
    81
alpar@100
    82
    ///The type of the map that indicates which nodes are reached.
alpar@209
    83
alpar@100
    84
    ///The type of the map that indicates which nodes are reached.
kpeter@716
    85
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
    86
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
kpeter@492
    87
    ///Instantiates a \c ReachedMap.
alpar@209
    88
kpeter@492
    89
    ///This function instantiates a \ref ReachedMap.
kpeter@244
    90
    ///\param g is the digraph, to which
kpeter@492
    91
    ///we would like to define the \ref ReachedMap.
kpeter@244
    92
    static ReachedMap *createReachedMap(const Digraph &g)
alpar@100
    93
    {
kpeter@244
    94
      return new ReachedMap(g);
alpar@100
    95
    }
alpar@209
    96
kpeter@244
    97
    ///The type of the map that stores the distances of the nodes.
kpeter@244
    98
kpeter@244
    99
    ///The type of the map that stores the distances of the nodes.
kpeter@716
   100
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   101
    typedef typename Digraph::template NodeMap<int> DistMap;
kpeter@492
   102
    ///Instantiates a \c DistMap.
alpar@209
   103
kpeter@492
   104
    ///This function instantiates a \ref DistMap.
kpeter@244
   105
    ///\param g is the digraph, to which we would like to define the
kpeter@492
   106
    ///\ref DistMap.
kpeter@244
   107
    static DistMap *createDistMap(const Digraph &g)
alpar@100
   108
    {
kpeter@244
   109
      return new DistMap(g);
alpar@100
   110
    }
alpar@100
   111
  };
alpar@209
   112
alpar@100
   113
  ///%DFS algorithm class.
alpar@209
   114
alpar@100
   115
  ///\ingroup search
alpar@100
   116
  ///This class provides an efficient implementation of the %DFS algorithm.
alpar@100
   117
  ///
kpeter@278
   118
  ///There is also a \ref dfs() "function-type interface" for the DFS
kpeter@244
   119
  ///algorithm, which is convenient in the simplier cases and it can be
kpeter@244
   120
  ///used easier.
kpeter@244
   121
  ///
kpeter@244
   122
  ///\tparam GR The type of the digraph the algorithm runs on.
kpeter@405
   123
  ///The default type is \ref ListDigraph.
alpar@100
   124
#ifdef DOXYGEN
alpar@100
   125
  template <typename GR,
alpar@209
   126
            typename TR>
alpar@100
   127
#else
alpar@100
   128
  template <typename GR=ListDigraph,
alpar@209
   129
            typename TR=DfsDefaultTraits<GR> >
alpar@100
   130
#endif
alpar@100
   131
  class Dfs {
alpar@100
   132
  public:
alpar@100
   133
kpeter@244
   134
    ///The type of the digraph the algorithm runs on.
kpeter@244
   135
    typedef typename TR::Digraph Digraph;
kpeter@244
   136
kpeter@244
   137
    ///\brief The type of the map that stores the predecessor arcs of the
kpeter@244
   138
    ///DFS paths.
kpeter@244
   139
    typedef typename TR::PredMap PredMap;
kpeter@244
   140
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   141
    typedef typename TR::DistMap DistMap;
kpeter@244
   142
    ///The type of the map that indicates which nodes are reached.
kpeter@244
   143
    typedef typename TR::ReachedMap ReachedMap;
kpeter@244
   144
    ///The type of the map that indicates which nodes are processed.
kpeter@244
   145
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@244
   146
    ///The type of the paths.
kpeter@244
   147
    typedef PredMapPath<Digraph, PredMap> Path;
kpeter@244
   148
kpeter@405
   149
    ///The \ref DfsDefaultTraits "traits class" of the algorithm.
alpar@100
   150
    typedef TR Traits;
kpeter@244
   151
kpeter@244
   152
  private:
kpeter@244
   153
alpar@100
   154
    typedef typename Digraph::Node Node;
alpar@100
   155
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   156
    typedef typename Digraph::Arc Arc;
alpar@100
   157
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@209
   158
kpeter@244
   159
    //Pointer to the underlying digraph.
alpar@100
   160
    const Digraph *G;
kpeter@244
   161
    //Pointer to the map of predecessor arcs.
alpar@100
   162
    PredMap *_pred;
kpeter@244
   163
    //Indicates if _pred is locally allocated (true) or not.
alpar@100
   164
    bool local_pred;
kpeter@244
   165
    //Pointer to the map of distances.
alpar@100
   166
    DistMap *_dist;
kpeter@244
   167
    //Indicates if _dist is locally allocated (true) or not.
alpar@100
   168
    bool local_dist;
kpeter@244
   169
    //Pointer to the map of reached status of the nodes.
alpar@100
   170
    ReachedMap *_reached;
kpeter@244
   171
    //Indicates if _reached is locally allocated (true) or not.
alpar@100
   172
    bool local_reached;
kpeter@244
   173
    //Pointer to the map of processed status of the nodes.
alpar@100
   174
    ProcessedMap *_processed;
kpeter@244
   175
    //Indicates if _processed is locally allocated (true) or not.
alpar@100
   176
    bool local_processed;
alpar@100
   177
alpar@100
   178
    std::vector<typename Digraph::OutArcIt> _stack;
alpar@100
   179
    int _stack_head;
alpar@100
   180
alpar@280
   181
    //Creates the maps if necessary.
alpar@209
   182
    void create_maps()
alpar@100
   183
    {
alpar@100
   184
      if(!_pred) {
alpar@209
   185
        local_pred = true;
alpar@209
   186
        _pred = Traits::createPredMap(*G);
alpar@100
   187
      }
alpar@100
   188
      if(!_dist) {
alpar@209
   189
        local_dist = true;
alpar@209
   190
        _dist = Traits::createDistMap(*G);
alpar@100
   191
      }
alpar@100
   192
      if(!_reached) {
alpar@209
   193
        local_reached = true;
alpar@209
   194
        _reached = Traits::createReachedMap(*G);
alpar@100
   195
      }
alpar@100
   196
      if(!_processed) {
alpar@209
   197
        local_processed = true;
alpar@209
   198
        _processed = Traits::createProcessedMap(*G);
alpar@100
   199
      }
alpar@100
   200
    }
alpar@100
   201
alpar@100
   202
  protected:
alpar@100
   203
alpar@100
   204
    Dfs() {}
alpar@209
   205
alpar@100
   206
  public:
alpar@100
   207
alpar@100
   208
    typedef Dfs Create;
alpar@100
   209
kpeter@584
   210
    ///\name Named Template Parameters
alpar@100
   211
alpar@100
   212
    ///@{
alpar@100
   213
alpar@100
   214
    template <class T>
kpeter@257
   215
    struct SetPredMapTraits : public Traits {
alpar@100
   216
      typedef T PredMap;
kpeter@244
   217
      static PredMap *createPredMap(const Digraph &)
alpar@100
   218
      {
deba@290
   219
        LEMON_ASSERT(false, "PredMap is not initialized");
deba@290
   220
        return 0; // ignore warnings
alpar@100
   221
      }
alpar@100
   222
    };
alpar@100
   223
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   224
    ///\c PredMap type.
alpar@100
   225
    ///
kpeter@244
   226
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   227
    ///\c PredMap type.
kpeter@716
   228
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   229
    template <class T>
kpeter@257
   230
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
kpeter@257
   231
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
alpar@100
   232
    };
alpar@209
   233
alpar@100
   234
    template <class T>
kpeter@257
   235
    struct SetDistMapTraits : public Traits {
alpar@100
   236
      typedef T DistMap;
alpar@209
   237
      static DistMap *createDistMap(const Digraph &)
alpar@100
   238
      {
deba@290
   239
        LEMON_ASSERT(false, "DistMap is not initialized");
deba@290
   240
        return 0; // ignore warnings
alpar@100
   241
      }
alpar@100
   242
    };
alpar@100
   243
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   244
    ///\c DistMap type.
alpar@100
   245
    ///
kpeter@244
   246
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   247
    ///\c DistMap type.
kpeter@716
   248
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   249
    template <class T>
kpeter@257
   250
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
kpeter@257
   251
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
alpar@100
   252
    };
alpar@209
   253
alpar@100
   254
    template <class T>
kpeter@257
   255
    struct SetReachedMapTraits : public Traits {
alpar@100
   256
      typedef T ReachedMap;
alpar@209
   257
      static ReachedMap *createReachedMap(const Digraph &)
alpar@100
   258
      {
deba@290
   259
        LEMON_ASSERT(false, "ReachedMap is not initialized");
deba@290
   260
        return 0; // ignore warnings
alpar@100
   261
      }
alpar@100
   262
    };
alpar@100
   263
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   264
    ///\c ReachedMap type.
alpar@100
   265
    ///
kpeter@244
   266
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   267
    ///\c ReachedMap type.
kpeter@716
   268
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
   269
    template <class T>
kpeter@257
   270
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
kpeter@257
   271
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
alpar@100
   272
    };
alpar@100
   273
alpar@100
   274
    template <class T>
kpeter@257
   275
    struct SetProcessedMapTraits : public Traits {
alpar@100
   276
      typedef T ProcessedMap;
alpar@209
   277
      static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   278
      {
deba@290
   279
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
deba@290
   280
        return 0; // ignore warnings
alpar@100
   281
      }
alpar@100
   282
    };
alpar@100
   283
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   284
    ///\c ProcessedMap type.
alpar@100
   285
    ///
kpeter@244
   286
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   287
    ///\c ProcessedMap type.
kpeter@716
   288
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   289
    template <class T>
kpeter@257
   290
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
kpeter@257
   291
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
alpar@100
   292
    };
alpar@209
   293
kpeter@257
   294
    struct SetStandardProcessedMapTraits : public Traits {
alpar@100
   295
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
kpeter@244
   296
      static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   297
      {
kpeter@244
   298
        return new ProcessedMap(g);
alpar@100
   299
      }
alpar@100
   300
    };
kpeter@244
   301
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   302
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
alpar@100
   303
    ///
kpeter@244
   304
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   305
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
kpeter@244
   306
    ///If you don't set it explicitly, it will be automatically allocated.
kpeter@257
   307
    struct SetStandardProcessedMap :
kpeter@257
   308
      public Dfs< Digraph, SetStandardProcessedMapTraits > {
kpeter@257
   309
      typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
alpar@100
   310
    };
alpar@209
   311
alpar@100
   312
    ///@}
alpar@100
   313
alpar@209
   314
  public:
alpar@209
   315
alpar@100
   316
    ///Constructor.
alpar@209
   317
kpeter@244
   318
    ///Constructor.
kpeter@244
   319
    ///\param g The digraph the algorithm runs on.
kpeter@244
   320
    Dfs(const Digraph &g) :
kpeter@244
   321
      G(&g),
alpar@100
   322
      _pred(NULL), local_pred(false),
alpar@100
   323
      _dist(NULL), local_dist(false),
alpar@100
   324
      _reached(NULL), local_reached(false),
alpar@100
   325
      _processed(NULL), local_processed(false)
alpar@100
   326
    { }
alpar@209
   327
alpar@100
   328
    ///Destructor.
alpar@209
   329
    ~Dfs()
alpar@100
   330
    {
alpar@100
   331
      if(local_pred) delete _pred;
alpar@100
   332
      if(local_dist) delete _dist;
alpar@100
   333
      if(local_reached) delete _reached;
alpar@100
   334
      if(local_processed) delete _processed;
alpar@100
   335
    }
alpar@100
   336
kpeter@244
   337
    ///Sets the map that stores the predecessor arcs.
alpar@100
   338
kpeter@244
   339
    ///Sets the map that stores the predecessor arcs.
kpeter@405
   340
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   341
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   342
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   343
    ///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.
kpeter@405
   358
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   359
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   360
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   361
    ///of course.
kpeter@244
   362
    ///\return <tt> (*this) </tt>
kpeter@244
   363
    Dfs &reachedMap(ReachedMap &m)
kpeter@244
   364
    {
kpeter@244
   365
      if(local_reached) {
kpeter@244
   366
        delete _reached;
kpeter@244
   367
        local_reached=false;
kpeter@244
   368
      }
kpeter@244
   369
      _reached = &m;
kpeter@244
   370
      return *this;
kpeter@244
   371
    }
kpeter@244
   372
kpeter@244
   373
    ///Sets the map that indicates which nodes are processed.
kpeter@244
   374
kpeter@244
   375
    ///Sets the map that indicates which nodes are processed.
kpeter@405
   376
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   377
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   378
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   379
    ///of course.
kpeter@244
   380
    ///\return <tt> (*this) </tt>
kpeter@244
   381
    Dfs &processedMap(ProcessedMap &m)
kpeter@244
   382
    {
kpeter@244
   383
      if(local_processed) {
kpeter@244
   384
        delete _processed;
kpeter@244
   385
        local_processed=false;
kpeter@244
   386
      }
kpeter@244
   387
      _processed = &m;
kpeter@244
   388
      return *this;
kpeter@244
   389
    }
kpeter@244
   390
kpeter@244
   391
    ///Sets the map that stores the distances of the nodes.
kpeter@244
   392
kpeter@244
   393
    ///Sets the map that stores the distances of the nodes calculated by
kpeter@244
   394
    ///the algorithm.
kpeter@405
   395
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   396
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   397
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   398
    ///of course.
alpar@100
   399
    ///\return <tt> (*this) </tt>
alpar@209
   400
    Dfs &distMap(DistMap &m)
alpar@100
   401
    {
alpar@100
   402
      if(local_dist) {
alpar@209
   403
        delete _dist;
alpar@209
   404
        local_dist=false;
alpar@100
   405
      }
alpar@100
   406
      _dist = &m;
alpar@100
   407
      return *this;
alpar@100
   408
    }
alpar@100
   409
kpeter@244
   410
  public:
alpar@100
   411
kpeter@405
   412
    ///\name Execution Control
kpeter@405
   413
    ///The simplest way to execute the DFS algorithm is to use one of the
kpeter@405
   414
    ///member functions called \ref run(Node) "run()".\n
kpeter@713
   415
    ///If you need better control on the execution, you have to call
kpeter@713
   416
    ///\ref init() first, then you can add a source node with \ref addSource()
kpeter@405
   417
    ///and perform the actual computation with \ref start().
kpeter@405
   418
    ///This procedure can be repeated if there are nodes that have not
kpeter@405
   419
    ///been reached.
alpar@100
   420
alpar@100
   421
    ///@{
alpar@100
   422
kpeter@405
   423
    ///\brief Initializes the internal data structures.
kpeter@405
   424
    ///
alpar@100
   425
    ///Initializes the internal data structures.
alpar@100
   426
    void init()
alpar@100
   427
    {
alpar@100
   428
      create_maps();
alpar@100
   429
      _stack.resize(countNodes(*G));
alpar@100
   430
      _stack_head=-1;
alpar@100
   431
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@209
   432
        _pred->set(u,INVALID);
alpar@209
   433
        _reached->set(u,false);
alpar@209
   434
        _processed->set(u,false);
alpar@100
   435
      }
alpar@100
   436
    }
alpar@209
   437
alpar@100
   438
    ///Adds a new source node.
alpar@100
   439
alpar@100
   440
    ///Adds a new source node to the set of nodes to be processed.
alpar@100
   441
    ///
kpeter@405
   442
    ///\pre The stack must be empty. Otherwise the algorithm gives
kpeter@405
   443
    ///wrong results. (One of the outgoing arcs of all the source nodes
kpeter@405
   444
    ///except for the last one will not be visited and distances will
kpeter@405
   445
    ///also be wrong.)
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
kpeter@405
   509
    ///Returns \c false if there are nodes to be processed.
kpeter@405
   510
kpeter@405
   511
    ///Returns \c false if there are nodes to be processed
kpeter@405
   512
    ///in the queue (stack).
kpeter@244
   513
    bool emptyQueue() const { return _stack_head<0; }
kpeter@244
   514
alpar@100
   515
    ///Returns the number of the nodes to be processed.
alpar@209
   516
kpeter@405
   517
    ///Returns the number of the nodes to be processed
kpeter@405
   518
    ///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@405
   640
    ///- the %DFS tree (forest),
kpeter@405
   641
    ///- the distance of each node from the root(s) 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
kpeter@405
   666
    ///The results of the DFS algorithm can be obtained using these
alpar@100
   667
    ///functions.\n
kpeter@405
   668
    ///Either \ref run(Node) "run()" or \ref start() should be called
kpeter@405
   669
    ///before using them.
alpar@209
   670
alpar@100
   671
    ///@{
alpar@100
   672
kpeter@716
   673
    ///The DFS path to the given node.
alpar@100
   674
kpeter@716
   675
    ///Returns the DFS path to the given node from the root(s).
kpeter@244
   676
    ///
kpeter@405
   677
    ///\warning \c t should be reached from the root(s).
kpeter@244
   678
    ///
kpeter@405
   679
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   680
    ///must be called before using this function.
kpeter@244
   681
    Path path(Node t) const { return Path(*G, *_pred, t); }
alpar@209
   682
kpeter@716
   683
    ///The distance of the given node from the root(s).
alpar@100
   684
kpeter@716
   685
    ///Returns the distance of the given node from the root(s).
kpeter@244
   686
    ///
kpeter@405
   687
    ///\warning If node \c v is not reached from the root(s), then
kpeter@244
   688
    ///the return value of this function is undefined.
kpeter@244
   689
    ///
kpeter@405
   690
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   691
    ///must be called before using this function.
alpar@100
   692
    int dist(Node v) const { return (*_dist)[v]; }
alpar@100
   693
kpeter@716
   694
    ///Returns the 'previous arc' of the %DFS tree for the given node.
alpar@100
   695
kpeter@244
   696
    ///This function returns the 'previous arc' of the %DFS tree for the
kpeter@405
   697
    ///node \c v, i.e. it returns the last arc of a %DFS path from a
kpeter@405
   698
    ///root to \c v. It is \c INVALID if \c v is not reached from the
kpeter@405
   699
    ///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
kpeter@716
   702
    ///\ref predNode() and \ref predMap().
kpeter@244
   703
    ///
kpeter@405
   704
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   705
    ///must be called before using this function.
alpar@100
   706
    Arc predArc(Node v) const { return (*_pred)[v];}
alpar@100
   707
kpeter@716
   708
    ///Returns the 'previous node' of the %DFS tree for the given node.
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@716
   712
    ///of a %DFS path from a root to \c v. It is \c INVALID
kpeter@405
   713
    ///if \c v is not reached 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@716
   716
    ///\ref predArc() and \ref predMap().
kpeter@244
   717
    ///
kpeter@405
   718
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   719
    ///must be called before 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@405
   729
    ///\pre Either \ref run(Node) "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@716
   737
    ///arcs, which form the DFS tree (forest).
kpeter@244
   738
    ///
kpeter@405
   739
    ///\pre Either \ref run(Node) "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@716
   743
    ///Checks if the given node. node is reached from the root(s).
alpar@100
   744
kpeter@405
   745
    ///Returns \c true if \c v is reached from the root(s).
kpeter@405
   746
    ///
kpeter@405
   747
    ///\pre Either \ref run(Node) "run()" or \ref init()
alpar@100
   748
    ///must be called before using this function.
kpeter@244
   749
    bool reached(Node v) const { return (*_reached)[v]; }
alpar@209
   750
alpar@100
   751
    ///@}
alpar@100
   752
  };
alpar@100
   753
kpeter@244
   754
  ///Default traits class of dfs() function.
alpar@100
   755
kpeter@244
   756
  ///Default traits class of dfs() function.
kpeter@157
   757
  ///\tparam GR Digraph type.
alpar@100
   758
  template<class GR>
alpar@100
   759
  struct DfsWizardDefaultTraits
alpar@100
   760
  {
kpeter@244
   761
    ///The type of the digraph the algorithm runs on.
alpar@100
   762
    typedef GR Digraph;
kpeter@244
   763
kpeter@244
   764
    ///\brief The type of the map that stores the predecessor
alpar@100
   765
    ///arcs of the %DFS paths.
alpar@209
   766
    ///
kpeter@244
   767
    ///The type of the map that stores the predecessor
alpar@100
   768
    ///arcs of the %DFS paths.
kpeter@716
   769
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   770
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@301
   771
    ///Instantiates a PredMap.
alpar@209
   772
kpeter@301
   773
    ///This function instantiates a PredMap.
kpeter@244
   774
    ///\param g is the digraph, to which we would like to define the
kpeter@301
   775
    ///PredMap.
kpeter@244
   776
    static PredMap *createPredMap(const Digraph &g)
alpar@100
   777
    {
kpeter@278
   778
      return new PredMap(g);
alpar@100
   779
    }
alpar@100
   780
alpar@100
   781
    ///The type of the map that indicates which nodes are processed.
alpar@209
   782
alpar@100
   783
    ///The type of the map that indicates which nodes are processed.
kpeter@716
   784
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   785
    ///By default it is a NullMap.
alpar@100
   786
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@301
   787
    ///Instantiates a ProcessedMap.
alpar@209
   788
kpeter@301
   789
    ///This function instantiates a ProcessedMap.
alpar@100
   790
    ///\param g is the digraph, to which
kpeter@301
   791
    ///we would like to define the ProcessedMap.
alpar@100
   792
#ifdef DOXYGEN
kpeter@244
   793
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   794
#else
kpeter@244
   795
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   796
#endif
alpar@100
   797
    {
alpar@100
   798
      return new ProcessedMap();
alpar@100
   799
    }
kpeter@244
   800
alpar@100
   801
    ///The type of the map that indicates which nodes are reached.
alpar@209
   802
alpar@100
   803
    ///The type of the map that indicates which nodes are reached.
kpeter@716
   804
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
   805
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
kpeter@301
   806
    ///Instantiates a ReachedMap.
alpar@209
   807
kpeter@301
   808
    ///This function instantiates a ReachedMap.
kpeter@244
   809
    ///\param g is the digraph, to which
kpeter@301
   810
    ///we would like to define the ReachedMap.
kpeter@244
   811
    static ReachedMap *createReachedMap(const Digraph &g)
alpar@100
   812
    {
kpeter@244
   813
      return new ReachedMap(g);
alpar@100
   814
    }
alpar@209
   815
kpeter@244
   816
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   817
kpeter@244
   818
    ///The type of the map that stores the distances of the nodes.
kpeter@716
   819
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   820
    typedef typename Digraph::template NodeMap<int> DistMap;
kpeter@301
   821
    ///Instantiates a DistMap.
alpar@209
   822
kpeter@301
   823
    ///This function instantiates a DistMap.
alpar@210
   824
    ///\param g is the digraph, to which we would like to define
kpeter@301
   825
    ///the DistMap
kpeter@244
   826
    static DistMap *createDistMap(const Digraph &g)
alpar@100
   827
    {
kpeter@278
   828
      return new DistMap(g);
alpar@100
   829
    }
kpeter@278
   830
kpeter@278
   831
    ///The type of the DFS paths.
kpeter@278
   832
kpeter@278
   833
    ///The type of the DFS paths.
kpeter@716
   834
    ///It must conform to the \ref concepts::Path "Path" concept.
kpeter@278
   835
    typedef lemon::Path<Digraph> Path;
alpar@100
   836
  };
alpar@209
   837
kpeter@313
   838
  /// Default traits class used by DfsWizard
alpar@100
   839
kpeter@716
   840
  /// Default traits class used by DfsWizard.
kpeter@716
   841
  /// \tparam GR The type of the digraph.
alpar@100
   842
  template<class GR>
alpar@100
   843
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
alpar@100
   844
  {
alpar@100
   845
alpar@100
   846
    typedef DfsWizardDefaultTraits<GR> Base;
alpar@100
   847
  protected:
kpeter@244
   848
    //The type of the nodes in the digraph.
alpar@100
   849
    typedef typename Base::Digraph::Node Node;
alpar@100
   850
kpeter@244
   851
    //Pointer to the digraph the algorithm runs on.
alpar@100
   852
    void *_g;
kpeter@244
   853
    //Pointer to the map of reached nodes.
alpar@100
   854
    void *_reached;
kpeter@244
   855
    //Pointer to the map of processed nodes.
alpar@100
   856
    void *_processed;
kpeter@244
   857
    //Pointer to the map of predecessors arcs.
alpar@100
   858
    void *_pred;
kpeter@244
   859
    //Pointer to the map of distances.
alpar@100
   860
    void *_dist;
kpeter@278
   861
    //Pointer to the DFS path to the target node.
kpeter@278
   862
    void *_path;
kpeter@278
   863
    //Pointer to the distance of the target node.
kpeter@278
   864
    int *_di;
alpar@209
   865
alpar@100
   866
    public:
alpar@100
   867
    /// Constructor.
alpar@209
   868
kpeter@716
   869
    /// This constructor does not require parameters, it initiates
kpeter@278
   870
    /// all of the attributes to \c 0.
alpar@100
   871
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
kpeter@278
   872
                      _dist(0), _path(0), _di(0) {}
alpar@100
   873
alpar@100
   874
    /// Constructor.
alpar@209
   875
kpeter@278
   876
    /// This constructor requires one parameter,
kpeter@278
   877
    /// others are initiated to \c 0.
kpeter@244
   878
    /// \param g The digraph the algorithm runs on.
kpeter@278
   879
    DfsWizardBase(const GR &g) :
alpar@209
   880
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
kpeter@278
   881
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
alpar@100
   882
alpar@100
   883
  };
alpar@209
   884
kpeter@278
   885
  /// Auxiliary class for the function-type interface of DFS algorithm.
alpar@100
   886
kpeter@278
   887
  /// This auxiliary class is created to implement the
kpeter@278
   888
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
kpeter@405
   889
  /// It does not have own \ref run(Node) "run()" method, it uses the
kpeter@405
   890
  /// functions and features of the plain \ref Dfs.
alpar@100
   891
  ///
kpeter@278
   892
  /// This class should only be used through the \ref dfs() function,
kpeter@278
   893
  /// which makes it easier to use the algorithm.
alpar@100
   894
  template<class TR>
alpar@100
   895
  class DfsWizard : public TR
alpar@100
   896
  {
alpar@100
   897
    typedef TR Base;
alpar@100
   898
alpar@100
   899
    typedef typename TR::Digraph Digraph;
kpeter@244
   900
alpar@100
   901
    typedef typename Digraph::Node Node;
alpar@100
   902
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   903
    typedef typename Digraph::Arc Arc;
alpar@100
   904
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@209
   905
kpeter@244
   906
    typedef typename TR::PredMap PredMap;
kpeter@244
   907
    typedef typename TR::DistMap DistMap;
alpar@100
   908
    typedef typename TR::ReachedMap ReachedMap;
alpar@100
   909
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@278
   910
    typedef typename TR::Path Path;
alpar@100
   911
alpar@100
   912
  public:
kpeter@244
   913
alpar@100
   914
    /// Constructor.
alpar@100
   915
    DfsWizard() : TR() {}
alpar@100
   916
alpar@100
   917
    /// Constructor that requires parameters.
alpar@100
   918
alpar@100
   919
    /// Constructor that requires parameters.
alpar@100
   920
    /// These parameters will be the default values for the traits class.
kpeter@278
   921
    /// \param g The digraph the algorithm runs on.
kpeter@278
   922
    DfsWizard(const Digraph &g) :
kpeter@278
   923
      TR(g) {}
alpar@100
   924
alpar@100
   925
    ///Copy constructor
alpar@100
   926
    DfsWizard(const TR &b) : TR(b) {}
alpar@100
   927
alpar@100
   928
    ~DfsWizard() {}
alpar@100
   929
kpeter@278
   930
    ///Runs DFS algorithm from the given source node.
alpar@209
   931
kpeter@278
   932
    ///This method runs DFS algorithm from node \c s
kpeter@278
   933
    ///in order to compute the DFS path to each node.
kpeter@278
   934
    void run(Node s)
kpeter@278
   935
    {
kpeter@278
   936
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
kpeter@278
   937
      if (Base::_pred)
kpeter@278
   938
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
   939
      if (Base::_dist)
kpeter@278
   940
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
   941
      if (Base::_reached)
kpeter@278
   942
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
kpeter@278
   943
      if (Base::_processed)
kpeter@278
   944
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
   945
      if (s!=INVALID)
kpeter@278
   946
        alg.run(s);
kpeter@278
   947
      else
kpeter@278
   948
        alg.run();
kpeter@278
   949
    }
kpeter@278
   950
kpeter@278
   951
    ///Finds the DFS path between \c s and \c t.
kpeter@278
   952
kpeter@278
   953
    ///This method runs DFS algorithm from node \c s
kpeter@278
   954
    ///in order to compute the DFS path to node \c t
kpeter@278
   955
    ///(it stops searching when \c t is processed).
kpeter@278
   956
    ///
kpeter@278
   957
    ///\return \c true if \c t is reachable form \c s.
kpeter@278
   958
    bool run(Node s, Node t)
kpeter@278
   959
    {
kpeter@278
   960
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
kpeter@278
   961
      if (Base::_pred)
kpeter@278
   962
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
   963
      if (Base::_dist)
kpeter@278
   964
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
   965
      if (Base::_reached)
kpeter@278
   966
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
kpeter@278
   967
      if (Base::_processed)
kpeter@278
   968
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
   969
      alg.run(s,t);
kpeter@278
   970
      if (Base::_path)
kpeter@278
   971
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
kpeter@278
   972
      if (Base::_di)
kpeter@278
   973
        *Base::_di = alg.dist(t);
kpeter@278
   974
      return alg.reached(t);
kpeter@278
   975
      }
kpeter@278
   976
kpeter@278
   977
    ///Runs DFS algorithm to visit all nodes in the digraph.
kpeter@278
   978
kpeter@278
   979
    ///This method runs DFS algorithm in order to compute
kpeter@278
   980
    ///the DFS path to each node.
alpar@100
   981
    void run()
alpar@100
   982
    {
kpeter@278
   983
      run(INVALID);
kpeter@244
   984
    }
kpeter@244
   985
alpar@100
   986
    template<class T>
kpeter@257
   987
    struct SetPredMapBase : public Base {
alpar@100
   988
      typedef T PredMap;
alpar@100
   989
      static PredMap *createPredMap(const Digraph &) { return 0; };
kpeter@257
   990
      SetPredMapBase(const TR &b) : TR(b) {}
alpar@100
   991
    };
kpeter@716
   992
kpeter@716
   993
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@716
   994
    ///the predecessor map.
alpar@100
   995
    ///
kpeter@716
   996
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
   997
    ///the map that stores the predecessor arcs of the nodes.
alpar@100
   998
    template<class T>
kpeter@257
   999
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
alpar@100
  1000
    {
alpar@100
  1001
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1002
      return DfsWizard<SetPredMapBase<T> >(*this);
alpar@100
  1003
    }
alpar@209
  1004
alpar@100
  1005
    template<class T>
kpeter@257
  1006
    struct SetReachedMapBase : public Base {
alpar@100
  1007
      typedef T ReachedMap;
alpar@100
  1008
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
kpeter@257
  1009
      SetReachedMapBase(const TR &b) : TR(b) {}
alpar@100
  1010
    };
kpeter@716
  1011
kpeter@716
  1012
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@716
  1013
    ///the reached map.
alpar@100
  1014
    ///
kpeter@716
  1015
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
  1016
    ///the map that indicates which nodes are reached.
alpar@100
  1017
    template<class T>
kpeter@257
  1018
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
alpar@100
  1019
    {
deba@158
  1020
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1021
      return DfsWizard<SetReachedMapBase<T> >(*this);
alpar@100
  1022
    }
alpar@209
  1023
alpar@100
  1024
    template<class T>
kpeter@278
  1025
    struct SetDistMapBase : public Base {
kpeter@278
  1026
      typedef T DistMap;
kpeter@278
  1027
      static DistMap *createDistMap(const Digraph &) { return 0; };
kpeter@278
  1028
      SetDistMapBase(const TR &b) : TR(b) {}
kpeter@278
  1029
    };
kpeter@716
  1030
kpeter@716
  1031
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@716
  1032
    ///the distance map.
kpeter@278
  1033
    ///
kpeter@716
  1034
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
  1035
    ///the map that stores the distances of the nodes calculated
kpeter@716
  1036
    ///by the algorithm.
kpeter@278
  1037
    template<class T>
kpeter@278
  1038
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
kpeter@278
  1039
    {
kpeter@278
  1040
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1041
      return DfsWizard<SetDistMapBase<T> >(*this);
kpeter@278
  1042
    }
kpeter@278
  1043
kpeter@278
  1044
    template<class T>
kpeter@257
  1045
    struct SetProcessedMapBase : public Base {
alpar@100
  1046
      typedef T ProcessedMap;
alpar@100
  1047
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
kpeter@257
  1048
      SetProcessedMapBase(const TR &b) : TR(b) {}
alpar@100
  1049
    };
kpeter@716
  1050
kpeter@716
  1051
    ///\brief \ref named-func-param "Named parameter" for setting
kpeter@716
  1052
    ///the processed map.
alpar@100
  1053
    ///
kpeter@716
  1054
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
  1055
    ///the map that indicates which nodes are processed.
alpar@100
  1056
    template<class T>
kpeter@257
  1057
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
alpar@100
  1058
    {
deba@158
  1059
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1060
      return DfsWizard<SetProcessedMapBase<T> >(*this);
alpar@100
  1061
    }
alpar@209
  1062
alpar@100
  1063
    template<class T>
kpeter@278
  1064
    struct SetPathBase : public Base {
kpeter@278
  1065
      typedef T Path;
kpeter@278
  1066
      SetPathBase(const TR &b) : TR(b) {}
alpar@100
  1067
    };
kpeter@278
  1068
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1069
    ///for getting the DFS path to the target node.
alpar@100
  1070
    ///
kpeter@278
  1071
    ///\ref named-func-param "Named parameter"
kpeter@278
  1072
    ///for getting the DFS path to the target node.
alpar@100
  1073
    template<class T>
kpeter@278
  1074
    DfsWizard<SetPathBase<T> > path(const T &t)
alpar@100
  1075
    {
kpeter@278
  1076
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1077
      return DfsWizard<SetPathBase<T> >(*this);
kpeter@278
  1078
    }
kpeter@278
  1079
kpeter@278
  1080
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1081
    ///for getting the distance of the target node.
kpeter@278
  1082
    ///
kpeter@278
  1083
    ///\ref named-func-param "Named parameter"
kpeter@278
  1084
    ///for getting the distance of the target node.
kpeter@278
  1085
    DfsWizard dist(const int &d)
kpeter@278
  1086
    {
kpeter@278
  1087
      Base::_di=const_cast<int*>(&d);
kpeter@278
  1088
      return *this;
alpar@100
  1089
    }
alpar@209
  1090
alpar@100
  1091
  };
alpar@209
  1092
kpeter@278
  1093
  ///Function-type interface for DFS algorithm.
alpar@100
  1094
alpar@100
  1095
  ///\ingroup search
kpeter@278
  1096
  ///Function-type interface for DFS algorithm.
alpar@100
  1097
  ///
kpeter@278
  1098
  ///This function also has several \ref named-func-param "named parameters",
alpar@100
  1099
  ///they are declared as the members of class \ref DfsWizard.
kpeter@278
  1100
  ///The following examples show how to use these parameters.
alpar@100
  1101
  ///\code
kpeter@278
  1102
  ///  // Compute the DFS tree
kpeter@278
  1103
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
kpeter@278
  1104
  ///
kpeter@278
  1105
  ///  // Compute the DFS path from s to t
kpeter@278
  1106
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
alpar@100
  1107
  ///\endcode
kpeter@405
  1108
  ///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()"
alpar@100
  1109
  ///to the end of the parameter list.
alpar@100
  1110
  ///\sa DfsWizard
alpar@100
  1111
  ///\sa Dfs
alpar@100
  1112
  template<class GR>
alpar@100
  1113
  DfsWizard<DfsWizardBase<GR> >
kpeter@278
  1114
  dfs(const GR &digraph)
alpar@100
  1115
  {
kpeter@278
  1116
    return DfsWizard<DfsWizardBase<GR> >(digraph);
alpar@100
  1117
  }
alpar@100
  1118
alpar@100
  1119
#ifdef DOXYGEN
kpeter@244
  1120
  /// \brief Visitor class for DFS.
alpar@209
  1121
  ///
kpeter@244
  1122
  /// This class defines the interface of the DfsVisit events, and
kpeter@244
  1123
  /// it could be the base of a real visitor class.
kpeter@492
  1124
  template <typename GR>
alpar@100
  1125
  struct DfsVisitor {
kpeter@492
  1126
    typedef GR Digraph;
alpar@100
  1127
    typedef typename Digraph::Arc Arc;
alpar@100
  1128
    typedef typename Digraph::Node Node;
kpeter@244
  1129
    /// \brief Called for the source node of the DFS.
alpar@209
  1130
    ///
kpeter@244
  1131
    /// This function is called for the source node of the DFS.
kpeter@244
  1132
    void start(const Node& node) {}
kpeter@244
  1133
    /// \brief Called when the source node is leaved.
kpeter@244
  1134
    ///
kpeter@244
  1135
    /// This function is called when the source node is leaved.
kpeter@244
  1136
    void stop(const Node& node) {}
kpeter@244
  1137
    /// \brief Called when a node is reached first time.
kpeter@244
  1138
    ///
kpeter@244
  1139
    /// This function is called when a node is reached first time.
kpeter@244
  1140
    void reach(const Node& node) {}
kpeter@244
  1141
    /// \brief Called when an arc reaches a new node.
kpeter@244
  1142
    ///
kpeter@244
  1143
    /// This function is called when the DFS finds an arc whose target node
kpeter@244
  1144
    /// is not reached yet.
alpar@100
  1145
    void discover(const Arc& arc) {}
kpeter@244
  1146
    /// \brief Called when an arc is examined but its target node is
alpar@100
  1147
    /// already discovered.
alpar@209
  1148
    ///
kpeter@244
  1149
    /// This function is called when an arc is examined but its target node is
alpar@100
  1150
    /// already discovered.
alpar@100
  1151
    void examine(const Arc& arc) {}
kpeter@244
  1152
    /// \brief Called when the DFS steps back from a node.
alpar@209
  1153
    ///
kpeter@244
  1154
    /// This function is called when the DFS steps back from a node.
kpeter@244
  1155
    void leave(const Node& node) {}
kpeter@244
  1156
    /// \brief Called when the DFS steps back on an arc.
alpar@209
  1157
    ///
kpeter@244
  1158
    /// This function is called when the DFS steps back on an arc.
kpeter@244
  1159
    void backtrack(const Arc& arc) {}
alpar@100
  1160
  };
alpar@100
  1161
#else
kpeter@492
  1162
  template <typename GR>
alpar@100
  1163
  struct DfsVisitor {
kpeter@492
  1164
    typedef GR Digraph;
alpar@100
  1165
    typedef typename Digraph::Arc Arc;
alpar@100
  1166
    typedef typename Digraph::Node Node;
alpar@100
  1167
    void start(const Node&) {}
alpar@100
  1168
    void stop(const Node&) {}
kpeter@244
  1169
    void reach(const Node&) {}
kpeter@244
  1170
    void discover(const Arc&) {}
kpeter@244
  1171
    void examine(const Arc&) {}
kpeter@244
  1172
    void leave(const Node&) {}
kpeter@244
  1173
    void backtrack(const Arc&) {}
alpar@100
  1174
alpar@100
  1175
    template <typename _Visitor>
alpar@100
  1176
    struct Constraints {
alpar@100
  1177
      void constraints() {
alpar@209
  1178
        Arc arc;
alpar@209
  1179
        Node node;
alpar@209
  1180
        visitor.start(node);
alpar@209
  1181
        visitor.stop(arc);
kpeter@244
  1182
        visitor.reach(node);
kpeter@244
  1183
        visitor.discover(arc);
kpeter@244
  1184
        visitor.examine(arc);
kpeter@244
  1185
        visitor.leave(node);
kpeter@244
  1186
        visitor.backtrack(arc);
alpar@100
  1187
      }
alpar@100
  1188
      _Visitor& visitor;
alpar@100
  1189
    };
alpar@100
  1190
  };
alpar@100
  1191
#endif
alpar@100
  1192
alpar@100
  1193
  /// \brief Default traits class of DfsVisit class.
alpar@100
  1194
  ///
alpar@100
  1195
  /// Default traits class of DfsVisit class.
kpeter@244
  1196
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
kpeter@492
  1197
  template<class GR>
alpar@100
  1198
  struct DfsVisitDefaultTraits {
alpar@100
  1199
kpeter@244
  1200
    /// \brief The type of the digraph the algorithm runs on.
kpeter@492
  1201
    typedef GR Digraph;
alpar@100
  1202
alpar@100
  1203
    /// \brief The type of the map that indicates which nodes are reached.
alpar@209
  1204
    ///
alpar@100
  1205
    /// The type of the map that indicates which nodes are reached.
kpeter@716
  1206
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
  1207
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
alpar@100
  1208
kpeter@301
  1209
    /// \brief Instantiates a ReachedMap.
alpar@100
  1210
    ///
kpeter@301
  1211
    /// This function instantiates a ReachedMap.
alpar@100
  1212
    /// \param digraph is the digraph, to which
kpeter@301
  1213
    /// we would like to define the ReachedMap.
alpar@100
  1214
    static ReachedMap *createReachedMap(const Digraph &digraph) {
alpar@100
  1215
      return new ReachedMap(digraph);
alpar@100
  1216
    }
alpar@100
  1217
alpar@100
  1218
  };
alpar@209
  1219
alpar@100
  1220
  /// \ingroup search
kpeter@244
  1221
  ///
kpeter@492
  1222
  /// \brief DFS algorithm class with visitor interface.
kpeter@244
  1223
  ///
kpeter@492
  1224
  /// This class provides an efficient implementation of the DFS algorithm
alpar@100
  1225
  /// with visitor interface.
alpar@100
  1226
  ///
kpeter@492
  1227
  /// The DfsVisit class provides an alternative interface to the Dfs
alpar@100
  1228
  /// class. It works with callback mechanism, the DfsVisit object calls
kpeter@244
  1229
  /// the member functions of the \c Visitor class on every DFS event.
alpar@100
  1230
  ///
kpeter@252
  1231
  /// This interface of the DFS algorithm should be used in special cases
kpeter@252
  1232
  /// when extra actions have to be performed in connection with certain
kpeter@252
  1233
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
kpeter@252
  1234
  /// instead.
kpeter@252
  1235
  ///
kpeter@492
  1236
  /// \tparam GR The type of the digraph the algorithm runs on.
kpeter@492
  1237
  /// The default type is \ref ListDigraph.
kpeter@492
  1238
  /// The value of GR is not used directly by \ref DfsVisit,
kpeter@492
  1239
  /// it is only passed to \ref DfsVisitDefaultTraits.
kpeter@492
  1240
  /// \tparam VS The Visitor type that is used by the algorithm.
kpeter@492
  1241
  /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
kpeter@244
  1242
  /// does not observe the DFS events. If you want to observe the DFS
kpeter@244
  1243
  /// events, you should implement your own visitor class.
kpeter@492
  1244
  /// \tparam TR Traits class to set various data types used by the
alpar@100
  1245
  /// algorithm. The default traits class is
kpeter@492
  1246
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>".
alpar@100
  1247
  /// See \ref DfsVisitDefaultTraits for the documentation of
kpeter@244
  1248
  /// a DFS visit traits class.
alpar@100
  1249
#ifdef DOXYGEN
kpeter@492
  1250
  template <typename GR, typename VS, typename TR>
alpar@100
  1251
#else
kpeter@492
  1252
  template <typename GR = ListDigraph,
kpeter@492
  1253
            typename VS = DfsVisitor<GR>,
kpeter@492
  1254
            typename TR = DfsVisitDefaultTraits<GR> >
alpar@100
  1255
#endif
alpar@100
  1256
  class DfsVisit {
alpar@100
  1257
  public:
alpar@209
  1258
kpeter@244
  1259
    ///The traits class.
kpeter@492
  1260
    typedef TR Traits;
alpar@100
  1261
kpeter@244
  1262
    ///The type of the digraph the algorithm runs on.
alpar@100
  1263
    typedef typename Traits::Digraph Digraph;
alpar@100
  1264
kpeter@244
  1265
    ///The visitor type used by the algorithm.
kpeter@492
  1266
    typedef VS Visitor;
alpar@100
  1267
kpeter@244
  1268
    ///The type of the map that indicates which nodes are reached.
alpar@100
  1269
    typedef typename Traits::ReachedMap ReachedMap;
alpar@100
  1270
alpar@100
  1271
  private:
alpar@100
  1272
alpar@100
  1273
    typedef typename Digraph::Node Node;
alpar@100
  1274
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
  1275
    typedef typename Digraph::Arc Arc;
alpar@100
  1276
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
  1277
kpeter@244
  1278
    //Pointer to the underlying digraph.
alpar@100
  1279
    const Digraph *_digraph;
kpeter@244
  1280
    //Pointer to the visitor object.
alpar@100
  1281
    Visitor *_visitor;
kpeter@244
  1282
    //Pointer to the map of reached status of the nodes.
alpar@100
  1283
    ReachedMap *_reached;
kpeter@244
  1284
    //Indicates if _reached is locally allocated (true) or not.
alpar@100
  1285
    bool local_reached;
alpar@100
  1286
alpar@100
  1287
    std::vector<typename Digraph::Arc> _stack;
alpar@100
  1288
    int _stack_head;
alpar@100
  1289
alpar@280
  1290
    //Creates the maps if necessary.
alpar@100
  1291
    void create_maps() {
alpar@100
  1292
      if(!_reached) {
alpar@209
  1293
        local_reached = true;
alpar@209
  1294
        _reached = Traits::createReachedMap(*_digraph);
alpar@100
  1295
      }
alpar@100
  1296
    }
alpar@100
  1297
alpar@100
  1298
  protected:
alpar@100
  1299
alpar@100
  1300
    DfsVisit() {}
alpar@209
  1301
alpar@100
  1302
  public:
alpar@100
  1303
alpar@100
  1304
    typedef DfsVisit Create;
alpar@100
  1305
kpeter@405
  1306
    /// \name Named Template Parameters
alpar@100
  1307
alpar@100
  1308
    ///@{
alpar@100
  1309
    template <class T>
kpeter@257
  1310
    struct SetReachedMapTraits : public Traits {
alpar@100
  1311
      typedef T ReachedMap;
alpar@100
  1312
      static ReachedMap *createReachedMap(const Digraph &digraph) {
deba@290
  1313
        LEMON_ASSERT(false, "ReachedMap is not initialized");
deba@290
  1314
        return 0; // ignore warnings
alpar@100
  1315
      }
alpar@100
  1316
    };
alpar@209
  1317
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@244
  1318
    /// ReachedMap type.
alpar@100
  1319
    ///
kpeter@244
  1320
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
alpar@100
  1321
    template <class T>
kpeter@257
  1322
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
kpeter@257
  1323
                                            SetReachedMapTraits<T> > {
kpeter@257
  1324
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
alpar@100
  1325
    };
alpar@100
  1326
    ///@}
alpar@100
  1327
alpar@209
  1328
  public:
alpar@209
  1329
alpar@100
  1330
    /// \brief Constructor.
alpar@100
  1331
    ///
alpar@100
  1332
    /// Constructor.
alpar@100
  1333
    ///
kpeter@244
  1334
    /// \param digraph The digraph the algorithm runs on.
kpeter@244
  1335
    /// \param visitor The visitor object of the algorithm.
alpar@209
  1336
    DfsVisit(const Digraph& digraph, Visitor& visitor)
alpar@100
  1337
      : _digraph(&digraph), _visitor(&visitor),
alpar@209
  1338
        _reached(0), local_reached(false) {}
alpar@209
  1339
alpar@100
  1340
    /// \brief Destructor.
alpar@100
  1341
    ~DfsVisit() {
alpar@100
  1342
      if(local_reached) delete _reached;
alpar@100
  1343
    }
alpar@100
  1344
kpeter@244
  1345
    /// \brief Sets the map that indicates which nodes are reached.
alpar@100
  1346
    ///
kpeter@244
  1347
    /// Sets the map that indicates which nodes are reached.
kpeter@405
  1348
    /// If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
  1349
    /// or \ref init(), an instance will be allocated automatically.
kpeter@405
  1350
    /// The destructor deallocates this automatically allocated map,
kpeter@405
  1351
    /// of course.
alpar@100
  1352
    /// \return <tt> (*this) </tt>
alpar@100
  1353
    DfsVisit &reachedMap(ReachedMap &m) {
alpar@100
  1354
      if(local_reached) {
alpar@209
  1355
        delete _reached;
alpar@209
  1356
        local_reached=false;
alpar@100
  1357
      }
alpar@100
  1358
      _reached = &m;
alpar@100
  1359
      return *this;
alpar@100
  1360
    }
alpar@100
  1361
alpar@100
  1362
  public:
kpeter@244
  1363
kpeter@405
  1364
    /// \name Execution Control
kpeter@405
  1365
    /// The simplest way to execute the DFS algorithm is to use one of the
kpeter@405
  1366
    /// member functions called \ref run(Node) "run()".\n
kpeter@713
  1367
    /// If you need better control on the execution, you have to call
kpeter@713
  1368
    /// \ref init() first, then you can add a source node with \ref addSource()
kpeter@405
  1369
    /// and perform the actual computation with \ref start().
kpeter@405
  1370
    /// This procedure can be repeated if there are nodes that have not
kpeter@405
  1371
    /// been reached.
alpar@100
  1372
alpar@100
  1373
    /// @{
kpeter@244
  1374
alpar@100
  1375
    /// \brief Initializes the internal data structures.
alpar@100
  1376
    ///
alpar@100
  1377
    /// Initializes the internal data structures.
alpar@100
  1378
    void init() {
alpar@100
  1379
      create_maps();
alpar@100
  1380
      _stack.resize(countNodes(*_digraph));
alpar@100
  1381
      _stack_head = -1;
alpar@100
  1382
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
alpar@209
  1383
        _reached->set(u, false);
alpar@100
  1384
      }
alpar@100
  1385
    }
alpar@209
  1386
kpeter@405
  1387
    /// \brief Adds a new source node.
alpar@100
  1388
    ///
kpeter@405
  1389
    /// Adds a new source node to the set of nodes to be processed.
kpeter@244
  1390
    ///
kpeter@405
  1391
    /// \pre The stack must be empty. Otherwise the algorithm gives
kpeter@405
  1392
    /// wrong results. (One of the outgoing arcs of all the source nodes
kpeter@405
  1393
    /// except for the last one will not be visited and distances will
kpeter@405
  1394
    /// also be wrong.)
kpeter@244
  1395
    void addSource(Node s)
kpeter@244
  1396
    {
kpeter@244
  1397
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
alpar@100
  1398
      if(!(*_reached)[s]) {
alpar@209
  1399
          _reached->set(s,true);
alpar@209
  1400
          _visitor->start(s);
alpar@209
  1401
          _visitor->reach(s);
alpar@209
  1402
          Arc e;
alpar@209
  1403
          _digraph->firstOut(e, s);
alpar@209
  1404
          if (e != INVALID) {
alpar@209
  1405
            _stack[++_stack_head] = e;
alpar@209
  1406
          } else {
alpar@209
  1407
            _visitor->leave(s);
deba@419
  1408
            _visitor->stop(s);
alpar@209
  1409
          }
alpar@209
  1410
        }
alpar@100
  1411
    }
alpar@209
  1412
alpar@100
  1413
    /// \brief Processes the next arc.
alpar@100
  1414
    ///
alpar@100
  1415
    /// Processes the next arc.
alpar@100
  1416
    ///
alpar@100
  1417
    /// \return The processed arc.
alpar@100
  1418
    ///
kpeter@244
  1419
    /// \pre The stack must not be empty.
alpar@209
  1420
    Arc processNextArc() {
alpar@100
  1421
      Arc e = _stack[_stack_head];
alpar@100
  1422
      Node m = _digraph->target(e);
alpar@100
  1423
      if(!(*_reached)[m]) {
alpar@209
  1424
        _visitor->discover(e);
alpar@209
  1425
        _visitor->reach(m);
alpar@209
  1426
        _reached->set(m, true);
alpar@209
  1427
        _digraph->firstOut(_stack[++_stack_head], m);
alpar@100
  1428
      } else {
alpar@209
  1429
        _visitor->examine(e);
alpar@209
  1430
        m = _digraph->source(e);
alpar@209
  1431
        _digraph->nextOut(_stack[_stack_head]);
alpar@100
  1432
      }
alpar@100
  1433
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
alpar@209
  1434
        _visitor->leave(m);
alpar@209
  1435
        --_stack_head;
alpar@209
  1436
        if (_stack_head >= 0) {
alpar@209
  1437
          _visitor->backtrack(_stack[_stack_head]);
alpar@209
  1438
          m = _digraph->source(_stack[_stack_head]);
alpar@209
  1439
          _digraph->nextOut(_stack[_stack_head]);
alpar@209
  1440
        } else {
alpar@209
  1441
          _visitor->stop(m);
alpar@209
  1442
        }
alpar@100
  1443
      }
alpar@100
  1444
      return e;
alpar@100
  1445
    }
alpar@100
  1446
alpar@100
  1447
    /// \brief Next arc to be processed.
alpar@100
  1448
    ///
alpar@100
  1449
    /// Next arc to be processed.
alpar@100
  1450
    ///
alpar@100
  1451
    /// \return The next arc to be processed or INVALID if the stack is
alpar@100
  1452
    /// empty.
kpeter@244
  1453
    Arc nextArc() const {
alpar@100
  1454
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
alpar@100
  1455
    }
alpar@100
  1456
alpar@100
  1457
    /// \brief Returns \c false if there are nodes
kpeter@244
  1458
    /// to be processed.
alpar@100
  1459
    ///
alpar@100
  1460
    /// Returns \c false if there are nodes
kpeter@244
  1461
    /// to be processed in the queue (stack).
kpeter@244
  1462
    bool emptyQueue() const { return _stack_head < 0; }
alpar@100
  1463
alpar@100
  1464
    /// \brief Returns the number of the nodes to be processed.
alpar@100
  1465
    ///
kpeter@244
  1466
    /// Returns the number of the nodes to be processed in the queue (stack).
kpeter@244
  1467
    int queueSize() const { return _stack_head + 1; }
alpar@209
  1468
alpar@100
  1469
    /// \brief Executes the algorithm.
alpar@100
  1470
    ///
alpar@100
  1471
    /// Executes the algorithm.
alpar@100
  1472
    ///
kpeter@244
  1473
    /// This method runs the %DFS algorithm from the root node
kpeter@244
  1474
    /// in order to compute the %DFS path to each node.
kpeter@244
  1475
    ///
kpeter@244
  1476
    /// The algorithm computes
kpeter@244
  1477
    /// - the %DFS tree,
kpeter@244
  1478
    /// - the distance of each node from the root in the %DFS tree.
kpeter@244
  1479
    ///
kpeter@244
  1480
    /// \pre init() must be called and a root node should be
kpeter@244
  1481
    /// added with addSource() before using this function.
kpeter@244
  1482
    ///
kpeter@244
  1483
    /// \note <tt>d.start()</tt> is just a shortcut of the following code.
kpeter@244
  1484
    /// \code
kpeter@244
  1485
    ///   while ( !d.emptyQueue() ) {
kpeter@244
  1486
    ///     d.processNextArc();
kpeter@244
  1487
    ///   }
kpeter@244
  1488
    /// \endcode
alpar@100
  1489
    void start() {
alpar@100
  1490
      while ( !emptyQueue() ) processNextArc();
alpar@100
  1491
    }
alpar@209
  1492
kpeter@244
  1493
    /// \brief Executes the algorithm until the given target node is reached.
alpar@100
  1494
    ///
kpeter@244
  1495
    /// Executes the algorithm until the given target node is reached.
alpar@100
  1496
    ///
kpeter@244
  1497
    /// This method runs the %DFS algorithm from the root node
kpeter@286
  1498
    /// in order to compute the DFS path to \c t.
kpeter@244
  1499
    ///
kpeter@244
  1500
    /// The algorithm computes
kpeter@286
  1501
    /// - the %DFS path to \c t,
kpeter@286
  1502
    /// - the distance of \c t from the root in the %DFS tree.
kpeter@244
  1503
    ///
kpeter@244
  1504
    /// \pre init() must be called and a root node should be added
alpar@100
  1505
    /// with addSource() before using this function.
kpeter@286
  1506
    void start(Node t) {
kpeter@286
  1507
      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
alpar@209
  1508
        processNextArc();
alpar@100
  1509
    }
alpar@209
  1510
alpar@100
  1511
    /// \brief Executes the algorithm until a condition is met.
alpar@100
  1512
    ///
alpar@100
  1513
    /// Executes the algorithm until a condition is met.
alpar@100
  1514
    ///
kpeter@244
  1515
    /// This method runs the %DFS algorithm from the root node
kpeter@244
  1516
    /// until an arc \c a with <tt>am[a]</tt> true is found.
kpeter@244
  1517
    ///
kpeter@244
  1518
    /// \param am A \c bool (or convertible) arc map. The algorithm
kpeter@244
  1519
    /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
kpeter@244
  1520
    ///
kpeter@244
  1521
    /// \return The reached arc \c a with <tt>am[a]</tt> true or
kpeter@244
  1522
    /// \c INVALID if no such arc was found.
kpeter@244
  1523
    ///
kpeter@244
  1524
    /// \pre init() must be called and a root node should be added
alpar@100
  1525
    /// with addSource() before using this function.
alpar@100
  1526
    ///
kpeter@244
  1527
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
alpar@100
  1528
    /// not a node map.
kpeter@244
  1529
    template <typename AM>
kpeter@244
  1530
    Arc start(const AM &am) {
kpeter@244
  1531
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
alpar@100
  1532
        processNextArc();
alpar@100
  1533
      return emptyQueue() ? INVALID : _stack[_stack_head];
alpar@100
  1534
    }
alpar@100
  1535
kpeter@286
  1536
    /// \brief Runs the algorithm from the given source node.
alpar@100
  1537
    ///
kpeter@244
  1538
    /// This method runs the %DFS algorithm from node \c s.
kpeter@244
  1539
    /// in order to compute the DFS path to each node.
kpeter@244
  1540
    ///
kpeter@244
  1541
    /// The algorithm computes
kpeter@244
  1542
    /// - the %DFS tree,
kpeter@244
  1543
    /// - the distance of each node from the root in the %DFS tree.
kpeter@244
  1544
    ///
kpeter@244
  1545
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
alpar@100
  1546
    ///\code
alpar@100
  1547
    ///   d.init();
alpar@100
  1548
    ///   d.addSource(s);
alpar@100
  1549
    ///   d.start();
alpar@100
  1550
    ///\endcode
alpar@100
  1551
    void run(Node s) {
alpar@100
  1552
      init();
alpar@100
  1553
      addSource(s);
alpar@100
  1554
      start();
alpar@100
  1555
    }
alpar@100
  1556
kpeter@244
  1557
    /// \brief Finds the %DFS path between \c s and \c t.
kpeter@244
  1558
kpeter@244
  1559
    /// This method runs the %DFS algorithm from node \c s
kpeter@286
  1560
    /// in order to compute the DFS path to node \c t
kpeter@286
  1561
    /// (it stops searching when \c t is processed).
kpeter@244
  1562
    ///
kpeter@286
  1563
    /// \return \c true if \c t is reachable form \c s.
kpeter@244
  1564
    ///
kpeter@244
  1565
    /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
kpeter@244
  1566
    /// just a shortcut of the following code.
kpeter@244
  1567
    ///\code
kpeter@244
  1568
    ///   d.init();
kpeter@244
  1569
    ///   d.addSource(s);
kpeter@244
  1570
    ///   d.start(t);
kpeter@244
  1571
    ///\endcode
kpeter@286
  1572
    bool run(Node s,Node t) {
kpeter@244
  1573
      init();
kpeter@244
  1574
      addSource(s);
kpeter@244
  1575
      start(t);
kpeter@286
  1576
      return reached(t);
kpeter@244
  1577
    }
kpeter@244
  1578
kpeter@244
  1579
    /// \brief Runs the algorithm to visit all nodes in the digraph.
alpar@209
  1580
alpar@100
  1581
    /// This method runs the %DFS algorithm in order to
kpeter@244
  1582
    /// compute the %DFS path to each node.
alpar@100
  1583
    ///
kpeter@244
  1584
    /// The algorithm computes
kpeter@405
  1585
    /// - the %DFS tree (forest),
kpeter@405
  1586
    /// - the distance of each node from the root(s) in the %DFS tree.
kpeter@244
  1587
    ///
kpeter@244
  1588
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
alpar@100
  1589
    ///\code
kpeter@244
  1590
    ///   d.init();
kpeter@244
  1591
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
kpeter@244
  1592
    ///     if (!d.reached(n)) {
kpeter@244
  1593
    ///       d.addSource(n);
kpeter@244
  1594
    ///       d.start();
kpeter@244
  1595
    ///     }
kpeter@244
  1596
    ///   }
alpar@100
  1597
    ///\endcode
alpar@100
  1598
    void run() {
alpar@100
  1599
      init();
alpar@100
  1600
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
alpar@100
  1601
        if (!reached(it)) {
alpar@100
  1602
          addSource(it);
alpar@100
  1603
          start();
alpar@100
  1604
        }
alpar@100
  1605
      }
alpar@100
  1606
    }
kpeter@244
  1607
alpar@100
  1608
    ///@}
alpar@100
  1609
alpar@100
  1610
    /// \name Query Functions
kpeter@405
  1611
    /// The results of the DFS algorithm can be obtained using these
alpar@100
  1612
    /// functions.\n
kpeter@405
  1613
    /// Either \ref run(Node) "run()" or \ref start() should be called
kpeter@405
  1614
    /// before using them.
kpeter@405
  1615
alpar@100
  1616
    ///@{
kpeter@244
  1617
kpeter@716
  1618
    /// \brief Checks if the given node is reached from the root(s).
alpar@100
  1619
    ///
kpeter@405
  1620
    /// Returns \c true if \c v is reached from the root(s).
kpeter@405
  1621
    ///
kpeter@405
  1622
    /// \pre Either \ref run(Node) "run()" or \ref init()
alpar@100
  1623
    /// must be called before using this function.
kpeter@420
  1624
    bool reached(Node v) const { return (*_reached)[v]; }
kpeter@244
  1625
alpar@100
  1626
    ///@}
kpeter@244
  1627
alpar@100
  1628
  };
alpar@100
  1629
alpar@100
  1630
} //END OF NAMESPACE LEMON
alpar@100
  1631
alpar@100
  1632
#endif