lemon/dfs.h
author deba
Wed, 07 Mar 2007 12:00:59 +0000
changeset 2400 b199ded24c19
parent 2386 81b47fc5c444
child 2439 3f1c7a6c33cd
permissions -rw-r--r--
Steiner 2-approximation demo
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@1865
   567
    ///will stop when it reaches an edge \c e with \code em[e]==true \endcode.
athos@1443
   568
    ///
deba@1749
   569
    ///\warning Contrary to \ref Dfs and \ref Dijkstra, \c em is an edge map,
alpar@1233
   570
    ///not a node map.
deba@1749
   571
    template<class EM>
deba@1749
   572
    void start(const EM &em)
deba@1749
   573
    {
deba@1749
   574
      while ( !emptyQueue() && !em[_stack[_stack_head]] ) processNextEdge();
deba@1749
   575
    }
deba@1749
   576
deba@1981
   577
    ///Runs %DFS algorithm to visit all nodes in the graph.
deba@1981
   578
    
deba@1981
   579
    ///This method runs the %DFS algorithm in order to
deba@1981
   580
    ///compute the
deba@1981
   581
    ///%DFS path to each node. The algorithm computes
deba@1981
   582
    ///- The %DFS tree.
deba@1981
   583
    ///- The distance of each node from the root in the %DFS tree.
deba@1981
   584
    ///
deba@1981
   585
    ///\note d.run() is just a shortcut of the following code.
deba@1981
   586
    ///\code
deba@1981
   587
    ///  d.init();
deba@1981
   588
    ///  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1981
   589
    ///    if (!d.reached(it)) {
deba@1981
   590
    ///      d.addSource(it);
deba@1981
   591
    ///      d.start();
deba@1981
   592
    ///    }
deba@1981
   593
    ///  }
deba@1981
   594
    ///\endcode
deba@1981
   595
    void run() {
deba@1981
   596
      init();
deba@1981
   597
      for (NodeIt it(*G); it != INVALID; ++it) {
deba@1981
   598
        if (!reached(it)) {
deba@1981
   599
          addSource(it);
deba@1981
   600
          start();
deba@1981
   601
        }
deba@1981
   602
      }
deba@1981
   603
    }
deba@1981
   604
alpar@1218
   605
    ///Runs %DFS algorithm from node \c s.
alpar@1218
   606
    
alpar@1218
   607
    ///This method runs the %DFS algorithm from a root node \c s
alpar@1218
   608
    ///in order to
alpar@1218
   609
    ///compute the
alpar@1218
   610
    ///%DFS path to each node. The algorithm computes
alpar@1218
   611
    ///- The %DFS tree.
athos@1443
   612
    ///- The distance of each node from the root in the %DFS tree.
alpar@1218
   613
    ///
alpar@1218
   614
    ///\note d.run(s) is just a shortcut of the following code.
alpar@1218
   615
    ///\code
alpar@1218
   616
    ///  d.init();
alpar@1218
   617
    ///  d.addSource(s);
alpar@1218
   618
    ///  d.start();
alpar@1218
   619
    ///\endcode
alpar@1218
   620
    void run(Node s) {
alpar@1218
   621
      init();
alpar@1218
   622
      addSource(s);
alpar@1218
   623
      start();
alpar@1218
   624
    }
alpar@1218
   625
    
alpar@1218
   626
    ///Finds the %DFS path between \c s and \c t.
alpar@1218
   627
    
alpar@1218
   628
    ///Finds the %DFS path between \c s and \c t.
alpar@1218
   629
    ///
alpar@1218
   630
    ///\return The length of the %DFS s---t path if there exists one,
alpar@1218
   631
    ///0 otherwise.
athos@1540
   632
    ///\note Apart from the return value, d.run(s,t) is
alpar@1218
   633
    ///just a shortcut of the following code.
alpar@1218
   634
    ///\code
alpar@1218
   635
    ///  d.init();
alpar@1218
   636
    ///  d.addSource(s);
alpar@1218
   637
    ///  d.start(t);
alpar@1218
   638
    ///\endcode
alpar@1218
   639
    int run(Node s,Node t) {
alpar@1218
   640
      init();
alpar@1218
   641
      addSource(s);
alpar@1218
   642
      start(t);
alpar@1233
   643
      return reached(t)?_stack_head+1:0;
alpar@1218
   644
    }
alpar@1218
   645
    
alpar@1218
   646
    ///@}
alpar@1218
   647
alpar@1218
   648
    ///\name Query Functions
alpar@1218
   649
    ///The result of the %DFS algorithm can be obtained using these
alpar@1218
   650
    ///functions.\n
alpar@1218
   651
    ///Before the use of these functions,
alpar@1218
   652
    ///either run() or start() must be called.
alpar@1218
   653
    
alpar@1218
   654
    ///@{
alpar@1218
   655
deba@2335
   656
    typedef PredMapPath<Graph, PredMap> Path;
deba@2335
   657
deba@2335
   658
    ///Gives back the shortest path.
alpar@1283
   659
    
deba@2335
   660
    ///Gives back the shortest path.
deba@2335
   661
    ///\pre The \c t should be reachable from the source.
deba@2335
   662
    Path path(Node t) 
alpar@1283
   663
    {
deba@2335
   664
      return Path(*G, *_pred, t);
alpar@1283
   665
    }
alpar@1283
   666
alpar@1218
   667
    ///The distance of a node from the root(s).
alpar@1218
   668
alpar@1218
   669
    ///Returns the distance of a node from the root(s).
alpar@780
   670
    ///\pre \ref run() must be called before using this function.
deba@1981
   671
    ///\warning If node \c v is unreachable from the root(s) then the return 
deba@1981
   672
    ///value of this funcion is undefined.
alpar@1218
   673
    int dist(Node v) const { return (*_dist)[v]; }
alpar@780
   674
alpar@1218
   675
    ///Returns the 'previous edge' of the %DFS tree.
alpar@780
   676
alpar@1218
   677
    ///For a node \c v it returns the 'previous edge'
alpar@1218
   678
    ///of the %DFS path,
alpar@1218
   679
    ///i.e. it returns the last edge of a %DFS path from the root(s) to \c
alpar@780
   680
    ///v. It is \ref INVALID
alpar@1218
   681
    ///if \c v is unreachable from the root(s) or \c v is a root. The
alpar@781
   682
    ///%DFS tree used here is equal to the %DFS tree used in
alpar@1631
   683
    ///\ref predNode().
alpar@1218
   684
    ///\pre Either \ref run() or \ref start() must be called before using
alpar@780
   685
    ///this function.
deba@1763
   686
    Edge predEdge(Node v) const { return (*_pred)[v];}
alpar@780
   687
alpar@781
   688
    ///Returns the 'previous node' of the %DFS tree.
alpar@780
   689
alpar@1218
   690
    ///For a node \c v it returns the 'previous node'
alpar@1218
   691
    ///of the %DFS tree,
alpar@1218
   692
    ///i.e. it returns the last but one node from a %DFS path from the
alpar@2156
   693
    ///root(s) to \c v.
alpar@1218
   694
    ///It is INVALID if \c v is unreachable from the root(s) or
alpar@1218
   695
    ///if \c v itself a root.
alpar@1218
   696
    ///The %DFS tree used here is equal to the %DFS
deba@1763
   697
    ///tree used in \ref predEdge().
alpar@1218
   698
    ///\pre Either \ref run() or \ref start() must be called before
alpar@780
   699
    ///using this function.
alpar@1218
   700
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@1218
   701
				  G->source((*_pred)[v]); }
alpar@780
   702
    
alpar@1218
   703
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   704
alpar@1218
   705
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   706
    ///\pre Either \ref run() or \ref init() must
alpar@780
   707
    ///be called before using this function.
alpar@1218
   708
    const DistMap &distMap() const { return *_dist;}
alpar@780
   709
 
alpar@1218
   710
    ///Returns a reference to the %DFS edge-tree map.
alpar@780
   711
alpar@780
   712
    ///Returns a reference to the NodeMap of the edges of the
alpar@781
   713
    ///%DFS tree.
alpar@1218
   714
    ///\pre Either \ref run() or \ref init()
alpar@1218
   715
    ///must be called before using this function.
alpar@1218
   716
    const PredMap &predMap() const { return *_pred;}
alpar@780
   717
 
alpar@780
   718
    ///Checks if a node is reachable from the root.
alpar@780
   719
athos@1438
   720
    ///Returns \c true if \c v is reachable from the root(s).
athos@1438
   721
    ///\warning The source nodes are inditated as unreachable.
alpar@1218
   722
    ///\pre Either \ref run() or \ref start()
alpar@1218
   723
    ///must be called before using this function.
alpar@780
   724
    ///
alpar@1218
   725
    bool reached(Node v) { return (*_reached)[v]; }
alpar@1218
   726
    
alpar@1218
   727
    ///@}
alpar@1218
   728
  };
alpar@1218
   729
alpar@1218
   730
  ///Default traits class of Dfs function.
alpar@1218
   731
alpar@1218
   732
  ///Default traits class of Dfs function.
alpar@1218
   733
  ///\param GR Graph type.
alpar@1218
   734
  template<class GR>
alpar@1218
   735
  struct DfsWizardDefaultTraits
alpar@1218
   736
  {
alpar@1218
   737
    ///The graph type the algorithm runs on. 
alpar@1218
   738
    typedef GR Graph;
alpar@1218
   739
    ///\brief The type of the map that stores the last
alpar@1218
   740
    ///edges of the %DFS paths.
alpar@1218
   741
    /// 
alpar@1218
   742
    ///The type of the map that stores the last
alpar@1218
   743
    ///edges of the %DFS paths.
alpar@2260
   744
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@780
   745
    ///
alpar@1218
   746
    typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
alpar@1218
   747
    ///Instantiates a PredMap.
alpar@1218
   748
 
alpar@1218
   749
    ///This function instantiates a \ref PredMap. 
alpar@1536
   750
    ///\param g is the graph, to which we would like to define the PredMap.
alpar@1218
   751
    ///\todo The graph alone may be insufficient to initialize
alpar@1536
   752
#ifdef DOXYGEN
alpar@1536
   753
    static PredMap *createPredMap(const GR &g) 
alpar@1536
   754
#else
alpar@1367
   755
    static PredMap *createPredMap(const GR &) 
alpar@1536
   756
#endif
alpar@1218
   757
    {
alpar@1218
   758
      return new PredMap();
alpar@1218
   759
    }
alpar@1218
   760
alpar@1218
   761
    ///The type of the map that indicates which nodes are processed.
alpar@1218
   762
 
alpar@1218
   763
    ///The type of the map that indicates which nodes are processed.
alpar@2260
   764
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   765
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   766
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   767
    ///Instantiates a ProcessedMap.
alpar@1218
   768
 
alpar@1218
   769
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
   770
    ///\param g is the graph, to which
alpar@1218
   771
    ///we would like to define the \ref ProcessedMap
alpar@1536
   772
#ifdef DOXYGEN
alpar@1536
   773
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   774
#else
alpar@1367
   775
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   776
#endif
alpar@1218
   777
    {
alpar@1218
   778
      return new ProcessedMap();
alpar@1218
   779
    }
alpar@1218
   780
    ///The type of the map that indicates which nodes are reached.
alpar@1218
   781
 
alpar@1218
   782
    ///The type of the map that indicates which nodes are reached.
alpar@2260
   783
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   784
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   785
    typedef typename Graph::template NodeMap<bool> ReachedMap;
alpar@1218
   786
    ///Instantiates a ReachedMap.
alpar@1218
   787
 
alpar@1218
   788
    ///This function instantiates a \ref ReachedMap. 
alpar@1218
   789
    ///\param G is the graph, to which
alpar@1218
   790
    ///we would like to define the \ref ReachedMap.
alpar@1218
   791
    static ReachedMap *createReachedMap(const GR &G)
alpar@1218
   792
    {
alpar@1218
   793
      return new ReachedMap(G);
alpar@1218
   794
    }
alpar@1218
   795
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   796
 
alpar@1218
   797
    ///The type of the map that stores the dists of the nodes.
alpar@2260
   798
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   799
    ///
alpar@1218
   800
    typedef NullMap<typename Graph::Node,int> DistMap;
alpar@1218
   801
    ///Instantiates a DistMap.
alpar@1218
   802
 
alpar@1218
   803
    ///This function instantiates a \ref DistMap. 
alpar@1536
   804
    ///\param g is the graph, to which we would like to define the \ref DistMap
alpar@1536
   805
#ifdef DOXYGEN
alpar@1536
   806
    static DistMap *createDistMap(const GR &g)
alpar@1536
   807
#else
alpar@1367
   808
    static DistMap *createDistMap(const GR &)
alpar@1536
   809
#endif
alpar@1218
   810
    {
alpar@1218
   811
      return new DistMap();
alpar@1218
   812
    }
alpar@1218
   813
  };
alpar@1218
   814
  
alpar@1218
   815
  /// Default traits used by \ref DfsWizard
alpar@1218
   816
alpar@1218
   817
  /// To make it easier to use Dfs algorithm
alpar@1218
   818
  ///we have created a wizard class.
alpar@1218
   819
  /// This \ref DfsWizard class needs default traits,
alpar@1218
   820
  ///as well as the \ref Dfs class.
alpar@1218
   821
  /// The \ref DfsWizardBase is a class to be the default traits of the
alpar@1218
   822
  /// \ref DfsWizard class.
alpar@1218
   823
  template<class GR>
alpar@1218
   824
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
alpar@1218
   825
  {
alpar@1218
   826
alpar@1218
   827
    typedef DfsWizardDefaultTraits<GR> Base;
alpar@1218
   828
  protected:
alpar@1218
   829
    /// Type of the nodes in the graph.
alpar@1218
   830
    typedef typename Base::Graph::Node Node;
alpar@1218
   831
alpar@1218
   832
    /// Pointer to the underlying graph.
alpar@1218
   833
    void *_g;
alpar@1218
   834
    ///Pointer to the map of reached nodes.
alpar@1218
   835
    void *_reached;
alpar@1218
   836
    ///Pointer to the map of processed nodes.
alpar@1218
   837
    void *_processed;
alpar@1218
   838
    ///Pointer to the map of predecessors edges.
alpar@1218
   839
    void *_pred;
alpar@1218
   840
    ///Pointer to the map of distances.
alpar@1218
   841
    void *_dist;
alpar@1218
   842
    ///Pointer to the source node.
alpar@1218
   843
    Node _source;
alpar@1218
   844
    
alpar@1218
   845
    public:
alpar@1218
   846
    /// Constructor.
alpar@1218
   847
    
alpar@1218
   848
    /// This constructor does not require parameters, therefore it initiates
alpar@1218
   849
    /// all of the attributes to default values (0, INVALID).
alpar@1218
   850
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
alpar@1218
   851
			   _dist(0), _source(INVALID) {}
alpar@1218
   852
alpar@1218
   853
    /// Constructor.
alpar@1218
   854
    
alpar@1218
   855
    /// This constructor requires some parameters,
alpar@1218
   856
    /// listed in the parameters list.
alpar@1218
   857
    /// Others are initiated to 0.
alpar@1218
   858
    /// \param g is the initial value of  \ref _g
alpar@1218
   859
    /// \param s is the initial value of  \ref _source
alpar@1218
   860
    DfsWizardBase(const GR &g, Node s=INVALID) :
deba@2386
   861
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
deba@2386
   862
      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
alpar@1218
   863
alpar@1218
   864
  };
alpar@1218
   865
  
athos@1443
   866
  /// A class to make the usage of the Dfs algorithm easier
alpar@1218
   867
athos@1443
   868
  /// This class is created to make it easier to use the Dfs algorithm.
alpar@1218
   869
  /// It uses the functions and features of the plain \ref Dfs,
alpar@1218
   870
  /// but it is much simpler to use it.
alpar@1218
   871
  ///
alpar@1218
   872
  /// Simplicity means that the way to change the types defined
alpar@1218
   873
  /// in the traits class is based on functions that returns the new class
alpar@1218
   874
  /// and not on templatable built-in classes.
alpar@1218
   875
  /// When using the plain \ref Dfs
alpar@1218
   876
  /// the new class with the modified type comes from
alpar@1218
   877
  /// the original class by using the ::
alpar@1218
   878
  /// operator. In the case of \ref DfsWizard only
alpar@1218
   879
  /// a function have to be called and it will
alpar@1218
   880
  /// return the needed class.
alpar@1218
   881
  ///
alpar@1218
   882
  /// It does not have own \ref run method. When its \ref run method is called
athos@1438
   883
  /// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run
alpar@1218
   884
  /// method of it.
alpar@1218
   885
  template<class TR>
alpar@1218
   886
  class DfsWizard : public TR
alpar@1218
   887
  {
alpar@1218
   888
    typedef TR Base;
alpar@1218
   889
alpar@1218
   890
    ///The type of the underlying graph.
alpar@1218
   891
    typedef typename TR::Graph Graph;
alpar@1218
   892
    //\e
alpar@1218
   893
    typedef typename Graph::Node Node;
alpar@1218
   894
    //\e
alpar@1218
   895
    typedef typename Graph::NodeIt NodeIt;
alpar@1218
   896
    //\e
alpar@1218
   897
    typedef typename Graph::Edge Edge;
alpar@1218
   898
    //\e
alpar@1218
   899
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1218
   900
    
alpar@1218
   901
    ///\brief The type of the map that stores
alpar@1218
   902
    ///the reached nodes
alpar@1218
   903
    typedef typename TR::ReachedMap ReachedMap;
alpar@1218
   904
    ///\brief The type of the map that stores
alpar@1218
   905
    ///the processed nodes
alpar@1218
   906
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@1218
   907
    ///\brief The type of the map that stores the last
alpar@1218
   908
    ///edges of the %DFS paths.
alpar@1218
   909
    typedef typename TR::PredMap PredMap;
athos@1443
   910
    ///The type of the map that stores the distances of the nodes.
alpar@1218
   911
    typedef typename TR::DistMap DistMap;
alpar@1218
   912
deba@2019
   913
  public:
alpar@1218
   914
    /// Constructor.
alpar@1218
   915
    DfsWizard() : TR() {}
alpar@1218
   916
alpar@1218
   917
    /// Constructor that requires parameters.
alpar@1218
   918
alpar@1218
   919
    /// Constructor that requires parameters.
alpar@1218
   920
    /// These parameters will be the default values for the traits class.
alpar@1218
   921
    DfsWizard(const Graph &g, Node s=INVALID) :
alpar@1218
   922
      TR(g,s) {}
alpar@1218
   923
alpar@1218
   924
    ///Copy constructor
alpar@1218
   925
    DfsWizard(const TR &b) : TR(b) {}
alpar@1218
   926
alpar@1218
   927
    ~DfsWizard() {}
alpar@1218
   928
alpar@1218
   929
    ///Runs Dfs algorithm from a given node.
alpar@1218
   930
    
alpar@1218
   931
    ///Runs Dfs algorithm from a given node.
alpar@1218
   932
    ///The node can be given by the \ref source function.
alpar@1218
   933
    void run()
alpar@1218
   934
    {
alpar@1218
   935
      if(Base::_source==INVALID) throw UninitializedParameter();
deba@2386
   936
      Dfs<Graph,TR> alg(*reinterpret_cast<const Graph*>(Base::_g));
deba@2386
   937
      if(Base::_reached) 
deba@2386
   938
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
deba@2386
   939
      if(Base::_processed) 
deba@2386
   940
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
deba@2386
   941
      if(Base::_pred) 
deba@2386
   942
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
deba@2386
   943
      if(Base::_dist) 
deba@2386
   944
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
alpar@1218
   945
      alg.run(Base::_source);
alpar@1218
   946
    }
alpar@1218
   947
alpar@1218
   948
    ///Runs Dfs algorithm from the given node.
alpar@1218
   949
alpar@1218
   950
    ///Runs Dfs algorithm from the given node.
alpar@1218
   951
    ///\param s is the given source.
alpar@1218
   952
    void run(Node s)
alpar@1218
   953
    {
alpar@1218
   954
      Base::_source=s;
alpar@1218
   955
      run();
alpar@1218
   956
    }
alpar@1218
   957
alpar@1218
   958
    template<class T>
alpar@1218
   959
    struct DefPredMapBase : public Base {
alpar@1218
   960
      typedef T PredMap;
alpar@1367
   961
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
   962
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1218
   963
    };
alpar@1218
   964
    
alpar@1218
   965
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   966
    ///function for setting PredMap type
alpar@1218
   967
    ///
alpar@1218
   968
    /// \ref named-templ-param "Named parameter"
alpar@1218
   969
    ///function for setting PredMap type
alpar@1218
   970
    ///
alpar@1218
   971
    template<class T>
alpar@1218
   972
    DfsWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@1218
   973
    {
deba@2386
   974
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
   975
      return DfsWizard<DefPredMapBase<T> >(*this);
alpar@1218
   976
    }
alpar@1218
   977
    
alpar@1218
   978
 
alpar@1218
   979
    template<class T>
alpar@1218
   980
    struct DefReachedMapBase : public Base {
alpar@1218
   981
      typedef T ReachedMap;
alpar@1367
   982
      static ReachedMap *createReachedMap(const Graph &) { return 0; };
alpar@1236
   983
      DefReachedMapBase(const TR &b) : TR(b) {}
alpar@1218
   984
    };
alpar@1218
   985
    
alpar@1218
   986
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   987
    ///function for setting ReachedMap
alpar@1218
   988
    ///
alpar@1218
   989
    /// \ref named-templ-param "Named parameter"
alpar@1218
   990
    ///function for setting ReachedMap
alpar@1218
   991
    ///
alpar@1218
   992
    template<class T>
alpar@1218
   993
    DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
alpar@1218
   994
    {
deba@2386
   995
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
   996
      return DfsWizard<DefReachedMapBase<T> >(*this);
alpar@1218
   997
    }
alpar@1218
   998
    
alpar@1218
   999
alpar@1218
  1000
    template<class T>
alpar@1218
  1001
    struct DefProcessedMapBase : public Base {
alpar@1218
  1002
      typedef T ProcessedMap;
alpar@1367
  1003
      static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
alpar@1236
  1004
      DefProcessedMapBase(const TR &b) : TR(b) {}
alpar@1218
  1005
    };
alpar@1218
  1006
    
alpar@1218
  1007
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1008
    ///function for setting ProcessedMap
alpar@1218
  1009
    ///
alpar@1218
  1010
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1011
    ///function for setting ProcessedMap
alpar@1218
  1012
    ///
alpar@1218
  1013
    template<class T>
alpar@1218
  1014
    DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
alpar@1218
  1015
    {
deba@2386
  1016
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
  1017
      return DfsWizard<DefProcessedMapBase<T> >(*this);
alpar@1218
  1018
    }
alpar@1218
  1019
    
alpar@1218
  1020
    template<class T>
alpar@1218
  1021
    struct DefDistMapBase : public Base {
alpar@1218
  1022
      typedef T DistMap;
alpar@1367
  1023
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
  1024
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1218
  1025
    };
alpar@1218
  1026
    
alpar@1218
  1027
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1028
    ///function for setting DistMap type
alpar@1218
  1029
    ///
alpar@1218
  1030
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1031
    ///function for setting DistMap type
alpar@1218
  1032
    ///
alpar@1218
  1033
    template<class T>
alpar@1218
  1034
    DfsWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@1218
  1035
    {
deba@2386
  1036
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
  1037
      return DfsWizard<DefDistMapBase<T> >(*this);
alpar@1218
  1038
    }
alpar@1218
  1039
    
alpar@1218
  1040
    /// Sets the source node, from which the Dfs algorithm runs.
alpar@1218
  1041
alpar@1218
  1042
    /// Sets the source node, from which the Dfs algorithm runs.
alpar@1218
  1043
    /// \param s is the source node.
alpar@1218
  1044
    DfsWizard<TR> &source(Node s) 
alpar@1218
  1045
    {
alpar@1218
  1046
      Base::_source=s;
alpar@1218
  1047
      return *this;
alpar@1218
  1048
    }
alpar@780
  1049
    
alpar@780
  1050
  };
alpar@780
  1051
  
alpar@1218
  1052
  ///Function type interface for Dfs algorithm.
alpar@1218
  1053
deba@2376
  1054
  ///\ingroup search
alpar@1218
  1055
  ///Function type interface for Dfs algorithm.
alpar@1218
  1056
  ///
alpar@1218
  1057
  ///This function also has several
alpar@1218
  1058
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1059
  ///they are declared as the members of class \ref DfsWizard.
alpar@1218
  1060
  ///The following
alpar@1218
  1061
  ///example shows how to use these parameters.
alpar@1218
  1062
  ///\code
alpar@1218
  1063
  ///  dfs(g,source).predMap(preds).run();
alpar@1218
  1064
  ///\endcode
alpar@1218
  1065
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
alpar@1218
  1066
  ///to the end of the parameter list.
alpar@1218
  1067
  ///\sa DfsWizard
alpar@1218
  1068
  ///\sa Dfs
alpar@1218
  1069
  template<class GR>
alpar@1218
  1070
  DfsWizard<DfsWizardBase<GR> >
alpar@1218
  1071
  dfs(const GR &g,typename GR::Node s=INVALID)
alpar@1218
  1072
  {
alpar@1218
  1073
    return DfsWizard<DfsWizardBase<GR> >(g,s);
alpar@1218
  1074
  }
alpar@1218
  1075
deba@1799
  1076
#ifdef DOXYGEN
deba@1749
  1077
  /// \brief Visitor class for dfs.
deba@1749
  1078
  ///  
deba@1749
  1079
  /// It gives a simple interface for a functional interface for dfs 
deba@1749
  1080
  /// traversal. The traversal on a linear data structure. 
deba@1749
  1081
  template <typename _Graph>
deba@1749
  1082
  struct DfsVisitor {
deba@1749
  1083
    typedef _Graph Graph;
deba@1749
  1084
    typedef typename Graph::Edge Edge;
deba@1749
  1085
    typedef typename Graph::Node Node;
deba@1749
  1086
    /// \brief Called when the edge reach a node.
deba@1749
  1087
    /// 
deba@1749
  1088
    /// It is called when the dfs find an edge which target is not
deba@1749
  1089
    /// reached yet.
deba@1749
  1090
    void discover(const Edge& edge) {}
deba@1749
  1091
    /// \brief Called when the node reached first time.
deba@1749
  1092
    /// 
deba@1749
  1093
    /// It is Called when the node reached first time.
deba@1749
  1094
    void reach(const Node& node) {}
deba@1749
  1095
    /// \brief Called when we step back on an edge.
deba@1749
  1096
    /// 
deba@1749
  1097
    /// It is called when the dfs should step back on the edge.
deba@1749
  1098
    void backtrack(const Edge& edge) {}
deba@1749
  1099
    /// \brief Called when we step back from the node.
deba@1749
  1100
    /// 
deba@1749
  1101
    /// It is called when we step back from the node.
deba@1749
  1102
    void leave(const Node& node) {}
deba@1749
  1103
    /// \brief Called when the edge examined but target of the edge 
deba@1749
  1104
    /// already discovered.
deba@1749
  1105
    /// 
deba@1749
  1106
    /// It called when the edge examined but the target of the edge 
deba@1749
  1107
    /// already discovered.
deba@1749
  1108
    void examine(const Edge& edge) {}
deba@1749
  1109
    /// \brief Called for the source node of the dfs.
deba@1749
  1110
    /// 
deba@1749
  1111
    /// It is called for the source node of the dfs.
deba@1799
  1112
    void start(const Node& node) {}
deba@1749
  1113
    /// \brief Called when we leave the source node of the dfs.
deba@1749
  1114
    /// 
deba@1749
  1115
    /// It is called when we leave the source node of the dfs.
deba@1799
  1116
    void stop(const Node& node) {}
deba@1799
  1117
deba@1799
  1118
  };
deba@1799
  1119
#else
deba@1799
  1120
  template <typename _Graph>
deba@1799
  1121
  struct DfsVisitor {
deba@1799
  1122
    typedef _Graph Graph;
deba@1799
  1123
    typedef typename Graph::Edge Edge;
deba@1799
  1124
    typedef typename Graph::Node Node;
deba@1799
  1125
    void discover(const Edge&) {}
deba@1799
  1126
    void reach(const Node&) {}
deba@1799
  1127
    void backtrack(const Edge&) {}
deba@1799
  1128
    void leave(const Node&) {}
deba@1799
  1129
    void examine(const Edge&) {}
deba@1799
  1130
    void start(const Node&) {}
deba@1749
  1131
    void stop(const Node&) {}
deba@1749
  1132
deba@1749
  1133
    template <typename _Visitor>
deba@1749
  1134
    struct Constraints {
deba@1749
  1135
      void constraints() {
deba@1749
  1136
	Edge edge;
deba@1749
  1137
	Node node;
deba@1749
  1138
	visitor.discover(edge);
deba@1749
  1139
	visitor.reach(node);
deba@1749
  1140
	visitor.backtrack(edge);
deba@1749
  1141
	visitor.leave(node);
deba@1749
  1142
	visitor.examine(edge);
deba@1749
  1143
	visitor.start(node);
deba@1749
  1144
	visitor.stop(edge);
deba@1749
  1145
      }
deba@1749
  1146
      _Visitor& visitor;
deba@1749
  1147
    };
deba@1749
  1148
  };
deba@1799
  1149
#endif
deba@1749
  1150
deba@1749
  1151
  /// \brief Default traits class of DfsVisit class.
deba@1749
  1152
  ///
deba@1749
  1153
  /// Default traits class of DfsVisit class.
deba@1749
  1154
  /// \param _Graph Graph type.
deba@1749
  1155
  template<class _Graph>
deba@1749
  1156
  struct DfsVisitDefaultTraits {
deba@1749
  1157
deba@1749
  1158
    /// \brief The graph type the algorithm runs on. 
deba@1749
  1159
    typedef _Graph Graph;
deba@1749
  1160
deba@1749
  1161
    /// \brief The type of the map that indicates which nodes are reached.
deba@1749
  1162
    /// 
deba@1749
  1163
    /// The type of the map that indicates which nodes are reached.
alpar@2260
  1164
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
deba@1749
  1165
    /// \todo named parameter to set this type, function to read and write.
deba@1749
  1166
    typedef typename Graph::template NodeMap<bool> ReachedMap;
deba@1749
  1167
deba@1749
  1168
    /// \brief Instantiates a ReachedMap.
deba@1749
  1169
    ///
deba@1749
  1170
    /// This function instantiates a \ref ReachedMap. 
deba@1865
  1171
    /// \param graph is the graph, to which
deba@1749
  1172
    /// we would like to define the \ref ReachedMap.
deba@1749
  1173
    static ReachedMap *createReachedMap(const Graph &graph) {
deba@1749
  1174
      return new ReachedMap(graph);
deba@1749
  1175
    }
deba@1749
  1176
deba@1749
  1177
  };
deba@1749
  1178
  
deba@1749
  1179
  /// %DFS Visit algorithm class.
deba@1749
  1180
  
deba@2376
  1181
  /// \ingroup search
deba@1749
  1182
  /// This class provides an efficient implementation of the %DFS algorithm
deba@1749
  1183
  /// with visitor interface.
deba@1749
  1184
  ///
deba@1749
  1185
  /// The %DfsVisit class provides an alternative interface to the Dfs
deba@1749
  1186
  /// class. It works with callback mechanism, the DfsVisit object calls
deba@1749
  1187
  /// on every dfs event the \c Visitor class member functions. 
deba@1749
  1188
  ///
deba@1749
  1189
  /// \param _Graph The graph type the algorithm runs on. The default value is
deba@1749
  1190
  /// \ref ListGraph. The value of _Graph is not used directly by Dfs, it
deba@1749
  1191
  /// is only passed to \ref DfsDefaultTraits.
deba@1749
  1192
  /// \param _Visitor The Visitor object for the algorithm. The 
deba@1749
  1193
  /// \ref DfsVisitor "DfsVisitor<_Graph>" is an empty Visitor which
deba@1749
  1194
  /// does not observe the Dfs events. If you want to observe the dfs
deba@1749
  1195
  /// events you should implement your own Visitor class.
deba@1749
  1196
  /// \param _Traits Traits class to set various data types used by the 
deba@1749
  1197
  /// algorithm. The default traits class is
deba@1749
  1198
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Graph>".
deba@1749
  1199
  /// See \ref DfsVisitDefaultTraits for the documentation of
deba@1749
  1200
  /// a Dfs visit traits class.
deba@1749
  1201
  ///
deba@1749
  1202
  /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
deba@1749
  1203
#ifdef DOXYGEN
deba@1749
  1204
  template <typename _Graph, typename _Visitor, typename _Traits>
deba@1749
  1205
#else
deba@1749
  1206
  template <typename _Graph = ListGraph,
deba@1749
  1207
	    typename _Visitor = DfsVisitor<_Graph>,
deba@1749
  1208
	    typename _Traits = DfsDefaultTraits<_Graph> >
deba@1749
  1209
#endif
deba@1749
  1210
  class DfsVisit {
deba@1749
  1211
  public:
deba@1749
  1212
    
deba@1749
  1213
    /// \brief \ref Exception for uninitialized parameters.
deba@1749
  1214
    ///
deba@1749
  1215
    /// This error represents problems in the initialization
deba@1749
  1216
    /// of the parameters of the algorithms.
deba@1749
  1217
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@1749
  1218
    public:
alpar@2151
  1219
      virtual const char* what() const throw() 
alpar@2152
  1220
      {
deba@1749
  1221
	return "lemon::DfsVisit::UninitializedParameter";
deba@1749
  1222
      }
deba@1749
  1223
    };
deba@1749
  1224
deba@1749
  1225
    typedef _Traits Traits;
deba@1749
  1226
deba@1749
  1227
    typedef typename Traits::Graph Graph;
deba@1749
  1228
deba@1749
  1229
    typedef _Visitor Visitor;
deba@1749
  1230
deba@1749
  1231
    ///The type of the map indicating which nodes are reached.
deba@1749
  1232
    typedef typename Traits::ReachedMap ReachedMap;
deba@1749
  1233
deba@1749
  1234
  private:
deba@1749
  1235
deba@1749
  1236
    typedef typename Graph::Node Node;
deba@1749
  1237
    typedef typename Graph::NodeIt NodeIt;
deba@1749
  1238
    typedef typename Graph::Edge Edge;
deba@1749
  1239
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@1749
  1240
deba@1749
  1241
    /// Pointer to the underlying graph.
deba@1749
  1242
    const Graph *_graph;
deba@1749
  1243
    /// Pointer to the visitor object.
deba@1749
  1244
    Visitor *_visitor;
deba@1749
  1245
    ///Pointer to the map of reached status of the nodes.
deba@1749
  1246
    ReachedMap *_reached;
deba@1749
  1247
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
deba@1749
  1248
    bool local_reached;
deba@1749
  1249
deba@1749
  1250
    std::vector<typename Graph::Edge> _stack;
deba@1749
  1251
    int _stack_head;
deba@1749
  1252
deba@1749
  1253
    /// \brief Creates the maps if necessary.
deba@1749
  1254
    ///
deba@1749
  1255
    /// Creates the maps if necessary.
deba@1749
  1256
    void create_maps() {
deba@1749
  1257
      if(!_reached) {
deba@1749
  1258
	local_reached = true;
deba@1749
  1259
	_reached = Traits::createReachedMap(*_graph);
deba@1749
  1260
      }
deba@1749
  1261
    }
deba@1749
  1262
deba@1749
  1263
  protected:
deba@1749
  1264
deba@1749
  1265
    DfsVisit() {}
deba@1749
  1266
    
deba@1749
  1267
  public:
deba@1749
  1268
deba@1749
  1269
    typedef DfsVisit Create;
deba@1749
  1270
deba@1749
  1271
    /// \name Named template parameters
deba@1749
  1272
deba@1749
  1273
    ///@{
deba@1749
  1274
    template <class T>
deba@1749
  1275
    struct DefReachedMapTraits : public Traits {
deba@1749
  1276
      typedef T ReachedMap;
deba@1749
  1277
      static ReachedMap *createReachedMap(const Graph &graph) {
deba@1749
  1278
	throw UninitializedParameter();
deba@1749
  1279
      }
deba@1749
  1280
    };
deba@1749
  1281
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@1749
  1282
    /// ReachedMap type
deba@1749
  1283
    ///
deba@1749
  1284
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
deba@1749
  1285
    template <class T>
alpar@1773
  1286
    struct DefReachedMap : public DfsVisit< Graph, Visitor,
alpar@1773
  1287
					    DefReachedMapTraits<T> > {
alpar@1773
  1288
      typedef DfsVisit< Graph, Visitor, DefReachedMapTraits<T> > Create;
deba@1749
  1289
    };
deba@1749
  1290
    ///@}
deba@1749
  1291
deba@1749
  1292
  public:      
deba@1749
  1293
    
deba@1749
  1294
    /// \brief Constructor.
deba@1749
  1295
    ///
deba@1749
  1296
    /// Constructor.
deba@1749
  1297
    ///
deba@1749
  1298
    /// \param graph the graph the algorithm will run on.
deba@1749
  1299
    /// \param visitor The visitor of the algorithm.
deba@1749
  1300
    ///
deba@1749
  1301
    DfsVisit(const Graph& graph, Visitor& visitor) 
deba@1749
  1302
      : _graph(&graph), _visitor(&visitor),
deba@1749
  1303
	_reached(0), local_reached(false) {}
deba@1749
  1304
    
deba@1749
  1305
    /// \brief Destructor.
deba@1749
  1306
    ///
deba@1749
  1307
    /// Destructor.
deba@1749
  1308
    ~DfsVisit() {
deba@1749
  1309
      if(local_reached) delete _reached;
deba@1749
  1310
    }
deba@1749
  1311
deba@1749
  1312
    /// \brief Sets the map indicating if a node is reached.
deba@1749
  1313
    ///
deba@1749
  1314
    /// Sets the map indicating if a node is reached.
deba@1749
  1315
    /// If you don't use this function before calling \ref run(),
deba@1749
  1316
    /// it will allocate one. The destuctor deallocates this
deba@1749
  1317
    /// automatically allocated map, of course.
deba@1749
  1318
    /// \return <tt> (*this) </tt>
deba@1749
  1319
    DfsVisit &reachedMap(ReachedMap &m) {
deba@1749
  1320
      if(local_reached) {
deba@1749
  1321
	delete _reached;
deba@1749
  1322
	local_reached=false;
deba@1749
  1323
      }
deba@1749
  1324
      _reached = &m;
deba@1749
  1325
      return *this;
deba@1749
  1326
    }
deba@1749
  1327
deba@1749
  1328
  public:
deba@1749
  1329
    /// \name Execution control
deba@1749
  1330
    /// The simplest way to execute the algorithm is to use
deba@1749
  1331
    /// one of the member functions called \c run(...).
deba@1749
  1332
    /// \n
deba@1749
  1333
    /// If you need more control on the execution,
deba@1761
  1334
    /// first you must call \ref init(), then you can adda source node
deba@1749
  1335
    /// with \ref addSource().
deba@1749
  1336
    /// Finally \ref start() will perform the actual path
deba@1749
  1337
    /// computation.
deba@1749
  1338
deba@1749
  1339
    /// @{
deba@1749
  1340
    /// \brief Initializes the internal data structures.
deba@1749
  1341
    ///
deba@1749
  1342
    /// Initializes the internal data structures.
deba@1749
  1343
    ///
deba@1749
  1344
    void init() {
deba@1749
  1345
      create_maps();
deba@1749
  1346
      _stack.resize(countNodes(*_graph));
deba@1749
  1347
      _stack_head = -1;
deba@1749
  1348
      for (NodeIt u(*_graph) ; u != INVALID ; ++u) {
deba@1749
  1349
	_reached->set(u, false);
deba@1749
  1350
      }
deba@1749
  1351
    }
deba@1749
  1352
    
deba@1749
  1353
    /// \brief Adds a new source node.
deba@1749
  1354
    ///
deba@1749
  1355
    /// Adds a new source node to the set of nodes to be processed.
deba@1749
  1356
    void addSource(Node s) {
deba@1749
  1357
      if(!(*_reached)[s]) {
deba@1749
  1358
	  _reached->set(s,true);
deba@1749
  1359
	  _visitor->start(s);
deba@1749
  1360
	  _visitor->reach(s);
deba@1749
  1361
	  Edge e; 
deba@1749
  1362
	  _graph->firstOut(e, s);
deba@1749
  1363
	  if (e != INVALID) {
deba@1749
  1364
	    _stack[++_stack_head] = e;
deba@1749
  1365
	  } else {
deba@1749
  1366
	    _visitor->leave(s);
deba@1749
  1367
	  }
deba@1749
  1368
	}
deba@1749
  1369
    }
deba@1749
  1370
    
deba@1749
  1371
    /// \brief Processes the next edge.
deba@1749
  1372
    ///
deba@1749
  1373
    /// Processes the next edge.
deba@1749
  1374
    ///
deba@1749
  1375
    /// \return The processed edge.
deba@1749
  1376
    ///
deba@1749
  1377
    /// \pre The stack must not be empty!
deba@1749
  1378
    Edge processNextEdge() { 
deba@1749
  1379
      Edge e = _stack[_stack_head];
deba@1749
  1380
      Node m = _graph->target(e);
deba@1749
  1381
      if(!(*_reached)[m]) {
deba@1749
  1382
	_visitor->discover(e);
deba@1749
  1383
	_visitor->reach(m);
deba@1749
  1384
	_reached->set(m, true);
deba@1749
  1385
	_graph->firstOut(_stack[++_stack_head], m);
deba@1749
  1386
      } else {
deba@1749
  1387
	_visitor->examine(e);
deba@1749
  1388
	m = _graph->source(e);
deba@1749
  1389
	_graph->nextOut(_stack[_stack_head]);
deba@1749
  1390
      }
deba@1749
  1391
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
deba@1749
  1392
	_visitor->leave(m);
deba@1749
  1393
	--_stack_head;
deba@1749
  1394
	if (_stack_head >= 0) {
deba@1749
  1395
	  _visitor->backtrack(_stack[_stack_head]);
deba@1749
  1396
	  m = _graph->source(_stack[_stack_head]);
deba@1749
  1397
	  _graph->nextOut(_stack[_stack_head]);
deba@1749
  1398
	} else {
deba@1749
  1399
	  _visitor->stop(m);	  
deba@1749
  1400
	}
deba@1749
  1401
      }
deba@1749
  1402
      return e;
deba@1749
  1403
    }
deba@1749
  1404
deba@1749
  1405
    /// \brief Next edge to be processed.
deba@1749
  1406
    ///
deba@1749
  1407
    /// Next edge to be processed.
deba@1749
  1408
    ///
deba@1749
  1409
    /// \return The next edge to be processed or INVALID if the stack is
deba@1749
  1410
    /// empty.
deba@1749
  1411
    Edge nextEdge() { 
deba@1749
  1412
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
deba@1749
  1413
    }
deba@1749
  1414
deba@1749
  1415
    /// \brief Returns \c false if there are nodes
deba@1749
  1416
    /// to be processed in the queue
deba@1749
  1417
    ///
deba@1749
  1418
    /// Returns \c false if there are nodes
deba@1749
  1419
    /// to be processed in the queue
deba@1749
  1420
    bool emptyQueue() { return _stack_head < 0; }
deba@1749
  1421
deba@1749
  1422
    /// \brief Returns the number of the nodes to be processed.
deba@1749
  1423
    ///
deba@1749
  1424
    /// Returns the number of the nodes to be processed in the queue.
deba@1749
  1425
    int queueSize() { return _stack_head + 1; }
deba@1749
  1426
    
deba@1749
  1427
    /// \brief Executes the algorithm.
deba@1749
  1428
    ///
deba@1749
  1429
    /// Executes the algorithm.
deba@1749
  1430
    ///
deba@1749
  1431
    /// \pre init() must be called and at least one node should be added
deba@1749
  1432
    /// with addSource() before using this function.
deba@1749
  1433
    void start() {
deba@1749
  1434
      while ( !emptyQueue() ) processNextEdge();
deba@1749
  1435
    }
deba@1749
  1436
    
deba@1749
  1437
    /// \brief Executes the algorithm until \c dest is reached.
deba@1749
  1438
    ///
deba@1749
  1439
    /// Executes the algorithm until \c dest is reached.
deba@1749
  1440
    ///
deba@1749
  1441
    /// \pre init() must be called and at least one node should be added
deba@1749
  1442
    /// with addSource() before using this function.
deba@1749
  1443
    void start(Node dest) {
deba@1749
  1444
      while ( !emptyQueue() && _graph->target(_stack[_stack_head]) != dest) 
deba@1749
  1445
	processNextEdge();
deba@1749
  1446
    }
deba@1749
  1447
    
deba@1749
  1448
    /// \brief Executes the algorithm until a condition is met.
deba@1749
  1449
    ///
deba@1749
  1450
    /// Executes the algorithm until a condition is met.
deba@1749
  1451
    ///
deba@1749
  1452
    /// \pre init() must be called and at least one node should be added
deba@1749
  1453
    /// with addSource() before using this function.
deba@1749
  1454
    ///
deba@1865
  1455
    /// \param em must be a bool (or convertible) edge map. The algorithm
deba@1865
  1456
    /// will stop when it reaches an edge \c e with \code nm[e]==true \endcode.
deba@1749
  1457
    ///
deba@1749
  1458
    /// \warning Contrary to \ref Dfs and \ref Dijkstra, \c em is an edge map,
deba@1749
  1459
    /// not a node map.
deba@1749
  1460
    template <typename EM>
deba@1749
  1461
    void start(const EM &em) {
deba@1749
  1462
      while (!emptyQueue() && !em[_stack[_stack_head]]) processNextEdge();
deba@1749
  1463
    }
deba@1749
  1464
deba@1981
  1465
    /// \brief Runs %DFSVisit algorithm from node \c s.
deba@1749
  1466
    ///
deba@1749
  1467
    /// This method runs the %DFS algorithm from a root node \c s.
deba@1749
  1468
    /// \note d.run(s) is just a shortcut of the following code.
alpar@1946
  1469
    ///\code
deba@1749
  1470
    ///   d.init();
deba@1749
  1471
    ///   d.addSource(s);
deba@1749
  1472
    ///   d.start();
alpar@1946
  1473
    ///\endcode
deba@1749
  1474
    void run(Node s) {
deba@1749
  1475
      init();
deba@1749
  1476
      addSource(s);
deba@1749
  1477
      start();
deba@1749
  1478
    }
deba@1981
  1479
deba@1981
  1480
    /// \brief Runs %DFSVisit algorithm to visit all nodes in the graph.
deba@1981
  1481
    
deba@1981
  1482
    /// This method runs the %DFS algorithm in order to
deba@1981
  1483
    /// compute the %DFS path to each node. The algorithm computes
deba@1981
  1484
    /// - The %DFS tree.
deba@1981
  1485
    /// - The distance of each node from the root in the %DFS tree.
deba@1981
  1486
    ///
deba@1981
  1487
    ///\note d.run() is just a shortcut of the following code.
deba@1981
  1488
    ///\code
deba@1981
  1489
    ///  d.init();
deba@1981
  1490
    ///  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1981
  1491
    ///    if (!d.reached(it)) {
deba@1981
  1492
    ///      d.addSource(it);
deba@1981
  1493
    ///      d.start();
deba@1981
  1494
    ///    }
deba@1981
  1495
    ///  }
deba@1981
  1496
    ///\endcode
deba@1981
  1497
    void run() {
deba@1981
  1498
      init();
deba@1981
  1499
      for (NodeIt it(*_graph); it != INVALID; ++it) {
deba@1981
  1500
        if (!reached(it)) {
deba@1981
  1501
          addSource(it);
deba@1981
  1502
          start();
deba@1981
  1503
        }
deba@1981
  1504
      }
deba@1981
  1505
    }
deba@1749
  1506
    ///@}
deba@1749
  1507
deba@1749
  1508
    /// \name Query Functions
deba@1749
  1509
    /// The result of the %DFS algorithm can be obtained using these
deba@1749
  1510
    /// functions.\n
deba@1749
  1511
    /// Before the use of these functions,
deba@1749
  1512
    /// either run() or start() must be called.
deba@1749
  1513
    ///@{
deba@1749
  1514
    /// \brief Checks if a node is reachable from the root.
deba@1749
  1515
    ///
deba@1749
  1516
    /// Returns \c true if \c v is reachable from the root(s).
deba@1749
  1517
    /// \warning The source nodes are inditated as unreachable.
deba@1749
  1518
    /// \pre Either \ref run() or \ref start()
deba@1749
  1519
    /// must be called before using this function.
deba@1749
  1520
    ///
deba@1749
  1521
    bool reached(Node v) { return (*_reached)[v]; }
deba@1749
  1522
    ///@}
deba@1749
  1523
  };
deba@1749
  1524
deba@1749
  1525
alpar@921
  1526
} //END OF NAMESPACE LEMON
alpar@780
  1527
alpar@780
  1528
#endif
alpar@780
  1529