| 1 | /* -*- C++ -*- |
|---|
| 2 | * src/hugo/dfs.h - Part of HUGOlib, a generic C++ optimization library |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|---|
| 5 | * (Egervary Combinatorial Optimization Research Group, 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 HUGO_DFS_H |
|---|
| 18 | #define HUGO_DFS_H |
|---|
| 19 | |
|---|
| 20 | ///\ingroup flowalgs |
|---|
| 21 | ///\file |
|---|
| 22 | ///\brief %DFS algorithm. |
|---|
| 23 | /// |
|---|
| 24 | ///\todo Revise Manual. |
|---|
| 25 | |
|---|
| 26 | #include <hugo/bin_heap.h> |
|---|
| 27 | #include <hugo/invalid.h> |
|---|
| 28 | |
|---|
| 29 | namespace hugo { |
|---|
| 30 | |
|---|
| 31 | /// \addtogroup flowalgs |
|---|
| 32 | /// @{ |
|---|
| 33 | |
|---|
| 34 | ///%DFS algorithm class. |
|---|
| 35 | |
|---|
| 36 | ///This class provides an efficient implementation of %DFS algorithm. |
|---|
| 37 | /// |
|---|
| 38 | ///\param GR The graph type the algorithm runs on. |
|---|
| 39 | /// |
|---|
| 40 | ///\author Alpar Juttner |
|---|
| 41 | |
|---|
| 42 | #ifdef DOXYGEN |
|---|
| 43 | template <typename GR> |
|---|
| 44 | #else |
|---|
| 45 | template <typename GR> |
|---|
| 46 | #endif |
|---|
| 47 | class Dfs{ |
|---|
| 48 | public: |
|---|
| 49 | ///The type of the underlying graph. |
|---|
| 50 | typedef GR Graph; |
|---|
| 51 | /// . |
|---|
| 52 | typedef typename Graph::Node Node; |
|---|
| 53 | /// . |
|---|
| 54 | typedef typename Graph::NodeIt NodeIt; |
|---|
| 55 | /// . |
|---|
| 56 | typedef typename Graph::Edge Edge; |
|---|
| 57 | /// . |
|---|
| 58 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
|---|
| 59 | |
|---|
| 60 | ///\brief The type of the map that stores the last |
|---|
| 61 | ///edges of the paths on the %DFS tree. |
|---|
| 62 | typedef typename Graph::template NodeMap<Edge> PredMap; |
|---|
| 63 | ///\brief The type of the map that stores the last but one |
|---|
| 64 | ///nodes of the paths on the %DFS tree. |
|---|
| 65 | typedef typename Graph::template NodeMap<Node> PredNodeMap; |
|---|
| 66 | ///The type of the map that stores the dists of the nodes on the %DFS tree. |
|---|
| 67 | typedef typename Graph::template NodeMap<int> DistMap; |
|---|
| 68 | |
|---|
| 69 | private: |
|---|
| 70 | /// Pointer to the underlying graph. |
|---|
| 71 | const Graph *G; |
|---|
| 72 | ///Pointer to the map of predecessors edges. |
|---|
| 73 | PredMap *predecessor; |
|---|
| 74 | ///Indicates if \ref predecessor is locally allocated (\c true) or not. |
|---|
| 75 | bool local_predecessor; |
|---|
| 76 | ///Pointer to the map of predecessors nodes. |
|---|
| 77 | PredNodeMap *pred_node; |
|---|
| 78 | ///Indicates if \ref pred_node is locally allocated (\c true) or not. |
|---|
| 79 | bool local_pred_node; |
|---|
| 80 | ///Pointer to the map of distances. |
|---|
| 81 | DistMap *distance; |
|---|
| 82 | ///Indicates if \ref distance is locally allocated (\c true) or not. |
|---|
| 83 | bool local_distance; |
|---|
| 84 | |
|---|
| 85 | ///The source node of the last execution. |
|---|
| 86 | Node source; |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | ///Initializes the maps. |
|---|
| 90 | void init_maps() |
|---|
| 91 | { |
|---|
| 92 | if(!predecessor) { |
|---|
| 93 | local_predecessor = true; |
|---|
| 94 | predecessor = new PredMap(*G); |
|---|
| 95 | } |
|---|
| 96 | if(!pred_node) { |
|---|
| 97 | local_pred_node = true; |
|---|
| 98 | pred_node = new PredNodeMap(*G); |
|---|
| 99 | } |
|---|
| 100 | if(!distance) { |
|---|
| 101 | local_distance = true; |
|---|
| 102 | distance = new DistMap(*G); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | public : |
|---|
| 107 | ///Constructor. |
|---|
| 108 | |
|---|
| 109 | ///\param _G the graph the algorithm will run on. |
|---|
| 110 | Dfs(const Graph& _G) : |
|---|
| 111 | G(&_G), |
|---|
| 112 | predecessor(NULL), local_predecessor(false), |
|---|
| 113 | pred_node(NULL), local_pred_node(false), |
|---|
| 114 | distance(NULL), local_distance(false) |
|---|
| 115 | { } |
|---|
| 116 | |
|---|
| 117 | ///Destructor. |
|---|
| 118 | ~Dfs() |
|---|
| 119 | { |
|---|
| 120 | if(local_predecessor) delete predecessor; |
|---|
| 121 | if(local_pred_node) delete pred_node; |
|---|
| 122 | if(local_distance) delete distance; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | ///Sets the map storing the predecessor edges. |
|---|
| 126 | |
|---|
| 127 | ///Sets the map storing the predecessor edges. |
|---|
| 128 | ///If you don't use this function before calling \ref run(), |
|---|
| 129 | ///it will allocate one. The destuctor deallocates this |
|---|
| 130 | ///automatically allocated map, of course. |
|---|
| 131 | ///\return <tt> (*this) </tt> |
|---|
| 132 | Dfs &setPredMap(PredMap &m) |
|---|
| 133 | { |
|---|
| 134 | if(local_predecessor) { |
|---|
| 135 | delete predecessor; |
|---|
| 136 | local_predecessor=false; |
|---|
| 137 | } |
|---|
| 138 | predecessor = &m; |
|---|
| 139 | return *this; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | ///Sets the map storing the predecessor nodes. |
|---|
| 143 | |
|---|
| 144 | ///Sets the map storing the predecessor nodes. |
|---|
| 145 | ///If you don't use this function before calling \ref run(), |
|---|
| 146 | ///it will allocate one. The destuctor deallocates this |
|---|
| 147 | ///automatically allocated map, of course. |
|---|
| 148 | ///\return <tt> (*this) </tt> |
|---|
| 149 | Dfs &setPredNodeMap(PredNodeMap &m) |
|---|
| 150 | { |
|---|
| 151 | if(local_pred_node) { |
|---|
| 152 | delete pred_node; |
|---|
| 153 | local_pred_node=false; |
|---|
| 154 | } |
|---|
| 155 | pred_node = &m; |
|---|
| 156 | return *this; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | ///Sets the map storing the distances calculated by the algorithm. |
|---|
| 160 | |
|---|
| 161 | ///Sets the map storing the distances calculated by the algorithm. |
|---|
| 162 | ///If you don't use this function before calling \ref run(), |
|---|
| 163 | ///it will allocate one. The destuctor deallocates this |
|---|
| 164 | ///automatically allocated map, of course. |
|---|
| 165 | ///\return <tt> (*this) </tt> |
|---|
| 166 | Dfs &setDistMap(DistMap &m) |
|---|
| 167 | { |
|---|
| 168 | if(local_distance) { |
|---|
| 169 | delete distance; |
|---|
| 170 | local_distance=false; |
|---|
| 171 | } |
|---|
| 172 | distance = &m; |
|---|
| 173 | return *this; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | ///Runs %DFS algorithm from node \c s. |
|---|
| 177 | |
|---|
| 178 | ///This method runs the %DFS algorithm from a root node \c s |
|---|
| 179 | ///in order to |
|---|
| 180 | ///compute |
|---|
| 181 | ///- a %DFS tree and |
|---|
| 182 | ///- the distance of each node from the root on this tree. |
|---|
| 183 | |
|---|
| 184 | void run(Node s) { |
|---|
| 185 | |
|---|
| 186 | init_maps(); |
|---|
| 187 | |
|---|
| 188 | source = s; |
|---|
| 189 | |
|---|
| 190 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
|---|
| 191 | predecessor->set(u,INVALID); |
|---|
| 192 | pred_node->set(u,INVALID); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | int N=G->nodeNum(); |
|---|
| 196 | std::vector<typename Graph::OutEdgeIt> Q(N); |
|---|
| 197 | |
|---|
| 198 | int Qh=0; |
|---|
| 199 | |
|---|
| 200 | G->first(Q[Qh],s); |
|---|
| 201 | distance->set(s, 0); |
|---|
| 202 | |
|---|
| 203 | Node n=s; |
|---|
| 204 | Node m; |
|---|
| 205 | OutEdgeIt e; |
|---|
| 206 | do { |
|---|
| 207 | if((e=Q[Qh])!=INVALID) |
|---|
| 208 | if((m=G->head(e))!=s && (*predecessor)[m=G->head(e)]==INVALID) { |
|---|
| 209 | predecessor->set(m,e); |
|---|
| 210 | pred_node->set(m,n); |
|---|
| 211 | G->first(Q[++Qh],m); |
|---|
| 212 | distance->set(m,Qh); |
|---|
| 213 | n=m; |
|---|
| 214 | } |
|---|
| 215 | else ++Q[Qh]; |
|---|
| 216 | else if(--Qh>=0) n=G->tail(Q[Qh]); |
|---|
| 217 | } while(Qh>=0); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | ///The distance of a node from the root on the %DFS tree. |
|---|
| 221 | |
|---|
| 222 | ///Returns the distance of a node from the root on the %DFS tree. |
|---|
| 223 | ///\pre \ref run() must be called before using this function. |
|---|
| 224 | ///\warning If node \c v in unreachable from the root the return value |
|---|
| 225 | ///of this funcion is undefined. |
|---|
| 226 | int dist(Node v) const { return (*distance)[v]; } |
|---|
| 227 | |
|---|
| 228 | ///Returns the 'previous edge' of the %DFS path tree. |
|---|
| 229 | |
|---|
| 230 | ///For a node \c v it returns the last edge of the path on the %DFS tree |
|---|
| 231 | ///from the root to \c |
|---|
| 232 | ///v. It is \ref INVALID |
|---|
| 233 | ///if \c v is unreachable from the root or if \c v=s. The |
|---|
| 234 | ///%DFS tree used here is equal to the %DFS tree used in |
|---|
| 235 | ///\ref predNode(Node v). \pre \ref run() must be called before using |
|---|
| 236 | ///this function. |
|---|
| 237 | Edge pred(Node v) const { return (*predecessor)[v]; } |
|---|
| 238 | |
|---|
| 239 | ///Returns the 'previous node' of the %DFS tree. |
|---|
| 240 | |
|---|
| 241 | ///For a node \c v it returns the 'previous node' on the %DFS tree, |
|---|
| 242 | ///i.e. it returns the last but one node of the path from the |
|---|
| 243 | ///root to \c /v on the %DFS tree. |
|---|
| 244 | ///It is INVALID if \c v is unreachable from the root or if |
|---|
| 245 | ///\c v=s. |
|---|
| 246 | ///\pre \ref run() must be called before |
|---|
| 247 | ///using this function. |
|---|
| 248 | Node predNode(Node v) const { return (*pred_node)[v]; } |
|---|
| 249 | |
|---|
| 250 | ///Returns a reference to the NodeMap of distances on the %DFS tree. |
|---|
| 251 | |
|---|
| 252 | ///Returns a reference to the NodeMap of distances on the %DFS tree. |
|---|
| 253 | ///\pre \ref run() must |
|---|
| 254 | ///be called before using this function. |
|---|
| 255 | const DistMap &distMap() const { return *distance;} |
|---|
| 256 | |
|---|
| 257 | ///Returns a reference to the %DFS tree map. |
|---|
| 258 | |
|---|
| 259 | ///Returns a reference to the NodeMap of the edges of the |
|---|
| 260 | ///%DFS tree. |
|---|
| 261 | ///\pre \ref run() must be called before using this function. |
|---|
| 262 | const PredMap &predMap() const { return *predecessor;} |
|---|
| 263 | |
|---|
| 264 | ///Returns a reference to the map of last but one nodes of the %DFS tree. |
|---|
| 265 | |
|---|
| 266 | ///Returns a reference to the NodeMap of the last but one nodes of the paths |
|---|
| 267 | ///on the |
|---|
| 268 | ///%DFS tree. |
|---|
| 269 | ///\pre \ref run() must be called before using this function. |
|---|
| 270 | const PredNodeMap &predNodeMap() const { return *pred_node;} |
|---|
| 271 | |
|---|
| 272 | ///Checks if a node is reachable from the root. |
|---|
| 273 | |
|---|
| 274 | ///Returns \c true if \c v is reachable from the root. |
|---|
| 275 | ///\note The root node is reported to be reached! |
|---|
| 276 | /// |
|---|
| 277 | ///\pre \ref run() must be called before using this function. |
|---|
| 278 | /// |
|---|
| 279 | bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; } |
|---|
| 280 | |
|---|
| 281 | }; |
|---|
| 282 | |
|---|
| 283 | /// @} |
|---|
| 284 | |
|---|
| 285 | } //END OF NAMESPACE HUGO |
|---|
| 286 | |
|---|
| 287 | #endif |
|---|
| 288 | |
|---|
| 289 | |
|---|