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