0
2
0
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_FULL_GRAPH_H |
20 | 20 |
#define LEMON_FULL_GRAPH_H |
21 | 21 |
|
22 |
#include <lemon/math.h> |
|
23 |
|
|
24 | 22 |
#include <lemon/core.h> |
25 | 23 |
#include <lemon/bits/graph_extender.h> |
26 | 24 |
|
27 | 25 |
///\ingroup graphs |
28 | 26 |
///\file |
29 |
///\brief |
|
27 |
///\brief FullGraph and FullDigraph classes. |
|
28 |
|
|
30 | 29 |
namespace lemon { |
31 | 30 |
|
32 | 31 |
class FullDigraphBase { |
33 | 32 |
public: |
34 | 33 |
|
35 | 34 |
typedef FullDigraphBase Graph; |
36 | 35 |
|
37 | 36 |
class Node; |
38 | 37 |
class Arc; |
39 | 38 |
|
40 | 39 |
protected: |
41 | 40 |
|
42 | 41 |
int _node_num; |
43 | 42 |
int _arc_num; |
44 | 43 |
|
45 | 44 |
FullDigraphBase() {} |
46 | 45 |
|
47 | 46 |
void construct(int n) { _node_num = n; _arc_num = n * n; } |
48 | 47 |
|
49 | 48 |
public: |
50 | 49 |
|
51 | 50 |
typedef True NodeNumTag; |
52 | 51 |
typedef True ArcNumTag; |
53 | 52 |
|
54 | 53 |
Node operator()(int ix) const { return Node(ix); } |
55 | 54 |
int index(const Node& node) const { return node._id; } |
56 | 55 |
|
57 | 56 |
Arc arc(const Node& s, const Node& t) const { |
58 | 57 |
return Arc(s._id * _node_num + t._id); |
59 | 58 |
} |
60 | 59 |
|
61 | 60 |
int nodeNum() const { return _node_num; } |
62 | 61 |
int arcNum() const { return _arc_num; } |
63 | 62 |
|
64 | 63 |
int maxNodeId() const { return _node_num - 1; } |
65 | 64 |
int maxArcId() const { return _arc_num - 1; } |
66 | 65 |
|
67 | 66 |
Node source(Arc arc) const { return arc._id / _node_num; } |
68 | 67 |
Node target(Arc arc) const { return arc._id % _node_num; } |
69 | 68 |
|
70 |
|
|
71 | 69 |
static int id(Node node) { return node._id; } |
72 | 70 |
static int id(Arc arc) { return arc._id; } |
73 | 71 |
|
74 | 72 |
static Node nodeFromId(int id) { return Node(id);} |
75 |
|
|
76 | 73 |
static Arc arcFromId(int id) { return Arc(id);} |
77 | 74 |
|
78 | 75 |
typedef True FindArcTag; |
79 | 76 |
|
80 | 77 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const { |
81 | 78 |
return prev != INVALID ? arc(s, t) : INVALID; |
82 | 79 |
} |
83 | 80 |
|
84 |
|
|
85 | 81 |
class Node { |
86 | 82 |
friend class FullDigraphBase; |
87 | 83 |
|
88 | 84 |
protected: |
89 | 85 |
int _id; |
90 | 86 |
Node(int id) : _id(id) {} |
91 | 87 |
public: |
92 | 88 |
Node() {} |
93 | 89 |
Node (Invalid) : _id(-1) {} |
94 | 90 |
bool operator==(const Node node) const {return _id == node._id;} |
95 | 91 |
bool operator!=(const Node node) const {return _id != node._id;} |
96 | 92 |
bool operator<(const Node node) const {return _id < node._id;} |
97 | 93 |
}; |
98 | 94 |
|
99 | 95 |
class Arc { |
100 | 96 |
friend class FullDigraphBase; |
101 | 97 |
|
102 | 98 |
protected: |
103 | 99 |
int _id; // _node_num * source + target; |
104 | 100 |
|
105 | 101 |
Arc(int id) : _id(id) {} |
106 | 102 |
|
107 | 103 |
public: |
108 | 104 |
Arc() { } |
109 | 105 |
Arc (Invalid) { _id = -1; } |
110 | 106 |
bool operator==(const Arc arc) const {return _id == arc._id;} |
111 | 107 |
bool operator!=(const Arc arc) const {return _id != arc._id;} |
112 | 108 |
bool operator<(const Arc arc) const {return _id < arc._id;} |
113 | 109 |
}; |
114 | 110 |
|
115 | 111 |
void first(Node& node) const { |
116 | 112 |
node._id = _node_num - 1; |
117 | 113 |
} |
118 | 114 |
|
119 | 115 |
static void next(Node& node) { |
120 | 116 |
--node._id; |
121 | 117 |
} |
122 | 118 |
|
123 | 119 |
void first(Arc& arc) const { |
124 | 120 |
arc._id = _arc_num - 1; |
125 | 121 |
} |
126 | 122 |
|
127 | 123 |
static void next(Arc& arc) { |
128 | 124 |
--arc._id; |
129 | 125 |
} |
130 | 126 |
|
131 | 127 |
void firstOut(Arc& arc, const Node& node) const { |
132 | 128 |
arc._id = (node._id + 1) * _node_num - 1; |
133 | 129 |
} |
134 | 130 |
|
135 | 131 |
void nextOut(Arc& arc) const { |
136 | 132 |
if (arc._id % _node_num == 0) arc._id = 0; |
137 | 133 |
--arc._id; |
138 | 134 |
} |
139 | 135 |
|
140 | 136 |
void firstIn(Arc& arc, const Node& node) const { |
141 | 137 |
arc._id = _arc_num + node._id - _node_num; |
142 | 138 |
} |
143 | 139 |
|
144 | 140 |
void nextIn(Arc& arc) const { |
145 | 141 |
arc._id -= _node_num; |
146 | 142 |
if (arc._id < 0) arc._id = -1; |
147 | 143 |
} |
148 | 144 |
|
149 | 145 |
}; |
150 | 146 |
|
151 | 147 |
typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase; |
152 | 148 |
|
153 | 149 |
/// \ingroup graphs |
154 | 150 |
/// |
155 | 151 |
/// \brief A full digraph class. |
156 | 152 |
/// |
157 | 153 |
/// This is a simple and fast directed full graph implementation. |
158 | 154 |
/// From each node go arcs to each node (including the source node), |
159 | 155 |
/// therefore the number of the arcs in the digraph is the square of |
160 |
/// the node number. The digraph is completely static, so you can |
|
161 |
/// neither add nor delete either arcs or nodes, and it needs just |
|
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 |
|
162 | 158 |
/// constant space in memory. |
163 | 159 |
/// |
164 |
/// |
|
160 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept |
|
165 | 161 |
/// and it also has an important extra feature that its maps are |
166 | 162 |
/// real \ref concepts::ReferenceMap "reference map"s. |
167 |
/// |
|
163 |
/// |
|
164 |
/// The \c FullDigraph and \c FullGraph classes are very similar, |
|
165 |
/// but there are two differences. While this class conforms only |
|
166 |
/// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph |
|
167 |
/// class conforms to the \ref concepts::Graph "Graph" concept, |
|
168 |
/// moreover \c FullGraph does not contain a loop arc for each |
|
169 |
/// node as \c FullDigraph does. |
|
168 | 170 |
/// |
169 | 171 |
/// \sa FullGraph |
170 | 172 |
class FullDigraph : public ExtendedFullDigraphBase { |
171 | 173 |
public: |
172 | 174 |
|
173 | 175 |
typedef ExtendedFullDigraphBase Parent; |
174 | 176 |
|
175 | 177 |
/// \brief Constructor |
176 | 178 |
FullDigraph() { construct(0); } |
177 | 179 |
|
178 | 180 |
/// \brief Constructor |
179 | 181 |
/// |
182 |
/// Constructor. |
|
180 | 183 |
/// \param n The number of the nodes. |
181 | 184 |
FullDigraph(int n) { construct(n); } |
182 | 185 |
|
183 |
/// \brief |
|
186 |
/// \brief Resizes the digraph |
|
184 | 187 |
/// |
185 |
/// Resize the digraph. The function will fully destroy and |
|
186 |
/// rebuild the digraph. This cause that the maps of the digraph |
|
187 |
/// will reallocated automatically and the previous values will be |
|
188 |
/// lost. |
|
188 |
/// Resizes the digraph. The function will fully destroy and |
|
189 |
/// rebuild the digraph. This cause that the maps of the digraph will |
|
190 |
/// reallocated automatically and the previous values will be lost. |
|
189 | 191 |
void resize(int n) { |
190 | 192 |
Parent::notifier(Arc()).clear(); |
191 | 193 |
Parent::notifier(Node()).clear(); |
192 | 194 |
construct(n); |
193 | 195 |
Parent::notifier(Node()).build(); |
194 | 196 |
Parent::notifier(Arc()).build(); |
195 | 197 |
} |
196 | 198 |
|
197 | 199 |
/// \brief Returns the node with the given index. |
198 | 200 |
/// |
199 |
/// Returns the node with the given index. Because it is a |
|
200 |
/// static size digraph the node's of the digraph can be indexed |
|
201 |
/// in the range <tt>[0..nodeNum()-1]</tt> and the index of |
|
202 |
/// the node can accessed by the \e index() member. |
|
201 |
/// Returns the node with the given index. Since it is a static |
|
202 |
/// digraph its nodes can be indexed with integers from the range |
|
203 |
/// <tt>[0..nodeNum()-1]</tt>. |
|
204 |
/// \sa index() |
|
203 | 205 |
Node operator()(int ix) const { return Parent::operator()(ix); } |
204 | 206 |
|
205 |
/// \brief Returns the index of the node. |
|
207 |
/// \brief Returns the index of the given node. |
|
206 | 208 |
/// |
207 |
/// Returns the index of the node. Because it is a |
|
208 |
/// static size digraph the node's of the digraph can be indexed |
|
209 |
/// in the range <tt>[0..nodeNum()-1]</tt> and the index of |
|
210 |
/// the node can accessed by the \e index() member. |
|
209 |
/// Returns the index of the given node. Since it is a static |
|
210 |
/// digraph its nodes can be indexed with integers from the range |
|
211 |
/// <tt>[0..nodeNum()-1]</tt>. |
|
212 |
/// \sa operator() |
|
211 | 213 |
int index(const Node& node) const { return Parent::index(node); } |
212 | 214 |
|
213 |
/// \brief Returns the arc |
|
215 |
/// \brief Returns the arc connecting the given nodes. |
|
214 | 216 |
/// |
215 |
/// Returns the arc |
|
217 |
/// Returns the arc connecting the given nodes. |
|
216 | 218 |
Arc arc(const Node& u, const Node& v) const { |
217 | 219 |
return Parent::arc(u, v); |
218 | 220 |
} |
219 | 221 |
|
220 | 222 |
/// \brief Number of nodes. |
221 | 223 |
int nodeNum() const { return Parent::nodeNum(); } |
222 | 224 |
/// \brief Number of arcs. |
223 | 225 |
int arcNum() const { return Parent::arcNum(); } |
224 | 226 |
}; |
225 | 227 |
|
226 | 228 |
|
227 | 229 |
class FullGraphBase { |
228 | 230 |
int _node_num; |
229 | 231 |
int _edge_num; |
230 | 232 |
public: |
231 | 233 |
|
232 | 234 |
typedef FullGraphBase Graph; |
233 | 235 |
|
234 | 236 |
class Node; |
235 | 237 |
class Arc; |
236 | 238 |
class Edge; |
237 | 239 |
|
238 | 240 |
protected: |
239 | 241 |
|
240 | 242 |
FullGraphBase() {} |
241 | 243 |
|
242 | 244 |
void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; } |
243 | 245 |
|
244 | 246 |
int _uid(int e) const { |
245 | 247 |
int u = e / _node_num; |
246 | 248 |
int v = e % _node_num; |
247 | 249 |
return u < v ? u : _node_num - 2 - u; |
248 | 250 |
} |
249 | 251 |
|
250 | 252 |
int _vid(int e) const { |
251 | 253 |
int u = e / _node_num; |
252 | 254 |
int v = e % _node_num; |
253 | 255 |
return u < v ? v : _node_num - 1 - v; |
254 | 256 |
} |
255 | 257 |
|
256 | 258 |
void _uvid(int e, int& u, int& v) const { |
257 | 259 |
u = e / _node_num; |
258 | 260 |
v = e % _node_num; |
259 | 261 |
if (u >= v) { |
260 | 262 |
u = _node_num - 2 - u; |
261 | 263 |
v = _node_num - 1 - v; |
262 | 264 |
} |
263 | 265 |
} |
264 | 266 |
|
265 | 267 |
void _stid(int a, int& s, int& t) const { |
266 | 268 |
if ((a & 1) == 1) { |
267 | 269 |
_uvid(a >> 1, s, t); |
268 | 270 |
} else { |
269 | 271 |
_uvid(a >> 1, t, s); |
270 | 272 |
} |
271 | 273 |
} |
272 | 274 |
|
273 | 275 |
int _eid(int u, int v) const { |
274 | 276 |
if (u < (_node_num - 1) / 2) { |
275 | 277 |
return u * _node_num + v; |
276 | 278 |
} else { |
277 | 279 |
return (_node_num - 1 - u) * _node_num - v - 1; |
278 | 280 |
} |
279 | 281 |
} |
280 | 282 |
|
281 | 283 |
public: |
282 | 284 |
|
283 |
|
|
284 | 285 |
Node operator()(int ix) const { return Node(ix); } |
285 | 286 |
int index(const Node& node) const { return node._id; } |
286 | 287 |
|
287 | 288 |
Edge edge(const Node& u, const Node& v) const { |
288 | 289 |
if (u._id < v._id) { |
289 | 290 |
return Edge(_eid(u._id, v._id)); |
290 | 291 |
} else if (u._id != v._id) { |
291 | 292 |
return Edge(_eid(v._id, u._id)); |
292 | 293 |
} else { |
293 | 294 |
return INVALID; |
294 | 295 |
} |
295 | 296 |
} |
296 | 297 |
|
297 | 298 |
Arc arc(const Node& s, const Node& t) const { |
298 | 299 |
if (s._id < t._id) { |
299 | 300 |
return Arc((_eid(s._id, t._id) << 1) | 1); |
300 | 301 |
} else if (s._id != t._id) { |
301 | 302 |
return Arc(_eid(t._id, s._id) << 1); |
302 | 303 |
} else { |
303 | 304 |
return INVALID; |
304 | 305 |
} |
305 | 306 |
} |
306 | 307 |
|
307 | 308 |
typedef True NodeNumTag; |
308 | 309 |
typedef True EdgeNumTag; |
309 | 310 |
|
310 | 311 |
int nodeNum() const { return _node_num; } |
311 | 312 |
int arcNum() const { return 2 * _edge_num; } |
312 | 313 |
int edgeNum() const { return _edge_num; } |
313 | 314 |
|
314 | 315 |
static int id(Node node) { return node._id; } |
315 | 316 |
static int id(Arc arc) { return arc._id; } |
316 | 317 |
static int id(Edge edge) { return edge._id; } |
317 | 318 |
|
318 | 319 |
int maxNodeId() const { return _node_num-1; } |
319 | 320 |
int maxArcId() const { return 2 * _edge_num-1; } |
320 | 321 |
int maxEdgeId() const { return _edge_num-1; } |
321 | 322 |
|
322 | 323 |
static Node nodeFromId(int id) { return Node(id);} |
323 | 324 |
static Arc arcFromId(int id) { return Arc(id);} |
324 | 325 |
static Edge edgeFromId(int id) { return Edge(id);} |
325 | 326 |
|
326 | 327 |
Node u(Edge edge) const { |
327 | 328 |
return Node(_uid(edge._id)); |
328 | 329 |
} |
329 | 330 |
|
330 | 331 |
Node v(Edge edge) const { |
331 | 332 |
return Node(_vid(edge._id)); |
332 | 333 |
} |
333 | 334 |
|
334 | 335 |
Node source(Arc arc) const { |
335 | 336 |
return Node((arc._id & 1) == 1 ? |
336 | 337 |
_uid(arc._id >> 1) : _vid(arc._id >> 1)); |
337 | 338 |
} |
338 | 339 |
|
339 | 340 |
Node target(Arc arc) const { |
340 | 341 |
return Node((arc._id & 1) == 1 ? |
341 | 342 |
_vid(arc._id >> 1) : _uid(arc._id >> 1)); |
342 | 343 |
} |
343 | 344 |
|
344 | 345 |
typedef True FindEdgeTag; |
345 | 346 |
|
346 | 347 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const { |
347 | 348 |
return prev != INVALID ? INVALID : edge(u, v); |
348 | 349 |
} |
349 | 350 |
|
350 | 351 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const { |
351 | 352 |
return prev != INVALID ? INVALID : arc(s, t); |
352 | 353 |
} |
353 | 354 |
|
354 | 355 |
class Node { |
355 | 356 |
friend class FullGraphBase; |
356 | 357 |
|
357 | 358 |
protected: |
358 | 359 |
int _id; |
359 | 360 |
Node(int id) : _id(id) {} |
360 | 361 |
public: |
361 | 362 |
Node() {} |
362 | 363 |
Node (Invalid) { _id = -1; } |
363 | 364 |
bool operator==(const Node node) const {return _id == node._id;} |
364 | 365 |
bool operator!=(const Node node) const {return _id != node._id;} |
365 | 366 |
bool operator<(const Node node) const {return _id < node._id;} |
366 | 367 |
}; |
367 | 368 |
|
368 | 369 |
class Edge { |
369 | 370 |
friend class FullGraphBase; |
371 |
friend class Arc; |
|
370 | 372 |
|
371 | 373 |
protected: |
372 | 374 |
int _id; |
373 | 375 |
|
374 | 376 |
Edge(int id) : _id(id) {} |
375 | 377 |
|
376 | 378 |
public: |
377 | 379 |
Edge() { } |
378 | 380 |
Edge (Invalid) { _id = -1; } |
379 | 381 |
|
380 | 382 |
bool operator==(const Edge edge) const {return _id == edge._id;} |
381 | 383 |
bool operator!=(const Edge edge) const {return _id != edge._id;} |
382 | 384 |
bool operator<(const Edge edge) const {return _id < edge._id;} |
383 | 385 |
}; |
384 | 386 |
|
385 | 387 |
class Arc { |
386 | 388 |
friend class FullGraphBase; |
387 | 389 |
|
388 | 390 |
protected: |
389 | 391 |
int _id; |
390 | 392 |
|
391 | 393 |
Arc(int id) : _id(id) {} |
392 | 394 |
|
393 | 395 |
public: |
394 | 396 |
Arc() { } |
395 | 397 |
Arc (Invalid) { _id = -1; } |
396 | 398 |
|
397 | 399 |
operator Edge() const { return Edge(_id != -1 ? (_id >> 1) : -1); } |
398 | 400 |
|
399 | 401 |
bool operator==(const Arc arc) const {return _id == arc._id;} |
400 | 402 |
bool operator!=(const Arc arc) const {return _id != arc._id;} |
401 | 403 |
bool operator<(const Arc arc) const {return _id < arc._id;} |
402 | 404 |
}; |
403 | 405 |
|
404 | 406 |
static bool direction(Arc arc) { |
405 | 407 |
return (arc._id & 1) == 1; |
406 | 408 |
} |
407 | 409 |
|
408 | 410 |
static Arc direct(Edge edge, bool dir) { |
409 | 411 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
410 | 412 |
} |
411 | 413 |
|
412 | 414 |
void first(Node& node) const { |
413 | 415 |
node._id = _node_num - 1; |
414 | 416 |
} |
415 | 417 |
|
416 | 418 |
static void next(Node& node) { |
417 | 419 |
--node._id; |
418 | 420 |
} |
419 | 421 |
|
420 | 422 |
void first(Arc& arc) const { |
421 | 423 |
arc._id = (_edge_num << 1) - 1; |
422 | 424 |
} |
423 | 425 |
|
424 | 426 |
static void next(Arc& arc) { |
425 | 427 |
--arc._id; |
426 | 428 |
} |
427 | 429 |
|
428 | 430 |
void first(Edge& edge) const { |
429 | 431 |
edge._id = _edge_num - 1; |
430 | 432 |
} |
431 | 433 |
|
432 | 434 |
static void next(Edge& edge) { |
433 | 435 |
--edge._id; |
434 | 436 |
} |
435 | 437 |
|
436 | 438 |
void firstOut(Arc& arc, const Node& node) const { |
437 | 439 |
int s = node._id, t = _node_num - 1; |
438 | 440 |
if (s < t) { |
439 | 441 |
arc._id = (_eid(s, t) << 1) | 1; |
440 | 442 |
} else { |
441 | 443 |
--t; |
442 | 444 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1); |
443 | 445 |
} |
444 | 446 |
} |
445 | 447 |
|
446 | 448 |
void nextOut(Arc& arc) const { |
447 | 449 |
int s, t; |
448 | 450 |
_stid(arc._id, s, t); |
449 | 451 |
--t; |
450 | 452 |
if (s < t) { |
451 | 453 |
arc._id = (_eid(s, t) << 1) | 1; |
452 | 454 |
} else { |
453 | 455 |
if (s == t) --t; |
454 | 456 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1); |
455 | 457 |
} |
456 | 458 |
} |
457 | 459 |
|
458 | 460 |
void firstIn(Arc& arc, const Node& node) const { |
459 | 461 |
int s = _node_num - 1, t = node._id; |
460 | 462 |
if (s > t) { |
461 | 463 |
arc._id = (_eid(t, s) << 1); |
462 | 464 |
} else { |
463 | 465 |
--s; |
464 | 466 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1); |
465 | 467 |
} |
466 | 468 |
} |
467 | 469 |
|
468 | 470 |
void nextIn(Arc& arc) const { |
469 | 471 |
int s, t; |
470 | 472 |
_stid(arc._id, s, t); |
471 | 473 |
--s; |
472 | 474 |
if (s > t) { |
473 | 475 |
arc._id = (_eid(t, s) << 1); |
474 | 476 |
} else { |
475 | 477 |
if (s == t) --s; |
476 | 478 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1); |
477 | 479 |
} |
478 | 480 |
} |
479 | 481 |
|
480 | 482 |
void firstInc(Edge& edge, bool& dir, const Node& node) const { |
481 | 483 |
int u = node._id, v = _node_num - 1; |
482 | 484 |
if (u < v) { |
483 | 485 |
edge._id = _eid(u, v); |
484 | 486 |
dir = true; |
485 | 487 |
} else { |
486 | 488 |
--v; |
487 | 489 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
488 | 490 |
dir = false; |
489 | 491 |
} |
490 | 492 |
} |
491 | 493 |
|
492 | 494 |
void nextInc(Edge& edge, bool& dir) const { |
493 | 495 |
int u, v; |
494 | 496 |
if (dir) { |
495 | 497 |
_uvid(edge._id, u, v); |
496 | 498 |
--v; |
497 | 499 |
if (u < v) { |
498 | 500 |
edge._id = _eid(u, v); |
499 | 501 |
} else { |
500 | 502 |
--v; |
501 | 503 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
502 | 504 |
dir = false; |
503 | 505 |
} |
504 | 506 |
} else { |
505 | 507 |
_uvid(edge._id, v, u); |
506 | 508 |
--v; |
507 | 509 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
508 | 510 |
} |
509 | 511 |
} |
510 | 512 |
|
511 | 513 |
}; |
512 | 514 |
|
513 | 515 |
typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase; |
514 | 516 |
|
515 | 517 |
/// \ingroup graphs |
516 | 518 |
/// |
517 | 519 |
/// \brief An undirected full graph class. |
518 | 520 |
/// |
519 | 521 |
/// This is a simple and fast undirected full graph |
520 | 522 |
/// implementation. From each node go edge to each other node, |
521 |
/// therefore the number of edges in the graph is |
|
522 |
/// <tt>n(n-1)/2</tt>. It is completely static, so you can neither |
|
523 |
/// |
|
523 |
/// therefore the number of edges in the graph is \f$n(n-1)/2\f$. |
|
524 |
/// This graph type is completely static, so you can neither |
|
525 |
/// add nor delete either edges or nodes, and it needs constant |
|
524 | 526 |
/// space in memory. |
525 | 527 |
/// |
526 |
/// The \e FullDigraph and \e FullGraph classes are very similar, |
|
527 |
/// but there are two differences. While the \e FullDigraph class is |
|
528 |
/// conform just to the \ref concepts::Digraph "Digraph" concept, |
|
529 |
/// this class is conform to the \ref concepts::Graph "Graph" |
|
530 |
/// concept. In addition, the \e FullGraph class does not contain a |
|
531 |
/// loop arc from each node as the \e FullDigraph does. |
|
528 |
/// This class conforms to the \ref concepts::Graph "Graph" concept |
|
529 |
/// and it also has an important extra feature that its maps are |
|
530 |
/// real \ref concepts::ReferenceMap "reference map"s. |
|
532 | 531 |
/// |
533 |
/// It also has an important extra feature that its maps are real |
|
534 |
/// \ref concepts::ReferenceMap "reference map"s. |
|
532 |
/// The \c FullGraph and \c FullDigraph classes are very similar, |
|
533 |
/// but there are two differences. While the \c FullDigraph class |
|
534 |
/// conforms only to the \ref concepts::Digraph "Digraph" concept, |
|
535 |
/// 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. |
|
535 | 538 |
/// |
536 | 539 |
/// \sa FullDigraph |
537 | 540 |
class FullGraph : public ExtendedFullGraphBase { |
538 | 541 |
public: |
539 | 542 |
|
540 | 543 |
typedef ExtendedFullGraphBase Parent; |
541 | 544 |
|
542 | 545 |
/// \brief Constructor |
543 | 546 |
FullGraph() { construct(0); } |
544 | 547 |
|
545 | 548 |
/// \brief Constructor |
546 | 549 |
/// |
550 |
/// Constructor. |
|
547 | 551 |
/// \param n The number of the nodes. |
548 | 552 |
FullGraph(int n) { construct(n); } |
549 | 553 |
|
550 |
/// \brief |
|
554 |
/// \brief Resizes the graph |
|
551 | 555 |
/// |
552 |
/// Resize the graph. The function will fully destroy and rebuild |
|
553 |
/// the graph. This cause that the maps of the graph will |
|
554 |
/// reallocated automatically and the previous values will be |
|
555 |
/// lost. |
|
556 |
/// Resizes the graph. The function will fully destroy and |
|
557 |
/// rebuild the graph. This cause that the maps of the graph will |
|
558 |
/// reallocated automatically and the previous values will be lost. |
|
556 | 559 |
void resize(int n) { |
557 | 560 |
Parent::notifier(Arc()).clear(); |
558 | 561 |
Parent::notifier(Edge()).clear(); |
559 | 562 |
Parent::notifier(Node()).clear(); |
560 | 563 |
construct(n); |
561 | 564 |
Parent::notifier(Node()).build(); |
562 | 565 |
Parent::notifier(Edge()).build(); |
563 | 566 |
Parent::notifier(Arc()).build(); |
564 | 567 |
} |
565 | 568 |
|
566 | 569 |
/// \brief Returns the node with the given index. |
567 | 570 |
/// |
568 |
/// Returns the node with the given index. Because it is a static |
|
569 |
/// size graph the node's of the graph can be indexed in the range |
|
570 |
/// <tt>[0..nodeNum()-1]</tt> and the index of the node can |
|
571 |
/// accessed by the \e index() member. |
|
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 |
/// <tt>[0..nodeNum()-1]</tt>. |
|
574 |
/// \sa index() |
|
572 | 575 |
Node operator()(int ix) const { return Parent::operator()(ix); } |
573 | 576 |
|
574 |
/// \brief Returns the index of the node. |
|
577 |
/// \brief Returns the index of the given node. |
|
575 | 578 |
/// |
576 |
/// Returns the index of the node. Because it is a static size |
|
577 |
/// graph the node's of the graph can be indexed in the range |
|
578 |
/// <tt>[0..nodeNum()-1]</tt> and the index of the node can |
|
579 |
/// accessed by the \e index() member. |
|
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() |
|
580 | 583 |
int index(const Node& node) const { return Parent::index(node); } |
581 | 584 |
|
582 |
/// \brief Number of nodes. |
|
583 |
int nodeNum() const { return Parent::nodeNum(); } |
|
584 |
/// \brief Number of arcs. |
|
585 |
int arcNum() const { return Parent::arcNum(); } |
|
586 |
/// \brief Number of edges. |
|
587 |
int edgeNum() const { return Parent::edgeNum(); } |
|
588 |
|
|
589 |
/// \brief Returns the arc connects the given nodes. |
|
585 |
/// \brief Returns the arc connecting the given nodes. |
|
590 | 586 |
/// |
591 |
/// Returns the arc |
|
587 |
/// Returns the arc connecting the given nodes. |
|
592 | 588 |
Arc arc(const Node& s, const Node& t) const { |
593 | 589 |
return Parent::arc(s, t); |
594 | 590 |
} |
595 | 591 |
|
596 | 592 |
/// \brief Returns the edge connects the given nodes. |
597 | 593 |
/// |
598 | 594 |
/// Returns the edge connects the given nodes. |
599 | 595 |
Edge edge(const Node& u, const Node& v) const { |
600 | 596 |
return Parent::edge(u, v); |
601 | 597 |
} |
598 |
|
|
599 |
/// \brief Number of nodes. |
|
600 |
int nodeNum() const { return Parent::nodeNum(); } |
|
601 |
/// \brief Number of arcs. |
|
602 |
int arcNum() const { return Parent::arcNum(); } |
|
603 |
/// \brief Number of edges. |
|
604 |
int edgeNum() const { return Parent::edgeNum(); } |
|
605 |
|
|
602 | 606 |
}; |
603 | 607 |
|
604 | 608 |
|
605 | 609 |
} //namespace lemon |
606 | 610 |
|
607 | 611 |
|
608 | 612 |
#endif //LEMON_FULL_GRAPH_H |
... | ... |
@@ -49,173 +49,173 @@ |
49 | 49 |
checkGraphArcList(G, 1); |
50 | 50 |
|
51 | 51 |
checkGraphOutArcList(G, n1, 1); |
52 | 52 |
checkGraphOutArcList(G, n2, 0); |
53 | 53 |
checkGraphOutArcList(G, n3, 0); |
54 | 54 |
|
55 | 55 |
checkGraphInArcList(G, n1, 0); |
56 | 56 |
checkGraphInArcList(G, n2, 1); |
57 | 57 |
checkGraphInArcList(G, n3, 0); |
58 | 58 |
|
59 | 59 |
checkGraphConArcList(G, 1); |
60 | 60 |
|
61 | 61 |
Arc a2 = G.addArc(n2, n1), a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); |
62 | 62 |
checkGraphNodeList(G, 3); |
63 | 63 |
checkGraphArcList(G, 4); |
64 | 64 |
|
65 | 65 |
checkGraphOutArcList(G, n1, 1); |
66 | 66 |
checkGraphOutArcList(G, n2, 3); |
67 | 67 |
checkGraphOutArcList(G, n3, 0); |
68 | 68 |
|
69 | 69 |
checkGraphInArcList(G, n1, 1); |
70 | 70 |
checkGraphInArcList(G, n2, 1); |
71 | 71 |
checkGraphInArcList(G, n3, 2); |
72 | 72 |
|
73 | 73 |
checkGraphConArcList(G, 4); |
74 | 74 |
|
75 | 75 |
checkNodeIds(G); |
76 | 76 |
checkArcIds(G); |
77 | 77 |
checkGraphNodeMap(G); |
78 | 78 |
checkGraphArcMap(G); |
79 | 79 |
|
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
void checkFullDigraph(int num) { |
83 | 83 |
typedef FullDigraph Digraph; |
84 | 84 |
DIGRAPH_TYPEDEFS(Digraph); |
85 | 85 |
Digraph G(num); |
86 | 86 |
|
87 | 87 |
checkGraphNodeList(G, num); |
88 | 88 |
checkGraphArcList(G, num * num); |
89 | 89 |
|
90 | 90 |
for (NodeIt n(G); n != INVALID; ++n) { |
91 | 91 |
checkGraphOutArcList(G, n, num); |
92 | 92 |
checkGraphInArcList(G, n, num); |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
checkGraphConArcList(G, num * num); |
96 | 96 |
|
97 | 97 |
checkNodeIds(G); |
98 | 98 |
checkArcIds(G); |
99 | 99 |
checkGraphNodeMap(G); |
100 | 100 |
checkGraphArcMap(G); |
101 | 101 |
|
102 | 102 |
for (int i = 0; i < G.nodeNum(); ++i) { |
103 | 103 |
check(G.index(G(i)) == i, "Wrong index"); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
for (NodeIt s(G); s != INVALID; ++s) { |
107 | 107 |
for (NodeIt t(G); t != INVALID; ++t) { |
108 | 108 |
Arc a = G.arc(s, t); |
109 | 109 |
check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup"); |
110 | 110 |
} |
111 | 111 |
} |
112 | 112 |
|
113 | 113 |
} |
114 | 114 |
|
115 | 115 |
|
116 | 116 |
void checkConcepts() { |
117 | 117 |
{ // Checking digraph components |
118 | 118 |
checkConcept<BaseDigraphComponent, BaseDigraphComponent >(); |
119 | 119 |
|
120 | 120 |
checkConcept<IDableDigraphComponent<>, |
121 | 121 |
IDableDigraphComponent<> >(); |
122 | 122 |
|
123 | 123 |
checkConcept<IterableDigraphComponent<>, |
124 | 124 |
IterableDigraphComponent<> >(); |
125 | 125 |
|
126 | 126 |
checkConcept<MappableDigraphComponent<>, |
127 | 127 |
MappableDigraphComponent<> >(); |
128 | 128 |
} |
129 | 129 |
{ // Checking skeleton digraph |
130 | 130 |
checkConcept<Digraph, Digraph>(); |
131 | 131 |
} |
132 | 132 |
{ // Checking ListDigraph |
133 | 133 |
checkConcept<Digraph, ListDigraph>(); |
134 | 134 |
checkConcept<AlterableDigraphComponent<>, ListDigraph>(); |
135 | 135 |
checkConcept<ExtendableDigraphComponent<>, ListDigraph>(); |
136 | 136 |
checkConcept<ClearableDigraphComponent<>, ListDigraph>(); |
137 | 137 |
checkConcept<ErasableDigraphComponent<>, ListDigraph>(); |
138 | 138 |
} |
139 | 139 |
{ // Checking SmartDigraph |
140 | 140 |
checkConcept<Digraph, SmartDigraph>(); |
141 | 141 |
checkConcept<AlterableDigraphComponent<>, SmartDigraph>(); |
142 | 142 |
checkConcept<ExtendableDigraphComponent<>, SmartDigraph>(); |
143 | 143 |
checkConcept<ClearableDigraphComponent<>, SmartDigraph>(); |
144 | 144 |
} |
145 |
// { // Checking FullDigraph |
|
146 |
// checkConcept<Digraph, FullDigraph>(); |
|
147 |
// |
|
145 |
{ // Checking FullDigraph |
|
146 |
checkConcept<Digraph, FullDigraph>(); |
|
147 |
} |
|
148 | 148 |
// { // Checking HyperCubeDigraph |
149 | 149 |
// checkConcept<Digraph, HyperCubeDigraph>(); |
150 | 150 |
// } |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
template <typename Digraph> |
154 | 154 |
void checkDigraphValidity() { |
155 | 155 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
156 | 156 |
Digraph g; |
157 | 157 |
|
158 | 158 |
Node |
159 | 159 |
n1 = g.addNode(), |
160 | 160 |
n2 = g.addNode(), |
161 | 161 |
n3 = g.addNode(); |
162 | 162 |
|
163 | 163 |
Arc |
164 | 164 |
e1 = g.addArc(n1, n2), |
165 | 165 |
e2 = g.addArc(n2, n3); |
166 | 166 |
|
167 | 167 |
check(g.valid(n1), "Wrong validity check"); |
168 | 168 |
check(g.valid(e1), "Wrong validity check"); |
169 | 169 |
|
170 | 170 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
171 | 171 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
template <typename Digraph> |
175 | 175 |
void checkDigraphValidityErase() { |
176 | 176 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
177 | 177 |
Digraph g; |
178 | 178 |
|
179 | 179 |
Node |
180 | 180 |
n1 = g.addNode(), |
181 | 181 |
n2 = g.addNode(), |
182 | 182 |
n3 = g.addNode(); |
183 | 183 |
|
184 | 184 |
Arc |
185 | 185 |
e1 = g.addArc(n1, n2), |
186 | 186 |
e2 = g.addArc(n2, n3); |
187 | 187 |
|
188 | 188 |
check(g.valid(n1), "Wrong validity check"); |
189 | 189 |
check(g.valid(e1), "Wrong validity check"); |
190 | 190 |
|
191 | 191 |
g.erase(n1); |
192 | 192 |
|
193 | 193 |
check(!g.valid(n1), "Wrong validity check"); |
194 | 194 |
check(g.valid(n2), "Wrong validity check"); |
195 | 195 |
check(g.valid(n3), "Wrong validity check"); |
196 | 196 |
check(!g.valid(e1), "Wrong validity check"); |
197 | 197 |
check(g.valid(e2), "Wrong validity check"); |
198 | 198 |
|
199 | 199 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
200 | 200 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
void checkDigraphs() { |
204 | 204 |
{ // Checking ListDigraph |
205 | 205 |
checkDigraph<ListDigraph>(); |
206 | 206 |
checkDigraphValidityErase<ListDigraph>(); |
207 | 207 |
} |
208 | 208 |
{ // Checking SmartDigraph |
209 | 209 |
checkDigraph<SmartDigraph>(); |
210 | 210 |
checkDigraphValidity<SmartDigraph>(); |
211 | 211 |
} |
212 | 212 |
{ // Checking FullDigraph |
213 | 213 |
checkFullDigraph(8); |
214 | 214 |
} |
215 | 215 |
} |
216 | 216 |
|
217 | 217 |
int main() { |
218 | 218 |
checkDigraphs(); |
219 | 219 |
checkConcepts(); |
220 | 220 |
return 0; |
221 | 221 |
} |
0 comments (0 inline)