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