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