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