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