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