src/hugo/dfs.h
changeset 798 6d1abeb62dd3
parent 780 e06d0d16595f
child 802 bc0c74eeb151
equal deleted inserted replaced
0:2b6f9e195bb4 1:45981ef6ec0f
     2 #ifndef HUGO_DFS_H
     2 #ifndef HUGO_DFS_H
     3 #define HUGO_DFS_H
     3 #define HUGO_DFS_H
     4 
     4 
     5 ///\ingroup flowalgs
     5 ///\ingroup flowalgs
     6 ///\file
     6 ///\file
     7 ///\brief Dfs algorithm.
     7 ///\brief %DFS algorithm.
     8 ///
     8 ///
     9 ///\todo Revise Manual.
     9 ///\todo Revise Manual.
    10 
    10 
    11 #include <hugo/bin_heap.h>
    11 #include <hugo/bin_heap.h>
    12 #include <hugo/invalid.h>
    12 #include <hugo/invalid.h>
    14 namespace hugo {
    14 namespace hugo {
    15 
    15 
    16 /// \addtogroup flowalgs
    16 /// \addtogroup flowalgs
    17 /// @{
    17 /// @{
    18 
    18 
    19   ///%Dfs algorithm class.
    19   ///%DFS algorithm class.
    20 
    20 
    21   ///This class provides an efficient implementation of %Dfs algorithm.
    21   ///This class provides an efficient implementation of %DFS algorithm.
    22   ///The edge lengths are passed to the algorithm using a
       
    23   ///\ref ReadMapSkeleton "readable map",
       
    24   ///so it is easy to change it to any kind of length.
       
    25   ///
       
    26   ///The type of the length is determined by the \c ValueType of the length map.
       
    27   ///
       
    28   ///It is also possible to change the underlying priority heap.
       
    29   ///
    22   ///
    30   ///\param GR The graph type the algorithm runs on.
    23   ///\param GR The graph type the algorithm runs on.
    31   ///\param LM This read-only
       
    32   ///EdgeMap
       
    33   ///determines the
       
    34   ///lengths of the edges. It is read once for each edge, so the map
       
    35   ///may involve in relatively time consuming process to compute the edge
       
    36   ///length if it is necessary. The default map type is
       
    37   ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
       
    38   ///\param Heap The heap type used by the %Dfs
       
    39   ///algorithm. The default
       
    40   ///is using \ref BinHeap "binary heap".
       
    41   ///
    24   ///
    42   ///\author Jacint Szabo and Alpar Juttner
    25   ///\author Alpar Juttner
    43   ///\todo We need a typedef-names should be standardized. (-:
       
    44   ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
       
    45   ///should not be fixed. (Problematic to solve).
       
    46 
    26 
    47 #ifdef DOXYGEN
    27 #ifdef DOXYGEN
    48   template <typename GR>
    28   template <typename GR>
    49 #else
    29 #else
    50   template <typename GR>
    30   template <typename GR>
    57     typedef typename Graph::NodeIt NodeIt;
    37     typedef typename Graph::NodeIt NodeIt;
    58     typedef typename Graph::Edge Edge;
    38     typedef typename Graph::Edge Edge;
    59     typedef typename Graph::OutEdgeIt OutEdgeIt;
    39     typedef typename Graph::OutEdgeIt OutEdgeIt;
    60     
    40     
    61     ///\brief The type of the map that stores the last
    41     ///\brief The type of the map that stores the last
    62     ///edges of the shortest paths.
    42     ///edges of the paths on the %DFS tree.
    63     typedef typename Graph::template NodeMap<Edge> PredMap;
    43     typedef typename Graph::template NodeMap<Edge> PredMap;
    64     ///\brief The type of the map that stores the last but one
    44     ///\brief The type of the map that stores the last but one
    65     ///nodes of the shortest paths.
    45     ///nodes of the paths on the %DFS tree.
    66     typedef typename Graph::template NodeMap<Node> PredNodeMap;
    46     typedef typename Graph::template NodeMap<Node> PredNodeMap;
    67     ///The type of the map that stores the dists of the nodes.
    47     ///The type of the map that stores the dists of the nodes on the %DFS tree.
    68     typedef typename Graph::template NodeMap<int> DistMap;
    48     typedef typename Graph::template NodeMap<int> DistMap;
    69 
    49 
    70   private:
    50   private:
    71     const Graph *G;
    51     const Graph *G;
    72     PredMap *predecessor;
    52     PredMap *predecessor;
    78 
    58 
    79     //The source node of the last execution.
    59     //The source node of the last execution.
    80     Node source;
    60     Node source;
    81 
    61 
    82 
    62 
    83     ///Initialize maps.
    63     ///Initializes the maps.
    84     
       
    85     ///\todo Error if \c G or are \c NULL.
       
    86     ///\todo Better memory allocation (instead of new).
       
    87     void init_maps() 
    64     void init_maps() 
    88     {
    65     {
    89 //       if(!length) {
       
    90 // 	local_length = true;
       
    91 // 	length = new LM(G);
       
    92 //       }
       
    93       if(!predecessor) {
    66       if(!predecessor) {
    94 	local_predecessor = true;
    67 	local_predecessor = true;
    95 	predecessor = new PredMap(*G);
    68 	predecessor = new PredMap(*G);
    96       }
    69       }
    97       if(!pred_node) {
    70       if(!pred_node) {
   112       distance(NULL), local_distance(false)
    85       distance(NULL), local_distance(false)
   113     { }
    86     { }
   114     
    87     
   115     ~Dfs() 
    88     ~Dfs() 
   116     {
    89     {
   117       //      if(local_length) delete length;
       
   118       if(local_predecessor) delete predecessor;
    90       if(local_predecessor) delete predecessor;
   119       if(local_pred_node) delete pred_node;
    91       if(local_pred_node) delete pred_node;
   120       if(local_distance) delete distance;
    92       if(local_distance) delete distance;
   121     }
    93     }
   122 
    94 
   123     ///Sets the graph the algorithm will run on.
    95     ///Sets the graph the algorithm will run on.
   124 
    96 
   125     ///Sets the graph the algorithm will run on.
    97     ///Sets the graph the algorithm will run on.
   126     ///\return <tt> (*this) </tt>
    98     ///\return <tt> (*this) </tt>
       
    99     ///\bug What about maps?
       
   100     ///\todo It may be unnecessary
   127     Dfs &setGraph(const Graph &_G) 
   101     Dfs &setGraph(const Graph &_G) 
   128     {
   102     {
   129       G = &_G;
   103       G = &_G;
   130       return *this;
   104       return *this;
   131     }
   105     }
   132     ///Sets the length map.
       
   133 
       
   134     ///Sets the map storing the predecessor edges.
   106     ///Sets the map storing the predecessor edges.
   135 
   107 
   136     ///Sets the map storing the predecessor edges.
   108     ///Sets the map storing the predecessor edges.
   137     ///If you don't use this function before calling \ref run(),
   109     ///If you don't use this function before calling \ref run(),
   138     ///it will allocate one. The destuctor deallocates this
   110     ///it will allocate one. The destuctor deallocates this
   184     
   156     
   185   ///Runs %DFS algorithm from node \c s.
   157   ///Runs %DFS algorithm from node \c s.
   186 
   158 
   187   ///This method runs the %DFS algorithm from a root node \c s
   159   ///This method runs the %DFS algorithm from a root node \c s
   188   ///in order to
   160   ///in order to
   189   ///compute the
   161   ///compute 
   190   ///shortest path to each node. The algorithm computes
   162   ///- a %DFS tree and
   191   ///- The shortest path tree.
   163   ///- the distance of each node from the root on this tree.
   192   ///- The distance of each node from the root.
       
   193  
   164  
   194     void run(Node s) {
   165     void run(Node s) {
   195       
   166       
   196       init_maps();
   167       init_maps();
   197       
   168       
   225 	  else ++Q[Qh];
   196 	  else ++Q[Qh];
   226 	else if(--Qh>=0) n=G->tail(Q[Qh]);
   197 	else if(--Qh>=0) n=G->tail(Q[Qh]);
   227       } while(Qh>=0);
   198       } while(Qh>=0);
   228     }
   199     }
   229     
   200     
   230     ///The distance of a node from the root.
   201     ///The distance of a node from the root on the %DFS tree.
   231 
   202 
   232     ///Returns the distance of a node from the root.
   203     ///Returns the distance of a node from the root on the %DFS tree.
   233     ///\pre \ref run() must be called before using this function.
   204     ///\pre \ref run() must be called before using this function.
   234     ///\warning If node \c v in unreachable from the root the return value
   205     ///\warning If node \c v in unreachable from the root the return value
   235     ///of this funcion is undefined.
   206     ///of this funcion is undefined.
   236     int dist(Node v) const { return (*distance)[v]; }
   207     int dist(Node v) const { return (*distance)[v]; }
   237 
   208 
   238     ///Returns the 'previous edge' of the shortest path tree.
   209     ///Returns the 'previous edge' of the %DFS path tree.
   239 
   210 
   240     ///For a node \c v it returns the 'previous edge' of the shortest path tree,
   211     ///For a node \c v it returns the last edge of the path on the %DFS tree
   241     ///i.e. it returns the last edge from a shortest path from the root to \c
   212     ///from the root to \c
   242     ///v. It is \ref INVALID
   213     ///v. It is \ref INVALID
   243     ///if \c v is unreachable from the root or if \c v=s. The
   214     ///if \c v is unreachable from the root or if \c v=s. The
   244     ///shortest path tree used here is equal to the shortest path tree used in
   215     ///%DFS tree used here is equal to the %DFS tree used in
   245     ///\ref predNode(Node v).  \pre \ref run() must be called before using
   216     ///\ref predNode(Node v).  \pre \ref run() must be called before using
   246     ///this function.
   217     ///this function.
   247     Edge pred(Node v) const { return (*predecessor)[v]; }
   218     Edge pred(Node v) const { return (*predecessor)[v]; }
   248 
   219 
   249     ///Returns the 'previous node' of the shortest path tree.
   220     ///Returns the 'previous node' of the %DFS tree.
   250 
   221 
   251     ///For a node \c v it returns the 'previous node' of the shortest path tree,
   222     ///For a node \c v it returns the 'previous node' on the %DFS tree,
   252     ///i.e. it returns the last but one node from a shortest path from the
   223     ///i.e. it returns the last but one node of the path from the
   253     ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
   224     ///root to \c /v on the %DFS tree.
   254     ///\c v=s. The shortest path tree used here is equal to the shortest path
   225     ///It is INVALID if \c v is unreachable from the root or if
   255     ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
   226     ///\c v=s.
       
   227     ///\pre \ref run() must be called before
   256     ///using this function.
   228     ///using this function.
   257     Node predNode(Node v) const { return (*pred_node)[v]; }
   229     Node predNode(Node v) const { return (*pred_node)[v]; }
   258     
   230     
   259     ///Returns a reference to the NodeMap of distances.
   231     ///Returns a reference to the NodeMap of distances on the %DFS tree.
   260     
   232     
   261     ///Returns a reference to the NodeMap of distances. \pre \ref run() must
   233     ///Returns a reference to the NodeMap of distances on the %DFS tree.
       
   234     ///\pre \ref run() must
   262     ///be called before using this function.
   235     ///be called before using this function.
   263     const DistMap &distMap() const { return *distance;}
   236     const DistMap &distMap() const { return *distance;}
   264  
   237  
   265     ///Returns a reference to the shortest path tree map.
   238     ///Returns a reference to the %DFS tree map.
   266 
   239 
   267     ///Returns a reference to the NodeMap of the edges of the
   240     ///Returns a reference to the NodeMap of the edges of the
   268     ///shortest path tree.
   241     ///%DFS tree.
   269     ///\pre \ref run() must be called before using this function.
   242     ///\pre \ref run() must be called before using this function.
   270     const PredMap &predMap() const { return *predecessor;}
   243     const PredMap &predMap() const { return *predecessor;}
   271  
   244  
   272     ///Returns a reference to the map of nodes of shortest paths.
   245     ///Returns a reference to the map of last but one nodes of the %DFS tree.
   273 
   246 
   274     ///Returns a reference to the NodeMap of the last but one nodes of the
   247     ///Returns a reference to the NodeMap of the last but one nodes of the paths
   275     ///shortest path tree.
   248     ///on the
       
   249     ///%DFS tree.
   276     ///\pre \ref run() must be called before using this function.
   250     ///\pre \ref run() must be called before using this function.
   277     const PredNodeMap &predNodeMap() const { return *pred_node;}
   251     const PredNodeMap &predNodeMap() const { return *pred_node;}
   278 
   252 
   279     ///Checks if a node is reachable from the root.
   253     ///Checks if a node is reachable from the root.
   280 
   254 
   285     ///
   259     ///
   286     bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
   260     bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
   287     
   261     
   288   };
   262   };
   289   
   263   
   290 
       
   291   // **********************************************************************
       
   292   //  IMPLEMENTATIONS
       
   293   // **********************************************************************
       
   294 
       
   295 /// @}
   264 /// @}
   296   
   265   
   297 } //END OF NAMESPACE HUGO
   266 } //END OF NAMESPACE HUGO
   298 
   267 
   299 #endif
   268 #endif