COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/dfs.h @ 1694:6d81e6f7a88d

Last change on this file since 1694:6d81e6f7a88d was 1694:6d81e6f7a88d, checked in by Balazs Dezso, 14 years ago

Fixing naming conventions
Temporarly bugfix with named-parameters
Removing dead codes

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