1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/dfs.h Wed Sep 29 15:30:04 2004 +0000
1.3 @@ -0,0 +1,290 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/dfs.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_DFS_H
1.21 +#define LEMON_DFS_H
1.22 +
1.23 +///\ingroup flowalgs
1.24 +///\file
1.25 +///\brief %DFS algorithm.
1.26 +///
1.27 +///\todo Revise Manual.
1.28 +
1.29 +#include <lemon/bin_heap.h>
1.30 +#include <lemon/invalid.h>
1.31 +
1.32 +namespace lemon {
1.33 +
1.34 +/// \addtogroup flowalgs
1.35 +/// @{
1.36 +
1.37 + ///%DFS algorithm class.
1.38 +
1.39 + ///This class provides an efficient implementation of %DFS algorithm.
1.40 + ///
1.41 + ///\param GR The graph type the algorithm runs on.
1.42 + ///
1.43 + ///\author Alpar Juttner
1.44 +
1.45 +#ifdef DOXYGEN
1.46 + template <typename GR>
1.47 +#else
1.48 + template <typename GR>
1.49 +#endif
1.50 + class Dfs{
1.51 + public:
1.52 + ///The type of the underlying graph.
1.53 + typedef GR Graph;
1.54 + ///\e
1.55 + typedef typename Graph::Node Node;
1.56 + ///\e
1.57 + typedef typename Graph::NodeIt NodeIt;
1.58 + ///\e
1.59 + typedef typename Graph::Edge Edge;
1.60 + ///\e
1.61 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.62 +
1.63 + ///\brief The type of the map that stores the last
1.64 + ///edges of the paths on the %DFS tree.
1.65 + typedef typename Graph::template NodeMap<Edge> PredMap;
1.66 + ///\brief The type of the map that stores the last but one
1.67 + ///nodes of the paths on the %DFS tree.
1.68 + typedef typename Graph::template NodeMap<Node> PredNodeMap;
1.69 + ///The type of the map that stores the dists of the nodes on the %DFS tree.
1.70 + typedef typename Graph::template NodeMap<int> DistMap;
1.71 +
1.72 + private:
1.73 + /// Pointer to the underlying graph.
1.74 + const Graph *G;
1.75 + ///Pointer to the map of predecessors edges.
1.76 + PredMap *predecessor;
1.77 + ///Indicates if \ref predecessor is locally allocated (\c true) or not.
1.78 + bool local_predecessor;
1.79 + ///Pointer to the map of predecessors nodes.
1.80 + PredNodeMap *pred_node;
1.81 + ///Indicates if \ref pred_node is locally allocated (\c true) or not.
1.82 + bool local_pred_node;
1.83 + ///Pointer to the map of distances.
1.84 + DistMap *distance;
1.85 + ///Indicates if \ref distance is locally allocated (\c true) or not.
1.86 + bool local_distance;
1.87 +
1.88 + ///The source node of the last execution.
1.89 + Node source;
1.90 +
1.91 +
1.92 + ///Initializes the maps.
1.93 + void init_maps()
1.94 + {
1.95 + if(!predecessor) {
1.96 + local_predecessor = true;
1.97 + predecessor = new PredMap(*G);
1.98 + }
1.99 + if(!pred_node) {
1.100 + local_pred_node = true;
1.101 + pred_node = new PredNodeMap(*G);
1.102 + }
1.103 + if(!distance) {
1.104 + local_distance = true;
1.105 + distance = new DistMap(*G);
1.106 + }
1.107 + }
1.108 +
1.109 + public :
1.110 + ///Constructor.
1.111 +
1.112 + ///\param _G the graph the algorithm will run on.
1.113 + ///
1.114 + Dfs(const Graph& _G) :
1.115 + G(&_G),
1.116 + predecessor(NULL), local_predecessor(false),
1.117 + pred_node(NULL), local_pred_node(false),
1.118 + distance(NULL), local_distance(false)
1.119 + { }
1.120 +
1.121 + ///Destructor.
1.122 + ~Dfs()
1.123 + {
1.124 + if(local_predecessor) delete predecessor;
1.125 + if(local_pred_node) delete pred_node;
1.126 + if(local_distance) delete distance;
1.127 + }
1.128 +
1.129 + ///Sets the map storing the predecessor edges.
1.130 +
1.131 + ///Sets the map storing the predecessor edges.
1.132 + ///If you don't use this function before calling \ref run(),
1.133 + ///it will allocate one. The destuctor deallocates this
1.134 + ///automatically allocated map, of course.
1.135 + ///\return <tt> (*this) </tt>
1.136 + Dfs &setPredMap(PredMap &m)
1.137 + {
1.138 + if(local_predecessor) {
1.139 + delete predecessor;
1.140 + local_predecessor=false;
1.141 + }
1.142 + predecessor = &m;
1.143 + return *this;
1.144 + }
1.145 +
1.146 + ///Sets the map storing the predecessor nodes.
1.147 +
1.148 + ///Sets the map storing the predecessor nodes.
1.149 + ///If you don't use this function before calling \ref run(),
1.150 + ///it will allocate one. The destuctor deallocates this
1.151 + ///automatically allocated map, of course.
1.152 + ///\return <tt> (*this) </tt>
1.153 + Dfs &setPredNodeMap(PredNodeMap &m)
1.154 + {
1.155 + if(local_pred_node) {
1.156 + delete pred_node;
1.157 + local_pred_node=false;
1.158 + }
1.159 + pred_node = &m;
1.160 + return *this;
1.161 + }
1.162 +
1.163 + ///Sets the map storing the distances calculated by the algorithm.
1.164 +
1.165 + ///Sets the map storing the distances calculated by the algorithm.
1.166 + ///If you don't use this function before calling \ref run(),
1.167 + ///it will allocate one. The destuctor deallocates this
1.168 + ///automatically allocated map, of course.
1.169 + ///\return <tt> (*this) </tt>
1.170 + Dfs &setDistMap(DistMap &m)
1.171 + {
1.172 + if(local_distance) {
1.173 + delete distance;
1.174 + local_distance=false;
1.175 + }
1.176 + distance = &m;
1.177 + return *this;
1.178 + }
1.179 +
1.180 + ///Runs %DFS algorithm from node \c s.
1.181 +
1.182 + ///This method runs the %DFS algorithm from a root node \c s
1.183 + ///in order to
1.184 + ///compute
1.185 + ///- a %DFS tree and
1.186 + ///- the distance of each node from the root on this tree.
1.187 +
1.188 + void run(Node s) {
1.189 +
1.190 + init_maps();
1.191 +
1.192 + source = s;
1.193 +
1.194 + for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
1.195 + predecessor->set(u,INVALID);
1.196 + pred_node->set(u,INVALID);
1.197 + }
1.198 +
1.199 + int N=G->nodeNum();
1.200 + std::vector<typename Graph::OutEdgeIt> Q(N);
1.201 +
1.202 + int Qh=0;
1.203 +
1.204 + G->first(Q[Qh],s);
1.205 + distance->set(s, 0);
1.206 +
1.207 + Node n=s;
1.208 + Node m;
1.209 + OutEdgeIt e;
1.210 + do {
1.211 + if((e=Q[Qh])!=INVALID)
1.212 + if((m=G->head(e))!=s && (*predecessor)[m=G->head(e)]==INVALID) {
1.213 + predecessor->set(m,e);
1.214 + pred_node->set(m,n);
1.215 + G->first(Q[++Qh],m);
1.216 + distance->set(m,Qh);
1.217 + n=m;
1.218 + }
1.219 + else ++Q[Qh];
1.220 + else if(--Qh>=0) n=G->tail(Q[Qh]);
1.221 + } while(Qh>=0);
1.222 + }
1.223 +
1.224 + ///The distance of a node from the root on the %DFS tree.
1.225 +
1.226 + ///Returns the distance of a node from the root on the %DFS tree.
1.227 + ///\pre \ref run() must be called before using this function.
1.228 + ///\warning If node \c v in unreachable from the root the return value
1.229 + ///of this funcion is undefined.
1.230 + int dist(Node v) const { return (*distance)[v]; }
1.231 +
1.232 + ///Returns the 'previous edge' of the %DFS path tree.
1.233 +
1.234 + ///For a node \c v it returns the last edge of the path on the %DFS tree
1.235 + ///from the root to \c
1.236 + ///v. It is \ref INVALID
1.237 + ///if \c v is unreachable from the root or if \c v=s. The
1.238 + ///%DFS tree used here is equal to the %DFS tree used in
1.239 + ///\ref predNode(Node v). \pre \ref run() must be called before using
1.240 + ///this function.
1.241 + Edge pred(Node v) const { return (*predecessor)[v]; }
1.242 +
1.243 + ///Returns the 'previous node' of the %DFS tree.
1.244 +
1.245 + ///For a node \c v it returns the 'previous node' on the %DFS tree,
1.246 + ///i.e. it returns the last but one node of the path from the
1.247 + ///root to \c /v on the %DFS tree.
1.248 + ///It is INVALID if \c v is unreachable from the root or if
1.249 + ///\c v=s.
1.250 + ///\pre \ref run() must be called before
1.251 + ///using this function.
1.252 + Node predNode(Node v) const { return (*pred_node)[v]; }
1.253 +
1.254 + ///Returns a reference to the NodeMap of distances on the %DFS tree.
1.255 +
1.256 + ///Returns a reference to the NodeMap of distances on the %DFS tree.
1.257 + ///\pre \ref run() must
1.258 + ///be called before using this function.
1.259 + const DistMap &distMap() const { return *distance;}
1.260 +
1.261 + ///Returns a reference to the %DFS tree map.
1.262 +
1.263 + ///Returns a reference to the NodeMap of the edges of the
1.264 + ///%DFS tree.
1.265 + ///\pre \ref run() must be called before using this function.
1.266 + const PredMap &predMap() const { return *predecessor;}
1.267 +
1.268 + ///Returns a reference to the map of last but one nodes of the %DFS tree.
1.269 +
1.270 + ///Returns a reference to the NodeMap of the last but one nodes of the paths
1.271 + ///on the
1.272 + ///%DFS tree.
1.273 + ///\pre \ref run() must be called before using this function.
1.274 + const PredNodeMap &predNodeMap() const { return *pred_node;}
1.275 +
1.276 + ///Checks if a node is reachable from the root.
1.277 +
1.278 + ///Returns \c true if \c v is reachable from the root.
1.279 + ///\note The root node is reported to be reached!
1.280 + ///
1.281 + ///\pre \ref run() must be called before using this function.
1.282 + ///
1.283 + bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
1.284 +
1.285 + };
1.286 +
1.287 +/// @}
1.288 +
1.289 +} //END OF NAMESPACE LEMON
1.290 +
1.291 +#endif
1.292 +
1.293 +