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