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