[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 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 | |
---|
[921] | 19 | #ifndef LEMON_BFS_H |
---|
| 20 | #define LEMON_BFS_H |
---|
[774] | 21 | |
---|
| 22 | ///\ingroup flowalgs |
---|
| 23 | ///\file |
---|
| 24 | ///\brief Bfs algorithm. |
---|
| 25 | |
---|
[1218] | 26 | #include <lemon/list_graph.h> |
---|
| 27 | #include <lemon/graph_utils.h> |
---|
[1993] | 28 | #include <lemon/bits/invalid.h> |
---|
[1218] | 29 | #include <lemon/error.h> |
---|
| 30 | #include <lemon/maps.h> |
---|
[774] | 31 | |
---|
[921] | 32 | namespace lemon { |
---|
[774] | 33 | |
---|
| 34 | |
---|
[1218] | 35 | |
---|
| 36 | ///Default traits class of Bfs class. |
---|
| 37 | |
---|
| 38 | ///Default traits class of Bfs class. |
---|
| 39 | ///\param GR Graph type. |
---|
| 40 | template<class GR> |
---|
| 41 | struct BfsDefaultTraits |
---|
| 42 | { |
---|
| 43 | ///The graph type the algorithm runs on. |
---|
| 44 | typedef GR Graph; |
---|
| 45 | ///\brief The type of the map that stores the last |
---|
| 46 | ///edges of the shortest paths. |
---|
| 47 | /// |
---|
| 48 | ///The type of the map that stores the last |
---|
| 49 | ///edges of the shortest paths. |
---|
| 50 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 51 | /// |
---|
| 52 | typedef typename Graph::template NodeMap<typename GR::Edge> PredMap; |
---|
| 53 | ///Instantiates a PredMap. |
---|
| 54 | |
---|
| 55 | ///This function instantiates a \ref PredMap. |
---|
| 56 | ///\param G is the graph, to which we would like to define the PredMap. |
---|
| 57 | ///\todo The graph alone may be insufficient to initialize |
---|
| 58 | static PredMap *createPredMap(const GR &G) |
---|
| 59 | { |
---|
| 60 | return new PredMap(G); |
---|
| 61 | } |
---|
| 62 | ///The type of the map that indicates which nodes are processed. |
---|
| 63 | |
---|
| 64 | ///The type of the map that indicates which nodes are processed. |
---|
| 65 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 66 | ///\todo named parameter to set this type, function to read and write. |
---|
| 67 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
| 68 | ///Instantiates a ProcessedMap. |
---|
| 69 | |
---|
| 70 | ///This function instantiates a \ref ProcessedMap. |
---|
[1536] | 71 | ///\param g is the graph, to which |
---|
[1218] | 72 | ///we would like to define the \ref ProcessedMap |
---|
[1536] | 73 | #ifdef DOXYGEN |
---|
| 74 | static ProcessedMap *createProcessedMap(const GR &g) |
---|
| 75 | #else |
---|
[1367] | 76 | static ProcessedMap *createProcessedMap(const GR &) |
---|
[1536] | 77 | #endif |
---|
[1218] | 78 | { |
---|
| 79 | return new ProcessedMap(); |
---|
| 80 | } |
---|
| 81 | ///The type of the map that indicates which nodes are reached. |
---|
| 82 | |
---|
| 83 | ///The type of the map that indicates which nodes are reached. |
---|
| 84 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 85 | ///\todo named parameter to set this type, function to read and write. |
---|
| 86 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
| 87 | ///Instantiates a ReachedMap. |
---|
| 88 | |
---|
| 89 | ///This function instantiates a \ref ReachedMap. |
---|
| 90 | ///\param G is the graph, to which |
---|
| 91 | ///we would like to define the \ref ReachedMap. |
---|
| 92 | static ReachedMap *createReachedMap(const GR &G) |
---|
| 93 | { |
---|
| 94 | return new ReachedMap(G); |
---|
| 95 | } |
---|
| 96 | ///The type of the map that stores the dists of the nodes. |
---|
| 97 | |
---|
| 98 | ///The type of the map that stores the dists of the nodes. |
---|
| 99 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 100 | /// |
---|
| 101 | typedef typename Graph::template NodeMap<int> DistMap; |
---|
| 102 | ///Instantiates a DistMap. |
---|
| 103 | |
---|
| 104 | ///This function instantiates a \ref DistMap. |
---|
| 105 | ///\param G is the graph, to which we would like to define the \ref DistMap |
---|
| 106 | static DistMap *createDistMap(const GR &G) |
---|
| 107 | { |
---|
| 108 | return new DistMap(G); |
---|
| 109 | } |
---|
| 110 | }; |
---|
| 111 | |
---|
[781] | 112 | ///%BFS algorithm class. |
---|
[1218] | 113 | |
---|
| 114 | ///\ingroup flowalgs |
---|
| 115 | ///This class provides an efficient implementation of the %BFS algorithm. |
---|
[774] | 116 | /// |
---|
[1218] | 117 | ///\param GR The graph type the algorithm runs on. The default value is |
---|
| 118 | ///\ref ListGraph. The value of GR is not used directly by Bfs, it |
---|
| 119 | ///is only passed to \ref BfsDefaultTraits. |
---|
| 120 | ///\param TR Traits class to set various data types used by the algorithm. |
---|
| 121 | ///The default traits class is |
---|
| 122 | ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
---|
| 123 | ///See \ref BfsDefaultTraits for the documentation of |
---|
| 124 | ///a Bfs traits class. |
---|
| 125 | /// |
---|
[1270] | 126 | ///\author Alpar Juttner |
---|
[774] | 127 | |
---|
| 128 | #ifdef DOXYGEN |
---|
[1218] | 129 | template <typename GR, |
---|
| 130 | typename TR> |
---|
[774] | 131 | #else |
---|
[1218] | 132 | template <typename GR=ListGraph, |
---|
| 133 | typename TR=BfsDefaultTraits<GR> > |
---|
[774] | 134 | #endif |
---|
[1218] | 135 | class Bfs { |
---|
[774] | 136 | public: |
---|
[1218] | 137 | /** |
---|
| 138 | * \brief \ref Exception for uninitialized parameters. |
---|
| 139 | * |
---|
| 140 | * This error represents problems in the initialization |
---|
| 141 | * of the parameters of the algorithms. |
---|
| 142 | */ |
---|
| 143 | class UninitializedParameter : public lemon::UninitializedParameter { |
---|
| 144 | public: |
---|
[2151] | 145 | virtual const char* what() const throw() { |
---|
[1218] | 146 | return "lemon::Bfs::UninitializedParameter"; |
---|
| 147 | } |
---|
| 148 | }; |
---|
| 149 | |
---|
| 150 | typedef TR Traits; |
---|
[774] | 151 | ///The type of the underlying graph. |
---|
[1218] | 152 | typedef typename TR::Graph Graph; |
---|
[911] | 153 | ///\e |
---|
[774] | 154 | typedef typename Graph::Node Node; |
---|
[911] | 155 | ///\e |
---|
[774] | 156 | typedef typename Graph::NodeIt NodeIt; |
---|
[911] | 157 | ///\e |
---|
[774] | 158 | typedef typename Graph::Edge Edge; |
---|
[911] | 159 | ///\e |
---|
[774] | 160 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 161 | |
---|
| 162 | ///\brief The type of the map that stores the last |
---|
| 163 | ///edges of the shortest paths. |
---|
[1218] | 164 | typedef typename TR::PredMap PredMap; |
---|
| 165 | ///The type of the map indicating which nodes are reached. |
---|
| 166 | typedef typename TR::ReachedMap ReachedMap; |
---|
| 167 | ///The type of the map indicating which nodes are processed. |
---|
| 168 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
[774] | 169 | ///The type of the map that stores the dists of the nodes. |
---|
[1218] | 170 | typedef typename TR::DistMap DistMap; |
---|
[774] | 171 | private: |
---|
[802] | 172 | /// Pointer to the underlying graph. |
---|
[774] | 173 | const Graph *G; |
---|
[802] | 174 | ///Pointer to the map of predecessors edges. |
---|
[1218] | 175 | PredMap *_pred; |
---|
| 176 | ///Indicates if \ref _pred is locally allocated (\c true) or not. |
---|
| 177 | bool local_pred; |
---|
[802] | 178 | ///Pointer to the map of distances. |
---|
[1218] | 179 | DistMap *_dist; |
---|
| 180 | ///Indicates if \ref _dist is locally allocated (\c true) or not. |
---|
| 181 | bool local_dist; |
---|
| 182 | ///Pointer to the map of reached status of the nodes. |
---|
| 183 | ReachedMap *_reached; |
---|
| 184 | ///Indicates if \ref _reached is locally allocated (\c true) or not. |
---|
| 185 | bool local_reached; |
---|
| 186 | ///Pointer to the map of processed status of the nodes. |
---|
| 187 | ProcessedMap *_processed; |
---|
| 188 | ///Indicates if \ref _processed is locally allocated (\c true) or not. |
---|
| 189 | bool local_processed; |
---|
[774] | 190 | |
---|
[1218] | 191 | std::vector<typename Graph::Node> _queue; |
---|
| 192 | int _queue_head,_queue_tail,_queue_next_dist; |
---|
| 193 | int _curr_dist; |
---|
[774] | 194 | |
---|
[1218] | 195 | ///Creates the maps if necessary. |
---|
| 196 | |
---|
| 197 | ///\todo Better memory allocation (instead of new). |
---|
| 198 | void create_maps() |
---|
[774] | 199 | { |
---|
[1218] | 200 | if(!_pred) { |
---|
| 201 | local_pred = true; |
---|
| 202 | _pred = Traits::createPredMap(*G); |
---|
[774] | 203 | } |
---|
[1218] | 204 | if(!_dist) { |
---|
| 205 | local_dist = true; |
---|
| 206 | _dist = Traits::createDistMap(*G); |
---|
[774] | 207 | } |
---|
[1218] | 208 | if(!_reached) { |
---|
| 209 | local_reached = true; |
---|
| 210 | _reached = Traits::createReachedMap(*G); |
---|
| 211 | } |
---|
| 212 | if(!_processed) { |
---|
| 213 | local_processed = true; |
---|
| 214 | _processed = Traits::createProcessedMap(*G); |
---|
[774] | 215 | } |
---|
| 216 | } |
---|
[1710] | 217 | |
---|
| 218 | protected: |
---|
[774] | 219 | |
---|
[1710] | 220 | Bfs() {} |
---|
| 221 | |
---|
| 222 | public: |
---|
[1218] | 223 | |
---|
[1710] | 224 | typedef Bfs Create; |
---|
| 225 | |
---|
[1218] | 226 | ///\name Named template parameters |
---|
| 227 | |
---|
| 228 | ///@{ |
---|
| 229 | |
---|
| 230 | template <class T> |
---|
| 231 | struct DefPredMapTraits : public Traits { |
---|
| 232 | typedef T PredMap; |
---|
[1799] | 233 | static PredMap *createPredMap(const Graph &) |
---|
[1218] | 234 | { |
---|
| 235 | throw UninitializedParameter(); |
---|
| 236 | } |
---|
| 237 | }; |
---|
| 238 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
| 239 | |
---|
| 240 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
| 241 | /// |
---|
| 242 | template <class T> |
---|
[1709] | 243 | struct DefPredMap : public Bfs< Graph, DefPredMapTraits<T> > { |
---|
| 244 | typedef Bfs< Graph, DefPredMapTraits<T> > Create; |
---|
| 245 | }; |
---|
[1218] | 246 | |
---|
| 247 | template <class T> |
---|
| 248 | struct DefDistMapTraits : public Traits { |
---|
| 249 | typedef T DistMap; |
---|
[1799] | 250 | static DistMap *createDistMap(const Graph &) |
---|
[1218] | 251 | { |
---|
| 252 | throw UninitializedParameter(); |
---|
| 253 | } |
---|
| 254 | }; |
---|
| 255 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
| 256 | |
---|
| 257 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
| 258 | /// |
---|
| 259 | template <class T> |
---|
[1709] | 260 | struct DefDistMap : public Bfs< Graph, DefDistMapTraits<T> > { |
---|
| 261 | typedef Bfs< Graph, DefDistMapTraits<T> > Create; |
---|
| 262 | }; |
---|
[1218] | 263 | |
---|
| 264 | template <class T> |
---|
| 265 | struct DefReachedMapTraits : public Traits { |
---|
| 266 | typedef T ReachedMap; |
---|
[1799] | 267 | static ReachedMap *createReachedMap(const Graph &) |
---|
[1218] | 268 | { |
---|
| 269 | throw UninitializedParameter(); |
---|
| 270 | } |
---|
| 271 | }; |
---|
| 272 | ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
---|
| 273 | |
---|
| 274 | ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
---|
| 275 | /// |
---|
| 276 | template <class T> |
---|
[1709] | 277 | struct DefReachedMap : public Bfs< Graph, DefReachedMapTraits<T> > { |
---|
| 278 | typedef Bfs< Graph, DefReachedMapTraits<T> > Create; |
---|
| 279 | }; |
---|
[1218] | 280 | |
---|
| 281 | template <class T> |
---|
| 282 | struct DefProcessedMapTraits : public Traits { |
---|
| 283 | typedef T ProcessedMap; |
---|
[2033] | 284 | static ProcessedMap *createProcessedMap(const Graph &) |
---|
[1218] | 285 | { |
---|
| 286 | throw UninitializedParameter(); |
---|
| 287 | } |
---|
| 288 | }; |
---|
| 289 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
| 290 | |
---|
| 291 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
| 292 | /// |
---|
| 293 | template <class T> |
---|
[1709] | 294 | struct DefProcessedMap : public Bfs< Graph, DefProcessedMapTraits<T> > { |
---|
| 295 | typedef Bfs< Graph, DefProcessedMapTraits<T> > Create; |
---|
| 296 | }; |
---|
[1218] | 297 | |
---|
| 298 | struct DefGraphProcessedMapTraits : public Traits { |
---|
| 299 | typedef typename Graph::template NodeMap<bool> ProcessedMap; |
---|
| 300 | static ProcessedMap *createProcessedMap(const Graph &G) |
---|
| 301 | { |
---|
| 302 | return new ProcessedMap(G); |
---|
| 303 | } |
---|
| 304 | }; |
---|
| 305 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 306 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
| 307 | /// |
---|
| 308 | ///\ref named-templ-param "Named parameter" |
---|
| 309 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
[1270] | 310 | ///If you don't set it explicitly, it will be automatically allocated. |
---|
[1218] | 311 | template <class T> |
---|
[1709] | 312 | struct DefProcessedMapToBeDefaultMap : |
---|
| 313 | public Bfs< Graph, DefGraphProcessedMapTraits> { |
---|
| 314 | typedef Bfs< Graph, DefGraphProcessedMapTraits> Create; |
---|
| 315 | }; |
---|
[1218] | 316 | |
---|
| 317 | ///@} |
---|
| 318 | |
---|
| 319 | public: |
---|
| 320 | |
---|
[802] | 321 | ///Constructor. |
---|
| 322 | |
---|
| 323 | ///\param _G the graph the algorithm will run on. |
---|
[911] | 324 | /// |
---|
[774] | 325 | Bfs(const Graph& _G) : |
---|
| 326 | G(&_G), |
---|
[1218] | 327 | _pred(NULL), local_pred(false), |
---|
| 328 | _dist(NULL), local_dist(false), |
---|
| 329 | _reached(NULL), local_reached(false), |
---|
| 330 | _processed(NULL), local_processed(false) |
---|
[774] | 331 | { } |
---|
| 332 | |
---|
[802] | 333 | ///Destructor. |
---|
[774] | 334 | ~Bfs() |
---|
| 335 | { |
---|
[1218] | 336 | if(local_pred) delete _pred; |
---|
| 337 | if(local_dist) delete _dist; |
---|
| 338 | if(local_reached) delete _reached; |
---|
| 339 | if(local_processed) delete _processed; |
---|
[774] | 340 | } |
---|
| 341 | |
---|
| 342 | ///Sets the map storing the predecessor edges. |
---|
| 343 | |
---|
| 344 | ///Sets the map storing the predecessor edges. |
---|
| 345 | ///If you don't use this function before calling \ref run(), |
---|
[1270] | 346 | ///it will allocate one. The destructor deallocates this |
---|
[774] | 347 | ///automatically allocated map, of course. |
---|
| 348 | ///\return <tt> (*this) </tt> |
---|
[1218] | 349 | Bfs &predMap(PredMap &m) |
---|
[774] | 350 | { |
---|
[1218] | 351 | if(local_pred) { |
---|
| 352 | delete _pred; |
---|
| 353 | local_pred=false; |
---|
[774] | 354 | } |
---|
[1218] | 355 | _pred = &m; |
---|
[774] | 356 | return *this; |
---|
| 357 | } |
---|
| 358 | |
---|
[1218] | 359 | ///Sets the map indicating the reached nodes. |
---|
[774] | 360 | |
---|
[1218] | 361 | ///Sets the map indicating the reached nodes. |
---|
[774] | 362 | ///If you don't use this function before calling \ref run(), |
---|
[1270] | 363 | ///it will allocate one. The destructor deallocates this |
---|
[774] | 364 | ///automatically allocated map, of course. |
---|
| 365 | ///\return <tt> (*this) </tt> |
---|
[1218] | 366 | Bfs &reachedMap(ReachedMap &m) |
---|
[774] | 367 | { |
---|
[1218] | 368 | if(local_reached) { |
---|
| 369 | delete _reached; |
---|
| 370 | local_reached=false; |
---|
[774] | 371 | } |
---|
[1218] | 372 | _reached = &m; |
---|
[774] | 373 | return *this; |
---|
| 374 | } |
---|
| 375 | |
---|
[1218] | 376 | ///Sets the map indicating the processed nodes. |
---|
| 377 | |
---|
| 378 | ///Sets the map indicating the processed nodes. |
---|
| 379 | ///If you don't use this function before calling \ref run(), |
---|
[1270] | 380 | ///it will allocate one. The destructor deallocates this |
---|
[1218] | 381 | ///automatically allocated map, of course. |
---|
| 382 | ///\return <tt> (*this) </tt> |
---|
| 383 | Bfs &processedMap(ProcessedMap &m) |
---|
| 384 | { |
---|
| 385 | if(local_processed) { |
---|
| 386 | delete _processed; |
---|
| 387 | local_processed=false; |
---|
| 388 | } |
---|
| 389 | _processed = &m; |
---|
| 390 | return *this; |
---|
| 391 | } |
---|
| 392 | |
---|
[774] | 393 | ///Sets the map storing the distances calculated by the algorithm. |
---|
| 394 | |
---|
| 395 | ///Sets the map storing the distances calculated by the algorithm. |
---|
| 396 | ///If you don't use this function before calling \ref run(), |
---|
[1270] | 397 | ///it will allocate one. The destructor deallocates this |
---|
[774] | 398 | ///automatically allocated map, of course. |
---|
| 399 | ///\return <tt> (*this) </tt> |
---|
[1218] | 400 | Bfs &distMap(DistMap &m) |
---|
[774] | 401 | { |
---|
[1218] | 402 | if(local_dist) { |
---|
| 403 | delete _dist; |
---|
| 404 | local_dist=false; |
---|
[774] | 405 | } |
---|
[1218] | 406 | _dist = &m; |
---|
[774] | 407 | return *this; |
---|
| 408 | } |
---|
| 409 | |
---|
[1218] | 410 | public: |
---|
| 411 | ///\name Execution control |
---|
| 412 | ///The simplest way to execute the algorithm is to use |
---|
| 413 | ///one of the member functions called \c run(...). |
---|
| 414 | ///\n |
---|
| 415 | ///If you need more control on the execution, |
---|
| 416 | ///first you must call \ref init(), then you can add several source nodes |
---|
| 417 | ///with \ref addSource(). |
---|
| 418 | ///Finally \ref start() will perform the actual path |
---|
| 419 | ///computation. |
---|
| 420 | |
---|
| 421 | ///@{ |
---|
| 422 | |
---|
| 423 | ///Initializes the internal data structures. |
---|
| 424 | |
---|
| 425 | ///Initializes the internal data structures. |
---|
| 426 | /// |
---|
| 427 | void init() |
---|
| 428 | { |
---|
| 429 | create_maps(); |
---|
| 430 | _queue.resize(countNodes(*G)); |
---|
| 431 | _queue_head=_queue_tail=0; |
---|
| 432 | _curr_dist=1; |
---|
[774] | 433 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
[1218] | 434 | _pred->set(u,INVALID); |
---|
| 435 | _reached->set(u,false); |
---|
| 436 | _processed->set(u,false); |
---|
[774] | 437 | } |
---|
| 438 | } |
---|
| 439 | |
---|
[1218] | 440 | ///Adds a new source node. |
---|
[774] | 441 | |
---|
[1218] | 442 | ///Adds a new source node to the set of nodes to be processed. |
---|
| 443 | /// |
---|
| 444 | void addSource(Node s) |
---|
| 445 | { |
---|
| 446 | if(!(*_reached)[s]) |
---|
| 447 | { |
---|
| 448 | _reached->set(s,true); |
---|
| 449 | _pred->set(s,INVALID); |
---|
| 450 | _dist->set(s,0); |
---|
| 451 | _queue[_queue_head++]=s; |
---|
| 452 | _queue_next_dist=_queue_head; |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | ///Processes the next node. |
---|
| 457 | |
---|
| 458 | ///Processes the next node. |
---|
| 459 | /// |
---|
[1516] | 460 | ///\return The processed node. |
---|
| 461 | /// |
---|
[1218] | 462 | ///\warning The queue must not be empty! |
---|
[1516] | 463 | Node processNextNode() |
---|
[1218] | 464 | { |
---|
| 465 | if(_queue_tail==_queue_next_dist) { |
---|
| 466 | _curr_dist++; |
---|
| 467 | _queue_next_dist=_queue_head; |
---|
| 468 | } |
---|
| 469 | Node n=_queue[_queue_tail++]; |
---|
| 470 | _processed->set(n,true); |
---|
| 471 | Node m; |
---|
| 472 | for(OutEdgeIt e(*G,n);e!=INVALID;++e) |
---|
| 473 | if(!(*_reached)[m=G->target(e)]) { |
---|
| 474 | _queue[_queue_head++]=m; |
---|
| 475 | _reached->set(m,true); |
---|
| 476 | _pred->set(m,e); |
---|
| 477 | _dist->set(m,_curr_dist); |
---|
| 478 | } |
---|
[1516] | 479 | return n; |
---|
[1218] | 480 | } |
---|
| 481 | |
---|
[1665] | 482 | ///Next node to be processed. |
---|
| 483 | |
---|
| 484 | ///Next node to be processed. |
---|
| 485 | /// |
---|
| 486 | ///\return The next node to be processed or INVALID if the queue is |
---|
| 487 | /// empty. |
---|
[1694] | 488 | Node nextNode() |
---|
[1665] | 489 | { |
---|
| 490 | return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
---|
| 491 | } |
---|
| 492 | |
---|
[1218] | 493 | ///\brief Returns \c false if there are nodes |
---|
| 494 | ///to be processed in the queue |
---|
| 495 | /// |
---|
| 496 | ///Returns \c false if there are nodes |
---|
| 497 | ///to be processed in the queue |
---|
| 498 | bool emptyQueue() { return _queue_tail==_queue_head; } |
---|
| 499 | ///Returns the number of the nodes to be processed. |
---|
| 500 | |
---|
| 501 | ///Returns the number of the nodes to be processed in the queue. |
---|
| 502 | /// |
---|
| 503 | int queueSize() { return _queue_head-_queue_tail; } |
---|
| 504 | |
---|
| 505 | ///Executes the algorithm. |
---|
| 506 | |
---|
| 507 | ///Executes the algorithm. |
---|
| 508 | /// |
---|
| 509 | ///\pre init() must be called and at least one node should be added |
---|
| 510 | ///with addSource() before using this function. |
---|
| 511 | /// |
---|
| 512 | ///This method runs the %BFS algorithm from the root node(s) |
---|
| 513 | ///in order to |
---|
| 514 | ///compute the |
---|
| 515 | ///shortest path to each node. The algorithm computes |
---|
| 516 | ///- The shortest path tree. |
---|
| 517 | ///- The distance of each node from the root(s). |
---|
| 518 | /// |
---|
| 519 | void start() |
---|
| 520 | { |
---|
| 521 | while ( !emptyQueue() ) processNextNode(); |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | ///Executes the algorithm until \c dest is reached. |
---|
| 525 | |
---|
| 526 | ///Executes the algorithm until \c dest is reached. |
---|
| 527 | /// |
---|
| 528 | ///\pre init() must be called and at least one node should be added |
---|
| 529 | ///with addSource() before using this function. |
---|
| 530 | /// |
---|
| 531 | ///This method runs the %BFS algorithm from the root node(s) |
---|
| 532 | ///in order to |
---|
| 533 | ///compute the |
---|
| 534 | ///shortest path to \c dest. The algorithm computes |
---|
| 535 | ///- The shortest path to \c dest. |
---|
| 536 | ///- The distance of \c dest from the root(s). |
---|
| 537 | /// |
---|
| 538 | void start(Node dest) |
---|
| 539 | { |
---|
| 540 | while ( !emptyQueue() && _queue[_queue_tail]!=dest ) processNextNode(); |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | ///Executes the algorithm until a condition is met. |
---|
| 544 | |
---|
| 545 | ///Executes the algorithm until a condition is met. |
---|
| 546 | /// |
---|
| 547 | ///\pre init() must be called and at least one node should be added |
---|
| 548 | ///with addSource() before using this function. |
---|
| 549 | /// |
---|
| 550 | ///\param nm must be a bool (or convertible) node map. The algorithm |
---|
| 551 | ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>. |
---|
| 552 | template<class NM> |
---|
[1755] | 553 | void start(const NM &nm) |
---|
| 554 | { |
---|
| 555 | while ( !emptyQueue() && !nm[_queue[_queue_tail]] ) processNextNode(); |
---|
| 556 | } |
---|
[1218] | 557 | |
---|
| 558 | ///Runs %BFS algorithm from node \c s. |
---|
| 559 | |
---|
| 560 | ///This method runs the %BFS algorithm from a root node \c s |
---|
| 561 | ///in order to |
---|
| 562 | ///compute the |
---|
| 563 | ///shortest path to each node. The algorithm computes |
---|
| 564 | ///- The shortest path tree. |
---|
| 565 | ///- The distance of each node from the root. |
---|
| 566 | /// |
---|
| 567 | ///\note d.run(s) is just a shortcut of the following code. |
---|
| 568 | ///\code |
---|
| 569 | /// d.init(); |
---|
| 570 | /// d.addSource(s); |
---|
| 571 | /// d.start(); |
---|
| 572 | ///\endcode |
---|
| 573 | void run(Node s) { |
---|
| 574 | init(); |
---|
| 575 | addSource(s); |
---|
| 576 | start(); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | ///Finds the shortest path between \c s and \c t. |
---|
| 580 | |
---|
| 581 | ///Finds the shortest path between \c s and \c t. |
---|
| 582 | /// |
---|
| 583 | ///\return The length of the shortest s---t path if there exists one, |
---|
| 584 | ///0 otherwise. |
---|
| 585 | ///\note Apart from the return value, d.run(s) is |
---|
| 586 | ///just a shortcut of the following code. |
---|
| 587 | ///\code |
---|
| 588 | /// d.init(); |
---|
| 589 | /// d.addSource(s); |
---|
| 590 | /// d.start(t); |
---|
| 591 | ///\endcode |
---|
| 592 | int run(Node s,Node t) { |
---|
| 593 | init(); |
---|
| 594 | addSource(s); |
---|
| 595 | start(t); |
---|
| 596 | return reached(t)?_curr_dist-1+(_queue_tail==_queue_next_dist):0; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | ///@} |
---|
| 600 | |
---|
| 601 | ///\name Query Functions |
---|
| 602 | ///The result of the %BFS algorithm can be obtained using these |
---|
| 603 | ///functions.\n |
---|
| 604 | ///Before the use of these functions, |
---|
| 605 | ///either run() or start() must be called. |
---|
| 606 | |
---|
| 607 | ///@{ |
---|
| 608 | |
---|
[1283] | 609 | ///Copies the shortest path to \c t into \c p |
---|
| 610 | |
---|
| 611 | ///This function copies the shortest path to \c t into \c p. |
---|
[1536] | 612 | ///If \c t is a source itself or unreachable, then it does not |
---|
[1283] | 613 | ///alter \c p. |
---|
| 614 | ///\return Returns \c true if a path to \c t was actually copied to \c p, |
---|
| 615 | ///\c false otherwise. |
---|
| 616 | ///\sa DirPath |
---|
| 617 | template<class P> |
---|
| 618 | bool getPath(P &p,Node t) |
---|
| 619 | { |
---|
| 620 | if(reached(t)) { |
---|
| 621 | p.clear(); |
---|
| 622 | typename P::Builder b(p); |
---|
[1763] | 623 | for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t)) |
---|
| 624 | b.pushFront(predEdge(t)); |
---|
[1283] | 625 | b.commit(); |
---|
| 626 | return true; |
---|
| 627 | } |
---|
| 628 | return false; |
---|
| 629 | } |
---|
| 630 | |
---|
[1218] | 631 | ///The distance of a node from the root(s). |
---|
| 632 | |
---|
| 633 | ///Returns the distance of a node from the root(s). |
---|
[774] | 634 | ///\pre \ref run() must be called before using this function. |
---|
[1218] | 635 | ///\warning If node \c v in unreachable from the root(s) the return value |
---|
[1270] | 636 | ///of this function is undefined. |
---|
[1218] | 637 | int dist(Node v) const { return (*_dist)[v]; } |
---|
[774] | 638 | |
---|
[1218] | 639 | ///Returns the 'previous edge' of the shortest path tree. |
---|
[774] | 640 | |
---|
[1218] | 641 | ///For a node \c v it returns the 'previous edge' |
---|
| 642 | ///of the shortest path tree, |
---|
| 643 | ///i.e. it returns the last edge of a shortest path from the root(s) to \c |
---|
[774] | 644 | ///v. It is \ref INVALID |
---|
[1218] | 645 | ///if \c v is unreachable from the root(s) or \c v is a root. The |
---|
| 646 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
[1631] | 647 | ///\ref predNode(). |
---|
[1218] | 648 | ///\pre Either \ref run() or \ref start() must be called before using |
---|
[774] | 649 | ///this function. |
---|
[1763] | 650 | Edge predEdge(Node v) const { return (*_pred)[v];} |
---|
[774] | 651 | |
---|
[1218] | 652 | ///Returns the 'previous node' of the shortest path tree. |
---|
[774] | 653 | |
---|
[1218] | 654 | ///For a node \c v it returns the 'previous node' |
---|
| 655 | ///of the shortest path tree, |
---|
[774] | 656 | ///i.e. it returns the last but one node from a shortest path from the |
---|
[1218] | 657 | ///root(a) to \c /v. |
---|
| 658 | ///It is INVALID if \c v is unreachable from the root(s) or |
---|
| 659 | ///if \c v itself a root. |
---|
| 660 | ///The shortest path tree used here is equal to the shortest path |
---|
[1763] | 661 | ///tree used in \ref predEdge(). |
---|
[1218] | 662 | ///\pre Either \ref run() or \ref start() must be called before |
---|
[774] | 663 | ///using this function. |
---|
[1218] | 664 | Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
---|
| 665 | G->source((*_pred)[v]); } |
---|
[774] | 666 | |
---|
| 667 | ///Returns a reference to the NodeMap of distances. |
---|
[1218] | 668 | |
---|
| 669 | ///Returns a reference to the NodeMap of distances. |
---|
| 670 | ///\pre Either \ref run() or \ref init() must |
---|
[774] | 671 | ///be called before using this function. |
---|
[1218] | 672 | const DistMap &distMap() const { return *_dist;} |
---|
[774] | 673 | |
---|
[1218] | 674 | ///Returns a reference to the shortest path tree map. |
---|
[774] | 675 | |
---|
| 676 | ///Returns a reference to the NodeMap of the edges of the |
---|
[1218] | 677 | ///shortest path tree. |
---|
| 678 | ///\pre Either \ref run() or \ref init() |
---|
| 679 | ///must be called before using this function. |
---|
| 680 | const PredMap &predMap() const { return *_pred;} |
---|
[774] | 681 | |
---|
| 682 | ///Checks if a node is reachable from the root. |
---|
| 683 | |
---|
| 684 | ///Returns \c true if \c v is reachable from the root. |
---|
[1270] | 685 | ///\warning The source nodes are indicated as unreached. |
---|
[1218] | 686 | ///\pre Either \ref run() or \ref start() |
---|
| 687 | ///must be called before using this function. |
---|
[774] | 688 | /// |
---|
[1218] | 689 | bool reached(Node v) { return (*_reached)[v]; } |
---|
| 690 | |
---|
| 691 | ///@} |
---|
| 692 | }; |
---|
| 693 | |
---|
| 694 | ///Default traits class of Bfs function. |
---|
| 695 | |
---|
| 696 | ///Default traits class of Bfs function. |
---|
| 697 | ///\param GR Graph type. |
---|
| 698 | template<class GR> |
---|
| 699 | struct BfsWizardDefaultTraits |
---|
| 700 | { |
---|
| 701 | ///The graph type the algorithm runs on. |
---|
| 702 | typedef GR Graph; |
---|
| 703 | ///\brief The type of the map that stores the last |
---|
| 704 | ///edges of the shortest paths. |
---|
| 705 | /// |
---|
| 706 | ///The type of the map that stores the last |
---|
| 707 | ///edges of the shortest paths. |
---|
| 708 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
[774] | 709 | /// |
---|
[1218] | 710 | typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap; |
---|
| 711 | ///Instantiates a PredMap. |
---|
| 712 | |
---|
| 713 | ///This function instantiates a \ref PredMap. |
---|
[1536] | 714 | ///\param g is the graph, to which we would like to define the PredMap. |
---|
[1218] | 715 | ///\todo The graph alone may be insufficient to initialize |
---|
[1536] | 716 | #ifdef DOXYGEN |
---|
| 717 | static PredMap *createPredMap(const GR &g) |
---|
| 718 | #else |
---|
[1367] | 719 | static PredMap *createPredMap(const GR &) |
---|
[1536] | 720 | #endif |
---|
[1218] | 721 | { |
---|
| 722 | return new PredMap(); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | ///The type of the map that indicates which nodes are processed. |
---|
| 726 | |
---|
| 727 | ///The type of the map that indicates which nodes are processed. |
---|
| 728 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 729 | ///\todo named parameter to set this type, function to read and write. |
---|
| 730 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
| 731 | ///Instantiates a ProcessedMap. |
---|
| 732 | |
---|
| 733 | ///This function instantiates a \ref ProcessedMap. |
---|
[1536] | 734 | ///\param g is the graph, to which |
---|
[1218] | 735 | ///we would like to define the \ref ProcessedMap |
---|
[1536] | 736 | #ifdef DOXYGEN |
---|
| 737 | static ProcessedMap *createProcessedMap(const GR &g) |
---|
| 738 | #else |
---|
[1367] | 739 | static ProcessedMap *createProcessedMap(const GR &) |
---|
[1536] | 740 | #endif |
---|
[1218] | 741 | { |
---|
| 742 | return new ProcessedMap(); |
---|
| 743 | } |
---|
| 744 | ///The type of the map that indicates which nodes are reached. |
---|
| 745 | |
---|
| 746 | ///The type of the map that indicates which nodes are reached. |
---|
| 747 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 748 | ///\todo named parameter to set this type, function to read and write. |
---|
| 749 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
| 750 | ///Instantiates a ReachedMap. |
---|
| 751 | |
---|
| 752 | ///This function instantiates a \ref ReachedMap. |
---|
| 753 | ///\param G is the graph, to which |
---|
| 754 | ///we would like to define the \ref ReachedMap. |
---|
| 755 | static ReachedMap *createReachedMap(const GR &G) |
---|
| 756 | { |
---|
| 757 | return new ReachedMap(G); |
---|
| 758 | } |
---|
| 759 | ///The type of the map that stores the dists of the nodes. |
---|
| 760 | |
---|
| 761 | ///The type of the map that stores the dists of the nodes. |
---|
| 762 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 763 | /// |
---|
| 764 | typedef NullMap<typename Graph::Node,int> DistMap; |
---|
| 765 | ///Instantiates a DistMap. |
---|
| 766 | |
---|
| 767 | ///This function instantiates a \ref DistMap. |
---|
[1536] | 768 | ///\param g is the graph, to which we would like to define the \ref DistMap |
---|
| 769 | #ifdef DOXYGEN |
---|
| 770 | static DistMap *createDistMap(const GR &g) |
---|
| 771 | #else |
---|
[1367] | 772 | static DistMap *createDistMap(const GR &) |
---|
[1536] | 773 | #endif |
---|
[1218] | 774 | { |
---|
| 775 | return new DistMap(); |
---|
| 776 | } |
---|
| 777 | }; |
---|
| 778 | |
---|
| 779 | /// Default traits used by \ref BfsWizard |
---|
| 780 | |
---|
| 781 | /// To make it easier to use Bfs algorithm |
---|
| 782 | ///we have created a wizard class. |
---|
| 783 | /// This \ref BfsWizard class needs default traits, |
---|
| 784 | ///as well as the \ref Bfs class. |
---|
| 785 | /// The \ref BfsWizardBase is a class to be the default traits of the |
---|
| 786 | /// \ref BfsWizard class. |
---|
| 787 | template<class GR> |
---|
| 788 | class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
---|
| 789 | { |
---|
| 790 | |
---|
| 791 | typedef BfsWizardDefaultTraits<GR> Base; |
---|
| 792 | protected: |
---|
| 793 | /// Type of the nodes in the graph. |
---|
| 794 | typedef typename Base::Graph::Node Node; |
---|
| 795 | |
---|
| 796 | /// Pointer to the underlying graph. |
---|
| 797 | void *_g; |
---|
| 798 | ///Pointer to the map of reached nodes. |
---|
| 799 | void *_reached; |
---|
| 800 | ///Pointer to the map of processed nodes. |
---|
| 801 | void *_processed; |
---|
| 802 | ///Pointer to the map of predecessors edges. |
---|
| 803 | void *_pred; |
---|
| 804 | ///Pointer to the map of distances. |
---|
| 805 | void *_dist; |
---|
| 806 | ///Pointer to the source node. |
---|
| 807 | Node _source; |
---|
| 808 | |
---|
| 809 | public: |
---|
| 810 | /// Constructor. |
---|
| 811 | |
---|
| 812 | /// This constructor does not require parameters, therefore it initiates |
---|
| 813 | /// all of the attributes to default values (0, INVALID). |
---|
| 814 | BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
---|
| 815 | _dist(0), _source(INVALID) {} |
---|
| 816 | |
---|
| 817 | /// Constructor. |
---|
| 818 | |
---|
| 819 | /// This constructor requires some parameters, |
---|
| 820 | /// listed in the parameters list. |
---|
| 821 | /// Others are initiated to 0. |
---|
| 822 | /// \param g is the initial value of \ref _g |
---|
| 823 | /// \param s is the initial value of \ref _source |
---|
| 824 | BfsWizardBase(const GR &g, Node s=INVALID) : |
---|
| 825 | _g((void *)&g), _reached(0), _processed(0), _pred(0), |
---|
| 826 | _dist(0), _source(s) {} |
---|
| 827 | |
---|
| 828 | }; |
---|
| 829 | |
---|
| 830 | /// A class to make the usage of Bfs algorithm easier |
---|
| 831 | |
---|
| 832 | /// This class is created to make it easier to use Bfs algorithm. |
---|
| 833 | /// It uses the functions and features of the plain \ref Bfs, |
---|
| 834 | /// but it is much simpler to use it. |
---|
| 835 | /// |
---|
| 836 | /// Simplicity means that the way to change the types defined |
---|
| 837 | /// in the traits class is based on functions that returns the new class |
---|
| 838 | /// and not on templatable built-in classes. |
---|
| 839 | /// When using the plain \ref Bfs |
---|
| 840 | /// the new class with the modified type comes from |
---|
| 841 | /// the original class by using the :: |
---|
| 842 | /// operator. In the case of \ref BfsWizard only |
---|
| 843 | /// a function have to be called and it will |
---|
| 844 | /// return the needed class. |
---|
| 845 | /// |
---|
| 846 | /// It does not have own \ref run method. When its \ref run method is called |
---|
| 847 | /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run |
---|
| 848 | /// method of it. |
---|
| 849 | template<class TR> |
---|
| 850 | class BfsWizard : public TR |
---|
| 851 | { |
---|
| 852 | typedef TR Base; |
---|
| 853 | |
---|
| 854 | ///The type of the underlying graph. |
---|
| 855 | typedef typename TR::Graph Graph; |
---|
| 856 | //\e |
---|
| 857 | typedef typename Graph::Node Node; |
---|
| 858 | //\e |
---|
| 859 | typedef typename Graph::NodeIt NodeIt; |
---|
| 860 | //\e |
---|
| 861 | typedef typename Graph::Edge Edge; |
---|
| 862 | //\e |
---|
| 863 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 864 | |
---|
| 865 | ///\brief The type of the map that stores |
---|
| 866 | ///the reached nodes |
---|
| 867 | typedef typename TR::ReachedMap ReachedMap; |
---|
| 868 | ///\brief The type of the map that stores |
---|
| 869 | ///the processed nodes |
---|
| 870 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
| 871 | ///\brief The type of the map that stores the last |
---|
| 872 | ///edges of the shortest paths. |
---|
| 873 | typedef typename TR::PredMap PredMap; |
---|
| 874 | ///The type of the map that stores the dists of the nodes. |
---|
| 875 | typedef typename TR::DistMap DistMap; |
---|
| 876 | |
---|
| 877 | public: |
---|
| 878 | /// Constructor. |
---|
| 879 | BfsWizard() : TR() {} |
---|
| 880 | |
---|
| 881 | /// Constructor that requires parameters. |
---|
| 882 | |
---|
| 883 | /// Constructor that requires parameters. |
---|
| 884 | /// These parameters will be the default values for the traits class. |
---|
| 885 | BfsWizard(const Graph &g, Node s=INVALID) : |
---|
| 886 | TR(g,s) {} |
---|
| 887 | |
---|
| 888 | ///Copy constructor |
---|
| 889 | BfsWizard(const TR &b) : TR(b) {} |
---|
| 890 | |
---|
| 891 | ~BfsWizard() {} |
---|
| 892 | |
---|
| 893 | ///Runs Bfs algorithm from a given node. |
---|
| 894 | |
---|
| 895 | ///Runs Bfs algorithm from a given node. |
---|
| 896 | ///The node can be given by the \ref source function. |
---|
| 897 | void run() |
---|
| 898 | { |
---|
| 899 | if(Base::_source==INVALID) throw UninitializedParameter(); |
---|
| 900 | Bfs<Graph,TR> alg(*(Graph*)Base::_g); |
---|
| 901 | if(Base::_reached) |
---|
| 902 | alg.reachedMap(*(ReachedMap*)Base::_reached); |
---|
| 903 | if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed); |
---|
| 904 | if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred); |
---|
| 905 | if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist); |
---|
| 906 | alg.run(Base::_source); |
---|
| 907 | } |
---|
| 908 | |
---|
| 909 | ///Runs Bfs algorithm from the given node. |
---|
| 910 | |
---|
| 911 | ///Runs Bfs algorithm from the given node. |
---|
| 912 | ///\param s is the given source. |
---|
| 913 | void run(Node s) |
---|
| 914 | { |
---|
| 915 | Base::_source=s; |
---|
| 916 | run(); |
---|
| 917 | } |
---|
| 918 | |
---|
| 919 | template<class T> |
---|
| 920 | struct DefPredMapBase : public Base { |
---|
| 921 | typedef T PredMap; |
---|
[1367] | 922 | static PredMap *createPredMap(const Graph &) { return 0; }; |
---|
[1236] | 923 | DefPredMapBase(const TR &b) : TR(b) {} |
---|
[1218] | 924 | }; |
---|
| 925 | |
---|
| 926 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 927 | ///function for setting PredMap |
---|
| 928 | /// |
---|
| 929 | /// \ref named-templ-param "Named parameter" |
---|
| 930 | ///function for setting PredMap |
---|
| 931 | /// |
---|
| 932 | template<class T> |
---|
| 933 | BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
| 934 | { |
---|
| 935 | Base::_pred=(void *)&t; |
---|
| 936 | return BfsWizard<DefPredMapBase<T> >(*this); |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | |
---|
| 940 | template<class T> |
---|
| 941 | struct DefReachedMapBase : public Base { |
---|
| 942 | typedef T ReachedMap; |
---|
[1367] | 943 | static ReachedMap *createReachedMap(const Graph &) { return 0; }; |
---|
[1236] | 944 | DefReachedMapBase(const TR &b) : TR(b) {} |
---|
[1218] | 945 | }; |
---|
| 946 | |
---|
| 947 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 948 | ///function for setting ReachedMap |
---|
| 949 | /// |
---|
| 950 | /// \ref named-templ-param "Named parameter" |
---|
| 951 | ///function for setting ReachedMap |
---|
| 952 | /// |
---|
| 953 | template<class T> |
---|
| 954 | BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
---|
| 955 | { |
---|
| 956 | Base::_pred=(void *)&t; |
---|
| 957 | return BfsWizard<DefReachedMapBase<T> >(*this); |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | |
---|
| 961 | template<class T> |
---|
| 962 | struct DefProcessedMapBase : public Base { |
---|
| 963 | typedef T ProcessedMap; |
---|
[1367] | 964 | static ProcessedMap *createProcessedMap(const Graph &) { return 0; }; |
---|
[1236] | 965 | DefProcessedMapBase(const TR &b) : TR(b) {} |
---|
[1218] | 966 | }; |
---|
| 967 | |
---|
| 968 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 969 | ///function for setting ProcessedMap |
---|
| 970 | /// |
---|
| 971 | /// \ref named-templ-param "Named parameter" |
---|
| 972 | ///function for setting ProcessedMap |
---|
| 973 | /// |
---|
| 974 | template<class T> |
---|
| 975 | BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
---|
| 976 | { |
---|
| 977 | Base::_pred=(void *)&t; |
---|
| 978 | return BfsWizard<DefProcessedMapBase<T> >(*this); |
---|
| 979 | } |
---|
| 980 | |
---|
| 981 | |
---|
| 982 | template<class T> |
---|
| 983 | struct DefDistMapBase : public Base { |
---|
| 984 | typedef T DistMap; |
---|
[1367] | 985 | static DistMap *createDistMap(const Graph &) { return 0; }; |
---|
[1236] | 986 | DefDistMapBase(const TR &b) : TR(b) {} |
---|
[1218] | 987 | }; |
---|
| 988 | |
---|
| 989 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 990 | ///function for setting DistMap type |
---|
| 991 | /// |
---|
| 992 | /// \ref named-templ-param "Named parameter" |
---|
| 993 | ///function for setting DistMap type |
---|
| 994 | /// |
---|
| 995 | template<class T> |
---|
| 996 | BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
| 997 | { |
---|
| 998 | Base::_dist=(void *)&t; |
---|
| 999 | return BfsWizard<DefDistMapBase<T> >(*this); |
---|
| 1000 | } |
---|
| 1001 | |
---|
| 1002 | /// Sets the source node, from which the Bfs algorithm runs. |
---|
| 1003 | |
---|
| 1004 | /// Sets the source node, from which the Bfs algorithm runs. |
---|
| 1005 | /// \param s is the source node. |
---|
| 1006 | BfsWizard<TR> &source(Node s) |
---|
| 1007 | { |
---|
| 1008 | Base::_source=s; |
---|
| 1009 | return *this; |
---|
| 1010 | } |
---|
[774] | 1011 | |
---|
| 1012 | }; |
---|
| 1013 | |
---|
[1218] | 1014 | ///Function type interface for Bfs algorithm. |
---|
| 1015 | |
---|
| 1016 | /// \ingroup flowalgs |
---|
| 1017 | ///Function type interface for Bfs algorithm. |
---|
| 1018 | /// |
---|
| 1019 | ///This function also has several |
---|
| 1020 | ///\ref named-templ-func-param "named parameters", |
---|
| 1021 | ///they are declared as the members of class \ref BfsWizard. |
---|
| 1022 | ///The following |
---|
| 1023 | ///example shows how to use these parameters. |
---|
| 1024 | ///\code |
---|
| 1025 | /// bfs(g,source).predMap(preds).run(); |
---|
| 1026 | ///\endcode |
---|
| 1027 | ///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
---|
| 1028 | ///to the end of the parameter list. |
---|
| 1029 | ///\sa BfsWizard |
---|
| 1030 | ///\sa Bfs |
---|
| 1031 | template<class GR> |
---|
| 1032 | BfsWizard<BfsWizardBase<GR> > |
---|
| 1033 | bfs(const GR &g,typename GR::Node s=INVALID) |
---|
| 1034 | { |
---|
| 1035 | return BfsWizard<BfsWizardBase<GR> >(g,s); |
---|
| 1036 | } |
---|
| 1037 | |
---|
[921] | 1038 | } //END OF NAMESPACE LEMON |
---|
[774] | 1039 | |
---|
| 1040 | #endif |
---|
| 1041 | |
---|