[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[100] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[100] | 4 | * |
---|
[440] | 5 | * Copyright (C) 2003-2009 |
---|
[100] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_BFS_H |
---|
| 20 | #define LEMON_BFS_H |
---|
| 21 | |
---|
| 22 | ///\ingroup search |
---|
| 23 | ///\file |
---|
[244] | 24 | ///\brief BFS algorithm. |
---|
[100] | 25 | |
---|
| 26 | #include <lemon/list_graph.h> |
---|
| 27 | #include <lemon/bits/path_dump.h> |
---|
[220] | 28 | #include <lemon/core.h> |
---|
[100] | 29 | #include <lemon/error.h> |
---|
| 30 | #include <lemon/maps.h> |
---|
[278] | 31 | #include <lemon/path.h> |
---|
[100] | 32 | |
---|
| 33 | namespace lemon { |
---|
| 34 | |
---|
| 35 | ///Default traits class of Bfs class. |
---|
| 36 | |
---|
| 37 | ///Default traits class of Bfs class. |
---|
[157] | 38 | ///\tparam GR Digraph type. |
---|
[100] | 39 | template<class GR> |
---|
| 40 | struct BfsDefaultTraits |
---|
| 41 | { |
---|
[244] | 42 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 43 | typedef GR Digraph; |
---|
[244] | 44 | |
---|
| 45 | ///\brief The type of the map that stores the predecessor |
---|
[100] | 46 | ///arcs of the shortest paths. |
---|
[209] | 47 | /// |
---|
[244] | 48 | ///The type of the map that stores the predecessor |
---|
[100] | 49 | ///arcs of the shortest paths. |
---|
[716] | 50 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[244] | 51 | typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
---|
[492] | 52 | ///Instantiates a \c PredMap. |
---|
[209] | 53 | |
---|
[492] | 54 | ///This function instantiates a \ref PredMap. |
---|
[244] | 55 | ///\param g is the digraph, to which we would like to define the |
---|
[492] | 56 | ///\ref PredMap. |
---|
[244] | 57 | static PredMap *createPredMap(const Digraph &g) |
---|
[100] | 58 | { |
---|
[244] | 59 | return new PredMap(g); |
---|
[100] | 60 | } |
---|
[244] | 61 | |
---|
[100] | 62 | ///The type of the map that indicates which nodes are processed. |
---|
[209] | 63 | |
---|
[100] | 64 | ///The type of the map that indicates which nodes are processed. |
---|
[716] | 65 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[786] | 66 | ///By default, it is a NullMap. |
---|
[100] | 67 | typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
---|
[492] | 68 | ///Instantiates a \c ProcessedMap. |
---|
[209] | 69 | |
---|
[492] | 70 | ///This function instantiates a \ref ProcessedMap. |
---|
[100] | 71 | ///\param g is the digraph, to which |
---|
[492] | 72 | ///we would like to define the \ref ProcessedMap |
---|
[100] | 73 | #ifdef DOXYGEN |
---|
[244] | 74 | static ProcessedMap *createProcessedMap(const Digraph &g) |
---|
[100] | 75 | #else |
---|
[244] | 76 | static ProcessedMap *createProcessedMap(const Digraph &) |
---|
[100] | 77 | #endif |
---|
| 78 | { |
---|
| 79 | return new ProcessedMap(); |
---|
| 80 | } |
---|
[244] | 81 | |
---|
[100] | 82 | ///The type of the map that indicates which nodes are reached. |
---|
[209] | 83 | |
---|
[405] | 84 | ///The type of the map that indicates which nodes are reached. |
---|
[716] | 85 | ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
---|
[100] | 86 | typedef typename Digraph::template NodeMap<bool> ReachedMap; |
---|
[492] | 87 | ///Instantiates a \c ReachedMap. |
---|
[209] | 88 | |
---|
[492] | 89 | ///This function instantiates a \ref ReachedMap. |
---|
[244] | 90 | ///\param g is the digraph, to which |
---|
[492] | 91 | ///we would like to define the \ref ReachedMap. |
---|
[244] | 92 | static ReachedMap *createReachedMap(const Digraph &g) |
---|
[100] | 93 | { |
---|
[244] | 94 | return new ReachedMap(g); |
---|
[100] | 95 | } |
---|
[209] | 96 | |
---|
[244] | 97 | ///The type of the map that stores the distances of the nodes. |
---|
| 98 | |
---|
| 99 | ///The type of the map that stores the distances of the nodes. |
---|
[716] | 100 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[100] | 101 | typedef typename Digraph::template NodeMap<int> DistMap; |
---|
[492] | 102 | ///Instantiates a \c DistMap. |
---|
[209] | 103 | |
---|
[492] | 104 | ///This function instantiates a \ref DistMap. |
---|
[244] | 105 | ///\param g is the digraph, to which we would like to define the |
---|
[492] | 106 | ///\ref DistMap. |
---|
[244] | 107 | static DistMap *createDistMap(const Digraph &g) |
---|
[100] | 108 | { |
---|
[244] | 109 | return new DistMap(g); |
---|
[100] | 110 | } |
---|
| 111 | }; |
---|
[209] | 112 | |
---|
[100] | 113 | ///%BFS algorithm class. |
---|
[209] | 114 | |
---|
[100] | 115 | ///\ingroup search |
---|
| 116 | ///This class provides an efficient implementation of the %BFS algorithm. |
---|
| 117 | /// |
---|
[278] | 118 | ///There is also a \ref bfs() "function-type interface" for the BFS |
---|
[244] | 119 | ///algorithm, which is convenient in the simplier cases and it can be |
---|
| 120 | ///used easier. |
---|
| 121 | /// |
---|
| 122 | ///\tparam GR The type of the digraph the algorithm runs on. |
---|
[405] | 123 | ///The default type is \ref ListDigraph. |
---|
[100] | 124 | #ifdef DOXYGEN |
---|
| 125 | template <typename GR, |
---|
[209] | 126 | typename TR> |
---|
[100] | 127 | #else |
---|
| 128 | template <typename GR=ListDigraph, |
---|
[209] | 129 | typename TR=BfsDefaultTraits<GR> > |
---|
[100] | 130 | #endif |
---|
| 131 | class Bfs { |
---|
| 132 | public: |
---|
| 133 | |
---|
[244] | 134 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 135 | typedef typename TR::Digraph Digraph; |
---|
[209] | 136 | |
---|
[244] | 137 | ///\brief The type of the map that stores the predecessor arcs of the |
---|
| 138 | ///shortest paths. |
---|
[100] | 139 | typedef typename TR::PredMap PredMap; |
---|
[244] | 140 | ///The type of the map that stores the distances of the nodes. |
---|
| 141 | typedef typename TR::DistMap DistMap; |
---|
| 142 | ///The type of the map that indicates which nodes are reached. |
---|
[100] | 143 | typedef typename TR::ReachedMap ReachedMap; |
---|
[244] | 144 | ///The type of the map that indicates which nodes are processed. |
---|
[100] | 145 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
[244] | 146 | ///The type of the paths. |
---|
| 147 | typedef PredMapPath<Digraph, PredMap> Path; |
---|
| 148 | |
---|
[405] | 149 | ///The \ref BfsDefaultTraits "traits class" of the algorithm. |
---|
[244] | 150 | typedef TR Traits; |
---|
| 151 | |
---|
[100] | 152 | private: |
---|
| 153 | |
---|
| 154 | typedef typename Digraph::Node Node; |
---|
| 155 | typedef typename Digraph::NodeIt NodeIt; |
---|
| 156 | typedef typename Digraph::Arc Arc; |
---|
| 157 | typedef typename Digraph::OutArcIt OutArcIt; |
---|
| 158 | |
---|
[244] | 159 | //Pointer to the underlying digraph. |
---|
[100] | 160 | const Digraph *G; |
---|
[244] | 161 | //Pointer to the map of predecessor arcs. |
---|
[100] | 162 | PredMap *_pred; |
---|
[244] | 163 | //Indicates if _pred is locally allocated (true) or not. |
---|
[100] | 164 | bool local_pred; |
---|
[244] | 165 | //Pointer to the map of distances. |
---|
[100] | 166 | DistMap *_dist; |
---|
[244] | 167 | //Indicates if _dist is locally allocated (true) or not. |
---|
[100] | 168 | bool local_dist; |
---|
[244] | 169 | //Pointer to the map of reached status of the nodes. |
---|
[100] | 170 | ReachedMap *_reached; |
---|
[244] | 171 | //Indicates if _reached is locally allocated (true) or not. |
---|
[100] | 172 | bool local_reached; |
---|
[244] | 173 | //Pointer to the map of processed status of the nodes. |
---|
[100] | 174 | ProcessedMap *_processed; |
---|
[244] | 175 | //Indicates if _processed is locally allocated (true) or not. |
---|
[100] | 176 | bool local_processed; |
---|
| 177 | |
---|
| 178 | std::vector<typename Digraph::Node> _queue; |
---|
| 179 | int _queue_head,_queue_tail,_queue_next_dist; |
---|
| 180 | int _curr_dist; |
---|
| 181 | |
---|
[280] | 182 | //Creates the maps if necessary. |
---|
[209] | 183 | void create_maps() |
---|
[100] | 184 | { |
---|
| 185 | if(!_pred) { |
---|
[209] | 186 | local_pred = true; |
---|
| 187 | _pred = Traits::createPredMap(*G); |
---|
[100] | 188 | } |
---|
| 189 | if(!_dist) { |
---|
[209] | 190 | local_dist = true; |
---|
| 191 | _dist = Traits::createDistMap(*G); |
---|
[100] | 192 | } |
---|
| 193 | if(!_reached) { |
---|
[209] | 194 | local_reached = true; |
---|
| 195 | _reached = Traits::createReachedMap(*G); |
---|
[100] | 196 | } |
---|
| 197 | if(!_processed) { |
---|
[209] | 198 | local_processed = true; |
---|
| 199 | _processed = Traits::createProcessedMap(*G); |
---|
[100] | 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | protected: |
---|
[209] | 204 | |
---|
[100] | 205 | Bfs() {} |
---|
[209] | 206 | |
---|
[100] | 207 | public: |
---|
[209] | 208 | |
---|
[100] | 209 | typedef Bfs Create; |
---|
| 210 | |
---|
[405] | 211 | ///\name Named Template Parameters |
---|
[100] | 212 | |
---|
| 213 | ///@{ |
---|
| 214 | |
---|
| 215 | template <class T> |
---|
[257] | 216 | struct SetPredMapTraits : public Traits { |
---|
[100] | 217 | typedef T PredMap; |
---|
[209] | 218 | static PredMap *createPredMap(const Digraph &) |
---|
[100] | 219 | { |
---|
[290] | 220 | LEMON_ASSERT(false, "PredMap is not initialized"); |
---|
| 221 | return 0; // ignore warnings |
---|
[100] | 222 | } |
---|
| 223 | }; |
---|
| 224 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
[492] | 225 | ///\c PredMap type. |
---|
[100] | 226 | /// |
---|
[244] | 227 | ///\ref named-templ-param "Named parameter" for setting |
---|
[492] | 228 | ///\c PredMap type. |
---|
[716] | 229 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[100] | 230 | template <class T> |
---|
[257] | 231 | struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
---|
| 232 | typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
---|
[100] | 233 | }; |
---|
[209] | 234 | |
---|
[100] | 235 | template <class T> |
---|
[257] | 236 | struct SetDistMapTraits : public Traits { |
---|
[100] | 237 | typedef T DistMap; |
---|
[209] | 238 | static DistMap *createDistMap(const Digraph &) |
---|
[100] | 239 | { |
---|
[290] | 240 | LEMON_ASSERT(false, "DistMap is not initialized"); |
---|
| 241 | return 0; // ignore warnings |
---|
[100] | 242 | } |
---|
| 243 | }; |
---|
| 244 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
[492] | 245 | ///\c DistMap type. |
---|
[100] | 246 | /// |
---|
[244] | 247 | ///\ref named-templ-param "Named parameter" for setting |
---|
[492] | 248 | ///\c DistMap type. |
---|
[716] | 249 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[100] | 250 | template <class T> |
---|
[257] | 251 | struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
---|
| 252 | typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
---|
[100] | 253 | }; |
---|
[209] | 254 | |
---|
[100] | 255 | template <class T> |
---|
[257] | 256 | struct SetReachedMapTraits : public Traits { |
---|
[100] | 257 | typedef T ReachedMap; |
---|
[209] | 258 | static ReachedMap *createReachedMap(const Digraph &) |
---|
[100] | 259 | { |
---|
[290] | 260 | LEMON_ASSERT(false, "ReachedMap is not initialized"); |
---|
| 261 | return 0; // ignore warnings |
---|
[100] | 262 | } |
---|
| 263 | }; |
---|
| 264 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
[492] | 265 | ///\c ReachedMap type. |
---|
[100] | 266 | /// |
---|
[244] | 267 | ///\ref named-templ-param "Named parameter" for setting |
---|
[492] | 268 | ///\c ReachedMap type. |
---|
[716] | 269 | ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
---|
[100] | 270 | template <class T> |
---|
[257] | 271 | struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
---|
| 272 | typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
---|
[100] | 273 | }; |
---|
[209] | 274 | |
---|
[100] | 275 | template <class T> |
---|
[257] | 276 | struct SetProcessedMapTraits : public Traits { |
---|
[100] | 277 | typedef T ProcessedMap; |
---|
[209] | 278 | static ProcessedMap *createProcessedMap(const Digraph &) |
---|
[100] | 279 | { |
---|
[290] | 280 | LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
---|
| 281 | return 0; // ignore warnings |
---|
[100] | 282 | } |
---|
| 283 | }; |
---|
| 284 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
[492] | 285 | ///\c ProcessedMap type. |
---|
[100] | 286 | /// |
---|
[244] | 287 | ///\ref named-templ-param "Named parameter" for setting |
---|
[492] | 288 | ///\c ProcessedMap type. |
---|
[716] | 289 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[100] | 290 | template <class T> |
---|
[257] | 291 | struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
---|
| 292 | typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
---|
[100] | 293 | }; |
---|
[209] | 294 | |
---|
[257] | 295 | struct SetStandardProcessedMapTraits : public Traits { |
---|
[100] | 296 | typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
---|
[244] | 297 | static ProcessedMap *createProcessedMap(const Digraph &g) |
---|
[100] | 298 | { |
---|
[244] | 299 | return new ProcessedMap(g); |
---|
[290] | 300 | return 0; // ignore warnings |
---|
[100] | 301 | } |
---|
| 302 | }; |
---|
[244] | 303 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
[492] | 304 | ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
---|
[100] | 305 | /// |
---|
[244] | 306 | ///\ref named-templ-param "Named parameter" for setting |
---|
[492] | 307 | ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
---|
[100] | 308 | ///If you don't set it explicitly, it will be automatically allocated. |
---|
[257] | 309 | struct SetStandardProcessedMap : |
---|
| 310 | public Bfs< Digraph, SetStandardProcessedMapTraits > { |
---|
| 311 | typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
---|
[100] | 312 | }; |
---|
[209] | 313 | |
---|
[100] | 314 | ///@} |
---|
| 315 | |
---|
[209] | 316 | public: |
---|
| 317 | |
---|
[100] | 318 | ///Constructor. |
---|
[209] | 319 | |
---|
[244] | 320 | ///Constructor. |
---|
| 321 | ///\param g The digraph the algorithm runs on. |
---|
| 322 | Bfs(const Digraph &g) : |
---|
| 323 | G(&g), |
---|
[100] | 324 | _pred(NULL), local_pred(false), |
---|
| 325 | _dist(NULL), local_dist(false), |
---|
| 326 | _reached(NULL), local_reached(false), |
---|
| 327 | _processed(NULL), local_processed(false) |
---|
| 328 | { } |
---|
[209] | 329 | |
---|
[100] | 330 | ///Destructor. |
---|
[209] | 331 | ~Bfs() |
---|
[100] | 332 | { |
---|
| 333 | if(local_pred) delete _pred; |
---|
| 334 | if(local_dist) delete _dist; |
---|
| 335 | if(local_reached) delete _reached; |
---|
| 336 | if(local_processed) delete _processed; |
---|
| 337 | } |
---|
| 338 | |
---|
[244] | 339 | ///Sets the map that stores the predecessor arcs. |
---|
[100] | 340 | |
---|
[244] | 341 | ///Sets the map that stores the predecessor arcs. |
---|
[405] | 342 | ///If you don't use this function before calling \ref run(Node) "run()" |
---|
| 343 | ///or \ref init(), an instance will be allocated automatically. |
---|
| 344 | ///The destructor deallocates this automatically allocated map, |
---|
| 345 | ///of course. |
---|
[100] | 346 | ///\return <tt> (*this) </tt> |
---|
[209] | 347 | Bfs &predMap(PredMap &m) |
---|
[100] | 348 | { |
---|
| 349 | if(local_pred) { |
---|
[209] | 350 | delete _pred; |
---|
| 351 | local_pred=false; |
---|
[100] | 352 | } |
---|
| 353 | _pred = &m; |
---|
| 354 | return *this; |
---|
| 355 | } |
---|
| 356 | |
---|
[244] | 357 | ///Sets the map that indicates which nodes are reached. |
---|
[100] | 358 | |
---|
[244] | 359 | ///Sets the map that indicates which nodes are reached. |
---|
[405] | 360 | ///If you don't use this function before calling \ref run(Node) "run()" |
---|
| 361 | ///or \ref init(), an instance will be allocated automatically. |
---|
| 362 | ///The destructor deallocates this automatically allocated map, |
---|
| 363 | ///of course. |
---|
[100] | 364 | ///\return <tt> (*this) </tt> |
---|
[209] | 365 | Bfs &reachedMap(ReachedMap &m) |
---|
[100] | 366 | { |
---|
| 367 | if(local_reached) { |
---|
[209] | 368 | delete _reached; |
---|
| 369 | local_reached=false; |
---|
[100] | 370 | } |
---|
| 371 | _reached = &m; |
---|
| 372 | return *this; |
---|
| 373 | } |
---|
| 374 | |
---|
[244] | 375 | ///Sets the map that indicates which nodes are processed. |
---|
[100] | 376 | |
---|
[244] | 377 | ///Sets the map that indicates which nodes are processed. |
---|
[405] | 378 | ///If you don't use this function before calling \ref run(Node) "run()" |
---|
| 379 | ///or \ref init(), an instance will be allocated automatically. |
---|
| 380 | ///The destructor deallocates this automatically allocated map, |
---|
| 381 | ///of course. |
---|
[100] | 382 | ///\return <tt> (*this) </tt> |
---|
[209] | 383 | Bfs &processedMap(ProcessedMap &m) |
---|
[100] | 384 | { |
---|
| 385 | if(local_processed) { |
---|
[209] | 386 | delete _processed; |
---|
| 387 | local_processed=false; |
---|
[100] | 388 | } |
---|
| 389 | _processed = &m; |
---|
| 390 | return *this; |
---|
| 391 | } |
---|
| 392 | |
---|
[244] | 393 | ///Sets the map that stores the distances of the nodes. |
---|
[100] | 394 | |
---|
[244] | 395 | ///Sets the map that stores the distances of the nodes calculated by |
---|
| 396 | ///the algorithm. |
---|
[405] | 397 | ///If you don't use this function before calling \ref run(Node) "run()" |
---|
| 398 | ///or \ref init(), an instance will be allocated automatically. |
---|
| 399 | ///The destructor deallocates this automatically allocated map, |
---|
| 400 | ///of course. |
---|
[100] | 401 | ///\return <tt> (*this) </tt> |
---|
[209] | 402 | Bfs &distMap(DistMap &m) |
---|
[100] | 403 | { |
---|
| 404 | if(local_dist) { |
---|
[209] | 405 | delete _dist; |
---|
| 406 | local_dist=false; |
---|
[100] | 407 | } |
---|
| 408 | _dist = &m; |
---|
| 409 | return *this; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | public: |
---|
[244] | 413 | |
---|
[405] | 414 | ///\name Execution Control |
---|
| 415 | ///The simplest way to execute the BFS algorithm is to use one of the |
---|
| 416 | ///member functions called \ref run(Node) "run()".\n |
---|
[713] | 417 | ///If you need better control on the execution, you have to call |
---|
| 418 | ///\ref init() first, then you can add several source nodes with |
---|
[405] | 419 | ///\ref addSource(). Finally the actual path computation can be |
---|
| 420 | ///performed with one of the \ref start() functions. |
---|
[100] | 421 | |
---|
| 422 | ///@{ |
---|
| 423 | |
---|
[405] | 424 | ///\brief Initializes the internal data structures. |
---|
| 425 | /// |
---|
[244] | 426 | ///Initializes the internal data structures. |
---|
[100] | 427 | void init() |
---|
| 428 | { |
---|
| 429 | create_maps(); |
---|
| 430 | _queue.resize(countNodes(*G)); |
---|
| 431 | _queue_head=_queue_tail=0; |
---|
| 432 | _curr_dist=1; |
---|
| 433 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
[209] | 434 | _pred->set(u,INVALID); |
---|
| 435 | _reached->set(u,false); |
---|
| 436 | _processed->set(u,false); |
---|
[100] | 437 | } |
---|
| 438 | } |
---|
[209] | 439 | |
---|
[100] | 440 | ///Adds a new source node. |
---|
| 441 | |
---|
| 442 | ///Adds a new source node to the set of nodes to be processed. |
---|
| 443 | /// |
---|
| 444 | void addSource(Node s) |
---|
| 445 | { |
---|
| 446 | if(!(*_reached)[s]) |
---|
[209] | 447 | { |
---|
| 448 | _reached->set(s,true); |
---|
| 449 | _pred->set(s,INVALID); |
---|
| 450 | _dist->set(s,0); |
---|
| 451 | _queue[_queue_head++]=s; |
---|
| 452 | _queue_next_dist=_queue_head; |
---|
| 453 | } |
---|
[100] | 454 | } |
---|
[209] | 455 | |
---|
[100] | 456 | ///Processes the next node. |
---|
| 457 | |
---|
| 458 | ///Processes the next node. |
---|
| 459 | /// |
---|
| 460 | ///\return The processed node. |
---|
| 461 | /// |
---|
[244] | 462 | ///\pre The queue must not be empty. |
---|
[100] | 463 | Node processNextNode() |
---|
| 464 | { |
---|
| 465 | if(_queue_tail==_queue_next_dist) { |
---|
[209] | 466 | _curr_dist++; |
---|
| 467 | _queue_next_dist=_queue_head; |
---|
[100] | 468 | } |
---|
| 469 | Node n=_queue[_queue_tail++]; |
---|
| 470 | _processed->set(n,true); |
---|
| 471 | Node m; |
---|
| 472 | for(OutArcIt e(*G,n);e!=INVALID;++e) |
---|
[209] | 473 | if(!(*_reached)[m=G->target(e)]) { |
---|
| 474 | _queue[_queue_head++]=m; |
---|
| 475 | _reached->set(m,true); |
---|
| 476 | _pred->set(m,e); |
---|
| 477 | _dist->set(m,_curr_dist); |
---|
| 478 | } |
---|
[100] | 479 | return n; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | ///Processes the next node. |
---|
| 483 | |
---|
[244] | 484 | ///Processes the next node and checks if the given target node |
---|
[100] | 485 | ///is reached. If the target node is reachable from the processed |
---|
[244] | 486 | ///node, then the \c reach parameter will be set to \c true. |
---|
[100] | 487 | /// |
---|
| 488 | ///\param target The target node. |
---|
[244] | 489 | ///\retval reach Indicates if the target node is reached. |
---|
| 490 | ///It should be initially \c false. |
---|
| 491 | /// |
---|
[100] | 492 | ///\return The processed node. |
---|
| 493 | /// |
---|
[244] | 494 | ///\pre The queue must not be empty. |
---|
[100] | 495 | Node processNextNode(Node target, bool& reach) |
---|
| 496 | { |
---|
| 497 | if(_queue_tail==_queue_next_dist) { |
---|
[209] | 498 | _curr_dist++; |
---|
| 499 | _queue_next_dist=_queue_head; |
---|
[100] | 500 | } |
---|
| 501 | Node n=_queue[_queue_tail++]; |
---|
| 502 | _processed->set(n,true); |
---|
| 503 | Node m; |
---|
| 504 | for(OutArcIt e(*G,n);e!=INVALID;++e) |
---|
[209] | 505 | if(!(*_reached)[m=G->target(e)]) { |
---|
| 506 | _queue[_queue_head++]=m; |
---|
| 507 | _reached->set(m,true); |
---|
| 508 | _pred->set(m,e); |
---|
| 509 | _dist->set(m,_curr_dist); |
---|
[100] | 510 | reach = reach || (target == m); |
---|
[209] | 511 | } |
---|
[100] | 512 | return n; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | ///Processes the next node. |
---|
| 516 | |
---|
[244] | 517 | ///Processes the next node and checks if at least one of reached |
---|
| 518 | ///nodes has \c true value in the \c nm node map. If one node |
---|
| 519 | ///with \c true value is reachable from the processed node, then the |
---|
| 520 | ///\c rnode parameter will be set to the first of such nodes. |
---|
[100] | 521 | /// |
---|
[244] | 522 | ///\param nm A \c bool (or convertible) node map that indicates the |
---|
| 523 | ///possible targets. |
---|
[100] | 524 | ///\retval rnode The reached target node. |
---|
[244] | 525 | ///It should be initially \c INVALID. |
---|
| 526 | /// |
---|
[100] | 527 | ///\return The processed node. |
---|
| 528 | /// |
---|
[244] | 529 | ///\pre The queue must not be empty. |
---|
[100] | 530 | template<class NM> |
---|
| 531 | Node processNextNode(const NM& nm, Node& rnode) |
---|
| 532 | { |
---|
| 533 | if(_queue_tail==_queue_next_dist) { |
---|
[209] | 534 | _curr_dist++; |
---|
| 535 | _queue_next_dist=_queue_head; |
---|
[100] | 536 | } |
---|
| 537 | Node n=_queue[_queue_tail++]; |
---|
| 538 | _processed->set(n,true); |
---|
| 539 | Node m; |
---|
| 540 | for(OutArcIt e(*G,n);e!=INVALID;++e) |
---|
[209] | 541 | if(!(*_reached)[m=G->target(e)]) { |
---|
| 542 | _queue[_queue_head++]=m; |
---|
| 543 | _reached->set(m,true); |
---|
| 544 | _pred->set(m,e); |
---|
| 545 | _dist->set(m,_curr_dist); |
---|
| 546 | if (nm[m] && rnode == INVALID) rnode = m; |
---|
| 547 | } |
---|
[100] | 548 | return n; |
---|
| 549 | } |
---|
[209] | 550 | |
---|
[244] | 551 | ///The next node to be processed. |
---|
[100] | 552 | |
---|
[244] | 553 | ///Returns the next node to be processed or \c INVALID if the queue |
---|
| 554 | ///is empty. |
---|
| 555 | Node nextNode() const |
---|
[209] | 556 | { |
---|
[100] | 557 | return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
---|
| 558 | } |
---|
[209] | 559 | |
---|
[405] | 560 | ///Returns \c false if there are nodes to be processed. |
---|
| 561 | |
---|
| 562 | ///Returns \c false if there are nodes to be processed |
---|
| 563 | ///in the queue. |
---|
[244] | 564 | bool emptyQueue() const { return _queue_tail==_queue_head; } |
---|
| 565 | |
---|
[100] | 566 | ///Returns the number of the nodes to be processed. |
---|
[209] | 567 | |
---|
[405] | 568 | ///Returns the number of the nodes to be processed |
---|
| 569 | ///in the queue. |
---|
[244] | 570 | int queueSize() const { return _queue_head-_queue_tail; } |
---|
[209] | 571 | |
---|
[100] | 572 | ///Executes the algorithm. |
---|
| 573 | |
---|
| 574 | ///Executes the algorithm. |
---|
| 575 | /// |
---|
[244] | 576 | ///This method runs the %BFS algorithm from the root node(s) |
---|
| 577 | ///in order to compute the shortest path to each node. |
---|
[100] | 578 | /// |
---|
[244] | 579 | ///The algorithm computes |
---|
| 580 | ///- the shortest path tree (forest), |
---|
| 581 | ///- the distance of each node from the root(s). |
---|
| 582 | /// |
---|
| 583 | ///\pre init() must be called and at least one root node should be |
---|
| 584 | ///added with addSource() before using this function. |
---|
| 585 | /// |
---|
| 586 | ///\note <tt>b.start()</tt> is just a shortcut of the following code. |
---|
| 587 | ///\code |
---|
| 588 | /// while ( !b.emptyQueue() ) { |
---|
| 589 | /// b.processNextNode(); |
---|
| 590 | /// } |
---|
| 591 | ///\endcode |
---|
[100] | 592 | void start() |
---|
| 593 | { |
---|
| 594 | while ( !emptyQueue() ) processNextNode(); |
---|
| 595 | } |
---|
[209] | 596 | |
---|
[244] | 597 | ///Executes the algorithm until the given target node is reached. |
---|
[100] | 598 | |
---|
[244] | 599 | ///Executes the algorithm until the given target node is reached. |
---|
[100] | 600 | /// |
---|
| 601 | ///This method runs the %BFS algorithm from the root node(s) |
---|
[286] | 602 | ///in order to compute the shortest path to \c t. |
---|
[244] | 603 | /// |
---|
[100] | 604 | ///The algorithm computes |
---|
[286] | 605 | ///- the shortest path to \c t, |
---|
| 606 | ///- the distance of \c t from the root(s). |
---|
[244] | 607 | /// |
---|
| 608 | ///\pre init() must be called and at least one root node should be |
---|
| 609 | ///added with addSource() before using this function. |
---|
| 610 | /// |
---|
| 611 | ///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
---|
| 612 | ///\code |
---|
| 613 | /// bool reach = false; |
---|
| 614 | /// while ( !b.emptyQueue() && !reach ) { |
---|
| 615 | /// b.processNextNode(t, reach); |
---|
| 616 | /// } |
---|
| 617 | ///\endcode |
---|
[286] | 618 | void start(Node t) |
---|
[100] | 619 | { |
---|
| 620 | bool reach = false; |
---|
[286] | 621 | while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
---|
[100] | 622 | } |
---|
[209] | 623 | |
---|
[100] | 624 | ///Executes the algorithm until a condition is met. |
---|
| 625 | |
---|
| 626 | ///Executes the algorithm until a condition is met. |
---|
| 627 | /// |
---|
[244] | 628 | ///This method runs the %BFS algorithm from the root node(s) in |
---|
| 629 | ///order to compute the shortest path to a node \c v with |
---|
| 630 | /// <tt>nm[v]</tt> true, if such a node can be found. |
---|
[100] | 631 | /// |
---|
[244] | 632 | ///\param nm A \c bool (or convertible) node map. The algorithm |
---|
| 633 | ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
---|
[100] | 634 | /// |
---|
| 635 | ///\return The reached node \c v with <tt>nm[v]</tt> true or |
---|
| 636 | ///\c INVALID if no such node was found. |
---|
[244] | 637 | /// |
---|
| 638 | ///\pre init() must be called and at least one root node should be |
---|
| 639 | ///added with addSource() before using this function. |
---|
| 640 | /// |
---|
| 641 | ///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
---|
| 642 | ///\code |
---|
| 643 | /// Node rnode = INVALID; |
---|
| 644 | /// while ( !b.emptyQueue() && rnode == INVALID ) { |
---|
| 645 | /// b.processNextNode(nm, rnode); |
---|
| 646 | /// } |
---|
| 647 | /// return rnode; |
---|
| 648 | ///\endcode |
---|
| 649 | template<class NodeBoolMap> |
---|
| 650 | Node start(const NodeBoolMap &nm) |
---|
[100] | 651 | { |
---|
| 652 | Node rnode = INVALID; |
---|
| 653 | while ( !emptyQueue() && rnode == INVALID ) { |
---|
[209] | 654 | processNextNode(nm, rnode); |
---|
[100] | 655 | } |
---|
| 656 | return rnode; |
---|
| 657 | } |
---|
[209] | 658 | |
---|
[286] | 659 | ///Runs the algorithm from the given source node. |
---|
[209] | 660 | |
---|
[244] | 661 | ///This method runs the %BFS algorithm from node \c s |
---|
| 662 | ///in order to compute the shortest path to each node. |
---|
[100] | 663 | /// |
---|
[244] | 664 | ///The algorithm computes |
---|
| 665 | ///- the shortest path tree, |
---|
| 666 | ///- the distance of each node from the root. |
---|
| 667 | /// |
---|
| 668 | ///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
---|
[100] | 669 | ///\code |
---|
| 670 | /// b.init(); |
---|
| 671 | /// b.addSource(s); |
---|
| 672 | /// b.start(); |
---|
| 673 | ///\endcode |
---|
| 674 | void run(Node s) { |
---|
| 675 | init(); |
---|
| 676 | addSource(s); |
---|
| 677 | start(); |
---|
| 678 | } |
---|
[209] | 679 | |
---|
[100] | 680 | ///Finds the shortest path between \c s and \c t. |
---|
[209] | 681 | |
---|
[244] | 682 | ///This method runs the %BFS algorithm from node \c s |
---|
[286] | 683 | ///in order to compute the shortest path to node \c t |
---|
| 684 | ///(it stops searching when \c t is processed). |
---|
[100] | 685 | /// |
---|
[286] | 686 | ///\return \c true if \c t is reachable form \c s. |
---|
[244] | 687 | /// |
---|
| 688 | ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
---|
| 689 | ///shortcut of the following code. |
---|
[100] | 690 | ///\code |
---|
| 691 | /// b.init(); |
---|
| 692 | /// b.addSource(s); |
---|
| 693 | /// b.start(t); |
---|
| 694 | ///\endcode |
---|
[286] | 695 | bool run(Node s,Node t) { |
---|
[100] | 696 | init(); |
---|
| 697 | addSource(s); |
---|
| 698 | start(t); |
---|
[286] | 699 | return reached(t); |
---|
[100] | 700 | } |
---|
[209] | 701 | |
---|
[244] | 702 | ///Runs the algorithm to visit all nodes in the digraph. |
---|
| 703 | |
---|
[787] | 704 | ///This method runs the %BFS algorithm in order to visit all nodes |
---|
| 705 | ///in the digraph. |
---|
[244] | 706 | /// |
---|
| 707 | ///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
---|
| 708 | ///\code |
---|
| 709 | /// b.init(); |
---|
| 710 | /// for (NodeIt n(gr); n != INVALID; ++n) { |
---|
| 711 | /// if (!b.reached(n)) { |
---|
| 712 | /// b.addSource(n); |
---|
| 713 | /// b.start(); |
---|
| 714 | /// } |
---|
| 715 | /// } |
---|
| 716 | ///\endcode |
---|
| 717 | void run() { |
---|
| 718 | init(); |
---|
| 719 | for (NodeIt n(*G); n != INVALID; ++n) { |
---|
| 720 | if (!reached(n)) { |
---|
| 721 | addSource(n); |
---|
| 722 | start(); |
---|
| 723 | } |
---|
| 724 | } |
---|
| 725 | } |
---|
| 726 | |
---|
[100] | 727 | ///@} |
---|
| 728 | |
---|
| 729 | ///\name Query Functions |
---|
[405] | 730 | ///The results of the BFS algorithm can be obtained using these |
---|
[100] | 731 | ///functions.\n |
---|
[405] | 732 | ///Either \ref run(Node) "run()" or \ref start() should be called |
---|
| 733 | ///before using them. |
---|
[209] | 734 | |
---|
[100] | 735 | ///@{ |
---|
| 736 | |
---|
[716] | 737 | ///The shortest path to the given node. |
---|
[100] | 738 | |
---|
[716] | 739 | ///Returns the shortest path to the given node from the root(s). |
---|
[244] | 740 | /// |
---|
[405] | 741 | ///\warning \c t should be reached from the root(s). |
---|
[244] | 742 | /// |
---|
[405] | 743 | ///\pre Either \ref run(Node) "run()" or \ref init() |
---|
| 744 | ///must be called before using this function. |
---|
[244] | 745 | Path path(Node t) const { return Path(*G, *_pred, t); } |
---|
[100] | 746 | |
---|
[716] | 747 | ///The distance of the given node from the root(s). |
---|
[100] | 748 | |
---|
[716] | 749 | ///Returns the distance of the given node from the root(s). |
---|
[244] | 750 | /// |
---|
[405] | 751 | ///\warning If node \c v is not reached from the root(s), then |
---|
[244] | 752 | ///the return value of this function is undefined. |
---|
| 753 | /// |
---|
[405] | 754 | ///\pre Either \ref run(Node) "run()" or \ref init() |
---|
| 755 | ///must be called before using this function. |
---|
[100] | 756 | int dist(Node v) const { return (*_dist)[v]; } |
---|
| 757 | |
---|
[716] | 758 | ///\brief Returns the 'previous arc' of the shortest path tree for |
---|
| 759 | ///the given node. |
---|
| 760 | /// |
---|
[244] | 761 | ///This function returns the 'previous arc' of the shortest path |
---|
| 762 | ///tree for the node \c v, i.e. it returns the last arc of a |
---|
[405] | 763 | ///shortest path from a root to \c v. It is \c INVALID if \c v |
---|
| 764 | ///is not reached from the root(s) or if \c v is a root. |
---|
[244] | 765 | /// |
---|
| 766 | ///The shortest path tree used here is equal to the shortest path |
---|
[716] | 767 | ///tree used in \ref predNode() and \ref predMap(). |
---|
[244] | 768 | /// |
---|
[405] | 769 | ///\pre Either \ref run(Node) "run()" or \ref init() |
---|
| 770 | ///must be called before using this function. |
---|
[100] | 771 | Arc predArc(Node v) const { return (*_pred)[v];} |
---|
| 772 | |
---|
[716] | 773 | ///\brief Returns the 'previous node' of the shortest path tree for |
---|
| 774 | ///the given node. |
---|
| 775 | /// |
---|
[244] | 776 | ///This function returns the 'previous node' of the shortest path |
---|
| 777 | ///tree for the node \c v, i.e. it returns the last but one node |
---|
[716] | 778 | ///of a shortest path from a root to \c v. It is \c INVALID |
---|
[405] | 779 | ///if \c v is not reached from the root(s) or if \c v is a root. |
---|
[244] | 780 | /// |
---|
[100] | 781 | ///The shortest path tree used here is equal to the shortest path |
---|
[716] | 782 | ///tree used in \ref predArc() and \ref predMap(). |
---|
[244] | 783 | /// |
---|
[405] | 784 | ///\pre Either \ref run(Node) "run()" or \ref init() |
---|
| 785 | ///must be called before using this function. |
---|
[100] | 786 | Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
---|
[209] | 787 | G->source((*_pred)[v]); } |
---|
| 788 | |
---|
[244] | 789 | ///\brief Returns a const reference to the node map that stores the |
---|
| 790 | /// distances of the nodes. |
---|
| 791 | /// |
---|
| 792 | ///Returns a const reference to the node map that stores the distances |
---|
| 793 | ///of the nodes calculated by the algorithm. |
---|
| 794 | /// |
---|
[405] | 795 | ///\pre Either \ref run(Node) "run()" or \ref init() |
---|
[244] | 796 | ///must be called before using this function. |
---|
[100] | 797 | const DistMap &distMap() const { return *_dist;} |
---|
[209] | 798 | |
---|
[244] | 799 | ///\brief Returns a const reference to the node map that stores the |
---|
| 800 | ///predecessor arcs. |
---|
| 801 | /// |
---|
| 802 | ///Returns a const reference to the node map that stores the predecessor |
---|
[716] | 803 | ///arcs, which form the shortest path tree (forest). |
---|
[244] | 804 | /// |
---|
[405] | 805 | ///\pre Either \ref run(Node) "run()" or \ref init() |
---|
[100] | 806 | ///must be called before using this function. |
---|
| 807 | const PredMap &predMap() const { return *_pred;} |
---|
[209] | 808 | |
---|
[716] | 809 | ///Checks if the given node is reached from the root(s). |
---|
[100] | 810 | |
---|
[405] | 811 | ///Returns \c true if \c v is reached from the root(s). |
---|
| 812 | /// |
---|
| 813 | ///\pre Either \ref run(Node) "run()" or \ref init() |
---|
[100] | 814 | ///must be called before using this function. |
---|
[244] | 815 | bool reached(Node v) const { return (*_reached)[v]; } |
---|
[209] | 816 | |
---|
[100] | 817 | ///@} |
---|
| 818 | }; |
---|
| 819 | |
---|
[244] | 820 | ///Default traits class of bfs() function. |
---|
[100] | 821 | |
---|
[244] | 822 | ///Default traits class of bfs() function. |
---|
[157] | 823 | ///\tparam GR Digraph type. |
---|
[100] | 824 | template<class GR> |
---|
| 825 | struct BfsWizardDefaultTraits |
---|
| 826 | { |
---|
[244] | 827 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 828 | typedef GR Digraph; |
---|
[244] | 829 | |
---|
| 830 | ///\brief The type of the map that stores the predecessor |
---|
[100] | 831 | ///arcs of the shortest paths. |
---|
[209] | 832 | /// |
---|
[244] | 833 | ///The type of the map that stores the predecessor |
---|
[100] | 834 | ///arcs of the shortest paths. |
---|
[716] | 835 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[278] | 836 | typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
---|
[301] | 837 | ///Instantiates a PredMap. |
---|
[209] | 838 | |
---|
[301] | 839 | ///This function instantiates a PredMap. |
---|
[244] | 840 | ///\param g is the digraph, to which we would like to define the |
---|
[301] | 841 | ///PredMap. |
---|
[244] | 842 | static PredMap *createPredMap(const Digraph &g) |
---|
[100] | 843 | { |
---|
[278] | 844 | return new PredMap(g); |
---|
[100] | 845 | } |
---|
| 846 | |
---|
| 847 | ///The type of the map that indicates which nodes are processed. |
---|
[209] | 848 | |
---|
[100] | 849 | ///The type of the map that indicates which nodes are processed. |
---|
[716] | 850 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[786] | 851 | ///By default, it is a NullMap. |
---|
[100] | 852 | typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
---|
[301] | 853 | ///Instantiates a ProcessedMap. |
---|
[209] | 854 | |
---|
[301] | 855 | ///This function instantiates a ProcessedMap. |
---|
[100] | 856 | ///\param g is the digraph, to which |
---|
[301] | 857 | ///we would like to define the ProcessedMap. |
---|
[100] | 858 | #ifdef DOXYGEN |
---|
[244] | 859 | static ProcessedMap *createProcessedMap(const Digraph &g) |
---|
[100] | 860 | #else |
---|
[244] | 861 | static ProcessedMap *createProcessedMap(const Digraph &) |
---|
[100] | 862 | #endif |
---|
| 863 | { |
---|
| 864 | return new ProcessedMap(); |
---|
| 865 | } |
---|
[244] | 866 | |
---|
[100] | 867 | ///The type of the map that indicates which nodes are reached. |
---|
[209] | 868 | |
---|
[100] | 869 | ///The type of the map that indicates which nodes are reached. |
---|
[716] | 870 | ///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
---|
[100] | 871 | typedef typename Digraph::template NodeMap<bool> ReachedMap; |
---|
[301] | 872 | ///Instantiates a ReachedMap. |
---|
[209] | 873 | |
---|
[301] | 874 | ///This function instantiates a ReachedMap. |
---|
[244] | 875 | ///\param g is the digraph, to which |
---|
[301] | 876 | ///we would like to define the ReachedMap. |
---|
[244] | 877 | static ReachedMap *createReachedMap(const Digraph &g) |
---|
[100] | 878 | { |
---|
[244] | 879 | return new ReachedMap(g); |
---|
[100] | 880 | } |
---|
[209] | 881 | |
---|
[244] | 882 | ///The type of the map that stores the distances of the nodes. |
---|
| 883 | |
---|
| 884 | ///The type of the map that stores the distances of the nodes. |
---|
[716] | 885 | ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
---|
[278] | 886 | typedef typename Digraph::template NodeMap<int> DistMap; |
---|
[301] | 887 | ///Instantiates a DistMap. |
---|
[209] | 888 | |
---|
[301] | 889 | ///This function instantiates a DistMap. |
---|
[210] | 890 | ///\param g is the digraph, to which we would like to define |
---|
[301] | 891 | ///the DistMap |
---|
[244] | 892 | static DistMap *createDistMap(const Digraph &g) |
---|
[100] | 893 | { |
---|
[278] | 894 | return new DistMap(g); |
---|
[100] | 895 | } |
---|
[278] | 896 | |
---|
| 897 | ///The type of the shortest paths. |
---|
| 898 | |
---|
| 899 | ///The type of the shortest paths. |
---|
[716] | 900 | ///It must conform to the \ref concepts::Path "Path" concept. |
---|
[278] | 901 | typedef lemon::Path<Digraph> Path; |
---|
[100] | 902 | }; |
---|
[209] | 903 | |
---|
[301] | 904 | /// Default traits class used by BfsWizard |
---|
[100] | 905 | |
---|
[716] | 906 | /// Default traits class used by BfsWizard. |
---|
| 907 | /// \tparam GR The type of the digraph. |
---|
[100] | 908 | template<class GR> |
---|
| 909 | class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
---|
| 910 | { |
---|
| 911 | |
---|
| 912 | typedef BfsWizardDefaultTraits<GR> Base; |
---|
| 913 | protected: |
---|
[244] | 914 | //The type of the nodes in the digraph. |
---|
[100] | 915 | typedef typename Base::Digraph::Node Node; |
---|
| 916 | |
---|
[244] | 917 | //Pointer to the digraph the algorithm runs on. |
---|
[100] | 918 | void *_g; |
---|
[244] | 919 | //Pointer to the map of reached nodes. |
---|
[100] | 920 | void *_reached; |
---|
[244] | 921 | //Pointer to the map of processed nodes. |
---|
[100] | 922 | void *_processed; |
---|
[244] | 923 | //Pointer to the map of predecessors arcs. |
---|
[100] | 924 | void *_pred; |
---|
[244] | 925 | //Pointer to the map of distances. |
---|
[100] | 926 | void *_dist; |
---|
[278] | 927 | //Pointer to the shortest path to the target node. |
---|
| 928 | void *_path; |
---|
| 929 | //Pointer to the distance of the target node. |
---|
| 930 | int *_di; |
---|
[209] | 931 | |
---|
[100] | 932 | public: |
---|
| 933 | /// Constructor. |
---|
[209] | 934 | |
---|
[716] | 935 | /// This constructor does not require parameters, it initiates |
---|
[278] | 936 | /// all of the attributes to \c 0. |
---|
[100] | 937 | BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
---|
[278] | 938 | _dist(0), _path(0), _di(0) {} |
---|
[100] | 939 | |
---|
| 940 | /// Constructor. |
---|
[209] | 941 | |
---|
[278] | 942 | /// This constructor requires one parameter, |
---|
| 943 | /// others are initiated to \c 0. |
---|
[244] | 944 | /// \param g The digraph the algorithm runs on. |
---|
[278] | 945 | BfsWizardBase(const GR &g) : |
---|
[209] | 946 | _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
---|
[278] | 947 | _reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
---|
[100] | 948 | |
---|
| 949 | }; |
---|
[209] | 950 | |
---|
[278] | 951 | /// Auxiliary class for the function-type interface of BFS algorithm. |
---|
[100] | 952 | |
---|
[278] | 953 | /// This auxiliary class is created to implement the |
---|
| 954 | /// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
---|
[405] | 955 | /// It does not have own \ref run(Node) "run()" method, it uses the |
---|
| 956 | /// functions and features of the plain \ref Bfs. |
---|
[100] | 957 | /// |
---|
[278] | 958 | /// This class should only be used through the \ref bfs() function, |
---|
| 959 | /// which makes it easier to use the algorithm. |
---|
[100] | 960 | template<class TR> |
---|
| 961 | class BfsWizard : public TR |
---|
| 962 | { |
---|
| 963 | typedef TR Base; |
---|
| 964 | |
---|
| 965 | typedef typename TR::Digraph Digraph; |
---|
[244] | 966 | |
---|
[100] | 967 | typedef typename Digraph::Node Node; |
---|
| 968 | typedef typename Digraph::NodeIt NodeIt; |
---|
| 969 | typedef typename Digraph::Arc Arc; |
---|
| 970 | typedef typename Digraph::OutArcIt OutArcIt; |
---|
[209] | 971 | |
---|
[100] | 972 | typedef typename TR::PredMap PredMap; |
---|
| 973 | typedef typename TR::DistMap DistMap; |
---|
[244] | 974 | typedef typename TR::ReachedMap ReachedMap; |
---|
| 975 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
[278] | 976 | typedef typename TR::Path Path; |
---|
[100] | 977 | |
---|
| 978 | public: |
---|
[244] | 979 | |
---|
[100] | 980 | /// Constructor. |
---|
| 981 | BfsWizard() : TR() {} |
---|
| 982 | |
---|
| 983 | /// Constructor that requires parameters. |
---|
| 984 | |
---|
| 985 | /// Constructor that requires parameters. |
---|
| 986 | /// These parameters will be the default values for the traits class. |
---|
[278] | 987 | /// \param g The digraph the algorithm runs on. |
---|
| 988 | BfsWizard(const Digraph &g) : |
---|
| 989 | TR(g) {} |
---|
[100] | 990 | |
---|
| 991 | ///Copy constructor |
---|
| 992 | BfsWizard(const TR &b) : TR(b) {} |
---|
| 993 | |
---|
| 994 | ~BfsWizard() {} |
---|
| 995 | |
---|
[278] | 996 | ///Runs BFS algorithm from the given source node. |
---|
[209] | 997 | |
---|
[278] | 998 | ///This method runs BFS algorithm from node \c s |
---|
| 999 | ///in order to compute the shortest path to each node. |
---|
| 1000 | void run(Node s) |
---|
| 1001 | { |
---|
| 1002 | Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
---|
| 1003 | if (Base::_pred) |
---|
| 1004 | alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
---|
| 1005 | if (Base::_dist) |
---|
| 1006 | alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
---|
| 1007 | if (Base::_reached) |
---|
| 1008 | alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
---|
| 1009 | if (Base::_processed) |
---|
| 1010 | alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
---|
| 1011 | if (s!=INVALID) |
---|
| 1012 | alg.run(s); |
---|
| 1013 | else |
---|
| 1014 | alg.run(); |
---|
| 1015 | } |
---|
| 1016 | |
---|
| 1017 | ///Finds the shortest path between \c s and \c t. |
---|
| 1018 | |
---|
| 1019 | ///This method runs BFS algorithm from node \c s |
---|
| 1020 | ///in order to compute the shortest path to node \c t |
---|
| 1021 | ///(it stops searching when \c t is processed). |
---|
| 1022 | /// |
---|
| 1023 | ///\return \c true if \c t is reachable form \c s. |
---|
| 1024 | bool run(Node s, Node t) |
---|
| 1025 | { |
---|
| 1026 | Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
---|
| 1027 | if (Base::_pred) |
---|
| 1028 | alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
---|
| 1029 | if (Base::_dist) |
---|
| 1030 | alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
---|
| 1031 | if (Base::_reached) |
---|
| 1032 | alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
---|
| 1033 | if (Base::_processed) |
---|
| 1034 | alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
---|
| 1035 | alg.run(s,t); |
---|
| 1036 | if (Base::_path) |
---|
| 1037 | *reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
---|
| 1038 | if (Base::_di) |
---|
| 1039 | *Base::_di = alg.dist(t); |
---|
| 1040 | return alg.reached(t); |
---|
| 1041 | } |
---|
| 1042 | |
---|
| 1043 | ///Runs BFS algorithm to visit all nodes in the digraph. |
---|
| 1044 | |
---|
[787] | 1045 | ///This method runs BFS algorithm in order to visit all nodes |
---|
| 1046 | ///in the digraph. |
---|
[100] | 1047 | void run() |
---|
| 1048 | { |
---|
[278] | 1049 | run(INVALID); |
---|
[100] | 1050 | } |
---|
[209] | 1051 | |
---|
[244] | 1052 | template<class T> |
---|
[257] | 1053 | struct SetPredMapBase : public Base { |
---|
[244] | 1054 | typedef T PredMap; |
---|
| 1055 | static PredMap *createPredMap(const Digraph &) { return 0; }; |
---|
[257] | 1056 | SetPredMapBase(const TR &b) : TR(b) {} |
---|
[244] | 1057 | }; |
---|
[716] | 1058 | |
---|
| 1059 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 1060 | ///the predecessor map. |
---|
[244] | 1061 | /// |
---|
[716] | 1062 | ///\ref named-templ-param "Named parameter" function for setting |
---|
| 1063 | ///the map that stores the predecessor arcs of the nodes. |
---|
[244] | 1064 | template<class T> |
---|
[257] | 1065 | BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
---|
[244] | 1066 | { |
---|
| 1067 | Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
[257] | 1068 | return BfsWizard<SetPredMapBase<T> >(*this); |
---|
[244] | 1069 | } |
---|
| 1070 | |
---|
| 1071 | template<class T> |
---|
[257] | 1072 | struct SetReachedMapBase : public Base { |
---|
[244] | 1073 | typedef T ReachedMap; |
---|
| 1074 | static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
---|
[257] | 1075 | SetReachedMapBase(const TR &b) : TR(b) {} |
---|
[244] | 1076 | }; |
---|
[716] | 1077 | |
---|
| 1078 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 1079 | ///the reached map. |
---|
[244] | 1080 | /// |
---|
[716] | 1081 | ///\ref named-templ-param "Named parameter" function for setting |
---|
| 1082 | ///the map that indicates which nodes are reached. |
---|
[244] | 1083 | template<class T> |
---|
[257] | 1084 | BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
---|
[244] | 1085 | { |
---|
| 1086 | Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
[257] | 1087 | return BfsWizard<SetReachedMapBase<T> >(*this); |
---|
[244] | 1088 | } |
---|
| 1089 | |
---|
| 1090 | template<class T> |
---|
[278] | 1091 | struct SetDistMapBase : public Base { |
---|
| 1092 | typedef T DistMap; |
---|
| 1093 | static DistMap *createDistMap(const Digraph &) { return 0; }; |
---|
| 1094 | SetDistMapBase(const TR &b) : TR(b) {} |
---|
| 1095 | }; |
---|
[716] | 1096 | |
---|
| 1097 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 1098 | ///the distance map. |
---|
[278] | 1099 | /// |
---|
[716] | 1100 | ///\ref named-templ-param "Named parameter" function for setting |
---|
| 1101 | ///the map that stores the distances of the nodes calculated |
---|
| 1102 | ///by the algorithm. |
---|
[278] | 1103 | template<class T> |
---|
| 1104 | BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
---|
| 1105 | { |
---|
| 1106 | Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
| 1107 | return BfsWizard<SetDistMapBase<T> >(*this); |
---|
| 1108 | } |
---|
| 1109 | |
---|
| 1110 | template<class T> |
---|
[257] | 1111 | struct SetProcessedMapBase : public Base { |
---|
[244] | 1112 | typedef T ProcessedMap; |
---|
| 1113 | static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
---|
[257] | 1114 | SetProcessedMapBase(const TR &b) : TR(b) {} |
---|
[244] | 1115 | }; |
---|
[716] | 1116 | |
---|
| 1117 | ///\brief \ref named-func-param "Named parameter" for setting |
---|
| 1118 | ///the processed map. |
---|
[244] | 1119 | /// |
---|
[716] | 1120 | ///\ref named-templ-param "Named parameter" function for setting |
---|
| 1121 | ///the map that indicates which nodes are processed. |
---|
[244] | 1122 | template<class T> |
---|
[257] | 1123 | BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
---|
[244] | 1124 | { |
---|
| 1125 | Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
[257] | 1126 | return BfsWizard<SetProcessedMapBase<T> >(*this); |
---|
[244] | 1127 | } |
---|
| 1128 | |
---|
| 1129 | template<class T> |
---|
[278] | 1130 | struct SetPathBase : public Base { |
---|
| 1131 | typedef T Path; |
---|
| 1132 | SetPathBase(const TR &b) : TR(b) {} |
---|
[244] | 1133 | }; |
---|
[278] | 1134 | ///\brief \ref named-func-param "Named parameter" |
---|
| 1135 | ///for getting the shortest path to the target node. |
---|
[244] | 1136 | /// |
---|
[278] | 1137 | ///\ref named-func-param "Named parameter" |
---|
| 1138 | ///for getting the shortest path to the target node. |
---|
[244] | 1139 | template<class T> |
---|
[278] | 1140 | BfsWizard<SetPathBase<T> > path(const T &t) |
---|
[244] | 1141 | { |
---|
[278] | 1142 | Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
| 1143 | return BfsWizard<SetPathBase<T> >(*this); |
---|
| 1144 | } |
---|
| 1145 | |
---|
| 1146 | ///\brief \ref named-func-param "Named parameter" |
---|
| 1147 | ///for getting the distance of the target node. |
---|
| 1148 | /// |
---|
| 1149 | ///\ref named-func-param "Named parameter" |
---|
| 1150 | ///for getting the distance of the target node. |
---|
| 1151 | BfsWizard dist(const int &d) |
---|
| 1152 | { |
---|
| 1153 | Base::_di=const_cast<int*>(&d); |
---|
| 1154 | return *this; |
---|
[244] | 1155 | } |
---|
| 1156 | |
---|
[100] | 1157 | }; |
---|
[209] | 1158 | |
---|
[278] | 1159 | ///Function-type interface for BFS algorithm. |
---|
[100] | 1160 | |
---|
| 1161 | /// \ingroup search |
---|
[278] | 1162 | ///Function-type interface for BFS algorithm. |
---|
[100] | 1163 | /// |
---|
[278] | 1164 | ///This function also has several \ref named-func-param "named parameters", |
---|
[100] | 1165 | ///they are declared as the members of class \ref BfsWizard. |
---|
[278] | 1166 | ///The following examples show how to use these parameters. |
---|
[100] | 1167 | ///\code |
---|
[278] | 1168 | /// // Compute shortest path from node s to each node |
---|
| 1169 | /// bfs(g).predMap(preds).distMap(dists).run(s); |
---|
| 1170 | /// |
---|
| 1171 | /// // Compute shortest path from s to t |
---|
| 1172 | /// bool reached = bfs(g).path(p).dist(d).run(s,t); |
---|
[100] | 1173 | ///\endcode |
---|
[405] | 1174 | ///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()" |
---|
[100] | 1175 | ///to the end of the parameter list. |
---|
| 1176 | ///\sa BfsWizard |
---|
| 1177 | ///\sa Bfs |
---|
| 1178 | template<class GR> |
---|
| 1179 | BfsWizard<BfsWizardBase<GR> > |
---|
[278] | 1180 | bfs(const GR &digraph) |
---|
[100] | 1181 | { |
---|
[278] | 1182 | return BfsWizard<BfsWizardBase<GR> >(digraph); |
---|
[100] | 1183 | } |
---|
| 1184 | |
---|
| 1185 | #ifdef DOXYGEN |
---|
[244] | 1186 | /// \brief Visitor class for BFS. |
---|
[209] | 1187 | /// |
---|
[100] | 1188 | /// This class defines the interface of the BfsVisit events, and |
---|
[244] | 1189 | /// it could be the base of a real visitor class. |
---|
[492] | 1190 | template <typename GR> |
---|
[100] | 1191 | struct BfsVisitor { |
---|
[492] | 1192 | typedef GR Digraph; |
---|
[100] | 1193 | typedef typename Digraph::Arc Arc; |
---|
| 1194 | typedef typename Digraph::Node Node; |
---|
[244] | 1195 | /// \brief Called for the source node(s) of the BFS. |
---|
[209] | 1196 | /// |
---|
[244] | 1197 | /// This function is called for the source node(s) of the BFS. |
---|
| 1198 | void start(const Node& node) {} |
---|
| 1199 | /// \brief Called when a node is reached first time. |
---|
| 1200 | /// |
---|
| 1201 | /// This function is called when a node is reached first time. |
---|
| 1202 | void reach(const Node& node) {} |
---|
| 1203 | /// \brief Called when a node is processed. |
---|
| 1204 | /// |
---|
| 1205 | /// This function is called when a node is processed. |
---|
| 1206 | void process(const Node& node) {} |
---|
| 1207 | /// \brief Called when an arc reaches a new node. |
---|
| 1208 | /// |
---|
| 1209 | /// This function is called when the BFS finds an arc whose target node |
---|
| 1210 | /// is not reached yet. |
---|
[100] | 1211 | void discover(const Arc& arc) {} |
---|
[244] | 1212 | /// \brief Called when an arc is examined but its target node is |
---|
[100] | 1213 | /// already discovered. |
---|
[209] | 1214 | /// |
---|
[244] | 1215 | /// This function is called when an arc is examined but its target node is |
---|
[100] | 1216 | /// already discovered. |
---|
| 1217 | void examine(const Arc& arc) {} |
---|
| 1218 | }; |
---|
| 1219 | #else |
---|
[492] | 1220 | template <typename GR> |
---|
[100] | 1221 | struct BfsVisitor { |
---|
[492] | 1222 | typedef GR Digraph; |
---|
[100] | 1223 | typedef typename Digraph::Arc Arc; |
---|
| 1224 | typedef typename Digraph::Node Node; |
---|
[244] | 1225 | void start(const Node&) {} |
---|
| 1226 | void reach(const Node&) {} |
---|
| 1227 | void process(const Node&) {} |
---|
[100] | 1228 | void discover(const Arc&) {} |
---|
| 1229 | void examine(const Arc&) {} |
---|
| 1230 | |
---|
| 1231 | template <typename _Visitor> |
---|
| 1232 | struct Constraints { |
---|
| 1233 | void constraints() { |
---|
[209] | 1234 | Arc arc; |
---|
| 1235 | Node node; |
---|
[244] | 1236 | visitor.start(node); |
---|
| 1237 | visitor.reach(node); |
---|
| 1238 | visitor.process(node); |
---|
[209] | 1239 | visitor.discover(arc); |
---|
| 1240 | visitor.examine(arc); |
---|
[100] | 1241 | } |
---|
| 1242 | _Visitor& visitor; |
---|
| 1243 | }; |
---|
| 1244 | }; |
---|
| 1245 | #endif |
---|
| 1246 | |
---|
| 1247 | /// \brief Default traits class of BfsVisit class. |
---|
| 1248 | /// |
---|
| 1249 | /// Default traits class of BfsVisit class. |
---|
[492] | 1250 | /// \tparam GR The type of the digraph the algorithm runs on. |
---|
| 1251 | template<class GR> |
---|
[100] | 1252 | struct BfsVisitDefaultTraits { |
---|
| 1253 | |
---|
[244] | 1254 | /// \brief The type of the digraph the algorithm runs on. |
---|
[492] | 1255 | typedef GR Digraph; |
---|
[100] | 1256 | |
---|
| 1257 | /// \brief The type of the map that indicates which nodes are reached. |
---|
[209] | 1258 | /// |
---|
[100] | 1259 | /// The type of the map that indicates which nodes are reached. |
---|
[716] | 1260 | /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
---|
[100] | 1261 | typedef typename Digraph::template NodeMap<bool> ReachedMap; |
---|
| 1262 | |
---|
[301] | 1263 | /// \brief Instantiates a ReachedMap. |
---|
[100] | 1264 | /// |
---|
[301] | 1265 | /// This function instantiates a ReachedMap. |
---|
[100] | 1266 | /// \param digraph is the digraph, to which |
---|
[301] | 1267 | /// we would like to define the ReachedMap. |
---|
[100] | 1268 | static ReachedMap *createReachedMap(const Digraph &digraph) { |
---|
| 1269 | return new ReachedMap(digraph); |
---|
| 1270 | } |
---|
| 1271 | |
---|
| 1272 | }; |
---|
| 1273 | |
---|
| 1274 | /// \ingroup search |
---|
[209] | 1275 | /// |
---|
[492] | 1276 | /// \brief BFS algorithm class with visitor interface. |
---|
[209] | 1277 | /// |
---|
[492] | 1278 | /// This class provides an efficient implementation of the BFS algorithm |
---|
[100] | 1279 | /// with visitor interface. |
---|
| 1280 | /// |
---|
[492] | 1281 | /// The BfsVisit class provides an alternative interface to the Bfs |
---|
[100] | 1282 | /// class. It works with callback mechanism, the BfsVisit object calls |
---|
[244] | 1283 | /// the member functions of the \c Visitor class on every BFS event. |
---|
[100] | 1284 | /// |
---|
[252] | 1285 | /// This interface of the BFS algorithm should be used in special cases |
---|
| 1286 | /// when extra actions have to be performed in connection with certain |
---|
| 1287 | /// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
---|
| 1288 | /// instead. |
---|
| 1289 | /// |
---|
[492] | 1290 | /// \tparam GR The type of the digraph the algorithm runs on. |
---|
| 1291 | /// The default type is \ref ListDigraph. |
---|
| 1292 | /// The value of GR is not used directly by \ref BfsVisit, |
---|
| 1293 | /// it is only passed to \ref BfsVisitDefaultTraits. |
---|
| 1294 | /// \tparam VS The Visitor type that is used by the algorithm. |
---|
| 1295 | /// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
---|
[244] | 1296 | /// does not observe the BFS events. If you want to observe the BFS |
---|
| 1297 | /// events, you should implement your own visitor class. |
---|
[492] | 1298 | /// \tparam TR Traits class to set various data types used by the |
---|
[100] | 1299 | /// algorithm. The default traits class is |
---|
[492] | 1300 | /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
---|
[100] | 1301 | /// See \ref BfsVisitDefaultTraits for the documentation of |
---|
[244] | 1302 | /// a BFS visit traits class. |
---|
[100] | 1303 | #ifdef DOXYGEN |
---|
[492] | 1304 | template <typename GR, typename VS, typename TR> |
---|
[100] | 1305 | #else |
---|
[492] | 1306 | template <typename GR = ListDigraph, |
---|
| 1307 | typename VS = BfsVisitor<GR>, |
---|
| 1308 | typename TR = BfsVisitDefaultTraits<GR> > |
---|
[100] | 1309 | #endif |
---|
| 1310 | class BfsVisit { |
---|
| 1311 | public: |
---|
[209] | 1312 | |
---|
[244] | 1313 | ///The traits class. |
---|
[492] | 1314 | typedef TR Traits; |
---|
[100] | 1315 | |
---|
[244] | 1316 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 1317 | typedef typename Traits::Digraph Digraph; |
---|
| 1318 | |
---|
[244] | 1319 | ///The visitor type used by the algorithm. |
---|
[492] | 1320 | typedef VS Visitor; |
---|
[100] | 1321 | |
---|
[244] | 1322 | ///The type of the map that indicates which nodes are reached. |
---|
[100] | 1323 | typedef typename Traits::ReachedMap ReachedMap; |
---|
| 1324 | |
---|
| 1325 | private: |
---|
| 1326 | |
---|
| 1327 | typedef typename Digraph::Node Node; |
---|
| 1328 | typedef typename Digraph::NodeIt NodeIt; |
---|
| 1329 | typedef typename Digraph::Arc Arc; |
---|
| 1330 | typedef typename Digraph::OutArcIt OutArcIt; |
---|
| 1331 | |
---|
[244] | 1332 | //Pointer to the underlying digraph. |
---|
[100] | 1333 | const Digraph *_digraph; |
---|
[244] | 1334 | //Pointer to the visitor object. |
---|
[100] | 1335 | Visitor *_visitor; |
---|
[244] | 1336 | //Pointer to the map of reached status of the nodes. |
---|
[100] | 1337 | ReachedMap *_reached; |
---|
[244] | 1338 | //Indicates if _reached is locally allocated (true) or not. |
---|
[100] | 1339 | bool local_reached; |
---|
| 1340 | |
---|
| 1341 | std::vector<typename Digraph::Node> _list; |
---|
| 1342 | int _list_front, _list_back; |
---|
| 1343 | |
---|
[280] | 1344 | //Creates the maps if necessary. |
---|
[100] | 1345 | void create_maps() { |
---|
| 1346 | if(!_reached) { |
---|
[209] | 1347 | local_reached = true; |
---|
| 1348 | _reached = Traits::createReachedMap(*_digraph); |
---|
[100] | 1349 | } |
---|
| 1350 | } |
---|
| 1351 | |
---|
| 1352 | protected: |
---|
| 1353 | |
---|
| 1354 | BfsVisit() {} |
---|
[209] | 1355 | |
---|
[100] | 1356 | public: |
---|
| 1357 | |
---|
| 1358 | typedef BfsVisit Create; |
---|
| 1359 | |
---|
[405] | 1360 | /// \name Named Template Parameters |
---|
[100] | 1361 | |
---|
| 1362 | ///@{ |
---|
| 1363 | template <class T> |
---|
[257] | 1364 | struct SetReachedMapTraits : public Traits { |
---|
[100] | 1365 | typedef T ReachedMap; |
---|
| 1366 | static ReachedMap *createReachedMap(const Digraph &digraph) { |
---|
[290] | 1367 | LEMON_ASSERT(false, "ReachedMap is not initialized"); |
---|
| 1368 | return 0; // ignore warnings |
---|
[100] | 1369 | } |
---|
| 1370 | }; |
---|
[209] | 1371 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
[244] | 1372 | /// ReachedMap type. |
---|
[100] | 1373 | /// |
---|
[244] | 1374 | /// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
---|
[100] | 1375 | template <class T> |
---|
[257] | 1376 | struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
---|
| 1377 | SetReachedMapTraits<T> > { |
---|
| 1378 | typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
---|
[100] | 1379 | }; |
---|
| 1380 | ///@} |
---|
| 1381 | |
---|
[209] | 1382 | public: |
---|
| 1383 | |
---|
[100] | 1384 | /// \brief Constructor. |
---|
| 1385 | /// |
---|
| 1386 | /// Constructor. |
---|
| 1387 | /// |
---|
[244] | 1388 | /// \param digraph The digraph the algorithm runs on. |
---|
| 1389 | /// \param visitor The visitor object of the algorithm. |
---|
[209] | 1390 | BfsVisit(const Digraph& digraph, Visitor& visitor) |
---|
[100] | 1391 | : _digraph(&digraph), _visitor(&visitor), |
---|
[209] | 1392 | _reached(0), local_reached(false) {} |
---|
| 1393 | |
---|
[100] | 1394 | /// \brief Destructor. |
---|
| 1395 | ~BfsVisit() { |
---|
| 1396 | if(local_reached) delete _reached; |
---|
| 1397 | } |
---|
| 1398 | |
---|
[244] | 1399 | /// \brief Sets the map that indicates which nodes are reached. |
---|
[100] | 1400 | /// |
---|
[244] | 1401 | /// Sets the map that indicates which nodes are reached. |
---|
[405] | 1402 | /// If you don't use this function before calling \ref run(Node) "run()" |
---|
| 1403 | /// or \ref init(), an instance will be allocated automatically. |
---|
| 1404 | /// The destructor deallocates this automatically allocated map, |
---|
| 1405 | /// of course. |
---|
[100] | 1406 | /// \return <tt> (*this) </tt> |
---|
| 1407 | BfsVisit &reachedMap(ReachedMap &m) { |
---|
| 1408 | if(local_reached) { |
---|
[209] | 1409 | delete _reached; |
---|
| 1410 | local_reached = false; |
---|
[100] | 1411 | } |
---|
| 1412 | _reached = &m; |
---|
| 1413 | return *this; |
---|
| 1414 | } |
---|
| 1415 | |
---|
| 1416 | public: |
---|
[244] | 1417 | |
---|
[405] | 1418 | /// \name Execution Control |
---|
| 1419 | /// The simplest way to execute the BFS algorithm is to use one of the |
---|
| 1420 | /// member functions called \ref run(Node) "run()".\n |
---|
[713] | 1421 | /// If you need better control on the execution, you have to call |
---|
| 1422 | /// \ref init() first, then you can add several source nodes with |
---|
[405] | 1423 | /// \ref addSource(). Finally the actual path computation can be |
---|
| 1424 | /// performed with one of the \ref start() functions. |
---|
[100] | 1425 | |
---|
| 1426 | /// @{ |
---|
[244] | 1427 | |
---|
[100] | 1428 | /// \brief Initializes the internal data structures. |
---|
| 1429 | /// |
---|
| 1430 | /// Initializes the internal data structures. |
---|
| 1431 | void init() { |
---|
| 1432 | create_maps(); |
---|
| 1433 | _list.resize(countNodes(*_digraph)); |
---|
| 1434 | _list_front = _list_back = -1; |
---|
| 1435 | for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
---|
[209] | 1436 | _reached->set(u, false); |
---|
[100] | 1437 | } |
---|
| 1438 | } |
---|
[209] | 1439 | |
---|
[100] | 1440 | /// \brief Adds a new source node. |
---|
| 1441 | /// |
---|
| 1442 | /// Adds a new source node to the set of nodes to be processed. |
---|
| 1443 | void addSource(Node s) { |
---|
| 1444 | if(!(*_reached)[s]) { |
---|
[209] | 1445 | _reached->set(s,true); |
---|
| 1446 | _visitor->start(s); |
---|
| 1447 | _visitor->reach(s); |
---|
[100] | 1448 | _list[++_list_back] = s; |
---|
[209] | 1449 | } |
---|
[100] | 1450 | } |
---|
[209] | 1451 | |
---|
[100] | 1452 | /// \brief Processes the next node. |
---|
| 1453 | /// |
---|
| 1454 | /// Processes the next node. |
---|
| 1455 | /// |
---|
| 1456 | /// \return The processed node. |
---|
| 1457 | /// |
---|
[244] | 1458 | /// \pre The queue must not be empty. |
---|
[209] | 1459 | Node processNextNode() { |
---|
[100] | 1460 | Node n = _list[++_list_front]; |
---|
| 1461 | _visitor->process(n); |
---|
| 1462 | Arc e; |
---|
| 1463 | for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
---|
| 1464 | Node m = _digraph->target(e); |
---|
| 1465 | if (!(*_reached)[m]) { |
---|
| 1466 | _visitor->discover(e); |
---|
| 1467 | _visitor->reach(m); |
---|
| 1468 | _reached->set(m, true); |
---|
| 1469 | _list[++_list_back] = m; |
---|
| 1470 | } else { |
---|
| 1471 | _visitor->examine(e); |
---|
| 1472 | } |
---|
| 1473 | } |
---|
| 1474 | return n; |
---|
| 1475 | } |
---|
| 1476 | |
---|
| 1477 | /// \brief Processes the next node. |
---|
| 1478 | /// |
---|
[244] | 1479 | /// Processes the next node and checks if the given target node |
---|
[100] | 1480 | /// is reached. If the target node is reachable from the processed |
---|
[244] | 1481 | /// node, then the \c reach parameter will be set to \c true. |
---|
[100] | 1482 | /// |
---|
| 1483 | /// \param target The target node. |
---|
[244] | 1484 | /// \retval reach Indicates if the target node is reached. |
---|
| 1485 | /// It should be initially \c false. |
---|
| 1486 | /// |
---|
[100] | 1487 | /// \return The processed node. |
---|
| 1488 | /// |
---|
[244] | 1489 | /// \pre The queue must not be empty. |
---|
[100] | 1490 | Node processNextNode(Node target, bool& reach) { |
---|
| 1491 | Node n = _list[++_list_front]; |
---|
| 1492 | _visitor->process(n); |
---|
| 1493 | Arc e; |
---|
| 1494 | for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
---|
| 1495 | Node m = _digraph->target(e); |
---|
| 1496 | if (!(*_reached)[m]) { |
---|
| 1497 | _visitor->discover(e); |
---|
| 1498 | _visitor->reach(m); |
---|
| 1499 | _reached->set(m, true); |
---|
| 1500 | _list[++_list_back] = m; |
---|
| 1501 | reach = reach || (target == m); |
---|
| 1502 | } else { |
---|
| 1503 | _visitor->examine(e); |
---|
| 1504 | } |
---|
| 1505 | } |
---|
| 1506 | return n; |
---|
| 1507 | } |
---|
| 1508 | |
---|
| 1509 | /// \brief Processes the next node. |
---|
| 1510 | /// |
---|
[244] | 1511 | /// Processes the next node and checks if at least one of reached |
---|
| 1512 | /// nodes has \c true value in the \c nm node map. If one node |
---|
| 1513 | /// with \c true value is reachable from the processed node, then the |
---|
| 1514 | /// \c rnode parameter will be set to the first of such nodes. |
---|
[100] | 1515 | /// |
---|
[244] | 1516 | /// \param nm A \c bool (or convertible) node map that indicates the |
---|
| 1517 | /// possible targets. |
---|
[100] | 1518 | /// \retval rnode The reached target node. |
---|
[244] | 1519 | /// It should be initially \c INVALID. |
---|
| 1520 | /// |
---|
[100] | 1521 | /// \return The processed node. |
---|
| 1522 | /// |
---|
[244] | 1523 | /// \pre The queue must not be empty. |
---|
[100] | 1524 | template <typename NM> |
---|
| 1525 | Node processNextNode(const NM& nm, Node& rnode) { |
---|
| 1526 | Node n = _list[++_list_front]; |
---|
| 1527 | _visitor->process(n); |
---|
| 1528 | Arc e; |
---|
| 1529 | for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
---|
| 1530 | Node m = _digraph->target(e); |
---|
| 1531 | if (!(*_reached)[m]) { |
---|
| 1532 | _visitor->discover(e); |
---|
| 1533 | _visitor->reach(m); |
---|
| 1534 | _reached->set(m, true); |
---|
| 1535 | _list[++_list_back] = m; |
---|
| 1536 | if (nm[m] && rnode == INVALID) rnode = m; |
---|
| 1537 | } else { |
---|
| 1538 | _visitor->examine(e); |
---|
| 1539 | } |
---|
| 1540 | } |
---|
| 1541 | return n; |
---|
| 1542 | } |
---|
| 1543 | |
---|
[244] | 1544 | /// \brief The next node to be processed. |
---|
[100] | 1545 | /// |
---|
[244] | 1546 | /// Returns the next node to be processed or \c INVALID if the queue |
---|
| 1547 | /// is empty. |
---|
| 1548 | Node nextNode() const { |
---|
[100] | 1549 | return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
---|
| 1550 | } |
---|
| 1551 | |
---|
| 1552 | /// \brief Returns \c false if there are nodes |
---|
[244] | 1553 | /// to be processed. |
---|
[100] | 1554 | /// |
---|
| 1555 | /// Returns \c false if there are nodes |
---|
[244] | 1556 | /// to be processed in the queue. |
---|
| 1557 | bool emptyQueue() const { return _list_front == _list_back; } |
---|
[100] | 1558 | |
---|
| 1559 | /// \brief Returns the number of the nodes to be processed. |
---|
| 1560 | /// |
---|
| 1561 | /// Returns the number of the nodes to be processed in the queue. |
---|
[244] | 1562 | int queueSize() const { return _list_back - _list_front; } |
---|
[209] | 1563 | |
---|
[100] | 1564 | /// \brief Executes the algorithm. |
---|
| 1565 | /// |
---|
| 1566 | /// Executes the algorithm. |
---|
| 1567 | /// |
---|
[244] | 1568 | /// This method runs the %BFS algorithm from the root node(s) |
---|
| 1569 | /// in order to compute the shortest path to each node. |
---|
| 1570 | /// |
---|
| 1571 | /// The algorithm computes |
---|
| 1572 | /// - the shortest path tree (forest), |
---|
| 1573 | /// - the distance of each node from the root(s). |
---|
| 1574 | /// |
---|
| 1575 | /// \pre init() must be called and at least one root node should be added |
---|
[100] | 1576 | /// with addSource() before using this function. |
---|
[244] | 1577 | /// |
---|
| 1578 | /// \note <tt>b.start()</tt> is just a shortcut of the following code. |
---|
| 1579 | /// \code |
---|
| 1580 | /// while ( !b.emptyQueue() ) { |
---|
| 1581 | /// b.processNextNode(); |
---|
| 1582 | /// } |
---|
| 1583 | /// \endcode |
---|
[100] | 1584 | void start() { |
---|
| 1585 | while ( !emptyQueue() ) processNextNode(); |
---|
| 1586 | } |
---|
[209] | 1587 | |
---|
[244] | 1588 | /// \brief Executes the algorithm until the given target node is reached. |
---|
[100] | 1589 | /// |
---|
[244] | 1590 | /// Executes the algorithm until the given target node is reached. |
---|
[100] | 1591 | /// |
---|
[244] | 1592 | /// This method runs the %BFS algorithm from the root node(s) |
---|
[286] | 1593 | /// in order to compute the shortest path to \c t. |
---|
[244] | 1594 | /// |
---|
| 1595 | /// The algorithm computes |
---|
[286] | 1596 | /// - the shortest path to \c t, |
---|
| 1597 | /// - the distance of \c t from the root(s). |
---|
[244] | 1598 | /// |
---|
| 1599 | /// \pre init() must be called and at least one root node should be |
---|
| 1600 | /// added with addSource() before using this function. |
---|
| 1601 | /// |
---|
| 1602 | /// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
---|
| 1603 | /// \code |
---|
| 1604 | /// bool reach = false; |
---|
| 1605 | /// while ( !b.emptyQueue() && !reach ) { |
---|
| 1606 | /// b.processNextNode(t, reach); |
---|
| 1607 | /// } |
---|
| 1608 | /// \endcode |
---|
[286] | 1609 | void start(Node t) { |
---|
[100] | 1610 | bool reach = false; |
---|
[286] | 1611 | while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
---|
[100] | 1612 | } |
---|
[209] | 1613 | |
---|
[100] | 1614 | /// \brief Executes the algorithm until a condition is met. |
---|
| 1615 | /// |
---|
| 1616 | /// Executes the algorithm until a condition is met. |
---|
| 1617 | /// |
---|
[244] | 1618 | /// This method runs the %BFS algorithm from the root node(s) in |
---|
| 1619 | /// order to compute the shortest path to a node \c v with |
---|
| 1620 | /// <tt>nm[v]</tt> true, if such a node can be found. |
---|
[100] | 1621 | /// |
---|
[244] | 1622 | /// \param nm must be a bool (or convertible) node map. The |
---|
| 1623 | /// algorithm will stop when it reaches a node \c v with |
---|
[100] | 1624 | /// <tt>nm[v]</tt> true. |
---|
| 1625 | /// |
---|
[244] | 1626 | /// \return The reached node \c v with <tt>nm[v]</tt> true or |
---|
| 1627 | /// \c INVALID if no such node was found. |
---|
| 1628 | /// |
---|
| 1629 | /// \pre init() must be called and at least one root node should be |
---|
| 1630 | /// added with addSource() before using this function. |
---|
| 1631 | /// |
---|
| 1632 | /// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
---|
| 1633 | /// \code |
---|
| 1634 | /// Node rnode = INVALID; |
---|
| 1635 | /// while ( !b.emptyQueue() && rnode == INVALID ) { |
---|
| 1636 | /// b.processNextNode(nm, rnode); |
---|
| 1637 | /// } |
---|
| 1638 | /// return rnode; |
---|
| 1639 | /// \endcode |
---|
[100] | 1640 | template <typename NM> |
---|
| 1641 | Node start(const NM &nm) { |
---|
| 1642 | Node rnode = INVALID; |
---|
| 1643 | while ( !emptyQueue() && rnode == INVALID ) { |
---|
[209] | 1644 | processNextNode(nm, rnode); |
---|
[100] | 1645 | } |
---|
| 1646 | return rnode; |
---|
| 1647 | } |
---|
| 1648 | |
---|
[286] | 1649 | /// \brief Runs the algorithm from the given source node. |
---|
[100] | 1650 | /// |
---|
[244] | 1651 | /// This method runs the %BFS algorithm from node \c s |
---|
| 1652 | /// in order to compute the shortest path to each node. |
---|
| 1653 | /// |
---|
| 1654 | /// The algorithm computes |
---|
| 1655 | /// - the shortest path tree, |
---|
| 1656 | /// - the distance of each node from the root. |
---|
| 1657 | /// |
---|
| 1658 | /// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
---|
[100] | 1659 | ///\code |
---|
| 1660 | /// b.init(); |
---|
| 1661 | /// b.addSource(s); |
---|
| 1662 | /// b.start(); |
---|
| 1663 | ///\endcode |
---|
| 1664 | void run(Node s) { |
---|
| 1665 | init(); |
---|
| 1666 | addSource(s); |
---|
| 1667 | start(); |
---|
| 1668 | } |
---|
| 1669 | |
---|
[286] | 1670 | /// \brief Finds the shortest path between \c s and \c t. |
---|
| 1671 | /// |
---|
| 1672 | /// This method runs the %BFS algorithm from node \c s |
---|
| 1673 | /// in order to compute the shortest path to node \c t |
---|
| 1674 | /// (it stops searching when \c t is processed). |
---|
| 1675 | /// |
---|
| 1676 | /// \return \c true if \c t is reachable form \c s. |
---|
| 1677 | /// |
---|
| 1678 | /// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
---|
| 1679 | /// shortcut of the following code. |
---|
| 1680 | ///\code |
---|
| 1681 | /// b.init(); |
---|
| 1682 | /// b.addSource(s); |
---|
| 1683 | /// b.start(t); |
---|
| 1684 | ///\endcode |
---|
| 1685 | bool run(Node s,Node t) { |
---|
| 1686 | init(); |
---|
| 1687 | addSource(s); |
---|
| 1688 | start(t); |
---|
| 1689 | return reached(t); |
---|
| 1690 | } |
---|
| 1691 | |
---|
[244] | 1692 | /// \brief Runs the algorithm to visit all nodes in the digraph. |
---|
[209] | 1693 | /// |
---|
[787] | 1694 | /// This method runs the %BFS algorithm in order to visit all nodes |
---|
| 1695 | /// in the digraph. |
---|
[244] | 1696 | /// |
---|
| 1697 | /// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
---|
[100] | 1698 | ///\code |
---|
| 1699 | /// b.init(); |
---|
[244] | 1700 | /// for (NodeIt n(gr); n != INVALID; ++n) { |
---|
| 1701 | /// if (!b.reached(n)) { |
---|
| 1702 | /// b.addSource(n); |
---|
[100] | 1703 | /// b.start(); |
---|
| 1704 | /// } |
---|
| 1705 | /// } |
---|
| 1706 | ///\endcode |
---|
| 1707 | void run() { |
---|
| 1708 | init(); |
---|
| 1709 | for (NodeIt it(*_digraph); it != INVALID; ++it) { |
---|
| 1710 | if (!reached(it)) { |
---|
| 1711 | addSource(it); |
---|
| 1712 | start(); |
---|
| 1713 | } |
---|
| 1714 | } |
---|
| 1715 | } |
---|
[244] | 1716 | |
---|
[100] | 1717 | ///@} |
---|
| 1718 | |
---|
| 1719 | /// \name Query Functions |
---|
[405] | 1720 | /// The results of the BFS algorithm can be obtained using these |
---|
[100] | 1721 | /// functions.\n |
---|
[405] | 1722 | /// Either \ref run(Node) "run()" or \ref start() should be called |
---|
| 1723 | /// before using them. |
---|
| 1724 | |
---|
[100] | 1725 | ///@{ |
---|
| 1726 | |
---|
[716] | 1727 | /// \brief Checks if the given node is reached from the root(s). |
---|
[100] | 1728 | /// |
---|
[405] | 1729 | /// Returns \c true if \c v is reached from the root(s). |
---|
| 1730 | /// |
---|
| 1731 | /// \pre Either \ref run(Node) "run()" or \ref init() |
---|
[100] | 1732 | /// must be called before using this function. |
---|
[420] | 1733 | bool reached(Node v) const { return (*_reached)[v]; } |
---|
[244] | 1734 | |
---|
[100] | 1735 | ///@} |
---|
[244] | 1736 | |
---|
[100] | 1737 | }; |
---|
| 1738 | |
---|
| 1739 | } //END OF NAMESPACE LEMON |
---|
| 1740 | |
---|
| 1741 | #endif |
---|