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