0
4
11
1597
346
1543
1209
3179
141
130
123
134
1
| 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 |
| 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_GRAPH_UTILS_H |
|
| 20 |
#define LEMON_GRAPH_UTILS_H |
|
| 21 |
|
|
| 22 |
#include <iterator> |
|
| 23 |
#include <vector> |
|
| 24 |
#include <map> |
|
| 25 |
#include <cmath> |
|
| 26 |
#include <algorithm> |
|
| 27 |
|
|
| 28 |
#include <lemon/bits/invalid.h> |
|
| 29 |
#include <lemon/bits/utility.h> |
|
| 30 |
#include <lemon/maps.h> |
|
| 31 |
#include <lemon/bits/traits.h> |
|
| 32 |
|
|
| 33 |
#include <lemon/bits/alteration_notifier.h> |
|
| 34 |
#include <lemon/bits/default_map.h> |
|
| 35 |
|
|
| 36 |
///\ingroup gutils |
|
| 37 |
///\file |
|
| 38 |
///\brief Digraph utilities. |
|
| 39 |
|
|
| 40 |
namespace lemon {
|
|
| 41 |
|
|
| 42 |
/// \addtogroup gutils |
|
| 43 |
/// @{
|
|
| 44 |
|
|
| 45 |
///Creates convenience typedefs for the digraph types and iterators |
|
| 46 |
|
|
| 47 |
///This \c \#define creates convenience typedefs for the following types |
|
| 48 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
|
| 49 |
///\c OutArcIt |
|
| 50 |
///\note If \c G it a template parameter, it should be used in this way. |
|
| 51 |
///\code |
|
| 52 |
/// GRAPH_TYPEDEFS(typename G); |
|
| 53 |
///\endcode |
|
| 54 |
/// |
|
| 55 |
///\warning There are no typedefs for the digraph maps because of the lack of |
|
| 56 |
///template typedefs in C++. |
|
| 57 |
#define GRAPH_TYPEDEFS(Digraph) \ |
|
| 58 |
typedef Digraph:: Node Node; \ |
|
| 59 |
typedef Digraph:: NodeIt NodeIt; \ |
|
| 60 |
typedef Digraph:: Arc Arc; \ |
|
| 61 |
typedef Digraph:: ArcIt ArcIt; \ |
|
| 62 |
typedef Digraph:: InArcIt InArcIt; \ |
|
| 63 |
typedef Digraph::OutArcIt OutArcIt |
|
| 64 |
|
|
| 65 |
///Creates convenience typedefs for the graph types and iterators |
|
| 66 |
|
|
| 67 |
///This \c \#define creates the same convenience typedefs as defined by |
|
| 68 |
///\ref GRAPH_TYPEDEFS(Digraph) and three more, namely it creates |
|
| 69 |
///\c Edge, \c EdgeIt, \c IncArcIt, |
|
| 70 |
/// |
|
| 71 |
///\note If \c G it a template parameter, it should be used in this way. |
|
| 72 |
///\code |
|
| 73 |
/// UGRAPH_TYPEDEFS(typename G); |
|
| 74 |
///\endcode |
|
| 75 |
/// |
|
| 76 |
///\warning There are no typedefs for the digraph maps because of the lack of |
|
| 77 |
///template typedefs in C++. |
|
| 78 |
#define UGRAPH_TYPEDEFS(Digraph) \ |
|
| 79 |
GRAPH_TYPEDEFS(Digraph); \ |
|
| 80 |
typedef Digraph:: Edge Edge; \ |
|
| 81 |
typedef Digraph:: EdgeIt EdgeIt; \ |
|
| 82 |
typedef Digraph:: IncArcIt IncArcIt |
|
| 83 |
|
|
| 84 |
///\brief Creates convenience typedefs for the bipartite digraph |
|
| 85 |
///types and iterators |
|
| 86 |
|
|
| 87 |
///This \c \#define creates the same convenience typedefs as defined by |
|
| 88 |
///\ref UGRAPH_TYPEDEFS(Digraph) and two more, namely it creates |
|
| 89 |
///\c RedIt, \c BlueIt, |
|
| 90 |
/// |
|
| 91 |
///\note If \c G it a template parameter, it should be used in this way. |
|
| 92 |
///\code |
|
| 93 |
/// BPUGRAPH_TYPEDEFS(typename G); |
|
| 94 |
///\endcode |
|
| 95 |
/// |
|
| 96 |
///\warning There are no typedefs for the digraph maps because of the lack of |
|
| 97 |
///template typedefs in C++. |
|
| 98 |
#define BPUGRAPH_TYPEDEFS(Digraph) \ |
|
| 99 |
UGRAPH_TYPEDEFS(Digraph); \ |
|
| 100 |
typedef Digraph::Red Red; \ |
|
| 101 |
typedef Digraph::Blue Blue; \ |
|
| 102 |
typedef Digraph::RedIt RedIt; \ |
|
| 103 |
typedef Digraph::BlueIt BlueIt |
|
| 104 |
|
|
| 105 |
/// \brief Function to count the items in the digraph. |
|
| 106 |
/// |
|
| 107 |
/// This function counts the items (nodes, arcs etc) in the digraph. |
|
| 108 |
/// The complexity of the function is O(n) because |
|
| 109 |
/// it iterates on all of the items. |
|
| 110 |
|
|
| 111 |
template <typename Digraph, typename Item> |
|
| 112 |
inline int countItems(const Digraph& g) {
|
|
| 113 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
| 114 |
int num = 0; |
|
| 115 |
for (ItemIt it(g); it != INVALID; ++it) {
|
|
| 116 |
++num; |
|
| 117 |
} |
|
| 118 |
return num; |
|
| 119 |
} |
|
| 120 |
|
|
| 121 |
// Node counting: |
|
| 122 |
|
|
| 123 |
namespace _digraph_utils_bits {
|
|
| 124 |
|
|
| 125 |
template <typename Digraph, typename Enable = void> |
|
| 126 |
struct CountNodesSelector {
|
|
| 127 |
static int count(const Digraph &g) {
|
|
| 128 |
return countItems<Digraph, typename Digraph::Node>(g); |
|
| 129 |
} |
|
| 130 |
}; |
|
| 131 |
|
|
| 132 |
template <typename Digraph> |
|
| 133 |
struct CountNodesSelector< |
|
| 134 |
Digraph, typename |
|
| 135 |
enable_if<typename Digraph::NodeNumTag, void>::type> |
|
| 136 |
{
|
|
| 137 |
static int count(const Digraph &g) {
|
|
| 138 |
return g.nodeNum(); |
|
| 139 |
} |
|
| 140 |
}; |
|
| 141 |
} |
|
| 142 |
|
|
| 143 |
/// \brief Function to count the nodes in the digraph. |
|
| 144 |
/// |
|
| 145 |
/// This function counts the nodes in the digraph. |
|
| 146 |
/// The complexity of the function is O(n) but for some |
|
| 147 |
/// digraph structures it is specialized to run in O(1). |
|
| 148 |
/// |
|
| 149 |
/// If the digraph contains a \e nodeNum() member function and a |
|
| 150 |
/// \e NodeNumTag tag then this function calls directly the member |
|
| 151 |
/// function to query the cardinality of the node set. |
|
| 152 |
template <typename Digraph> |
|
| 153 |
inline int countNodes(const Digraph& g) {
|
|
| 154 |
return _digraph_utils_bits::CountNodesSelector<Digraph>::count(g); |
|
| 155 |
} |
|
| 156 |
|
|
| 157 |
namespace _digraph_utils_bits {
|
|
| 158 |
|
|
| 159 |
template <typename Digraph, typename Enable = void> |
|
| 160 |
struct CountRedsSelector {
|
|
| 161 |
static int count(const Digraph &g) {
|
|
| 162 |
return countItems<Digraph, typename Digraph::Red>(g); |
|
| 163 |
} |
|
| 164 |
}; |
|
| 165 |
|
|
| 166 |
template <typename Digraph> |
|
| 167 |
struct CountRedsSelector< |
|
| 168 |
Digraph, typename |
|
| 169 |
enable_if<typename Digraph::NodeNumTag, void>::type> |
|
| 170 |
{
|
|
| 171 |
static int count(const Digraph &g) {
|
|
| 172 |
return g.redNum(); |
|
| 173 |
} |
|
| 174 |
}; |
|
| 175 |
} |
|
| 176 |
|
|
| 177 |
/// \brief Function to count the reds in the digraph. |
|
| 178 |
/// |
|
| 179 |
/// This function counts the reds in the digraph. |
|
| 180 |
/// The complexity of the function is O(an) but for some |
|
| 181 |
/// digraph structures it is specialized to run in O(1). |
|
| 182 |
/// |
|
| 183 |
/// If the digraph contains an \e redNum() member function and a |
|
| 184 |
/// \e NodeNumTag tag then this function calls directly the member |
|
| 185 |
/// function to query the cardinality of the A-node set. |
|
| 186 |
template <typename Digraph> |
|
| 187 |
inline int countReds(const Digraph& g) {
|
|
| 188 |
return _digraph_utils_bits::CountRedsSelector<Digraph>::count(g); |
|
| 189 |
} |
|
| 190 |
|
|
| 191 |
namespace _digraph_utils_bits {
|
|
| 192 |
|
|
| 193 |
template <typename Digraph, typename Enable = void> |
|
| 194 |
struct CountBluesSelector {
|
|
| 195 |
static int count(const Digraph &g) {
|
|
| 196 |
return countItems<Digraph, typename Digraph::Blue>(g); |
|
| 197 |
} |
|
| 198 |
}; |
|
| 199 |
|
|
| 200 |
template <typename Digraph> |
|
| 201 |
struct CountBluesSelector< |
|
| 202 |
Digraph, typename |
|
| 203 |
enable_if<typename Digraph::NodeNumTag, void>::type> |
|
| 204 |
{
|
|
| 205 |
static int count(const Digraph &g) {
|
|
| 206 |
return g.blueNum(); |
|
| 207 |
} |
|
| 208 |
}; |
|
| 209 |
} |
|
| 210 |
|
|
| 211 |
/// \brief Function to count the blues in the digraph. |
|
| 212 |
/// |
|
| 213 |
/// This function counts the blues in the digraph. |
|
| 214 |
/// The complexity of the function is O(bn) but for some |
|
| 215 |
/// digraph structures it is specialized to run in O(1). |
|
| 216 |
/// |
|
| 217 |
/// If the digraph contains a \e blueNum() member function and a |
|
| 218 |
/// \e NodeNumTag tag then this function calls directly the member |
|
| 219 |
/// function to query the cardinality of the B-node set. |
|
| 220 |
template <typename Digraph> |
|
| 221 |
inline int countBlues(const Digraph& g) {
|
|
| 222 |
return _digraph_utils_bits::CountBluesSelector<Digraph>::count(g); |
|
| 223 |
} |
|
| 224 |
|
|
| 225 |
|
|
| 226 |
// Arc counting: |
|
| 227 |
|
|
| 228 |
namespace _digraph_utils_bits {
|
|
| 229 |
|
|
| 230 |
template <typename Digraph, typename Enable = void> |
|
| 231 |
struct CountArcsSelector {
|
|
| 232 |
static int count(const Digraph &g) {
|
|
| 233 |
return countItems<Digraph, typename Digraph::Arc>(g); |
|
| 234 |
} |
|
| 235 |
}; |
|
| 236 |
|
|
| 237 |
template <typename Digraph> |
|
| 238 |
struct CountArcsSelector< |
|
| 239 |
Digraph, |
|
| 240 |
typename enable_if<typename Digraph::ArcNumTag, void>::type> |
|
| 241 |
{
|
|
| 242 |
static int count(const Digraph &g) {
|
|
| 243 |
return g.arcNum(); |
|
| 244 |
} |
|
| 245 |
}; |
|
| 246 |
} |
|
| 247 |
|
|
| 248 |
/// \brief Function to count the arcs in the digraph. |
|
| 249 |
/// |
|
| 250 |
/// This function counts the arcs in the digraph. |
|
| 251 |
/// The complexity of the function is O(e) but for some |
|
| 252 |
/// digraph structures it is specialized to run in O(1). |
|
| 253 |
/// |
|
| 254 |
/// If the digraph contains a \e arcNum() member function and a |
|
| 255 |
/// \e ArcNumTag tag then this function calls directly the member |
|
| 256 |
/// function to query the cardinality of the arc set. |
|
| 257 |
template <typename Digraph> |
|
| 258 |
inline int countArcs(const Digraph& g) {
|
|
| 259 |
return _digraph_utils_bits::CountArcsSelector<Digraph>::count(g); |
|
| 260 |
} |
|
| 261 |
|
|
| 262 |
// Undirected arc counting: |
|
| 263 |
namespace _digraph_utils_bits {
|
|
| 264 |
|
|
| 265 |
template <typename Digraph, typename Enable = void> |
|
| 266 |
struct CountEdgesSelector {
|
|
| 267 |
static int count(const Digraph &g) {
|
|
| 268 |
return countItems<Digraph, typename Digraph::Edge>(g); |
|
| 269 |
} |
|
| 270 |
}; |
|
| 271 |
|
|
| 272 |
template <typename Digraph> |
|
| 273 |
struct CountEdgesSelector< |
|
| 274 |
Digraph, |
|
| 275 |
typename enable_if<typename Digraph::ArcNumTag, void>::type> |
|
| 276 |
{
|
|
| 277 |
static int count(const Digraph &g) {
|
|
| 278 |
return g.edgeNum(); |
|
| 279 |
} |
|
| 280 |
}; |
|
| 281 |
} |
|
| 282 |
|
|
| 283 |
/// \brief Function to count the edges in the digraph. |
|
| 284 |
/// |
|
| 285 |
/// This function counts the edges in the digraph. |
|
| 286 |
/// The complexity of the function is O(e) but for some |
|
| 287 |
/// digraph structures it is specialized to run in O(1). |
|
| 288 |
/// |
|
| 289 |
/// If the digraph contains a \e edgeNum() member function and a |
|
| 290 |
/// \e ArcNumTag tag then this function calls directly the member |
|
| 291 |
/// function to query the cardinality of the edge set. |
|
| 292 |
template <typename Digraph> |
|
| 293 |
inline int countEdges(const Digraph& g) {
|
|
| 294 |
return _digraph_utils_bits::CountEdgesSelector<Digraph>::count(g); |
|
| 295 |
|
|
| 296 |
} |
|
| 297 |
|
|
| 298 |
|
|
| 299 |
template <typename Digraph, typename DegIt> |
|
| 300 |
inline int countNodeDegree(const Digraph& _g, const typename Digraph::Node& _n) {
|
|
| 301 |
int num = 0; |
|
| 302 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
|
| 303 |
++num; |
|
| 304 |
} |
|
| 305 |
return num; |
|
| 306 |
} |
|
| 307 |
|
|
| 308 |
/// \brief Function to count the number of the out-arcs from node \c n. |
|
| 309 |
/// |
|
| 310 |
/// This function counts the number of the out-arcs from node \c n |
|
| 311 |
/// in the digraph. |
|
| 312 |
template <typename Digraph> |
|
| 313 |
inline int countOutArcs(const Digraph& _g, const typename Digraph::Node& _n) {
|
|
| 314 |
return countNodeDegree<Digraph, typename Digraph::OutArcIt>(_g, _n); |
|
| 315 |
} |
|
| 316 |
|
|
| 317 |
/// \brief Function to count the number of the in-arcs to node \c n. |
|
| 318 |
/// |
|
| 319 |
/// This function counts the number of the in-arcs to node \c n |
|
| 320 |
/// in the digraph. |
|
| 321 |
template <typename Digraph> |
|
| 322 |
inline int countInArcs(const Digraph& _g, const typename Digraph::Node& _n) {
|
|
| 323 |
return countNodeDegree<Digraph, typename Digraph::InArcIt>(_g, _n); |
|
| 324 |
} |
|
| 325 |
|
|
| 326 |
/// \brief Function to count the number of the inc-arcs to node \c n. |
|
| 327 |
/// |
|
| 328 |
/// This function counts the number of the inc-arcs to node \c n |
|
| 329 |
/// in the digraph. |
|
| 330 |
template <typename Digraph> |
|
| 331 |
inline int countIncArcs(const Digraph& _g, const typename Digraph::Node& _n) {
|
|
| 332 |
return countNodeDegree<Digraph, typename Digraph::IncArcIt>(_g, _n); |
|
| 333 |
} |
|
| 334 |
|
|
| 335 |
namespace _digraph_utils_bits {
|
|
| 336 |
|
|
| 337 |
template <typename Digraph, typename Enable = void> |
|
| 338 |
struct FindArcSelector {
|
|
| 339 |
typedef typename Digraph::Node Node; |
|
| 340 |
typedef typename Digraph::Arc Arc; |
|
| 341 |
static Arc find(const Digraph &g, Node u, Node v, Arc e) {
|
|
| 342 |
if (e == INVALID) {
|
|
| 343 |
g.firstOut(e, u); |
|
| 344 |
} else {
|
|
| 345 |
g.nextOut(e); |
|
| 346 |
} |
|
| 347 |
while (e != INVALID && g.target(e) != v) {
|
|
| 348 |
g.nextOut(e); |
|
| 349 |
} |
|
| 350 |
return e; |
|
| 351 |
} |
|
| 352 |
}; |
|
| 353 |
|
|
| 354 |
template <typename Digraph> |
|
| 355 |
struct FindArcSelector< |
|
| 356 |
Digraph, |
|
| 357 |
typename enable_if<typename Digraph::FindArcTag, void>::type> |
|
| 358 |
{
|
|
| 359 |
typedef typename Digraph::Node Node; |
|
| 360 |
typedef typename Digraph::Arc Arc; |
|
| 361 |
static Arc find(const Digraph &g, Node u, Node v, Arc prev) {
|
|
| 362 |
return g.findArc(u, v, prev); |
|
| 363 |
} |
|
| 364 |
}; |
|
| 365 |
} |
|
| 366 |
|
|
| 367 |
/// \brief Finds an arc between two nodes of a digraph. |
|
| 368 |
/// |
|
| 369 |
/// Finds an arc from node \c u to node \c v in digraph \c g. |
|
| 370 |
/// |
|
| 371 |
/// If \c prev is \ref INVALID (this is the default value), then |
|
| 372 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
| 373 |
/// the next arc from \c u to \c v after \c prev. |
|
| 374 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
|
| 375 |
/// |
|
| 376 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
|
| 377 |
///\code |
|
| 378 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
|
|
| 379 |
/// ... |
|
| 380 |
/// } |
|
| 381 |
///\endcode |
|
| 382 |
/// |
|
| 383 |
///\sa ArcLookUp |
|
| 384 |
///\sa AllArcLookUp |
|
| 385 |
///\sa DynArcLookUp |
|
| 386 |
///\sa ConArcIt |
|
| 387 |
template <typename Digraph> |
|
| 388 |
inline typename Digraph::Arc |
|
| 389 |
findArc(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v, |
|
| 390 |
typename Digraph::Arc prev = INVALID) {
|
|
| 391 |
return _digraph_utils_bits::FindArcSelector<Digraph>::find(g, u, v, prev); |
|
| 392 |
} |
|
| 393 |
|
|
| 394 |
/// \brief Iterator for iterating on arcs connected the same nodes. |
|
| 395 |
/// |
|
| 396 |
/// Iterator for iterating on arcs connected the same nodes. It is |
|
| 397 |
/// higher level interface for the findArc() function. You can |
|
| 398 |
/// use it the following way: |
|
| 399 |
///\code |
|
| 400 |
/// for (ConArcIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
|
|
| 401 |
/// ... |
|
| 402 |
/// } |
|
| 403 |
///\endcode |
|
| 404 |
/// |
|
| 405 |
///\sa findArc() |
|
| 406 |
///\sa ArcLookUp |
|
| 407 |
///\sa AllArcLookUp |
|
| 408 |
///\sa DynArcLookUp |
|
| 409 |
/// |
|
| 410 |
/// \author Balazs Dezso |
|
| 411 |
template <typename _Digraph> |
|
| 412 |
class ConArcIt : public _Digraph::Arc {
|
|
| 413 |
public: |
|
| 414 |
|
|
| 415 |
typedef _Digraph Digraph; |
|
| 416 |
typedef typename Digraph::Arc Parent; |
|
| 417 |
|
|
| 418 |
typedef typename Digraph::Arc Arc; |
|
| 419 |
typedef typename Digraph::Node Node; |
|
| 420 |
|
|
| 421 |
/// \brief Constructor. |
|
| 422 |
/// |
|
| 423 |
/// Construct a new ConArcIt iterating on the arcs which |
|
| 424 |
/// connects the \c u and \c v node. |
|
| 425 |
ConArcIt(const Digraph& g, Node u, Node v) : digraph(g) {
|
|
| 426 |
Parent::operator=(findArc(digraph, u, v)); |
|
| 427 |
} |
|
| 428 |
|
|
| 429 |
/// \brief Constructor. |
|
| 430 |
/// |
|
| 431 |
/// Construct a new ConArcIt which continues the iterating from |
|
| 432 |
/// the \c e arc. |
|
| 433 |
ConArcIt(const Digraph& g, Arc e) : Parent(e), digraph(g) {}
|
|
| 434 |
|
|
| 435 |
/// \brief Increment operator. |
|
| 436 |
/// |
|
| 437 |
/// It increments the iterator and gives back the next arc. |
|
| 438 |
ConArcIt& operator++() {
|
|
| 439 |
Parent::operator=(findArc(digraph, digraph.source(*this), |
|
| 440 |
digraph.target(*this), *this)); |
|
| 441 |
return *this; |
|
| 442 |
} |
|
| 443 |
private: |
|
| 444 |
const Digraph& digraph; |
|
| 445 |
}; |
|
| 446 |
|
|
| 447 |
namespace _digraph_utils_bits {
|
|
| 448 |
|
|
| 449 |
template <typename Digraph, typename Enable = void> |
|
| 450 |
struct FindEdgeSelector {
|
|
| 451 |
typedef typename Digraph::Node Node; |
|
| 452 |
typedef typename Digraph::Edge Edge; |
|
| 453 |
static Edge find(const Digraph &g, Node u, Node v, Edge e) {
|
|
| 454 |
bool b; |
|
| 455 |
if (u != v) {
|
|
| 456 |
if (e == INVALID) {
|
|
| 457 |
g.firstInc(e, b, u); |
|
| 458 |
} else {
|
|
| 459 |
b = g.source(e) == u; |
|
| 460 |
g.nextInc(e, b); |
|
| 461 |
} |
|
| 462 |
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
|
|
| 463 |
g.nextInc(e, b); |
|
| 464 |
} |
|
| 465 |
} else {
|
|
| 466 |
if (e == INVALID) {
|
|
| 467 |
g.firstInc(e, b, u); |
|
| 468 |
} else {
|
|
| 469 |
b = true; |
|
| 470 |
g.nextInc(e, b); |
|
| 471 |
} |
|
| 472 |
while (e != INVALID && (!b || g.target(e) != v)) {
|
|
| 473 |
g.nextInc(e, b); |
|
| 474 |
} |
|
| 475 |
} |
|
| 476 |
return e; |
|
| 477 |
} |
|
| 478 |
}; |
|
| 479 |
|
|
| 480 |
template <typename Digraph> |
|
| 481 |
struct FindEdgeSelector< |
|
| 482 |
Digraph, |
|
| 483 |
typename enable_if<typename Digraph::FindArcTag, void>::type> |
|
| 484 |
{
|
|
| 485 |
typedef typename Digraph::Node Node; |
|
| 486 |
typedef typename Digraph::Edge Edge; |
|
| 487 |
static Edge find(const Digraph &g, Node u, Node v, Edge prev) {
|
|
| 488 |
return g.findEdge(u, v, prev); |
|
| 489 |
} |
|
| 490 |
}; |
|
| 491 |
} |
|
| 492 |
|
|
| 493 |
/// \brief Finds an edge between two nodes of a digraph. |
|
| 494 |
/// |
|
| 495 |
/// Finds an edge from node \c u to node \c v in digraph \c g. |
|
| 496 |
/// If the node \c u and node \c v is equal then each loop arc |
|
| 497 |
/// will be enumerated. |
|
| 498 |
/// |
|
| 499 |
/// If \c prev is \ref INVALID (this is the default value), then |
|
| 500 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
| 501 |
/// the next arc from \c u to \c v after \c prev. |
|
| 502 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
|
| 503 |
/// |
|
| 504 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
|
| 505 |
///\code |
|
| 506 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
|
| 507 |
/// e = findEdge(g,u,v,e)) {
|
|
| 508 |
/// ... |
|
| 509 |
/// } |
|
| 510 |
///\endcode |
|
| 511 |
/// |
|
| 512 |
///\sa ConArcIt |
|
| 513 |
|
|
| 514 |
template <typename Digraph> |
|
| 515 |
inline typename Digraph::Edge |
|
| 516 |
findEdge(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v, |
|
| 517 |
typename Digraph::Edge p = INVALID) {
|
|
| 518 |
return _digraph_utils_bits::FindEdgeSelector<Digraph>::find(g, u, v, p); |
|
| 519 |
} |
|
| 520 |
|
|
| 521 |
/// \brief Iterator for iterating on edges connected the same nodes. |
|
| 522 |
/// |
|
| 523 |
/// Iterator for iterating on edges connected the same nodes. It is |
|
| 524 |
/// higher level interface for the findEdge() function. You can |
|
| 525 |
/// use it the following way: |
|
| 526 |
///\code |
|
| 527 |
/// for (ConEdgeIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
|
|
| 528 |
/// ... |
|
| 529 |
/// } |
|
| 530 |
///\endcode |
|
| 531 |
/// |
|
| 532 |
///\sa findEdge() |
|
| 533 |
/// |
|
| 534 |
/// \author Balazs Dezso |
|
| 535 |
template <typename _Digraph> |
|
| 536 |
class ConEdgeIt : public _Digraph::Edge {
|
|
| 537 |
public: |
|
| 538 |
|
|
| 539 |
typedef _Digraph Digraph; |
|
| 540 |
typedef typename Digraph::Edge Parent; |
|
| 541 |
|
|
| 542 |
typedef typename Digraph::Edge Edge; |
|
| 543 |
typedef typename Digraph::Node Node; |
|
| 544 |
|
|
| 545 |
/// \brief Constructor. |
|
| 546 |
/// |
|
| 547 |
/// Construct a new ConEdgeIt iterating on the arcs which |
|
| 548 |
/// connects the \c u and \c v node. |
|
| 549 |
ConEdgeIt(const Digraph& g, Node u, Node v) : digraph(g) {
|
|
| 550 |
Parent::operator=(findEdge(digraph, u, v)); |
|
| 551 |
} |
|
| 552 |
|
|
| 553 |
/// \brief Constructor. |
|
| 554 |
/// |
|
| 555 |
/// Construct a new ConEdgeIt which continues the iterating from |
|
| 556 |
/// the \c e arc. |
|
| 557 |
ConEdgeIt(const Digraph& g, Edge e) : Parent(e), digraph(g) {}
|
|
| 558 |
|
|
| 559 |
/// \brief Increment operator. |
|
| 560 |
/// |
|
| 561 |
/// It increments the iterator and gives back the next arc. |
|
| 562 |
ConEdgeIt& operator++() {
|
|
| 563 |
Parent::operator=(findEdge(digraph, digraph.source(*this), |
|
| 564 |
digraph.target(*this), *this)); |
|
| 565 |
return *this; |
|
| 566 |
} |
|
| 567 |
private: |
|
| 568 |
const Digraph& digraph; |
|
| 569 |
}; |
|
| 570 |
|
|
| 571 |
/// \brief Copy a map. |
|
| 572 |
/// |
|
| 573 |
/// This function copies the \c from map to the \c to map. It uses the |
|
| 574 |
/// given iterator to iterate on the data structure and it uses the \c ref |
|
| 575 |
/// mapping to convert the from's keys to the to's keys. |
|
| 576 |
template <typename To, typename From, |
|
| 577 |
typename ItemIt, typename Ref> |
|
| 578 |
void copyMap(To& to, const From& from, |
|
| 579 |
ItemIt it, const Ref& ref) {
|
|
| 580 |
for (; it != INVALID; ++it) {
|
|
| 581 |
to[ref[it]] = from[it]; |
|
| 582 |
} |
|
| 583 |
} |
|
| 584 |
|
|
| 585 |
/// \brief Copy the from map to the to map. |
|
| 586 |
/// |
|
| 587 |
/// Copy the \c from map to the \c to map. It uses the given iterator |
|
| 588 |
/// to iterate on the data structure. |
|
| 589 |
template <typename To, typename From, typename ItemIt> |
|
| 590 |
void copyMap(To& to, const From& from, ItemIt it) {
|
|
| 591 |
for (; it != INVALID; ++it) {
|
|
| 592 |
to[it] = from[it]; |
|
| 593 |
} |
|
| 594 |
} |
|
| 595 |
|
|
| 596 |
namespace _digraph_utils_bits {
|
|
| 597 |
|
|
| 598 |
template <typename Digraph, typename Item, typename RefMap> |
|
| 599 |
class MapCopyBase {
|
|
| 600 |
public: |
|
| 601 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
|
| 602 |
|
|
| 603 |
virtual ~MapCopyBase() {}
|
|
| 604 |
}; |
|
| 605 |
|
|
| 606 |
template <typename Digraph, typename Item, typename RefMap, |
|
| 607 |
typename ToMap, typename FromMap> |
|
| 608 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
|
| 609 |
public: |
|
| 610 |
|
|
| 611 |
MapCopy(ToMap& tmap, const FromMap& map) |
|
| 612 |
: _tmap(tmap), _map(map) {}
|
|
| 613 |
|
|
| 614 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
|
| 615 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
| 616 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
|
| 617 |
_tmap.set(refMap[it], _map[it]); |
|
| 618 |
} |
|
| 619 |
} |
|
| 620 |
|
|
| 621 |
private: |
|
| 622 |
ToMap& _tmap; |
|
| 623 |
const FromMap& _map; |
|
| 624 |
}; |
|
| 625 |
|
|
| 626 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
|
| 627 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
|
| 628 |
public: |
|
| 629 |
|
|
| 630 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
|
|
| 631 |
|
|
| 632 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
|
| 633 |
_it = refMap[_item]; |
|
| 634 |
} |
|
| 635 |
|
|
| 636 |
private: |
|
| 637 |
It& _it; |
|
| 638 |
Item _item; |
|
| 639 |
}; |
|
| 640 |
|
|
| 641 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
|
| 642 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
|
| 643 |
public: |
|
| 644 |
|
|
| 645 |
RefCopy(Ref& map) : _map(map) {}
|
|
| 646 |
|
|
| 647 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
|
| 648 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
| 649 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
|
| 650 |
_map.set(it, refMap[it]); |
|
| 651 |
} |
|
| 652 |
} |
|
| 653 |
|
|
| 654 |
private: |
|
| 655 |
Ref& _map; |
|
| 656 |
}; |
|
| 657 |
|
|
| 658 |
template <typename Digraph, typename Item, typename RefMap, |
|
| 659 |
typename CrossRef> |
|
| 660 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
|
| 661 |
public: |
|
| 662 |
|
|
| 663 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
|
| 664 |
|
|
| 665 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
|
| 666 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
| 667 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
|
| 668 |
_cmap.set(refMap[it], it); |
|
| 669 |
} |
|
| 670 |
} |
|
| 671 |
|
|
| 672 |
private: |
|
| 673 |
CrossRef& _cmap; |
|
| 674 |
}; |
|
| 675 |
|
|
| 676 |
template <typename Digraph, typename Enable = void> |
|
| 677 |
struct DigraphCopySelector {
|
|
| 678 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
|
| 679 |
static void copy(Digraph &to, const From& from, |
|
| 680 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
|
| 681 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
|
| 682 |
nodeRefMap[it] = to.addNode(); |
|
| 683 |
} |
|
| 684 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
|
| 685 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
|
| 686 |
nodeRefMap[from.target(it)]); |
|
| 687 |
} |
|
| 688 |
} |
|
| 689 |
}; |
|
| 690 |
|
|
| 691 |
template <typename Digraph> |
|
| 692 |
struct DigraphCopySelector< |
|
| 693 |
Digraph, |
|
| 694 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
|
| 695 |
{
|
|
| 696 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
|
| 697 |
static void copy(Digraph &to, const From& from, |
|
| 698 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
|
| 699 |
to.build(from, nodeRefMap, arcRefMap); |
|
| 700 |
} |
|
| 701 |
}; |
|
| 702 |
|
|
| 703 |
template <typename Graph, typename Enable = void> |
|
| 704 |
struct GraphCopySelector {
|
|
| 705 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
|
| 706 |
static void copy(Graph &to, const From& from, |
|
| 707 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
|
| 708 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
|
| 709 |
nodeRefMap[it] = to.addNode(); |
|
| 710 |
} |
|
| 711 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
|
| 712 |
edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
|
| 713 |
nodeRefMap[from.target(it)]); |
|
| 714 |
} |
|
| 715 |
} |
|
| 716 |
}; |
|
| 717 |
|
|
| 718 |
template <typename Graph> |
|
| 719 |
struct GraphCopySelector< |
|
| 720 |
Graph, |
|
| 721 |
typename enable_if<typename Graph::BuildTag, void>::type> |
|
| 722 |
{
|
|
| 723 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
|
| 724 |
static void copy(Graph &to, const From& from, |
|
| 725 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
|
| 726 |
to.build(from, nodeRefMap, edgeRefMap); |
|
| 727 |
} |
|
| 728 |
}; |
|
| 729 |
|
|
| 730 |
template <typename BpGraph, typename Enable = void> |
|
| 731 |
struct BpGraphCopySelector {
|
|
| 732 |
template <typename From, typename RedRefMap, |
|
| 733 |
typename BlueRefMap, typename EdgeRefMap> |
|
| 734 |
static void copy(BpGraph &to, const From& from, |
|
| 735 |
RedRefMap& redRefMap, BlueRefMap& blueRefMap, |
|
| 736 |
EdgeRefMap& edgeRefMap) {
|
|
| 737 |
for (typename From::RedIt it(from); it != INVALID; ++it) {
|
|
| 738 |
redRefMap[it] = to.addRed(); |
|
| 739 |
} |
|
| 740 |
for (typename From::BlueIt it(from); it != INVALID; ++it) {
|
|
| 741 |
blueRefMap[it] = to.addBlue(); |
|
| 742 |
} |
|
| 743 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
|
| 744 |
edgeRefMap[it] = to.addArc(redRefMap[from.red(it)], |
|
| 745 |
blueRefMap[from.blue(it)]); |
|
| 746 |
} |
|
| 747 |
} |
|
| 748 |
}; |
|
| 749 |
|
|
| 750 |
template <typename BpGraph> |
|
| 751 |
struct BpGraphCopySelector< |
|
| 752 |
BpGraph, |
|
| 753 |
typename enable_if<typename BpGraph::BuildTag, void>::type> |
|
| 754 |
{
|
|
| 755 |
template <typename From, typename RedRefMap, |
|
| 756 |
typename BlueRefMap, typename EdgeRefMap> |
|
| 757 |
static void copy(BpGraph &to, const From& from, |
|
| 758 |
RedRefMap& redRefMap, BlueRefMap& blueRefMap, |
|
| 759 |
EdgeRefMap& edgeRefMap) {
|
|
| 760 |
to.build(from, redRefMap, blueRefMap, edgeRefMap); |
|
| 761 |
} |
|
| 762 |
}; |
|
| 763 |
|
|
| 764 |
|
|
| 765 |
} |
|
| 766 |
|
|
| 767 |
/// \brief Class to copy a digraph. |
|
| 768 |
/// |
|
| 769 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
|
| 770 |
/// simplest way of using it is through the \c copyDigraph() function. |
|
| 771 |
template <typename To, typename From> |
|
| 772 |
class DigraphCopy {
|
|
| 773 |
private: |
|
| 774 |
|
|
| 775 |
typedef typename From::Node Node; |
|
| 776 |
typedef typename From::NodeIt NodeIt; |
|
| 777 |
typedef typename From::Arc Arc; |
|
| 778 |
typedef typename From::ArcIt ArcIt; |
|
| 779 |
|
|
| 780 |
typedef typename To::Node TNode; |
|
| 781 |
typedef typename To::Arc TArc; |
|
| 782 |
|
|
| 783 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
|
| 784 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
|
| 785 |
|
|
| 786 |
|
|
| 787 |
public: |
|
| 788 |
|
|
| 789 |
|
|
| 790 |
/// \brief Constructor for the DigraphCopy. |
|
| 791 |
/// |
|
| 792 |
/// It copies the content of the \c _from digraph into the |
|
| 793 |
/// \c _to digraph. |
|
| 794 |
DigraphCopy(To& _to, const From& _from) |
|
| 795 |
: from(_from), to(_to) {}
|
|
| 796 |
|
|
| 797 |
/// \brief Destructor of the DigraphCopy |
|
| 798 |
/// |
|
| 799 |
/// Destructor of the DigraphCopy |
|
| 800 |
~DigraphCopy() {
|
|
| 801 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
|
| 802 |
delete nodeMapCopies[i]; |
|
| 803 |
} |
|
| 804 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
|
|
| 805 |
delete arcMapCopies[i]; |
|
| 806 |
} |
|
| 807 |
|
|
| 808 |
} |
|
| 809 |
|
|
| 810 |
/// \brief Copies the node references into the given map. |
|
| 811 |
/// |
|
| 812 |
/// Copies the node references into the given map. |
|
| 813 |
template <typename NodeRef> |
|
| 814 |
DigraphCopy& nodeRef(NodeRef& map) {
|
|
| 815 |
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, |
|
| 816 |
NodeRefMap, NodeRef>(map)); |
|
| 817 |
return *this; |
|
| 818 |
} |
|
| 819 |
|
|
| 820 |
/// \brief Copies the node cross references into the given map. |
|
| 821 |
/// |
|
| 822 |
/// Copies the node cross references (reverse references) into |
|
| 823 |
/// the given map. |
|
| 824 |
template <typename NodeCrossRef> |
|
| 825 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
|
| 826 |
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node, |
|
| 827 |
NodeRefMap, NodeCrossRef>(map)); |
|
| 828 |
return *this; |
|
| 829 |
} |
|
| 830 |
|
|
| 831 |
/// \brief Make copy of the given map. |
|
| 832 |
/// |
|
| 833 |
/// Makes copy of the given map for the newly created digraph. |
|
| 834 |
/// The new map's key type is the to digraph's node type, |
|
| 835 |
/// and the copied map's key type is the from digraph's node |
|
| 836 |
/// type. |
|
| 837 |
template <typename ToMap, typename FromMap> |
|
| 838 |
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
|
|
| 839 |
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, |
|
| 840 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
| 841 |
return *this; |
|
| 842 |
} |
|
| 843 |
|
|
| 844 |
/// \brief Make a copy of the given node. |
|
| 845 |
/// |
|
| 846 |
/// Make a copy of the given node. |
|
| 847 |
DigraphCopy& node(TNode& tnode, const Node& snode) {
|
|
| 848 |
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, |
|
| 849 |
NodeRefMap, TNode>(tnode, snode)); |
|
| 850 |
return *this; |
|
| 851 |
} |
|
| 852 |
|
|
| 853 |
/// \brief Copies the arc references into the given map. |
|
| 854 |
/// |
|
| 855 |
/// Copies the arc references into the given map. |
|
| 856 |
template <typename ArcRef> |
|
| 857 |
DigraphCopy& arcRef(ArcRef& map) {
|
|
| 858 |
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, |
|
| 859 |
ArcRefMap, ArcRef>(map)); |
|
| 860 |
return *this; |
|
| 861 |
} |
|
| 862 |
|
|
| 863 |
/// \brief Copies the arc cross references into the given map. |
|
| 864 |
/// |
|
| 865 |
/// Copies the arc cross references (reverse references) into |
|
| 866 |
/// the given map. |
|
| 867 |
template <typename ArcCrossRef> |
|
| 868 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
|
|
| 869 |
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc, |
|
| 870 |
ArcRefMap, ArcCrossRef>(map)); |
|
| 871 |
return *this; |
|
| 872 |
} |
|
| 873 |
|
|
| 874 |
/// \brief Make copy of the given map. |
|
| 875 |
/// |
|
| 876 |
/// Makes copy of the given map for the newly created digraph. |
|
| 877 |
/// The new map's key type is the to digraph's arc type, |
|
| 878 |
/// and the copied map's key type is the from digraph's arc |
|
| 879 |
/// type. |
|
| 880 |
template <typename ToMap, typename FromMap> |
|
| 881 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
|
|
| 882 |
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, |
|
| 883 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
| 884 |
return *this; |
|
| 885 |
} |
|
| 886 |
|
|
| 887 |
/// \brief Make a copy of the given arc. |
|
| 888 |
/// |
|
| 889 |
/// Make a copy of the given arc. |
|
| 890 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
|
|
| 891 |
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, |
|
| 892 |
ArcRefMap, TArc>(tarc, sarc)); |
|
| 893 |
return *this; |
|
| 894 |
} |
|
| 895 |
|
|
| 896 |
/// \brief Executes the copies. |
|
| 897 |
/// |
|
| 898 |
/// Executes the copies. |
|
| 899 |
void run() {
|
|
| 900 |
NodeRefMap nodeRefMap(from); |
|
| 901 |
ArcRefMap arcRefMap(from); |
|
| 902 |
_digraph_utils_bits::DigraphCopySelector<To>:: |
|
| 903 |
copy(to, from, nodeRefMap, arcRefMap); |
|
| 904 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
|
| 905 |
nodeMapCopies[i]->copy(from, nodeRefMap); |
|
| 906 |
} |
|
| 907 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
|
|
| 908 |
arcMapCopies[i]->copy(from, arcRefMap); |
|
| 909 |
} |
|
| 910 |
} |
|
| 911 |
|
|
| 912 |
protected: |
|
| 913 |
|
|
| 914 |
|
|
| 915 |
const From& from; |
|
| 916 |
To& to; |
|
| 917 |
|
|
| 918 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
| 919 |
nodeMapCopies; |
|
| 920 |
|
|
| 921 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
| 922 |
arcMapCopies; |
|
| 923 |
|
|
| 924 |
}; |
|
| 925 |
|
|
| 926 |
/// \brief Copy a digraph to another digraph. |
|
| 927 |
/// |
|
| 928 |
/// Copy a digraph to another digraph. |
|
| 929 |
/// The usage of the function: |
|
| 930 |
/// |
|
| 931 |
///\code |
|
| 932 |
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
|
| 933 |
///\endcode |
|
| 934 |
/// |
|
| 935 |
/// After the copy the \c nr map will contain the mapping from the |
|
| 936 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
|
| 937 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
|
| 938 |
/// to the arcs of the \c from digraph. |
|
| 939 |
/// |
|
| 940 |
/// \see DigraphCopy |
|
| 941 |
template <typename To, typename From> |
|
| 942 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
|
|
| 943 |
return DigraphCopy<To, From>(to, from); |
|
| 944 |
} |
|
| 945 |
|
|
| 946 |
/// \brief Class to copy an graph. |
|
| 947 |
/// |
|
| 948 |
/// Class to copy an graph to another digraph (duplicate a digraph). |
|
| 949 |
/// The simplest way of using it is through the \c copyGraph() function. |
|
| 950 |
template <typename To, typename From> |
|
| 951 |
class GraphCopy {
|
|
| 952 |
private: |
|
| 953 |
|
|
| 954 |
typedef typename From::Node Node; |
|
| 955 |
typedef typename From::NodeIt NodeIt; |
|
| 956 |
typedef typename From::Arc Arc; |
|
| 957 |
typedef typename From::ArcIt ArcIt; |
|
| 958 |
typedef typename From::Edge Edge; |
|
| 959 |
typedef typename From::EdgeIt EdgeIt; |
|
| 960 |
|
|
| 961 |
typedef typename To::Node TNode; |
|
| 962 |
typedef typename To::Arc TArc; |
|
| 963 |
typedef typename To::Edge TEdge; |
|
| 964 |
|
|
| 965 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
|
| 966 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
|
| 967 |
|
|
| 968 |
struct ArcRefMap {
|
|
| 969 |
ArcRefMap(const To& _to, const From& _from, |
|
| 970 |
const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref) |
|
| 971 |
: to(_to), from(_from), |
|
| 972 |
edge_ref(_edge_ref), node_ref(_node_ref) {}
|
|
| 973 |
|
|
| 974 |
typedef typename From::Arc Key; |
|
| 975 |
typedef typename To::Arc Value; |
|
| 976 |
|
|
| 977 |
Value operator[](const Key& key) const {
|
|
| 978 |
bool forward = |
|
| 979 |
(from.direction(key) == |
|
| 980 |
(node_ref[from.source(static_cast<const Edge&>(key))] == |
|
| 981 |
to.source(edge_ref[static_cast<const Edge&>(key)]))); |
|
| 982 |
return to.direct(edge_ref[key], forward); |
|
| 983 |
} |
|
| 984 |
|
|
| 985 |
const To& to; |
|
| 986 |
const From& from; |
|
| 987 |
const EdgeRefMap& edge_ref; |
|
| 988 |
const NodeRefMap& node_ref; |
|
| 989 |
}; |
|
| 990 |
|
|
| 991 |
|
|
| 992 |
public: |
|
| 993 |
|
|
| 994 |
|
|
| 995 |
/// \brief Constructor for the DigraphCopy. |
|
| 996 |
/// |
|
| 997 |
/// It copies the content of the \c _from digraph into the |
|
| 998 |
/// \c _to digraph. |
|
| 999 |
GraphCopy(To& _to, const From& _from) |
|
| 1000 |
: from(_from), to(_to) {}
|
|
| 1001 |
|
|
| 1002 |
/// \brief Destructor of the DigraphCopy |
|
| 1003 |
/// |
|
| 1004 |
/// Destructor of the DigraphCopy |
|
| 1005 |
~GraphCopy() {
|
|
| 1006 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
|
| 1007 |
delete nodeMapCopies[i]; |
|
| 1008 |
} |
|
| 1009 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
|
|
| 1010 |
delete arcMapCopies[i]; |
|
| 1011 |
} |
|
| 1012 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
|
| 1013 |
delete edgeMapCopies[i]; |
|
| 1014 |
} |
|
| 1015 |
|
|
| 1016 |
} |
|
| 1017 |
|
|
| 1018 |
/// \brief Copies the node references into the given map. |
|
| 1019 |
/// |
|
| 1020 |
/// Copies the node references into the given map. |
|
| 1021 |
template <typename NodeRef> |
|
| 1022 |
GraphCopy& nodeRef(NodeRef& map) {
|
|
| 1023 |
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, |
|
| 1024 |
NodeRefMap, NodeRef>(map)); |
|
| 1025 |
return *this; |
|
| 1026 |
} |
|
| 1027 |
|
|
| 1028 |
/// \brief Copies the node cross references into the given map. |
|
| 1029 |
/// |
|
| 1030 |
/// Copies the node cross references (reverse references) into |
|
| 1031 |
/// the given map. |
|
| 1032 |
template <typename NodeCrossRef> |
|
| 1033 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
|
| 1034 |
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node, |
|
| 1035 |
NodeRefMap, NodeCrossRef>(map)); |
|
| 1036 |
return *this; |
|
| 1037 |
} |
|
| 1038 |
|
|
| 1039 |
/// \brief Make copy of the given map. |
|
| 1040 |
/// |
|
| 1041 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1042 |
/// The new map's key type is the to digraph's node type, |
|
| 1043 |
/// and the copied map's key type is the from digraph's node |
|
| 1044 |
/// type. |
|
| 1045 |
template <typename ToMap, typename FromMap> |
|
| 1046 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
|
|
| 1047 |
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, |
|
| 1048 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1049 |
return *this; |
|
| 1050 |
} |
|
| 1051 |
|
|
| 1052 |
/// \brief Make a copy of the given node. |
|
| 1053 |
/// |
|
| 1054 |
/// Make a copy of the given node. |
|
| 1055 |
GraphCopy& node(TNode& tnode, const Node& snode) {
|
|
| 1056 |
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, |
|
| 1057 |
NodeRefMap, TNode>(tnode, snode)); |
|
| 1058 |
return *this; |
|
| 1059 |
} |
|
| 1060 |
|
|
| 1061 |
/// \brief Copies the arc references into the given map. |
|
| 1062 |
/// |
|
| 1063 |
/// Copies the arc references into the given map. |
|
| 1064 |
template <typename ArcRef> |
|
| 1065 |
GraphCopy& arcRef(ArcRef& map) {
|
|
| 1066 |
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, |
|
| 1067 |
ArcRefMap, ArcRef>(map)); |
|
| 1068 |
return *this; |
|
| 1069 |
} |
|
| 1070 |
|
|
| 1071 |
/// \brief Copies the arc cross references into the given map. |
|
| 1072 |
/// |
|
| 1073 |
/// Copies the arc cross references (reverse references) into |
|
| 1074 |
/// the given map. |
|
| 1075 |
template <typename ArcCrossRef> |
|
| 1076 |
GraphCopy& arcCrossRef(ArcCrossRef& map) {
|
|
| 1077 |
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc, |
|
| 1078 |
ArcRefMap, ArcCrossRef>(map)); |
|
| 1079 |
return *this; |
|
| 1080 |
} |
|
| 1081 |
|
|
| 1082 |
/// \brief Make copy of the given map. |
|
| 1083 |
/// |
|
| 1084 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1085 |
/// The new map's key type is the to digraph's arc type, |
|
| 1086 |
/// and the copied map's key type is the from digraph's arc |
|
| 1087 |
/// type. |
|
| 1088 |
template <typename ToMap, typename FromMap> |
|
| 1089 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
|
|
| 1090 |
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, |
|
| 1091 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1092 |
return *this; |
|
| 1093 |
} |
|
| 1094 |
|
|
| 1095 |
/// \brief Make a copy of the given arc. |
|
| 1096 |
/// |
|
| 1097 |
/// Make a copy of the given arc. |
|
| 1098 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) {
|
|
| 1099 |
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, |
|
| 1100 |
ArcRefMap, TArc>(tarc, sarc)); |
|
| 1101 |
return *this; |
|
| 1102 |
} |
|
| 1103 |
|
|
| 1104 |
/// \brief Copies the edge references into the given map. |
|
| 1105 |
/// |
|
| 1106 |
/// Copies the edge references into the given map. |
|
| 1107 |
template <typename EdgeRef> |
|
| 1108 |
GraphCopy& edgeRef(EdgeRef& map) {
|
|
| 1109 |
edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge, |
|
| 1110 |
EdgeRefMap, EdgeRef>(map)); |
|
| 1111 |
return *this; |
|
| 1112 |
} |
|
| 1113 |
|
|
| 1114 |
/// \brief Copies the edge cross references into the given map. |
|
| 1115 |
/// |
|
| 1116 |
/// Copies the edge cross references (reverse |
|
| 1117 |
/// references) into the given map. |
|
| 1118 |
template <typename EdgeCrossRef> |
|
| 1119 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
|
| 1120 |
edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
| 1121 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
|
| 1122 |
return *this; |
|
| 1123 |
} |
|
| 1124 |
|
|
| 1125 |
/// \brief Make copy of the given map. |
|
| 1126 |
/// |
|
| 1127 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1128 |
/// The new map's key type is the to digraph's edge type, |
|
| 1129 |
/// and the copied map's key type is the from digraph's edge |
|
| 1130 |
/// type. |
|
| 1131 |
template <typename ToMap, typename FromMap> |
|
| 1132 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
|
|
| 1133 |
edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge, |
|
| 1134 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1135 |
return *this; |
|
| 1136 |
} |
|
| 1137 |
|
|
| 1138 |
/// \brief Make a copy of the given edge. |
|
| 1139 |
/// |
|
| 1140 |
/// Make a copy of the given edge. |
|
| 1141 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
|
|
| 1142 |
edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge, |
|
| 1143 |
EdgeRefMap, TEdge>(tedge, sedge)); |
|
| 1144 |
return *this; |
|
| 1145 |
} |
|
| 1146 |
|
|
| 1147 |
/// \brief Executes the copies. |
|
| 1148 |
/// |
|
| 1149 |
/// Executes the copies. |
|
| 1150 |
void run() {
|
|
| 1151 |
NodeRefMap nodeRefMap(from); |
|
| 1152 |
EdgeRefMap edgeRefMap(from); |
|
| 1153 |
ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap); |
|
| 1154 |
_digraph_utils_bits::GraphCopySelector<To>:: |
|
| 1155 |
copy(to, from, nodeRefMap, edgeRefMap); |
|
| 1156 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
|
| 1157 |
nodeMapCopies[i]->copy(from, nodeRefMap); |
|
| 1158 |
} |
|
| 1159 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
|
| 1160 |
edgeMapCopies[i]->copy(from, edgeRefMap); |
|
| 1161 |
} |
|
| 1162 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
|
|
| 1163 |
arcMapCopies[i]->copy(from, arcRefMap); |
|
| 1164 |
} |
|
| 1165 |
} |
|
| 1166 |
|
|
| 1167 |
private: |
|
| 1168 |
|
|
| 1169 |
const From& from; |
|
| 1170 |
To& to; |
|
| 1171 |
|
|
| 1172 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
| 1173 |
nodeMapCopies; |
|
| 1174 |
|
|
| 1175 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
| 1176 |
arcMapCopies; |
|
| 1177 |
|
|
| 1178 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
|
| 1179 |
edgeMapCopies; |
|
| 1180 |
|
|
| 1181 |
}; |
|
| 1182 |
|
|
| 1183 |
/// \brief Copy an graph to another digraph. |
|
| 1184 |
/// |
|
| 1185 |
/// Copy an graph to another digraph. |
|
| 1186 |
/// The usage of the function: |
|
| 1187 |
/// |
|
| 1188 |
///\code |
|
| 1189 |
/// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
|
| 1190 |
///\endcode |
|
| 1191 |
/// |
|
| 1192 |
/// After the copy the \c nr map will contain the mapping from the |
|
| 1193 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
|
| 1194 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
|
| 1195 |
/// to the arcs of the \c from digraph. |
|
| 1196 |
/// |
|
| 1197 |
/// \see GraphCopy |
|
| 1198 |
template <typename To, typename From> |
|
| 1199 |
GraphCopy<To, From> |
|
| 1200 |
copyGraph(To& to, const From& from) {
|
|
| 1201 |
return GraphCopy<To, From>(to, from); |
|
| 1202 |
} |
|
| 1203 |
|
|
| 1204 |
/// \brief Class to copy a bipartite digraph. |
|
| 1205 |
/// |
|
| 1206 |
/// Class to copy a bipartite digraph to another digraph |
|
| 1207 |
/// (duplicate a digraph). The simplest way of using it is through |
|
| 1208 |
/// the \c copyBpGraph() function. |
|
| 1209 |
template <typename To, typename From> |
|
| 1210 |
class BpGraphCopy {
|
|
| 1211 |
private: |
|
| 1212 |
|
|
| 1213 |
typedef typename From::Node Node; |
|
| 1214 |
typedef typename From::Red Red; |
|
| 1215 |
typedef typename From::Blue Blue; |
|
| 1216 |
typedef typename From::NodeIt NodeIt; |
|
| 1217 |
typedef typename From::Arc Arc; |
|
| 1218 |
typedef typename From::ArcIt ArcIt; |
|
| 1219 |
typedef typename From::Edge Edge; |
|
| 1220 |
typedef typename From::EdgeIt EdgeIt; |
|
| 1221 |
|
|
| 1222 |
typedef typename To::Node TNode; |
|
| 1223 |
typedef typename To::Arc TArc; |
|
| 1224 |
typedef typename To::Edge TEdge; |
|
| 1225 |
|
|
| 1226 |
typedef typename From::template RedMap<TNode> RedRefMap; |
|
| 1227 |
typedef typename From::template BlueMap<TNode> BlueRefMap; |
|
| 1228 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
|
| 1229 |
|
|
| 1230 |
struct NodeRefMap {
|
|
| 1231 |
NodeRefMap(const From& _from, const RedRefMap& _red_ref, |
|
| 1232 |
const BlueRefMap& _blue_ref) |
|
| 1233 |
: from(_from), red_ref(_red_ref), blue_ref(_blue_ref) {}
|
|
| 1234 |
|
|
| 1235 |
typedef typename From::Node Key; |
|
| 1236 |
typedef typename To::Node Value; |
|
| 1237 |
|
|
| 1238 |
Value operator[](const Key& key) const {
|
|
| 1239 |
return from.red(key) ? red_ref[key] : blue_ref[key]; |
|
| 1240 |
} |
|
| 1241 |
|
|
| 1242 |
const From& from; |
|
| 1243 |
const RedRefMap& red_ref; |
|
| 1244 |
const BlueRefMap& blue_ref; |
|
| 1245 |
}; |
|
| 1246 |
|
|
| 1247 |
struct ArcRefMap {
|
|
| 1248 |
ArcRefMap(const To& _to, const From& _from, |
|
| 1249 |
const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref) |
|
| 1250 |
: to(_to), from(_from), |
|
| 1251 |
edge_ref(_edge_ref), node_ref(_node_ref) {}
|
|
| 1252 |
|
|
| 1253 |
typedef typename From::Arc Key; |
|
| 1254 |
typedef typename To::Arc Value; |
|
| 1255 |
|
|
| 1256 |
Value operator[](const Key& key) const {
|
|
| 1257 |
bool forward = |
|
| 1258 |
(from.direction(key) == |
|
| 1259 |
(node_ref[from.source(static_cast<const Edge&>(key))] == |
|
| 1260 |
to.source(edge_ref[static_cast<const Edge&>(key)]))); |
|
| 1261 |
return to.direct(edge_ref[key], forward); |
|
| 1262 |
} |
|
| 1263 |
|
|
| 1264 |
const To& to; |
|
| 1265 |
const From& from; |
|
| 1266 |
const EdgeRefMap& edge_ref; |
|
| 1267 |
const NodeRefMap& node_ref; |
|
| 1268 |
}; |
|
| 1269 |
|
|
| 1270 |
public: |
|
| 1271 |
|
|
| 1272 |
|
|
| 1273 |
/// \brief Constructor for the DigraphCopy. |
|
| 1274 |
/// |
|
| 1275 |
/// It copies the content of the \c _from digraph into the |
|
| 1276 |
/// \c _to digraph. |
|
| 1277 |
BpGraphCopy(To& _to, const From& _from) |
|
| 1278 |
: from(_from), to(_to) {}
|
|
| 1279 |
|
|
| 1280 |
/// \brief Destructor of the DigraphCopy |
|
| 1281 |
/// |
|
| 1282 |
/// Destructor of the DigraphCopy |
|
| 1283 |
~BpGraphCopy() {
|
|
| 1284 |
for (int i = 0; i < int(redMapCopies.size()); ++i) {
|
|
| 1285 |
delete redMapCopies[i]; |
|
| 1286 |
} |
|
| 1287 |
for (int i = 0; i < int(blueMapCopies.size()); ++i) {
|
|
| 1288 |
delete blueMapCopies[i]; |
|
| 1289 |
} |
|
| 1290 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
|
| 1291 |
delete nodeMapCopies[i]; |
|
| 1292 |
} |
|
| 1293 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
|
|
| 1294 |
delete arcMapCopies[i]; |
|
| 1295 |
} |
|
| 1296 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
|
| 1297 |
delete edgeMapCopies[i]; |
|
| 1298 |
} |
|
| 1299 |
|
|
| 1300 |
} |
|
| 1301 |
|
|
| 1302 |
/// \brief Copies the A-node references into the given map. |
|
| 1303 |
/// |
|
| 1304 |
/// Copies the A-node references into the given map. |
|
| 1305 |
template <typename RedRef> |
|
| 1306 |
BpGraphCopy& redRef(RedRef& map) {
|
|
| 1307 |
redMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Red, |
|
| 1308 |
RedRefMap, RedRef>(map)); |
|
| 1309 |
return *this; |
|
| 1310 |
} |
|
| 1311 |
|
|
| 1312 |
/// \brief Copies the A-node cross references into the given map. |
|
| 1313 |
/// |
|
| 1314 |
/// Copies the A-node cross references (reverse references) into |
|
| 1315 |
/// the given map. |
|
| 1316 |
template <typename RedCrossRef> |
|
| 1317 |
BpGraphCopy& redCrossRef(RedCrossRef& map) {
|
|
| 1318 |
redMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
| 1319 |
Red, RedRefMap, RedCrossRef>(map)); |
|
| 1320 |
return *this; |
|
| 1321 |
} |
|
| 1322 |
|
|
| 1323 |
/// \brief Make copy of the given A-node map. |
|
| 1324 |
/// |
|
| 1325 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1326 |
/// The new map's key type is the to digraph's node type, |
|
| 1327 |
/// and the copied map's key type is the from digraph's node |
|
| 1328 |
/// type. |
|
| 1329 |
template <typename ToMap, typename FromMap> |
|
| 1330 |
BpGraphCopy& redMap(ToMap& tmap, const FromMap& map) {
|
|
| 1331 |
redMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Red, |
|
| 1332 |
RedRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1333 |
return *this; |
|
| 1334 |
} |
|
| 1335 |
|
|
| 1336 |
/// \brief Copies the B-node references into the given map. |
|
| 1337 |
/// |
|
| 1338 |
/// Copies the B-node references into the given map. |
|
| 1339 |
template <typename BlueRef> |
|
| 1340 |
BpGraphCopy& blueRef(BlueRef& map) {
|
|
| 1341 |
blueMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Blue, |
|
| 1342 |
BlueRefMap, BlueRef>(map)); |
|
| 1343 |
return *this; |
|
| 1344 |
} |
|
| 1345 |
|
|
| 1346 |
/// \brief Copies the B-node cross references into the given map. |
|
| 1347 |
/// |
|
| 1348 |
/// Copies the B-node cross references (reverse references) into |
|
| 1349 |
/// the given map. |
|
| 1350 |
template <typename BlueCrossRef> |
|
| 1351 |
BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
|
|
| 1352 |
blueMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
| 1353 |
Blue, BlueRefMap, BlueCrossRef>(map)); |
|
| 1354 |
return *this; |
|
| 1355 |
} |
|
| 1356 |
|
|
| 1357 |
/// \brief Make copy of the given B-node map. |
|
| 1358 |
/// |
|
| 1359 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1360 |
/// The new map's key type is the to digraph's node type, |
|
| 1361 |
/// and the copied map's key type is the from digraph's node |
|
| 1362 |
/// type. |
|
| 1363 |
template <typename ToMap, typename FromMap> |
|
| 1364 |
BpGraphCopy& blueMap(ToMap& tmap, const FromMap& map) {
|
|
| 1365 |
blueMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Blue, |
|
| 1366 |
BlueRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1367 |
return *this; |
|
| 1368 |
} |
|
| 1369 |
/// \brief Copies the node references into the given map. |
|
| 1370 |
/// |
|
| 1371 |
/// Copies the node references into the given map. |
|
| 1372 |
template <typename NodeRef> |
|
| 1373 |
BpGraphCopy& nodeRef(NodeRef& map) {
|
|
| 1374 |
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, |
|
| 1375 |
NodeRefMap, NodeRef>(map)); |
|
| 1376 |
return *this; |
|
| 1377 |
} |
|
| 1378 |
|
|
| 1379 |
/// \brief Copies the node cross references into the given map. |
|
| 1380 |
/// |
|
| 1381 |
/// Copies the node cross references (reverse references) into |
|
| 1382 |
/// the given map. |
|
| 1383 |
template <typename NodeCrossRef> |
|
| 1384 |
BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
|
| 1385 |
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node, |
|
| 1386 |
NodeRefMap, NodeCrossRef>(map)); |
|
| 1387 |
return *this; |
|
| 1388 |
} |
|
| 1389 |
|
|
| 1390 |
/// \brief Make copy of the given map. |
|
| 1391 |
/// |
|
| 1392 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1393 |
/// The new map's key type is the to digraph's node type, |
|
| 1394 |
/// and the copied map's key type is the from digraph's node |
|
| 1395 |
/// type. |
|
| 1396 |
template <typename ToMap, typename FromMap> |
|
| 1397 |
BpGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
|
|
| 1398 |
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, |
|
| 1399 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1400 |
return *this; |
|
| 1401 |
} |
|
| 1402 |
|
|
| 1403 |
/// \brief Make a copy of the given node. |
|
| 1404 |
/// |
|
| 1405 |
/// Make a copy of the given node. |
|
| 1406 |
BpGraphCopy& node(TNode& tnode, const Node& snode) {
|
|
| 1407 |
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, |
|
| 1408 |
NodeRefMap, TNode>(tnode, snode)); |
|
| 1409 |
return *this; |
|
| 1410 |
} |
|
| 1411 |
|
|
| 1412 |
/// \brief Copies the arc references into the given map. |
|
| 1413 |
/// |
|
| 1414 |
/// Copies the arc references into the given map. |
|
| 1415 |
template <typename ArcRef> |
|
| 1416 |
BpGraphCopy& arcRef(ArcRef& map) {
|
|
| 1417 |
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, |
|
| 1418 |
ArcRefMap, ArcRef>(map)); |
|
| 1419 |
return *this; |
|
| 1420 |
} |
|
| 1421 |
|
|
| 1422 |
/// \brief Copies the arc cross references into the given map. |
|
| 1423 |
/// |
|
| 1424 |
/// Copies the arc cross references (reverse references) into |
|
| 1425 |
/// the given map. |
|
| 1426 |
template <typename ArcCrossRef> |
|
| 1427 |
BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
|
|
| 1428 |
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc, |
|
| 1429 |
ArcRefMap, ArcCrossRef>(map)); |
|
| 1430 |
return *this; |
|
| 1431 |
} |
|
| 1432 |
|
|
| 1433 |
/// \brief Make copy of the given map. |
|
| 1434 |
/// |
|
| 1435 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1436 |
/// The new map's key type is the to digraph's arc type, |
|
| 1437 |
/// and the copied map's key type is the from digraph's arc |
|
| 1438 |
/// type. |
|
| 1439 |
template <typename ToMap, typename FromMap> |
|
| 1440 |
BpGraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
|
|
| 1441 |
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, |
|
| 1442 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1443 |
return *this; |
|
| 1444 |
} |
|
| 1445 |
|
|
| 1446 |
/// \brief Make a copy of the given arc. |
|
| 1447 |
/// |
|
| 1448 |
/// Make a copy of the given arc. |
|
| 1449 |
BpGraphCopy& arc(TArc& tarc, const Arc& sarc) {
|
|
| 1450 |
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, |
|
| 1451 |
ArcRefMap, TArc>(tarc, sarc)); |
|
| 1452 |
return *this; |
|
| 1453 |
} |
|
| 1454 |
|
|
| 1455 |
/// \brief Copies the edge references into the given map. |
|
| 1456 |
/// |
|
| 1457 |
/// Copies the edge references into the given map. |
|
| 1458 |
template <typename EdgeRef> |
|
| 1459 |
BpGraphCopy& edgeRef(EdgeRef& map) {
|
|
| 1460 |
edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge, |
|
| 1461 |
EdgeRefMap, EdgeRef>(map)); |
|
| 1462 |
return *this; |
|
| 1463 |
} |
|
| 1464 |
|
|
| 1465 |
/// \brief Copies the edge cross references into the given map. |
|
| 1466 |
/// |
|
| 1467 |
/// Copies the edge cross references (reverse |
|
| 1468 |
/// references) into the given map. |
|
| 1469 |
template <typename EdgeCrossRef> |
|
| 1470 |
BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
|
| 1471 |
edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
| 1472 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
|
| 1473 |
return *this; |
|
| 1474 |
} |
|
| 1475 |
|
|
| 1476 |
/// \brief Make copy of the given map. |
|
| 1477 |
/// |
|
| 1478 |
/// Makes copy of the given map for the newly created digraph. |
|
| 1479 |
/// The new map's key type is the to digraph's edge type, |
|
| 1480 |
/// and the copied map's key type is the from digraph's edge |
|
| 1481 |
/// type. |
|
| 1482 |
template <typename ToMap, typename FromMap> |
|
| 1483 |
BpGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
|
|
| 1484 |
edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge, |
|
| 1485 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
|
| 1486 |
return *this; |
|
| 1487 |
} |
|
| 1488 |
|
|
| 1489 |
/// \brief Make a copy of the given edge. |
|
| 1490 |
/// |
|
| 1491 |
/// Make a copy of the given edge. |
|
| 1492 |
BpGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
|
|
| 1493 |
edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge, |
|
| 1494 |
EdgeRefMap, TEdge>(tedge, sedge)); |
|
| 1495 |
return *this; |
|
| 1496 |
} |
|
| 1497 |
|
|
| 1498 |
/// \brief Executes the copies. |
|
| 1499 |
/// |
|
| 1500 |
/// Executes the copies. |
|
| 1501 |
void run() {
|
|
| 1502 |
RedRefMap redRefMap(from); |
|
| 1503 |
BlueRefMap blueRefMap(from); |
|
| 1504 |
NodeRefMap nodeRefMap(from, redRefMap, blueRefMap); |
|
| 1505 |
EdgeRefMap edgeRefMap(from); |
|
| 1506 |
ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap); |
|
| 1507 |
_digraph_utils_bits::BpGraphCopySelector<To>:: |
|
| 1508 |
copy(to, from, redRefMap, blueRefMap, edgeRefMap); |
|
| 1509 |
for (int i = 0; i < int(redMapCopies.size()); ++i) {
|
|
| 1510 |
redMapCopies[i]->copy(from, redRefMap); |
|
| 1511 |
} |
|
| 1512 |
for (int i = 0; i < int(blueMapCopies.size()); ++i) {
|
|
| 1513 |
blueMapCopies[i]->copy(from, blueRefMap); |
|
| 1514 |
} |
|
| 1515 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
|
|
| 1516 |
nodeMapCopies[i]->copy(from, nodeRefMap); |
|
| 1517 |
} |
|
| 1518 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
|
|
| 1519 |
edgeMapCopies[i]->copy(from, edgeRefMap); |
|
| 1520 |
} |
|
| 1521 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) {
|
|
| 1522 |
arcMapCopies[i]->copy(from, arcRefMap); |
|
| 1523 |
} |
|
| 1524 |
} |
|
| 1525 |
|
|
| 1526 |
private: |
|
| 1527 |
|
|
| 1528 |
const From& from; |
|
| 1529 |
To& to; |
|
| 1530 |
|
|
| 1531 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Red, RedRefMap>* > |
|
| 1532 |
redMapCopies; |
|
| 1533 |
|
|
| 1534 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Blue, BlueRefMap>* > |
|
| 1535 |
blueMapCopies; |
|
| 1536 |
|
|
| 1537 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
| 1538 |
nodeMapCopies; |
|
| 1539 |
|
|
| 1540 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
| 1541 |
arcMapCopies; |
|
| 1542 |
|
|
| 1543 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
|
| 1544 |
edgeMapCopies; |
|
| 1545 |
|
|
| 1546 |
}; |
|
| 1547 |
|
|
| 1548 |
/// \brief Copy a bipartite digraph to another digraph. |
|
| 1549 |
/// |
|
| 1550 |
/// Copy a bipartite digraph to another digraph. |
|
| 1551 |
/// The usage of the function: |
|
| 1552 |
/// |
|
| 1553 |
///\code |
|
| 1554 |
/// copyBpGraph(trg, src).redRef(anr).arcCrossRef(ecr).run(); |
|
| 1555 |
///\endcode |
|
| 1556 |
/// |
|
| 1557 |
/// After the copy the \c nr map will contain the mapping from the |
|
| 1558 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
|
| 1559 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
|
| 1560 |
/// to the arcs of the \c from digraph. |
|
| 1561 |
/// |
|
| 1562 |
/// \see BpGraphCopy |
|
| 1563 |
template <typename To, typename From> |
|
| 1564 |
BpGraphCopy<To, From> |
|
| 1565 |
copyBpGraph(To& to, const From& from) {
|
|
| 1566 |
return BpGraphCopy<To, From>(to, from); |
|
| 1567 |
} |
|
| 1568 |
|
|
| 1569 |
|
|
| 1570 |
/// @} |
|
| 1571 |
|
|
| 1572 |
/// \addtogroup digraph_maps |
|
| 1573 |
/// @{
|
|
| 1574 |
|
|
| 1575 |
/// Provides an immutable and unique id for each item in the digraph. |
|
| 1576 |
|
|
| 1577 |
/// The IdMap class provides a unique and immutable id for each item of the |
|
| 1578 |
/// same type (e.g. node) in the digraph. This id is <ul><li>\b unique: |
|
| 1579 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
|
| 1580 |
/// item (node) does not change (even if you delete other nodes). </ul> |
|
| 1581 |
/// Through this map you get access (i.e. can read) the inner id values of |
|
| 1582 |
/// the items stored in the digraph. This map can be inverted with its member |
|
| 1583 |
/// class \c InverseMap. |
|
| 1584 |
/// |
|
| 1585 |
template <typename _Digraph, typename _Item> |
|
| 1586 |
class IdMap {
|
|
| 1587 |
public: |
|
| 1588 |
typedef _Digraph Digraph; |
|
| 1589 |
typedef int Value; |
|
| 1590 |
typedef _Item Item; |
|
| 1591 |
typedef _Item Key; |
|
| 1592 |
|
|
| 1593 |
/// \brief Constructor. |
|
| 1594 |
/// |
|
| 1595 |
/// Constructor of the map. |
|
| 1596 |
explicit IdMap(const Digraph& _digraph) : digraph(&_digraph) {}
|
|
| 1597 |
|
|
| 1598 |
/// \brief Gives back the \e id of the item. |
|
| 1599 |
/// |
|
| 1600 |
/// Gives back the immutable and unique \e id of the item. |
|
| 1601 |
int operator[](const Item& item) const { return digraph->id(item);}
|
|
| 1602 |
|
|
| 1603 |
/// \brief Gives back the item by its id. |
|
| 1604 |
/// |
|
| 1605 |
/// Gives back the item by its id. |
|
| 1606 |
Item operator()(int id) { return digraph->fromId(id, Item()); }
|
|
| 1607 |
|
|
| 1608 |
private: |
|
| 1609 |
const Digraph* digraph; |
|
| 1610 |
|
|
| 1611 |
public: |
|
| 1612 |
|
|
| 1613 |
/// \brief The class represents the inverse of its owner (IdMap). |
|
| 1614 |
/// |
|
| 1615 |
/// The class represents the inverse of its owner (IdMap). |
|
| 1616 |
/// \see inverse() |
|
| 1617 |
class InverseMap {
|
|
| 1618 |
public: |
|
| 1619 |
|
|
| 1620 |
/// \brief Constructor. |
|
| 1621 |
/// |
|
| 1622 |
/// Constructor for creating an id-to-item map. |
|
| 1623 |
explicit InverseMap(const Digraph& _digraph) : digraph(&_digraph) {}
|
|
| 1624 |
|
|
| 1625 |
/// \brief Constructor. |
|
| 1626 |
/// |
|
| 1627 |
/// Constructor for creating an id-to-item map. |
|
| 1628 |
explicit InverseMap(const IdMap& idMap) : digraph(idMap.digraph) {}
|
|
| 1629 |
|
|
| 1630 |
/// \brief Gives back the given item from its id. |
|
| 1631 |
/// |
|
| 1632 |
/// Gives back the given item from its id. |
|
| 1633 |
/// |
|
| 1634 |
Item operator[](int id) const { return digraph->fromId(id, Item());}
|
|
| 1635 |
|
|
| 1636 |
private: |
|
| 1637 |
const Digraph* digraph; |
|
| 1638 |
}; |
|
| 1639 |
|
|
| 1640 |
/// \brief Gives back the inverse of the map. |
|
| 1641 |
/// |
|
| 1642 |
/// Gives back the inverse of the IdMap. |
|
| 1643 |
InverseMap inverse() const { return InverseMap(*digraph);}
|
|
| 1644 |
|
|
| 1645 |
}; |
|
| 1646 |
|
|
| 1647 |
|
|
| 1648 |
/// \brief General invertable digraph-map type. |
|
| 1649 |
|
|
| 1650 |
/// This type provides simple invertable digraph-maps. |
|
| 1651 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
|
| 1652 |
/// and if a key is set to a new value then store it |
|
| 1653 |
/// in the inverse map. |
|
| 1654 |
/// |
|
| 1655 |
/// The values of the map can be accessed |
|
| 1656 |
/// with stl compatible forward iterator. |
|
| 1657 |
/// |
|
| 1658 |
/// \param _Digraph The digraph type. |
|
| 1659 |
/// \param _Item The item type of the digraph. |
|
| 1660 |
/// \param _Value The value type of the map. |
|
| 1661 |
/// |
|
| 1662 |
/// \see IterableValueMap |
|
| 1663 |
template <typename _Digraph, typename _Item, typename _Value> |
|
| 1664 |
class InvertableMap : protected DefaultMap<_Digraph, _Item, _Value> {
|
|
| 1665 |
private: |
|
| 1666 |
|
|
| 1667 |
typedef DefaultMap<_Digraph, _Item, _Value> Map; |
|
| 1668 |
typedef _Digraph Digraph; |
|
| 1669 |
|
|
| 1670 |
typedef std::map<_Value, _Item> Container; |
|
| 1671 |
Container invMap; |
|
| 1672 |
|
|
| 1673 |
public: |
|
| 1674 |
|
|
| 1675 |
/// The key type of InvertableMap (Node, Arc, Edge). |
|
| 1676 |
typedef typename Map::Key Key; |
|
| 1677 |
/// The value type of the InvertableMap. |
|
| 1678 |
typedef typename Map::Value Value; |
|
| 1679 |
|
|
| 1680 |
|
|
| 1681 |
|
|
| 1682 |
/// \brief Constructor. |
|
| 1683 |
/// |
|
| 1684 |
/// Construct a new InvertableMap for the digraph. |
|
| 1685 |
/// |
|
| 1686 |
explicit InvertableMap(const Digraph& digraph) : Map(digraph) {}
|
|
| 1687 |
|
|
| 1688 |
/// \brief Forward iterator for values. |
|
| 1689 |
/// |
|
| 1690 |
/// This iterator is an stl compatible forward |
|
| 1691 |
/// iterator on the values of the map. The values can |
|
| 1692 |
/// be accessed in the [beginValue, endValue) range. |
|
| 1693 |
/// |
|
| 1694 |
class ValueIterator |
|
| 1695 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
|
| 1696 |
friend class InvertableMap; |
|
| 1697 |
private: |
|
| 1698 |
ValueIterator(typename Container::const_iterator _it) |
|
| 1699 |
: it(_it) {}
|
|
| 1700 |
public: |
|
| 1701 |
|
|
| 1702 |
ValueIterator() {}
|
|
| 1703 |
|
|
| 1704 |
ValueIterator& operator++() { ++it; return *this; }
|
|
| 1705 |
ValueIterator operator++(int) {
|
|
| 1706 |
ValueIterator tmp(*this); |
|
| 1707 |
operator++(); |
|
| 1708 |
return tmp; |
|
| 1709 |
} |
|
| 1710 |
|
|
| 1711 |
const Value& operator*() const { return it->first; }
|
|
| 1712 |
const Value* operator->() const { return &(it->first); }
|
|
| 1713 |
|
|
| 1714 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
|
| 1715 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
|
| 1716 |
|
|
| 1717 |
private: |
|
| 1718 |
typename Container::const_iterator it; |
|
| 1719 |
}; |
|
| 1720 |
|
|
| 1721 |
/// \brief Returns an iterator to the first value. |
|
| 1722 |
/// |
|
| 1723 |
/// Returns an stl compatible iterator to the |
|
| 1724 |
/// first value of the map. The values of the |
|
| 1725 |
/// map can be accessed in the [beginValue, endValue) |
|
| 1726 |
/// range. |
|
| 1727 |
ValueIterator beginValue() const {
|
|
| 1728 |
return ValueIterator(invMap.begin()); |
|
| 1729 |
} |
|
| 1730 |
|
|
| 1731 |
/// \brief Returns an iterator after the last value. |
|
| 1732 |
/// |
|
| 1733 |
/// Returns an stl compatible iterator after the |
|
| 1734 |
/// last value of the map. The values of the |
|
| 1735 |
/// map can be accessed in the [beginValue, endValue) |
|
| 1736 |
/// range. |
|
| 1737 |
ValueIterator endValue() const {
|
|
| 1738 |
return ValueIterator(invMap.end()); |
|
| 1739 |
} |
|
| 1740 |
|
|
| 1741 |
/// \brief The setter function of the map. |
|
| 1742 |
/// |
|
| 1743 |
/// Sets the mapped value. |
|
| 1744 |
void set(const Key& key, const Value& val) {
|
|
| 1745 |
Value oldval = Map::operator[](key); |
|
| 1746 |
typename Container::iterator it = invMap.find(oldval); |
|
| 1747 |
if (it != invMap.end() && it->second == key) {
|
|
| 1748 |
invMap.erase(it); |
|
| 1749 |
} |
|
| 1750 |
invMap.insert(make_pair(val, key)); |
|
| 1751 |
Map::set(key, val); |
|
| 1752 |
} |
|
| 1753 |
|
|
| 1754 |
/// \brief The getter function of the map. |
|
| 1755 |
/// |
|
| 1756 |
/// It gives back the value associated with the key. |
|
| 1757 |
typename MapTraits<Map>::ConstReturnValue |
|
| 1758 |
operator[](const Key& key) const {
|
|
| 1759 |
return Map::operator[](key); |
|
| 1760 |
} |
|
| 1761 |
|
|
| 1762 |
/// \brief Gives back the item by its value. |
|
| 1763 |
/// |
|
| 1764 |
/// Gives back the item by its value. |
|
| 1765 |
Key operator()(const Value& key) const {
|
|
| 1766 |
typename Container::const_iterator it = invMap.find(key); |
|
| 1767 |
return it != invMap.end() ? it->second : INVALID; |
|
| 1768 |
} |
|
| 1769 |
|
|
| 1770 |
protected: |
|
| 1771 |
|
|
| 1772 |
/// \brief Erase the key from the map. |
|
| 1773 |
/// |
|
| 1774 |
/// Erase the key to the map. It is called by the |
|
| 1775 |
/// \c AlterationNotifier. |
|
| 1776 |
virtual void erase(const Key& key) {
|
|
| 1777 |
Value val = Map::operator[](key); |
|
| 1778 |
typename Container::iterator it = invMap.find(val); |
|
| 1779 |
if (it != invMap.end() && it->second == key) {
|
|
| 1780 |
invMap.erase(it); |
|
| 1781 |
} |
|
| 1782 |
Map::erase(key); |
|
| 1783 |
} |
|
| 1784 |
|
|
| 1785 |
/// \brief Erase more keys from the map. |
|
| 1786 |
/// |
|
| 1787 |
/// Erase more keys from the map. It is called by the |
|
| 1788 |
/// \c AlterationNotifier. |
|
| 1789 |
virtual void erase(const std::vector<Key>& keys) {
|
|
| 1790 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
| 1791 |
Value val = Map::operator[](keys[i]); |
|
| 1792 |
typename Container::iterator it = invMap.find(val); |
|
| 1793 |
if (it != invMap.end() && it->second == keys[i]) {
|
|
| 1794 |
invMap.erase(it); |
|
| 1795 |
} |
|
| 1796 |
} |
|
| 1797 |
Map::erase(keys); |
|
| 1798 |
} |
|
| 1799 |
|
|
| 1800 |
/// \brief Clear the keys from the map and inverse map. |
|
| 1801 |
/// |
|
| 1802 |
/// Clear the keys from the map and inverse map. It is called by the |
|
| 1803 |
/// \c AlterationNotifier. |
|
| 1804 |
virtual void clear() {
|
|
| 1805 |
invMap.clear(); |
|
| 1806 |
Map::clear(); |
|
| 1807 |
} |
|
| 1808 |
|
|
| 1809 |
public: |
|
| 1810 |
|
|
| 1811 |
/// \brief The inverse map type. |
|
| 1812 |
/// |
|
| 1813 |
/// The inverse of this map. The subscript operator of the map |
|
| 1814 |
/// gives back always the item what was last assigned to the value. |
|
| 1815 |
class InverseMap {
|
|
| 1816 |
public: |
|
| 1817 |
/// \brief Constructor of the InverseMap. |
|
| 1818 |
/// |
|
| 1819 |
/// Constructor of the InverseMap. |
|
| 1820 |
explicit InverseMap(const InvertableMap& _inverted) |
|
| 1821 |
: inverted(_inverted) {}
|
|
| 1822 |
|
|
| 1823 |
/// The value type of the InverseMap. |
|
| 1824 |
typedef typename InvertableMap::Key Value; |
|
| 1825 |
/// The key type of the InverseMap. |
|
| 1826 |
typedef typename InvertableMap::Value Key; |
|
| 1827 |
|
|
| 1828 |
/// \brief Subscript operator. |
|
| 1829 |
/// |
|
| 1830 |
/// Subscript operator. It gives back always the item |
|
| 1831 |
/// what was last assigned to the value. |
|
| 1832 |
Value operator[](const Key& key) const {
|
|
| 1833 |
return inverted(key); |
|
| 1834 |
} |
|
| 1835 |
|
|
| 1836 |
private: |
|
| 1837 |
const InvertableMap& inverted; |
|
| 1838 |
}; |
|
| 1839 |
|
|
| 1840 |
/// \brief It gives back the just readable inverse map. |
|
| 1841 |
/// |
|
| 1842 |
/// It gives back the just readable inverse map. |
|
| 1843 |
InverseMap inverse() const {
|
|
| 1844 |
return InverseMap(*this); |
|
| 1845 |
} |
|
| 1846 |
|
|
| 1847 |
|
|
| 1848 |
|
|
| 1849 |
}; |
|
| 1850 |
|
|
| 1851 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
|
| 1852 |
/// item in the digraph. |
|
| 1853 |
/// |
|
| 1854 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
|
| 1855 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
|
| 1856 |
/// digraph. This id is <ul><li>\b unique: different items (nodes) get |
|
| 1857 |
/// different ids <li>\b continuous: the range of the ids is the set of |
|
| 1858 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
|
| 1859 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
|
| 1860 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
|
| 1861 |
/// with its member class \c InverseMap. |
|
| 1862 |
/// |
|
| 1863 |
/// \param _Digraph The digraph class the \c DescriptorMap belongs to. |
|
| 1864 |
/// \param _Item The Item is the Key of the Map. It may be Node, Arc or |
|
| 1865 |
/// Edge. |
|
| 1866 |
template <typename _Digraph, typename _Item> |
|
| 1867 |
class DescriptorMap : protected DefaultMap<_Digraph, _Item, int> {
|
|
| 1868 |
|
|
| 1869 |
typedef _Item Item; |
|
| 1870 |
typedef DefaultMap<_Digraph, _Item, int> Map; |
|
| 1871 |
|
|
| 1872 |
public: |
|
| 1873 |
/// The digraph class of DescriptorMap. |
|
| 1874 |
typedef _Digraph Digraph; |
|
| 1875 |
|
|
| 1876 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
|
| 1877 |
typedef typename Map::Key Key; |
|
| 1878 |
/// The value type of DescriptorMap. |
|
| 1879 |
typedef typename Map::Value Value; |
|
| 1880 |
|
|
| 1881 |
/// \brief Constructor. |
|
| 1882 |
/// |
|
| 1883 |
/// Constructor for descriptor map. |
|
| 1884 |
explicit DescriptorMap(const Digraph& _digraph) : Map(_digraph) {
|
|
| 1885 |
Item it; |
|
| 1886 |
const typename Map::Notifier* nf = Map::notifier(); |
|
| 1887 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
|
| 1888 |
Map::set(it, invMap.size()); |
|
| 1889 |
invMap.push_back(it); |
|
| 1890 |
} |
|
| 1891 |
} |
|
| 1892 |
|
|
| 1893 |
protected: |
|
| 1894 |
|
|
| 1895 |
/// \brief Add a new key to the map. |
|
| 1896 |
/// |
|
| 1897 |
/// Add a new key to the map. It is called by the |
|
| 1898 |
/// \c AlterationNotifier. |
|
| 1899 |
virtual void add(const Item& item) {
|
|
| 1900 |
Map::add(item); |
|
| 1901 |
Map::set(item, invMap.size()); |
|
| 1902 |
invMap.push_back(item); |
|
| 1903 |
} |
|
| 1904 |
|
|
| 1905 |
/// \brief Add more new keys to the map. |
|
| 1906 |
/// |
|
| 1907 |
/// Add more new keys to the map. It is called by the |
|
| 1908 |
/// \c AlterationNotifier. |
|
| 1909 |
virtual void add(const std::vector<Item>& items) {
|
|
| 1910 |
Map::add(items); |
|
| 1911 |
for (int i = 0; i < int(items.size()); ++i) {
|
|
| 1912 |
Map::set(items[i], invMap.size()); |
|
| 1913 |
invMap.push_back(items[i]); |
|
| 1914 |
} |
|
| 1915 |
} |
|
| 1916 |
|
|
| 1917 |
/// \brief Erase the key from the map. |
|
| 1918 |
/// |
|
| 1919 |
/// Erase the key from the map. It is called by the |
|
| 1920 |
/// \c AlterationNotifier. |
|
| 1921 |
virtual void erase(const Item& item) {
|
|
| 1922 |
Map::set(invMap.back(), Map::operator[](item)); |
|
| 1923 |
invMap[Map::operator[](item)] = invMap.back(); |
|
| 1924 |
invMap.pop_back(); |
|
| 1925 |
Map::erase(item); |
|
| 1926 |
} |
|
| 1927 |
|
|
| 1928 |
/// \brief Erase more keys from the map. |
|
| 1929 |
/// |
|
| 1930 |
/// Erase more keys from the map. It is called by the |
|
| 1931 |
/// \c AlterationNotifier. |
|
| 1932 |
virtual void erase(const std::vector<Item>& items) {
|
|
| 1933 |
for (int i = 0; i < int(items.size()); ++i) {
|
|
| 1934 |
Map::set(invMap.back(), Map::operator[](items[i])); |
|
| 1935 |
invMap[Map::operator[](items[i])] = invMap.back(); |
|
| 1936 |
invMap.pop_back(); |
|
| 1937 |
} |
|
| 1938 |
Map::erase(items); |
|
| 1939 |
} |
|
| 1940 |
|
|
| 1941 |
/// \brief Build the unique map. |
|
| 1942 |
/// |
|
| 1943 |
/// Build the unique map. It is called by the |
|
| 1944 |
/// \c AlterationNotifier. |
|
| 1945 |
virtual void build() {
|
|
| 1946 |
Map::build(); |
|
| 1947 |
Item it; |
|
| 1948 |
const typename Map::Notifier* nf = Map::notifier(); |
|
| 1949 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
|
| 1950 |
Map::set(it, invMap.size()); |
|
| 1951 |
invMap.push_back(it); |
|
| 1952 |
} |
|
| 1953 |
} |
|
| 1954 |
|
|
| 1955 |
/// \brief Clear the keys from the map. |
|
| 1956 |
/// |
|
| 1957 |
/// Clear the keys from the map. It is called by the |
|
| 1958 |
/// \c AlterationNotifier. |
|
| 1959 |
virtual void clear() {
|
|
| 1960 |
invMap.clear(); |
|
| 1961 |
Map::clear(); |
|
| 1962 |
} |
|
| 1963 |
|
|
| 1964 |
public: |
|
| 1965 |
|
|
| 1966 |
/// \brief Returns the maximal value plus one. |
|
| 1967 |
/// |
|
| 1968 |
/// Returns the maximal value plus one in the map. |
|
| 1969 |
unsigned int size() const {
|
|
| 1970 |
return invMap.size(); |
|
| 1971 |
} |
|
| 1972 |
|
|
| 1973 |
/// \brief Swaps the position of the two items in the map. |
|
| 1974 |
/// |
|
| 1975 |
/// Swaps the position of the two items in the map. |
|
| 1976 |
void swap(const Item& p, const Item& q) {
|
|
| 1977 |
int pi = Map::operator[](p); |
|
| 1978 |
int qi = Map::operator[](q); |
|
| 1979 |
Map::set(p, qi); |
|
| 1980 |
invMap[qi] = p; |
|
| 1981 |
Map::set(q, pi); |
|
| 1982 |
invMap[pi] = q; |
|
| 1983 |
} |
|
| 1984 |
|
|
| 1985 |
/// \brief Gives back the \e descriptor of the item. |
|
| 1986 |
/// |
|
| 1987 |
/// Gives back the mutable and unique \e descriptor of the map. |
|
| 1988 |
int operator[](const Item& item) const {
|
|
| 1989 |
return Map::operator[](item); |
|
| 1990 |
} |
|
| 1991 |
|
|
| 1992 |
/// \brief Gives back the item by its descriptor. |
|
| 1993 |
/// |
|
| 1994 |
/// Gives back th item by its descriptor. |
|
| 1995 |
Item operator()(int id) const {
|
|
| 1996 |
return invMap[id]; |
|
| 1997 |
} |
|
| 1998 |
|
|
| 1999 |
private: |
|
| 2000 |
|
|
| 2001 |
typedef std::vector<Item> Container; |
|
| 2002 |
Container invMap; |
|
| 2003 |
|
|
| 2004 |
public: |
|
| 2005 |
/// \brief The inverse map type of DescriptorMap. |
|
| 2006 |
/// |
|
| 2007 |
/// The inverse map type of DescriptorMap. |
|
| 2008 |
class InverseMap {
|
|
| 2009 |
public: |
|
| 2010 |
/// \brief Constructor of the InverseMap. |
|
| 2011 |
/// |
|
| 2012 |
/// Constructor of the InverseMap. |
|
| 2013 |
explicit InverseMap(const DescriptorMap& _inverted) |
|
| 2014 |
: inverted(_inverted) {}
|
|
| 2015 |
|
|
| 2016 |
|
|
| 2017 |
/// The value type of the InverseMap. |
|
| 2018 |
typedef typename DescriptorMap::Key Value; |
|
| 2019 |
/// The key type of the InverseMap. |
|
| 2020 |
typedef typename DescriptorMap::Value Key; |
|
| 2021 |
|
|
| 2022 |
/// \brief Subscript operator. |
|
| 2023 |
/// |
|
| 2024 |
/// Subscript operator. It gives back the item |
|
| 2025 |
/// that the descriptor belongs to currently. |
|
| 2026 |
Value operator[](const Key& key) const {
|
|
| 2027 |
return inverted(key); |
|
| 2028 |
} |
|
| 2029 |
|
|
| 2030 |
/// \brief Size of the map. |
|
| 2031 |
/// |
|
| 2032 |
/// Returns the size of the map. |
|
| 2033 |
unsigned int size() const {
|
|
| 2034 |
return inverted.size(); |
|
| 2035 |
} |
|
| 2036 |
|
|
| 2037 |
private: |
|
| 2038 |
const DescriptorMap& inverted; |
|
| 2039 |
}; |
|
| 2040 |
|
|
| 2041 |
/// \brief Gives back the inverse of the map. |
|
| 2042 |
/// |
|
| 2043 |
/// Gives back the inverse of the map. |
|
| 2044 |
const InverseMap inverse() const {
|
|
| 2045 |
return InverseMap(*this); |
|
| 2046 |
} |
|
| 2047 |
}; |
|
| 2048 |
|
|
| 2049 |
/// \brief Returns the source of the given arc. |
|
| 2050 |
/// |
|
| 2051 |
/// The SourceMap gives back the source Node of the given arc. |
|
| 2052 |
/// \see TargetMap |
|
| 2053 |
/// \author Balazs Dezso |
|
| 2054 |
template <typename Digraph> |
|
| 2055 |
class SourceMap {
|
|
| 2056 |
public: |
|
| 2057 |
|
|
| 2058 |
typedef typename Digraph::Node Value; |
|
| 2059 |
typedef typename Digraph::Arc Key; |
|
| 2060 |
|
|
| 2061 |
/// \brief Constructor |
|
| 2062 |
/// |
|
| 2063 |
/// Constructor |
|
| 2064 |
/// \param _digraph The digraph that the map belongs to. |
|
| 2065 |
explicit SourceMap(const Digraph& _digraph) : digraph(_digraph) {}
|
|
| 2066 |
|
|
| 2067 |
/// \brief The subscript operator. |
|
| 2068 |
/// |
|
| 2069 |
/// The subscript operator. |
|
| 2070 |
/// \param arc The arc |
|
| 2071 |
/// \return The source of the arc |
|
| 2072 |
Value operator[](const Key& arc) const {
|
|
| 2073 |
return digraph.source(arc); |
|
| 2074 |
} |
|
| 2075 |
|
|
| 2076 |
private: |
|
| 2077 |
const Digraph& digraph; |
|
| 2078 |
}; |
|
| 2079 |
|
|
| 2080 |
/// \brief Returns a \ref SourceMap class. |
|
| 2081 |
/// |
|
| 2082 |
/// This function just returns an \ref SourceMap class. |
|
| 2083 |
/// \relates SourceMap |
|
| 2084 |
template <typename Digraph> |
|
| 2085 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
|
|
| 2086 |
return SourceMap<Digraph>(digraph); |
|
| 2087 |
} |
|
| 2088 |
|
|
| 2089 |
/// \brief Returns the target of the given arc. |
|
| 2090 |
/// |
|
| 2091 |
/// The TargetMap gives back the target Node of the given arc. |
|
| 2092 |
/// \see SourceMap |
|
| 2093 |
/// \author Balazs Dezso |
|
| 2094 |
template <typename Digraph> |
|
| 2095 |
class TargetMap {
|
|
| 2096 |
public: |
|
| 2097 |
|
|
| 2098 |
typedef typename Digraph::Node Value; |
|
| 2099 |
typedef typename Digraph::Arc Key; |
|
| 2100 |
|
|
| 2101 |
/// \brief Constructor |
|
| 2102 |
/// |
|
| 2103 |
/// Constructor |
|
| 2104 |
/// \param _digraph The digraph that the map belongs to. |
|
| 2105 |
explicit TargetMap(const Digraph& _digraph) : digraph(_digraph) {}
|
|
| 2106 |
|
|
| 2107 |
/// \brief The subscript operator. |
|
| 2108 |
/// |
|
| 2109 |
/// The subscript operator. |
|
| 2110 |
/// \param e The arc |
|
| 2111 |
/// \return The target of the arc |
|
| 2112 |
Value operator[](const Key& e) const {
|
|
| 2113 |
return digraph.target(e); |
|
| 2114 |
} |
|
| 2115 |
|
|
| 2116 |
private: |
|
| 2117 |
const Digraph& digraph; |
|
| 2118 |
}; |
|
| 2119 |
|
|
| 2120 |
/// \brief Returns a \ref TargetMap class. |
|
| 2121 |
/// |
|
| 2122 |
/// This function just returns a \ref TargetMap class. |
|
| 2123 |
/// \relates TargetMap |
|
| 2124 |
template <typename Digraph> |
|
| 2125 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
|
|
| 2126 |
return TargetMap<Digraph>(digraph); |
|
| 2127 |
} |
|
| 2128 |
|
|
| 2129 |
/// \brief Returns the "forward" directed arc view of an edge. |
|
| 2130 |
/// |
|
| 2131 |
/// Returns the "forward" directed arc view of an edge. |
|
| 2132 |
/// \see BackwardMap |
|
| 2133 |
/// \author Balazs Dezso |
|
| 2134 |
template <typename Digraph> |
|
| 2135 |
class ForwardMap {
|
|
| 2136 |
public: |
|
| 2137 |
|
|
| 2138 |
typedef typename Digraph::Arc Value; |
|
| 2139 |
typedef typename Digraph::Edge Key; |
|
| 2140 |
|
|
| 2141 |
/// \brief Constructor |
|
| 2142 |
/// |
|
| 2143 |
/// Constructor |
|
| 2144 |
/// \param _digraph The digraph that the map belongs to. |
|
| 2145 |
explicit ForwardMap(const Digraph& _digraph) : digraph(_digraph) {}
|
|
| 2146 |
|
|
| 2147 |
/// \brief The subscript operator. |
|
| 2148 |
/// |
|
| 2149 |
/// The subscript operator. |
|
| 2150 |
/// \param key An edge |
|
| 2151 |
/// \return The "forward" directed arc view of edge |
|
| 2152 |
Value operator[](const Key& key) const {
|
|
| 2153 |
return digraph.direct(key, true); |
|
| 2154 |
} |
|
| 2155 |
|
|
| 2156 |
private: |
|
| 2157 |
const Digraph& digraph; |
|
| 2158 |
}; |
|
| 2159 |
|
|
| 2160 |
/// \brief Returns a \ref ForwardMap class. |
|
| 2161 |
/// |
|
| 2162 |
/// This function just returns an \ref ForwardMap class. |
|
| 2163 |
/// \relates ForwardMap |
|
| 2164 |
template <typename Digraph> |
|
| 2165 |
inline ForwardMap<Digraph> forwardMap(const Digraph& digraph) {
|
|
| 2166 |
return ForwardMap<Digraph>(digraph); |
|
| 2167 |
} |
|
| 2168 |
|
|
| 2169 |
/// \brief Returns the "backward" directed arc view of an edge. |
|
| 2170 |
/// |
|
| 2171 |
/// Returns the "backward" directed arc view of an edge. |
|
| 2172 |
/// \see ForwardMap |
|
| 2173 |
/// \author Balazs Dezso |
|
| 2174 |
template <typename Digraph> |
|
| 2175 |
class BackwardMap {
|
|
| 2176 |
public: |
|
| 2177 |
|
|
| 2178 |
typedef typename Digraph::Arc Value; |
|
| 2179 |
typedef typename Digraph::Edge Key; |
|
| 2180 |
|
|
| 2181 |
/// \brief Constructor |
|
| 2182 |
/// |
|
| 2183 |
/// Constructor |
|
| 2184 |
/// \param _digraph The digraph that the map belongs to. |
|
| 2185 |
explicit BackwardMap(const Digraph& _digraph) : digraph(_digraph) {}
|
|
| 2186 |
|
|
| 2187 |
/// \brief The subscript operator. |
|
| 2188 |
/// |
|
| 2189 |
/// The subscript operator. |
|
| 2190 |
/// \param key An edge |
|
| 2191 |
/// \return The "backward" directed arc view of edge |
|
| 2192 |
Value operator[](const Key& key) const {
|
|
| 2193 |
return digraph.direct(key, false); |
|
| 2194 |
} |
|
| 2195 |
|
|
| 2196 |
private: |
|
| 2197 |
const Digraph& digraph; |
|
| 2198 |
}; |
|
| 2199 |
|
|
| 2200 |
/// \brief Returns a \ref BackwardMap class |
|
| 2201 |
|
|
| 2202 |
/// This function just returns a \ref BackwardMap class. |
|
| 2203 |
/// \relates BackwardMap |
|
| 2204 |
template <typename Digraph> |
|
| 2205 |
inline BackwardMap<Digraph> backwardMap(const Digraph& digraph) {
|
|
| 2206 |
return BackwardMap<Digraph>(digraph); |
|
| 2207 |
} |
|
| 2208 |
|
|
| 2209 |
/// \brief Potential difference map |
|
| 2210 |
/// |
|
| 2211 |
/// If there is an potential map on the nodes then we |
|
| 2212 |
/// can get an arc map as we get the substraction of the |
|
| 2213 |
/// values of the target and source. |
|
| 2214 |
template <typename Digraph, typename NodeMap> |
|
| 2215 |
class PotentialDifferenceMap {
|
|
| 2216 |
public: |
|
| 2217 |
typedef typename Digraph::Arc Key; |
|
| 2218 |
typedef typename NodeMap::Value Value; |
|
| 2219 |
|
|
| 2220 |
/// \brief Constructor |
|
| 2221 |
/// |
|
| 2222 |
/// Contructor of the map |
|
| 2223 |
explicit PotentialDifferenceMap(const Digraph& _digraph, |
|
| 2224 |
const NodeMap& _potential) |
|
| 2225 |
: digraph(_digraph), potential(_potential) {}
|
|
| 2226 |
|
|
| 2227 |
/// \brief Const subscription operator |
|
| 2228 |
/// |
|
| 2229 |
/// Const subscription operator |
|
| 2230 |
Value operator[](const Key& arc) const {
|
|
| 2231 |
return potential[digraph.target(arc)] - potential[digraph.source(arc)]; |
|
| 2232 |
} |
|
| 2233 |
|
|
| 2234 |
private: |
|
| 2235 |
const Digraph& digraph; |
|
| 2236 |
const NodeMap& potential; |
|
| 2237 |
}; |
|
| 2238 |
|
|
| 2239 |
/// \brief Returns a PotentialDifferenceMap. |
|
| 2240 |
/// |
|
| 2241 |
/// This function just returns a PotentialDifferenceMap. |
|
| 2242 |
/// \relates PotentialDifferenceMap |
|
| 2243 |
template <typename Digraph, typename NodeMap> |
|
| 2244 |
PotentialDifferenceMap<Digraph, NodeMap> |
|
| 2245 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
|
|
| 2246 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
|
| 2247 |
} |
|
| 2248 |
|
|
| 2249 |
/// \brief Map of the node in-degrees. |
|
| 2250 |
/// |
|
| 2251 |
/// This map returns the in-degree of a node. Once it is constructed, |
|
| 2252 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
| 2253 |
/// in constant time. On the other hand, the values are updated automatically |
|
| 2254 |
/// whenever the digraph changes. |
|
| 2255 |
/// |
|
| 2256 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
| 2257 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
|
| 2258 |
/// is not guarantied if these additional features are used. For example |
|
| 2259 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
| 2260 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
|
| 2261 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
|
| 2262 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
|
| 2263 |
/// |
|
| 2264 |
/// \sa OutDegMap |
|
| 2265 |
|
|
| 2266 |
template <typename _Digraph> |
|
| 2267 |
class InDegMap |
|
| 2268 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
| 2269 |
::ItemNotifier::ObserverBase {
|
|
| 2270 |
|
|
| 2271 |
public: |
|
| 2272 |
|
|
| 2273 |
typedef _Digraph Digraph; |
|
| 2274 |
typedef int Value; |
|
| 2275 |
typedef typename Digraph::Node Key; |
|
| 2276 |
|
|
| 2277 |
typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
| 2278 |
::ItemNotifier::ObserverBase Parent; |
|
| 2279 |
|
|
| 2280 |
private: |
|
| 2281 |
|
|
| 2282 |
class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
|
|
| 2283 |
public: |
|
| 2284 |
|
|
| 2285 |
typedef DefaultMap<_Digraph, Key, int> Parent; |
|
| 2286 |
typedef typename Parent::Digraph Digraph; |
|
| 2287 |
|
|
| 2288 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
|
| 2289 |
|
|
| 2290 |
virtual void add(const Key& key) {
|
|
| 2291 |
Parent::add(key); |
|
| 2292 |
Parent::set(key, 0); |
|
| 2293 |
} |
|
| 2294 |
|
|
| 2295 |
virtual void add(const std::vector<Key>& keys) {
|
|
| 2296 |
Parent::add(keys); |
|
| 2297 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
| 2298 |
Parent::set(keys[i], 0); |
|
| 2299 |
} |
|
| 2300 |
} |
|
| 2301 |
|
|
| 2302 |
virtual void build() {
|
|
| 2303 |
Parent::build(); |
|
| 2304 |
Key it; |
|
| 2305 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
| 2306 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
|
| 2307 |
Parent::set(it, 0); |
|
| 2308 |
} |
|
| 2309 |
} |
|
| 2310 |
}; |
|
| 2311 |
|
|
| 2312 |
public: |
|
| 2313 |
|
|
| 2314 |
/// \brief Constructor. |
|
| 2315 |
/// |
|
| 2316 |
/// Constructor for creating in-degree map. |
|
| 2317 |
explicit InDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
|
|
| 2318 |
Parent::attach(digraph.notifier(typename _Digraph::Arc())); |
|
| 2319 |
|
|
| 2320 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
|
|
| 2321 |
deg[it] = countInArcs(digraph, it); |
|
| 2322 |
} |
|
| 2323 |
} |
|
| 2324 |
|
|
| 2325 |
/// Gives back the in-degree of a Node. |
|
| 2326 |
int operator[](const Key& key) const {
|
|
| 2327 |
return deg[key]; |
|
| 2328 |
} |
|
| 2329 |
|
|
| 2330 |
protected: |
|
| 2331 |
|
|
| 2332 |
typedef typename Digraph::Arc Arc; |
|
| 2333 |
|
|
| 2334 |
virtual void add(const Arc& arc) {
|
|
| 2335 |
++deg[digraph.target(arc)]; |
|
| 2336 |
} |
|
| 2337 |
|
|
| 2338 |
virtual void add(const std::vector<Arc>& arcs) {
|
|
| 2339 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
|
| 2340 |
++deg[digraph.target(arcs[i])]; |
|
| 2341 |
} |
|
| 2342 |
} |
|
| 2343 |
|
|
| 2344 |
virtual void erase(const Arc& arc) {
|
|
| 2345 |
--deg[digraph.target(arc)]; |
|
| 2346 |
} |
|
| 2347 |
|
|
| 2348 |
virtual void erase(const std::vector<Arc>& arcs) {
|
|
| 2349 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
|
| 2350 |
--deg[digraph.target(arcs[i])]; |
|
| 2351 |
} |
|
| 2352 |
} |
|
| 2353 |
|
|
| 2354 |
virtual void build() {
|
|
| 2355 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
|
|
| 2356 |
deg[it] = countInArcs(digraph, it); |
|
| 2357 |
} |
|
| 2358 |
} |
|
| 2359 |
|
|
| 2360 |
virtual void clear() {
|
|
| 2361 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
|
|
| 2362 |
deg[it] = 0; |
|
| 2363 |
} |
|
| 2364 |
} |
|
| 2365 |
private: |
|
| 2366 |
|
|
| 2367 |
const _Digraph& digraph; |
|
| 2368 |
AutoNodeMap deg; |
|
| 2369 |
}; |
|
| 2370 |
|
|
| 2371 |
/// \brief Map of the node out-degrees. |
|
| 2372 |
/// |
|
| 2373 |
/// This map returns the out-degree of a node. Once it is constructed, |
|
| 2374 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
| 2375 |
/// in constant time. On the other hand, the values are updated automatically |
|
| 2376 |
/// whenever the digraph changes. |
|
| 2377 |
/// |
|
| 2378 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
| 2379 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap |
|
| 2380 |
/// is not guarantied if these additional features are used. For example |
|
| 2381 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
| 2382 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
|
| 2383 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
|
| 2384 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
|
| 2385 |
/// |
|
| 2386 |
/// \sa InDegMap |
|
| 2387 |
|
|
| 2388 |
template <typename _Digraph> |
|
| 2389 |
class OutDegMap |
|
| 2390 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
| 2391 |
::ItemNotifier::ObserverBase {
|
|
| 2392 |
|
|
| 2393 |
public: |
|
| 2394 |
|
|
| 2395 |
typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
| 2396 |
::ItemNotifier::ObserverBase Parent; |
|
| 2397 |
|
|
| 2398 |
typedef _Digraph Digraph; |
|
| 2399 |
typedef int Value; |
|
| 2400 |
typedef typename Digraph::Node Key; |
|
| 2401 |
|
|
| 2402 |
private: |
|
| 2403 |
|
|
| 2404 |
class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
|
|
| 2405 |
public: |
|
| 2406 |
|
|
| 2407 |
typedef DefaultMap<_Digraph, Key, int> Parent; |
|
| 2408 |
typedef typename Parent::Digraph Digraph; |
|
| 2409 |
|
|
| 2410 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
|
| 2411 |
|
|
| 2412 |
virtual void add(const Key& key) {
|
|
| 2413 |
Parent::add(key); |
|
| 2414 |
Parent::set(key, 0); |
|
| 2415 |
} |
|
| 2416 |
virtual void add(const std::vector<Key>& keys) {
|
|
| 2417 |
Parent::add(keys); |
|
| 2418 |
for (int i = 0; i < int(keys.size()); ++i) {
|
|
| 2419 |
Parent::set(keys[i], 0); |
|
| 2420 |
} |
|
| 2421 |
} |
|
| 2422 |
virtual void build() {
|
|
| 2423 |
Parent::build(); |
|
| 2424 |
Key it; |
|
| 2425 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
| 2426 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
|
| 2427 |
Parent::set(it, 0); |
|
| 2428 |
} |
|
| 2429 |
} |
|
| 2430 |
}; |
|
| 2431 |
|
|
| 2432 |
public: |
|
| 2433 |
|
|
| 2434 |
/// \brief Constructor. |
|
| 2435 |
/// |
|
| 2436 |
/// Constructor for creating out-degree map. |
|
| 2437 |
explicit OutDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
|
|
| 2438 |
Parent::attach(digraph.notifier(typename _Digraph::Arc())); |
|
| 2439 |
|
|
| 2440 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
|
|
| 2441 |
deg[it] = countOutArcs(digraph, it); |
|
| 2442 |
} |
|
| 2443 |
} |
|
| 2444 |
|
|
| 2445 |
/// Gives back the out-degree of a Node. |
|
| 2446 |
int operator[](const Key& key) const {
|
|
| 2447 |
return deg[key]; |
|
| 2448 |
} |
|
| 2449 |
|
|
| 2450 |
protected: |
|
| 2451 |
|
|
| 2452 |
typedef typename Digraph::Arc Arc; |
|
| 2453 |
|
|
| 2454 |
virtual void add(const Arc& arc) {
|
|
| 2455 |
++deg[digraph.source(arc)]; |
|
| 2456 |
} |
|
| 2457 |
|
|
| 2458 |
virtual void add(const std::vector<Arc>& arcs) {
|
|
| 2459 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
|
| 2460 |
++deg[digraph.source(arcs[i])]; |
|
| 2461 |
} |
|
| 2462 |
} |
|
| 2463 |
|
|
| 2464 |
virtual void erase(const Arc& arc) {
|
|
| 2465 |
--deg[digraph.source(arc)]; |
|
| 2466 |
} |
|
| 2467 |
|
|
| 2468 |
virtual void erase(const std::vector<Arc>& arcs) {
|
|
| 2469 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
|
| 2470 |
--deg[digraph.source(arcs[i])]; |
|
| 2471 |
} |
|
| 2472 |
} |
|
| 2473 |
|
|
| 2474 |
virtual void build() {
|
|
| 2475 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
|
|
| 2476 |
deg[it] = countOutArcs(digraph, it); |
|
| 2477 |
} |
|
| 2478 |
} |
|
| 2479 |
|
|
| 2480 |
virtual void clear() {
|
|
| 2481 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
|
|
| 2482 |
deg[it] = 0; |
|
| 2483 |
} |
|
| 2484 |
} |
|
| 2485 |
private: |
|
| 2486 |
|
|
| 2487 |
const _Digraph& digraph; |
|
| 2488 |
AutoNodeMap deg; |
|
| 2489 |
}; |
|
| 2490 |
|
|
| 2491 |
|
|
| 2492 |
///Dynamic arc look up between given endpoints. |
|
| 2493 |
|
|
| 2494 |
///\ingroup gutils |
|
| 2495 |
///Using this class, you can find an arc in a digraph from a given |
|
| 2496 |
///source to a given target in amortized time <em>O(log d)</em>, |
|
| 2497 |
///where <em>d</em> is the out-degree of the source node. |
|
| 2498 |
/// |
|
| 2499 |
///It is possible to find \e all parallel arcs between two nodes with |
|
| 2500 |
///the \c findFirst() and \c findNext() members. |
|
| 2501 |
/// |
|
| 2502 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
|
| 2503 |
///digraph do not changed so frequently. |
|
| 2504 |
/// |
|
| 2505 |
///This class uses a self-adjusting binary search tree, Sleator's |
|
| 2506 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
|
| 2507 |
///time bound for arc lookups. This class also guarantees the |
|
| 2508 |
///optimal time bound in a constant factor for any distribution of |
|
| 2509 |
///queries. |
|
| 2510 |
/// |
|
| 2511 |
///\param G The type of the underlying digraph. |
|
| 2512 |
/// |
|
| 2513 |
///\sa ArcLookUp |
|
| 2514 |
///\sa AllArcLookUp |
|
| 2515 |
template<class G> |
|
| 2516 |
class DynArcLookUp |
|
| 2517 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
|
| 2518 |
{
|
|
| 2519 |
public: |
|
| 2520 |
typedef typename ItemSetTraits<G, typename G::Arc> |
|
| 2521 |
::ItemNotifier::ObserverBase Parent; |
|
| 2522 |
|
|
| 2523 |
GRAPH_TYPEDEFS(typename G); |
|
| 2524 |
typedef G Digraph; |
|
| 2525 |
|
|
| 2526 |
protected: |
|
| 2527 |
|
|
| 2528 |
class AutoNodeMap : public DefaultMap<G, Node, Arc> {
|
|
| 2529 |
public: |
|
| 2530 |
|
|
| 2531 |
typedef DefaultMap<G, Node, Arc> Parent; |
|
| 2532 |
|
|
| 2533 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
|
|
| 2534 |
|
|
| 2535 |
virtual void add(const Node& node) {
|
|
| 2536 |
Parent::add(node); |
|
| 2537 |
Parent::set(node, INVALID); |
|
| 2538 |
} |
|
| 2539 |
|
|
| 2540 |
virtual void add(const std::vector<Node>& nodes) {
|
|
| 2541 |
Parent::add(nodes); |
|
| 2542 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
|
| 2543 |
Parent::set(nodes[i], INVALID); |
|
| 2544 |
} |
|
| 2545 |
} |
|
| 2546 |
|
|
| 2547 |
virtual void build() {
|
|
| 2548 |
Parent::build(); |
|
| 2549 |
Node it; |
|
| 2550 |
typename Parent::Notifier* nf = Parent::notifier(); |
|
| 2551 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
|
| 2552 |
Parent::set(it, INVALID); |
|
| 2553 |
} |
|
| 2554 |
} |
|
| 2555 |
}; |
|
| 2556 |
|
|
| 2557 |
const Digraph &_g; |
|
| 2558 |
AutoNodeMap _head; |
|
| 2559 |
typename Digraph::template ArcMap<Arc> _parent; |
|
| 2560 |
typename Digraph::template ArcMap<Arc> _left; |
|
| 2561 |
typename Digraph::template ArcMap<Arc> _right; |
|
| 2562 |
|
|
| 2563 |
class ArcLess {
|
|
| 2564 |
const Digraph &g; |
|
| 2565 |
public: |
|
| 2566 |
ArcLess(const Digraph &_g) : g(_g) {}
|
|
| 2567 |
bool operator()(Arc a,Arc b) const |
|
| 2568 |
{
|
|
| 2569 |
return g.target(a)<g.target(b); |
|
| 2570 |
} |
|
| 2571 |
}; |
|
| 2572 |
|
|
| 2573 |
public: |
|
| 2574 |
|
|
| 2575 |
///Constructor |
|
| 2576 |
|
|
| 2577 |
///Constructor. |
|
| 2578 |
/// |
|
| 2579 |
///It builds up the search database. |
|
| 2580 |
DynArcLookUp(const Digraph &g) |
|
| 2581 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
|
| 2582 |
{
|
|
| 2583 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
|
| 2584 |
refresh(); |
|
| 2585 |
} |
|
| 2586 |
|
|
| 2587 |
protected: |
|
| 2588 |
|
|
| 2589 |
virtual void add(const Arc& arc) {
|
|
| 2590 |
insert(arc); |
|
| 2591 |
} |
|
| 2592 |
|
|
| 2593 |
virtual void add(const std::vector<Arc>& arcs) {
|
|
| 2594 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
|
| 2595 |
insert(arcs[i]); |
|
| 2596 |
} |
|
| 2597 |
} |
|
| 2598 |
|
|
| 2599 |
virtual void erase(const Arc& arc) {
|
|
| 2600 |
remove(arc); |
|
| 2601 |
} |
|
| 2602 |
|
|
| 2603 |
virtual void erase(const std::vector<Arc>& arcs) {
|
|
| 2604 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
|
| 2605 |
remove(arcs[i]); |
|
| 2606 |
} |
|
| 2607 |
} |
|
| 2608 |
|
|
| 2609 |
virtual void build() {
|
|
| 2610 |
refresh(); |
|
| 2611 |
} |
|
| 2612 |
|
|
| 2613 |
virtual void clear() {
|
|
| 2614 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
|
| 2615 |
_head.set(n, INVALID); |
|
| 2616 |
} |
|
| 2617 |
} |
|
| 2618 |
|
|
| 2619 |
void insert(Arc arc) {
|
|
| 2620 |
Node s = _g.source(arc); |
|
| 2621 |
Node t = _g.target(arc); |
|
| 2622 |
_left.set(arc, INVALID); |
|
| 2623 |
_right.set(arc, INVALID); |
|
| 2624 |
|
|
| 2625 |
Arc e = _head[s]; |
|
| 2626 |
if (e == INVALID) {
|
|
| 2627 |
_head.set(s, arc); |
|
| 2628 |
_parent.set(arc, INVALID); |
|
| 2629 |
return; |
|
| 2630 |
} |
|
| 2631 |
while (true) {
|
|
| 2632 |
if (t < _g.target(e)) {
|
|
| 2633 |
if (_left[e] == INVALID) {
|
|
| 2634 |
_left.set(e, arc); |
|
| 2635 |
_parent.set(arc, e); |
|
| 2636 |
splay(arc); |
|
| 2637 |
return; |
|
| 2638 |
} else {
|
|
| 2639 |
e = _left[e]; |
|
| 2640 |
} |
|
| 2641 |
} else {
|
|
| 2642 |
if (_right[e] == INVALID) {
|
|
| 2643 |
_right.set(e, arc); |
|
| 2644 |
_parent.set(arc, e); |
|
| 2645 |
splay(arc); |
|
| 2646 |
return; |
|
| 2647 |
} else {
|
|
| 2648 |
e = _right[e]; |
|
| 2649 |
} |
|
| 2650 |
} |
|
| 2651 |
} |
|
| 2652 |
} |
|
| 2653 |
|
|
| 2654 |
void remove(Arc arc) {
|
|
| 2655 |
if (_left[arc] == INVALID) {
|
|
| 2656 |
if (_right[arc] != INVALID) {
|
|
| 2657 |
_parent.set(_right[arc], _parent[arc]); |
|
| 2658 |
} |
|
| 2659 |
if (_parent[arc] != INVALID) {
|
|
| 2660 |
if (_left[_parent[arc]] == arc) {
|
|
| 2661 |
_left.set(_parent[arc], _right[arc]); |
|
| 2662 |
} else {
|
|
| 2663 |
_right.set(_parent[arc], _right[arc]); |
|
| 2664 |
} |
|
| 2665 |
} else {
|
|
| 2666 |
_head.set(_g.source(arc), _right[arc]); |
|
| 2667 |
} |
|
| 2668 |
} else if (_right[arc] == INVALID) {
|
|
| 2669 |
_parent.set(_left[arc], _parent[arc]); |
|
| 2670 |
if (_parent[arc] != INVALID) {
|
|
| 2671 |
if (_left[_parent[arc]] == arc) {
|
|
| 2672 |
_left.set(_parent[arc], _left[arc]); |
|
| 2673 |
} else {
|
|
| 2674 |
_right.set(_parent[arc], _left[arc]); |
|
| 2675 |
} |
|
| 2676 |
} else {
|
|
| 2677 |
_head.set(_g.source(arc), _left[arc]); |
|
| 2678 |
} |
|
| 2679 |
} else {
|
|
| 2680 |
Arc e = _left[arc]; |
|
| 2681 |
if (_right[e] != INVALID) {
|
|
| 2682 |
e = _right[e]; |
|
| 2683 |
while (_right[e] != INVALID) {
|
|
| 2684 |
e = _right[e]; |
|
| 2685 |
} |
|
| 2686 |
Arc s = _parent[e]; |
|
| 2687 |
_right.set(_parent[e], _left[e]); |
|
| 2688 |
if (_left[e] != INVALID) {
|
|
| 2689 |
_parent.set(_left[e], _parent[e]); |
|
| 2690 |
} |
|
| 2691 |
|
|
| 2692 |
_left.set(e, _left[arc]); |
|
| 2693 |
_parent.set(_left[arc], e); |
|
| 2694 |
_right.set(e, _right[arc]); |
|
| 2695 |
_parent.set(_right[arc], e); |
|
| 2696 |
|
|
| 2697 |
_parent.set(e, _parent[arc]); |
|
| 2698 |
if (_parent[arc] != INVALID) {
|
|
| 2699 |
if (_left[_parent[arc]] == arc) {
|
|
| 2700 |
_left.set(_parent[arc], e); |
|
| 2701 |
} else {
|
|
| 2702 |
_right.set(_parent[arc], e); |
|
| 2703 |
} |
|
| 2704 |
} |
|
| 2705 |
splay(s); |
|
| 2706 |
} else {
|
|
| 2707 |
_right.set(e, _right[arc]); |
|
| 2708 |
_parent.set(_right[arc], e); |
|
| 2709 |
|
|
| 2710 |
if (_parent[arc] != INVALID) {
|
|
| 2711 |
if (_left[_parent[arc]] == arc) {
|
|
| 2712 |
_left.set(_parent[arc], e); |
|
| 2713 |
} else {
|
|
| 2714 |
_right.set(_parent[arc], e); |
|
| 2715 |
} |
|
| 2716 |
} else {
|
|
| 2717 |
_head.set(_g.source(arc), e); |
|
| 2718 |
} |
|
| 2719 |
} |
|
| 2720 |
} |
|
| 2721 |
} |
|
| 2722 |
|
|
| 2723 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
|
| 2724 |
{
|
|
| 2725 |
int m=(a+b)/2; |
|
| 2726 |
Arc me=v[m]; |
|
| 2727 |
if (a < m) {
|
|
| 2728 |
Arc left = refreshRec(v,a,m-1); |
|
| 2729 |
_left.set(me, left); |
|
| 2730 |
_parent.set(left, me); |
|
| 2731 |
} else {
|
|
| 2732 |
_left.set(me, INVALID); |
|
| 2733 |
} |
|
| 2734 |
if (m < b) {
|
|
| 2735 |
Arc right = refreshRec(v,m+1,b); |
|
| 2736 |
_right.set(me, right); |
|
| 2737 |
_parent.set(right, me); |
|
| 2738 |
} else {
|
|
| 2739 |
_right.set(me, INVALID); |
|
| 2740 |
} |
|
| 2741 |
return me; |
|
| 2742 |
} |
|
| 2743 |
|
|
| 2744 |
void refresh() {
|
|
| 2745 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
|
| 2746 |
std::vector<Arc> v; |
|
| 2747 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
|
| 2748 |
if(v.size()) {
|
|
| 2749 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
|
| 2750 |
Arc head = refreshRec(v,0,v.size()-1); |
|
| 2751 |
_head.set(n, head); |
|
| 2752 |
_parent.set(head, INVALID); |
|
| 2753 |
} |
|
| 2754 |
else _head.set(n, INVALID); |
|
| 2755 |
} |
|
| 2756 |
} |
|
| 2757 |
|
|
| 2758 |
void zig(Arc v) {
|
|
| 2759 |
Arc w = _parent[v]; |
|
| 2760 |
_parent.set(v, _parent[w]); |
|
| 2761 |
_parent.set(w, v); |
|
| 2762 |
_left.set(w, _right[v]); |
|
| 2763 |
_right.set(v, w); |
|
| 2764 |
if (_parent[v] != INVALID) {
|
|
| 2765 |
if (_right[_parent[v]] == w) {
|
|
| 2766 |
_right.set(_parent[v], v); |
|
| 2767 |
} else {
|
|
| 2768 |
_left.set(_parent[v], v); |
|
| 2769 |
} |
|
| 2770 |
} |
|
| 2771 |
if (_left[w] != INVALID){
|
|
| 2772 |
_parent.set(_left[w], w); |
|
| 2773 |
} |
|
| 2774 |
} |
|
| 2775 |
|
|
| 2776 |
void zag(Arc v) {
|
|
| 2777 |
Arc w = _parent[v]; |
|
| 2778 |
_parent.set(v, _parent[w]); |
|
| 2779 |
_parent.set(w, v); |
|
| 2780 |
_right.set(w, _left[v]); |
|
| 2781 |
_left.set(v, w); |
|
| 2782 |
if (_parent[v] != INVALID){
|
|
| 2783 |
if (_left[_parent[v]] == w) {
|
|
| 2784 |
_left.set(_parent[v], v); |
|
| 2785 |
} else {
|
|
| 2786 |
_right.set(_parent[v], v); |
|
| 2787 |
} |
|
| 2788 |
} |
|
| 2789 |
if (_right[w] != INVALID){
|
|
| 2790 |
_parent.set(_right[w], w); |
|
| 2791 |
} |
|
| 2792 |
} |
|
| 2793 |
|
|
| 2794 |
void splay(Arc v) {
|
|
| 2795 |
while (_parent[v] != INVALID) {
|
|
| 2796 |
if (v == _left[_parent[v]]) {
|
|
| 2797 |
if (_parent[_parent[v]] == INVALID) {
|
|
| 2798 |
zig(v); |
|
| 2799 |
} else {
|
|
| 2800 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
|
| 2801 |
zig(_parent[v]); |
|
| 2802 |
zig(v); |
|
| 2803 |
} else {
|
|
| 2804 |
zig(v); |
|
| 2805 |
zag(v); |
|
| 2806 |
} |
|
| 2807 |
} |
|
| 2808 |
} else {
|
|
| 2809 |
if (_parent[_parent[v]] == INVALID) {
|
|
| 2810 |
zag(v); |
|
| 2811 |
} else {
|
|
| 2812 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
|
| 2813 |
zag(v); |
|
| 2814 |
zig(v); |
|
| 2815 |
} else {
|
|
| 2816 |
zag(_parent[v]); |
|
| 2817 |
zag(v); |
|
| 2818 |
} |
|
| 2819 |
} |
|
| 2820 |
} |
|
| 2821 |
} |
|
| 2822 |
_head[_g.source(v)] = v; |
|
| 2823 |
} |
|
| 2824 |
|
|
| 2825 |
|
|
| 2826 |
public: |
|
| 2827 |
|
|
| 2828 |
///Find an arc between two nodes. |
|
| 2829 |
|
|
| 2830 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
| 2831 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
| 2832 |
///\param s The source node |
|
| 2833 |
///\param t The target node |
|
| 2834 |
///\return An arc from \c s to \c t if there exists, |
|
| 2835 |
///\ref INVALID otherwise. |
|
| 2836 |
Arc operator()(Node s, Node t) const |
|
| 2837 |
{
|
|
| 2838 |
Arc e = _head[s]; |
|
| 2839 |
while (true) {
|
|
| 2840 |
if (_g.target(e) == t) {
|
|
| 2841 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
| 2842 |
return e; |
|
| 2843 |
} else if (t < _g.target(e)) {
|
|
| 2844 |
if (_left[e] == INVALID) {
|
|
| 2845 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
| 2846 |
return INVALID; |
|
| 2847 |
} else {
|
|
| 2848 |
e = _left[e]; |
|
| 2849 |
} |
|
| 2850 |
} else {
|
|
| 2851 |
if (_right[e] == INVALID) {
|
|
| 2852 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
| 2853 |
return INVALID; |
|
| 2854 |
} else {
|
|
| 2855 |
e = _right[e]; |
|
| 2856 |
} |
|
| 2857 |
} |
|
| 2858 |
} |
|
| 2859 |
} |
|
| 2860 |
|
|
| 2861 |
///Find the first arc between two nodes. |
|
| 2862 |
|
|
| 2863 |
///Find the first arc between two nodes in time |
|
| 2864 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
| 2865 |
/// outgoing arcs of \c s. |
|
| 2866 |
///\param s The source node |
|
| 2867 |
///\param t The target node |
|
| 2868 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
| 2869 |
/// otherwise. |
|
| 2870 |
Arc findFirst(Node s, Node t) const |
|
| 2871 |
{
|
|
| 2872 |
Arc e = _head[s]; |
|
| 2873 |
Arc r = INVALID; |
|
| 2874 |
while (true) {
|
|
| 2875 |
if (_g.target(e) < t) {
|
|
| 2876 |
if (_right[e] == INVALID) {
|
|
| 2877 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
| 2878 |
return r; |
|
| 2879 |
} else {
|
|
| 2880 |
e = _right[e]; |
|
| 2881 |
} |
|
| 2882 |
} else {
|
|
| 2883 |
if (_g.target(e) == t) {
|
|
| 2884 |
r = e; |
|
| 2885 |
} |
|
| 2886 |
if (_left[e] == INVALID) {
|
|
| 2887 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
| 2888 |
return r; |
|
| 2889 |
} else {
|
|
| 2890 |
e = _left[e]; |
|
| 2891 |
} |
|
| 2892 |
} |
|
| 2893 |
} |
|
| 2894 |
} |
|
| 2895 |
|
|
| 2896 |
///Find the next arc between two nodes. |
|
| 2897 |
|
|
| 2898 |
///Find the next arc between two nodes in time |
|
| 2899 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
| 2900 |
/// outgoing arcs of \c s. |
|
| 2901 |
///\param s The source node |
|
| 2902 |
///\param t The target node |
|
| 2903 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
| 2904 |
/// otherwise. |
|
| 2905 |
|
|
| 2906 |
///\note If \c e is not the result of the previous \c findFirst() |
|
| 2907 |
///operation then the amorized time bound can not be guaranteed. |
|
| 2908 |
#ifdef DOXYGEN |
|
| 2909 |
Arc findNext(Node s, Node t, Arc e) const |
|
| 2910 |
#else |
|
| 2911 |
Arc findNext(Node, Node t, Arc e) const |
|
| 2912 |
#endif |
|
| 2913 |
{
|
|
| 2914 |
if (_right[e] != INVALID) {
|
|
| 2915 |
e = _right[e]; |
|
| 2916 |
while (_left[e] != INVALID) {
|
|
| 2917 |
e = _left[e]; |
|
| 2918 |
} |
|
| 2919 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
| 2920 |
} else {
|
|
| 2921 |
while (_parent[e] != INVALID && _right[_parent[e]] == e) {
|
|
| 2922 |
e = _parent[e]; |
|
| 2923 |
} |
|
| 2924 |
if (_parent[e] == INVALID) {
|
|
| 2925 |
return INVALID; |
|
| 2926 |
} else {
|
|
| 2927 |
e = _parent[e]; |
|
| 2928 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
| 2929 |
} |
|
| 2930 |
} |
|
| 2931 |
if (_g.target(e) == t) return e; |
|
| 2932 |
else return INVALID; |
|
| 2933 |
} |
|
| 2934 |
|
|
| 2935 |
}; |
|
| 2936 |
|
|
| 2937 |
///Fast arc look up between given endpoints. |
|
| 2938 |
|
|
| 2939 |
///\ingroup gutils |
|
| 2940 |
///Using this class, you can find an arc in a digraph from a given |
|
| 2941 |
///source to a given target in time <em>O(log d)</em>, |
|
| 2942 |
///where <em>d</em> is the out-degree of the source node. |
|
| 2943 |
/// |
|
| 2944 |
///It is not possible to find \e all parallel arcs between two nodes. |
|
| 2945 |
///Use \ref AllArcLookUp for this purpose. |
|
| 2946 |
/// |
|
| 2947 |
///\warning This class is static, so you should refresh() (or at least |
|
| 2948 |
///refresh(Node)) this data structure |
|
| 2949 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
| 2950 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
| 2951 |
/// |
|
| 2952 |
///\param G The type of the underlying digraph. |
|
| 2953 |
/// |
|
| 2954 |
///\sa DynArcLookUp |
|
| 2955 |
///\sa AllArcLookUp |
|
| 2956 |
template<class G> |
|
| 2957 |
class ArcLookUp |
|
| 2958 |
{
|
|
| 2959 |
public: |
|
| 2960 |
GRAPH_TYPEDEFS(typename G); |
|
| 2961 |
typedef G Digraph; |
|
| 2962 |
|
|
| 2963 |
protected: |
|
| 2964 |
const Digraph &_g; |
|
| 2965 |
typename Digraph::template NodeMap<Arc> _head; |
|
| 2966 |
typename Digraph::template ArcMap<Arc> _left; |
|
| 2967 |
typename Digraph::template ArcMap<Arc> _right; |
|
| 2968 |
|
|
| 2969 |
class ArcLess {
|
|
| 2970 |
const Digraph &g; |
|
| 2971 |
public: |
|
| 2972 |
ArcLess(const Digraph &_g) : g(_g) {}
|
|
| 2973 |
bool operator()(Arc a,Arc b) const |
|
| 2974 |
{
|
|
| 2975 |
return g.target(a)<g.target(b); |
|
| 2976 |
} |
|
| 2977 |
}; |
|
| 2978 |
|
|
| 2979 |
public: |
|
| 2980 |
|
|
| 2981 |
///Constructor |
|
| 2982 |
|
|
| 2983 |
///Constructor. |
|
| 2984 |
/// |
|
| 2985 |
///It builds up the search database, which remains valid until the digraph |
|
| 2986 |
///changes. |
|
| 2987 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
|
|
| 2988 |
|
|
| 2989 |
private: |
|
| 2990 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
|
| 2991 |
{
|
|
| 2992 |
int m=(a+b)/2; |
|
| 2993 |
Arc me=v[m]; |
|
| 2994 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
|
| 2995 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
|
| 2996 |
return me; |
|
| 2997 |
} |
|
| 2998 |
public: |
|
| 2999 |
///Refresh the data structure at a node. |
|
| 3000 |
|
|
| 3001 |
///Build up the search database of node \c n. |
|
| 3002 |
/// |
|
| 3003 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
| 3004 |
///the number of the outgoing arcs of \c n. |
|
| 3005 |
void refresh(Node n) |
|
| 3006 |
{
|
|
| 3007 |
std::vector<Arc> v; |
|
| 3008 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
|
| 3009 |
if(v.size()) {
|
|
| 3010 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
|
| 3011 |
_head[n]=refreshRec(v,0,v.size()-1); |
|
| 3012 |
} |
|
| 3013 |
else _head[n]=INVALID; |
|
| 3014 |
} |
|
| 3015 |
///Refresh the full data structure. |
|
| 3016 |
|
|
| 3017 |
///Build up the full search database. In fact, it simply calls |
|
| 3018 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
|
| 3019 |
/// |
|
| 3020 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
| 3021 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
| 3022 |
///out-degree of the digraph. |
|
| 3023 |
|
|
| 3024 |
void refresh() |
|
| 3025 |
{
|
|
| 3026 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
|
| 3027 |
} |
|
| 3028 |
|
|
| 3029 |
///Find an arc between two nodes. |
|
| 3030 |
|
|
| 3031 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
| 3032 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
| 3033 |
///\param s The source node |
|
| 3034 |
///\param t The target node |
|
| 3035 |
///\return An arc from \c s to \c t if there exists, |
|
| 3036 |
///\ref INVALID otherwise. |
|
| 3037 |
/// |
|
| 3038 |
///\warning If you change the digraph, refresh() must be called before using |
|
| 3039 |
///this operator. If you change the outgoing arcs of |
|
| 3040 |
///a single node \c n, then |
|
| 3041 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
| 3042 |
/// |
|
| 3043 |
Arc operator()(Node s, Node t) const |
|
| 3044 |
{
|
|
| 3045 |
Arc e; |
|
| 3046 |
for(e=_head[s]; |
|
| 3047 |
e!=INVALID&&_g.target(e)!=t; |
|
| 3048 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
|
| 3049 |
return e; |
|
| 3050 |
} |
|
| 3051 |
|
|
| 3052 |
}; |
|
| 3053 |
|
|
| 3054 |
///Fast look up of all arcs between given endpoints. |
|
| 3055 |
|
|
| 3056 |
///\ingroup gutils |
|
| 3057 |
///This class is the same as \ref ArcLookUp, with the addition |
|
| 3058 |
///that it makes it possible to find all arcs between given endpoints. |
|
| 3059 |
/// |
|
| 3060 |
///\warning This class is static, so you should refresh() (or at least |
|
| 3061 |
///refresh(Node)) this data structure |
|
| 3062 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
| 3063 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
| 3064 |
/// |
|
| 3065 |
///\param G The type of the underlying digraph. |
|
| 3066 |
/// |
|
| 3067 |
///\sa DynArcLookUp |
|
| 3068 |
///\sa ArcLookUp |
|
| 3069 |
template<class G> |
|
| 3070 |
class AllArcLookUp : public ArcLookUp<G> |
|
| 3071 |
{
|
|
| 3072 |
using ArcLookUp<G>::_g; |
|
| 3073 |
using ArcLookUp<G>::_right; |
|
| 3074 |
using ArcLookUp<G>::_left; |
|
| 3075 |
using ArcLookUp<G>::_head; |
|
| 3076 |
|
|
| 3077 |
GRAPH_TYPEDEFS(typename G); |
|
| 3078 |
typedef G Digraph; |
|
| 3079 |
|
|
| 3080 |
typename Digraph::template ArcMap<Arc> _next; |
|
| 3081 |
|
|
| 3082 |
Arc refreshNext(Arc head,Arc next=INVALID) |
|
| 3083 |
{
|
|
| 3084 |
if(head==INVALID) return next; |
|
| 3085 |
else {
|
|
| 3086 |
next=refreshNext(_right[head],next); |
|
| 3087 |
// _next[head]=next; |
|
| 3088 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
|
| 3089 |
? next : INVALID; |
|
| 3090 |
return refreshNext(_left[head],head); |
|
| 3091 |
} |
|
| 3092 |
} |
|
| 3093 |
|
|
| 3094 |
void refreshNext() |
|
| 3095 |
{
|
|
| 3096 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
|
| 3097 |
} |
|
| 3098 |
|
|
| 3099 |
public: |
|
| 3100 |
///Constructor |
|
| 3101 |
|
|
| 3102 |
///Constructor. |
|
| 3103 |
/// |
|
| 3104 |
///It builds up the search database, which remains valid until the digraph |
|
| 3105 |
///changes. |
|
| 3106 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
|
|
| 3107 |
|
|
| 3108 |
///Refresh the data structure at a node. |
|
| 3109 |
|
|
| 3110 |
///Build up the search database of node \c n. |
|
| 3111 |
/// |
|
| 3112 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
| 3113 |
///the number of the outgoing arcs of \c n. |
|
| 3114 |
|
|
| 3115 |
void refresh(Node n) |
|
| 3116 |
{
|
|
| 3117 |
ArcLookUp<G>::refresh(n); |
|
| 3118 |
refreshNext(_head[n]); |
|
| 3119 |
} |
|
| 3120 |
|
|
| 3121 |
///Refresh the full data structure. |
|
| 3122 |
|
|
| 3123 |
///Build up the full search database. In fact, it simply calls |
|
| 3124 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
|
| 3125 |
/// |
|
| 3126 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
| 3127 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
| 3128 |
///out-degree of the digraph. |
|
| 3129 |
|
|
| 3130 |
void refresh() |
|
| 3131 |
{
|
|
| 3132 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
|
| 3133 |
} |
|
| 3134 |
|
|
| 3135 |
///Find an arc between two nodes. |
|
| 3136 |
|
|
| 3137 |
///Find an arc between two nodes. |
|
| 3138 |
///\param s The source node |
|
| 3139 |
///\param t The target node |
|
| 3140 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
|
| 3141 |
///not given, the operator finds the first appropriate arc. |
|
| 3142 |
///\return An arc from \c s to \c t after \c prev or |
|
| 3143 |
///\ref INVALID if there is no more. |
|
| 3144 |
/// |
|
| 3145 |
///For example, you can count the number of arcs from \c u to \c v in the |
|
| 3146 |
///following way. |
|
| 3147 |
///\code |
|
| 3148 |
///AllArcLookUp<ListDigraph> ae(g); |
|
| 3149 |
///... |
|
| 3150 |
///int n=0; |
|
| 3151 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
| 3152 |
///\endcode |
|
| 3153 |
/// |
|
| 3154 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where |
|
| 3155 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
| 3156 |
///consecutive arcs are found in constant time. |
|
| 3157 |
/// |
|
| 3158 |
///\warning If you change the digraph, refresh() must be called before using |
|
| 3159 |
///this operator. If you change the outgoing arcs of |
|
| 3160 |
///a single node \c n, then |
|
| 3161 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
| 3162 |
/// |
|
| 3163 |
#ifdef DOXYGEN |
|
| 3164 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
|
|
| 3165 |
#else |
|
| 3166 |
using ArcLookUp<G>::operator() ; |
|
| 3167 |
Arc operator()(Node s, Node t, Arc prev) const |
|
| 3168 |
{
|
|
| 3169 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
|
| 3170 |
} |
|
| 3171 |
#endif |
|
| 3172 |
|
|
| 3173 |
}; |
|
| 3174 |
|
|
| 3175 |
/// @} |
|
| 3176 |
|
|
| 3177 |
} //END OF NAMESPACE LEMON |
|
| 3178 |
|
|
| 3179 |
#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 |
#include "test_tools.h" |
|
| 20 |
//#include <lemon/smart_graph.h> |
|
| 21 |
#include <lemon/list_graph.h> |
|
| 22 |
#include <lemon/bfs.h> |
|
| 23 |
#include <lemon/path.h> |
|
| 24 |
#include<lemon/concepts/digraph.h> |
|
| 25 |
|
|
| 26 |
using namespace lemon; |
|
| 27 |
|
|
| 28 |
const int PET_SIZE =5; |
|
| 29 |
|
|
| 30 |
|
|
| 31 |
void check_Bfs_Compile() |
|
| 32 |
{
|
|
| 33 |
typedef concepts::Digraph Digraph; |
|
| 34 |
|
|
| 35 |
typedef Digraph::Arc Arc; |
|
| 36 |
typedef Digraph::Node Node; |
|
| 37 |
typedef Digraph::ArcIt ArcIt; |
|
| 38 |
typedef Digraph::NodeIt NodeIt; |
|
| 39 |
|
|
| 40 |
typedef Bfs<Digraph> BType; |
|
| 41 |
|
|
| 42 |
Digraph G; |
|
| 43 |
Node n; |
|
| 44 |
Arc e; |
|
| 45 |
int l; |
|
| 46 |
bool b; |
|
| 47 |
BType::DistMap d(G); |
|
| 48 |
BType::PredMap p(G); |
|
| 49 |
// BType::PredNodeMap pn(G); |
|
| 50 |
|
|
| 51 |
BType bfs_test(G); |
|
| 52 |
|
|
| 53 |
bfs_test.run(n); |
|
| 54 |
|
|
| 55 |
l = bfs_test.dist(n); |
|
| 56 |
e = bfs_test.predArc(n); |
|
| 57 |
n = bfs_test.predNode(n); |
|
| 58 |
d = bfs_test.distMap(); |
|
| 59 |
p = bfs_test.predMap(); |
|
| 60 |
// pn = bfs_test.predNodeMap(); |
|
| 61 |
b = bfs_test.reached(n); |
|
| 62 |
|
|
| 63 |
Path<Digraph> pp = bfs_test.path(n); |
|
| 64 |
} |
|
| 65 |
|
|
| 66 |
void check_Bfs_Function_Compile() |
|
| 67 |
{
|
|
| 68 |
typedef int VType; |
|
| 69 |
typedef concepts::Digraph Digraph; |
|
| 70 |
|
|
| 71 |
typedef Digraph::Arc Arc; |
|
| 72 |
typedef Digraph::Node Node; |
|
| 73 |
typedef Digraph::ArcIt ArcIt; |
|
| 74 |
typedef Digraph::NodeIt NodeIt; |
|
| 75 |
typedef concepts::ReadMap<Arc,VType> LengthMap; |
|
| 76 |
|
|
| 77 |
Digraph g; |
|
| 78 |
bfs(g,Node()).run(); |
|
| 79 |
bfs(g).source(Node()).run(); |
|
| 80 |
bfs(g) |
|
| 81 |
.predMap(concepts::WriteMap<Node,Arc>()) |
|
| 82 |
.distMap(concepts::WriteMap<Node,VType>()) |
|
| 83 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
|
| 84 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
| 85 |
.run(Node()); |
|
| 86 |
|
|
| 87 |
} |
|
| 88 |
|
|
| 89 |
int main() |
|
| 90 |
{
|
|
| 91 |
|
|
| 92 |
// typedef SmartDigraph Digraph; |
|
| 93 |
typedef ListDigraph Digraph; |
|
| 94 |
|
|
| 95 |
typedef Digraph::Arc Arc; |
|
| 96 |
typedef Digraph::Node Node; |
|
| 97 |
typedef Digraph::ArcIt ArcIt; |
|
| 98 |
typedef Digraph::NodeIt NodeIt; |
|
| 99 |
typedef Digraph::ArcMap<int> LengthMap; |
|
| 100 |
|
|
| 101 |
Digraph G; |
|
| 102 |
Node s, t; |
|
| 103 |
PetStruct<Digraph> ps = addPetersen(G,PET_SIZE); |
|
| 104 |
|
|
| 105 |
s=ps.outer[2]; |
|
| 106 |
t=ps.inner[0]; |
|
| 107 |
|
|
| 108 |
Bfs<Digraph> bfs_test(G); |
|
| 109 |
bfs_test.run(s); |
|
| 110 |
|
|
| 111 |
check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t)); |
|
| 112 |
|
|
| 113 |
Path<Digraph> p = bfs_test.path(t); |
|
| 114 |
check(p.length()==3,"getPath() found a wrong path."); |
|
| 115 |
check(checkPath(G, p),"path() found a wrong path."); |
|
| 116 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
|
| 117 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
|
| 118 |
|
|
| 119 |
|
|
| 120 |
for(ArcIt e(G); e==INVALID; ++e) {
|
|
| 121 |
Node u=G.source(e); |
|
| 122 |
Node v=G.target(e); |
|
| 123 |
check( !bfs_test.reached(u) || |
|
| 124 |
(bfs_test.dist(v) > bfs_test.dist(u)+1), |
|
| 125 |
"Wrong output."); |
|
| 126 |
} |
|
| 127 |
|
|
| 128 |
for(NodeIt v(G); v==INVALID; ++v) {
|
|
| 129 |
check(bfs_test.reached(v),"Each node should be reached."); |
|
| 130 |
if ( bfs_test.predArc(v)!=INVALID ) {
|
|
| 131 |
Arc e=bfs_test.predArc(v); |
|
| 132 |
Node u=G.source(e); |
|
| 133 |
check(u==bfs_test.predNode(v),"Wrong tree."); |
|
| 134 |
check(bfs_test.dist(v) - bfs_test.dist(u) == 1, |
|
| 135 |
"Wrong distance. Difference: " |
|
| 136 |
<< std::abs(bfs_test.dist(v) - bfs_test.dist(u) |
|
| 137 |
- 1)); |
|
| 138 |
} |
|
| 139 |
} |
|
| 140 |
} |
|
| 141 |
| 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 |
#include "test_tools.h" |
|
| 20 |
// #include <lemon/smart_graph.h> |
|
| 21 |
#include <lemon/list_graph.h> |
|
| 22 |
#include <lemon/dfs.h> |
|
| 23 |
#include <lemon/path.h> |
|
| 24 |
#include <lemon/concepts/digraph.h> |
|
| 25 |
|
|
| 26 |
using namespace lemon; |
|
| 27 |
|
|
| 28 |
const int PET_SIZE =5; |
|
| 29 |
|
|
| 30 |
|
|
| 31 |
void check_Dfs_SmartDigraph_Compile() |
|
| 32 |
{
|
|
| 33 |
typedef concepts::Digraph Digraph; |
|
| 34 |
|
|
| 35 |
typedef Digraph::Arc Arc; |
|
| 36 |
typedef Digraph::Node Node; |
|
| 37 |
typedef Digraph::ArcIt ArcIt; |
|
| 38 |
typedef Digraph::NodeIt NodeIt; |
|
| 39 |
|
|
| 40 |
typedef Dfs<Digraph> DType; |
|
| 41 |
|
|
| 42 |
Digraph G; |
|
| 43 |
Node n; |
|
| 44 |
Arc e; |
|
| 45 |
int l; |
|
| 46 |
bool b; |
|
| 47 |
DType::DistMap d(G); |
|
| 48 |
DType::PredMap p(G); |
|
| 49 |
// DType::PredNodeMap pn(G); |
|
| 50 |
|
|
| 51 |
DType dfs_test(G); |
|
| 52 |
|
|
| 53 |
dfs_test.run(n); |
|
| 54 |
|
|
| 55 |
l = dfs_test.dist(n); |
|
| 56 |
e = dfs_test.predArc(n); |
|
| 57 |
n = dfs_test.predNode(n); |
|
| 58 |
d = dfs_test.distMap(); |
|
| 59 |
p = dfs_test.predMap(); |
|
| 60 |
// pn = dfs_test.predNodeMap(); |
|
| 61 |
b = dfs_test.reached(n); |
|
| 62 |
|
|
| 63 |
Path<Digraph> pp = dfs_test.path(n); |
|
| 64 |
} |
|
| 65 |
|
|
| 66 |
|
|
| 67 |
void check_Dfs_Function_Compile() |
|
| 68 |
{
|
|
| 69 |
typedef int VType; |
|
| 70 |
typedef concepts::Digraph Digraph; |
|
| 71 |
|
|
| 72 |
typedef Digraph::Arc Arc; |
|
| 73 |
typedef Digraph::Node Node; |
|
| 74 |
typedef Digraph::ArcIt ArcIt; |
|
| 75 |
typedef Digraph::NodeIt NodeIt; |
|
| 76 |
typedef concepts::ReadMap<Arc,VType> LengthMap; |
|
| 77 |
|
|
| 78 |
Digraph g; |
|
| 79 |
dfs(g,Node()).run(); |
|
| 80 |
dfs(g).source(Node()).run(); |
|
| 81 |
dfs(g) |
|
| 82 |
.predMap(concepts::WriteMap<Node,Arc>()) |
|
| 83 |
.distMap(concepts::WriteMap<Node,VType>()) |
|
| 84 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
|
| 85 |
.processedMap(concepts::WriteMap<Node,bool>()) |
|
| 86 |
.run(Node()); |
|
| 87 |
|
|
| 88 |
} |
|
| 89 |
|
|
| 90 |
int main() |
|
| 91 |
{
|
|
| 92 |
|
|
| 93 |
// typedef SmartDigraph Digraph; |
|
| 94 |
typedef ListDigraph Digraph; |
|
| 95 |
|
|
| 96 |
typedef Digraph::Arc Arc; |
|
| 97 |
typedef Digraph::Node Node; |
|
| 98 |
typedef Digraph::ArcIt ArcIt; |
|
| 99 |
typedef Digraph::NodeIt NodeIt; |
|
| 100 |
typedef Digraph::ArcMap<int> LengthMap; |
|
| 101 |
|
|
| 102 |
Digraph G; |
|
| 103 |
Node s, t; |
|
| 104 |
PetStruct<Digraph> ps = addPetersen(G,PET_SIZE); |
|
| 105 |
|
|
| 106 |
s=ps.outer[2]; |
|
| 107 |
t=ps.inner[0]; |
|
| 108 |
|
|
| 109 |
Dfs<Digraph> dfs_test(G); |
|
| 110 |
dfs_test.run(s); |
|
| 111 |
|
|
| 112 |
Path<Digraph> p = dfs_test.path(t); |
|
| 113 |
check(p.length()==dfs_test.dist(t),"path() found a wrong path."); |
|
| 114 |
check(checkPath(G, p),"path() found a wrong path."); |
|
| 115 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
|
| 116 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
|
| 117 |
|
|
| 118 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
|
| 119 |
check(dfs_test.reached(v),"Each node should be reached."); |
|
| 120 |
if ( dfs_test.predArc(v)!=INVALID ) {
|
|
| 121 |
Arc e=dfs_test.predArc(v); |
|
| 122 |
Node u=G.source(e); |
|
| 123 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
|
| 124 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
|
| 125 |
"Wrong distance. (" << dfs_test.dist(u) << "->"
|
|
| 126 |
<<dfs_test.dist(v) << ')'); |
|
| 127 |
} |
|
| 128 |
} |
|
| 129 |
} |
|
| 130 |
| 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 |
#include <iostream> |
|
| 20 |
#include <fstream> |
|
| 21 |
#include <string> |
|
| 22 |
#include <vector> |
|
| 23 |
|
|
| 24 |
#include <lemon/concept_check.h> |
|
| 25 |
#include <lemon/concepts/heap.h> |
|
| 26 |
|
|
| 27 |
#include <lemon/list_graph.h> |
|
| 28 |
|
|
| 29 |
#include <lemon/digraph_reader.h> |
|
| 30 |
|
|
| 31 |
#include <lemon/bin_heap.h> |
|
| 32 |
#include <lemon/fib_heap.h> |
|
| 33 |
#include <lemon/radix_heap.h> |
|
| 34 |
#include <lemon/bucket_heap.h> |
|
| 35 |
|
|
| 36 |
#include "test_tools.h" |
|
| 37 |
|
|
| 38 |
#include "heap_test.h" |
|
| 39 |
|
|
| 40 |
#include <lemon/time_measure.h> |
|
| 41 |
|
|
| 42 |
using namespace lemon; |
|
| 43 |
using namespace lemon::concepts; |
|
| 44 |
|
|
| 45 |
|
|
| 46 |
int main() {
|
|
| 47 |
|
|
| 48 |
typedef int Item; |
|
| 49 |
typedef int Prio; |
|
| 50 |
typedef IntIntMap ItemIntMap; |
|
| 51 |
|
|
| 52 |
typedef ListDigraph Digraph; |
|
| 53 |
|
|
| 54 |
typedef Digraph::Arc Arc; |
|
| 55 |
typedef Digraph::Node Node; |
|
| 56 |
typedef Digraph::ArcIt ArcIt; |
|
| 57 |
typedef Digraph::NodeIt NodeIt; |
|
| 58 |
typedef Digraph::ArcMap<int> LengthMap; |
|
| 59 |
|
|
| 60 |
Digraph digraph; |
|
| 61 |
LengthMap length(digraph); |
|
| 62 |
Node start; |
|
| 63 |
|
|
| 64 |
/// \todo create own test digraph |
|
| 65 |
|
|
| 66 |
std::string f_name; |
|
| 67 |
if( getenv("srcdir") )
|
|
| 68 |
f_name = std::string(getenv("srcdir"));
|
|
| 69 |
else f_name = "."; |
|
| 70 |
f_name += "/test/dijkstra_test.lgf"; |
|
| 71 |
|
|
| 72 |
std::ifstream input(f_name.c_str()); |
|
| 73 |
check(input, "Input file '" << f_name << "' not found."); |
|
| 74 |
DigraphReader<Digraph>(input, digraph). |
|
| 75 |
readArcMap("capacity", length).
|
|
| 76 |
readNode("source", start).
|
|
| 77 |
run(); |
|
| 78 |
|
|
| 79 |
{
|
|
| 80 |
std::cerr << "Checking Bin Heap" << std::endl; |
|
| 81 |
|
|
| 82 |
typedef BinHeap<Prio, ItemIntMap> IntHeap; |
|
| 83 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
| 84 |
heapSortTest<IntHeap>(100); |
|
| 85 |
heapIncreaseTest<IntHeap>(100); |
|
| 86 |
|
|
| 87 |
typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap; |
|
| 88 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
| 89 |
Timer timer; |
|
| 90 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
| 91 |
std::cout << timer << std::endl; |
|
| 92 |
} |
|
| 93 |
{
|
|
| 94 |
std::cerr << "Checking Fib Heap" << std::endl; |
|
| 95 |
|
|
| 96 |
typedef FibHeap<Prio, ItemIntMap> IntHeap; |
|
| 97 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
| 98 |
heapSortTest<IntHeap>(100); |
|
| 99 |
heapIncreaseTest<IntHeap>(100); |
|
| 100 |
|
|
| 101 |
typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap; |
|
| 102 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
| 103 |
Timer timer; |
|
| 104 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
| 105 |
std::cout << timer << std::endl; |
|
| 106 |
} |
|
| 107 |
{
|
|
| 108 |
std::cerr << "Checking Radix Heap" << std::endl; |
|
| 109 |
|
|
| 110 |
typedef RadixHeap<ItemIntMap> IntHeap; |
|
| 111 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
| 112 |
heapSortTest<IntHeap>(100); |
|
| 113 |
heapIncreaseTest<IntHeap>(100); |
|
| 114 |
|
|
| 115 |
typedef RadixHeap<Digraph::NodeMap<int> > NodeHeap; |
|
| 116 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
| 117 |
Timer timer; |
|
| 118 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
| 119 |
std::cout << timer << std::endl; |
|
| 120 |
} |
|
| 121 |
|
|
| 122 |
{
|
|
| 123 |
std::cerr << "Checking Bucket Heap" << std::endl; |
|
| 124 |
|
|
| 125 |
typedef BucketHeap<ItemIntMap> IntHeap; |
|
| 126 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
| 127 |
heapSortTest<IntHeap>(100); |
|
| 128 |
heapIncreaseTest<IntHeap>(100); |
|
| 129 |
|
|
| 130 |
typedef BucketHeap<Digraph::NodeMap<int> > NodeHeap; |
|
| 131 |
checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>(); |
|
| 132 |
Timer timer; |
|
| 133 |
dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start); |
|
| 134 |
std::cout << timer << std::endl; |
|
| 135 |
} |
|
| 136 |
|
|
| 137 |
std::cout << __FILE__ ": All tests passed.\n"; |
|
| 138 |
|
|
| 139 |
return 0; |
|
| 140 |
} |
| 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 |
#include <vector> |
|
| 20 |
#include <algorithm> |
|
| 21 |
|
|
| 22 |
#include <lemon/dijkstra.h> |
|
| 23 |
|
|
| 24 |
class IntIntMap : public std::vector<int> {
|
|
| 25 |
public: |
|
| 26 |
typedef std::vector<int> Parent; |
|
| 27 |
|
|
| 28 |
typedef int Key; |
|
| 29 |
typedef int Value; |
|
| 30 |
|
|
| 31 |
IntIntMap() : Parent() {}
|
|
| 32 |
IntIntMap(int n) : Parent(n) {}
|
|
| 33 |
IntIntMap(int n, int v) : Parent(n, v) {}
|
|
| 34 |
|
|
| 35 |
void set(int key, int value) {
|
|
| 36 |
Parent::operator[](key) = value; |
|
| 37 |
} |
|
| 38 |
}; |
|
| 39 |
|
|
| 40 |
|
|
| 41 |
template <typename _Heap> |
|
| 42 |
void heapSortTest(int n) {
|
|
| 43 |
typedef _Heap Heap; |
|
| 44 |
IntIntMap map(n, -1); |
|
| 45 |
|
|
| 46 |
Heap heap(map); |
|
| 47 |
|
|
| 48 |
std::vector<int> v(n); |
|
| 49 |
|
|
| 50 |
for (int i = 0; i < n; ++i) {
|
|
| 51 |
v[i] = rnd[1000]; |
|
| 52 |
heap.push(i, v[i]); |
|
| 53 |
} |
|
| 54 |
std::sort(v.begin(), v.end()); |
|
| 55 |
for (int i = 0; i < n; ++i) {
|
|
| 56 |
check(v[i] == heap.prio() ,"Wrong order in heap sort."); |
|
| 57 |
heap.pop(); |
|
| 58 |
} |
|
| 59 |
} |
|
| 60 |
|
|
| 61 |
template <typename _Heap> |
|
| 62 |
void heapIncreaseTest(int n) {
|
|
| 63 |
typedef _Heap Heap; |
|
| 64 |
IntIntMap map(n, -1); |
|
| 65 |
|
|
| 66 |
Heap heap(map); |
|
| 67 |
|
|
| 68 |
std::vector<int> v(n); |
|
| 69 |
|
|
| 70 |
for (int i = 0; i < n; ++i) {
|
|
| 71 |
v[i] = rnd[1000]; |
|
| 72 |
heap.push(i, v[i]); |
|
| 73 |
} |
|
| 74 |
for (int i = 0; i < n; ++i) {
|
|
| 75 |
v[i] += rnd[1000]; |
|
| 76 |
heap.increase(i, v[i]); |
|
| 77 |
} |
|
| 78 |
std::sort(v.begin(), v.end()); |
|
| 79 |
for (int i = 0; i < n; ++i) {
|
|
| 80 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test."); |
|
| 81 |
heap.pop(); |
|
| 82 |
} |
|
| 83 |
} |
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
template <typename _Digraph, typename _LengthMap, typename _Heap> |
|
| 88 |
void dijkstraHeapTest(_Digraph& digraph, _LengthMap& length, |
|
| 89 |
typename _Digraph::Node& start) {
|
|
| 90 |
|
|
| 91 |
typedef _Heap Heap; |
|
| 92 |
typedef _Digraph Digraph; |
|
| 93 |
typedef _LengthMap LengthMap; |
|
| 94 |
|
|
| 95 |
typedef typename Digraph::Node Node; |
|
| 96 |
typedef typename Digraph::Arc Arc; |
|
| 97 |
typedef typename Digraph::NodeIt NodeIt; |
|
| 98 |
typedef typename Digraph::ArcIt ArcIt; |
|
| 99 |
|
|
| 100 |
typename Dijkstra<Digraph, LengthMap>::template DefStandardHeap<Heap>:: |
|
| 101 |
Create dijkstra(digraph, length); |
|
| 102 |
|
|
| 103 |
dijkstra.run(start); |
|
| 104 |
|
|
| 105 |
for(ArcIt e(digraph); e!=INVALID; ++e) {
|
|
| 106 |
Node u=digraph.source(e); |
|
| 107 |
Node v=digraph.target(e); |
|
| 108 |
if (dijkstra.reached(u)) {
|
|
| 109 |
check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e], |
|
| 110 |
"Error in a shortest path tree arc!"); |
|
| 111 |
} |
|
| 112 |
} |
|
| 113 |
|
|
| 114 |
for(NodeIt v(digraph); v!=INVALID; ++v) {
|
|
| 115 |
if ( dijkstra.reached(v) && dijkstra.predArc(v) != INVALID ) {
|
|
| 116 |
Arc e=dijkstra.predArc(v); |
|
| 117 |
Node u=digraph.source(e); |
|
| 118 |
check( dijkstra.dist(v) - dijkstra .dist(u) == length[e], |
|
| 119 |
"Error in a shortest path tree arc!"); |
|
| 120 |
} |
|
| 121 |
} |
|
| 122 |
|
|
| 123 |
} |
| ... | ... |
@@ -16,7 +16,11 @@ |
| 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 \ |
| ... | ... |
@@ -33,4 +37,5 @@ |
| 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 \ |
| ... | ... |
@@ -41,4 +46,5 @@ |
| 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 \ |
| ... | ... |
@@ -4,8 +4,11 @@ |
| 4 | 4 |
noinst_HEADERS += \ |
| 5 | 5 |
test/digraph_test.h \ |
| 6 |
test/heap_test.h \ |
|
| 6 | 7 |
test/map_test.h \ |
| 7 | 8 |
test/test_tools.h |
| 8 | 9 |
|
| 9 | 10 |
check_PROGRAMS += \ |
| 11 |
test/bfs_test \ |
|
| 12 |
test/dfs_test \ |
|
| 10 | 13 |
test/digraph_test \ |
| 11 | 14 |
test/dim_test \ |
| ... | ... |
@@ -20,8 +23,11 @@ |
| 20 | 23 |
XFAIL_TESTS += test/test_tools_fail$(EXEEXT) |
| 21 | 24 |
|
| 25 |
test_bfs_test_SOURCES = test/bfs_test.cc |
|
| 26 |
test_dfs_test_SOURCES = test/dfs_test.cc |
|
| 22 | 27 |
test_digraph_test_SOURCES = test/digraph_test.cc |
| 23 | 28 |
test_dim_test_SOURCES = test/dim_test.cc |
| 24 | 29 |
#test_error_test_SOURCES = test/error_test.cc |
| 25 | 30 |
test_graph_test_SOURCES = test/graph_test.cc |
| 31 |
# test_heap_test_SOURCES = test/heap_test.cc |
|
| 26 | 32 |
test_maps_test_SOURCES = test/maps_test.cc |
| 27 | 33 |
test_path_test_SOURCES = test/path_test.cc |
| ... | ... |
@@ -21,4 +21,15 @@ |
| 21 | 21 |
|
| 22 | 22 |
#include <iostream> |
| 23 |
#include <vector> |
|
| 24 |
|
|
| 25 |
#include <cstdlib> |
|
| 26 |
#include <ctime> |
|
| 27 |
|
|
| 28 |
#include <lemon/concept_check.h> |
|
| 29 |
#include <lemon/concepts/digraph.h> |
|
| 30 |
|
|
| 31 |
#include <lemon/random.h> |
|
| 32 |
|
|
| 33 |
using namespace lemon; |
|
| 23 | 34 |
|
| 24 | 35 |
//! \ingroup misc |
| ... | ... |
@@ -37,5 +48,5 @@ |
| 37 | 48 |
///\code check(0==1,"This is obviously false.");\endcode will |
| 38 | 49 |
///print this (and then exits). |
| 39 |
///\verbatim |
|
| 50 |
///\verbatim digraph_test.cc:123: error: This is obviously false. \endverbatim |
|
| 40 | 51 |
/// |
| 41 | 52 |
///\todo It should be in \c error.h |
| ... | ... |
@@ -46,3 +57,125 @@ |
| 46 | 57 |
} else { } \
|
| 47 | 58 |
|
| 59 |
///Structure returned by \ref addPetersen(). |
|
| 60 |
|
|
| 61 |
///Structure returned by \ref addPetersen(). |
|
| 62 |
/// |
|
| 63 |
template<class Digraph> struct PetStruct |
|
| 64 |
{
|
|
| 65 |
///Vector containing the outer nodes. |
|
| 66 |
std::vector<typename Digraph::Node> outer; |
|
| 67 |
///Vector containing the inner nodes. |
|
| 68 |
std::vector<typename Digraph::Node> inner; |
|
| 69 |
///Vector containing the arcs of the inner circle. |
|
| 70 |
std::vector<typename Digraph::Arc> incir; |
|
| 71 |
///Vector containing the arcs of the outer circle. |
|
| 72 |
std::vector<typename Digraph::Arc> outcir; |
|
| 73 |
///Vector containing the chord arcs. |
|
| 74 |
std::vector<typename Digraph::Arc> chords; |
|
| 75 |
}; |
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
///Adds a Petersen digraph to \c G. |
|
| 80 |
|
|
| 81 |
///Adds a Petersen digraph to \c G. |
|
| 82 |
///\return The nodes and arcs of the generated digraph. |
|
| 83 |
|
|
| 84 |
template<typename Digraph> |
|
| 85 |
PetStruct<Digraph> addPetersen(Digraph &G,int num = 5) |
|
| 86 |
{
|
|
| 87 |
PetStruct<Digraph> n; |
|
| 88 |
|
|
| 89 |
for(int i=0;i<num;i++) {
|
|
| 90 |
n.outer.push_back(G.addNode()); |
|
| 91 |
n.inner.push_back(G.addNode()); |
|
| 92 |
} |
|
| 93 |
|
|
| 94 |
for(int i=0;i<num;i++) {
|
|
| 95 |
n.chords.push_back(G.addArc(n.outer[i],n.inner[i])); |
|
| 96 |
n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1) % num])); |
|
| 97 |
n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2) % num])); |
|
| 98 |
} |
|
| 99 |
return n; |
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
/// \brief Adds to the digraph the reverse pair of all arcs. |
|
| 103 |
/// |
|
| 104 |
/// Adds to the digraph the reverse pair of all arcs. |
|
| 105 |
/// |
|
| 106 |
template<class Digraph> void bidirDigraph(Digraph &G) |
|
| 107 |
{
|
|
| 108 |
typedef typename Digraph::Arc Arc; |
|
| 109 |
typedef typename Digraph::ArcIt ArcIt; |
|
| 110 |
|
|
| 111 |
std::vector<Arc> ee; |
|
| 112 |
|
|
| 113 |
for(ArcIt e(G);e!=INVALID;++e) ee.push_back(e); |
|
| 114 |
|
|
| 115 |
for(typename std::vector<Arc>::iterator p=ee.begin();p!=ee.end();p++) |
|
| 116 |
G.addArc(G.target(*p),G.source(*p)); |
|
| 117 |
} |
|
| 118 |
|
|
| 119 |
|
|
| 120 |
/// \brief Checks the bidirectioned Petersen digraph. |
|
| 121 |
/// |
|
| 122 |
/// Checks the bidirectioned Petersen digraph. |
|
| 123 |
/// |
|
| 124 |
template<class Digraph> void checkBidirPetersen(Digraph &G, int num = 5) |
|
| 125 |
{
|
|
| 126 |
typedef typename Digraph::Node Node; |
|
| 127 |
|
|
| 128 |
typedef typename Digraph::ArcIt ArcIt; |
|
| 129 |
typedef typename Digraph::NodeIt NodeIt; |
|
| 130 |
|
|
| 131 |
checkDigraphNodeList(G, 2 * num); |
|
| 132 |
checkDigraphArcList(G, 6 * num); |
|
| 133 |
|
|
| 134 |
for(NodeIt n(G);n!=INVALID;++n) {
|
|
| 135 |
checkDigraphInArcList(G, n, 3); |
|
| 136 |
checkDigraphOutArcList(G, n, 3); |
|
| 137 |
} |
|
| 138 |
} |
|
| 139 |
|
|
| 140 |
///Structure returned by \ref addUPetersen(). |
|
| 141 |
|
|
| 142 |
///Structure returned by \ref addUPetersen(). |
|
| 143 |
/// |
|
| 144 |
template<class Digraph> struct UPetStruct |
|
| 145 |
{
|
|
| 146 |
///Vector containing the outer nodes. |
|
| 147 |
std::vector<typename Digraph::Node> outer; |
|
| 148 |
///Vector containing the inner nodes. |
|
| 149 |
std::vector<typename Digraph::Node> inner; |
|
| 150 |
///Vector containing the arcs of the inner circle. |
|
| 151 |
std::vector<typename Digraph::Edge> incir; |
|
| 152 |
///Vector containing the arcs of the outer circle. |
|
| 153 |
std::vector<typename Digraph::Edge> outcir; |
|
| 154 |
///Vector containing the chord arcs. |
|
| 155 |
std::vector<typename Digraph::Edge> chords; |
|
| 156 |
}; |
|
| 157 |
|
|
| 158 |
///Adds a Petersen digraph to the undirected \c G. |
|
| 159 |
|
|
| 160 |
///Adds a Petersen digraph to the undirected \c G. |
|
| 161 |
///\return The nodes and arcs of the generated digraph. |
|
| 162 |
|
|
| 163 |
template<typename Digraph> |
|
| 164 |
UPetStruct<Digraph> addUPetersen(Digraph &G,int num=5) |
|
| 165 |
{
|
|
| 166 |
UPetStruct<Digraph> n; |
|
| 167 |
|
|
| 168 |
for(int i=0;i<num;i++) {
|
|
| 169 |
n.outer.push_back(G.addNode()); |
|
| 170 |
n.inner.push_back(G.addNode()); |
|
| 171 |
} |
|
| 172 |
|
|
| 173 |
for(int i=0;i<num;i++) {
|
|
| 174 |
n.chords.push_back(G.addArc(n.outer[i],n.inner[i])); |
|
| 175 |
n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1)%5])); |
|
| 176 |
n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2)%5])); |
|
| 177 |
} |
|
| 178 |
return n; |
|
| 179 |
} |
|
| 180 |
|
|
| 48 | 181 |
#endif |
0 comments (0 inline)