lemon/dfs.h
author deba
Wed, 05 Oct 2005 16:45:37 +0000
changeset 1709 a323456bf7c8
parent 1694 6d81e6f7a88d
child 1710 f531c16dd923
permissions -rw-r--r--
Template Named Parameter bugfix
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/dfs.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_DFS_H
alpar@921
    18
#define LEMON_DFS_H
alpar@780
    19
alpar@780
    20
///\ingroup flowalgs
alpar@780
    21
///\file
alpar@1218
    22
///\brief Dfs algorithm.
alpar@780
    23
alpar@1218
    24
#include <lemon/list_graph.h>
klao@946
    25
#include <lemon/graph_utils.h>
alpar@921
    26
#include <lemon/invalid.h>
alpar@1218
    27
#include <lemon/error.h>
alpar@1218
    28
#include <lemon/maps.h>
alpar@780
    29
alpar@921
    30
namespace lemon {
alpar@780
    31
alpar@780
    32
alpar@1218
    33
  
alpar@1218
    34
  ///Default traits class of Dfs class.
alpar@1218
    35
alpar@1218
    36
  ///Default traits class of Dfs class.
alpar@1218
    37
  ///\param GR Graph type.
alpar@1218
    38
  template<class GR>
alpar@1218
    39
  struct DfsDefaultTraits
alpar@1218
    40
  {
alpar@1218
    41
    ///The graph type the algorithm runs on. 
alpar@1218
    42
    typedef GR Graph;
alpar@1218
    43
    ///\brief The type of the map that stores the last
alpar@1218
    44
    ///edges of the %DFS paths.
alpar@1218
    45
    /// 
alpar@1218
    46
    ///The type of the map that stores the last
alpar@1218
    47
    ///edges of the %DFS paths.
alpar@1218
    48
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    49
    ///
alpar@1218
    50
    typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
alpar@1218
    51
    ///Instantiates a PredMap.
alpar@1218
    52
 
alpar@1218
    53
    ///This function instantiates a \ref PredMap. 
alpar@1218
    54
    ///\param G is the graph, to which we would like to define the PredMap.
alpar@1218
    55
    ///\todo The graph alone may be insufficient to initialize
alpar@1218
    56
    static PredMap *createPredMap(const GR &G) 
alpar@1218
    57
    {
alpar@1218
    58
      return new PredMap(G);
alpar@1218
    59
    }
alpar@1218
    60
alpar@1218
    61
    ///The type of the map that indicates which nodes are processed.
alpar@1218
    62
 
alpar@1218
    63
    ///The type of the map that indicates which nodes are processed.
alpar@1218
    64
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    65
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
    66
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
    67
    ///Instantiates a ProcessedMap.
alpar@1218
    68
 
alpar@1218
    69
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
    70
    ///\param g is the graph, to which
alpar@1218
    71
    ///we would like to define the \ref ProcessedMap
alpar@1536
    72
#ifdef DOXYGEN
alpar@1536
    73
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
    74
#else
alpar@1367
    75
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
    76
#endif
alpar@1218
    77
    {
alpar@1218
    78
      return new ProcessedMap();
alpar@1218
    79
    }
alpar@1218
    80
    ///The type of the map that indicates which nodes are reached.
alpar@1218
    81
 
alpar@1218
    82
    ///The type of the map that indicates which nodes are reached.
alpar@1218
    83
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    84
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
    85
    typedef typename Graph::template NodeMap<bool> ReachedMap;
alpar@1218
    86
    ///Instantiates a ReachedMap.
alpar@1218
    87
 
alpar@1218
    88
    ///This function instantiates a \ref ReachedMap. 
alpar@1218
    89
    ///\param G is the graph, to which
alpar@1218
    90
    ///we would like to define the \ref ReachedMap.
alpar@1218
    91
    static ReachedMap *createReachedMap(const GR &G)
alpar@1218
    92
    {
alpar@1218
    93
      return new ReachedMap(G);
alpar@1218
    94
    }
alpar@1218
    95
    ///The type of the map that stores the dists of the nodes.
alpar@1218
    96
 
alpar@1218
    97
    ///The type of the map that stores the dists of the nodes.
alpar@1218
    98
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    99
    ///
alpar@1218
   100
    typedef typename Graph::template NodeMap<int> DistMap;
alpar@1218
   101
    ///Instantiates a DistMap.
alpar@1218
   102
 
alpar@1218
   103
    ///This function instantiates a \ref DistMap. 
alpar@1218
   104
    ///\param G is the graph, to which we would like to define the \ref DistMap
alpar@1218
   105
    static DistMap *createDistMap(const GR &G)
alpar@1218
   106
    {
alpar@1218
   107
      return new DistMap(G);
alpar@1218
   108
    }
alpar@1218
   109
  };
alpar@1218
   110
  
alpar@781
   111
  ///%DFS algorithm class.
alpar@1218
   112
  
alpar@1218
   113
  ///\ingroup flowalgs
alpar@1218
   114
  ///This class provides an efficient implementation of the %DFS algorithm.
alpar@780
   115
  ///
alpar@1218
   116
  ///\param GR The graph type the algorithm runs on. The default value is
alpar@1218
   117
  ///\ref ListGraph. The value of GR is not used directly by Dfs, it
alpar@1218
   118
  ///is only passed to \ref DfsDefaultTraits.
alpar@1218
   119
  ///\param TR Traits class to set various data types used by the algorithm.
alpar@1218
   120
  ///The default traits class is
alpar@1218
   121
  ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
alpar@1218
   122
  ///See \ref DfsDefaultTraits for the documentation of
alpar@1218
   123
  ///a Dfs traits class.
alpar@780
   124
  ///
alpar@1218
   125
  ///\author Jacint Szabo and Alpar Juttner
alpar@1218
   126
  ///\todo A compare object would be nice.
alpar@780
   127
alpar@780
   128
#ifdef DOXYGEN
alpar@1218
   129
  template <typename GR,
alpar@1218
   130
	    typename TR>
alpar@780
   131
#else
alpar@1218
   132
  template <typename GR=ListGraph,
alpar@1218
   133
	    typename TR=DfsDefaultTraits<GR> >
alpar@780
   134
#endif
alpar@1218
   135
  class Dfs {
alpar@780
   136
  public:
alpar@1218
   137
    /**
alpar@1218
   138
     * \brief \ref Exception for uninitialized parameters.
alpar@1218
   139
     *
alpar@1218
   140
     * This error represents problems in the initialization
alpar@1218
   141
     * of the parameters of the algorithms.
alpar@1218
   142
     */
alpar@1218
   143
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@1218
   144
    public:
alpar@1218
   145
      virtual const char* exceptionName() const {
alpar@1218
   146
	return "lemon::Dfs::UninitializedParameter";
alpar@1218
   147
      }
alpar@1218
   148
    };
alpar@1218
   149
alpar@1218
   150
    typedef TR Traits;
alpar@780
   151
    ///The type of the underlying graph.
alpar@1218
   152
    typedef typename TR::Graph Graph;
alpar@911
   153
    ///\e
alpar@780
   154
    typedef typename Graph::Node Node;
alpar@911
   155
    ///\e
alpar@780
   156
    typedef typename Graph::NodeIt NodeIt;
alpar@911
   157
    ///\e
alpar@780
   158
    typedef typename Graph::Edge Edge;
alpar@911
   159
    ///\e
alpar@780
   160
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@780
   161
    
alpar@780
   162
    ///\brief The type of the map that stores the last
alpar@1218
   163
    ///edges of the %DFS paths.
alpar@1218
   164
    typedef typename TR::PredMap PredMap;
alpar@1218
   165
    ///The type of the map indicating which nodes are reached.
alpar@1218
   166
    typedef typename TR::ReachedMap ReachedMap;
alpar@1218
   167
    ///The type of the map indicating which nodes are processed.
alpar@1218
   168
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@1218
   169
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   170
    typedef typename TR::DistMap DistMap;
alpar@780
   171
  private:
alpar@802
   172
    /// Pointer to the underlying graph.
alpar@780
   173
    const Graph *G;
alpar@802
   174
    ///Pointer to the map of predecessors edges.
alpar@1218
   175
    PredMap *_pred;
alpar@1218
   176
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@1218
   177
    bool local_pred;
alpar@802
   178
    ///Pointer to the map of distances.
alpar@1218
   179
    DistMap *_dist;
alpar@1218
   180
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
alpar@1218
   181
    bool local_dist;
alpar@1218
   182
    ///Pointer to the map of reached status of the nodes.
alpar@1218
   183
    ReachedMap *_reached;
alpar@1218
   184
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@1218
   185
    bool local_reached;
alpar@1218
   186
    ///Pointer to the map of processed status of the nodes.
alpar@1218
   187
    ProcessedMap *_processed;
alpar@1218
   188
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
alpar@1218
   189
    bool local_processed;
alpar@780
   190
alpar@1218
   191
    std::vector<typename Graph::OutEdgeIt> _stack;
alpar@1218
   192
    int _stack_head;
alpar@780
   193
alpar@1218
   194
    ///Creates the maps if necessary.
alpar@1218
   195
    
alpar@1218
   196
    ///\todo Error if \c G are \c NULL.
alpar@1218
   197
    ///\todo Better memory allocation (instead of new).
alpar@1218
   198
    void create_maps() 
alpar@780
   199
    {
alpar@1218
   200
      if(!_pred) {
alpar@1218
   201
	local_pred = true;
alpar@1218
   202
	_pred = Traits::createPredMap(*G);
alpar@780
   203
      }
alpar@1218
   204
      if(!_dist) {
alpar@1218
   205
	local_dist = true;
alpar@1218
   206
	_dist = Traits::createDistMap(*G);
alpar@780
   207
      }
alpar@1218
   208
      if(!_reached) {
alpar@1218
   209
	local_reached = true;
alpar@1218
   210
	_reached = Traits::createReachedMap(*G);
alpar@1218
   211
      }
alpar@1218
   212
      if(!_processed) {
alpar@1218
   213
	local_processed = true;
alpar@1218
   214
	_processed = Traits::createProcessedMap(*G);
alpar@780
   215
      }
alpar@780
   216
    }
alpar@780
   217
    
alpar@1218
   218
  public :
deba@1709
   219
deba@1709
   220
    typedef Dfs Create;
deba@1709
   221
alpar@1218
   222
    ///\name Named template parameters
alpar@1218
   223
alpar@1218
   224
    ///@{
alpar@1218
   225
alpar@1218
   226
    template <class T>
alpar@1218
   227
    struct DefPredMapTraits : public Traits {
alpar@1218
   228
      typedef T PredMap;
alpar@1218
   229
      static PredMap *createPredMap(const Graph &G) 
alpar@1218
   230
      {
alpar@1218
   231
	throw UninitializedParameter();
alpar@1218
   232
      }
alpar@1218
   233
    };
alpar@1218
   234
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1218
   235
alpar@1218
   236
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1218
   237
    ///
alpar@1218
   238
    template <class T>
deba@1709
   239
    struct DefPredMap : public Dfs<Graph, DefPredMapTraits<T> > {
deba@1709
   240
      typedef Dfs<Graph, DefPredMapTraits<T> > Create;
deba@1709
   241
    };
alpar@1218
   242
    
alpar@1218
   243
    
alpar@1218
   244
    template <class T>
alpar@1218
   245
    struct DefDistMapTraits : public Traits {
alpar@1218
   246
      typedef T DistMap;
alpar@1218
   247
      static DistMap *createDistMap(const Graph &G) 
alpar@1218
   248
      {
alpar@1218
   249
	throw UninitializedParameter();
alpar@1218
   250
      }
alpar@1218
   251
    };
alpar@1218
   252
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1218
   253
alpar@1218
   254
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1218
   255
    ///
alpar@1218
   256
    template <class T>
deba@1709
   257
    struct DefDistMap {
deba@1709
   258
      typedef Dfs<Graph, DefDistMapTraits<T> > Create;
deba@1709
   259
    };
alpar@1218
   260
    
alpar@1218
   261
    template <class T>
alpar@1218
   262
    struct DefReachedMapTraits : public Traits {
alpar@1218
   263
      typedef T ReachedMap;
alpar@1218
   264
      static ReachedMap *createReachedMap(const Graph &G) 
alpar@1218
   265
      {
alpar@1218
   266
	throw UninitializedParameter();
alpar@1218
   267
      }
alpar@1218
   268
    };
alpar@1218
   269
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@1218
   270
alpar@1218
   271
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@1218
   272
    ///
alpar@1218
   273
    template <class T>
deba@1709
   274
    struct DefReachedMap {
deba@1709
   275
      typedef Dfs< Graph, DefReachedMapTraits<T> > Create;
alpar@1218
   276
    };
deba@1709
   277
alpar@1218
   278
    template <class T>
alpar@1218
   279
    struct DefProcessedMapTraits : public Traits {
alpar@1218
   280
      typedef T ProcessedMap;
alpar@1218
   281
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1218
   282
      {
alpar@1218
   283
	throw UninitializedParameter();
alpar@1218
   284
      }
alpar@1218
   285
    };
alpar@1218
   286
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1218
   287
alpar@1218
   288
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1218
   289
    ///
alpar@1218
   290
    template <class T>
deba@1694
   291
    struct DefProcessedMap : public Dfs< Graph, DefProcessedMapTraits<T> > { 
deba@1709
   292
      typedef Dfs< Graph, DefProcessedMapTraits<T> > Create;
deba@1694
   293
    };
alpar@1218
   294
    
alpar@1218
   295
    struct DefGraphProcessedMapTraits : public Traits {
alpar@1218
   296
      typedef typename Graph::template NodeMap<bool> ProcessedMap;
alpar@1218
   297
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1218
   298
      {
alpar@1218
   299
	return new ProcessedMap(G);
alpar@1218
   300
      }
alpar@1218
   301
    };
alpar@1218
   302
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   303
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
alpar@1218
   304
    ///
alpar@1218
   305
    ///\ref named-templ-param "Named parameter"
alpar@1218
   306
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
alpar@1218
   307
    ///If you don't set it explicitely, it will be automatically allocated.
alpar@1218
   308
    template <class T>
alpar@1218
   309
    class DefProcessedMapToBeDefaultMap :
deba@1709
   310
      public Dfs< Graph, DefGraphProcessedMapTraits> { 
deba@1709
   311
      typedef Dfs< Graph, DefGraphProcessedMapTraits> Create;
deba@1709
   312
    };
alpar@1218
   313
    
alpar@1218
   314
    ///@}
alpar@1218
   315
alpar@1218
   316
  public:      
alpar@1218
   317
    
alpar@802
   318
    ///Constructor.
alpar@802
   319
    
alpar@802
   320
    ///\param _G the graph the algorithm will run on.
alpar@911
   321
    ///
alpar@780
   322
    Dfs(const Graph& _G) :
alpar@780
   323
      G(&_G),
alpar@1218
   324
      _pred(NULL), local_pred(false),
alpar@1218
   325
//       _predNode(NULL), local_predNode(false),
alpar@1218
   326
      _dist(NULL), local_dist(false),
alpar@1218
   327
      _reached(NULL), local_reached(false),
alpar@1218
   328
      _processed(NULL), local_processed(false)
alpar@780
   329
    { }
alpar@780
   330
    
alpar@802
   331
    ///Destructor.
alpar@780
   332
    ~Dfs() 
alpar@780
   333
    {
alpar@1218
   334
      if(local_pred) delete _pred;
alpar@1218
   335
//       if(local_predNode) delete _predNode;
alpar@1218
   336
      if(local_dist) delete _dist;
alpar@1218
   337
      if(local_reached) delete _reached;
alpar@1218
   338
      if(local_processed) delete _processed;
alpar@780
   339
    }
alpar@780
   340
alpar@780
   341
    ///Sets the map storing the predecessor edges.
alpar@780
   342
alpar@780
   343
    ///Sets the map storing the predecessor edges.
alpar@780
   344
    ///If you don't use this function before calling \ref run(),
alpar@780
   345
    ///it will allocate one. The destuctor deallocates this
alpar@780
   346
    ///automatically allocated map, of course.
alpar@780
   347
    ///\return <tt> (*this) </tt>
alpar@1218
   348
    Dfs &predMap(PredMap &m) 
alpar@780
   349
    {
alpar@1218
   350
      if(local_pred) {
alpar@1218
   351
	delete _pred;
alpar@1218
   352
	local_pred=false;
alpar@780
   353
      }
alpar@1218
   354
      _pred = &m;
alpar@780
   355
      return *this;
alpar@780
   356
    }
alpar@780
   357
alpar@1218
   358
//     ///Sets the map storing the predecessor nodes.
alpar@780
   359
alpar@1218
   360
//     ///Sets the map storing the predecessor nodes.
alpar@1218
   361
//     ///If you don't use this function before calling \ref run(),
alpar@1218
   362
//     ///it will allocate one. The destuctor deallocates this
alpar@1218
   363
//     ///automatically allocated map, of course.
alpar@1218
   364
//     ///\return <tt> (*this) </tt>
alpar@1218
   365
//     Dfs &predNodeMap(PredNodeMap &m) 
alpar@1218
   366
//     {
alpar@1218
   367
//       if(local_predNode) {
alpar@1218
   368
// 	delete _predNode;
alpar@1218
   369
// 	local_predNode=false;
alpar@1218
   370
//       }
alpar@1218
   371
//       _predNode = &m;
alpar@1218
   372
//       return *this;
alpar@1218
   373
//     }
alpar@780
   374
alpar@780
   375
    ///Sets the map storing the distances calculated by the algorithm.
alpar@780
   376
alpar@780
   377
    ///Sets the map storing the distances calculated by the algorithm.
alpar@780
   378
    ///If you don't use this function before calling \ref run(),
alpar@780
   379
    ///it will allocate one. The destuctor deallocates this
alpar@780
   380
    ///automatically allocated map, of course.
alpar@780
   381
    ///\return <tt> (*this) </tt>
alpar@1218
   382
    Dfs &distMap(DistMap &m) 
alpar@780
   383
    {
alpar@1218
   384
      if(local_dist) {
alpar@1218
   385
	delete _dist;
alpar@1218
   386
	local_dist=false;
alpar@780
   387
      }
alpar@1218
   388
      _dist = &m;
alpar@780
   389
      return *this;
alpar@780
   390
    }
alpar@780
   391
alpar@1220
   392
    ///Sets the map indicating if a node is reached.
alpar@1220
   393
alpar@1220
   394
    ///Sets the map indicating if a node is reached.
alpar@1220
   395
    ///If you don't use this function before calling \ref run(),
alpar@1220
   396
    ///it will allocate one. The destuctor deallocates this
alpar@1220
   397
    ///automatically allocated map, of course.
alpar@1220
   398
    ///\return <tt> (*this) </tt>
alpar@1220
   399
    Dfs &reachedMap(ReachedMap &m) 
alpar@1220
   400
    {
alpar@1220
   401
      if(local_reached) {
alpar@1220
   402
	delete _reached;
alpar@1220
   403
	local_reached=false;
alpar@1220
   404
      }
alpar@1220
   405
      _reached = &m;
alpar@1220
   406
      return *this;
alpar@1220
   407
    }
alpar@1220
   408
alpar@1220
   409
    ///Sets the map indicating if a node is processed.
alpar@1220
   410
alpar@1220
   411
    ///Sets the map indicating if a node is processed.
alpar@1220
   412
    ///If you don't use this function before calling \ref run(),
alpar@1220
   413
    ///it will allocate one. The destuctor deallocates this
alpar@1220
   414
    ///automatically allocated map, of course.
alpar@1220
   415
    ///\return <tt> (*this) </tt>
alpar@1220
   416
    Dfs &processedMap(ProcessedMap &m) 
alpar@1220
   417
    {
alpar@1220
   418
      if(local_processed) {
alpar@1220
   419
	delete _processed;
alpar@1220
   420
	local_processed=false;
alpar@1220
   421
      }
alpar@1220
   422
      _processed = &m;
alpar@1220
   423
      return *this;
alpar@1220
   424
    }
alpar@1220
   425
alpar@1218
   426
  public:
alpar@1218
   427
    ///\name Execution control
alpar@1218
   428
    ///The simplest way to execute the algorithm is to use
alpar@1218
   429
    ///one of the member functions called \c run(...).
alpar@1218
   430
    ///\n
alpar@1218
   431
    ///If you need more control on the execution,
alpar@1218
   432
    ///first you must call \ref init(), then you can add several source nodes
alpar@1218
   433
    ///with \ref addSource().
alpar@1218
   434
    ///Finally \ref start() will perform the actual path
alpar@1218
   435
    ///computation.
alpar@1218
   436
alpar@1218
   437
    ///@{
alpar@1218
   438
alpar@1218
   439
    ///Initializes the internal data structures.
alpar@1218
   440
alpar@1218
   441
    ///Initializes the internal data structures.
alpar@1218
   442
    ///
alpar@1218
   443
    void init()
alpar@1218
   444
    {
alpar@1218
   445
      create_maps();
alpar@1218
   446
      _stack.resize(countNodes(*G));
alpar@1218
   447
      _stack_head=-1;
alpar@780
   448
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@1218
   449
	_pred->set(u,INVALID);
alpar@1218
   450
	// _predNode->set(u,INVALID);
alpar@1218
   451
	_reached->set(u,false);
alpar@1218
   452
	_processed->set(u,false);
alpar@780
   453
      }
alpar@780
   454
    }
alpar@780
   455
    
alpar@1218
   456
    ///Adds a new source node.
alpar@780
   457
alpar@1218
   458
    ///Adds a new source node to the set of nodes to be processed.
alpar@1218
   459
    ///
athos@1443
   460
    ///\bug dists are wrong (or at least strange) in case of multiple sources.
alpar@1218
   461
    void addSource(Node s)
alpar@1218
   462
    {
alpar@1218
   463
      if(!(*_reached)[s])
alpar@1218
   464
	{
alpar@1218
   465
	  _reached->set(s,true);
alpar@1218
   466
	  _pred->set(s,INVALID);
alpar@1218
   467
	  // _predNode->set(u,INVALID);
alpar@1664
   468
	  OutEdgeIt e(*G,s);
alpar@1666
   469
	  if(e!=INVALID) {
alpar@1666
   470
	    _stack[++_stack_head]=e;
alpar@1666
   471
	    _dist->set(s,_stack_head);
alpar@1666
   472
	  }
alpar@1666
   473
	  else {
alpar@1666
   474
	    _processed->set(s,true);
alpar@1666
   475
	    _dist->set(s,0);
alpar@1666
   476
	  }
alpar@1218
   477
	}
alpar@1218
   478
    }
alpar@1218
   479
    
deba@1529
   480
    ///Processes the next edge.
alpar@1218
   481
deba@1529
   482
    ///Processes the next edge.
alpar@1218
   483
    ///
alpar@1516
   484
    ///\return The processed edge.
alpar@1516
   485
    ///
athos@1443
   486
    ///\pre The stack must not be empty!
alpar@1516
   487
    Edge processNextEdge()
alpar@1218
   488
    { 
alpar@1218
   489
      Node m;
alpar@1218
   490
      Edge e=_stack[_stack_head];
alpar@1218
   491
      if(!(*_reached)[m=G->target(e)]) {
alpar@1218
   492
	_pred->set(m,e);
alpar@1218
   493
	_reached->set(m,true);
alpar@1218
   494
	//	  _pred_node->set(m,G->source(e));
alpar@1233
   495
	++_stack_head;
alpar@1233
   496
	_stack[_stack_head] = OutEdgeIt(*G, m);
alpar@1218
   497
	_dist->set(m,_stack_head);
alpar@1218
   498
      }
alpar@1218
   499
      else {
alpar@1663
   500
	m=G->source(e);
alpar@1663
   501
	++_stack[_stack_head];
alpar@1663
   502
      }
alpar@1663
   503
      //'m' is now the (original) source of the _stack[_stack_head] 
alpar@1663
   504
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
alpar@1663
   505
	_processed->set(m,true);
alpar@1663
   506
	--_stack_head;
alpar@1663
   507
	if(_stack_head>=0) {
alpar@1663
   508
	  m=G->source(_stack[_stack_head]);
alpar@1663
   509
	  ++_stack[_stack_head];
alpar@1663
   510
	}
alpar@1218
   511
      }
alpar@1516
   512
      return e;
alpar@1218
   513
    }
alpar@1665
   514
    ///Next edge to be processed.
alpar@1665
   515
alpar@1665
   516
    ///Next edge to be processed.
alpar@1665
   517
    ///
alpar@1665
   518
    ///\return The next edge to be processed or INVALID if the stack is
alpar@1665
   519
    /// empty.
deba@1694
   520
    OutEdgeIt nextEdge()
alpar@1665
   521
    { 
alpar@1665
   522
      return _stack_head>=0?_stack[_stack_head]:INVALID;
alpar@1665
   523
    }
deba@1694
   524
alpar@1218
   525
    ///\brief Returns \c false if there are nodes
alpar@1218
   526
    ///to be processed in the queue
alpar@1218
   527
    ///
alpar@1218
   528
    ///Returns \c false if there are nodes
alpar@1218
   529
    ///to be processed in the queue
alpar@1665
   530
    ///
alpar@1665
   531
    ///\todo This should be called emptyStack() or some "neutral" name.
alpar@1218
   532
    bool emptyQueue() { return _stack_head<0; }
alpar@1218
   533
    ///Returns the number of the nodes to be processed.
alpar@1218
   534
    
alpar@1218
   535
    ///Returns the number of the nodes to be processed in the queue.
alpar@1218
   536
    ///
alpar@1665
   537
    ///\todo This should be called stackSize() or some "neutral" name.
alpar@1218
   538
    int queueSize() { return _stack_head+1; }
alpar@1218
   539
    
alpar@1218
   540
    ///Executes the algorithm.
alpar@1218
   541
alpar@1218
   542
    ///Executes the algorithm.
alpar@1218
   543
    ///
alpar@1218
   544
    ///\pre init() must be called and at least one node should be added
alpar@1218
   545
    ///with addSource() before using this function.
alpar@1218
   546
    ///
alpar@1218
   547
    ///This method runs the %DFS algorithm from the root node(s)
alpar@1218
   548
    ///in order to
alpar@1218
   549
    ///compute the
alpar@1218
   550
    ///%DFS path to each node. The algorithm computes
alpar@1218
   551
    ///- The %DFS tree.
athos@1443
   552
    ///- The distance of each node from the root(s) in the %DFS tree.
alpar@1218
   553
    ///
alpar@1218
   554
    void start()
alpar@1218
   555
    {
alpar@1218
   556
      while ( !emptyQueue() ) processNextEdge();
alpar@1218
   557
    }
alpar@1218
   558
    
alpar@1218
   559
    ///Executes the algorithm until \c dest is reached.
alpar@1218
   560
alpar@1218
   561
    ///Executes the algorithm until \c dest is reached.
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
    ///
alpar@1218
   566
    ///This method runs the %DFS algorithm from the root node(s)
alpar@1218
   567
    ///in order to
alpar@1218
   568
    ///compute the
alpar@1218
   569
    ///%DFS path to \c dest. The algorithm computes
alpar@1218
   570
    ///- The %DFS path to \c  dest.
athos@1443
   571
    ///- The distance of \c dest from the root(s) in the %DFS tree.
alpar@1218
   572
    ///
alpar@1218
   573
    void start(Node dest)
alpar@1218
   574
    {
alpar@1233
   575
      while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) 
alpar@1233
   576
	processNextEdge();
alpar@1218
   577
    }
alpar@1218
   578
    
alpar@1218
   579
    ///Executes the algorithm until a condition is met.
alpar@1218
   580
alpar@1218
   581
    ///Executes the algorithm until a condition is met.
alpar@1218
   582
    ///
alpar@1218
   583
    ///\pre init() must be called and at least one node should be added
alpar@1218
   584
    ///with addSource() before using this function.
alpar@1218
   585
    ///
alpar@1233
   586
    ///\param nm must be a bool (or convertible) edge map. The algorithm
athos@1438
   587
    ///will stop when it reaches an edge \c e with <tt>nm[e]==true</tt>.
athos@1443
   588
    ///
athos@1438
   589
    ///\warning Contrary to \ref Dfs and \ref Dijkstra, \c nm is an edge map,
alpar@1233
   590
    ///not a node map.
alpar@1218
   591
    template<class NM>
alpar@1218
   592
      void start(const NM &nm)
alpar@1218
   593
      {
alpar@1233
   594
	while ( !emptyQueue() && !nm[_stack[_stack_head]] ) processNextEdge();
alpar@1218
   595
      }
alpar@1218
   596
    
alpar@1218
   597
    ///Runs %DFS algorithm from node \c s.
alpar@1218
   598
    
alpar@1218
   599
    ///This method runs the %DFS algorithm from a root node \c s
alpar@1218
   600
    ///in order to
alpar@1218
   601
    ///compute the
alpar@1218
   602
    ///%DFS path to each node. The algorithm computes
alpar@1218
   603
    ///- The %DFS tree.
athos@1443
   604
    ///- The distance of each node from the root in the %DFS tree.
alpar@1218
   605
    ///
alpar@1218
   606
    ///\note d.run(s) is just a shortcut of the following code.
alpar@1218
   607
    ///\code
alpar@1218
   608
    ///  d.init();
alpar@1218
   609
    ///  d.addSource(s);
alpar@1218
   610
    ///  d.start();
alpar@1218
   611
    ///\endcode
alpar@1218
   612
    void run(Node s) {
alpar@1218
   613
      init();
alpar@1218
   614
      addSource(s);
alpar@1218
   615
      start();
alpar@1218
   616
    }
alpar@1218
   617
    
alpar@1218
   618
    ///Finds the %DFS path between \c s and \c t.
alpar@1218
   619
    
alpar@1218
   620
    ///Finds the %DFS path between \c s and \c t.
alpar@1218
   621
    ///
alpar@1218
   622
    ///\return The length of the %DFS s---t path if there exists one,
alpar@1218
   623
    ///0 otherwise.
athos@1540
   624
    ///\note Apart from the return value, d.run(s,t) is
alpar@1218
   625
    ///just a shortcut of the following code.
alpar@1218
   626
    ///\code
alpar@1218
   627
    ///  d.init();
alpar@1218
   628
    ///  d.addSource(s);
alpar@1218
   629
    ///  d.start(t);
alpar@1218
   630
    ///\endcode
alpar@1218
   631
    int run(Node s,Node t) {
alpar@1218
   632
      init();
alpar@1218
   633
      addSource(s);
alpar@1218
   634
      start(t);
alpar@1233
   635
      return reached(t)?_stack_head+1:0;
alpar@1218
   636
    }
alpar@1218
   637
    
alpar@1218
   638
    ///@}
alpar@1218
   639
alpar@1218
   640
    ///\name Query Functions
alpar@1218
   641
    ///The result of the %DFS algorithm can be obtained using these
alpar@1218
   642
    ///functions.\n
alpar@1218
   643
    ///Before the use of these functions,
alpar@1218
   644
    ///either run() or start() must be called.
alpar@1218
   645
    
alpar@1218
   646
    ///@{
alpar@1218
   647
alpar@1283
   648
    ///Copies the path to \c t on the DFS tree into \c p
alpar@1283
   649
    
athos@1443
   650
    ///This function copies the path to \c t on the DFS tree  into \c p.
athos@1438
   651
    ///If \c t is a source itself or unreachable, then it does not
alpar@1283
   652
    ///alter \c p.
athos@1438
   653
    ///\todo Is this the right way to handle unreachable nodes?
athos@1438
   654
    ///
alpar@1283
   655
    ///\return Returns \c true if a path to \c t was actually copied to \c p,
alpar@1283
   656
    ///\c false otherwise.
alpar@1283
   657
    ///\sa DirPath
alpar@1283
   658
    template<class P>
alpar@1283
   659
    bool getPath(P &p,Node t) 
alpar@1283
   660
    {
alpar@1283
   661
      if(reached(t)) {
alpar@1283
   662
	p.clear();
alpar@1283
   663
	typename P::Builder b(p);
alpar@1283
   664
	for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
alpar@1283
   665
	  b.pushFront(pred(t));
alpar@1283
   666
	b.commit();
alpar@1283
   667
	return true;
alpar@1283
   668
      }
alpar@1283
   669
      return false;
alpar@1283
   670
    }
alpar@1283
   671
alpar@1218
   672
    ///The distance of a node from the root(s).
alpar@1218
   673
alpar@1218
   674
    ///Returns the distance of a node from the root(s).
alpar@780
   675
    ///\pre \ref run() must be called before using this function.
athos@1438
   676
    ///\warning If node \c v is unreachable from the root(s) then the return value
alpar@780
   677
    ///of this funcion is undefined.
alpar@1218
   678
    int dist(Node v) const { return (*_dist)[v]; }
alpar@780
   679
alpar@1218
   680
    ///Returns the 'previous edge' of the %DFS tree.
alpar@780
   681
alpar@1218
   682
    ///For a node \c v it returns the 'previous edge'
alpar@1218
   683
    ///of the %DFS path,
alpar@1218
   684
    ///i.e. it returns the last edge of a %DFS path from the root(s) to \c
alpar@780
   685
    ///v. It is \ref INVALID
alpar@1218
   686
    ///if \c v is unreachable from the root(s) or \c v is a root. The
alpar@781
   687
    ///%DFS tree used here is equal to the %DFS tree used in
alpar@1631
   688
    ///\ref predNode().
alpar@1218
   689
    ///\pre Either \ref run() or \ref start() must be called before using
alpar@780
   690
    ///this function.
alpar@1218
   691
    ///\todo predEdge could be a better name.
alpar@1218
   692
    Edge pred(Node v) const { return (*_pred)[v];}
alpar@780
   693
alpar@781
   694
    ///Returns the 'previous node' of the %DFS tree.
alpar@780
   695
alpar@1218
   696
    ///For a node \c v it returns the 'previous node'
alpar@1218
   697
    ///of the %DFS tree,
alpar@1218
   698
    ///i.e. it returns the last but one node from a %DFS path from the
alpar@1218
   699
    ///root(a) to \c /v.
alpar@1218
   700
    ///It is INVALID if \c v is unreachable from the root(s) or
alpar@1218
   701
    ///if \c v itself a root.
alpar@1218
   702
    ///The %DFS tree used here is equal to the %DFS
alpar@1631
   703
    ///tree used in \ref pred().
alpar@1218
   704
    ///\pre Either \ref run() or \ref start() must be called before
alpar@780
   705
    ///using this function.
alpar@1218
   706
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@1218
   707
				  G->source((*_pred)[v]); }
alpar@780
   708
    
alpar@1218
   709
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   710
alpar@1218
   711
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   712
    ///\pre Either \ref run() or \ref init() must
alpar@780
   713
    ///be called before using this function.
alpar@1218
   714
    const DistMap &distMap() const { return *_dist;}
alpar@780
   715
 
alpar@1218
   716
    ///Returns a reference to the %DFS edge-tree map.
alpar@780
   717
alpar@780
   718
    ///Returns a reference to the NodeMap of the edges of the
alpar@781
   719
    ///%DFS tree.
alpar@1218
   720
    ///\pre Either \ref run() or \ref init()
alpar@1218
   721
    ///must be called before using this function.
alpar@1218
   722
    const PredMap &predMap() const { return *_pred;}
alpar@780
   723
 
alpar@1218
   724
//     ///Returns a reference to the map of nodes of %DFS paths.
alpar@780
   725
alpar@1218
   726
//     ///Returns a reference to the NodeMap of the last but one nodes of the
alpar@1218
   727
//     ///%DFS tree.
alpar@1218
   728
//     ///\pre \ref run() must be called before using this function.
alpar@1218
   729
//     const PredNodeMap &predNodeMap() const { return *_predNode;}
alpar@780
   730
alpar@780
   731
    ///Checks if a node is reachable from the root.
alpar@780
   732
athos@1438
   733
    ///Returns \c true if \c v is reachable from the root(s).
athos@1438
   734
    ///\warning The source nodes are inditated as unreachable.
alpar@1218
   735
    ///\pre Either \ref run() or \ref start()
alpar@1218
   736
    ///must be called before using this function.
alpar@780
   737
    ///
alpar@1218
   738
    bool reached(Node v) { return (*_reached)[v]; }
alpar@1218
   739
    
alpar@1218
   740
    ///@}
alpar@1218
   741
  };
alpar@1218
   742
alpar@1218
   743
  ///Default traits class of Dfs function.
alpar@1218
   744
alpar@1218
   745
  ///Default traits class of Dfs function.
alpar@1218
   746
  ///\param GR Graph type.
alpar@1218
   747
  template<class GR>
alpar@1218
   748
  struct DfsWizardDefaultTraits
alpar@1218
   749
  {
alpar@1218
   750
    ///The graph type the algorithm runs on. 
alpar@1218
   751
    typedef GR Graph;
alpar@1218
   752
    ///\brief The type of the map that stores the last
alpar@1218
   753
    ///edges of the %DFS paths.
alpar@1218
   754
    /// 
alpar@1218
   755
    ///The type of the map that stores the last
alpar@1218
   756
    ///edges of the %DFS paths.
alpar@1218
   757
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@780
   758
    ///
alpar@1218
   759
    typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
alpar@1218
   760
    ///Instantiates a PredMap.
alpar@1218
   761
 
alpar@1218
   762
    ///This function instantiates a \ref PredMap. 
alpar@1536
   763
    ///\param g is the graph, to which we would like to define the PredMap.
alpar@1218
   764
    ///\todo The graph alone may be insufficient to initialize
alpar@1536
   765
#ifdef DOXYGEN
alpar@1536
   766
    static PredMap *createPredMap(const GR &g) 
alpar@1536
   767
#else
alpar@1367
   768
    static PredMap *createPredMap(const GR &) 
alpar@1536
   769
#endif
alpar@1218
   770
    {
alpar@1218
   771
      return new PredMap();
alpar@1218
   772
    }
alpar@1218
   773
//     ///\brief The type of the map that stores the last but one
alpar@1218
   774
//     ///nodes of the %DFS paths.
alpar@1218
   775
//     ///
alpar@1218
   776
//     ///The type of the map that stores the last but one
alpar@1218
   777
//     ///nodes of the %DFS paths.
alpar@1218
   778
//     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   779
//     ///
alpar@1218
   780
//     typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
alpar@1218
   781
//     ///Instantiates a PredNodeMap.
alpar@1218
   782
    
alpar@1218
   783
//     ///This function instantiates a \ref PredNodeMap. 
alpar@1218
   784
//     ///\param G is the graph, to which
alpar@1218
   785
//     ///we would like to define the \ref PredNodeMap
alpar@1218
   786
//     static PredNodeMap *createPredNodeMap(const GR &G)
alpar@1218
   787
//     {
alpar@1218
   788
//       return new PredNodeMap();
alpar@1218
   789
//     }
alpar@1218
   790
alpar@1218
   791
    ///The type of the map that indicates which nodes are processed.
alpar@1218
   792
 
alpar@1218
   793
    ///The type of the map that indicates which nodes are processed.
alpar@1218
   794
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   795
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   796
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   797
    ///Instantiates a ProcessedMap.
alpar@1218
   798
 
alpar@1218
   799
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
   800
    ///\param g is the graph, to which
alpar@1218
   801
    ///we would like to define the \ref ProcessedMap
alpar@1536
   802
#ifdef DOXYGEN
alpar@1536
   803
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   804
#else
alpar@1367
   805
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   806
#endif
alpar@1218
   807
    {
alpar@1218
   808
      return new ProcessedMap();
alpar@1218
   809
    }
alpar@1218
   810
    ///The type of the map that indicates which nodes are reached.
alpar@1218
   811
 
alpar@1218
   812
    ///The type of the map that indicates which nodes are reached.
alpar@1218
   813
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   814
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   815
    typedef typename Graph::template NodeMap<bool> ReachedMap;
alpar@1218
   816
    ///Instantiates a ReachedMap.
alpar@1218
   817
 
alpar@1218
   818
    ///This function instantiates a \ref ReachedMap. 
alpar@1218
   819
    ///\param G is the graph, to which
alpar@1218
   820
    ///we would like to define the \ref ReachedMap.
alpar@1218
   821
    static ReachedMap *createReachedMap(const GR &G)
alpar@1218
   822
    {
alpar@1218
   823
      return new ReachedMap(G);
alpar@1218
   824
    }
alpar@1218
   825
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   826
 
alpar@1218
   827
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   828
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   829
    ///
alpar@1218
   830
    typedef NullMap<typename Graph::Node,int> DistMap;
alpar@1218
   831
    ///Instantiates a DistMap.
alpar@1218
   832
 
alpar@1218
   833
    ///This function instantiates a \ref DistMap. 
alpar@1536
   834
    ///\param g is the graph, to which we would like to define the \ref DistMap
alpar@1536
   835
#ifdef DOXYGEN
alpar@1536
   836
    static DistMap *createDistMap(const GR &g)
alpar@1536
   837
#else
alpar@1367
   838
    static DistMap *createDistMap(const GR &)
alpar@1536
   839
#endif
alpar@1218
   840
    {
alpar@1218
   841
      return new DistMap();
alpar@1218
   842
    }
alpar@1218
   843
  };
alpar@1218
   844
  
alpar@1218
   845
  /// Default traits used by \ref DfsWizard
alpar@1218
   846
alpar@1218
   847
  /// To make it easier to use Dfs algorithm
alpar@1218
   848
  ///we have created a wizard class.
alpar@1218
   849
  /// This \ref DfsWizard class needs default traits,
alpar@1218
   850
  ///as well as the \ref Dfs class.
alpar@1218
   851
  /// The \ref DfsWizardBase is a class to be the default traits of the
alpar@1218
   852
  /// \ref DfsWizard class.
alpar@1218
   853
  template<class GR>
alpar@1218
   854
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
alpar@1218
   855
  {
alpar@1218
   856
alpar@1218
   857
    typedef DfsWizardDefaultTraits<GR> Base;
alpar@1218
   858
  protected:
alpar@1218
   859
    /// Type of the nodes in the graph.
alpar@1218
   860
    typedef typename Base::Graph::Node Node;
alpar@1218
   861
alpar@1218
   862
    /// Pointer to the underlying graph.
alpar@1218
   863
    void *_g;
alpar@1218
   864
    ///Pointer to the map of reached nodes.
alpar@1218
   865
    void *_reached;
alpar@1218
   866
    ///Pointer to the map of processed nodes.
alpar@1218
   867
    void *_processed;
alpar@1218
   868
    ///Pointer to the map of predecessors edges.
alpar@1218
   869
    void *_pred;
alpar@1218
   870
//     ///Pointer to the map of predecessors nodes.
alpar@1218
   871
//     void *_predNode;
alpar@1218
   872
    ///Pointer to the map of distances.
alpar@1218
   873
    void *_dist;
alpar@1218
   874
    ///Pointer to the source node.
alpar@1218
   875
    Node _source;
alpar@1218
   876
    
alpar@1218
   877
    public:
alpar@1218
   878
    /// Constructor.
alpar@1218
   879
    
alpar@1218
   880
    /// This constructor does not require parameters, therefore it initiates
alpar@1218
   881
    /// all of the attributes to default values (0, INVALID).
alpar@1218
   882
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
alpar@1218
   883
// 			   _predNode(0),
alpar@1218
   884
			   _dist(0), _source(INVALID) {}
alpar@1218
   885
alpar@1218
   886
    /// Constructor.
alpar@1218
   887
    
alpar@1218
   888
    /// This constructor requires some parameters,
alpar@1218
   889
    /// listed in the parameters list.
alpar@1218
   890
    /// Others are initiated to 0.
alpar@1218
   891
    /// \param g is the initial value of  \ref _g
alpar@1218
   892
    /// \param s is the initial value of  \ref _source
alpar@1218
   893
    DfsWizardBase(const GR &g, Node s=INVALID) :
alpar@1218
   894
      _g((void *)&g), _reached(0), _processed(0), _pred(0),
alpar@1218
   895
//       _predNode(0),
alpar@1218
   896
      _dist(0), _source(s) {}
alpar@1218
   897
alpar@1218
   898
  };
alpar@1218
   899
  
athos@1443
   900
  /// A class to make the usage of the Dfs algorithm easier
alpar@1218
   901
athos@1443
   902
  /// This class is created to make it easier to use the Dfs algorithm.
alpar@1218
   903
  /// It uses the functions and features of the plain \ref Dfs,
alpar@1218
   904
  /// but it is much simpler to use it.
alpar@1218
   905
  ///
alpar@1218
   906
  /// Simplicity means that the way to change the types defined
alpar@1218
   907
  /// in the traits class is based on functions that returns the new class
alpar@1218
   908
  /// and not on templatable built-in classes.
alpar@1218
   909
  /// When using the plain \ref Dfs
alpar@1218
   910
  /// the new class with the modified type comes from
alpar@1218
   911
  /// the original class by using the ::
alpar@1218
   912
  /// operator. In the case of \ref DfsWizard only
alpar@1218
   913
  /// a function have to be called and it will
alpar@1218
   914
  /// return the needed class.
alpar@1218
   915
  ///
alpar@1218
   916
  /// It does not have own \ref run method. When its \ref run method is called
athos@1438
   917
  /// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run
alpar@1218
   918
  /// method of it.
alpar@1218
   919
  template<class TR>
alpar@1218
   920
  class DfsWizard : public TR
alpar@1218
   921
  {
alpar@1218
   922
    typedef TR Base;
alpar@1218
   923
alpar@1218
   924
    ///The type of the underlying graph.
alpar@1218
   925
    typedef typename TR::Graph Graph;
alpar@1218
   926
    //\e
alpar@1218
   927
    typedef typename Graph::Node Node;
alpar@1218
   928
    //\e
alpar@1218
   929
    typedef typename Graph::NodeIt NodeIt;
alpar@1218
   930
    //\e
alpar@1218
   931
    typedef typename Graph::Edge Edge;
alpar@1218
   932
    //\e
alpar@1218
   933
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1218
   934
    
alpar@1218
   935
    ///\brief The type of the map that stores
alpar@1218
   936
    ///the reached nodes
alpar@1218
   937
    typedef typename TR::ReachedMap ReachedMap;
alpar@1218
   938
    ///\brief The type of the map that stores
alpar@1218
   939
    ///the processed nodes
alpar@1218
   940
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@1218
   941
    ///\brief The type of the map that stores the last
alpar@1218
   942
    ///edges of the %DFS paths.
alpar@1218
   943
    typedef typename TR::PredMap PredMap;
alpar@1218
   944
//     ///\brief The type of the map that stores the last but one
alpar@1218
   945
//     ///nodes of the %DFS paths.
alpar@1218
   946
//     typedef typename TR::PredNodeMap PredNodeMap;
athos@1443
   947
    ///The type of the map that stores the distances of the nodes.
alpar@1218
   948
    typedef typename TR::DistMap DistMap;
alpar@1218
   949
alpar@1218
   950
public:
alpar@1218
   951
    /// Constructor.
alpar@1218
   952
    DfsWizard() : TR() {}
alpar@1218
   953
alpar@1218
   954
    /// Constructor that requires parameters.
alpar@1218
   955
alpar@1218
   956
    /// Constructor that requires parameters.
alpar@1218
   957
    /// These parameters will be the default values for the traits class.
alpar@1218
   958
    DfsWizard(const Graph &g, Node s=INVALID) :
alpar@1218
   959
      TR(g,s) {}
alpar@1218
   960
alpar@1218
   961
    ///Copy constructor
alpar@1218
   962
    DfsWizard(const TR &b) : TR(b) {}
alpar@1218
   963
alpar@1218
   964
    ~DfsWizard() {}
alpar@1218
   965
alpar@1218
   966
    ///Runs Dfs algorithm from a given node.
alpar@1218
   967
    
alpar@1218
   968
    ///Runs Dfs algorithm from a given node.
alpar@1218
   969
    ///The node can be given by the \ref source function.
alpar@1218
   970
    void run()
alpar@1218
   971
    {
alpar@1218
   972
      if(Base::_source==INVALID) throw UninitializedParameter();
alpar@1218
   973
      Dfs<Graph,TR> alg(*(Graph*)Base::_g);
alpar@1218
   974
      if(Base::_reached) alg.reachedMap(*(ReachedMap*)Base::_reached);
alpar@1218
   975
      if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
alpar@1218
   976
      if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
alpar@1218
   977
//       if(Base::_predNode) alg.predNodeMap(*(PredNodeMap*)Base::_predNode);
alpar@1218
   978
      if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
alpar@1218
   979
      alg.run(Base::_source);
alpar@1218
   980
    }
alpar@1218
   981
alpar@1218
   982
    ///Runs Dfs algorithm from the given node.
alpar@1218
   983
alpar@1218
   984
    ///Runs Dfs algorithm from the given node.
alpar@1218
   985
    ///\param s is the given source.
alpar@1218
   986
    void run(Node s)
alpar@1218
   987
    {
alpar@1218
   988
      Base::_source=s;
alpar@1218
   989
      run();
alpar@1218
   990
    }
alpar@1218
   991
alpar@1218
   992
    template<class T>
alpar@1218
   993
    struct DefPredMapBase : public Base {
alpar@1218
   994
      typedef T PredMap;
alpar@1367
   995
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
   996
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1218
   997
    };
alpar@1218
   998
    
alpar@1218
   999
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1000
    ///function for setting PredMap type
alpar@1218
  1001
    ///
alpar@1218
  1002
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1003
    ///function for setting PredMap type
alpar@1218
  1004
    ///
alpar@1218
  1005
    template<class T>
alpar@1218
  1006
    DfsWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@1218
  1007
    {
alpar@1218
  1008
      Base::_pred=(void *)&t;
alpar@1218
  1009
      return DfsWizard<DefPredMapBase<T> >(*this);
alpar@1218
  1010
    }
alpar@1218
  1011
    
alpar@1218
  1012
 
alpar@1218
  1013
    template<class T>
alpar@1218
  1014
    struct DefReachedMapBase : public Base {
alpar@1218
  1015
      typedef T ReachedMap;
alpar@1367
  1016
      static ReachedMap *createReachedMap(const Graph &) { return 0; };
alpar@1236
  1017
      DefReachedMapBase(const TR &b) : TR(b) {}
alpar@1218
  1018
    };
alpar@1218
  1019
    
alpar@1218
  1020
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1021
    ///function for setting ReachedMap
alpar@1218
  1022
    ///
alpar@1218
  1023
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1024
    ///function for setting ReachedMap
alpar@1218
  1025
    ///
alpar@1218
  1026
    template<class T>
alpar@1218
  1027
    DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
alpar@1218
  1028
    {
alpar@1218
  1029
      Base::_pred=(void *)&t;
alpar@1218
  1030
      return DfsWizard<DefReachedMapBase<T> >(*this);
alpar@1218
  1031
    }
alpar@1218
  1032
    
alpar@1218
  1033
alpar@1218
  1034
    template<class T>
alpar@1218
  1035
    struct DefProcessedMapBase : public Base {
alpar@1218
  1036
      typedef T ProcessedMap;
alpar@1367
  1037
      static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
alpar@1236
  1038
      DefProcessedMapBase(const TR &b) : TR(b) {}
alpar@1218
  1039
    };
alpar@1218
  1040
    
alpar@1218
  1041
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1042
    ///function for setting ProcessedMap
alpar@1218
  1043
    ///
alpar@1218
  1044
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1045
    ///function for setting ProcessedMap
alpar@1218
  1046
    ///
alpar@1218
  1047
    template<class T>
alpar@1218
  1048
    DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
alpar@1218
  1049
    {
alpar@1218
  1050
      Base::_pred=(void *)&t;
alpar@1218
  1051
      return DfsWizard<DefProcessedMapBase<T> >(*this);
alpar@1218
  1052
    }
alpar@1218
  1053
    
alpar@1218
  1054
alpar@1218
  1055
//     template<class T>
alpar@1218
  1056
//     struct DefPredNodeMapBase : public Base {
alpar@1218
  1057
//       typedef T PredNodeMap;
alpar@1218
  1058
//       static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
alpar@1236
  1059
//       DefPredNodeMapBase(const TR &b) : TR(b) {}
alpar@1218
  1060
//     };
alpar@1218
  1061
    
alpar@1218
  1062
//     ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1063
//     ///function for setting PredNodeMap type
alpar@1218
  1064
//     ///
alpar@1218
  1065
//     /// \ref named-templ-param "Named parameter"
alpar@1218
  1066
//     ///function for setting PredNodeMap type
alpar@1218
  1067
//     ///
alpar@1218
  1068
//     template<class T>
alpar@1218
  1069
//     DfsWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
alpar@1218
  1070
//     {
alpar@1218
  1071
//       Base::_predNode=(void *)&t;
alpar@1218
  1072
//       return DfsWizard<DefPredNodeMapBase<T> >(*this);
alpar@1218
  1073
//     }
alpar@1218
  1074
   
alpar@1218
  1075
    template<class T>
alpar@1218
  1076
    struct DefDistMapBase : public Base {
alpar@1218
  1077
      typedef T DistMap;
alpar@1367
  1078
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
  1079
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1218
  1080
    };
alpar@1218
  1081
    
alpar@1218
  1082
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1083
    ///function for setting DistMap type
alpar@1218
  1084
    ///
alpar@1218
  1085
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1086
    ///function for setting DistMap type
alpar@1218
  1087
    ///
alpar@1218
  1088
    template<class T>
alpar@1218
  1089
    DfsWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@1218
  1090
    {
alpar@1218
  1091
      Base::_dist=(void *)&t;
alpar@1218
  1092
      return DfsWizard<DefDistMapBase<T> >(*this);
alpar@1218
  1093
    }
alpar@1218
  1094
    
alpar@1218
  1095
    /// Sets the source node, from which the Dfs algorithm runs.
alpar@1218
  1096
alpar@1218
  1097
    /// Sets the source node, from which the Dfs algorithm runs.
alpar@1218
  1098
    /// \param s is the source node.
alpar@1218
  1099
    DfsWizard<TR> &source(Node s) 
alpar@1218
  1100
    {
alpar@1218
  1101
      Base::_source=s;
alpar@1218
  1102
      return *this;
alpar@1218
  1103
    }
alpar@780
  1104
    
alpar@780
  1105
  };
alpar@780
  1106
  
alpar@1218
  1107
  ///Function type interface for Dfs algorithm.
alpar@1218
  1108
alpar@1218
  1109
  /// \ingroup flowalgs
alpar@1218
  1110
  ///Function type interface for Dfs algorithm.
alpar@1218
  1111
  ///
alpar@1218
  1112
  ///This function also has several
alpar@1218
  1113
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1114
  ///they are declared as the members of class \ref DfsWizard.
alpar@1218
  1115
  ///The following
alpar@1218
  1116
  ///example shows how to use these parameters.
alpar@1218
  1117
  ///\code
alpar@1218
  1118
  ///  dfs(g,source).predMap(preds).run();
alpar@1218
  1119
  ///\endcode
alpar@1218
  1120
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
alpar@1218
  1121
  ///to the end of the parameter list.
alpar@1218
  1122
  ///\sa DfsWizard
alpar@1218
  1123
  ///\sa Dfs
alpar@1218
  1124
  template<class GR>
alpar@1218
  1125
  DfsWizard<DfsWizardBase<GR> >
alpar@1218
  1126
  dfs(const GR &g,typename GR::Node s=INVALID)
alpar@1218
  1127
  {
alpar@1218
  1128
    return DfsWizard<DfsWizardBase<GR> >(g,s);
alpar@1218
  1129
  }
alpar@1218
  1130
alpar@921
  1131
} //END OF NAMESPACE LEMON
alpar@780
  1132
alpar@780
  1133
#endif
alpar@780
  1134