0
7
0
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-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_ADAPTORS_H |
| 20 | 20 |
#define LEMON_ADAPTORS_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup graph_adaptors |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Adaptor classes for digraphs and graphs |
| 25 | 25 |
/// |
| 26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/maps.h> |
| 30 | 30 |
#include <lemon/bits/variant.h> |
| 31 | 31 |
|
| 32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
| 33 |
#include <lemon/bits/map_extender.h> |
|
| 33 | 34 |
#include <lemon/tolerance.h> |
| 34 | 35 |
|
| 35 | 36 |
#include <algorithm> |
| 36 | 37 |
|
| 37 | 38 |
namespace lemon {
|
| 38 | 39 |
|
| 39 | 40 |
#ifdef _MSC_VER |
| 40 | 41 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED |
| 41 | 42 |
#else |
| 42 | 43 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED |
| 43 | 44 |
#endif |
| 44 | 45 |
|
| 45 | 46 |
template<typename DGR> |
| 46 | 47 |
class DigraphAdaptorBase {
|
| 47 | 48 |
public: |
| 48 | 49 |
typedef DGR Digraph; |
| 49 | 50 |
typedef DigraphAdaptorBase Adaptor; |
| 50 | 51 |
|
| 51 | 52 |
protected: |
| 52 | 53 |
DGR* _digraph; |
| 53 | 54 |
DigraphAdaptorBase() : _digraph(0) { }
|
| 54 | 55 |
void initialize(DGR& digraph) { _digraph = &digraph; }
|
| 55 | 56 |
|
| 56 | 57 |
public: |
| 57 | 58 |
DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { }
|
| 58 | 59 |
|
| 59 | 60 |
typedef typename DGR::Node Node; |
| 60 | 61 |
typedef typename DGR::Arc Arc; |
| 61 | 62 |
|
| 62 | 63 |
void first(Node& i) const { _digraph->first(i); }
|
| 63 | 64 |
void first(Arc& i) const { _digraph->first(i); }
|
| 64 | 65 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
|
| 65 | 66 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
|
| 66 | 67 |
|
| 67 | 68 |
void next(Node& i) const { _digraph->next(i); }
|
| 68 | 69 |
void next(Arc& i) const { _digraph->next(i); }
|
| 69 | 70 |
void nextIn(Arc& i) const { _digraph->nextIn(i); }
|
| 70 | 71 |
void nextOut(Arc& i) const { _digraph->nextOut(i); }
|
| 71 | 72 |
|
| 72 | 73 |
Node source(const Arc& a) const { return _digraph->source(a); }
|
| 73 | 74 |
Node target(const Arc& a) const { return _digraph->target(a); }
|
| 74 | 75 |
|
| 75 | 76 |
typedef NodeNumTagIndicator<DGR> NodeNumTag; |
| 76 | 77 |
int nodeNum() const { return _digraph->nodeNum(); }
|
| 77 | 78 |
|
| 78 | 79 |
typedef ArcNumTagIndicator<DGR> ArcNumTag; |
| 79 | 80 |
int arcNum() const { return _digraph->arcNum(); }
|
| 80 | 81 |
|
| 81 | 82 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 82 | 83 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const {
|
| 83 | 84 |
return _digraph->findArc(u, v, prev); |
| 84 | 85 |
} |
| 85 | 86 |
|
| 86 | 87 |
Node addNode() { return _digraph->addNode(); }
|
| 87 | 88 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
|
| 88 | 89 |
|
| 89 | 90 |
void erase(const Node& n) { _digraph->erase(n); }
|
| 90 | 91 |
void erase(const Arc& a) { _digraph->erase(a); }
|
| 91 | 92 |
|
| 92 | 93 |
void clear() { _digraph->clear(); }
|
| 93 | 94 |
|
| 94 | 95 |
int id(const Node& n) const { return _digraph->id(n); }
|
| 95 | 96 |
int id(const Arc& a) const { return _digraph->id(a); }
|
| 96 | 97 |
|
| 97 | 98 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
|
| 98 | 99 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
|
| 99 | 100 |
|
| 100 | 101 |
int maxNodeId() const { return _digraph->maxNodeId(); }
|
| 101 | 102 |
int maxArcId() const { return _digraph->maxArcId(); }
|
| 102 | 103 |
|
| 103 | 104 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
| 104 | 105 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
|
| 105 | 106 |
|
| 106 | 107 |
typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier; |
| 107 | 108 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
|
| 108 | 109 |
|
| 109 | 110 |
template <typename V> |
| 110 | 111 |
class NodeMap : public DGR::template NodeMap<V> {
|
| 111 | 112 |
public: |
| 112 | 113 |
|
| 113 | 114 |
typedef typename DGR::template NodeMap<V> Parent; |
| 114 | 115 |
|
| 115 | 116 |
explicit NodeMap(const Adaptor& adaptor) |
| 116 | 117 |
: Parent(*adaptor._digraph) {}
|
| 117 | 118 |
|
| 118 | 119 |
NodeMap(const Adaptor& adaptor, const V& value) |
| 119 | 120 |
: Parent(*adaptor._digraph, value) { }
|
| 120 | 121 |
|
| 121 | 122 |
private: |
| 122 | 123 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 123 | 124 |
return operator=<NodeMap>(cmap); |
| 124 | 125 |
} |
| 125 | 126 |
|
| 126 | 127 |
template <typename CMap> |
| 127 | 128 |
NodeMap& operator=(const CMap& cmap) {
|
| 128 | 129 |
Parent::operator=(cmap); |
| 129 | 130 |
return *this; |
| 130 | 131 |
} |
| 131 | 132 |
|
| 132 | 133 |
}; |
| 133 | 134 |
|
| 134 | 135 |
template <typename V> |
| 135 | 136 |
class ArcMap : public DGR::template ArcMap<V> {
|
| 136 | 137 |
public: |
| 137 | 138 |
|
| 138 | 139 |
typedef typename DGR::template ArcMap<V> Parent; |
| 139 | 140 |
|
| 140 | 141 |
explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor) |
| 141 | 142 |
: Parent(*adaptor._digraph) {}
|
| 142 | 143 |
|
| 143 | 144 |
ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value) |
| 144 | 145 |
: Parent(*adaptor._digraph, value) {}
|
| 145 | 146 |
|
| 146 | 147 |
private: |
| 147 | 148 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 148 | 149 |
return operator=<ArcMap>(cmap); |
| 149 | 150 |
} |
| 150 | 151 |
|
| 151 | 152 |
template <typename CMap> |
| 152 | 153 |
ArcMap& operator=(const CMap& cmap) {
|
| 153 | 154 |
Parent::operator=(cmap); |
| 154 | 155 |
return *this; |
| 155 | 156 |
} |
| 156 | 157 |
|
| 157 | 158 |
}; |
| 158 | 159 |
|
| 159 | 160 |
}; |
| 160 | 161 |
|
| 161 | 162 |
template<typename GR> |
| 162 | 163 |
class GraphAdaptorBase {
|
| 163 | 164 |
public: |
| 164 | 165 |
typedef GR Graph; |
| 165 | 166 |
|
| 166 | 167 |
protected: |
| 167 | 168 |
GR* _graph; |
| 168 | 169 |
|
| 169 | 170 |
GraphAdaptorBase() : _graph(0) {}
|
| 170 | 171 |
|
| 171 | 172 |
void initialize(GR& graph) { _graph = &graph; }
|
| 172 | 173 |
|
| 173 | 174 |
public: |
| 174 | 175 |
GraphAdaptorBase(GR& graph) : _graph(&graph) {}
|
| 175 | 176 |
|
| 176 | 177 |
typedef typename GR::Node Node; |
| 177 | 178 |
typedef typename GR::Arc Arc; |
| 178 | 179 |
typedef typename GR::Edge Edge; |
| 179 | 180 |
|
| 180 | 181 |
void first(Node& i) const { _graph->first(i); }
|
| 181 | 182 |
void first(Arc& i) const { _graph->first(i); }
|
| 182 | 183 |
void first(Edge& i) const { _graph->first(i); }
|
| 183 | 184 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
|
| 184 | 185 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
|
| 185 | 186 |
void firstInc(Edge &i, bool &d, const Node &n) const {
|
| 186 | 187 |
_graph->firstInc(i, d, n); |
| 187 | 188 |
} |
| 188 | 189 |
|
| 189 | 190 |
void next(Node& i) const { _graph->next(i); }
|
| 190 | 191 |
void next(Arc& i) const { _graph->next(i); }
|
| 191 | 192 |
void next(Edge& i) const { _graph->next(i); }
|
| 192 | 193 |
void nextIn(Arc& i) const { _graph->nextIn(i); }
|
| 193 | 194 |
void nextOut(Arc& i) const { _graph->nextOut(i); }
|
| 194 | 195 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
|
| 195 | 196 |
|
| 196 | 197 |
Node u(const Edge& e) const { return _graph->u(e); }
|
| 197 | 198 |
Node v(const Edge& e) const { return _graph->v(e); }
|
| 198 | 199 |
|
| 199 | 200 |
Node source(const Arc& a) const { return _graph->source(a); }
|
| 200 | 201 |
Node target(const Arc& a) const { return _graph->target(a); }
|
| 201 | 202 |
|
| 202 | 203 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
| 203 | 204 |
int nodeNum() const { return _graph->nodeNum(); }
|
| 204 | 205 |
|
| 205 | 206 |
typedef ArcNumTagIndicator<Graph> ArcNumTag; |
| 206 | 207 |
int arcNum() const { return _graph->arcNum(); }
|
| 207 | 208 |
|
| 208 | 209 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
| 209 | 210 |
int edgeNum() const { return _graph->edgeNum(); }
|
| 210 | 211 |
|
| 211 | 212 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
| 212 | 213 |
Arc findArc(const Node& u, const Node& v, |
| 213 | 214 |
const Arc& prev = INVALID) const {
|
| 214 | 215 |
return _graph->findArc(u, v, prev); |
| 215 | 216 |
} |
| 216 | 217 |
|
| 217 | 218 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 218 | 219 |
Edge findEdge(const Node& u, const Node& v, |
| 219 | 220 |
const Edge& prev = INVALID) const {
|
| 220 | 221 |
return _graph->findEdge(u, v, prev); |
| 221 | 222 |
} |
| 222 | 223 |
|
| 223 | 224 |
Node addNode() { return _graph->addNode(); }
|
| 224 | 225 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
|
| 225 | 226 |
|
| 226 | 227 |
void erase(const Node& i) { _graph->erase(i); }
|
| 227 | 228 |
void erase(const Edge& i) { _graph->erase(i); }
|
| 228 | 229 |
|
| 229 | 230 |
void clear() { _graph->clear(); }
|
| 230 | 231 |
|
| 231 | 232 |
bool direction(const Arc& a) const { return _graph->direction(a); }
|
| 232 | 233 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
|
| 233 | 234 |
|
| 234 | 235 |
int id(const Node& v) const { return _graph->id(v); }
|
| 235 | 236 |
int id(const Arc& a) const { return _graph->id(a); }
|
| 236 | 237 |
int id(const Edge& e) const { return _graph->id(e); }
|
| 237 | 238 |
|
| 238 | 239 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
|
| 239 | 240 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
|
| 240 | 241 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
|
| 241 | 242 |
|
| 242 | 243 |
int maxNodeId() const { return _graph->maxNodeId(); }
|
| 243 | 244 |
int maxArcId() const { return _graph->maxArcId(); }
|
| 244 | 245 |
int maxEdgeId() const { return _graph->maxEdgeId(); }
|
| 245 | 246 |
|
| 246 | 247 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
| 247 | 248 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
|
| 248 | 249 |
|
| 249 | 250 |
typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
| 250 | 251 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
|
| 251 | 252 |
|
| 252 | 253 |
typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier; |
| 253 | 254 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
|
| 254 | 255 |
|
| 255 | 256 |
template <typename V> |
| 256 | 257 |
class NodeMap : public GR::template NodeMap<V> {
|
| 257 | 258 |
public: |
| 258 | 259 |
typedef typename GR::template NodeMap<V> Parent; |
| 259 | 260 |
explicit NodeMap(const GraphAdaptorBase<GR>& adapter) |
| 260 | 261 |
: Parent(*adapter._graph) {}
|
| 261 | 262 |
NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
| 262 | 263 |
: Parent(*adapter._graph, value) {}
|
| 263 | 264 |
|
| 264 | 265 |
private: |
| 265 | 266 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 266 | 267 |
return operator=<NodeMap>(cmap); |
| 267 | 268 |
} |
| 268 | 269 |
|
| 269 | 270 |
template <typename CMap> |
| 270 | 271 |
NodeMap& operator=(const CMap& cmap) {
|
| 271 | 272 |
Parent::operator=(cmap); |
| 272 | 273 |
return *this; |
| 273 | 274 |
} |
| 274 | 275 |
|
| 275 | 276 |
}; |
| 276 | 277 |
|
| 277 | 278 |
template <typename V> |
| 278 | 279 |
class ArcMap : public GR::template ArcMap<V> {
|
| 279 | 280 |
public: |
| 280 | 281 |
typedef typename GR::template ArcMap<V> Parent; |
| 281 | 282 |
explicit ArcMap(const GraphAdaptorBase<GR>& adapter) |
| 282 | 283 |
: Parent(*adapter._graph) {}
|
| 283 | 284 |
ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
| 284 | 285 |
: Parent(*adapter._graph, value) {}
|
| 285 | 286 |
|
| 286 | 287 |
private: |
| 287 | 288 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 288 | 289 |
return operator=<ArcMap>(cmap); |
| 289 | 290 |
} |
| 290 | 291 |
|
| 291 | 292 |
template <typename CMap> |
| 292 | 293 |
ArcMap& operator=(const CMap& cmap) {
|
| 293 | 294 |
Parent::operator=(cmap); |
| 294 | 295 |
return *this; |
| 295 | 296 |
} |
| 296 | 297 |
}; |
| 297 | 298 |
|
| 298 | 299 |
template <typename V> |
| 299 | 300 |
class EdgeMap : public GR::template EdgeMap<V> {
|
| 300 | 301 |
public: |
| 301 | 302 |
typedef typename GR::template EdgeMap<V> Parent; |
| 302 | 303 |
explicit EdgeMap(const GraphAdaptorBase<GR>& adapter) |
| 303 | 304 |
: Parent(*adapter._graph) {}
|
| 304 | 305 |
EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
| 305 | 306 |
: Parent(*adapter._graph, value) {}
|
| 306 | 307 |
|
| 307 | 308 |
private: |
| 308 | 309 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 309 | 310 |
return operator=<EdgeMap>(cmap); |
| 310 | 311 |
} |
| 311 | 312 |
|
| 312 | 313 |
template <typename CMap> |
| 313 | 314 |
EdgeMap& operator=(const CMap& cmap) {
|
| 314 | 315 |
Parent::operator=(cmap); |
| 315 | 316 |
return *this; |
| 316 | 317 |
} |
| 317 | 318 |
}; |
| 318 | 319 |
|
| 319 | 320 |
}; |
| 320 | 321 |
|
| 321 | 322 |
template <typename DGR> |
| 322 | 323 |
class ReverseDigraphBase : public DigraphAdaptorBase<DGR> {
|
| 323 | 324 |
public: |
| 324 | 325 |
typedef DGR Digraph; |
| 325 | 326 |
typedef DigraphAdaptorBase<DGR> Parent; |
| 326 | 327 |
protected: |
| 327 | 328 |
ReverseDigraphBase() : Parent() { }
|
| 328 | 329 |
public: |
| 329 | 330 |
typedef typename Parent::Node Node; |
| 330 | 331 |
typedef typename Parent::Arc Arc; |
| 331 | 332 |
|
| 332 | 333 |
void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
|
| 333 | 334 |
void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
|
| 334 | 335 |
|
| 335 | 336 |
void nextIn(Arc& a) const { Parent::nextOut(a); }
|
| 336 | 337 |
void nextOut(Arc& a) const { Parent::nextIn(a); }
|
| 337 | 338 |
|
| 338 | 339 |
Node source(const Arc& a) const { return Parent::target(a); }
|
| 339 | 340 |
Node target(const Arc& a) const { return Parent::source(a); }
|
| 340 | 341 |
|
| 341 | 342 |
Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
|
| 342 | 343 |
|
| 343 | 344 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 344 | 345 |
Arc findArc(const Node& u, const Node& v, |
| 345 | 346 |
const Arc& prev = INVALID) const {
|
| 346 | 347 |
return Parent::findArc(v, u, prev); |
| 347 | 348 |
} |
| 348 | 349 |
|
| 349 | 350 |
}; |
| 350 | 351 |
|
| 351 | 352 |
/// \ingroup graph_adaptors |
| 352 | 353 |
/// |
| 353 | 354 |
/// \brief Adaptor class for reversing the orientation of the arcs in |
| 354 | 355 |
/// a digraph. |
| 355 | 356 |
/// |
| 356 | 357 |
/// ReverseDigraph can be used for reversing the arcs in a digraph. |
| 357 | 358 |
/// It conforms to the \ref concepts::Digraph "Digraph" concept. |
| 358 | 359 |
/// |
| 359 | 360 |
/// The adapted digraph can also be modified through this adaptor |
| 360 | 361 |
/// by adding or removing nodes or arcs, unless the \c GR template |
| 361 | 362 |
/// parameter is set to be \c const. |
| 362 | 363 |
/// |
| 363 | 364 |
/// \tparam DGR The type of the adapted digraph. |
| 364 | 365 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
| 365 | 366 |
/// It can also be specified to be \c const. |
| 366 | 367 |
/// |
| 367 | 368 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
| 368 | 369 |
/// digraph are convertible to each other. |
| 369 | 370 |
template<typename DGR> |
| 370 | 371 |
#ifdef DOXYGEN |
| 371 | 372 |
class ReverseDigraph {
|
| 372 | 373 |
#else |
| 373 | 374 |
class ReverseDigraph : |
| 374 | 375 |
public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > {
|
| 375 | 376 |
#endif |
| 376 | 377 |
public: |
| 377 | 378 |
/// The type of the adapted digraph. |
| 378 | 379 |
typedef DGR Digraph; |
| 379 | 380 |
typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent; |
| 380 | 381 |
protected: |
| 381 | 382 |
ReverseDigraph() { }
|
| 382 | 383 |
public: |
| 383 | 384 |
|
| 384 | 385 |
/// \brief Constructor |
| 385 | 386 |
/// |
| 386 | 387 |
/// Creates a reverse digraph adaptor for the given digraph. |
| 387 | 388 |
explicit ReverseDigraph(DGR& digraph) {
|
| 388 | 389 |
Parent::initialize(digraph); |
| 389 | 390 |
} |
| 390 | 391 |
}; |
| 391 | 392 |
|
| 392 | 393 |
/// \brief Returns a read-only ReverseDigraph adaptor |
| 393 | 394 |
/// |
| 394 | 395 |
/// This function just returns a read-only \ref ReverseDigraph adaptor. |
| 395 | 396 |
/// \ingroup graph_adaptors |
| 396 | 397 |
/// \relates ReverseDigraph |
| 397 | 398 |
template<typename DGR> |
| 398 | 399 |
ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) {
|
| 399 | 400 |
return ReverseDigraph<const DGR>(digraph); |
| 400 | 401 |
} |
| 401 | 402 |
|
| 402 | 403 |
|
| 403 | 404 |
template <typename DGR, typename NF, typename AF, bool ch = true> |
| 404 | 405 |
class SubDigraphBase : public DigraphAdaptorBase<DGR> {
|
| 405 | 406 |
public: |
| 406 | 407 |
typedef DGR Digraph; |
| 407 | 408 |
typedef NF NodeFilterMap; |
| 408 | 409 |
typedef AF ArcFilterMap; |
| 409 | 410 |
|
| 410 | 411 |
typedef SubDigraphBase Adaptor; |
| 411 | 412 |
typedef DigraphAdaptorBase<DGR> Parent; |
| 412 | 413 |
protected: |
| 413 | 414 |
NF* _node_filter; |
| 414 | 415 |
AF* _arc_filter; |
| 415 | 416 |
SubDigraphBase() |
| 416 | 417 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 417 | 418 |
|
| 418 | 419 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
|
| 419 | 420 |
Parent::initialize(digraph); |
| 420 | 421 |
_node_filter = &node_filter; |
| 421 | 422 |
_arc_filter = &arc_filter; |
| 422 | 423 |
} |
| 423 | 424 |
|
| 424 | 425 |
public: |
| 425 | 426 |
|
| 426 | 427 |
typedef typename Parent::Node Node; |
| 427 | 428 |
typedef typename Parent::Arc Arc; |
| 428 | 429 |
|
| 429 | 430 |
void first(Node& i) const {
|
| 430 | 431 |
Parent::first(i); |
| 431 | 432 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 432 | 433 |
} |
| 433 | 434 |
|
| 434 | 435 |
void first(Arc& i) const {
|
| 435 | 436 |
Parent::first(i); |
| 436 | 437 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 437 | 438 |
|| !(*_node_filter)[Parent::source(i)] |
| 438 | 439 |
|| !(*_node_filter)[Parent::target(i)])) |
| 439 | 440 |
Parent::next(i); |
| 440 | 441 |
} |
| 441 | 442 |
|
| 442 | 443 |
void firstIn(Arc& i, const Node& n) const {
|
| 443 | 444 |
Parent::firstIn(i, n); |
| 444 | 445 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 445 | 446 |
|| !(*_node_filter)[Parent::source(i)])) |
| 446 | 447 |
Parent::nextIn(i); |
| 447 | 448 |
} |
| 448 | 449 |
|
| 449 | 450 |
void firstOut(Arc& i, const Node& n) const {
|
| 450 | 451 |
Parent::firstOut(i, n); |
| 451 | 452 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 452 | 453 |
|| !(*_node_filter)[Parent::target(i)])) |
| 453 | 454 |
Parent::nextOut(i); |
| 454 | 455 |
} |
| 455 | 456 |
|
| 456 | 457 |
void next(Node& i) const {
|
| 457 | 458 |
Parent::next(i); |
| 458 | 459 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 459 | 460 |
} |
| 460 | 461 |
|
| 461 | 462 |
void next(Arc& i) const {
|
| 462 | 463 |
Parent::next(i); |
| 463 | 464 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 464 | 465 |
|| !(*_node_filter)[Parent::source(i)] |
| 465 | 466 |
|| !(*_node_filter)[Parent::target(i)])) |
| 466 | 467 |
Parent::next(i); |
| 467 | 468 |
} |
| 468 | 469 |
|
| 469 | 470 |
void nextIn(Arc& i) const {
|
| 470 | 471 |
Parent::nextIn(i); |
| 471 | 472 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 472 | 473 |
|| !(*_node_filter)[Parent::source(i)])) |
| 473 | 474 |
Parent::nextIn(i); |
| 474 | 475 |
} |
| 475 | 476 |
|
| 476 | 477 |
void nextOut(Arc& i) const {
|
| 477 | 478 |
Parent::nextOut(i); |
| 478 | 479 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 479 | 480 |
|| !(*_node_filter)[Parent::target(i)])) |
| 480 | 481 |
Parent::nextOut(i); |
| 481 | 482 |
} |
| 482 | 483 |
|
| 483 | 484 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); }
|
| 484 | 485 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
|
| 485 | 486 |
|
| 486 | 487 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 487 | 488 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; }
|
| 488 | 489 |
|
| 489 | 490 |
typedef False NodeNumTag; |
| 490 | 491 |
typedef False ArcNumTag; |
| 491 | 492 |
|
| 492 | 493 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 493 | 494 |
Arc findArc(const Node& source, const Node& target, |
| 494 | 495 |
const Arc& prev = INVALID) const {
|
| 495 | 496 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 496 | 497 |
return INVALID; |
| 497 | 498 |
} |
| 498 | 499 |
Arc arc = Parent::findArc(source, target, prev); |
| 499 | 500 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 500 | 501 |
arc = Parent::findArc(source, target, arc); |
| 501 | 502 |
} |
| 502 | 503 |
return arc; |
| 503 | 504 |
} |
| 504 | 505 |
|
| 505 | 506 |
public: |
| 506 | 507 |
|
| 507 | 508 |
template <typename V> |
| 508 | 509 |
class NodeMap |
| 509 | 510 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 510 | 511 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
|
| 511 | 512 |
public: |
| 512 | 513 |
typedef V Value; |
| 513 | 514 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 514 | 515 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
| 515 | 516 |
|
| 516 | 517 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
| 517 | 518 |
: Parent(adaptor) {}
|
| 518 | 519 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
| 519 | 520 |
: Parent(adaptor, value) {}
|
| 520 | 521 |
|
| 521 | 522 |
private: |
| 522 | 523 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 523 | 524 |
return operator=<NodeMap>(cmap); |
| 524 | 525 |
} |
| 525 | 526 |
|
| 526 | 527 |
template <typename CMap> |
| 527 | 528 |
NodeMap& operator=(const CMap& cmap) {
|
| 528 | 529 |
Parent::operator=(cmap); |
| 529 | 530 |
return *this; |
| 530 | 531 |
} |
| 531 | 532 |
}; |
| 532 | 533 |
|
| 533 | 534 |
template <typename V> |
| 534 | 535 |
class ArcMap |
| 535 | 536 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 536 | 537 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
|
| 537 | 538 |
public: |
| 538 | 539 |
typedef V Value; |
| 539 | 540 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 540 | 541 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
| 541 | 542 |
|
| 542 | 543 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
| 543 | 544 |
: Parent(adaptor) {}
|
| 544 | 545 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
| 545 | 546 |
: Parent(adaptor, value) {}
|
| 546 | 547 |
|
| 547 | 548 |
private: |
| 548 | 549 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 549 | 550 |
return operator=<ArcMap>(cmap); |
| 550 | 551 |
} |
| 551 | 552 |
|
| 552 | 553 |
template <typename CMap> |
| 553 | 554 |
ArcMap& operator=(const CMap& cmap) {
|
| 554 | 555 |
Parent::operator=(cmap); |
| 555 | 556 |
return *this; |
| 556 | 557 |
} |
| 557 | 558 |
}; |
| 558 | 559 |
|
| 559 | 560 |
}; |
| 560 | 561 |
|
| 561 | 562 |
template <typename DGR, typename NF, typename AF> |
| 562 | 563 |
class SubDigraphBase<DGR, NF, AF, false> |
| 563 | 564 |
: public DigraphAdaptorBase<DGR> {
|
| 564 | 565 |
public: |
| 565 | 566 |
typedef DGR Digraph; |
| 566 | 567 |
typedef NF NodeFilterMap; |
| 567 | 568 |
typedef AF ArcFilterMap; |
| 568 | 569 |
|
| 569 | 570 |
typedef SubDigraphBase Adaptor; |
| 570 | 571 |
typedef DigraphAdaptorBase<Digraph> Parent; |
| 571 | 572 |
protected: |
| 572 | 573 |
NF* _node_filter; |
| 573 | 574 |
AF* _arc_filter; |
| 574 | 575 |
SubDigraphBase() |
| 575 | 576 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 576 | 577 |
|
| 577 | 578 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
|
| 578 | 579 |
Parent::initialize(digraph); |
| 579 | 580 |
_node_filter = &node_filter; |
| 580 | 581 |
_arc_filter = &arc_filter; |
| 581 | 582 |
} |
| 582 | 583 |
|
| 583 | 584 |
public: |
| 584 | 585 |
|
| 585 | 586 |
typedef typename Parent::Node Node; |
| 586 | 587 |
typedef typename Parent::Arc Arc; |
| 587 | 588 |
|
| 588 | 589 |
void first(Node& i) const {
|
| 589 | 590 |
Parent::first(i); |
| 590 | 591 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 591 | 592 |
} |
| 592 | 593 |
|
| 593 | 594 |
void first(Arc& i) const {
|
| 594 | 595 |
Parent::first(i); |
| 595 | 596 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
| 596 | 597 |
} |
| 597 | 598 |
|
| 598 | 599 |
void firstIn(Arc& i, const Node& n) const {
|
| 599 | 600 |
Parent::firstIn(i, n); |
| 600 | 601 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
| 601 | 602 |
} |
| 602 | 603 |
|
| 603 | 604 |
void firstOut(Arc& i, const Node& n) const {
|
| 604 | 605 |
Parent::firstOut(i, n); |
| 605 | 606 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
| 606 | 607 |
} |
| 607 | 608 |
|
| 608 | 609 |
void next(Node& i) const {
|
| 609 | 610 |
Parent::next(i); |
| 610 | 611 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 611 | 612 |
} |
| 612 | 613 |
void next(Arc& i) const {
|
| 613 | 614 |
Parent::next(i); |
| 614 | 615 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
| 615 | 616 |
} |
| 616 | 617 |
void nextIn(Arc& i) const {
|
| 617 | 618 |
Parent::nextIn(i); |
| 618 | 619 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
| 619 | 620 |
} |
| 620 | 621 |
|
| 621 | 622 |
void nextOut(Arc& i) const {
|
| 622 | 623 |
Parent::nextOut(i); |
| 623 | 624 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
| 624 | 625 |
} |
| 625 | 626 |
|
| 626 | 627 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); }
|
| 627 | 628 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
|
| 628 | 629 |
|
| 629 | 630 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 630 | 631 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; }
|
| 631 | 632 |
|
| 632 | 633 |
typedef False NodeNumTag; |
| 633 | 634 |
typedef False ArcNumTag; |
| 634 | 635 |
|
| 635 | 636 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 636 | 637 |
Arc findArc(const Node& source, const Node& target, |
| 637 | 638 |
const Arc& prev = INVALID) const {
|
| 638 | 639 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 639 | 640 |
return INVALID; |
| 640 | 641 |
} |
| 641 | 642 |
Arc arc = Parent::findArc(source, target, prev); |
| 642 | 643 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 643 | 644 |
arc = Parent::findArc(source, target, arc); |
| 644 | 645 |
} |
| 645 | 646 |
return arc; |
| 646 | 647 |
} |
| 647 | 648 |
|
| 648 | 649 |
template <typename V> |
| 649 | 650 |
class NodeMap |
| 650 | 651 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 651 | 652 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
|
| 652 | 653 |
public: |
| 653 | 654 |
typedef V Value; |
| 654 | 655 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 655 | 656 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
| 656 | 657 |
|
| 657 | 658 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
| 658 | 659 |
: Parent(adaptor) {}
|
| 659 | 660 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
| 660 | 661 |
: Parent(adaptor, value) {}
|
| 661 | 662 |
|
| 662 | 663 |
private: |
| 663 | 664 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 664 | 665 |
return operator=<NodeMap>(cmap); |
| 665 | 666 |
} |
| 666 | 667 |
|
| 667 | 668 |
template <typename CMap> |
| 668 | 669 |
NodeMap& operator=(const CMap& cmap) {
|
| 669 | 670 |
Parent::operator=(cmap); |
| 670 | 671 |
return *this; |
| 671 | 672 |
} |
| 672 | 673 |
}; |
| 673 | 674 |
|
| 674 | 675 |
template <typename V> |
| 675 | 676 |
class ArcMap |
| 676 | 677 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 677 | 678 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
|
| 678 | 679 |
public: |
| 679 | 680 |
typedef V Value; |
| 680 | 681 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 681 | 682 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
| 682 | 683 |
|
| 683 | 684 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
| 684 | 685 |
: Parent(adaptor) {}
|
| 685 | 686 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
| 686 | 687 |
: Parent(adaptor, value) {}
|
| 687 | 688 |
|
| 688 | 689 |
private: |
| 689 | 690 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 690 | 691 |
return operator=<ArcMap>(cmap); |
| 691 | 692 |
} |
| 692 | 693 |
|
| 693 | 694 |
template <typename CMap> |
| 694 | 695 |
ArcMap& operator=(const CMap& cmap) {
|
| 695 | 696 |
Parent::operator=(cmap); |
| 696 | 697 |
return *this; |
| 697 | 698 |
} |
| 698 | 699 |
}; |
| 699 | 700 |
|
| 700 | 701 |
}; |
| 701 | 702 |
|
| 702 | 703 |
/// \ingroup graph_adaptors |
| 703 | 704 |
/// |
| 704 | 705 |
/// \brief Adaptor class for hiding nodes and arcs in a digraph |
| 705 | 706 |
/// |
| 706 | 707 |
/// SubDigraph can be used for hiding nodes and arcs in a digraph. |
| 707 | 708 |
/// A \c bool node map and a \c bool arc map must be specified, which |
| 708 | 709 |
/// define the filters for nodes and arcs. |
| 709 | 710 |
/// Only the nodes and arcs with \c true filter value are |
| 710 | 711 |
/// shown in the subdigraph. The arcs that are incident to hidden |
| 711 | 712 |
/// nodes are also filtered out. |
| 712 | 713 |
/// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept. |
| 713 | 714 |
/// |
| 714 | 715 |
/// The adapted digraph can also be modified through this adaptor |
| 715 | 716 |
/// by adding or removing nodes or arcs, unless the \c GR template |
| 716 | 717 |
/// parameter is set to be \c const. |
| 717 | 718 |
/// |
| 718 | 719 |
/// \tparam DGR The type of the adapted digraph. |
| 719 | 720 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
| 720 | 721 |
/// It can also be specified to be \c const. |
| 721 | 722 |
/// \tparam NF The type of the node filter map. |
| 722 | 723 |
/// It must be a \c bool (or convertible) node map of the |
| 723 | 724 |
/// adapted digraph. The default type is |
| 724 | 725 |
/// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
| 725 | 726 |
/// \tparam AF The type of the arc filter map. |
| 726 | 727 |
/// It must be \c bool (or convertible) arc map of the |
| 727 | 728 |
/// adapted digraph. The default type is |
| 728 | 729 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
| 729 | 730 |
/// |
| 730 | 731 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
| 731 | 732 |
/// digraph are convertible to each other. |
| 732 | 733 |
/// |
| 733 | 734 |
/// \see FilterNodes |
| 734 | 735 |
/// \see FilterArcs |
| 735 | 736 |
#ifdef DOXYGEN |
| 736 | 737 |
template<typename DGR, typename NF, typename AF> |
| 737 | 738 |
class SubDigraph {
|
| 738 | 739 |
#else |
| 739 | 740 |
template<typename DGR, |
| 740 | 741 |
typename NF = typename DGR::template NodeMap<bool>, |
| 741 | 742 |
typename AF = typename DGR::template ArcMap<bool> > |
| 742 | 743 |
class SubDigraph : |
| 743 | 744 |
public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > {
|
| 744 | 745 |
#endif |
| 745 | 746 |
public: |
| 746 | 747 |
/// The type of the adapted digraph. |
| 747 | 748 |
typedef DGR Digraph; |
| 748 | 749 |
/// The type of the node filter map. |
| 749 | 750 |
typedef NF NodeFilterMap; |
| 750 | 751 |
/// The type of the arc filter map. |
| 751 | 752 |
typedef AF ArcFilterMap; |
| 752 | 753 |
|
| 753 | 754 |
typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > |
| 754 | 755 |
Parent; |
| 755 | 756 |
|
| 756 | 757 |
typedef typename Parent::Node Node; |
| 757 | 758 |
typedef typename Parent::Arc Arc; |
| 758 | 759 |
|
| 759 | 760 |
protected: |
| 760 | 761 |
SubDigraph() { }
|
| 761 | 762 |
public: |
| 762 | 763 |
|
| 763 | 764 |
/// \brief Constructor |
| 764 | 765 |
/// |
| 765 | 766 |
/// Creates a subdigraph for the given digraph with the |
| 766 | 767 |
/// given node and arc filter maps. |
| 767 | 768 |
SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) {
|
| 768 | 769 |
Parent::initialize(digraph, node_filter, arc_filter); |
| 769 | 770 |
} |
| 770 | 771 |
|
| 771 | 772 |
/// \brief Sets the status of the given node |
| 772 | 773 |
/// |
| 773 | 774 |
/// This function sets the status of the given node. |
| 774 | 775 |
/// It is done by simply setting the assigned value of \c n |
| 775 | 776 |
/// to \c v in the node filter map. |
| 776 | 777 |
void status(const Node& n, bool v) const { Parent::status(n, v); }
|
| 777 | 778 |
|
| 778 | 779 |
/// \brief Sets the status of the given arc |
| 779 | 780 |
/// |
| 780 | 781 |
/// This function sets the status of the given arc. |
| 781 | 782 |
/// It is done by simply setting the assigned value of \c a |
| 782 | 783 |
/// to \c v in the arc filter map. |
| 783 | 784 |
void status(const Arc& a, bool v) const { Parent::status(a, v); }
|
| 784 | 785 |
|
| 785 | 786 |
/// \brief Returns the status of the given node |
| 786 | 787 |
/// |
| 787 | 788 |
/// This function returns the status of the given node. |
| 788 | 789 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
| 789 | 790 |
bool status(const Node& n) const { return Parent::status(n); }
|
| 790 | 791 |
|
| 791 | 792 |
/// \brief Returns the status of the given arc |
| 792 | 793 |
/// |
| 793 | 794 |
/// This function returns the status of the given arc. |
| 794 | 795 |
/// It is \c true if the given arc is enabled (i.e. not hidden). |
| 795 | 796 |
bool status(const Arc& a) const { return Parent::status(a); }
|
| 796 | 797 |
|
| 797 | 798 |
/// \brief Disables the given node |
| 798 | 799 |
/// |
| 799 | 800 |
/// This function disables the given node in the subdigraph, |
| 800 | 801 |
/// so the iteration jumps over it. |
| 801 | 802 |
/// It is the same as \ref status() "status(n, false)". |
| 802 | 803 |
void disable(const Node& n) const { Parent::status(n, false); }
|
| 803 | 804 |
|
| 804 | 805 |
/// \brief Disables the given arc |
| 805 | 806 |
/// |
| 806 | 807 |
/// This function disables the given arc in the subdigraph, |
| 807 | 808 |
/// so the iteration jumps over it. |
| 808 | 809 |
/// It is the same as \ref status() "status(a, false)". |
| 809 | 810 |
void disable(const Arc& a) const { Parent::status(a, false); }
|
| 810 | 811 |
|
| 811 | 812 |
/// \brief Enables the given node |
| 812 | 813 |
/// |
| 813 | 814 |
/// This function enables the given node in the subdigraph. |
| 814 | 815 |
/// It is the same as \ref status() "status(n, true)". |
| 815 | 816 |
void enable(const Node& n) const { Parent::status(n, true); }
|
| 816 | 817 |
|
| 817 | 818 |
/// \brief Enables the given arc |
| 818 | 819 |
/// |
| 819 | 820 |
/// This function enables the given arc in the subdigraph. |
| 820 | 821 |
/// It is the same as \ref status() "status(a, true)". |
| 821 | 822 |
void enable(const Arc& a) const { Parent::status(a, true); }
|
| 822 | 823 |
|
| 823 | 824 |
}; |
| 824 | 825 |
|
| 825 | 826 |
/// \brief Returns a read-only SubDigraph adaptor |
| 826 | 827 |
/// |
| 827 | 828 |
/// This function just returns a read-only \ref SubDigraph adaptor. |
| 828 | 829 |
/// \ingroup graph_adaptors |
| 829 | 830 |
/// \relates SubDigraph |
| 830 | 831 |
template<typename DGR, typename NF, typename AF> |
| 831 | 832 |
SubDigraph<const DGR, NF, AF> |
| 832 | 833 |
subDigraph(const DGR& digraph, |
| 833 | 834 |
NF& node_filter, AF& arc_filter) {
|
| 834 | 835 |
return SubDigraph<const DGR, NF, AF> |
| 835 | 836 |
(digraph, node_filter, arc_filter); |
| 836 | 837 |
} |
| 837 | 838 |
|
| 838 | 839 |
template<typename DGR, typename NF, typename AF> |
| 839 | 840 |
SubDigraph<const DGR, const NF, AF> |
| 840 | 841 |
subDigraph(const DGR& digraph, |
| 841 | 842 |
const NF& node_filter, AF& arc_filter) {
|
| 842 | 843 |
return SubDigraph<const DGR, const NF, AF> |
| 843 | 844 |
(digraph, node_filter, arc_filter); |
| 844 | 845 |
} |
| 845 | 846 |
|
| 846 | 847 |
template<typename DGR, typename NF, typename AF> |
| 847 | 848 |
SubDigraph<const DGR, NF, const AF> |
| 848 | 849 |
subDigraph(const DGR& digraph, |
| 849 | 850 |
NF& node_filter, const AF& arc_filter) {
|
| 850 | 851 |
return SubDigraph<const DGR, NF, const AF> |
| 851 | 852 |
(digraph, node_filter, arc_filter); |
| 852 | 853 |
} |
| 853 | 854 |
|
| 854 | 855 |
template<typename DGR, typename NF, typename AF> |
| 855 | 856 |
SubDigraph<const DGR, const NF, const AF> |
| 856 | 857 |
subDigraph(const DGR& digraph, |
| 857 | 858 |
const NF& node_filter, const AF& arc_filter) {
|
| 858 | 859 |
return SubDigraph<const DGR, const NF, const AF> |
| 859 | 860 |
(digraph, node_filter, arc_filter); |
| 860 | 861 |
} |
| 861 | 862 |
|
| 862 | 863 |
|
| 863 | 864 |
template <typename GR, typename NF, typename EF, bool ch = true> |
| 864 | 865 |
class SubGraphBase : public GraphAdaptorBase<GR> {
|
| 865 | 866 |
public: |
| 866 | 867 |
typedef GR Graph; |
| 867 | 868 |
typedef NF NodeFilterMap; |
| 868 | 869 |
typedef EF EdgeFilterMap; |
| 869 | 870 |
|
| 870 | 871 |
typedef SubGraphBase Adaptor; |
| 871 | 872 |
typedef GraphAdaptorBase<GR> Parent; |
| 872 | 873 |
protected: |
| 873 | 874 |
|
| 874 | 875 |
NF* _node_filter; |
| 875 | 876 |
EF* _edge_filter; |
| 876 | 877 |
|
| 877 | 878 |
SubGraphBase() |
| 878 | 879 |
: Parent(), _node_filter(0), _edge_filter(0) { }
|
| 879 | 880 |
|
| 880 | 881 |
void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
|
| 881 | 882 |
Parent::initialize(graph); |
| 882 | 883 |
_node_filter = &node_filter; |
| 883 | 884 |
_edge_filter = &edge_filter; |
| 884 | 885 |
} |
| 885 | 886 |
|
| 886 | 887 |
public: |
| 887 | 888 |
|
| 888 | 889 |
typedef typename Parent::Node Node; |
| 889 | 890 |
typedef typename Parent::Arc Arc; |
| 890 | 891 |
typedef typename Parent::Edge Edge; |
| 891 | 892 |
|
| 892 | 893 |
void first(Node& i) const {
|
| 893 | 894 |
Parent::first(i); |
| 894 | 895 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 895 | 896 |
} |
| 896 | 897 |
|
| 897 | 898 |
void first(Arc& i) const {
|
| 898 | 899 |
Parent::first(i); |
| 899 | 900 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 900 | 901 |
|| !(*_node_filter)[Parent::source(i)] |
| 901 | 902 |
|| !(*_node_filter)[Parent::target(i)])) |
| 902 | 903 |
Parent::next(i); |
| 903 | 904 |
} |
| 904 | 905 |
|
| 905 | 906 |
void first(Edge& i) const {
|
| 906 | 907 |
Parent::first(i); |
| 907 | 908 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 908 | 909 |
|| !(*_node_filter)[Parent::u(i)] |
| 909 | 910 |
|| !(*_node_filter)[Parent::v(i)])) |
| 910 | 911 |
Parent::next(i); |
| 911 | 912 |
} |
| 912 | 913 |
|
| 913 | 914 |
void firstIn(Arc& i, const Node& n) const {
|
| 914 | 915 |
Parent::firstIn(i, n); |
| 915 | 916 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 916 | 917 |
|| !(*_node_filter)[Parent::source(i)])) |
| 917 | 918 |
Parent::nextIn(i); |
| 918 | 919 |
} |
| 919 | 920 |
|
| 920 | 921 |
void firstOut(Arc& i, const Node& n) const {
|
| 921 | 922 |
Parent::firstOut(i, n); |
| 922 | 923 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 923 | 924 |
|| !(*_node_filter)[Parent::target(i)])) |
| 924 | 925 |
Parent::nextOut(i); |
| 925 | 926 |
} |
| 926 | 927 |
|
| 927 | 928 |
void firstInc(Edge& i, bool& d, const Node& n) const {
|
| 928 | 929 |
Parent::firstInc(i, d, n); |
| 929 | 930 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 930 | 931 |
|| !(*_node_filter)[Parent::u(i)] |
| 931 | 932 |
|| !(*_node_filter)[Parent::v(i)])) |
| 932 | 933 |
Parent::nextInc(i, d); |
| 933 | 934 |
} |
| 934 | 935 |
|
| 935 | 936 |
void next(Node& i) const {
|
| 936 | 937 |
Parent::next(i); |
| 937 | 938 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 938 | 939 |
} |
| 939 | 940 |
|
| 940 | 941 |
void next(Arc& i) const {
|
| 941 | 942 |
Parent::next(i); |
| 942 | 943 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 943 | 944 |
|| !(*_node_filter)[Parent::source(i)] |
| 944 | 945 |
|| !(*_node_filter)[Parent::target(i)])) |
| 945 | 946 |
Parent::next(i); |
| 946 | 947 |
} |
| 947 | 948 |
|
| 948 | 949 |
void next(Edge& i) const {
|
| 949 | 950 |
Parent::next(i); |
| 950 | 951 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 951 | 952 |
|| !(*_node_filter)[Parent::u(i)] |
| 952 | 953 |
|| !(*_node_filter)[Parent::v(i)])) |
| 953 | 954 |
Parent::next(i); |
| 954 | 955 |
} |
| 955 | 956 |
|
| 956 | 957 |
void nextIn(Arc& i) const {
|
| 957 | 958 |
Parent::nextIn(i); |
| 958 | 959 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 959 | 960 |
|| !(*_node_filter)[Parent::source(i)])) |
| 960 | 961 |
Parent::nextIn(i); |
| 961 | 962 |
} |
| 962 | 963 |
|
| 963 | 964 |
void nextOut(Arc& i) const {
|
| 964 | 965 |
Parent::nextOut(i); |
| 965 | 966 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 966 | 967 |
|| !(*_node_filter)[Parent::target(i)])) |
| 967 | 968 |
Parent::nextOut(i); |
| 968 | 969 |
} |
| 969 | 970 |
|
| 970 | 971 |
void nextInc(Edge& i, bool& d) const {
|
| 971 | 972 |
Parent::nextInc(i, d); |
| 972 | 973 |
while (i!=INVALID && (!(*_edge_filter)[i] |
| 973 | 974 |
|| !(*_node_filter)[Parent::u(i)] |
| 974 | 975 |
|| !(*_node_filter)[Parent::v(i)])) |
| 975 | 976 |
Parent::nextInc(i, d); |
| 976 | 977 |
} |
| 977 | 978 |
|
| 978 | 979 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); }
|
| 979 | 980 |
void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
|
| 980 | 981 |
|
| 981 | 982 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 982 | 983 |
bool status(const Edge& e) const { return (*_edge_filter)[e]; }
|
| 983 | 984 |
|
| 984 | 985 |
typedef False NodeNumTag; |
| 985 | 986 |
typedef False ArcNumTag; |
| 986 | 987 |
typedef False EdgeNumTag; |
| 987 | 988 |
|
| 988 | 989 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
| 989 | 990 |
Arc findArc(const Node& u, const Node& v, |
| 990 | 991 |
const Arc& prev = INVALID) const {
|
| 991 | 992 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
|
| 992 | 993 |
return INVALID; |
| 993 | 994 |
} |
| 994 | 995 |
Arc arc = Parent::findArc(u, v, prev); |
| 995 | 996 |
while (arc != INVALID && !(*_edge_filter)[arc]) {
|
| 996 | 997 |
arc = Parent::findArc(u, v, arc); |
| 997 | 998 |
} |
| 998 | 999 |
return arc; |
| 999 | 1000 |
} |
| 1000 | 1001 |
|
| 1001 | 1002 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 1002 | 1003 |
Edge findEdge(const Node& u, const Node& v, |
| 1003 | 1004 |
const Edge& prev = INVALID) const {
|
| 1004 | 1005 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
|
| 1005 | 1006 |
return INVALID; |
| 1006 | 1007 |
} |
| 1007 | 1008 |
Edge edge = Parent::findEdge(u, v, prev); |
| 1008 | 1009 |
while (edge != INVALID && !(*_edge_filter)[edge]) {
|
| 1009 | 1010 |
edge = Parent::findEdge(u, v, edge); |
| 1010 | 1011 |
} |
| 1011 | 1012 |
return edge; |
| 1012 | 1013 |
} |
| 1013 | 1014 |
|
| 1014 | 1015 |
template <typename V> |
| 1015 | 1016 |
class NodeMap |
| 1016 | 1017 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1017 | 1018 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
|
| 1018 | 1019 |
public: |
| 1019 | 1020 |
typedef V Value; |
| 1020 | 1021 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1021 | 1022 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
| 1022 | 1023 |
|
| 1023 | 1024 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
| 1024 | 1025 |
: Parent(adaptor) {}
|
| 1025 | 1026 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
| 1026 | 1027 |
: Parent(adaptor, value) {}
|
| 1027 | 1028 |
|
| 1028 | 1029 |
private: |
| 1029 | 1030 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 1030 | 1031 |
return operator=<NodeMap>(cmap); |
| 1031 | 1032 |
} |
| 1032 | 1033 |
|
| 1033 | 1034 |
template <typename CMap> |
| 1034 | 1035 |
NodeMap& operator=(const CMap& cmap) {
|
| 1035 | 1036 |
Parent::operator=(cmap); |
| 1036 | 1037 |
return *this; |
| 1037 | 1038 |
} |
| 1038 | 1039 |
}; |
| 1039 | 1040 |
|
| 1040 | 1041 |
template <typename V> |
| 1041 | 1042 |
class ArcMap |
| 1042 | 1043 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1043 | 1044 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
|
| 1044 | 1045 |
public: |
| 1045 | 1046 |
typedef V Value; |
| 1046 | 1047 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1047 | 1048 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
| 1048 | 1049 |
|
| 1049 | 1050 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
| 1050 | 1051 |
: Parent(adaptor) {}
|
| 1051 | 1052 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
| 1052 | 1053 |
: Parent(adaptor, value) {}
|
| 1053 | 1054 |
|
| 1054 | 1055 |
private: |
| 1055 | 1056 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 1056 | 1057 |
return operator=<ArcMap>(cmap); |
| 1057 | 1058 |
} |
| 1058 | 1059 |
|
| 1059 | 1060 |
template <typename CMap> |
| 1060 | 1061 |
ArcMap& operator=(const CMap& cmap) {
|
| 1061 | 1062 |
Parent::operator=(cmap); |
| 1062 | 1063 |
return *this; |
| 1063 | 1064 |
} |
| 1064 | 1065 |
}; |
| 1065 | 1066 |
|
| 1066 | 1067 |
template <typename V> |
| 1067 | 1068 |
class EdgeMap |
| 1068 | 1069 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1069 | 1070 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
|
| 1070 | 1071 |
public: |
| 1071 | 1072 |
typedef V Value; |
| 1072 | 1073 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1073 | 1074 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
| 1074 | 1075 |
|
| 1075 | 1076 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
| 1076 | 1077 |
: Parent(adaptor) {}
|
| 1077 | 1078 |
|
| 1078 | 1079 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
| 1079 | 1080 |
: Parent(adaptor, value) {}
|
| 1080 | 1081 |
|
| 1081 | 1082 |
private: |
| 1082 | 1083 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 1083 | 1084 |
return operator=<EdgeMap>(cmap); |
| 1084 | 1085 |
} |
| 1085 | 1086 |
|
| 1086 | 1087 |
template <typename CMap> |
| 1087 | 1088 |
EdgeMap& operator=(const CMap& cmap) {
|
| 1088 | 1089 |
Parent::operator=(cmap); |
| 1089 | 1090 |
return *this; |
| 1090 | 1091 |
} |
| 1091 | 1092 |
}; |
| 1092 | 1093 |
|
| 1093 | 1094 |
}; |
| 1094 | 1095 |
|
| 1095 | 1096 |
template <typename GR, typename NF, typename EF> |
| 1096 | 1097 |
class SubGraphBase<GR, NF, EF, false> |
| 1097 | 1098 |
: public GraphAdaptorBase<GR> {
|
| 1098 | 1099 |
public: |
| 1099 | 1100 |
typedef GR Graph; |
| 1100 | 1101 |
typedef NF NodeFilterMap; |
| 1101 | 1102 |
typedef EF EdgeFilterMap; |
| 1102 | 1103 |
|
| 1103 | 1104 |
typedef SubGraphBase Adaptor; |
| 1104 | 1105 |
typedef GraphAdaptorBase<GR> Parent; |
| 1105 | 1106 |
protected: |
| 1106 | 1107 |
NF* _node_filter; |
| 1107 | 1108 |
EF* _edge_filter; |
| 1108 | 1109 |
SubGraphBase() |
| 1109 | 1110 |
: Parent(), _node_filter(0), _edge_filter(0) { }
|
| 1110 | 1111 |
|
| 1111 | 1112 |
void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
|
| 1112 | 1113 |
Parent::initialize(graph); |
| 1113 | 1114 |
_node_filter = &node_filter; |
| 1114 | 1115 |
_edge_filter = &edge_filter; |
| 1115 | 1116 |
} |
| 1116 | 1117 |
|
| 1117 | 1118 |
public: |
| 1118 | 1119 |
|
| 1119 | 1120 |
typedef typename Parent::Node Node; |
| 1120 | 1121 |
typedef typename Parent::Arc Arc; |
| 1121 | 1122 |
typedef typename Parent::Edge Edge; |
| 1122 | 1123 |
|
| 1123 | 1124 |
void first(Node& i) const {
|
| 1124 | 1125 |
Parent::first(i); |
| 1125 | 1126 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 1126 | 1127 |
} |
| 1127 | 1128 |
|
| 1128 | 1129 |
void first(Arc& i) const {
|
| 1129 | 1130 |
Parent::first(i); |
| 1130 | 1131 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
| 1131 | 1132 |
} |
| 1132 | 1133 |
|
| 1133 | 1134 |
void first(Edge& i) const {
|
| 1134 | 1135 |
Parent::first(i); |
| 1135 | 1136 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
| 1136 | 1137 |
} |
| 1137 | 1138 |
|
| 1138 | 1139 |
void firstIn(Arc& i, const Node& n) const {
|
| 1139 | 1140 |
Parent::firstIn(i, n); |
| 1140 | 1141 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
| 1141 | 1142 |
} |
| 1142 | 1143 |
|
| 1143 | 1144 |
void firstOut(Arc& i, const Node& n) const {
|
| 1144 | 1145 |
Parent::firstOut(i, n); |
| 1145 | 1146 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
| 1146 | 1147 |
} |
| 1147 | 1148 |
|
| 1148 | 1149 |
void firstInc(Edge& i, bool& d, const Node& n) const {
|
| 1149 | 1150 |
Parent::firstInc(i, d, n); |
| 1150 | 1151 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
| 1151 | 1152 |
} |
| 1152 | 1153 |
|
| 1153 | 1154 |
void next(Node& i) const {
|
| 1154 | 1155 |
Parent::next(i); |
| 1155 | 1156 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 1156 | 1157 |
} |
| 1157 | 1158 |
void next(Arc& i) const {
|
| 1158 | 1159 |
Parent::next(i); |
| 1159 | 1160 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
| 1160 | 1161 |
} |
| 1161 | 1162 |
void next(Edge& i) const {
|
| 1162 | 1163 |
Parent::next(i); |
| 1163 | 1164 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
| 1164 | 1165 |
} |
| 1165 | 1166 |
void nextIn(Arc& i) const {
|
| 1166 | 1167 |
Parent::nextIn(i); |
| 1167 | 1168 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
| 1168 | 1169 |
} |
| 1169 | 1170 |
|
| 1170 | 1171 |
void nextOut(Arc& i) const {
|
| 1171 | 1172 |
Parent::nextOut(i); |
| 1172 | 1173 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
| 1173 | 1174 |
} |
| 1174 | 1175 |
void nextInc(Edge& i, bool& d) const {
|
| 1175 | 1176 |
Parent::nextInc(i, d); |
| 1176 | 1177 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
| 1177 | 1178 |
} |
| 1178 | 1179 |
|
| 1179 | 1180 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); }
|
| 1180 | 1181 |
void status(const Edge& e, bool v) const { _edge_filter->set(e, v); }
|
| 1181 | 1182 |
|
| 1182 | 1183 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 1183 | 1184 |
bool status(const Edge& e) const { return (*_edge_filter)[e]; }
|
| 1184 | 1185 |
|
| 1185 | 1186 |
typedef False NodeNumTag; |
| 1186 | 1187 |
typedef False ArcNumTag; |
| 1187 | 1188 |
typedef False EdgeNumTag; |
| 1188 | 1189 |
|
| 1189 | 1190 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
| 1190 | 1191 |
Arc findArc(const Node& u, const Node& v, |
| 1191 | 1192 |
const Arc& prev = INVALID) const {
|
| 1192 | 1193 |
Arc arc = Parent::findArc(u, v, prev); |
| 1193 | 1194 |
while (arc != INVALID && !(*_edge_filter)[arc]) {
|
| 1194 | 1195 |
arc = Parent::findArc(u, v, arc); |
| 1195 | 1196 |
} |
| 1196 | 1197 |
return arc; |
| 1197 | 1198 |
} |
| 1198 | 1199 |
|
| 1199 | 1200 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 1200 | 1201 |
Edge findEdge(const Node& u, const Node& v, |
| 1201 | 1202 |
const Edge& prev = INVALID) const {
|
| 1202 | 1203 |
Edge edge = Parent::findEdge(u, v, prev); |
| 1203 | 1204 |
while (edge != INVALID && !(*_edge_filter)[edge]) {
|
| 1204 | 1205 |
edge = Parent::findEdge(u, v, edge); |
| 1205 | 1206 |
} |
| 1206 | 1207 |
return edge; |
| 1207 | 1208 |
} |
| 1208 | 1209 |
|
| 1209 | 1210 |
template <typename V> |
| 1210 | 1211 |
class NodeMap |
| 1211 | 1212 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1212 | 1213 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
|
| 1213 | 1214 |
public: |
| 1214 | 1215 |
typedef V Value; |
| 1215 | 1216 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1216 | 1217 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
| 1217 | 1218 |
|
| 1218 | 1219 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
| 1219 | 1220 |
: Parent(adaptor) {}
|
| 1220 | 1221 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
| 1221 | 1222 |
: Parent(adaptor, value) {}
|
| 1222 | 1223 |
|
| 1223 | 1224 |
private: |
| 1224 | 1225 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 1225 | 1226 |
return operator=<NodeMap>(cmap); |
| 1226 | 1227 |
} |
| 1227 | 1228 |
|
| 1228 | 1229 |
template <typename CMap> |
| 1229 | 1230 |
NodeMap& operator=(const CMap& cmap) {
|
| 1230 | 1231 |
Parent::operator=(cmap); |
| 1231 | 1232 |
return *this; |
| 1232 | 1233 |
} |
| 1233 | 1234 |
}; |
| 1234 | 1235 |
|
| 1235 | 1236 |
template <typename V> |
| 1236 | 1237 |
class ArcMap |
| 1237 | 1238 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1238 | 1239 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
|
| 1239 | 1240 |
public: |
| 1240 | 1241 |
typedef V Value; |
| 1241 | 1242 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1242 | 1243 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
| 1243 | 1244 |
|
| 1244 | 1245 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
| 1245 | 1246 |
: Parent(adaptor) {}
|
| 1246 | 1247 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
| 1247 | 1248 |
: Parent(adaptor, value) {}
|
| 1248 | 1249 |
|
| 1249 | 1250 |
private: |
| 1250 | 1251 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 1251 | 1252 |
return operator=<ArcMap>(cmap); |
| 1252 | 1253 |
} |
| 1253 | 1254 |
|
| 1254 | 1255 |
template <typename CMap> |
| 1255 | 1256 |
ArcMap& operator=(const CMap& cmap) {
|
| 1256 | 1257 |
Parent::operator=(cmap); |
| 1257 | 1258 |
return *this; |
| 1258 | 1259 |
} |
| 1259 | 1260 |
}; |
| 1260 | 1261 |
|
| 1261 | 1262 |
template <typename V> |
| 1262 | 1263 |
class EdgeMap |
| 1263 | 1264 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1264 | 1265 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
|
| 1265 | 1266 |
public: |
| 1266 | 1267 |
typedef V Value; |
| 1267 | 1268 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1268 | 1269 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
| 1269 | 1270 |
|
| 1270 | 1271 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
| 1271 | 1272 |
: Parent(adaptor) {}
|
| 1272 | 1273 |
|
| 1273 | 1274 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
| 1274 | 1275 |
: Parent(adaptor, value) {}
|
| 1275 | 1276 |
|
| 1276 | 1277 |
private: |
| 1277 | 1278 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 1278 | 1279 |
return operator=<EdgeMap>(cmap); |
| 1279 | 1280 |
} |
| 1280 | 1281 |
|
| 1281 | 1282 |
template <typename CMap> |
| 1282 | 1283 |
EdgeMap& operator=(const CMap& cmap) {
|
| 1283 | 1284 |
Parent::operator=(cmap); |
| 1284 | 1285 |
return *this; |
| 1285 | 1286 |
} |
| 1286 | 1287 |
}; |
| 1287 | 1288 |
|
| 1288 | 1289 |
}; |
| 1289 | 1290 |
|
| 1290 | 1291 |
/// \ingroup graph_adaptors |
| 1291 | 1292 |
/// |
| 1292 | 1293 |
/// \brief Adaptor class for hiding nodes and edges in an undirected |
| 1293 | 1294 |
/// graph. |
| 1294 | 1295 |
/// |
| 1295 | 1296 |
/// SubGraph can be used for hiding nodes and edges in a graph. |
| 1296 | 1297 |
/// A \c bool node map and a \c bool edge map must be specified, which |
| 1297 | 1298 |
/// define the filters for nodes and edges. |
| 1298 | 1299 |
/// Only the nodes and edges with \c true filter value are |
| 1299 | 1300 |
/// shown in the subgraph. The edges that are incident to hidden |
| 1300 | 1301 |
/// nodes are also filtered out. |
| 1301 | 1302 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
| 1302 | 1303 |
/// |
| 1303 | 1304 |
/// The adapted graph can also be modified through this adaptor |
| 1304 | 1305 |
/// by adding or removing nodes or edges, unless the \c GR template |
| 1305 | 1306 |
/// parameter is set to be \c const. |
| 1306 | 1307 |
/// |
| 1307 | 1308 |
/// \tparam GR The type of the adapted graph. |
| 1308 | 1309 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
| 1309 | 1310 |
/// It can also be specified to be \c const. |
| 1310 | 1311 |
/// \tparam NF The type of the node filter map. |
| 1311 | 1312 |
/// It must be a \c bool (or convertible) node map of the |
| 1312 | 1313 |
/// adapted graph. The default type is |
| 1313 | 1314 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
| 1314 | 1315 |
/// \tparam EF The type of the edge filter map. |
| 1315 | 1316 |
/// It must be a \c bool (or convertible) edge map of the |
| 1316 | 1317 |
/// adapted graph. The default type is |
| 1317 | 1318 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
| 1318 | 1319 |
/// |
| 1319 | 1320 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
| 1320 | 1321 |
/// adapted graph are convertible to each other. |
| 1321 | 1322 |
/// |
| 1322 | 1323 |
/// \see FilterNodes |
| 1323 | 1324 |
/// \see FilterEdges |
| 1324 | 1325 |
#ifdef DOXYGEN |
| 1325 | 1326 |
template<typename GR, typename NF, typename EF> |
| 1326 | 1327 |
class SubGraph {
|
| 1327 | 1328 |
#else |
| 1328 | 1329 |
template<typename GR, |
| 1329 | 1330 |
typename NF = typename GR::template NodeMap<bool>, |
| 1330 | 1331 |
typename EF = typename GR::template EdgeMap<bool> > |
| 1331 | 1332 |
class SubGraph : |
| 1332 | 1333 |
public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > {
|
| 1333 | 1334 |
#endif |
| 1334 | 1335 |
public: |
| 1335 | 1336 |
/// The type of the adapted graph. |
| 1336 | 1337 |
typedef GR Graph; |
| 1337 | 1338 |
/// The type of the node filter map. |
| 1338 | 1339 |
typedef NF NodeFilterMap; |
| 1339 | 1340 |
/// The type of the edge filter map. |
| 1340 | 1341 |
typedef EF EdgeFilterMap; |
| 1341 | 1342 |
|
| 1342 | 1343 |
typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > |
| 1343 | 1344 |
Parent; |
| 1344 | 1345 |
|
| 1345 | 1346 |
typedef typename Parent::Node Node; |
| 1346 | 1347 |
typedef typename Parent::Edge Edge; |
| 1347 | 1348 |
|
| 1348 | 1349 |
protected: |
| 1349 | 1350 |
SubGraph() { }
|
| 1350 | 1351 |
public: |
| 1351 | 1352 |
|
| 1352 | 1353 |
/// \brief Constructor |
| 1353 | 1354 |
/// |
| 1354 | 1355 |
/// Creates a subgraph for the given graph with the given node |
| 1355 | 1356 |
/// and edge filter maps. |
| 1356 | 1357 |
SubGraph(GR& graph, NF& node_filter, EF& edge_filter) {
|
| 1357 | 1358 |
initialize(graph, node_filter, edge_filter); |
| 1358 | 1359 |
} |
| 1359 | 1360 |
|
| 1360 | 1361 |
/// \brief Sets the status of the given node |
| 1361 | 1362 |
/// |
| 1362 | 1363 |
/// This function sets the status of the given node. |
| 1363 | 1364 |
/// It is done by simply setting the assigned value of \c n |
| 1364 | 1365 |
/// to \c v in the node filter map. |
| 1365 | 1366 |
void status(const Node& n, bool v) const { Parent::status(n, v); }
|
| 1366 | 1367 |
|
| 1367 | 1368 |
/// \brief Sets the status of the given edge |
| 1368 | 1369 |
/// |
| 1369 | 1370 |
/// This function sets the status of the given edge. |
| 1370 | 1371 |
/// It is done by simply setting the assigned value of \c e |
| 1371 | 1372 |
/// to \c v in the edge filter map. |
| 1372 | 1373 |
void status(const Edge& e, bool v) const { Parent::status(e, v); }
|
| 1373 | 1374 |
|
| 1374 | 1375 |
/// \brief Returns the status of the given node |
| 1375 | 1376 |
/// |
| 1376 | 1377 |
/// This function returns the status of the given node. |
| 1377 | 1378 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
| 1378 | 1379 |
bool status(const Node& n) const { return Parent::status(n); }
|
| 1379 | 1380 |
|
| 1380 | 1381 |
/// \brief Returns the status of the given edge |
| 1381 | 1382 |
/// |
| 1382 | 1383 |
/// This function returns the status of the given edge. |
| 1383 | 1384 |
/// It is \c true if the given edge is enabled (i.e. not hidden). |
| 1384 | 1385 |
bool status(const Edge& e) const { return Parent::status(e); }
|
| 1385 | 1386 |
|
| 1386 | 1387 |
/// \brief Disables the given node |
| 1387 | 1388 |
/// |
| 1388 | 1389 |
/// This function disables the given node in the subdigraph, |
| 1389 | 1390 |
/// so the iteration jumps over it. |
| 1390 | 1391 |
/// It is the same as \ref status() "status(n, false)". |
| 1391 | 1392 |
void disable(const Node& n) const { Parent::status(n, false); }
|
| 1392 | 1393 |
|
| 1393 | 1394 |
/// \brief Disables the given edge |
| 1394 | 1395 |
/// |
| 1395 | 1396 |
/// This function disables the given edge in the subgraph, |
| 1396 | 1397 |
/// so the iteration jumps over it. |
| 1397 | 1398 |
/// It is the same as \ref status() "status(e, false)". |
| 1398 | 1399 |
void disable(const Edge& e) const { Parent::status(e, false); }
|
| 1399 | 1400 |
|
| 1400 | 1401 |
/// \brief Enables the given node |
| 1401 | 1402 |
/// |
| 1402 | 1403 |
/// This function enables the given node in the subdigraph. |
| 1403 | 1404 |
/// It is the same as \ref status() "status(n, true)". |
| 1404 | 1405 |
void enable(const Node& n) const { Parent::status(n, true); }
|
| 1405 | 1406 |
|
| 1406 | 1407 |
/// \brief Enables the given edge |
| 1407 | 1408 |
/// |
| 1408 | 1409 |
/// This function enables the given edge in the subgraph. |
| 1409 | 1410 |
/// It is the same as \ref status() "status(e, true)". |
| 1410 | 1411 |
void enable(const Edge& e) const { Parent::status(e, true); }
|
| 1411 | 1412 |
|
| 1412 | 1413 |
}; |
| 1413 | 1414 |
|
| 1414 | 1415 |
/// \brief Returns a read-only SubGraph adaptor |
| 1415 | 1416 |
/// |
| 1416 | 1417 |
/// This function just returns a read-only \ref SubGraph adaptor. |
| 1417 | 1418 |
/// \ingroup graph_adaptors |
| 1418 | 1419 |
/// \relates SubGraph |
| 1419 | 1420 |
template<typename GR, typename NF, typename EF> |
| 1420 | 1421 |
SubGraph<const GR, NF, EF> |
| 1421 | 1422 |
subGraph(const GR& graph, NF& node_filter, EF& edge_filter) {
|
| 1422 | 1423 |
return SubGraph<const GR, NF, EF> |
| 1423 | 1424 |
(graph, node_filter, edge_filter); |
| 1424 | 1425 |
} |
| 1425 | 1426 |
|
| 1426 | 1427 |
template<typename GR, typename NF, typename EF> |
| 1427 | 1428 |
SubGraph<const GR, const NF, EF> |
| 1428 | 1429 |
subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) {
|
| 1429 | 1430 |
return SubGraph<const GR, const NF, EF> |
| 1430 | 1431 |
(graph, node_filter, edge_filter); |
| 1431 | 1432 |
} |
| 1432 | 1433 |
|
| 1433 | 1434 |
template<typename GR, typename NF, typename EF> |
| 1434 | 1435 |
SubGraph<const GR, NF, const EF> |
| 1435 | 1436 |
subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) {
|
| 1436 | 1437 |
return SubGraph<const GR, NF, const EF> |
| 1437 | 1438 |
(graph, node_filter, edge_filter); |
| 1438 | 1439 |
} |
| 1439 | 1440 |
|
| 1440 | 1441 |
template<typename GR, typename NF, typename EF> |
| 1441 | 1442 |
SubGraph<const GR, const NF, const EF> |
| 1442 | 1443 |
subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) {
|
| 1443 | 1444 |
return SubGraph<const GR, const NF, const EF> |
| 1444 | 1445 |
(graph, node_filter, edge_filter); |
| 1445 | 1446 |
} |
| 1446 | 1447 |
|
| 1447 | 1448 |
|
| 1448 | 1449 |
/// \ingroup graph_adaptors |
| 1449 | 1450 |
/// |
| 1450 | 1451 |
/// \brief Adaptor class for hiding nodes in a digraph or a graph. |
| 1451 | 1452 |
/// |
| 1452 | 1453 |
/// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
| 1453 | 1454 |
/// graph. A \c bool node map must be specified, which defines the filter |
| 1454 | 1455 |
/// for the nodes. Only the nodes with \c true filter value and the |
| 1455 | 1456 |
/// arcs/edges incident to nodes both with \c true filter value are shown |
| 1456 | 1457 |
/// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
| 1457 | 1458 |
/// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
| 1458 | 1459 |
/// depending on the \c GR template parameter. |
| 1459 | 1460 |
/// |
| 1460 | 1461 |
/// The adapted (di)graph can also be modified through this adaptor |
| 1461 | 1462 |
/// by adding or removing nodes or arcs/edges, unless the \c GR template |
| 1462 | 1463 |
/// parameter is set to be \c const. |
| 1463 | 1464 |
/// |
| 1464 | 1465 |
/// \tparam GR The type of the adapted digraph or graph. |
| 1465 | 1466 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept |
| 1466 | 1467 |
/// or the \ref concepts::Graph "Graph" concept. |
| 1467 | 1468 |
/// It can also be specified to be \c const. |
| 1468 | 1469 |
/// \tparam NF The type of the node filter map. |
| 1469 | 1470 |
/// It must be a \c bool (or convertible) node map of the |
| 1470 | 1471 |
/// adapted (di)graph. The default type is |
| 1471 | 1472 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
| 1472 | 1473 |
/// |
| 1473 | 1474 |
/// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
| 1474 | 1475 |
/// adapted (di)graph are convertible to each other. |
| 1475 | 1476 |
#ifdef DOXYGEN |
| 1476 | 1477 |
template<typename GR, typename NF> |
| 1477 | 1478 |
class FilterNodes {
|
| 1478 | 1479 |
#else |
| 1479 | 1480 |
template<typename GR, |
| 1480 | 1481 |
typename NF = typename GR::template NodeMap<bool>, |
| 1481 | 1482 |
typename Enable = void> |
| 1482 | 1483 |
class FilterNodes : |
| 1483 | 1484 |
public DigraphAdaptorExtender< |
| 1484 | 1485 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
| 1485 | 1486 |
true> > {
|
| 1486 | 1487 |
#endif |
| 1487 | 1488 |
public: |
| 1488 | 1489 |
|
| 1489 | 1490 |
typedef GR Digraph; |
| 1490 | 1491 |
typedef NF NodeFilterMap; |
| 1491 | 1492 |
|
| 1492 | 1493 |
typedef DigraphAdaptorExtender< |
| 1493 | 1494 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
| 1494 | 1495 |
true> > Parent; |
| 1495 | 1496 |
|
| 1496 | 1497 |
typedef typename Parent::Node Node; |
| 1497 | 1498 |
|
| 1498 | 1499 |
protected: |
| 1499 | 1500 |
ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
| 1500 | 1501 |
|
| 1501 | 1502 |
FilterNodes() : const_true_map() {}
|
| 1502 | 1503 |
|
| 1503 | 1504 |
public: |
| 1504 | 1505 |
|
| 1505 | 1506 |
/// \brief Constructor |
| 1506 | 1507 |
/// |
| 1507 | 1508 |
/// Creates a subgraph for the given digraph or graph with the |
| 1508 | 1509 |
/// given node filter map. |
| 1509 | 1510 |
FilterNodes(GR& graph, NF& node_filter) |
| 1510 | 1511 |
: Parent(), const_true_map() |
| 1511 | 1512 |
{
|
| 1512 | 1513 |
Parent::initialize(graph, node_filter, const_true_map); |
| 1513 | 1514 |
} |
| 1514 | 1515 |
|
| 1515 | 1516 |
/// \brief Sets the status of the given node |
| 1516 | 1517 |
/// |
| 1517 | 1518 |
/// This function sets the status of the given node. |
| 1518 | 1519 |
/// It is done by simply setting the assigned value of \c n |
| 1519 | 1520 |
/// to \c v in the node filter map. |
| 1520 | 1521 |
void status(const Node& n, bool v) const { Parent::status(n, v); }
|
| 1521 | 1522 |
|
| 1522 | 1523 |
/// \brief Returns the status of the given node |
| 1523 | 1524 |
/// |
| 1524 | 1525 |
/// This function returns the status of the given node. |
| 1525 | 1526 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
| 1526 | 1527 |
bool status(const Node& n) const { return Parent::status(n); }
|
| 1527 | 1528 |
|
| 1528 | 1529 |
/// \brief Disables the given node |
| 1529 | 1530 |
/// |
| 1530 | 1531 |
/// This function disables the given node, so the iteration |
| 1531 | 1532 |
/// jumps over it. |
| 1532 | 1533 |
/// It is the same as \ref status() "status(n, false)". |
| 1533 | 1534 |
void disable(const Node& n) const { Parent::status(n, false); }
|
| 1534 | 1535 |
|
| 1535 | 1536 |
/// \brief Enables the given node |
| 1536 | 1537 |
/// |
| 1537 | 1538 |
/// This function enables the given node. |
| 1538 | 1539 |
/// It is the same as \ref status() "status(n, true)". |
| 1539 | 1540 |
void enable(const Node& n) const { Parent::status(n, true); }
|
| 1540 | 1541 |
|
| 1541 | 1542 |
}; |
| 1542 | 1543 |
|
| 1543 | 1544 |
template<typename GR, typename NF> |
| 1544 | 1545 |
class FilterNodes<GR, NF, |
| 1545 | 1546 |
typename enable_if<UndirectedTagIndicator<GR> >::type> : |
| 1546 | 1547 |
public GraphAdaptorExtender< |
| 1547 | 1548 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
| 1548 | 1549 |
true> > {
|
| 1549 | 1550 |
|
| 1550 | 1551 |
public: |
| 1551 | 1552 |
typedef GR Graph; |
| 1552 | 1553 |
typedef NF NodeFilterMap; |
| 1553 | 1554 |
typedef GraphAdaptorExtender< |
| 1554 | 1555 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
| 1555 | 1556 |
true> > Parent; |
| 1556 | 1557 |
|
| 1557 | 1558 |
typedef typename Parent::Node Node; |
| 1558 | 1559 |
protected: |
| 1559 | 1560 |
ConstMap<typename GR::Edge, Const<bool, true> > const_true_map; |
| 1560 | 1561 |
|
| 1561 | 1562 |
FilterNodes() : const_true_map() {}
|
| 1562 | 1563 |
|
| 1563 | 1564 |
public: |
| 1564 | 1565 |
|
| 1565 | 1566 |
FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
| 1566 | 1567 |
Parent(), const_true_map() {
|
| 1567 | 1568 |
Parent::initialize(graph, node_filter, const_true_map); |
| 1568 | 1569 |
} |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_EDGE_SET_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_EDGE_SET_EXTENDER_H |
| 21 | 21 |
|
| 22 |
#include <lemon/core.h> |
|
| 22 | 23 |
#include <lemon/error.h> |
| 23 | 24 |
#include <lemon/bits/default_map.h> |
| 25 |
#include <lemon/bits/map_extender.h> |
|
| 24 | 26 |
|
| 25 | 27 |
///\ingroup digraphbits |
| 26 | 28 |
///\file |
| 27 | 29 |
///\brief Extenders for the arc set types |
| 28 | 30 |
namespace lemon {
|
| 29 | 31 |
|
| 30 | 32 |
/// \ingroup digraphbits |
| 31 | 33 |
/// |
| 32 | 34 |
/// \brief Extender for the ArcSets |
| 33 | 35 |
template <typename Base> |
| 34 | 36 |
class ArcSetExtender : public Base {
|
| 35 | 37 |
public: |
| 36 | 38 |
|
| 37 | 39 |
typedef Base Parent; |
| 38 | 40 |
typedef ArcSetExtender Digraph; |
| 39 | 41 |
|
| 40 | 42 |
// Base extensions |
| 41 | 43 |
|
| 42 | 44 |
typedef typename Parent::Node Node; |
| 43 | 45 |
typedef typename Parent::Arc Arc; |
| 44 | 46 |
|
| 45 | 47 |
int maxId(Node) const {
|
| 46 | 48 |
return Parent::maxNodeId(); |
| 47 | 49 |
} |
| 48 | 50 |
|
| 49 | 51 |
int maxId(Arc) const {
|
| 50 | 52 |
return Parent::maxArcId(); |
| 51 | 53 |
} |
| 52 | 54 |
|
| 53 | 55 |
Node fromId(int id, Node) const {
|
| 54 | 56 |
return Parent::nodeFromId(id); |
| 55 | 57 |
} |
| 56 | 58 |
|
| 57 | 59 |
Arc fromId(int id, Arc) const {
|
| 58 | 60 |
return Parent::arcFromId(id); |
| 59 | 61 |
} |
| 60 | 62 |
|
| 61 | 63 |
Node oppositeNode(const Node &n, const Arc &e) const {
|
| 62 | 64 |
if (n == Parent::source(e)) |
| 63 | 65 |
return Parent::target(e); |
| 64 | 66 |
else if(n==Parent::target(e)) |
| 65 | 67 |
return Parent::source(e); |
| 66 | 68 |
else |
| 67 | 69 |
return INVALID; |
| 68 | 70 |
} |
| 69 | 71 |
|
| 70 | 72 |
|
| 71 | 73 |
// Alteration notifier extensions |
| 72 | 74 |
|
| 73 | 75 |
/// The arc observer registry. |
| 74 | 76 |
typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier; |
| 75 | 77 |
|
| 76 | 78 |
protected: |
| 77 | 79 |
|
| 78 | 80 |
mutable ArcNotifier arc_notifier; |
| 79 | 81 |
|
| 80 | 82 |
public: |
| 81 | 83 |
|
| 82 | 84 |
using Parent::notifier; |
| 83 | 85 |
|
| 84 | 86 |
/// \brief Gives back the arc alteration notifier. |
| 85 | 87 |
/// |
| 86 | 88 |
/// Gives back the arc alteration notifier. |
| 87 | 89 |
ArcNotifier& notifier(Arc) const {
|
| 88 | 90 |
return arc_notifier; |
| 89 | 91 |
} |
| 90 | 92 |
|
| 91 | 93 |
// Iterable extensions |
| 92 | 94 |
|
| 93 | 95 |
class NodeIt : public Node {
|
| 94 | 96 |
const Digraph* digraph; |
| 95 | 97 |
public: |
| 96 | 98 |
|
| 97 | 99 |
NodeIt() {}
|
| 98 | 100 |
|
| 99 | 101 |
NodeIt(Invalid i) : Node(i) { }
|
| 100 | 102 |
|
| 101 | 103 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
|
| 102 | 104 |
_graph.first(static_cast<Node&>(*this)); |
| 103 | 105 |
} |
| 104 | 106 |
|
| 105 | 107 |
NodeIt(const Digraph& _graph, const Node& node) |
| 106 | 108 |
: Node(node), digraph(&_graph) {}
|
| 107 | 109 |
|
| 108 | 110 |
NodeIt& operator++() {
|
| 109 | 111 |
digraph->next(*this); |
| 110 | 112 |
return *this; |
| 111 | 113 |
} |
| 112 | 114 |
|
| 113 | 115 |
}; |
| 114 | 116 |
|
| 115 | 117 |
|
| 116 | 118 |
class ArcIt : public Arc {
|
| 117 | 119 |
const Digraph* digraph; |
| 118 | 120 |
public: |
| 119 | 121 |
|
| 120 | 122 |
ArcIt() { }
|
| 121 | 123 |
|
| 122 | 124 |
ArcIt(Invalid i) : Arc(i) { }
|
| 123 | 125 |
|
| 124 | 126 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
|
| 125 | 127 |
_graph.first(static_cast<Arc&>(*this)); |
| 126 | 128 |
} |
| 127 | 129 |
|
| 128 | 130 |
ArcIt(const Digraph& _graph, const Arc& e) : |
| 129 | 131 |
Arc(e), digraph(&_graph) { }
|
| 130 | 132 |
|
| 131 | 133 |
ArcIt& operator++() {
|
| 132 | 134 |
digraph->next(*this); |
| 133 | 135 |
return *this; |
| 134 | 136 |
} |
| 135 | 137 |
|
| 136 | 138 |
}; |
| 137 | 139 |
|
| 138 | 140 |
|
| 139 | 141 |
class OutArcIt : public Arc {
|
| 140 | 142 |
const Digraph* digraph; |
| 141 | 143 |
public: |
| 142 | 144 |
|
| 143 | 145 |
OutArcIt() { }
|
| 144 | 146 |
|
| 145 | 147 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 146 | 148 |
|
| 147 | 149 |
OutArcIt(const Digraph& _graph, const Node& node) |
| 148 | 150 |
: digraph(&_graph) {
|
| 149 | 151 |
_graph.firstOut(*this, node); |
| 150 | 152 |
} |
| 151 | 153 |
|
| 152 | 154 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
| 153 | 155 |
: Arc(arc), digraph(&_graph) {}
|
| 154 | 156 |
|
| 155 | 157 |
OutArcIt& operator++() {
|
| 156 | 158 |
digraph->nextOut(*this); |
| 157 | 159 |
return *this; |
| 158 | 160 |
} |
| 159 | 161 |
|
| 160 | 162 |
}; |
| 161 | 163 |
|
| 162 | 164 |
|
| 163 | 165 |
class InArcIt : public Arc {
|
| 164 | 166 |
const Digraph* digraph; |
| 165 | 167 |
public: |
| 166 | 168 |
|
| 167 | 169 |
InArcIt() { }
|
| 168 | 170 |
|
| 169 | 171 |
InArcIt(Invalid i) : Arc(i) { }
|
| 170 | 172 |
|
| 171 | 173 |
InArcIt(const Digraph& _graph, const Node& node) |
| 172 | 174 |
: digraph(&_graph) {
|
| 173 | 175 |
_graph.firstIn(*this, node); |
| 174 | 176 |
} |
| 175 | 177 |
|
| 176 | 178 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
| 177 | 179 |
Arc(arc), digraph(&_graph) {}
|
| 178 | 180 |
|
| 179 | 181 |
InArcIt& operator++() {
|
| 180 | 182 |
digraph->nextIn(*this); |
| 181 | 183 |
return *this; |
| 182 | 184 |
} |
| 183 | 185 |
|
| 184 | 186 |
}; |
| 185 | 187 |
|
| 186 | 188 |
/// \brief Base node of the iterator |
| 187 | 189 |
/// |
| 188 | 190 |
/// Returns the base node (ie. the source in this case) of the iterator |
| 189 | 191 |
Node baseNode(const OutArcIt &e) const {
|
| 190 | 192 |
return Parent::source(static_cast<const Arc&>(e)); |
| 191 | 193 |
} |
| 192 | 194 |
/// \brief Running node of the iterator |
| 193 | 195 |
/// |
| 194 | 196 |
/// Returns the running node (ie. the target in this case) of the |
| 195 | 197 |
/// iterator |
| 196 | 198 |
Node runningNode(const OutArcIt &e) const {
|
| 197 | 199 |
return Parent::target(static_cast<const Arc&>(e)); |
| 198 | 200 |
} |
| 199 | 201 |
|
| 200 | 202 |
/// \brief Base node of the iterator |
| 201 | 203 |
/// |
| 202 | 204 |
/// Returns the base node (ie. the target in this case) of the iterator |
| 203 | 205 |
Node baseNode(const InArcIt &e) const {
|
| 204 | 206 |
return Parent::target(static_cast<const Arc&>(e)); |
| 205 | 207 |
} |
| 206 | 208 |
/// \brief Running node of the iterator |
| 207 | 209 |
/// |
| 208 | 210 |
/// Returns the running node (ie. the source in this case) of the |
| 209 | 211 |
/// iterator |
| 210 | 212 |
Node runningNode(const InArcIt &e) const {
|
| 211 | 213 |
return Parent::source(static_cast<const Arc&>(e)); |
| 212 | 214 |
} |
| 213 | 215 |
|
| 214 | 216 |
using Parent::first; |
| 215 | 217 |
|
| 216 | 218 |
// Mappable extension |
| 217 | 219 |
|
| 218 | 220 |
template <typename _Value> |
| 219 | 221 |
class ArcMap |
| 220 | 222 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
|
| 221 | 223 |
public: |
| 222 | 224 |
typedef ArcSetExtender Digraph; |
| 223 | 225 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
| 224 | 226 |
|
| 225 | 227 |
explicit ArcMap(const Digraph& _g) |
| 226 | 228 |
: Parent(_g) {}
|
| 227 | 229 |
ArcMap(const Digraph& _g, const _Value& _v) |
| 228 | 230 |
: Parent(_g, _v) {}
|
| 229 | 231 |
|
| 230 | 232 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 231 | 233 |
return operator=<ArcMap>(cmap); |
| 232 | 234 |
} |
| 233 | 235 |
|
| 234 | 236 |
template <typename CMap> |
| 235 | 237 |
ArcMap& operator=(const CMap& cmap) {
|
| 236 | 238 |
Parent::operator=(cmap); |
| 237 | 239 |
return *this; |
| 238 | 240 |
} |
| 239 | 241 |
|
| 240 | 242 |
}; |
| 241 | 243 |
|
| 242 | 244 |
|
| 243 | 245 |
// Alteration extension |
| 244 | 246 |
|
| 245 | 247 |
Arc addArc(const Node& from, const Node& to) {
|
| 246 | 248 |
Arc arc = Parent::addArc(from, to); |
| 247 | 249 |
notifier(Arc()).add(arc); |
| 248 | 250 |
return arc; |
| 249 | 251 |
} |
| 250 | 252 |
|
| 251 | 253 |
void clear() {
|
| 252 | 254 |
notifier(Arc()).clear(); |
| 253 | 255 |
Parent::clear(); |
| 254 | 256 |
} |
| 255 | 257 |
|
| 256 | 258 |
void erase(const Arc& arc) {
|
| 257 | 259 |
notifier(Arc()).erase(arc); |
| 258 | 260 |
Parent::erase(arc); |
| 259 | 261 |
} |
| 260 | 262 |
|
| 261 | 263 |
ArcSetExtender() {
|
| 262 | 264 |
arc_notifier.setContainer(*this); |
| 263 | 265 |
} |
| 264 | 266 |
|
| 265 | 267 |
~ArcSetExtender() {
|
| 266 | 268 |
arc_notifier.clear(); |
| 267 | 269 |
} |
| 268 | 270 |
|
| 269 | 271 |
}; |
| 270 | 272 |
|
| 271 | 273 |
|
| 272 | 274 |
/// \ingroup digraphbits |
| 273 | 275 |
/// |
| 274 | 276 |
/// \brief Extender for the EdgeSets |
| 275 | 277 |
template <typename Base> |
| 276 | 278 |
class EdgeSetExtender : public Base {
|
| 277 | 279 |
|
| 278 | 280 |
public: |
| 279 | 281 |
|
| 280 | 282 |
typedef Base Parent; |
| 281 | 283 |
typedef EdgeSetExtender Digraph; |
| 282 | 284 |
|
| 283 | 285 |
typedef typename Parent::Node Node; |
| 284 | 286 |
typedef typename Parent::Arc Arc; |
| 285 | 287 |
typedef typename Parent::Edge Edge; |
| 286 | 288 |
|
| 287 | 289 |
|
| 288 | 290 |
int maxId(Node) const {
|
| 289 | 291 |
return Parent::maxNodeId(); |
| 290 | 292 |
} |
| 291 | 293 |
|
| 292 | 294 |
int maxId(Arc) const {
|
| 293 | 295 |
return Parent::maxArcId(); |
| 294 | 296 |
} |
| 295 | 297 |
|
| 296 | 298 |
int maxId(Edge) const {
|
| 297 | 299 |
return Parent::maxEdgeId(); |
| 298 | 300 |
} |
| 299 | 301 |
|
| 300 | 302 |
Node fromId(int id, Node) const {
|
| 301 | 303 |
return Parent::nodeFromId(id); |
| 302 | 304 |
} |
| 303 | 305 |
|
| 304 | 306 |
Arc fromId(int id, Arc) const {
|
| 305 | 307 |
return Parent::arcFromId(id); |
| 306 | 308 |
} |
| 307 | 309 |
|
| 308 | 310 |
Edge fromId(int id, Edge) const {
|
| 309 | 311 |
return Parent::edgeFromId(id); |
| 310 | 312 |
} |
| 311 | 313 |
|
| 312 | 314 |
Node oppositeNode(const Node &n, const Edge &e) const {
|
| 313 | 315 |
if( n == Parent::u(e)) |
| 314 | 316 |
return Parent::v(e); |
| 315 | 317 |
else if( n == Parent::v(e)) |
| 316 | 318 |
return Parent::u(e); |
| 317 | 319 |
else |
| 318 | 320 |
return INVALID; |
| 319 | 321 |
} |
| 320 | 322 |
|
| 321 | 323 |
Arc oppositeArc(const Arc &e) const {
|
| 322 | 324 |
return Parent::direct(e, !Parent::direction(e)); |
| 323 | 325 |
} |
| 324 | 326 |
|
| 325 | 327 |
using Parent::direct; |
| 326 | 328 |
Arc direct(const Edge &e, const Node &s) const {
|
| 327 | 329 |
return Parent::direct(e, Parent::u(e) == s); |
| 328 | 330 |
} |
| 329 | 331 |
|
| 330 | 332 |
typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier; |
| 331 | 333 |
typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier; |
| 332 | 334 |
|
| 333 | 335 |
|
| 334 | 336 |
protected: |
| 335 | 337 |
|
| 336 | 338 |
mutable ArcNotifier arc_notifier; |
| 337 | 339 |
mutable EdgeNotifier edge_notifier; |
| 338 | 340 |
|
| 339 | 341 |
public: |
| 340 | 342 |
|
| 341 | 343 |
using Parent::notifier; |
| 342 | 344 |
|
| 343 | 345 |
ArcNotifier& notifier(Arc) const {
|
| 344 | 346 |
return arc_notifier; |
| 345 | 347 |
} |
| 346 | 348 |
|
| 347 | 349 |
EdgeNotifier& notifier(Edge) const {
|
| 348 | 350 |
return edge_notifier; |
| 349 | 351 |
} |
| 350 | 352 |
|
| 351 | 353 |
|
| 352 | 354 |
class NodeIt : public Node {
|
| 353 | 355 |
const Digraph* digraph; |
| 354 | 356 |
public: |
| 355 | 357 |
|
| 356 | 358 |
NodeIt() {}
|
| 357 | 359 |
|
| 358 | 360 |
NodeIt(Invalid i) : Node(i) { }
|
| 359 | 361 |
|
| 360 | 362 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
|
| 361 | 363 |
_graph.first(static_cast<Node&>(*this)); |
| 362 | 364 |
} |
| 363 | 365 |
|
| 364 | 366 |
NodeIt(const Digraph& _graph, const Node& node) |
| 365 | 367 |
: Node(node), digraph(&_graph) {}
|
| 366 | 368 |
|
| 367 | 369 |
NodeIt& operator++() {
|
| 368 | 370 |
digraph->next(*this); |
| 369 | 371 |
return *this; |
| 370 | 372 |
} |
| 371 | 373 |
|
| 372 | 374 |
}; |
| 373 | 375 |
|
| 374 | 376 |
|
| 375 | 377 |
class ArcIt : public Arc {
|
| 376 | 378 |
const Digraph* digraph; |
| 377 | 379 |
public: |
| 378 | 380 |
|
| 379 | 381 |
ArcIt() { }
|
| 380 | 382 |
|
| 381 | 383 |
ArcIt(Invalid i) : Arc(i) { }
|
| 382 | 384 |
|
| 383 | 385 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
|
| 384 | 386 |
_graph.first(static_cast<Arc&>(*this)); |
| 385 | 387 |
} |
| 386 | 388 |
|
| 387 | 389 |
ArcIt(const Digraph& _graph, const Arc& e) : |
| 388 | 390 |
Arc(e), digraph(&_graph) { }
|
| 389 | 391 |
|
| 390 | 392 |
ArcIt& operator++() {
|
| 391 | 393 |
digraph->next(*this); |
| 392 | 394 |
return *this; |
| 393 | 395 |
} |
| 394 | 396 |
|
| 395 | 397 |
}; |
| 396 | 398 |
|
| 397 | 399 |
|
| 398 | 400 |
class OutArcIt : public Arc {
|
| 399 | 401 |
const Digraph* digraph; |
| 400 | 402 |
public: |
| 401 | 403 |
|
| 402 | 404 |
OutArcIt() { }
|
| 403 | 405 |
|
| 404 | 406 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 405 | 407 |
|
| 406 | 408 |
OutArcIt(const Digraph& _graph, const Node& node) |
| 407 | 409 |
: digraph(&_graph) {
|
| 408 | 410 |
_graph.firstOut(*this, node); |
| 409 | 411 |
} |
| 410 | 412 |
|
| 411 | 413 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
| 412 | 414 |
: Arc(arc), digraph(&_graph) {}
|
| 413 | 415 |
|
| 414 | 416 |
OutArcIt& operator++() {
|
| 415 | 417 |
digraph->nextOut(*this); |
| 416 | 418 |
return *this; |
| 417 | 419 |
} |
| 418 | 420 |
|
| 419 | 421 |
}; |
| 420 | 422 |
|
| 421 | 423 |
|
| 422 | 424 |
class InArcIt : public Arc {
|
| 423 | 425 |
const Digraph* digraph; |
| 424 | 426 |
public: |
| 425 | 427 |
|
| 426 | 428 |
InArcIt() { }
|
| 427 | 429 |
|
| 428 | 430 |
InArcIt(Invalid i) : Arc(i) { }
|
| 429 | 431 |
|
| 430 | 432 |
InArcIt(const Digraph& _graph, const Node& node) |
| 431 | 433 |
: digraph(&_graph) {
|
| 432 | 434 |
_graph.firstIn(*this, node); |
| 433 | 435 |
} |
| 434 | 436 |
|
| 435 | 437 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
| 436 | 438 |
Arc(arc), digraph(&_graph) {}
|
| 437 | 439 |
|
| 438 | 440 |
InArcIt& operator++() {
|
| 439 | 441 |
digraph->nextIn(*this); |
| 440 | 442 |
return *this; |
| 441 | 443 |
} |
| 442 | 444 |
|
| 443 | 445 |
}; |
| 444 | 446 |
|
| 445 | 447 |
|
| 446 | 448 |
class EdgeIt : public Parent::Edge {
|
| 447 | 449 |
const Digraph* digraph; |
| 448 | 450 |
public: |
| 449 | 451 |
|
| 450 | 452 |
EdgeIt() { }
|
| 451 | 453 |
|
| 452 | 454 |
EdgeIt(Invalid i) : Edge(i) { }
|
| 453 | 455 |
|
| 454 | 456 |
explicit EdgeIt(const Digraph& _graph) : digraph(&_graph) {
|
| 455 | 457 |
_graph.first(static_cast<Edge&>(*this)); |
| 456 | 458 |
} |
| 457 | 459 |
|
| 458 | 460 |
EdgeIt(const Digraph& _graph, const Edge& e) : |
| 459 | 461 |
Edge(e), digraph(&_graph) { }
|
| 460 | 462 |
|
| 461 | 463 |
EdgeIt& operator++() {
|
| 462 | 464 |
digraph->next(*this); |
| 463 | 465 |
return *this; |
| 464 | 466 |
} |
| 465 | 467 |
|
| 466 | 468 |
}; |
| 467 | 469 |
|
| 468 | 470 |
class IncEdgeIt : public Parent::Edge {
|
| 469 | 471 |
friend class EdgeSetExtender; |
| 470 | 472 |
const Digraph* digraph; |
| 471 | 473 |
bool direction; |
| 472 | 474 |
public: |
| 473 | 475 |
|
| 474 | 476 |
IncEdgeIt() { }
|
| 475 | 477 |
|
| 476 | 478 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
|
| 477 | 479 |
|
| 478 | 480 |
IncEdgeIt(const Digraph& _graph, const Node &n) : digraph(&_graph) {
|
| 479 | 481 |
_graph.firstInc(*this, direction, n); |
| 480 | 482 |
} |
| 481 | 483 |
|
| 482 | 484 |
IncEdgeIt(const Digraph& _graph, const Edge &ue, const Node &n) |
| 483 | 485 |
: digraph(&_graph), Edge(ue) {
|
| 484 | 486 |
direction = (_graph.source(ue) == n); |
| 485 | 487 |
} |
| 486 | 488 |
|
| 487 | 489 |
IncEdgeIt& operator++() {
|
| 488 | 490 |
digraph->nextInc(*this, direction); |
| 489 | 491 |
return *this; |
| 490 | 492 |
} |
| 491 | 493 |
}; |
| 492 | 494 |
|
| 493 | 495 |
/// \brief Base node of the iterator |
| 494 | 496 |
/// |
| 495 | 497 |
/// Returns the base node (ie. the source in this case) of the iterator |
| 496 | 498 |
Node baseNode(const OutArcIt &e) const {
|
| 497 | 499 |
return Parent::source(static_cast<const Arc&>(e)); |
| 498 | 500 |
} |
| 499 | 501 |
/// \brief Running node of the iterator |
| 500 | 502 |
/// |
| 501 | 503 |
/// Returns the running node (ie. the target in this case) of the |
| 502 | 504 |
/// iterator |
| 503 | 505 |
Node runningNode(const OutArcIt &e) const {
|
| 504 | 506 |
return Parent::target(static_cast<const Arc&>(e)); |
| 505 | 507 |
} |
| 506 | 508 |
|
| 507 | 509 |
/// \brief Base node of the iterator |
| 508 | 510 |
/// |
| 509 | 511 |
/// Returns the base node (ie. the target in this case) of the iterator |
| 510 | 512 |
Node baseNode(const InArcIt &e) const {
|
| 511 | 513 |
return Parent::target(static_cast<const Arc&>(e)); |
| 512 | 514 |
} |
| 513 | 515 |
/// \brief Running node of the iterator |
| 514 | 516 |
/// |
| 515 | 517 |
/// Returns the running node (ie. the source in this case) of the |
| 516 | 518 |
/// iterator |
| 517 | 519 |
Node runningNode(const InArcIt &e) const {
|
| 518 | 520 |
return Parent::source(static_cast<const Arc&>(e)); |
| 519 | 521 |
} |
| 520 | 522 |
|
| 521 | 523 |
/// Base node of the iterator |
| 522 | 524 |
/// |
| 523 | 525 |
/// Returns the base node of the iterator |
| 524 | 526 |
Node baseNode(const IncEdgeIt &e) const {
|
| 525 | 527 |
return e.direction ? u(e) : v(e); |
| 526 | 528 |
} |
| 527 | 529 |
/// Running node of the iterator |
| 528 | 530 |
/// |
| 529 | 531 |
/// Returns the running node of the iterator |
| 530 | 532 |
Node runningNode(const IncEdgeIt &e) const {
|
| 531 | 533 |
return e.direction ? v(e) : u(e); |
| 532 | 534 |
} |
| 533 | 535 |
|
| 534 | 536 |
|
| 535 | 537 |
template <typename _Value> |
| 536 | 538 |
class ArcMap |
| 537 | 539 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
|
| 538 | 540 |
public: |
| 539 | 541 |
typedef EdgeSetExtender Digraph; |
| 540 | 542 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
| 541 | 543 |
|
| 542 | 544 |
ArcMap(const Digraph& _g) |
| 543 | 545 |
: Parent(_g) {}
|
| 544 | 546 |
ArcMap(const Digraph& _g, const _Value& _v) |
| 545 | 547 |
: Parent(_g, _v) {}
|
| 546 | 548 |
|
| 547 | 549 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 548 | 550 |
return operator=<ArcMap>(cmap); |
| 549 | 551 |
} |
| 550 | 552 |
|
| 551 | 553 |
template <typename CMap> |
| 552 | 554 |
ArcMap& operator=(const CMap& cmap) {
|
| 553 | 555 |
Parent::operator=(cmap); |
| 554 | 556 |
return *this; |
| 555 | 557 |
} |
| 556 | 558 |
|
| 557 | 559 |
}; |
| 558 | 560 |
|
| 559 | 561 |
|
| 560 | 562 |
template <typename _Value> |
| 561 | 563 |
class EdgeMap |
| 562 | 564 |
: public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
|
| 563 | 565 |
public: |
| 564 | 566 |
typedef EdgeSetExtender Digraph; |
| 565 | 567 |
typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent; |
| 566 | 568 |
|
| 567 | 569 |
EdgeMap(const Digraph& _g) |
| 568 | 570 |
: Parent(_g) {}
|
| 569 | 571 |
|
| 570 | 572 |
EdgeMap(const Digraph& _g, const _Value& _v) |
| 571 | 573 |
: Parent(_g, _v) {}
|
| 572 | 574 |
|
| 573 | 575 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 574 | 576 |
return operator=<EdgeMap>(cmap); |
| 575 | 577 |
} |
| 576 | 578 |
|
| 577 | 579 |
template <typename CMap> |
| 578 | 580 |
EdgeMap& operator=(const CMap& cmap) {
|
| 579 | 581 |
Parent::operator=(cmap); |
| 580 | 582 |
return *this; |
| 581 | 583 |
} |
| 582 | 584 |
|
| 583 | 585 |
}; |
| 584 | 586 |
|
| 585 | 587 |
|
| 586 | 588 |
// Alteration extension |
| 587 | 589 |
|
| 588 | 590 |
Edge addEdge(const Node& from, const Node& to) {
|
| 589 | 591 |
Edge edge = Parent::addEdge(from, to); |
| 590 | 592 |
notifier(Edge()).add(edge); |
| 591 | 593 |
std::vector<Arc> arcs; |
| 592 | 594 |
arcs.push_back(Parent::direct(edge, true)); |
| 593 | 595 |
arcs.push_back(Parent::direct(edge, false)); |
| 594 | 596 |
notifier(Arc()).add(arcs); |
| 595 | 597 |
return edge; |
| 596 | 598 |
} |
| 597 | 599 |
|
| 598 | 600 |
void clear() {
|
| 599 | 601 |
notifier(Arc()).clear(); |
| 600 | 602 |
notifier(Edge()).clear(); |
| 601 | 603 |
Parent::clear(); |
| 602 | 604 |
} |
| 603 | 605 |
|
| 604 | 606 |
void erase(const Edge& edge) {
|
| 605 | 607 |
std::vector<Arc> arcs; |
| 606 | 608 |
arcs.push_back(Parent::direct(edge, true)); |
| 607 | 609 |
arcs.push_back(Parent::direct(edge, false)); |
| 608 | 610 |
notifier(Arc()).erase(arcs); |
| 609 | 611 |
notifier(Edge()).erase(edge); |
| 610 | 612 |
Parent::erase(edge); |
| 611 | 613 |
} |
| 612 | 614 |
|
| 613 | 615 |
|
| 614 | 616 |
EdgeSetExtender() {
|
| 615 | 617 |
arc_notifier.setContainer(*this); |
| 616 | 618 |
edge_notifier.setContainer(*this); |
| 617 | 619 |
} |
| 618 | 620 |
|
| 619 | 621 |
~EdgeSetExtender() {
|
| 620 | 622 |
edge_notifier.clear(); |
| 621 | 623 |
arc_notifier.clear(); |
| 622 | 624 |
} |
| 623 | 625 |
|
| 624 | 626 |
}; |
| 625 | 627 |
|
| 626 | 628 |
} |
| 627 | 629 |
|
| 628 | 630 |
#endif |
| 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_BITS_PRED_MAP_PATH_H |
| 20 | 20 |
#define LEMON_BITS_PRED_MAP_PATH_H |
| 21 | 21 |
|
| 22 |
#include <lemon/core.h> |
|
| 23 |
#include <lemon/concept_check.h> |
|
| 24 |
|
|
| 22 | 25 |
namespace lemon {
|
| 23 | 26 |
|
| 24 | 27 |
template <typename _Digraph, typename _PredMap> |
| 25 | 28 |
class PredMapPath {
|
| 26 | 29 |
public: |
| 27 | 30 |
typedef True RevPathTag; |
| 28 | 31 |
|
| 29 | 32 |
typedef _Digraph Digraph; |
| 30 | 33 |
typedef typename Digraph::Arc Arc; |
| 31 | 34 |
typedef _PredMap PredMap; |
| 32 | 35 |
|
| 33 | 36 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
| 34 | 37 |
typename Digraph::Node _target) |
| 35 | 38 |
: digraph(_digraph), predMap(_predMap), target(_target) {}
|
| 36 | 39 |
|
| 37 | 40 |
int length() const {
|
| 38 | 41 |
int len = 0; |
| 39 | 42 |
typename Digraph::Node node = target; |
| 40 | 43 |
typename Digraph::Arc arc; |
| 41 | 44 |
while ((arc = predMap[node]) != INVALID) {
|
| 42 | 45 |
node = digraph.source(arc); |
| 43 | 46 |
++len; |
| 44 | 47 |
} |
| 45 | 48 |
return len; |
| 46 | 49 |
} |
| 47 | 50 |
|
| 48 | 51 |
bool empty() const {
|
| 49 | 52 |
return predMap[target] != INVALID; |
| 50 | 53 |
} |
| 51 | 54 |
|
| 52 | 55 |
class RevArcIt {
|
| 53 | 56 |
public: |
| 54 | 57 |
RevArcIt() {}
|
| 55 | 58 |
RevArcIt(Invalid) : path(0), current(INVALID) {}
|
| 56 | 59 |
RevArcIt(const PredMapPath& _path) |
| 57 | 60 |
: path(&_path), current(_path.target) {
|
| 58 | 61 |
if (path->predMap[current] == INVALID) current = INVALID; |
| 59 | 62 |
} |
| 60 | 63 |
|
| 61 | 64 |
operator const typename Digraph::Arc() const {
|
| 62 | 65 |
return path->predMap[current]; |
| 63 | 66 |
} |
| 64 | 67 |
|
| 65 | 68 |
RevArcIt& operator++() {
|
| 66 | 69 |
current = path->digraph.source(path->predMap[current]); |
| 67 | 70 |
if (path->predMap[current] == INVALID) current = INVALID; |
| 68 | 71 |
return *this; |
| 69 | 72 |
} |
| 70 | 73 |
|
| 71 | 74 |
bool operator==(const RevArcIt& e) const {
|
| 72 | 75 |
return current == e.current; |
| 73 | 76 |
} |
| 74 | 77 |
|
| 75 | 78 |
bool operator!=(const RevArcIt& e) const {
|
| 76 | 79 |
return current != e.current; |
| 77 | 80 |
} |
| 78 | 81 |
|
| 79 | 82 |
bool operator<(const RevArcIt& e) const {
|
| 80 | 83 |
return current < e.current; |
| 81 | 84 |
} |
| 82 | 85 |
|
| 83 | 86 |
private: |
| 84 | 87 |
const PredMapPath* path; |
| 85 | 88 |
typename Digraph::Node current; |
| 86 | 89 |
}; |
| 87 | 90 |
|
| 88 | 91 |
private: |
| 89 | 92 |
const Digraph& digraph; |
| 90 | 93 |
const PredMap& predMap; |
| 91 | 94 |
typename Digraph::Node target; |
| 92 | 95 |
}; |
| 93 | 96 |
|
| 94 | 97 |
|
| 95 | 98 |
template <typename _Digraph, typename _PredMatrixMap> |
| 96 | 99 |
class PredMatrixMapPath {
|
| 97 | 100 |
public: |
| 98 | 101 |
typedef True RevPathTag; |
| 99 | 102 |
|
| 100 | 103 |
typedef _Digraph Digraph; |
| 101 | 104 |
typedef typename Digraph::Arc Arc; |
| 102 | 105 |
typedef _PredMatrixMap PredMatrixMap; |
| 103 | 106 |
|
| 104 | 107 |
PredMatrixMapPath(const Digraph& _digraph, |
| 105 | 108 |
const PredMatrixMap& _predMatrixMap, |
| 106 | 109 |
typename Digraph::Node _source, |
| 107 | 110 |
typename Digraph::Node _target) |
| 108 | 111 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
| 109 | 112 |
source(_source), target(_target) {}
|
| 110 | 113 |
|
| 111 | 114 |
int length() const {
|
| 112 | 115 |
int len = 0; |
| 113 | 116 |
typename Digraph::Node node = target; |
| 114 | 117 |
typename Digraph::Arc arc; |
| 115 | 118 |
while ((arc = predMatrixMap(source, node)) != INVALID) {
|
| 116 | 119 |
node = digraph.source(arc); |
| 117 | 120 |
++len; |
| 118 | 121 |
} |
| 119 | 122 |
return len; |
| 120 | 123 |
} |
| 121 | 124 |
|
| 122 | 125 |
bool empty() const {
|
| 123 | 126 |
return source != target; |
| 124 | 127 |
} |
| 125 | 128 |
|
| 126 | 129 |
class RevArcIt {
|
| 127 | 130 |
public: |
| 128 | 131 |
RevArcIt() {}
|
| 129 | 132 |
RevArcIt(Invalid) : path(0), current(INVALID) {}
|
| 130 | 133 |
RevArcIt(const PredMatrixMapPath& _path) |
| 131 | 134 |
: path(&_path), current(_path.target) {
|
| 132 | 135 |
if (path->predMatrixMap(path->source, current) == INVALID) |
| 133 | 136 |
current = INVALID; |
| 134 | 137 |
} |
| 135 | 138 |
|
| 136 | 139 |
operator const typename Digraph::Arc() const {
|
| 137 | 140 |
return path->predMatrixMap(path->source, current); |
| 138 | 141 |
} |
| 139 | 142 |
|
| 140 | 143 |
RevArcIt& operator++() {
|
| 141 | 144 |
current = |
| 142 | 145 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
| 143 | 146 |
if (path->predMatrixMap(path->source, current) == INVALID) |
| 144 | 147 |
current = INVALID; |
| 145 | 148 |
return *this; |
| 146 | 149 |
} |
| 147 | 150 |
|
| 148 | 151 |
bool operator==(const RevArcIt& e) const {
|
| 149 | 152 |
return current == e.current; |
| 150 | 153 |
} |
| 151 | 154 |
|
| 152 | 155 |
bool operator!=(const RevArcIt& e) const {
|
| 153 | 156 |
return current != e.current; |
| 154 | 157 |
} |
| 155 | 158 |
|
| 156 | 159 |
bool operator<(const RevArcIt& e) const {
|
| 157 | 160 |
return current < e.current; |
| 158 | 161 |
} |
| 159 | 162 |
|
| 160 | 163 |
private: |
| 161 | 164 |
const PredMatrixMapPath* path; |
| 162 | 165 |
typename Digraph::Node current; |
| 163 | 166 |
}; |
| 164 | 167 |
|
| 165 | 168 |
private: |
| 166 | 169 |
const Digraph& digraph; |
| 167 | 170 |
const PredMatrixMap& predMatrixMap; |
| 168 | 171 |
typename Digraph::Node source; |
| 169 | 172 |
typename Digraph::Node target; |
| 170 | 173 |
}; |
| 171 | 174 |
|
| 172 | 175 |
} |
| 173 | 176 |
|
| 174 | 177 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_SOLVER_BITS_H |
| 20 | 20 |
#define LEMON_BITS_SOLVER_BITS_H |
| 21 | 21 |
|
| 22 |
#include <vector> |
|
| 23 |
|
|
| 22 | 24 |
namespace lemon {
|
| 23 | 25 |
|
| 24 | 26 |
namespace _solver_bits {
|
| 25 | 27 |
|
| 26 | 28 |
class VarIndex {
|
| 27 | 29 |
private: |
| 28 | 30 |
struct ItemT {
|
| 29 | 31 |
int prev, next; |
| 30 | 32 |
int index; |
| 31 | 33 |
}; |
| 32 | 34 |
std::vector<ItemT> items; |
| 33 | 35 |
int first_item, last_item, first_free_item; |
| 34 | 36 |
|
| 35 | 37 |
std::vector<int> cross; |
| 36 | 38 |
|
| 37 | 39 |
public: |
| 38 | 40 |
|
| 39 | 41 |
VarIndex() |
| 40 | 42 |
: first_item(-1), last_item(-1), first_free_item(-1) {
|
| 41 | 43 |
} |
| 42 | 44 |
|
| 43 | 45 |
void clear() {
|
| 44 | 46 |
first_item = -1; |
| 45 | 47 |
first_free_item = -1; |
| 46 | 48 |
items.clear(); |
| 47 | 49 |
cross.clear(); |
| 48 | 50 |
} |
| 49 | 51 |
|
| 50 | 52 |
int addIndex(int idx) {
|
| 51 | 53 |
int n; |
| 52 | 54 |
if (first_free_item == -1) {
|
| 53 | 55 |
n = items.size(); |
| 54 | 56 |
items.push_back(ItemT()); |
| 55 | 57 |
} else {
|
| 56 | 58 |
n = first_free_item; |
| 57 | 59 |
first_free_item = items[n].next; |
| 58 | 60 |
if (first_free_item != -1) {
|
| 59 | 61 |
items[first_free_item].prev = -1; |
| 60 | 62 |
} |
| 61 | 63 |
} |
| 62 | 64 |
items[n].index = idx; |
| 63 | 65 |
if (static_cast<int>(cross.size()) <= idx) {
|
| 64 | 66 |
cross.resize(idx + 1, -1); |
| 65 | 67 |
} |
| 66 | 68 |
cross[idx] = n; |
| 67 | 69 |
|
| 68 | 70 |
items[n].prev = last_item; |
| 69 | 71 |
items[n].next = -1; |
| 70 | 72 |
if (last_item != -1) {
|
| 71 | 73 |
items[last_item].next = n; |
| 72 | 74 |
} else {
|
| 73 | 75 |
first_item = n; |
| 74 | 76 |
} |
| 75 | 77 |
last_item = n; |
| 76 | 78 |
|
| 77 | 79 |
return n; |
| 78 | 80 |
} |
| 79 | 81 |
|
| 80 | 82 |
int addIndex(int idx, int n) {
|
| 81 | 83 |
while (n >= static_cast<int>(items.size())) {
|
| 82 | 84 |
items.push_back(ItemT()); |
| 83 | 85 |
items.back().prev = -1; |
| 84 | 86 |
items.back().next = first_free_item; |
| 85 | 87 |
if (first_free_item != -1) {
|
| 86 | 88 |
items[first_free_item].prev = items.size() - 1; |
| 87 | 89 |
} |
| 88 | 90 |
first_free_item = items.size() - 1; |
| 89 | 91 |
} |
| 90 | 92 |
if (items[n].next != -1) {
|
| 91 | 93 |
items[items[n].next].prev = items[n].prev; |
| 92 | 94 |
} |
| 93 | 95 |
if (items[n].prev != -1) {
|
| 94 | 96 |
items[items[n].prev].next = items[n].next; |
| 95 | 97 |
} else {
|
| 96 | 98 |
first_free_item = items[n].next; |
| 97 | 99 |
} |
| 98 | 100 |
|
| 99 | 101 |
items[n].index = idx; |
| 100 | 102 |
if (static_cast<int>(cross.size()) <= idx) {
|
| 101 | 103 |
cross.resize(idx + 1, -1); |
| 102 | 104 |
} |
| 103 | 105 |
cross[idx] = n; |
| 104 | 106 |
|
| 105 | 107 |
items[n].prev = last_item; |
| 106 | 108 |
items[n].next = -1; |
| 107 | 109 |
if (last_item != -1) {
|
| 108 | 110 |
items[last_item].next = n; |
| 109 | 111 |
} else {
|
| 110 | 112 |
first_item = n; |
| 111 | 113 |
} |
| 112 | 114 |
last_item = n; |
| 113 | 115 |
|
| 114 | 116 |
return n; |
| 115 | 117 |
} |
| 116 | 118 |
|
| 117 | 119 |
void eraseIndex(int idx) {
|
| 118 | 120 |
int n = cross[idx]; |
| 119 | 121 |
|
| 120 | 122 |
if (items[n].prev != -1) {
|
| 121 | 123 |
items[items[n].prev].next = items[n].next; |
| 122 | 124 |
} else {
|
| 123 | 125 |
first_item = items[n].next; |
| 124 | 126 |
} |
| 125 | 127 |
if (items[n].next != -1) {
|
| 126 | 128 |
items[items[n].next].prev = items[n].prev; |
| 127 | 129 |
} else {
|
| 128 | 130 |
last_item = items[n].prev; |
| 129 | 131 |
} |
| 130 | 132 |
|
| 131 | 133 |
if (first_free_item != -1) {
|
| 132 | 134 |
items[first_free_item].prev = n; |
| 133 | 135 |
} |
| 134 | 136 |
items[n].next = first_free_item; |
| 135 | 137 |
items[n].prev = -1; |
| 136 | 138 |
first_free_item = n; |
| 137 | 139 |
|
| 138 | 140 |
while (!cross.empty() && cross.back() == -1) {
|
| 139 | 141 |
cross.pop_back(); |
| 140 | 142 |
} |
| 141 | 143 |
} |
| 142 | 144 |
|
| 143 | 145 |
int maxIndex() const {
|
| 144 | 146 |
return cross.size() - 1; |
| 145 | 147 |
} |
| 146 | 148 |
|
| 147 | 149 |
void shiftIndices(int idx) {
|
| 148 | 150 |
for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
|
| 149 | 151 |
cross[i - 1] = cross[i]; |
| 150 | 152 |
if (cross[i] != -1) {
|
| 151 | 153 |
--items[cross[i]].index; |
| 152 | 154 |
} |
| 153 | 155 |
} |
| 154 | 156 |
cross.back() = -1; |
| 155 | 157 |
cross.pop_back(); |
| 156 | 158 |
while (!cross.empty() && cross.back() == -1) {
|
| 157 | 159 |
cross.pop_back(); |
| 158 | 160 |
} |
| 159 | 161 |
} |
| 160 | 162 |
|
| 161 | 163 |
void relocateIndex(int idx, int jdx) {
|
| 162 | 164 |
cross[idx] = cross[jdx]; |
| 163 | 165 |
items[cross[jdx]].index = idx; |
| 164 | 166 |
cross[jdx] = -1; |
| 165 | 167 |
|
| 166 | 168 |
while (!cross.empty() && cross.back() == -1) {
|
| 167 | 169 |
cross.pop_back(); |
| 168 | 170 |
} |
| 169 | 171 |
} |
| 170 | 172 |
|
| 171 | 173 |
int operator[](int idx) const {
|
| 172 | 174 |
return cross[idx]; |
| 173 | 175 |
} |
| 174 | 176 |
|
| 175 | 177 |
int operator()(int fdx) const {
|
| 176 | 178 |
return items[fdx].index; |
| 177 | 179 |
} |
| 178 | 180 |
|
| 179 | 181 |
void firstItem(int& fdx) const {
|
| 180 | 182 |
fdx = first_item; |
| 181 | 183 |
} |
| 182 | 184 |
|
| 183 | 185 |
void nextItem(int& fdx) const {
|
| 184 | 186 |
fdx = items[fdx].next; |
| 185 | 187 |
} |
| 186 | 188 |
|
| 187 | 189 |
}; |
| 188 | 190 |
} |
| 189 | 191 |
} |
| 190 | 192 |
|
| 191 | 193 |
#endif |
| 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 |
///\ingroup concept |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief The concept of heaps. |
| 22 | 22 |
|
| 23 | 23 |
#ifndef LEMON_CONCEPT_HEAP_H |
| 24 | 24 |
#define LEMON_CONCEPT_HEAP_H |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 |
#include <lemon/concept_check.h> |
|
| 27 | 28 |
|
| 28 | 29 |
namespace lemon {
|
| 29 | 30 |
|
| 30 | 31 |
namespace concepts {
|
| 31 | 32 |
|
| 32 | 33 |
/// \addtogroup concept |
| 33 | 34 |
/// @{
|
| 34 | 35 |
|
| 35 | 36 |
/// \brief The heap concept. |
| 36 | 37 |
/// |
| 37 | 38 |
/// Concept class describing the main interface of heaps. |
| 38 | 39 |
template <typename Priority, typename ItemIntMap> |
| 39 | 40 |
class Heap {
|
| 40 | 41 |
public: |
| 41 | 42 |
|
| 42 | 43 |
/// Type of the items stored in the heap. |
| 43 | 44 |
typedef typename ItemIntMap::Key Item; |
| 44 | 45 |
|
| 45 | 46 |
/// Type of the priorities. |
| 46 | 47 |
typedef Priority Prio; |
| 47 | 48 |
|
| 48 | 49 |
/// \brief Type to represent the states of the items. |
| 49 | 50 |
/// |
| 50 | 51 |
/// Each item has a state associated to it. It can be "in heap", |
| 51 | 52 |
/// "pre heap" or "post heap". The later two are indifferent |
| 52 | 53 |
/// from the point of view of the heap, but may be useful for |
| 53 | 54 |
/// the user. |
| 54 | 55 |
/// |
| 55 | 56 |
/// The \c ItemIntMap must be initialized in such a way, that it |
| 56 | 57 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
| 57 | 58 |
enum State {
|
| 58 | 59 |
IN_HEAP = 0, |
| 59 | 60 |
PRE_HEAP = -1, |
| 60 | 61 |
POST_HEAP = -2 |
| 61 | 62 |
}; |
| 62 | 63 |
|
| 63 | 64 |
/// \brief The constructor. |
| 64 | 65 |
/// |
| 65 | 66 |
/// The constructor. |
| 66 | 67 |
/// \param map A map that assigns \c int values to keys of type |
| 67 | 68 |
/// \c Item. It is used internally by the heap implementations to |
| 68 | 69 |
/// handle the cross references. The assigned value must be |
| 69 | 70 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
| 70 | 71 |
explicit Heap(ItemIntMap &map) {}
|
| 71 | 72 |
|
| 72 | 73 |
/// \brief The number of items stored in the heap. |
| 73 | 74 |
/// |
| 74 | 75 |
/// Returns the number of items stored in the heap. |
| 75 | 76 |
int size() const { return 0; }
|
| 76 | 77 |
|
| 77 | 78 |
/// \brief Checks if the heap is empty. |
| 78 | 79 |
/// |
| 79 | 80 |
/// Returns \c true if the heap is empty. |
| 80 | 81 |
bool empty() const { return false; }
|
| 81 | 82 |
|
| 82 | 83 |
/// \brief Makes the heap empty. |
| 83 | 84 |
/// |
| 84 | 85 |
/// Makes the heap empty. |
| 85 | 86 |
void clear(); |
| 86 | 87 |
|
| 87 | 88 |
/// \brief Inserts an item into the heap with the given priority. |
| 88 | 89 |
/// |
| 89 | 90 |
/// Inserts the given item into the heap with the given priority. |
| 90 | 91 |
/// \param i The item to insert. |
| 91 | 92 |
/// \param p The priority of the item. |
| 92 | 93 |
void push(const Item &i, const Prio &p) {}
|
| 93 | 94 |
|
| 94 | 95 |
/// \brief Returns the item having minimum priority. |
| 95 | 96 |
/// |
| 96 | 97 |
/// Returns the item having minimum priority. |
| 97 | 98 |
/// \pre The heap must be non-empty. |
| 98 | 99 |
Item top() const {}
|
| 99 | 100 |
|
| 100 | 101 |
/// \brief The minimum priority. |
| 101 | 102 |
/// |
| 102 | 103 |
/// Returns the minimum priority. |
| 103 | 104 |
/// \pre The heap must be non-empty. |
| 104 | 105 |
Prio prio() const {}
|
| 105 | 106 |
|
| 106 | 107 |
/// \brief Removes the item having minimum priority. |
| 107 | 108 |
/// |
| 108 | 109 |
/// Removes the item having minimum priority. |
| 109 | 110 |
/// \pre The heap must be non-empty. |
| 110 | 111 |
void pop() {}
|
| 111 | 112 |
|
| 112 | 113 |
/// \brief Removes an item from the heap. |
| 113 | 114 |
/// |
| 114 | 115 |
/// Removes the given item from the heap if it is already stored. |
| 115 | 116 |
/// \param i The item to delete. |
| 116 | 117 |
void erase(const Item &i) {}
|
| 117 | 118 |
|
| 118 | 119 |
/// \brief The priority of an item. |
| 119 | 120 |
/// |
| 120 | 121 |
/// Returns the priority of the given item. |
| 121 | 122 |
/// \pre \c i must be in the heap. |
| 122 | 123 |
/// \param i The item. |
| 123 | 124 |
Prio operator[](const Item &i) const {}
|
| 124 | 125 |
|
| 125 | 126 |
/// \brief Sets the priority of an item or inserts it, if it is |
| 126 | 127 |
/// not stored in the heap. |
| 127 | 128 |
/// |
| 128 | 129 |
/// This method sets the priority of the given item if it is |
| 129 | 130 |
/// already stored in the heap. |
| 130 | 131 |
/// Otherwise it inserts the given item with the given priority. |
| 131 | 132 |
/// |
| 132 | 133 |
/// \param i The item. |
| 133 | 134 |
/// \param p The priority. |
| 134 | 135 |
void set(const Item &i, const Prio &p) {}
|
| 135 | 136 |
|
| 136 | 137 |
/// \brief Decreases the priority of an item to the given value. |
| 137 | 138 |
/// |
| 138 | 139 |
/// Decreases the priority of an item to the given value. |
| 139 | 140 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
| 140 | 141 |
/// \param i The item. |
| 141 | 142 |
/// \param p The priority. |
| 142 | 143 |
void decrease(const Item &i, const Prio &p) {}
|
| 143 | 144 |
|
| 144 | 145 |
/// \brief Increases the priority of an item to the given value. |
| 145 | 146 |
/// |
| 146 | 147 |
/// Increases the priority of an item to the given value. |
| 147 | 148 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
| 148 | 149 |
/// \param i The item. |
| 149 | 150 |
/// \param p The priority. |
| 150 | 151 |
void increase(const Item &i, const Prio &p) {}
|
| 151 | 152 |
|
| 152 | 153 |
/// \brief Returns if an item is in, has already been in, or has |
| 153 | 154 |
/// never been in the heap. |
| 154 | 155 |
/// |
| 155 | 156 |
/// This method returns \c PRE_HEAP if the given item has never |
| 156 | 157 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
| 157 | 158 |
/// and \c POST_HEAP otherwise. |
| 158 | 159 |
/// In the latter case it is possible that the item will get back |
| 159 | 160 |
/// to the heap again. |
| 160 | 161 |
/// \param i The item. |
| 161 | 162 |
State state(const Item &i) const {}
|
| 162 | 163 |
|
| 163 | 164 |
/// \brief Sets the state of an item in the heap. |
| 164 | 165 |
/// |
| 165 | 166 |
/// Sets the state of the given item in the heap. It can be used |
| 166 | 167 |
/// to manually clear the heap when it is important to achive the |
| 167 | 168 |
/// better time complexity. |
| 168 | 169 |
/// \param i The item. |
| 169 | 170 |
/// \param st The state. It should not be \c IN_HEAP. |
| 170 | 171 |
void state(const Item& i, State st) {}
|
| 171 | 172 |
|
| 172 | 173 |
|
| 173 | 174 |
template <typename _Heap> |
| 174 | 175 |
struct Constraints {
|
| 175 | 176 |
public: |
| 176 | 177 |
void constraints() {
|
| 177 | 178 |
typedef typename _Heap::Item OwnItem; |
| 178 | 179 |
typedef typename _Heap::Prio OwnPrio; |
| 179 | 180 |
typedef typename _Heap::State OwnState; |
| 180 | 181 |
|
| 181 | 182 |
Item item; |
| 182 | 183 |
Prio prio; |
| 183 | 184 |
item=Item(); |
| 184 | 185 |
prio=Prio(); |
| 185 | 186 |
ignore_unused_variable_warning(item); |
| 186 | 187 |
ignore_unused_variable_warning(prio); |
| 187 | 188 |
|
| 188 | 189 |
OwnItem own_item; |
| 189 | 190 |
OwnPrio own_prio; |
| 190 | 191 |
OwnState own_state; |
| 191 | 192 |
own_item=Item(); |
| 192 | 193 |
own_prio=Prio(); |
| 193 | 194 |
ignore_unused_variable_warning(own_item); |
| 194 | 195 |
ignore_unused_variable_warning(own_prio); |
| 195 | 196 |
ignore_unused_variable_warning(own_state); |
| 196 | 197 |
|
| 197 | 198 |
_Heap heap1(map); |
| 198 | 199 |
_Heap heap2 = heap1; |
| 199 | 200 |
ignore_unused_variable_warning(heap1); |
| 200 | 201 |
ignore_unused_variable_warning(heap2); |
| 201 | 202 |
|
| 202 | 203 |
int s = heap.size(); |
| 203 | 204 |
ignore_unused_variable_warning(s); |
| 204 | 205 |
bool e = heap.empty(); |
| 205 | 206 |
ignore_unused_variable_warning(e); |
| 206 | 207 |
|
| 207 | 208 |
prio = heap.prio(); |
| 208 | 209 |
item = heap.top(); |
| 209 | 210 |
prio = heap[item]; |
| 210 | 211 |
own_prio = heap.prio(); |
| 211 | 212 |
own_item = heap.top(); |
| 212 | 213 |
own_prio = heap[own_item]; |
| 213 | 214 |
|
| 214 | 215 |
heap.push(item, prio); |
| 215 | 216 |
heap.push(own_item, own_prio); |
| 216 | 217 |
heap.pop(); |
| 217 | 218 |
|
| 218 | 219 |
heap.set(item, prio); |
| 219 | 220 |
heap.decrease(item, prio); |
| 220 | 221 |
heap.increase(item, prio); |
| 221 | 222 |
heap.set(own_item, own_prio); |
| 222 | 223 |
heap.decrease(own_item, own_prio); |
| 223 | 224 |
heap.increase(own_item, own_prio); |
| 224 | 225 |
|
| 225 | 226 |
heap.erase(item); |
| 226 | 227 |
heap.erase(own_item); |
| 227 | 228 |
heap.clear(); |
| 228 | 229 |
|
| 229 | 230 |
own_state = heap.state(own_item); |
| 230 | 231 |
heap.state(own_item, own_state); |
| 231 | 232 |
|
| 232 | 233 |
own_state = _Heap::PRE_HEAP; |
| 233 | 234 |
own_state = _Heap::IN_HEAP; |
| 234 | 235 |
own_state = _Heap::POST_HEAP; |
| 235 | 236 |
} |
| 236 | 237 |
|
| 237 | 238 |
_Heap& heap; |
| 238 | 239 |
ItemIntMap& map; |
| 239 | 240 |
}; |
| 240 | 241 |
}; |
| 241 | 242 |
|
| 242 | 243 |
/// @} |
| 243 | 244 |
} // namespace lemon |
| 244 | 245 |
} |
| 245 | 246 |
#endif // LEMON_CONCEPT_PATH_H |
| 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_ELEVATOR_H |
| 20 | 20 |
#define LEMON_ELEVATOR_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup auxdat |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Elevator class |
| 25 | 25 |
/// |
| 26 | 26 |
///Elevator class implements an efficient data structure |
| 27 | 27 |
///for labeling items in push-relabel type algorithms. |
| 28 | 28 |
/// |
| 29 | 29 |
|
| 30 |
#include <lemon/core.h> |
|
| 30 | 31 |
#include <lemon/bits/traits.h> |
| 31 | 32 |
|
| 32 | 33 |
namespace lemon {
|
| 33 | 34 |
|
| 34 | 35 |
///Class for handling "labels" in push-relabel type algorithms. |
| 35 | 36 |
|
| 36 | 37 |
///A class for handling "labels" in push-relabel type algorithms. |
| 37 | 38 |
/// |
| 38 | 39 |
///\ingroup auxdat |
| 39 | 40 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
| 40 | 41 |
///to the edges or nodes of a graph, manipulate and query them through |
| 41 | 42 |
///operations typically arising in "push-relabel" type algorithms. |
| 42 | 43 |
/// |
| 43 | 44 |
///Each item is either \em active or not, and you can also choose a |
| 44 | 45 |
///highest level active item. |
| 45 | 46 |
/// |
| 46 | 47 |
///\sa LinkedElevator |
| 47 | 48 |
/// |
| 48 | 49 |
///\param Graph Type of the underlying graph. |
| 49 | 50 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
| 50 | 51 |
///Graph::Arc, Graph::Edge). |
| 51 | 52 |
template<class Graph, class Item> |
| 52 | 53 |
class Elevator |
| 53 | 54 |
{
|
| 54 | 55 |
public: |
| 55 | 56 |
|
| 56 | 57 |
typedef Item Key; |
| 57 | 58 |
typedef int Value; |
| 58 | 59 |
|
| 59 | 60 |
private: |
| 60 | 61 |
|
| 61 | 62 |
typedef Item *Vit; |
| 62 | 63 |
typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap; |
| 63 | 64 |
typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap; |
| 64 | 65 |
|
| 65 | 66 |
const Graph &_g; |
| 66 | 67 |
int _max_level; |
| 67 | 68 |
int _item_num; |
| 68 | 69 |
VitMap _where; |
| 69 | 70 |
IntMap _level; |
| 70 | 71 |
std::vector<Item> _items; |
| 71 | 72 |
std::vector<Vit> _first; |
| 72 | 73 |
std::vector<Vit> _last_active; |
| 73 | 74 |
|
| 74 | 75 |
int _highest_active; |
| 75 | 76 |
|
| 76 | 77 |
void copy(Item i, Vit p) |
| 77 | 78 |
{
|
| 78 | 79 |
_where.set(*p=i,p); |
| 79 | 80 |
} |
| 80 | 81 |
void copy(Vit s, Vit p) |
| 81 | 82 |
{
|
| 82 | 83 |
if(s!=p) |
| 83 | 84 |
{
|
| 84 | 85 |
Item i=*s; |
| 85 | 86 |
*p=i; |
| 86 | 87 |
_where.set(i,p); |
| 87 | 88 |
} |
| 88 | 89 |
} |
| 89 | 90 |
void swap(Vit i, Vit j) |
| 90 | 91 |
{
|
| 91 | 92 |
Item ti=*i; |
| 92 | 93 |
Vit ct = _where[ti]; |
| 93 | 94 |
_where.set(ti,_where[*i=*j]); |
| 94 | 95 |
_where.set(*j,ct); |
| 95 | 96 |
*j=ti; |
| 96 | 97 |
} |
| 97 | 98 |
|
| 98 | 99 |
public: |
| 99 | 100 |
|
| 100 | 101 |
///Constructor with given maximum level. |
| 101 | 102 |
|
| 102 | 103 |
///Constructor with given maximum level. |
| 103 | 104 |
/// |
| 104 | 105 |
///\param graph The underlying graph. |
| 105 | 106 |
///\param max_level The maximum allowed level. |
| 106 | 107 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
| 107 | 108 |
Elevator(const Graph &graph,int max_level) : |
| 108 | 109 |
_g(graph), |
| 109 | 110 |
_max_level(max_level), |
| 110 | 111 |
_item_num(_max_level), |
| 111 | 112 |
_where(graph), |
| 112 | 113 |
_level(graph,0), |
| 113 | 114 |
_items(_max_level), |
| 114 | 115 |
_first(_max_level+2), |
| 115 | 116 |
_last_active(_max_level+2), |
| 116 | 117 |
_highest_active(-1) {}
|
| 117 | 118 |
///Constructor. |
| 118 | 119 |
|
| 119 | 120 |
///Constructor. |
| 120 | 121 |
/// |
| 121 | 122 |
///\param graph The underlying graph. |
| 122 | 123 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
| 123 | 124 |
///where \c max_level is equal to the number of labeled items in the graph. |
| 124 | 125 |
Elevator(const Graph &graph) : |
| 125 | 126 |
_g(graph), |
| 126 | 127 |
_max_level(countItems<Graph, Item>(graph)), |
| 127 | 128 |
_item_num(_max_level), |
| 128 | 129 |
_where(graph), |
| 129 | 130 |
_level(graph,0), |
| 130 | 131 |
_items(_max_level), |
| 131 | 132 |
_first(_max_level+2), |
| 132 | 133 |
_last_active(_max_level+2), |
| 133 | 134 |
_highest_active(-1) |
| 134 | 135 |
{
|
| 135 | 136 |
} |
| 136 | 137 |
|
| 137 | 138 |
///Activate item \c i. |
| 138 | 139 |
|
| 139 | 140 |
///Activate item \c i. |
| 140 | 141 |
///\pre Item \c i shouldn't be active before. |
| 141 | 142 |
void activate(Item i) |
| 142 | 143 |
{
|
| 143 | 144 |
const int l=_level[i]; |
| 144 | 145 |
swap(_where[i],++_last_active[l]); |
| 145 | 146 |
if(l>_highest_active) _highest_active=l; |
| 146 | 147 |
} |
| 147 | 148 |
|
| 148 | 149 |
///Deactivate item \c i. |
| 149 | 150 |
|
| 150 | 151 |
///Deactivate item \c i. |
| 151 | 152 |
///\pre Item \c i must be active before. |
| 152 | 153 |
void deactivate(Item i) |
| 153 | 154 |
{
|
| 154 | 155 |
swap(_where[i],_last_active[_level[i]]--); |
| 155 | 156 |
while(_highest_active>=0 && |
| 156 | 157 |
_last_active[_highest_active]<_first[_highest_active]) |
| 157 | 158 |
_highest_active--; |
| 158 | 159 |
} |
| 159 | 160 |
|
| 160 | 161 |
///Query whether item \c i is active |
| 161 | 162 |
bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
|
| 162 | 163 |
|
| 163 | 164 |
///Return the level of item \c i. |
| 164 | 165 |
int operator[](Item i) const { return _level[i]; }
|
| 165 | 166 |
|
| 166 | 167 |
///Return the number of items on level \c l. |
| 167 | 168 |
int onLevel(int l) const |
| 168 | 169 |
{
|
| 169 | 170 |
return _first[l+1]-_first[l]; |
| 170 | 171 |
} |
| 171 | 172 |
///Return true if level \c l is empty. |
| 172 | 173 |
bool emptyLevel(int l) const |
| 173 | 174 |
{
|
| 174 | 175 |
return _first[l+1]-_first[l]==0; |
| 175 | 176 |
} |
| 176 | 177 |
///Return the number of items above level \c l. |
| 177 | 178 |
int aboveLevel(int l) const |
| 178 | 179 |
{
|
| 179 | 180 |
return _first[_max_level+1]-_first[l+1]; |
| 180 | 181 |
} |
| 181 | 182 |
///Return the number of active items on level \c l. |
| 182 | 183 |
int activesOnLevel(int l) const |
| 183 | 184 |
{
|
| 184 | 185 |
return _last_active[l]-_first[l]+1; |
| 185 | 186 |
} |
| 186 | 187 |
///Return true if there is no active item on level \c l. |
| 187 | 188 |
bool activeFree(int l) const |
| 188 | 189 |
{
|
| 189 | 190 |
return _last_active[l]<_first[l]; |
| 190 | 191 |
} |
| 191 | 192 |
///Return the maximum allowed level. |
| 192 | 193 |
int maxLevel() const |
| 193 | 194 |
{
|
| 194 | 195 |
return _max_level; |
| 195 | 196 |
} |
| 196 | 197 |
|
| 197 | 198 |
///\name Highest Active Item |
| 198 | 199 |
///Functions for working with the highest level |
| 199 | 200 |
///active item. |
| 200 | 201 |
|
| 201 | 202 |
///@{
|
| 202 | 203 |
|
| 203 | 204 |
///Return a highest level active item. |
| 204 | 205 |
|
| 205 | 206 |
///Return a highest level active item or INVALID if there is no active |
| 206 | 207 |
///item. |
| 207 | 208 |
Item highestActive() const |
| 208 | 209 |
{
|
| 209 | 210 |
return _highest_active>=0?*_last_active[_highest_active]:INVALID; |
| 210 | 211 |
} |
| 211 | 212 |
|
| 212 | 213 |
///Return the highest active level. |
| 213 | 214 |
|
| 214 | 215 |
///Return the level of the highest active item or -1 if there is no active |
| 215 | 216 |
///item. |
| 216 | 217 |
int highestActiveLevel() const |
| 217 | 218 |
{
|
| 218 | 219 |
return _highest_active; |
| 219 | 220 |
} |
| 220 | 221 |
|
| 221 | 222 |
///Lift the highest active item by one. |
| 222 | 223 |
|
| 223 | 224 |
///Lift the item returned by highestActive() by one. |
| 224 | 225 |
/// |
| 225 | 226 |
void liftHighestActive() |
| 226 | 227 |
{
|
| 227 | 228 |
Item it = *_last_active[_highest_active]; |
| 228 | 229 |
_level.set(it,_level[it]+1); |
| 229 | 230 |
swap(_last_active[_highest_active]--,_last_active[_highest_active+1]); |
| 230 | 231 |
--_first[++_highest_active]; |
| 231 | 232 |
} |
| 232 | 233 |
|
| 233 | 234 |
///Lift the highest active item to the given level. |
| 234 | 235 |
|
| 235 | 236 |
///Lift the item returned by highestActive() to level \c new_level. |
| 236 | 237 |
/// |
| 237 | 238 |
///\warning \c new_level must be strictly higher |
| 238 | 239 |
///than the current level. |
| 239 | 240 |
/// |
| 240 | 241 |
void liftHighestActive(int new_level) |
| 241 | 242 |
{
|
| 242 | 243 |
const Item li = *_last_active[_highest_active]; |
| 243 | 244 |
|
| 244 | 245 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
| 245 | 246 |
for(int l=_highest_active+1;l<new_level;l++) |
| 246 | 247 |
{
|
| 247 | 248 |
copy(--_first[l+1],_first[l]); |
| 248 | 249 |
--_last_active[l]; |
| 249 | 250 |
} |
| 250 | 251 |
copy(li,_first[new_level]); |
| 251 | 252 |
_level.set(li,new_level); |
| 252 | 253 |
_highest_active=new_level; |
| 253 | 254 |
} |
| 254 | 255 |
|
| 255 | 256 |
///Lift the highest active item to the top level. |
| 256 | 257 |
|
| 257 | 258 |
///Lift the item returned by highestActive() to the top level and |
| 258 | 259 |
///deactivate it. |
| 259 | 260 |
void liftHighestActiveToTop() |
| 260 | 261 |
{
|
| 261 | 262 |
const Item li = *_last_active[_highest_active]; |
| 262 | 263 |
|
| 263 | 264 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
| 264 | 265 |
for(int l=_highest_active+1;l<_max_level;l++) |
| 265 | 266 |
{
|
| 266 | 267 |
copy(--_first[l+1],_first[l]); |
| 267 | 268 |
--_last_active[l]; |
| 268 | 269 |
} |
| 269 | 270 |
copy(li,_first[_max_level]); |
| 270 | 271 |
--_last_active[_max_level]; |
| 271 | 272 |
_level.set(li,_max_level); |
| 272 | 273 |
|
| 273 | 274 |
while(_highest_active>=0 && |
| 274 | 275 |
_last_active[_highest_active]<_first[_highest_active]) |
| 275 | 276 |
_highest_active--; |
| 276 | 277 |
} |
| 277 | 278 |
|
| 278 | 279 |
///@} |
| 279 | 280 |
|
| 280 | 281 |
///\name Active Item on Certain Level |
| 281 | 282 |
///Functions for working with the active items. |
| 282 | 283 |
|
| 283 | 284 |
///@{
|
| 284 | 285 |
|
| 285 | 286 |
///Return an active item on level \c l. |
| 286 | 287 |
|
| 287 | 288 |
///Return an active item on level \c l or \ref INVALID if there is no such |
| 288 | 289 |
///an item. (\c l must be from the range [0...\c max_level]. |
| 289 | 290 |
Item activeOn(int l) const |
| 290 | 291 |
{
|
| 291 | 292 |
return _last_active[l]>=_first[l]?*_last_active[l]:INVALID; |
| 292 | 293 |
} |
| 293 | 294 |
|
| 294 | 295 |
///Lift the active item returned by \c activeOn(level) by one. |
| 295 | 296 |
|
| 296 | 297 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
| 297 | 298 |
///by one. |
| 298 | 299 |
Item liftActiveOn(int level) |
| 299 | 300 |
{
|
| 300 | 301 |
Item it =*_last_active[level]; |
| 301 | 302 |
_level.set(it,_level[it]+1); |
| 302 | 303 |
swap(_last_active[level]--, --_first[level+1]); |
| 303 | 304 |
if (level+1>_highest_active) ++_highest_active; |
| 304 | 305 |
} |
| 305 | 306 |
|
| 306 | 307 |
///Lift the active item returned by \c activeOn(level) to the given level. |
| 307 | 308 |
|
| 308 | 309 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
| 309 | 310 |
///to the given level. |
| 310 | 311 |
void liftActiveOn(int level, int new_level) |
| 311 | 312 |
{
|
| 312 | 313 |
const Item ai = *_last_active[level]; |
| 313 | 314 |
|
| 314 | 315 |
copy(--_first[level+1], _last_active[level]--); |
| 315 | 316 |
for(int l=level+1;l<new_level;l++) |
| 316 | 317 |
{
|
| 317 | 318 |
copy(_last_active[l],_first[l]); |
| 318 | 319 |
copy(--_first[l+1], _last_active[l]--); |
| 319 | 320 |
} |
| 320 | 321 |
copy(ai,_first[new_level]); |
| 321 | 322 |
_level.set(ai,new_level); |
| 322 | 323 |
if (new_level>_highest_active) _highest_active=new_level; |
| 323 | 324 |
} |
| 324 | 325 |
|
| 325 | 326 |
///Lift the active item returned by \c activeOn(level) to the top level. |
| 326 | 327 |
|
| 327 | 328 |
///Lift the active item returned by \ref activeOn() "activeOn(level)" |
| 328 | 329 |
///to the top level and deactivate it. |
| 329 | 330 |
void liftActiveToTop(int level) |
| 330 | 331 |
{
|
| 331 | 332 |
const Item ai = *_last_active[level]; |
| 332 | 333 |
|
| 333 | 334 |
copy(--_first[level+1],_last_active[level]--); |
| 334 | 335 |
for(int l=level+1;l<_max_level;l++) |
| 335 | 336 |
{
|
| 336 | 337 |
copy(_last_active[l],_first[l]); |
| 337 | 338 |
copy(--_first[l+1], _last_active[l]--); |
| 338 | 339 |
} |
| 339 | 340 |
copy(ai,_first[_max_level]); |
| 340 | 341 |
--_last_active[_max_level]; |
| 341 | 342 |
_level.set(ai,_max_level); |
| 342 | 343 |
|
| 343 | 344 |
if (_highest_active==level) {
|
| 344 | 345 |
while(_highest_active>=0 && |
| 345 | 346 |
_last_active[_highest_active]<_first[_highest_active]) |
| 346 | 347 |
_highest_active--; |
| 347 | 348 |
} |
| 348 | 349 |
} |
| 349 | 350 |
|
| 350 | 351 |
///@} |
| 351 | 352 |
|
| 352 | 353 |
///Lift an active item to a higher level. |
| 353 | 354 |
|
| 354 | 355 |
///Lift an active item to a higher level. |
| 355 | 356 |
///\param i The item to be lifted. It must be active. |
| 356 | 357 |
///\param new_level The new level of \c i. It must be strictly higher |
| 357 | 358 |
///than the current level. |
| 358 | 359 |
/// |
| 359 | 360 |
void lift(Item i, int new_level) |
| 360 | 361 |
{
|
| 361 | 362 |
const int lo = _level[i]; |
| 362 | 363 |
const Vit w = _where[i]; |
| 363 | 364 |
|
| 364 | 365 |
copy(_last_active[lo],w); |
| 365 | 366 |
copy(--_first[lo+1],_last_active[lo]--); |
| 366 | 367 |
for(int l=lo+1;l<new_level;l++) |
| 367 | 368 |
{
|
| 368 | 369 |
copy(_last_active[l],_first[l]); |
| 369 | 370 |
copy(--_first[l+1],_last_active[l]--); |
| 370 | 371 |
} |
| 371 | 372 |
copy(i,_first[new_level]); |
| 372 | 373 |
_level.set(i,new_level); |
| 373 | 374 |
if(new_level>_highest_active) _highest_active=new_level; |
| 374 | 375 |
} |
| 375 | 376 |
|
| 376 | 377 |
///Move an inactive item to the top but one level (in a dirty way). |
| 377 | 378 |
|
| 378 | 379 |
///This function moves an inactive item from the top level to the top |
| 379 | 380 |
///but one level (in a dirty way). |
| 380 | 381 |
///\warning It makes the underlying datastructure corrupt, so use it |
| 381 | 382 |
///only if you really know what it is for. |
| 382 | 383 |
///\pre The item is on the top level. |
| 383 | 384 |
void dirtyTopButOne(Item i) {
|
| 384 | 385 |
_level.set(i,_max_level - 1); |
| 385 | 386 |
} |
| 386 | 387 |
|
| 387 | 388 |
///Lift all items on and above the given level to the top level. |
| 388 | 389 |
|
| 389 | 390 |
///This function lifts all items on and above level \c l to the top |
| 390 | 391 |
///level and deactivates them. |
| 391 | 392 |
void liftToTop(int l) |
| 392 | 393 |
{
|
| 393 | 394 |
const Vit f=_first[l]; |
| 394 | 395 |
const Vit tl=_first[_max_level]; |
| 395 | 396 |
for(Vit i=f;i!=tl;++i) |
| 396 | 397 |
_level.set(*i,_max_level); |
| 397 | 398 |
for(int i=l;i<=_max_level;i++) |
| 398 | 399 |
{
|
| 399 | 400 |
_first[i]=f; |
| 400 | 401 |
_last_active[i]=f-1; |
| 401 | 402 |
} |
| 402 | 403 |
for(_highest_active=l-1; |
| 403 | 404 |
_highest_active>=0 && |
| 404 | 405 |
_last_active[_highest_active]<_first[_highest_active]; |
| 405 | 406 |
_highest_active--) ; |
| 406 | 407 |
} |
| 407 | 408 |
|
| 408 | 409 |
private: |
| 409 | 410 |
int _init_lev; |
| 410 | 411 |
Vit _init_num; |
| 411 | 412 |
|
| 412 | 413 |
public: |
| 413 | 414 |
|
| 414 | 415 |
///\name Initialization |
| 415 | 416 |
///Using these functions you can initialize the levels of the items. |
| 416 | 417 |
///\n |
| 417 | 418 |
///The initialization must be started with calling \c initStart(). |
| 418 | 419 |
///Then the items should be listed level by level starting with the |
| 419 | 420 |
///lowest one (level 0) using \c initAddItem() and \c initNewLevel(). |
| 420 | 421 |
///Finally \c initFinish() must be called. |
| 421 | 422 |
///The items not listed are put on the highest level. |
| 422 | 423 |
///@{
|
| 423 | 424 |
|
| 424 | 425 |
///Start the initialization process. |
| 425 | 426 |
void initStart() |
| 426 | 427 |
{
|
| 427 | 428 |
_init_lev=0; |
| 428 | 429 |
_init_num=&_items[0]; |
| 429 | 430 |
_first[0]=&_items[0]; |
| 430 | 431 |
_last_active[0]=&_items[0]-1; |
| 431 | 432 |
Vit n=&_items[0]; |
| 432 | 433 |
for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i) |
| 433 | 434 |
{
|
| 434 | 435 |
*n=i; |
| 435 | 436 |
_where.set(i,n); |
| 436 | 437 |
_level.set(i,_max_level); |
| 437 | 438 |
++n; |
| 438 | 439 |
} |
| 439 | 440 |
} |
| 440 | 441 |
|
| 441 | 442 |
///Add an item to the current level. |
| 442 | 443 |
void initAddItem(Item i) |
| 443 | 444 |
{
|
| 444 | 445 |
swap(_where[i],_init_num); |
| 445 | 446 |
_level.set(i,_init_lev); |
| 446 | 447 |
++_init_num; |
| 447 | 448 |
} |
| 448 | 449 |
|
| 449 | 450 |
///Start a new level. |
| 450 | 451 |
|
| 451 | 452 |
///Start a new level. |
| 452 | 453 |
///It shouldn't be used before the items on level 0 are listed. |
| 453 | 454 |
void initNewLevel() |
| 454 | 455 |
{
|
| 455 | 456 |
_init_lev++; |
| 456 | 457 |
_first[_init_lev]=_init_num; |
| 457 | 458 |
_last_active[_init_lev]=_init_num-1; |
| 458 | 459 |
} |
| 459 | 460 |
|
| 460 | 461 |
///Finalize the initialization process. |
| 461 | 462 |
void initFinish() |
| 462 | 463 |
{
|
| 463 | 464 |
for(_init_lev++;_init_lev<=_max_level;_init_lev++) |
| 464 | 465 |
{
|
| 465 | 466 |
_first[_init_lev]=_init_num; |
| 466 | 467 |
_last_active[_init_lev]=_init_num-1; |
| 467 | 468 |
} |
| 468 | 469 |
_first[_max_level+1]=&_items[0]+_item_num; |
| 469 | 470 |
_last_active[_max_level+1]=&_items[0]+_item_num-1; |
| 470 | 471 |
_highest_active = -1; |
| 471 | 472 |
} |
| 472 | 473 |
|
| 473 | 474 |
///@} |
| 474 | 475 |
|
| 475 | 476 |
}; |
| 476 | 477 |
|
| 477 | 478 |
///Class for handling "labels" in push-relabel type algorithms. |
| 478 | 479 |
|
| 479 | 480 |
///A class for handling "labels" in push-relabel type algorithms. |
| 480 | 481 |
/// |
| 481 | 482 |
///\ingroup auxdat |
| 482 | 483 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
| 483 | 484 |
///to the edges or nodes of a graph, manipulate and query them through |
| 484 | 485 |
///operations typically arising in "push-relabel" type algorithms. |
| 485 | 486 |
/// |
| 486 | 487 |
///Each item is either \em active or not, and you can also choose a |
| 487 | 488 |
///highest level active item. |
| 488 | 489 |
/// |
| 489 | 490 |
///\sa Elevator |
| 490 | 491 |
/// |
| 491 | 492 |
///\param Graph Type of the underlying graph. |
| 492 | 493 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
| 493 | 494 |
///Graph::Arc, Graph::Edge). |
| 494 | 495 |
template <class Graph, class Item> |
| 495 | 496 |
class LinkedElevator {
|
| 496 | 497 |
public: |
| 497 | 498 |
|
| 498 | 499 |
typedef Item Key; |
| 499 | 500 |
typedef int Value; |
| 500 | 501 |
|
| 501 | 502 |
private: |
| 502 | 503 |
|
| 503 | 504 |
typedef typename ItemSetTraits<Graph,Item>:: |
| 504 | 505 |
template Map<Item>::Type ItemMap; |
| 505 | 506 |
typedef typename ItemSetTraits<Graph,Item>:: |
| 506 | 507 |
template Map<int>::Type IntMap; |
| 507 | 508 |
typedef typename ItemSetTraits<Graph,Item>:: |
| 508 | 509 |
template Map<bool>::Type BoolMap; |
| 509 | 510 |
|
| 510 | 511 |
const Graph &_graph; |
| 511 | 512 |
int _max_level; |
| 512 | 513 |
int _item_num; |
| 513 | 514 |
std::vector<Item> _first, _last; |
| 514 | 515 |
ItemMap _prev, _next; |
| 515 | 516 |
int _highest_active; |
| 516 | 517 |
IntMap _level; |
| 517 | 518 |
BoolMap _active; |
| 518 | 519 |
|
| 519 | 520 |
public: |
| 520 | 521 |
///Constructor with given maximum level. |
| 521 | 522 |
|
| 522 | 523 |
///Constructor with given maximum level. |
| 523 | 524 |
/// |
| 524 | 525 |
///\param graph The underlying graph. |
| 525 | 526 |
///\param max_level The maximum allowed level. |
| 526 | 527 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
| 527 | 528 |
LinkedElevator(const Graph& graph, int max_level) |
| 528 | 529 |
: _graph(graph), _max_level(max_level), _item_num(_max_level), |
| 529 | 530 |
_first(_max_level + 1), _last(_max_level + 1), |
| 530 | 531 |
_prev(graph), _next(graph), |
| 531 | 532 |
_highest_active(-1), _level(graph), _active(graph) {}
|
| 532 | 533 |
|
| 533 | 534 |
///Constructor. |
| 534 | 535 |
|
| 535 | 536 |
///Constructor. |
| 536 | 537 |
/// |
| 537 | 538 |
///\param graph The underlying graph. |
| 538 | 539 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
| 539 | 540 |
///where \c max_level is equal to the number of labeled items in the graph. |
| 540 | 541 |
LinkedElevator(const Graph& graph) |
| 541 | 542 |
: _graph(graph), _max_level(countItems<Graph, Item>(graph)), |
| 542 | 543 |
_item_num(_max_level), |
| 543 | 544 |
_first(_max_level + 1), _last(_max_level + 1), |
| 544 | 545 |
_prev(graph, INVALID), _next(graph, INVALID), |
| 545 | 546 |
_highest_active(-1), _level(graph), _active(graph) {}
|
| 546 | 547 |
|
| 547 | 548 |
|
| 548 | 549 |
///Activate item \c i. |
| 549 | 550 |
|
| 550 | 551 |
///Activate item \c i. |
| 551 | 552 |
///\pre Item \c i shouldn't be active before. |
| 552 | 553 |
void activate(Item i) {
|
| 553 | 554 |
_active.set(i, true); |
| 554 | 555 |
|
| 555 | 556 |
int level = _level[i]; |
| 556 | 557 |
if (level > _highest_active) {
|
| 557 | 558 |
_highest_active = level; |
| 558 | 559 |
} |
| 559 | 560 |
|
| 560 | 561 |
if (_prev[i] == INVALID || _active[_prev[i]]) return; |
| 561 | 562 |
//unlace |
| 562 | 563 |
_next.set(_prev[i], _next[i]); |
| 563 | 564 |
if (_next[i] != INVALID) {
|
| 564 | 565 |
_prev.set(_next[i], _prev[i]); |
| 565 | 566 |
} else {
|
| 566 | 567 |
_last[level] = _prev[i]; |
| 567 | 568 |
} |
| 568 | 569 |
//lace |
| 569 | 570 |
_next.set(i, _first[level]); |
| 570 | 571 |
_prev.set(_first[level], i); |
| 571 | 572 |
_prev.set(i, INVALID); |
| 572 | 573 |
_first[level] = i; |
| 573 | 574 |
|
| 574 | 575 |
} |
| 575 | 576 |
|
| 576 | 577 |
///Deactivate item \c i. |
| 577 | 578 |
|
| 578 | 579 |
///Deactivate item \c i. |
| 579 | 580 |
///\pre Item \c i must be active before. |
| 580 | 581 |
void deactivate(Item i) {
|
| 581 | 582 |
_active.set(i, false); |
| 582 | 583 |
int level = _level[i]; |
| 583 | 584 |
|
| 584 | 585 |
if (_next[i] == INVALID || !_active[_next[i]]) |
| 585 | 586 |
goto find_highest_level; |
| 586 | 587 |
|
| 587 | 588 |
//unlace |
| 588 | 589 |
_prev.set(_next[i], _prev[i]); |
| 589 | 590 |
if (_prev[i] != INVALID) {
|
| 590 | 591 |
_next.set(_prev[i], _next[i]); |
| 591 | 592 |
} else {
|
| 592 | 593 |
_first[_level[i]] = _next[i]; |
| 593 | 594 |
} |
| 594 | 595 |
//lace |
| 595 | 596 |
_prev.set(i, _last[level]); |
| 596 | 597 |
_next.set(_last[level], i); |
| 597 | 598 |
_next.set(i, INVALID); |
| 598 | 599 |
_last[level] = i; |
| 599 | 600 |
|
| 600 | 601 |
find_highest_level: |
| 601 | 602 |
if (level == _highest_active) {
|
| 602 | 603 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
| 603 | 604 |
--_highest_active; |
| 604 | 605 |
} |
| 605 | 606 |
} |
| 606 | 607 |
|
| 607 | 608 |
///Query whether item \c i is active |
| 608 | 609 |
bool active(Item i) const { return _active[i]; }
|
| 609 | 610 |
|
| 610 | 611 |
///Return the level of item \c i. |
| 611 | 612 |
int operator[](Item i) const { return _level[i]; }
|
| 612 | 613 |
|
| 613 | 614 |
///Return the number of items on level \c l. |
| 614 | 615 |
int onLevel(int l) const {
|
| 615 | 616 |
int num = 0; |
| 616 | 617 |
Item n = _first[l]; |
| 617 | 618 |
while (n != INVALID) {
|
| 618 | 619 |
++num; |
| 619 | 620 |
n = _next[n]; |
| 620 | 621 |
} |
| 621 | 622 |
return num; |
| 622 | 623 |
} |
| 623 | 624 |
|
| 624 | 625 |
///Return true if the level is empty. |
| 625 | 626 |
bool emptyLevel(int l) const {
|
| 626 | 627 |
return _first[l] == INVALID; |
| 627 | 628 |
} |
| 628 | 629 |
|
| 629 | 630 |
///Return the number of items above level \c l. |
| 630 | 631 |
int aboveLevel(int l) const {
|
| 631 | 632 |
int num = 0; |
| 632 | 633 |
for (int level = l + 1; level < _max_level; ++level) |
| 633 | 634 |
num += onLevel(level); |
| 634 | 635 |
return num; |
| 635 | 636 |
} |
| 636 | 637 |
|
| 637 | 638 |
///Return the number of active items on level \c l. |
| 638 | 639 |
int activesOnLevel(int l) const {
|
| 639 | 640 |
int num = 0; |
| 640 | 641 |
Item n = _first[l]; |
| 641 | 642 |
while (n != INVALID && _active[n]) {
|
| 642 | 643 |
++num; |
| 643 | 644 |
n = _next[n]; |
| 644 | 645 |
} |
| 645 | 646 |
return num; |
| 646 | 647 |
} |
| 647 | 648 |
|
| 648 | 649 |
///Return true if there is no active item on level \c l. |
| 649 | 650 |
bool activeFree(int l) const {
|
| 650 | 651 |
return _first[l] == INVALID || !_active[_first[l]]; |
| 651 | 652 |
} |
| 652 | 653 |
|
| 653 | 654 |
///Return the maximum allowed level. |
| 654 | 655 |
int maxLevel() const {
|
| 655 | 656 |
return _max_level; |
| 656 | 657 |
} |
| 657 | 658 |
|
| 658 | 659 |
///\name Highest Active Item |
| 659 | 660 |
///Functions for working with the highest level |
| 660 | 661 |
///active item. |
| 661 | 662 |
|
| 662 | 663 |
///@{
|
| 663 | 664 |
|
| 664 | 665 |
///Return a highest level active item. |
| 665 | 666 |
|
| 666 | 667 |
///Return a highest level active item or INVALID if there is no active |
| 667 | 668 |
///item. |
| 668 | 669 |
Item highestActive() const {
|
| 669 | 670 |
return _highest_active >= 0 ? _first[_highest_active] : INVALID; |
| 670 | 671 |
} |
| 671 | 672 |
|
| 672 | 673 |
///Return the highest active level. |
| 673 | 674 |
|
| 674 | 675 |
///Return the level of the highest active item or -1 if there is no active |
| 675 | 676 |
///item. |
| 676 | 677 |
int highestActiveLevel() const {
|
| 677 | 678 |
return _highest_active; |
| 678 | 679 |
} |
| 679 | 680 |
|
| 680 | 681 |
///Lift the highest active item by one. |
| 681 | 682 |
|
| 682 | 683 |
///Lift the item returned by highestActive() by one. |
| 683 | 684 |
/// |
| 684 | 685 |
void liftHighestActive() {
|
| 685 | 686 |
Item i = _first[_highest_active]; |
| 686 | 687 |
if (_next[i] != INVALID) {
|
| 687 | 688 |
_prev.set(_next[i], INVALID); |
| 688 | 689 |
_first[_highest_active] = _next[i]; |
| 689 | 690 |
} else {
|
| 690 | 691 |
_first[_highest_active] = INVALID; |
| 691 | 692 |
_last[_highest_active] = INVALID; |
| 692 | 693 |
} |
| 693 | 694 |
_level.set(i, ++_highest_active); |
| 694 | 695 |
if (_first[_highest_active] == INVALID) {
|
| 695 | 696 |
_first[_highest_active] = i; |
| 696 | 697 |
_last[_highest_active] = i; |
| 697 | 698 |
_prev.set(i, INVALID); |
| 698 | 699 |
_next.set(i, INVALID); |
| 699 | 700 |
} else {
|
| 700 | 701 |
_prev.set(_first[_highest_active], i); |
| 701 | 702 |
_next.set(i, _first[_highest_active]); |
| 702 | 703 |
_first[_highest_active] = i; |
| 703 | 704 |
} |
| 704 | 705 |
} |
| 705 | 706 |
|
| 706 | 707 |
///Lift the highest active item to the given level. |
| 707 | 708 |
|
| 708 | 709 |
///Lift the item returned by highestActive() to level \c new_level. |
| 709 | 710 |
/// |
| 710 | 711 |
///\warning \c new_level must be strictly higher |
| 711 | 712 |
///than the current level. |
| 712 | 713 |
/// |
| 713 | 714 |
void liftHighestActive(int new_level) {
|
| 714 | 715 |
Item i = _first[_highest_active]; |
| 715 | 716 |
if (_next[i] != INVALID) {
|
| 716 | 717 |
_prev.set(_next[i], INVALID); |
| 717 | 718 |
_first[_highest_active] = _next[i]; |
| 718 | 719 |
} else {
|
| 719 | 720 |
_first[_highest_active] = INVALID; |
| 720 | 721 |
_last[_highest_active] = INVALID; |
| 721 | 722 |
} |
| 722 | 723 |
_level.set(i, _highest_active = new_level); |
| 723 | 724 |
if (_first[_highest_active] == INVALID) {
|
| 724 | 725 |
_first[_highest_active] = _last[_highest_active] = i; |
| 725 | 726 |
_prev.set(i, INVALID); |
| 726 | 727 |
_next.set(i, INVALID); |
| 727 | 728 |
} else {
|
| 728 | 729 |
_prev.set(_first[_highest_active], i); |
| 729 | 730 |
_next.set(i, _first[_highest_active]); |
| 730 | 731 |
_first[_highest_active] = i; |
| 731 | 732 |
} |
| 732 | 733 |
} |
| 733 | 734 |
|
| 734 | 735 |
///Lift the highest active item to the top level. |
| 735 | 736 |
|
| 736 | 737 |
///Lift the item returned by highestActive() to the top level and |
| 737 | 738 |
///deactivate it. |
| 738 | 739 |
void liftHighestActiveToTop() {
|
| 739 | 740 |
Item i = _first[_highest_active]; |
| 740 | 741 |
_level.set(i, _max_level); |
| 741 | 742 |
if (_next[i] != INVALID) {
|
| 742 | 743 |
_prev.set(_next[i], INVALID); |
| 743 | 744 |
_first[_highest_active] = _next[i]; |
| 744 | 745 |
} else {
|
| 745 | 746 |
_first[_highest_active] = INVALID; |
| 746 | 747 |
_last[_highest_active] = INVALID; |
| 747 | 748 |
} |
| 748 | 749 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
| 749 | 750 |
--_highest_active; |
| 750 | 751 |
} |
| 751 | 752 |
|
| 752 | 753 |
///@} |
| 753 | 754 |
|
| 754 | 755 |
///\name Active Item on Certain Level |
| 755 | 756 |
///Functions for working with the active items. |
| 756 | 757 |
|
| 757 | 758 |
///@{
|
| 758 | 759 |
|
| 759 | 760 |
///Return an active item on level \c l. |
| 760 | 761 |
|
| 761 | 762 |
///Return an active item on level \c l or \ref INVALID if there is no such |
| 762 | 763 |
///an item. (\c l must be from the range [0...\c max_level]. |
| 763 | 764 |
Item activeOn(int l) const |
| 764 | 765 |
{
|
| 765 | 766 |
return _active[_first[l]] ? _first[l] : INVALID; |
| 766 | 767 |
} |
| 767 | 768 |
|
| 768 | 769 |
///Lift the active item returned by \c activeOn(l) by one. |
| 769 | 770 |
|
| 770 | 771 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
| 771 | 772 |
///by one. |
| 772 | 773 |
Item liftActiveOn(int l) |
| 773 | 774 |
{
|
| 774 | 775 |
Item i = _first[l]; |
| 775 | 776 |
if (_next[i] != INVALID) {
|
| 776 | 777 |
_prev.set(_next[i], INVALID); |
| 777 | 778 |
_first[l] = _next[i]; |
| 778 | 779 |
} else {
|
| 779 | 780 |
_first[l] = INVALID; |
| 780 | 781 |
_last[l] = INVALID; |
| 781 | 782 |
} |
| 782 | 783 |
_level.set(i, ++l); |
| 783 | 784 |
if (_first[l] == INVALID) {
|
| 784 | 785 |
_first[l] = _last[l] = i; |
| 785 | 786 |
_prev.set(i, INVALID); |
| 786 | 787 |
_next.set(i, INVALID); |
| 787 | 788 |
} else {
|
| 788 | 789 |
_prev.set(_first[l], i); |
| 789 | 790 |
_next.set(i, _first[l]); |
| 790 | 791 |
_first[l] = i; |
| 791 | 792 |
} |
| 792 | 793 |
if (_highest_active < l) {
|
| 793 | 794 |
_highest_active = l; |
| 794 | 795 |
} |
| 795 | 796 |
} |
| 796 | 797 |
|
| 797 | 798 |
///Lift the active item returned by \c activeOn(l) to the given level. |
| 798 | 799 |
|
| 799 | 800 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
| 800 | 801 |
///to the given level. |
| 801 | 802 |
void liftActiveOn(int l, int new_level) |
| 802 | 803 |
{
|
| 803 | 804 |
Item i = _first[l]; |
| 804 | 805 |
if (_next[i] != INVALID) {
|
| 805 | 806 |
_prev.set(_next[i], INVALID); |
| 806 | 807 |
_first[l] = _next[i]; |
| 807 | 808 |
} else {
|
| 808 | 809 |
_first[l] = INVALID; |
| 809 | 810 |
_last[l] = INVALID; |
| 810 | 811 |
} |
| 811 | 812 |
_level.set(i, l = new_level); |
| 812 | 813 |
if (_first[l] == INVALID) {
|
| 813 | 814 |
_first[l] = _last[l] = i; |
| 814 | 815 |
_prev.set(i, INVALID); |
| 815 | 816 |
_next.set(i, INVALID); |
| 816 | 817 |
} else {
|
| 817 | 818 |
_prev.set(_first[l], i); |
| 818 | 819 |
_next.set(i, _first[l]); |
| 819 | 820 |
_first[l] = i; |
| 820 | 821 |
} |
| 821 | 822 |
if (_highest_active < l) {
|
| 822 | 823 |
_highest_active = l; |
| 823 | 824 |
} |
| 824 | 825 |
} |
| 825 | 826 |
|
| 826 | 827 |
///Lift the active item returned by \c activeOn(l) to the top level. |
| 827 | 828 |
|
| 828 | 829 |
///Lift the active item returned by \ref activeOn() "activeOn(l)" |
| 829 | 830 |
///to the top level and deactivate it. |
| 830 | 831 |
void liftActiveToTop(int l) |
| 831 | 832 |
{
|
| 832 | 833 |
Item i = _first[l]; |
| 833 | 834 |
if (_next[i] != INVALID) {
|
| 834 | 835 |
_prev.set(_next[i], INVALID); |
| 835 | 836 |
_first[l] = _next[i]; |
| 836 | 837 |
} else {
|
| 837 | 838 |
_first[l] = INVALID; |
| 838 | 839 |
_last[l] = INVALID; |
| 839 | 840 |
} |
| 840 | 841 |
_level.set(i, _max_level); |
| 841 | 842 |
if (l == _highest_active) {
|
| 842 | 843 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
| 843 | 844 |
--_highest_active; |
| 844 | 845 |
} |
| 845 | 846 |
} |
| 846 | 847 |
|
| 847 | 848 |
///@} |
| 848 | 849 |
|
| 849 | 850 |
/// \brief Lift an active item to a higher level. |
| 850 | 851 |
/// |
| 851 | 852 |
/// Lift an active item to a higher level. |
| 852 | 853 |
/// \param i The item to be lifted. It must be active. |
| 853 | 854 |
/// \param new_level The new level of \c i. It must be strictly higher |
| 854 | 855 |
/// than the current level. |
| 855 | 856 |
/// |
| 856 | 857 |
void lift(Item i, int new_level) {
|
| 857 | 858 |
if (_next[i] != INVALID) {
|
| 858 | 859 |
_prev.set(_next[i], _prev[i]); |
| 859 | 860 |
} else {
|
| 860 | 861 |
_last[new_level] = _prev[i]; |
| 861 | 862 |
} |
| 862 | 863 |
if (_prev[i] != INVALID) {
|
| 863 | 864 |
_next.set(_prev[i], _next[i]); |
| 864 | 865 |
} else {
|
| 865 | 866 |
_first[new_level] = _next[i]; |
| 866 | 867 |
} |
| 867 | 868 |
_level.set(i, new_level); |
| 868 | 869 |
if (_first[new_level] == INVALID) {
|
| 869 | 870 |
_first[new_level] = _last[new_level] = i; |
| 870 | 871 |
_prev.set(i, INVALID); |
| 871 | 872 |
_next.set(i, INVALID); |
| 872 | 873 |
} else {
|
| 873 | 874 |
_prev.set(_first[new_level], i); |
| 874 | 875 |
_next.set(i, _first[new_level]); |
| 875 | 876 |
_first[new_level] = i; |
| 876 | 877 |
} |
| 877 | 878 |
if (_highest_active < new_level) {
|
| 878 | 879 |
_highest_active = new_level; |
| 879 | 880 |
} |
| 880 | 881 |
} |
| 881 | 882 |
|
| 882 | 883 |
///Move an inactive item to the top but one level (in a dirty way). |
| 883 | 884 |
|
| 884 | 885 |
///This function moves an inactive item from the top level to the top |
| 885 | 886 |
///but one level (in a dirty way). |
| 886 | 887 |
///\warning It makes the underlying datastructure corrupt, so use it |
| 887 | 888 |
///only if you really know what it is for. |
| 888 | 889 |
///\pre The item is on the top level. |
| 889 | 890 |
void dirtyTopButOne(Item i) {
|
| 890 | 891 |
_level.set(i, _max_level - 1); |
| 891 | 892 |
} |
| 892 | 893 |
|
| 893 | 894 |
///Lift all items on and above the given level to the top level. |
| 894 | 895 |
|
| 895 | 896 |
///This function lifts all items on and above level \c l to the top |
| 896 | 897 |
///level and deactivates them. |
| 897 | 898 |
void liftToTop(int l) {
|
| 898 | 899 |
for (int i = l + 1; _first[i] != INVALID; ++i) {
|
| 899 | 900 |
Item n = _first[i]; |
| 900 | 901 |
while (n != INVALID) {
|
| 901 | 902 |
_level.set(n, _max_level); |
| 902 | 903 |
n = _next[n]; |
| 903 | 904 |
} |
| 904 | 905 |
_first[i] = INVALID; |
| 905 | 906 |
_last[i] = INVALID; |
| 906 | 907 |
} |
| 907 | 908 |
if (_highest_active > l - 1) {
|
| 908 | 909 |
_highest_active = l - 1; |
| 909 | 910 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
| 910 | 911 |
--_highest_active; |
| 911 | 912 |
} |
| 912 | 913 |
} |
| 913 | 914 |
|
| 914 | 915 |
private: |
| 915 | 916 |
|
| 916 | 917 |
int _init_level; |
| 917 | 918 |
|
| 918 | 919 |
public: |
| 919 | 920 |
|
| 920 | 921 |
///\name Initialization |
| 921 | 922 |
///Using these functions you can initialize the levels of the items. |
| 922 | 923 |
///\n |
| 923 | 924 |
///The initialization must be started with calling \c initStart(). |
| 924 | 925 |
///Then the items should be listed level by level starting with the |
| 925 | 926 |
///lowest one (level 0) using \c initAddItem() and \c initNewLevel(). |
| 926 | 927 |
///Finally \c initFinish() must be called. |
| 927 | 928 |
///The items not listed are put on the highest level. |
| 928 | 929 |
///@{
|
| 929 | 930 |
|
| 930 | 931 |
///Start the initialization process. |
| 931 | 932 |
void initStart() {
|
| 932 | 933 |
|
| 933 | 934 |
for (int i = 0; i <= _max_level; ++i) {
|
| 934 | 935 |
_first[i] = _last[i] = INVALID; |
| 935 | 936 |
} |
| 936 | 937 |
_init_level = 0; |
| 937 | 938 |
for(typename ItemSetTraits<Graph,Item>::ItemIt i(_graph); |
| 938 | 939 |
i != INVALID; ++i) {
|
| 939 | 940 |
_level.set(i, _max_level); |
| 940 | 941 |
_active.set(i, false); |
| 941 | 942 |
} |
| 942 | 943 |
} |
| 943 | 944 |
|
| 944 | 945 |
///Add an item to the current level. |
| 945 | 946 |
void initAddItem(Item i) {
|
| 946 | 947 |
_level.set(i, _init_level); |
| 947 | 948 |
if (_last[_init_level] == INVALID) {
|
| 948 | 949 |
_first[_init_level] = i; |
| 949 | 950 |
_last[_init_level] = i; |
| 950 | 951 |
_prev.set(i, INVALID); |
| 951 | 952 |
_next.set(i, INVALID); |
| 952 | 953 |
} else {
|
| 953 | 954 |
_prev.set(i, _last[_init_level]); |
| 954 | 955 |
_next.set(i, INVALID); |
| 955 | 956 |
_next.set(_last[_init_level], i); |
| 956 | 957 |
_last[_init_level] = i; |
| 957 | 958 |
} |
| 958 | 959 |
} |
| 959 | 960 |
|
| 960 | 961 |
///Start a new level. |
| 961 | 962 |
|
| 962 | 963 |
///Start a new level. |
| 963 | 964 |
///It shouldn't be used before the items on level 0 are listed. |
| 964 | 965 |
void initNewLevel() {
|
| 965 | 966 |
++_init_level; |
| 966 | 967 |
} |
| 967 | 968 |
|
| 968 | 969 |
///Finalize the initialization process. |
| 969 | 970 |
void initFinish() {
|
| 970 | 971 |
_highest_active = -1; |
| 971 | 972 |
} |
| 972 | 973 |
|
| 973 | 974 |
///@} |
| 974 | 975 |
|
| 975 | 976 |
}; |
| 976 | 977 |
|
| 977 | 978 |
|
| 978 | 979 |
} //END OF NAMESPACE LEMON |
| 979 | 980 |
|
| 980 | 981 |
#endif |
| 981 | 982 |
| 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_SUURBALLE_H |
| 20 | 20 |
#define LEMON_SUURBALLE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup shortest_path |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief An algorithm for finding arc-disjoint paths between two |
| 25 | 25 |
/// nodes having minimum total length. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <lemon/bin_heap.h> |
| 29 | 29 |
#include <lemon/path.h> |
| 30 |
#include <lemon/list_graph.h> |
|
| 31 |
#include <lemon/maps.h> |
|
| 30 | 32 |
|
| 31 | 33 |
namespace lemon {
|
| 32 | 34 |
|
| 33 | 35 |
/// \addtogroup shortest_path |
| 34 | 36 |
/// @{
|
| 35 | 37 |
|
| 36 | 38 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes |
| 37 | 39 |
/// having minimum total length. |
| 38 | 40 |
/// |
| 39 | 41 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for |
| 40 | 42 |
/// finding arc-disjoint paths having minimum total length (cost) |
| 41 | 43 |
/// from a given source node to a given target node in a digraph. |
| 42 | 44 |
/// |
| 43 | 45 |
/// In fact, this implementation is the specialization of the |
| 44 | 46 |
/// \ref CapacityScaling "successive shortest path" algorithm. |
| 45 | 47 |
/// |
| 46 | 48 |
/// \tparam Digraph The digraph type the algorithm runs on. |
| 47 | 49 |
/// The default value is \c ListDigraph. |
| 48 | 50 |
/// \tparam LengthMap The type of the length (cost) map. |
| 49 | 51 |
/// The default value is <tt>Digraph::ArcMap<int></tt>. |
| 50 | 52 |
/// |
| 51 | 53 |
/// \warning Length values should be \e non-negative \e integers. |
| 52 | 54 |
/// |
| 53 | 55 |
/// \note For finding node-disjoint paths this algorithm can be used |
| 54 | 56 |
/// with \ref SplitNodes. |
| 55 | 57 |
#ifdef DOXYGEN |
| 56 | 58 |
template <typename Digraph, typename LengthMap> |
| 57 | 59 |
#else |
| 58 | 60 |
template < typename Digraph = ListDigraph, |
| 59 | 61 |
typename LengthMap = typename Digraph::template ArcMap<int> > |
| 60 | 62 |
#endif |
| 61 | 63 |
class Suurballe |
| 62 | 64 |
{
|
| 63 | 65 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 64 | 66 |
|
| 65 | 67 |
typedef typename LengthMap::Value Length; |
| 66 | 68 |
typedef ConstMap<Arc, int> ConstArcMap; |
| 67 | 69 |
typedef typename Digraph::template NodeMap<Arc> PredMap; |
| 68 | 70 |
|
| 69 | 71 |
public: |
| 70 | 72 |
|
| 71 | 73 |
/// The type of the flow map. |
| 72 | 74 |
typedef typename Digraph::template ArcMap<int> FlowMap; |
| 73 | 75 |
/// The type of the potential map. |
| 74 | 76 |
typedef typename Digraph::template NodeMap<Length> PotentialMap; |
| 75 | 77 |
/// The type of the path structures. |
| 76 | 78 |
typedef SimplePath<Digraph> Path; |
| 77 | 79 |
|
| 78 | 80 |
private: |
| 79 | 81 |
|
| 80 | 82 |
/// \brief Special implementation of the Dijkstra algorithm |
| 81 | 83 |
/// for finding shortest paths in the residual network. |
| 82 | 84 |
/// |
| 83 | 85 |
/// \ref ResidualDijkstra is a special implementation of the |
| 84 | 86 |
/// \ref Dijkstra algorithm for finding shortest paths in the |
| 85 | 87 |
/// residual network of the digraph with respect to the reduced arc |
| 86 | 88 |
/// lengths and modifying the node potentials according to the |
| 87 | 89 |
/// distance of the nodes. |
| 88 | 90 |
class ResidualDijkstra |
| 89 | 91 |
{
|
| 90 | 92 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 91 | 93 |
typedef BinHeap<Length, HeapCrossRef> Heap; |
| 92 | 94 |
|
| 93 | 95 |
private: |
| 94 | 96 |
|
| 95 | 97 |
// The digraph the algorithm runs on |
| 96 | 98 |
const Digraph &_graph; |
| 97 | 99 |
|
| 98 | 100 |
// The main maps |
| 99 | 101 |
const FlowMap &_flow; |
| 100 | 102 |
const LengthMap &_length; |
| 101 | 103 |
PotentialMap &_potential; |
| 102 | 104 |
|
| 103 | 105 |
// The distance map |
| 104 | 106 |
PotentialMap _dist; |
| 105 | 107 |
// The pred arc map |
| 106 | 108 |
PredMap &_pred; |
| 107 | 109 |
// The processed (i.e. permanently labeled) nodes |
| 108 | 110 |
std::vector<Node> _proc_nodes; |
| 109 | 111 |
|
| 110 | 112 |
Node _s; |
| 111 | 113 |
Node _t; |
| 112 | 114 |
|
| 113 | 115 |
public: |
| 114 | 116 |
|
| 115 | 117 |
/// Constructor. |
| 116 | 118 |
ResidualDijkstra( const Digraph &digraph, |
| 117 | 119 |
const FlowMap &flow, |
| 118 | 120 |
const LengthMap &length, |
| 119 | 121 |
PotentialMap &potential, |
| 120 | 122 |
PredMap &pred, |
| 121 | 123 |
Node s, Node t ) : |
| 122 | 124 |
_graph(digraph), _flow(flow), _length(length), _potential(potential), |
| 123 | 125 |
_dist(digraph), _pred(pred), _s(s), _t(t) {}
|
| 124 | 126 |
|
| 125 | 127 |
/// \brief Run the algorithm. It returns \c true if a path is found |
| 126 | 128 |
/// from the source node to the target node. |
| 127 | 129 |
bool run() {
|
| 128 | 130 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
| 129 | 131 |
Heap heap(heap_cross_ref); |
| 130 | 132 |
heap.push(_s, 0); |
| 131 | 133 |
_pred[_s] = INVALID; |
| 132 | 134 |
_proc_nodes.clear(); |
| 133 | 135 |
|
| 134 | 136 |
// Process nodes |
| 135 | 137 |
while (!heap.empty() && heap.top() != _t) {
|
| 136 | 138 |
Node u = heap.top(), v; |
| 137 | 139 |
Length d = heap.prio() + _potential[u], nd; |
| 138 | 140 |
_dist[u] = heap.prio(); |
| 139 | 141 |
heap.pop(); |
| 140 | 142 |
_proc_nodes.push_back(u); |
| 141 | 143 |
|
| 142 | 144 |
// Traverse outgoing arcs |
| 143 | 145 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
|
| 144 | 146 |
if (_flow[e] == 0) {
|
| 145 | 147 |
v = _graph.target(e); |
| 146 | 148 |
switch(heap.state(v)) {
|
| 147 | 149 |
case Heap::PRE_HEAP: |
| 148 | 150 |
heap.push(v, d + _length[e] - _potential[v]); |
| 149 | 151 |
_pred[v] = e; |
| 150 | 152 |
break; |
| 151 | 153 |
case Heap::IN_HEAP: |
| 152 | 154 |
nd = d + _length[e] - _potential[v]; |
| 153 | 155 |
if (nd < heap[v]) {
|
| 154 | 156 |
heap.decrease(v, nd); |
| 155 | 157 |
_pred[v] = e; |
| 156 | 158 |
} |
| 157 | 159 |
break; |
| 158 | 160 |
case Heap::POST_HEAP: |
| 159 | 161 |
break; |
| 160 | 162 |
} |
| 161 | 163 |
} |
| 162 | 164 |
} |
| 163 | 165 |
|
| 164 | 166 |
// Traverse incoming arcs |
| 165 | 167 |
for (InArcIt e(_graph, u); e != INVALID; ++e) {
|
| 166 | 168 |
if (_flow[e] == 1) {
|
| 167 | 169 |
v = _graph.source(e); |
| 168 | 170 |
switch(heap.state(v)) {
|
| 169 | 171 |
case Heap::PRE_HEAP: |
| 170 | 172 |
heap.push(v, d - _length[e] - _potential[v]); |
| 171 | 173 |
_pred[v] = e; |
| 172 | 174 |
break; |
| 173 | 175 |
case Heap::IN_HEAP: |
| 174 | 176 |
nd = d - _length[e] - _potential[v]; |
| 175 | 177 |
if (nd < heap[v]) {
|
| 176 | 178 |
heap.decrease(v, nd); |
| 177 | 179 |
_pred[v] = e; |
| 178 | 180 |
} |
| 179 | 181 |
break; |
| 180 | 182 |
case Heap::POST_HEAP: |
| 181 | 183 |
break; |
| 182 | 184 |
} |
| 183 | 185 |
} |
| 184 | 186 |
} |
| 185 | 187 |
} |
| 186 | 188 |
if (heap.empty()) return false; |
| 187 | 189 |
|
| 188 | 190 |
// Update potentials of processed nodes |
| 189 | 191 |
Length t_dist = heap.prio(); |
| 190 | 192 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) |
| 191 | 193 |
_potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist; |
| 192 | 194 |
return true; |
| 193 | 195 |
} |
| 194 | 196 |
|
| 195 | 197 |
}; //class ResidualDijkstra |
| 196 | 198 |
|
| 197 | 199 |
private: |
| 198 | 200 |
|
| 199 | 201 |
// The digraph the algorithm runs on |
| 200 | 202 |
const Digraph &_graph; |
| 201 | 203 |
// The length map |
| 202 | 204 |
const LengthMap &_length; |
| 203 | 205 |
|
| 204 | 206 |
// Arc map of the current flow |
| 205 | 207 |
FlowMap *_flow; |
| 206 | 208 |
bool _local_flow; |
| 207 | 209 |
// Node map of the current potentials |
| 208 | 210 |
PotentialMap *_potential; |
| 209 | 211 |
bool _local_potential; |
| 210 | 212 |
|
| 211 | 213 |
// The source node |
| 212 | 214 |
Node _source; |
| 213 | 215 |
// The target node |
| 214 | 216 |
Node _target; |
| 215 | 217 |
|
| 216 | 218 |
// Container to store the found paths |
| 217 | 219 |
std::vector< SimplePath<Digraph> > paths; |
| 218 | 220 |
int _path_num; |
| 219 | 221 |
|
| 220 | 222 |
// The pred arc map |
| 221 | 223 |
PredMap _pred; |
| 222 | 224 |
// Implementation of the Dijkstra algorithm for finding augmenting |
| 223 | 225 |
// shortest paths in the residual network |
| 224 | 226 |
ResidualDijkstra *_dijkstra; |
| 225 | 227 |
|
| 226 | 228 |
public: |
| 227 | 229 |
|
| 228 | 230 |
/// \brief Constructor. |
| 229 | 231 |
/// |
| 230 | 232 |
/// Constructor. |
| 231 | 233 |
/// |
| 232 | 234 |
/// \param digraph The digraph the algorithm runs on. |
| 233 | 235 |
/// \param length The length (cost) values of the arcs. |
| 234 | 236 |
/// \param s The source node. |
| 235 | 237 |
/// \param t The target node. |
| 236 | 238 |
Suurballe( const Digraph &digraph, |
| 237 | 239 |
const LengthMap &length, |
| 238 | 240 |
Node s, Node t ) : |
| 239 | 241 |
_graph(digraph), _length(length), _flow(0), _local_flow(false), |
| 240 | 242 |
_potential(0), _local_potential(false), _source(s), _target(t), |
| 241 | 243 |
_pred(digraph) {}
|
| 242 | 244 |
|
| 243 | 245 |
/// Destructor. |
| 244 | 246 |
~Suurballe() {
|
| 245 | 247 |
if (_local_flow) delete _flow; |
| 246 | 248 |
if (_local_potential) delete _potential; |
| 247 | 249 |
delete _dijkstra; |
| 248 | 250 |
} |
| 249 | 251 |
|
| 250 | 252 |
/// \brief Set the flow map. |
| 251 | 253 |
/// |
| 252 | 254 |
/// This function sets the flow map. |
| 253 | 255 |
/// |
| 254 | 256 |
/// The found flow contains only 0 and 1 values. It is the union of |
| 255 | 257 |
/// the found arc-disjoint paths. |
| 256 | 258 |
/// |
| 257 | 259 |
/// \return \c (*this) |
| 258 | 260 |
Suurballe& flowMap(FlowMap &map) {
|
| 259 | 261 |
if (_local_flow) {
|
| 260 | 262 |
delete _flow; |
| 261 | 263 |
_local_flow = false; |
| 262 | 264 |
} |
| 263 | 265 |
_flow = ↦ |
| 264 | 266 |
return *this; |
| 265 | 267 |
} |
| 266 | 268 |
|
| 267 | 269 |
/// \brief Set the potential map. |
| 268 | 270 |
/// |
| 269 | 271 |
/// This function sets the potential map. |
| 270 | 272 |
/// |
| 271 | 273 |
/// The potentials provide the dual solution of the underlying |
| 272 | 274 |
/// minimum cost flow problem. |
| 273 | 275 |
/// |
| 274 | 276 |
/// \return \c (*this) |
| 275 | 277 |
Suurballe& potentialMap(PotentialMap &map) {
|
| 276 | 278 |
if (_local_potential) {
|
| 277 | 279 |
delete _potential; |
| 278 | 280 |
_local_potential = false; |
| 279 | 281 |
} |
| 280 | 282 |
_potential = ↦ |
| 281 | 283 |
return *this; |
| 282 | 284 |
} |
| 283 | 285 |
|
| 284 | 286 |
/// \name Execution control |
| 285 | 287 |
/// The simplest way to execute the algorithm is to call the run() |
| 286 | 288 |
/// function. |
| 287 | 289 |
/// \n |
| 288 | 290 |
/// If you only need the flow that is the union of the found |
| 289 | 291 |
/// arc-disjoint paths, you may call init() and findFlow(). |
| 290 | 292 |
|
| 291 | 293 |
/// @{
|
| 292 | 294 |
|
| 293 | 295 |
/// \brief Run the algorithm. |
| 294 | 296 |
/// |
| 295 | 297 |
/// This function runs the algorithm. |
| 296 | 298 |
/// |
| 297 | 299 |
/// \param k The number of paths to be found. |
| 298 | 300 |
/// |
| 299 | 301 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 300 | 302 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
| 301 | 303 |
/// arc-disjoint paths found. |
| 302 | 304 |
/// |
| 303 | 305 |
/// \note Apart from the return value, <tt>s.run(k)</tt> is just a |
| 304 | 306 |
/// shortcut of the following code. |
| 305 | 307 |
/// \code |
| 306 | 308 |
/// s.init(); |
| 307 | 309 |
/// s.findFlow(k); |
| 308 | 310 |
/// s.findPaths(); |
| 309 | 311 |
/// \endcode |
| 310 | 312 |
int run(int k = 2) {
|
| 311 | 313 |
init(); |
| 312 | 314 |
findFlow(k); |
| 313 | 315 |
findPaths(); |
| 314 | 316 |
return _path_num; |
| 315 | 317 |
} |
| 316 | 318 |
|
| 317 | 319 |
/// \brief Initialize the algorithm. |
| 318 | 320 |
/// |
| 319 | 321 |
/// This function initializes the algorithm. |
| 320 | 322 |
void init() {
|
| 321 | 323 |
// Initialize maps |
| 322 | 324 |
if (!_flow) {
|
| 323 | 325 |
_flow = new FlowMap(_graph); |
| 324 | 326 |
_local_flow = true; |
| 325 | 327 |
} |
| 326 | 328 |
if (!_potential) {
|
| 327 | 329 |
_potential = new PotentialMap(_graph); |
| 328 | 330 |
_local_potential = true; |
| 329 | 331 |
} |
| 330 | 332 |
for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0; |
| 331 | 333 |
for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0; |
| 332 | 334 |
|
| 333 | 335 |
_dijkstra = new ResidualDijkstra( _graph, *_flow, _length, |
| 334 | 336 |
*_potential, _pred, |
| 335 | 337 |
_source, _target ); |
| 336 | 338 |
} |
| 337 | 339 |
|
| 338 | 340 |
/// \brief Execute the successive shortest path algorithm to find |
| 339 | 341 |
/// an optimal flow. |
| 340 | 342 |
/// |
| 341 | 343 |
/// This function executes the successive shortest path algorithm to |
| 342 | 344 |
/// find a minimum cost flow, which is the union of \c k or less |
| 343 | 345 |
/// arc-disjoint paths. |
| 344 | 346 |
/// |
| 345 | 347 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 346 | 348 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
| 347 | 349 |
/// arc-disjoint paths found. |
| 348 | 350 |
/// |
| 349 | 351 |
/// \pre \ref init() must be called before using this function. |
| 350 | 352 |
int findFlow(int k = 2) {
|
| 351 | 353 |
// Find shortest paths |
| 352 | 354 |
_path_num = 0; |
| 353 | 355 |
while (_path_num < k) {
|
| 354 | 356 |
// Run Dijkstra |
| 355 | 357 |
if (!_dijkstra->run()) break; |
| 356 | 358 |
++_path_num; |
| 357 | 359 |
|
| 358 | 360 |
// Set the flow along the found shortest path |
| 359 | 361 |
Node u = _target; |
| 360 | 362 |
Arc e; |
| 361 | 363 |
while ((e = _pred[u]) != INVALID) {
|
| 362 | 364 |
if (u == _graph.target(e)) {
|
| 363 | 365 |
(*_flow)[e] = 1; |
| 364 | 366 |
u = _graph.source(e); |
| 365 | 367 |
} else {
|
| 366 | 368 |
(*_flow)[e] = 0; |
| 367 | 369 |
u = _graph.target(e); |
| 368 | 370 |
} |
| 369 | 371 |
} |
| 370 | 372 |
} |
| 371 | 373 |
return _path_num; |
| 372 | 374 |
} |
| 373 | 375 |
|
| 374 | 376 |
/// \brief Compute the paths from the flow. |
| 375 | 377 |
/// |
| 376 | 378 |
/// This function computes the paths from the flow. |
| 377 | 379 |
/// |
| 378 | 380 |
/// \pre \ref init() and \ref findFlow() must be called before using |
| 379 | 381 |
/// this function. |
| 380 | 382 |
void findPaths() {
|
| 381 | 383 |
// Create the residual flow map (the union of the paths not found |
| 382 | 384 |
// so far) |
| 383 | 385 |
FlowMap res_flow(_graph); |
| 384 | 386 |
for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a]; |
| 385 | 387 |
|
| 386 | 388 |
paths.clear(); |
| 387 | 389 |
paths.resize(_path_num); |
| 388 | 390 |
for (int i = 0; i < _path_num; ++i) {
|
| 389 | 391 |
Node n = _source; |
| 390 | 392 |
while (n != _target) {
|
| 391 | 393 |
OutArcIt e(_graph, n); |
| 392 | 394 |
for ( ; res_flow[e] == 0; ++e) ; |
| 393 | 395 |
n = _graph.target(e); |
| 394 | 396 |
paths[i].addBack(e); |
| 395 | 397 |
res_flow[e] = 0; |
| 396 | 398 |
} |
| 397 | 399 |
} |
| 398 | 400 |
} |
| 399 | 401 |
|
| 400 | 402 |
/// @} |
| 401 | 403 |
|
| 402 | 404 |
/// \name Query Functions |
| 403 | 405 |
/// The results of the algorithm can be obtained using these |
| 404 | 406 |
/// functions. |
| 405 | 407 |
/// \n The algorithm should be executed before using them. |
| 406 | 408 |
|
| 407 | 409 |
/// @{
|
| 408 | 410 |
|
| 409 | 411 |
/// \brief Return a const reference to the arc map storing the |
| 410 | 412 |
/// found flow. |
| 411 | 413 |
/// |
| 412 | 414 |
/// This function returns a const reference to the arc map storing |
| 413 | 415 |
/// the flow that is the union of the found arc-disjoint paths. |
| 414 | 416 |
/// |
| 415 | 417 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 416 | 418 |
/// this function. |
| 417 | 419 |
const FlowMap& flowMap() const {
|
| 418 | 420 |
return *_flow; |
| 419 | 421 |
} |
| 420 | 422 |
|
| 421 | 423 |
/// \brief Return a const reference to the node map storing the |
| 422 | 424 |
/// found potentials (the dual solution). |
| 423 | 425 |
/// |
| 424 | 426 |
/// This function returns a const reference to the node map storing |
| 425 | 427 |
/// the found potentials that provide the dual solution of the |
| 426 | 428 |
/// underlying minimum cost flow problem. |
| 427 | 429 |
/// |
| 428 | 430 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 429 | 431 |
/// this function. |
| 430 | 432 |
const PotentialMap& potentialMap() const {
|
| 431 | 433 |
return *_potential; |
| 432 | 434 |
} |
| 433 | 435 |
|
| 434 | 436 |
/// \brief Return the flow on the given arc. |
| 435 | 437 |
/// |
| 436 | 438 |
/// This function returns the flow on the given arc. |
| 437 | 439 |
/// It is \c 1 if the arc is involved in one of the found paths, |
| 438 | 440 |
/// otherwise it is \c 0. |
| 439 | 441 |
/// |
| 440 | 442 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 441 | 443 |
/// this function. |
| 442 | 444 |
int flow(const Arc& arc) const {
|
| 443 | 445 |
return (*_flow)[arc]; |
| 444 | 446 |
} |
| 445 | 447 |
|
| 446 | 448 |
/// \brief Return the potential of the given node. |
| 447 | 449 |
/// |
| 448 | 450 |
/// This function returns the potential of the given node. |
| 449 | 451 |
/// |
| 450 | 452 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 451 | 453 |
/// this function. |
| 452 | 454 |
Length potential(const Node& node) const {
|
| 453 | 455 |
return (*_potential)[node]; |
| 454 | 456 |
} |
| 455 | 457 |
|
| 456 | 458 |
/// \brief Return the total length (cost) of the found paths (flow). |
| 457 | 459 |
/// |
| 458 | 460 |
/// This function returns the total length (cost) of the found paths |
| 459 | 461 |
/// (flow). The complexity of the function is \f$ O(e) \f$. |
| 460 | 462 |
/// |
| 461 | 463 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 462 | 464 |
/// this function. |
| 463 | 465 |
Length totalLength() const {
|
| 464 | 466 |
Length c = 0; |
| 465 | 467 |
for (ArcIt e(_graph); e != INVALID; ++e) |
| 466 | 468 |
c += (*_flow)[e] * _length[e]; |
| 467 | 469 |
return c; |
| 468 | 470 |
} |
| 469 | 471 |
|
| 470 | 472 |
/// \brief Return the number of the found paths. |
| 471 | 473 |
/// |
| 472 | 474 |
/// This function returns the number of the found paths. |
| 473 | 475 |
/// |
| 474 | 476 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 475 | 477 |
/// this function. |
| 476 | 478 |
int pathNum() const {
|
| 477 | 479 |
return _path_num; |
| 478 | 480 |
} |
| 479 | 481 |
|
| 480 | 482 |
/// \brief Return a const reference to the specified path. |
| 481 | 483 |
/// |
| 482 | 484 |
/// This function returns a const reference to the specified path. |
| 483 | 485 |
/// |
| 484 | 486 |
/// \param i The function returns the \c i-th path. |
| 485 | 487 |
/// \c i must be between \c 0 and <tt>%pathNum()-1</tt>. |
| 486 | 488 |
/// |
| 487 | 489 |
/// \pre \ref run() or \ref findPaths() must be called before using |
| 488 | 490 |
/// this function. |
| 489 | 491 |
Path path(int i) const {
|
| 490 | 492 |
return paths[i]; |
| 491 | 493 |
} |
| 492 | 494 |
|
| 493 | 495 |
/// @} |
| 494 | 496 |
|
| 495 | 497 |
}; //class Suurballe |
| 496 | 498 |
|
| 497 | 499 |
///@} |
| 498 | 500 |
|
| 499 | 501 |
} //namespace lemon |
| 500 | 502 |
|
| 501 | 503 |
#endif //LEMON_SUURBALLE_H |
0 comments (0 inline)