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