| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_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 | 194 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
| 195 | 195 |
///an important extra feature that its maps are real \ref |
| 196 | 196 |
///concepts::ReferenceMap "reference map"s. |
| 197 | 197 |
/// |
| 198 | 198 |
///\sa concepts::Digraph. |
| 199 | 199 |
class SmartDigraph : public ExtendedSmartDigraphBase {
|
| 200 | 200 |
public: |
| 201 | 201 |
|
| 202 | 202 |
typedef ExtendedSmartDigraphBase Parent; |
| 203 | 203 |
|
| 204 | 204 |
private: |
| 205 | 205 |
|
| 206 | 206 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 207 | 207 |
|
| 208 | 208 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 209 | 209 |
/// |
| 210 | 210 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
|
| 211 | 211 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
| 212 | 212 |
///Use DigraphCopy() instead. |
| 213 | 213 |
|
| 214 | 214 |
///Assignment of SmartDigraph to another one is \e not allowed. |
| 215 | 215 |
///Use DigraphCopy() instead. |
| 216 | 216 |
void operator=(const SmartDigraph &) {}
|
| 217 | 217 |
|
| 218 | 218 |
public: |
| 219 | 219 |
|
| 220 | 220 |
/// Constructor |
| 221 | 221 |
|
| 222 | 222 |
/// Constructor. |
| 223 | 223 |
/// |
| 224 | 224 |
SmartDigraph() {};
|
| 225 | 225 |
|
| 226 | 226 |
///Add a new node to the digraph. |
| 227 | 227 |
|
| 228 | 228 |
/// \return the new node. |
| 229 | 229 |
/// |
| 230 | 230 |
Node addNode() { return Parent::addNode(); }
|
| 231 | 231 |
|
| 232 | 232 |
///Add a new arc to the digraph. |
| 233 | 233 |
|
| 234 | 234 |
///Add a new arc to the digraph with source node \c s |
| 235 | 235 |
///and target node \c t. |
| 236 | 236 |
///\return the new arc. |
| 237 | 237 |
Arc addArc(const Node& s, const Node& t) {
|
| 238 | 238 |
return Parent::addArc(s, t); |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 242 | 242 |
/// allocation. |
| 243 | 243 |
|
| 244 | 244 |
/// Using this it is possible to avoid the superfluous memory |
| 245 | 245 |
/// allocation: if you know that the digraph you want to build will |
| 246 | 246 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 247 | 247 |
/// then it is worth reserving space for this amount before starting |
| 248 | 248 |
/// to build the digraph. |
| 249 | 249 |
/// \sa reserveArc |
| 250 | 250 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 251 | 251 |
|
| 252 | 252 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 253 | 253 |
/// allocation. |
| 254 | 254 |
|
| 255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
| 256 | 256 |
/// allocation: if you know that the digraph you want to build will |
| 257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 258 | 258 |
/// then it is worth reserving space for this amount before starting |
| 259 | 259 |
/// to build the digraph. |
| 260 | 260 |
/// \sa reserveNode |
| 261 | 261 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 262 | 262 |
|
| 263 | 263 |
/// \brief Node validity check |
| 264 | 264 |
/// |
| 265 | 265 |
/// This function gives back true if the given node is valid, |
| 266 | 266 |
/// ie. it is a real node of the graph. |
| 267 | 267 |
/// |
| 268 | 268 |
/// \warning A removed node (using Snapshot) could become valid again |
| 269 | 269 |
/// when new nodes are added to the graph. |
| 270 | 270 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 271 | 271 |
|
| 272 | 272 |
/// \brief Arc validity check |
| 273 | 273 |
/// |
| 274 | 274 |
/// This function gives back true if the given arc is valid, |
| 275 | 275 |
/// ie. it is a real arc of the graph. |
| 276 | 276 |
/// |
| 277 | 277 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 278 | 278 |
/// when new arcs are added to the graph. |
| 279 | 279 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 280 | 280 |
|
| 281 | 281 |
///Clear the digraph. |
| 282 | 282 |
|
| 283 | 283 |
///Erase all the nodes and arcs from the digraph. |
| 284 | 284 |
/// |
| 285 | 285 |
void clear() {
|
| 286 | 286 |
Parent::clear(); |
| 287 | 287 |
} |
| 288 | 288 |
|
| 289 | 289 |
///Split a node. |
| 290 | 290 |
|
| 291 | 291 |
///This function splits a node. First a new node is added to the digraph, |
| 292 | 292 |
///then the source of each outgoing arc of \c n is moved to this new node. |
| 293 | 293 |
///If \c connect is \c true (this is the default value), then a new arc |
| 294 | 294 |
///from \c n to the newly created node is also added. |
| 295 | 295 |
///\return The newly created node. |
| 296 | 296 |
/// |
| 297 | 297 |
///\note The <tt>Arc</tt>s |
| 298 | 298 |
///referencing a moved arc remain |
| 299 | 299 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
| 300 | 300 |
///may be invalidated. |
| 301 | 301 |
///\warning This functionality cannot be used together with the Snapshot |
| 302 | 302 |
///feature. |
| 303 | 303 |
Node split(Node n, bool connect = true) |
| 304 | 304 |
{
|
| 305 | 305 |
Node b = addNode(); |
| 306 | 306 |
nodes[b._id].first_out=nodes[n._id].first_out; |
| 307 | 307 |
nodes[n._id].first_out=-1; |
| 308 |
for(int i=nodes[b._id].first_out;i!=-1; |
|
| 308 |
for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
|
|
| 309 |
arcs[i].source=b._id; |
|
| 310 |
} |
|
| 309 | 311 |
if(connect) addArc(n,b); |
| 310 | 312 |
return b; |
| 311 | 313 |
} |
| 312 | 314 |
|
| 313 | 315 |
public: |
| 314 | 316 |
|
| 315 | 317 |
class Snapshot; |
| 316 | 318 |
|
| 317 | 319 |
protected: |
| 318 | 320 |
|
| 319 | 321 |
void restoreSnapshot(const Snapshot &s) |
| 320 | 322 |
{
|
| 321 | 323 |
while(s.arc_num<arcs.size()) {
|
| 322 | 324 |
Arc arc = arcFromId(arcs.size()-1); |
| 323 | 325 |
Parent::notifier(Arc()).erase(arc); |
| 324 | 326 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
| 325 | 327 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
| 326 | 328 |
arcs.pop_back(); |
| 327 | 329 |
} |
| 328 | 330 |
while(s.node_num<nodes.size()) {
|
| 329 | 331 |
Node node = nodeFromId(nodes.size()-1); |
| 330 | 332 |
Parent::notifier(Node()).erase(node); |
| 331 | 333 |
nodes.pop_back(); |
| 332 | 334 |
} |
| 333 | 335 |
} |
| 334 | 336 |
|
| 335 | 337 |
public: |
| 336 | 338 |
|
| 337 | 339 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 338 | 340 |
|
| 339 | 341 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 340 | 342 |
/// |
| 341 | 343 |
///The newly added nodes and arcs can be removed using the |
| 342 | 344 |
///restore() function. |
| 343 | 345 |
///\note After you restore a state, you cannot restore |
| 344 | 346 |
///a later state, in other word you cannot add again the arcs deleted |
| 345 | 347 |
///by restore() using another one Snapshot instance. |
| 346 | 348 |
/// |
| 347 | 349 |
///\warning If you do not use correctly the snapshot that can cause |
| 348 | 350 |
///either broken program, invalid state of the digraph, valid but |
| 349 | 351 |
///not the restored digraph or no change. Because the runtime performance |
| 350 | 352 |
///the validity of the snapshot is not stored. |
| 351 | 353 |
class Snapshot |
| 352 | 354 |
{
|
| 353 | 355 |
SmartDigraph *_graph; |
| 354 | 356 |
protected: |
| 355 | 357 |
friend class SmartDigraph; |
| 356 | 358 |
unsigned int node_num; |
| 357 | 359 |
unsigned int arc_num; |
| 358 | 360 |
public: |
| 359 | 361 |
///Default constructor. |
| 360 | 362 |
|
| 361 | 363 |
///Default constructor. |
| 362 | 364 |
///To actually make a snapshot you must call save(). |
| 363 | 365 |
/// |
| 364 | 366 |
Snapshot() : _graph(0) {}
|
| 365 | 367 |
///Constructor that immediately makes a snapshot |
| 366 | 368 |
|
| 367 | 369 |
///This constructor immediately makes a snapshot of the digraph. |
| 368 | 370 |
///\param graph The digraph we make a snapshot of. |
| 369 | 371 |
Snapshot(SmartDigraph &graph) : _graph(&graph) {
|
| 370 | 372 |
node_num=_graph->nodes.size(); |
| 371 | 373 |
arc_num=_graph->arcs.size(); |
| 372 | 374 |
} |
| 373 | 375 |
|
| 374 | 376 |
///Make a snapshot. |
| 375 | 377 |
|
| 376 | 378 |
///Make a snapshot of the digraph. |
| 377 | 379 |
/// |
| 378 | 380 |
///This function can be called more than once. In case of a repeated |
| 379 | 381 |
///call, the previous snapshot gets lost. |
| 380 | 382 |
///\param graph The digraph we make the snapshot of. |
| 381 | 383 |
void save(SmartDigraph &graph) |
| 382 | 384 |
{
|
| 383 | 385 |
_graph=&graph; |
| 384 | 386 |
node_num=_graph->nodes.size(); |
| 385 | 387 |
arc_num=_graph->arcs.size(); |
| 386 | 388 |
} |
| 387 | 389 |
|
| 388 | 390 |
///Undo the changes until a snapshot. |
| 389 | 391 |
|
| 390 | 392 |
///Undo the changes until a snapshot created by save(). |
| 391 | 393 |
/// |
| 392 | 394 |
///\note After you restored a state, you cannot restore |
| 393 | 395 |
///a later state, in other word you cannot add again the arcs deleted |
| 394 | 396 |
///by restore(). |
| 395 | 397 |
void restore() |
| 396 | 398 |
{
|
| 397 | 399 |
_graph->restoreSnapshot(*this); |
| 398 | 400 |
} |
| 399 | 401 |
}; |
| 400 | 402 |
}; |
| 401 | 403 |
|
| 402 | 404 |
|
| 403 | 405 |
class SmartGraphBase {
|
| 404 | 406 |
|
| 405 | 407 |
protected: |
| 406 | 408 |
|
| 407 | 409 |
struct NodeT {
|
| 408 | 410 |
int first_out; |
| 409 | 411 |
}; |
| 410 | 412 |
|
| 411 | 413 |
struct ArcT {
|
| 412 | 414 |
int target; |
| 413 | 415 |
int next_out; |
| 414 | 416 |
}; |
| 415 | 417 |
|
| 416 | 418 |
std::vector<NodeT> nodes; |
| 417 | 419 |
std::vector<ArcT> arcs; |
| 418 | 420 |
|
| 419 | 421 |
int first_free_arc; |
| 420 | 422 |
|
| 421 | 423 |
public: |
| 422 | 424 |
|
| 423 | 425 |
typedef SmartGraphBase Digraph; |
| 424 | 426 |
|
| 425 | 427 |
class Node; |
| 426 | 428 |
class Arc; |
| 427 | 429 |
class Edge; |
| 428 | 430 |
|
| 429 | 431 |
class Node {
|
| 430 | 432 |
friend class SmartGraphBase; |
| 431 | 433 |
protected: |
| 432 | 434 |
|
| 433 | 435 |
int _id; |
| 434 | 436 |
explicit Node(int id) { _id = id;}
|
| 435 | 437 |
|
| 436 | 438 |
public: |
| 437 | 439 |
Node() {}
|
| 438 | 440 |
Node (Invalid) { _id = -1; }
|
| 439 | 441 |
bool operator==(const Node& node) const {return _id == node._id;}
|
| 440 | 442 |
bool operator!=(const Node& node) const {return _id != node._id;}
|
| 441 | 443 |
bool operator<(const Node& node) const {return _id < node._id;}
|
| 442 | 444 |
}; |
| 443 | 445 |
|
| 444 | 446 |
class Edge {
|
| 445 | 447 |
friend class SmartGraphBase; |
| 446 | 448 |
protected: |
| 447 | 449 |
|
| 448 | 450 |
int _id; |
| 449 | 451 |
explicit Edge(int id) { _id = id;}
|
| 450 | 452 |
|
| 451 | 453 |
public: |
| 452 | 454 |
Edge() {}
|
| 453 | 455 |
Edge (Invalid) { _id = -1; }
|
| 454 | 456 |
bool operator==(const Edge& arc) const {return _id == arc._id;}
|
| 455 | 457 |
bool operator!=(const Edge& arc) const {return _id != arc._id;}
|
| 456 | 458 |
bool operator<(const Edge& arc) const {return _id < arc._id;}
|
| 457 | 459 |
}; |
| 458 | 460 |
|
| 459 | 461 |
class Arc {
|
| 460 | 462 |
friend class SmartGraphBase; |
| 461 | 463 |
protected: |
| 462 | 464 |
|
| 463 | 465 |
int _id; |
| 464 | 466 |
explicit Arc(int id) { _id = id;}
|
| 465 | 467 |
|
| 466 | 468 |
public: |
| 467 | 469 |
operator Edge() const {
|
| 468 | 470 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
| 469 | 471 |
} |
| 470 | 472 |
|
| 471 | 473 |
Arc() {}
|
| 472 | 474 |
Arc (Invalid) { _id = -1; }
|
| 473 | 475 |
bool operator==(const Arc& arc) const {return _id == arc._id;}
|
| 474 | 476 |
bool operator!=(const Arc& arc) const {return _id != arc._id;}
|
| 475 | 477 |
bool operator<(const Arc& arc) const {return _id < arc._id;}
|
| 476 | 478 |
}; |
| 477 | 479 |
|
| 478 | 480 |
|
| 479 | 481 |
|
| 480 | 482 |
SmartGraphBase() |
| 481 | 483 |
: nodes(), arcs() {}
|
| 482 | 484 |
|
| 483 | 485 |
typedef True NodeNumTag; |
| 484 | 486 |
typedef True EdgeNumTag; |
| 485 | 487 |
typedef True ArcNumTag; |
| 486 | 488 |
|
| 487 | 489 |
int nodeNum() const { return nodes.size(); }
|
| 488 | 490 |
int edgeNum() const { return arcs.size() / 2; }
|
| 489 | 491 |
int arcNum() const { return arcs.size(); }
|
| 490 | 492 |
|
| 491 | 493 |
int maxNodeId() const { return nodes.size()-1; }
|
| 492 | 494 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 493 | 495 |
int maxArcId() const { return arcs.size()-1; }
|
| 494 | 496 |
|
| 495 | 497 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
|
| 496 | 498 |
Node target(Arc e) const { return Node(arcs[e._id].target); }
|
| 497 | 499 |
|
| 498 | 500 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
|
| 499 | 501 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
|
| 500 | 502 |
|
| 501 | 503 |
static bool direction(Arc e) {
|
| 502 | 504 |
return (e._id & 1) == 1; |
| 503 | 505 |
} |
| 504 | 506 |
|
| 505 | 507 |
static Arc direct(Edge e, bool d) {
|
| 506 | 508 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
| 507 | 509 |
} |
| 508 | 510 |
|
| 509 | 511 |
void first(Node& node) const {
|
| 510 | 512 |
node._id = nodes.size() - 1; |
| 511 | 513 |
} |
| 512 | 514 |
|
| 513 | 515 |
void next(Node& node) const {
|
| 514 | 516 |
--node._id; |
| 515 | 517 |
} |
| 516 | 518 |
|
| 517 | 519 |
void first(Arc& arc) const {
|
| 518 | 520 |
arc._id = arcs.size() - 1; |
| 519 | 521 |
} |
| 520 | 522 |
|
| 521 | 523 |
void next(Arc& arc) const {
|
| 522 | 524 |
--arc._id; |
| 523 | 525 |
} |
| 524 | 526 |
|
| 525 | 527 |
void first(Edge& arc) const {
|
| 526 | 528 |
arc._id = arcs.size() / 2 - 1; |
| 527 | 529 |
} |
| 528 | 530 |
|
| 529 | 531 |
void next(Edge& arc) const {
|
| 530 | 532 |
--arc._id; |
| 531 | 533 |
} |
| 532 | 534 |
|
| 533 | 535 |
void firstOut(Arc &arc, const Node& v) const {
|
| 534 | 536 |
arc._id = nodes[v._id].first_out; |
| 535 | 537 |
} |
| 536 | 538 |
void nextOut(Arc &arc) const {
|
| 537 | 539 |
arc._id = arcs[arc._id].next_out; |
| 538 | 540 |
} |
| 539 | 541 |
|
| 540 | 542 |
void firstIn(Arc &arc, const Node& v) const {
|
| 541 | 543 |
arc._id = ((nodes[v._id].first_out) ^ 1); |
| 542 | 544 |
if (arc._id == -2) arc._id = -1; |
| 543 | 545 |
} |
| 544 | 546 |
void nextIn(Arc &arc) const {
|
| 545 | 547 |
arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
| 546 | 548 |
if (arc._id == -2) arc._id = -1; |
| 547 | 549 |
} |
| 548 | 550 |
|
| 549 | 551 |
void firstInc(Edge &arc, bool& d, const Node& v) const {
|
| 550 | 552 |
int de = nodes[v._id].first_out; |
| 551 | 553 |
if (de != -1) {
|
| 552 | 554 |
arc._id = de / 2; |
| 553 | 555 |
d = ((de & 1) == 1); |
| 554 | 556 |
} else {
|
| 555 | 557 |
arc._id = -1; |
| 556 | 558 |
d = true; |
| 557 | 559 |
} |
| 558 | 560 |
} |
| 559 | 561 |
void nextInc(Edge &arc, bool& d) const {
|
| 560 | 562 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
| 561 | 563 |
if (de != -1) {
|
| 562 | 564 |
arc._id = de / 2; |
| 563 | 565 |
d = ((de & 1) == 1); |
| 564 | 566 |
} else {
|
| 565 | 567 |
arc._id = -1; |
| 566 | 568 |
d = true; |
| 567 | 569 |
} |
| 568 | 570 |
} |
| 569 | 571 |
|
| 570 | 572 |
static int id(Node v) { return v._id; }
|
| 571 | 573 |
static int id(Arc e) { return e._id; }
|
| 572 | 574 |
static int id(Edge e) { return e._id; }
|
| 573 | 575 |
|
| 574 | 576 |
static Node nodeFromId(int id) { return Node(id);}
|
| 575 | 577 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 576 | 578 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 577 | 579 |
|
| 578 | 580 |
bool valid(Node n) const {
|
| 579 | 581 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
| 580 | 582 |
} |
| 581 | 583 |
bool valid(Arc a) const {
|
| 582 | 584 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 583 | 585 |
} |
| 584 | 586 |
bool valid(Edge e) const {
|
| 585 | 587 |
return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
| 586 | 588 |
} |
| 587 | 589 |
|
| 588 | 590 |
Node addNode() {
|
| 589 | 591 |
int n = nodes.size(); |
| 590 | 592 |
nodes.push_back(NodeT()); |
| 591 | 593 |
nodes[n].first_out = -1; |
| 592 | 594 |
|
| 593 | 595 |
return Node(n); |
| 594 | 596 |
} |
| 595 | 597 |
|
| 596 | 598 |
Edge addEdge(Node u, Node v) {
|
| 597 | 599 |
int n = arcs.size(); |
| 598 | 600 |
arcs.push_back(ArcT()); |
| 599 | 601 |
arcs.push_back(ArcT()); |
| 600 | 602 |
|
| 601 | 603 |
arcs[n].target = u._id; |
| 602 | 604 |
arcs[n | 1].target = v._id; |
| 603 | 605 |
|
| 604 | 606 |
arcs[n].next_out = nodes[v._id].first_out; |
| 605 | 607 |
nodes[v._id].first_out = n; |
| 606 | 608 |
|
| 607 | 609 |
arcs[n | 1].next_out = nodes[u._id].first_out; |
| 608 | 610 |
nodes[u._id].first_out = (n | 1); |
| 609 | 611 |
|
| 610 | 612 |
return Edge(n / 2); |
| 611 | 613 |
} |
| 612 | 614 |
|
| 613 | 615 |
void clear() {
|
| 614 | 616 |
arcs.clear(); |
| 615 | 617 |
nodes.clear(); |
| 616 | 618 |
} |
| 617 | 619 |
|
| 618 | 620 |
}; |
| 619 | 621 |
|
| 620 | 622 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
| 621 | 623 |
|
| 622 | 624 |
/// \ingroup graphs |
| 623 | 625 |
/// |
| 624 | 626 |
/// \brief A smart undirected graph class. |
| 625 | 627 |
/// |
| 626 | 628 |
/// This is a simple and fast graph implementation. |
| 627 | 629 |
/// It is also quite memory efficient, but at the price |
| 628 | 630 |
/// that <b> it does support only limited (only stack-like) |
| 629 | 631 |
/// node and arc deletions</b>. |
| 630 | 632 |
/// Except from this it conforms to |
| 631 | 633 |
/// the \ref concepts::Graph "Graph concept". |
| 632 | 634 |
/// |
| 633 | 635 |
/// It also has an |
| 634 | 636 |
/// important extra feature that |
| 635 | 637 |
/// its maps are real \ref concepts::ReferenceMap "reference map"s. |
| 636 | 638 |
/// |
| 637 | 639 |
/// \sa concepts::Graph. |
| 638 | 640 |
/// |
| 639 | 641 |
class SmartGraph : public ExtendedSmartGraphBase {
|
| 640 | 642 |
private: |
| 641 | 643 |
|
| 642 | 644 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 643 | 645 |
|
| 644 | 646 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
| 645 | 647 |
/// |
| 646 | 648 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
|
| 647 | 649 |
|
| 648 | 650 |
///\brief Assignment of SmartGraph to another one is \e not allowed. |
| 649 | 651 |
///Use GraphCopy() instead. |
| 650 | 652 |
|
| 651 | 653 |
///Assignment of SmartGraph to another one is \e not allowed. |
| 652 | 654 |
///Use GraphCopy() instead. |
| 653 | 655 |
void operator=(const SmartGraph &) {}
|
| 654 | 656 |
|
| 655 | 657 |
public: |
| 656 | 658 |
|
| 657 | 659 |
typedef ExtendedSmartGraphBase Parent; |
| 658 | 660 |
|
| 659 | 661 |
/// Constructor |
| 660 | 662 |
|
| 661 | 663 |
/// Constructor. |
| 662 | 664 |
/// |
| 663 | 665 |
SmartGraph() {}
|
| 664 | 666 |
|
| 665 | 667 |
///Add a new node to the graph. |
| 666 | 668 |
|
| 667 | 669 |
/// \return the new node. |
| 668 | 670 |
/// |
| 669 | 671 |
Node addNode() { return Parent::addNode(); }
|
| 670 | 672 |
|
| 671 | 673 |
///Add a new edge to the graph. |
| 672 | 674 |
|
| 673 | 675 |
///Add a new edge to the graph with node \c s |
| 674 | 676 |
///and \c t. |
| 675 | 677 |
///\return the new edge. |
| 676 | 678 |
Edge addEdge(const Node& s, const Node& t) {
|
| 677 | 679 |
return Parent::addEdge(s, t); |
| 678 | 680 |
} |
| 679 | 681 |
|
| 680 | 682 |
/// \brief Node validity check |
| 681 | 683 |
/// |
| 682 | 684 |
/// This function gives back true if the given node is valid, |
| 683 | 685 |
/// ie. it is a real node of the graph. |
| 684 | 686 |
/// |
| 685 | 687 |
/// \warning A removed node (using Snapshot) could become valid again |
| 686 | 688 |
/// when new nodes are added to the graph. |
| 687 | 689 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 688 | 690 |
|
| 689 | 691 |
/// \brief Arc validity check |
| 690 | 692 |
/// |
| 691 | 693 |
/// This function gives back true if the given arc is valid, |
| 692 | 694 |
/// ie. it is a real arc of the graph. |
0 comments (0 inline)