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