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