0
5
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_FULL_GRAPH_H |
| 20 | 20 |
#define LEMON_FULL_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/graph_extender.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup graphs |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief FullGraph and FullDigraph classes. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
class FullDigraphBase {
|
| 32 | 32 |
public: |
| 33 | 33 |
|
| 34 | 34 |
typedef FullDigraphBase Graph; |
| 35 | 35 |
|
| 36 | 36 |
class Node; |
| 37 | 37 |
class Arc; |
| 38 | 38 |
|
| 39 | 39 |
protected: |
| 40 | 40 |
|
| 41 | 41 |
int _node_num; |
| 42 | 42 |
int _arc_num; |
| 43 | 43 |
|
| 44 | 44 |
FullDigraphBase() {}
|
| 45 | 45 |
|
| 46 | 46 |
void construct(int n) { _node_num = n; _arc_num = n * n; }
|
| 47 | 47 |
|
| 48 | 48 |
public: |
| 49 | 49 |
|
| 50 | 50 |
typedef True NodeNumTag; |
| 51 | 51 |
typedef True ArcNumTag; |
| 52 | 52 |
|
| 53 | 53 |
Node operator()(int ix) const { return Node(ix); }
|
| 54 | 54 |
int index(const Node& node) const { return node._id; }
|
| 55 | 55 |
|
| 56 | 56 |
Arc arc(const Node& s, const Node& t) const {
|
| 57 | 57 |
return Arc(s._id * _node_num + t._id); |
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
int nodeNum() const { return _node_num; }
|
| 61 | 61 |
int arcNum() const { return _arc_num; }
|
| 62 | 62 |
|
| 63 | 63 |
int maxNodeId() const { return _node_num - 1; }
|
| 64 | 64 |
int maxArcId() const { return _arc_num - 1; }
|
| 65 | 65 |
|
| 66 | 66 |
Node source(Arc arc) const { return arc._id / _node_num; }
|
| 67 | 67 |
Node target(Arc arc) const { return arc._id % _node_num; }
|
| 68 | 68 |
|
| 69 | 69 |
static int id(Node node) { return node._id; }
|
| 70 | 70 |
static int id(Arc arc) { return arc._id; }
|
| 71 | 71 |
|
| 72 | 72 |
static Node nodeFromId(int id) { return Node(id);}
|
| 73 | 73 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 74 | 74 |
|
| 75 | 75 |
typedef True FindArcTag; |
| 76 | 76 |
|
| 77 | 77 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const {
|
| 78 | 78 |
return prev == INVALID ? arc(s, t) : INVALID; |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
class Node {
|
| 82 | 82 |
friend class FullDigraphBase; |
| 83 | 83 |
|
| 84 | 84 |
protected: |
| 85 | 85 |
int _id; |
| 86 | 86 |
Node(int id) : _id(id) {}
|
| 87 | 87 |
public: |
| 88 | 88 |
Node() {}
|
| 89 | 89 |
Node (Invalid) : _id(-1) {}
|
| 90 | 90 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 91 | 91 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 92 | 92 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 93 | 93 |
}; |
| 94 | 94 |
|
| 95 | 95 |
class Arc {
|
| 96 | 96 |
friend class FullDigraphBase; |
| 97 | 97 |
|
| 98 | 98 |
protected: |
| 99 | 99 |
int _id; // _node_num * source + target; |
| 100 | 100 |
|
| 101 | 101 |
Arc(int id) : _id(id) {}
|
| 102 | 102 |
|
| 103 | 103 |
public: |
| 104 | 104 |
Arc() { }
|
| 105 | 105 |
Arc (Invalid) { _id = -1; }
|
| 106 | 106 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 107 | 107 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 108 | 108 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 109 | 109 |
}; |
| 110 | 110 |
|
| 111 | 111 |
void first(Node& node) const {
|
| 112 | 112 |
node._id = _node_num - 1; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
static void next(Node& node) {
|
| 116 | 116 |
--node._id; |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
void first(Arc& arc) const {
|
| 120 | 120 |
arc._id = _arc_num - 1; |
| 121 | 121 |
} |
| 122 | 122 |
|
| 123 | 123 |
static void next(Arc& arc) {
|
| 124 | 124 |
--arc._id; |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 | 127 |
void firstOut(Arc& arc, const Node& node) const {
|
| 128 | 128 |
arc._id = (node._id + 1) * _node_num - 1; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
void nextOut(Arc& arc) const {
|
| 132 | 132 |
if (arc._id % _node_num == 0) arc._id = 0; |
| 133 | 133 |
--arc._id; |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
void firstIn(Arc& arc, const Node& node) const {
|
| 137 | 137 |
arc._id = _arc_num + node._id - _node_num; |
| 138 | 138 |
} |
| 139 | 139 |
|
| 140 | 140 |
void nextIn(Arc& arc) const {
|
| 141 | 141 |
arc._id -= _node_num; |
| 142 | 142 |
if (arc._id < 0) arc._id = -1; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
}; |
| 146 | 146 |
|
| 147 | 147 |
typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase; |
| 148 | 148 |
|
| 149 | 149 |
/// \ingroup graphs |
| 150 | 150 |
/// |
| 151 | 151 |
/// \brief A full digraph class. |
| 152 | 152 |
/// |
| 153 | 153 |
/// This is a simple and fast directed full graph implementation. |
| 154 | 154 |
/// From each node go arcs to each node (including the source node), |
| 155 | 155 |
/// therefore the number of the arcs in the digraph is the square of |
| 156 | 156 |
/// the node number. This digraph type is completely static, so you |
| 157 | 157 |
/// can neither add nor delete either arcs or nodes, and it needs |
| 158 | 158 |
/// constant space in memory. |
| 159 | 159 |
/// |
| 160 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept |
|
| 161 |
/// and it also has an important extra feature that its maps are |
|
| 162 |
/// |
|
| 160 |
/// This class fully conforms to the \ref concepts::Digraph |
|
| 161 |
/// "Digraph concept". |
|
| 163 | 162 |
/// |
| 164 | 163 |
/// The \c FullDigraph and \c FullGraph classes are very similar, |
| 165 | 164 |
/// but there are two differences. While this class conforms only |
| 166 | 165 |
/// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph |
| 167 | 166 |
/// class conforms to the \ref concepts::Graph "Graph" concept, |
| 168 | 167 |
/// moreover \c FullGraph does not contain a loop arc for each |
| 169 | 168 |
/// node as \c FullDigraph does. |
| 170 | 169 |
/// |
| 171 | 170 |
/// \sa FullGraph |
| 172 | 171 |
class FullDigraph : public ExtendedFullDigraphBase {
|
| 173 | 172 |
public: |
| 174 | 173 |
|
| 175 | 174 |
typedef ExtendedFullDigraphBase Parent; |
| 176 | 175 |
|
| 177 | 176 |
/// \brief Constructor |
| 178 | 177 |
FullDigraph() { construct(0); }
|
| 179 | 178 |
|
| 180 | 179 |
/// \brief Constructor |
| 181 | 180 |
/// |
| 182 | 181 |
/// Constructor. |
| 183 | 182 |
/// \param n The number of the nodes. |
| 184 | 183 |
FullDigraph(int n) { construct(n); }
|
| 185 | 184 |
|
| 186 | 185 |
/// \brief Resizes the digraph |
| 187 | 186 |
/// |
| 188 | 187 |
/// Resizes the digraph. The function will fully destroy and |
| 189 | 188 |
/// rebuild the digraph. This cause that the maps of the digraph will |
| 190 | 189 |
/// reallocated automatically and the previous values will be lost. |
| 191 | 190 |
void resize(int n) {
|
| 192 | 191 |
Parent::notifier(Arc()).clear(); |
| 193 | 192 |
Parent::notifier(Node()).clear(); |
| 194 | 193 |
construct(n); |
| 195 | 194 |
Parent::notifier(Node()).build(); |
| 196 | 195 |
Parent::notifier(Arc()).build(); |
| 197 | 196 |
} |
| 198 | 197 |
|
| 199 | 198 |
/// \brief Returns the node with the given index. |
| 200 | 199 |
/// |
| 201 | 200 |
/// Returns the node with the given index. Since it is a static |
| 202 | 201 |
/// digraph its nodes can be indexed with integers from the range |
| 203 | 202 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 204 | 203 |
/// \sa index() |
| 205 | 204 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
| 206 | 205 |
|
| 207 | 206 |
/// \brief Returns the index of the given node. |
| 208 | 207 |
/// |
| 209 | 208 |
/// Returns the index of the given node. Since it is a static |
| 210 | 209 |
/// digraph its nodes can be indexed with integers from the range |
| 211 | 210 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 212 | 211 |
/// \sa operator() |
| 213 | 212 |
int index(const Node& node) const { return Parent::index(node); }
|
| 214 | 213 |
|
| 215 | 214 |
/// \brief Returns the arc connecting the given nodes. |
| 216 | 215 |
/// |
| 217 | 216 |
/// Returns the arc connecting the given nodes. |
| 218 | 217 |
Arc arc(const Node& u, const Node& v) const {
|
| 219 | 218 |
return Parent::arc(u, v); |
| 220 | 219 |
} |
| 221 | 220 |
|
| 222 | 221 |
/// \brief Number of nodes. |
| 223 | 222 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 224 | 223 |
/// \brief Number of arcs. |
| 225 | 224 |
int arcNum() const { return Parent::arcNum(); }
|
| 226 | 225 |
}; |
| 227 | 226 |
|
| 228 | 227 |
|
| 229 | 228 |
class FullGraphBase {
|
| 230 | 229 |
int _node_num; |
| 231 | 230 |
int _edge_num; |
| 232 | 231 |
public: |
| 233 | 232 |
|
| 234 | 233 |
typedef FullGraphBase Graph; |
| 235 | 234 |
|
| 236 | 235 |
class Node; |
| 237 | 236 |
class Arc; |
| 238 | 237 |
class Edge; |
| 239 | 238 |
|
| 240 | 239 |
protected: |
| 241 | 240 |
|
| 242 | 241 |
FullGraphBase() {}
|
| 243 | 242 |
|
| 244 | 243 |
void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; }
|
| 245 | 244 |
|
| 246 | 245 |
int _uid(int e) const {
|
| 247 | 246 |
int u = e / _node_num; |
| 248 | 247 |
int v = e % _node_num; |
| 249 | 248 |
return u < v ? u : _node_num - 2 - u; |
| 250 | 249 |
} |
| 251 | 250 |
|
| 252 | 251 |
int _vid(int e) const {
|
| 253 | 252 |
int u = e / _node_num; |
| 254 | 253 |
int v = e % _node_num; |
| 255 | 254 |
return u < v ? v : _node_num - 1 - v; |
| 256 | 255 |
} |
| 257 | 256 |
|
| 258 | 257 |
void _uvid(int e, int& u, int& v) const {
|
| 259 | 258 |
u = e / _node_num; |
| 260 | 259 |
v = e % _node_num; |
| 261 | 260 |
if (u >= v) {
|
| 262 | 261 |
u = _node_num - 2 - u; |
| 263 | 262 |
v = _node_num - 1 - v; |
| 264 | 263 |
} |
| 265 | 264 |
} |
| 266 | 265 |
|
| 267 | 266 |
void _stid(int a, int& s, int& t) const {
|
| 268 | 267 |
if ((a & 1) == 1) {
|
| 269 | 268 |
_uvid(a >> 1, s, t); |
| 270 | 269 |
} else {
|
| 271 | 270 |
_uvid(a >> 1, t, s); |
| 272 | 271 |
} |
| 273 | 272 |
} |
| 274 | 273 |
|
| 275 | 274 |
int _eid(int u, int v) const {
|
| 276 | 275 |
if (u < (_node_num - 1) / 2) {
|
| 277 | 276 |
return u * _node_num + v; |
| 278 | 277 |
} else {
|
| 279 | 278 |
return (_node_num - 1 - u) * _node_num - v - 1; |
| 280 | 279 |
} |
| 281 | 280 |
} |
| 282 | 281 |
|
| 283 | 282 |
public: |
| 284 | 283 |
|
| 285 | 284 |
Node operator()(int ix) const { return Node(ix); }
|
| 286 | 285 |
int index(const Node& node) const { return node._id; }
|
| 287 | 286 |
|
| 288 | 287 |
Edge edge(const Node& u, const Node& v) const {
|
| 289 | 288 |
if (u._id < v._id) {
|
| 290 | 289 |
return Edge(_eid(u._id, v._id)); |
| 291 | 290 |
} else if (u._id != v._id) {
|
| 292 | 291 |
return Edge(_eid(v._id, u._id)); |
| 293 | 292 |
} else {
|
| 294 | 293 |
return INVALID; |
| 295 | 294 |
} |
| 296 | 295 |
} |
| 297 | 296 |
|
| 298 | 297 |
Arc arc(const Node& s, const Node& t) const {
|
| 299 | 298 |
if (s._id < t._id) {
|
| 300 | 299 |
return Arc((_eid(s._id, t._id) << 1) | 1); |
| 301 | 300 |
} else if (s._id != t._id) {
|
| 302 | 301 |
return Arc(_eid(t._id, s._id) << 1); |
| 303 | 302 |
} else {
|
| 304 | 303 |
return INVALID; |
| 305 | 304 |
} |
| 306 | 305 |
} |
| 307 | 306 |
|
| 308 | 307 |
typedef True NodeNumTag; |
| 309 | 308 |
typedef True ArcNumTag; |
| 310 | 309 |
typedef True EdgeNumTag; |
| 311 | 310 |
|
| 312 | 311 |
int nodeNum() const { return _node_num; }
|
| 313 | 312 |
int arcNum() const { return 2 * _edge_num; }
|
| 314 | 313 |
int edgeNum() const { return _edge_num; }
|
| 315 | 314 |
|
| 316 | 315 |
static int id(Node node) { return node._id; }
|
| 317 | 316 |
static int id(Arc arc) { return arc._id; }
|
| 318 | 317 |
static int id(Edge edge) { return edge._id; }
|
| 319 | 318 |
|
| 320 | 319 |
int maxNodeId() const { return _node_num-1; }
|
| 321 | 320 |
int maxArcId() const { return 2 * _edge_num-1; }
|
| 322 | 321 |
int maxEdgeId() const { return _edge_num-1; }
|
| 323 | 322 |
|
| 324 | 323 |
static Node nodeFromId(int id) { return Node(id);}
|
| 325 | 324 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 326 | 325 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 327 | 326 |
|
| 328 | 327 |
Node u(Edge edge) const {
|
| 329 | 328 |
return Node(_uid(edge._id)); |
| 330 | 329 |
} |
| 331 | 330 |
|
| 332 | 331 |
Node v(Edge edge) const {
|
| 333 | 332 |
return Node(_vid(edge._id)); |
| 334 | 333 |
} |
| 335 | 334 |
|
| 336 | 335 |
Node source(Arc arc) const {
|
| 337 | 336 |
return Node((arc._id & 1) == 1 ? |
| 338 | 337 |
_uid(arc._id >> 1) : _vid(arc._id >> 1)); |
| 339 | 338 |
} |
| 340 | 339 |
|
| 341 | 340 |
Node target(Arc arc) const {
|
| 342 | 341 |
return Node((arc._id & 1) == 1 ? |
| 343 | 342 |
_vid(arc._id >> 1) : _uid(arc._id >> 1)); |
| 344 | 343 |
} |
| 345 | 344 |
|
| 346 | 345 |
typedef True FindEdgeTag; |
| 347 | 346 |
typedef True FindArcTag; |
| 348 | 347 |
|
| 349 | 348 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
| 350 | 349 |
return prev != INVALID ? INVALID : edge(u, v); |
| 351 | 350 |
} |
| 352 | 351 |
|
| 353 | 352 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const {
|
| 354 | 353 |
return prev != INVALID ? INVALID : arc(s, t); |
| 355 | 354 |
} |
| 356 | 355 |
|
| 357 | 356 |
class Node {
|
| 358 | 357 |
friend class FullGraphBase; |
| 359 | 358 |
|
| 360 | 359 |
protected: |
| 361 | 360 |
int _id; |
| 362 | 361 |
Node(int id) : _id(id) {}
|
| 363 | 362 |
public: |
| 364 | 363 |
Node() {}
|
| 365 | 364 |
Node (Invalid) { _id = -1; }
|
| 366 | 365 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 367 | 366 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 368 | 367 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 369 | 368 |
}; |
| 370 | 369 |
|
| 371 | 370 |
class Edge {
|
| 372 | 371 |
friend class FullGraphBase; |
| 373 | 372 |
friend class Arc; |
| 374 | 373 |
|
| 375 | 374 |
protected: |
| 376 | 375 |
int _id; |
| 377 | 376 |
|
| 378 | 377 |
Edge(int id) : _id(id) {}
|
| 379 | 378 |
|
| 380 | 379 |
public: |
| 381 | 380 |
Edge() { }
|
| 382 | 381 |
Edge (Invalid) { _id = -1; }
|
| 383 | 382 |
|
| 384 | 383 |
bool operator==(const Edge edge) const {return _id == edge._id;}
|
| 385 | 384 |
bool operator!=(const Edge edge) const {return _id != edge._id;}
|
| 386 | 385 |
bool operator<(const Edge edge) const {return _id < edge._id;}
|
| 387 | 386 |
}; |
| 388 | 387 |
|
| 389 | 388 |
class Arc {
|
| 390 | 389 |
friend class FullGraphBase; |
| 391 | 390 |
|
| 392 | 391 |
protected: |
| 393 | 392 |
int _id; |
| 394 | 393 |
|
| 395 | 394 |
Arc(int id) : _id(id) {}
|
| 396 | 395 |
|
| 397 | 396 |
public: |
| 398 | 397 |
Arc() { }
|
| 399 | 398 |
Arc (Invalid) { _id = -1; }
|
| 400 | 399 |
|
| 401 | 400 |
operator Edge() const { return Edge(_id != -1 ? (_id >> 1) : -1); }
|
| 402 | 401 |
|
| 403 | 402 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 404 | 403 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 405 | 404 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 406 | 405 |
}; |
| 407 | 406 |
|
| 408 | 407 |
static bool direction(Arc arc) {
|
| 409 | 408 |
return (arc._id & 1) == 1; |
| 410 | 409 |
} |
| 411 | 410 |
|
| 412 | 411 |
static Arc direct(Edge edge, bool dir) {
|
| 413 | 412 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
| 414 | 413 |
} |
| 415 | 414 |
|
| 416 | 415 |
void first(Node& node) const {
|
| 417 | 416 |
node._id = _node_num - 1; |
| 418 | 417 |
} |
| 419 | 418 |
|
| 420 | 419 |
static void next(Node& node) {
|
| 421 | 420 |
--node._id; |
| 422 | 421 |
} |
| 423 | 422 |
|
| 424 | 423 |
void first(Arc& arc) const {
|
| 425 | 424 |
arc._id = (_edge_num << 1) - 1; |
| 426 | 425 |
} |
| 427 | 426 |
|
| 428 | 427 |
static void next(Arc& arc) {
|
| 429 | 428 |
--arc._id; |
| 430 | 429 |
} |
| 431 | 430 |
|
| 432 | 431 |
void first(Edge& edge) const {
|
| 433 | 432 |
edge._id = _edge_num - 1; |
| 434 | 433 |
} |
| 435 | 434 |
|
| 436 | 435 |
static void next(Edge& edge) {
|
| 437 | 436 |
--edge._id; |
| 438 | 437 |
} |
| 439 | 438 |
|
| 440 | 439 |
void firstOut(Arc& arc, const Node& node) const {
|
| 441 | 440 |
int s = node._id, t = _node_num - 1; |
| 442 | 441 |
if (s < t) {
|
| 443 | 442 |
arc._id = (_eid(s, t) << 1) | 1; |
| 444 | 443 |
} else {
|
| 445 | 444 |
--t; |
| 446 | 445 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1); |
| 447 | 446 |
} |
| 448 | 447 |
} |
| 449 | 448 |
|
| 450 | 449 |
void nextOut(Arc& arc) const {
|
| 451 | 450 |
int s, t; |
| 452 | 451 |
_stid(arc._id, s, t); |
| 453 | 452 |
--t; |
| 454 | 453 |
if (s < t) {
|
| 455 | 454 |
arc._id = (_eid(s, t) << 1) | 1; |
| 456 | 455 |
} else {
|
| 457 | 456 |
if (s == t) --t; |
| 458 | 457 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1); |
| 459 | 458 |
} |
| 460 | 459 |
} |
| 461 | 460 |
|
| 462 | 461 |
void firstIn(Arc& arc, const Node& node) const {
|
| 463 | 462 |
int s = _node_num - 1, t = node._id; |
| 464 | 463 |
if (s > t) {
|
| 465 | 464 |
arc._id = (_eid(t, s) << 1); |
| 466 | 465 |
} else {
|
| 467 | 466 |
--s; |
| 468 | 467 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1); |
| 469 | 468 |
} |
| 470 | 469 |
} |
| 471 | 470 |
|
| 472 | 471 |
void nextIn(Arc& arc) const {
|
| 473 | 472 |
int s, t; |
| 474 | 473 |
_stid(arc._id, s, t); |
| 475 | 474 |
--s; |
| 476 | 475 |
if (s > t) {
|
| 477 | 476 |
arc._id = (_eid(t, s) << 1); |
| 478 | 477 |
} else {
|
| 479 | 478 |
if (s == t) --s; |
| 480 | 479 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1); |
| 481 | 480 |
} |
| 482 | 481 |
} |
| 483 | 482 |
|
| 484 | 483 |
void firstInc(Edge& edge, bool& dir, const Node& node) const {
|
| 485 | 484 |
int u = node._id, v = _node_num - 1; |
| 486 | 485 |
if (u < v) {
|
| 487 | 486 |
edge._id = _eid(u, v); |
| 488 | 487 |
dir = true; |
| 489 | 488 |
} else {
|
| 490 | 489 |
--v; |
| 491 | 490 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
| 492 | 491 |
dir = false; |
| 493 | 492 |
} |
| 494 | 493 |
} |
| 495 | 494 |
|
| 496 | 495 |
void nextInc(Edge& edge, bool& dir) const {
|
| 497 | 496 |
int u, v; |
| 498 | 497 |
if (dir) {
|
| 499 | 498 |
_uvid(edge._id, u, v); |
| 500 | 499 |
--v; |
| 501 | 500 |
if (u < v) {
|
| 502 | 501 |
edge._id = _eid(u, v); |
| 503 | 502 |
} else {
|
| 504 | 503 |
--v; |
| 505 | 504 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
| 506 | 505 |
dir = false; |
| 507 | 506 |
} |
| 508 | 507 |
} else {
|
| 509 | 508 |
_uvid(edge._id, v, u); |
| 510 | 509 |
--v; |
| 511 | 510 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
| 512 | 511 |
} |
| 513 | 512 |
} |
| 514 | 513 |
|
| 515 | 514 |
}; |
| 516 | 515 |
|
| 517 | 516 |
typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase; |
| 518 | 517 |
|
| 519 | 518 |
/// \ingroup graphs |
| 520 | 519 |
/// |
| 521 | 520 |
/// \brief An undirected full graph class. |
| 522 | 521 |
/// |
| 523 | 522 |
/// This is a simple and fast undirected full graph |
| 524 | 523 |
/// implementation. From each node go edge to each other node, |
| 525 | 524 |
/// therefore the number of edges in the graph is \f$n(n-1)/2\f$. |
| 526 | 525 |
/// This graph type is completely static, so you can neither |
| 527 | 526 |
/// add nor delete either edges or nodes, and it needs constant |
| 528 | 527 |
/// space in memory. |
| 529 | 528 |
/// |
| 530 |
/// This class conforms to the \ref concepts::Graph "Graph" concept |
|
| 531 |
/// and it also has an important extra feature that its maps are |
|
| 532 |
/// |
|
| 529 |
/// This class fully conforms to the \ref concepts::Graph "Graph concept". |
|
| 533 | 530 |
/// |
| 534 | 531 |
/// The \c FullGraph and \c FullDigraph classes are very similar, |
| 535 | 532 |
/// but there are two differences. While the \c FullDigraph class |
| 536 | 533 |
/// conforms only to the \ref concepts::Digraph "Digraph" concept, |
| 537 | 534 |
/// this class conforms to the \ref concepts::Graph "Graph" concept, |
| 538 | 535 |
/// moreover \c FullGraph does not contain a loop arc for each |
| 539 | 536 |
/// node as \c FullDigraph does. |
| 540 | 537 |
/// |
| 541 | 538 |
/// \sa FullDigraph |
| 542 | 539 |
class FullGraph : public ExtendedFullGraphBase {
|
| 543 | 540 |
public: |
| 544 | 541 |
|
| 545 | 542 |
typedef ExtendedFullGraphBase Parent; |
| 546 | 543 |
|
| 547 | 544 |
/// \brief Constructor |
| 548 | 545 |
FullGraph() { construct(0); }
|
| 549 | 546 |
|
| 550 | 547 |
/// \brief Constructor |
| 551 | 548 |
/// |
| 552 | 549 |
/// Constructor. |
| 553 | 550 |
/// \param n The number of the nodes. |
| 554 | 551 |
FullGraph(int n) { construct(n); }
|
| 555 | 552 |
|
| 556 | 553 |
/// \brief Resizes the graph |
| 557 | 554 |
/// |
| 558 | 555 |
/// Resizes the graph. The function will fully destroy and |
| 559 | 556 |
/// rebuild the graph. This cause that the maps of the graph will |
| 560 | 557 |
/// reallocated automatically and the previous values will be lost. |
| 561 | 558 |
void resize(int n) {
|
| 562 | 559 |
Parent::notifier(Arc()).clear(); |
| 563 | 560 |
Parent::notifier(Edge()).clear(); |
| 564 | 561 |
Parent::notifier(Node()).clear(); |
| 565 | 562 |
construct(n); |
| 566 | 563 |
Parent::notifier(Node()).build(); |
| 567 | 564 |
Parent::notifier(Edge()).build(); |
| 568 | 565 |
Parent::notifier(Arc()).build(); |
| 569 | 566 |
} |
| 570 | 567 |
|
| 571 | 568 |
/// \brief Returns the node with the given index. |
| 572 | 569 |
/// |
| 573 | 570 |
/// Returns the node with the given index. Since it is a static |
| 574 | 571 |
/// graph its nodes can be indexed with integers from the range |
| 575 | 572 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 576 | 573 |
/// \sa index() |
| 577 | 574 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
| 578 | 575 |
|
| 579 | 576 |
/// \brief Returns the index of the given node. |
| 580 | 577 |
/// |
| 581 | 578 |
/// Returns the index of the given node. Since it is a static |
| 582 | 579 |
/// graph its nodes can be indexed with integers from the range |
| 583 | 580 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 584 | 581 |
/// \sa operator() |
| 585 | 582 |
int index(const Node& node) const { return Parent::index(node); }
|
| 586 | 583 |
|
| 587 | 584 |
/// \brief Returns the arc connecting the given nodes. |
| 588 | 585 |
/// |
| 589 | 586 |
/// Returns the arc connecting the given nodes. |
| 590 | 587 |
Arc arc(const Node& s, const Node& t) const {
|
| 591 | 588 |
return Parent::arc(s, t); |
| 592 | 589 |
} |
| 593 | 590 |
|
| 594 | 591 |
/// \brief Returns the edge connects the given nodes. |
| 595 | 592 |
/// |
| 596 | 593 |
/// Returns the edge connects the given nodes. |
| 597 | 594 |
Edge edge(const Node& u, const Node& v) const {
|
| 598 | 595 |
return Parent::edge(u, v); |
| 599 | 596 |
} |
| 600 | 597 |
|
| 601 | 598 |
/// \brief Number of nodes. |
| 602 | 599 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 603 | 600 |
/// \brief Number of arcs. |
| 604 | 601 |
int arcNum() const { return Parent::arcNum(); }
|
| 605 | 602 |
/// \brief Number of edges. |
| 606 | 603 |
int edgeNum() const { return Parent::edgeNum(); }
|
| 607 | 604 |
|
| 608 | 605 |
}; |
| 609 | 606 |
|
| 610 | 607 |
|
| 611 | 608 |
} //namespace lemon |
| 612 | 609 |
|
| 613 | 610 |
|
| 614 | 611 |
#endif //LEMON_FULL_GRAPH_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 GRID_GRAPH_H |
| 20 | 20 |
#define GRID_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/graph_extender.h> |
| 24 | 24 |
#include <lemon/dim2.h> |
| 25 | 25 |
#include <lemon/assert.h> |
| 26 | 26 |
|
| 27 | 27 |
///\ingroup graphs |
| 28 | 28 |
///\file |
| 29 | 29 |
///\brief GridGraph class. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
class GridGraphBase {
|
| 34 | 34 |
|
| 35 | 35 |
public: |
| 36 | 36 |
|
| 37 | 37 |
typedef GridGraphBase Graph; |
| 38 | 38 |
|
| 39 | 39 |
class Node; |
| 40 | 40 |
class Edge; |
| 41 | 41 |
class Arc; |
| 42 | 42 |
|
| 43 | 43 |
public: |
| 44 | 44 |
|
| 45 | 45 |
GridGraphBase() {}
|
| 46 | 46 |
|
| 47 | 47 |
protected: |
| 48 | 48 |
|
| 49 | 49 |
void construct(int width, int height) {
|
| 50 | 50 |
_width = width; _height = height; |
| 51 | 51 |
_node_num = width * height; |
| 52 | 52 |
_edge_num = 2 * _node_num - width - height; |
| 53 | 53 |
_edge_limit = _node_num - _width; |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
Node operator()(int i, int j) const {
|
| 59 | 59 |
LEMON_DEBUG(0 <= i && i < _width && |
| 60 | 60 |
0 <= j && j < _height, "Index out of range"); |
| 61 | 61 |
return Node(i + j * _width); |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 | 64 |
int col(Node n) const {
|
| 65 | 65 |
return n._id % _width; |
| 66 | 66 |
} |
| 67 | 67 |
|
| 68 | 68 |
int row(Node n) const {
|
| 69 | 69 |
return n._id / _width; |
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
dim2::Point<int> pos(Node n) const {
|
| 73 | 73 |
return dim2::Point<int>(col(n), row(n)); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
int width() const {
|
| 77 | 77 |
return _width; |
| 78 | 78 |
} |
| 79 | 79 |
|
| 80 | 80 |
int height() const {
|
| 81 | 81 |
return _height; |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
typedef True NodeNumTag; |
| 85 | 85 |
typedef True EdgeNumTag; |
| 86 | 86 |
typedef True ArcNumTag; |
| 87 | 87 |
|
| 88 | 88 |
int nodeNum() const { return _node_num; }
|
| 89 | 89 |
int edgeNum() const { return _edge_num; }
|
| 90 | 90 |
int arcNum() const { return 2 * _edge_num; }
|
| 91 | 91 |
|
| 92 | 92 |
Node u(Edge edge) const {
|
| 93 | 93 |
if (edge._id < _edge_limit) {
|
| 94 | 94 |
return edge._id; |
| 95 | 95 |
} else {
|
| 96 | 96 |
return (edge._id - _edge_limit) % (_width - 1) + |
| 97 | 97 |
(edge._id - _edge_limit) / (_width - 1) * _width; |
| 98 | 98 |
} |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 | 101 |
Node v(Edge edge) const {
|
| 102 | 102 |
if (edge._id < _edge_limit) {
|
| 103 | 103 |
return edge._id + _width; |
| 104 | 104 |
} else {
|
| 105 | 105 |
return (edge._id - _edge_limit) % (_width - 1) + |
| 106 | 106 |
(edge._id - _edge_limit) / (_width - 1) * _width + 1; |
| 107 | 107 |
} |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
Node source(Arc arc) const {
|
| 111 | 111 |
return (arc._id & 1) == 1 ? u(arc) : v(arc); |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
Node target(Arc arc) const {
|
| 115 | 115 |
return (arc._id & 1) == 1 ? v(arc) : u(arc); |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
static int id(Node node) { return node._id; }
|
| 119 | 119 |
static int id(Edge edge) { return edge._id; }
|
| 120 | 120 |
static int id(Arc arc) { return arc._id; }
|
| 121 | 121 |
|
| 122 | 122 |
int maxNodeId() const { return _node_num - 1; }
|
| 123 | 123 |
int maxEdgeId() const { return _edge_num - 1; }
|
| 124 | 124 |
int maxArcId() const { return 2 * _edge_num - 1; }
|
| 125 | 125 |
|
| 126 | 126 |
static Node nodeFromId(int id) { return Node(id);}
|
| 127 | 127 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 128 | 128 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 129 | 129 |
|
| 130 | 130 |
typedef True FindEdgeTag; |
| 131 | 131 |
typedef True FindArcTag; |
| 132 | 132 |
|
| 133 | 133 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
| 134 | 134 |
if (prev != INVALID) return INVALID; |
| 135 | 135 |
if (v._id > u._id) {
|
| 136 | 136 |
if (v._id - u._id == _width) |
| 137 | 137 |
return Edge(u._id); |
| 138 | 138 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) {
|
| 139 | 139 |
return Edge(u._id / _width * (_width - 1) + |
| 140 | 140 |
u._id % _width + _edge_limit); |
| 141 | 141 |
} |
| 142 | 142 |
} else {
|
| 143 | 143 |
if (u._id - v._id == _width) |
| 144 | 144 |
return Edge(v._id); |
| 145 | 145 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) {
|
| 146 | 146 |
return Edge(v._id / _width * (_width - 1) + |
| 147 | 147 |
v._id % _width + _edge_limit); |
| 148 | 148 |
} |
| 149 | 149 |
} |
| 150 | 150 |
return INVALID; |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const {
|
| 154 | 154 |
if (prev != INVALID) return INVALID; |
| 155 | 155 |
if (v._id > u._id) {
|
| 156 | 156 |
if (v._id - u._id == _width) |
| 157 | 157 |
return Arc((u._id << 1) | 1); |
| 158 | 158 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) {
|
| 159 | 159 |
return Arc(((u._id / _width * (_width - 1) + |
| 160 | 160 |
u._id % _width + _edge_limit) << 1) | 1); |
| 161 | 161 |
} |
| 162 | 162 |
} else {
|
| 163 | 163 |
if (u._id - v._id == _width) |
| 164 | 164 |
return Arc(v._id << 1); |
| 165 | 165 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) {
|
| 166 | 166 |
return Arc((v._id / _width * (_width - 1) + |
| 167 | 167 |
v._id % _width + _edge_limit) << 1); |
| 168 | 168 |
} |
| 169 | 169 |
} |
| 170 | 170 |
return INVALID; |
| 171 | 171 |
} |
| 172 | 172 |
|
| 173 | 173 |
class Node {
|
| 174 | 174 |
friend class GridGraphBase; |
| 175 | 175 |
|
| 176 | 176 |
protected: |
| 177 | 177 |
int _id; |
| 178 | 178 |
Node(int id) : _id(id) {}
|
| 179 | 179 |
public: |
| 180 | 180 |
Node() {}
|
| 181 | 181 |
Node (Invalid) : _id(-1) {}
|
| 182 | 182 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 183 | 183 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 184 | 184 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 185 | 185 |
}; |
| 186 | 186 |
|
| 187 | 187 |
class Edge {
|
| 188 | 188 |
friend class GridGraphBase; |
| 189 | 189 |
friend class Arc; |
| 190 | 190 |
|
| 191 | 191 |
protected: |
| 192 | 192 |
int _id; |
| 193 | 193 |
|
| 194 | 194 |
Edge(int id) : _id(id) {}
|
| 195 | 195 |
|
| 196 | 196 |
public: |
| 197 | 197 |
Edge() {}
|
| 198 | 198 |
Edge (Invalid) : _id(-1) {}
|
| 199 | 199 |
bool operator==(const Edge edge) const {return _id == edge._id;}
|
| 200 | 200 |
bool operator!=(const Edge edge) const {return _id != edge._id;}
|
| 201 | 201 |
bool operator<(const Edge edge) const {return _id < edge._id;}
|
| 202 | 202 |
}; |
| 203 | 203 |
|
| 204 | 204 |
class Arc {
|
| 205 | 205 |
friend class GridGraphBase; |
| 206 | 206 |
|
| 207 | 207 |
protected: |
| 208 | 208 |
int _id; |
| 209 | 209 |
|
| 210 | 210 |
Arc(int id) : _id(id) {}
|
| 211 | 211 |
|
| 212 | 212 |
public: |
| 213 | 213 |
Arc() {}
|
| 214 | 214 |
Arc (Invalid) : _id(-1) {}
|
| 215 | 215 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
|
| 216 | 216 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 217 | 217 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 218 | 218 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 219 | 219 |
}; |
| 220 | 220 |
|
| 221 | 221 |
static bool direction(Arc arc) {
|
| 222 | 222 |
return (arc._id & 1) == 1; |
| 223 | 223 |
} |
| 224 | 224 |
|
| 225 | 225 |
static Arc direct(Edge edge, bool dir) {
|
| 226 | 226 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
| 227 | 227 |
} |
| 228 | 228 |
|
| 229 | 229 |
void first(Node& node) const {
|
| 230 | 230 |
node._id = _node_num - 1; |
| 231 | 231 |
} |
| 232 | 232 |
|
| 233 | 233 |
static void next(Node& node) {
|
| 234 | 234 |
--node._id; |
| 235 | 235 |
} |
| 236 | 236 |
|
| 237 | 237 |
void first(Edge& edge) const {
|
| 238 | 238 |
edge._id = _edge_num - 1; |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
static void next(Edge& edge) {
|
| 242 | 242 |
--edge._id; |
| 243 | 243 |
} |
| 244 | 244 |
|
| 245 | 245 |
void first(Arc& arc) const {
|
| 246 | 246 |
arc._id = 2 * _edge_num - 1; |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
static void next(Arc& arc) {
|
| 250 | 250 |
--arc._id; |
| 251 | 251 |
} |
| 252 | 252 |
|
| 253 | 253 |
void firstOut(Arc& arc, const Node& node) const {
|
| 254 | 254 |
if (node._id % _width < _width - 1) {
|
| 255 | 255 |
arc._id = (_edge_limit + node._id % _width + |
| 256 | 256 |
(node._id / _width) * (_width - 1)) << 1 | 1; |
| 257 | 257 |
return; |
| 258 | 258 |
} |
| 259 | 259 |
if (node._id < _node_num - _width) {
|
| 260 | 260 |
arc._id = node._id << 1 | 1; |
| 261 | 261 |
return; |
| 262 | 262 |
} |
| 263 | 263 |
if (node._id % _width > 0) {
|
| 264 | 264 |
arc._id = (_edge_limit + node._id % _width + |
| 265 | 265 |
(node._id / _width) * (_width - 1) - 1) << 1; |
| 266 | 266 |
return; |
| 267 | 267 |
} |
| 268 | 268 |
if (node._id >= _width) {
|
| 269 | 269 |
arc._id = (node._id - _width) << 1; |
| 270 | 270 |
return; |
| 271 | 271 |
} |
| 272 | 272 |
arc._id = -1; |
| 273 | 273 |
} |
| 274 | 274 |
|
| 275 | 275 |
void nextOut(Arc& arc) const {
|
| 276 | 276 |
int nid = arc._id >> 1; |
| 277 | 277 |
if ((arc._id & 1) == 1) {
|
| 278 | 278 |
if (nid >= _edge_limit) {
|
| 279 | 279 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 280 | 280 |
(nid - _edge_limit) / (_width - 1) * _width; |
| 281 | 281 |
if (nid < _node_num - _width) {
|
| 282 | 282 |
arc._id = nid << 1 | 1; |
| 283 | 283 |
return; |
| 284 | 284 |
} |
| 285 | 285 |
} |
| 286 | 286 |
if (nid % _width > 0) {
|
| 287 | 287 |
arc._id = (_edge_limit + nid % _width + |
| 288 | 288 |
(nid / _width) * (_width - 1) - 1) << 1; |
| 289 | 289 |
return; |
| 290 | 290 |
} |
| 291 | 291 |
if (nid >= _width) {
|
| 292 | 292 |
arc._id = (nid - _width) << 1; |
| 293 | 293 |
return; |
| 294 | 294 |
} |
| 295 | 295 |
} else {
|
| 296 | 296 |
if (nid >= _edge_limit) {
|
| 297 | 297 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 298 | 298 |
(nid - _edge_limit) / (_width - 1) * _width + 1; |
| 299 | 299 |
if (nid >= _width) {
|
| 300 | 300 |
arc._id = (nid - _width) << 1; |
| 301 | 301 |
return; |
| 302 | 302 |
} |
| 303 | 303 |
} |
| 304 | 304 |
} |
| 305 | 305 |
arc._id = -1; |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 | 308 |
void firstIn(Arc& arc, const Node& node) const {
|
| 309 | 309 |
if (node._id % _width < _width - 1) {
|
| 310 | 310 |
arc._id = (_edge_limit + node._id % _width + |
| 311 | 311 |
(node._id / _width) * (_width - 1)) << 1; |
| 312 | 312 |
return; |
| 313 | 313 |
} |
| 314 | 314 |
if (node._id < _node_num - _width) {
|
| 315 | 315 |
arc._id = node._id << 1; |
| 316 | 316 |
return; |
| 317 | 317 |
} |
| 318 | 318 |
if (node._id % _width > 0) {
|
| 319 | 319 |
arc._id = (_edge_limit + node._id % _width + |
| 320 | 320 |
(node._id / _width) * (_width - 1) - 1) << 1 | 1; |
| 321 | 321 |
return; |
| 322 | 322 |
} |
| 323 | 323 |
if (node._id >= _width) {
|
| 324 | 324 |
arc._id = (node._id - _width) << 1 | 1; |
| 325 | 325 |
return; |
| 326 | 326 |
} |
| 327 | 327 |
arc._id = -1; |
| 328 | 328 |
} |
| 329 | 329 |
|
| 330 | 330 |
void nextIn(Arc& arc) const {
|
| 331 | 331 |
int nid = arc._id >> 1; |
| 332 | 332 |
if ((arc._id & 1) == 0) {
|
| 333 | 333 |
if (nid >= _edge_limit) {
|
| 334 | 334 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 335 | 335 |
(nid - _edge_limit) / (_width - 1) * _width; |
| 336 | 336 |
if (nid < _node_num - _width) {
|
| 337 | 337 |
arc._id = nid << 1; |
| 338 | 338 |
return; |
| 339 | 339 |
} |
| 340 | 340 |
} |
| 341 | 341 |
if (nid % _width > 0) {
|
| 342 | 342 |
arc._id = (_edge_limit + nid % _width + |
| 343 | 343 |
(nid / _width) * (_width - 1) - 1) << 1 | 1; |
| 344 | 344 |
return; |
| 345 | 345 |
} |
| 346 | 346 |
if (nid >= _width) {
|
| 347 | 347 |
arc._id = (nid - _width) << 1 | 1; |
| 348 | 348 |
return; |
| 349 | 349 |
} |
| 350 | 350 |
} else {
|
| 351 | 351 |
if (nid >= _edge_limit) {
|
| 352 | 352 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 353 | 353 |
(nid - _edge_limit) / (_width - 1) * _width + 1; |
| 354 | 354 |
if (nid >= _width) {
|
| 355 | 355 |
arc._id = (nid - _width) << 1 | 1; |
| 356 | 356 |
return; |
| 357 | 357 |
} |
| 358 | 358 |
} |
| 359 | 359 |
} |
| 360 | 360 |
arc._id = -1; |
| 361 | 361 |
} |
| 362 | 362 |
|
| 363 | 363 |
void firstInc(Edge& edge, bool& dir, const Node& node) const {
|
| 364 | 364 |
if (node._id % _width < _width - 1) {
|
| 365 | 365 |
edge._id = _edge_limit + node._id % _width + |
| 366 | 366 |
(node._id / _width) * (_width - 1); |
| 367 | 367 |
dir = true; |
| 368 | 368 |
return; |
| 369 | 369 |
} |
| 370 | 370 |
if (node._id < _node_num - _width) {
|
| 371 | 371 |
edge._id = node._id; |
| 372 | 372 |
dir = true; |
| 373 | 373 |
return; |
| 374 | 374 |
} |
| 375 | 375 |
if (node._id % _width > 0) {
|
| 376 | 376 |
edge._id = _edge_limit + node._id % _width + |
| 377 | 377 |
(node._id / _width) * (_width - 1) - 1; |
| 378 | 378 |
dir = false; |
| 379 | 379 |
return; |
| 380 | 380 |
} |
| 381 | 381 |
if (node._id >= _width) {
|
| 382 | 382 |
edge._id = node._id - _width; |
| 383 | 383 |
dir = false; |
| 384 | 384 |
return; |
| 385 | 385 |
} |
| 386 | 386 |
edge._id = -1; |
| 387 | 387 |
dir = true; |
| 388 | 388 |
} |
| 389 | 389 |
|
| 390 | 390 |
void nextInc(Edge& edge, bool& dir) const {
|
| 391 | 391 |
int nid = edge._id; |
| 392 | 392 |
if (dir) {
|
| 393 | 393 |
if (nid >= _edge_limit) {
|
| 394 | 394 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 395 | 395 |
(nid - _edge_limit) / (_width - 1) * _width; |
| 396 | 396 |
if (nid < _node_num - _width) {
|
| 397 | 397 |
edge._id = nid; |
| 398 | 398 |
return; |
| 399 | 399 |
} |
| 400 | 400 |
} |
| 401 | 401 |
if (nid % _width > 0) {
|
| 402 | 402 |
edge._id = _edge_limit + nid % _width + |
| 403 | 403 |
(nid / _width) * (_width - 1) - 1; |
| 404 | 404 |
dir = false; |
| 405 | 405 |
return; |
| 406 | 406 |
} |
| 407 | 407 |
if (nid >= _width) {
|
| 408 | 408 |
edge._id = nid - _width; |
| 409 | 409 |
dir = false; |
| 410 | 410 |
return; |
| 411 | 411 |
} |
| 412 | 412 |
} else {
|
| 413 | 413 |
if (nid >= _edge_limit) {
|
| 414 | 414 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 415 | 415 |
(nid - _edge_limit) / (_width - 1) * _width + 1; |
| 416 | 416 |
if (nid >= _width) {
|
| 417 | 417 |
edge._id = nid - _width; |
| 418 | 418 |
return; |
| 419 | 419 |
} |
| 420 | 420 |
} |
| 421 | 421 |
} |
| 422 | 422 |
edge._id = -1; |
| 423 | 423 |
dir = true; |
| 424 | 424 |
} |
| 425 | 425 |
|
| 426 | 426 |
Arc right(Node n) const {
|
| 427 | 427 |
if (n._id % _width < _width - 1) {
|
| 428 | 428 |
return Arc(((_edge_limit + n._id % _width + |
| 429 | 429 |
(n._id / _width) * (_width - 1)) << 1) | 1); |
| 430 | 430 |
} else {
|
| 431 | 431 |
return INVALID; |
| 432 | 432 |
} |
| 433 | 433 |
} |
| 434 | 434 |
|
| 435 | 435 |
Arc left(Node n) const {
|
| 436 | 436 |
if (n._id % _width > 0) {
|
| 437 | 437 |
return Arc((_edge_limit + n._id % _width + |
| 438 | 438 |
(n._id / _width) * (_width - 1) - 1) << 1); |
| 439 | 439 |
} else {
|
| 440 | 440 |
return INVALID; |
| 441 | 441 |
} |
| 442 | 442 |
} |
| 443 | 443 |
|
| 444 | 444 |
Arc up(Node n) const {
|
| 445 | 445 |
if (n._id < _edge_limit) {
|
| 446 | 446 |
return Arc((n._id << 1) | 1); |
| 447 | 447 |
} else {
|
| 448 | 448 |
return INVALID; |
| 449 | 449 |
} |
| 450 | 450 |
} |
| 451 | 451 |
|
| 452 | 452 |
Arc down(Node n) const {
|
| 453 | 453 |
if (n._id >= _width) {
|
| 454 | 454 |
return Arc((n._id - _width) << 1); |
| 455 | 455 |
} else {
|
| 456 | 456 |
return INVALID; |
| 457 | 457 |
} |
| 458 | 458 |
} |
| 459 | 459 |
|
| 460 | 460 |
private: |
| 461 | 461 |
int _width, _height; |
| 462 | 462 |
int _node_num, _edge_num; |
| 463 | 463 |
int _edge_limit; |
| 464 | 464 |
}; |
| 465 | 465 |
|
| 466 | 466 |
|
| 467 | 467 |
typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase; |
| 468 | 468 |
|
| 469 | 469 |
/// \ingroup graphs |
| 470 | 470 |
/// |
| 471 | 471 |
/// \brief Grid graph class |
| 472 | 472 |
/// |
| 473 | 473 |
/// This class implements a special graph type. The nodes of the |
| 474 | 474 |
/// graph can be indexed by two integer \c (i,j) value where \c i is |
| 475 | 475 |
/// in the \c [0..width()-1] range and j is in the \c |
| 476 | 476 |
/// [0..height()-1] range. Two nodes are connected in the graph if |
| 477 | 477 |
/// the indexes differ exactly on one position and exactly one is |
| 478 | 478 |
/// the difference. The nodes of the graph can be indexed by position |
| 479 | 479 |
/// with the \c operator()() function. The positions of the nodes can be |
| 480 | 480 |
/// get with \c pos(), \c col() and \c row() members. The outgoing |
| 481 | 481 |
/// arcs can be retrieved with the \c right(), \c up(), \c left() |
| 482 | 482 |
/// and \c down() functions, where the bottom-left corner is the |
| 483 | 483 |
/// origin. |
| 484 | 484 |
/// |
| 485 | 485 |
/// \image html grid_graph.png |
| 486 | 486 |
/// \image latex grid_graph.eps "Grid graph" width=\textwidth |
| 487 | 487 |
/// |
| 488 | 488 |
/// A short example about the basic usage: |
| 489 | 489 |
///\code |
| 490 | 490 |
/// GridGraph graph(rows, cols); |
| 491 | 491 |
/// GridGraph::NodeMap<int> val(graph); |
| 492 | 492 |
/// for (int i = 0; i < graph.width(); ++i) {
|
| 493 | 493 |
/// for (int j = 0; j < graph.height(); ++j) {
|
| 494 | 494 |
/// val[graph(i, j)] = i + j; |
| 495 | 495 |
/// } |
| 496 | 496 |
/// } |
| 497 | 497 |
///\endcode |
| 498 | 498 |
/// |
| 499 | 499 |
/// This graph type fully conforms to the \ref concepts::Graph |
| 500 |
/// "Graph" concept, and it also has an important extra feature |
|
| 501 |
/// that its maps are real \ref concepts::ReferenceMap |
|
| 502 |
/// " |
|
| 500 |
/// "Graph concept". |
|
| 503 | 501 |
class GridGraph : public ExtendedGridGraphBase {
|
| 504 | 502 |
public: |
| 505 | 503 |
|
| 506 | 504 |
typedef ExtendedGridGraphBase Parent; |
| 507 | 505 |
|
| 508 | 506 |
/// \brief Map to get the indices of the nodes as dim2::Point<int>. |
| 509 | 507 |
/// |
| 510 | 508 |
/// Map to get the indices of the nodes as dim2::Point<int>. |
| 511 | 509 |
class IndexMap {
|
| 512 | 510 |
public: |
| 513 | 511 |
/// \brief The key type of the map |
| 514 | 512 |
typedef GridGraph::Node Key; |
| 515 | 513 |
/// \brief The value type of the map |
| 516 | 514 |
typedef dim2::Point<int> Value; |
| 517 | 515 |
|
| 518 | 516 |
/// \brief Constructor |
| 519 | 517 |
/// |
| 520 | 518 |
/// Constructor |
| 521 | 519 |
IndexMap(const GridGraph& graph) : _graph(graph) {}
|
| 522 | 520 |
|
| 523 | 521 |
/// \brief The subscript operator |
| 524 | 522 |
/// |
| 525 | 523 |
/// The subscript operator. |
| 526 | 524 |
Value operator[](Key key) const {
|
| 527 | 525 |
return _graph.pos(key); |
| 528 | 526 |
} |
| 529 | 527 |
|
| 530 | 528 |
private: |
| 531 | 529 |
const GridGraph& _graph; |
| 532 | 530 |
}; |
| 533 | 531 |
|
| 534 | 532 |
/// \brief Map to get the column of the nodes. |
| 535 | 533 |
/// |
| 536 | 534 |
/// Map to get the column of the nodes. |
| 537 | 535 |
class ColMap {
|
| 538 | 536 |
public: |
| 539 | 537 |
/// \brief The key type of the map |
| 540 | 538 |
typedef GridGraph::Node Key; |
| 541 | 539 |
/// \brief The value type of the map |
| 542 | 540 |
typedef int Value; |
| 543 | 541 |
|
| 544 | 542 |
/// \brief Constructor |
| 545 | 543 |
/// |
| 546 | 544 |
/// Constructor |
| 547 | 545 |
ColMap(const GridGraph& graph) : _graph(graph) {}
|
| 548 | 546 |
|
| 549 | 547 |
/// \brief The subscript operator |
| 550 | 548 |
/// |
| 551 | 549 |
/// The subscript operator. |
| 552 | 550 |
Value operator[](Key key) const {
|
| 553 | 551 |
return _graph.col(key); |
| 554 | 552 |
} |
| 555 | 553 |
|
| 556 | 554 |
private: |
| 557 | 555 |
const GridGraph& _graph; |
| 558 | 556 |
}; |
| 559 | 557 |
|
| 560 | 558 |
/// \brief Map to get the row of the nodes. |
| 561 | 559 |
/// |
| 562 | 560 |
/// Map to get the row of the nodes. |
| 563 | 561 |
class RowMap {
|
| 564 | 562 |
public: |
| 565 | 563 |
/// \brief The key type of the map |
| 566 | 564 |
typedef GridGraph::Node Key; |
| 567 | 565 |
/// \brief The value type of the map |
| 568 | 566 |
typedef int Value; |
| 569 | 567 |
|
| 570 | 568 |
/// \brief Constructor |
| 571 | 569 |
/// |
| 572 | 570 |
/// Constructor |
| 573 | 571 |
RowMap(const GridGraph& graph) : _graph(graph) {}
|
| 574 | 572 |
|
| 575 | 573 |
/// \brief The subscript operator |
| 576 | 574 |
/// |
| 577 | 575 |
/// The subscript operator. |
| 578 | 576 |
Value operator[](Key key) const {
|
| 579 | 577 |
return _graph.row(key); |
| 580 | 578 |
} |
| 581 | 579 |
|
| 582 | 580 |
private: |
| 583 | 581 |
const GridGraph& _graph; |
| 584 | 582 |
}; |
| 585 | 583 |
|
| 586 | 584 |
/// \brief Constructor |
| 587 | 585 |
/// |
| 588 | 586 |
/// Construct a grid graph with given size. |
| 589 | 587 |
GridGraph(int width, int height) { construct(width, height); }
|
| 590 | 588 |
|
| 591 | 589 |
/// \brief Resize the graph |
| 592 | 590 |
/// |
| 593 | 591 |
/// Resize the graph. The function will fully destroy and rebuild |
| 594 | 592 |
/// the graph. This cause that the maps of the graph will |
| 595 | 593 |
/// reallocated automatically and the previous values will be |
| 596 | 594 |
/// lost. |
| 597 | 595 |
void resize(int width, int height) {
|
| 598 | 596 |
Parent::notifier(Arc()).clear(); |
| 599 | 597 |
Parent::notifier(Edge()).clear(); |
| 600 | 598 |
Parent::notifier(Node()).clear(); |
| 601 | 599 |
construct(width, height); |
| 602 | 600 |
Parent::notifier(Node()).build(); |
| 603 | 601 |
Parent::notifier(Edge()).build(); |
| 604 | 602 |
Parent::notifier(Arc()).build(); |
| 605 | 603 |
} |
| 606 | 604 |
|
| 607 | 605 |
/// \brief The node on the given position. |
| 608 | 606 |
/// |
| 609 | 607 |
/// Gives back the node on the given position. |
| 610 | 608 |
Node operator()(int i, int j) const {
|
| 611 | 609 |
return Parent::operator()(i, j); |
| 612 | 610 |
} |
| 613 | 611 |
|
| 614 | 612 |
/// \brief Gives back the column index of the node. |
| 615 | 613 |
/// |
| 616 | 614 |
/// Gives back the column index of the node. |
| 617 | 615 |
int col(Node n) const {
|
| 618 | 616 |
return Parent::col(n); |
| 619 | 617 |
} |
| 620 | 618 |
|
| 621 | 619 |
/// \brief Gives back the row index of the node. |
| 622 | 620 |
/// |
| 623 | 621 |
/// Gives back the row index of the node. |
| 624 | 622 |
int row(Node n) const {
|
| 625 | 623 |
return Parent::row(n); |
| 626 | 624 |
} |
| 627 | 625 |
|
| 628 | 626 |
/// \brief Gives back the position of the node. |
| 629 | 627 |
/// |
| 630 | 628 |
/// Gives back the position of the node, ie. the <tt>(col,row)</tt> pair. |
| 631 | 629 |
dim2::Point<int> pos(Node n) const {
|
| 632 | 630 |
return Parent::pos(n); |
| 633 | 631 |
} |
| 634 | 632 |
|
| 635 | 633 |
/// \brief Gives back the number of the columns. |
| 636 | 634 |
/// |
| 637 | 635 |
/// Gives back the number of the columns. |
| 638 | 636 |
int width() const {
|
| 639 | 637 |
return Parent::width(); |
| 640 | 638 |
} |
| 641 | 639 |
|
| 642 | 640 |
/// \brief Gives back the number of the rows. |
| 643 | 641 |
/// |
| 644 | 642 |
/// Gives back the number of the rows. |
| 645 | 643 |
int height() const {
|
| 646 | 644 |
return Parent::height(); |
| 647 | 645 |
} |
| 648 | 646 |
|
| 649 | 647 |
/// \brief Gives back the arc goes right from the node. |
| 650 | 648 |
/// |
| 651 | 649 |
/// Gives back the arc goes right from the node. If there is not |
| 652 | 650 |
/// outgoing arc then it gives back INVALID. |
| 653 | 651 |
Arc right(Node n) const {
|
| 654 | 652 |
return Parent::right(n); |
| 655 | 653 |
} |
| 656 | 654 |
|
| 657 | 655 |
/// \brief Gives back the arc goes left from the node. |
| 658 | 656 |
/// |
| 659 | 657 |
/// Gives back the arc goes left from the node. If there is not |
| 660 | 658 |
/// outgoing arc then it gives back INVALID. |
| 661 | 659 |
Arc left(Node n) const {
|
| 662 | 660 |
return Parent::left(n); |
| 663 | 661 |
} |
| 664 | 662 |
|
| 665 | 663 |
/// \brief Gives back the arc goes up from the node. |
| 666 | 664 |
/// |
| 667 | 665 |
/// Gives back the arc goes up from the node. If there is not |
| 668 | 666 |
/// outgoing arc then it gives back INVALID. |
| 669 | 667 |
Arc up(Node n) const {
|
| 670 | 668 |
return Parent::up(n); |
| 671 | 669 |
} |
| 672 | 670 |
|
| 673 | 671 |
/// \brief Gives back the arc goes down from the node. |
| 674 | 672 |
/// |
| 675 | 673 |
/// Gives back the arc goes down from the node. If there is not |
| 676 | 674 |
/// outgoing arc then it gives back INVALID. |
| 677 | 675 |
Arc down(Node n) const {
|
| 678 | 676 |
return Parent::down(n); |
| 679 | 677 |
} |
| 680 | 678 |
|
| 681 | 679 |
/// \brief Index map of the grid graph |
| 682 | 680 |
/// |
| 683 | 681 |
/// Just returns an IndexMap for the grid graph. |
| 684 | 682 |
IndexMap indexMap() const {
|
| 685 | 683 |
return IndexMap(*this); |
| 686 | 684 |
} |
| 687 | 685 |
|
| 688 | 686 |
/// \brief Row map of the grid graph |
| 689 | 687 |
/// |
| 690 | 688 |
/// Just returns a RowMap for the grid graph. |
| 691 | 689 |
RowMap rowMap() const {
|
| 692 | 690 |
return RowMap(*this); |
| 693 | 691 |
} |
| 694 | 692 |
|
| 695 | 693 |
/// \brief Column map of the grid graph |
| 696 | 694 |
/// |
| 697 | 695 |
/// Just returns a ColMap for the grid graph. |
| 698 | 696 |
ColMap colMap() const {
|
| 699 | 697 |
return ColMap(*this); |
| 700 | 698 |
} |
| 701 | 699 |
|
| 702 | 700 |
}; |
| 703 | 701 |
|
| 704 | 702 |
} |
| 705 | 703 |
#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 HYPERCUBE_GRAPH_H |
| 20 | 20 |
#define HYPERCUBE_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <lemon/core.h> |
| 24 | 24 |
#include <lemon/assert.h> |
| 25 | 25 |
#include <lemon/bits/graph_extender.h> |
| 26 | 26 |
|
| 27 | 27 |
///\ingroup graphs |
| 28 | 28 |
///\file |
| 29 | 29 |
///\brief HypercubeGraph class. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
class HypercubeGraphBase {
|
| 34 | 34 |
|
| 35 | 35 |
public: |
| 36 | 36 |
|
| 37 | 37 |
typedef HypercubeGraphBase Graph; |
| 38 | 38 |
|
| 39 | 39 |
class Node; |
| 40 | 40 |
class Edge; |
| 41 | 41 |
class Arc; |
| 42 | 42 |
|
| 43 | 43 |
public: |
| 44 | 44 |
|
| 45 | 45 |
HypercubeGraphBase() {}
|
| 46 | 46 |
|
| 47 | 47 |
protected: |
| 48 | 48 |
|
| 49 | 49 |
void construct(int dim) {
|
| 50 | 50 |
LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1."); |
| 51 | 51 |
_dim = dim; |
| 52 | 52 |
_node_num = 1 << dim; |
| 53 | 53 |
_edge_num = dim * (1 << (dim-1)); |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
typedef True NodeNumTag; |
| 59 | 59 |
typedef True EdgeNumTag; |
| 60 | 60 |
typedef True ArcNumTag; |
| 61 | 61 |
|
| 62 | 62 |
int nodeNum() const { return _node_num; }
|
| 63 | 63 |
int edgeNum() const { return _edge_num; }
|
| 64 | 64 |
int arcNum() const { return 2 * _edge_num; }
|
| 65 | 65 |
|
| 66 | 66 |
int maxNodeId() const { return _node_num - 1; }
|
| 67 | 67 |
int maxEdgeId() const { return _edge_num - 1; }
|
| 68 | 68 |
int maxArcId() const { return 2 * _edge_num - 1; }
|
| 69 | 69 |
|
| 70 | 70 |
static Node nodeFromId(int id) { return Node(id); }
|
| 71 | 71 |
static Edge edgeFromId(int id) { return Edge(id); }
|
| 72 | 72 |
static Arc arcFromId(int id) { return Arc(id); }
|
| 73 | 73 |
|
| 74 | 74 |
static int id(Node node) { return node._id; }
|
| 75 | 75 |
static int id(Edge edge) { return edge._id; }
|
| 76 | 76 |
static int id(Arc arc) { return arc._id; }
|
| 77 | 77 |
|
| 78 | 78 |
Node u(Edge edge) const {
|
| 79 | 79 |
int base = edge._id & ((1 << (_dim-1)) - 1); |
| 80 | 80 |
int k = edge._id >> (_dim-1); |
| 81 | 81 |
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)); |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
Node v(Edge edge) const {
|
| 85 | 85 |
int base = edge._id & ((1 << (_dim-1)) - 1); |
| 86 | 86 |
int k = edge._id >> (_dim-1); |
| 87 | 87 |
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)) | (1 << k); |
| 88 | 88 |
} |
| 89 | 89 |
|
| 90 | 90 |
Node source(Arc arc) const {
|
| 91 | 91 |
return (arc._id & 1) == 1 ? u(arc) : v(arc); |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
Node target(Arc arc) const {
|
| 95 | 95 |
return (arc._id & 1) == 1 ? v(arc) : u(arc); |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
typedef True FindEdgeTag; |
| 99 | 99 |
typedef True FindArcTag; |
| 100 | 100 |
|
| 101 | 101 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
| 102 | 102 |
if (prev != INVALID) return INVALID; |
| 103 | 103 |
int d = u._id ^ v._id; |
| 104 | 104 |
int k = 0; |
| 105 | 105 |
if (d == 0) return INVALID; |
| 106 | 106 |
for ( ; (d & 1) == 0; d >>= 1) ++k; |
| 107 | 107 |
if (d >> 1 != 0) return INVALID; |
| 108 | 108 |
return (k << (_dim-1)) | ((u._id >> (k+1)) << k) | |
| 109 | 109 |
(u._id & ((1 << k) - 1)); |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const {
|
| 113 | 113 |
Edge edge = findEdge(u, v, prev); |
| 114 | 114 |
if (edge == INVALID) return INVALID; |
| 115 | 115 |
int k = edge._id >> (_dim-1); |
| 116 | 116 |
return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1; |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
class Node {
|
| 120 | 120 |
friend class HypercubeGraphBase; |
| 121 | 121 |
|
| 122 | 122 |
protected: |
| 123 | 123 |
int _id; |
| 124 | 124 |
Node(int id) : _id(id) {}
|
| 125 | 125 |
public: |
| 126 | 126 |
Node() {}
|
| 127 | 127 |
Node (Invalid) : _id(-1) {}
|
| 128 | 128 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 129 | 129 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 130 | 130 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 131 | 131 |
}; |
| 132 | 132 |
|
| 133 | 133 |
class Edge {
|
| 134 | 134 |
friend class HypercubeGraphBase; |
| 135 | 135 |
friend class Arc; |
| 136 | 136 |
|
| 137 | 137 |
protected: |
| 138 | 138 |
int _id; |
| 139 | 139 |
|
| 140 | 140 |
Edge(int id) : _id(id) {}
|
| 141 | 141 |
|
| 142 | 142 |
public: |
| 143 | 143 |
Edge() {}
|
| 144 | 144 |
Edge (Invalid) : _id(-1) {}
|
| 145 | 145 |
bool operator==(const Edge edge) const {return _id == edge._id;}
|
| 146 | 146 |
bool operator!=(const Edge edge) const {return _id != edge._id;}
|
| 147 | 147 |
bool operator<(const Edge edge) const {return _id < edge._id;}
|
| 148 | 148 |
}; |
| 149 | 149 |
|
| 150 | 150 |
class Arc {
|
| 151 | 151 |
friend class HypercubeGraphBase; |
| 152 | 152 |
|
| 153 | 153 |
protected: |
| 154 | 154 |
int _id; |
| 155 | 155 |
|
| 156 | 156 |
Arc(int id) : _id(id) {}
|
| 157 | 157 |
|
| 158 | 158 |
public: |
| 159 | 159 |
Arc() {}
|
| 160 | 160 |
Arc (Invalid) : _id(-1) {}
|
| 161 | 161 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
|
| 162 | 162 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 163 | 163 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 164 | 164 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 165 | 165 |
}; |
| 166 | 166 |
|
| 167 | 167 |
void first(Node& node) const {
|
| 168 | 168 |
node._id = _node_num - 1; |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
static void next(Node& node) {
|
| 172 | 172 |
--node._id; |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
void first(Edge& edge) const {
|
| 176 | 176 |
edge._id = _edge_num - 1; |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
static void next(Edge& edge) {
|
| 180 | 180 |
--edge._id; |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
void first(Arc& arc) const {
|
| 184 | 184 |
arc._id = 2 * _edge_num - 1; |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
static void next(Arc& arc) {
|
| 188 | 188 |
--arc._id; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
void firstInc(Edge& edge, bool& dir, const Node& node) const {
|
| 192 | 192 |
edge._id = node._id >> 1; |
| 193 | 193 |
dir = (node._id & 1) == 0; |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
void nextInc(Edge& edge, bool& dir) const {
|
| 197 | 197 |
Node n = dir ? u(edge) : v(edge); |
| 198 | 198 |
int k = (edge._id >> (_dim-1)) + 1; |
| 199 | 199 |
if (k < _dim) {
|
| 200 | 200 |
edge._id = (k << (_dim-1)) | |
| 201 | 201 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
| 202 | 202 |
dir = ((n._id >> k) & 1) == 0; |
| 203 | 203 |
} else {
|
| 204 | 204 |
edge._id = -1; |
| 205 | 205 |
dir = true; |
| 206 | 206 |
} |
| 207 | 207 |
} |
| 208 | 208 |
|
| 209 | 209 |
void firstOut(Arc& arc, const Node& node) const {
|
| 210 | 210 |
arc._id = ((node._id >> 1) << 1) | (~node._id & 1); |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
void nextOut(Arc& arc) const {
|
| 214 | 214 |
Node n = (arc._id & 1) == 1 ? u(arc) : v(arc); |
| 215 | 215 |
int k = (arc._id >> _dim) + 1; |
| 216 | 216 |
if (k < _dim) {
|
| 217 | 217 |
arc._id = (k << (_dim-1)) | |
| 218 | 218 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
| 219 | 219 |
arc._id = (arc._id << 1) | (~(n._id >> k) & 1); |
| 220 | 220 |
} else {
|
| 221 | 221 |
arc._id = -1; |
| 222 | 222 |
} |
| 223 | 223 |
} |
| 224 | 224 |
|
| 225 | 225 |
void firstIn(Arc& arc, const Node& node) const {
|
| 226 | 226 |
arc._id = ((node._id >> 1) << 1) | (node._id & 1); |
| 227 | 227 |
} |
| 228 | 228 |
|
| 229 | 229 |
void nextIn(Arc& arc) const {
|
| 230 | 230 |
Node n = (arc._id & 1) == 1 ? v(arc) : u(arc); |
| 231 | 231 |
int k = (arc._id >> _dim) + 1; |
| 232 | 232 |
if (k < _dim) {
|
| 233 | 233 |
arc._id = (k << (_dim-1)) | |
| 234 | 234 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
| 235 | 235 |
arc._id = (arc._id << 1) | ((n._id >> k) & 1); |
| 236 | 236 |
} else {
|
| 237 | 237 |
arc._id = -1; |
| 238 | 238 |
} |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
static bool direction(Arc arc) {
|
| 242 | 242 |
return (arc._id & 1) == 1; |
| 243 | 243 |
} |
| 244 | 244 |
|
| 245 | 245 |
static Arc direct(Edge edge, bool dir) {
|
| 246 | 246 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
int dimension() const {
|
| 250 | 250 |
return _dim; |
| 251 | 251 |
} |
| 252 | 252 |
|
| 253 | 253 |
bool projection(Node node, int n) const {
|
| 254 | 254 |
return static_cast<bool>(node._id & (1 << n)); |
| 255 | 255 |
} |
| 256 | 256 |
|
| 257 | 257 |
int dimension(Edge edge) const {
|
| 258 | 258 |
return edge._id >> (_dim-1); |
| 259 | 259 |
} |
| 260 | 260 |
|
| 261 | 261 |
int dimension(Arc arc) const {
|
| 262 | 262 |
return arc._id >> _dim; |
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 | 265 |
int index(Node node) const {
|
| 266 | 266 |
return node._id; |
| 267 | 267 |
} |
| 268 | 268 |
|
| 269 | 269 |
Node operator()(int ix) const {
|
| 270 | 270 |
return Node(ix); |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
private: |
| 274 | 274 |
int _dim; |
| 275 | 275 |
int _node_num, _edge_num; |
| 276 | 276 |
}; |
| 277 | 277 |
|
| 278 | 278 |
|
| 279 | 279 |
typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase; |
| 280 | 280 |
|
| 281 | 281 |
/// \ingroup graphs |
| 282 | 282 |
/// |
| 283 | 283 |
/// \brief Hypercube graph class |
| 284 | 284 |
/// |
| 285 | 285 |
/// This class implements a special graph type. The nodes of the graph |
| 286 | 286 |
/// are indiced with integers with at most \c dim binary digits. |
| 287 | 287 |
/// Two nodes are connected in the graph if and only if their indices |
| 288 | 288 |
/// differ only on one position in the binary form. |
| 289 | 289 |
/// |
| 290 | 290 |
/// \note The type of the indices is chosen to \c int for efficiency |
| 291 | 291 |
/// reasons. Thus the maximum dimension of this implementation is 26 |
| 292 | 292 |
/// (assuming that the size of \c int is 32 bit). |
| 293 | 293 |
/// |
| 294 | 294 |
/// This graph type fully conforms to the \ref concepts::Graph |
| 295 |
/// "Graph" concept, and it also has an important extra feature |
|
| 296 |
/// that its maps are real \ref concepts::ReferenceMap |
|
| 297 |
/// " |
|
| 295 |
/// "Graph concept". |
|
| 298 | 296 |
class HypercubeGraph : public ExtendedHypercubeGraphBase {
|
| 299 | 297 |
public: |
| 300 | 298 |
|
| 301 | 299 |
typedef ExtendedHypercubeGraphBase Parent; |
| 302 | 300 |
|
| 303 | 301 |
/// \brief Constructs a hypercube graph with \c dim dimensions. |
| 304 | 302 |
/// |
| 305 | 303 |
/// Constructs a hypercube graph with \c dim dimensions. |
| 306 | 304 |
HypercubeGraph(int dim) { construct(dim); }
|
| 307 | 305 |
|
| 308 | 306 |
/// \brief The number of dimensions. |
| 309 | 307 |
/// |
| 310 | 308 |
/// Gives back the number of dimensions. |
| 311 | 309 |
int dimension() const {
|
| 312 | 310 |
return Parent::dimension(); |
| 313 | 311 |
} |
| 314 | 312 |
|
| 315 | 313 |
/// \brief Returns \c true if the n'th bit of the node is one. |
| 316 | 314 |
/// |
| 317 | 315 |
/// Returns \c true if the n'th bit of the node is one. |
| 318 | 316 |
bool projection(Node node, int n) const {
|
| 319 | 317 |
return Parent::projection(node, n); |
| 320 | 318 |
} |
| 321 | 319 |
|
| 322 | 320 |
/// \brief The dimension id of an edge. |
| 323 | 321 |
/// |
| 324 | 322 |
/// Gives back the dimension id of the given edge. |
| 325 | 323 |
/// It is in the [0..dim-1] range. |
| 326 | 324 |
int dimension(Edge edge) const {
|
| 327 | 325 |
return Parent::dimension(edge); |
| 328 | 326 |
} |
| 329 | 327 |
|
| 330 | 328 |
/// \brief The dimension id of an arc. |
| 331 | 329 |
/// |
| 332 | 330 |
/// Gives back the dimension id of the given arc. |
| 333 | 331 |
/// It is in the [0..dim-1] range. |
| 334 | 332 |
int dimension(Arc arc) const {
|
| 335 | 333 |
return Parent::dimension(arc); |
| 336 | 334 |
} |
| 337 | 335 |
|
| 338 | 336 |
/// \brief The index of a node. |
| 339 | 337 |
/// |
| 340 | 338 |
/// Gives back the index of the given node. |
| 341 | 339 |
/// The lower bits of the integer describes the node. |
| 342 | 340 |
int index(Node node) const {
|
| 343 | 341 |
return Parent::index(node); |
| 344 | 342 |
} |
| 345 | 343 |
|
| 346 | 344 |
/// \brief Gives back a node by its index. |
| 347 | 345 |
/// |
| 348 | 346 |
/// Gives back a node by its index. |
| 349 | 347 |
Node operator()(int ix) const {
|
| 350 | 348 |
return Parent::operator()(ix); |
| 351 | 349 |
} |
| 352 | 350 |
|
| 353 | 351 |
/// \brief Number of nodes. |
| 354 | 352 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 355 | 353 |
/// \brief Number of edges. |
| 356 | 354 |
int edgeNum() const { return Parent::edgeNum(); }
|
| 357 | 355 |
/// \brief Number of arcs. |
| 358 | 356 |
int arcNum() const { return Parent::arcNum(); }
|
| 359 | 357 |
|
| 360 | 358 |
/// \brief Linear combination map. |
| 361 | 359 |
/// |
| 362 | 360 |
/// This map makes possible to give back a linear combination |
| 363 | 361 |
/// for each node. It works like the \c std::accumulate function, |
| 364 | 362 |
/// so it accumulates the \c bf binary function with the \c fv first |
| 365 | 363 |
/// value. The map accumulates only on that positions (dimensions) |
| 366 | 364 |
/// where the index of the node is one. The values that have to be |
| 367 | 365 |
/// accumulated should be given by the \c begin and \c end iterators |
| 368 | 366 |
/// and the length of this range should be equal to the dimension |
| 369 | 367 |
/// number of the graph. |
| 370 | 368 |
/// |
| 371 | 369 |
///\code |
| 372 | 370 |
/// const int DIM = 3; |
| 373 | 371 |
/// HypercubeGraph graph(DIM); |
| 374 | 372 |
/// dim2::Point<double> base[DIM]; |
| 375 | 373 |
/// for (int k = 0; k < DIM; ++k) {
|
| 376 | 374 |
/// base[k].x = rnd(); |
| 377 | 375 |
/// base[k].y = rnd(); |
| 378 | 376 |
/// } |
| 379 | 377 |
/// HypercubeGraph::HyperMap<dim2::Point<double> > |
| 380 | 378 |
/// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0)); |
| 381 | 379 |
///\endcode |
| 382 | 380 |
/// |
| 383 | 381 |
/// \see HypercubeGraph |
| 384 | 382 |
template <typename T, typename BF = std::plus<T> > |
| 385 | 383 |
class HyperMap {
|
| 386 | 384 |
public: |
| 387 | 385 |
|
| 388 | 386 |
/// \brief The key type of the map |
| 389 | 387 |
typedef Node Key; |
| 390 | 388 |
/// \brief The value type of the map |
| 391 | 389 |
typedef T Value; |
| 392 | 390 |
|
| 393 | 391 |
/// \brief Constructor for HyperMap. |
| 394 | 392 |
/// |
| 395 | 393 |
/// Construct a HyperMap for the given graph. The values that have |
| 396 | 394 |
/// to be accumulated should be given by the \c begin and \c end |
| 397 | 395 |
/// iterators and the length of this range should be equal to the |
| 398 | 396 |
/// dimension number of the graph. |
| 399 | 397 |
/// |
| 400 | 398 |
/// This map accumulates the \c bf binary function with the \c fv |
| 401 | 399 |
/// first value on that positions (dimensions) where the index of |
| 402 | 400 |
/// the node is one. |
| 403 | 401 |
template <typename It> |
| 404 | 402 |
HyperMap(const Graph& graph, It begin, It end, |
| 405 | 403 |
T fv = 0, const BF& bf = BF()) |
| 406 | 404 |
: _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf) |
| 407 | 405 |
{
|
| 408 | 406 |
LEMON_ASSERT(_values.size() == graph.dimension(), |
| 409 | 407 |
"Wrong size of range"); |
| 410 | 408 |
} |
| 411 | 409 |
|
| 412 | 410 |
/// \brief The partial accumulated value. |
| 413 | 411 |
/// |
| 414 | 412 |
/// Gives back the partial accumulated value. |
| 415 | 413 |
Value operator[](const Key& k) const {
|
| 416 | 414 |
Value val = _first_value; |
| 417 | 415 |
int id = _graph.index(k); |
| 418 | 416 |
int n = 0; |
| 419 | 417 |
while (id != 0) {
|
| 420 | 418 |
if (id & 1) {
|
| 421 | 419 |
val = _bin_func(val, _values[n]); |
| 422 | 420 |
} |
| 423 | 421 |
id >>= 1; |
| 424 | 422 |
++n; |
| 425 | 423 |
} |
| 426 | 424 |
return val; |
| 427 | 425 |
} |
| 428 | 426 |
|
| 429 | 427 |
private: |
| 430 | 428 |
const Graph& _graph; |
| 431 | 429 |
std::vector<T> _values; |
| 432 | 430 |
T _first_value; |
| 433 | 431 |
BF _bin_func; |
| 434 | 432 |
}; |
| 435 | 433 |
|
| 436 | 434 |
}; |
| 437 | 435 |
|
| 438 | 436 |
} |
| 439 | 437 |
|
| 440 | 438 |
#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_LIST_GRAPH_H |
| 20 | 20 |
#define LEMON_LIST_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief ListDigraph, ListGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/error.h> |
| 28 | 28 |
#include <lemon/bits/graph_extender.h> |
| 29 | 29 |
|
| 30 | 30 |
#include <vector> |
| 31 | 31 |
#include <list> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
class ListDigraphBase {
|
| 36 | 36 |
|
| 37 | 37 |
protected: |
| 38 | 38 |
struct NodeT {
|
| 39 | 39 |
int first_in, first_out; |
| 40 | 40 |
int prev, next; |
| 41 | 41 |
}; |
| 42 | 42 |
|
| 43 | 43 |
struct ArcT {
|
| 44 | 44 |
int target, source; |
| 45 | 45 |
int prev_in, prev_out; |
| 46 | 46 |
int next_in, next_out; |
| 47 | 47 |
}; |
| 48 | 48 |
|
| 49 | 49 |
std::vector<NodeT> nodes; |
| 50 | 50 |
|
| 51 | 51 |
int first_node; |
| 52 | 52 |
|
| 53 | 53 |
int first_free_node; |
| 54 | 54 |
|
| 55 | 55 |
std::vector<ArcT> arcs; |
| 56 | 56 |
|
| 57 | 57 |
int first_free_arc; |
| 58 | 58 |
|
| 59 | 59 |
public: |
| 60 | 60 |
|
| 61 | 61 |
typedef ListDigraphBase Digraph; |
| 62 | 62 |
|
| 63 | 63 |
class Node {
|
| 64 | 64 |
friend class ListDigraphBase; |
| 65 | 65 |
protected: |
| 66 | 66 |
|
| 67 | 67 |
int id; |
| 68 | 68 |
explicit Node(int pid) { id = pid;}
|
| 69 | 69 |
|
| 70 | 70 |
public: |
| 71 | 71 |
Node() {}
|
| 72 | 72 |
Node (Invalid) { id = -1; }
|
| 73 | 73 |
bool operator==(const Node& node) const {return id == node.id;}
|
| 74 | 74 |
bool operator!=(const Node& node) const {return id != node.id;}
|
| 75 | 75 |
bool operator<(const Node& node) const {return id < node.id;}
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
class Arc {
|
| 79 | 79 |
friend class ListDigraphBase; |
| 80 | 80 |
protected: |
| 81 | 81 |
|
| 82 | 82 |
int id; |
| 83 | 83 |
explicit Arc(int pid) { id = pid;}
|
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
Arc() {}
|
| 87 | 87 |
Arc (Invalid) { id = -1; }
|
| 88 | 88 |
bool operator==(const Arc& arc) const {return id == arc.id;}
|
| 89 | 89 |
bool operator!=(const Arc& arc) const {return id != arc.id;}
|
| 90 | 90 |
bool operator<(const Arc& arc) const {return id < arc.id;}
|
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
|
| 94 | 94 |
|
| 95 | 95 |
ListDigraphBase() |
| 96 | 96 |
: nodes(), first_node(-1), |
| 97 | 97 |
first_free_node(-1), arcs(), first_free_arc(-1) {}
|
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
int maxNodeId() const { return nodes.size()-1; }
|
| 101 | 101 |
int maxArcId() const { return arcs.size()-1; }
|
| 102 | 102 |
|
| 103 | 103 |
Node source(Arc e) const { return Node(arcs[e.id].source); }
|
| 104 | 104 |
Node target(Arc e) const { return Node(arcs[e.id].target); }
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
void first(Node& node) const {
|
| 108 | 108 |
node.id = first_node; |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
void next(Node& node) const {
|
| 112 | 112 |
node.id = nodes[node.id].next; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
|
| 116 | 116 |
void first(Arc& arc) const {
|
| 117 | 117 |
int n; |
| 118 | 118 |
for(n = first_node; |
| 119 | 119 |
n!=-1 && nodes[n].first_in == -1; |
| 120 | 120 |
n = nodes[n].next) {}
|
| 121 | 121 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
void next(Arc& arc) const {
|
| 125 | 125 |
if (arcs[arc.id].next_in != -1) {
|
| 126 | 126 |
arc.id = arcs[arc.id].next_in; |
| 127 | 127 |
} else {
|
| 128 | 128 |
int n; |
| 129 | 129 |
for(n = nodes[arcs[arc.id].target].next; |
| 130 | 130 |
n!=-1 && nodes[n].first_in == -1; |
| 131 | 131 |
n = nodes[n].next) {}
|
| 132 | 132 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
| 133 | 133 |
} |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
void firstOut(Arc &e, const Node& v) const {
|
| 137 | 137 |
e.id = nodes[v.id].first_out; |
| 138 | 138 |
} |
| 139 | 139 |
void nextOut(Arc &e) const {
|
| 140 | 140 |
e.id=arcs[e.id].next_out; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
void firstIn(Arc &e, const Node& v) const {
|
| 144 | 144 |
e.id = nodes[v.id].first_in; |
| 145 | 145 |
} |
| 146 | 146 |
void nextIn(Arc &e) const {
|
| 147 | 147 |
e.id=arcs[e.id].next_in; |
| 148 | 148 |
} |
| 149 | 149 |
|
| 150 | 150 |
|
| 151 | 151 |
static int id(Node v) { return v.id; }
|
| 152 | 152 |
static int id(Arc e) { return e.id; }
|
| 153 | 153 |
|
| 154 | 154 |
static Node nodeFromId(int id) { return Node(id);}
|
| 155 | 155 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 156 | 156 |
|
| 157 | 157 |
bool valid(Node n) const {
|
| 158 | 158 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
| 159 | 159 |
nodes[n.id].prev != -2; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
bool valid(Arc a) const {
|
| 163 | 163 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
| 164 | 164 |
arcs[a.id].prev_in != -2; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
Node addNode() {
|
| 168 | 168 |
int n; |
| 169 | 169 |
|
| 170 | 170 |
if(first_free_node==-1) {
|
| 171 | 171 |
n = nodes.size(); |
| 172 | 172 |
nodes.push_back(NodeT()); |
| 173 | 173 |
} else {
|
| 174 | 174 |
n = first_free_node; |
| 175 | 175 |
first_free_node = nodes[n].next; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
nodes[n].next = first_node; |
| 179 | 179 |
if(first_node != -1) nodes[first_node].prev = n; |
| 180 | 180 |
first_node = n; |
| 181 | 181 |
nodes[n].prev = -1; |
| 182 | 182 |
|
| 183 | 183 |
nodes[n].first_in = nodes[n].first_out = -1; |
| 184 | 184 |
|
| 185 | 185 |
return Node(n); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
Arc addArc(Node u, Node v) {
|
| 189 | 189 |
int n; |
| 190 | 190 |
|
| 191 | 191 |
if (first_free_arc == -1) {
|
| 192 | 192 |
n = arcs.size(); |
| 193 | 193 |
arcs.push_back(ArcT()); |
| 194 | 194 |
} else {
|
| 195 | 195 |
n = first_free_arc; |
| 196 | 196 |
first_free_arc = arcs[n].next_in; |
| 197 | 197 |
} |
| 198 | 198 |
|
| 199 | 199 |
arcs[n].source = u.id; |
| 200 | 200 |
arcs[n].target = v.id; |
| 201 | 201 |
|
| 202 | 202 |
arcs[n].next_out = nodes[u.id].first_out; |
| 203 | 203 |
if(nodes[u.id].first_out != -1) {
|
| 204 | 204 |
arcs[nodes[u.id].first_out].prev_out = n; |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 | 207 |
arcs[n].next_in = nodes[v.id].first_in; |
| 208 | 208 |
if(nodes[v.id].first_in != -1) {
|
| 209 | 209 |
arcs[nodes[v.id].first_in].prev_in = n; |
| 210 | 210 |
} |
| 211 | 211 |
|
| 212 | 212 |
arcs[n].prev_in = arcs[n].prev_out = -1; |
| 213 | 213 |
|
| 214 | 214 |
nodes[u.id].first_out = nodes[v.id].first_in = n; |
| 215 | 215 |
|
| 216 | 216 |
return Arc(n); |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 | 219 |
void erase(const Node& node) {
|
| 220 | 220 |
int n = node.id; |
| 221 | 221 |
|
| 222 | 222 |
if(nodes[n].next != -1) {
|
| 223 | 223 |
nodes[nodes[n].next].prev = nodes[n].prev; |
| 224 | 224 |
} |
| 225 | 225 |
|
| 226 | 226 |
if(nodes[n].prev != -1) {
|
| 227 | 227 |
nodes[nodes[n].prev].next = nodes[n].next; |
| 228 | 228 |
} else {
|
| 229 | 229 |
first_node = nodes[n].next; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
nodes[n].next = first_free_node; |
| 233 | 233 |
first_free_node = n; |
| 234 | 234 |
nodes[n].prev = -2; |
| 235 | 235 |
|
| 236 | 236 |
} |
| 237 | 237 |
|
| 238 | 238 |
void erase(const Arc& arc) {
|
| 239 | 239 |
int n = arc.id; |
| 240 | 240 |
|
| 241 | 241 |
if(arcs[n].next_in!=-1) {
|
| 242 | 242 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
| 243 | 243 |
} |
| 244 | 244 |
|
| 245 | 245 |
if(arcs[n].prev_in!=-1) {
|
| 246 | 246 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
| 247 | 247 |
} else {
|
| 248 | 248 |
nodes[arcs[n].target].first_in = arcs[n].next_in; |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
|
| 252 | 252 |
if(arcs[n].next_out!=-1) {
|
| 253 | 253 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
| 254 | 254 |
} |
| 255 | 255 |
|
| 256 | 256 |
if(arcs[n].prev_out!=-1) {
|
| 257 | 257 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 258 | 258 |
} else {
|
| 259 | 259 |
nodes[arcs[n].source].first_out = arcs[n].next_out; |
| 260 | 260 |
} |
| 261 | 261 |
|
| 262 | 262 |
arcs[n].next_in = first_free_arc; |
| 263 | 263 |
first_free_arc = n; |
| 264 | 264 |
arcs[n].prev_in = -2; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
void clear() {
|
| 268 | 268 |
arcs.clear(); |
| 269 | 269 |
nodes.clear(); |
| 270 | 270 |
first_node = first_free_node = first_free_arc = -1; |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
protected: |
| 274 | 274 |
void changeTarget(Arc e, Node n) |
| 275 | 275 |
{
|
| 276 | 276 |
if(arcs[e.id].next_in != -1) |
| 277 | 277 |
arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in; |
| 278 | 278 |
if(arcs[e.id].prev_in != -1) |
| 279 | 279 |
arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in; |
| 280 | 280 |
else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in; |
| 281 | 281 |
if (nodes[n.id].first_in != -1) {
|
| 282 | 282 |
arcs[nodes[n.id].first_in].prev_in = e.id; |
| 283 | 283 |
} |
| 284 | 284 |
arcs[e.id].target = n.id; |
| 285 | 285 |
arcs[e.id].prev_in = -1; |
| 286 | 286 |
arcs[e.id].next_in = nodes[n.id].first_in; |
| 287 | 287 |
nodes[n.id].first_in = e.id; |
| 288 | 288 |
} |
| 289 | 289 |
void changeSource(Arc e, Node n) |
| 290 | 290 |
{
|
| 291 | 291 |
if(arcs[e.id].next_out != -1) |
| 292 | 292 |
arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
| 293 | 293 |
if(arcs[e.id].prev_out != -1) |
| 294 | 294 |
arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
| 295 | 295 |
else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
| 296 | 296 |
if (nodes[n.id].first_out != -1) {
|
| 297 | 297 |
arcs[nodes[n.id].first_out].prev_out = e.id; |
| 298 | 298 |
} |
| 299 | 299 |
arcs[e.id].source = n.id; |
| 300 | 300 |
arcs[e.id].prev_out = -1; |
| 301 | 301 |
arcs[e.id].next_out = nodes[n.id].first_out; |
| 302 | 302 |
nodes[n.id].first_out = e.id; |
| 303 | 303 |
} |
| 304 | 304 |
|
| 305 | 305 |
}; |
| 306 | 306 |
|
| 307 | 307 |
typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
| 308 | 308 |
|
| 309 | 309 |
/// \addtogroup graphs |
| 310 | 310 |
/// @{
|
| 311 | 311 |
|
| 312 | 312 |
///A general directed graph structure. |
| 313 | 313 |
|
| 314 | 314 |
///\ref ListDigraph is a simple and fast <em>directed graph</em> |
| 315 | 315 |
///implementation based on static linked lists that are stored in |
| 316 | 316 |
///\c std::vector structures. |
| 317 | 317 |
/// |
| 318 | 318 |
///It conforms to the \ref concepts::Digraph "Digraph concept" and it |
| 319 | 319 |
///also provides several useful additional functionalities. |
| 320 | 320 |
///Most of the member functions and nested classes are documented |
| 321 | 321 |
///only in the concept class. |
| 322 | 322 |
/// |
| 323 |
///An important extra feature of this digraph implementation is that |
|
| 324 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
|
| 325 |
/// |
|
| 326 | 323 |
///\sa concepts::Digraph |
| 327 | 324 |
|
| 328 | 325 |
class ListDigraph : public ExtendedListDigraphBase {
|
| 329 | 326 |
private: |
| 330 | 327 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
| 331 | 328 |
|
| 332 | 329 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
| 333 | 330 |
/// |
| 334 | 331 |
ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
|
| 335 | 332 |
///\brief Assignment of ListDigraph to another one is \e not allowed. |
| 336 | 333 |
///Use copyDigraph() instead. |
| 337 | 334 |
|
| 338 | 335 |
///Assignment of ListDigraph to another one is \e not allowed. |
| 339 | 336 |
///Use copyDigraph() instead. |
| 340 | 337 |
void operator=(const ListDigraph &) {}
|
| 341 | 338 |
public: |
| 342 | 339 |
|
| 343 | 340 |
typedef ExtendedListDigraphBase Parent; |
| 344 | 341 |
|
| 345 | 342 |
/// Constructor |
| 346 | 343 |
|
| 347 | 344 |
/// Constructor. |
| 348 | 345 |
/// |
| 349 | 346 |
ListDigraph() {}
|
| 350 | 347 |
|
| 351 | 348 |
///Add a new node to the digraph. |
| 352 | 349 |
|
| 353 | 350 |
///Add a new node to the digraph. |
| 354 | 351 |
///\return The new node. |
| 355 | 352 |
Node addNode() { return Parent::addNode(); }
|
| 356 | 353 |
|
| 357 | 354 |
///Add a new arc to the digraph. |
| 358 | 355 |
|
| 359 | 356 |
///Add a new arc to the digraph with source node \c s |
| 360 | 357 |
///and target node \c t. |
| 361 | 358 |
///\return The new arc. |
| 362 | 359 |
Arc addArc(const Node& s, const Node& t) {
|
| 363 | 360 |
return Parent::addArc(s, t); |
| 364 | 361 |
} |
| 365 | 362 |
|
| 366 | 363 |
///\brief Erase a node from the digraph. |
| 367 | 364 |
/// |
| 368 | 365 |
///Erase a node from the digraph. |
| 369 | 366 |
/// |
| 370 | 367 |
void erase(const Node& n) { Parent::erase(n); }
|
| 371 | 368 |
|
| 372 | 369 |
///\brief Erase an arc from the digraph. |
| 373 | 370 |
/// |
| 374 | 371 |
///Erase an arc from the digraph. |
| 375 | 372 |
/// |
| 376 | 373 |
void erase(const Arc& a) { Parent::erase(a); }
|
| 377 | 374 |
|
| 378 | 375 |
/// Node validity check |
| 379 | 376 |
|
| 380 | 377 |
/// This function gives back true if the given node is valid, |
| 381 | 378 |
/// ie. it is a real node of the graph. |
| 382 | 379 |
/// |
| 383 | 380 |
/// \warning A Node pointing to a removed item |
| 384 | 381 |
/// could become valid again later if new nodes are |
| 385 | 382 |
/// added to the graph. |
| 386 | 383 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 387 | 384 |
|
| 388 | 385 |
/// Arc validity check |
| 389 | 386 |
|
| 390 | 387 |
/// This function gives back true if the given arc is valid, |
| 391 | 388 |
/// ie. it is a real arc of the graph. |
| 392 | 389 |
/// |
| 393 | 390 |
/// \warning An Arc pointing to a removed item |
| 394 | 391 |
/// could become valid again later if new nodes are |
| 395 | 392 |
/// added to the graph. |
| 396 | 393 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 397 | 394 |
|
| 398 | 395 |
/// Change the target of \c a to \c n |
| 399 | 396 |
|
| 400 | 397 |
/// Change the target of \c a to \c n |
| 401 | 398 |
/// |
| 402 | 399 |
///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing |
| 403 | 400 |
///the changed arc remain valid. However <tt>InArcIt</tt>s are |
| 404 | 401 |
///invalidated. |
| 405 | 402 |
/// |
| 406 | 403 |
///\warning This functionality cannot be used together with the Snapshot |
| 407 | 404 |
///feature. |
| 408 | 405 |
void changeTarget(Arc a, Node n) {
|
| 409 | 406 |
Parent::changeTarget(a,n); |
| 410 | 407 |
} |
| 411 | 408 |
/// Change the source of \c a to \c n |
| 412 | 409 |
|
| 413 | 410 |
/// Change the source of \c a to \c n |
| 414 | 411 |
/// |
| 415 | 412 |
///\note The <tt>InArcIt</tt>s referencing the changed arc remain |
| 416 | 413 |
///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are |
| 417 | 414 |
///invalidated. |
| 418 | 415 |
/// |
| 419 | 416 |
///\warning This functionality cannot be used together with the Snapshot |
| 420 | 417 |
///feature. |
| 421 | 418 |
void changeSource(Arc a, Node n) {
|
| 422 | 419 |
Parent::changeSource(a,n); |
| 423 | 420 |
} |
| 424 | 421 |
|
| 425 | 422 |
/// Invert the direction of an arc. |
| 426 | 423 |
|
| 427 | 424 |
///\note The <tt>ArcIt</tt>s referencing the changed arc remain |
| 428 | 425 |
///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are |
| 429 | 426 |
///invalidated. |
| 430 | 427 |
/// |
| 431 | 428 |
///\warning This functionality cannot be used together with the Snapshot |
| 432 | 429 |
///feature. |
| 433 | 430 |
void reverseArc(Arc e) {
|
| 434 | 431 |
Node t=target(e); |
| 435 | 432 |
changeTarget(e,source(e)); |
| 436 | 433 |
changeSource(e,t); |
| 437 | 434 |
} |
| 438 | 435 |
|
| 439 | 436 |
/// Reserve memory for nodes. |
| 440 | 437 |
|
| 441 | 438 |
/// Using this function it is possible to avoid the superfluous memory |
| 442 | 439 |
/// allocation: if you know that the digraph you want to build will |
| 443 | 440 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 444 | 441 |
/// then it is worth reserving space for this amount before starting |
| 445 | 442 |
/// to build the digraph. |
| 446 | 443 |
/// \sa reserveArc |
| 447 | 444 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 448 | 445 |
|
| 449 | 446 |
/// Reserve memory for arcs. |
| 450 | 447 |
|
| 451 | 448 |
/// Using this function it is possible to avoid the superfluous memory |
| 452 | 449 |
/// allocation: if you know that the digraph you want to build will |
| 453 | 450 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 454 | 451 |
/// then it is worth reserving space for this amount before starting |
| 455 | 452 |
/// to build the digraph. |
| 456 | 453 |
/// \sa reserveNode |
| 457 | 454 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 458 | 455 |
|
| 459 | 456 |
///Contract two nodes. |
| 460 | 457 |
|
| 461 | 458 |
///This function contracts two nodes. |
| 462 | 459 |
///Node \p b will be removed but instead of deleting |
| 463 | 460 |
///incident arcs, they will be joined to \p a. |
| 464 | 461 |
///The last parameter \p r controls whether to remove loops. \c true |
| 465 | 462 |
///means that loops will be removed. |
| 466 | 463 |
/// |
| 467 | 464 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
| 468 | 465 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s |
| 469 | 466 |
///may be invalidated. |
| 470 | 467 |
/// |
| 471 | 468 |
///\warning This functionality cannot be used together with the Snapshot |
| 472 | 469 |
///feature. |
| 473 | 470 |
void contract(Node a, Node b, bool r = true) |
| 474 | 471 |
{
|
| 475 | 472 |
for(OutArcIt e(*this,b);e!=INVALID;) {
|
| 476 | 473 |
OutArcIt f=e; |
| 477 | 474 |
++f; |
| 478 | 475 |
if(r && target(e)==a) erase(e); |
| 479 | 476 |
else changeSource(e,a); |
| 480 | 477 |
e=f; |
| 481 | 478 |
} |
| 482 | 479 |
for(InArcIt e(*this,b);e!=INVALID;) {
|
| 483 | 480 |
InArcIt f=e; |
| 484 | 481 |
++f; |
| 485 | 482 |
if(r && source(e)==a) erase(e); |
| 486 | 483 |
else changeTarget(e,a); |
| 487 | 484 |
e=f; |
| 488 | 485 |
} |
| 489 | 486 |
erase(b); |
| 490 | 487 |
} |
| 491 | 488 |
|
| 492 | 489 |
///Split a node. |
| 493 | 490 |
|
| 494 | 491 |
///This function splits a node. First a new node is added to the digraph, |
| 495 | 492 |
///then the source of each outgoing arc of \c n is moved to this new node. |
| 496 | 493 |
///If \c connect is \c true (this is the default value), then a new arc |
| 497 | 494 |
///from \c n to the newly created node is also added. |
| 498 | 495 |
///\return The newly created node. |
| 499 | 496 |
/// |
| 500 | 497 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
| 501 | 498 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may |
| 502 | 499 |
///be invalidated. |
| 503 | 500 |
/// |
| 504 | 501 |
///\warning This functionality cannot be used in conjunction with the |
| 505 | 502 |
///Snapshot feature. |
| 506 | 503 |
Node split(Node n, bool connect = true) {
|
| 507 | 504 |
Node b = addNode(); |
| 508 | 505 |
for(OutArcIt e(*this,n);e!=INVALID;) {
|
| 509 | 506 |
OutArcIt f=e; |
| 510 | 507 |
++f; |
| 511 | 508 |
changeSource(e,b); |
| 512 | 509 |
e=f; |
| 513 | 510 |
} |
| 514 | 511 |
if (connect) addArc(n,b); |
| 515 | 512 |
return b; |
| 516 | 513 |
} |
| 517 | 514 |
|
| 518 | 515 |
///Split an arc. |
| 519 | 516 |
|
| 520 | 517 |
///This function splits an arc. First a new node \c b is added to |
| 521 | 518 |
///the digraph, then the original arc is re-targeted to \c |
| 522 | 519 |
///b. Finally an arc from \c b to the original target is added. |
| 523 | 520 |
/// |
| 524 | 521 |
///\return The newly created node. |
| 525 | 522 |
/// |
| 526 | 523 |
///\warning This functionality cannot be used together with the |
| 527 | 524 |
///Snapshot feature. |
| 528 | 525 |
Node split(Arc e) {
|
| 529 | 526 |
Node b = addNode(); |
| 530 | 527 |
addArc(b,target(e)); |
| 531 | 528 |
changeTarget(e,b); |
| 532 | 529 |
return b; |
| 533 | 530 |
} |
| 534 | 531 |
|
| 535 | 532 |
/// \brief Class to make a snapshot of the digraph and restore |
| 536 | 533 |
/// it later. |
| 537 | 534 |
/// |
| 538 | 535 |
/// Class to make a snapshot of the digraph and restore it later. |
| 539 | 536 |
/// |
| 540 | 537 |
/// The newly added nodes and arcs can be removed using the |
| 541 | 538 |
/// restore() function. |
| 542 | 539 |
/// |
| 543 | 540 |
/// \warning Arc and node deletions and other modifications (e.g. |
| 544 | 541 |
/// contracting, splitting, reversing arcs or nodes) cannot be |
| 545 | 542 |
/// restored. These events invalidate the snapshot. |
| 546 | 543 |
class Snapshot {
|
| 547 | 544 |
protected: |
| 548 | 545 |
|
| 549 | 546 |
typedef Parent::NodeNotifier NodeNotifier; |
| 550 | 547 |
|
| 551 | 548 |
class NodeObserverProxy : public NodeNotifier::ObserverBase {
|
| 552 | 549 |
public: |
| 553 | 550 |
|
| 554 | 551 |
NodeObserverProxy(Snapshot& _snapshot) |
| 555 | 552 |
: snapshot(_snapshot) {}
|
| 556 | 553 |
|
| 557 | 554 |
using NodeNotifier::ObserverBase::attach; |
| 558 | 555 |
using NodeNotifier::ObserverBase::detach; |
| 559 | 556 |
using NodeNotifier::ObserverBase::attached; |
| 560 | 557 |
|
| 561 | 558 |
protected: |
| 562 | 559 |
|
| 563 | 560 |
virtual void add(const Node& node) {
|
| 564 | 561 |
snapshot.addNode(node); |
| 565 | 562 |
} |
| 566 | 563 |
virtual void add(const std::vector<Node>& nodes) {
|
| 567 | 564 |
for (int i = nodes.size() - 1; i >= 0; ++i) {
|
| 568 | 565 |
snapshot.addNode(nodes[i]); |
| 569 | 566 |
} |
| 570 | 567 |
} |
| 571 | 568 |
virtual void erase(const Node& node) {
|
| 572 | 569 |
snapshot.eraseNode(node); |
| 573 | 570 |
} |
| 574 | 571 |
virtual void erase(const std::vector<Node>& nodes) {
|
| 575 | 572 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 576 | 573 |
snapshot.eraseNode(nodes[i]); |
| 577 | 574 |
} |
| 578 | 575 |
} |
| 579 | 576 |
virtual void build() {
|
| 580 | 577 |
Node node; |
| 581 | 578 |
std::vector<Node> nodes; |
| 582 | 579 |
for (notifier()->first(node); node != INVALID; |
| 583 | 580 |
notifier()->next(node)) {
|
| 584 | 581 |
nodes.push_back(node); |
| 585 | 582 |
} |
| 586 | 583 |
for (int i = nodes.size() - 1; i >= 0; --i) {
|
| 587 | 584 |
snapshot.addNode(nodes[i]); |
| 588 | 585 |
} |
| 589 | 586 |
} |
| 590 | 587 |
virtual void clear() {
|
| 591 | 588 |
Node node; |
| 592 | 589 |
for (notifier()->first(node); node != INVALID; |
| 593 | 590 |
notifier()->next(node)) {
|
| 594 | 591 |
snapshot.eraseNode(node); |
| 595 | 592 |
} |
| 596 | 593 |
} |
| 597 | 594 |
|
| 598 | 595 |
Snapshot& snapshot; |
| 599 | 596 |
}; |
| 600 | 597 |
|
| 601 | 598 |
class ArcObserverProxy : public ArcNotifier::ObserverBase {
|
| 602 | 599 |
public: |
| 603 | 600 |
|
| 604 | 601 |
ArcObserverProxy(Snapshot& _snapshot) |
| 605 | 602 |
: snapshot(_snapshot) {}
|
| 606 | 603 |
|
| 607 | 604 |
using ArcNotifier::ObserverBase::attach; |
| 608 | 605 |
using ArcNotifier::ObserverBase::detach; |
| 609 | 606 |
using ArcNotifier::ObserverBase::attached; |
| 610 | 607 |
|
| 611 | 608 |
protected: |
| 612 | 609 |
|
| 613 | 610 |
virtual void add(const Arc& arc) {
|
| 614 | 611 |
snapshot.addArc(arc); |
| 615 | 612 |
} |
| 616 | 613 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 617 | 614 |
for (int i = arcs.size() - 1; i >= 0; ++i) {
|
| 618 | 615 |
snapshot.addArc(arcs[i]); |
| 619 | 616 |
} |
| 620 | 617 |
} |
| 621 | 618 |
virtual void erase(const Arc& arc) {
|
| 622 | 619 |
snapshot.eraseArc(arc); |
| 623 | 620 |
} |
| 624 | 621 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 625 | 622 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 626 | 623 |
snapshot.eraseArc(arcs[i]); |
| 627 | 624 |
} |
| 628 | 625 |
} |
| 629 | 626 |
virtual void build() {
|
| 630 | 627 |
Arc arc; |
| 631 | 628 |
std::vector<Arc> arcs; |
| 632 | 629 |
for (notifier()->first(arc); arc != INVALID; |
| 633 | 630 |
notifier()->next(arc)) {
|
| 634 | 631 |
arcs.push_back(arc); |
| 635 | 632 |
} |
| 636 | 633 |
for (int i = arcs.size() - 1; i >= 0; --i) {
|
| 637 | 634 |
snapshot.addArc(arcs[i]); |
| 638 | 635 |
} |
| 639 | 636 |
} |
| 640 | 637 |
virtual void clear() {
|
| 641 | 638 |
Arc arc; |
| 642 | 639 |
for (notifier()->first(arc); arc != INVALID; |
| 643 | 640 |
notifier()->next(arc)) {
|
| 644 | 641 |
snapshot.eraseArc(arc); |
| 645 | 642 |
} |
| 646 | 643 |
} |
| 647 | 644 |
|
| 648 | 645 |
Snapshot& snapshot; |
| 649 | 646 |
}; |
| 650 | 647 |
|
| 651 | 648 |
ListDigraph *digraph; |
| 652 | 649 |
|
| 653 | 650 |
NodeObserverProxy node_observer_proxy; |
| 654 | 651 |
ArcObserverProxy arc_observer_proxy; |
| 655 | 652 |
|
| 656 | 653 |
std::list<Node> added_nodes; |
| 657 | 654 |
std::list<Arc> added_arcs; |
| 658 | 655 |
|
| 659 | 656 |
|
| 660 | 657 |
void addNode(const Node& node) {
|
| 661 | 658 |
added_nodes.push_front(node); |
| 662 | 659 |
} |
| 663 | 660 |
void eraseNode(const Node& node) {
|
| 664 | 661 |
std::list<Node>::iterator it = |
| 665 | 662 |
std::find(added_nodes.begin(), added_nodes.end(), node); |
| 666 | 663 |
if (it == added_nodes.end()) {
|
| 667 | 664 |
clear(); |
| 668 | 665 |
arc_observer_proxy.detach(); |
| 669 | 666 |
throw NodeNotifier::ImmediateDetach(); |
| 670 | 667 |
} else {
|
| 671 | 668 |
added_nodes.erase(it); |
| 672 | 669 |
} |
| 673 | 670 |
} |
| 674 | 671 |
|
| 675 | 672 |
void addArc(const Arc& arc) {
|
| 676 | 673 |
added_arcs.push_front(arc); |
| 677 | 674 |
} |
| 678 | 675 |
void eraseArc(const Arc& arc) {
|
| 679 | 676 |
std::list<Arc>::iterator it = |
| 680 | 677 |
std::find(added_arcs.begin(), added_arcs.end(), arc); |
| 681 | 678 |
if (it == added_arcs.end()) {
|
| 682 | 679 |
clear(); |
| 683 | 680 |
node_observer_proxy.detach(); |
| 684 | 681 |
throw ArcNotifier::ImmediateDetach(); |
| 685 | 682 |
} else {
|
| 686 | 683 |
added_arcs.erase(it); |
| 687 | 684 |
} |
| 688 | 685 |
} |
| 689 | 686 |
|
| 690 | 687 |
void attach(ListDigraph &_digraph) {
|
| 691 | 688 |
digraph = &_digraph; |
| 692 | 689 |
node_observer_proxy.attach(digraph->notifier(Node())); |
| 693 | 690 |
arc_observer_proxy.attach(digraph->notifier(Arc())); |
| 694 | 691 |
} |
| 695 | 692 |
|
| 696 | 693 |
void detach() {
|
| 697 | 694 |
node_observer_proxy.detach(); |
| 698 | 695 |
arc_observer_proxy.detach(); |
| 699 | 696 |
} |
| 700 | 697 |
|
| 701 | 698 |
bool attached() const {
|
| 702 | 699 |
return node_observer_proxy.attached(); |
| 703 | 700 |
} |
| 704 | 701 |
|
| 705 | 702 |
void clear() {
|
| 706 | 703 |
added_nodes.clear(); |
| 707 | 704 |
added_arcs.clear(); |
| 708 | 705 |
} |
| 709 | 706 |
|
| 710 | 707 |
public: |
| 711 | 708 |
|
| 712 | 709 |
/// \brief Default constructor. |
| 713 | 710 |
/// |
| 714 | 711 |
/// Default constructor. |
| 715 | 712 |
/// To actually make a snapshot you must call save(). |
| 716 | 713 |
Snapshot() |
| 717 | 714 |
: digraph(0), node_observer_proxy(*this), |
| 718 | 715 |
arc_observer_proxy(*this) {}
|
| 719 | 716 |
|
| 720 | 717 |
/// \brief Constructor that immediately makes a snapshot. |
| 721 | 718 |
/// |
| 722 | 719 |
/// This constructor immediately makes a snapshot of the digraph. |
| 723 | 720 |
/// \param _digraph The digraph we make a snapshot of. |
| 724 | 721 |
Snapshot(ListDigraph &_digraph) |
| 725 | 722 |
: node_observer_proxy(*this), |
| 726 | 723 |
arc_observer_proxy(*this) {
|
| 727 | 724 |
attach(_digraph); |
| 728 | 725 |
} |
| 729 | 726 |
|
| 730 | 727 |
/// \brief Make a snapshot. |
| 731 | 728 |
/// |
| 732 | 729 |
/// Make a snapshot of the digraph. |
| 733 | 730 |
/// |
| 734 | 731 |
/// This function can be called more than once. In case of a repeated |
| 735 | 732 |
/// call, the previous snapshot gets lost. |
| 736 | 733 |
/// \param _digraph The digraph we make the snapshot of. |
| 737 | 734 |
void save(ListDigraph &_digraph) {
|
| 738 | 735 |
if (attached()) {
|
| 739 | 736 |
detach(); |
| 740 | 737 |
clear(); |
| 741 | 738 |
} |
| 742 | 739 |
attach(_digraph); |
| 743 | 740 |
} |
| 744 | 741 |
|
| 745 | 742 |
/// \brief Undo the changes until the last snapshot. |
| 746 | 743 |
// |
| 747 | 744 |
/// Undo the changes until the last snapshot created by save(). |
| 748 | 745 |
void restore() {
|
| 749 | 746 |
detach(); |
| 750 | 747 |
for(std::list<Arc>::iterator it = added_arcs.begin(); |
| 751 | 748 |
it != added_arcs.end(); ++it) {
|
| 752 | 749 |
digraph->erase(*it); |
| 753 | 750 |
} |
| 754 | 751 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
| 755 | 752 |
it != added_nodes.end(); ++it) {
|
| 756 | 753 |
digraph->erase(*it); |
| 757 | 754 |
} |
| 758 | 755 |
clear(); |
| 759 | 756 |
} |
| 760 | 757 |
|
| 761 | 758 |
/// \brief Gives back true when the snapshot is valid. |
| 762 | 759 |
/// |
| 763 | 760 |
/// Gives back true when the snapshot is valid. |
| 764 | 761 |
bool valid() const {
|
| 765 | 762 |
return attached(); |
| 766 | 763 |
} |
| 767 | 764 |
}; |
| 768 | 765 |
|
| 769 | 766 |
}; |
| 770 | 767 |
|
| 771 | 768 |
///@} |
| 772 | 769 |
|
| 773 | 770 |
class ListGraphBase {
|
| 774 | 771 |
|
| 775 | 772 |
protected: |
| 776 | 773 |
|
| 777 | 774 |
struct NodeT {
|
| 778 | 775 |
int first_out; |
| 779 | 776 |
int prev, next; |
| 780 | 777 |
}; |
| 781 | 778 |
|
| 782 | 779 |
struct ArcT {
|
| 783 | 780 |
int target; |
| 784 | 781 |
int prev_out, next_out; |
| 785 | 782 |
}; |
| 786 | 783 |
|
| 787 | 784 |
std::vector<NodeT> nodes; |
| 788 | 785 |
|
| 789 | 786 |
int first_node; |
| 790 | 787 |
|
| 791 | 788 |
int first_free_node; |
| 792 | 789 |
|
| 793 | 790 |
std::vector<ArcT> arcs; |
| 794 | 791 |
|
| 795 | 792 |
int first_free_arc; |
| 796 | 793 |
|
| 797 | 794 |
public: |
| 798 | 795 |
|
| 799 | 796 |
typedef ListGraphBase Digraph; |
| 800 | 797 |
|
| 801 | 798 |
class Node; |
| 802 | 799 |
class Arc; |
| 803 | 800 |
class Edge; |
| 804 | 801 |
|
| 805 | 802 |
class Node {
|
| 806 | 803 |
friend class ListGraphBase; |
| 807 | 804 |
protected: |
| 808 | 805 |
|
| 809 | 806 |
int id; |
| 810 | 807 |
explicit Node(int pid) { id = pid;}
|
| 811 | 808 |
|
| 812 | 809 |
public: |
| 813 | 810 |
Node() {}
|
| 814 | 811 |
Node (Invalid) { id = -1; }
|
| 815 | 812 |
bool operator==(const Node& node) const {return id == node.id;}
|
| 816 | 813 |
bool operator!=(const Node& node) const {return id != node.id;}
|
| 817 | 814 |
bool operator<(const Node& node) const {return id < node.id;}
|
| 818 | 815 |
}; |
| 819 | 816 |
|
| 820 | 817 |
class Edge {
|
| 821 | 818 |
friend class ListGraphBase; |
| 822 | 819 |
protected: |
| 823 | 820 |
|
| 824 | 821 |
int id; |
| 825 | 822 |
explicit Edge(int pid) { id = pid;}
|
| 826 | 823 |
|
| 827 | 824 |
public: |
| 828 | 825 |
Edge() {}
|
| 829 | 826 |
Edge (Invalid) { id = -1; }
|
| 830 | 827 |
bool operator==(const Edge& edge) const {return id == edge.id;}
|
| 831 | 828 |
bool operator!=(const Edge& edge) const {return id != edge.id;}
|
| 832 | 829 |
bool operator<(const Edge& edge) const {return id < edge.id;}
|
| 833 | 830 |
}; |
| 834 | 831 |
|
| 835 | 832 |
class Arc {
|
| 836 | 833 |
friend class ListGraphBase; |
| 837 | 834 |
protected: |
| 838 | 835 |
|
| 839 | 836 |
int id; |
| 840 | 837 |
explicit Arc(int pid) { id = pid;}
|
| 841 | 838 |
|
| 842 | 839 |
public: |
| 843 | 840 |
operator Edge() const {
|
| 844 | 841 |
return id != -1 ? edgeFromId(id / 2) : INVALID; |
| 845 | 842 |
} |
| 846 | 843 |
|
| 847 | 844 |
Arc() {}
|
| 848 | 845 |
Arc (Invalid) { id = -1; }
|
| 849 | 846 |
bool operator==(const Arc& arc) const {return id == arc.id;}
|
| 850 | 847 |
bool operator!=(const Arc& arc) const {return id != arc.id;}
|
| 851 | 848 |
bool operator<(const Arc& arc) const {return id < arc.id;}
|
| 852 | 849 |
}; |
| 853 | 850 |
|
| 854 | 851 |
|
| 855 | 852 |
|
| 856 | 853 |
ListGraphBase() |
| 857 | 854 |
: nodes(), first_node(-1), |
| 858 | 855 |
first_free_node(-1), arcs(), first_free_arc(-1) {}
|
| 859 | 856 |
|
| 860 | 857 |
|
| 861 | 858 |
int maxNodeId() const { return nodes.size()-1; }
|
| 862 | 859 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 863 | 860 |
int maxArcId() const { return arcs.size()-1; }
|
| 864 | 861 |
|
| 865 | 862 |
Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
|
| 866 | 863 |
Node target(Arc e) const { return Node(arcs[e.id].target); }
|
| 867 | 864 |
|
| 868 | 865 |
Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
|
| 869 | 866 |
Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
|
| 870 | 867 |
|
| 871 | 868 |
static bool direction(Arc e) {
|
| 872 | 869 |
return (e.id & 1) == 1; |
| 873 | 870 |
} |
| 874 | 871 |
|
| 875 | 872 |
static Arc direct(Edge e, bool d) {
|
| 876 | 873 |
return Arc(e.id * 2 + (d ? 1 : 0)); |
| 877 | 874 |
} |
| 878 | 875 |
|
| 879 | 876 |
void first(Node& node) const {
|
| 880 | 877 |
node.id = first_node; |
| 881 | 878 |
} |
| 882 | 879 |
|
| 883 | 880 |
void next(Node& node) const {
|
| 884 | 881 |
node.id = nodes[node.id].next; |
| 885 | 882 |
} |
| 886 | 883 |
|
| 887 | 884 |
void first(Arc& e) const {
|
| 888 | 885 |
int n = first_node; |
| 889 | 886 |
while (n != -1 && nodes[n].first_out == -1) {
|
| 890 | 887 |
n = nodes[n].next; |
| 891 | 888 |
} |
| 892 | 889 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
| 893 | 890 |
} |
| 894 | 891 |
|
| 895 | 892 |
void next(Arc& e) const {
|
| 896 | 893 |
if (arcs[e.id].next_out != -1) {
|
| 897 | 894 |
e.id = arcs[e.id].next_out; |
| 898 | 895 |
} else {
|
| 899 | 896 |
int n = nodes[arcs[e.id ^ 1].target].next; |
| 900 | 897 |
while(n != -1 && nodes[n].first_out == -1) {
|
| 901 | 898 |
n = nodes[n].next; |
| 902 | 899 |
} |
| 903 | 900 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
| 904 | 901 |
} |
| 905 | 902 |
} |
| 906 | 903 |
|
| 907 | 904 |
void first(Edge& e) const {
|
| 908 | 905 |
int n = first_node; |
| 909 | 906 |
while (n != -1) {
|
| 910 | 907 |
e.id = nodes[n].first_out; |
| 911 | 908 |
while ((e.id & 1) != 1) {
|
| 912 | 909 |
e.id = arcs[e.id].next_out; |
| 913 | 910 |
} |
| 914 | 911 |
if (e.id != -1) {
|
| 915 | 912 |
e.id /= 2; |
| 916 | 913 |
return; |
| 917 | 914 |
} |
| 918 | 915 |
n = nodes[n].next; |
| 919 | 916 |
} |
| 920 | 917 |
e.id = -1; |
| 921 | 918 |
} |
| 922 | 919 |
|
| 923 | 920 |
void next(Edge& e) const {
|
| 924 | 921 |
int n = arcs[e.id * 2].target; |
| 925 | 922 |
e.id = arcs[(e.id * 2) | 1].next_out; |
| 926 | 923 |
while ((e.id & 1) != 1) {
|
| 927 | 924 |
e.id = arcs[e.id].next_out; |
| 928 | 925 |
} |
| 929 | 926 |
if (e.id != -1) {
|
| 930 | 927 |
e.id /= 2; |
| 931 | 928 |
return; |
| 932 | 929 |
} |
| 933 | 930 |
n = nodes[n].next; |
| 934 | 931 |
while (n != -1) {
|
| 935 | 932 |
e.id = nodes[n].first_out; |
| 936 | 933 |
while ((e.id & 1) != 1) {
|
| 937 | 934 |
e.id = arcs[e.id].next_out; |
| 938 | 935 |
} |
| 939 | 936 |
if (e.id != -1) {
|
| 940 | 937 |
e.id /= 2; |
| 941 | 938 |
return; |
| 942 | 939 |
} |
| 943 | 940 |
n = nodes[n].next; |
| 944 | 941 |
} |
| 945 | 942 |
e.id = -1; |
| 946 | 943 |
} |
| 947 | 944 |
|
| 948 | 945 |
void firstOut(Arc &e, const Node& v) const {
|
| 949 | 946 |
e.id = nodes[v.id].first_out; |
| 950 | 947 |
} |
| 951 | 948 |
void nextOut(Arc &e) const {
|
| 952 | 949 |
e.id = arcs[e.id].next_out; |
| 953 | 950 |
} |
| 954 | 951 |
|
| 955 | 952 |
void firstIn(Arc &e, const Node& v) const {
|
| 956 | 953 |
e.id = ((nodes[v.id].first_out) ^ 1); |
| 957 | 954 |
if (e.id == -2) e.id = -1; |
| 958 | 955 |
} |
| 959 | 956 |
void nextIn(Arc &e) const {
|
| 960 | 957 |
e.id = ((arcs[e.id ^ 1].next_out) ^ 1); |
| 961 | 958 |
if (e.id == -2) e.id = -1; |
| 962 | 959 |
} |
| 963 | 960 |
|
| 964 | 961 |
void firstInc(Edge &e, bool& d, const Node& v) const {
|
| 965 | 962 |
int a = nodes[v.id].first_out; |
| 966 | 963 |
if (a != -1 ) {
|
| 967 | 964 |
e.id = a / 2; |
| 968 | 965 |
d = ((a & 1) == 1); |
| 969 | 966 |
} else {
|
| 970 | 967 |
e.id = -1; |
| 971 | 968 |
d = true; |
| 972 | 969 |
} |
| 973 | 970 |
} |
| 974 | 971 |
void nextInc(Edge &e, bool& d) const {
|
| 975 | 972 |
int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out); |
| 976 | 973 |
if (a != -1 ) {
|
| 977 | 974 |
e.id = a / 2; |
| 978 | 975 |
d = ((a & 1) == 1); |
| 979 | 976 |
} else {
|
| 980 | 977 |
e.id = -1; |
| 981 | 978 |
d = true; |
| 982 | 979 |
} |
| 983 | 980 |
} |
| 984 | 981 |
|
| 985 | 982 |
static int id(Node v) { return v.id; }
|
| 986 | 983 |
static int id(Arc e) { return e.id; }
|
| 987 | 984 |
static int id(Edge e) { return e.id; }
|
| 988 | 985 |
|
| 989 | 986 |
static Node nodeFromId(int id) { return Node(id);}
|
| 990 | 987 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 991 | 988 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 992 | 989 |
|
| 993 | 990 |
bool valid(Node n) const {
|
| 994 | 991 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
| 995 | 992 |
nodes[n.id].prev != -2; |
| 996 | 993 |
} |
| 997 | 994 |
|
| 998 | 995 |
bool valid(Arc a) const {
|
| 999 | 996 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
| 1000 | 997 |
arcs[a.id].prev_out != -2; |
| 1001 | 998 |
} |
| 1002 | 999 |
|
| 1003 | 1000 |
bool valid(Edge e) const {
|
| 1004 | 1001 |
return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) && |
| 1005 | 1002 |
arcs[2 * e.id].prev_out != -2; |
| 1006 | 1003 |
} |
| 1007 | 1004 |
|
| 1008 | 1005 |
Node addNode() {
|
| 1009 | 1006 |
int n; |
| 1010 | 1007 |
|
| 1011 | 1008 |
if(first_free_node==-1) {
|
| 1012 | 1009 |
n = nodes.size(); |
| 1013 | 1010 |
nodes.push_back(NodeT()); |
| 1014 | 1011 |
} else {
|
| 1015 | 1012 |
n = first_free_node; |
| 1016 | 1013 |
first_free_node = nodes[n].next; |
| 1017 | 1014 |
} |
| 1018 | 1015 |
|
| 1019 | 1016 |
nodes[n].next = first_node; |
| 1020 | 1017 |
if (first_node != -1) nodes[first_node].prev = n; |
| 1021 | 1018 |
first_node = n; |
| 1022 | 1019 |
nodes[n].prev = -1; |
| 1023 | 1020 |
|
| 1024 | 1021 |
nodes[n].first_out = -1; |
| 1025 | 1022 |
|
| 1026 | 1023 |
return Node(n); |
| 1027 | 1024 |
} |
| 1028 | 1025 |
|
| 1029 | 1026 |
Edge addEdge(Node u, Node v) {
|
| 1030 | 1027 |
int n; |
| 1031 | 1028 |
|
| 1032 | 1029 |
if (first_free_arc == -1) {
|
| 1033 | 1030 |
n = arcs.size(); |
| 1034 | 1031 |
arcs.push_back(ArcT()); |
| 1035 | 1032 |
arcs.push_back(ArcT()); |
| 1036 | 1033 |
} else {
|
| 1037 | 1034 |
n = first_free_arc; |
| 1038 | 1035 |
first_free_arc = arcs[n].next_out; |
| 1039 | 1036 |
} |
| 1040 | 1037 |
|
| 1041 | 1038 |
arcs[n].target = u.id; |
| 1042 | 1039 |
arcs[n | 1].target = v.id; |
| 1043 | 1040 |
|
| 1044 | 1041 |
arcs[n].next_out = nodes[v.id].first_out; |
| 1045 | 1042 |
if (nodes[v.id].first_out != -1) {
|
| 1046 | 1043 |
arcs[nodes[v.id].first_out].prev_out = n; |
| 1047 | 1044 |
} |
| 1048 | 1045 |
arcs[n].prev_out = -1; |
| 1049 | 1046 |
nodes[v.id].first_out = n; |
| 1050 | 1047 |
|
| 1051 | 1048 |
arcs[n | 1].next_out = nodes[u.id].first_out; |
| 1052 | 1049 |
if (nodes[u.id].first_out != -1) {
|
| 1053 | 1050 |
arcs[nodes[u.id].first_out].prev_out = (n | 1); |
| 1054 | 1051 |
} |
| 1055 | 1052 |
arcs[n | 1].prev_out = -1; |
| 1056 | 1053 |
nodes[u.id].first_out = (n | 1); |
| 1057 | 1054 |
|
| 1058 | 1055 |
return Edge(n / 2); |
| 1059 | 1056 |
} |
| 1060 | 1057 |
|
| 1061 | 1058 |
void erase(const Node& node) {
|
| 1062 | 1059 |
int n = node.id; |
| 1063 | 1060 |
|
| 1064 | 1061 |
if(nodes[n].next != -1) {
|
| 1065 | 1062 |
nodes[nodes[n].next].prev = nodes[n].prev; |
| 1066 | 1063 |
} |
| 1067 | 1064 |
|
| 1068 | 1065 |
if(nodes[n].prev != -1) {
|
| 1069 | 1066 |
nodes[nodes[n].prev].next = nodes[n].next; |
| 1070 | 1067 |
} else {
|
| 1071 | 1068 |
first_node = nodes[n].next; |
| 1072 | 1069 |
} |
| 1073 | 1070 |
|
| 1074 | 1071 |
nodes[n].next = first_free_node; |
| 1075 | 1072 |
first_free_node = n; |
| 1076 | 1073 |
nodes[n].prev = -2; |
| 1077 | 1074 |
} |
| 1078 | 1075 |
|
| 1079 | 1076 |
void erase(const Edge& edge) {
|
| 1080 | 1077 |
int n = edge.id * 2; |
| 1081 | 1078 |
|
| 1082 | 1079 |
if (arcs[n].next_out != -1) {
|
| 1083 | 1080 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
| 1084 | 1081 |
} |
| 1085 | 1082 |
|
| 1086 | 1083 |
if (arcs[n].prev_out != -1) {
|
| 1087 | 1084 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 1088 | 1085 |
} else {
|
| 1089 | 1086 |
nodes[arcs[n | 1].target].first_out = arcs[n].next_out; |
| 1090 | 1087 |
} |
| 1091 | 1088 |
|
| 1092 | 1089 |
if (arcs[n | 1].next_out != -1) {
|
| 1093 | 1090 |
arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
| 1094 | 1091 |
} |
| 1095 | 1092 |
|
| 1096 | 1093 |
if (arcs[n | 1].prev_out != -1) {
|
| 1097 | 1094 |
arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
| 1098 | 1095 |
} else {
|
| 1099 | 1096 |
nodes[arcs[n].target].first_out = arcs[n | 1].next_out; |
| 1100 | 1097 |
} |
| 1101 | 1098 |
|
| 1102 | 1099 |
arcs[n].next_out = first_free_arc; |
| 1103 | 1100 |
first_free_arc = n; |
| 1104 | 1101 |
arcs[n].prev_out = -2; |
| 1105 | 1102 |
arcs[n | 1].prev_out = -2; |
| 1106 | 1103 |
|
| 1107 | 1104 |
} |
| 1108 | 1105 |
|
| 1109 | 1106 |
void clear() {
|
| 1110 | 1107 |
arcs.clear(); |
| 1111 | 1108 |
nodes.clear(); |
| 1112 | 1109 |
first_node = first_free_node = first_free_arc = -1; |
| 1113 | 1110 |
} |
| 1114 | 1111 |
|
| 1115 | 1112 |
protected: |
| 1116 | 1113 |
|
| 1117 | 1114 |
void changeV(Edge e, Node n) {
|
| 1118 | 1115 |
if(arcs[2 * e.id].next_out != -1) {
|
| 1119 | 1116 |
arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out; |
| 1120 | 1117 |
} |
| 1121 | 1118 |
if(arcs[2 * e.id].prev_out != -1) {
|
| 1122 | 1119 |
arcs[arcs[2 * e.id].prev_out].next_out = |
| 1123 | 1120 |
arcs[2 * e.id].next_out; |
| 1124 | 1121 |
} else {
|
| 1125 | 1122 |
nodes[arcs[(2 * e.id) | 1].target].first_out = |
| 1126 | 1123 |
arcs[2 * e.id].next_out; |
| 1127 | 1124 |
} |
| 1128 | 1125 |
|
| 1129 | 1126 |
if (nodes[n.id].first_out != -1) {
|
| 1130 | 1127 |
arcs[nodes[n.id].first_out].prev_out = 2 * e.id; |
| 1131 | 1128 |
} |
| 1132 | 1129 |
arcs[(2 * e.id) | 1].target = n.id; |
| 1133 | 1130 |
arcs[2 * e.id].prev_out = -1; |
| 1134 | 1131 |
arcs[2 * e.id].next_out = nodes[n.id].first_out; |
| 1135 | 1132 |
nodes[n.id].first_out = 2 * e.id; |
| 1136 | 1133 |
} |
| 1137 | 1134 |
|
| 1138 | 1135 |
void changeU(Edge e, Node n) {
|
| 1139 | 1136 |
if(arcs[(2 * e.id) | 1].next_out != -1) {
|
| 1140 | 1137 |
arcs[arcs[(2 * e.id) | 1].next_out].prev_out = |
| 1141 | 1138 |
arcs[(2 * e.id) | 1].prev_out; |
| 1142 | 1139 |
} |
| 1143 | 1140 |
if(arcs[(2 * e.id) | 1].prev_out != -1) {
|
| 1144 | 1141 |
arcs[arcs[(2 * e.id) | 1].prev_out].next_out = |
| 1145 | 1142 |
arcs[(2 * e.id) | 1].next_out; |
| 1146 | 1143 |
} else {
|
| 1147 | 1144 |
nodes[arcs[2 * e.id].target].first_out = |
| 1148 | 1145 |
arcs[(2 * e.id) | 1].next_out; |
| 1149 | 1146 |
} |
| 1150 | 1147 |
|
| 1151 | 1148 |
if (nodes[n.id].first_out != -1) {
|
| 1152 | 1149 |
arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
| 1153 | 1150 |
} |
| 1154 | 1151 |
arcs[2 * e.id].target = n.id; |
| 1155 | 1152 |
arcs[(2 * e.id) | 1].prev_out = -1; |
| 1156 | 1153 |
arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out; |
| 1157 | 1154 |
nodes[n.id].first_out = ((2 * e.id) | 1); |
| 1158 | 1155 |
} |
| 1159 | 1156 |
|
| 1160 | 1157 |
}; |
| 1161 | 1158 |
|
| 1162 | 1159 |
typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
| 1163 | 1160 |
|
| 1164 | 1161 |
|
| 1165 | 1162 |
/// \addtogroup graphs |
| 1166 | 1163 |
/// @{
|
| 1167 | 1164 |
|
| 1168 | 1165 |
///A general undirected graph structure. |
| 1169 | 1166 |
|
| 1170 | 1167 |
///\ref ListGraph is a simple and fast <em>undirected graph</em> |
| 1171 | 1168 |
///implementation based on static linked lists that are stored in |
| 1172 | 1169 |
///\c std::vector structures. |
| 1173 | 1170 |
/// |
| 1174 | 1171 |
///It conforms to the \ref concepts::Graph "Graph concept" and it |
| 1175 | 1172 |
///also provides several useful additional functionalities. |
| 1176 | 1173 |
///Most of the member functions and nested classes are documented |
| 1177 | 1174 |
///only in the concept class. |
| 1178 | 1175 |
/// |
| 1179 |
///An important extra feature of this graph implementation is that |
|
| 1180 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
|
| 1181 |
/// |
|
| 1182 | 1176 |
///\sa concepts::Graph |
| 1183 | 1177 |
|
| 1184 | 1178 |
class ListGraph : public ExtendedListGraphBase {
|
| 1185 | 1179 |
private: |
| 1186 | 1180 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
| 1187 | 1181 |
|
| 1188 | 1182 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
| 1189 | 1183 |
/// |
| 1190 | 1184 |
ListGraph(const ListGraph &) :ExtendedListGraphBase() {};
|
| 1191 | 1185 |
///\brief Assignment of ListGraph to another one is \e not allowed. |
| 1192 | 1186 |
///Use copyGraph() instead. |
| 1193 | 1187 |
|
| 1194 | 1188 |
///Assignment of ListGraph to another one is \e not allowed. |
| 1195 | 1189 |
///Use copyGraph() instead. |
| 1196 | 1190 |
void operator=(const ListGraph &) {}
|
| 1197 | 1191 |
public: |
| 1198 | 1192 |
/// Constructor |
| 1199 | 1193 |
|
| 1200 | 1194 |
/// Constructor. |
| 1201 | 1195 |
/// |
| 1202 | 1196 |
ListGraph() {}
|
| 1203 | 1197 |
|
| 1204 | 1198 |
typedef ExtendedListGraphBase Parent; |
| 1205 | 1199 |
|
| 1206 | 1200 |
typedef Parent::OutArcIt IncEdgeIt; |
| 1207 | 1201 |
|
| 1208 | 1202 |
/// \brief Add a new node to the graph. |
| 1209 | 1203 |
/// |
| 1210 | 1204 |
/// Add a new node to the graph. |
| 1211 | 1205 |
/// \return The new node. |
| 1212 | 1206 |
Node addNode() { return Parent::addNode(); }
|
| 1213 | 1207 |
|
| 1214 | 1208 |
/// \brief Add a new edge to the graph. |
| 1215 | 1209 |
/// |
| 1216 | 1210 |
/// Add a new edge to the graph with source node \c s |
| 1217 | 1211 |
/// and target node \c t. |
| 1218 | 1212 |
/// \return The new edge. |
| 1219 | 1213 |
Edge addEdge(const Node& s, const Node& t) {
|
| 1220 | 1214 |
return Parent::addEdge(s, t); |
| 1221 | 1215 |
} |
| 1222 | 1216 |
|
| 1223 | 1217 |
/// \brief Erase a node from the graph. |
| 1224 | 1218 |
/// |
| 1225 | 1219 |
/// Erase a node from the graph. |
| 1226 | 1220 |
/// |
| 1227 | 1221 |
void erase(const Node& n) { Parent::erase(n); }
|
| 1228 | 1222 |
|
| 1229 | 1223 |
/// \brief Erase an edge from the graph. |
| 1230 | 1224 |
/// |
| 1231 | 1225 |
/// Erase an edge from the graph. |
| 1232 | 1226 |
/// |
| 1233 | 1227 |
void erase(const Edge& e) { Parent::erase(e); }
|
| 1234 | 1228 |
/// Node validity check |
| 1235 | 1229 |
|
| 1236 | 1230 |
/// This function gives back true if the given node is valid, |
| 1237 | 1231 |
/// ie. it is a real node of the graph. |
| 1238 | 1232 |
/// |
| 1239 | 1233 |
/// \warning A Node pointing to a removed item |
| 1240 | 1234 |
/// could become valid again later if new nodes are |
| 1241 | 1235 |
/// added to the graph. |
| 1242 | 1236 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 1243 | 1237 |
/// Arc validity check |
| 1244 | 1238 |
|
| 1245 | 1239 |
/// This function gives back true if the given arc is valid, |
| 1246 | 1240 |
/// ie. it is a real arc of the graph. |
| 1247 | 1241 |
/// |
| 1248 | 1242 |
/// \warning An Arc pointing to a removed item |
| 1249 | 1243 |
/// could become valid again later if new edges are |
| 1250 | 1244 |
/// added to the graph. |
| 1251 | 1245 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 1252 | 1246 |
/// Edge validity check |
| 1253 | 1247 |
|
| 1254 | 1248 |
/// This function gives back true if the given edge is valid, |
| 1255 | 1249 |
/// ie. it is a real arc of the graph. |
| 1256 | 1250 |
/// |
| 1257 | 1251 |
/// \warning A Edge pointing to a removed item |
| 1258 | 1252 |
/// could become valid again later if new edges are |
| 1259 | 1253 |
/// added to the graph. |
| 1260 | 1254 |
bool valid(Edge e) const { return Parent::valid(e); }
|
| 1261 | 1255 |
/// \brief Change the end \c u of \c e to \c n |
| 1262 | 1256 |
/// |
| 1263 | 1257 |
/// This function changes the end \c u of \c e to node \c n. |
| 1264 | 1258 |
/// |
| 1265 | 1259 |
///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the |
| 1266 | 1260 |
///changed edge are invalidated and if the changed node is the |
| 1267 | 1261 |
///base node of an iterator then this iterator is also |
| 1268 | 1262 |
///invalidated. |
| 1269 | 1263 |
/// |
| 1270 | 1264 |
///\warning This functionality cannot be used together with the |
| 1271 | 1265 |
///Snapshot feature. |
| 1272 | 1266 |
void changeU(Edge e, Node n) {
|
| 1273 | 1267 |
Parent::changeU(e,n); |
| 1274 | 1268 |
} |
| 1275 | 1269 |
/// \brief Change the end \c v of \c e to \c n |
| 1276 | 1270 |
/// |
| 1277 | 1271 |
/// This function changes the end \c v of \c e to \c n. |
| 1278 | 1272 |
/// |
| 1279 | 1273 |
///\note The <tt>EdgeIt</tt>s referencing the changed edge remain |
| 1280 | 1274 |
///valid, however <tt>ArcIt</tt>s and if the changed node is the |
| 1281 | 1275 |
///base node of an iterator then this iterator is invalidated. |
| 1282 | 1276 |
/// |
| 1283 | 1277 |
///\warning This functionality cannot be used together with the |
| 1284 | 1278 |
///Snapshot feature. |
| 1285 | 1279 |
void changeV(Edge e, Node n) {
|
| 1286 | 1280 |
Parent::changeV(e,n); |
| 1287 | 1281 |
} |
| 1288 | 1282 |
/// \brief Contract two nodes. |
| 1289 | 1283 |
/// |
| 1290 | 1284 |
/// This function contracts two nodes. |
| 1291 | 1285 |
/// Node \p b will be removed but instead of deleting |
| 1292 | 1286 |
/// its neighboring arcs, they will be joined to \p a. |
| 1293 | 1287 |
/// The last parameter \p r controls whether to remove loops. \c true |
| 1294 | 1288 |
/// means that loops will be removed. |
| 1295 | 1289 |
/// |
| 1296 | 1290 |
/// \note The <tt>ArcIt</tt>s referencing a moved arc remain |
| 1297 | 1291 |
/// valid. |
| 1298 | 1292 |
/// |
| 1299 | 1293 |
///\warning This functionality cannot be used together with the |
| 1300 | 1294 |
///Snapshot feature. |
| 1301 | 1295 |
void contract(Node a, Node b, bool r = true) {
|
| 1302 | 1296 |
for(IncEdgeIt e(*this, b); e!=INVALID;) {
|
| 1303 | 1297 |
IncEdgeIt f = e; ++f; |
| 1304 | 1298 |
if (r && runningNode(e) == a) {
|
| 1305 | 1299 |
erase(e); |
| 1306 | 1300 |
} else if (u(e) == b) {
|
| 1307 | 1301 |
changeU(e, a); |
| 1308 | 1302 |
} else {
|
| 1309 | 1303 |
changeV(e, a); |
| 1310 | 1304 |
} |
| 1311 | 1305 |
e = f; |
| 1312 | 1306 |
} |
| 1313 | 1307 |
erase(b); |
| 1314 | 1308 |
} |
| 1315 | 1309 |
|
| 1316 | 1310 |
|
| 1317 | 1311 |
/// \brief Class to make a snapshot of the graph and restore |
| 1318 | 1312 |
/// it later. |
| 1319 | 1313 |
/// |
| 1320 | 1314 |
/// Class to make a snapshot of the graph and restore it later. |
| 1321 | 1315 |
/// |
| 1322 | 1316 |
/// The newly added nodes and edges can be removed |
| 1323 | 1317 |
/// using the restore() function. |
| 1324 | 1318 |
/// |
| 1325 | 1319 |
/// \warning Edge and node deletions and other modifications |
| 1326 | 1320 |
/// (e.g. changing nodes of edges, contracting nodes) cannot be |
| 1327 | 1321 |
/// restored. These events invalidate the snapshot. |
| 1328 | 1322 |
class Snapshot {
|
| 1329 | 1323 |
protected: |
| 1330 | 1324 |
|
| 1331 | 1325 |
typedef Parent::NodeNotifier NodeNotifier; |
| 1332 | 1326 |
|
| 1333 | 1327 |
class NodeObserverProxy : public NodeNotifier::ObserverBase {
|
| 1334 | 1328 |
public: |
| 1335 | 1329 |
|
| 1336 | 1330 |
NodeObserverProxy(Snapshot& _snapshot) |
| 1337 | 1331 |
: snapshot(_snapshot) {}
|
| 1338 | 1332 |
|
| 1339 | 1333 |
using NodeNotifier::ObserverBase::attach; |
| 1340 | 1334 |
using NodeNotifier::ObserverBase::detach; |
| 1341 | 1335 |
using NodeNotifier::ObserverBase::attached; |
| 1342 | 1336 |
|
| 1343 | 1337 |
protected: |
| 1344 | 1338 |
|
| 1345 | 1339 |
virtual void add(const Node& node) {
|
| 1346 | 1340 |
snapshot.addNode(node); |
| 1347 | 1341 |
} |
| 1348 | 1342 |
virtual void add(const std::vector<Node>& nodes) {
|
| 1349 | 1343 |
for (int i = nodes.size() - 1; i >= 0; ++i) {
|
| 1350 | 1344 |
snapshot.addNode(nodes[i]); |
| 1351 | 1345 |
} |
| 1352 | 1346 |
} |
| 1353 | 1347 |
virtual void erase(const Node& node) {
|
| 1354 | 1348 |
snapshot.eraseNode(node); |
| 1355 | 1349 |
} |
| 1356 | 1350 |
virtual void erase(const std::vector<Node>& nodes) {
|
| 1357 | 1351 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 1358 | 1352 |
snapshot.eraseNode(nodes[i]); |
| 1359 | 1353 |
} |
| 1360 | 1354 |
} |
| 1361 | 1355 |
virtual void build() {
|
| 1362 | 1356 |
Node node; |
| 1363 | 1357 |
std::vector<Node> nodes; |
| 1364 | 1358 |
for (notifier()->first(node); node != INVALID; |
| 1365 | 1359 |
notifier()->next(node)) {
|
| 1366 | 1360 |
nodes.push_back(node); |
| 1367 | 1361 |
} |
| 1368 | 1362 |
for (int i = nodes.size() - 1; i >= 0; --i) {
|
| 1369 | 1363 |
snapshot.addNode(nodes[i]); |
| 1370 | 1364 |
} |
| 1371 | 1365 |
} |
| 1372 | 1366 |
virtual void clear() {
|
| 1373 | 1367 |
Node node; |
| 1374 | 1368 |
for (notifier()->first(node); node != INVALID; |
| 1375 | 1369 |
notifier()->next(node)) {
|
| 1376 | 1370 |
snapshot.eraseNode(node); |
| 1377 | 1371 |
} |
| 1378 | 1372 |
} |
| 1379 | 1373 |
|
| 1380 | 1374 |
Snapshot& snapshot; |
| 1381 | 1375 |
}; |
| 1382 | 1376 |
|
| 1383 | 1377 |
class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
|
| 1384 | 1378 |
public: |
| 1385 | 1379 |
|
| 1386 | 1380 |
EdgeObserverProxy(Snapshot& _snapshot) |
| 1387 | 1381 |
: snapshot(_snapshot) {}
|
| 1388 | 1382 |
|
| 1389 | 1383 |
using EdgeNotifier::ObserverBase::attach; |
| 1390 | 1384 |
using EdgeNotifier::ObserverBase::detach; |
| 1391 | 1385 |
using EdgeNotifier::ObserverBase::attached; |
| 1392 | 1386 |
|
| 1393 | 1387 |
protected: |
| 1394 | 1388 |
|
| 1395 | 1389 |
virtual void add(const Edge& edge) {
|
| 1396 | 1390 |
snapshot.addEdge(edge); |
| 1397 | 1391 |
} |
| 1398 | 1392 |
virtual void add(const std::vector<Edge>& edges) {
|
| 1399 | 1393 |
for (int i = edges.size() - 1; i >= 0; ++i) {
|
| 1400 | 1394 |
snapshot.addEdge(edges[i]); |
| 1401 | 1395 |
} |
| 1402 | 1396 |
} |
| 1403 | 1397 |
virtual void erase(const Edge& edge) {
|
| 1404 | 1398 |
snapshot.eraseEdge(edge); |
| 1405 | 1399 |
} |
| 1406 | 1400 |
virtual void erase(const std::vector<Edge>& edges) {
|
| 1407 | 1401 |
for (int i = 0; i < int(edges.size()); ++i) {
|
| 1408 | 1402 |
snapshot.eraseEdge(edges[i]); |
| 1409 | 1403 |
} |
| 1410 | 1404 |
} |
| 1411 | 1405 |
virtual void build() {
|
| 1412 | 1406 |
Edge edge; |
| 1413 | 1407 |
std::vector<Edge> edges; |
| 1414 | 1408 |
for (notifier()->first(edge); edge != INVALID; |
| 1415 | 1409 |
notifier()->next(edge)) {
|
| 1416 | 1410 |
edges.push_back(edge); |
| 1417 | 1411 |
} |
| 1418 | 1412 |
for (int i = edges.size() - 1; i >= 0; --i) {
|
| 1419 | 1413 |
snapshot.addEdge(edges[i]); |
| 1420 | 1414 |
} |
| 1421 | 1415 |
} |
| 1422 | 1416 |
virtual void clear() {
|
| 1423 | 1417 |
Edge edge; |
| 1424 | 1418 |
for (notifier()->first(edge); edge != INVALID; |
| 1425 | 1419 |
notifier()->next(edge)) {
|
| 1426 | 1420 |
snapshot.eraseEdge(edge); |
| 1427 | 1421 |
} |
| 1428 | 1422 |
} |
| 1429 | 1423 |
|
| 1430 | 1424 |
Snapshot& snapshot; |
| 1431 | 1425 |
}; |
| 1432 | 1426 |
|
| 1433 | 1427 |
ListGraph *graph; |
| 1434 | 1428 |
|
| 1435 | 1429 |
NodeObserverProxy node_observer_proxy; |
| 1436 | 1430 |
EdgeObserverProxy edge_observer_proxy; |
| 1437 | 1431 |
|
| 1438 | 1432 |
std::list<Node> added_nodes; |
| 1439 | 1433 |
std::list<Edge> added_edges; |
| 1440 | 1434 |
|
| 1441 | 1435 |
|
| 1442 | 1436 |
void addNode(const Node& node) {
|
| 1443 | 1437 |
added_nodes.push_front(node); |
| 1444 | 1438 |
} |
| 1445 | 1439 |
void eraseNode(const Node& node) {
|
| 1446 | 1440 |
std::list<Node>::iterator it = |
| 1447 | 1441 |
std::find(added_nodes.begin(), added_nodes.end(), node); |
| 1448 | 1442 |
if (it == added_nodes.end()) {
|
| 1449 | 1443 |
clear(); |
| 1450 | 1444 |
edge_observer_proxy.detach(); |
| 1451 | 1445 |
throw NodeNotifier::ImmediateDetach(); |
| 1452 | 1446 |
} else {
|
| 1453 | 1447 |
added_nodes.erase(it); |
| 1454 | 1448 |
} |
| 1455 | 1449 |
} |
| 1456 | 1450 |
|
| 1457 | 1451 |
void addEdge(const Edge& edge) {
|
| 1458 | 1452 |
added_edges.push_front(edge); |
| 1459 | 1453 |
} |
| 1460 | 1454 |
void eraseEdge(const Edge& edge) {
|
| 1461 | 1455 |
std::list<Edge>::iterator it = |
| 1462 | 1456 |
std::find(added_edges.begin(), added_edges.end(), edge); |
| 1463 | 1457 |
if (it == added_edges.end()) {
|
| 1464 | 1458 |
clear(); |
| 1465 | 1459 |
node_observer_proxy.detach(); |
| 1466 | 1460 |
throw EdgeNotifier::ImmediateDetach(); |
| 1467 | 1461 |
} else {
|
| 1468 | 1462 |
added_edges.erase(it); |
| 1469 | 1463 |
} |
| 1470 | 1464 |
} |
| 1471 | 1465 |
|
| 1472 | 1466 |
void attach(ListGraph &_graph) {
|
| 1473 | 1467 |
graph = &_graph; |
| 1474 | 1468 |
node_observer_proxy.attach(graph->notifier(Node())); |
| 1475 | 1469 |
edge_observer_proxy.attach(graph->notifier(Edge())); |
| 1476 | 1470 |
} |
| 1477 | 1471 |
|
| 1478 | 1472 |
void detach() {
|
| 1479 | 1473 |
node_observer_proxy.detach(); |
| 1480 | 1474 |
edge_observer_proxy.detach(); |
| 1481 | 1475 |
} |
| 1482 | 1476 |
|
| 1483 | 1477 |
bool attached() const {
|
| 1484 | 1478 |
return node_observer_proxy.attached(); |
| 1485 | 1479 |
} |
| 1486 | 1480 |
|
| 1487 | 1481 |
void clear() {
|
| 1488 | 1482 |
added_nodes.clear(); |
| 1489 | 1483 |
added_edges.clear(); |
| 1490 | 1484 |
} |
| 1491 | 1485 |
|
| 1492 | 1486 |
public: |
| 1493 | 1487 |
|
| 1494 | 1488 |
/// \brief Default constructor. |
| 1495 | 1489 |
/// |
| 1496 | 1490 |
/// Default constructor. |
| 1497 | 1491 |
/// To actually make a snapshot you must call save(). |
| 1498 | 1492 |
Snapshot() |
| 1499 | 1493 |
: graph(0), node_observer_proxy(*this), |
| 1500 | 1494 |
edge_observer_proxy(*this) {}
|
| 1501 | 1495 |
|
| 1502 | 1496 |
/// \brief Constructor that immediately makes a snapshot. |
| 1503 | 1497 |
/// |
| 1504 | 1498 |
/// This constructor immediately makes a snapshot of the graph. |
| 1505 | 1499 |
/// \param _graph The graph we make a snapshot of. |
| 1506 | 1500 |
Snapshot(ListGraph &_graph) |
| 1507 | 1501 |
: node_observer_proxy(*this), |
| 1508 | 1502 |
edge_observer_proxy(*this) {
|
| 1509 | 1503 |
attach(_graph); |
| 1510 | 1504 |
} |
| 1511 | 1505 |
|
| 1512 | 1506 |
/// \brief Make a snapshot. |
| 1513 | 1507 |
/// |
| 1514 | 1508 |
/// Make a snapshot of the graph. |
| 1515 | 1509 |
/// |
| 1516 | 1510 |
/// This function can be called more than once. In case of a repeated |
| 1517 | 1511 |
/// call, the previous snapshot gets lost. |
| 1518 | 1512 |
/// \param _graph The graph we make the snapshot of. |
| 1519 | 1513 |
void save(ListGraph &_graph) {
|
| 1520 | 1514 |
if (attached()) {
|
| 1521 | 1515 |
detach(); |
| 1522 | 1516 |
clear(); |
| 1523 | 1517 |
} |
| 1524 | 1518 |
attach(_graph); |
| 1525 | 1519 |
} |
| 1526 | 1520 |
|
| 1527 | 1521 |
/// \brief Undo the changes until the last snapshot. |
| 1528 | 1522 |
// |
| 1529 | 1523 |
/// Undo the changes until the last snapshot created by save(). |
| 1530 | 1524 |
void restore() {
|
| 1531 | 1525 |
detach(); |
| 1532 | 1526 |
for(std::list<Edge>::iterator it = added_edges.begin(); |
| 1533 | 1527 |
it != added_edges.end(); ++it) {
|
| 1534 | 1528 |
graph->erase(*it); |
| 1535 | 1529 |
} |
| 1536 | 1530 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
| 1537 | 1531 |
it != added_nodes.end(); ++it) {
|
| 1538 | 1532 |
graph->erase(*it); |
| 1539 | 1533 |
} |
| 1540 | 1534 |
clear(); |
| 1541 | 1535 |
} |
| 1542 | 1536 |
|
| 1543 | 1537 |
/// \brief Gives back true when the snapshot is valid. |
| 1544 | 1538 |
/// |
| 1545 | 1539 |
/// Gives back true when the snapshot is valid. |
| 1546 | 1540 |
bool valid() const {
|
| 1547 | 1541 |
return attached(); |
| 1548 | 1542 |
} |
| 1549 | 1543 |
}; |
| 1550 | 1544 |
}; |
| 1551 | 1545 |
|
| 1552 | 1546 |
/// @} |
| 1553 | 1547 |
} //namespace lemon |
| 1554 | 1548 |
|
| 1555 | 1549 |
|
| 1556 | 1550 |
#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_SMART_GRAPH_H |
| 20 | 20 |
#define LEMON_SMART_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief SmartDigraph and SmartGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/bits/graph_extender.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
class SmartDigraph; |
| 35 | 35 |
///Base of SmartDigraph |
| 36 | 36 |
|
| 37 | 37 |
///Base of SmartDigraph |
| 38 | 38 |
/// |
| 39 | 39 |
class SmartDigraphBase {
|
| 40 | 40 |
protected: |
| 41 | 41 |
|
| 42 | 42 |
struct NodeT |
| 43 | 43 |
{
|
| 44 | 44 |
int first_in, first_out; |
| 45 | 45 |
NodeT() {}
|
| 46 | 46 |
}; |
| 47 | 47 |
struct ArcT |
| 48 | 48 |
{
|
| 49 | 49 |
int target, source, next_in, next_out; |
| 50 | 50 |
ArcT() {}
|
| 51 | 51 |
}; |
| 52 | 52 |
|
| 53 | 53 |
std::vector<NodeT> nodes; |
| 54 | 54 |
std::vector<ArcT> arcs; |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
typedef SmartDigraphBase Graph; |
| 59 | 59 |
|
| 60 | 60 |
class Node; |
| 61 | 61 |
class Arc; |
| 62 | 62 |
|
| 63 | 63 |
public: |
| 64 | 64 |
|
| 65 | 65 |
SmartDigraphBase() : nodes(), arcs() { }
|
| 66 | 66 |
SmartDigraphBase(const SmartDigraphBase &_g) |
| 67 | 67 |
: nodes(_g.nodes), arcs(_g.arcs) { }
|
| 68 | 68 |
|
| 69 | 69 |
typedef True NodeNumTag; |
| 70 | 70 |
typedef True ArcNumTag; |
| 71 | 71 |
|
| 72 | 72 |
int nodeNum() const { return nodes.size(); }
|
| 73 | 73 |
int arcNum() const { return arcs.size(); }
|
| 74 | 74 |
|
| 75 | 75 |
int maxNodeId() const { return nodes.size()-1; }
|
| 76 | 76 |
int maxArcId() const { return arcs.size()-1; }
|
| 77 | 77 |
|
| 78 | 78 |
Node addNode() {
|
| 79 | 79 |
int n = nodes.size(); |
| 80 | 80 |
nodes.push_back(NodeT()); |
| 81 | 81 |
nodes[n].first_in = -1; |
| 82 | 82 |
nodes[n].first_out = -1; |
| 83 | 83 |
return Node(n); |
| 84 | 84 |
} |
| 85 | 85 |
|
| 86 | 86 |
Arc addArc(Node u, Node v) {
|
| 87 | 87 |
int n = arcs.size(); |
| 88 | 88 |
arcs.push_back(ArcT()); |
| 89 | 89 |
arcs[n].source = u._id; |
| 90 | 90 |
arcs[n].target = v._id; |
| 91 | 91 |
arcs[n].next_out = nodes[u._id].first_out; |
| 92 | 92 |
arcs[n].next_in = nodes[v._id].first_in; |
| 93 | 93 |
nodes[u._id].first_out = nodes[v._id].first_in = n; |
| 94 | 94 |
|
| 95 | 95 |
return Arc(n); |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
void clear() {
|
| 99 | 99 |
arcs.clear(); |
| 100 | 100 |
nodes.clear(); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
Node source(Arc a) const { return Node(arcs[a._id].source); }
|
| 104 | 104 |
Node target(Arc a) const { return Node(arcs[a._id].target); }
|
| 105 | 105 |
|
| 106 | 106 |
static int id(Node v) { return v._id; }
|
| 107 | 107 |
static int id(Arc a) { return a._id; }
|
| 108 | 108 |
|
| 109 | 109 |
static Node nodeFromId(int id) { return Node(id);}
|
| 110 | 110 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 111 | 111 |
|
| 112 | 112 |
bool valid(Node n) const {
|
| 113 | 113 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
| 114 | 114 |
} |
| 115 | 115 |
bool valid(Arc a) const {
|
| 116 | 116 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
class Node {
|
| 120 | 120 |
friend class SmartDigraphBase; |
| 121 | 121 |
friend class SmartDigraph; |
| 122 | 122 |
|
| 123 | 123 |
protected: |
| 124 | 124 |
int _id; |
| 125 | 125 |
explicit Node(int id) : _id(id) {}
|
| 126 | 126 |
public: |
| 127 | 127 |
Node() {}
|
| 128 | 128 |
Node (Invalid) : _id(-1) {}
|
| 129 | 129 |
bool operator==(const Node i) const {return _id == i._id;}
|
| 130 | 130 |
bool operator!=(const Node i) const {return _id != i._id;}
|
| 131 | 131 |
bool operator<(const Node i) const {return _id < i._id;}
|
| 132 | 132 |
}; |
| 133 | 133 |
|
| 134 | 134 |
|
| 135 | 135 |
class Arc {
|
| 136 | 136 |
friend class SmartDigraphBase; |
| 137 | 137 |
friend class SmartDigraph; |
| 138 | 138 |
|
| 139 | 139 |
protected: |
| 140 | 140 |
int _id; |
| 141 | 141 |
explicit Arc(int id) : _id(id) {}
|
| 142 | 142 |
public: |
| 143 | 143 |
Arc() { }
|
| 144 | 144 |
Arc (Invalid) : _id(-1) {}
|
| 145 | 145 |
bool operator==(const Arc i) const {return _id == i._id;}
|
| 146 | 146 |
bool operator!=(const Arc i) const {return _id != i._id;}
|
| 147 | 147 |
bool operator<(const Arc i) const {return _id < i._id;}
|
| 148 | 148 |
}; |
| 149 | 149 |
|
| 150 | 150 |
void first(Node& node) const {
|
| 151 | 151 |
node._id = nodes.size() - 1; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
static void next(Node& node) {
|
| 155 | 155 |
--node._id; |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
void first(Arc& arc) const {
|
| 159 | 159 |
arc._id = arcs.size() - 1; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
static void next(Arc& arc) {
|
| 163 | 163 |
--arc._id; |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
void firstOut(Arc& arc, const Node& node) const {
|
| 167 | 167 |
arc._id = nodes[node._id].first_out; |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
void nextOut(Arc& arc) const {
|
| 171 | 171 |
arc._id = arcs[arc._id].next_out; |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
void firstIn(Arc& arc, const Node& node) const {
|
| 175 | 175 |
arc._id = nodes[node._id].first_in; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
void nextIn(Arc& arc) const {
|
| 179 | 179 |
arc._id = arcs[arc._id].next_in; |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
}; |
| 183 | 183 |
|
| 184 | 184 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
| 185 | 185 |
|
| 186 | 186 |
///\ingroup graphs |
| 187 | 187 |
/// |
| 188 | 188 |
///\brief A smart directed graph class. |
| 189 | 189 |
/// |
| 190 | 190 |
///This is a simple and fast digraph implementation. |
| 191 | 191 |
///It is also quite memory efficient, but at the price |
| 192 | 192 |
///that <b> it does support only limited (only stack-like) |
| 193 | 193 |
///node and arc deletions</b>. |
| 194 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
|
| 195 |
///an important extra feature that its maps are real \ref |
|
| 196 |
///concepts:: |
|
| 194 |
///It fully conforms to the \ref concepts::Digraph "Digraph concept". |
|
| 197 | 195 |
/// |
| 198 | 196 |
///\sa concepts::Digraph. |
| 199 | 197 |
class SmartDigraph : public ExtendedSmartDigraphBase {
|
| 200 | 198 |
public: |
| 201 | 199 |
|
| 202 | 200 |
typedef ExtendedSmartDigraphBase Parent; |
| 203 | 201 |
|
| 204 | 202 |
private: |
| 205 | 203 |
|
| 206 | 204 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 207 | 205 |
|
| 208 | 206 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 209 | 207 |
/// |
| 210 | 208 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
|
| 211 | 209 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
| 212 | 210 |
///Use DigraphCopy() instead. |
| 213 | 211 |
|
| 214 | 212 |
///Assignment of SmartDigraph to another one is \e not allowed. |
| 215 | 213 |
///Use DigraphCopy() instead. |
| 216 | 214 |
void operator=(const SmartDigraph &) {}
|
| 217 | 215 |
|
| 218 | 216 |
public: |
| 219 | 217 |
|
| 220 | 218 |
/// Constructor |
| 221 | 219 |
|
| 222 | 220 |
/// Constructor. |
| 223 | 221 |
/// |
| 224 | 222 |
SmartDigraph() {};
|
| 225 | 223 |
|
| 226 | 224 |
///Add a new node to the digraph. |
| 227 | 225 |
|
| 228 | 226 |
/// Add a new node to the digraph. |
| 229 | 227 |
/// \return The new node. |
| 230 | 228 |
Node addNode() { return Parent::addNode(); }
|
| 231 | 229 |
|
| 232 | 230 |
///Add a new arc to the digraph. |
| 233 | 231 |
|
| 234 | 232 |
///Add a new arc to the digraph with source node \c s |
| 235 | 233 |
///and target node \c t. |
| 236 | 234 |
///\return The new arc. |
| 237 | 235 |
Arc addArc(const Node& s, const Node& t) {
|
| 238 | 236 |
return Parent::addArc(s, t); |
| 239 | 237 |
} |
| 240 | 238 |
|
| 241 | 239 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 242 | 240 |
/// allocation. |
| 243 | 241 |
|
| 244 | 242 |
/// Using this it is possible to avoid the superfluous memory |
| 245 | 243 |
/// allocation: if you know that the digraph you want to build will |
| 246 | 244 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 247 | 245 |
/// then it is worth reserving space for this amount before starting |
| 248 | 246 |
/// to build the digraph. |
| 249 | 247 |
/// \sa reserveArc |
| 250 | 248 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 251 | 249 |
|
| 252 | 250 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 253 | 251 |
/// allocation. |
| 254 | 252 |
|
| 255 | 253 |
/// Using this it is possible to avoid the superfluous memory |
| 256 | 254 |
/// allocation: if you know that the digraph you want to build will |
| 257 | 255 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 258 | 256 |
/// then it is worth reserving space for this amount before starting |
| 259 | 257 |
/// to build the digraph. |
| 260 | 258 |
/// \sa reserveNode |
| 261 | 259 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 262 | 260 |
|
| 263 | 261 |
/// \brief Node validity check |
| 264 | 262 |
/// |
| 265 | 263 |
/// This function gives back true if the given node is valid, |
| 266 | 264 |
/// ie. it is a real node of the graph. |
| 267 | 265 |
/// |
| 268 | 266 |
/// \warning A removed node (using Snapshot) could become valid again |
| 269 | 267 |
/// when new nodes are added to the graph. |
| 270 | 268 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 271 | 269 |
|
| 272 | 270 |
/// \brief Arc validity check |
| 273 | 271 |
/// |
| 274 | 272 |
/// This function gives back true if the given arc is valid, |
| 275 | 273 |
/// ie. it is a real arc of the graph. |
| 276 | 274 |
/// |
| 277 | 275 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 278 | 276 |
/// when new arcs are added to the graph. |
| 279 | 277 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 280 | 278 |
|
| 281 | 279 |
///Clear the digraph. |
| 282 | 280 |
|
| 283 | 281 |
///Erase all the nodes and arcs from the digraph. |
| 284 | 282 |
/// |
| 285 | 283 |
void clear() {
|
| 286 | 284 |
Parent::clear(); |
| 287 | 285 |
} |
| 288 | 286 |
|
| 289 | 287 |
///Split a node. |
| 290 | 288 |
|
| 291 | 289 |
///This function splits a node. First a new node is added to the digraph, |
| 292 | 290 |
///then the source of each outgoing arc of \c n is moved to this new node. |
| 293 | 291 |
///If \c connect is \c true (this is the default value), then a new arc |
| 294 | 292 |
///from \c n to the newly created node is also added. |
| 295 | 293 |
///\return The newly created node. |
| 296 | 294 |
/// |
| 297 | 295 |
///\note The <tt>Arc</tt>s |
| 298 | 296 |
///referencing a moved arc remain |
| 299 | 297 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
| 300 | 298 |
///may be invalidated. |
| 301 | 299 |
///\warning This functionality cannot be used together with the Snapshot |
| 302 | 300 |
///feature. |
| 303 | 301 |
Node split(Node n, bool connect = true) |
| 304 | 302 |
{
|
| 305 | 303 |
Node b = addNode(); |
| 306 | 304 |
nodes[b._id].first_out=nodes[n._id].first_out; |
| 307 | 305 |
nodes[n._id].first_out=-1; |
| 308 | 306 |
for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
|
| 309 | 307 |
arcs[i].source=b._id; |
| 310 | 308 |
} |
| 311 | 309 |
if(connect) addArc(n,b); |
| 312 | 310 |
return b; |
| 313 | 311 |
} |
| 314 | 312 |
|
| 315 | 313 |
public: |
| 316 | 314 |
|
| 317 | 315 |
class Snapshot; |
| 318 | 316 |
|
| 319 | 317 |
protected: |
| 320 | 318 |
|
| 321 | 319 |
void restoreSnapshot(const Snapshot &s) |
| 322 | 320 |
{
|
| 323 | 321 |
while(s.arc_num<arcs.size()) {
|
| 324 | 322 |
Arc arc = arcFromId(arcs.size()-1); |
| 325 | 323 |
Parent::notifier(Arc()).erase(arc); |
| 326 | 324 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
| 327 | 325 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
| 328 | 326 |
arcs.pop_back(); |
| 329 | 327 |
} |
| 330 | 328 |
while(s.node_num<nodes.size()) {
|
| 331 | 329 |
Node node = nodeFromId(nodes.size()-1); |
| 332 | 330 |
Parent::notifier(Node()).erase(node); |
| 333 | 331 |
nodes.pop_back(); |
| 334 | 332 |
} |
| 335 | 333 |
} |
| 336 | 334 |
|
| 337 | 335 |
public: |
| 338 | 336 |
|
| 339 | 337 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 340 | 338 |
|
| 341 | 339 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 342 | 340 |
/// |
| 343 | 341 |
///The newly added nodes and arcs can be removed using the |
| 344 | 342 |
///restore() function. |
| 345 | 343 |
///\note After you restore a state, you cannot restore |
| 346 | 344 |
///a later state, in other word you cannot add again the arcs deleted |
| 347 | 345 |
///by restore() using another one Snapshot instance. |
| 348 | 346 |
/// |
| 349 | 347 |
///\warning If you do not use correctly the snapshot that can cause |
| 350 | 348 |
///either broken program, invalid state of the digraph, valid but |
| 351 | 349 |
///not the restored digraph or no change. Because the runtime performance |
| 352 | 350 |
///the validity of the snapshot is not stored. |
| 353 | 351 |
class Snapshot |
| 354 | 352 |
{
|
| 355 | 353 |
SmartDigraph *_graph; |
| 356 | 354 |
protected: |
| 357 | 355 |
friend class SmartDigraph; |
| 358 | 356 |
unsigned int node_num; |
| 359 | 357 |
unsigned int arc_num; |
| 360 | 358 |
public: |
| 361 | 359 |
///Default constructor. |
| 362 | 360 |
|
| 363 | 361 |
///Default constructor. |
| 364 | 362 |
///To actually make a snapshot you must call save(). |
| 365 | 363 |
/// |
| 366 | 364 |
Snapshot() : _graph(0) {}
|
| 367 | 365 |
///Constructor that immediately makes a snapshot |
| 368 | 366 |
|
| 369 | 367 |
///This constructor immediately makes a snapshot of the digraph. |
| 370 | 368 |
///\param graph The digraph we make a snapshot of. |
| 371 | 369 |
Snapshot(SmartDigraph &graph) : _graph(&graph) {
|
| 372 | 370 |
node_num=_graph->nodes.size(); |
| 373 | 371 |
arc_num=_graph->arcs.size(); |
| 374 | 372 |
} |
| 375 | 373 |
|
| 376 | 374 |
///Make a snapshot. |
| 377 | 375 |
|
| 378 | 376 |
///Make a snapshot of the digraph. |
| 379 | 377 |
/// |
| 380 | 378 |
///This function can be called more than once. In case of a repeated |
| 381 | 379 |
///call, the previous snapshot gets lost. |
| 382 | 380 |
///\param graph The digraph we make the snapshot of. |
| 383 | 381 |
void save(SmartDigraph &graph) |
| 384 | 382 |
{
|
| 385 | 383 |
_graph=&graph; |
| 386 | 384 |
node_num=_graph->nodes.size(); |
| 387 | 385 |
arc_num=_graph->arcs.size(); |
| 388 | 386 |
} |
| 389 | 387 |
|
| 390 | 388 |
///Undo the changes until a snapshot. |
| 391 | 389 |
|
| 392 | 390 |
///Undo the changes until a snapshot created by save(). |
| 393 | 391 |
/// |
| 394 | 392 |
///\note After you restored a state, you cannot restore |
| 395 | 393 |
///a later state, in other word you cannot add again the arcs deleted |
| 396 | 394 |
///by restore(). |
| 397 | 395 |
void restore() |
| 398 | 396 |
{
|
| 399 | 397 |
_graph->restoreSnapshot(*this); |
| 400 | 398 |
} |
| 401 | 399 |
}; |
| 402 | 400 |
}; |
| 403 | 401 |
|
| 404 | 402 |
|
| 405 | 403 |
class SmartGraphBase {
|
| 406 | 404 |
|
| 407 | 405 |
protected: |
| 408 | 406 |
|
| 409 | 407 |
struct NodeT {
|
| 410 | 408 |
int first_out; |
| 411 | 409 |
}; |
| 412 | 410 |
|
| 413 | 411 |
struct ArcT {
|
| 414 | 412 |
int target; |
| 415 | 413 |
int next_out; |
| 416 | 414 |
}; |
| 417 | 415 |
|
| 418 | 416 |
std::vector<NodeT> nodes; |
| 419 | 417 |
std::vector<ArcT> arcs; |
| 420 | 418 |
|
| 421 | 419 |
int first_free_arc; |
| 422 | 420 |
|
| 423 | 421 |
public: |
| 424 | 422 |
|
| 425 | 423 |
typedef SmartGraphBase Digraph; |
| 426 | 424 |
|
| 427 | 425 |
class Node; |
| 428 | 426 |
class Arc; |
| 429 | 427 |
class Edge; |
| 430 | 428 |
|
| 431 | 429 |
class Node {
|
| 432 | 430 |
friend class SmartGraphBase; |
| 433 | 431 |
protected: |
| 434 | 432 |
|
| 435 | 433 |
int _id; |
| 436 | 434 |
explicit Node(int id) { _id = id;}
|
| 437 | 435 |
|
| 438 | 436 |
public: |
| 439 | 437 |
Node() {}
|
| 440 | 438 |
Node (Invalid) { _id = -1; }
|
| 441 | 439 |
bool operator==(const Node& node) const {return _id == node._id;}
|
| 442 | 440 |
bool operator!=(const Node& node) const {return _id != node._id;}
|
| 443 | 441 |
bool operator<(const Node& node) const {return _id < node._id;}
|
| 444 | 442 |
}; |
| 445 | 443 |
|
| 446 | 444 |
class Edge {
|
| 447 | 445 |
friend class SmartGraphBase; |
| 448 | 446 |
protected: |
| 449 | 447 |
|
| 450 | 448 |
int _id; |
| 451 | 449 |
explicit Edge(int id) { _id = id;}
|
| 452 | 450 |
|
| 453 | 451 |
public: |
| 454 | 452 |
Edge() {}
|
| 455 | 453 |
Edge (Invalid) { _id = -1; }
|
| 456 | 454 |
bool operator==(const Edge& arc) const {return _id == arc._id;}
|
| 457 | 455 |
bool operator!=(const Edge& arc) const {return _id != arc._id;}
|
| 458 | 456 |
bool operator<(const Edge& arc) const {return _id < arc._id;}
|
| 459 | 457 |
}; |
| 460 | 458 |
|
| 461 | 459 |
class Arc {
|
| 462 | 460 |
friend class SmartGraphBase; |
| 463 | 461 |
protected: |
| 464 | 462 |
|
| 465 | 463 |
int _id; |
| 466 | 464 |
explicit Arc(int id) { _id = id;}
|
| 467 | 465 |
|
| 468 | 466 |
public: |
| 469 | 467 |
operator Edge() const {
|
| 470 | 468 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
| 471 | 469 |
} |
| 472 | 470 |
|
| 473 | 471 |
Arc() {}
|
| 474 | 472 |
Arc (Invalid) { _id = -1; }
|
| 475 | 473 |
bool operator==(const Arc& arc) const {return _id == arc._id;}
|
| 476 | 474 |
bool operator!=(const Arc& arc) const {return _id != arc._id;}
|
| 477 | 475 |
bool operator<(const Arc& arc) const {return _id < arc._id;}
|
| 478 | 476 |
}; |
| 479 | 477 |
|
| 480 | 478 |
|
| 481 | 479 |
|
| 482 | 480 |
SmartGraphBase() |
| 483 | 481 |
: nodes(), arcs() {}
|
| 484 | 482 |
|
| 485 | 483 |
typedef True NodeNumTag; |
| 486 | 484 |
typedef True EdgeNumTag; |
| 487 | 485 |
typedef True ArcNumTag; |
| 488 | 486 |
|
| 489 | 487 |
int nodeNum() const { return nodes.size(); }
|
| 490 | 488 |
int edgeNum() const { return arcs.size() / 2; }
|
| 491 | 489 |
int arcNum() const { return arcs.size(); }
|
| 492 | 490 |
|
| 493 | 491 |
int maxNodeId() const { return nodes.size()-1; }
|
| 494 | 492 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 495 | 493 |
int maxArcId() const { return arcs.size()-1; }
|
| 496 | 494 |
|
| 497 | 495 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
|
| 498 | 496 |
Node target(Arc e) const { return Node(arcs[e._id].target); }
|
| 499 | 497 |
|
| 500 | 498 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
|
| 501 | 499 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
|
| 502 | 500 |
|
| 503 | 501 |
static bool direction(Arc e) {
|
| 504 | 502 |
return (e._id & 1) == 1; |
| 505 | 503 |
} |
| 506 | 504 |
|
| 507 | 505 |
static Arc direct(Edge e, bool d) {
|
| 508 | 506 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
| 509 | 507 |
} |
| 510 | 508 |
|
| 511 | 509 |
void first(Node& node) const {
|
| 512 | 510 |
node._id = nodes.size() - 1; |
| 513 | 511 |
} |
| 514 | 512 |
|
| 515 | 513 |
void next(Node& node) const {
|
| 516 | 514 |
--node._id; |
| 517 | 515 |
} |
| 518 | 516 |
|
| 519 | 517 |
void first(Arc& arc) const {
|
| 520 | 518 |
arc._id = arcs.size() - 1; |
| 521 | 519 |
} |
| 522 | 520 |
|
| 523 | 521 |
void next(Arc& arc) const {
|
| 524 | 522 |
--arc._id; |
| 525 | 523 |
} |
| 526 | 524 |
|
| 527 | 525 |
void first(Edge& arc) const {
|
| 528 | 526 |
arc._id = arcs.size() / 2 - 1; |
| 529 | 527 |
} |
| 530 | 528 |
|
| 531 | 529 |
void next(Edge& arc) const {
|
| 532 | 530 |
--arc._id; |
| 533 | 531 |
} |
| 534 | 532 |
|
| 535 | 533 |
void firstOut(Arc &arc, const Node& v) const {
|
| 536 | 534 |
arc._id = nodes[v._id].first_out; |
| 537 | 535 |
} |
| 538 | 536 |
void nextOut(Arc &arc) const {
|
| 539 | 537 |
arc._id = arcs[arc._id].next_out; |
| 540 | 538 |
} |
| 541 | 539 |
|
| 542 | 540 |
void firstIn(Arc &arc, const Node& v) const {
|
| 543 | 541 |
arc._id = ((nodes[v._id].first_out) ^ 1); |
| 544 | 542 |
if (arc._id == -2) arc._id = -1; |
| 545 | 543 |
} |
| 546 | 544 |
void nextIn(Arc &arc) const {
|
| 547 | 545 |
arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
| 548 | 546 |
if (arc._id == -2) arc._id = -1; |
| 549 | 547 |
} |
| 550 | 548 |
|
| 551 | 549 |
void firstInc(Edge &arc, bool& d, const Node& v) const {
|
| 552 | 550 |
int de = nodes[v._id].first_out; |
| 553 | 551 |
if (de != -1) {
|
| 554 | 552 |
arc._id = de / 2; |
| 555 | 553 |
d = ((de & 1) == 1); |
| 556 | 554 |
} else {
|
| 557 | 555 |
arc._id = -1; |
| 558 | 556 |
d = true; |
| 559 | 557 |
} |
| 560 | 558 |
} |
| 561 | 559 |
void nextInc(Edge &arc, bool& d) const {
|
| 562 | 560 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
| 563 | 561 |
if (de != -1) {
|
| 564 | 562 |
arc._id = de / 2; |
| 565 | 563 |
d = ((de & 1) == 1); |
| 566 | 564 |
} else {
|
| 567 | 565 |
arc._id = -1; |
| 568 | 566 |
d = true; |
| 569 | 567 |
} |
| 570 | 568 |
} |
| 571 | 569 |
|
| 572 | 570 |
static int id(Node v) { return v._id; }
|
| 573 | 571 |
static int id(Arc e) { return e._id; }
|
| 574 | 572 |
static int id(Edge e) { return e._id; }
|
| 575 | 573 |
|
| 576 | 574 |
static Node nodeFromId(int id) { return Node(id);}
|
| 577 | 575 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 578 | 576 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 579 | 577 |
|
| 580 | 578 |
bool valid(Node n) const {
|
| 581 | 579 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
| 582 | 580 |
} |
| 583 | 581 |
bool valid(Arc a) const {
|
| 584 | 582 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 585 | 583 |
} |
| 586 | 584 |
bool valid(Edge e) const {
|
| 587 | 585 |
return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
| 588 | 586 |
} |
| 589 | 587 |
|
| 590 | 588 |
Node addNode() {
|
| 591 | 589 |
int n = nodes.size(); |
| 592 | 590 |
nodes.push_back(NodeT()); |
| 593 | 591 |
nodes[n].first_out = -1; |
| 594 | 592 |
|
| 595 | 593 |
return Node(n); |
| 596 | 594 |
} |
| 597 | 595 |
|
| 598 | 596 |
Edge addEdge(Node u, Node v) {
|
| 599 | 597 |
int n = arcs.size(); |
| 600 | 598 |
arcs.push_back(ArcT()); |
| 601 | 599 |
arcs.push_back(ArcT()); |
| 602 | 600 |
|
| 603 | 601 |
arcs[n].target = u._id; |
| 604 | 602 |
arcs[n | 1].target = v._id; |
| 605 | 603 |
|
| 606 | 604 |
arcs[n].next_out = nodes[v._id].first_out; |
| 607 | 605 |
nodes[v._id].first_out = n; |
| 608 | 606 |
|
| 609 | 607 |
arcs[n | 1].next_out = nodes[u._id].first_out; |
| 610 | 608 |
nodes[u._id].first_out = (n | 1); |
| 611 | 609 |
|
| 612 | 610 |
return Edge(n / 2); |
| 613 | 611 |
} |
| 614 | 612 |
|
| 615 | 613 |
void clear() {
|
| 616 | 614 |
arcs.clear(); |
| 617 | 615 |
nodes.clear(); |
| 618 | 616 |
} |
| 619 | 617 |
|
| 620 | 618 |
}; |
| 621 | 619 |
|
| 622 | 620 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
| 623 | 621 |
|
| 624 | 622 |
/// \ingroup graphs |
| 625 | 623 |
/// |
| 626 | 624 |
/// \brief A smart undirected graph class. |
| 627 | 625 |
/// |
| 628 | 626 |
/// This is a simple and fast graph implementation. |
| 629 | 627 |
/// It is also quite memory efficient, but at the price |
| 630 | 628 |
/// that <b> it does support only limited (only stack-like) |
| 631 | 629 |
/// node and arc deletions</b>. |
| 632 |
/// Except from this it conforms to |
|
| 633 |
/// the \ref concepts::Graph "Graph concept". |
|
| 634 |
/// |
|
| 635 |
/// It also has an |
|
| 636 |
/// important extra feature that |
|
| 637 |
/// its maps are real \ref concepts::ReferenceMap "reference map"s. |
|
| 630 |
/// It fully conforms to the \ref concepts::Graph "Graph concept". |
|
| 638 | 631 |
/// |
| 639 | 632 |
/// \sa concepts::Graph. |
| 640 |
/// |
|
| 641 | 633 |
class SmartGraph : public ExtendedSmartGraphBase {
|
| 642 | 634 |
private: |
| 643 | 635 |
|
| 644 | 636 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 645 | 637 |
|
| 646 | 638 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 647 | 639 |
/// |
| 648 | 640 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
|
| 649 | 641 |
|
| 650 | 642 |
///\brief Assignment of SmartGraph to another one is \e not allowed. |
| 651 | 643 |
///Use GraphCopy() instead. |
| 652 | 644 |
|
| 653 | 645 |
///Assignment of SmartGraph to another one is \e not allowed. |
| 654 | 646 |
///Use GraphCopy() instead. |
| 655 | 647 |
void operator=(const SmartGraph &) {}
|
| 656 | 648 |
|
| 657 | 649 |
public: |
| 658 | 650 |
|
| 659 | 651 |
typedef ExtendedSmartGraphBase Parent; |
| 660 | 652 |
|
| 661 | 653 |
/// Constructor |
| 662 | 654 |
|
| 663 | 655 |
/// Constructor. |
| 664 | 656 |
/// |
| 665 | 657 |
SmartGraph() {}
|
| 666 | 658 |
|
| 667 | 659 |
///Add a new node to the graph. |
| 668 | 660 |
|
| 669 | 661 |
/// Add a new node to the graph. |
| 670 | 662 |
/// \return The new node. |
| 671 | 663 |
Node addNode() { return Parent::addNode(); }
|
| 672 | 664 |
|
| 673 | 665 |
///Add a new edge to the graph. |
| 674 | 666 |
|
| 675 | 667 |
///Add a new edge to the graph with node \c s |
| 676 | 668 |
///and \c t. |
| 677 | 669 |
///\return The new edge. |
| 678 | 670 |
Edge addEdge(const Node& s, const Node& t) {
|
| 679 | 671 |
return Parent::addEdge(s, t); |
| 680 | 672 |
} |
| 681 | 673 |
|
| 682 | 674 |
/// \brief Node validity check |
| 683 | 675 |
/// |
| 684 | 676 |
/// This function gives back true if the given node is valid, |
| 685 | 677 |
/// ie. it is a real node of the graph. |
| 686 | 678 |
/// |
| 687 | 679 |
/// \warning A removed node (using Snapshot) could become valid again |
| 688 | 680 |
/// when new nodes are added to the graph. |
| 689 | 681 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 690 | 682 |
|
| 691 | 683 |
/// \brief Arc validity check |
| 692 | 684 |
/// |
| 693 | 685 |
/// This function gives back true if the given arc is valid, |
| 694 | 686 |
/// ie. it is a real arc of the graph. |
| 695 | 687 |
/// |
| 696 | 688 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 697 | 689 |
/// when new edges are added to the graph. |
| 698 | 690 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 699 | 691 |
|
| 700 | 692 |
/// \brief Edge validity check |
| 701 | 693 |
/// |
| 702 | 694 |
/// This function gives back true if the given edge is valid, |
| 703 | 695 |
/// ie. it is a real edge of the graph. |
| 704 | 696 |
/// |
| 705 | 697 |
/// \warning A removed edge (using Snapshot) could become valid again |
| 706 | 698 |
/// when new edges are added to the graph. |
| 707 | 699 |
bool valid(Edge e) const { return Parent::valid(e); }
|
| 708 | 700 |
|
| 709 | 701 |
///Clear the graph. |
| 710 | 702 |
|
| 711 | 703 |
///Erase all the nodes and edges from the graph. |
| 712 | 704 |
/// |
| 713 | 705 |
void clear() {
|
| 714 | 706 |
Parent::clear(); |
| 715 | 707 |
} |
| 716 | 708 |
|
| 717 | 709 |
public: |
| 718 | 710 |
|
| 719 | 711 |
class Snapshot; |
| 720 | 712 |
|
| 721 | 713 |
protected: |
| 722 | 714 |
|
| 723 | 715 |
void saveSnapshot(Snapshot &s) |
| 724 | 716 |
{
|
| 725 | 717 |
s._graph = this; |
| 726 | 718 |
s.node_num = nodes.size(); |
| 727 | 719 |
s.arc_num = arcs.size(); |
| 728 | 720 |
} |
| 729 | 721 |
|
| 730 | 722 |
void restoreSnapshot(const Snapshot &s) |
| 731 | 723 |
{
|
| 732 | 724 |
while(s.arc_num<arcs.size()) {
|
| 733 | 725 |
int n=arcs.size()-1; |
| 734 | 726 |
Edge arc=edgeFromId(n/2); |
| 735 | 727 |
Parent::notifier(Edge()).erase(arc); |
| 736 | 728 |
std::vector<Arc> dir; |
| 737 | 729 |
dir.push_back(arcFromId(n)); |
| 738 | 730 |
dir.push_back(arcFromId(n-1)); |
| 739 | 731 |
Parent::notifier(Arc()).erase(dir); |
| 740 | 732 |
nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
| 741 | 733 |
nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
| 742 | 734 |
arcs.pop_back(); |
| 743 | 735 |
arcs.pop_back(); |
| 744 | 736 |
} |
| 745 | 737 |
while(s.node_num<nodes.size()) {
|
| 746 | 738 |
int n=nodes.size()-1; |
| 747 | 739 |
Node node = nodeFromId(n); |
| 748 | 740 |
Parent::notifier(Node()).erase(node); |
| 749 | 741 |
nodes.pop_back(); |
| 750 | 742 |
} |
| 751 | 743 |
} |
| 752 | 744 |
|
| 753 | 745 |
public: |
| 754 | 746 |
|
| 755 | 747 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 756 | 748 |
|
| 757 | 749 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 758 | 750 |
/// |
| 759 | 751 |
///The newly added nodes and arcs can be removed using the |
| 760 | 752 |
///restore() function. |
| 761 | 753 |
/// |
| 762 | 754 |
///\note After you restore a state, you cannot restore |
| 763 | 755 |
///a later state, in other word you cannot add again the arcs deleted |
| 764 | 756 |
///by restore() using another one Snapshot instance. |
| 765 | 757 |
/// |
| 766 | 758 |
///\warning If you do not use correctly the snapshot that can cause |
| 767 | 759 |
///either broken program, invalid state of the digraph, valid but |
| 768 | 760 |
///not the restored digraph or no change. Because the runtime performance |
| 769 | 761 |
///the validity of the snapshot is not stored. |
| 770 | 762 |
class Snapshot |
| 771 | 763 |
{
|
| 772 | 764 |
SmartGraph *_graph; |
| 773 | 765 |
protected: |
| 774 | 766 |
friend class SmartGraph; |
| 775 | 767 |
unsigned int node_num; |
| 776 | 768 |
unsigned int arc_num; |
| 777 | 769 |
public: |
| 778 | 770 |
///Default constructor. |
| 779 | 771 |
|
| 780 | 772 |
///Default constructor. |
| 781 | 773 |
///To actually make a snapshot you must call save(). |
| 782 | 774 |
/// |
| 783 | 775 |
Snapshot() : _graph(0) {}
|
| 784 | 776 |
///Constructor that immediately makes a snapshot |
| 785 | 777 |
|
| 786 | 778 |
///This constructor immediately makes a snapshot of the digraph. |
| 787 | 779 |
///\param graph The digraph we make a snapshot of. |
| 788 | 780 |
Snapshot(SmartGraph &graph) {
|
| 789 | 781 |
graph.saveSnapshot(*this); |
| 790 | 782 |
} |
| 791 | 783 |
|
| 792 | 784 |
///Make a snapshot. |
| 793 | 785 |
|
| 794 | 786 |
///Make a snapshot of the graph. |
| 795 | 787 |
/// |
| 796 | 788 |
///This function can be called more than once. In case of a repeated |
| 797 | 789 |
///call, the previous snapshot gets lost. |
| 798 | 790 |
///\param graph The digraph we make the snapshot of. |
| 799 | 791 |
void save(SmartGraph &graph) |
| 800 | 792 |
{
|
| 801 | 793 |
graph.saveSnapshot(*this); |
| 802 | 794 |
} |
| 803 | 795 |
|
| 804 | 796 |
///Undo the changes until a snapshot. |
| 805 | 797 |
|
| 806 | 798 |
///Undo the changes until a snapshot created by save(). |
| 807 | 799 |
/// |
| 808 | 800 |
///\note After you restored a state, you cannot restore |
| 809 | 801 |
///a later state, in other word you cannot add again the arcs deleted |
| 810 | 802 |
///by restore(). |
| 811 | 803 |
void restore() |
| 812 | 804 |
{
|
| 813 | 805 |
_graph->restoreSnapshot(*this); |
| 814 | 806 |
} |
| 815 | 807 |
}; |
| 816 | 808 |
}; |
| 817 | 809 |
|
| 818 | 810 |
} //namespace lemon |
| 819 | 811 |
|
| 820 | 812 |
|
| 821 | 813 |
#endif //LEMON_SMART_GRAPH_H |
0 comments (0 inline)