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