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