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