lemon/dfs.h
author deba
Thu, 15 Feb 2007 13:06:23 +0000
changeset 2362 eb37b9774ef6
parent 2260 4274224f8a7d
child 2376 0ed45a6c74b1
permissions -rw-r--r--
Small changes
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@921
    19
#ifndef LEMON_DFS_H
alpar@921
    20
#define LEMON_DFS_H
alpar@780
    21
alpar@780
    22
///\ingroup flowalgs
alpar@780
    23
///\file
alpar@1218
    24
///\brief Dfs algorithm.
alpar@780
    25
alpar@1218
    26
#include <lemon/list_graph.h>
klao@946
    27
#include <lemon/graph_utils.h>
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
  
alpar@1218
   117
  ///\ingroup flowalgs
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) :
alpar@1218
   861
      _g((void *)&g), _reached(0), _processed(0), _pred(0),
alpar@1218
   862
      _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();
alpar@1218
   936
      Dfs<Graph,TR> alg(*(Graph*)Base::_g);
alpar@1218
   937
      if(Base::_reached) alg.reachedMap(*(ReachedMap*)Base::_reached);
alpar@1218
   938
      if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
alpar@1218
   939
      if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
alpar@1218
   940
      if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
alpar@1218
   941
      alg.run(Base::_source);
alpar@1218
   942
    }
alpar@1218
   943
alpar@1218
   944
    ///Runs Dfs algorithm from the given node.
alpar@1218
   945
alpar@1218
   946
    ///Runs Dfs algorithm from the given node.
alpar@1218
   947
    ///\param s is the given source.
alpar@1218
   948
    void run(Node s)
alpar@1218
   949
    {
alpar@1218
   950
      Base::_source=s;
alpar@1218
   951
      run();
alpar@1218
   952
    }
alpar@1218
   953
alpar@1218
   954
    template<class T>
alpar@1218
   955
    struct DefPredMapBase : public Base {
alpar@1218
   956
      typedef T PredMap;
alpar@1367
   957
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
   958
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1218
   959
    };
alpar@1218
   960
    
alpar@1218
   961
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   962
    ///function for setting PredMap type
alpar@1218
   963
    ///
alpar@1218
   964
    /// \ref named-templ-param "Named parameter"
alpar@1218
   965
    ///function for setting PredMap type
alpar@1218
   966
    ///
alpar@1218
   967
    template<class T>
alpar@1218
   968
    DfsWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@1218
   969
    {
alpar@1218
   970
      Base::_pred=(void *)&t;
alpar@1218
   971
      return DfsWizard<DefPredMapBase<T> >(*this);
alpar@1218
   972
    }
alpar@1218
   973
    
alpar@1218
   974
 
alpar@1218
   975
    template<class T>
alpar@1218
   976
    struct DefReachedMapBase : public Base {
alpar@1218
   977
      typedef T ReachedMap;
alpar@1367
   978
      static ReachedMap *createReachedMap(const Graph &) { return 0; };
alpar@1236
   979
      DefReachedMapBase(const TR &b) : TR(b) {}
alpar@1218
   980
    };
alpar@1218
   981
    
alpar@1218
   982
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   983
    ///function for setting ReachedMap
alpar@1218
   984
    ///
alpar@1218
   985
    /// \ref named-templ-param "Named parameter"
alpar@1218
   986
    ///function for setting ReachedMap
alpar@1218
   987
    ///
alpar@1218
   988
    template<class T>
alpar@1218
   989
    DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
alpar@1218
   990
    {
alpar@1218
   991
      Base::_pred=(void *)&t;
alpar@1218
   992
      return DfsWizard<DefReachedMapBase<T> >(*this);
alpar@1218
   993
    }
alpar@1218
   994
    
alpar@1218
   995
alpar@1218
   996
    template<class T>
alpar@1218
   997
    struct DefProcessedMapBase : public Base {
alpar@1218
   998
      typedef T ProcessedMap;
alpar@1367
   999
      static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
alpar@1236
  1000
      DefProcessedMapBase(const TR &b) : TR(b) {}
alpar@1218
  1001
    };
alpar@1218
  1002
    
alpar@1218
  1003
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1004
    ///function for setting ProcessedMap
alpar@1218
  1005
    ///
alpar@1218
  1006
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1007
    ///function for setting ProcessedMap
alpar@1218
  1008
    ///
alpar@1218
  1009
    template<class T>
alpar@1218
  1010
    DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
alpar@1218
  1011
    {
alpar@1218
  1012
      Base::_pred=(void *)&t;
alpar@1218
  1013
      return DfsWizard<DefProcessedMapBase<T> >(*this);
alpar@1218
  1014
    }
alpar@1218
  1015
    
alpar@1218
  1016
    template<class T>
alpar@1218
  1017
    struct DefDistMapBase : public Base {
alpar@1218
  1018
      typedef T DistMap;
alpar@1367
  1019
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
  1020
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1218
  1021
    };
alpar@1218
  1022
    
alpar@1218
  1023
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1024
    ///function for setting DistMap type
alpar@1218
  1025
    ///
alpar@1218
  1026
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1027
    ///function for setting DistMap type
alpar@1218
  1028
    ///
alpar@1218
  1029
    template<class T>
alpar@1218
  1030
    DfsWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@1218
  1031
    {
alpar@1218
  1032
      Base::_dist=(void *)&t;
alpar@1218
  1033
      return DfsWizard<DefDistMapBase<T> >(*this);
alpar@1218
  1034
    }
alpar@1218
  1035
    
alpar@1218
  1036
    /// Sets the source node, from which the Dfs algorithm runs.
alpar@1218
  1037
alpar@1218
  1038
    /// Sets the source node, from which the Dfs algorithm runs.
alpar@1218
  1039
    /// \param s is the source node.
alpar@1218
  1040
    DfsWizard<TR> &source(Node s) 
alpar@1218
  1041
    {
alpar@1218
  1042
      Base::_source=s;
alpar@1218
  1043
      return *this;
alpar@1218
  1044
    }
alpar@780
  1045
    
alpar@780
  1046
  };
alpar@780
  1047
  
alpar@1218
  1048
  ///Function type interface for Dfs algorithm.
alpar@1218
  1049
alpar@1218
  1050
  /// \ingroup flowalgs
alpar@1218
  1051
  ///Function type interface for Dfs algorithm.
alpar@1218
  1052
  ///
alpar@1218
  1053
  ///This function also has several
alpar@1218
  1054
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1055
  ///they are declared as the members of class \ref DfsWizard.
alpar@1218
  1056
  ///The following
alpar@1218
  1057
  ///example shows how to use these parameters.
alpar@1218
  1058
  ///\code
alpar@1218
  1059
  ///  dfs(g,source).predMap(preds).run();
alpar@1218
  1060
  ///\endcode
alpar@1218
  1061
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
alpar@1218
  1062
  ///to the end of the parameter list.
alpar@1218
  1063
  ///\sa DfsWizard
alpar@1218
  1064
  ///\sa Dfs
alpar@1218
  1065
  template<class GR>
alpar@1218
  1066
  DfsWizard<DfsWizardBase<GR> >
alpar@1218
  1067
  dfs(const GR &g,typename GR::Node s=INVALID)
alpar@1218
  1068
  {
alpar@1218
  1069
    return DfsWizard<DfsWizardBase<GR> >(g,s);
alpar@1218
  1070
  }
alpar@1218
  1071
deba@1799
  1072
#ifdef DOXYGEN
deba@1749
  1073
  /// \brief Visitor class for dfs.
deba@1749
  1074
  ///  
deba@1749
  1075
  /// It gives a simple interface for a functional interface for dfs 
deba@1749
  1076
  /// traversal. The traversal on a linear data structure. 
deba@1749
  1077
  template <typename _Graph>
deba@1749
  1078
  struct DfsVisitor {
deba@1749
  1079
    typedef _Graph Graph;
deba@1749
  1080
    typedef typename Graph::Edge Edge;
deba@1749
  1081
    typedef typename Graph::Node Node;
deba@1749
  1082
    /// \brief Called when the edge reach a node.
deba@1749
  1083
    /// 
deba@1749
  1084
    /// It is called when the dfs find an edge which target is not
deba@1749
  1085
    /// reached yet.
deba@1749
  1086
    void discover(const Edge& edge) {}
deba@1749
  1087
    /// \brief Called when the node reached first time.
deba@1749
  1088
    /// 
deba@1749
  1089
    /// It is Called when the node reached first time.
deba@1749
  1090
    void reach(const Node& node) {}
deba@1749
  1091
    /// \brief Called when we step back on an edge.
deba@1749
  1092
    /// 
deba@1749
  1093
    /// It is called when the dfs should step back on the edge.
deba@1749
  1094
    void backtrack(const Edge& edge) {}
deba@1749
  1095
    /// \brief Called when we step back from the node.
deba@1749
  1096
    /// 
deba@1749
  1097
    /// It is called when we step back from the node.
deba@1749
  1098
    void leave(const Node& node) {}
deba@1749
  1099
    /// \brief Called when the edge examined but target of the edge 
deba@1749
  1100
    /// already discovered.
deba@1749
  1101
    /// 
deba@1749
  1102
    /// It called when the edge examined but the target of the edge 
deba@1749
  1103
    /// already discovered.
deba@1749
  1104
    void examine(const Edge& edge) {}
deba@1749
  1105
    /// \brief Called for the source node of the dfs.
deba@1749
  1106
    /// 
deba@1749
  1107
    /// It is called for the source node of the dfs.
deba@1799
  1108
    void start(const Node& node) {}
deba@1749
  1109
    /// \brief Called when we leave the source node of the dfs.
deba@1749
  1110
    /// 
deba@1749
  1111
    /// It is called when we leave the source node of the dfs.
deba@1799
  1112
    void stop(const Node& node) {}
deba@1799
  1113
deba@1799
  1114
  };
deba@1799
  1115
#else
deba@1799
  1116
  template <typename _Graph>
deba@1799
  1117
  struct DfsVisitor {
deba@1799
  1118
    typedef _Graph Graph;
deba@1799
  1119
    typedef typename Graph::Edge Edge;
deba@1799
  1120
    typedef typename Graph::Node Node;
deba@1799
  1121
    void discover(const Edge&) {}
deba@1799
  1122
    void reach(const Node&) {}
deba@1799
  1123
    void backtrack(const Edge&) {}
deba@1799
  1124
    void leave(const Node&) {}
deba@1799
  1125
    void examine(const Edge&) {}
deba@1799
  1126
    void start(const Node&) {}
deba@1749
  1127
    void stop(const Node&) {}
deba@1749
  1128
deba@1749
  1129
    template <typename _Visitor>
deba@1749
  1130
    struct Constraints {
deba@1749
  1131
      void constraints() {
deba@1749
  1132
	Edge edge;
deba@1749
  1133
	Node node;
deba@1749
  1134
	visitor.discover(edge);
deba@1749
  1135
	visitor.reach(node);
deba@1749
  1136
	visitor.backtrack(edge);
deba@1749
  1137
	visitor.leave(node);
deba@1749
  1138
	visitor.examine(edge);
deba@1749
  1139
	visitor.start(node);
deba@1749
  1140
	visitor.stop(edge);
deba@1749
  1141
      }
deba@1749
  1142
      _Visitor& visitor;
deba@1749
  1143
    };
deba@1749
  1144
  };
deba@1799
  1145
#endif
deba@1749
  1146
deba@1749
  1147
  /// \brief Default traits class of DfsVisit class.
deba@1749
  1148
  ///
deba@1749
  1149
  /// Default traits class of DfsVisit class.
deba@1749
  1150
  /// \param _Graph Graph type.
deba@1749
  1151
  template<class _Graph>
deba@1749
  1152
  struct DfsVisitDefaultTraits {
deba@1749
  1153
deba@1749
  1154
    /// \brief The graph type the algorithm runs on. 
deba@1749
  1155
    typedef _Graph Graph;
deba@1749
  1156
deba@1749
  1157
    /// \brief The type of the map that indicates which nodes are reached.
deba@1749
  1158
    /// 
deba@1749
  1159
    /// The type of the map that indicates which nodes are reached.
alpar@2260
  1160
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
deba@1749
  1161
    /// \todo named parameter to set this type, function to read and write.
deba@1749
  1162
    typedef typename Graph::template NodeMap<bool> ReachedMap;
deba@1749
  1163
deba@1749
  1164
    /// \brief Instantiates a ReachedMap.
deba@1749
  1165
    ///
deba@1749
  1166
    /// This function instantiates a \ref ReachedMap. 
deba@1865
  1167
    /// \param graph is the graph, to which
deba@1749
  1168
    /// we would like to define the \ref ReachedMap.
deba@1749
  1169
    static ReachedMap *createReachedMap(const Graph &graph) {
deba@1749
  1170
      return new ReachedMap(graph);
deba@1749
  1171
    }
deba@1749
  1172
deba@1749
  1173
  };
deba@1749
  1174
  
deba@1749
  1175
  /// %DFS Visit algorithm class.
deba@1749
  1176
  
deba@1749
  1177
  /// \ingroup flowalgs
deba@1749
  1178
  /// This class provides an efficient implementation of the %DFS algorithm
deba@1749
  1179
  /// with visitor interface.
deba@1749
  1180
  ///
deba@1749
  1181
  /// The %DfsVisit class provides an alternative interface to the Dfs
deba@1749
  1182
  /// class. It works with callback mechanism, the DfsVisit object calls
deba@1749
  1183
  /// on every dfs event the \c Visitor class member functions. 
deba@1749
  1184
  ///
deba@1749
  1185
  /// \param _Graph The graph type the algorithm runs on. The default value is
deba@1749
  1186
  /// \ref ListGraph. The value of _Graph is not used directly by Dfs, it
deba@1749
  1187
  /// is only passed to \ref DfsDefaultTraits.
deba@1749
  1188
  /// \param _Visitor The Visitor object for the algorithm. The 
deba@1749
  1189
  /// \ref DfsVisitor "DfsVisitor<_Graph>" is an empty Visitor which
deba@1749
  1190
  /// does not observe the Dfs events. If you want to observe the dfs
deba@1749
  1191
  /// events you should implement your own Visitor class.
deba@1749
  1192
  /// \param _Traits Traits class to set various data types used by the 
deba@1749
  1193
  /// algorithm. The default traits class is
deba@1749
  1194
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Graph>".
deba@1749
  1195
  /// See \ref DfsVisitDefaultTraits for the documentation of
deba@1749
  1196
  /// a Dfs visit traits class.
deba@1749
  1197
  ///
deba@1749
  1198
  /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
deba@1749
  1199
#ifdef DOXYGEN
deba@1749
  1200
  template <typename _Graph, typename _Visitor, typename _Traits>
deba@1749
  1201
#else
deba@1749
  1202
  template <typename _Graph = ListGraph,
deba@1749
  1203
	    typename _Visitor = DfsVisitor<_Graph>,
deba@1749
  1204
	    typename _Traits = DfsDefaultTraits<_Graph> >
deba@1749
  1205
#endif
deba@1749
  1206
  class DfsVisit {
deba@1749
  1207
  public:
deba@1749
  1208
    
deba@1749
  1209
    /// \brief \ref Exception for uninitialized parameters.
deba@1749
  1210
    ///
deba@1749
  1211
    /// This error represents problems in the initialization
deba@1749
  1212
    /// of the parameters of the algorithms.
deba@1749
  1213
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@1749
  1214
    public:
alpar@2151
  1215
      virtual const char* what() const throw() 
alpar@2152
  1216
      {
deba@1749
  1217
	return "lemon::DfsVisit::UninitializedParameter";
deba@1749
  1218
      }
deba@1749
  1219
    };
deba@1749
  1220
deba@1749
  1221
    typedef _Traits Traits;
deba@1749
  1222
deba@1749
  1223
    typedef typename Traits::Graph Graph;
deba@1749
  1224
deba@1749
  1225
    typedef _Visitor Visitor;
deba@1749
  1226
deba@1749
  1227
    ///The type of the map indicating which nodes are reached.
deba@1749
  1228
    typedef typename Traits::ReachedMap ReachedMap;
deba@1749
  1229
deba@1749
  1230
  private:
deba@1749
  1231
deba@1749
  1232
    typedef typename Graph::Node Node;
deba@1749
  1233
    typedef typename Graph::NodeIt NodeIt;
deba@1749
  1234
    typedef typename Graph::Edge Edge;
deba@1749
  1235
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@1749
  1236
deba@1749
  1237
    /// Pointer to the underlying graph.
deba@1749
  1238
    const Graph *_graph;
deba@1749
  1239
    /// Pointer to the visitor object.
deba@1749
  1240
    Visitor *_visitor;
deba@1749
  1241
    ///Pointer to the map of reached status of the nodes.
deba@1749
  1242
    ReachedMap *_reached;
deba@1749
  1243
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
deba@1749
  1244
    bool local_reached;
deba@1749
  1245
deba@1749
  1246
    std::vector<typename Graph::Edge> _stack;
deba@1749
  1247
    int _stack_head;
deba@1749
  1248
deba@1749
  1249
    /// \brief Creates the maps if necessary.
deba@1749
  1250
    ///
deba@1749
  1251
    /// Creates the maps if necessary.
deba@1749
  1252
    void create_maps() {
deba@1749
  1253
      if(!_reached) {
deba@1749
  1254
	local_reached = true;
deba@1749
  1255
	_reached = Traits::createReachedMap(*_graph);
deba@1749
  1256
      }
deba@1749
  1257
    }
deba@1749
  1258
deba@1749
  1259
  protected:
deba@1749
  1260
deba@1749
  1261
    DfsVisit() {}
deba@1749
  1262
    
deba@1749
  1263
  public:
deba@1749
  1264
deba@1749
  1265
    typedef DfsVisit Create;
deba@1749
  1266
deba@1749
  1267
    /// \name Named template parameters
deba@1749
  1268
deba@1749
  1269
    ///@{
deba@1749
  1270
    template <class T>
deba@1749
  1271
    struct DefReachedMapTraits : public Traits {
deba@1749
  1272
      typedef T ReachedMap;
deba@1749
  1273
      static ReachedMap *createReachedMap(const Graph &graph) {
deba@1749
  1274
	throw UninitializedParameter();
deba@1749
  1275
      }
deba@1749
  1276
    };
deba@1749
  1277
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@1749
  1278
    /// ReachedMap type
deba@1749
  1279
    ///
deba@1749
  1280
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
deba@1749
  1281
    template <class T>
alpar@1773
  1282
    struct DefReachedMap : public DfsVisit< Graph, Visitor,
alpar@1773
  1283
					    DefReachedMapTraits<T> > {
alpar@1773
  1284
      typedef DfsVisit< Graph, Visitor, DefReachedMapTraits<T> > Create;
deba@1749
  1285
    };
deba@1749
  1286
    ///@}
deba@1749
  1287
deba@1749
  1288
  public:      
deba@1749
  1289
    
deba@1749
  1290
    /// \brief Constructor.
deba@1749
  1291
    ///
deba@1749
  1292
    /// Constructor.
deba@1749
  1293
    ///
deba@1749
  1294
    /// \param graph the graph the algorithm will run on.
deba@1749
  1295
    /// \param visitor The visitor of the algorithm.
deba@1749
  1296
    ///
deba@1749
  1297
    DfsVisit(const Graph& graph, Visitor& visitor) 
deba@1749
  1298
      : _graph(&graph), _visitor(&visitor),
deba@1749
  1299
	_reached(0), local_reached(false) {}
deba@1749
  1300
    
deba@1749
  1301
    /// \brief Destructor.
deba@1749
  1302
    ///
deba@1749
  1303
    /// Destructor.
deba@1749
  1304
    ~DfsVisit() {
deba@1749
  1305
      if(local_reached) delete _reached;
deba@1749
  1306
    }
deba@1749
  1307
deba@1749
  1308
    /// \brief Sets the map indicating if a node is reached.
deba@1749
  1309
    ///
deba@1749
  1310
    /// Sets the map indicating if a node is reached.
deba@1749
  1311
    /// If you don't use this function before calling \ref run(),
deba@1749
  1312
    /// it will allocate one. The destuctor deallocates this
deba@1749
  1313
    /// automatically allocated map, of course.
deba@1749
  1314
    /// \return <tt> (*this) </tt>
deba@1749
  1315
    DfsVisit &reachedMap(ReachedMap &m) {
deba@1749
  1316
      if(local_reached) {
deba@1749
  1317
	delete _reached;
deba@1749
  1318
	local_reached=false;
deba@1749
  1319
      }
deba@1749
  1320
      _reached = &m;
deba@1749
  1321
      return *this;
deba@1749
  1322
    }
deba@1749
  1323
deba@1749
  1324
  public:
deba@1749
  1325
    /// \name Execution control
deba@1749
  1326
    /// The simplest way to execute the algorithm is to use
deba@1749
  1327
    /// one of the member functions called \c run(...).
deba@1749
  1328
    /// \n
deba@1749
  1329
    /// If you need more control on the execution,
deba@1761
  1330
    /// first you must call \ref init(), then you can adda source node
deba@1749
  1331
    /// with \ref addSource().
deba@1749
  1332
    /// Finally \ref start() will perform the actual path
deba@1749
  1333
    /// computation.
deba@1749
  1334
deba@1749
  1335
    /// @{
deba@1749
  1336
    /// \brief Initializes the internal data structures.
deba@1749
  1337
    ///
deba@1749
  1338
    /// Initializes the internal data structures.
deba@1749
  1339
    ///
deba@1749
  1340
    void init() {
deba@1749
  1341
      create_maps();
deba@1749
  1342
      _stack.resize(countNodes(*_graph));
deba@1749
  1343
      _stack_head = -1;
deba@1749
  1344
      for (NodeIt u(*_graph) ; u != INVALID ; ++u) {
deba@1749
  1345
	_reached->set(u, false);
deba@1749
  1346
      }
deba@1749
  1347
    }
deba@1749
  1348
    
deba@1749
  1349
    /// \brief Adds a new source node.
deba@1749
  1350
    ///
deba@1749
  1351
    /// Adds a new source node to the set of nodes to be processed.
deba@1749
  1352
    void addSource(Node s) {
deba@1749
  1353
      if(!(*_reached)[s]) {
deba@1749
  1354
	  _reached->set(s,true);
deba@1749
  1355
	  _visitor->start(s);
deba@1749
  1356
	  _visitor->reach(s);
deba@1749
  1357
	  Edge e; 
deba@1749
  1358
	  _graph->firstOut(e, s);
deba@1749
  1359
	  if (e != INVALID) {
deba@1749
  1360
	    _stack[++_stack_head] = e;
deba@1749
  1361
	  } else {
deba@1749
  1362
	    _visitor->leave(s);
deba@1749
  1363
	  }
deba@1749
  1364
	}
deba@1749
  1365
    }
deba@1749
  1366
    
deba@1749
  1367
    /// \brief Processes the next edge.
deba@1749
  1368
    ///
deba@1749
  1369
    /// Processes the next edge.
deba@1749
  1370
    ///
deba@1749
  1371
    /// \return The processed edge.
deba@1749
  1372
    ///
deba@1749
  1373
    /// \pre The stack must not be empty!
deba@1749
  1374
    Edge processNextEdge() { 
deba@1749
  1375
      Edge e = _stack[_stack_head];
deba@1749
  1376
      Node m = _graph->target(e);
deba@1749
  1377
      if(!(*_reached)[m]) {
deba@1749
  1378
	_visitor->discover(e);
deba@1749
  1379
	_visitor->reach(m);
deba@1749
  1380
	_reached->set(m, true);
deba@1749
  1381
	_graph->firstOut(_stack[++_stack_head], m);
deba@1749
  1382
      } else {
deba@1749
  1383
	_visitor->examine(e);
deba@1749
  1384
	m = _graph->source(e);
deba@1749
  1385
	_graph->nextOut(_stack[_stack_head]);
deba@1749
  1386
      }
deba@1749
  1387
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
deba@1749
  1388
	_visitor->leave(m);
deba@1749
  1389
	--_stack_head;
deba@1749
  1390
	if (_stack_head >= 0) {
deba@1749
  1391
	  _visitor->backtrack(_stack[_stack_head]);
deba@1749
  1392
	  m = _graph->source(_stack[_stack_head]);
deba@1749
  1393
	  _graph->nextOut(_stack[_stack_head]);
deba@1749
  1394
	} else {
deba@1749
  1395
	  _visitor->stop(m);	  
deba@1749
  1396
	}
deba@1749
  1397
      }
deba@1749
  1398
      return e;
deba@1749
  1399
    }
deba@1749
  1400
deba@1749
  1401
    /// \brief Next edge to be processed.
deba@1749
  1402
    ///
deba@1749
  1403
    /// Next edge to be processed.
deba@1749
  1404
    ///
deba@1749
  1405
    /// \return The next edge to be processed or INVALID if the stack is
deba@1749
  1406
    /// empty.
deba@1749
  1407
    Edge nextEdge() { 
deba@1749
  1408
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
deba@1749
  1409
    }
deba@1749
  1410
deba@1749
  1411
    /// \brief Returns \c false if there are nodes
deba@1749
  1412
    /// to be processed in the queue
deba@1749
  1413
    ///
deba@1749
  1414
    /// Returns \c false if there are nodes
deba@1749
  1415
    /// to be processed in the queue
deba@1749
  1416
    bool emptyQueue() { return _stack_head < 0; }
deba@1749
  1417
deba@1749
  1418
    /// \brief Returns the number of the nodes to be processed.
deba@1749
  1419
    ///
deba@1749
  1420
    /// Returns the number of the nodes to be processed in the queue.
deba@1749
  1421
    int queueSize() { return _stack_head + 1; }
deba@1749
  1422
    
deba@1749
  1423
    /// \brief Executes the algorithm.
deba@1749
  1424
    ///
deba@1749
  1425
    /// Executes the algorithm.
deba@1749
  1426
    ///
deba@1749
  1427
    /// \pre init() must be called and at least one node should be added
deba@1749
  1428
    /// with addSource() before using this function.
deba@1749
  1429
    void start() {
deba@1749
  1430
      while ( !emptyQueue() ) processNextEdge();
deba@1749
  1431
    }
deba@1749
  1432
    
deba@1749
  1433
    /// \brief Executes the algorithm until \c dest is reached.
deba@1749
  1434
    ///
deba@1749
  1435
    /// Executes the algorithm until \c dest is reached.
deba@1749
  1436
    ///
deba@1749
  1437
    /// \pre init() must be called and at least one node should be added
deba@1749
  1438
    /// with addSource() before using this function.
deba@1749
  1439
    void start(Node dest) {
deba@1749
  1440
      while ( !emptyQueue() && _graph->target(_stack[_stack_head]) != dest) 
deba@1749
  1441
	processNextEdge();
deba@1749
  1442
    }
deba@1749
  1443
    
deba@1749
  1444
    /// \brief Executes the algorithm until a condition is met.
deba@1749
  1445
    ///
deba@1749
  1446
    /// Executes the algorithm until a condition is met.
deba@1749
  1447
    ///
deba@1749
  1448
    /// \pre init() must be called and at least one node should be added
deba@1749
  1449
    /// with addSource() before using this function.
deba@1749
  1450
    ///
deba@1865
  1451
    /// \param em must be a bool (or convertible) edge map. The algorithm
deba@1865
  1452
    /// will stop when it reaches an edge \c e with \code nm[e]==true \endcode.
deba@1749
  1453
    ///
deba@1749
  1454
    /// \warning Contrary to \ref Dfs and \ref Dijkstra, \c em is an edge map,
deba@1749
  1455
    /// not a node map.
deba@1749
  1456
    template <typename EM>
deba@1749
  1457
    void start(const EM &em) {
deba@1749
  1458
      while (!emptyQueue() && !em[_stack[_stack_head]]) processNextEdge();
deba@1749
  1459
    }
deba@1749
  1460
deba@1981
  1461
    /// \brief Runs %DFSVisit algorithm from node \c s.
deba@1749
  1462
    ///
deba@1749
  1463
    /// This method runs the %DFS algorithm from a root node \c s.
deba@1749
  1464
    /// \note d.run(s) is just a shortcut of the following code.
alpar@1946
  1465
    ///\code
deba@1749
  1466
    ///   d.init();
deba@1749
  1467
    ///   d.addSource(s);
deba@1749
  1468
    ///   d.start();
alpar@1946
  1469
    ///\endcode
deba@1749
  1470
    void run(Node s) {
deba@1749
  1471
      init();
deba@1749
  1472
      addSource(s);
deba@1749
  1473
      start();
deba@1749
  1474
    }
deba@1981
  1475
deba@1981
  1476
    /// \brief Runs %DFSVisit algorithm to visit all nodes in the graph.
deba@1981
  1477
    
deba@1981
  1478
    /// This method runs the %DFS algorithm in order to
deba@1981
  1479
    /// compute the %DFS path to each node. The algorithm computes
deba@1981
  1480
    /// - The %DFS tree.
deba@1981
  1481
    /// - The distance of each node from the root in the %DFS tree.
deba@1981
  1482
    ///
deba@1981
  1483
    ///\note d.run() is just a shortcut of the following code.
deba@1981
  1484
    ///\code
deba@1981
  1485
    ///  d.init();
deba@1981
  1486
    ///  for (NodeIt it(graph); it != INVALID; ++it) {
deba@1981
  1487
    ///    if (!d.reached(it)) {
deba@1981
  1488
    ///      d.addSource(it);
deba@1981
  1489
    ///      d.start();
deba@1981
  1490
    ///    }
deba@1981
  1491
    ///  }
deba@1981
  1492
    ///\endcode
deba@1981
  1493
    void run() {
deba@1981
  1494
      init();
deba@1981
  1495
      for (NodeIt it(*_graph); it != INVALID; ++it) {
deba@1981
  1496
        if (!reached(it)) {
deba@1981
  1497
          addSource(it);
deba@1981
  1498
          start();
deba@1981
  1499
        }
deba@1981
  1500
      }
deba@1981
  1501
    }
deba@1749
  1502
    ///@}
deba@1749
  1503
deba@1749
  1504
    /// \name Query Functions
deba@1749
  1505
    /// The result of the %DFS algorithm can be obtained using these
deba@1749
  1506
    /// functions.\n
deba@1749
  1507
    /// Before the use of these functions,
deba@1749
  1508
    /// either run() or start() must be called.
deba@1749
  1509
    ///@{
deba@1749
  1510
    /// \brief Checks if a node is reachable from the root.
deba@1749
  1511
    ///
deba@1749
  1512
    /// Returns \c true if \c v is reachable from the root(s).
deba@1749
  1513
    /// \warning The source nodes are inditated as unreachable.
deba@1749
  1514
    /// \pre Either \ref run() or \ref start()
deba@1749
  1515
    /// must be called before using this function.
deba@1749
  1516
    ///
deba@1749
  1517
    bool reached(Node v) { return (*_reached)[v]; }
deba@1749
  1518
    ///@}
deba@1749
  1519
  };
deba@1749
  1520
deba@1749
  1521
alpar@921
  1522
} //END OF NAMESPACE LEMON
alpar@780
  1523
alpar@780
  1524
#endif
alpar@780
  1525