0
6
0
61
53
33
39
217
210
148
166
... | ... |
@@ -591,82 +591,82 @@ |
591 | 591 |
@ingroup io_group |
592 | 592 |
\brief Read and write files in DIMACS format |
593 | 593 |
|
594 | 594 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
595 | 595 |
*/ |
596 | 596 |
|
597 | 597 |
/** |
598 | 598 |
@defgroup nauty_group NAUTY Format |
599 | 599 |
@ingroup io_group |
600 | 600 |
\brief Read \e Nauty format |
601 | 601 |
|
602 | 602 |
Tool to read graphs from \e Nauty format data. |
603 | 603 |
*/ |
604 | 604 |
|
605 | 605 |
/** |
606 | 606 |
@defgroup concept Concepts |
607 | 607 |
\brief Skeleton classes and concept checking classes |
608 | 608 |
|
609 | 609 |
This group contains the data/algorithm skeletons and concept checking |
610 | 610 |
classes implemented in LEMON. |
611 | 611 |
|
612 | 612 |
The purpose of the classes in this group is fourfold. |
613 | 613 |
|
614 | 614 |
- These classes contain the documentations of the %concepts. In order |
615 | 615 |
to avoid document multiplications, an implementation of a concept |
616 | 616 |
simply refers to the corresponding concept class. |
617 | 617 |
|
618 | 618 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
619 | 619 |
implementation of the %concepts should provide, however completely |
620 | 620 |
without implementations and real data structures behind the |
621 | 621 |
interface. On the other hand they should provide nothing else. All |
622 | 622 |
the algorithms working on a data structure meeting a certain concept |
623 | 623 |
should compile with these classes. (Though it will not run properly, |
624 | 624 |
of course.) In this way it is easily to check if an algorithm |
625 | 625 |
doesn't use any extra feature of a certain implementation. |
626 | 626 |
|
627 | 627 |
- The concept descriptor classes also provide a <em>checker class</em> |
628 | 628 |
that makes it possible to check whether a certain implementation of a |
629 | 629 |
concept indeed provides all the required features. |
630 | 630 |
|
631 | 631 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
632 | 632 |
*/ |
633 | 633 |
|
634 | 634 |
/** |
635 | 635 |
@defgroup graph_concepts Graph Structure Concepts |
636 | 636 |
@ingroup concept |
637 | 637 |
\brief Skeleton and concept checking classes for graph structures |
638 | 638 |
|
639 |
This group contains the skeletons and concept checking classes of LEMON's |
|
640 |
graph structures and helper classes used to implement these. |
|
639 |
This group contains the skeletons and concept checking classes of |
|
640 |
graph structures. |
|
641 | 641 |
*/ |
642 | 642 |
|
643 | 643 |
/** |
644 | 644 |
@defgroup map_concepts Map Concepts |
645 | 645 |
@ingroup concept |
646 | 646 |
\brief Skeleton and concept checking classes for maps |
647 | 647 |
|
648 | 648 |
This group contains the skeletons and concept checking classes of maps. |
649 | 649 |
*/ |
650 | 650 |
|
651 | 651 |
/** |
652 | 652 |
\anchor demoprograms |
653 | 653 |
|
654 | 654 |
@defgroup demos Demo Programs |
655 | 655 |
|
656 | 656 |
Some demo programs are listed here. Their full source codes can be found in |
657 | 657 |
the \c demo subdirectory of the source tree. |
658 | 658 |
|
659 | 659 |
In order to compile them, use the <tt>make demo</tt> or the |
660 | 660 |
<tt>make check</tt> commands. |
661 | 661 |
*/ |
662 | 662 |
|
663 | 663 |
/** |
664 | 664 |
@defgroup tools Standalone Utility Applications |
665 | 665 |
|
666 | 666 |
Some utility applications are listed here. |
667 | 667 |
|
668 | 668 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
669 | 669 |
them, as well. |
670 | 670 |
*/ |
671 | 671 |
|
672 | 672 |
} |
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 |
///\brief |
|
27 |
///\brief FullDigraph and FullGraph classes. |
|
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
class FullDigraphBase { |
32 | 32 |
public: |
33 | 33 |
|
34 | 34 |
typedef FullDigraphBase Digraph; |
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; |
... | ... |
@@ -103,163 +103,167 @@ |
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 |
/// \brief A full |
|
151 |
/// \brief A directed full graph class. |
|
152 | 152 |
/// |
153 |
/// This is a simple and fast directed full graph implementation. |
|
154 |
/// From each node go arcs to each node (including the source node), |
|
155 |
/// therefore the number of the arcs in the digraph is the square of |
|
156 |
/// the node number. This digraph type is completely static, so you |
|
157 |
/// can neither add nor delete either arcs or nodes, and it needs |
|
158 |
/// constant space in memory. |
|
153 |
/// FullDigraph is a simple and fast implmenetation of directed full |
|
154 |
/// (complete) graphs. It contains an arc from each node to each node |
|
155 |
/// (including a loop for each node), therefore the number of arcs |
|
156 |
/// is the square of the number of nodes. |
|
157 |
/// This class is completely static and it needs constant memory space. |
|
158 |
/// Thus you can neither add nor delete nodes or arcs, however |
|
159 |
/// the structure can be resized using resize(). |
|
159 | 160 |
/// |
160 |
/// This class fully conforms to the \ref concepts::Digraph |
|
161 |
/// "Digraph concept". |
|
161 |
/// This type fully conforms to the \ref concepts::Digraph "Digraph concept". |
|
162 |
/// Most of its member functions and nested classes are documented |
|
163 |
/// only in the concept class. |
|
162 | 164 |
/// |
163 |
/// |
|
165 |
/// \note FullDigraph and FullGraph classes are very similar, |
|
164 | 166 |
/// but there are two differences. While this class conforms only |
165 |
/// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph |
|
166 |
/// class conforms to the \ref concepts::Graph "Graph" concept, |
|
167 |
/// moreover \c FullGraph does not contain a loop arc for each |
|
168 |
/// node as \c FullDigraph does. |
|
167 |
/// to the \ref concepts::Digraph "Digraph" concept, FullGraph |
|
168 |
/// conforms to the \ref concepts::Graph "Graph" concept, |
|
169 |
/// moreover FullGraph does not contain a loop for each |
|
170 |
/// node as this class does. |
|
169 | 171 |
/// |
170 | 172 |
/// \sa FullGraph |
171 | 173 |
class FullDigraph : public ExtendedFullDigraphBase { |
172 | 174 |
typedef ExtendedFullDigraphBase Parent; |
173 | 175 |
|
174 | 176 |
public: |
175 | 177 |
|
176 |
/// \brief |
|
178 |
/// \brief Default constructor. |
|
179 |
/// |
|
180 |
/// Default constructor. The number of nodes and arcs will be zero. |
|
177 | 181 |
FullDigraph() { construct(0); } |
178 | 182 |
|
179 | 183 |
/// \brief Constructor |
180 | 184 |
/// |
181 | 185 |
/// Constructor. |
182 | 186 |
/// \param n The number of the nodes. |
183 | 187 |
FullDigraph(int n) { construct(n); } |
184 | 188 |
|
185 | 189 |
/// \brief Resizes the digraph |
186 | 190 |
/// |
187 |
/// Resizes the digraph. The function will fully destroy and |
|
188 |
/// rebuild the digraph. This cause that the maps of the digraph will |
|
191 |
/// This function resizes the digraph. It fully destroys and |
|
192 |
/// rebuilds the structure, therefore the maps of the digraph will be |
|
189 | 193 |
/// reallocated automatically and the previous values will be lost. |
190 | 194 |
void resize(int n) { |
191 | 195 |
Parent::notifier(Arc()).clear(); |
192 | 196 |
Parent::notifier(Node()).clear(); |
193 | 197 |
construct(n); |
194 | 198 |
Parent::notifier(Node()).build(); |
195 | 199 |
Parent::notifier(Arc()).build(); |
196 | 200 |
} |
197 | 201 |
|
198 | 202 |
/// \brief Returns the node with the given index. |
199 | 203 |
/// |
200 |
/// Returns the node with the given index. Since it is a static |
|
201 |
/// digraph its nodes can be indexed with integers from the range |
|
202 |
/// |
|
204 |
/// Returns the node with the given index. Since this structure is |
|
205 |
/// completely static, the nodes can be indexed with integers from |
|
206 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
|
203 | 207 |
/// \sa index() |
204 | 208 |
Node operator()(int ix) const { return Parent::operator()(ix); } |
205 | 209 |
|
206 | 210 |
/// \brief Returns the index of the given node. |
207 | 211 |
/// |
208 |
/// Returns the index of the given node. Since it is a static |
|
209 |
/// digraph its nodes can be indexed with integers from the range |
|
210 |
/// <tt>[0..nodeNum()-1]</tt>. |
|
211 |
/// \sa operator() |
|
212 |
|
|
212 |
/// Returns the index of the given node. Since this structure is |
|
213 |
/// completely static, the nodes can be indexed with integers from |
|
214 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
|
215 |
/// \sa operator()() |
|
216 |
int index(Node node) const { return Parent::index(node); } |
|
213 | 217 |
|
214 | 218 |
/// \brief Returns the arc connecting the given nodes. |
215 | 219 |
/// |
216 | 220 |
/// Returns the arc connecting the given nodes. |
217 |
Arc arc( |
|
221 |
Arc arc(Node u, Node v) const { |
|
218 | 222 |
return Parent::arc(u, v); |
219 | 223 |
} |
220 | 224 |
|
221 | 225 |
/// \brief Number of nodes. |
222 | 226 |
int nodeNum() const { return Parent::nodeNum(); } |
223 | 227 |
/// \brief Number of arcs. |
224 | 228 |
int arcNum() const { return Parent::arcNum(); } |
225 | 229 |
}; |
226 | 230 |
|
227 | 231 |
|
228 | 232 |
class FullGraphBase { |
229 | 233 |
public: |
230 | 234 |
|
231 | 235 |
typedef FullGraphBase Graph; |
232 | 236 |
|
233 | 237 |
class Node; |
234 | 238 |
class Arc; |
235 | 239 |
class Edge; |
236 | 240 |
|
237 | 241 |
protected: |
238 | 242 |
|
239 | 243 |
int _node_num; |
240 | 244 |
int _edge_num; |
241 | 245 |
|
242 | 246 |
FullGraphBase() {} |
243 | 247 |
|
244 | 248 |
void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; } |
245 | 249 |
|
246 | 250 |
int _uid(int e) const { |
247 | 251 |
int u = e / _node_num; |
248 | 252 |
int v = e % _node_num; |
249 | 253 |
return u < v ? u : _node_num - 2 - u; |
250 | 254 |
} |
251 | 255 |
|
252 | 256 |
int _vid(int e) const { |
253 | 257 |
int u = e / _node_num; |
254 | 258 |
int v = e % _node_num; |
255 | 259 |
return u < v ? v : _node_num - 1 - v; |
256 | 260 |
} |
257 | 261 |
|
258 | 262 |
void _uvid(int e, int& u, int& v) const { |
259 | 263 |
u = e / _node_num; |
260 | 264 |
v = e % _node_num; |
261 | 265 |
if (u >= v) { |
262 | 266 |
u = _node_num - 2 - u; |
263 | 267 |
v = _node_num - 1 - v; |
264 | 268 |
} |
265 | 269 |
} |
... | ... |
@@ -475,138 +479,142 @@ |
475 | 479 |
--s; |
476 | 480 |
if (s > t) { |
477 | 481 |
arc._id = (_eid(t, s) << 1); |
478 | 482 |
} else { |
479 | 483 |
if (s == t) --s; |
480 | 484 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1); |
481 | 485 |
} |
482 | 486 |
} |
483 | 487 |
|
484 | 488 |
void firstInc(Edge& edge, bool& dir, const Node& node) const { |
485 | 489 |
int u = node._id, v = _node_num - 1; |
486 | 490 |
if (u < v) { |
487 | 491 |
edge._id = _eid(u, v); |
488 | 492 |
dir = true; |
489 | 493 |
} else { |
490 | 494 |
--v; |
491 | 495 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
492 | 496 |
dir = false; |
493 | 497 |
} |
494 | 498 |
} |
495 | 499 |
|
496 | 500 |
void nextInc(Edge& edge, bool& dir) const { |
497 | 501 |
int u, v; |
498 | 502 |
if (dir) { |
499 | 503 |
_uvid(edge._id, u, v); |
500 | 504 |
--v; |
501 | 505 |
if (u < v) { |
502 | 506 |
edge._id = _eid(u, v); |
503 | 507 |
} else { |
504 | 508 |
--v; |
505 | 509 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
506 | 510 |
dir = false; |
507 | 511 |
} |
508 | 512 |
} else { |
509 | 513 |
_uvid(edge._id, v, u); |
510 | 514 |
--v; |
511 | 515 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
512 | 516 |
} |
513 | 517 |
} |
514 | 518 |
|
515 | 519 |
}; |
516 | 520 |
|
517 | 521 |
typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase; |
518 | 522 |
|
519 | 523 |
/// \ingroup graphs |
520 | 524 |
/// |
521 | 525 |
/// \brief An undirected full graph class. |
522 | 526 |
/// |
523 |
/// This is a simple and fast undirected full graph |
|
524 |
/// implementation. From each node go edge to each other node, |
|
525 |
/// therefore the number of edges in the graph is \f$n(n-1)/2\f$. |
|
526 |
/// This graph type is completely static, so you can neither |
|
527 |
/// add nor delete either edges or nodes, and it needs constant |
|
528 |
/// space in memory. |
|
527 |
/// FullGraph is a simple and fast implmenetation of undirected full |
|
528 |
/// (complete) graphs. It contains an edge between every distinct pair |
|
529 |
/// of nodes, therefore the number of edges is <tt>n(n-1)/2</tt>. |
|
530 |
/// This class is completely static and it needs constant memory space. |
|
531 |
/// Thus you can neither add nor delete nodes or edges, however |
|
532 |
/// the structure can be resized using resize(). |
|
529 | 533 |
/// |
530 |
/// This |
|
534 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept". |
|
535 |
/// Most of its member functions and nested classes are documented |
|
536 |
/// only in the concept class. |
|
531 | 537 |
/// |
532 |
/// The \c FullGraph and \c FullDigraph classes are very similar, |
|
533 |
/// but there are two differences. While the \c FullDigraph class |
|
538 |
/// \note FullDigraph and FullGraph classes are very similar, |
|
539 |
/// but there are two differences. While FullDigraph |
|
534 | 540 |
/// conforms only to the \ref concepts::Digraph "Digraph" concept, |
535 | 541 |
/// this class conforms to the \ref concepts::Graph "Graph" concept, |
536 |
/// moreover \c FullGraph does not contain a loop arc for each |
|
537 |
/// node as \c FullDigraph does. |
|
542 |
/// moreover this class does not contain a loop for each |
|
543 |
/// node as FullDigraph does. |
|
538 | 544 |
/// |
539 | 545 |
/// \sa FullDigraph |
540 | 546 |
class FullGraph : public ExtendedFullGraphBase { |
541 | 547 |
typedef ExtendedFullGraphBase Parent; |
542 | 548 |
|
543 | 549 |
public: |
544 | 550 |
|
545 |
/// \brief |
|
551 |
/// \brief Default constructor. |
|
552 |
/// |
|
553 |
/// Default constructor. The number of nodes and edges will be zero. |
|
546 | 554 |
FullGraph() { construct(0); } |
547 | 555 |
|
548 | 556 |
/// \brief Constructor |
549 | 557 |
/// |
550 | 558 |
/// Constructor. |
551 | 559 |
/// \param n The number of the nodes. |
552 | 560 |
FullGraph(int n) { construct(n); } |
553 | 561 |
|
554 | 562 |
/// \brief Resizes the graph |
555 | 563 |
/// |
556 |
/// Resizes the graph. The function will fully destroy and |
|
557 |
/// rebuild the graph. This cause that the maps of the graph will |
|
564 |
/// This function resizes the graph. It fully destroys and |
|
565 |
/// rebuilds the structure, therefore the maps of the graph will be |
|
558 | 566 |
/// reallocated automatically and the previous values will be lost. |
559 | 567 |
void resize(int n) { |
560 | 568 |
Parent::notifier(Arc()).clear(); |
561 | 569 |
Parent::notifier(Edge()).clear(); |
562 | 570 |
Parent::notifier(Node()).clear(); |
563 | 571 |
construct(n); |
564 | 572 |
Parent::notifier(Node()).build(); |
565 | 573 |
Parent::notifier(Edge()).build(); |
566 | 574 |
Parent::notifier(Arc()).build(); |
567 | 575 |
} |
568 | 576 |
|
569 | 577 |
/// \brief Returns the node with the given index. |
570 | 578 |
/// |
571 |
/// Returns the node with the given index. Since it is a static |
|
572 |
/// graph its nodes can be indexed with integers from the range |
|
573 |
/// |
|
579 |
/// Returns the node with the given index. Since this structure is |
|
580 |
/// completely static, the nodes can be indexed with integers from |
|
581 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
|
574 | 582 |
/// \sa index() |
575 | 583 |
Node operator()(int ix) const { return Parent::operator()(ix); } |
576 | 584 |
|
577 | 585 |
/// \brief Returns the index of the given node. |
578 | 586 |
/// |
579 |
/// Returns the index of the given node. Since it is a static |
|
580 |
/// graph its nodes can be indexed with integers from the range |
|
581 |
/// <tt>[0..nodeNum()-1]</tt>. |
|
582 |
/// \sa operator() |
|
583 |
|
|
587 |
/// Returns the index of the given node. Since this structure is |
|
588 |
/// completely static, the nodes can be indexed with integers from |
|
589 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
|
590 |
/// \sa operator()() |
|
591 |
int index(Node node) const { return Parent::index(node); } |
|
584 | 592 |
|
585 | 593 |
/// \brief Returns the arc connecting the given nodes. |
586 | 594 |
/// |
587 | 595 |
/// Returns the arc connecting the given nodes. |
588 |
Arc arc( |
|
596 |
Arc arc(Node s, Node t) const { |
|
589 | 597 |
return Parent::arc(s, t); |
590 | 598 |
} |
591 | 599 |
|
592 |
/// \brief Returns the edge |
|
600 |
/// \brief Returns the edge connecting the given nodes. |
|
593 | 601 |
/// |
594 |
/// Returns the edge connects the given nodes. |
|
595 |
Edge edge(const Node& u, const Node& v) const { |
|
602 |
/// Returns the edge connecting the given nodes. |
|
603 |
Edge edge(Node u, Node v) const { |
|
596 | 604 |
return Parent::edge(u, v); |
597 | 605 |
} |
598 | 606 |
|
599 | 607 |
/// \brief Number of nodes. |
600 | 608 |
int nodeNum() const { return Parent::nodeNum(); } |
601 | 609 |
/// \brief Number of arcs. |
602 | 610 |
int arcNum() const { return Parent::arcNum(); } |
603 | 611 |
/// \brief Number of edges. |
604 | 612 |
int edgeNum() const { return Parent::edgeNum(); } |
605 | 613 |
|
606 | 614 |
}; |
607 | 615 |
|
608 | 616 |
|
609 | 617 |
} //namespace lemon |
610 | 618 |
|
611 | 619 |
|
612 | 620 |
#endif //LEMON_FULL_GRAPH_H |
... | ... |
@@ -425,279 +425,273 @@ |
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 |
/// This class implements a special graph type. The nodes of the |
|
474 |
/// graph can be indexed by two integer \c (i,j) value where \c i is |
|
475 |
/// in the \c [0..width()-1] range and j is in the \c |
|
476 |
/// [0..height()-1] range. Two nodes are connected in the graph if |
|
477 |
/// the indexes differ exactly on one position and exactly one is |
|
478 |
/// the difference. The nodes of the graph can be indexed by position |
|
479 |
/// with the \c operator()() function. The positions of the nodes can be |
|
480 |
/// get with \c pos(), \c col() and \c row() members. The outgoing |
|
473 |
/// GridGraph implements a special graph type. The nodes of the |
|
474 |
/// graph can be indexed by two integer values \c (i,j) where \c i is |
|
475 |
/// in the range <tt>[0..width()-1]</tt> and j is in the range |
|
476 |
/// <tt>[0..height()-1]</tt>. Two nodes are connected in the graph if |
|
477 |
/// the indices differ exactly on one position and the difference is |
|
478 |
/// also exactly one. The nodes of the graph can be obtained by position |
|
479 |
/// using the \c operator()() function and the indices of the nodes can |
|
480 |
/// be obtained using \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 |
/// This class is completely static and it needs constant memory space. |
|
486 |
/// Thus you can neither add nor delete nodes or edges, however |
|
487 |
/// the structure can be resized using resize(). |
|
488 |
/// |
|
485 | 489 |
/// \image html grid_graph.png |
486 | 490 |
/// \image latex grid_graph.eps "Grid graph" width=\textwidth |
487 | 491 |
/// |
488 | 492 |
/// A short example about the basic usage: |
489 | 493 |
///\code |
490 | 494 |
/// GridGraph graph(rows, cols); |
491 | 495 |
/// GridGraph::NodeMap<int> val(graph); |
492 | 496 |
/// for (int i = 0; i < graph.width(); ++i) { |
493 | 497 |
/// for (int j = 0; j < graph.height(); ++j) { |
494 | 498 |
/// val[graph(i, j)] = i + j; |
495 | 499 |
/// } |
496 | 500 |
/// } |
497 | 501 |
///\endcode |
498 | 502 |
/// |
499 |
/// This graph type fully conforms to the \ref concepts::Graph |
|
500 |
/// "Graph concept". |
|
503 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept". |
|
504 |
/// Most of its member functions and nested classes are documented |
|
505 |
/// only in the concept class. |
|
501 | 506 |
class GridGraph : public ExtendedGridGraphBase { |
502 | 507 |
typedef ExtendedGridGraphBase Parent; |
503 | 508 |
|
504 | 509 |
public: |
505 | 510 |
|
506 |
/// \brief Map to get the indices of the nodes as dim2::Point |
|
511 |
/// \brief Map to get the indices of the nodes as \ref dim2::Point |
|
512 |
/// "dim2::Point<int>". |
|
507 | 513 |
/// |
508 |
/// Map to get the indices of the nodes as dim2::Point |
|
514 |
/// Map to get the indices of the nodes as \ref dim2::Point |
|
515 |
/// "dim2::Point<int>". |
|
509 | 516 |
class IndexMap { |
510 | 517 |
public: |
511 | 518 |
/// \brief The key type of the map |
512 | 519 |
typedef GridGraph::Node Key; |
513 | 520 |
/// \brief The value type of the map |
514 | 521 |
typedef dim2::Point<int> Value; |
515 | 522 |
|
516 | 523 |
/// \brief Constructor |
517 |
/// |
|
518 |
/// Constructor |
|
519 | 524 |
IndexMap(const GridGraph& graph) : _graph(graph) {} |
520 | 525 |
|
521 | 526 |
/// \brief The subscript operator |
522 |
/// |
|
523 |
/// The subscript operator. |
|
524 | 527 |
Value operator[](Key key) const { |
525 | 528 |
return _graph.pos(key); |
526 | 529 |
} |
527 | 530 |
|
528 | 531 |
private: |
529 | 532 |
const GridGraph& _graph; |
530 | 533 |
}; |
531 | 534 |
|
532 | 535 |
/// \brief Map to get the column of the nodes. |
533 | 536 |
/// |
534 | 537 |
/// Map to get the column of the nodes. |
535 | 538 |
class ColMap { |
536 | 539 |
public: |
537 | 540 |
/// \brief The key type of the map |
538 | 541 |
typedef GridGraph::Node Key; |
539 | 542 |
/// \brief The value type of the map |
540 | 543 |
typedef int Value; |
541 | 544 |
|
542 | 545 |
/// \brief Constructor |
543 |
/// |
|
544 |
/// Constructor |
|
545 | 546 |
ColMap(const GridGraph& graph) : _graph(graph) {} |
546 | 547 |
|
547 | 548 |
/// \brief The subscript operator |
548 |
/// |
|
549 |
/// The subscript operator. |
|
550 | 549 |
Value operator[](Key key) const { |
551 | 550 |
return _graph.col(key); |
552 | 551 |
} |
553 | 552 |
|
554 | 553 |
private: |
555 | 554 |
const GridGraph& _graph; |
556 | 555 |
}; |
557 | 556 |
|
558 | 557 |
/// \brief Map to get the row of the nodes. |
559 | 558 |
/// |
560 | 559 |
/// Map to get the row of the nodes. |
561 | 560 |
class RowMap { |
562 | 561 |
public: |
563 | 562 |
/// \brief The key type of the map |
564 | 563 |
typedef GridGraph::Node Key; |
565 | 564 |
/// \brief The value type of the map |
566 | 565 |
typedef int Value; |
567 | 566 |
|
568 | 567 |
/// \brief Constructor |
569 |
/// |
|
570 |
/// Constructor |
|
571 | 568 |
RowMap(const GridGraph& graph) : _graph(graph) {} |
572 | 569 |
|
573 | 570 |
/// \brief The subscript operator |
574 |
/// |
|
575 |
/// The subscript operator. |
|
576 | 571 |
Value operator[](Key key) const { |
577 | 572 |
return _graph.row(key); |
578 | 573 |
} |
579 | 574 |
|
580 | 575 |
private: |
581 | 576 |
const GridGraph& _graph; |
582 | 577 |
}; |
583 | 578 |
|
584 | 579 |
/// \brief Constructor |
585 | 580 |
/// |
586 |
/// Construct a grid graph with given size. |
|
581 |
/// Construct a grid graph with the given size. |
|
587 | 582 |
GridGraph(int width, int height) { construct(width, height); } |
588 | 583 |
|
589 |
/// \brief |
|
584 |
/// \brief Resizes the graph |
|
590 | 585 |
/// |
591 |
/// Resize the graph. The function will fully destroy and rebuild |
|
592 |
/// the graph. This cause that the maps of the graph will |
|
593 |
/// reallocated automatically and the previous values will be |
|
594 |
/// lost. |
|
586 |
/// This function resizes the graph. It fully destroys and |
|
587 |
/// rebuilds the structure, therefore the maps of the graph will be |
|
588 |
/// reallocated automatically and the previous values will be lost. |
|
595 | 589 |
void resize(int width, int height) { |
596 | 590 |
Parent::notifier(Arc()).clear(); |
597 | 591 |
Parent::notifier(Edge()).clear(); |
598 | 592 |
Parent::notifier(Node()).clear(); |
599 | 593 |
construct(width, height); |
600 | 594 |
Parent::notifier(Node()).build(); |
601 | 595 |
Parent::notifier(Edge()).build(); |
602 | 596 |
Parent::notifier(Arc()).build(); |
603 | 597 |
} |
604 | 598 |
|
605 | 599 |
/// \brief The node on the given position. |
606 | 600 |
/// |
607 | 601 |
/// Gives back the node on the given position. |
608 | 602 |
Node operator()(int i, int j) const { |
609 | 603 |
return Parent::operator()(i, j); |
610 | 604 |
} |
611 | 605 |
|
612 |
/// \brief |
|
606 |
/// \brief The column index of the node. |
|
613 | 607 |
/// |
614 | 608 |
/// Gives back the column index of the node. |
615 | 609 |
int col(Node n) const { |
616 | 610 |
return Parent::col(n); |
617 | 611 |
} |
618 | 612 |
|
619 |
/// \brief |
|
613 |
/// \brief The row index of the node. |
|
620 | 614 |
/// |
621 | 615 |
/// Gives back the row index of the node. |
622 | 616 |
int row(Node n) const { |
623 | 617 |
return Parent::row(n); |
624 | 618 |
} |
625 | 619 |
|
626 |
/// \brief |
|
620 |
/// \brief The position of the node. |
|
627 | 621 |
/// |
628 | 622 |
/// Gives back the position of the node, ie. the <tt>(col,row)</tt> pair. |
629 | 623 |
dim2::Point<int> pos(Node n) const { |
630 | 624 |
return Parent::pos(n); |
631 | 625 |
} |
632 | 626 |
|
633 |
/// \brief |
|
627 |
/// \brief The number of the columns. |
|
634 | 628 |
/// |
635 | 629 |
/// Gives back the number of the columns. |
636 | 630 |
int width() const { |
637 | 631 |
return Parent::width(); |
638 | 632 |
} |
639 | 633 |
|
640 |
/// \brief |
|
634 |
/// \brief The number of the rows. |
|
641 | 635 |
/// |
642 | 636 |
/// Gives back the number of the rows. |
643 | 637 |
int height() const { |
644 | 638 |
return Parent::height(); |
645 | 639 |
} |
646 | 640 |
|
647 |
/// \brief |
|
641 |
/// \brief The arc goes right from the node. |
|
648 | 642 |
/// |
649 | 643 |
/// Gives back the arc goes right from the node. If there is not |
650 | 644 |
/// outgoing arc then it gives back INVALID. |
651 | 645 |
Arc right(Node n) const { |
652 | 646 |
return Parent::right(n); |
653 | 647 |
} |
654 | 648 |
|
655 |
/// \brief |
|
649 |
/// \brief The arc goes left from the node. |
|
656 | 650 |
/// |
657 | 651 |
/// Gives back the arc goes left from the node. If there is not |
658 | 652 |
/// outgoing arc then it gives back INVALID. |
659 | 653 |
Arc left(Node n) const { |
660 | 654 |
return Parent::left(n); |
661 | 655 |
} |
662 | 656 |
|
663 |
/// \brief |
|
657 |
/// \brief The arc goes up from the node. |
|
664 | 658 |
/// |
665 | 659 |
/// Gives back the arc goes up from the node. If there is not |
666 | 660 |
/// outgoing arc then it gives back INVALID. |
667 | 661 |
Arc up(Node n) const { |
668 | 662 |
return Parent::up(n); |
669 | 663 |
} |
670 | 664 |
|
671 |
/// \brief |
|
665 |
/// \brief The arc goes down from the node. |
|
672 | 666 |
/// |
673 | 667 |
/// Gives back the arc goes down from the node. If there is not |
674 | 668 |
/// outgoing arc then it gives back INVALID. |
675 | 669 |
Arc down(Node n) const { |
676 | 670 |
return Parent::down(n); |
677 | 671 |
} |
678 | 672 |
|
679 | 673 |
/// \brief Index map of the grid graph |
680 | 674 |
/// |
681 | 675 |
/// Just returns an IndexMap for the grid graph. |
682 | 676 |
IndexMap indexMap() const { |
683 | 677 |
return IndexMap(*this); |
684 | 678 |
} |
685 | 679 |
|
686 | 680 |
/// \brief Row map of the grid graph |
687 | 681 |
/// |
688 | 682 |
/// Just returns a RowMap for the grid graph. |
689 | 683 |
RowMap rowMap() const { |
690 | 684 |
return RowMap(*this); |
691 | 685 |
} |
692 | 686 |
|
693 | 687 |
/// \brief Column map of the grid graph |
694 | 688 |
/// |
695 | 689 |
/// Just returns a ColMap for the grid graph. |
696 | 690 |
ColMap colMap() const { |
697 | 691 |
return ColMap(*this); |
698 | 692 |
} |
699 | 693 |
|
700 | 694 |
}; |
701 | 695 |
|
702 | 696 |
} |
703 | 697 |
#endif |
... | ... |
@@ -237,143 +237,146 @@ |
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 |
/// This class implements a special graph type. The nodes of the graph |
|
286 |
/// are indiced with integers with at most \c dim binary digits. |
|
285 |
/// HypercubeGraph implements a special graph type. The nodes of the |
|
286 |
/// graph are indexed with integers having 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 |
/// This class is completely static and it needs constant memory space. |
|
290 |
/// Thus you can neither add nor delete nodes or edges. |
|
291 |
/// |
|
292 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept". |
|
293 |
/// Most of its member functions and nested classes are documented |
|
294 |
/// only in the concept class. |
|
289 | 295 |
/// |
290 | 296 |
/// \note The type of the indices is chosen to \c int for efficiency |
291 | 297 |
/// reasons. Thus the maximum dimension of this implementation is 26 |
292 | 298 |
/// (assuming that the size of \c int is 32 bit). |
293 |
/// |
|
294 |
/// This graph type fully conforms to the \ref concepts::Graph |
|
295 |
/// "Graph concept". |
|
296 | 299 |
class HypercubeGraph : public ExtendedHypercubeGraphBase { |
297 | 300 |
typedef ExtendedHypercubeGraphBase Parent; |
298 | 301 |
|
299 | 302 |
public: |
300 | 303 |
|
301 | 304 |
/// \brief Constructs a hypercube graph with \c dim dimensions. |
302 | 305 |
/// |
303 | 306 |
/// Constructs a hypercube graph with \c dim dimensions. |
304 | 307 |
HypercubeGraph(int dim) { construct(dim); } |
305 | 308 |
|
306 | 309 |
/// \brief The number of dimensions. |
307 | 310 |
/// |
308 | 311 |
/// Gives back the number of dimensions. |
309 | 312 |
int dimension() const { |
310 | 313 |
return Parent::dimension(); |
311 | 314 |
} |
312 | 315 |
|
313 | 316 |
/// \brief Returns \c true if the n'th bit of the node is one. |
314 | 317 |
/// |
315 | 318 |
/// Returns \c true if the n'th bit of the node is one. |
316 | 319 |
bool projection(Node node, int n) const { |
317 | 320 |
return Parent::projection(node, n); |
318 | 321 |
} |
319 | 322 |
|
320 | 323 |
/// \brief The dimension id of an edge. |
321 | 324 |
/// |
322 | 325 |
/// Gives back the dimension id of the given edge. |
323 |
/// It is in the [0..dim-1] |
|
326 |
/// It is in the range <tt>[0..dim-1]</tt>. |
|
324 | 327 |
int dimension(Edge edge) const { |
325 | 328 |
return Parent::dimension(edge); |
326 | 329 |
} |
327 | 330 |
|
328 | 331 |
/// \brief The dimension id of an arc. |
329 | 332 |
/// |
330 | 333 |
/// Gives back the dimension id of the given arc. |
331 |
/// It is in the [0..dim-1] |
|
334 |
/// It is in the range <tt>[0..dim-1]</tt>. |
|
332 | 335 |
int dimension(Arc arc) const { |
333 | 336 |
return Parent::dimension(arc); |
334 | 337 |
} |
335 | 338 |
|
336 | 339 |
/// \brief The index of a node. |
337 | 340 |
/// |
338 | 341 |
/// Gives back the index of the given node. |
339 | 342 |
/// The lower bits of the integer describes the node. |
340 | 343 |
int index(Node node) const { |
341 | 344 |
return Parent::index(node); |
342 | 345 |
} |
343 | 346 |
|
344 | 347 |
/// \brief Gives back a node by its index. |
345 | 348 |
/// |
346 | 349 |
/// Gives back a node by its index. |
347 | 350 |
Node operator()(int ix) const { |
348 | 351 |
return Parent::operator()(ix); |
349 | 352 |
} |
350 | 353 |
|
351 | 354 |
/// \brief Number of nodes. |
352 | 355 |
int nodeNum() const { return Parent::nodeNum(); } |
353 | 356 |
/// \brief Number of edges. |
354 | 357 |
int edgeNum() const { return Parent::edgeNum(); } |
355 | 358 |
/// \brief Number of arcs. |
356 | 359 |
int arcNum() const { return Parent::arcNum(); } |
357 | 360 |
|
358 | 361 |
/// \brief Linear combination map. |
359 | 362 |
/// |
360 | 363 |
/// This map makes possible to give back a linear combination |
361 | 364 |
/// for each node. It works like the \c std::accumulate function, |
362 | 365 |
/// so it accumulates the \c bf binary function with the \c fv first |
363 | 366 |
/// value. The map accumulates only on that positions (dimensions) |
364 | 367 |
/// where the index of the node is one. The values that have to be |
365 | 368 |
/// accumulated should be given by the \c begin and \c end iterators |
366 | 369 |
/// and the length of this range should be equal to the dimension |
367 | 370 |
/// number of the graph. |
368 | 371 |
/// |
369 | 372 |
///\code |
370 | 373 |
/// const int DIM = 3; |
371 | 374 |
/// HypercubeGraph graph(DIM); |
372 | 375 |
/// dim2::Point<double> base[DIM]; |
373 | 376 |
/// for (int k = 0; k < DIM; ++k) { |
374 | 377 |
/// base[k].x = rnd(); |
375 | 378 |
/// base[k].y = rnd(); |
376 | 379 |
/// } |
377 | 380 |
/// HypercubeGraph::HyperMap<dim2::Point<double> > |
378 | 381 |
/// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0)); |
379 | 382 |
///\endcode |
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 |
///\brief ListDigraph |
|
24 |
///\brief ListDigraph and 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; } |
... | ... |
@@ -266,325 +266,334 @@ |
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 |
///\ref ListDigraph is a simple and fast <em>directed graph</em> |
|
315 |
///implementation based on static linked lists that are stored in |
|
314 |
///\ref ListDigraph is a versatile and fast directed graph |
|
315 |
///implementation based on linked lists that are stored in |
|
316 | 316 |
///\c std::vector structures. |
317 | 317 |
/// |
318 |
///It conforms to the \ref concepts::Digraph "Digraph concept" and it |
|
319 |
///also provides several useful additional functionalities. |
|
320 |
/// |
|
318 |
///This type fully conforms to the \ref concepts::Digraph "Digraph concept" |
|
319 |
///and it also provides several useful additional functionalities. |
|
320 |
///Most of its member functions and nested classes are documented |
|
321 | 321 |
///only in the concept class. |
322 | 322 |
/// |
323 | 323 |
///\sa concepts::Digraph |
324 |
|
|
324 |
///\sa ListGraph |
|
325 | 325 |
class ListDigraph : public ExtendedListDigraphBase { |
326 | 326 |
typedef ExtendedListDigraphBase Parent; |
327 | 327 |
|
328 | 328 |
private: |
329 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
|
330 |
|
|
331 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
|
332 |
/// |
|
329 |
/// Digraphs are \e not copy constructible. Use DigraphCopy instead. |
|
333 | 330 |
ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {}; |
334 |
///\brief Assignment of ListDigraph to another one is \e not allowed. |
|
335 |
///Use copyDigraph() instead. |
|
336 |
|
|
337 |
///Assignment of ListDigraph to another one is \e not allowed. |
|
338 |
/// |
|
331 |
/// \brief Assignment of a digraph to another one is \e not allowed. |
|
332 |
/// Use DigraphCopy instead. |
|
339 | 333 |
void operator=(const ListDigraph &) {} |
340 | 334 |
public: |
341 | 335 |
|
342 | 336 |
/// Constructor |
343 | 337 |
|
344 | 338 |
/// Constructor. |
345 | 339 |
/// |
346 | 340 |
ListDigraph() {} |
347 | 341 |
|
348 | 342 |
///Add a new node to the digraph. |
349 | 343 |
|
350 |
/// |
|
344 |
///This function adds a new node to the digraph. |
|
351 | 345 |
///\return The new node. |
352 | 346 |
Node addNode() { return Parent::addNode(); } |
353 | 347 |
|
354 | 348 |
///Add a new arc to the digraph. |
355 | 349 |
|
356 |
/// |
|
350 |
///This function adds a new arc to the digraph with source node \c s |
|
357 | 351 |
///and target node \c t. |
358 | 352 |
///\return The new arc. |
359 |
Arc addArc( |
|
353 |
Arc addArc(Node s, Node t) { |
|
360 | 354 |
return Parent::addArc(s, t); |
361 | 355 |
} |
362 | 356 |
|
363 | 357 |
///\brief Erase a node from the digraph. |
364 | 358 |
/// |
365 |
///Erase a node from the digraph. |
|
366 |
/// |
|
367 |
|
|
359 |
///This function erases the given node from the digraph. |
|
360 |
void erase(Node n) { Parent::erase(n); } |
|
368 | 361 |
|
369 | 362 |
///\brief Erase an arc from the digraph. |
370 | 363 |
/// |
371 |
///Erase an arc from the digraph. |
|
372 |
/// |
|
373 |
|
|
364 |
///This function erases the given arc from the digraph. |
|
365 |
void erase(Arc a) { Parent::erase(a); } |
|
374 | 366 |
|
375 | 367 |
/// Node validity check |
376 | 368 |
|
377 |
/// This function gives back true if the given node is valid, |
|
378 |
/// ie. it is a real node of the graph. |
|
369 |
/// This function gives back \c true if the given node is valid, |
|
370 |
/// i.e. it is a real node of the digraph. |
|
379 | 371 |
/// |
380 |
/// \warning A Node pointing to a removed item |
|
381 |
/// could become valid again later if new nodes are |
|
382 |
/// |
|
372 |
/// \warning A removed node could become valid again if new nodes are |
|
373 |
/// added to the digraph. |
|
383 | 374 |
bool valid(Node n) const { return Parent::valid(n); } |
384 | 375 |
|
385 | 376 |
/// Arc validity check |
386 | 377 |
|
387 |
/// This function gives back true if the given arc is valid, |
|
388 |
/// ie. it is a real arc of the graph. |
|
378 |
/// This function gives back \c true if the given arc is valid, |
|
379 |
/// i.e. it is a real arc of the digraph. |
|
389 | 380 |
/// |
390 |
/// \warning An Arc pointing to a removed item |
|
391 |
/// could become valid again later if new nodes are |
|
392 |
/// |
|
381 |
/// \warning A removed arc could become valid again if new arcs are |
|
382 |
/// added to the digraph. |
|
393 | 383 |
bool valid(Arc a) const { return Parent::valid(a); } |
394 | 384 |
|
395 |
/// Change the target of |
|
385 |
/// Change the target node of an arc |
|
396 | 386 |
|
397 |
/// |
|
387 |
/// This function changes the target node of the given arc \c a to \c n. |
|
398 | 388 |
/// |
399 |
///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing |
|
400 |
///the changed arc remain valid. However <tt>InArcIt</tt>s are |
|
401 |
/// |
|
389 |
///\note \c ArcIt and \c OutArcIt iterators referencing the changed |
|
390 |
///arc remain valid, however \c InArcIt iterators are invalidated. |
|
402 | 391 |
/// |
403 | 392 |
///\warning This functionality cannot be used together with the Snapshot |
404 | 393 |
///feature. |
405 | 394 |
void changeTarget(Arc a, Node n) { |
406 | 395 |
Parent::changeTarget(a,n); |
407 | 396 |
} |
408 |
/// Change the source of |
|
397 |
/// Change the source node of an arc |
|
409 | 398 |
|
410 |
/// |
|
399 |
/// This function changes the source node of the given arc \c a to \c n. |
|
411 | 400 |
/// |
412 |
///\note The <tt>InArcIt</tt>s referencing the changed arc remain |
|
413 |
///valid. However the <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s are |
|
414 |
/// |
|
401 |
///\note \c InArcIt iterators referencing the changed arc remain |
|
402 |
///valid, however \c ArcIt and \c OutArcIt iterators are invalidated. |
|
415 | 403 |
/// |
416 | 404 |
///\warning This functionality cannot be used together with the Snapshot |
417 | 405 |
///feature. |
418 | 406 |
void changeSource(Arc a, Node n) { |
419 | 407 |
Parent::changeSource(a,n); |
420 | 408 |
} |
421 | 409 |
|
422 |
/// |
|
410 |
/// Reverse the direction of an arc. |
|
423 | 411 |
|
424 |
///\note The <tt>ArcIt</tt>s referencing the changed arc remain |
|
425 |
///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are |
|
426 |
/// |
|
412 |
/// This function reverses the direction of the given arc. |
|
413 |
///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing |
|
414 |
///the changed arc are invalidated. |
|
427 | 415 |
/// |
428 | 416 |
///\warning This functionality cannot be used together with the Snapshot |
429 | 417 |
///feature. |
430 |
void reverseArc(Arc e) { |
|
431 |
Node t=target(e); |
|
432 |
changeTarget(e,source(e)); |
|
433 |
changeSource(e,t); |
|
418 |
void reverseArc(Arc a) { |
|
419 |
Node t=target(a); |
|
420 |
changeTarget(a,source(a)); |
|
421 |
changeSource(a,t); |
|
434 | 422 |
} |
435 | 423 |
|
436 |
/// Reserve memory for nodes. |
|
437 |
|
|
438 |
/// Using this function it is possible to avoid the superfluous memory |
|
439 |
/// allocation: if you know that the digraph you want to build will |
|
440 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
|
441 |
/// then it is worth reserving space for this amount before starting |
|
442 |
/// to build the digraph. |
|
443 |
/// \sa reserveArc |
|
444 |
void reserveNode(int n) { nodes.reserve(n); }; |
|
445 |
|
|
446 |
/// Reserve memory for arcs. |
|
447 |
|
|
448 |
/// Using this function it is possible to avoid the superfluous memory |
|
449 |
/// allocation: if you know that the digraph you want to build will |
|
450 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
|
451 |
/// then it is worth reserving space for this amount before starting |
|
452 |
/// to build the digraph. |
|
453 |
/// \sa reserveNode |
|
454 |
void reserveArc(int m) { arcs.reserve(m); }; |
|
455 |
|
|
456 | 424 |
///Contract two nodes. |
457 | 425 |
|
458 |
///This function contracts two nodes. |
|
459 |
///Node \p b will be removed but instead of deleting |
|
460 |
///incident arcs, they will be joined to \p a. |
|
461 |
///The last parameter \p r controls whether to remove loops. \c true |
|
462 |
/// |
|
426 |
///This function contracts the given two nodes. |
|
427 |
///Node \c v is removed, but instead of deleting its |
|
428 |
///incident arcs, they are joined to node \c u. |
|
429 |
///If the last parameter \c r is \c true (this is the default value), |
|
430 |
///then the newly created loops are removed. |
|
463 | 431 |
/// |
464 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
|
465 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s |
|
466 |
/// |
|
432 |
///\note The moved arcs are joined to node \c u using changeSource() |
|
433 |
///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are |
|
434 |
///invalidated for the outgoing arcs of node \c v and \c InArcIt |
|
435 |
///iterators are invalidated for the incomming arcs of \c v. |
|
436 |
///Moreover all iterators referencing node \c v or the removed |
|
437 |
///loops are also invalidated. Other iterators remain valid. |
|
467 | 438 |
/// |
468 | 439 |
///\warning This functionality cannot be used together with the Snapshot |
469 | 440 |
///feature. |
470 |
void contract(Node |
|
441 |
void contract(Node u, Node v, bool r = true) |
|
471 | 442 |
{ |
472 |
for(OutArcIt e(*this, |
|
443 |
for(OutArcIt e(*this,v);e!=INVALID;) { |
|
473 | 444 |
OutArcIt f=e; |
474 | 445 |
++f; |
475 |
if(r && target(e)==a) erase(e); |
|
476 |
else changeSource(e,a); |
|
446 |
if(r && target(e)==u) erase(e); |
|
447 |
else changeSource(e,u); |
|
477 | 448 |
e=f; |
478 | 449 |
} |
479 |
for(InArcIt e(*this, |
|
450 |
for(InArcIt e(*this,v);e!=INVALID;) { |
|
480 | 451 |
InArcIt f=e; |
481 | 452 |
++f; |
482 |
if(r && source(e)==a) erase(e); |
|
483 |
else changeTarget(e,a); |
|
453 |
if(r && source(e)==u) erase(e); |
|
454 |
else changeTarget(e,u); |
|
484 | 455 |
e=f; |
485 | 456 |
} |
486 |
erase( |
|
457 |
erase(v); |
|
487 | 458 |
} |
488 | 459 |
|
489 | 460 |
///Split a node. |
490 | 461 |
|
491 |
///This function splits a node. First a new node is added to the digraph, |
|
492 |
///then the source of each outgoing arc of \c n is moved to this new node. |
|
493 |
///If \c connect is \c true (this is the default value), then a new arc |
|
494 |
///from \c n to the newly created node is also added. |
|
462 |
///This function splits the given node. First, a new node is added |
|
463 |
///to the digraph, then the source of each outgoing arc of node \c n |
|
464 |
///is moved to this new node. |
|
465 |
///If the second parameter \c connect is \c true (this is the default |
|
466 |
///value), then a new arc from node \c n to the newly created node |
|
467 |
///is also added. |
|
495 | 468 |
///\return The newly created node. |
496 | 469 |
/// |
497 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
|
498 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may |
|
499 |
/// |
|
470 |
///\note \c ArcIt and \c OutArcIt iterators referencing the outgoing |
|
471 |
///arcs of node \c n are invalidated. Other iterators remain valid. |
|
500 | 472 |
/// |
501 |
///\warning This functionality cannot be used |
|
473 |
///\warning This functionality cannot be used together with the |
|
502 | 474 |
///Snapshot feature. |
503 | 475 |
Node split(Node n, bool connect = true) { |
504 | 476 |
Node b = addNode(); |
505 | 477 |
for(OutArcIt e(*this,n);e!=INVALID;) { |
506 | 478 |
OutArcIt f=e; |
507 | 479 |
++f; |
508 | 480 |
changeSource(e,b); |
509 | 481 |
e=f; |
510 | 482 |
} |
511 | 483 |
if (connect) addArc(n,b); |
512 | 484 |
return b; |
513 | 485 |
} |
514 | 486 |
|
515 | 487 |
///Split an arc. |
516 | 488 |
|
517 |
///This function splits an arc. First a new node \c b is added to |
|
518 |
///the digraph, then the original arc is re-targeted to \c |
|
519 |
/// |
|
489 |
///This function splits the given arc. First, a new node \c v is |
|
490 |
///added to the digraph, then the target node of the original arc |
|
491 |
///is set to \c v. Finally, an arc from \c v to the original target |
|
492 |
///is added. |
|
493 |
///\return The newly created node. |
|
520 | 494 |
/// |
521 |
///\ |
|
495 |
///\note \c InArcIt iterators referencing the original arc are |
|
496 |
///invalidated. Other iterators remain valid. |
|
522 | 497 |
/// |
523 | 498 |
///\warning This functionality cannot be used together with the |
524 | 499 |
///Snapshot feature. |
525 |
Node split(Arc e) { |
|
526 |
Node b = addNode(); |
|
527 |
addArc(b,target(e)); |
|
528 |
changeTarget(e,b); |
|
529 |
|
|
500 |
Node split(Arc a) { |
|
501 |
Node v = addNode(); |
|
502 |
addArc(v,target(a)); |
|
503 |
changeTarget(a,v); |
|
504 |
return v; |
|
530 | 505 |
} |
531 | 506 |
|
507 |
///Clear the digraph. |
|
508 |
|
|
509 |
///This function erases all nodes and arcs from the digraph. |
|
510 |
/// |
|
511 |
void clear() { |
|
512 |
Parent::clear(); |
|
513 |
} |
|
514 |
|
|
515 |
/// Reserve memory for nodes. |
|
516 |
|
|
517 |
/// Using this function, it is possible to avoid superfluous memory |
|
518 |
/// allocation: if you know that the digraph you want to build will |
|
519 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
|
520 |
/// then it is worth reserving space for this amount before starting |
|
521 |
/// to build the digraph. |
|
522 |
/// \sa reserveArc() |
|
523 |
void reserveNode(int n) { nodes.reserve(n); }; |
|
524 |
|
|
525 |
/// Reserve memory for arcs. |
|
526 |
|
|
527 |
/// Using this function, it is possible to avoid superfluous memory |
|
528 |
/// allocation: if you know that the digraph you want to build will |
|
529 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
|
530 |
/// then it is worth reserving space for this amount before starting |
|
531 |
/// to build the digraph. |
|
532 |
/// \sa reserveNode() |
|
533 |
void reserveArc(int m) { arcs.reserve(m); }; |
|
534 |
|
|
532 | 535 |
/// \brief Class to make a snapshot of the digraph and restore |
533 | 536 |
/// it later. |
534 | 537 |
/// |
535 | 538 |
/// Class to make a snapshot of the digraph and restore it later. |
536 | 539 |
/// |
537 | 540 |
/// The newly added nodes and arcs can be removed using the |
538 | 541 |
/// restore() function. |
539 | 542 |
/// |
540 |
/// \warning Arc and node deletions and other modifications (e.g. |
|
541 |
/// contracting, splitting, reversing arcs or nodes) cannot be |
|
543 |
/// \note After a state is restored, you cannot restore a later state, |
|
544 |
/// i.e. you cannot add the removed nodes and arcs again using |
|
545 |
/// another Snapshot instance. |
|
546 |
/// |
|
547 |
/// \warning Node and arc deletions and other modifications (e.g. |
|
548 |
/// reversing, contracting, splitting arcs or nodes) cannot be |
|
542 | 549 |
/// restored. These events invalidate the snapshot. |
550 |
/// However the arcs and nodes that were added to the digraph after |
|
551 |
/// making the current snapshot can be removed without invalidating it. |
|
543 | 552 |
class Snapshot { |
544 | 553 |
protected: |
545 | 554 |
|
546 | 555 |
typedef Parent::NodeNotifier NodeNotifier; |
547 | 556 |
|
548 | 557 |
class NodeObserverProxy : public NodeNotifier::ObserverBase { |
549 | 558 |
public: |
550 | 559 |
|
551 | 560 |
NodeObserverProxy(Snapshot& _snapshot) |
552 | 561 |
: snapshot(_snapshot) {} |
553 | 562 |
|
554 | 563 |
using NodeNotifier::ObserverBase::attach; |
555 | 564 |
using NodeNotifier::ObserverBase::detach; |
556 | 565 |
using NodeNotifier::ObserverBase::attached; |
557 | 566 |
|
558 | 567 |
protected: |
559 | 568 |
|
560 | 569 |
virtual void add(const Node& node) { |
561 | 570 |
snapshot.addNode(node); |
562 | 571 |
} |
563 | 572 |
virtual void add(const std::vector<Node>& nodes) { |
564 | 573 |
for (int i = nodes.size() - 1; i >= 0; ++i) { |
565 | 574 |
snapshot.addNode(nodes[i]); |
566 | 575 |
} |
567 | 576 |
} |
568 | 577 |
virtual void erase(const Node& node) { |
569 | 578 |
snapshot.eraseNode(node); |
570 | 579 |
} |
571 | 580 |
virtual void erase(const std::vector<Node>& nodes) { |
572 | 581 |
for (int i = 0; i < int(nodes.size()); ++i) { |
573 | 582 |
snapshot.eraseNode(nodes[i]); |
574 | 583 |
} |
575 | 584 |
} |
576 | 585 |
virtual void build() { |
577 | 586 |
Node node; |
578 | 587 |
std::vector<Node> nodes; |
579 | 588 |
for (notifier()->first(node); node != INVALID; |
580 | 589 |
notifier()->next(node)) { |
581 | 590 |
nodes.push_back(node); |
582 | 591 |
} |
583 | 592 |
for (int i = nodes.size() - 1; i >= 0; --i) { |
584 | 593 |
snapshot.addNode(nodes[i]); |
585 | 594 |
} |
586 | 595 |
} |
587 | 596 |
virtual void clear() { |
588 | 597 |
Node node; |
589 | 598 |
for (notifier()->first(node); node != INVALID; |
590 | 599 |
notifier()->next(node)) { |
... | ... |
@@ -664,237 +673,229 @@ |
664 | 673 |
clear(); |
665 | 674 |
arc_observer_proxy.detach(); |
666 | 675 |
throw NodeNotifier::ImmediateDetach(); |
667 | 676 |
} else { |
668 | 677 |
added_nodes.erase(it); |
669 | 678 |
} |
670 | 679 |
} |
671 | 680 |
|
672 | 681 |
void addArc(const Arc& arc) { |
673 | 682 |
added_arcs.push_front(arc); |
674 | 683 |
} |
675 | 684 |
void eraseArc(const Arc& arc) { |
676 | 685 |
std::list<Arc>::iterator it = |
677 | 686 |
std::find(added_arcs.begin(), added_arcs.end(), arc); |
678 | 687 |
if (it == added_arcs.end()) { |
679 | 688 |
clear(); |
680 | 689 |
node_observer_proxy.detach(); |
681 | 690 |
throw ArcNotifier::ImmediateDetach(); |
682 | 691 |
} else { |
683 | 692 |
added_arcs.erase(it); |
684 | 693 |
} |
685 | 694 |
} |
686 | 695 |
|
687 | 696 |
void attach(ListDigraph &_digraph) { |
688 | 697 |
digraph = &_digraph; |
689 | 698 |
node_observer_proxy.attach(digraph->notifier(Node())); |
690 | 699 |
arc_observer_proxy.attach(digraph->notifier(Arc())); |
691 | 700 |
} |
692 | 701 |
|
693 | 702 |
void detach() { |
694 | 703 |
node_observer_proxy.detach(); |
695 | 704 |
arc_observer_proxy.detach(); |
696 | 705 |
} |
697 | 706 |
|
698 | 707 |
bool attached() const { |
699 | 708 |
return node_observer_proxy.attached(); |
700 | 709 |
} |
701 | 710 |
|
702 | 711 |
void clear() { |
703 | 712 |
added_nodes.clear(); |
704 | 713 |
added_arcs.clear(); |
705 | 714 |
} |
706 | 715 |
|
707 | 716 |
public: |
708 | 717 |
|
709 | 718 |
/// \brief Default constructor. |
710 | 719 |
/// |
711 | 720 |
/// Default constructor. |
712 |
/// |
|
721 |
/// You have to call save() to actually make a snapshot. |
|
713 | 722 |
Snapshot() |
714 | 723 |
: digraph(0), node_observer_proxy(*this), |
715 | 724 |
arc_observer_proxy(*this) {} |
716 | 725 |
|
717 | 726 |
/// \brief Constructor that immediately makes a snapshot. |
718 | 727 |
/// |
719 |
/// This constructor immediately makes a snapshot of the digraph. |
|
720 |
/// \param _digraph The digraph we make a snapshot of. |
|
721 |
|
|
728 |
/// This constructor immediately makes a snapshot of the given digraph. |
|
729 |
Snapshot(ListDigraph &gr) |
|
722 | 730 |
: node_observer_proxy(*this), |
723 | 731 |
arc_observer_proxy(*this) { |
724 |
attach( |
|
732 |
attach(gr); |
|
725 | 733 |
} |
726 | 734 |
|
727 | 735 |
/// \brief Make a snapshot. |
728 | 736 |
/// |
729 |
/// Make a snapshot of the digraph. |
|
730 |
/// |
|
731 |
/// This function |
|
737 |
/// This function makes a snapshot of the given digraph. |
|
738 |
/// It can be called more than once. In case of a repeated |
|
732 | 739 |
/// call, the previous snapshot gets lost. |
733 |
/// \param _digraph The digraph we make the snapshot of. |
|
734 |
void save(ListDigraph &_digraph) { |
|
740 |
void save(ListDigraph &gr) { |
|
735 | 741 |
if (attached()) { |
736 | 742 |
detach(); |
737 | 743 |
clear(); |
738 | 744 |
} |
739 |
attach( |
|
745 |
attach(gr); |
|
740 | 746 |
} |
741 | 747 |
|
742 | 748 |
/// \brief Undo the changes until the last snapshot. |
743 |
// |
|
744 |
/// Undo the changes until the last snapshot created by save(). |
|
749 |
/// |
|
750 |
/// This function undos the changes until the last snapshot |
|
751 |
/// created by save() or Snapshot(ListDigraph&). |
|
745 | 752 |
void restore() { |
746 | 753 |
detach(); |
747 | 754 |
for(std::list<Arc>::iterator it = added_arcs.begin(); |
748 | 755 |
it != added_arcs.end(); ++it) { |
749 | 756 |
digraph->erase(*it); |
750 | 757 |
} |
751 | 758 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
752 | 759 |
it != added_nodes.end(); ++it) { |
753 | 760 |
digraph->erase(*it); |
754 | 761 |
} |
755 | 762 |
clear(); |
756 | 763 |
} |
757 | 764 |
|
758 |
/// \brief |
|
765 |
/// \brief Returns \c true if the snapshot is valid. |
|
759 | 766 |
/// |
760 |
/// |
|
767 |
/// This function returns \c true if the snapshot is valid. |
|
761 | 768 |
bool valid() const { |
762 | 769 |
return attached(); |
763 | 770 |
} |
764 | 771 |
}; |
765 | 772 |
|
766 | 773 |
}; |
767 | 774 |
|
768 | 775 |
///@} |
769 | 776 |
|
770 | 777 |
class ListGraphBase { |
771 | 778 |
|
772 | 779 |
protected: |
773 | 780 |
|
774 | 781 |
struct NodeT { |
775 | 782 |
int first_out; |
776 | 783 |
int prev, next; |
777 | 784 |
}; |
778 | 785 |
|
779 | 786 |
struct ArcT { |
780 | 787 |
int target; |
781 | 788 |
int prev_out, next_out; |
782 | 789 |
}; |
783 | 790 |
|
784 | 791 |
std::vector<NodeT> nodes; |
785 | 792 |
|
786 | 793 |
int first_node; |
787 | 794 |
|
788 | 795 |
int first_free_node; |
789 | 796 |
|
790 | 797 |
std::vector<ArcT> arcs; |
791 | 798 |
|
792 | 799 |
int first_free_arc; |
793 | 800 |
|
794 | 801 |
public: |
795 | 802 |
|
796 | 803 |
typedef ListGraphBase Graph; |
797 | 804 |
|
798 |
class Node; |
|
799 |
class Arc; |
|
800 |
class Edge; |
|
801 |
|
|
802 | 805 |
class Node { |
803 | 806 |
friend class ListGraphBase; |
804 | 807 |
protected: |
805 | 808 |
|
806 | 809 |
int id; |
807 | 810 |
explicit Node(int pid) { id = pid;} |
808 | 811 |
|
809 | 812 |
public: |
810 | 813 |
Node() {} |
811 | 814 |
Node (Invalid) { id = -1; } |
812 | 815 |
bool operator==(const Node& node) const {return id == node.id;} |
813 | 816 |
bool operator!=(const Node& node) const {return id != node.id;} |
814 | 817 |
bool operator<(const Node& node) const {return id < node.id;} |
815 | 818 |
}; |
816 | 819 |
|
817 | 820 |
class Edge { |
818 | 821 |
friend class ListGraphBase; |
819 | 822 |
protected: |
820 | 823 |
|
821 | 824 |
int id; |
822 | 825 |
explicit Edge(int pid) { id = pid;} |
823 | 826 |
|
824 | 827 |
public: |
825 | 828 |
Edge() {} |
826 | 829 |
Edge (Invalid) { id = -1; } |
827 | 830 |
bool operator==(const Edge& edge) const {return id == edge.id;} |
828 | 831 |
bool operator!=(const Edge& edge) const {return id != edge.id;} |
829 | 832 |
bool operator<(const Edge& edge) const {return id < edge.id;} |
830 | 833 |
}; |
831 | 834 |
|
832 | 835 |
class Arc { |
833 | 836 |
friend class ListGraphBase; |
834 | 837 |
protected: |
835 | 838 |
|
836 | 839 |
int id; |
837 | 840 |
explicit Arc(int pid) { id = pid;} |
838 | 841 |
|
839 | 842 |
public: |
840 | 843 |
operator Edge() const { |
841 | 844 |
return id != -1 ? edgeFromId(id / 2) : INVALID; |
842 | 845 |
} |
843 | 846 |
|
844 | 847 |
Arc() {} |
845 | 848 |
Arc (Invalid) { id = -1; } |
846 | 849 |
bool operator==(const Arc& arc) const {return id == arc.id;} |
847 | 850 |
bool operator!=(const Arc& arc) const {return id != arc.id;} |
848 | 851 |
bool operator<(const Arc& arc) const {return id < arc.id;} |
849 | 852 |
}; |
850 | 853 |
|
851 |
|
|
852 |
|
|
853 | 854 |
ListGraphBase() |
854 | 855 |
: nodes(), first_node(-1), |
855 | 856 |
first_free_node(-1), arcs(), first_free_arc(-1) {} |
856 | 857 |
|
857 | 858 |
|
858 | 859 |
int maxNodeId() const { return nodes.size()-1; } |
859 | 860 |
int maxEdgeId() const { return arcs.size() / 2 - 1; } |
860 | 861 |
int maxArcId() const { return arcs.size()-1; } |
861 | 862 |
|
862 | 863 |
Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); } |
863 | 864 |
Node target(Arc e) const { return Node(arcs[e.id].target); } |
864 | 865 |
|
865 | 866 |
Node u(Edge e) const { return Node(arcs[2 * e.id].target); } |
866 | 867 |
Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); } |
867 | 868 |
|
868 | 869 |
static bool direction(Arc e) { |
869 | 870 |
return (e.id & 1) == 1; |
870 | 871 |
} |
871 | 872 |
|
872 | 873 |
static Arc direct(Edge e, bool d) { |
873 | 874 |
return Arc(e.id * 2 + (d ? 1 : 0)); |
874 | 875 |
} |
875 | 876 |
|
876 | 877 |
void first(Node& node) const { |
877 | 878 |
node.id = first_node; |
878 | 879 |
} |
879 | 880 |
|
880 | 881 |
void next(Node& node) const { |
881 | 882 |
node.id = nodes[node.id].next; |
882 | 883 |
} |
883 | 884 |
|
884 | 885 |
void first(Arc& e) const { |
885 | 886 |
int n = first_node; |
886 | 887 |
while (n != -1 && nodes[n].first_out == -1) { |
887 | 888 |
n = nodes[n].next; |
888 | 889 |
} |
889 | 890 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
890 | 891 |
} |
891 | 892 |
|
892 | 893 |
void next(Arc& e) const { |
893 | 894 |
if (arcs[e.id].next_out != -1) { |
894 | 895 |
e.id = arcs[e.id].next_out; |
895 | 896 |
} else { |
896 | 897 |
int n = nodes[arcs[e.id ^ 1].target].next; |
897 | 898 |
while(n != -1 && nodes[n].first_out == -1) { |
898 | 899 |
n = nodes[n].next; |
899 | 900 |
} |
900 | 901 |
e.id = (n == -1) ? -1 : nodes[n].first_out; |
... | ... |
@@ -1119,251 +1120,259 @@ |
1119 | 1120 |
arcs[arcs[2 * e.id].prev_out].next_out = |
1120 | 1121 |
arcs[2 * e.id].next_out; |
1121 | 1122 |
} else { |
1122 | 1123 |
nodes[arcs[(2 * e.id) | 1].target].first_out = |
1123 | 1124 |
arcs[2 * e.id].next_out; |
1124 | 1125 |
} |
1125 | 1126 |
|
1126 | 1127 |
if (nodes[n.id].first_out != -1) { |
1127 | 1128 |
arcs[nodes[n.id].first_out].prev_out = 2 * e.id; |
1128 | 1129 |
} |
1129 | 1130 |
arcs[(2 * e.id) | 1].target = n.id; |
1130 | 1131 |
arcs[2 * e.id].prev_out = -1; |
1131 | 1132 |
arcs[2 * e.id].next_out = nodes[n.id].first_out; |
1132 | 1133 |
nodes[n.id].first_out = 2 * e.id; |
1133 | 1134 |
} |
1134 | 1135 |
|
1135 | 1136 |
void changeU(Edge e, Node n) { |
1136 | 1137 |
if(arcs[(2 * e.id) | 1].next_out != -1) { |
1137 | 1138 |
arcs[arcs[(2 * e.id) | 1].next_out].prev_out = |
1138 | 1139 |
arcs[(2 * e.id) | 1].prev_out; |
1139 | 1140 |
} |
1140 | 1141 |
if(arcs[(2 * e.id) | 1].prev_out != -1) { |
1141 | 1142 |
arcs[arcs[(2 * e.id) | 1].prev_out].next_out = |
1142 | 1143 |
arcs[(2 * e.id) | 1].next_out; |
1143 | 1144 |
} else { |
1144 | 1145 |
nodes[arcs[2 * e.id].target].first_out = |
1145 | 1146 |
arcs[(2 * e.id) | 1].next_out; |
1146 | 1147 |
} |
1147 | 1148 |
|
1148 | 1149 |
if (nodes[n.id].first_out != -1) { |
1149 | 1150 |
arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
1150 | 1151 |
} |
1151 | 1152 |
arcs[2 * e.id].target = n.id; |
1152 | 1153 |
arcs[(2 * e.id) | 1].prev_out = -1; |
1153 | 1154 |
arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out; |
1154 | 1155 |
nodes[n.id].first_out = ((2 * e.id) | 1); |
1155 | 1156 |
} |
1156 | 1157 |
|
1157 | 1158 |
}; |
1158 | 1159 |
|
1159 | 1160 |
typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
1160 | 1161 |
|
1161 | 1162 |
|
1162 | 1163 |
/// \addtogroup graphs |
1163 | 1164 |
/// @{ |
1164 | 1165 |
|
1165 | 1166 |
///A general undirected graph structure. |
1166 | 1167 |
|
1167 |
///\ref ListGraph is a simple and fast <em>undirected graph</em> |
|
1168 |
///implementation based on static linked lists that are stored in |
|
1168 |
///\ref ListGraph is a versatile and fast undirected graph |
|
1169 |
///implementation based on linked lists that are stored in |
|
1169 | 1170 |
///\c std::vector structures. |
1170 | 1171 |
/// |
1171 |
///It conforms to the \ref concepts::Graph "Graph concept" and it |
|
1172 |
///also provides several useful additional functionalities. |
|
1173 |
/// |
|
1172 |
///This type fully conforms to the \ref concepts::Graph "Graph concept" |
|
1173 |
///and it also provides several useful additional functionalities. |
|
1174 |
///Most of its member functions and nested classes are documented |
|
1174 | 1175 |
///only in the concept class. |
1175 | 1176 |
/// |
1176 | 1177 |
///\sa concepts::Graph |
1177 |
|
|
1178 |
///\sa ListDigraph |
|
1178 | 1179 |
class ListGraph : public ExtendedListGraphBase { |
1179 | 1180 |
typedef ExtendedListGraphBase Parent; |
1180 | 1181 |
|
1181 | 1182 |
private: |
1182 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
|
1183 |
|
|
1184 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
|
1185 |
/// |
|
1183 |
/// Graphs are \e not copy constructible. Use GraphCopy instead. |
|
1186 | 1184 |
ListGraph(const ListGraph &) :ExtendedListGraphBase() {}; |
1187 |
///\brief Assignment of ListGraph to another one is \e not allowed. |
|
1188 |
///Use copyGraph() instead. |
|
1189 |
|
|
1190 |
///Assignment of ListGraph to another one is \e not allowed. |
|
1191 |
/// |
|
1185 |
/// \brief Assignment of a graph to another one is \e not allowed. |
|
1186 |
/// Use GraphCopy instead. |
|
1192 | 1187 |
void operator=(const ListGraph &) {} |
1193 | 1188 |
public: |
1194 | 1189 |
/// Constructor |
1195 | 1190 |
|
1196 | 1191 |
/// Constructor. |
1197 | 1192 |
/// |
1198 | 1193 |
ListGraph() {} |
1199 | 1194 |
|
1200 | 1195 |
typedef Parent::OutArcIt IncEdgeIt; |
1201 | 1196 |
|
1202 | 1197 |
/// \brief Add a new node to the graph. |
1203 | 1198 |
/// |
1204 |
/// |
|
1199 |
/// This function adds a new node to the graph. |
|
1205 | 1200 |
/// \return The new node. |
1206 | 1201 |
Node addNode() { return Parent::addNode(); } |
1207 | 1202 |
|
1208 | 1203 |
/// \brief Add a new edge to the graph. |
1209 | 1204 |
/// |
1210 |
/// Add a new edge to the graph with source node \c s |
|
1211 |
/// and target node \c t. |
|
1205 |
/// This function adds a new edge to the graph between nodes |
|
1206 |
/// \c u and \c v with inherent orientation from node \c u to |
|
1207 |
/// node \c v. |
|
1212 | 1208 |
/// \return The new edge. |
1213 |
Edge addEdge(const Node& s, const Node& t) { |
|
1214 |
return Parent::addEdge(s, t); |
|
1209 |
Edge addEdge(Node u, Node v) { |
|
1210 |
return Parent::addEdge(u, v); |
|
1215 | 1211 |
} |
1216 | 1212 |
|
1217 |
/// |
|
1213 |
///\brief Erase a node from the graph. |
|
1218 | 1214 |
/// |
1219 |
/// |
|
1215 |
/// This function erases the given node from the graph. |
|
1216 |
void erase(Node n) { Parent::erase(n); } |
|
1217 |
|
|
1218 |
///\brief Erase an edge from the graph. |
|
1220 | 1219 |
/// |
1221 |
void erase(const Node& n) { Parent::erase(n); } |
|
1222 |
|
|
1223 |
/// \brief Erase an edge from the graph. |
|
1224 |
/// |
|
1225 |
/// Erase an edge from the graph. |
|
1226 |
/// |
|
1227 |
|
|
1220 |
/// This function erases the given edge from the graph. |
|
1221 |
void erase(Edge e) { Parent::erase(e); } |
|
1228 | 1222 |
/// Node validity check |
1229 | 1223 |
|
1230 |
/// This function gives back true if the given node is valid, |
|
1231 |
/// ie. it is a real node of the graph. |
|
1224 |
/// This function gives back \c true if the given node is valid, |
|
1225 |
/// i.e. it is a real node of the graph. |
|
1232 | 1226 |
/// |
1233 |
/// \warning A Node pointing to a removed item |
|
1234 |
/// could become valid again later if new nodes are |
|
1227 |
/// \warning A removed node could become valid again if new nodes are |
|
1235 | 1228 |
/// added to the graph. |
1236 | 1229 |
bool valid(Node n) const { return Parent::valid(n); } |
1230 |
/// Edge validity check |
|
1231 |
|
|
1232 |
/// This function gives back \c true if the given edge is valid, |
|
1233 |
/// i.e. it is a real edge of the graph. |
|
1234 |
/// |
|
1235 |
/// \warning A removed edge could become valid again if new edges are |
|
1236 |
/// added to the graph. |
|
1237 |
bool valid(Edge e) const { return Parent::valid(e); } |
|
1237 | 1238 |
/// Arc validity check |
1238 | 1239 |
|
1239 |
/// This function gives back true if the given arc is valid, |
|
1240 |
/// ie. it is a real arc of the graph. |
|
1240 |
/// This function gives back \c true if the given arc is valid, |
|
1241 |
/// i.e. it is a real arc of the graph. |
|
1241 | 1242 |
/// |
1242 |
/// \warning An Arc pointing to a removed item |
|
1243 |
/// could become valid again later if new edges are |
|
1243 |
/// \warning A removed arc could become valid again if new edges are |
|
1244 | 1244 |
/// added to the graph. |
1245 | 1245 |
bool valid(Arc a) const { return Parent::valid(a); } |
1246 |
/// Edge validity check |
|
1247 | 1246 |
|
1248 |
/// This function gives back true if the given edge is valid, |
|
1249 |
/// ie. it is a real arc of the graph. |
|
1247 |
/// \brief Change the first node of an edge. |
|
1250 | 1248 |
/// |
1251 |
/// \warning A Edge pointing to a removed item |
|
1252 |
/// could become valid again later if new edges are |
|
1253 |
/// added to the graph. |
|
1254 |
bool valid(Edge e) const { return Parent::valid(e); } |
|
1255 |
/// |
|
1249 |
/// This function changes the first node of the given edge \c e to \c n. |
|
1256 | 1250 |
/// |
1257 |
/// This function changes the end \c u of \c e to node \c n. |
|
1258 |
/// |
|
1259 |
///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the |
|
1260 |
///changed edge are invalidated and if the changed node is the |
|
1261 |
///base node of an iterator then this iterator is also |
|
1262 |
///invalidated. |
|
1251 |
///\note \c EdgeIt and \c ArcIt iterators referencing the |
|
1252 |
///changed edge are invalidated and all other iterators whose |
|
1253 |
///base node is the changed node are also invalidated. |
|
1263 | 1254 |
/// |
1264 | 1255 |
///\warning This functionality cannot be used together with the |
1265 | 1256 |
///Snapshot feature. |
1266 | 1257 |
void changeU(Edge e, Node n) { |
1267 | 1258 |
Parent::changeU(e,n); |
1268 | 1259 |
} |
1269 |
/// \brief Change the |
|
1260 |
/// \brief Change the second node of an edge. |
|
1270 | 1261 |
/// |
1271 |
/// This function changes the |
|
1262 |
/// This function changes the second node of the given edge \c e to \c n. |
|
1272 | 1263 |
/// |
1273 |
///\note The <tt>EdgeIt</tt>s referencing the changed edge remain |
|
1274 |
///valid, however <tt>ArcIt</tt>s and if the changed node is the |
|
1275 |
/// |
|
1264 |
///\note \c EdgeIt iterators referencing the changed edge remain |
|
1265 |
///valid, however \c ArcIt iterators referencing the changed edge and |
|
1266 |
///all other iterators whose base node is the changed node are also |
|
1267 |
///invalidated. |
|
1276 | 1268 |
/// |
1277 | 1269 |
///\warning This functionality cannot be used together with the |
1278 | 1270 |
///Snapshot feature. |
1279 | 1271 |
void changeV(Edge e, Node n) { |
1280 | 1272 |
Parent::changeV(e,n); |
1281 | 1273 |
} |
1274 |
|
|
1282 | 1275 |
/// \brief Contract two nodes. |
1283 | 1276 |
/// |
1284 |
/// This function contracts two nodes. |
|
1285 |
/// Node \p b will be removed but instead of deleting |
|
1286 |
/// its neighboring arcs, they will be joined to \p a. |
|
1287 |
/// The last parameter \p r controls whether to remove loops. \c true |
|
1288 |
/// |
|
1277 |
/// This function contracts the given two nodes. |
|
1278 |
/// Node \c b is removed, but instead of deleting |
|
1279 |
/// its incident edges, they are joined to node \c a. |
|
1280 |
/// If the last parameter \c r is \c true (this is the default value), |
|
1281 |
/// then the newly created loops are removed. |
|
1289 | 1282 |
/// |
1290 |
/// \note The <tt>ArcIt</tt>s referencing a moved arc remain |
|
1291 |
/// valid. |
|
1283 |
/// \note The moved edges are joined to node \c a using changeU() |
|
1284 |
/// or changeV(), thus all edge and arc iterators whose base node is |
|
1285 |
/// \c b are invalidated. |
|
1286 |
/// Moreover all iterators referencing node \c b or the removed |
|
1287 |
/// loops are also invalidated. Other iterators remain valid. |
|
1292 | 1288 |
/// |
1293 | 1289 |
///\warning This functionality cannot be used together with the |
1294 | 1290 |
///Snapshot feature. |
1295 | 1291 |
void contract(Node a, Node b, bool r = true) { |
1296 | 1292 |
for(IncEdgeIt e(*this, b); e!=INVALID;) { |
1297 | 1293 |
IncEdgeIt f = e; ++f; |
1298 | 1294 |
if (r && runningNode(e) == a) { |
1299 | 1295 |
erase(e); |
1300 | 1296 |
} else if (u(e) == b) { |
1301 | 1297 |
changeU(e, a); |
1302 | 1298 |
} else { |
1303 | 1299 |
changeV(e, a); |
1304 | 1300 |
} |
1305 | 1301 |
e = f; |
1306 | 1302 |
} |
1307 | 1303 |
erase(b); |
1308 | 1304 |
} |
1309 | 1305 |
|
1306 |
///Clear the graph. |
|
1307 |
|
|
1308 |
///This function erases all nodes and arcs from the graph. |
|
1309 |
/// |
|
1310 |
void clear() { |
|
1311 |
Parent::clear(); |
|
1312 |
} |
|
1310 | 1313 |
|
1311 | 1314 |
/// \brief Class to make a snapshot of the graph and restore |
1312 | 1315 |
/// it later. |
1313 | 1316 |
/// |
1314 | 1317 |
/// Class to make a snapshot of the graph and restore it later. |
1315 | 1318 |
/// |
1316 | 1319 |
/// The newly added nodes and edges can be removed |
1317 | 1320 |
/// using the restore() function. |
1318 | 1321 |
/// |
1319 |
/// \warning Edge and node deletions and other modifications |
|
1320 |
/// (e.g. changing nodes of edges, contracting nodes) cannot be |
|
1321 |
/// restored |
|
1322 |
/// \note After a state is restored, you cannot restore a later state, |
|
1323 |
/// i.e. you cannot add the removed nodes and edges again using |
|
1324 |
/// another Snapshot instance. |
|
1325 |
/// |
|
1326 |
/// \warning Node and edge deletions and other modifications |
|
1327 |
/// (e.g. changing the end-nodes of edges or contracting nodes) |
|
1328 |
/// cannot be restored. These events invalidate the snapshot. |
|
1329 |
/// However the edges and nodes that were added to the graph after |
|
1330 |
/// making the current snapshot can be removed without invalidating it. |
|
1322 | 1331 |
class Snapshot { |
1323 | 1332 |
protected: |
1324 | 1333 |
|
1325 | 1334 |
typedef Parent::NodeNotifier NodeNotifier; |
1326 | 1335 |
|
1327 | 1336 |
class NodeObserverProxy : public NodeNotifier::ObserverBase { |
1328 | 1337 |
public: |
1329 | 1338 |
|
1330 | 1339 |
NodeObserverProxy(Snapshot& _snapshot) |
1331 | 1340 |
: snapshot(_snapshot) {} |
1332 | 1341 |
|
1333 | 1342 |
using NodeNotifier::ObserverBase::attach; |
1334 | 1343 |
using NodeNotifier::ObserverBase::detach; |
1335 | 1344 |
using NodeNotifier::ObserverBase::attached; |
1336 | 1345 |
|
1337 | 1346 |
protected: |
1338 | 1347 |
|
1339 | 1348 |
virtual void add(const Node& node) { |
1340 | 1349 |
snapshot.addNode(node); |
1341 | 1350 |
} |
1342 | 1351 |
virtual void add(const std::vector<Node>& nodes) { |
1343 | 1352 |
for (int i = nodes.size() - 1; i >= 0; ++i) { |
1344 | 1353 |
snapshot.addNode(nodes[i]); |
1345 | 1354 |
} |
1346 | 1355 |
} |
1347 | 1356 |
virtual void erase(const Node& node) { |
1348 | 1357 |
snapshot.eraseNode(node); |
1349 | 1358 |
} |
1350 | 1359 |
virtual void erase(const std::vector<Node>& nodes) { |
1351 | 1360 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1352 | 1361 |
snapshot.eraseNode(nodes[i]); |
1353 | 1362 |
} |
1354 | 1363 |
} |
1355 | 1364 |
virtual void build() { |
1356 | 1365 |
Node node; |
1357 | 1366 |
std::vector<Node> nodes; |
1358 | 1367 |
for (notifier()->first(node); node != INVALID; |
1359 | 1368 |
notifier()->next(node)) { |
1360 | 1369 |
nodes.push_back(node); |
1361 | 1370 |
} |
1362 | 1371 |
for (int i = nodes.size() - 1; i >= 0; --i) { |
1363 | 1372 |
snapshot.addNode(nodes[i]); |
1364 | 1373 |
} |
1365 | 1374 |
} |
1366 | 1375 |
virtual void clear() { |
1367 | 1376 |
Node node; |
1368 | 1377 |
for (notifier()->first(node); node != INVALID; |
1369 | 1378 |
notifier()->next(node)) { |
... | ... |
@@ -1443,108 +1452,106 @@ |
1443 | 1452 |
clear(); |
1444 | 1453 |
edge_observer_proxy.detach(); |
1445 | 1454 |
throw NodeNotifier::ImmediateDetach(); |
1446 | 1455 |
} else { |
1447 | 1456 |
added_nodes.erase(it); |
1448 | 1457 |
} |
1449 | 1458 |
} |
1450 | 1459 |
|
1451 | 1460 |
void addEdge(const Edge& edge) { |
1452 | 1461 |
added_edges.push_front(edge); |
1453 | 1462 |
} |
1454 | 1463 |
void eraseEdge(const Edge& edge) { |
1455 | 1464 |
std::list<Edge>::iterator it = |
1456 | 1465 |
std::find(added_edges.begin(), added_edges.end(), edge); |
1457 | 1466 |
if (it == added_edges.end()) { |
1458 | 1467 |
clear(); |
1459 | 1468 |
node_observer_proxy.detach(); |
1460 | 1469 |
throw EdgeNotifier::ImmediateDetach(); |
1461 | 1470 |
} else { |
1462 | 1471 |
added_edges.erase(it); |
1463 | 1472 |
} |
1464 | 1473 |
} |
1465 | 1474 |
|
1466 | 1475 |
void attach(ListGraph &_graph) { |
1467 | 1476 |
graph = &_graph; |
1468 | 1477 |
node_observer_proxy.attach(graph->notifier(Node())); |
1469 | 1478 |
edge_observer_proxy.attach(graph->notifier(Edge())); |
1470 | 1479 |
} |
1471 | 1480 |
|
1472 | 1481 |
void detach() { |
1473 | 1482 |
node_observer_proxy.detach(); |
1474 | 1483 |
edge_observer_proxy.detach(); |
1475 | 1484 |
} |
1476 | 1485 |
|
1477 | 1486 |
bool attached() const { |
1478 | 1487 |
return node_observer_proxy.attached(); |
1479 | 1488 |
} |
1480 | 1489 |
|
1481 | 1490 |
void clear() { |
1482 | 1491 |
added_nodes.clear(); |
1483 | 1492 |
added_edges.clear(); |
1484 | 1493 |
} |
1485 | 1494 |
|
1486 | 1495 |
public: |
1487 | 1496 |
|
1488 | 1497 |
/// \brief Default constructor. |
1489 | 1498 |
/// |
1490 | 1499 |
/// Default constructor. |
1491 |
/// |
|
1500 |
/// You have to call save() to actually make a snapshot. |
|
1492 | 1501 |
Snapshot() |
1493 | 1502 |
: graph(0), node_observer_proxy(*this), |
1494 | 1503 |
edge_observer_proxy(*this) {} |
1495 | 1504 |
|
1496 | 1505 |
/// \brief Constructor that immediately makes a snapshot. |
1497 | 1506 |
/// |
1498 |
/// This constructor immediately makes a snapshot of the graph. |
|
1499 |
/// \param _graph The graph we make a snapshot of. |
|
1500 |
|
|
1507 |
/// This constructor immediately makes a snapshot of the given graph. |
|
1508 |
Snapshot(ListGraph &gr) |
|
1501 | 1509 |
: node_observer_proxy(*this), |
1502 | 1510 |
edge_observer_proxy(*this) { |
1503 |
attach( |
|
1511 |
attach(gr); |
|
1504 | 1512 |
} |
1505 | 1513 |
|
1506 | 1514 |
/// \brief Make a snapshot. |
1507 | 1515 |
/// |
1508 |
/// Make a snapshot of the graph. |
|
1509 |
/// |
|
1510 |
/// This function |
|
1516 |
/// This function makes a snapshot of the given graph. |
|
1517 |
/// It can be called more than once. In case of a repeated |
|
1511 | 1518 |
/// call, the previous snapshot gets lost. |
1512 |
/// \param _graph The graph we make the snapshot of. |
|
1513 |
void save(ListGraph &_graph) { |
|
1519 |
void save(ListGraph &gr) { |
|
1514 | 1520 |
if (attached()) { |
1515 | 1521 |
detach(); |
1516 | 1522 |
clear(); |
1517 | 1523 |
} |
1518 |
attach( |
|
1524 |
attach(gr); |
|
1519 | 1525 |
} |
1520 | 1526 |
|
1521 | 1527 |
/// \brief Undo the changes until the last snapshot. |
1522 |
// |
|
1523 |
/// Undo the changes until the last snapshot created by save(). |
|
1528 |
/// |
|
1529 |
/// This function undos the changes until the last snapshot |
|
1530 |
/// created by save() or Snapshot(ListGraph&). |
|
1524 | 1531 |
void restore() { |
1525 | 1532 |
detach(); |
1526 | 1533 |
for(std::list<Edge>::iterator it = added_edges.begin(); |
1527 | 1534 |
it != added_edges.end(); ++it) { |
1528 | 1535 |
graph->erase(*it); |
1529 | 1536 |
} |
1530 | 1537 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
1531 | 1538 |
it != added_nodes.end(); ++it) { |
1532 | 1539 |
graph->erase(*it); |
1533 | 1540 |
} |
1534 | 1541 |
clear(); |
1535 | 1542 |
} |
1536 | 1543 |
|
1537 |
/// \brief |
|
1544 |
/// \brief Returns \c true if the snapshot is valid. |
|
1538 | 1545 |
/// |
1539 |
/// |
|
1546 |
/// This function returns \c true if the snapshot is valid. |
|
1540 | 1547 |
bool valid() const { |
1541 | 1548 |
return attached(); |
1542 | 1549 |
} |
1543 | 1550 |
}; |
1544 | 1551 |
}; |
1545 | 1552 |
|
1546 | 1553 |
/// @} |
1547 | 1554 |
} //namespace lemon |
1548 | 1555 |
|
1549 | 1556 |
|
1550 | 1557 |
#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 |
///Base of SmartDigraph |
|
36 | 35 |
|
37 |
///Base of SmartDigraph |
|
38 |
/// |
|
39 | 36 |
class SmartDigraphBase { |
40 | 37 |
protected: |
41 | 38 |
|
42 | 39 |
struct NodeT |
43 | 40 |
{ |
44 | 41 |
int first_in, first_out; |
45 | 42 |
NodeT() {} |
46 | 43 |
}; |
47 | 44 |
struct ArcT |
48 | 45 |
{ |
49 | 46 |
int target, source, next_in, next_out; |
50 | 47 |
ArcT() {} |
51 | 48 |
}; |
52 | 49 |
|
53 | 50 |
std::vector<NodeT> nodes; |
54 | 51 |
std::vector<ArcT> arcs; |
55 | 52 |
|
56 | 53 |
public: |
57 | 54 |
|
58 | 55 |
typedef SmartDigraphBase Digraph; |
59 | 56 |
|
60 | 57 |
class Node; |
61 | 58 |
class Arc; |
62 | 59 |
|
63 | 60 |
public: |
64 | 61 |
|
65 | 62 |
SmartDigraphBase() : nodes(), arcs() { } |
66 | 63 |
SmartDigraphBase(const SmartDigraphBase &_g) |
67 | 64 |
: nodes(_g.nodes), arcs(_g.arcs) { } |
68 | 65 |
|
69 | 66 |
typedef True NodeNumTag; |
70 | 67 |
typedef True ArcNumTag; |
71 | 68 |
|
72 | 69 |
int nodeNum() const { return nodes.size(); } |
73 | 70 |
int arcNum() const { return arcs.size(); } |
74 | 71 |
|
75 | 72 |
int maxNodeId() const { return nodes.size()-1; } |
76 | 73 |
int maxArcId() const { return arcs.size()-1; } |
77 | 74 |
|
78 | 75 |
Node addNode() { |
79 | 76 |
int n = nodes.size(); |
80 | 77 |
nodes.push_back(NodeT()); |
81 | 78 |
nodes[n].first_in = -1; |
82 | 79 |
nodes[n].first_out = -1; |
83 | 80 |
return Node(n); |
84 | 81 |
} |
85 | 82 |
|
86 | 83 |
Arc addArc(Node u, Node v) { |
... | ... |
@@ -142,299 +139,291 @@ |
142 | 139 |
public: |
143 | 140 |
Arc() { } |
144 | 141 |
Arc (Invalid) : _id(-1) {} |
145 | 142 |
bool operator==(const Arc i) const {return _id == i._id;} |
146 | 143 |
bool operator!=(const Arc i) const {return _id != i._id;} |
147 | 144 |
bool operator<(const Arc i) const {return _id < i._id;} |
148 | 145 |
}; |
149 | 146 |
|
150 | 147 |
void first(Node& node) const { |
151 | 148 |
node._id = nodes.size() - 1; |
152 | 149 |
} |
153 | 150 |
|
154 | 151 |
static void next(Node& node) { |
155 | 152 |
--node._id; |
156 | 153 |
} |
157 | 154 |
|
158 | 155 |
void first(Arc& arc) const { |
159 | 156 |
arc._id = arcs.size() - 1; |
160 | 157 |
} |
161 | 158 |
|
162 | 159 |
static void next(Arc& arc) { |
163 | 160 |
--arc._id; |
164 | 161 |
} |
165 | 162 |
|
166 | 163 |
void firstOut(Arc& arc, const Node& node) const { |
167 | 164 |
arc._id = nodes[node._id].first_out; |
168 | 165 |
} |
169 | 166 |
|
170 | 167 |
void nextOut(Arc& arc) const { |
171 | 168 |
arc._id = arcs[arc._id].next_out; |
172 | 169 |
} |
173 | 170 |
|
174 | 171 |
void firstIn(Arc& arc, const Node& node) const { |
175 | 172 |
arc._id = nodes[node._id].first_in; |
176 | 173 |
} |
177 | 174 |
|
178 | 175 |
void nextIn(Arc& arc) const { |
179 | 176 |
arc._id = arcs[arc._id].next_in; |
180 | 177 |
} |
181 | 178 |
|
182 | 179 |
}; |
183 | 180 |
|
184 | 181 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
185 | 182 |
|
186 | 183 |
///\ingroup graphs |
187 | 184 |
/// |
188 | 185 |
///\brief A smart directed graph class. |
189 | 186 |
/// |
190 |
///This is a simple and fast digraph implementation. |
|
191 |
///It is also quite memory efficient, but at the price |
|
192 |
///that <b> it does support only limited (only stack-like) |
|
193 |
///node and arc deletions</b>. |
|
194 |
/// |
|
187 |
///\ref SmartDigraph is a simple and fast digraph implementation. |
|
188 |
///It is also quite memory efficient but at the price |
|
189 |
///that it does not support node and arc deletion |
|
190 |
///(except for the Snapshot feature). |
|
195 | 191 |
/// |
196 |
///\ |
|
192 |
///This type fully conforms to the \ref concepts::Digraph "Digraph concept" |
|
193 |
///and it also provides some additional functionalities. |
|
194 |
///Most of its member functions and nested classes are documented |
|
195 |
///only in the concept class. |
|
196 |
/// |
|
197 |
///\sa concepts::Digraph |
|
198 |
///\sa SmartGraph |
|
197 | 199 |
class SmartDigraph : public ExtendedSmartDigraphBase { |
198 | 200 |
typedef ExtendedSmartDigraphBase Parent; |
199 | 201 |
|
200 | 202 |
private: |
201 |
|
|
202 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
|
203 |
|
|
204 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
|
205 |
/// |
|
203 |
/// Digraphs are \e not copy constructible. Use DigraphCopy instead. |
|
206 | 204 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {}; |
207 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
|
208 |
///Use DigraphCopy() instead. |
|
209 |
|
|
210 |
///Assignment of SmartDigraph to another one is \e not allowed. |
|
211 |
/// |
|
205 |
/// \brief Assignment of a digraph to another one is \e not allowed. |
|
206 |
/// Use DigraphCopy instead. |
|
212 | 207 |
void operator=(const SmartDigraph &) {} |
213 | 208 |
|
214 | 209 |
public: |
215 | 210 |
|
216 | 211 |
/// Constructor |
217 | 212 |
|
218 | 213 |
/// Constructor. |
219 | 214 |
/// |
220 | 215 |
SmartDigraph() {}; |
221 | 216 |
|
222 | 217 |
///Add a new node to the digraph. |
223 | 218 |
|
224 |
/// Add a new node to the digraph. |
|
225 |
/// \return The new node. |
|
219 |
///This function adds a new node to the digraph. |
|
220 |
///\return The new node. |
|
226 | 221 |
Node addNode() { return Parent::addNode(); } |
227 | 222 |
|
228 | 223 |
///Add a new arc to the digraph. |
229 | 224 |
|
230 |
/// |
|
225 |
///This function adds a new arc to the digraph with source node \c s |
|
231 | 226 |
///and target node \c t. |
232 | 227 |
///\return The new arc. |
233 |
Arc addArc( |
|
228 |
Arc addArc(Node s, Node t) { |
|
234 | 229 |
return Parent::addArc(s, t); |
235 | 230 |
} |
236 | 231 |
|
237 |
/// \brief Using this it is possible to avoid the superfluous memory |
|
238 |
/// allocation. |
|
239 |
|
|
240 |
/// Using this it is possible to avoid the superfluous memory |
|
241 |
/// allocation: if you know that the digraph you want to build will |
|
242 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
|
243 |
/// then it is worth reserving space for this amount before starting |
|
244 |
/// to build the digraph. |
|
245 |
/// \sa reserveArc |
|
246 |
void reserveNode(int n) { nodes.reserve(n); }; |
|
247 |
|
|
248 |
/// \brief Using this it is possible to avoid the superfluous memory |
|
249 |
/// allocation. |
|
250 |
|
|
251 |
/// Using this it is possible to avoid the superfluous memory |
|
252 |
/// allocation: if you know that the digraph you want to build will |
|
253 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
|
254 |
/// then it is worth reserving space for this amount before starting |
|
255 |
/// to build the digraph. |
|
256 |
/// \sa reserveNode |
|
257 |
void reserveArc(int m) { arcs.reserve(m); }; |
|
258 |
|
|
259 | 232 |
/// \brief Node validity check |
260 | 233 |
/// |
261 |
/// This function gives back true if the given node is valid, |
|
262 |
/// ie. it is a real node of the graph. |
|
234 |
/// This function gives back \c true if the given node is valid, |
|
235 |
/// i.e. it is a real node of the digraph. |
|
263 | 236 |
/// |
264 | 237 |
/// \warning A removed node (using Snapshot) could become valid again |
265 |
/// |
|
238 |
/// if new nodes are added to the digraph. |
|
266 | 239 |
bool valid(Node n) const { return Parent::valid(n); } |
267 | 240 |
|
268 | 241 |
/// \brief Arc validity check |
269 | 242 |
/// |
270 |
/// This function gives back true if the given arc is valid, |
|
271 |
/// ie. it is a real arc of the graph. |
|
243 |
/// This function gives back \c true if the given arc is valid, |
|
244 |
/// i.e. it is a real arc of the digraph. |
|
272 | 245 |
/// |
273 | 246 |
/// \warning A removed arc (using Snapshot) could become valid again |
274 |
/// |
|
247 |
/// if new arcs are added to the graph. |
|
275 | 248 |
bool valid(Arc a) const { return Parent::valid(a); } |
276 | 249 |
|
277 |
///Clear the digraph. |
|
278 |
|
|
279 |
///Erase all the nodes and arcs from the digraph. |
|
280 |
/// |
|
281 |
void clear() { |
|
282 |
Parent::clear(); |
|
283 |
} |
|
284 |
|
|
285 | 250 |
///Split a node. |
286 | 251 |
|
287 |
///This function splits a node. First a new node is added to the digraph, |
|
288 |
///then the source of each outgoing arc of \c n is moved to this new node. |
|
289 |
///If \c connect is \c true (this is the default value), then a new arc |
|
290 |
///from \c n to the newly created node is also added. |
|
252 |
///This function splits the given node. First, a new node is added |
|
253 |
///to the digraph, then the source of each outgoing arc of node \c n |
|
254 |
///is moved to this new node. |
|
255 |
///If the second parameter \c connect is \c true (this is the default |
|
256 |
///value), then a new arc from node \c n to the newly created node |
|
257 |
///is also added. |
|
291 | 258 |
///\return The newly created node. |
292 | 259 |
/// |
293 |
///\note The <tt>Arc</tt>s |
|
294 |
///referencing a moved arc remain |
|
295 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
|
296 |
///may be invalidated. |
|
260 |
///\note All iterators remain valid. |
|
261 |
/// |
|
297 | 262 |
///\warning This functionality cannot be used together with the Snapshot |
298 | 263 |
///feature. |
299 | 264 |
Node split(Node n, bool connect = true) |
300 | 265 |
{ |
301 | 266 |
Node b = addNode(); |
302 | 267 |
nodes[b._id].first_out=nodes[n._id].first_out; |
303 | 268 |
nodes[n._id].first_out=-1; |
304 | 269 |
for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) { |
305 | 270 |
arcs[i].source=b._id; |
306 | 271 |
} |
307 | 272 |
if(connect) addArc(n,b); |
308 | 273 |
return b; |
309 | 274 |
} |
310 | 275 |
|
276 |
///Clear the digraph. |
|
277 |
|
|
278 |
///This function erases all nodes and arcs from the digraph. |
|
279 |
/// |
|
280 |
void clear() { |
|
281 |
Parent::clear(); |
|
282 |
} |
|
283 |
|
|
284 |
/// Reserve memory for nodes. |
|
285 |
|
|
286 |
/// Using this function, it is possible to avoid superfluous memory |
|
287 |
/// allocation: if you know that the digraph you want to build will |
|
288 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
|
289 |
/// then it is worth reserving space for this amount before starting |
|
290 |
/// to build the digraph. |
|
291 |
/// \sa reserveArc() |
|
292 |
void reserveNode(int n) { nodes.reserve(n); }; |
|
293 |
|
|
294 |
/// Reserve memory for arcs. |
|
295 |
|
|
296 |
/// Using this function, it is possible to avoid superfluous memory |
|
297 |
/// allocation: if you know that the digraph you want to build will |
|
298 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
|
299 |
/// then it is worth reserving space for this amount before starting |
|
300 |
/// to build the digraph. |
|
301 |
/// \sa reserveNode() |
|
302 |
void reserveArc(int m) { arcs.reserve(m); }; |
|
303 |
|
|
311 | 304 |
public: |
312 | 305 |
|
313 | 306 |
class Snapshot; |
314 | 307 |
|
315 | 308 |
protected: |
316 | 309 |
|
317 | 310 |
void restoreSnapshot(const Snapshot &s) |
318 | 311 |
{ |
319 | 312 |
while(s.arc_num<arcs.size()) { |
320 | 313 |
Arc arc = arcFromId(arcs.size()-1); |
321 | 314 |
Parent::notifier(Arc()).erase(arc); |
322 | 315 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
323 | 316 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
324 | 317 |
arcs.pop_back(); |
325 | 318 |
} |
326 | 319 |
while(s.node_num<nodes.size()) { |
327 | 320 |
Node node = nodeFromId(nodes.size()-1); |
328 | 321 |
Parent::notifier(Node()).erase(node); |
329 | 322 |
nodes.pop_back(); |
330 | 323 |
} |
331 | 324 |
} |
332 | 325 |
|
333 | 326 |
public: |
334 | 327 |
|
335 |
///Class to make a snapshot of the digraph and to |
|
328 |
///Class to make a snapshot of the digraph and to restore it later. |
|
336 | 329 |
|
337 |
///Class to make a snapshot of the digraph and to |
|
330 |
///Class to make a snapshot of the digraph and to restore it later. |
|
338 | 331 |
/// |
339 | 332 |
///The newly added nodes and arcs can be removed using the |
340 |
///restore() function. |
|
341 |
///\note After you restore a state, you cannot restore |
|
342 |
///a later state, in other word you cannot add again the arcs deleted |
|
343 |
///by restore() using another one Snapshot instance. |
|
333 |
///restore() function. This is the only way for deleting nodes and/or |
|
334 |
///arcs from a SmartDigraph structure. |
|
344 | 335 |
/// |
345 |
///\warning If you do not use correctly the snapshot that can cause |
|
346 |
///either broken program, invalid state of the digraph, valid but |
|
347 |
///not the restored digraph or no change. Because the runtime performance |
|
348 |
///the validity of the snapshot is not stored. |
|
336 |
///\note After a state is restored, you cannot restore a later state, |
|
337 |
///i.e. you cannot add the removed nodes and arcs again using |
|
338 |
///another Snapshot instance. |
|
339 |
/// |
|
340 |
///\warning Node splitting cannot be restored. |
|
341 |
///\warning The validity of the snapshot is not stored due to |
|
342 |
///performance reasons. If you do not use the snapshot correctly, |
|
343 |
///it can cause broken program, invalid or not restored state of |
|
344 |
///the digraph or no change. |
|
349 | 345 |
class Snapshot |
350 | 346 |
{ |
351 | 347 |
SmartDigraph *_graph; |
352 | 348 |
protected: |
353 | 349 |
friend class SmartDigraph; |
354 | 350 |
unsigned int node_num; |
355 | 351 |
unsigned int arc_num; |
356 | 352 |
public: |
357 | 353 |
///Default constructor. |
358 | 354 |
|
359 | 355 |
///Default constructor. |
360 |
///To actually make a snapshot you must call save(). |
|
361 |
/// |
|
356 |
///You have to call save() to actually make a snapshot. |
|
362 | 357 |
Snapshot() : _graph(0) {} |
363 | 358 |
///Constructor that immediately makes a snapshot |
364 | 359 |
|
365 |
///This constructor immediately makes a snapshot of the digraph. |
|
366 |
///\param graph The digraph we make a snapshot of. |
|
367 |
|
|
360 |
///This constructor immediately makes a snapshot of the given digraph. |
|
361 |
/// |
|
362 |
Snapshot(SmartDigraph &gr) : _graph(&gr) { |
|
368 | 363 |
node_num=_graph->nodes.size(); |
369 | 364 |
arc_num=_graph->arcs.size(); |
370 | 365 |
} |
371 | 366 |
|
372 | 367 |
///Make a snapshot. |
373 | 368 |
|
374 |
///Make a snapshot of the digraph. |
|
375 |
/// |
|
376 |
///This function |
|
369 |
///This function makes a snapshot of the given digraph. |
|
370 |
///It can be called more than once. In case of a repeated |
|
377 | 371 |
///call, the previous snapshot gets lost. |
378 |
///\param graph The digraph we make the snapshot of. |
|
379 |
void save(SmartDigraph &graph) |
|
380 |
{ |
|
381 |
_graph=&graph; |
|
372 |
void save(SmartDigraph &gr) { |
|
373 |
_graph=&gr; |
|
382 | 374 |
node_num=_graph->nodes.size(); |
383 | 375 |
arc_num=_graph->arcs.size(); |
384 | 376 |
} |
385 | 377 |
|
386 | 378 |
///Undo the changes until a snapshot. |
387 | 379 |
|
388 |
///Undo the changes until a snapshot created by save(). |
|
389 |
/// |
|
390 |
///\note After you restored a state, you cannot restore |
|
391 |
///a later state, in other word you cannot add again the arcs deleted |
|
392 |
/// |
|
380 |
///This function undos the changes until the last snapshot |
|
381 |
///created by save() or Snapshot(SmartDigraph&). |
|
393 | 382 |
void restore() |
394 | 383 |
{ |
395 | 384 |
_graph->restoreSnapshot(*this); |
396 | 385 |
} |
397 | 386 |
}; |
398 | 387 |
}; |
399 | 388 |
|
400 | 389 |
|
401 | 390 |
class SmartGraphBase { |
402 | 391 |
|
403 | 392 |
protected: |
404 | 393 |
|
405 | 394 |
struct NodeT { |
406 | 395 |
int first_out; |
407 | 396 |
}; |
408 | 397 |
|
409 | 398 |
struct ArcT { |
410 | 399 |
int target; |
411 | 400 |
int next_out; |
412 | 401 |
}; |
413 | 402 |
|
414 | 403 |
std::vector<NodeT> nodes; |
415 | 404 |
std::vector<ArcT> arcs; |
416 | 405 |
|
417 | 406 |
int first_free_arc; |
418 | 407 |
|
419 | 408 |
public: |
420 | 409 |
|
421 | 410 |
typedef SmartGraphBase Graph; |
422 | 411 |
|
423 | 412 |
class Node; |
424 | 413 |
class Arc; |
425 | 414 |
class Edge; |
426 | 415 |
|
427 | 416 |
class Node { |
428 | 417 |
friend class SmartGraphBase; |
429 | 418 |
protected: |
430 | 419 |
|
431 | 420 |
int _id; |
432 | 421 |
explicit Node(int id) { _id = id;} |
433 | 422 |
|
434 | 423 |
public: |
435 | 424 |
Node() {} |
436 | 425 |
Node (Invalid) { _id = -1; } |
437 | 426 |
bool operator==(const Node& node) const {return _id == node._id;} |
438 | 427 |
bool operator!=(const Node& node) const {return _id != node._id;} |
439 | 428 |
bool operator<(const Node& node) const {return _id < node._id;} |
440 | 429 |
}; |
... | ... |
@@ -576,236 +565,229 @@ |
576 | 565 |
bool valid(Node n) const { |
577 | 566 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
578 | 567 |
} |
579 | 568 |
bool valid(Arc a) const { |
580 | 569 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
581 | 570 |
} |
582 | 571 |
bool valid(Edge e) const { |
583 | 572 |
return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
584 | 573 |
} |
585 | 574 |
|
586 | 575 |
Node addNode() { |
587 | 576 |
int n = nodes.size(); |
588 | 577 |
nodes.push_back(NodeT()); |
589 | 578 |
nodes[n].first_out = -1; |
590 | 579 |
|
591 | 580 |
return Node(n); |
592 | 581 |
} |
593 | 582 |
|
594 | 583 |
Edge addEdge(Node u, Node v) { |
595 | 584 |
int n = arcs.size(); |
596 | 585 |
arcs.push_back(ArcT()); |
597 | 586 |
arcs.push_back(ArcT()); |
598 | 587 |
|
599 | 588 |
arcs[n].target = u._id; |
600 | 589 |
arcs[n | 1].target = v._id; |
601 | 590 |
|
602 | 591 |
arcs[n].next_out = nodes[v._id].first_out; |
603 | 592 |
nodes[v._id].first_out = n; |
604 | 593 |
|
605 | 594 |
arcs[n | 1].next_out = nodes[u._id].first_out; |
606 | 595 |
nodes[u._id].first_out = (n | 1); |
607 | 596 |
|
608 | 597 |
return Edge(n / 2); |
609 | 598 |
} |
610 | 599 |
|
611 | 600 |
void clear() { |
612 | 601 |
arcs.clear(); |
613 | 602 |
nodes.clear(); |
614 | 603 |
} |
615 | 604 |
|
616 | 605 |
}; |
617 | 606 |
|
618 | 607 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
619 | 608 |
|
620 | 609 |
/// \ingroup graphs |
621 | 610 |
/// |
622 | 611 |
/// \brief A smart undirected graph class. |
623 | 612 |
/// |
624 |
/// This is a simple and fast graph implementation. |
|
625 |
/// It is also quite memory efficient, but at the price |
|
626 |
/// that <b> it does support only limited (only stack-like) |
|
627 |
/// node and arc deletions</b>. |
|
628 |
/// |
|
613 |
/// \ref SmartGraph is a simple and fast graph implementation. |
|
614 |
/// It is also quite memory efficient but at the price |
|
615 |
/// that it does not support node and edge deletion |
|
616 |
/// (except for the Snapshot feature). |
|
629 | 617 |
/// |
630 |
/// \ |
|
618 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept" |
|
619 |
/// and it also provides some additional functionalities. |
|
620 |
/// Most of its member functions and nested classes are documented |
|
621 |
/// only in the concept class. |
|
622 |
/// |
|
623 |
/// \sa concepts::Graph |
|
624 |
/// \sa SmartDigraph |
|
631 | 625 |
class SmartGraph : public ExtendedSmartGraphBase { |
632 | 626 |
typedef ExtendedSmartGraphBase Parent; |
633 | 627 |
|
634 | 628 |
private: |
635 |
|
|
636 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
|
637 |
|
|
638 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
|
639 |
/// |
|
629 |
/// Graphs are \e not copy constructible. Use GraphCopy instead. |
|
640 | 630 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {}; |
641 |
|
|
642 |
///\brief Assignment of SmartGraph to another one is \e not allowed. |
|
643 |
///Use GraphCopy() instead. |
|
644 |
|
|
645 |
///Assignment of SmartGraph to another one is \e not allowed. |
|
646 |
///Use GraphCopy() instead. |
|
631 |
/// \brief Assignment of a graph to another one is \e not allowed. |
|
632 |
/// Use GraphCopy instead. |
|
647 | 633 |
void operator=(const SmartGraph &) {} |
648 | 634 |
|
649 | 635 |
public: |
650 | 636 |
|
651 | 637 |
/// Constructor |
652 | 638 |
|
653 | 639 |
/// Constructor. |
654 | 640 |
/// |
655 | 641 |
SmartGraph() {} |
656 | 642 |
|
657 |
///Add a new node to the graph. |
|
658 |
|
|
659 |
/// Add a new node to the graph. |
|
643 |
/// \brief Add a new node to the graph. |
|
644 |
/// |
|
645 |
/// This function adds a new node to the graph. |
|
660 | 646 |
/// \return The new node. |
661 | 647 |
Node addNode() { return Parent::addNode(); } |
662 | 648 |
|
663 |
///Add a new edge to the graph. |
|
664 |
|
|
665 |
///Add a new edge to the graph with node \c s |
|
666 |
///and \c t. |
|
667 |
///\return The new edge. |
|
668 |
Edge addEdge(const Node& s, const Node& t) { |
|
669 |
|
|
649 |
/// \brief Add a new edge to the graph. |
|
650 |
/// |
|
651 |
/// This function adds a new edge to the graph between nodes |
|
652 |
/// \c u and \c v with inherent orientation from node \c u to |
|
653 |
/// node \c v. |
|
654 |
/// \return The new edge. |
|
655 |
Edge addEdge(Node u, Node v) { |
|
656 |
return Parent::addEdge(u, v); |
|
670 | 657 |
} |
671 | 658 |
|
672 | 659 |
/// \brief Node validity check |
673 | 660 |
/// |
674 |
/// This function gives back true if the given node is valid, |
|
675 |
/// ie. it is a real node of the graph. |
|
661 |
/// This function gives back \c true if the given node is valid, |
|
662 |
/// i.e. it is a real node of the graph. |
|
676 | 663 |
/// |
677 | 664 |
/// \warning A removed node (using Snapshot) could become valid again |
678 |
/// |
|
665 |
/// if new nodes are added to the graph. |
|
679 | 666 |
bool valid(Node n) const { return Parent::valid(n); } |
680 | 667 |
|
668 |
/// \brief Edge validity check |
|
669 |
/// |
|
670 |
/// This function gives back \c true if the given edge is valid, |
|
671 |
/// i.e. it is a real edge of the graph. |
|
672 |
/// |
|
673 |
/// \warning A removed edge (using Snapshot) could become valid again |
|
674 |
/// if new edges are added to the graph. |
|
675 |
bool valid(Edge e) const { return Parent::valid(e); } |
|
676 |
|
|
681 | 677 |
/// \brief Arc validity check |
682 | 678 |
/// |
683 |
/// This function gives back true if the given arc is valid, |
|
684 |
/// ie. it is a real arc of the graph. |
|
679 |
/// This function gives back \c true if the given arc is valid, |
|
680 |
/// i.e. it is a real arc of the graph. |
|
685 | 681 |
/// |
686 | 682 |
/// \warning A removed arc (using Snapshot) could become valid again |
687 |
/// |
|
683 |
/// if new edges are added to the graph. |
|
688 | 684 |
bool valid(Arc a) const { return Parent::valid(a); } |
689 | 685 |
|
690 |
/// \brief Edge validity check |
|
691 |
/// |
|
692 |
/// This function gives back true if the given edge is valid, |
|
693 |
/// ie. it is a real edge of the graph. |
|
694 |
/// |
|
695 |
/// \warning A removed edge (using Snapshot) could become valid again |
|
696 |
/// when new edges are added to the graph. |
|
697 |
bool valid(Edge e) const { return Parent::valid(e); } |
|
698 |
|
|
699 | 686 |
///Clear the graph. |
700 | 687 |
|
701 |
/// |
|
688 |
///This function erases all nodes and arcs from the graph. |
|
702 | 689 |
/// |
703 | 690 |
void clear() { |
704 | 691 |
Parent::clear(); |
705 | 692 |
} |
706 | 693 |
|
707 | 694 |
public: |
708 | 695 |
|
709 | 696 |
class Snapshot; |
710 | 697 |
|
711 | 698 |
protected: |
712 | 699 |
|
713 | 700 |
void saveSnapshot(Snapshot &s) |
714 | 701 |
{ |
715 | 702 |
s._graph = this; |
716 | 703 |
s.node_num = nodes.size(); |
717 | 704 |
s.arc_num = arcs.size(); |
718 | 705 |
} |
719 | 706 |
|
720 | 707 |
void restoreSnapshot(const Snapshot &s) |
721 | 708 |
{ |
722 | 709 |
while(s.arc_num<arcs.size()) { |
723 | 710 |
int n=arcs.size()-1; |
724 | 711 |
Edge arc=edgeFromId(n/2); |
725 | 712 |
Parent::notifier(Edge()).erase(arc); |
726 | 713 |
std::vector<Arc> dir; |
727 | 714 |
dir.push_back(arcFromId(n)); |
728 | 715 |
dir.push_back(arcFromId(n-1)); |
729 | 716 |
Parent::notifier(Arc()).erase(dir); |
730 | 717 |
nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
731 | 718 |
nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
732 | 719 |
arcs.pop_back(); |
733 | 720 |
arcs.pop_back(); |
734 | 721 |
} |
735 | 722 |
while(s.node_num<nodes.size()) { |
736 | 723 |
int n=nodes.size()-1; |
737 | 724 |
Node node = nodeFromId(n); |
738 | 725 |
Parent::notifier(Node()).erase(node); |
739 | 726 |
nodes.pop_back(); |
740 | 727 |
} |
741 | 728 |
} |
742 | 729 |
|
743 | 730 |
public: |
744 | 731 |
|
745 |
///Class to make a snapshot of the |
|
732 |
///Class to make a snapshot of the graph and to restore it later. |
|
746 | 733 |
|
747 |
///Class to make a snapshot of the |
|
734 |
///Class to make a snapshot of the graph and to restore it later. |
|
748 | 735 |
/// |
749 |
///The newly added nodes and arcs can be removed using the |
|
750 |
///restore() function. |
|
736 |
///The newly added nodes and edges can be removed using the |
|
737 |
///restore() function. This is the only way for deleting nodes and/or |
|
738 |
///edges from a SmartGraph structure. |
|
751 | 739 |
/// |
752 |
///\note After you restore a state, you cannot restore |
|
753 |
///a later state, in other word you cannot add again the arcs deleted |
|
754 |
/// |
|
740 |
///\note After a state is restored, you cannot restore a later state, |
|
741 |
///i.e. you cannot add the removed nodes and edges again using |
|
742 |
///another Snapshot instance. |
|
755 | 743 |
/// |
756 |
///\warning If you do not use correctly the snapshot that can cause |
|
757 |
///either broken program, invalid state of the digraph, valid but |
|
758 |
///not the restored digraph or no change. Because the runtime performance |
|
759 |
///the validity of the snapshot is not stored. |
|
744 |
///\warning The validity of the snapshot is not stored due to |
|
745 |
///performance reasons. If you do not use the snapshot correctly, |
|
746 |
///it can cause broken program, invalid or not restored state of |
|
747 |
///the graph or no change. |
|
760 | 748 |
class Snapshot |
761 | 749 |
{ |
762 | 750 |
SmartGraph *_graph; |
763 | 751 |
protected: |
764 | 752 |
friend class SmartGraph; |
765 | 753 |
unsigned int node_num; |
766 | 754 |
unsigned int arc_num; |
767 | 755 |
public: |
768 | 756 |
///Default constructor. |
769 | 757 |
|
770 | 758 |
///Default constructor. |
771 |
///To actually make a snapshot you must call save(). |
|
772 |
/// |
|
759 |
///You have to call save() to actually make a snapshot. |
|
773 | 760 |
Snapshot() : _graph(0) {} |
774 | 761 |
///Constructor that immediately makes a snapshot |
775 | 762 |
|
776 |
///This constructor immediately makes a snapshot of the digraph. |
|
777 |
///\param graph The digraph we make a snapshot of. |
|
778 |
Snapshot(SmartGraph &graph) { |
|
779 |
graph.saveSnapshot(*this); |
|
763 |
/// This constructor immediately makes a snapshot of the given graph. |
|
764 |
/// |
|
765 |
Snapshot(SmartGraph &gr) { |
|
766 |
gr.saveSnapshot(*this); |
|
780 | 767 |
} |
781 | 768 |
|
782 | 769 |
///Make a snapshot. |
783 | 770 |
|
784 |
///Make a snapshot of the graph. |
|
785 |
/// |
|
786 |
///This function |
|
771 |
///This function makes a snapshot of the given graph. |
|
772 |
///It can be called more than once. In case of a repeated |
|
787 | 773 |
///call, the previous snapshot gets lost. |
788 |
///\param graph The digraph we make the snapshot of. |
|
789 |
void save(SmartGraph &graph) |
|
774 |
void save(SmartGraph &gr) |
|
790 | 775 |
{ |
791 |
|
|
776 |
gr.saveSnapshot(*this); |
|
792 | 777 |
} |
793 | 778 |
|
794 |
///Undo the changes until |
|
779 |
///Undo the changes until the last snapshot. |
|
795 | 780 |
|
796 |
///Undo the changes until a snapshot created by save(). |
|
797 |
/// |
|
798 |
///\note After you restored a state, you cannot restore |
|
799 |
///a later state, in other word you cannot add again the arcs deleted |
|
800 |
/// |
|
781 |
///This function undos the changes until the last snapshot |
|
782 |
///created by save() or Snapshot(SmartGraph&). |
|
801 | 783 |
void restore() |
802 | 784 |
{ |
803 | 785 |
_graph->restoreSnapshot(*this); |
804 | 786 |
} |
805 | 787 |
}; |
806 | 788 |
}; |
807 | 789 |
|
808 | 790 |
} //namespace lemon |
809 | 791 |
|
810 | 792 |
|
811 | 793 |
#endif //LEMON_SMART_GRAPH_H |
0 comments (0 inline)