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