lemon/dfs.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 13 Jul 2008 16:46:56 +0100
changeset 208 4317d277ba21
parent 157 2ccc1afc2c52
child 209 765619b7cbb2
permissions -rw-r--r--
Better source unifier

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