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