lemon/dfs.h
author Peter Kovacs <kpeter@inf.elte.hu>
Wed, 17 Feb 2010 23:10:36 +0100
changeset 836 c841ae1aca29
parent 788 c92296660262
child 877 141f9c0db4a3
permissions -rw-r--r--
Modify the header of scripts/bib2dox.py (#184)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@100
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
alpar@100
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100
     8
 *
alpar@100
     9
 * Permission to use, modify and distribute this software is granted
alpar@100
    10
 * provided that this copyright notice appears in all copies. For
alpar@100
    11
 * precise terms see the accompanying LICENSE file.
alpar@100
    12
 *
alpar@100
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@100
    14
 * express or implied, and with no claim as to its suitability for any
alpar@100
    15
 * purpose.
alpar@100
    16
 *
alpar@100
    17
 */
alpar@100
    18
alpar@100
    19
#ifndef LEMON_DFS_H
alpar@100
    20
#define LEMON_DFS_H
alpar@100
    21
alpar@100
    22
///\ingroup search
alpar@100
    23
///\file
kpeter@244
    24
///\brief DFS algorithm.
alpar@100
    25
alpar@100
    26
#include <lemon/list_graph.h>
alpar@100
    27
#include <lemon/bits/path_dump.h>
deba@220
    28
#include <lemon/core.h>
alpar@100
    29
#include <lemon/error.h>
alpar@100
    30
#include <lemon/maps.h>
kpeter@278
    31
#include <lemon/path.h>
alpar@100
    32
alpar@100
    33
namespace lemon {
alpar@100
    34
alpar@100
    35
  ///Default traits class of Dfs class.
alpar@100
    36
alpar@100
    37
  ///Default traits class of Dfs class.
kpeter@157
    38
  ///\tparam GR Digraph type.
alpar@100
    39
  template<class GR>
alpar@100
    40
  struct DfsDefaultTraits
alpar@100
    41
  {
kpeter@244
    42
    ///The type of the digraph the algorithm runs on.
alpar@100
    43
    typedef GR Digraph;
kpeter@244
    44
kpeter@244
    45
    ///\brief The type of the map that stores the predecessor
alpar@100
    46
    ///arcs of the %DFS paths.
alpar@209
    47
    ///
kpeter@244
    48
    ///The type of the map that stores the predecessor
alpar@100
    49
    ///arcs of the %DFS paths.
kpeter@716
    50
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@244
    51
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@492
    52
    ///Instantiates a \c PredMap.
alpar@209
    53
kpeter@492
    54
    ///This function instantiates a \ref PredMap.
kpeter@244
    55
    ///\param g is the digraph, to which we would like to define the
kpeter@492
    56
    ///\ref PredMap.
kpeter@244
    57
    static PredMap *createPredMap(const Digraph &g)
alpar@100
    58
    {
kpeter@244
    59
      return new PredMap(g);
alpar@100
    60
    }
alpar@100
    61
alpar@100
    62
    ///The type of the map that indicates which nodes are processed.
alpar@209
    63
alpar@100
    64
    ///The type of the map that indicates which nodes are processed.
kpeter@716
    65
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@786
    66
    ///By default, it is a NullMap.
alpar@100
    67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@492
    68
    ///Instantiates a \c ProcessedMap.
alpar@209
    69
kpeter@492
    70
    ///This function instantiates a \ref ProcessedMap.
alpar@100
    71
    ///\param g is the digraph, to which
kpeter@492
    72
    ///we would like to define the \ref ProcessedMap.
alpar@100
    73
#ifdef DOXYGEN
kpeter@244
    74
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
    75
#else
kpeter@244
    76
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
    77
#endif
alpar@100
    78
    {
alpar@100
    79
      return new ProcessedMap();
alpar@100
    80
    }
kpeter@244
    81
alpar@100
    82
    ///The type of the map that indicates which nodes are reached.
alpar@209
    83
alpar@100
    84
    ///The type of the map that indicates which nodes are reached.
kpeter@716
    85
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
    86
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
kpeter@492
    87
    ///Instantiates a \c ReachedMap.
alpar@209
    88
kpeter@492
    89
    ///This function instantiates a \ref ReachedMap.
kpeter@244
    90
    ///\param g is the digraph, to which
kpeter@492
    91
    ///we would like to define the \ref ReachedMap.
kpeter@244
    92
    static ReachedMap *createReachedMap(const Digraph &g)
alpar@100
    93
    {
kpeter@244
    94
      return new ReachedMap(g);
alpar@100
    95
    }
alpar@209
    96
kpeter@244
    97
    ///The type of the map that stores the distances of the nodes.
kpeter@244
    98
kpeter@244
    99
    ///The type of the map that stores the distances of the nodes.
kpeter@716
   100
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   101
    typedef typename Digraph::template NodeMap<int> DistMap;
kpeter@492
   102
    ///Instantiates a \c DistMap.
alpar@209
   103
kpeter@492
   104
    ///This function instantiates a \ref DistMap.
kpeter@244
   105
    ///\param g is the digraph, to which we would like to define the
kpeter@492
   106
    ///\ref DistMap.
kpeter@244
   107
    static DistMap *createDistMap(const Digraph &g)
alpar@100
   108
    {
kpeter@244
   109
      return new DistMap(g);
alpar@100
   110
    }
alpar@100
   111
  };
alpar@209
   112
alpar@100
   113
  ///%DFS algorithm class.
alpar@209
   114
alpar@100
   115
  ///\ingroup search
alpar@100
   116
  ///This class provides an efficient implementation of the %DFS algorithm.
alpar@100
   117
  ///
kpeter@278
   118
  ///There is also a \ref dfs() "function-type interface" for the DFS
kpeter@244
   119
  ///algorithm, which is convenient in the simplier cases and it can be
kpeter@244
   120
  ///used easier.
kpeter@244
   121
  ///
kpeter@244
   122
  ///\tparam GR The type of the digraph the algorithm runs on.
kpeter@405
   123
  ///The default type is \ref ListDigraph.
kpeter@825
   124
  ///\tparam TR The traits class that defines various types used by the
kpeter@825
   125
  ///algorithm. By default, it is \ref DfsDefaultTraits
kpeter@825
   126
  ///"DfsDefaultTraits<GR>".
kpeter@825
   127
  ///In most cases, this parameter should not be set directly,
kpeter@825
   128
  ///consider to use the named template parameters instead.
alpar@100
   129
#ifdef DOXYGEN
alpar@100
   130
  template <typename GR,
alpar@209
   131
            typename TR>
alpar@100
   132
#else
alpar@100
   133
  template <typename GR=ListDigraph,
alpar@209
   134
            typename TR=DfsDefaultTraits<GR> >
alpar@100
   135
#endif
alpar@100
   136
  class Dfs {
alpar@100
   137
  public:
alpar@100
   138
kpeter@244
   139
    ///The type of the digraph the algorithm runs on.
kpeter@244
   140
    typedef typename TR::Digraph Digraph;
kpeter@244
   141
kpeter@244
   142
    ///\brief The type of the map that stores the predecessor arcs of the
kpeter@244
   143
    ///DFS paths.
kpeter@244
   144
    typedef typename TR::PredMap PredMap;
kpeter@244
   145
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   146
    typedef typename TR::DistMap DistMap;
kpeter@244
   147
    ///The type of the map that indicates which nodes are reached.
kpeter@244
   148
    typedef typename TR::ReachedMap ReachedMap;
kpeter@244
   149
    ///The type of the map that indicates which nodes are processed.
kpeter@244
   150
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@244
   151
    ///The type of the paths.
kpeter@244
   152
    typedef PredMapPath<Digraph, PredMap> Path;
kpeter@244
   153
kpeter@405
   154
    ///The \ref DfsDefaultTraits "traits class" of the algorithm.
alpar@100
   155
    typedef TR Traits;
kpeter@244
   156
kpeter@244
   157
  private:
kpeter@244
   158
alpar@100
   159
    typedef typename Digraph::Node Node;
alpar@100
   160
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   161
    typedef typename Digraph::Arc Arc;
alpar@100
   162
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@209
   163
kpeter@244
   164
    //Pointer to the underlying digraph.
alpar@100
   165
    const Digraph *G;
kpeter@244
   166
    //Pointer to the map of predecessor arcs.
alpar@100
   167
    PredMap *_pred;
kpeter@244
   168
    //Indicates if _pred is locally allocated (true) or not.
alpar@100
   169
    bool local_pred;
kpeter@244
   170
    //Pointer to the map of distances.
alpar@100
   171
    DistMap *_dist;
kpeter@244
   172
    //Indicates if _dist is locally allocated (true) or not.
alpar@100
   173
    bool local_dist;
kpeter@244
   174
    //Pointer to the map of reached status of the nodes.
alpar@100
   175
    ReachedMap *_reached;
kpeter@244
   176
    //Indicates if _reached is locally allocated (true) or not.
alpar@100
   177
    bool local_reached;
kpeter@244
   178
    //Pointer to the map of processed status of the nodes.
alpar@100
   179
    ProcessedMap *_processed;
kpeter@244
   180
    //Indicates if _processed is locally allocated (true) or not.
alpar@100
   181
    bool local_processed;
alpar@100
   182
alpar@100
   183
    std::vector<typename Digraph::OutArcIt> _stack;
alpar@100
   184
    int _stack_head;
alpar@100
   185
alpar@280
   186
    //Creates the maps if necessary.
alpar@209
   187
    void create_maps()
alpar@100
   188
    {
alpar@100
   189
      if(!_pred) {
alpar@209
   190
        local_pred = true;
alpar@209
   191
        _pred = Traits::createPredMap(*G);
alpar@100
   192
      }
alpar@100
   193
      if(!_dist) {
alpar@209
   194
        local_dist = true;
alpar@209
   195
        _dist = Traits::createDistMap(*G);
alpar@100
   196
      }
alpar@100
   197
      if(!_reached) {
alpar@209
   198
        local_reached = true;
alpar@209
   199
        _reached = Traits::createReachedMap(*G);
alpar@100
   200
      }
alpar@100
   201
      if(!_processed) {
alpar@209
   202
        local_processed = true;
alpar@209
   203
        _processed = Traits::createProcessedMap(*G);
alpar@100
   204
      }
alpar@100
   205
    }
alpar@100
   206
alpar@100
   207
  protected:
alpar@100
   208
alpar@100
   209
    Dfs() {}
alpar@209
   210
alpar@100
   211
  public:
alpar@100
   212
alpar@100
   213
    typedef Dfs Create;
alpar@100
   214
kpeter@584
   215
    ///\name Named Template Parameters
alpar@100
   216
alpar@100
   217
    ///@{
alpar@100
   218
alpar@100
   219
    template <class T>
kpeter@257
   220
    struct SetPredMapTraits : public Traits {
alpar@100
   221
      typedef T PredMap;
kpeter@244
   222
      static PredMap *createPredMap(const Digraph &)
alpar@100
   223
      {
deba@290
   224
        LEMON_ASSERT(false, "PredMap is not initialized");
deba@290
   225
        return 0; // ignore warnings
alpar@100
   226
      }
alpar@100
   227
    };
alpar@100
   228
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   229
    ///\c PredMap type.
alpar@100
   230
    ///
kpeter@244
   231
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   232
    ///\c PredMap type.
kpeter@716
   233
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   234
    template <class T>
kpeter@257
   235
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
kpeter@257
   236
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
alpar@100
   237
    };
alpar@209
   238
alpar@100
   239
    template <class T>
kpeter@257
   240
    struct SetDistMapTraits : public Traits {
alpar@100
   241
      typedef T DistMap;
alpar@209
   242
      static DistMap *createDistMap(const Digraph &)
alpar@100
   243
      {
deba@290
   244
        LEMON_ASSERT(false, "DistMap is not initialized");
deba@290
   245
        return 0; // ignore warnings
alpar@100
   246
      }
alpar@100
   247
    };
alpar@100
   248
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   249
    ///\c DistMap type.
alpar@100
   250
    ///
kpeter@244
   251
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   252
    ///\c DistMap type.
kpeter@716
   253
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   254
    template <class T>
kpeter@257
   255
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
kpeter@257
   256
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
alpar@100
   257
    };
alpar@209
   258
alpar@100
   259
    template <class T>
kpeter@257
   260
    struct SetReachedMapTraits : public Traits {
alpar@100
   261
      typedef T ReachedMap;
alpar@209
   262
      static ReachedMap *createReachedMap(const Digraph &)
alpar@100
   263
      {
deba@290
   264
        LEMON_ASSERT(false, "ReachedMap is not initialized");
deba@290
   265
        return 0; // ignore warnings
alpar@100
   266
      }
alpar@100
   267
    };
alpar@100
   268
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   269
    ///\c ReachedMap type.
alpar@100
   270
    ///
kpeter@244
   271
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   272
    ///\c ReachedMap type.
kpeter@716
   273
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
   274
    template <class T>
kpeter@257
   275
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
kpeter@257
   276
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
alpar@100
   277
    };
alpar@100
   278
alpar@100
   279
    template <class T>
kpeter@257
   280
    struct SetProcessedMapTraits : public Traits {
alpar@100
   281
      typedef T ProcessedMap;
alpar@209
   282
      static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   283
      {
deba@290
   284
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
deba@290
   285
        return 0; // ignore warnings
alpar@100
   286
      }
alpar@100
   287
    };
alpar@100
   288
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   289
    ///\c ProcessedMap type.
alpar@100
   290
    ///
kpeter@244
   291
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   292
    ///\c ProcessedMap type.
kpeter@716
   293
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   294
    template <class T>
kpeter@257
   295
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
kpeter@257
   296
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
alpar@100
   297
    };
alpar@209
   298
kpeter@257
   299
    struct SetStandardProcessedMapTraits : public Traits {
alpar@100
   300
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
kpeter@244
   301
      static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   302
      {
kpeter@244
   303
        return new ProcessedMap(g);
alpar@100
   304
      }
alpar@100
   305
    };
kpeter@244
   306
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@492
   307
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
alpar@100
   308
    ///
kpeter@244
   309
    ///\ref named-templ-param "Named parameter" for setting
kpeter@492
   310
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
kpeter@244
   311
    ///If you don't set it explicitly, it will be automatically allocated.
kpeter@257
   312
    struct SetStandardProcessedMap :
kpeter@257
   313
      public Dfs< Digraph, SetStandardProcessedMapTraits > {
kpeter@257
   314
      typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
alpar@100
   315
    };
alpar@209
   316
alpar@100
   317
    ///@}
alpar@100
   318
alpar@209
   319
  public:
alpar@209
   320
alpar@100
   321
    ///Constructor.
alpar@209
   322
kpeter@244
   323
    ///Constructor.
kpeter@244
   324
    ///\param g The digraph the algorithm runs on.
kpeter@244
   325
    Dfs(const Digraph &g) :
kpeter@244
   326
      G(&g),
alpar@100
   327
      _pred(NULL), local_pred(false),
alpar@100
   328
      _dist(NULL), local_dist(false),
alpar@100
   329
      _reached(NULL), local_reached(false),
alpar@100
   330
      _processed(NULL), local_processed(false)
alpar@100
   331
    { }
alpar@209
   332
alpar@100
   333
    ///Destructor.
alpar@209
   334
    ~Dfs()
alpar@100
   335
    {
alpar@100
   336
      if(local_pred) delete _pred;
alpar@100
   337
      if(local_dist) delete _dist;
alpar@100
   338
      if(local_reached) delete _reached;
alpar@100
   339
      if(local_processed) delete _processed;
alpar@100
   340
    }
alpar@100
   341
kpeter@244
   342
    ///Sets the map that stores the predecessor arcs.
alpar@100
   343
kpeter@244
   344
    ///Sets the map that stores the predecessor arcs.
kpeter@405
   345
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   346
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   347
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   348
    ///of course.
alpar@100
   349
    ///\return <tt> (*this) </tt>
alpar@209
   350
    Dfs &predMap(PredMap &m)
alpar@100
   351
    {
alpar@100
   352
      if(local_pred) {
alpar@209
   353
        delete _pred;
alpar@209
   354
        local_pred=false;
alpar@100
   355
      }
alpar@100
   356
      _pred = &m;
alpar@100
   357
      return *this;
alpar@100
   358
    }
alpar@100
   359
kpeter@244
   360
    ///Sets the map that indicates which nodes are reached.
alpar@100
   361
kpeter@244
   362
    ///Sets the map that indicates which nodes are reached.
kpeter@405
   363
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   364
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   365
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   366
    ///of course.
kpeter@244
   367
    ///\return <tt> (*this) </tt>
kpeter@244
   368
    Dfs &reachedMap(ReachedMap &m)
kpeter@244
   369
    {
kpeter@244
   370
      if(local_reached) {
kpeter@244
   371
        delete _reached;
kpeter@244
   372
        local_reached=false;
kpeter@244
   373
      }
kpeter@244
   374
      _reached = &m;
kpeter@244
   375
      return *this;
kpeter@244
   376
    }
kpeter@244
   377
kpeter@244
   378
    ///Sets the map that indicates which nodes are processed.
kpeter@244
   379
kpeter@244
   380
    ///Sets the map that indicates which nodes are processed.
kpeter@405
   381
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   382
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   383
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   384
    ///of course.
kpeter@244
   385
    ///\return <tt> (*this) </tt>
kpeter@244
   386
    Dfs &processedMap(ProcessedMap &m)
kpeter@244
   387
    {
kpeter@244
   388
      if(local_processed) {
kpeter@244
   389
        delete _processed;
kpeter@244
   390
        local_processed=false;
kpeter@244
   391
      }
kpeter@244
   392
      _processed = &m;
kpeter@244
   393
      return *this;
kpeter@244
   394
    }
kpeter@244
   395
kpeter@244
   396
    ///Sets the map that stores the distances of the nodes.
kpeter@244
   397
kpeter@244
   398
    ///Sets the map that stores the distances of the nodes calculated by
kpeter@244
   399
    ///the algorithm.
kpeter@405
   400
    ///If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
   401
    ///or \ref init(), an instance will be allocated automatically.
kpeter@405
   402
    ///The destructor deallocates this automatically allocated map,
kpeter@405
   403
    ///of course.
alpar@100
   404
    ///\return <tt> (*this) </tt>
alpar@209
   405
    Dfs &distMap(DistMap &m)
alpar@100
   406
    {
alpar@100
   407
      if(local_dist) {
alpar@209
   408
        delete _dist;
alpar@209
   409
        local_dist=false;
alpar@100
   410
      }
alpar@100
   411
      _dist = &m;
alpar@100
   412
      return *this;
alpar@100
   413
    }
alpar@100
   414
kpeter@244
   415
  public:
alpar@100
   416
kpeter@405
   417
    ///\name Execution Control
kpeter@405
   418
    ///The simplest way to execute the DFS algorithm is to use one of the
kpeter@405
   419
    ///member functions called \ref run(Node) "run()".\n
kpeter@713
   420
    ///If you need better control on the execution, you have to call
kpeter@713
   421
    ///\ref init() first, then you can add a source node with \ref addSource()
kpeter@405
   422
    ///and perform the actual computation with \ref start().
kpeter@405
   423
    ///This procedure can be repeated if there are nodes that have not
kpeter@405
   424
    ///been reached.
alpar@100
   425
alpar@100
   426
    ///@{
alpar@100
   427
kpeter@405
   428
    ///\brief Initializes the internal data structures.
kpeter@405
   429
    ///
alpar@100
   430
    ///Initializes the internal data structures.
alpar@100
   431
    void init()
alpar@100
   432
    {
alpar@100
   433
      create_maps();
alpar@100
   434
      _stack.resize(countNodes(*G));
alpar@100
   435
      _stack_head=-1;
alpar@100
   436
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@209
   437
        _pred->set(u,INVALID);
alpar@209
   438
        _reached->set(u,false);
alpar@209
   439
        _processed->set(u,false);
alpar@100
   440
      }
alpar@100
   441
    }
alpar@209
   442
alpar@100
   443
    ///Adds a new source node.
alpar@100
   444
alpar@100
   445
    ///Adds a new source node to the set of nodes to be processed.
alpar@100
   446
    ///
kpeter@405
   447
    ///\pre The stack must be empty. Otherwise the algorithm gives
kpeter@405
   448
    ///wrong results. (One of the outgoing arcs of all the source nodes
kpeter@405
   449
    ///except for the last one will not be visited and distances will
kpeter@405
   450
    ///also be wrong.)
alpar@100
   451
    void addSource(Node s)
alpar@100
   452
    {
kpeter@244
   453
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
alpar@100
   454
      if(!(*_reached)[s])
alpar@209
   455
        {
alpar@209
   456
          _reached->set(s,true);
alpar@209
   457
          _pred->set(s,INVALID);
alpar@209
   458
          OutArcIt e(*G,s);
alpar@209
   459
          if(e!=INVALID) {
alpar@209
   460
            _stack[++_stack_head]=e;
alpar@209
   461
            _dist->set(s,_stack_head);
alpar@209
   462
          }
alpar@209
   463
          else {
alpar@209
   464
            _processed->set(s,true);
alpar@209
   465
            _dist->set(s,0);
alpar@209
   466
          }
alpar@209
   467
        }
alpar@100
   468
    }
alpar@209
   469
alpar@100
   470
    ///Processes the next arc.
alpar@100
   471
alpar@100
   472
    ///Processes the next arc.
alpar@100
   473
    ///
alpar@100
   474
    ///\return The processed arc.
alpar@100
   475
    ///
kpeter@244
   476
    ///\pre The stack must not be empty.
alpar@100
   477
    Arc processNextArc()
alpar@209
   478
    {
alpar@100
   479
      Node m;
alpar@100
   480
      Arc e=_stack[_stack_head];
alpar@100
   481
      if(!(*_reached)[m=G->target(e)]) {
alpar@209
   482
        _pred->set(m,e);
alpar@209
   483
        _reached->set(m,true);
alpar@209
   484
        ++_stack_head;
alpar@209
   485
        _stack[_stack_head] = OutArcIt(*G, m);
alpar@209
   486
        _dist->set(m,_stack_head);
alpar@100
   487
      }
alpar@100
   488
      else {
alpar@209
   489
        m=G->source(e);
alpar@209
   490
        ++_stack[_stack_head];
alpar@100
   491
      }
alpar@100
   492
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
alpar@209
   493
        _processed->set(m,true);
alpar@209
   494
        --_stack_head;
alpar@209
   495
        if(_stack_head>=0) {
alpar@209
   496
          m=G->source(_stack[_stack_head]);
alpar@209
   497
          ++_stack[_stack_head];
alpar@209
   498
        }
alpar@100
   499
      }
alpar@100
   500
      return e;
alpar@100
   501
    }
kpeter@244
   502
alpar@100
   503
    ///Next arc to be processed.
alpar@100
   504
alpar@100
   505
    ///Next arc to be processed.
alpar@100
   506
    ///
kpeter@244
   507
    ///\return The next arc to be processed or \c INVALID if the stack
kpeter@244
   508
    ///is empty.
kpeter@244
   509
    OutArcIt nextArc() const
alpar@209
   510
    {
alpar@100
   511
      return _stack_head>=0?_stack[_stack_head]:INVALID;
alpar@100
   512
    }
alpar@100
   513
kpeter@405
   514
    ///Returns \c false if there are nodes to be processed.
kpeter@405
   515
kpeter@405
   516
    ///Returns \c false if there are nodes to be processed
kpeter@405
   517
    ///in the queue (stack).
kpeter@244
   518
    bool emptyQueue() const { return _stack_head<0; }
kpeter@244
   519
alpar@100
   520
    ///Returns the number of the nodes to be processed.
alpar@209
   521
kpeter@405
   522
    ///Returns the number of the nodes to be processed
kpeter@405
   523
    ///in the queue (stack).
kpeter@244
   524
    int queueSize() const { return _stack_head+1; }
alpar@209
   525
alpar@100
   526
    ///Executes the algorithm.
alpar@100
   527
alpar@100
   528
    ///Executes the algorithm.
alpar@100
   529
    ///
kpeter@244
   530
    ///This method runs the %DFS algorithm from the root node
kpeter@244
   531
    ///in order to compute the DFS path to each node.
alpar@100
   532
    ///
kpeter@244
   533
    /// The algorithm computes
kpeter@244
   534
    ///- the %DFS tree,
kpeter@244
   535
    ///- the distance of each node from the root in the %DFS tree.
alpar@100
   536
    ///
kpeter@244
   537
    ///\pre init() must be called and a root node should be
kpeter@244
   538
    ///added with addSource() before using this function.
kpeter@244
   539
    ///
kpeter@244
   540
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
kpeter@244
   541
    ///\code
kpeter@244
   542
    ///  while ( !d.emptyQueue() ) {
kpeter@244
   543
    ///    d.processNextArc();
kpeter@244
   544
    ///  }
kpeter@244
   545
    ///\endcode
alpar@100
   546
    void start()
alpar@100
   547
    {
alpar@100
   548
      while ( !emptyQueue() ) processNextArc();
alpar@100
   549
    }
alpar@209
   550
kpeter@244
   551
    ///Executes the algorithm until the given target node is reached.
alpar@100
   552
kpeter@244
   553
    ///Executes the algorithm until the given target node is reached.
alpar@100
   554
    ///
kpeter@244
   555
    ///This method runs the %DFS algorithm from the root node
kpeter@286
   556
    ///in order to compute the DFS path to \c t.
alpar@100
   557
    ///
kpeter@244
   558
    ///The algorithm computes
kpeter@286
   559
    ///- the %DFS path to \c t,
kpeter@286
   560
    ///- the distance of \c t from the root in the %DFS tree.
alpar@100
   561
    ///
kpeter@244
   562
    ///\pre init() must be called and a root node should be
kpeter@244
   563
    ///added with addSource() before using this function.
kpeter@286
   564
    void start(Node t)
alpar@100
   565
    {
kpeter@286
   566
      while ( !emptyQueue() && G->target(_stack[_stack_head])!=t )
alpar@209
   567
        processNextArc();
alpar@100
   568
    }
alpar@209
   569
alpar@100
   570
    ///Executes the algorithm until a condition is met.
alpar@100
   571
alpar@100
   572
    ///Executes the algorithm until a condition is met.
alpar@100
   573
    ///
kpeter@244
   574
    ///This method runs the %DFS algorithm from the root node
kpeter@244
   575
    ///until an arc \c a with <tt>am[a]</tt> true is found.
alpar@100
   576
    ///
kpeter@244
   577
    ///\param am A \c bool (or convertible) arc map. The algorithm
kpeter@244
   578
    ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
alpar@100
   579
    ///
kpeter@244
   580
    ///\return The reached arc \c a with <tt>am[a]</tt> true or
alpar@100
   581
    ///\c INVALID if no such arc was found.
alpar@100
   582
    ///
kpeter@244
   583
    ///\pre init() must be called and a root node should be
kpeter@244
   584
    ///added with addSource() before using this function.
kpeter@244
   585
    ///
kpeter@244
   586
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
alpar@100
   587
    ///not a node map.
kpeter@244
   588
    template<class ArcBoolMap>
kpeter@244
   589
    Arc start(const ArcBoolMap &am)
alpar@100
   590
    {
kpeter@244
   591
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
alpar@100
   592
        processNextArc();
alpar@100
   593
      return emptyQueue() ? INVALID : _stack[_stack_head];
alpar@100
   594
    }
alpar@100
   595
kpeter@286
   596
    ///Runs the algorithm from the given source node.
alpar@209
   597
kpeter@244
   598
    ///This method runs the %DFS algorithm from node \c s
kpeter@244
   599
    ///in order to compute the DFS path to each node.
alpar@100
   600
    ///
kpeter@244
   601
    ///The algorithm computes
kpeter@244
   602
    ///- the %DFS tree,
kpeter@244
   603
    ///- the distance of each node from the root in the %DFS tree.
kpeter@244
   604
    ///
kpeter@244
   605
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
alpar@100
   606
    ///\code
alpar@100
   607
    ///  d.init();
kpeter@244
   608
    ///  d.addSource(s);
kpeter@244
   609
    ///  d.start();
kpeter@244
   610
    ///\endcode
kpeter@244
   611
    void run(Node s) {
kpeter@244
   612
      init();
kpeter@244
   613
      addSource(s);
kpeter@244
   614
      start();
kpeter@244
   615
    }
kpeter@244
   616
kpeter@244
   617
    ///Finds the %DFS path between \c s and \c t.
kpeter@244
   618
kpeter@244
   619
    ///This method runs the %DFS algorithm from node \c s
kpeter@286
   620
    ///in order to compute the DFS path to node \c t
kpeter@286
   621
    ///(it stops searching when \c t is processed)
kpeter@244
   622
    ///
kpeter@286
   623
    ///\return \c true if \c t is reachable form \c s.
kpeter@244
   624
    ///
kpeter@244
   625
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
kpeter@244
   626
    ///just a shortcut of the following code.
kpeter@244
   627
    ///\code
kpeter@244
   628
    ///  d.init();
kpeter@244
   629
    ///  d.addSource(s);
kpeter@244
   630
    ///  d.start(t);
kpeter@244
   631
    ///\endcode
kpeter@286
   632
    bool run(Node s,Node t) {
kpeter@244
   633
      init();
kpeter@244
   634
      addSource(s);
kpeter@244
   635
      start(t);
kpeter@286
   636
      return reached(t);
kpeter@244
   637
    }
kpeter@244
   638
kpeter@244
   639
    ///Runs the algorithm to visit all nodes in the digraph.
kpeter@244
   640
kpeter@787
   641
    ///This method runs the %DFS algorithm in order to visit all nodes
kpeter@787
   642
    ///in the digraph.
kpeter@244
   643
    ///
kpeter@244
   644
    ///\note <tt>d.run()</tt> is just a shortcut of the following code.
kpeter@244
   645
    ///\code
kpeter@244
   646
    ///  d.init();
kpeter@244
   647
    ///  for (NodeIt n(digraph); n != INVALID; ++n) {
kpeter@244
   648
    ///    if (!d.reached(n)) {
kpeter@244
   649
    ///      d.addSource(n);
alpar@100
   650
    ///      d.start();
alpar@100
   651
    ///    }
alpar@100
   652
    ///  }
alpar@100
   653
    ///\endcode
alpar@100
   654
    void run() {
alpar@100
   655
      init();
alpar@100
   656
      for (NodeIt it(*G); it != INVALID; ++it) {
alpar@100
   657
        if (!reached(it)) {
alpar@100
   658
          addSource(it);
alpar@100
   659
          start();
alpar@100
   660
        }
alpar@100
   661
      }
alpar@100
   662
    }
alpar@100
   663
alpar@100
   664
    ///@}
alpar@100
   665
alpar@100
   666
    ///\name Query Functions
kpeter@405
   667
    ///The results of the DFS algorithm can be obtained using these
alpar@100
   668
    ///functions.\n
kpeter@405
   669
    ///Either \ref run(Node) "run()" or \ref start() should be called
kpeter@405
   670
    ///before using them.
alpar@209
   671
alpar@100
   672
    ///@{
alpar@100
   673
kpeter@716
   674
    ///The DFS path to the given node.
alpar@100
   675
kpeter@716
   676
    ///Returns the DFS path to the given node from the root(s).
kpeter@244
   677
    ///
kpeter@405
   678
    ///\warning \c t should be reached from the root(s).
kpeter@244
   679
    ///
kpeter@405
   680
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   681
    ///must be called before using this function.
kpeter@244
   682
    Path path(Node t) const { return Path(*G, *_pred, t); }
alpar@209
   683
kpeter@716
   684
    ///The distance of the given node from the root(s).
alpar@100
   685
kpeter@716
   686
    ///Returns the distance of the given node from the root(s).
kpeter@244
   687
    ///
kpeter@405
   688
    ///\warning If node \c v is not reached from the root(s), then
kpeter@244
   689
    ///the return value of this function is undefined.
kpeter@244
   690
    ///
kpeter@405
   691
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   692
    ///must be called before using this function.
alpar@100
   693
    int dist(Node v) const { return (*_dist)[v]; }
alpar@100
   694
kpeter@716
   695
    ///Returns the 'previous arc' of the %DFS tree for the given node.
alpar@100
   696
kpeter@244
   697
    ///This function returns the 'previous arc' of the %DFS tree for the
kpeter@405
   698
    ///node \c v, i.e. it returns the last arc of a %DFS path from a
kpeter@405
   699
    ///root to \c v. It is \c INVALID if \c v is not reached from the
kpeter@405
   700
    ///root(s) or if \c v is a root.
kpeter@244
   701
    ///
kpeter@244
   702
    ///The %DFS tree used here is equal to the %DFS tree used in
kpeter@716
   703
    ///\ref predNode() and \ref predMap().
kpeter@244
   704
    ///
kpeter@405
   705
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   706
    ///must be called before using this function.
alpar@100
   707
    Arc predArc(Node v) const { return (*_pred)[v];}
alpar@100
   708
kpeter@716
   709
    ///Returns the 'previous node' of the %DFS tree for the given node.
alpar@100
   710
kpeter@244
   711
    ///This function returns the 'previous node' of the %DFS
kpeter@244
   712
    ///tree for the node \c v, i.e. it returns the last but one node
kpeter@716
   713
    ///of a %DFS path from a root to \c v. It is \c INVALID
kpeter@405
   714
    ///if \c v is not reached from the root(s) or if \c v is a root.
kpeter@244
   715
    ///
kpeter@244
   716
    ///The %DFS tree used here is equal to the %DFS tree used in
kpeter@716
   717
    ///\ref predArc() and \ref predMap().
kpeter@244
   718
    ///
kpeter@405
   719
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@405
   720
    ///must be called before using this function.
alpar@100
   721
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@209
   722
                                  G->source((*_pred)[v]); }
alpar@209
   723
kpeter@244
   724
    ///\brief Returns a const reference to the node map that stores the
kpeter@244
   725
    ///distances of the nodes.
kpeter@244
   726
    ///
kpeter@244
   727
    ///Returns a const reference to the node map that stores the
kpeter@244
   728
    ///distances of the nodes calculated by the algorithm.
kpeter@244
   729
    ///
kpeter@405
   730
    ///\pre Either \ref run(Node) "run()" or \ref init()
kpeter@244
   731
    ///must be called before using this function.
alpar@100
   732
    const DistMap &distMap() const { return *_dist;}
alpar@209
   733
kpeter@244
   734
    ///\brief Returns a const reference to the node map that stores the
kpeter@244
   735
    ///predecessor arcs.
kpeter@244
   736
    ///
kpeter@244
   737
    ///Returns a const reference to the node map that stores the predecessor
kpeter@716
   738
    ///arcs, which form the DFS tree (forest).
kpeter@244
   739
    ///
kpeter@405
   740
    ///\pre Either \ref run(Node) "run()" or \ref init()
alpar@100
   741
    ///must be called before using this function.
alpar@100
   742
    const PredMap &predMap() const { return *_pred;}
alpar@209
   743
kpeter@716
   744
    ///Checks if the given node. node is reached from the root(s).
alpar@100
   745
kpeter@405
   746
    ///Returns \c true if \c v is reached from the root(s).
kpeter@405
   747
    ///
kpeter@405
   748
    ///\pre Either \ref run(Node) "run()" or \ref init()
alpar@100
   749
    ///must be called before using this function.
kpeter@244
   750
    bool reached(Node v) const { return (*_reached)[v]; }
alpar@209
   751
alpar@100
   752
    ///@}
alpar@100
   753
  };
alpar@100
   754
kpeter@244
   755
  ///Default traits class of dfs() function.
alpar@100
   756
kpeter@244
   757
  ///Default traits class of dfs() function.
kpeter@157
   758
  ///\tparam GR Digraph type.
alpar@100
   759
  template<class GR>
alpar@100
   760
  struct DfsWizardDefaultTraits
alpar@100
   761
  {
kpeter@244
   762
    ///The type of the digraph the algorithm runs on.
alpar@100
   763
    typedef GR Digraph;
kpeter@244
   764
kpeter@244
   765
    ///\brief The type of the map that stores the predecessor
alpar@100
   766
    ///arcs of the %DFS paths.
alpar@209
   767
    ///
kpeter@244
   768
    ///The type of the map that stores the predecessor
alpar@100
   769
    ///arcs of the %DFS paths.
kpeter@716
   770
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   771
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
kpeter@301
   772
    ///Instantiates a PredMap.
alpar@209
   773
kpeter@301
   774
    ///This function instantiates a PredMap.
kpeter@244
   775
    ///\param g is the digraph, to which we would like to define the
kpeter@301
   776
    ///PredMap.
kpeter@244
   777
    static PredMap *createPredMap(const Digraph &g)
alpar@100
   778
    {
kpeter@278
   779
      return new PredMap(g);
alpar@100
   780
    }
alpar@100
   781
alpar@100
   782
    ///The type of the map that indicates which nodes are processed.
alpar@209
   783
alpar@100
   784
    ///The type of the map that indicates which nodes are processed.
kpeter@716
   785
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@786
   786
    ///By default, it is a NullMap.
alpar@100
   787
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
kpeter@301
   788
    ///Instantiates a ProcessedMap.
alpar@209
   789
kpeter@301
   790
    ///This function instantiates a ProcessedMap.
alpar@100
   791
    ///\param g is the digraph, to which
kpeter@301
   792
    ///we would like to define the ProcessedMap.
alpar@100
   793
#ifdef DOXYGEN
kpeter@244
   794
    static ProcessedMap *createProcessedMap(const Digraph &g)
alpar@100
   795
#else
kpeter@244
   796
    static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   797
#endif
alpar@100
   798
    {
alpar@100
   799
      return new ProcessedMap();
alpar@100
   800
    }
kpeter@244
   801
alpar@100
   802
    ///The type of the map that indicates which nodes are reached.
alpar@209
   803
alpar@100
   804
    ///The type of the map that indicates which nodes are reached.
kpeter@716
   805
    ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
   806
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
kpeter@301
   807
    ///Instantiates a ReachedMap.
alpar@209
   808
kpeter@301
   809
    ///This function instantiates a ReachedMap.
kpeter@244
   810
    ///\param g is the digraph, to which
kpeter@301
   811
    ///we would like to define the ReachedMap.
kpeter@244
   812
    static ReachedMap *createReachedMap(const Digraph &g)
alpar@100
   813
    {
kpeter@244
   814
      return new ReachedMap(g);
alpar@100
   815
    }
alpar@209
   816
kpeter@244
   817
    ///The type of the map that stores the distances of the nodes.
kpeter@244
   818
kpeter@244
   819
    ///The type of the map that stores the distances of the nodes.
kpeter@716
   820
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
kpeter@278
   821
    typedef typename Digraph::template NodeMap<int> DistMap;
kpeter@301
   822
    ///Instantiates a DistMap.
alpar@209
   823
kpeter@301
   824
    ///This function instantiates a DistMap.
alpar@210
   825
    ///\param g is the digraph, to which we would like to define
kpeter@301
   826
    ///the DistMap
kpeter@244
   827
    static DistMap *createDistMap(const Digraph &g)
alpar@100
   828
    {
kpeter@278
   829
      return new DistMap(g);
alpar@100
   830
    }
kpeter@278
   831
kpeter@278
   832
    ///The type of the DFS paths.
kpeter@278
   833
kpeter@278
   834
    ///The type of the DFS paths.
kpeter@716
   835
    ///It must conform to the \ref concepts::Path "Path" concept.
kpeter@278
   836
    typedef lemon::Path<Digraph> Path;
alpar@100
   837
  };
alpar@209
   838
kpeter@313
   839
  /// Default traits class used by DfsWizard
alpar@100
   840
kpeter@716
   841
  /// Default traits class used by DfsWizard.
kpeter@716
   842
  /// \tparam GR The type of the digraph.
alpar@100
   843
  template<class GR>
alpar@100
   844
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
alpar@100
   845
  {
alpar@100
   846
alpar@100
   847
    typedef DfsWizardDefaultTraits<GR> Base;
alpar@100
   848
  protected:
kpeter@244
   849
    //The type of the nodes in the digraph.
alpar@100
   850
    typedef typename Base::Digraph::Node Node;
alpar@100
   851
kpeter@244
   852
    //Pointer to the digraph the algorithm runs on.
alpar@100
   853
    void *_g;
kpeter@244
   854
    //Pointer to the map of reached nodes.
alpar@100
   855
    void *_reached;
kpeter@244
   856
    //Pointer to the map of processed nodes.
alpar@100
   857
    void *_processed;
kpeter@244
   858
    //Pointer to the map of predecessors arcs.
alpar@100
   859
    void *_pred;
kpeter@244
   860
    //Pointer to the map of distances.
alpar@100
   861
    void *_dist;
kpeter@278
   862
    //Pointer to the DFS path to the target node.
kpeter@278
   863
    void *_path;
kpeter@278
   864
    //Pointer to the distance of the target node.
kpeter@278
   865
    int *_di;
alpar@209
   866
alpar@100
   867
    public:
alpar@100
   868
    /// Constructor.
alpar@209
   869
kpeter@716
   870
    /// This constructor does not require parameters, it initiates
kpeter@278
   871
    /// all of the attributes to \c 0.
alpar@100
   872
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
kpeter@278
   873
                      _dist(0), _path(0), _di(0) {}
alpar@100
   874
alpar@100
   875
    /// Constructor.
alpar@209
   876
kpeter@278
   877
    /// This constructor requires one parameter,
kpeter@278
   878
    /// others are initiated to \c 0.
kpeter@244
   879
    /// \param g The digraph the algorithm runs on.
kpeter@278
   880
    DfsWizardBase(const GR &g) :
alpar@209
   881
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
kpeter@278
   882
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
alpar@100
   883
alpar@100
   884
  };
alpar@209
   885
kpeter@278
   886
  /// Auxiliary class for the function-type interface of DFS algorithm.
alpar@100
   887
kpeter@278
   888
  /// This auxiliary class is created to implement the
kpeter@278
   889
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
kpeter@405
   890
  /// It does not have own \ref run(Node) "run()" method, it uses the
kpeter@405
   891
  /// functions and features of the plain \ref Dfs.
alpar@100
   892
  ///
kpeter@278
   893
  /// This class should only be used through the \ref dfs() function,
kpeter@278
   894
  /// which makes it easier to use the algorithm.
kpeter@825
   895
  ///
kpeter@825
   896
  /// \tparam TR The traits class that defines various types used by the
kpeter@825
   897
  /// algorithm.
alpar@100
   898
  template<class TR>
alpar@100
   899
  class DfsWizard : public TR
alpar@100
   900
  {
alpar@100
   901
    typedef TR Base;
alpar@100
   902
alpar@100
   903
    typedef typename TR::Digraph Digraph;
kpeter@244
   904
alpar@100
   905
    typedef typename Digraph::Node Node;
alpar@100
   906
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   907
    typedef typename Digraph::Arc Arc;
alpar@100
   908
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@209
   909
kpeter@244
   910
    typedef typename TR::PredMap PredMap;
kpeter@244
   911
    typedef typename TR::DistMap DistMap;
alpar@100
   912
    typedef typename TR::ReachedMap ReachedMap;
alpar@100
   913
    typedef typename TR::ProcessedMap ProcessedMap;
kpeter@278
   914
    typedef typename TR::Path Path;
alpar@100
   915
alpar@100
   916
  public:
kpeter@244
   917
alpar@100
   918
    /// Constructor.
alpar@100
   919
    DfsWizard() : TR() {}
alpar@100
   920
alpar@100
   921
    /// Constructor that requires parameters.
alpar@100
   922
alpar@100
   923
    /// Constructor that requires parameters.
alpar@100
   924
    /// These parameters will be the default values for the traits class.
kpeter@278
   925
    /// \param g The digraph the algorithm runs on.
kpeter@278
   926
    DfsWizard(const Digraph &g) :
kpeter@278
   927
      TR(g) {}
alpar@100
   928
alpar@100
   929
    ///Copy constructor
alpar@100
   930
    DfsWizard(const TR &b) : TR(b) {}
alpar@100
   931
alpar@100
   932
    ~DfsWizard() {}
alpar@100
   933
kpeter@278
   934
    ///Runs DFS algorithm from the given source node.
alpar@209
   935
kpeter@278
   936
    ///This method runs DFS algorithm from node \c s
kpeter@278
   937
    ///in order to compute the DFS path to each node.
kpeter@278
   938
    void run(Node s)
kpeter@278
   939
    {
kpeter@278
   940
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
kpeter@278
   941
      if (Base::_pred)
kpeter@278
   942
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
   943
      if (Base::_dist)
kpeter@278
   944
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
   945
      if (Base::_reached)
kpeter@278
   946
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
kpeter@278
   947
      if (Base::_processed)
kpeter@278
   948
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
   949
      if (s!=INVALID)
kpeter@278
   950
        alg.run(s);
kpeter@278
   951
      else
kpeter@278
   952
        alg.run();
kpeter@278
   953
    }
kpeter@278
   954
kpeter@278
   955
    ///Finds the DFS path between \c s and \c t.
kpeter@278
   956
kpeter@278
   957
    ///This method runs DFS algorithm from node \c s
kpeter@278
   958
    ///in order to compute the DFS path to node \c t
kpeter@278
   959
    ///(it stops searching when \c t is processed).
kpeter@278
   960
    ///
kpeter@278
   961
    ///\return \c true if \c t is reachable form \c s.
kpeter@278
   962
    bool run(Node s, Node t)
kpeter@278
   963
    {
kpeter@278
   964
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
kpeter@278
   965
      if (Base::_pred)
kpeter@278
   966
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
kpeter@278
   967
      if (Base::_dist)
kpeter@278
   968
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
kpeter@278
   969
      if (Base::_reached)
kpeter@278
   970
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
kpeter@278
   971
      if (Base::_processed)
kpeter@278
   972
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
kpeter@278
   973
      alg.run(s,t);
kpeter@278
   974
      if (Base::_path)
kpeter@278
   975
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
kpeter@278
   976
      if (Base::_di)
kpeter@278
   977
        *Base::_di = alg.dist(t);
kpeter@278
   978
      return alg.reached(t);
kpeter@278
   979
      }
kpeter@278
   980
kpeter@278
   981
    ///Runs DFS algorithm to visit all nodes in the digraph.
kpeter@278
   982
kpeter@787
   983
    ///This method runs DFS algorithm in order to visit all nodes
kpeter@787
   984
    ///in the digraph.
alpar@100
   985
    void run()
alpar@100
   986
    {
kpeter@278
   987
      run(INVALID);
kpeter@244
   988
    }
kpeter@244
   989
alpar@100
   990
    template<class T>
kpeter@257
   991
    struct SetPredMapBase : public Base {
alpar@100
   992
      typedef T PredMap;
alpar@100
   993
      static PredMap *createPredMap(const Digraph &) { return 0; };
kpeter@257
   994
      SetPredMapBase(const TR &b) : TR(b) {}
alpar@100
   995
    };
kpeter@716
   996
kpeter@716
   997
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@716
   998
    ///the predecessor map.
alpar@100
   999
    ///
kpeter@716
  1000
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
  1001
    ///the map that stores the predecessor arcs of the nodes.
alpar@100
  1002
    template<class T>
kpeter@257
  1003
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
alpar@100
  1004
    {
alpar@100
  1005
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1006
      return DfsWizard<SetPredMapBase<T> >(*this);
alpar@100
  1007
    }
alpar@209
  1008
alpar@100
  1009
    template<class T>
kpeter@257
  1010
    struct SetReachedMapBase : public Base {
alpar@100
  1011
      typedef T ReachedMap;
alpar@100
  1012
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
kpeter@257
  1013
      SetReachedMapBase(const TR &b) : TR(b) {}
alpar@100
  1014
    };
kpeter@716
  1015
kpeter@716
  1016
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@716
  1017
    ///the reached map.
alpar@100
  1018
    ///
kpeter@716
  1019
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
  1020
    ///the map that indicates which nodes are reached.
alpar@100
  1021
    template<class T>
kpeter@257
  1022
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
alpar@100
  1023
    {
deba@158
  1024
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1025
      return DfsWizard<SetReachedMapBase<T> >(*this);
alpar@100
  1026
    }
alpar@209
  1027
alpar@100
  1028
    template<class T>
kpeter@278
  1029
    struct SetDistMapBase : public Base {
kpeter@278
  1030
      typedef T DistMap;
kpeter@278
  1031
      static DistMap *createDistMap(const Digraph &) { return 0; };
kpeter@278
  1032
      SetDistMapBase(const TR &b) : TR(b) {}
kpeter@278
  1033
    };
kpeter@716
  1034
kpeter@716
  1035
    ///\brief \ref named-templ-param "Named parameter" for setting
kpeter@716
  1036
    ///the distance map.
kpeter@278
  1037
    ///
kpeter@716
  1038
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
  1039
    ///the map that stores the distances of the nodes calculated
kpeter@716
  1040
    ///by the algorithm.
kpeter@278
  1041
    template<class T>
kpeter@278
  1042
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
kpeter@278
  1043
    {
kpeter@278
  1044
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1045
      return DfsWizard<SetDistMapBase<T> >(*this);
kpeter@278
  1046
    }
kpeter@278
  1047
kpeter@278
  1048
    template<class T>
kpeter@257
  1049
    struct SetProcessedMapBase : public Base {
alpar@100
  1050
      typedef T ProcessedMap;
alpar@100
  1051
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
kpeter@257
  1052
      SetProcessedMapBase(const TR &b) : TR(b) {}
alpar@100
  1053
    };
kpeter@716
  1054
kpeter@716
  1055
    ///\brief \ref named-func-param "Named parameter" for setting
kpeter@716
  1056
    ///the processed map.
alpar@100
  1057
    ///
kpeter@716
  1058
    ///\ref named-templ-param "Named parameter" function for setting
kpeter@716
  1059
    ///the map that indicates which nodes are processed.
alpar@100
  1060
    template<class T>
kpeter@257
  1061
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
alpar@100
  1062
    {
deba@158
  1063
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@257
  1064
      return DfsWizard<SetProcessedMapBase<T> >(*this);
alpar@100
  1065
    }
alpar@209
  1066
alpar@100
  1067
    template<class T>
kpeter@278
  1068
    struct SetPathBase : public Base {
kpeter@278
  1069
      typedef T Path;
kpeter@278
  1070
      SetPathBase(const TR &b) : TR(b) {}
alpar@100
  1071
    };
kpeter@278
  1072
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1073
    ///for getting the DFS path to the target node.
alpar@100
  1074
    ///
kpeter@278
  1075
    ///\ref named-func-param "Named parameter"
kpeter@278
  1076
    ///for getting the DFS path to the target node.
alpar@100
  1077
    template<class T>
kpeter@278
  1078
    DfsWizard<SetPathBase<T> > path(const T &t)
alpar@100
  1079
    {
kpeter@278
  1080
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
kpeter@278
  1081
      return DfsWizard<SetPathBase<T> >(*this);
kpeter@278
  1082
    }
kpeter@278
  1083
kpeter@278
  1084
    ///\brief \ref named-func-param "Named parameter"
kpeter@278
  1085
    ///for getting the distance of the target node.
kpeter@278
  1086
    ///
kpeter@278
  1087
    ///\ref named-func-param "Named parameter"
kpeter@278
  1088
    ///for getting the distance of the target node.
kpeter@278
  1089
    DfsWizard dist(const int &d)
kpeter@278
  1090
    {
kpeter@278
  1091
      Base::_di=const_cast<int*>(&d);
kpeter@278
  1092
      return *this;
alpar@100
  1093
    }
alpar@209
  1094
alpar@100
  1095
  };
alpar@209
  1096
kpeter@278
  1097
  ///Function-type interface for DFS algorithm.
alpar@100
  1098
alpar@100
  1099
  ///\ingroup search
kpeter@278
  1100
  ///Function-type interface for DFS algorithm.
alpar@100
  1101
  ///
kpeter@278
  1102
  ///This function also has several \ref named-func-param "named parameters",
alpar@100
  1103
  ///they are declared as the members of class \ref DfsWizard.
kpeter@278
  1104
  ///The following examples show how to use these parameters.
alpar@100
  1105
  ///\code
kpeter@278
  1106
  ///  // Compute the DFS tree
kpeter@278
  1107
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
kpeter@278
  1108
  ///
kpeter@278
  1109
  ///  // Compute the DFS path from s to t
kpeter@278
  1110
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
alpar@100
  1111
  ///\endcode
kpeter@405
  1112
  ///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()"
alpar@100
  1113
  ///to the end of the parameter list.
alpar@100
  1114
  ///\sa DfsWizard
alpar@100
  1115
  ///\sa Dfs
alpar@100
  1116
  template<class GR>
alpar@100
  1117
  DfsWizard<DfsWizardBase<GR> >
kpeter@278
  1118
  dfs(const GR &digraph)
alpar@100
  1119
  {
kpeter@278
  1120
    return DfsWizard<DfsWizardBase<GR> >(digraph);
alpar@100
  1121
  }
alpar@100
  1122
alpar@100
  1123
#ifdef DOXYGEN
kpeter@244
  1124
  /// \brief Visitor class for DFS.
alpar@209
  1125
  ///
kpeter@244
  1126
  /// This class defines the interface of the DfsVisit events, and
kpeter@244
  1127
  /// it could be the base of a real visitor class.
kpeter@492
  1128
  template <typename GR>
alpar@100
  1129
  struct DfsVisitor {
kpeter@492
  1130
    typedef GR Digraph;
alpar@100
  1131
    typedef typename Digraph::Arc Arc;
alpar@100
  1132
    typedef typename Digraph::Node Node;
kpeter@244
  1133
    /// \brief Called for the source node of the DFS.
alpar@209
  1134
    ///
kpeter@244
  1135
    /// This function is called for the source node of the DFS.
kpeter@244
  1136
    void start(const Node& node) {}
kpeter@244
  1137
    /// \brief Called when the source node is leaved.
kpeter@244
  1138
    ///
kpeter@244
  1139
    /// This function is called when the source node is leaved.
kpeter@244
  1140
    void stop(const Node& node) {}
kpeter@244
  1141
    /// \brief Called when a node is reached first time.
kpeter@244
  1142
    ///
kpeter@244
  1143
    /// This function is called when a node is reached first time.
kpeter@244
  1144
    void reach(const Node& node) {}
kpeter@244
  1145
    /// \brief Called when an arc reaches a new node.
kpeter@244
  1146
    ///
kpeter@244
  1147
    /// This function is called when the DFS finds an arc whose target node
kpeter@244
  1148
    /// is not reached yet.
alpar@100
  1149
    void discover(const Arc& arc) {}
kpeter@244
  1150
    /// \brief Called when an arc is examined but its target node is
alpar@100
  1151
    /// already discovered.
alpar@209
  1152
    ///
kpeter@244
  1153
    /// This function is called when an arc is examined but its target node is
alpar@100
  1154
    /// already discovered.
alpar@100
  1155
    void examine(const Arc& arc) {}
kpeter@244
  1156
    /// \brief Called when the DFS steps back from a node.
alpar@209
  1157
    ///
kpeter@244
  1158
    /// This function is called when the DFS steps back from a node.
kpeter@244
  1159
    void leave(const Node& node) {}
kpeter@244
  1160
    /// \brief Called when the DFS steps back on an arc.
alpar@209
  1161
    ///
kpeter@244
  1162
    /// This function is called when the DFS steps back on an arc.
kpeter@244
  1163
    void backtrack(const Arc& arc) {}
alpar@100
  1164
  };
alpar@100
  1165
#else
kpeter@492
  1166
  template <typename GR>
alpar@100
  1167
  struct DfsVisitor {
kpeter@492
  1168
    typedef GR Digraph;
alpar@100
  1169
    typedef typename Digraph::Arc Arc;
alpar@100
  1170
    typedef typename Digraph::Node Node;
alpar@100
  1171
    void start(const Node&) {}
alpar@100
  1172
    void stop(const Node&) {}
kpeter@244
  1173
    void reach(const Node&) {}
kpeter@244
  1174
    void discover(const Arc&) {}
kpeter@244
  1175
    void examine(const Arc&) {}
kpeter@244
  1176
    void leave(const Node&) {}
kpeter@244
  1177
    void backtrack(const Arc&) {}
alpar@100
  1178
alpar@100
  1179
    template <typename _Visitor>
alpar@100
  1180
    struct Constraints {
alpar@100
  1181
      void constraints() {
alpar@209
  1182
        Arc arc;
alpar@209
  1183
        Node node;
alpar@209
  1184
        visitor.start(node);
alpar@209
  1185
        visitor.stop(arc);
kpeter@244
  1186
        visitor.reach(node);
kpeter@244
  1187
        visitor.discover(arc);
kpeter@244
  1188
        visitor.examine(arc);
kpeter@244
  1189
        visitor.leave(node);
kpeter@244
  1190
        visitor.backtrack(arc);
alpar@100
  1191
      }
alpar@100
  1192
      _Visitor& visitor;
alpar@100
  1193
    };
alpar@100
  1194
  };
alpar@100
  1195
#endif
alpar@100
  1196
alpar@100
  1197
  /// \brief Default traits class of DfsVisit class.
alpar@100
  1198
  ///
alpar@100
  1199
  /// Default traits class of DfsVisit class.
kpeter@244
  1200
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
kpeter@492
  1201
  template<class GR>
alpar@100
  1202
  struct DfsVisitDefaultTraits {
alpar@100
  1203
kpeter@244
  1204
    /// \brief The type of the digraph the algorithm runs on.
kpeter@492
  1205
    typedef GR Digraph;
alpar@100
  1206
alpar@100
  1207
    /// \brief The type of the map that indicates which nodes are reached.
alpar@209
  1208
    ///
alpar@100
  1209
    /// The type of the map that indicates which nodes are reached.
kpeter@716
  1210
    /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
alpar@100
  1211
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
alpar@100
  1212
kpeter@301
  1213
    /// \brief Instantiates a ReachedMap.
alpar@100
  1214
    ///
kpeter@301
  1215
    /// This function instantiates a ReachedMap.
alpar@100
  1216
    /// \param digraph is the digraph, to which
kpeter@301
  1217
    /// we would like to define the ReachedMap.
alpar@100
  1218
    static ReachedMap *createReachedMap(const Digraph &digraph) {
alpar@100
  1219
      return new ReachedMap(digraph);
alpar@100
  1220
    }
alpar@100
  1221
alpar@100
  1222
  };
alpar@209
  1223
alpar@100
  1224
  /// \ingroup search
kpeter@244
  1225
  ///
kpeter@492
  1226
  /// \brief DFS algorithm class with visitor interface.
kpeter@244
  1227
  ///
kpeter@492
  1228
  /// This class provides an efficient implementation of the DFS algorithm
alpar@100
  1229
  /// with visitor interface.
alpar@100
  1230
  ///
kpeter@492
  1231
  /// The DfsVisit class provides an alternative interface to the Dfs
alpar@100
  1232
  /// class. It works with callback mechanism, the DfsVisit object calls
kpeter@244
  1233
  /// the member functions of the \c Visitor class on every DFS event.
alpar@100
  1234
  ///
kpeter@252
  1235
  /// This interface of the DFS algorithm should be used in special cases
kpeter@252
  1236
  /// when extra actions have to be performed in connection with certain
kpeter@252
  1237
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
kpeter@252
  1238
  /// instead.
kpeter@252
  1239
  ///
kpeter@492
  1240
  /// \tparam GR The type of the digraph the algorithm runs on.
kpeter@492
  1241
  /// The default type is \ref ListDigraph.
kpeter@492
  1242
  /// The value of GR is not used directly by \ref DfsVisit,
kpeter@492
  1243
  /// it is only passed to \ref DfsVisitDefaultTraits.
kpeter@492
  1244
  /// \tparam VS The Visitor type that is used by the algorithm.
kpeter@492
  1245
  /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
kpeter@244
  1246
  /// does not observe the DFS events. If you want to observe the DFS
kpeter@244
  1247
  /// events, you should implement your own visitor class.
kpeter@825
  1248
  /// \tparam TR The traits class that defines various types used by the
kpeter@825
  1249
  /// algorithm. By default, it is \ref DfsVisitDefaultTraits
kpeter@825
  1250
  /// "DfsVisitDefaultTraits<GR>".
kpeter@825
  1251
  /// In most cases, this parameter should not be set directly,
kpeter@825
  1252
  /// consider to use the named template parameters instead.
alpar@100
  1253
#ifdef DOXYGEN
kpeter@492
  1254
  template <typename GR, typename VS, typename TR>
alpar@100
  1255
#else
kpeter@492
  1256
  template <typename GR = ListDigraph,
kpeter@492
  1257
            typename VS = DfsVisitor<GR>,
kpeter@492
  1258
            typename TR = DfsVisitDefaultTraits<GR> >
alpar@100
  1259
#endif
alpar@100
  1260
  class DfsVisit {
alpar@100
  1261
  public:
alpar@209
  1262
kpeter@244
  1263
    ///The traits class.
kpeter@492
  1264
    typedef TR Traits;
alpar@100
  1265
kpeter@244
  1266
    ///The type of the digraph the algorithm runs on.
alpar@100
  1267
    typedef typename Traits::Digraph Digraph;
alpar@100
  1268
kpeter@244
  1269
    ///The visitor type used by the algorithm.
kpeter@492
  1270
    typedef VS Visitor;
alpar@100
  1271
kpeter@244
  1272
    ///The type of the map that indicates which nodes are reached.
alpar@100
  1273
    typedef typename Traits::ReachedMap ReachedMap;
alpar@100
  1274
alpar@100
  1275
  private:
alpar@100
  1276
alpar@100
  1277
    typedef typename Digraph::Node Node;
alpar@100
  1278
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
  1279
    typedef typename Digraph::Arc Arc;
alpar@100
  1280
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
  1281
kpeter@244
  1282
    //Pointer to the underlying digraph.
alpar@100
  1283
    const Digraph *_digraph;
kpeter@244
  1284
    //Pointer to the visitor object.
alpar@100
  1285
    Visitor *_visitor;
kpeter@244
  1286
    //Pointer to the map of reached status of the nodes.
alpar@100
  1287
    ReachedMap *_reached;
kpeter@244
  1288
    //Indicates if _reached is locally allocated (true) or not.
alpar@100
  1289
    bool local_reached;
alpar@100
  1290
alpar@100
  1291
    std::vector<typename Digraph::Arc> _stack;
alpar@100
  1292
    int _stack_head;
alpar@100
  1293
alpar@280
  1294
    //Creates the maps if necessary.
alpar@100
  1295
    void create_maps() {
alpar@100
  1296
      if(!_reached) {
alpar@209
  1297
        local_reached = true;
alpar@209
  1298
        _reached = Traits::createReachedMap(*_digraph);
alpar@100
  1299
      }
alpar@100
  1300
    }
alpar@100
  1301
alpar@100
  1302
  protected:
alpar@100
  1303
alpar@100
  1304
    DfsVisit() {}
alpar@209
  1305
alpar@100
  1306
  public:
alpar@100
  1307
alpar@100
  1308
    typedef DfsVisit Create;
alpar@100
  1309
kpeter@405
  1310
    /// \name Named Template Parameters
alpar@100
  1311
alpar@100
  1312
    ///@{
alpar@100
  1313
    template <class T>
kpeter@257
  1314
    struct SetReachedMapTraits : public Traits {
alpar@100
  1315
      typedef T ReachedMap;
alpar@100
  1316
      static ReachedMap *createReachedMap(const Digraph &digraph) {
deba@290
  1317
        LEMON_ASSERT(false, "ReachedMap is not initialized");
deba@290
  1318
        return 0; // ignore warnings
alpar@100
  1319
      }
alpar@100
  1320
    };
alpar@209
  1321
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@244
  1322
    /// ReachedMap type.
alpar@100
  1323
    ///
kpeter@244
  1324
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
alpar@100
  1325
    template <class T>
kpeter@257
  1326
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
kpeter@257
  1327
                                            SetReachedMapTraits<T> > {
kpeter@257
  1328
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
alpar@100
  1329
    };
alpar@100
  1330
    ///@}
alpar@100
  1331
alpar@209
  1332
  public:
alpar@209
  1333
alpar@100
  1334
    /// \brief Constructor.
alpar@100
  1335
    ///
alpar@100
  1336
    /// Constructor.
alpar@100
  1337
    ///
kpeter@244
  1338
    /// \param digraph The digraph the algorithm runs on.
kpeter@244
  1339
    /// \param visitor The visitor object of the algorithm.
alpar@209
  1340
    DfsVisit(const Digraph& digraph, Visitor& visitor)
alpar@100
  1341
      : _digraph(&digraph), _visitor(&visitor),
alpar@209
  1342
        _reached(0), local_reached(false) {}
alpar@209
  1343
alpar@100
  1344
    /// \brief Destructor.
alpar@100
  1345
    ~DfsVisit() {
alpar@100
  1346
      if(local_reached) delete _reached;
alpar@100
  1347
    }
alpar@100
  1348
kpeter@244
  1349
    /// \brief Sets the map that indicates which nodes are reached.
alpar@100
  1350
    ///
kpeter@244
  1351
    /// Sets the map that indicates which nodes are reached.
kpeter@405
  1352
    /// If you don't use this function before calling \ref run(Node) "run()"
kpeter@405
  1353
    /// or \ref init(), an instance will be allocated automatically.
kpeter@405
  1354
    /// The destructor deallocates this automatically allocated map,
kpeter@405
  1355
    /// of course.
alpar@100
  1356
    /// \return <tt> (*this) </tt>
alpar@100
  1357
    DfsVisit &reachedMap(ReachedMap &m) {
alpar@100
  1358
      if(local_reached) {
alpar@209
  1359
        delete _reached;
alpar@209
  1360
        local_reached=false;
alpar@100
  1361
      }
alpar@100
  1362
      _reached = &m;
alpar@100
  1363
      return *this;
alpar@100
  1364
    }
alpar@100
  1365
alpar@100
  1366
  public:
kpeter@244
  1367
kpeter@405
  1368
    /// \name Execution Control
kpeter@405
  1369
    /// The simplest way to execute the DFS algorithm is to use one of the
kpeter@405
  1370
    /// member functions called \ref run(Node) "run()".\n
kpeter@713
  1371
    /// If you need better control on the execution, you have to call
kpeter@713
  1372
    /// \ref init() first, then you can add a source node with \ref addSource()
kpeter@405
  1373
    /// and perform the actual computation with \ref start().
kpeter@405
  1374
    /// This procedure can be repeated if there are nodes that have not
kpeter@405
  1375
    /// been reached.
alpar@100
  1376
alpar@100
  1377
    /// @{
kpeter@244
  1378
alpar@100
  1379
    /// \brief Initializes the internal data structures.
alpar@100
  1380
    ///
alpar@100
  1381
    /// Initializes the internal data structures.
alpar@100
  1382
    void init() {
alpar@100
  1383
      create_maps();
alpar@100
  1384
      _stack.resize(countNodes(*_digraph));
alpar@100
  1385
      _stack_head = -1;
alpar@100
  1386
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
alpar@209
  1387
        _reached->set(u, false);
alpar@100
  1388
      }
alpar@100
  1389
    }
alpar@209
  1390
kpeter@405
  1391
    /// \brief Adds a new source node.
alpar@100
  1392
    ///
kpeter@405
  1393
    /// Adds a new source node to the set of nodes to be processed.
kpeter@244
  1394
    ///
kpeter@405
  1395
    /// \pre The stack must be empty. Otherwise the algorithm gives
kpeter@405
  1396
    /// wrong results. (One of the outgoing arcs of all the source nodes
kpeter@405
  1397
    /// except for the last one will not be visited and distances will
kpeter@405
  1398
    /// also be wrong.)
kpeter@244
  1399
    void addSource(Node s)
kpeter@244
  1400
    {
kpeter@244
  1401
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
alpar@100
  1402
      if(!(*_reached)[s]) {
alpar@209
  1403
          _reached->set(s,true);
alpar@209
  1404
          _visitor->start(s);
alpar@209
  1405
          _visitor->reach(s);
alpar@209
  1406
          Arc e;
alpar@209
  1407
          _digraph->firstOut(e, s);
alpar@209
  1408
          if (e != INVALID) {
alpar@209
  1409
            _stack[++_stack_head] = e;
alpar@209
  1410
          } else {
alpar@209
  1411
            _visitor->leave(s);
deba@419
  1412
            _visitor->stop(s);
alpar@209
  1413
          }
alpar@209
  1414
        }
alpar@100
  1415
    }
alpar@209
  1416
alpar@100
  1417
    /// \brief Processes the next arc.
alpar@100
  1418
    ///
alpar@100
  1419
    /// Processes the next arc.
alpar@100
  1420
    ///
alpar@100
  1421
    /// \return The processed arc.
alpar@100
  1422
    ///
kpeter@244
  1423
    /// \pre The stack must not be empty.
alpar@209
  1424
    Arc processNextArc() {
alpar@100
  1425
      Arc e = _stack[_stack_head];
alpar@100
  1426
      Node m = _digraph->target(e);
alpar@100
  1427
      if(!(*_reached)[m]) {
alpar@209
  1428
        _visitor->discover(e);
alpar@209
  1429
        _visitor->reach(m);
alpar@209
  1430
        _reached->set(m, true);
alpar@209
  1431
        _digraph->firstOut(_stack[++_stack_head], m);
alpar@100
  1432
      } else {
alpar@209
  1433
        _visitor->examine(e);
alpar@209
  1434
        m = _digraph->source(e);
alpar@209
  1435
        _digraph->nextOut(_stack[_stack_head]);
alpar@100
  1436
      }
alpar@100
  1437
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
alpar@209
  1438
        _visitor->leave(m);
alpar@209
  1439
        --_stack_head;
alpar@209
  1440
        if (_stack_head >= 0) {
alpar@209
  1441
          _visitor->backtrack(_stack[_stack_head]);
alpar@209
  1442
          m = _digraph->source(_stack[_stack_head]);
alpar@209
  1443
          _digraph->nextOut(_stack[_stack_head]);
alpar@209
  1444
        } else {
alpar@209
  1445
          _visitor->stop(m);
alpar@209
  1446
        }
alpar@100
  1447
      }
alpar@100
  1448
      return e;
alpar@100
  1449
    }
alpar@100
  1450
alpar@100
  1451
    /// \brief Next arc to be processed.
alpar@100
  1452
    ///
alpar@100
  1453
    /// Next arc to be processed.
alpar@100
  1454
    ///
alpar@100
  1455
    /// \return The next arc to be processed or INVALID if the stack is
alpar@100
  1456
    /// empty.
kpeter@244
  1457
    Arc nextArc() const {
alpar@100
  1458
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
alpar@100
  1459
    }
alpar@100
  1460
alpar@100
  1461
    /// \brief Returns \c false if there are nodes
kpeter@244
  1462
    /// to be processed.
alpar@100
  1463
    ///
alpar@100
  1464
    /// Returns \c false if there are nodes
kpeter@244
  1465
    /// to be processed in the queue (stack).
kpeter@244
  1466
    bool emptyQueue() const { return _stack_head < 0; }
alpar@100
  1467
alpar@100
  1468
    /// \brief Returns the number of the nodes to be processed.
alpar@100
  1469
    ///
kpeter@244
  1470
    /// Returns the number of the nodes to be processed in the queue (stack).
kpeter@244
  1471
    int queueSize() const { return _stack_head + 1; }
alpar@209
  1472
alpar@100
  1473
    /// \brief Executes the algorithm.
alpar@100
  1474
    ///
alpar@100
  1475
    /// Executes the algorithm.
alpar@100
  1476
    ///
kpeter@244
  1477
    /// This method runs the %DFS algorithm from the root node
kpeter@244
  1478
    /// in order to compute the %DFS path to each node.
kpeter@244
  1479
    ///
kpeter@244
  1480
    /// The algorithm computes
kpeter@244
  1481
    /// - the %DFS tree,
kpeter@244
  1482
    /// - the distance of each node from the root in the %DFS tree.
kpeter@244
  1483
    ///
kpeter@244
  1484
    /// \pre init() must be called and a root node should be
kpeter@244
  1485
    /// added with addSource() before using this function.
kpeter@244
  1486
    ///
kpeter@244
  1487
    /// \note <tt>d.start()</tt> is just a shortcut of the following code.
kpeter@244
  1488
    /// \code
kpeter@244
  1489
    ///   while ( !d.emptyQueue() ) {
kpeter@244
  1490
    ///     d.processNextArc();
kpeter@244
  1491
    ///   }
kpeter@244
  1492
    /// \endcode
alpar@100
  1493
    void start() {
alpar@100
  1494
      while ( !emptyQueue() ) processNextArc();
alpar@100
  1495
    }
alpar@209
  1496
kpeter@244
  1497
    /// \brief Executes the algorithm until the given target node is reached.
alpar@100
  1498
    ///
kpeter@244
  1499
    /// Executes the algorithm until the given target node is reached.
alpar@100
  1500
    ///
kpeter@244
  1501
    /// This method runs the %DFS algorithm from the root node
kpeter@286
  1502
    /// in order to compute the DFS path to \c t.
kpeter@244
  1503
    ///
kpeter@244
  1504
    /// The algorithm computes
kpeter@286
  1505
    /// - the %DFS path to \c t,
kpeter@286
  1506
    /// - the distance of \c t from the root in the %DFS tree.
kpeter@244
  1507
    ///
kpeter@244
  1508
    /// \pre init() must be called and a root node should be added
alpar@100
  1509
    /// with addSource() before using this function.
kpeter@286
  1510
    void start(Node t) {
kpeter@286
  1511
      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
alpar@209
  1512
        processNextArc();
alpar@100
  1513
    }
alpar@209
  1514
alpar@100
  1515
    /// \brief Executes the algorithm until a condition is met.
alpar@100
  1516
    ///
alpar@100
  1517
    /// Executes the algorithm until a condition is met.
alpar@100
  1518
    ///
kpeter@244
  1519
    /// This method runs the %DFS algorithm from the root node
kpeter@244
  1520
    /// until an arc \c a with <tt>am[a]</tt> true is found.
kpeter@244
  1521
    ///
kpeter@244
  1522
    /// \param am A \c bool (or convertible) arc map. The algorithm
kpeter@244
  1523
    /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
kpeter@244
  1524
    ///
kpeter@244
  1525
    /// \return The reached arc \c a with <tt>am[a]</tt> true or
kpeter@244
  1526
    /// \c INVALID if no such arc was found.
kpeter@244
  1527
    ///
kpeter@244
  1528
    /// \pre init() must be called and a root node should be added
alpar@100
  1529
    /// with addSource() before using this function.
alpar@100
  1530
    ///
kpeter@244
  1531
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
alpar@100
  1532
    /// not a node map.
kpeter@244
  1533
    template <typename AM>
kpeter@244
  1534
    Arc start(const AM &am) {
kpeter@244
  1535
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
alpar@100
  1536
        processNextArc();
alpar@100
  1537
      return emptyQueue() ? INVALID : _stack[_stack_head];
alpar@100
  1538
    }
alpar@100
  1539
kpeter@286
  1540
    /// \brief Runs the algorithm from the given source node.
alpar@100
  1541
    ///
kpeter@244
  1542
    /// This method runs the %DFS algorithm from node \c s.
kpeter@244
  1543
    /// in order to compute the DFS path to each node.
kpeter@244
  1544
    ///
kpeter@244
  1545
    /// The algorithm computes
kpeter@244
  1546
    /// - the %DFS tree,
kpeter@244
  1547
    /// - the distance of each node from the root in the %DFS tree.
kpeter@244
  1548
    ///
kpeter@244
  1549
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
alpar@100
  1550
    ///\code
alpar@100
  1551
    ///   d.init();
alpar@100
  1552
    ///   d.addSource(s);
alpar@100
  1553
    ///   d.start();
alpar@100
  1554
    ///\endcode
alpar@100
  1555
    void run(Node s) {
alpar@100
  1556
      init();
alpar@100
  1557
      addSource(s);
alpar@100
  1558
      start();
alpar@100
  1559
    }
alpar@100
  1560
kpeter@244
  1561
    /// \brief Finds the %DFS path between \c s and \c t.
kpeter@244
  1562
kpeter@244
  1563
    /// This method runs the %DFS algorithm from node \c s
kpeter@286
  1564
    /// in order to compute the DFS path to node \c t
kpeter@286
  1565
    /// (it stops searching when \c t is processed).
kpeter@244
  1566
    ///
kpeter@286
  1567
    /// \return \c true if \c t is reachable form \c s.
kpeter@244
  1568
    ///
kpeter@244
  1569
    /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
kpeter@244
  1570
    /// just a shortcut of the following code.
kpeter@244
  1571
    ///\code
kpeter@244
  1572
    ///   d.init();
kpeter@244
  1573
    ///   d.addSource(s);
kpeter@244
  1574
    ///   d.start(t);
kpeter@244
  1575
    ///\endcode
kpeter@286
  1576
    bool run(Node s,Node t) {
kpeter@244
  1577
      init();
kpeter@244
  1578
      addSource(s);
kpeter@244
  1579
      start(t);
kpeter@286
  1580
      return reached(t);
kpeter@244
  1581
    }
kpeter@244
  1582
kpeter@244
  1583
    /// \brief Runs the algorithm to visit all nodes in the digraph.
alpar@209
  1584
kpeter@787
  1585
    /// This method runs the %DFS algorithm in order to visit all nodes
kpeter@787
  1586
    /// in the digraph.
kpeter@244
  1587
    ///
kpeter@244
  1588
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
alpar@100
  1589
    ///\code
kpeter@244
  1590
    ///   d.init();
kpeter@244
  1591
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
kpeter@244
  1592
    ///     if (!d.reached(n)) {
kpeter@244
  1593
    ///       d.addSource(n);
kpeter@244
  1594
    ///       d.start();
kpeter@244
  1595
    ///     }
kpeter@244
  1596
    ///   }
alpar@100
  1597
    ///\endcode
alpar@100
  1598
    void run() {
alpar@100
  1599
      init();
alpar@100
  1600
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
alpar@100
  1601
        if (!reached(it)) {
alpar@100
  1602
          addSource(it);
alpar@100
  1603
          start();
alpar@100
  1604
        }
alpar@100
  1605
      }
alpar@100
  1606
    }
kpeter@244
  1607
alpar@100
  1608
    ///@}
alpar@100
  1609
alpar@100
  1610
    /// \name Query Functions
kpeter@405
  1611
    /// The results of the DFS algorithm can be obtained using these
alpar@100
  1612
    /// functions.\n
kpeter@405
  1613
    /// Either \ref run(Node) "run()" or \ref start() should be called
kpeter@405
  1614
    /// before using them.
kpeter@405
  1615
alpar@100
  1616
    ///@{
kpeter@244
  1617
kpeter@716
  1618
    /// \brief Checks if the given node is reached from the root(s).
alpar@100
  1619
    ///
kpeter@405
  1620
    /// Returns \c true if \c v is reached from the root(s).
kpeter@405
  1621
    ///
kpeter@405
  1622
    /// \pre Either \ref run(Node) "run()" or \ref init()
alpar@100
  1623
    /// must be called before using this function.
kpeter@420
  1624
    bool reached(Node v) const { return (*_reached)[v]; }
kpeter@244
  1625
alpar@100
  1626
    ///@}
kpeter@244
  1627
alpar@100
  1628
  };
alpar@100
  1629
alpar@100
  1630
} //END OF NAMESPACE LEMON
alpar@100
  1631
alpar@100
  1632
#endif