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