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