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