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