0
8
0
46
46
28
30
45
45
32
31
13
13
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BFS_H |
| 20 | 20 |
#define LEMON_BFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief BFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Bfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Bfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct BfsDefaultTraits |
| 41 | 41 |
{
|
| 42 | 42 |
///The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef GR Digraph; |
| 44 | 44 |
|
| 45 | 45 |
///\brief The type of the map that stores the predecessor |
| 46 | 46 |
///arcs of the shortest paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the shortest paths. |
| 50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 |
///Instantiates a PredMap. |
|
| 52 |
///Instantiates a \c PredMap. |
|
| 53 | 53 |
|
| 54 |
///This function instantiates a PredMap. |
|
| 54 |
///This function instantiates a \ref PredMap. |
|
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 |
///PredMap. |
|
| 56 |
///\ref PredMap. |
|
| 57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 58 |
{
|
| 59 | 59 |
return new PredMap(g); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 67 |
///Instantiates a ProcessedMap. |
|
| 67 |
///Instantiates a \c ProcessedMap. |
|
| 68 | 68 |
|
| 69 |
///This function instantiates a ProcessedMap. |
|
| 69 |
///This function instantiates a \ref ProcessedMap. |
|
| 70 | 70 |
///\param g is the digraph, to which |
| 71 |
///we would like to define the ProcessedMap |
|
| 71 |
///we would like to define the \ref ProcessedMap |
|
| 72 | 72 |
#ifdef DOXYGEN |
| 73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 74 | 74 |
#else |
| 75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 76 | 76 |
#endif |
| 77 | 77 |
{
|
| 78 | 78 |
return new ProcessedMap(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
///The type of the map that indicates which nodes are reached. |
| 82 | 82 |
|
| 83 | 83 |
///The type of the map that indicates which nodes are reached. |
| 84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 86 |
///Instantiates a ReachedMap. |
|
| 86 |
///Instantiates a \c ReachedMap. |
|
| 87 | 87 |
|
| 88 |
///This function instantiates a ReachedMap. |
|
| 88 |
///This function instantiates a \ref ReachedMap. |
|
| 89 | 89 |
///\param g is the digraph, to which |
| 90 |
///we would like to define the ReachedMap. |
|
| 90 |
///we would like to define the \ref ReachedMap. |
|
| 91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 92 | 92 |
{
|
| 93 | 93 |
return new ReachedMap(g); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
///The type of the map that stores the distances of the nodes. |
| 97 | 97 |
|
| 98 | 98 |
///The type of the map that stores the distances of the nodes. |
| 99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 101 |
///Instantiates a DistMap. |
|
| 101 |
///Instantiates a \c DistMap. |
|
| 102 | 102 |
|
| 103 |
///This function instantiates a DistMap. |
|
| 103 |
///This function instantiates a \ref DistMap. |
|
| 104 | 104 |
///\param g is the digraph, to which we would like to define the |
| 105 |
///DistMap. |
|
| 105 |
///\ref DistMap. |
|
| 106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 107 | 107 |
{
|
| 108 | 108 |
return new DistMap(g); |
| 109 | 109 |
} |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
///%BFS algorithm class. |
| 113 | 113 |
|
| 114 | 114 |
///\ingroup search |
| 115 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
| 116 | 116 |
/// |
| 117 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
| 118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 119 | 119 |
///used easier. |
| 120 | 120 |
/// |
| 121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 122 | 122 |
///The default type is \ref ListDigraph. |
| 123 | 123 |
#ifdef DOXYGEN |
| 124 | 124 |
template <typename GR, |
| 125 | 125 |
typename TR> |
| 126 | 126 |
#else |
| 127 | 127 |
template <typename GR=ListDigraph, |
| 128 | 128 |
typename TR=BfsDefaultTraits<GR> > |
| 129 | 129 |
#endif |
| 130 | 130 |
class Bfs {
|
| 131 | 131 |
public: |
| 132 | 132 |
|
| 133 | 133 |
///The type of the digraph the algorithm runs on. |
| 134 | 134 |
typedef typename TR::Digraph Digraph; |
| 135 | 135 |
|
| 136 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
| 137 | 137 |
///shortest paths. |
| 138 | 138 |
typedef typename TR::PredMap PredMap; |
| 139 | 139 |
///The type of the map that stores the distances of the nodes. |
| 140 | 140 |
typedef typename TR::DistMap DistMap; |
| 141 | 141 |
///The type of the map that indicates which nodes are reached. |
| 142 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
| 143 | 143 |
///The type of the map that indicates which nodes are processed. |
| 144 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 145 | 145 |
///The type of the paths. |
| 146 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 147 | 147 |
|
| 148 | 148 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
| 149 | 149 |
typedef TR Traits; |
| 150 | 150 |
|
| 151 | 151 |
private: |
| 152 | 152 |
|
| 153 | 153 |
typedef typename Digraph::Node Node; |
| 154 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
| 155 | 155 |
typedef typename Digraph::Arc Arc; |
| 156 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 157 | 157 |
|
| 158 | 158 |
//Pointer to the underlying digraph. |
| 159 | 159 |
const Digraph *G; |
| 160 | 160 |
//Pointer to the map of predecessor arcs. |
| 161 | 161 |
PredMap *_pred; |
| 162 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
| 163 | 163 |
bool local_pred; |
| 164 | 164 |
//Pointer to the map of distances. |
| 165 | 165 |
DistMap *_dist; |
| 166 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
| 167 | 167 |
bool local_dist; |
| 168 | 168 |
//Pointer to the map of reached status of the nodes. |
| 169 | 169 |
ReachedMap *_reached; |
| 170 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
| 171 | 171 |
bool local_reached; |
| 172 | 172 |
//Pointer to the map of processed status of the nodes. |
| 173 | 173 |
ProcessedMap *_processed; |
| 174 | 174 |
//Indicates if _processed is locally allocated (true) or not. |
| 175 | 175 |
bool local_processed; |
| 176 | 176 |
|
| 177 | 177 |
std::vector<typename Digraph::Node> _queue; |
| 178 | 178 |
int _queue_head,_queue_tail,_queue_next_dist; |
| 179 | 179 |
int _curr_dist; |
| 180 | 180 |
|
| 181 | 181 |
//Creates the maps if necessary. |
| 182 | 182 |
void create_maps() |
| 183 | 183 |
{
|
| 184 | 184 |
if(!_pred) {
|
| 185 | 185 |
local_pred = true; |
| 186 | 186 |
_pred = Traits::createPredMap(*G); |
| 187 | 187 |
} |
| 188 | 188 |
if(!_dist) {
|
| 189 | 189 |
local_dist = true; |
| 190 | 190 |
_dist = Traits::createDistMap(*G); |
| 191 | 191 |
} |
| 192 | 192 |
if(!_reached) {
|
| 193 | 193 |
local_reached = true; |
| 194 | 194 |
_reached = Traits::createReachedMap(*G); |
| 195 | 195 |
} |
| 196 | 196 |
if(!_processed) {
|
| 197 | 197 |
local_processed = true; |
| 198 | 198 |
_processed = Traits::createProcessedMap(*G); |
| 199 | 199 |
} |
| 200 | 200 |
} |
| 201 | 201 |
|
| 202 | 202 |
protected: |
| 203 | 203 |
|
| 204 | 204 |
Bfs() {}
|
| 205 | 205 |
|
| 206 | 206 |
public: |
| 207 | 207 |
|
| 208 | 208 |
typedef Bfs Create; |
| 209 | 209 |
|
| 210 | 210 |
///\name Named Template Parameters |
| 211 | 211 |
|
| 212 | 212 |
///@{
|
| 213 | 213 |
|
| 214 | 214 |
template <class T> |
| 215 | 215 |
struct SetPredMapTraits : public Traits {
|
| 216 | 216 |
typedef T PredMap; |
| 217 | 217 |
static PredMap *createPredMap(const Digraph &) |
| 218 | 218 |
{
|
| 219 | 219 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 220 | 220 |
return 0; // ignore warnings |
| 221 | 221 |
} |
| 222 | 222 |
}; |
| 223 | 223 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 224 |
///PredMap type. |
|
| 224 |
///\c PredMap type. |
|
| 225 | 225 |
/// |
| 226 | 226 |
///\ref named-templ-param "Named parameter" for setting |
| 227 |
///PredMap type. |
|
| 227 |
///\c PredMap type. |
|
| 228 | 228 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 229 | 229 |
template <class T> |
| 230 | 230 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
|
| 231 | 231 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
| 232 | 232 |
}; |
| 233 | 233 |
|
| 234 | 234 |
template <class T> |
| 235 | 235 |
struct SetDistMapTraits : public Traits {
|
| 236 | 236 |
typedef T DistMap; |
| 237 | 237 |
static DistMap *createDistMap(const Digraph &) |
| 238 | 238 |
{
|
| 239 | 239 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 240 | 240 |
return 0; // ignore warnings |
| 241 | 241 |
} |
| 242 | 242 |
}; |
| 243 | 243 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 244 |
///DistMap type. |
|
| 244 |
///\c DistMap type. |
|
| 245 | 245 |
/// |
| 246 | 246 |
///\ref named-templ-param "Named parameter" for setting |
| 247 |
///DistMap type. |
|
| 247 |
///\c DistMap type. |
|
| 248 | 248 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 249 | 249 |
template <class T> |
| 250 | 250 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
|
| 251 | 251 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
| 252 | 252 |
}; |
| 253 | 253 |
|
| 254 | 254 |
template <class T> |
| 255 | 255 |
struct SetReachedMapTraits : public Traits {
|
| 256 | 256 |
typedef T ReachedMap; |
| 257 | 257 |
static ReachedMap *createReachedMap(const Digraph &) |
| 258 | 258 |
{
|
| 259 | 259 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 260 | 260 |
return 0; // ignore warnings |
| 261 | 261 |
} |
| 262 | 262 |
}; |
| 263 | 263 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 264 |
///ReachedMap type. |
|
| 264 |
///\c ReachedMap type. |
|
| 265 | 265 |
/// |
| 266 | 266 |
///\ref named-templ-param "Named parameter" for setting |
| 267 |
///ReachedMap type. |
|
| 267 |
///\c ReachedMap type. |
|
| 268 | 268 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 269 | 269 |
template <class T> |
| 270 | 270 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
|
| 271 | 271 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
| 272 | 272 |
}; |
| 273 | 273 |
|
| 274 | 274 |
template <class T> |
| 275 | 275 |
struct SetProcessedMapTraits : public Traits {
|
| 276 | 276 |
typedef T ProcessedMap; |
| 277 | 277 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 278 | 278 |
{
|
| 279 | 279 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 280 | 280 |
return 0; // ignore warnings |
| 281 | 281 |
} |
| 282 | 282 |
}; |
| 283 | 283 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 284 |
///ProcessedMap type. |
|
| 284 |
///\c ProcessedMap type. |
|
| 285 | 285 |
/// |
| 286 | 286 |
///\ref named-templ-param "Named parameter" for setting |
| 287 |
///ProcessedMap type. |
|
| 287 |
///\c ProcessedMap type. |
|
| 288 | 288 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 289 | 289 |
template <class T> |
| 290 | 290 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
|
| 291 | 291 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 292 | 292 |
}; |
| 293 | 293 |
|
| 294 | 294 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 295 | 295 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 296 | 296 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 297 | 297 |
{
|
| 298 | 298 |
return new ProcessedMap(g); |
| 299 | 299 |
return 0; // ignore warnings |
| 300 | 300 |
} |
| 301 | 301 |
}; |
| 302 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 303 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 303 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 304 | 304 |
/// |
| 305 | 305 |
///\ref named-templ-param "Named parameter" for setting |
| 306 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 306 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 307 | 307 |
///If you don't set it explicitly, it will be automatically allocated. |
| 308 | 308 |
struct SetStandardProcessedMap : |
| 309 | 309 |
public Bfs< Digraph, SetStandardProcessedMapTraits > {
|
| 310 | 310 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
| 311 | 311 |
}; |
| 312 | 312 |
|
| 313 | 313 |
///@} |
| 314 | 314 |
|
| 315 | 315 |
public: |
| 316 | 316 |
|
| 317 | 317 |
///Constructor. |
| 318 | 318 |
|
| 319 | 319 |
///Constructor. |
| 320 | 320 |
///\param g The digraph the algorithm runs on. |
| 321 | 321 |
Bfs(const Digraph &g) : |
| 322 | 322 |
G(&g), |
| 323 | 323 |
_pred(NULL), local_pred(false), |
| 324 | 324 |
_dist(NULL), local_dist(false), |
| 325 | 325 |
_reached(NULL), local_reached(false), |
| 326 | 326 |
_processed(NULL), local_processed(false) |
| 327 | 327 |
{ }
|
| 328 | 328 |
|
| 329 | 329 |
///Destructor. |
| 330 | 330 |
~Bfs() |
| 331 | 331 |
{
|
| 332 | 332 |
if(local_pred) delete _pred; |
| 333 | 333 |
if(local_dist) delete _dist; |
| 334 | 334 |
if(local_reached) delete _reached; |
| 335 | 335 |
if(local_processed) delete _processed; |
| 336 | 336 |
} |
| 337 | 337 |
|
| 338 | 338 |
///Sets the map that stores the predecessor arcs. |
| 339 | 339 |
|
| 340 | 340 |
///Sets the map that stores the predecessor arcs. |
| 341 | 341 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 342 | 342 |
///or \ref init(), an instance will be allocated automatically. |
| 343 | 343 |
///The destructor deallocates this automatically allocated map, |
| 344 | 344 |
///of course. |
| 345 | 345 |
///\return <tt> (*this) </tt> |
| 346 | 346 |
Bfs &predMap(PredMap &m) |
| 347 | 347 |
{
|
| 348 | 348 |
if(local_pred) {
|
| 349 | 349 |
delete _pred; |
| 350 | 350 |
local_pred=false; |
| 351 | 351 |
} |
| 352 | 352 |
_pred = &m; |
| 353 | 353 |
return *this; |
| 354 | 354 |
} |
| 355 | 355 |
|
| 356 | 356 |
///Sets the map that indicates which nodes are reached. |
| 357 | 357 |
|
| 358 | 358 |
///Sets the map that indicates which nodes are reached. |
| 359 | 359 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 360 | 360 |
///or \ref init(), an instance will be allocated automatically. |
| 361 | 361 |
///The destructor deallocates this automatically allocated map, |
| 362 | 362 |
///of course. |
| 363 | 363 |
///\return <tt> (*this) </tt> |
| 364 | 364 |
Bfs &reachedMap(ReachedMap &m) |
| 365 | 365 |
{
|
| 366 | 366 |
if(local_reached) {
|
| 367 | 367 |
delete _reached; |
| 368 | 368 |
local_reached=false; |
| 369 | 369 |
} |
| 370 | 370 |
_reached = &m; |
| 371 | 371 |
return *this; |
| 372 | 372 |
} |
| 373 | 373 |
|
| 374 | 374 |
///Sets the map that indicates which nodes are processed. |
| 375 | 375 |
|
| 376 | 376 |
///Sets the map that indicates which nodes are processed. |
| 377 | 377 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 378 | 378 |
///or \ref init(), an instance will be allocated automatically. |
| 379 | 379 |
///The destructor deallocates this automatically allocated map, |
| 380 | 380 |
///of course. |
| 381 | 381 |
///\return <tt> (*this) </tt> |
| 382 | 382 |
Bfs &processedMap(ProcessedMap &m) |
| 383 | 383 |
{
|
| 384 | 384 |
if(local_processed) {
|
| 385 | 385 |
delete _processed; |
| 386 | 386 |
local_processed=false; |
| 387 | 387 |
} |
| 388 | 388 |
_processed = &m; |
| 389 | 389 |
return *this; |
| 390 | 390 |
} |
| 391 | 391 |
|
| 392 | 392 |
///Sets the map that stores the distances of the nodes. |
| 393 | 393 |
|
| 394 | 394 |
///Sets the map that stores the distances of the nodes calculated by |
| 395 | 395 |
///the algorithm. |
| 396 | 396 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 397 | 397 |
///or \ref init(), an instance will be allocated automatically. |
| 398 | 398 |
///The destructor deallocates this automatically allocated map, |
| 399 | 399 |
///of course. |
| 400 | 400 |
///\return <tt> (*this) </tt> |
| 401 | 401 |
Bfs &distMap(DistMap &m) |
| 402 | 402 |
{
|
| ... | ... |
@@ -1101,323 +1101,323 @@ |
| 1101 | 1101 |
struct SetDistMapBase : public Base {
|
| 1102 | 1102 |
typedef T DistMap; |
| 1103 | 1103 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1104 | 1104 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1105 | 1105 |
}; |
| 1106 | 1106 |
///\brief \ref named-func-param "Named parameter" |
| 1107 | 1107 |
///for setting DistMap object. |
| 1108 | 1108 |
/// |
| 1109 | 1109 |
/// \ref named-func-param "Named parameter" |
| 1110 | 1110 |
///for setting DistMap object. |
| 1111 | 1111 |
template<class T> |
| 1112 | 1112 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1113 | 1113 |
{
|
| 1114 | 1114 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1115 | 1115 |
return BfsWizard<SetDistMapBase<T> >(*this); |
| 1116 | 1116 |
} |
| 1117 | 1117 |
|
| 1118 | 1118 |
template<class T> |
| 1119 | 1119 |
struct SetProcessedMapBase : public Base {
|
| 1120 | 1120 |
typedef T ProcessedMap; |
| 1121 | 1121 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1122 | 1122 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1123 | 1123 |
}; |
| 1124 | 1124 |
///\brief \ref named-func-param "Named parameter" |
| 1125 | 1125 |
///for setting ProcessedMap object. |
| 1126 | 1126 |
/// |
| 1127 | 1127 |
/// \ref named-func-param "Named parameter" |
| 1128 | 1128 |
///for setting ProcessedMap object. |
| 1129 | 1129 |
template<class T> |
| 1130 | 1130 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1131 | 1131 |
{
|
| 1132 | 1132 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1133 | 1133 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
| 1134 | 1134 |
} |
| 1135 | 1135 |
|
| 1136 | 1136 |
template<class T> |
| 1137 | 1137 |
struct SetPathBase : public Base {
|
| 1138 | 1138 |
typedef T Path; |
| 1139 | 1139 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1140 | 1140 |
}; |
| 1141 | 1141 |
///\brief \ref named-func-param "Named parameter" |
| 1142 | 1142 |
///for getting the shortest path to the target node. |
| 1143 | 1143 |
/// |
| 1144 | 1144 |
///\ref named-func-param "Named parameter" |
| 1145 | 1145 |
///for getting the shortest path to the target node. |
| 1146 | 1146 |
template<class T> |
| 1147 | 1147 |
BfsWizard<SetPathBase<T> > path(const T &t) |
| 1148 | 1148 |
{
|
| 1149 | 1149 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1150 | 1150 |
return BfsWizard<SetPathBase<T> >(*this); |
| 1151 | 1151 |
} |
| 1152 | 1152 |
|
| 1153 | 1153 |
///\brief \ref named-func-param "Named parameter" |
| 1154 | 1154 |
///for getting the distance of the target node. |
| 1155 | 1155 |
/// |
| 1156 | 1156 |
///\ref named-func-param "Named parameter" |
| 1157 | 1157 |
///for getting the distance of the target node. |
| 1158 | 1158 |
BfsWizard dist(const int &d) |
| 1159 | 1159 |
{
|
| 1160 | 1160 |
Base::_di=const_cast<int*>(&d); |
| 1161 | 1161 |
return *this; |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 | 1164 |
}; |
| 1165 | 1165 |
|
| 1166 | 1166 |
///Function-type interface for BFS algorithm. |
| 1167 | 1167 |
|
| 1168 | 1168 |
/// \ingroup search |
| 1169 | 1169 |
///Function-type interface for BFS algorithm. |
| 1170 | 1170 |
/// |
| 1171 | 1171 |
///This function also has several \ref named-func-param "named parameters", |
| 1172 | 1172 |
///they are declared as the members of class \ref BfsWizard. |
| 1173 | 1173 |
///The following examples show how to use these parameters. |
| 1174 | 1174 |
///\code |
| 1175 | 1175 |
/// // Compute shortest path from node s to each node |
| 1176 | 1176 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
| 1177 | 1177 |
/// |
| 1178 | 1178 |
/// // Compute shortest path from s to t |
| 1179 | 1179 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
| 1180 | 1180 |
///\endcode |
| 1181 | 1181 |
///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()" |
| 1182 | 1182 |
///to the end of the parameter list. |
| 1183 | 1183 |
///\sa BfsWizard |
| 1184 | 1184 |
///\sa Bfs |
| 1185 | 1185 |
template<class GR> |
| 1186 | 1186 |
BfsWizard<BfsWizardBase<GR> > |
| 1187 | 1187 |
bfs(const GR &digraph) |
| 1188 | 1188 |
{
|
| 1189 | 1189 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
| 1190 | 1190 |
} |
| 1191 | 1191 |
|
| 1192 | 1192 |
#ifdef DOXYGEN |
| 1193 | 1193 |
/// \brief Visitor class for BFS. |
| 1194 | 1194 |
/// |
| 1195 | 1195 |
/// This class defines the interface of the BfsVisit events, and |
| 1196 | 1196 |
/// it could be the base of a real visitor class. |
| 1197 |
template <typename |
|
| 1197 |
template <typename GR> |
|
| 1198 | 1198 |
struct BfsVisitor {
|
| 1199 |
typedef |
|
| 1199 |
typedef GR Digraph; |
|
| 1200 | 1200 |
typedef typename Digraph::Arc Arc; |
| 1201 | 1201 |
typedef typename Digraph::Node Node; |
| 1202 | 1202 |
/// \brief Called for the source node(s) of the BFS. |
| 1203 | 1203 |
/// |
| 1204 | 1204 |
/// This function is called for the source node(s) of the BFS. |
| 1205 | 1205 |
void start(const Node& node) {}
|
| 1206 | 1206 |
/// \brief Called when a node is reached first time. |
| 1207 | 1207 |
/// |
| 1208 | 1208 |
/// This function is called when a node is reached first time. |
| 1209 | 1209 |
void reach(const Node& node) {}
|
| 1210 | 1210 |
/// \brief Called when a node is processed. |
| 1211 | 1211 |
/// |
| 1212 | 1212 |
/// This function is called when a node is processed. |
| 1213 | 1213 |
void process(const Node& node) {}
|
| 1214 | 1214 |
/// \brief Called when an arc reaches a new node. |
| 1215 | 1215 |
/// |
| 1216 | 1216 |
/// This function is called when the BFS finds an arc whose target node |
| 1217 | 1217 |
/// is not reached yet. |
| 1218 | 1218 |
void discover(const Arc& arc) {}
|
| 1219 | 1219 |
/// \brief Called when an arc is examined but its target node is |
| 1220 | 1220 |
/// already discovered. |
| 1221 | 1221 |
/// |
| 1222 | 1222 |
/// This function is called when an arc is examined but its target node is |
| 1223 | 1223 |
/// already discovered. |
| 1224 | 1224 |
void examine(const Arc& arc) {}
|
| 1225 | 1225 |
}; |
| 1226 | 1226 |
#else |
| 1227 |
template <typename |
|
| 1227 |
template <typename GR> |
|
| 1228 | 1228 |
struct BfsVisitor {
|
| 1229 |
typedef |
|
| 1229 |
typedef GR Digraph; |
|
| 1230 | 1230 |
typedef typename Digraph::Arc Arc; |
| 1231 | 1231 |
typedef typename Digraph::Node Node; |
| 1232 | 1232 |
void start(const Node&) {}
|
| 1233 | 1233 |
void reach(const Node&) {}
|
| 1234 | 1234 |
void process(const Node&) {}
|
| 1235 | 1235 |
void discover(const Arc&) {}
|
| 1236 | 1236 |
void examine(const Arc&) {}
|
| 1237 | 1237 |
|
| 1238 | 1238 |
template <typename _Visitor> |
| 1239 | 1239 |
struct Constraints {
|
| 1240 | 1240 |
void constraints() {
|
| 1241 | 1241 |
Arc arc; |
| 1242 | 1242 |
Node node; |
| 1243 | 1243 |
visitor.start(node); |
| 1244 | 1244 |
visitor.reach(node); |
| 1245 | 1245 |
visitor.process(node); |
| 1246 | 1246 |
visitor.discover(arc); |
| 1247 | 1247 |
visitor.examine(arc); |
| 1248 | 1248 |
} |
| 1249 | 1249 |
_Visitor& visitor; |
| 1250 | 1250 |
}; |
| 1251 | 1251 |
}; |
| 1252 | 1252 |
#endif |
| 1253 | 1253 |
|
| 1254 | 1254 |
/// \brief Default traits class of BfsVisit class. |
| 1255 | 1255 |
/// |
| 1256 | 1256 |
/// Default traits class of BfsVisit class. |
| 1257 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
| 1258 |
template<class _Digraph> |
|
| 1257 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
| 1258 |
template<class GR> |
|
| 1259 | 1259 |
struct BfsVisitDefaultTraits {
|
| 1260 | 1260 |
|
| 1261 | 1261 |
/// \brief The type of the digraph the algorithm runs on. |
| 1262 |
typedef |
|
| 1262 |
typedef GR Digraph; |
|
| 1263 | 1263 |
|
| 1264 | 1264 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1265 | 1265 |
/// |
| 1266 | 1266 |
/// The type of the map that indicates which nodes are reached. |
| 1267 | 1267 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1268 | 1268 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1269 | 1269 |
|
| 1270 | 1270 |
/// \brief Instantiates a ReachedMap. |
| 1271 | 1271 |
/// |
| 1272 | 1272 |
/// This function instantiates a ReachedMap. |
| 1273 | 1273 |
/// \param digraph is the digraph, to which |
| 1274 | 1274 |
/// we would like to define the ReachedMap. |
| 1275 | 1275 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1276 | 1276 |
return new ReachedMap(digraph); |
| 1277 | 1277 |
} |
| 1278 | 1278 |
|
| 1279 | 1279 |
}; |
| 1280 | 1280 |
|
| 1281 | 1281 |
/// \ingroup search |
| 1282 | 1282 |
/// |
| 1283 |
/// \brief |
|
| 1283 |
/// \brief BFS algorithm class with visitor interface. |
|
| 1284 | 1284 |
/// |
| 1285 |
/// This class provides an efficient implementation of the |
|
| 1285 |
/// This class provides an efficient implementation of the BFS algorithm |
|
| 1286 | 1286 |
/// with visitor interface. |
| 1287 | 1287 |
/// |
| 1288 |
/// The |
|
| 1288 |
/// The BfsVisit class provides an alternative interface to the Bfs |
|
| 1289 | 1289 |
/// class. It works with callback mechanism, the BfsVisit object calls |
| 1290 | 1290 |
/// the member functions of the \c Visitor class on every BFS event. |
| 1291 | 1291 |
/// |
| 1292 | 1292 |
/// This interface of the BFS algorithm should be used in special cases |
| 1293 | 1293 |
/// when extra actions have to be performed in connection with certain |
| 1294 | 1294 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
| 1295 | 1295 |
/// instead. |
| 1296 | 1296 |
/// |
| 1297 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
| 1298 |
/// The default value is |
|
| 1299 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
|
| 1300 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
|
| 1301 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
|
| 1302 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
|
| 1297 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
| 1298 |
/// The default type is \ref ListDigraph. |
|
| 1299 |
/// The value of GR is not used directly by \ref BfsVisit, |
|
| 1300 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
|
| 1301 |
/// \tparam VS The Visitor type that is used by the algorithm. |
|
| 1302 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
|
| 1303 | 1303 |
/// does not observe the BFS events. If you want to observe the BFS |
| 1304 | 1304 |
/// events, you should implement your own visitor class. |
| 1305 |
/// \tparam |
|
| 1305 |
/// \tparam TR Traits class to set various data types used by the |
|
| 1306 | 1306 |
/// algorithm. The default traits class is |
| 1307 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits< |
|
| 1307 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
|
| 1308 | 1308 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
| 1309 | 1309 |
/// a BFS visit traits class. |
| 1310 | 1310 |
#ifdef DOXYGEN |
| 1311 |
template <typename |
|
| 1311 |
template <typename GR, typename VS, typename TR> |
|
| 1312 | 1312 |
#else |
| 1313 |
template <typename _Digraph = ListDigraph, |
|
| 1314 |
typename _Visitor = BfsVisitor<_Digraph>, |
|
| 1315 |
|
|
| 1313 |
template <typename GR = ListDigraph, |
|
| 1314 |
typename VS = BfsVisitor<GR>, |
|
| 1315 |
typename TR = BfsVisitDefaultTraits<GR> > |
|
| 1316 | 1316 |
#endif |
| 1317 | 1317 |
class BfsVisit {
|
| 1318 | 1318 |
public: |
| 1319 | 1319 |
|
| 1320 | 1320 |
///The traits class. |
| 1321 |
typedef |
|
| 1321 |
typedef TR Traits; |
|
| 1322 | 1322 |
|
| 1323 | 1323 |
///The type of the digraph the algorithm runs on. |
| 1324 | 1324 |
typedef typename Traits::Digraph Digraph; |
| 1325 | 1325 |
|
| 1326 | 1326 |
///The visitor type used by the algorithm. |
| 1327 |
typedef |
|
| 1327 |
typedef VS Visitor; |
|
| 1328 | 1328 |
|
| 1329 | 1329 |
///The type of the map that indicates which nodes are reached. |
| 1330 | 1330 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1331 | 1331 |
|
| 1332 | 1332 |
private: |
| 1333 | 1333 |
|
| 1334 | 1334 |
typedef typename Digraph::Node Node; |
| 1335 | 1335 |
typedef typename Digraph::NodeIt NodeIt; |
| 1336 | 1336 |
typedef typename Digraph::Arc Arc; |
| 1337 | 1337 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1338 | 1338 |
|
| 1339 | 1339 |
//Pointer to the underlying digraph. |
| 1340 | 1340 |
const Digraph *_digraph; |
| 1341 | 1341 |
//Pointer to the visitor object. |
| 1342 | 1342 |
Visitor *_visitor; |
| 1343 | 1343 |
//Pointer to the map of reached status of the nodes. |
| 1344 | 1344 |
ReachedMap *_reached; |
| 1345 | 1345 |
//Indicates if _reached is locally allocated (true) or not. |
| 1346 | 1346 |
bool local_reached; |
| 1347 | 1347 |
|
| 1348 | 1348 |
std::vector<typename Digraph::Node> _list; |
| 1349 | 1349 |
int _list_front, _list_back; |
| 1350 | 1350 |
|
| 1351 | 1351 |
//Creates the maps if necessary. |
| 1352 | 1352 |
void create_maps() {
|
| 1353 | 1353 |
if(!_reached) {
|
| 1354 | 1354 |
local_reached = true; |
| 1355 | 1355 |
_reached = Traits::createReachedMap(*_digraph); |
| 1356 | 1356 |
} |
| 1357 | 1357 |
} |
| 1358 | 1358 |
|
| 1359 | 1359 |
protected: |
| 1360 | 1360 |
|
| 1361 | 1361 |
BfsVisit() {}
|
| 1362 | 1362 |
|
| 1363 | 1363 |
public: |
| 1364 | 1364 |
|
| 1365 | 1365 |
typedef BfsVisit Create; |
| 1366 | 1366 |
|
| 1367 | 1367 |
/// \name Named Template Parameters |
| 1368 | 1368 |
|
| 1369 | 1369 |
///@{
|
| 1370 | 1370 |
template <class T> |
| 1371 | 1371 |
struct SetReachedMapTraits : public Traits {
|
| 1372 | 1372 |
typedef T ReachedMap; |
| 1373 | 1373 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1374 | 1374 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1375 | 1375 |
return 0; // ignore warnings |
| 1376 | 1376 |
} |
| 1377 | 1377 |
}; |
| 1378 | 1378 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1379 | 1379 |
/// ReachedMap type. |
| 1380 | 1380 |
/// |
| 1381 | 1381 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1382 | 1382 |
template <class T> |
| 1383 | 1383 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
| 1384 | 1384 |
SetReachedMapTraits<T> > {
|
| 1385 | 1385 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1386 | 1386 |
}; |
| 1387 | 1387 |
///@} |
| 1388 | 1388 |
|
| 1389 | 1389 |
public: |
| 1390 | 1390 |
|
| 1391 | 1391 |
/// \brief Constructor. |
| 1392 | 1392 |
/// |
| 1393 | 1393 |
/// Constructor. |
| 1394 | 1394 |
/// |
| 1395 | 1395 |
/// \param digraph The digraph the algorithm runs on. |
| 1396 | 1396 |
/// \param visitor The visitor object of the algorithm. |
| 1397 | 1397 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1398 | 1398 |
: _digraph(&digraph), _visitor(&visitor), |
| 1399 | 1399 |
_reached(0), local_reached(false) {}
|
| 1400 | 1400 |
|
| 1401 | 1401 |
/// \brief Destructor. |
| 1402 | 1402 |
~BfsVisit() {
|
| 1403 | 1403 |
if(local_reached) delete _reached; |
| 1404 | 1404 |
} |
| 1405 | 1405 |
|
| 1406 | 1406 |
/// \brief Sets the map that indicates which nodes are reached. |
| 1407 | 1407 |
/// |
| 1408 | 1408 |
/// Sets the map that indicates which nodes are reached. |
| 1409 | 1409 |
/// If you don't use this function before calling \ref run(Node) "run()" |
| 1410 | 1410 |
/// or \ref init(), an instance will be allocated automatically. |
| 1411 | 1411 |
/// The destructor deallocates this automatically allocated map, |
| 1412 | 1412 |
/// of course. |
| 1413 | 1413 |
/// \return <tt> (*this) </tt> |
| 1414 | 1414 |
BfsVisit &reachedMap(ReachedMap &m) {
|
| 1415 | 1415 |
if(local_reached) {
|
| 1416 | 1416 |
delete _reached; |
| 1417 | 1417 |
local_reached = false; |
| 1418 | 1418 |
} |
| 1419 | 1419 |
_reached = &m; |
| 1420 | 1420 |
return *this; |
| 1421 | 1421 |
} |
| 1422 | 1422 |
|
| 1423 | 1423 |
public: |
| ... | ... |
@@ -42,193 +42,193 @@ |
| 42 | 42 |
// |
| 43 | 43 |
// The template parameters are the Graph, the current Item type and |
| 44 | 44 |
// the Value type of the map. |
| 45 | 45 |
template <typename _Graph, typename _Item, typename _Value> |
| 46 | 46 |
class ArrayMap |
| 47 | 47 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
| 48 | 48 |
public: |
| 49 | 49 |
// The graph type. |
| 50 | 50 |
typedef _Graph Graph; |
| 51 | 51 |
// The item type. |
| 52 | 52 |
typedef _Item Item; |
| 53 | 53 |
// The reference map tag. |
| 54 | 54 |
typedef True ReferenceMapTag; |
| 55 | 55 |
|
| 56 | 56 |
// The key type of the map. |
| 57 | 57 |
typedef _Item Key; |
| 58 | 58 |
// The value type of the map. |
| 59 | 59 |
typedef _Value Value; |
| 60 | 60 |
|
| 61 | 61 |
// The const reference type of the map. |
| 62 | 62 |
typedef const _Value& ConstReference; |
| 63 | 63 |
// The reference type of the map. |
| 64 | 64 |
typedef _Value& Reference; |
| 65 | 65 |
|
| 66 | 66 |
// The notifier type. |
| 67 | 67 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 68 | 68 |
|
| 69 | 69 |
// The MapBase of the Map which imlements the core regisitry function. |
| 70 | 70 |
typedef typename Notifier::ObserverBase Parent; |
| 71 | 71 |
|
| 72 | 72 |
private: |
| 73 | 73 |
typedef std::allocator<Value> Allocator; |
| 74 | 74 |
|
| 75 | 75 |
public: |
| 76 | 76 |
|
| 77 | 77 |
// \brief Graph initialized map constructor. |
| 78 | 78 |
// |
| 79 | 79 |
// Graph initialized map constructor. |
| 80 | 80 |
explicit ArrayMap(const Graph& graph) {
|
| 81 | 81 |
Parent::attach(graph.notifier(Item())); |
| 82 | 82 |
allocate_memory(); |
| 83 | 83 |
Notifier* nf = Parent::notifier(); |
| 84 | 84 |
Item it; |
| 85 | 85 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 86 | 86 |
int id = nf->id(it);; |
| 87 | 87 |
allocator.construct(&(values[id]), Value()); |
| 88 | 88 |
} |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
// \brief Constructor to use default value to initialize the map. |
| 92 | 92 |
// |
| 93 | 93 |
// It constructs a map and initialize all of the the map. |
| 94 | 94 |
ArrayMap(const Graph& graph, const Value& value) {
|
| 95 | 95 |
Parent::attach(graph.notifier(Item())); |
| 96 | 96 |
allocate_memory(); |
| 97 | 97 |
Notifier* nf = Parent::notifier(); |
| 98 | 98 |
Item it; |
| 99 | 99 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 100 | 100 |
int id = nf->id(it);; |
| 101 | 101 |
allocator.construct(&(values[id]), value); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
private: |
| 106 | 106 |
// \brief Constructor to copy a map of the same map type. |
| 107 | 107 |
// |
| 108 | 108 |
// Constructor to copy a map of the same map type. |
| 109 | 109 |
ArrayMap(const ArrayMap& copy) : Parent() {
|
| 110 | 110 |
if (copy.attached()) {
|
| 111 | 111 |
attach(*copy.notifier()); |
| 112 | 112 |
} |
| 113 | 113 |
capacity = copy.capacity; |
| 114 | 114 |
if (capacity == 0) return; |
| 115 | 115 |
values = allocator.allocate(capacity); |
| 116 | 116 |
Notifier* nf = Parent::notifier(); |
| 117 | 117 |
Item it; |
| 118 | 118 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 119 | 119 |
int id = nf->id(it);; |
| 120 | 120 |
allocator.construct(&(values[id]), copy.values[id]); |
| 121 | 121 |
} |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
// \brief Assign operator. |
| 125 | 125 |
// |
| 126 | 126 |
// This operator assigns for each item in the map the |
| 127 | 127 |
// value mapped to the same item in the copied map. |
| 128 | 128 |
// The parameter map should be indiced with the same |
| 129 | 129 |
// itemset because this assign operator does not change |
| 130 | 130 |
// the container of the map. |
| 131 | 131 |
ArrayMap& operator=(const ArrayMap& cmap) {
|
| 132 | 132 |
return operator=<ArrayMap>(cmap); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
|
| 136 | 136 |
// \brief Template assign operator. |
| 137 | 137 |
// |
| 138 |
// The given parameter should |
|
| 138 |
// The given parameter should conform to the ReadMap |
|
| 139 | 139 |
// concecpt and could be indiced by the current item set of |
| 140 | 140 |
// the NodeMap. In this case the value for each item |
| 141 | 141 |
// is assigned by the value of the given ReadMap. |
| 142 | 142 |
template <typename CMap> |
| 143 | 143 |
ArrayMap& operator=(const CMap& cmap) {
|
| 144 | 144 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 145 | 145 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 146 | 146 |
Item it; |
| 147 | 147 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 148 | 148 |
set(it, cmap[it]); |
| 149 | 149 |
} |
| 150 | 150 |
return *this; |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
public: |
| 154 | 154 |
// \brief The destructor of the map. |
| 155 | 155 |
// |
| 156 | 156 |
// The destructor of the map. |
| 157 | 157 |
virtual ~ArrayMap() {
|
| 158 | 158 |
if (attached()) {
|
| 159 | 159 |
clear(); |
| 160 | 160 |
detach(); |
| 161 | 161 |
} |
| 162 | 162 |
} |
| 163 | 163 |
|
| 164 | 164 |
protected: |
| 165 | 165 |
|
| 166 | 166 |
using Parent::attach; |
| 167 | 167 |
using Parent::detach; |
| 168 | 168 |
using Parent::attached; |
| 169 | 169 |
|
| 170 | 170 |
public: |
| 171 | 171 |
|
| 172 | 172 |
// \brief The subscript operator. |
| 173 | 173 |
// |
| 174 | 174 |
// The subscript operator. The map can be subscripted by the |
| 175 | 175 |
// actual keys of the graph. |
| 176 | 176 |
Value& operator[](const Key& key) {
|
| 177 | 177 |
int id = Parent::notifier()->id(key); |
| 178 | 178 |
return values[id]; |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
// \brief The const subscript operator. |
| 182 | 182 |
// |
| 183 | 183 |
// The const subscript operator. The map can be subscripted by the |
| 184 | 184 |
// actual keys of the graph. |
| 185 | 185 |
const Value& operator[](const Key& key) const {
|
| 186 | 186 |
int id = Parent::notifier()->id(key); |
| 187 | 187 |
return values[id]; |
| 188 | 188 |
} |
| 189 | 189 |
|
| 190 | 190 |
// \brief Setter function of the map. |
| 191 | 191 |
// |
| 192 | 192 |
// Setter function of the map. Equivalent with map[key] = val. |
| 193 | 193 |
// This is a compatibility feature with the not dereferable maps. |
| 194 | 194 |
void set(const Key& key, const Value& val) {
|
| 195 | 195 |
(*this)[key] = val; |
| 196 | 196 |
} |
| 197 | 197 |
|
| 198 | 198 |
protected: |
| 199 | 199 |
|
| 200 | 200 |
// \brief Adds a new key to the map. |
| 201 | 201 |
// |
| 202 | 202 |
// It adds a new key to the map. It is called by the observer notifier |
| 203 | 203 |
// and it overrides the add() member function of the observer base. |
| 204 | 204 |
virtual void add(const Key& key) {
|
| 205 | 205 |
Notifier* nf = Parent::notifier(); |
| 206 | 206 |
int id = nf->id(key); |
| 207 | 207 |
if (id >= capacity) {
|
| 208 | 208 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
| 209 | 209 |
while (new_capacity <= id) {
|
| 210 | 210 |
new_capacity <<= 1; |
| 211 | 211 |
} |
| 212 | 212 |
Value* new_values = allocator.allocate(new_capacity); |
| 213 | 213 |
Item it; |
| 214 | 214 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 215 | 215 |
int jd = nf->id(it);; |
| 216 | 216 |
if (id != jd) {
|
| 217 | 217 |
allocator.construct(&(new_values[jd]), values[jd]); |
| 218 | 218 |
allocator.destroy(&(values[jd])); |
| 219 | 219 |
} |
| 220 | 220 |
} |
| 221 | 221 |
if (capacity != 0) allocator.deallocate(values, capacity); |
| 222 | 222 |
values = new_values; |
| 223 | 223 |
capacity = new_capacity; |
| 224 | 224 |
} |
| 225 | 225 |
allocator.construct(&(values[id]), Value()); |
| 226 | 226 |
} |
| 227 | 227 |
|
| 228 | 228 |
// \brief Adds more new keys to the map. |
| 229 | 229 |
// |
| 230 | 230 |
// It adds more new keys to the map. It is called by the observer notifier |
| 231 | 231 |
// and it overrides the add() member function of the observer base. |
| 232 | 232 |
virtual void add(const std::vector<Key>& keys) {
|
| 233 | 233 |
Notifier* nf = Parent::notifier(); |
| 234 | 234 |
int max_id = -1; |
| ... | ... |
@@ -31,193 +31,193 @@ |
| 31 | 31 |
//\ingroup graphbits |
| 32 | 32 |
// |
| 33 | 33 |
//\file |
| 34 | 34 |
//\brief Vector based graph maps. |
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
// \ingroup graphbits |
| 38 | 38 |
// |
| 39 | 39 |
// \brief Graph map based on the std::vector storage. |
| 40 | 40 |
// |
| 41 | 41 |
// The VectorMap template class is graph map structure that automatically |
| 42 | 42 |
// updates the map when a key is added to or erased from the graph. |
| 43 | 43 |
// This map type uses std::vector to store the values. |
| 44 | 44 |
// |
| 45 | 45 |
// \tparam _Graph The graph this map is attached to. |
| 46 | 46 |
// \tparam _Item The item type of the graph items. |
| 47 | 47 |
// \tparam _Value The value type of the map. |
| 48 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
| 49 | 49 |
class VectorMap |
| 50 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
| 51 | 51 |
private: |
| 52 | 52 |
|
| 53 | 53 |
// The container type of the map. |
| 54 | 54 |
typedef std::vector<_Value> Container; |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
// The graph type of the map. |
| 59 | 59 |
typedef _Graph Graph; |
| 60 | 60 |
// The item type of the map. |
| 61 | 61 |
typedef _Item Item; |
| 62 | 62 |
// The reference map tag. |
| 63 | 63 |
typedef True ReferenceMapTag; |
| 64 | 64 |
|
| 65 | 65 |
// The key type of the map. |
| 66 | 66 |
typedef _Item Key; |
| 67 | 67 |
// The value type of the map. |
| 68 | 68 |
typedef _Value Value; |
| 69 | 69 |
|
| 70 | 70 |
// The notifier type. |
| 71 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 72 | 72 |
|
| 73 | 73 |
// The map type. |
| 74 | 74 |
typedef VectorMap Map; |
| 75 | 75 |
// The base class of the map. |
| 76 | 76 |
typedef typename Notifier::ObserverBase Parent; |
| 77 | 77 |
|
| 78 | 78 |
// The reference type of the map; |
| 79 | 79 |
typedef typename Container::reference Reference; |
| 80 | 80 |
// The const reference type of the map; |
| 81 | 81 |
typedef typename Container::const_reference ConstReference; |
| 82 | 82 |
|
| 83 | 83 |
|
| 84 | 84 |
// \brief Constructor to attach the new map into the notifier. |
| 85 | 85 |
// |
| 86 | 86 |
// It constructs a map and attachs it into the notifier. |
| 87 | 87 |
// It adds all the items of the graph to the map. |
| 88 | 88 |
VectorMap(const Graph& graph) {
|
| 89 | 89 |
Parent::attach(graph.notifier(Item())); |
| 90 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 | 93 |
// \brief Constructor uses given value to initialize the map. |
| 94 | 94 |
// |
| 95 | 95 |
// It constructs a map uses a given value to initialize the map. |
| 96 | 96 |
// It adds all the items of the graph to the map. |
| 97 | 97 |
VectorMap(const Graph& graph, const Value& value) {
|
| 98 | 98 |
Parent::attach(graph.notifier(Item())); |
| 99 | 99 |
container.resize(Parent::notifier()->maxId() + 1, value); |
| 100 | 100 |
} |
| 101 | 101 |
|
| 102 | 102 |
private: |
| 103 | 103 |
// \brief Copy constructor |
| 104 | 104 |
// |
| 105 | 105 |
// Copy constructor. |
| 106 | 106 |
VectorMap(const VectorMap& _copy) : Parent() {
|
| 107 | 107 |
if (_copy.attached()) {
|
| 108 | 108 |
Parent::attach(*_copy.notifier()); |
| 109 | 109 |
container = _copy.container; |
| 110 | 110 |
} |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
// \brief Assign operator. |
| 114 | 114 |
// |
| 115 | 115 |
// This operator assigns for each item in the map the |
| 116 | 116 |
// value mapped to the same item in the copied map. |
| 117 | 117 |
// The parameter map should be indiced with the same |
| 118 | 118 |
// itemset because this assign operator does not change |
| 119 | 119 |
// the container of the map. |
| 120 | 120 |
VectorMap& operator=(const VectorMap& cmap) {
|
| 121 | 121 |
return operator=<VectorMap>(cmap); |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
|
| 125 | 125 |
// \brief Template assign operator. |
| 126 | 126 |
// |
| 127 |
// The given parameter should |
|
| 127 |
// The given parameter should conform to the ReadMap |
|
| 128 | 128 |
// concecpt and could be indiced by the current item set of |
| 129 | 129 |
// the NodeMap. In this case the value for each item |
| 130 | 130 |
// is assigned by the value of the given ReadMap. |
| 131 | 131 |
template <typename CMap> |
| 132 | 132 |
VectorMap& operator=(const CMap& cmap) {
|
| 133 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 134 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 135 | 135 |
Item it; |
| 136 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 137 | 137 |
set(it, cmap[it]); |
| 138 | 138 |
} |
| 139 | 139 |
return *this; |
| 140 | 140 |
} |
| 141 | 141 |
|
| 142 | 142 |
public: |
| 143 | 143 |
|
| 144 | 144 |
// \brief The subcript operator. |
| 145 | 145 |
// |
| 146 | 146 |
// The subscript operator. The map can be subscripted by the |
| 147 | 147 |
// actual items of the graph. |
| 148 | 148 |
Reference operator[](const Key& key) {
|
| 149 | 149 |
return container[Parent::notifier()->id(key)]; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
// \brief The const subcript operator. |
| 153 | 153 |
// |
| 154 | 154 |
// The const subscript operator. The map can be subscripted by the |
| 155 | 155 |
// actual items of the graph. |
| 156 | 156 |
ConstReference operator[](const Key& key) const {
|
| 157 | 157 |
return container[Parent::notifier()->id(key)]; |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
|
| 161 | 161 |
// \brief The setter function of the map. |
| 162 | 162 |
// |
| 163 | 163 |
// It the same as operator[](key) = value expression. |
| 164 | 164 |
void set(const Key& key, const Value& value) {
|
| 165 | 165 |
(*this)[key] = value; |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
protected: |
| 169 | 169 |
|
| 170 | 170 |
// \brief Adds a new key to the map. |
| 171 | 171 |
// |
| 172 | 172 |
// It adds a new key to the map. It is called by the observer notifier |
| 173 | 173 |
// and it overrides the add() member function of the observer base. |
| 174 | 174 |
virtual void add(const Key& key) {
|
| 175 | 175 |
int id = Parent::notifier()->id(key); |
| 176 | 176 |
if (id >= int(container.size())) {
|
| 177 | 177 |
container.resize(id + 1); |
| 178 | 178 |
} |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
// \brief Adds more new keys to the map. |
| 182 | 182 |
// |
| 183 | 183 |
// It adds more new keys to the map. It is called by the observer notifier |
| 184 | 184 |
// and it overrides the add() member function of the observer base. |
| 185 | 185 |
virtual void add(const std::vector<Key>& keys) {
|
| 186 | 186 |
int max = container.size() - 1; |
| 187 | 187 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 188 | 188 |
int id = Parent::notifier()->id(keys[i]); |
| 189 | 189 |
if (id >= max) {
|
| 190 | 190 |
max = id; |
| 191 | 191 |
} |
| 192 | 192 |
} |
| 193 | 193 |
container.resize(max + 1); |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
// \brief Erase a key from the map. |
| 197 | 197 |
// |
| 198 | 198 |
// Erase a key from the map. It is called by the observer notifier |
| 199 | 199 |
// and it overrides the erase() member function of the observer base. |
| 200 | 200 |
virtual void erase(const Key& key) {
|
| 201 | 201 |
container[Parent::notifier()->id(key)] = Value(); |
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 | 204 |
// \brief Erase more keys from the map. |
| 205 | 205 |
// |
| 206 | 206 |
// It erases more keys from the map. It is called by the observer notifier |
| 207 | 207 |
// and it overrides the erase() member function of the observer base. |
| 208 | 208 |
virtual void erase(const std::vector<Key>& keys) {
|
| 209 | 209 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 210 | 210 |
container[Parent::notifier()->id(keys[i])] = Value(); |
| 211 | 211 |
} |
| 212 | 212 |
} |
| 213 | 213 |
|
| 214 | 214 |
// \brief Build the map. |
| 215 | 215 |
// |
| 216 | 216 |
// It builds the map. It is called by the observer notifier |
| 217 | 217 |
// and it overrides the build() member function of the observer base. |
| 218 | 218 |
virtual void build() {
|
| 219 | 219 |
int size = Parent::notifier()->maxId() + 1; |
| 220 | 220 |
container.reserve(size); |
| 221 | 221 |
container.resize(size); |
| 222 | 222 |
} |
| 223 | 223 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CIRCULATION_H |
| 20 | 20 |
#define LEMON_CIRCULATION_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup max_flow |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
| 28 | 28 |
/// |
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
/// \brief Default traits class of Circulation class. |
| 32 | 32 |
/// |
| 33 | 33 |
/// Default traits class of Circulation class. |
| 34 |
/// \tparam _Diraph Digraph type. |
|
| 35 |
/// \tparam _LCapMap Lower bound capacity map type. |
|
| 36 |
/// \tparam _UCapMap Upper bound capacity map type. |
|
| 37 |
/// \tparam _DeltaMap Delta map type. |
|
| 38 |
template <typename _Diraph, typename _LCapMap, |
|
| 39 |
typename _UCapMap, typename _DeltaMap> |
|
| 34 |
/// \tparam GR Digraph type. |
|
| 35 |
/// \tparam LM Lower bound capacity map type. |
|
| 36 |
/// \tparam UM Upper bound capacity map type. |
|
| 37 |
/// \tparam DM Delta map type. |
|
| 38 |
template <typename GR, typename LM, |
|
| 39 |
typename UM, typename DM> |
|
| 40 | 40 |
struct CirculationDefaultTraits {
|
| 41 | 41 |
|
| 42 | 42 |
/// \brief The type of the digraph the algorithm runs on. |
| 43 |
typedef |
|
| 43 |
typedef GR Digraph; |
|
| 44 | 44 |
|
| 45 | 45 |
/// \brief The type of the map that stores the circulation lower |
| 46 | 46 |
/// bound. |
| 47 | 47 |
/// |
| 48 | 48 |
/// The type of the map that stores the circulation lower bound. |
| 49 | 49 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 50 |
typedef |
|
| 50 |
typedef LM LCapMap; |
|
| 51 | 51 |
|
| 52 | 52 |
/// \brief The type of the map that stores the circulation upper |
| 53 | 53 |
/// bound. |
| 54 | 54 |
/// |
| 55 | 55 |
/// The type of the map that stores the circulation upper bound. |
| 56 | 56 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 57 |
typedef |
|
| 57 |
typedef UM UCapMap; |
|
| 58 | 58 |
|
| 59 | 59 |
/// \brief The type of the map that stores the lower bound for |
| 60 | 60 |
/// the supply of the nodes. |
| 61 | 61 |
/// |
| 62 | 62 |
/// The type of the map that stores the lower bound for the supply |
| 63 | 63 |
/// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap" |
| 64 | 64 |
/// concept. |
| 65 |
typedef |
|
| 65 |
typedef DM DeltaMap; |
|
| 66 | 66 |
|
| 67 | 67 |
/// \brief The type of the flow values. |
| 68 | 68 |
typedef typename DeltaMap::Value Value; |
| 69 | 69 |
|
| 70 | 70 |
/// \brief The type of the map that stores the flow values. |
| 71 | 71 |
/// |
| 72 | 72 |
/// The type of the map that stores the flow values. |
| 73 | 73 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 74 | 74 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 75 | 75 |
|
| 76 | 76 |
/// \brief Instantiates a FlowMap. |
| 77 | 77 |
/// |
| 78 | 78 |
/// This function instantiates a \ref FlowMap. |
| 79 | 79 |
/// \param digraph The digraph, to which we would like to define |
| 80 | 80 |
/// the flow map. |
| 81 | 81 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 82 | 82 |
return new FlowMap(digraph); |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
/// \brief The elevator type used by the algorithm. |
| 86 | 86 |
/// |
| 87 | 87 |
/// The elevator type used by the algorithm. |
| 88 | 88 |
/// |
| 89 | 89 |
/// \sa Elevator |
| 90 | 90 |
/// \sa LinkedElevator |
| 91 | 91 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
| 92 | 92 |
|
| 93 | 93 |
/// \brief Instantiates an Elevator. |
| 94 | 94 |
/// |
| 95 | 95 |
/// This function instantiates an \ref Elevator. |
| 96 | 96 |
/// \param digraph The digraph, to which we would like to define |
| 97 | 97 |
/// the elevator. |
| 98 | 98 |
/// \param max_level The maximum level of the elevator. |
| 99 | 99 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 100 | 100 |
return new Elevator(digraph, max_level); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
/// \brief The tolerance used by the algorithm |
| 104 | 104 |
/// |
| 105 | 105 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 106 | 106 |
typedef lemon::Tolerance<Value> Tolerance; |
| 107 | 107 |
|
| 108 | 108 |
}; |
| 109 | 109 |
|
| 110 | 110 |
/** |
| 111 | 111 |
\brief Push-relabel algorithm for the network circulation problem. |
| 112 | 112 |
|
| 113 | 113 |
\ingroup max_flow |
| 114 | 114 |
This class implements a push-relabel algorithm for the network |
| 115 | 115 |
circulation problem. |
| 116 | 116 |
It is to find a feasible circulation when lower and upper bounds |
| 117 | 117 |
are given for the flow values on the arcs and lower bounds |
| 118 | 118 |
are given for the supply values of the nodes. |
| 119 | 119 |
|
| 120 | 120 |
The exact formulation of this problem is the following. |
| 121 | 121 |
Let \f$G=(V,A)\f$ be a digraph, |
| 122 | 122 |
\f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$,
|
| 123 | 123 |
\f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation
|
| 124 | 124 |
\f$f: A\rightarrow\mathbf{R}^+_0\f$ so that
|
| 125 | 125 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a)
|
| 126 | 126 |
\geq delta(v) \quad \forall v\in V, \f] |
| 127 | 127 |
\f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f] |
| 128 | 128 |
\note \f$delta(v)\f$ specifies a lower bound for the supply of node |
| 129 | 129 |
\f$v\f$. It can be either positive or negative, however note that |
| 130 | 130 |
\f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to
|
| 131 | 131 |
have a feasible solution. |
| 132 | 132 |
|
| 133 | 133 |
\note A special case of this problem is when |
| 134 | 134 |
\f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$
|
| 135 | 135 |
will be \e equal \e to \f$delta(v)\f$, if a circulation can be found. |
| 136 | 136 |
Thus a feasible solution for the |
| 137 | 137 |
\ref min_cost_flow "minimum cost flow" problem can be calculated |
| 138 | 138 |
in this way. |
| 139 | 139 |
|
| 140 |
\tparam _Digraph The type of the digraph the algorithm runs on. |
|
| 141 |
\tparam _LCapMap The type of the lower bound capacity map. The default |
|
| 142 |
map type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>". |
|
| 143 |
\tparam _UCapMap The type of the upper bound capacity map. The default |
|
| 144 |
map type is \c _LCapMap. |
|
| 145 |
\tparam _DeltaMap The type of the map that stores the lower bound |
|
| 140 |
\tparam GR The type of the digraph the algorithm runs on. |
|
| 141 |
\tparam LM The type of the lower bound capacity map. The default |
|
| 142 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
| 143 |
\tparam UM The type of the upper bound capacity map. The default |
|
| 144 |
map type is \c LM. |
|
| 145 |
\tparam DM The type of the map that stores the lower bound |
|
| 146 | 146 |
for the supply of the nodes. The default map type is |
| 147 |
\ |
|
| 147 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
|
| 148 | 148 |
*/ |
| 149 | 149 |
#ifdef DOXYGEN |
| 150 |
template< typename _Digraph, |
|
| 151 |
typename _LCapMap, |
|
| 152 |
typename _UCapMap, |
|
| 153 |
typename _DeltaMap, |
|
| 154 |
|
|
| 150 |
template< typename GR, |
|
| 151 |
typename LM, |
|
| 152 |
typename UM, |
|
| 153 |
typename DM, |
|
| 154 |
typename TR > |
|
| 155 | 155 |
#else |
| 156 |
template< typename _Digraph, |
|
| 157 |
typename _LCapMap = typename _Digraph::template ArcMap<int>, |
|
| 158 |
typename _UCapMap = _LCapMap, |
|
| 159 |
typename _DeltaMap = typename _Digraph:: |
|
| 160 |
template NodeMap<typename _UCapMap::Value>, |
|
| 161 |
typename _Traits=CirculationDefaultTraits<_Digraph, _LCapMap, |
|
| 162 |
|
|
| 156 |
template< typename GR, |
|
| 157 |
typename LM = typename GR::template ArcMap<int>, |
|
| 158 |
typename UM = LM, |
|
| 159 |
typename DM = typename GR::template NodeMap<typename UM::Value>, |
|
| 160 |
typename TR = CirculationDefaultTraits<GR, LM, UM, DM> > |
|
| 163 | 161 |
#endif |
| 164 | 162 |
class Circulation {
|
| 165 | 163 |
public: |
| 166 | 164 |
|
| 167 | 165 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
| 168 |
typedef |
|
| 166 |
typedef TR Traits; |
|
| 169 | 167 |
///The type of the digraph the algorithm runs on. |
| 170 | 168 |
typedef typename Traits::Digraph Digraph; |
| 171 | 169 |
///The type of the flow values. |
| 172 | 170 |
typedef typename Traits::Value Value; |
| 173 | 171 |
|
| 174 | 172 |
/// The type of the lower bound capacity map. |
| 175 | 173 |
typedef typename Traits::LCapMap LCapMap; |
| 176 | 174 |
/// The type of the upper bound capacity map. |
| 177 | 175 |
typedef typename Traits::UCapMap UCapMap; |
| 178 | 176 |
/// \brief The type of the map that stores the lower bound for |
| 179 | 177 |
/// the supply of the nodes. |
| 180 | 178 |
typedef typename Traits::DeltaMap DeltaMap; |
| 181 | 179 |
///The type of the flow map. |
| 182 | 180 |
typedef typename Traits::FlowMap FlowMap; |
| 183 | 181 |
|
| 184 | 182 |
///The type of the elevator. |
| 185 | 183 |
typedef typename Traits::Elevator Elevator; |
| 186 | 184 |
///The type of the tolerance. |
| 187 | 185 |
typedef typename Traits::Tolerance Tolerance; |
| 188 | 186 |
|
| 189 | 187 |
private: |
| 190 | 188 |
|
| 191 | 189 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 192 | 190 |
|
| 193 | 191 |
const Digraph &_g; |
| 194 | 192 |
int _node_num; |
| 195 | 193 |
|
| 196 | 194 |
const LCapMap *_lo; |
| 197 | 195 |
const UCapMap *_up; |
| 198 | 196 |
const DeltaMap *_delta; |
| 199 | 197 |
|
| 200 | 198 |
FlowMap *_flow; |
| 201 | 199 |
bool _local_flow; |
| 202 | 200 |
|
| 203 | 201 |
Elevator* _level; |
| 204 | 202 |
bool _local_level; |
| 205 | 203 |
|
| 206 | 204 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 207 | 205 |
ExcessMap* _excess; |
| 208 | 206 |
|
| 209 | 207 |
Tolerance _tol; |
| 210 | 208 |
int _el; |
| 211 | 209 |
|
| 212 | 210 |
public: |
| 213 | 211 |
|
| 214 | 212 |
typedef Circulation Create; |
| 215 | 213 |
|
| 216 | 214 |
///\name Named Template Parameters |
| 217 | 215 |
|
| 218 | 216 |
///@{
|
| 219 | 217 |
|
| 220 | 218 |
template <typename _FlowMap> |
| 221 | 219 |
struct SetFlowMapTraits : public Traits {
|
| 222 | 220 |
typedef _FlowMap FlowMap; |
| 223 | 221 |
static FlowMap *createFlowMap(const Digraph&) {
|
| 224 | 222 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
| 225 | 223 |
return 0; // ignore warnings |
| 226 | 224 |
} |
| 227 | 225 |
}; |
| 228 | 226 |
|
| 229 | 227 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 230 | 228 |
/// FlowMap type |
| 231 | 229 |
/// |
| 232 | 230 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
| 233 | 231 |
/// type. |
| 234 | 232 |
template <typename _FlowMap> |
| 235 | 233 |
struct SetFlowMap |
| 236 | 234 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
| 237 | 235 |
SetFlowMapTraits<_FlowMap> > {
|
| 238 | 236 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
| 239 | 237 |
SetFlowMapTraits<_FlowMap> > Create; |
| 240 | 238 |
}; |
| 241 | 239 |
|
| 242 | 240 |
template <typename _Elevator> |
| 243 | 241 |
struct SetElevatorTraits : public Traits {
|
| 244 | 242 |
typedef _Elevator Elevator; |
| 245 | 243 |
static Elevator *createElevator(const Digraph&, int) {
|
| 246 | 244 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
| 247 | 245 |
return 0; // ignore warnings |
| 248 | 246 |
} |
| 249 | 247 |
}; |
| 250 | 248 |
|
| 251 | 249 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 252 | 250 |
/// Elevator type |
| 253 | 251 |
/// |
| 254 | 252 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
| 255 | 253 |
/// type. If this named parameter is used, then an external |
| 256 | 254 |
/// elevator object must be passed to the algorithm using the |
| 257 | 255 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
| 258 | 256 |
/// \ref run() or \ref init(). |
| 259 | 257 |
/// \sa SetStandardElevator |
| 260 | 258 |
template <typename _Elevator> |
| 261 | 259 |
struct SetElevator |
| 262 | 260 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
| 263 | 261 |
SetElevatorTraits<_Elevator> > {
|
| 264 | 262 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
| ... | ... |
@@ -21,451 +21,451 @@ |
| 21 | 21 |
///\brief The concept of graph components. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
| 25 | 25 |
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/concepts/maps.h> |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/bits/alteration_notifier.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
namespace concepts {
|
| 34 | 34 |
|
| 35 | 35 |
/// \brief Skeleton class for graph Node and Arc types |
| 36 | 36 |
/// |
| 37 | 37 |
/// This class describes the interface of Node and Arc (and Edge |
| 38 | 38 |
/// in undirected graphs) subtypes of graph types. |
| 39 | 39 |
/// |
| 40 | 40 |
/// \note This class is a template class so that we can use it to |
| 41 | 41 |
/// create graph skeleton classes. The reason for this is than Node |
| 42 | 42 |
/// and Arc types should \em not derive from the same base class. |
| 43 | 43 |
/// For Node you should instantiate it with character 'n' and for Arc |
| 44 | 44 |
/// with 'a'. |
| 45 | 45 |
|
| 46 | 46 |
#ifndef DOXYGEN |
| 47 | 47 |
template <char _selector = '0'> |
| 48 | 48 |
#endif |
| 49 | 49 |
class GraphItem {
|
| 50 | 50 |
public: |
| 51 | 51 |
/// \brief Default constructor. |
| 52 | 52 |
/// |
| 53 | 53 |
/// \warning The default constructor is not required to set |
| 54 | 54 |
/// the item to some well-defined value. So you should consider it |
| 55 | 55 |
/// as uninitialized. |
| 56 | 56 |
GraphItem() {}
|
| 57 | 57 |
/// \brief Copy constructor. |
| 58 | 58 |
/// |
| 59 | 59 |
/// Copy constructor. |
| 60 | 60 |
/// |
| 61 | 61 |
GraphItem(const GraphItem &) {}
|
| 62 | 62 |
/// \brief Invalid constructor \& conversion. |
| 63 | 63 |
/// |
| 64 | 64 |
/// This constructor initializes the item to be invalid. |
| 65 | 65 |
/// \sa Invalid for more details. |
| 66 | 66 |
GraphItem(Invalid) {}
|
| 67 | 67 |
/// \brief Assign operator for nodes. |
| 68 | 68 |
/// |
| 69 | 69 |
/// The nodes are assignable. |
| 70 | 70 |
/// |
| 71 | 71 |
GraphItem& operator=(GraphItem const&) { return *this; }
|
| 72 | 72 |
/// \brief Equality operator. |
| 73 | 73 |
/// |
| 74 | 74 |
/// Two iterators are equal if and only if they represents the |
| 75 | 75 |
/// same node in the graph or both are invalid. |
| 76 | 76 |
bool operator==(GraphItem) const { return false; }
|
| 77 | 77 |
/// \brief Inequality operator. |
| 78 | 78 |
/// |
| 79 | 79 |
/// \sa operator==(const Node& n) |
| 80 | 80 |
/// |
| 81 | 81 |
bool operator!=(GraphItem) const { return false; }
|
| 82 | 82 |
|
| 83 | 83 |
/// \brief Artificial ordering operator. |
| 84 | 84 |
/// |
| 85 | 85 |
/// To allow the use of graph descriptors as key type in std::map or |
| 86 | 86 |
/// similar associative container we require this. |
| 87 | 87 |
/// |
| 88 | 88 |
/// \note This operator only have to define some strict ordering of |
| 89 | 89 |
/// the items; this order has nothing to do with the iteration |
| 90 | 90 |
/// ordering of the items. |
| 91 | 91 |
bool operator<(GraphItem) const { return false; }
|
| 92 | 92 |
|
| 93 | 93 |
template<typename _GraphItem> |
| 94 | 94 |
struct Constraints {
|
| 95 | 95 |
void constraints() {
|
| 96 | 96 |
_GraphItem i1; |
| 97 | 97 |
_GraphItem i2 = i1; |
| 98 | 98 |
_GraphItem i3 = INVALID; |
| 99 | 99 |
|
| 100 | 100 |
i1 = i2 = i3; |
| 101 | 101 |
|
| 102 | 102 |
bool b; |
| 103 | 103 |
// b = (ia == ib) && (ia != ib) && (ia < ib); |
| 104 | 104 |
b = (ia == ib) && (ia != ib); |
| 105 | 105 |
b = (ia == INVALID) && (ib != INVALID); |
| 106 | 106 |
b = (ia < ib); |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
const _GraphItem &ia; |
| 110 | 110 |
const _GraphItem &ib; |
| 111 | 111 |
}; |
| 112 | 112 |
}; |
| 113 | 113 |
|
| 114 | 114 |
/// \brief An empty base directed graph class. |
| 115 | 115 |
/// |
| 116 | 116 |
/// This class provides the minimal set of features needed for a |
| 117 |
/// directed graph structure. All digraph concepts have to |
|
| 117 |
/// directed graph structure. All digraph concepts have to |
|
| 118 | 118 |
/// conform to this base directed graph. It just provides types |
| 119 | 119 |
/// for nodes and arcs and functions to get the source and the |
| 120 | 120 |
/// target of the arcs. |
| 121 | 121 |
class BaseDigraphComponent {
|
| 122 | 122 |
public: |
| 123 | 123 |
|
| 124 | 124 |
typedef BaseDigraphComponent Digraph; |
| 125 | 125 |
|
| 126 | 126 |
/// \brief Node class of the digraph. |
| 127 | 127 |
/// |
| 128 | 128 |
/// This class represents the Nodes of the digraph. |
| 129 | 129 |
/// |
| 130 | 130 |
typedef GraphItem<'n'> Node; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief Arc class of the digraph. |
| 133 | 133 |
/// |
| 134 | 134 |
/// This class represents the Arcs of the digraph. |
| 135 | 135 |
/// |
| 136 | 136 |
typedef GraphItem<'e'> Arc; |
| 137 | 137 |
|
| 138 | 138 |
/// \brief Gives back the target node of an arc. |
| 139 | 139 |
/// |
| 140 | 140 |
/// Gives back the target node of an arc. |
| 141 | 141 |
/// |
| 142 | 142 |
Node target(const Arc&) const { return INVALID;}
|
| 143 | 143 |
|
| 144 | 144 |
/// \brief Gives back the source node of an arc. |
| 145 | 145 |
/// |
| 146 | 146 |
/// Gives back the source node of an arc. |
| 147 | 147 |
/// |
| 148 | 148 |
Node source(const Arc&) const { return INVALID;}
|
| 149 | 149 |
|
| 150 | 150 |
/// \brief Gives back the opposite node on the given arc. |
| 151 | 151 |
/// |
| 152 | 152 |
/// Gives back the opposite node on the given arc. |
| 153 | 153 |
Node oppositeNode(const Node&, const Arc&) const {
|
| 154 | 154 |
return INVALID; |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
template <typename _Digraph> |
| 158 | 158 |
struct Constraints {
|
| 159 | 159 |
typedef typename _Digraph::Node Node; |
| 160 | 160 |
typedef typename _Digraph::Arc Arc; |
| 161 | 161 |
|
| 162 | 162 |
void constraints() {
|
| 163 | 163 |
checkConcept<GraphItem<'n'>, Node>(); |
| 164 | 164 |
checkConcept<GraphItem<'a'>, Arc>(); |
| 165 | 165 |
{
|
| 166 | 166 |
Node n; |
| 167 | 167 |
Arc e(INVALID); |
| 168 | 168 |
n = digraph.source(e); |
| 169 | 169 |
n = digraph.target(e); |
| 170 | 170 |
n = digraph.oppositeNode(n, e); |
| 171 | 171 |
} |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
const _Digraph& digraph; |
| 175 | 175 |
}; |
| 176 | 176 |
}; |
| 177 | 177 |
|
| 178 | 178 |
/// \brief An empty base undirected graph class. |
| 179 | 179 |
/// |
| 180 | 180 |
/// This class provides the minimal set of features needed for an |
| 181 | 181 |
/// undirected graph structure. All undirected graph concepts have |
| 182 |
/// to |
|
| 182 |
/// to conform to this base graph. It just provides types for |
|
| 183 | 183 |
/// nodes, arcs and edges and functions to get the |
| 184 | 184 |
/// source and the target of the arcs and edges, |
| 185 | 185 |
/// conversion from arcs to edges and function to get |
| 186 | 186 |
/// both direction of the edges. |
| 187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent {
|
| 188 | 188 |
public: |
| 189 | 189 |
typedef BaseDigraphComponent::Node Node; |
| 190 | 190 |
typedef BaseDigraphComponent::Arc Arc; |
| 191 | 191 |
/// \brief Undirected arc class of the graph. |
| 192 | 192 |
/// |
| 193 | 193 |
/// This class represents the edges of the graph. |
| 194 | 194 |
/// The undirected graphs can be used as a directed graph which |
| 195 | 195 |
/// for each arc contains the opposite arc too so the graph is |
| 196 | 196 |
/// bidirected. The edge represents two opposite |
| 197 | 197 |
/// directed arcs. |
| 198 | 198 |
class Edge : public GraphItem<'u'> {
|
| 199 | 199 |
public: |
| 200 | 200 |
typedef GraphItem<'u'> Parent; |
| 201 | 201 |
/// \brief Default constructor. |
| 202 | 202 |
/// |
| 203 | 203 |
/// \warning The default constructor is not required to set |
| 204 | 204 |
/// the item to some well-defined value. So you should consider it |
| 205 | 205 |
/// as uninitialized. |
| 206 | 206 |
Edge() {}
|
| 207 | 207 |
/// \brief Copy constructor. |
| 208 | 208 |
/// |
| 209 | 209 |
/// Copy constructor. |
| 210 | 210 |
/// |
| 211 | 211 |
Edge(const Edge &) : Parent() {}
|
| 212 | 212 |
/// \brief Invalid constructor \& conversion. |
| 213 | 213 |
/// |
| 214 | 214 |
/// This constructor initializes the item to be invalid. |
| 215 | 215 |
/// \sa Invalid for more details. |
| 216 | 216 |
Edge(Invalid) {}
|
| 217 | 217 |
/// \brief Converter from arc to edge. |
| 218 | 218 |
/// |
| 219 | 219 |
/// Besides the core graph item functionality each arc should |
| 220 | 220 |
/// be convertible to the represented edge. |
| 221 | 221 |
Edge(const Arc&) {}
|
| 222 | 222 |
/// \brief Assign arc to edge. |
| 223 | 223 |
/// |
| 224 | 224 |
/// Besides the core graph item functionality each arc should |
| 225 | 225 |
/// be convertible to the represented edge. |
| 226 | 226 |
Edge& operator=(const Arc&) { return *this; }
|
| 227 | 227 |
}; |
| 228 | 228 |
|
| 229 | 229 |
/// \brief Returns the direction of the arc. |
| 230 | 230 |
/// |
| 231 | 231 |
/// Returns the direction of the arc. Each arc represents an |
| 232 | 232 |
/// edge with a direction. It gives back the |
| 233 | 233 |
/// direction. |
| 234 | 234 |
bool direction(const Arc&) const { return true; }
|
| 235 | 235 |
|
| 236 | 236 |
/// \brief Returns the directed arc. |
| 237 | 237 |
/// |
| 238 | 238 |
/// Returns the directed arc from its direction and the |
| 239 | 239 |
/// represented edge. |
| 240 | 240 |
Arc direct(const Edge&, bool) const { return INVALID;}
|
| 241 | 241 |
|
| 242 | 242 |
/// \brief Returns the directed arc. |
| 243 | 243 |
/// |
| 244 | 244 |
/// Returns the directed arc from its source and the |
| 245 | 245 |
/// represented edge. |
| 246 | 246 |
Arc direct(const Edge&, const Node&) const { return INVALID;}
|
| 247 | 247 |
|
| 248 | 248 |
/// \brief Returns the opposite arc. |
| 249 | 249 |
/// |
| 250 | 250 |
/// Returns the opposite arc. It is the arc representing the |
| 251 | 251 |
/// same edge and has opposite direction. |
| 252 | 252 |
Arc oppositeArc(const Arc&) const { return INVALID;}
|
| 253 | 253 |
|
| 254 | 254 |
/// \brief Gives back one ending of an edge. |
| 255 | 255 |
/// |
| 256 | 256 |
/// Gives back one ending of an edge. |
| 257 | 257 |
Node u(const Edge&) const { return INVALID;}
|
| 258 | 258 |
|
| 259 | 259 |
/// \brief Gives back the other ending of an edge. |
| 260 | 260 |
/// |
| 261 | 261 |
/// Gives back the other ending of an edge. |
| 262 | 262 |
Node v(const Edge&) const { return INVALID;}
|
| 263 | 263 |
|
| 264 | 264 |
template <typename _Graph> |
| 265 | 265 |
struct Constraints {
|
| 266 | 266 |
typedef typename _Graph::Node Node; |
| 267 | 267 |
typedef typename _Graph::Arc Arc; |
| 268 | 268 |
typedef typename _Graph::Edge Edge; |
| 269 | 269 |
|
| 270 | 270 |
void constraints() {
|
| 271 | 271 |
checkConcept<BaseDigraphComponent, _Graph>(); |
| 272 | 272 |
checkConcept<GraphItem<'u'>, Edge>(); |
| 273 | 273 |
{
|
| 274 | 274 |
Node n; |
| 275 | 275 |
Edge ue(INVALID); |
| 276 | 276 |
Arc e; |
| 277 | 277 |
n = graph.u(ue); |
| 278 | 278 |
n = graph.v(ue); |
| 279 | 279 |
e = graph.direct(ue, true); |
| 280 | 280 |
e = graph.direct(ue, n); |
| 281 | 281 |
e = graph.oppositeArc(e); |
| 282 | 282 |
ue = e; |
| 283 | 283 |
bool d = graph.direction(e); |
| 284 | 284 |
ignore_unused_variable_warning(d); |
| 285 | 285 |
} |
| 286 | 286 |
} |
| 287 | 287 |
|
| 288 | 288 |
const _Graph& graph; |
| 289 | 289 |
}; |
| 290 | 290 |
|
| 291 | 291 |
}; |
| 292 | 292 |
|
| 293 | 293 |
/// \brief An empty idable base digraph class. |
| 294 | 294 |
/// |
| 295 | 295 |
/// This class provides beside the core digraph features |
| 296 | 296 |
/// core id functions for the digraph structure. |
| 297 |
/// The most of the base digraphs should |
|
| 297 |
/// The most of the base digraphs should conform to this concept. |
|
| 298 | 298 |
/// The id's are unique and immutable. |
| 299 | 299 |
template <typename _Base = BaseDigraphComponent> |
| 300 | 300 |
class IDableDigraphComponent : public _Base {
|
| 301 | 301 |
public: |
| 302 | 302 |
|
| 303 | 303 |
typedef _Base Base; |
| 304 | 304 |
typedef typename Base::Node Node; |
| 305 | 305 |
typedef typename Base::Arc Arc; |
| 306 | 306 |
|
| 307 | 307 |
/// \brief Gives back an unique integer id for the Node. |
| 308 | 308 |
/// |
| 309 | 309 |
/// Gives back an unique integer id for the Node. |
| 310 | 310 |
/// |
| 311 | 311 |
int id(const Node&) const { return -1;}
|
| 312 | 312 |
|
| 313 | 313 |
/// \brief Gives back the node by the unique id. |
| 314 | 314 |
/// |
| 315 | 315 |
/// Gives back the node by the unique id. |
| 316 | 316 |
/// If the digraph does not contain node with the given id |
| 317 | 317 |
/// then the result of the function is undetermined. |
| 318 | 318 |
Node nodeFromId(int) const { return INVALID;}
|
| 319 | 319 |
|
| 320 | 320 |
/// \brief Gives back an unique integer id for the Arc. |
| 321 | 321 |
/// |
| 322 | 322 |
/// Gives back an unique integer id for the Arc. |
| 323 | 323 |
/// |
| 324 | 324 |
int id(const Arc&) const { return -1;}
|
| 325 | 325 |
|
| 326 | 326 |
/// \brief Gives back the arc by the unique id. |
| 327 | 327 |
/// |
| 328 | 328 |
/// Gives back the arc by the unique id. |
| 329 | 329 |
/// If the digraph does not contain arc with the given id |
| 330 | 330 |
/// then the result of the function is undetermined. |
| 331 | 331 |
Arc arcFromId(int) const { return INVALID;}
|
| 332 | 332 |
|
| 333 | 333 |
/// \brief Gives back an integer greater or equal to the maximum |
| 334 | 334 |
/// Node id. |
| 335 | 335 |
/// |
| 336 | 336 |
/// Gives back an integer greater or equal to the maximum Node |
| 337 | 337 |
/// id. |
| 338 | 338 |
int maxNodeId() const { return -1;}
|
| 339 | 339 |
|
| 340 | 340 |
/// \brief Gives back an integer greater or equal to the maximum |
| 341 | 341 |
/// Arc id. |
| 342 | 342 |
/// |
| 343 | 343 |
/// Gives back an integer greater or equal to the maximum Arc |
| 344 | 344 |
/// id. |
| 345 | 345 |
int maxArcId() const { return -1;}
|
| 346 | 346 |
|
| 347 | 347 |
template <typename _Digraph> |
| 348 | 348 |
struct Constraints {
|
| 349 | 349 |
|
| 350 | 350 |
void constraints() {
|
| 351 | 351 |
checkConcept<Base, _Digraph >(); |
| 352 | 352 |
typename _Digraph::Node node; |
| 353 | 353 |
int nid = digraph.id(node); |
| 354 | 354 |
nid = digraph.id(node); |
| 355 | 355 |
node = digraph.nodeFromId(nid); |
| 356 | 356 |
typename _Digraph::Arc arc; |
| 357 | 357 |
int eid = digraph.id(arc); |
| 358 | 358 |
eid = digraph.id(arc); |
| 359 | 359 |
arc = digraph.arcFromId(eid); |
| 360 | 360 |
|
| 361 | 361 |
nid = digraph.maxNodeId(); |
| 362 | 362 |
ignore_unused_variable_warning(nid); |
| 363 | 363 |
eid = digraph.maxArcId(); |
| 364 | 364 |
ignore_unused_variable_warning(eid); |
| 365 | 365 |
} |
| 366 | 366 |
|
| 367 | 367 |
const _Digraph& digraph; |
| 368 | 368 |
}; |
| 369 | 369 |
}; |
| 370 | 370 |
|
| 371 | 371 |
/// \brief An empty idable base undirected graph class. |
| 372 | 372 |
/// |
| 373 | 373 |
/// This class provides beside the core undirected graph features |
| 374 | 374 |
/// core id functions for the undirected graph structure. The |
| 375 |
/// most of the base undirected graphs should |
|
| 375 |
/// most of the base undirected graphs should conform to this |
|
| 376 | 376 |
/// concept. The id's are unique and immutable. |
| 377 | 377 |
template <typename _Base = BaseGraphComponent> |
| 378 | 378 |
class IDableGraphComponent : public IDableDigraphComponent<_Base> {
|
| 379 | 379 |
public: |
| 380 | 380 |
|
| 381 | 381 |
typedef _Base Base; |
| 382 | 382 |
typedef typename Base::Edge Edge; |
| 383 | 383 |
|
| 384 | 384 |
using IDableDigraphComponent<_Base>::id; |
| 385 | 385 |
|
| 386 | 386 |
/// \brief Gives back an unique integer id for the Edge. |
| 387 | 387 |
/// |
| 388 | 388 |
/// Gives back an unique integer id for the Edge. |
| 389 | 389 |
/// |
| 390 | 390 |
int id(const Edge&) const { return -1;}
|
| 391 | 391 |
|
| 392 | 392 |
/// \brief Gives back the edge by the unique id. |
| 393 | 393 |
/// |
| 394 | 394 |
/// Gives back the edge by the unique id. If the |
| 395 | 395 |
/// graph does not contain arc with the given id then the |
| 396 | 396 |
/// result of the function is undetermined. |
| 397 | 397 |
Edge edgeFromId(int) const { return INVALID;}
|
| 398 | 398 |
|
| 399 | 399 |
/// \brief Gives back an integer greater or equal to the maximum |
| 400 | 400 |
/// Edge id. |
| 401 | 401 |
/// |
| 402 | 402 |
/// Gives back an integer greater or equal to the maximum Edge |
| 403 | 403 |
/// id. |
| 404 | 404 |
int maxEdgeId() const { return -1;}
|
| 405 | 405 |
|
| 406 | 406 |
template <typename _Graph> |
| 407 | 407 |
struct Constraints {
|
| 408 | 408 |
|
| 409 | 409 |
void constraints() {
|
| 410 | 410 |
checkConcept<Base, _Graph >(); |
| 411 | 411 |
checkConcept<IDableDigraphComponent<Base>, _Graph >(); |
| 412 | 412 |
typename _Graph::Edge edge; |
| 413 | 413 |
int ueid = graph.id(edge); |
| 414 | 414 |
ueid = graph.id(edge); |
| 415 | 415 |
edge = graph.edgeFromId(ueid); |
| 416 | 416 |
ueid = graph.maxEdgeId(); |
| 417 | 417 |
ignore_unused_variable_warning(ueid); |
| 418 | 418 |
} |
| 419 | 419 |
|
| 420 | 420 |
const _Graph& graph; |
| 421 | 421 |
}; |
| 422 | 422 |
}; |
| 423 | 423 |
|
| 424 | 424 |
/// \brief Skeleton class for graph NodeIt and ArcIt |
| 425 | 425 |
/// |
| 426 | 426 |
/// Skeleton class for graph NodeIt and ArcIt. |
| 427 | 427 |
/// |
| 428 | 428 |
template <typename _Graph, typename _Item> |
| 429 | 429 |
class GraphItemIt : public _Item {
|
| 430 | 430 |
public: |
| 431 | 431 |
/// \brief Default constructor. |
| 432 | 432 |
/// |
| 433 | 433 |
/// @warning The default constructor sets the iterator |
| 434 | 434 |
/// to an undefined value. |
| 435 | 435 |
GraphItemIt() {}
|
| 436 | 436 |
/// \brief Copy constructor. |
| 437 | 437 |
/// |
| 438 | 438 |
/// Copy constructor. |
| 439 | 439 |
/// |
| 440 | 440 |
GraphItemIt(const GraphItemIt& ) {}
|
| 441 | 441 |
/// \brief Sets the iterator to the first item. |
| 442 | 442 |
/// |
| 443 | 443 |
/// Sets the iterator to the first item of \c the graph. |
| 444 | 444 |
/// |
| 445 | 445 |
explicit GraphItemIt(const _Graph&) {}
|
| 446 | 446 |
/// \brief Invalid constructor \& conversion. |
| 447 | 447 |
/// |
| 448 | 448 |
/// This constructor initializes the item to be invalid. |
| 449 | 449 |
/// \sa Invalid for more details. |
| 450 | 450 |
GraphItemIt(Invalid) {}
|
| 451 | 451 |
/// \brief Assign operator for items. |
| 452 | 452 |
/// |
| 453 | 453 |
/// The items are assignable. |
| 454 | 454 |
/// |
| 455 | 455 |
GraphItemIt& operator=(const GraphItemIt&) { return *this; }
|
| 456 | 456 |
/// \brief Next item. |
| 457 | 457 |
/// |
| 458 | 458 |
/// Assign the iterator to the next item. |
| 459 | 459 |
/// |
| 460 | 460 |
GraphItemIt& operator++() { return *this; }
|
| 461 | 461 |
/// \brief Equality operator |
| 462 | 462 |
/// |
| 463 | 463 |
/// Two iterators are equal if and only if they point to the |
| 464 | 464 |
/// same object or both are invalid. |
| 465 | 465 |
bool operator==(const GraphItemIt&) const { return true;}
|
| 466 | 466 |
/// \brief Inequality operator |
| 467 | 467 |
/// |
| 468 | 468 |
/// \sa operator==(Node n) |
| 469 | 469 |
/// |
| 470 | 470 |
bool operator!=(const GraphItemIt&) const { return true;}
|
| 471 | 471 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DFS_H |
| 20 | 20 |
#define LEMON_DFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief DFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Dfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Dfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct DfsDefaultTraits |
| 41 | 41 |
{
|
| 42 | 42 |
///The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef GR Digraph; |
| 44 | 44 |
|
| 45 | 45 |
///\brief The type of the map that stores the predecessor |
| 46 | 46 |
///arcs of the %DFS paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the %DFS paths. |
| 50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 |
///Instantiates a PredMap. |
|
| 52 |
///Instantiates a \c PredMap. |
|
| 53 | 53 |
|
| 54 |
///This function instantiates a PredMap. |
|
| 54 |
///This function instantiates a \ref PredMap. |
|
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 |
///PredMap. |
|
| 56 |
///\ref PredMap. |
|
| 57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 58 |
{
|
| 59 | 59 |
return new PredMap(g); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 67 |
///Instantiates a ProcessedMap. |
|
| 67 |
///Instantiates a \c ProcessedMap. |
|
| 68 | 68 |
|
| 69 |
///This function instantiates a ProcessedMap. |
|
| 69 |
///This function instantiates a \ref ProcessedMap. |
|
| 70 | 70 |
///\param g is the digraph, to which |
| 71 |
///we would like to define the ProcessedMap |
|
| 71 |
///we would like to define the \ref ProcessedMap. |
|
| 72 | 72 |
#ifdef DOXYGEN |
| 73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 74 | 74 |
#else |
| 75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 76 | 76 |
#endif |
| 77 | 77 |
{
|
| 78 | 78 |
return new ProcessedMap(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
///The type of the map that indicates which nodes are reached. |
| 82 | 82 |
|
| 83 | 83 |
///The type of the map that indicates which nodes are reached. |
| 84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 86 |
///Instantiates a ReachedMap. |
|
| 86 |
///Instantiates a \c ReachedMap. |
|
| 87 | 87 |
|
| 88 |
///This function instantiates a ReachedMap. |
|
| 88 |
///This function instantiates a \ref ReachedMap. |
|
| 89 | 89 |
///\param g is the digraph, to which |
| 90 |
///we would like to define the ReachedMap. |
|
| 90 |
///we would like to define the \ref ReachedMap. |
|
| 91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 92 | 92 |
{
|
| 93 | 93 |
return new ReachedMap(g); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
///The type of the map that stores the distances of the nodes. |
| 97 | 97 |
|
| 98 | 98 |
///The type of the map that stores the distances of the nodes. |
| 99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 101 |
///Instantiates a DistMap. |
|
| 101 |
///Instantiates a \c DistMap. |
|
| 102 | 102 |
|
| 103 |
///This function instantiates a DistMap. |
|
| 103 |
///This function instantiates a \ref DistMap. |
|
| 104 | 104 |
///\param g is the digraph, to which we would like to define the |
| 105 |
///DistMap. |
|
| 105 |
///\ref DistMap. |
|
| 106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 107 | 107 |
{
|
| 108 | 108 |
return new DistMap(g); |
| 109 | 109 |
} |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
///%DFS algorithm class. |
| 113 | 113 |
|
| 114 | 114 |
///\ingroup search |
| 115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
| 116 | 116 |
/// |
| 117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
| 118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 119 | 119 |
///used easier. |
| 120 | 120 |
/// |
| 121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 122 | 122 |
///The default type is \ref ListDigraph. |
| 123 | 123 |
#ifdef DOXYGEN |
| 124 | 124 |
template <typename GR, |
| 125 | 125 |
typename TR> |
| 126 | 126 |
#else |
| 127 | 127 |
template <typename GR=ListDigraph, |
| 128 | 128 |
typename TR=DfsDefaultTraits<GR> > |
| 129 | 129 |
#endif |
| 130 | 130 |
class Dfs {
|
| 131 | 131 |
public: |
| 132 | 132 |
|
| 133 | 133 |
///The type of the digraph the algorithm runs on. |
| 134 | 134 |
typedef typename TR::Digraph Digraph; |
| 135 | 135 |
|
| 136 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
| 137 | 137 |
///DFS paths. |
| 138 | 138 |
typedef typename TR::PredMap PredMap; |
| 139 | 139 |
///The type of the map that stores the distances of the nodes. |
| 140 | 140 |
typedef typename TR::DistMap DistMap; |
| 141 | 141 |
///The type of the map that indicates which nodes are reached. |
| 142 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
| 143 | 143 |
///The type of the map that indicates which nodes are processed. |
| 144 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 145 | 145 |
///The type of the paths. |
| 146 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 147 | 147 |
|
| 148 | 148 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
| 149 | 149 |
typedef TR Traits; |
| 150 | 150 |
|
| 151 | 151 |
private: |
| 152 | 152 |
|
| 153 | 153 |
typedef typename Digraph::Node Node; |
| 154 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
| 155 | 155 |
typedef typename Digraph::Arc Arc; |
| 156 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 157 | 157 |
|
| 158 | 158 |
//Pointer to the underlying digraph. |
| 159 | 159 |
const Digraph *G; |
| 160 | 160 |
//Pointer to the map of predecessor arcs. |
| 161 | 161 |
PredMap *_pred; |
| 162 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
| 163 | 163 |
bool local_pred; |
| 164 | 164 |
//Pointer to the map of distances. |
| 165 | 165 |
DistMap *_dist; |
| 166 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
| 167 | 167 |
bool local_dist; |
| 168 | 168 |
//Pointer to the map of reached status of the nodes. |
| 169 | 169 |
ReachedMap *_reached; |
| 170 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
| 171 | 171 |
bool local_reached; |
| 172 | 172 |
//Pointer to the map of processed status of the nodes. |
| 173 | 173 |
ProcessedMap *_processed; |
| 174 | 174 |
//Indicates if _processed is locally allocated (true) or not. |
| 175 | 175 |
bool local_processed; |
| 176 | 176 |
|
| 177 | 177 |
std::vector<typename Digraph::OutArcIt> _stack; |
| 178 | 178 |
int _stack_head; |
| 179 | 179 |
|
| 180 | 180 |
//Creates the maps if necessary. |
| 181 | 181 |
void create_maps() |
| 182 | 182 |
{
|
| 183 | 183 |
if(!_pred) {
|
| 184 | 184 |
local_pred = true; |
| 185 | 185 |
_pred = Traits::createPredMap(*G); |
| 186 | 186 |
} |
| 187 | 187 |
if(!_dist) {
|
| 188 | 188 |
local_dist = true; |
| 189 | 189 |
_dist = Traits::createDistMap(*G); |
| 190 | 190 |
} |
| 191 | 191 |
if(!_reached) {
|
| 192 | 192 |
local_reached = true; |
| 193 | 193 |
_reached = Traits::createReachedMap(*G); |
| 194 | 194 |
} |
| 195 | 195 |
if(!_processed) {
|
| 196 | 196 |
local_processed = true; |
| 197 | 197 |
_processed = Traits::createProcessedMap(*G); |
| 198 | 198 |
} |
| 199 | 199 |
} |
| 200 | 200 |
|
| 201 | 201 |
protected: |
| 202 | 202 |
|
| 203 | 203 |
Dfs() {}
|
| 204 | 204 |
|
| 205 | 205 |
public: |
| 206 | 206 |
|
| 207 | 207 |
typedef Dfs Create; |
| 208 | 208 |
|
| 209 | 209 |
///\name Named template parameters |
| 210 | 210 |
|
| 211 | 211 |
///@{
|
| 212 | 212 |
|
| 213 | 213 |
template <class T> |
| 214 | 214 |
struct SetPredMapTraits : public Traits {
|
| 215 | 215 |
typedef T PredMap; |
| 216 | 216 |
static PredMap *createPredMap(const Digraph &) |
| 217 | 217 |
{
|
| 218 | 218 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 219 | 219 |
return 0; // ignore warnings |
| 220 | 220 |
} |
| 221 | 221 |
}; |
| 222 | 222 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 223 |
///PredMap type. |
|
| 223 |
///\c PredMap type. |
|
| 224 | 224 |
/// |
| 225 | 225 |
///\ref named-templ-param "Named parameter" for setting |
| 226 |
///PredMap type. |
|
| 226 |
///\c PredMap type. |
|
| 227 | 227 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 228 | 228 |
template <class T> |
| 229 | 229 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
|
| 230 | 230 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
| 231 | 231 |
}; |
| 232 | 232 |
|
| 233 | 233 |
template <class T> |
| 234 | 234 |
struct SetDistMapTraits : public Traits {
|
| 235 | 235 |
typedef T DistMap; |
| 236 | 236 |
static DistMap *createDistMap(const Digraph &) |
| 237 | 237 |
{
|
| 238 | 238 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 239 | 239 |
return 0; // ignore warnings |
| 240 | 240 |
} |
| 241 | 241 |
}; |
| 242 | 242 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 243 |
///DistMap type. |
|
| 243 |
///\c DistMap type. |
|
| 244 | 244 |
/// |
| 245 | 245 |
///\ref named-templ-param "Named parameter" for setting |
| 246 |
///DistMap type. |
|
| 246 |
///\c DistMap type. |
|
| 247 | 247 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 248 | 248 |
template <class T> |
| 249 | 249 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
|
| 250 | 250 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
| 251 | 251 |
}; |
| 252 | 252 |
|
| 253 | 253 |
template <class T> |
| 254 | 254 |
struct SetReachedMapTraits : public Traits {
|
| 255 | 255 |
typedef T ReachedMap; |
| 256 | 256 |
static ReachedMap *createReachedMap(const Digraph &) |
| 257 | 257 |
{
|
| 258 | 258 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 259 | 259 |
return 0; // ignore warnings |
| 260 | 260 |
} |
| 261 | 261 |
}; |
| 262 | 262 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 263 |
///ReachedMap type. |
|
| 263 |
///\c ReachedMap type. |
|
| 264 | 264 |
/// |
| 265 | 265 |
///\ref named-templ-param "Named parameter" for setting |
| 266 |
///ReachedMap type. |
|
| 266 |
///\c ReachedMap type. |
|
| 267 | 267 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 268 | 268 |
template <class T> |
| 269 | 269 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
|
| 270 | 270 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
| 271 | 271 |
}; |
| 272 | 272 |
|
| 273 | 273 |
template <class T> |
| 274 | 274 |
struct SetProcessedMapTraits : public Traits {
|
| 275 | 275 |
typedef T ProcessedMap; |
| 276 | 276 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 277 | 277 |
{
|
| 278 | 278 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 279 | 279 |
return 0; // ignore warnings |
| 280 | 280 |
} |
| 281 | 281 |
}; |
| 282 | 282 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 283 |
///ProcessedMap type. |
|
| 283 |
///\c ProcessedMap type. |
|
| 284 | 284 |
/// |
| 285 | 285 |
///\ref named-templ-param "Named parameter" for setting |
| 286 |
///ProcessedMap type. |
|
| 286 |
///\c ProcessedMap type. |
|
| 287 | 287 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 288 | 288 |
template <class T> |
| 289 | 289 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
|
| 290 | 290 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 291 | 291 |
}; |
| 292 | 292 |
|
| 293 | 293 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 294 | 294 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 295 | 295 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 296 | 296 |
{
|
| 297 | 297 |
return new ProcessedMap(g); |
| 298 | 298 |
} |
| 299 | 299 |
}; |
| 300 | 300 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 301 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 301 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 302 | 302 |
/// |
| 303 | 303 |
///\ref named-templ-param "Named parameter" for setting |
| 304 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 304 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 305 | 305 |
///If you don't set it explicitly, it will be automatically allocated. |
| 306 | 306 |
struct SetStandardProcessedMap : |
| 307 | 307 |
public Dfs< Digraph, SetStandardProcessedMapTraits > {
|
| 308 | 308 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
| 309 | 309 |
}; |
| 310 | 310 |
|
| 311 | 311 |
///@} |
| 312 | 312 |
|
| 313 | 313 |
public: |
| 314 | 314 |
|
| 315 | 315 |
///Constructor. |
| 316 | 316 |
|
| 317 | 317 |
///Constructor. |
| 318 | 318 |
///\param g The digraph the algorithm runs on. |
| 319 | 319 |
Dfs(const Digraph &g) : |
| 320 | 320 |
G(&g), |
| 321 | 321 |
_pred(NULL), local_pred(false), |
| 322 | 322 |
_dist(NULL), local_dist(false), |
| 323 | 323 |
_reached(NULL), local_reached(false), |
| 324 | 324 |
_processed(NULL), local_processed(false) |
| 325 | 325 |
{ }
|
| 326 | 326 |
|
| 327 | 327 |
///Destructor. |
| 328 | 328 |
~Dfs() |
| 329 | 329 |
{
|
| 330 | 330 |
if(local_pred) delete _pred; |
| 331 | 331 |
if(local_dist) delete _dist; |
| 332 | 332 |
if(local_reached) delete _reached; |
| 333 | 333 |
if(local_processed) delete _processed; |
| 334 | 334 |
} |
| 335 | 335 |
|
| 336 | 336 |
///Sets the map that stores the predecessor arcs. |
| 337 | 337 |
|
| 338 | 338 |
///Sets the map that stores the predecessor arcs. |
| 339 | 339 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 340 | 340 |
///or \ref init(), an instance will be allocated automatically. |
| 341 | 341 |
///The destructor deallocates this automatically allocated map, |
| 342 | 342 |
///of course. |
| 343 | 343 |
///\return <tt> (*this) </tt> |
| 344 | 344 |
Dfs &predMap(PredMap &m) |
| 345 | 345 |
{
|
| 346 | 346 |
if(local_pred) {
|
| 347 | 347 |
delete _pred; |
| 348 | 348 |
local_pred=false; |
| 349 | 349 |
} |
| 350 | 350 |
_pred = &m; |
| 351 | 351 |
return *this; |
| 352 | 352 |
} |
| 353 | 353 |
|
| 354 | 354 |
///Sets the map that indicates which nodes are reached. |
| 355 | 355 |
|
| 356 | 356 |
///Sets the map that indicates which nodes are reached. |
| 357 | 357 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 358 | 358 |
///or \ref init(), an instance will be allocated automatically. |
| 359 | 359 |
///The destructor deallocates this automatically allocated map, |
| 360 | 360 |
///of course. |
| 361 | 361 |
///\return <tt> (*this) </tt> |
| 362 | 362 |
Dfs &reachedMap(ReachedMap &m) |
| 363 | 363 |
{
|
| 364 | 364 |
if(local_reached) {
|
| 365 | 365 |
delete _reached; |
| 366 | 366 |
local_reached=false; |
| 367 | 367 |
} |
| 368 | 368 |
_reached = &m; |
| 369 | 369 |
return *this; |
| 370 | 370 |
} |
| 371 | 371 |
|
| 372 | 372 |
///Sets the map that indicates which nodes are processed. |
| 373 | 373 |
|
| 374 | 374 |
///Sets the map that indicates which nodes are processed. |
| 375 | 375 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 376 | 376 |
///or \ref init(), an instance will be allocated automatically. |
| 377 | 377 |
///The destructor deallocates this automatically allocated map, |
| 378 | 378 |
///of course. |
| 379 | 379 |
///\return <tt> (*this) </tt> |
| 380 | 380 |
Dfs &processedMap(ProcessedMap &m) |
| 381 | 381 |
{
|
| 382 | 382 |
if(local_processed) {
|
| 383 | 383 |
delete _processed; |
| 384 | 384 |
local_processed=false; |
| 385 | 385 |
} |
| 386 | 386 |
_processed = &m; |
| 387 | 387 |
return *this; |
| 388 | 388 |
} |
| 389 | 389 |
|
| 390 | 390 |
///Sets the map that stores the distances of the nodes. |
| 391 | 391 |
|
| 392 | 392 |
///Sets the map that stores the distances of the nodes calculated by |
| 393 | 393 |
///the algorithm. |
| 394 | 394 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 395 | 395 |
///or \ref init(), an instance will be allocated automatically. |
| 396 | 396 |
///The destructor deallocates this automatically allocated map, |
| 397 | 397 |
///of course. |
| 398 | 398 |
///\return <tt> (*this) </tt> |
| 399 | 399 |
Dfs &distMap(DistMap &m) |
| 400 | 400 |
{
|
| ... | ... |
@@ -1033,335 +1033,335 @@ |
| 1033 | 1033 |
struct SetDistMapBase : public Base {
|
| 1034 | 1034 |
typedef T DistMap; |
| 1035 | 1035 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1036 | 1036 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1037 | 1037 |
}; |
| 1038 | 1038 |
///\brief \ref named-func-param "Named parameter" |
| 1039 | 1039 |
///for setting DistMap object. |
| 1040 | 1040 |
/// |
| 1041 | 1041 |
/// \ref named-func-param "Named parameter" |
| 1042 | 1042 |
///for setting DistMap object. |
| 1043 | 1043 |
template<class T> |
| 1044 | 1044 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1045 | 1045 |
{
|
| 1046 | 1046 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1047 | 1047 |
return DfsWizard<SetDistMapBase<T> >(*this); |
| 1048 | 1048 |
} |
| 1049 | 1049 |
|
| 1050 | 1050 |
template<class T> |
| 1051 | 1051 |
struct SetProcessedMapBase : public Base {
|
| 1052 | 1052 |
typedef T ProcessedMap; |
| 1053 | 1053 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1054 | 1054 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1055 | 1055 |
}; |
| 1056 | 1056 |
///\brief \ref named-func-param "Named parameter" |
| 1057 | 1057 |
///for setting ProcessedMap object. |
| 1058 | 1058 |
/// |
| 1059 | 1059 |
/// \ref named-func-param "Named parameter" |
| 1060 | 1060 |
///for setting ProcessedMap object. |
| 1061 | 1061 |
template<class T> |
| 1062 | 1062 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1063 | 1063 |
{
|
| 1064 | 1064 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1065 | 1065 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
| 1066 | 1066 |
} |
| 1067 | 1067 |
|
| 1068 | 1068 |
template<class T> |
| 1069 | 1069 |
struct SetPathBase : public Base {
|
| 1070 | 1070 |
typedef T Path; |
| 1071 | 1071 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1072 | 1072 |
}; |
| 1073 | 1073 |
///\brief \ref named-func-param "Named parameter" |
| 1074 | 1074 |
///for getting the DFS path to the target node. |
| 1075 | 1075 |
/// |
| 1076 | 1076 |
///\ref named-func-param "Named parameter" |
| 1077 | 1077 |
///for getting the DFS path to the target node. |
| 1078 | 1078 |
template<class T> |
| 1079 | 1079 |
DfsWizard<SetPathBase<T> > path(const T &t) |
| 1080 | 1080 |
{
|
| 1081 | 1081 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1082 | 1082 |
return DfsWizard<SetPathBase<T> >(*this); |
| 1083 | 1083 |
} |
| 1084 | 1084 |
|
| 1085 | 1085 |
///\brief \ref named-func-param "Named parameter" |
| 1086 | 1086 |
///for getting the distance of the target node. |
| 1087 | 1087 |
/// |
| 1088 | 1088 |
///\ref named-func-param "Named parameter" |
| 1089 | 1089 |
///for getting the distance of the target node. |
| 1090 | 1090 |
DfsWizard dist(const int &d) |
| 1091 | 1091 |
{
|
| 1092 | 1092 |
Base::_di=const_cast<int*>(&d); |
| 1093 | 1093 |
return *this; |
| 1094 | 1094 |
} |
| 1095 | 1095 |
|
| 1096 | 1096 |
}; |
| 1097 | 1097 |
|
| 1098 | 1098 |
///Function-type interface for DFS algorithm. |
| 1099 | 1099 |
|
| 1100 | 1100 |
///\ingroup search |
| 1101 | 1101 |
///Function-type interface for DFS algorithm. |
| 1102 | 1102 |
/// |
| 1103 | 1103 |
///This function also has several \ref named-func-param "named parameters", |
| 1104 | 1104 |
///they are declared as the members of class \ref DfsWizard. |
| 1105 | 1105 |
///The following examples show how to use these parameters. |
| 1106 | 1106 |
///\code |
| 1107 | 1107 |
/// // Compute the DFS tree |
| 1108 | 1108 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
| 1109 | 1109 |
/// |
| 1110 | 1110 |
/// // Compute the DFS path from s to t |
| 1111 | 1111 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
| 1112 | 1112 |
///\endcode |
| 1113 | 1113 |
///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()" |
| 1114 | 1114 |
///to the end of the parameter list. |
| 1115 | 1115 |
///\sa DfsWizard |
| 1116 | 1116 |
///\sa Dfs |
| 1117 | 1117 |
template<class GR> |
| 1118 | 1118 |
DfsWizard<DfsWizardBase<GR> > |
| 1119 | 1119 |
dfs(const GR &digraph) |
| 1120 | 1120 |
{
|
| 1121 | 1121 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
| 1122 | 1122 |
} |
| 1123 | 1123 |
|
| 1124 | 1124 |
#ifdef DOXYGEN |
| 1125 | 1125 |
/// \brief Visitor class for DFS. |
| 1126 | 1126 |
/// |
| 1127 | 1127 |
/// This class defines the interface of the DfsVisit events, and |
| 1128 | 1128 |
/// it could be the base of a real visitor class. |
| 1129 |
template <typename |
|
| 1129 |
template <typename GR> |
|
| 1130 | 1130 |
struct DfsVisitor {
|
| 1131 |
typedef |
|
| 1131 |
typedef GR Digraph; |
|
| 1132 | 1132 |
typedef typename Digraph::Arc Arc; |
| 1133 | 1133 |
typedef typename Digraph::Node Node; |
| 1134 | 1134 |
/// \brief Called for the source node of the DFS. |
| 1135 | 1135 |
/// |
| 1136 | 1136 |
/// This function is called for the source node of the DFS. |
| 1137 | 1137 |
void start(const Node& node) {}
|
| 1138 | 1138 |
/// \brief Called when the source node is leaved. |
| 1139 | 1139 |
/// |
| 1140 | 1140 |
/// This function is called when the source node is leaved. |
| 1141 | 1141 |
void stop(const Node& node) {}
|
| 1142 | 1142 |
/// \brief Called when a node is reached first time. |
| 1143 | 1143 |
/// |
| 1144 | 1144 |
/// This function is called when a node is reached first time. |
| 1145 | 1145 |
void reach(const Node& node) {}
|
| 1146 | 1146 |
/// \brief Called when an arc reaches a new node. |
| 1147 | 1147 |
/// |
| 1148 | 1148 |
/// This function is called when the DFS finds an arc whose target node |
| 1149 | 1149 |
/// is not reached yet. |
| 1150 | 1150 |
void discover(const Arc& arc) {}
|
| 1151 | 1151 |
/// \brief Called when an arc is examined but its target node is |
| 1152 | 1152 |
/// already discovered. |
| 1153 | 1153 |
/// |
| 1154 | 1154 |
/// This function is called when an arc is examined but its target node is |
| 1155 | 1155 |
/// already discovered. |
| 1156 | 1156 |
void examine(const Arc& arc) {}
|
| 1157 | 1157 |
/// \brief Called when the DFS steps back from a node. |
| 1158 | 1158 |
/// |
| 1159 | 1159 |
/// This function is called when the DFS steps back from a node. |
| 1160 | 1160 |
void leave(const Node& node) {}
|
| 1161 | 1161 |
/// \brief Called when the DFS steps back on an arc. |
| 1162 | 1162 |
/// |
| 1163 | 1163 |
/// This function is called when the DFS steps back on an arc. |
| 1164 | 1164 |
void backtrack(const Arc& arc) {}
|
| 1165 | 1165 |
}; |
| 1166 | 1166 |
#else |
| 1167 |
template <typename |
|
| 1167 |
template <typename GR> |
|
| 1168 | 1168 |
struct DfsVisitor {
|
| 1169 |
typedef |
|
| 1169 |
typedef GR Digraph; |
|
| 1170 | 1170 |
typedef typename Digraph::Arc Arc; |
| 1171 | 1171 |
typedef typename Digraph::Node Node; |
| 1172 | 1172 |
void start(const Node&) {}
|
| 1173 | 1173 |
void stop(const Node&) {}
|
| 1174 | 1174 |
void reach(const Node&) {}
|
| 1175 | 1175 |
void discover(const Arc&) {}
|
| 1176 | 1176 |
void examine(const Arc&) {}
|
| 1177 | 1177 |
void leave(const Node&) {}
|
| 1178 | 1178 |
void backtrack(const Arc&) {}
|
| 1179 | 1179 |
|
| 1180 | 1180 |
template <typename _Visitor> |
| 1181 | 1181 |
struct Constraints {
|
| 1182 | 1182 |
void constraints() {
|
| 1183 | 1183 |
Arc arc; |
| 1184 | 1184 |
Node node; |
| 1185 | 1185 |
visitor.start(node); |
| 1186 | 1186 |
visitor.stop(arc); |
| 1187 | 1187 |
visitor.reach(node); |
| 1188 | 1188 |
visitor.discover(arc); |
| 1189 | 1189 |
visitor.examine(arc); |
| 1190 | 1190 |
visitor.leave(node); |
| 1191 | 1191 |
visitor.backtrack(arc); |
| 1192 | 1192 |
} |
| 1193 | 1193 |
_Visitor& visitor; |
| 1194 | 1194 |
}; |
| 1195 | 1195 |
}; |
| 1196 | 1196 |
#endif |
| 1197 | 1197 |
|
| 1198 | 1198 |
/// \brief Default traits class of DfsVisit class. |
| 1199 | 1199 |
/// |
| 1200 | 1200 |
/// Default traits class of DfsVisit class. |
| 1201 | 1201 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1202 |
template<class |
|
| 1202 |
template<class GR> |
|
| 1203 | 1203 |
struct DfsVisitDefaultTraits {
|
| 1204 | 1204 |
|
| 1205 | 1205 |
/// \brief The type of the digraph the algorithm runs on. |
| 1206 |
typedef |
|
| 1206 |
typedef GR Digraph; |
|
| 1207 | 1207 |
|
| 1208 | 1208 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1209 | 1209 |
/// |
| 1210 | 1210 |
/// The type of the map that indicates which nodes are reached. |
| 1211 | 1211 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1212 | 1212 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1213 | 1213 |
|
| 1214 | 1214 |
/// \brief Instantiates a ReachedMap. |
| 1215 | 1215 |
/// |
| 1216 | 1216 |
/// This function instantiates a ReachedMap. |
| 1217 | 1217 |
/// \param digraph is the digraph, to which |
| 1218 | 1218 |
/// we would like to define the ReachedMap. |
| 1219 | 1219 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1220 | 1220 |
return new ReachedMap(digraph); |
| 1221 | 1221 |
} |
| 1222 | 1222 |
|
| 1223 | 1223 |
}; |
| 1224 | 1224 |
|
| 1225 | 1225 |
/// \ingroup search |
| 1226 | 1226 |
/// |
| 1227 |
/// \brief |
|
| 1227 |
/// \brief DFS algorithm class with visitor interface. |
|
| 1228 | 1228 |
/// |
| 1229 |
/// This class provides an efficient implementation of the |
|
| 1229 |
/// This class provides an efficient implementation of the DFS algorithm |
|
| 1230 | 1230 |
/// with visitor interface. |
| 1231 | 1231 |
/// |
| 1232 |
/// The |
|
| 1232 |
/// The DfsVisit class provides an alternative interface to the Dfs |
|
| 1233 | 1233 |
/// class. It works with callback mechanism, the DfsVisit object calls |
| 1234 | 1234 |
/// the member functions of the \c Visitor class on every DFS event. |
| 1235 | 1235 |
/// |
| 1236 | 1236 |
/// This interface of the DFS algorithm should be used in special cases |
| 1237 | 1237 |
/// when extra actions have to be performed in connection with certain |
| 1238 | 1238 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
| 1239 | 1239 |
/// instead. |
| 1240 | 1240 |
/// |
| 1241 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
| 1242 |
/// The default value is |
|
| 1243 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
|
| 1244 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
|
| 1245 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
|
| 1246 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
|
| 1241 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
| 1242 |
/// The default type is \ref ListDigraph. |
|
| 1243 |
/// The value of GR is not used directly by \ref DfsVisit, |
|
| 1244 |
/// it is only passed to \ref DfsVisitDefaultTraits. |
|
| 1245 |
/// \tparam VS The Visitor type that is used by the algorithm. |
|
| 1246 |
/// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which |
|
| 1247 | 1247 |
/// does not observe the DFS events. If you want to observe the DFS |
| 1248 | 1248 |
/// events, you should implement your own visitor class. |
| 1249 |
/// \tparam |
|
| 1249 |
/// \tparam TR Traits class to set various data types used by the |
|
| 1250 | 1250 |
/// algorithm. The default traits class is |
| 1251 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits< |
|
| 1251 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>". |
|
| 1252 | 1252 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
| 1253 | 1253 |
/// a DFS visit traits class. |
| 1254 | 1254 |
#ifdef DOXYGEN |
| 1255 |
template <typename |
|
| 1255 |
template <typename GR, typename VS, typename TR> |
|
| 1256 | 1256 |
#else |
| 1257 |
template <typename _Digraph = ListDigraph, |
|
| 1258 |
typename _Visitor = DfsVisitor<_Digraph>, |
|
| 1259 |
|
|
| 1257 |
template <typename GR = ListDigraph, |
|
| 1258 |
typename VS = DfsVisitor<GR>, |
|
| 1259 |
typename TR = DfsVisitDefaultTraits<GR> > |
|
| 1260 | 1260 |
#endif |
| 1261 | 1261 |
class DfsVisit {
|
| 1262 | 1262 |
public: |
| 1263 | 1263 |
|
| 1264 | 1264 |
///The traits class. |
| 1265 |
typedef |
|
| 1265 |
typedef TR Traits; |
|
| 1266 | 1266 |
|
| 1267 | 1267 |
///The type of the digraph the algorithm runs on. |
| 1268 | 1268 |
typedef typename Traits::Digraph Digraph; |
| 1269 | 1269 |
|
| 1270 | 1270 |
///The visitor type used by the algorithm. |
| 1271 |
typedef |
|
| 1271 |
typedef VS Visitor; |
|
| 1272 | 1272 |
|
| 1273 | 1273 |
///The type of the map that indicates which nodes are reached. |
| 1274 | 1274 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1275 | 1275 |
|
| 1276 | 1276 |
private: |
| 1277 | 1277 |
|
| 1278 | 1278 |
typedef typename Digraph::Node Node; |
| 1279 | 1279 |
typedef typename Digraph::NodeIt NodeIt; |
| 1280 | 1280 |
typedef typename Digraph::Arc Arc; |
| 1281 | 1281 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1282 | 1282 |
|
| 1283 | 1283 |
//Pointer to the underlying digraph. |
| 1284 | 1284 |
const Digraph *_digraph; |
| 1285 | 1285 |
//Pointer to the visitor object. |
| 1286 | 1286 |
Visitor *_visitor; |
| 1287 | 1287 |
//Pointer to the map of reached status of the nodes. |
| 1288 | 1288 |
ReachedMap *_reached; |
| 1289 | 1289 |
//Indicates if _reached is locally allocated (true) or not. |
| 1290 | 1290 |
bool local_reached; |
| 1291 | 1291 |
|
| 1292 | 1292 |
std::vector<typename Digraph::Arc> _stack; |
| 1293 | 1293 |
int _stack_head; |
| 1294 | 1294 |
|
| 1295 | 1295 |
//Creates the maps if necessary. |
| 1296 | 1296 |
void create_maps() {
|
| 1297 | 1297 |
if(!_reached) {
|
| 1298 | 1298 |
local_reached = true; |
| 1299 | 1299 |
_reached = Traits::createReachedMap(*_digraph); |
| 1300 | 1300 |
} |
| 1301 | 1301 |
} |
| 1302 | 1302 |
|
| 1303 | 1303 |
protected: |
| 1304 | 1304 |
|
| 1305 | 1305 |
DfsVisit() {}
|
| 1306 | 1306 |
|
| 1307 | 1307 |
public: |
| 1308 | 1308 |
|
| 1309 | 1309 |
typedef DfsVisit Create; |
| 1310 | 1310 |
|
| 1311 | 1311 |
/// \name Named Template Parameters |
| 1312 | 1312 |
|
| 1313 | 1313 |
///@{
|
| 1314 | 1314 |
template <class T> |
| 1315 | 1315 |
struct SetReachedMapTraits : public Traits {
|
| 1316 | 1316 |
typedef T ReachedMap; |
| 1317 | 1317 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1318 | 1318 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1319 | 1319 |
return 0; // ignore warnings |
| 1320 | 1320 |
} |
| 1321 | 1321 |
}; |
| 1322 | 1322 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1323 | 1323 |
/// ReachedMap type. |
| 1324 | 1324 |
/// |
| 1325 | 1325 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1326 | 1326 |
template <class T> |
| 1327 | 1327 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
| 1328 | 1328 |
SetReachedMapTraits<T> > {
|
| 1329 | 1329 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1330 | 1330 |
}; |
| 1331 | 1331 |
///@} |
| 1332 | 1332 |
|
| 1333 | 1333 |
public: |
| 1334 | 1334 |
|
| 1335 | 1335 |
/// \brief Constructor. |
| 1336 | 1336 |
/// |
| 1337 | 1337 |
/// Constructor. |
| 1338 | 1338 |
/// |
| 1339 | 1339 |
/// \param digraph The digraph the algorithm runs on. |
| 1340 | 1340 |
/// \param visitor The visitor object of the algorithm. |
| 1341 | 1341 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1342 | 1342 |
: _digraph(&digraph), _visitor(&visitor), |
| 1343 | 1343 |
_reached(0), local_reached(false) {}
|
| 1344 | 1344 |
|
| 1345 | 1345 |
/// \brief Destructor. |
| 1346 | 1346 |
~DfsVisit() {
|
| 1347 | 1347 |
if(local_reached) delete _reached; |
| 1348 | 1348 |
} |
| 1349 | 1349 |
|
| 1350 | 1350 |
/// \brief Sets the map that indicates which nodes are reached. |
| 1351 | 1351 |
/// |
| 1352 | 1352 |
/// Sets the map that indicates which nodes are reached. |
| 1353 | 1353 |
/// If you don't use this function before calling \ref run(Node) "run()" |
| 1354 | 1354 |
/// or \ref init(), an instance will be allocated automatically. |
| 1355 | 1355 |
/// The destructor deallocates this automatically allocated map, |
| 1356 | 1356 |
/// of course. |
| 1357 | 1357 |
/// \return <tt> (*this) </tt> |
| 1358 | 1358 |
DfsVisit &reachedMap(ReachedMap &m) {
|
| 1359 | 1359 |
if(local_reached) {
|
| 1360 | 1360 |
delete _reached; |
| 1361 | 1361 |
local_reached=false; |
| 1362 | 1362 |
} |
| 1363 | 1363 |
_reached = &m; |
| 1364 | 1364 |
return *this; |
| 1365 | 1365 |
} |
| 1366 | 1366 |
|
| 1367 | 1367 |
public: |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DIJKSTRA_H |
| 20 | 20 |
#define LEMON_DIJKSTRA_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup shortest_path |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Dijkstra algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <limits> |
| 27 | 27 |
#include <lemon/list_graph.h> |
| 28 | 28 |
#include <lemon/bin_heap.h> |
| 29 | 29 |
#include <lemon/bits/path_dump.h> |
| 30 | 30 |
#include <lemon/core.h> |
| 31 | 31 |
#include <lemon/error.h> |
| 32 | 32 |
#include <lemon/maps.h> |
| 33 | 33 |
#include <lemon/path.h> |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
| 38 | 38 |
/// |
| 39 | 39 |
/// This operation traits class defines all computational operations and |
| 40 | 40 |
/// constants which are used in the Dijkstra algorithm. |
| 41 | 41 |
template <typename Value> |
| 42 | 42 |
struct DijkstraDefaultOperationTraits {
|
| 43 | 43 |
/// \brief Gives back the zero value of the type. |
| 44 | 44 |
static Value zero() {
|
| 45 | 45 |
return static_cast<Value>(0); |
| 46 | 46 |
} |
| 47 | 47 |
/// \brief Gives back the sum of the given two elements. |
| 48 | 48 |
static Value plus(const Value& left, const Value& right) {
|
| 49 | 49 |
return left + right; |
| 50 | 50 |
} |
| 51 | 51 |
/// \brief Gives back true only if the first value is less than the second. |
| 52 | 52 |
static bool less(const Value& left, const Value& right) {
|
| 53 | 53 |
return left < right; |
| 54 | 54 |
} |
| 55 | 55 |
}; |
| 56 | 56 |
|
| 57 | 57 |
///Default traits class of Dijkstra class. |
| 58 | 58 |
|
| 59 | 59 |
///Default traits class of Dijkstra class. |
| 60 | 60 |
///\tparam GR The type of the digraph. |
| 61 | 61 |
///\tparam LM The type of the length map. |
| 62 | 62 |
template<class GR, class LM> |
| 63 | 63 |
struct DijkstraDefaultTraits |
| 64 | 64 |
{
|
| 65 | 65 |
///The type of the digraph the algorithm runs on. |
| 66 | 66 |
typedef GR Digraph; |
| 67 | 67 |
|
| 68 | 68 |
///The type of the map that stores the arc lengths. |
| 69 | 69 |
|
| 70 | 70 |
///The type of the map that stores the arc lengths. |
| 71 | 71 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 72 | 72 |
typedef LM LengthMap; |
| 73 | 73 |
///The type of the length of the arcs. |
| 74 | 74 |
typedef typename LM::Value Value; |
| 75 | 75 |
|
| 76 |
/// Operation traits for Dijkstra algorithm. |
|
| 76 |
/// Operation traits for %Dijkstra algorithm. |
|
| 77 | 77 |
|
| 78 | 78 |
/// This class defines the operations that are used in the algorithm. |
| 79 | 79 |
/// \see DijkstraDefaultOperationTraits |
| 80 | 80 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
| 81 | 81 |
|
| 82 | 82 |
/// The cross reference type used by the heap. |
| 83 | 83 |
|
| 84 | 84 |
/// The cross reference type used by the heap. |
| 85 | 85 |
/// Usually it is \c Digraph::NodeMap<int>. |
| 86 | 86 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 87 |
///Instantiates a \ |
|
| 87 |
///Instantiates a \c HeapCrossRef. |
|
| 88 | 88 |
|
| 89 | 89 |
///This function instantiates a \ref HeapCrossRef. |
| 90 | 90 |
/// \param g is the digraph, to which we would like to define the |
| 91 | 91 |
/// \ref HeapCrossRef. |
| 92 | 92 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
| 93 | 93 |
{
|
| 94 | 94 |
return new HeapCrossRef(g); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 |
///The heap type used by the Dijkstra algorithm. |
|
| 97 |
///The heap type used by the %Dijkstra algorithm. |
|
| 98 | 98 |
|
| 99 | 99 |
///The heap type used by the Dijkstra algorithm. |
| 100 | 100 |
/// |
| 101 | 101 |
///\sa BinHeap |
| 102 | 102 |
///\sa Dijkstra |
| 103 | 103 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
| 104 |
///Instantiates a \ |
|
| 104 |
///Instantiates a \c Heap. |
|
| 105 | 105 |
|
| 106 | 106 |
///This function instantiates a \ref Heap. |
| 107 | 107 |
static Heap *createHeap(HeapCrossRef& r) |
| 108 | 108 |
{
|
| 109 | 109 |
return new Heap(r); |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
///\brief The type of the map that stores the predecessor |
| 113 | 113 |
///arcs of the shortest paths. |
| 114 | 114 |
/// |
| 115 | 115 |
///The type of the map that stores the predecessor |
| 116 | 116 |
///arcs of the shortest paths. |
| 117 | 117 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 118 | 118 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 119 |
///Instantiates a PredMap. |
|
| 119 |
///Instantiates a \c PredMap. |
|
| 120 | 120 |
|
| 121 |
///This function instantiates a PredMap. |
|
| 121 |
///This function instantiates a \ref PredMap. |
|
| 122 | 122 |
///\param g is the digraph, to which we would like to define the |
| 123 |
///PredMap. |
|
| 123 |
///\ref PredMap. |
|
| 124 | 124 |
static PredMap *createPredMap(const Digraph &g) |
| 125 | 125 |
{
|
| 126 | 126 |
return new PredMap(g); |
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 | 129 |
///The type of the map that indicates which nodes are processed. |
| 130 | 130 |
|
| 131 | 131 |
///The type of the map that indicates which nodes are processed. |
| 132 | 132 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 133 | 133 |
///By default it is a NullMap. |
| 134 | 134 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 135 |
///Instantiates a ProcessedMap. |
|
| 135 |
///Instantiates a \c ProcessedMap. |
|
| 136 | 136 |
|
| 137 |
///This function instantiates a ProcessedMap. |
|
| 137 |
///This function instantiates a \ref ProcessedMap. |
|
| 138 | 138 |
///\param g is the digraph, to which |
| 139 |
///we would like to define the ProcessedMap |
|
| 139 |
///we would like to define the \ref ProcessedMap. |
|
| 140 | 140 |
#ifdef DOXYGEN |
| 141 | 141 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 142 | 142 |
#else |
| 143 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 144 | 144 |
#endif |
| 145 | 145 |
{
|
| 146 | 146 |
return new ProcessedMap(); |
| 147 | 147 |
} |
| 148 | 148 |
|
| 149 | 149 |
///The type of the map that stores the distances of the nodes. |
| 150 | 150 |
|
| 151 | 151 |
///The type of the map that stores the distances of the nodes. |
| 152 | 152 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 153 | 153 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
| 154 |
///Instantiates a DistMap. |
|
| 154 |
///Instantiates a \c DistMap. |
|
| 155 | 155 |
|
| 156 |
///This function instantiates a DistMap. |
|
| 156 |
///This function instantiates a \ref DistMap. |
|
| 157 | 157 |
///\param g is the digraph, to which we would like to define |
| 158 |
///the DistMap |
|
| 158 |
///the \ref DistMap. |
|
| 159 | 159 |
static DistMap *createDistMap(const Digraph &g) |
| 160 | 160 |
{
|
| 161 | 161 |
return new DistMap(g); |
| 162 | 162 |
} |
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
///%Dijkstra algorithm class. |
| 166 | 166 |
|
| 167 | 167 |
/// \ingroup shortest_path |
| 168 | 168 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
| 169 | 169 |
/// |
| 170 | 170 |
///The arc lengths are passed to the algorithm using a |
| 171 | 171 |
///\ref concepts::ReadMap "ReadMap", |
| 172 | 172 |
///so it is easy to change it to any kind of length. |
| 173 | 173 |
///The type of the length is determined by the |
| 174 | 174 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
| 175 | 175 |
///It is also possible to change the underlying priority heap. |
| 176 | 176 |
/// |
| 177 | 177 |
///There is also a \ref dijkstra() "function-type interface" for the |
| 178 | 178 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
| 179 | 179 |
///it can be used easier. |
| 180 | 180 |
/// |
| 181 | 181 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 182 | 182 |
///The default type is \ref ListDigraph. |
| 183 | 183 |
///\tparam LM A \ref concepts::ReadMap "readable" arc map that specifies |
| 184 | 184 |
///the lengths of the arcs. |
| 185 | 185 |
///It is read once for each arc, so the map may involve in |
| 186 | 186 |
///relatively time consuming process to compute the arc lengths if |
| 187 | 187 |
///it is necessary. The default map type is \ref |
| 188 | 188 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 189 | 189 |
#ifdef DOXYGEN |
| 190 | 190 |
template <typename GR, typename LM, typename TR> |
| 191 | 191 |
#else |
| 192 | 192 |
template <typename GR=ListDigraph, |
| 193 | 193 |
typename LM=typename GR::template ArcMap<int>, |
| 194 | 194 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
| 195 | 195 |
#endif |
| 196 | 196 |
class Dijkstra {
|
| 197 | 197 |
public: |
| 198 | 198 |
|
| 199 | 199 |
///The type of the digraph the algorithm runs on. |
| 200 | 200 |
typedef typename TR::Digraph Digraph; |
| 201 | 201 |
|
| 202 | 202 |
///The type of the length of the arcs. |
| 203 | 203 |
typedef typename TR::LengthMap::Value Value; |
| 204 | 204 |
///The type of the map that stores the arc lengths. |
| 205 | 205 |
typedef typename TR::LengthMap LengthMap; |
| 206 | 206 |
///\brief The type of the map that stores the predecessor arcs of the |
| 207 | 207 |
///shortest paths. |
| 208 | 208 |
typedef typename TR::PredMap PredMap; |
| 209 | 209 |
///The type of the map that stores the distances of the nodes. |
| 210 | 210 |
typedef typename TR::DistMap DistMap; |
| 211 | 211 |
///The type of the map that indicates which nodes are processed. |
| 212 | 212 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 213 | 213 |
///The type of the paths. |
| 214 | 214 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 215 | 215 |
///The cross reference type used for the current heap. |
| 216 | 216 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
| 217 | 217 |
///The heap type used by the algorithm. |
| 218 | 218 |
typedef typename TR::Heap Heap; |
| 219 |
///The operation traits class |
|
| 219 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
|
| 220 |
///of the algorithm. |
|
| 220 | 221 |
typedef typename TR::OperationTraits OperationTraits; |
| 221 | 222 |
|
| 222 | 223 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
| 223 | 224 |
typedef TR Traits; |
| 224 | 225 |
|
| 225 | 226 |
private: |
| 226 | 227 |
|
| 227 | 228 |
typedef typename Digraph::Node Node; |
| 228 | 229 |
typedef typename Digraph::NodeIt NodeIt; |
| 229 | 230 |
typedef typename Digraph::Arc Arc; |
| 230 | 231 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 231 | 232 |
|
| 232 | 233 |
//Pointer to the underlying digraph. |
| 233 | 234 |
const Digraph *G; |
| 234 | 235 |
//Pointer to the length map. |
| 235 |
const LengthMap * |
|
| 236 |
const LengthMap *_length; |
|
| 236 | 237 |
//Pointer to the map of predecessors arcs. |
| 237 | 238 |
PredMap *_pred; |
| 238 | 239 |
//Indicates if _pred is locally allocated (true) or not. |
| 239 | 240 |
bool local_pred; |
| 240 | 241 |
//Pointer to the map of distances. |
| 241 | 242 |
DistMap *_dist; |
| 242 | 243 |
//Indicates if _dist is locally allocated (true) or not. |
| 243 | 244 |
bool local_dist; |
| 244 | 245 |
//Pointer to the map of processed status of the nodes. |
| 245 | 246 |
ProcessedMap *_processed; |
| 246 | 247 |
//Indicates if _processed is locally allocated (true) or not. |
| 247 | 248 |
bool local_processed; |
| 248 | 249 |
//Pointer to the heap cross references. |
| 249 | 250 |
HeapCrossRef *_heap_cross_ref; |
| 250 | 251 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
| 251 | 252 |
bool local_heap_cross_ref; |
| 252 | 253 |
//Pointer to the heap. |
| 253 | 254 |
Heap *_heap; |
| 254 | 255 |
//Indicates if _heap is locally allocated (true) or not. |
| 255 | 256 |
bool local_heap; |
| 256 | 257 |
|
| 257 | 258 |
//Creates the maps if necessary. |
| 258 | 259 |
void create_maps() |
| 259 | 260 |
{
|
| 260 | 261 |
if(!_pred) {
|
| 261 | 262 |
local_pred = true; |
| 262 | 263 |
_pred = Traits::createPredMap(*G); |
| 263 | 264 |
} |
| 264 | 265 |
if(!_dist) {
|
| 265 | 266 |
local_dist = true; |
| 266 | 267 |
_dist = Traits::createDistMap(*G); |
| 267 | 268 |
} |
| 268 | 269 |
if(!_processed) {
|
| 269 | 270 |
local_processed = true; |
| 270 | 271 |
_processed = Traits::createProcessedMap(*G); |
| 271 | 272 |
} |
| 272 | 273 |
if (!_heap_cross_ref) {
|
| 273 | 274 |
local_heap_cross_ref = true; |
| 274 | 275 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
| 275 | 276 |
} |
| 276 | 277 |
if (!_heap) {
|
| 277 | 278 |
local_heap = true; |
| 278 | 279 |
_heap = Traits::createHeap(*_heap_cross_ref); |
| 279 | 280 |
} |
| 280 | 281 |
} |
| 281 | 282 |
|
| 282 | 283 |
public: |
| 283 | 284 |
|
| 284 | 285 |
typedef Dijkstra Create; |
| 285 | 286 |
|
| 286 | 287 |
///\name Named template parameters |
| 287 | 288 |
|
| 288 | 289 |
///@{
|
| 289 | 290 |
|
| 290 | 291 |
template <class T> |
| 291 | 292 |
struct SetPredMapTraits : public Traits {
|
| 292 | 293 |
typedef T PredMap; |
| 293 | 294 |
static PredMap *createPredMap(const Digraph &) |
| 294 | 295 |
{
|
| 295 | 296 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 296 | 297 |
return 0; // ignore warnings |
| 297 | 298 |
} |
| 298 | 299 |
}; |
| 299 | 300 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 300 |
///PredMap type. |
|
| 301 |
///\c PredMap type. |
|
| 301 | 302 |
/// |
| 302 | 303 |
///\ref named-templ-param "Named parameter" for setting |
| 303 |
///PredMap type. |
|
| 304 |
///\c PredMap type. |
|
| 304 | 305 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 305 | 306 |
template <class T> |
| 306 | 307 |
struct SetPredMap |
| 307 | 308 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
|
| 308 | 309 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
| 309 | 310 |
}; |
| 310 | 311 |
|
| 311 | 312 |
template <class T> |
| 312 | 313 |
struct SetDistMapTraits : public Traits {
|
| 313 | 314 |
typedef T DistMap; |
| 314 | 315 |
static DistMap *createDistMap(const Digraph &) |
| 315 | 316 |
{
|
| 316 | 317 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 317 | 318 |
return 0; // ignore warnings |
| 318 | 319 |
} |
| 319 | 320 |
}; |
| 320 | 321 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 321 |
///DistMap type. |
|
| 322 |
///\c DistMap type. |
|
| 322 | 323 |
/// |
| 323 | 324 |
///\ref named-templ-param "Named parameter" for setting |
| 324 |
///DistMap type. |
|
| 325 |
///\c DistMap type. |
|
| 325 | 326 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 326 | 327 |
template <class T> |
| 327 | 328 |
struct SetDistMap |
| 328 | 329 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > {
|
| 329 | 330 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
| 330 | 331 |
}; |
| 331 | 332 |
|
| 332 | 333 |
template <class T> |
| 333 | 334 |
struct SetProcessedMapTraits : public Traits {
|
| 334 | 335 |
typedef T ProcessedMap; |
| 335 | 336 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 336 | 337 |
{
|
| 337 | 338 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 338 | 339 |
return 0; // ignore warnings |
| 339 | 340 |
} |
| 340 | 341 |
}; |
| 341 | 342 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 342 |
///ProcessedMap type. |
|
| 343 |
///\c ProcessedMap type. |
|
| 343 | 344 |
/// |
| 344 | 345 |
///\ref named-templ-param "Named parameter" for setting |
| 345 |
///ProcessedMap type. |
|
| 346 |
///\c ProcessedMap type. |
|
| 346 | 347 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 347 | 348 |
template <class T> |
| 348 | 349 |
struct SetProcessedMap |
| 349 | 350 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > {
|
| 350 | 351 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
| 351 | 352 |
}; |
| 352 | 353 |
|
| 353 | 354 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 354 | 355 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 355 | 356 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 356 | 357 |
{
|
| 357 | 358 |
return new ProcessedMap(g); |
| 358 | 359 |
} |
| 359 | 360 |
}; |
| 360 | 361 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 361 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 362 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 362 | 363 |
/// |
| 363 | 364 |
///\ref named-templ-param "Named parameter" for setting |
| 364 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 365 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
| 365 | 366 |
///If you don't set it explicitly, it will be automatically allocated. |
| 366 | 367 |
struct SetStandardProcessedMap |
| 367 | 368 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > {
|
| 368 | 369 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
| 369 | 370 |
Create; |
| 370 | 371 |
}; |
| 371 | 372 |
|
| 372 | 373 |
template <class H, class CR> |
| 373 | 374 |
struct SetHeapTraits : public Traits {
|
| 374 | 375 |
typedef CR HeapCrossRef; |
| 375 | 376 |
typedef H Heap; |
| 376 | 377 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) {
|
| 377 | 378 |
LEMON_ASSERT(false, "HeapCrossRef is not initialized"); |
| 378 | 379 |
return 0; // ignore warnings |
| 379 | 380 |
} |
| 380 | 381 |
static Heap *createHeap(HeapCrossRef &) |
| 381 | 382 |
{
|
| 382 | 383 |
LEMON_ASSERT(false, "Heap is not initialized"); |
| 383 | 384 |
return 0; // ignore warnings |
| 384 | 385 |
} |
| 385 | 386 |
}; |
| 386 | 387 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 387 | 388 |
///heap and cross reference types |
| 388 | 389 |
/// |
| 389 | 390 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
| 390 | 391 |
///reference types. If this named parameter is used, then external |
| 391 | 392 |
///heap and cross reference objects must be passed to the algorithm |
| 392 | 393 |
///using the \ref heap() function before calling \ref run(Node) "run()" |
| 393 | 394 |
///or \ref init(). |
| 394 | 395 |
///\sa SetStandardHeap |
| 395 | 396 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
| 396 | 397 |
struct SetHeap |
| 397 | 398 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > {
|
| 398 | 399 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
| 399 | 400 |
}; |
| 400 | 401 |
|
| 401 | 402 |
template <class H, class CR> |
| 402 | 403 |
struct SetStandardHeapTraits : public Traits {
|
| 403 | 404 |
typedef CR HeapCrossRef; |
| 404 | 405 |
typedef H Heap; |
| 405 | 406 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) {
|
| 406 | 407 |
return new HeapCrossRef(G); |
| 407 | 408 |
} |
| 408 | 409 |
static Heap *createHeap(HeapCrossRef &R) |
| 409 | 410 |
{
|
| 410 | 411 |
return new Heap(R); |
| 411 | 412 |
} |
| 412 | 413 |
}; |
| 413 | 414 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 414 | 415 |
///heap and cross reference types with automatic allocation |
| 415 | 416 |
/// |
| 416 | 417 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
| 417 | 418 |
///reference types with automatic allocation. |
| 418 | 419 |
///They should have standard constructor interfaces to be able to |
| 419 | 420 |
///automatically created by the algorithm (i.e. the digraph should be |
| 420 | 421 |
///passed to the constructor of the cross reference and the cross |
| 421 | 422 |
///reference should be passed to the constructor of the heap). |
| 422 | 423 |
///However external heap and cross reference objects could also be |
| 423 | 424 |
///passed to the algorithm using the \ref heap() function before |
| 424 | 425 |
///calling \ref run(Node) "run()" or \ref init(). |
| 425 | 426 |
///\sa SetHeap |
| 426 | 427 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
| 427 | 428 |
struct SetStandardHeap |
| 428 | 429 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > {
|
| 429 | 430 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
| 430 | 431 |
Create; |
| 431 | 432 |
}; |
| 432 | 433 |
|
| 433 | 434 |
template <class T> |
| 434 | 435 |
struct SetOperationTraitsTraits : public Traits {
|
| 435 | 436 |
typedef T OperationTraits; |
| 436 | 437 |
}; |
| 437 | 438 |
|
| 438 | 439 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 439 | 440 |
///\c OperationTraits type |
| 440 | 441 |
/// |
| 441 | 442 |
///\ref named-templ-param "Named parameter" for setting |
| 442 |
///\ |
|
| 443 |
///\c OperationTraits type. |
|
| 443 | 444 |
template <class T> |
| 444 | 445 |
struct SetOperationTraits |
| 445 | 446 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > {
|
| 446 | 447 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
| 447 | 448 |
Create; |
| 448 | 449 |
}; |
| 449 | 450 |
|
| 450 | 451 |
///@} |
| 451 | 452 |
|
| 452 | 453 |
protected: |
| 453 | 454 |
|
| 454 | 455 |
Dijkstra() {}
|
| 455 | 456 |
|
| 456 | 457 |
public: |
| 457 | 458 |
|
| 458 | 459 |
///Constructor. |
| 459 | 460 |
|
| 460 | 461 |
///Constructor. |
| 461 |
///\param _g The digraph the algorithm runs on. |
|
| 462 |
///\param _length The length map used by the algorithm. |
|
| 463 |
Dijkstra(const Digraph& _g, const LengthMap& _length) : |
|
| 464 |
G(&_g), length(&_length), |
|
| 462 |
///\param g The digraph the algorithm runs on. |
|
| 463 |
///\param length The length map used by the algorithm. |
|
| 464 |
Dijkstra(const Digraph& g, const LengthMap& length) : |
|
| 465 |
G(&g), _length(&length), |
|
| 465 | 466 |
_pred(NULL), local_pred(false), |
| 466 | 467 |
_dist(NULL), local_dist(false), |
| 467 | 468 |
_processed(NULL), local_processed(false), |
| 468 | 469 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
| 469 | 470 |
_heap(NULL), local_heap(false) |
| 470 | 471 |
{ }
|
| 471 | 472 |
|
| 472 | 473 |
///Destructor. |
| 473 | 474 |
~Dijkstra() |
| 474 | 475 |
{
|
| 475 | 476 |
if(local_pred) delete _pred; |
| 476 | 477 |
if(local_dist) delete _dist; |
| 477 | 478 |
if(local_processed) delete _processed; |
| 478 | 479 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
| 479 | 480 |
if(local_heap) delete _heap; |
| 480 | 481 |
} |
| 481 | 482 |
|
| 482 | 483 |
///Sets the length map. |
| 483 | 484 |
|
| 484 | 485 |
///Sets the length map. |
| 485 | 486 |
///\return <tt> (*this) </tt> |
| 486 | 487 |
Dijkstra &lengthMap(const LengthMap &m) |
| 487 | 488 |
{
|
| 488 |
|
|
| 489 |
_length = &m; |
|
| 489 | 490 |
return *this; |
| 490 | 491 |
} |
| 491 | 492 |
|
| 492 | 493 |
///Sets the map that stores the predecessor arcs. |
| 493 | 494 |
|
| 494 | 495 |
///Sets the map that stores the predecessor arcs. |
| 495 | 496 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 496 | 497 |
///or \ref init(), an instance will be allocated automatically. |
| 497 | 498 |
///The destructor deallocates this automatically allocated map, |
| 498 | 499 |
///of course. |
| 499 | 500 |
///\return <tt> (*this) </tt> |
| 500 | 501 |
Dijkstra &predMap(PredMap &m) |
| 501 | 502 |
{
|
| 502 | 503 |
if(local_pred) {
|
| 503 | 504 |
delete _pred; |
| 504 | 505 |
local_pred=false; |
| 505 | 506 |
} |
| 506 | 507 |
_pred = &m; |
| 507 | 508 |
return *this; |
| 508 | 509 |
} |
| 509 | 510 |
|
| 510 | 511 |
///Sets the map that indicates which nodes are processed. |
| 511 | 512 |
|
| 512 | 513 |
///Sets the map that indicates which nodes are processed. |
| 513 | 514 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 514 | 515 |
///or \ref init(), an instance will be allocated automatically. |
| 515 | 516 |
///The destructor deallocates this automatically allocated map, |
| 516 | 517 |
///of course. |
| 517 | 518 |
///\return <tt> (*this) </tt> |
| 518 | 519 |
Dijkstra &processedMap(ProcessedMap &m) |
| 519 | 520 |
{
|
| 520 | 521 |
if(local_processed) {
|
| 521 | 522 |
delete _processed; |
| 522 | 523 |
local_processed=false; |
| 523 | 524 |
} |
| 524 | 525 |
_processed = &m; |
| 525 | 526 |
return *this; |
| 526 | 527 |
} |
| 527 | 528 |
|
| 528 | 529 |
///Sets the map that stores the distances of the nodes. |
| 529 | 530 |
|
| 530 | 531 |
///Sets the map that stores the distances of the nodes calculated by the |
| 531 | 532 |
///algorithm. |
| 532 | 533 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 533 | 534 |
///or \ref init(), an instance will be allocated automatically. |
| 534 | 535 |
///The destructor deallocates this automatically allocated map, |
| 535 | 536 |
///of course. |
| 536 | 537 |
///\return <tt> (*this) </tt> |
| 537 | 538 |
Dijkstra &distMap(DistMap &m) |
| 538 | 539 |
{
|
| 539 | 540 |
if(local_dist) {
|
| 540 | 541 |
delete _dist; |
| 541 | 542 |
local_dist=false; |
| 542 | 543 |
} |
| 543 | 544 |
_dist = &m; |
| 544 | 545 |
return *this; |
| 545 | 546 |
} |
| 546 | 547 |
|
| 547 | 548 |
///Sets the heap and the cross reference used by algorithm. |
| 548 | 549 |
|
| 549 | 550 |
///Sets the heap and the cross reference used by algorithm. |
| 550 | 551 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 551 | 552 |
///or \ref init(), heap and cross reference instances will be |
| 552 | 553 |
///allocated automatically. |
| 553 | 554 |
///The destructor deallocates these automatically allocated objects, |
| 554 | 555 |
///of course. |
| 555 | 556 |
///\return <tt> (*this) </tt> |
| 556 | 557 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
| 557 | 558 |
{
|
| 558 | 559 |
if(local_heap_cross_ref) {
|
| 559 | 560 |
delete _heap_cross_ref; |
| 560 | 561 |
local_heap_cross_ref=false; |
| 561 | 562 |
} |
| 562 | 563 |
_heap_cross_ref = &cr; |
| 563 | 564 |
if(local_heap) {
|
| 564 | 565 |
delete _heap; |
| 565 | 566 |
local_heap=false; |
| 566 | 567 |
} |
| 567 | 568 |
_heap = &hp; |
| 568 | 569 |
return *this; |
| 569 | 570 |
} |
| 570 | 571 |
|
| 571 | 572 |
private: |
| 572 | 573 |
|
| 573 | 574 |
void finalizeNodeData(Node v,Value dst) |
| 574 | 575 |
{
|
| 575 | 576 |
_processed->set(v,true); |
| 576 | 577 |
_dist->set(v, dst); |
| 577 | 578 |
} |
| 578 | 579 |
|
| 579 | 580 |
public: |
| 580 | 581 |
|
| 581 | 582 |
///\name Execution Control |
| 582 | 583 |
///The simplest way to execute the %Dijkstra algorithm is to use |
| 583 | 584 |
///one of the member functions called \ref run(Node) "run()".\n |
| 584 | 585 |
///If you need more control on the execution, first you have to call |
| 585 | 586 |
///\ref init(), then you can add several source nodes with |
| 586 | 587 |
///\ref addSource(). Finally the actual path computation can be |
| 587 | 588 |
///performed with one of the \ref start() functions. |
| 588 | 589 |
|
| 589 | 590 |
///@{
|
| 590 | 591 |
|
| 591 | 592 |
///\brief Initializes the internal data structures. |
| 592 | 593 |
/// |
| 593 | 594 |
///Initializes the internal data structures. |
| 594 | 595 |
void init() |
| 595 | 596 |
{
|
| 596 | 597 |
create_maps(); |
| 597 | 598 |
_heap->clear(); |
| 598 | 599 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 599 | 600 |
_pred->set(u,INVALID); |
| 600 | 601 |
_processed->set(u,false); |
| 601 | 602 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
| 602 | 603 |
} |
| 603 | 604 |
} |
| 604 | 605 |
|
| 605 | 606 |
///Adds a new source node. |
| 606 | 607 |
|
| 607 | 608 |
///Adds a new source node to the priority heap. |
| 608 | 609 |
///The optional second parameter is the initial distance of the node. |
| 609 | 610 |
/// |
| 610 | 611 |
///The function checks if the node has already been added to the heap and |
| 611 | 612 |
///it is pushed to the heap only if either it was not in the heap |
| 612 | 613 |
///or the shortest path found till then is shorter than \c dst. |
| 613 | 614 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
| 614 | 615 |
{
|
| 615 | 616 |
if(_heap->state(s) != Heap::IN_HEAP) {
|
| 616 | 617 |
_heap->push(s,dst); |
| 617 | 618 |
} else if(OperationTraits::less((*_heap)[s], dst)) {
|
| 618 | 619 |
_heap->set(s,dst); |
| 619 | 620 |
_pred->set(s,INVALID); |
| 620 | 621 |
} |
| 621 | 622 |
} |
| 622 | 623 |
|
| 623 | 624 |
///Processes the next node in the priority heap |
| 624 | 625 |
|
| 625 | 626 |
///Processes the next node in the priority heap. |
| 626 | 627 |
/// |
| 627 | 628 |
///\return The processed node. |
| 628 | 629 |
/// |
| 629 | 630 |
///\warning The priority heap must not be empty. |
| 630 | 631 |
Node processNextNode() |
| 631 | 632 |
{
|
| 632 | 633 |
Node v=_heap->top(); |
| 633 | 634 |
Value oldvalue=_heap->prio(); |
| 634 | 635 |
_heap->pop(); |
| 635 | 636 |
finalizeNodeData(v,oldvalue); |
| 636 | 637 |
|
| 637 | 638 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) {
|
| 638 | 639 |
Node w=G->target(e); |
| 639 | 640 |
switch(_heap->state(w)) {
|
| 640 | 641 |
case Heap::PRE_HEAP: |
| 641 |
_heap->push(w,OperationTraits::plus(oldvalue, (* |
|
| 642 |
_heap->push(w,OperationTraits::plus(oldvalue, (*_length)[e])); |
|
| 642 | 643 |
_pred->set(w,e); |
| 643 | 644 |
break; |
| 644 | 645 |
case Heap::IN_HEAP: |
| 645 | 646 |
{
|
| 646 |
Value newvalue = OperationTraits::plus(oldvalue, (* |
|
| 647 |
Value newvalue = OperationTraits::plus(oldvalue, (*_length)[e]); |
|
| 647 | 648 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
|
| 648 | 649 |
_heap->decrease(w, newvalue); |
| 649 | 650 |
_pred->set(w,e); |
| 650 | 651 |
} |
| 651 | 652 |
} |
| 652 | 653 |
break; |
| 653 | 654 |
case Heap::POST_HEAP: |
| 654 | 655 |
break; |
| 655 | 656 |
} |
| 656 | 657 |
} |
| 657 | 658 |
return v; |
| 658 | 659 |
} |
| 659 | 660 |
|
| 660 | 661 |
///The next node to be processed. |
| 661 | 662 |
|
| 662 | 663 |
///Returns the next node to be processed or \c INVALID if the |
| 663 | 664 |
///priority heap is empty. |
| 664 | 665 |
Node nextNode() const |
| 665 | 666 |
{
|
| 666 | 667 |
return !_heap->empty()?_heap->top():INVALID; |
| 667 | 668 |
} |
| 668 | 669 |
|
| 669 | 670 |
///Returns \c false if there are nodes to be processed. |
| 670 | 671 |
|
| 671 | 672 |
///Returns \c false if there are nodes to be processed |
| 672 | 673 |
///in the priority heap. |
| 673 | 674 |
bool emptyQueue() const { return _heap->empty(); }
|
| 674 | 675 |
|
| 675 | 676 |
///Returns the number of the nodes to be processed. |
| 676 | 677 |
|
| 677 | 678 |
///Returns the number of the nodes to be processed |
| 678 | 679 |
///in the priority heap. |
| 679 | 680 |
int queueSize() const { return _heap->size(); }
|
| 680 | 681 |
|
| 681 | 682 |
///Executes the algorithm. |
| 682 | 683 |
|
| 683 | 684 |
///Executes the algorithm. |
| 684 | 685 |
/// |
| 685 | 686 |
///This method runs the %Dijkstra algorithm from the root node(s) |
| 686 | 687 |
///in order to compute the shortest path to each node. |
| 687 | 688 |
/// |
| 688 | 689 |
///The algorithm computes |
| 689 | 690 |
///- the shortest path tree (forest), |
| 690 | 691 |
///- the distance of each node from the root(s). |
| 691 | 692 |
/// |
| 692 | 693 |
///\pre init() must be called and at least one root node should be |
| 693 | 694 |
///added with addSource() before using this function. |
| 694 | 695 |
/// |
| 695 | 696 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
| 696 | 697 |
///\code |
| 697 | 698 |
/// while ( !d.emptyQueue() ) {
|
| 698 | 699 |
/// d.processNextNode(); |
| 699 | 700 |
/// } |
| 700 | 701 |
///\endcode |
| 701 | 702 |
void start() |
| 702 | 703 |
{
|
| 703 | 704 |
while ( !emptyQueue() ) processNextNode(); |
| 704 | 705 |
} |
| 705 | 706 |
|
| 706 | 707 |
///Executes the algorithm until the given target node is processed. |
| 707 | 708 |
|
| 708 | 709 |
///Executes the algorithm until the given target node is processed. |
| 709 | 710 |
/// |
| 710 | 711 |
///This method runs the %Dijkstra algorithm from the root node(s) |
| 711 | 712 |
///in order to compute the shortest path to \c t. |
| 712 | 713 |
/// |
| 713 | 714 |
///The algorithm computes |
| 714 | 715 |
///- the shortest path to \c t, |
| 715 | 716 |
///- the distance of \c t from the root(s). |
| 716 | 717 |
/// |
| 717 | 718 |
///\pre init() must be called and at least one root node should be |
| 718 | 719 |
///added with addSource() before using this function. |
| 719 | 720 |
void start(Node t) |
| 720 | 721 |
{
|
| 721 | 722 |
while ( !_heap->empty() && _heap->top()!=t ) processNextNode(); |
| 722 | 723 |
if ( !_heap->empty() ) {
|
| 723 | 724 |
finalizeNodeData(_heap->top(),_heap->prio()); |
| 724 | 725 |
_heap->pop(); |
| 725 | 726 |
} |
| 726 | 727 |
} |
| 727 | 728 |
|
| 728 | 729 |
///Executes the algorithm until a condition is met. |
| 729 | 730 |
|
| 730 | 731 |
///Executes the algorithm until a condition is met. |
| 731 | 732 |
/// |
| 732 | 733 |
///This method runs the %Dijkstra algorithm from the root node(s) in |
| 733 | 734 |
///order to compute the shortest path to a node \c v with |
| 734 | 735 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
| 735 | 736 |
/// |
| 736 | 737 |
///\param nm A \c bool (or convertible) node map. The algorithm |
| 737 | 738 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
| 738 | 739 |
/// |
| 739 | 740 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
| 740 | 741 |
///\c INVALID if no such node was found. |
| 741 | 742 |
/// |
| 742 | 743 |
///\pre init() must be called and at least one root node should be |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_PREFLOW_H |
| 20 | 20 |
#define LEMON_PREFLOW_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
|
| 25 | 25 |
/// \file |
| 26 | 26 |
/// \ingroup max_flow |
| 27 | 27 |
/// \brief Implementation of the preflow algorithm. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
/// \brief Default traits class of Preflow class. |
| 32 | 32 |
/// |
| 33 | 33 |
/// Default traits class of Preflow class. |
| 34 |
/// \tparam _Digraph Digraph type. |
|
| 35 |
/// \tparam _CapacityMap Capacity map type. |
|
| 36 |
|
|
| 34 |
/// \tparam GR Digraph type. |
|
| 35 |
/// \tparam CM Capacity map type. |
|
| 36 |
template <typename GR, typename CM> |
|
| 37 | 37 |
struct PreflowDefaultTraits {
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
| 40 |
typedef |
|
| 40 |
typedef GR Digraph; |
|
| 41 | 41 |
|
| 42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
| 43 | 43 |
/// |
| 44 | 44 |
/// The type of the map that stores the arc capacities. |
| 45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 46 |
typedef |
|
| 46 |
typedef CM CapacityMap; |
|
| 47 | 47 |
|
| 48 | 48 |
/// \brief The type of the flow values. |
| 49 | 49 |
typedef typename CapacityMap::Value Value; |
| 50 | 50 |
|
| 51 | 51 |
/// \brief The type of the map that stores the flow values. |
| 52 | 52 |
/// |
| 53 | 53 |
/// The type of the map that stores the flow values. |
| 54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 55 | 55 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief Instantiates a FlowMap. |
| 58 | 58 |
/// |
| 59 | 59 |
/// This function instantiates a \ref FlowMap. |
| 60 | 60 |
/// \param digraph The digraph, to which we would like to define |
| 61 | 61 |
/// the flow map. |
| 62 | 62 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 63 | 63 |
return new FlowMap(digraph); |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 | 66 |
/// \brief The elevator type used by Preflow algorithm. |
| 67 | 67 |
/// |
| 68 | 68 |
/// The elevator type used by Preflow algorithm. |
| 69 | 69 |
/// |
| 70 | 70 |
/// \sa Elevator |
| 71 | 71 |
/// \sa LinkedElevator |
| 72 | 72 |
typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator; |
| 73 | 73 |
|
| 74 | 74 |
/// \brief Instantiates an Elevator. |
| 75 | 75 |
/// |
| 76 | 76 |
/// This function instantiates an \ref Elevator. |
| 77 | 77 |
/// \param digraph The digraph, to which we would like to define |
| 78 | 78 |
/// the elevator. |
| 79 | 79 |
/// \param max_level The maximum level of the elevator. |
| 80 | 80 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 81 | 81 |
return new Elevator(digraph, max_level); |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
/// \brief The tolerance used by the algorithm |
| 85 | 85 |
/// |
| 86 | 86 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 87 | 87 |
typedef lemon::Tolerance<Value> Tolerance; |
| 88 | 88 |
|
| 89 | 89 |
}; |
| 90 | 90 |
|
| 91 | 91 |
|
| 92 | 92 |
/// \ingroup max_flow |
| 93 | 93 |
/// |
| 94 | 94 |
/// \brief %Preflow algorithm class. |
| 95 | 95 |
/// |
| 96 | 96 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
| 97 | 97 |
/// \e push-relabel algorithm producing a flow of maximum value in a |
| 98 | 98 |
/// digraph. The preflow algorithms are the fastest known maximum |
| 99 | 99 |
/// flow algorithms. The current implementation use a mixture of the |
| 100 | 100 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
| 101 | 101 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
|
| 102 | 102 |
/// |
| 103 | 103 |
/// The algorithm consists of two phases. After the first phase |
| 104 | 104 |
/// the maximum flow value and the minimum cut is obtained. The |
| 105 | 105 |
/// second phase constructs a feasible maximum flow on each arc. |
| 106 | 106 |
/// |
| 107 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
| 108 |
/// \tparam _CapacityMap The type of the capacity map. The default map |
|
| 109 |
/// type |
|
| 107 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
| 108 |
/// \tparam CM The type of the capacity map. The default map |
|
| 109 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
| 110 | 110 |
#ifdef DOXYGEN |
| 111 |
template <typename |
|
| 111 |
template <typename GR, typename CM, typename TR> |
|
| 112 | 112 |
#else |
| 113 |
template <typename _Digraph, |
|
| 114 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
| 115 |
|
|
| 113 |
template <typename GR, |
|
| 114 |
typename CM = typename GR::template ArcMap<int>, |
|
| 115 |
typename TR = PreflowDefaultTraits<GR, CM> > |
|
| 116 | 116 |
#endif |
| 117 | 117 |
class Preflow {
|
| 118 | 118 |
public: |
| 119 | 119 |
|
| 120 | 120 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
| 121 |
typedef |
|
| 121 |
typedef TR Traits; |
|
| 122 | 122 |
///The type of the digraph the algorithm runs on. |
| 123 | 123 |
typedef typename Traits::Digraph Digraph; |
| 124 | 124 |
///The type of the capacity map. |
| 125 | 125 |
typedef typename Traits::CapacityMap CapacityMap; |
| 126 | 126 |
///The type of the flow values. |
| 127 | 127 |
typedef typename Traits::Value Value; |
| 128 | 128 |
|
| 129 | 129 |
///The type of the flow map. |
| 130 | 130 |
typedef typename Traits::FlowMap FlowMap; |
| 131 | 131 |
///The type of the elevator. |
| 132 | 132 |
typedef typename Traits::Elevator Elevator; |
| 133 | 133 |
///The type of the tolerance. |
| 134 | 134 |
typedef typename Traits::Tolerance Tolerance; |
| 135 | 135 |
|
| 136 | 136 |
private: |
| 137 | 137 |
|
| 138 | 138 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 139 | 139 |
|
| 140 | 140 |
const Digraph& _graph; |
| 141 | 141 |
const CapacityMap* _capacity; |
| 142 | 142 |
|
| 143 | 143 |
int _node_num; |
| 144 | 144 |
|
| 145 | 145 |
Node _source, _target; |
| 146 | 146 |
|
| 147 | 147 |
FlowMap* _flow; |
| 148 | 148 |
bool _local_flow; |
| 149 | 149 |
|
| 150 | 150 |
Elevator* _level; |
| 151 | 151 |
bool _local_level; |
| 152 | 152 |
|
| 153 | 153 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 154 | 154 |
ExcessMap* _excess; |
| 155 | 155 |
|
| 156 | 156 |
Tolerance _tolerance; |
| 157 | 157 |
|
| 158 | 158 |
bool _phase; |
| 159 | 159 |
|
| 160 | 160 |
|
| 161 | 161 |
void createStructures() {
|
| 162 | 162 |
_node_num = countNodes(_graph); |
| 163 | 163 |
|
| 164 | 164 |
if (!_flow) {
|
| 165 | 165 |
_flow = Traits::createFlowMap(_graph); |
| 166 | 166 |
_local_flow = true; |
| 167 | 167 |
} |
| 168 | 168 |
if (!_level) {
|
| 169 | 169 |
_level = Traits::createElevator(_graph, _node_num); |
| 170 | 170 |
_local_level = true; |
| 171 | 171 |
} |
| 172 | 172 |
if (!_excess) {
|
| 173 | 173 |
_excess = new ExcessMap(_graph); |
| 174 | 174 |
} |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
void destroyStructures() {
|
| 178 | 178 |
if (_local_flow) {
|
| 179 | 179 |
delete _flow; |
| 180 | 180 |
} |
| 181 | 181 |
if (_local_level) {
|
| 182 | 182 |
delete _level; |
| 183 | 183 |
} |
| 184 | 184 |
if (_excess) {
|
| 185 | 185 |
delete _excess; |
| 186 | 186 |
} |
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 | 189 |
public: |
| 190 | 190 |
|
| 191 | 191 |
typedef Preflow Create; |
| 192 | 192 |
|
| 193 | 193 |
///\name Named Template Parameters |
| 194 | 194 |
|
| 195 | 195 |
///@{
|
| 196 | 196 |
|
| 197 | 197 |
template <typename _FlowMap> |
| 198 | 198 |
struct SetFlowMapTraits : public Traits {
|
| 199 | 199 |
typedef _FlowMap FlowMap; |
| 200 | 200 |
static FlowMap *createFlowMap(const Digraph&) {
|
| 201 | 201 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
| 202 | 202 |
return 0; // ignore warnings |
| 203 | 203 |
} |
| 204 | 204 |
}; |
| 205 | 205 |
|
| 206 | 206 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 207 | 207 |
/// FlowMap type |
| 208 | 208 |
/// |
| 209 | 209 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
| 210 | 210 |
/// type. |
| 211 | 211 |
template <typename _FlowMap> |
| 212 | 212 |
struct SetFlowMap |
| 213 | 213 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<_FlowMap> > {
|
| 214 | 214 |
typedef Preflow<Digraph, CapacityMap, |
| 215 | 215 |
SetFlowMapTraits<_FlowMap> > Create; |
| 216 | 216 |
}; |
| 217 | 217 |
|
0 comments (0 inline)