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