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