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