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