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@463
|
5 |
* Copyright (C) 2003-2009
|
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); }
|
deba@365
|
54 |
int index(const Node& node) const { 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@782
|
165 |
/// \note FullDigraph and FullGraph classes are very similar,
|
kpeter@366
|
166 |
/// but there are two differences. While this class conforms only
|
kpeter@782
|
167 |
/// to the \ref concepts::Digraph "Digraph" concept, FullGraph
|
kpeter@782
|
168 |
/// conforms to the \ref concepts::Graph "Graph" concept,
|
kpeter@782
|
169 |
/// moreover FullGraph does not contain a loop for each
|
kpeter@782
|
170 |
/// node as this class does.
|
deba@365
|
171 |
///
|
deba@365
|
172 |
/// \sa FullGraph
|
deba@365
|
173 |
class FullDigraph : public ExtendedFullDigraphBase {
|
kpeter@664
|
174 |
typedef ExtendedFullDigraphBase Parent;
|
kpeter@664
|
175 |
|
deba@365
|
176 |
public:
|
deba@365
|
177 |
|
kpeter@782
|
178 |
/// \brief Default constructor.
|
kpeter@782
|
179 |
///
|
kpeter@782
|
180 |
/// Default constructor. The number of nodes and arcs will be zero.
|
deba@365
|
181 |
FullDigraph() { construct(0); }
|
deba@365
|
182 |
|
deba@365
|
183 |
/// \brief Constructor
|
deba@365
|
184 |
///
|
kpeter@366
|
185 |
/// Constructor.
|
deba@365
|
186 |
/// \param n The number of the nodes.
|
deba@365
|
187 |
FullDigraph(int n) { construct(n); }
|
deba@365
|
188 |
|
kpeter@366
|
189 |
/// \brief Resizes the digraph
|
deba@365
|
190 |
///
|
kpeter@782
|
191 |
/// This function resizes the digraph. It fully destroys and
|
kpeter@782
|
192 |
/// rebuilds the structure, therefore the maps of the digraph will be
|
kpeter@366
|
193 |
/// reallocated automatically and the previous values will be lost.
|
deba@365
|
194 |
void resize(int n) {
|
deba@365
|
195 |
Parent::notifier(Arc()).clear();
|
deba@365
|
196 |
Parent::notifier(Node()).clear();
|
deba@365
|
197 |
construct(n);
|
deba@365
|
198 |
Parent::notifier(Node()).build();
|
deba@365
|
199 |
Parent::notifier(Arc()).build();
|
deba@365
|
200 |
}
|
deba@365
|
201 |
|
deba@365
|
202 |
/// \brief Returns the node with the given index.
|
deba@365
|
203 |
///
|
kpeter@782
|
204 |
/// Returns the node with the given index. Since this structure is
|
kpeter@782
|
205 |
/// completely static, the nodes can be indexed with integers from
|
kpeter@782
|
206 |
/// the range <tt>[0..nodeNum()-1]</tt>.
|
kpeter@366
|
207 |
/// \sa index()
|
deba@365
|
208 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
deba@365
|
209 |
|
kpeter@366
|
210 |
/// \brief Returns the index of the given node.
|
deba@365
|
211 |
///
|
kpeter@782
|
212 |
/// Returns the index of the given node. Since this structure is
|
kpeter@782
|
213 |
/// completely static, the nodes can be indexed with integers from
|
kpeter@782
|
214 |
/// the range <tt>[0..nodeNum()-1]</tt>.
|
kpeter@782
|
215 |
/// \sa operator()()
|
kpeter@782
|
216 |
int index(Node node) const { return Parent::index(node); }
|
deba@365
|
217 |
|
kpeter@366
|
218 |
/// \brief Returns the arc connecting the given nodes.
|
deba@365
|
219 |
///
|
kpeter@366
|
220 |
/// Returns the arc connecting the given nodes.
|
kpeter@782
|
221 |
Arc arc(Node u, Node v) const {
|
deba@365
|
222 |
return Parent::arc(u, v);
|
deba@365
|
223 |
}
|
deba@365
|
224 |
|
deba@365
|
225 |
/// \brief Number of nodes.
|
deba@365
|
226 |
int nodeNum() const { return Parent::nodeNum(); }
|
deba@365
|
227 |
/// \brief Number of arcs.
|
deba@365
|
228 |
int arcNum() const { return Parent::arcNum(); }
|
deba@365
|
229 |
};
|
deba@365
|
230 |
|
deba@365
|
231 |
|
deba@365
|
232 |
class FullGraphBase {
|
deba@365
|
233 |
public:
|
deba@365
|
234 |
|
deba@365
|
235 |
typedef FullGraphBase Graph;
|
deba@365
|
236 |
|
deba@365
|
237 |
class Node;
|
deba@365
|
238 |
class Arc;
|
deba@365
|
239 |
class Edge;
|
deba@365
|
240 |
|
deba@365
|
241 |
protected:
|
deba@365
|
242 |
|
kpeter@664
|
243 |
int _node_num;
|
kpeter@664
|
244 |
int _edge_num;
|
kpeter@664
|
245 |
|
deba@365
|
246 |
FullGraphBase() {}
|
deba@365
|
247 |
|
deba@365
|
248 |
void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; }
|
deba@365
|
249 |
|
deba@365
|
250 |
int _uid(int e) const {
|
deba@365
|
251 |
int u = e / _node_num;
|
deba@365
|
252 |
int v = e % _node_num;
|
deba@365
|
253 |
return u < v ? u : _node_num - 2 - u;
|
deba@365
|
254 |
}
|
deba@365
|
255 |
|
deba@365
|
256 |
int _vid(int e) const {
|
deba@365
|
257 |
int u = e / _node_num;
|
deba@365
|
258 |
int v = e % _node_num;
|
deba@365
|
259 |
return u < v ? v : _node_num - 1 - v;
|
deba@365
|
260 |
}
|
deba@365
|
261 |
|
deba@365
|
262 |
void _uvid(int e, int& u, int& v) const {
|
deba@365
|
263 |
u = e / _node_num;
|
deba@365
|
264 |
v = e % _node_num;
|
deba@365
|
265 |
if (u >= v) {
|
deba@365
|
266 |
u = _node_num - 2 - u;
|
deba@365
|
267 |
v = _node_num - 1 - v;
|
deba@365
|
268 |
}
|
deba@365
|
269 |
}
|
deba@365
|
270 |
|
deba@365
|
271 |
void _stid(int a, int& s, int& t) const {
|
deba@365
|
272 |
if ((a & 1) == 1) {
|
deba@365
|
273 |
_uvid(a >> 1, s, t);
|
deba@365
|
274 |
} else {
|
deba@365
|
275 |
_uvid(a >> 1, t, s);
|
deba@365
|
276 |
}
|
deba@365
|
277 |
}
|
deba@365
|
278 |
|
deba@365
|
279 |
int _eid(int u, int v) const {
|
deba@365
|
280 |
if (u < (_node_num - 1) / 2) {
|
deba@365
|
281 |
return u * _node_num + v;
|
deba@365
|
282 |
} else {
|
deba@365
|
283 |
return (_node_num - 1 - u) * _node_num - v - 1;
|
deba@365
|
284 |
}
|
deba@365
|
285 |
}
|
deba@365
|
286 |
|
deba@365
|
287 |
public:
|
deba@365
|
288 |
|
deba@365
|
289 |
Node operator()(int ix) const { return Node(ix); }
|
deba@365
|
290 |
int index(const Node& node) const { return node._id; }
|
deba@365
|
291 |
|
deba@365
|
292 |
Edge edge(const Node& u, const Node& v) const {
|
deba@365
|
293 |
if (u._id < v._id) {
|
deba@365
|
294 |
return Edge(_eid(u._id, v._id));
|
deba@365
|
295 |
} else if (u._id != v._id) {
|
deba@365
|
296 |
return Edge(_eid(v._id, u._id));
|
deba@365
|
297 |
} else {
|
deba@365
|
298 |
return INVALID;
|
deba@365
|
299 |
}
|
deba@365
|
300 |
}
|
deba@365
|
301 |
|
deba@365
|
302 |
Arc arc(const Node& s, const Node& t) const {
|
deba@365
|
303 |
if (s._id < t._id) {
|
deba@365
|
304 |
return Arc((_eid(s._id, t._id) << 1) | 1);
|
deba@365
|
305 |
} else if (s._id != t._id) {
|
deba@365
|
306 |
return Arc(_eid(t._id, s._id) << 1);
|
deba@365
|
307 |
} else {
|
deba@365
|
308 |
return INVALID;
|
deba@365
|
309 |
}
|
deba@365
|
310 |
}
|
deba@365
|
311 |
|
deba@365
|
312 |
typedef True NodeNumTag;
|
kpeter@372
|
313 |
typedef True ArcNumTag;
|
deba@365
|
314 |
typedef True EdgeNumTag;
|
deba@365
|
315 |
|
deba@365
|
316 |
int nodeNum() const { return _node_num; }
|
deba@365
|
317 |
int arcNum() const { return 2 * _edge_num; }
|
deba@365
|
318 |
int edgeNum() const { return _edge_num; }
|
deba@365
|
319 |
|
deba@365
|
320 |
static int id(Node node) { return node._id; }
|
deba@365
|
321 |
static int id(Arc arc) { return arc._id; }
|
deba@365
|
322 |
static int id(Edge edge) { return edge._id; }
|
deba@365
|
323 |
|
deba@365
|
324 |
int maxNodeId() const { return _node_num-1; }
|
deba@365
|
325 |
int maxArcId() const { return 2 * _edge_num-1; }
|
deba@365
|
326 |
int maxEdgeId() const { return _edge_num-1; }
|
deba@365
|
327 |
|
deba@365
|
328 |
static Node nodeFromId(int id) { return Node(id);}
|
deba@365
|
329 |
static Arc arcFromId(int id) { return Arc(id);}
|
deba@365
|
330 |
static Edge edgeFromId(int id) { return Edge(id);}
|
deba@365
|
331 |
|
deba@365
|
332 |
Node u(Edge edge) const {
|
deba@365
|
333 |
return Node(_uid(edge._id));
|
deba@365
|
334 |
}
|
deba@365
|
335 |
|
deba@365
|
336 |
Node v(Edge edge) const {
|
deba@365
|
337 |
return Node(_vid(edge._id));
|
deba@365
|
338 |
}
|
deba@365
|
339 |
|
deba@365
|
340 |
Node source(Arc arc) const {
|
deba@365
|
341 |
return Node((arc._id & 1) == 1 ?
|
deba@365
|
342 |
_uid(arc._id >> 1) : _vid(arc._id >> 1));
|
deba@365
|
343 |
}
|
deba@365
|
344 |
|
deba@365
|
345 |
Node target(Arc arc) const {
|
deba@365
|
346 |
return Node((arc._id & 1) == 1 ?
|
deba@365
|
347 |
_vid(arc._id >> 1) : _uid(arc._id >> 1));
|
deba@365
|
348 |
}
|
deba@365
|
349 |
|
deba@365
|
350 |
typedef True FindEdgeTag;
|
kpeter@372
|
351 |
typedef True FindArcTag;
|
deba@365
|
352 |
|
deba@365
|
353 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
deba@365
|
354 |
return prev != INVALID ? INVALID : edge(u, v);
|
deba@365
|
355 |
}
|
deba@365
|
356 |
|
deba@365
|
357 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const {
|
deba@365
|
358 |
return prev != INVALID ? INVALID : arc(s, t);
|
deba@365
|
359 |
}
|
deba@365
|
360 |
|
deba@365
|
361 |
class Node {
|
deba@365
|
362 |
friend class FullGraphBase;
|
deba@365
|
363 |
|
deba@365
|
364 |
protected:
|
deba@365
|
365 |
int _id;
|
deba@365
|
366 |
Node(int id) : _id(id) {}
|
deba@365
|
367 |
public:
|
deba@365
|
368 |
Node() {}
|
deba@365
|
369 |
Node (Invalid) { _id = -1; }
|
deba@365
|
370 |
bool operator==(const Node node) const {return _id == node._id;}
|
deba@365
|
371 |
bool operator!=(const Node node) const {return _id != node._id;}
|
deba@365
|
372 |
bool operator<(const Node node) const {return _id < node._id;}
|
deba@365
|
373 |
};
|
deba@365
|
374 |
|
deba@365
|
375 |
class Edge {
|
deba@365
|
376 |
friend class FullGraphBase;
|
kpeter@366
|
377 |
friend class Arc;
|
deba@365
|
378 |
|
deba@365
|
379 |
protected:
|
deba@365
|
380 |
int _id;
|
deba@365
|
381 |
|
deba@365
|
382 |
Edge(int id) : _id(id) {}
|
deba@365
|
383 |
|
deba@365
|
384 |
public:
|
deba@365
|
385 |
Edge() { }
|
deba@365
|
386 |
Edge (Invalid) { _id = -1; }
|
deba@365
|
387 |
|
deba@365
|
388 |
bool operator==(const Edge edge) const {return _id == edge._id;}
|
deba@365
|
389 |
bool operator!=(const Edge edge) const {return _id != edge._id;}
|
deba@365
|
390 |
bool operator<(const Edge edge) const {return _id < edge._id;}
|
deba@365
|
391 |
};
|
deba@365
|
392 |
|
deba@365
|
393 |
class Arc {
|
deba@365
|
394 |
friend class FullGraphBase;
|
deba@365
|
395 |
|
deba@365
|
396 |
protected:
|
deba@365
|
397 |
int _id;
|
deba@365
|
398 |
|
deba@365
|
399 |
Arc(int id) : _id(id) {}
|
deba@365
|
400 |
|
deba@365
|
401 |
public:
|
deba@365
|
402 |
Arc() { }
|
deba@365
|
403 |
Arc (Invalid) { _id = -1; }
|
deba@365
|
404 |
|
deba@365
|
405 |
operator Edge() const { return Edge(_id != -1 ? (_id >> 1) : -1); }
|
deba@365
|
406 |
|
deba@365
|
407 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
deba@365
|
408 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
deba@365
|
409 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
deba@365
|
410 |
};
|
deba@365
|
411 |
|
deba@365
|
412 |
static bool direction(Arc arc) {
|
deba@365
|
413 |
return (arc._id & 1) == 1;
|
deba@365
|
414 |
}
|
deba@365
|
415 |
|
deba@365
|
416 |
static Arc direct(Edge edge, bool dir) {
|
deba@365
|
417 |
return Arc((edge._id << 1) | (dir ? 1 : 0));
|
deba@365
|
418 |
}
|
deba@365
|
419 |
|
deba@365
|
420 |
void first(Node& node) const {
|
deba@365
|
421 |
node._id = _node_num - 1;
|
deba@365
|
422 |
}
|
deba@365
|
423 |
|
deba@365
|
424 |
static void next(Node& node) {
|
deba@365
|
425 |
--node._id;
|
deba@365
|
426 |
}
|
deba@365
|
427 |
|
deba@365
|
428 |
void first(Arc& arc) const {
|
deba@365
|
429 |
arc._id = (_edge_num << 1) - 1;
|
deba@365
|
430 |
}
|
deba@365
|
431 |
|
deba@365
|
432 |
static void next(Arc& arc) {
|
deba@365
|
433 |
--arc._id;
|
deba@365
|
434 |
}
|
deba@365
|
435 |
|
deba@365
|
436 |
void first(Edge& edge) const {
|
deba@365
|
437 |
edge._id = _edge_num - 1;
|
deba@365
|
438 |
}
|
deba@365
|
439 |
|
deba@365
|
440 |
static void next(Edge& edge) {
|
deba@365
|
441 |
--edge._id;
|
deba@365
|
442 |
}
|
deba@365
|
443 |
|
deba@365
|
444 |
void firstOut(Arc& arc, const Node& node) const {
|
deba@365
|
445 |
int s = node._id, t = _node_num - 1;
|
deba@365
|
446 |
if (s < t) {
|
deba@365
|
447 |
arc._id = (_eid(s, t) << 1) | 1;
|
deba@365
|
448 |
} else {
|
deba@365
|
449 |
--t;
|
deba@365
|
450 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
|
deba@365
|
451 |
}
|
deba@365
|
452 |
}
|
deba@365
|
453 |
|
deba@365
|
454 |
void nextOut(Arc& arc) const {
|
deba@365
|
455 |
int s, t;
|
deba@365
|
456 |
_stid(arc._id, s, t);
|
deba@365
|
457 |
--t;
|
deba@365
|
458 |
if (s < t) {
|
deba@365
|
459 |
arc._id = (_eid(s, t) << 1) | 1;
|
deba@365
|
460 |
} else {
|
deba@365
|
461 |
if (s == t) --t;
|
deba@365
|
462 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1);
|
deba@365
|
463 |
}
|
deba@365
|
464 |
}
|
deba@365
|
465 |
|
deba@365
|
466 |
void firstIn(Arc& arc, const Node& node) const {
|
deba@365
|
467 |
int s = _node_num - 1, t = node._id;
|
deba@365
|
468 |
if (s > t) {
|
deba@365
|
469 |
arc._id = (_eid(t, s) << 1);
|
deba@365
|
470 |
} else {
|
deba@365
|
471 |
--s;
|
deba@365
|
472 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
|
deba@365
|
473 |
}
|
deba@365
|
474 |
}
|
deba@365
|
475 |
|
deba@365
|
476 |
void nextIn(Arc& arc) const {
|
deba@365
|
477 |
int s, t;
|
deba@365
|
478 |
_stid(arc._id, s, t);
|
deba@365
|
479 |
--s;
|
deba@365
|
480 |
if (s > t) {
|
deba@365
|
481 |
arc._id = (_eid(t, s) << 1);
|
deba@365
|
482 |
} else {
|
deba@365
|
483 |
if (s == t) --s;
|
deba@365
|
484 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1);
|
deba@365
|
485 |
}
|
deba@365
|
486 |
}
|
deba@365
|
487 |
|
deba@365
|
488 |
void firstInc(Edge& edge, bool& dir, const Node& node) const {
|
deba@365
|
489 |
int u = node._id, v = _node_num - 1;
|
deba@365
|
490 |
if (u < v) {
|
deba@365
|
491 |
edge._id = _eid(u, v);
|
deba@365
|
492 |
dir = true;
|
deba@365
|
493 |
} else {
|
deba@365
|
494 |
--v;
|
deba@365
|
495 |
edge._id = (v != -1 ? _eid(v, u) : -1);
|
deba@365
|
496 |
dir = false;
|
deba@365
|
497 |
}
|
deba@365
|
498 |
}
|
deba@365
|
499 |
|
deba@365
|
500 |
void nextInc(Edge& edge, bool& dir) const {
|
deba@365
|
501 |
int u, v;
|
deba@365
|
502 |
if (dir) {
|
deba@365
|
503 |
_uvid(edge._id, u, v);
|
deba@365
|
504 |
--v;
|
deba@365
|
505 |
if (u < v) {
|
deba@365
|
506 |
edge._id = _eid(u, v);
|
deba@365
|
507 |
} else {
|
deba@365
|
508 |
--v;
|
deba@365
|
509 |
edge._id = (v != -1 ? _eid(v, u) : -1);
|
deba@365
|
510 |
dir = false;
|
deba@365
|
511 |
}
|
deba@365
|
512 |
} else {
|
deba@365
|
513 |
_uvid(edge._id, v, u);
|
deba@365
|
514 |
--v;
|
deba@365
|
515 |
edge._id = (v != -1 ? _eid(v, u) : -1);
|
deba@365
|
516 |
}
|
deba@365
|
517 |
}
|
deba@365
|
518 |
|
deba@365
|
519 |
};
|
deba@365
|
520 |
|
deba@365
|
521 |
typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
|
deba@365
|
522 |
|
deba@365
|
523 |
/// \ingroup graphs
|
deba@365
|
524 |
///
|
deba@365
|
525 |
/// \brief An undirected full graph class.
|
deba@365
|
526 |
///
|
kpeter@782
|
527 |
/// FullGraph is a simple and fast implmenetation of undirected full
|
kpeter@782
|
528 |
/// (complete) graphs. It contains an edge between every distinct pair
|
kpeter@782
|
529 |
/// of nodes, therefore the number of edges is <tt>n(n-1)/2</tt>.
|
kpeter@782
|
530 |
/// This class is completely static and it needs constant memory space.
|
kpeter@782
|
531 |
/// Thus you can neither add nor delete nodes or edges, however
|
kpeter@782
|
532 |
/// the structure can be resized using resize().
|
deba@365
|
533 |
///
|
kpeter@782
|
534 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept".
|
kpeter@782
|
535 |
/// Most of its member functions and nested classes are documented
|
kpeter@782
|
536 |
/// only in the concept class.
|
deba@365
|
537 |
///
|
kpeter@782
|
538 |
/// \note FullDigraph and FullGraph classes are very similar,
|
kpeter@782
|
539 |
/// but there are two differences. While FullDigraph
|
kpeter@366
|
540 |
/// conforms only to the \ref concepts::Digraph "Digraph" concept,
|
kpeter@366
|
541 |
/// this class conforms to the \ref concepts::Graph "Graph" concept,
|
kpeter@782
|
542 |
/// moreover this class does not contain a loop for each
|
kpeter@782
|
543 |
/// node as FullDigraph does.
|
deba@365
|
544 |
///
|
deba@365
|
545 |
/// \sa FullDigraph
|
deba@365
|
546 |
class FullGraph : public ExtendedFullGraphBase {
|
kpeter@664
|
547 |
typedef ExtendedFullGraphBase Parent;
|
kpeter@664
|
548 |
|
deba@365
|
549 |
public:
|
deba@365
|
550 |
|
kpeter@782
|
551 |
/// \brief Default constructor.
|
kpeter@782
|
552 |
///
|
kpeter@782
|
553 |
/// Default constructor. The number of nodes and edges will be zero.
|
deba@365
|
554 |
FullGraph() { construct(0); }
|
deba@365
|
555 |
|
deba@365
|
556 |
/// \brief Constructor
|
deba@365
|
557 |
///
|
kpeter@366
|
558 |
/// Constructor.
|
deba@365
|
559 |
/// \param n The number of the nodes.
|
deba@365
|
560 |
FullGraph(int n) { construct(n); }
|
deba@365
|
561 |
|
kpeter@366
|
562 |
/// \brief Resizes the graph
|
deba@365
|
563 |
///
|
kpeter@782
|
564 |
/// This function resizes the graph. It fully destroys and
|
kpeter@782
|
565 |
/// rebuilds the structure, therefore the maps of the graph will be
|
kpeter@366
|
566 |
/// reallocated automatically and the previous values will be lost.
|
deba@365
|
567 |
void resize(int n) {
|
deba@365
|
568 |
Parent::notifier(Arc()).clear();
|
deba@365
|
569 |
Parent::notifier(Edge()).clear();
|
deba@365
|
570 |
Parent::notifier(Node()).clear();
|
deba@365
|
571 |
construct(n);
|
deba@365
|
572 |
Parent::notifier(Node()).build();
|
deba@365
|
573 |
Parent::notifier(Edge()).build();
|
deba@365
|
574 |
Parent::notifier(Arc()).build();
|
deba@365
|
575 |
}
|
deba@365
|
576 |
|
deba@365
|
577 |
/// \brief Returns the node with the given index.
|
deba@365
|
578 |
///
|
kpeter@782
|
579 |
/// Returns the node with the given index. Since this structure is
|
kpeter@782
|
580 |
/// completely static, the nodes can be indexed with integers from
|
kpeter@782
|
581 |
/// the range <tt>[0..nodeNum()-1]</tt>.
|
kpeter@366
|
582 |
/// \sa index()
|
deba@365
|
583 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
deba@365
|
584 |
|
kpeter@366
|
585 |
/// \brief Returns the index of the given node.
|
deba@365
|
586 |
///
|
kpeter@782
|
587 |
/// Returns the index of the given node. Since this structure is
|
kpeter@782
|
588 |
/// completely static, the nodes can be indexed with integers from
|
kpeter@782
|
589 |
/// the range <tt>[0..nodeNum()-1]</tt>.
|
kpeter@782
|
590 |
/// \sa operator()()
|
kpeter@782
|
591 |
int index(Node node) const { return Parent::index(node); }
|
deba@365
|
592 |
|
kpeter@366
|
593 |
/// \brief Returns the arc connecting the given nodes.
|
deba@365
|
594 |
///
|
kpeter@366
|
595 |
/// Returns the arc connecting the given nodes.
|
kpeter@782
|
596 |
Arc arc(Node s, Node t) const {
|
deba@365
|
597 |
return Parent::arc(s, t);
|
deba@365
|
598 |
}
|
deba@365
|
599 |
|
kpeter@782
|
600 |
/// \brief Returns the edge connecting the given nodes.
|
deba@365
|
601 |
///
|
kpeter@782
|
602 |
/// Returns the edge connecting the given nodes.
|
kpeter@782
|
603 |
Edge edge(Node u, Node v) const {
|
deba@365
|
604 |
return Parent::edge(u, v);
|
deba@365
|
605 |
}
|
kpeter@366
|
606 |
|
kpeter@366
|
607 |
/// \brief Number of nodes.
|
kpeter@366
|
608 |
int nodeNum() const { return Parent::nodeNum(); }
|
kpeter@366
|
609 |
/// \brief Number of arcs.
|
kpeter@366
|
610 |
int arcNum() const { return Parent::arcNum(); }
|
kpeter@366
|
611 |
/// \brief Number of edges.
|
kpeter@366
|
612 |
int edgeNum() const { return Parent::edgeNum(); }
|
kpeter@366
|
613 |
|
deba@365
|
614 |
};
|
deba@365
|
615 |
|
deba@365
|
616 |
|
deba@365
|
617 |
} //namespace lemon
|
deba@365
|
618 |
|
deba@365
|
619 |
|
deba@365
|
620 |
#endif //LEMON_FULL_GRAPH_H
|