COIN-OR::LEMON - Graph Library

source: lemon/lemon/dfs.h @ 258:0310c8984732

Last change on this file since 258:0310c8984732 was 258:0310c8984732, checked in by Alpar Juttner <alpar@…>, 16 years ago

Merge

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