alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@906
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@1956
|
5 |
* Copyright (C) 2003-2006
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@906
|
8 |
*
|
alpar@906
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
12 |
*
|
alpar@906
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
15 |
* purpose.
|
alpar@906
|
16 |
*
|
alpar@906
|
17 |
*/
|
alpar@591
|
18 |
|
alpar@921
|
19 |
#ifndef LEMON_FULL_GRAPH_H
|
alpar@921
|
20 |
#define LEMON_FULL_GRAPH_H
|
alpar@591
|
21 |
|
deba@983
|
22 |
#include <cmath>
|
deba@983
|
23 |
|
klao@946
|
24 |
|
deba@1307
|
25 |
#include <lemon/bits/iterable_graph_extender.h>
|
deba@1307
|
26 |
#include <lemon/bits/alteration_notifier.h>
|
deba@1703
|
27 |
#include <lemon/bits/static_map.h>
|
deba@1791
|
28 |
#include <lemon/bits/graph_extender.h>
|
deba@1566
|
29 |
|
klao@977
|
30 |
#include <lemon/invalid.h>
|
klao@977
|
31 |
#include <lemon/utility.h>
|
klao@977
|
32 |
|
klao@977
|
33 |
|
alpar@591
|
34 |
///\ingroup graphs
|
alpar@591
|
35 |
///\file
|
klao@1909
|
36 |
///\brief FullGraph and FullUGraph classes.
|
alpar@591
|
37 |
|
alpar@591
|
38 |
|
alpar@921
|
39 |
namespace lemon {
|
alpar@591
|
40 |
|
klao@946
|
41 |
class FullGraphBase {
|
deba@1566
|
42 |
int _nodeNum;
|
deba@1566
|
43 |
int _edgeNum;
|
alpar@591
|
44 |
public:
|
deba@782
|
45 |
|
klao@946
|
46 |
typedef FullGraphBase Graph;
|
alpar@591
|
47 |
|
alpar@591
|
48 |
class Node;
|
alpar@591
|
49 |
class Edge;
|
deba@782
|
50 |
|
alpar@591
|
51 |
public:
|
alpar@591
|
52 |
|
klao@946
|
53 |
FullGraphBase() {}
|
klao@946
|
54 |
|
klao@946
|
55 |
|
alpar@591
|
56 |
///Creates a full graph with \c n nodes.
|
deba@1566
|
57 |
void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
|
alpar@591
|
58 |
///
|
klao@946
|
59 |
// FullGraphBase(const FullGraphBase &_g)
|
deba@1566
|
60 |
// : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
|
alpar@591
|
61 |
|
klao@977
|
62 |
typedef True NodeNumTag;
|
klao@977
|
63 |
typedef True EdgeNumTag;
|
klao@977
|
64 |
|
alpar@813
|
65 |
///Number of nodes.
|
deba@1566
|
66 |
int nodeNum() const { return _nodeNum; }
|
alpar@813
|
67 |
///Number of edges.
|
deba@1566
|
68 |
int edgeNum() const { return _edgeNum; }
|
alpar@591
|
69 |
|
alpar@813
|
70 |
/// Maximum node ID.
|
alpar@813
|
71 |
|
alpar@813
|
72 |
/// Maximum node ID.
|
alpar@813
|
73 |
///\sa id(Node)
|
deba@1791
|
74 |
int maxNodeId() const { return _nodeNum-1; }
|
alpar@813
|
75 |
/// Maximum edge ID.
|
alpar@813
|
76 |
|
alpar@813
|
77 |
/// Maximum edge ID.
|
alpar@813
|
78 |
///\sa id(Edge)
|
deba@1791
|
79 |
int maxEdgeId() const { return _edgeNum-1; }
|
alpar@591
|
80 |
|
deba@1566
|
81 |
Node source(Edge e) const { return e.id % _nodeNum; }
|
deba@1566
|
82 |
Node target(Edge e) const { return e.id / _nodeNum; }
|
alpar@591
|
83 |
|
alpar@591
|
84 |
|
alpar@813
|
85 |
/// Node ID.
|
alpar@813
|
86 |
|
alpar@813
|
87 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
alpar@813
|
88 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
alpar@813
|
89 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
alpar@813
|
90 |
///
|
alpar@813
|
91 |
/// The ID of the \ref INVALID node is -1.
|
alpar@813
|
92 |
///\return The ID of the node \c v.
|
klao@946
|
93 |
|
klao@946
|
94 |
static int id(Node v) { return v.id; }
|
alpar@813
|
95 |
/// Edge ID.
|
alpar@813
|
96 |
|
alpar@813
|
97 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
alpar@813
|
98 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
alpar@813
|
99 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
alpar@813
|
100 |
///
|
alpar@813
|
101 |
/// The ID of the \ref INVALID edge is -1.
|
alpar@813
|
102 |
///\return The ID of the edge \c e.
|
klao@946
|
103 |
static int id(Edge e) { return e.id; }
|
alpar@591
|
104 |
|
deba@1791
|
105 |
static Node nodeFromId(int id) { return Node(id);}
|
deba@1106
|
106 |
|
deba@1791
|
107 |
static Edge edgeFromId(int id) { return Edge(id);}
|
deba@1106
|
108 |
|
deba@1566
|
109 |
typedef True FindEdgeTag;
|
deba@1566
|
110 |
|
alpar@774
|
111 |
/// Finds an edge between two nodes.
|
alpar@774
|
112 |
|
alpar@774
|
113 |
/// Finds an edge from node \c u to node \c v.
|
alpar@774
|
114 |
///
|
alpar@774
|
115 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@774
|
116 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@774
|
117 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@774
|
118 |
/// \return The found edge or INVALID if there is no such an edge.
|
deba@1566
|
119 |
Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
|
klao@946
|
120 |
return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
|
alpar@774
|
121 |
}
|
alpar@774
|
122 |
|
alpar@774
|
123 |
|
alpar@591
|
124 |
class Node {
|
klao@946
|
125 |
friend class FullGraphBase;
|
alpar@591
|
126 |
|
alpar@591
|
127 |
protected:
|
klao@946
|
128 |
int id;
|
alpar@1643
|
129 |
Node(int _id) : id(_id) {}
|
alpar@591
|
130 |
public:
|
alpar@591
|
131 |
Node() {}
|
alpar@1643
|
132 |
Node (Invalid) : id(-1) {}
|
klao@946
|
133 |
bool operator==(const Node node) const {return id == node.id;}
|
klao@946
|
134 |
bool operator!=(const Node node) const {return id != node.id;}
|
klao@946
|
135 |
bool operator<(const Node node) const {return id < node.id;}
|
alpar@591
|
136 |
};
|
alpar@591
|
137 |
|
klao@946
|
138 |
|
klao@946
|
139 |
|
klao@946
|
140 |
class Edge {
|
klao@946
|
141 |
friend class FullGraphBase;
|
klao@946
|
142 |
|
klao@946
|
143 |
protected:
|
deba@1566
|
144 |
int id; // _nodeNum * target + source;
|
klao@946
|
145 |
|
klao@946
|
146 |
Edge(int _id) : id(_id) {}
|
klao@946
|
147 |
|
alpar@986
|
148 |
Edge(const FullGraphBase& _graph, int source, int target)
|
deba@1566
|
149 |
: id(_graph._nodeNum * target+source) {}
|
alpar@591
|
150 |
public:
|
klao@946
|
151 |
Edge() { }
|
klao@946
|
152 |
Edge (Invalid) { id = -1; }
|
klao@946
|
153 |
bool operator==(const Edge edge) const {return id == edge.id;}
|
klao@946
|
154 |
bool operator!=(const Edge edge) const {return id != edge.id;}
|
klao@946
|
155 |
bool operator<(const Edge edge) const {return id < edge.id;}
|
alpar@591
|
156 |
};
|
alpar@591
|
157 |
|
klao@946
|
158 |
void first(Node& node) const {
|
deba@1566
|
159 |
node.id = _nodeNum-1;
|
klao@946
|
160 |
}
|
alpar@591
|
161 |
|
klao@946
|
162 |
static void next(Node& node) {
|
klao@946
|
163 |
--node.id;
|
klao@946
|
164 |
}
|
klao@946
|
165 |
|
klao@946
|
166 |
void first(Edge& edge) const {
|
deba@1566
|
167 |
edge.id = _edgeNum-1;
|
klao@946
|
168 |
}
|
klao@946
|
169 |
|
klao@946
|
170 |
static void next(Edge& edge) {
|
klao@946
|
171 |
--edge.id;
|
klao@946
|
172 |
}
|
klao@946
|
173 |
|
klao@946
|
174 |
void firstOut(Edge& edge, const Node& node) const {
|
deba@1566
|
175 |
edge.id = _edgeNum + node.id - _nodeNum;
|
klao@946
|
176 |
}
|
klao@946
|
177 |
|
klao@946
|
178 |
void nextOut(Edge& edge) const {
|
deba@1566
|
179 |
edge.id -= _nodeNum;
|
klao@946
|
180 |
if (edge.id < 0) edge.id = -1;
|
klao@946
|
181 |
}
|
klao@946
|
182 |
|
klao@946
|
183 |
void firstIn(Edge& edge, const Node& node) const {
|
deba@1566
|
184 |
edge.id = node.id * _nodeNum;
|
klao@946
|
185 |
}
|
alpar@591
|
186 |
|
klao@946
|
187 |
void nextIn(Edge& edge) const {
|
klao@946
|
188 |
++edge.id;
|
deba@1566
|
189 |
if (edge.id % _nodeNum == 0) edge.id = -1;
|
klao@946
|
190 |
}
|
alpar@591
|
191 |
|
alpar@591
|
192 |
};
|
alpar@591
|
193 |
|
deba@1703
|
194 |
typedef StaticMappableGraphExtender<
|
deba@1669
|
195 |
IterableGraphExtender<
|
deba@1791
|
196 |
AlterableGraphExtender<
|
deba@1791
|
197 |
GraphExtender<FullGraphBase> > > > ExtendedFullGraphBase;
|
klao@946
|
198 |
|
deba@1566
|
199 |
/// \ingroup graphs
|
alpar@951
|
200 |
///
|
deba@1566
|
201 |
/// \brief A full graph class.
|
deba@1566
|
202 |
///
|
deba@1566
|
203 |
/// This is a simple and fast directed full graph implementation.
|
deba@1566
|
204 |
/// It is completely static, so you can neither add nor delete either
|
deba@1566
|
205 |
/// edges or nodes.
|
deba@1566
|
206 |
/// Thus it conforms to
|
deba@1566
|
207 |
/// the \ref concept::StaticGraph "StaticGraph" concept
|
deba@1566
|
208 |
/// \sa concept::StaticGraph.
|
deba@1566
|
209 |
///
|
deba@1566
|
210 |
/// \author Alpar Juttner
|
deba@1669
|
211 |
class FullGraph : public ExtendedFullGraphBase {
|
klao@946
|
212 |
public:
|
klao@946
|
213 |
|
klao@946
|
214 |
FullGraph(int n) { construct(n); }
|
klao@946
|
215 |
};
|
klao@946
|
216 |
|
deba@983
|
217 |
|
klao@1909
|
218 |
class FullUGraphBase {
|
deba@1566
|
219 |
int _nodeNum;
|
deba@1566
|
220 |
int _edgeNum;
|
deba@983
|
221 |
public:
|
deba@983
|
222 |
|
klao@1909
|
223 |
typedef FullUGraphBase Graph;
|
deba@983
|
224 |
|
deba@983
|
225 |
class Node;
|
deba@983
|
226 |
class Edge;
|
deba@983
|
227 |
|
deba@983
|
228 |
public:
|
deba@983
|
229 |
|
klao@1909
|
230 |
FullUGraphBase() {}
|
deba@983
|
231 |
|
deba@983
|
232 |
|
deba@983
|
233 |
///Creates a full graph with \c n nodes.
|
deba@1566
|
234 |
void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
|
deba@983
|
235 |
///
|
deba@983
|
236 |
// FullGraphBase(const FullGraphBase &_g)
|
deba@1566
|
237 |
// : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
|
deba@983
|
238 |
|
deba@983
|
239 |
typedef True NodeNumTag;
|
deba@983
|
240 |
typedef True EdgeNumTag;
|
deba@983
|
241 |
|
deba@983
|
242 |
///Number of nodes.
|
deba@1566
|
243 |
int nodeNum() const { return _nodeNum; }
|
deba@983
|
244 |
///Number of edges.
|
deba@1566
|
245 |
int edgeNum() const { return _edgeNum; }
|
deba@983
|
246 |
|
deba@983
|
247 |
/// Maximum node ID.
|
deba@983
|
248 |
|
deba@983
|
249 |
/// Maximum node ID.
|
deba@983
|
250 |
///\sa id(Node)
|
deba@1791
|
251 |
int maxNodeId() const { return _nodeNum-1; }
|
deba@983
|
252 |
/// Maximum edge ID.
|
deba@983
|
253 |
|
deba@983
|
254 |
/// Maximum edge ID.
|
deba@983
|
255 |
///\sa id(Edge)
|
deba@1791
|
256 |
int maxEdgeId() const { return _edgeNum-1; }
|
deba@983
|
257 |
|
alpar@986
|
258 |
Node source(Edge e) const {
|
deba@983
|
259 |
/// \todo we may do it faster
|
alpar@1643
|
260 |
return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
|
deba@983
|
261 |
}
|
deba@983
|
262 |
|
alpar@986
|
263 |
Node target(Edge e) const {
|
alpar@986
|
264 |
int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
|
alpar@1643
|
265 |
return Node(e.id - (source) * (source - 1) / 2);
|
deba@983
|
266 |
}
|
deba@983
|
267 |
|
deba@983
|
268 |
|
deba@983
|
269 |
/// Node ID.
|
deba@983
|
270 |
|
deba@983
|
271 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
deba@983
|
272 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
deba@983
|
273 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
deba@983
|
274 |
///
|
deba@983
|
275 |
/// The ID of the \ref INVALID node is -1.
|
deba@983
|
276 |
///\return The ID of the node \c v.
|
deba@983
|
277 |
|
deba@983
|
278 |
static int id(Node v) { return v.id; }
|
deba@983
|
279 |
/// Edge ID.
|
deba@983
|
280 |
|
deba@983
|
281 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
deba@983
|
282 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
deba@983
|
283 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
deba@983
|
284 |
///
|
deba@983
|
285 |
/// The ID of the \ref INVALID edge is -1.
|
deba@983
|
286 |
///\return The ID of the edge \c e.
|
deba@983
|
287 |
static int id(Edge e) { return e.id; }
|
deba@983
|
288 |
|
deba@983
|
289 |
/// Finds an edge between two nodes.
|
deba@983
|
290 |
|
deba@983
|
291 |
/// Finds an edge from node \c u to node \c v.
|
deba@983
|
292 |
///
|
deba@983
|
293 |
/// If \c prev is \ref INVALID (this is the default value), then
|
deba@983
|
294 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
deba@983
|
295 |
/// the next edge from \c u to \c v after \c prev.
|
deba@983
|
296 |
/// \return The found edge or INVALID if there is no such an edge.
|
deba@1703
|
297 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
deba@1703
|
298 |
if (prev.id != -1 || u.id <= v.id) return -1;
|
deba@1703
|
299 |
return Edge(u.id * (u.id - 1) / 2 + v.id);
|
deba@983
|
300 |
}
|
deba@1703
|
301 |
|
deba@1703
|
302 |
typedef True FindEdgeTag;
|
deba@983
|
303 |
|
deba@983
|
304 |
|
deba@983
|
305 |
class Node {
|
klao@1909
|
306 |
friend class FullUGraphBase;
|
deba@983
|
307 |
|
deba@983
|
308 |
protected:
|
deba@983
|
309 |
int id;
|
deba@983
|
310 |
Node(int _id) { id = _id;}
|
deba@983
|
311 |
public:
|
deba@983
|
312 |
Node() {}
|
deba@983
|
313 |
Node (Invalid) { id = -1; }
|
deba@983
|
314 |
bool operator==(const Node node) const {return id == node.id;}
|
deba@983
|
315 |
bool operator!=(const Node node) const {return id != node.id;}
|
deba@983
|
316 |
bool operator<(const Node node) const {return id < node.id;}
|
deba@983
|
317 |
};
|
deba@983
|
318 |
|
deba@983
|
319 |
|
deba@983
|
320 |
|
deba@983
|
321 |
class Edge {
|
klao@1909
|
322 |
friend class FullUGraphBase;
|
deba@983
|
323 |
|
deba@983
|
324 |
protected:
|
deba@1566
|
325 |
int id; // _nodeNum * target + source;
|
deba@983
|
326 |
|
deba@983
|
327 |
Edge(int _id) : id(_id) {}
|
deba@983
|
328 |
|
deba@983
|
329 |
public:
|
deba@983
|
330 |
Edge() { }
|
deba@983
|
331 |
Edge (Invalid) { id = -1; }
|
deba@983
|
332 |
bool operator==(const Edge edge) const {return id == edge.id;}
|
deba@983
|
333 |
bool operator!=(const Edge edge) const {return id != edge.id;}
|
deba@983
|
334 |
bool operator<(const Edge edge) const {return id < edge.id;}
|
deba@983
|
335 |
};
|
deba@983
|
336 |
|
deba@983
|
337 |
void first(Node& node) const {
|
deba@1703
|
338 |
node.id = _nodeNum - 1;
|
deba@983
|
339 |
}
|
deba@983
|
340 |
|
deba@983
|
341 |
static void next(Node& node) {
|
deba@983
|
342 |
--node.id;
|
deba@983
|
343 |
}
|
deba@983
|
344 |
|
deba@983
|
345 |
void first(Edge& edge) const {
|
deba@1703
|
346 |
edge.id = _edgeNum - 1;
|
deba@983
|
347 |
}
|
deba@983
|
348 |
|
deba@983
|
349 |
static void next(Edge& edge) {
|
deba@983
|
350 |
--edge.id;
|
deba@983
|
351 |
}
|
deba@983
|
352 |
|
deba@983
|
353 |
void firstOut(Edge& edge, const Node& node) const {
|
deba@1703
|
354 |
int src = node.id;
|
deba@1703
|
355 |
int trg = 0;
|
deba@1703
|
356 |
edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
|
deba@983
|
357 |
}
|
deba@983
|
358 |
|
deba@983
|
359 |
/// \todo with specialized iterators we can make faster iterating
|
deba@1703
|
360 |
void nextOut(Edge& edge) const {
|
deba@1703
|
361 |
int src = source(edge).id;
|
deba@1703
|
362 |
int trg = target(edge).id;
|
deba@1703
|
363 |
++trg;
|
deba@1703
|
364 |
edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
|
deba@983
|
365 |
}
|
deba@983
|
366 |
|
deba@983
|
367 |
void firstIn(Edge& edge, const Node& node) const {
|
deba@1703
|
368 |
int src = node.id + 1;
|
deba@1703
|
369 |
int trg = node.id;
|
deba@1703
|
370 |
edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
|
deba@983
|
371 |
}
|
deba@983
|
372 |
|
deba@1703
|
373 |
void nextIn(Edge& edge) const {
|
deba@1703
|
374 |
int src = source(edge).id;
|
deba@1703
|
375 |
int trg = target(edge).id;
|
deba@1703
|
376 |
++src;
|
deba@1703
|
377 |
edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
|
deba@983
|
378 |
}
|
deba@983
|
379 |
|
deba@983
|
380 |
};
|
deba@983
|
381 |
|
klao@1909
|
382 |
typedef StaticMappableUGraphExtender<
|
klao@1909
|
383 |
IterableUGraphExtender<
|
klao@1909
|
384 |
AlterableUGraphExtender<
|
klao@1909
|
385 |
UGraphExtender<FullUGraphBase> > > > ExtendedFullUGraphBase;
|
alpar@1555
|
386 |
|
deba@1566
|
387 |
/// \ingroup graphs
|
deba@1566
|
388 |
///
|
deba@1566
|
389 |
/// \brief An undirected full graph class.
|
deba@1566
|
390 |
///
|
deba@1726
|
391 |
/// This is a simple and fast undirected full graph implementation.
|
deba@1566
|
392 |
/// It is completely static, so you can neither add nor delete either
|
deba@1566
|
393 |
/// edges or nodes.
|
deba@1566
|
394 |
///
|
klao@1909
|
395 |
/// The main difference beetween the \e FullGraph and \e FullUGraph class
|
deba@1566
|
396 |
/// is that this class conforms to the undirected graph concept and
|
deba@1726
|
397 |
/// it does not contain the loop edges.
|
deba@1566
|
398 |
///
|
deba@1566
|
399 |
/// \sa FullGraph
|
deba@1566
|
400 |
///
|
deba@1566
|
401 |
/// \author Balazs Dezso
|
klao@1909
|
402 |
class FullUGraph : public ExtendedFullUGraphBase {
|
deba@1566
|
403 |
public:
|
klao@1909
|
404 |
FullUGraph(int n) { construct(n); }
|
deba@1566
|
405 |
};
|
alpar@591
|
406 |
|
deba@1820
|
407 |
|
deba@1910
|
408 |
class FullBpUGraphBase {
|
deba@1820
|
409 |
protected:
|
deba@1820
|
410 |
|
deba@1910
|
411 |
int _aNodeNum;
|
deba@1910
|
412 |
int _bNodeNum;
|
deba@1820
|
413 |
|
deba@1820
|
414 |
int _edgeNum;
|
deba@1820
|
415 |
|
deba@1820
|
416 |
public:
|
deba@1820
|
417 |
|
deba@1820
|
418 |
class NodeSetError : public LogicError {
|
deba@1820
|
419 |
virtual const char* exceptionName() const {
|
deba@1910
|
420 |
return "lemon::FullBpUGraph::NodeSetError";
|
deba@1820
|
421 |
}
|
deba@1820
|
422 |
};
|
deba@1820
|
423 |
|
deba@1820
|
424 |
class Node {
|
deba@1910
|
425 |
friend class FullBpUGraphBase;
|
deba@1820
|
426 |
protected:
|
deba@1820
|
427 |
int id;
|
deba@1820
|
428 |
|
deba@1820
|
429 |
Node(int _id) : id(_id) {}
|
deba@1820
|
430 |
public:
|
deba@1820
|
431 |
Node() {}
|
deba@1820
|
432 |
Node(Invalid) { id = -1; }
|
deba@1820
|
433 |
bool operator==(const Node i) const {return id==i.id;}
|
deba@1820
|
434 |
bool operator!=(const Node i) const {return id!=i.id;}
|
deba@1820
|
435 |
bool operator<(const Node i) const {return id<i.id;}
|
deba@1820
|
436 |
};
|
deba@1820
|
437 |
|
deba@1820
|
438 |
class Edge {
|
deba@1910
|
439 |
friend class FullBpUGraphBase;
|
deba@1820
|
440 |
protected:
|
deba@1820
|
441 |
int id;
|
deba@1820
|
442 |
|
deba@1820
|
443 |
Edge(int _id) { id = _id;}
|
deba@1820
|
444 |
public:
|
deba@1820
|
445 |
Edge() {}
|
deba@1820
|
446 |
Edge (Invalid) { id = -1; }
|
deba@1820
|
447 |
bool operator==(const Edge i) const {return id==i.id;}
|
deba@1820
|
448 |
bool operator!=(const Edge i) const {return id!=i.id;}
|
deba@1820
|
449 |
bool operator<(const Edge i) const {return id<i.id;}
|
deba@1820
|
450 |
};
|
deba@1820
|
451 |
|
deba@1910
|
452 |
void construct(int aNodeNum, int bNodeNum) {
|
deba@1910
|
453 |
_aNodeNum = aNodeNum;
|
deba@1910
|
454 |
_bNodeNum = bNodeNum;
|
deba@1910
|
455 |
_edgeNum = aNodeNum * bNodeNum;
|
deba@1820
|
456 |
}
|
deba@1820
|
457 |
|
deba@1910
|
458 |
void firstANode(Node& node) const {
|
deba@1910
|
459 |
node.id = 2 * _aNodeNum - 2;
|
deba@1820
|
460 |
if (node.id < 0) node.id = -1;
|
deba@1820
|
461 |
}
|
deba@1910
|
462 |
void nextANode(Node& node) const {
|
deba@1820
|
463 |
node.id -= 2;
|
deba@1820
|
464 |
if (node.id < 0) node.id = -1;
|
deba@1820
|
465 |
}
|
deba@1820
|
466 |
|
deba@1910
|
467 |
void firstBNode(Node& node) const {
|
deba@1910
|
468 |
node.id = 2 * _bNodeNum - 1;
|
deba@1820
|
469 |
}
|
deba@1910
|
470 |
void nextBNode(Node& node) const {
|
deba@1820
|
471 |
node.id -= 2;
|
deba@1820
|
472 |
}
|
deba@1820
|
473 |
|
deba@1820
|
474 |
void first(Node& node) const {
|
deba@1910
|
475 |
if (_aNodeNum > 0) {
|
deba@1910
|
476 |
node.id = 2 * _aNodeNum - 2;
|
deba@1820
|
477 |
} else {
|
deba@1910
|
478 |
node.id = 2 * _bNodeNum - 1;
|
deba@1820
|
479 |
}
|
deba@1820
|
480 |
}
|
deba@1820
|
481 |
void next(Node& node) const {
|
deba@1820
|
482 |
node.id -= 2;
|
deba@1820
|
483 |
if (node.id == -2) {
|
deba@1910
|
484 |
node.id = 2 * _bNodeNum - 1;
|
deba@1820
|
485 |
}
|
deba@1820
|
486 |
}
|
deba@1820
|
487 |
|
deba@1820
|
488 |
void first(Edge& edge) const {
|
deba@1820
|
489 |
edge.id = _edgeNum - 1;
|
deba@1820
|
490 |
}
|
deba@1820
|
491 |
void next(Edge& edge) const {
|
deba@1820
|
492 |
--edge.id;
|
deba@1820
|
493 |
}
|
deba@1820
|
494 |
|
deba@1910
|
495 |
void firstOut(Edge& edge, const Node& node) const {
|
deba@1820
|
496 |
LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
|
deba@1910
|
497 |
edge.id = (node.id >> 1) * _bNodeNum;
|
deba@1820
|
498 |
}
|
deba@1910
|
499 |
void nextOut(Edge& edge) const {
|
deba@1820
|
500 |
++(edge.id);
|
deba@1910
|
501 |
if (edge.id % _bNodeNum == 0) edge.id = -1;
|
deba@1820
|
502 |
}
|
deba@1820
|
503 |
|
deba@1910
|
504 |
void firstIn(Edge& edge, const Node& node) const {
|
deba@1820
|
505 |
LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
|
deba@1820
|
506 |
edge.id = (node.id >> 1);
|
deba@1820
|
507 |
}
|
deba@1910
|
508 |
void nextIn(Edge& edge) const {
|
deba@1910
|
509 |
edge.id += _bNodeNum;
|
deba@1820
|
510 |
if (edge.id >= _edgeNum) edge.id = -1;
|
deba@1820
|
511 |
}
|
deba@1820
|
512 |
|
deba@1820
|
513 |
static int id(const Node& node) {
|
deba@1820
|
514 |
return node.id;
|
deba@1820
|
515 |
}
|
deba@1820
|
516 |
static Node nodeFromId(int id) {
|
deba@1820
|
517 |
return Node(id);
|
deba@1820
|
518 |
}
|
deba@1820
|
519 |
int maxNodeId() const {
|
deba@1910
|
520 |
return _aNodeNum > _bNodeNum ?
|
deba@1910
|
521 |
_aNodeNum * 2 - 2 : _bNodeNum * 2 - 1;
|
deba@1820
|
522 |
}
|
deba@1820
|
523 |
|
deba@1820
|
524 |
static int id(const Edge& edge) {
|
deba@1820
|
525 |
return edge.id;
|
deba@1820
|
526 |
}
|
deba@1820
|
527 |
static Edge edgeFromId(int id) {
|
deba@1820
|
528 |
return Edge(id);
|
deba@1820
|
529 |
}
|
deba@1820
|
530 |
int maxEdgeId() const {
|
deba@1820
|
531 |
return _edgeNum - 1;
|
deba@1820
|
532 |
}
|
deba@1820
|
533 |
|
deba@1910
|
534 |
static int aNodeId(const Node& node) {
|
deba@1820
|
535 |
return node.id >> 1;
|
deba@1820
|
536 |
}
|
deba@1910
|
537 |
static Node fromANodeId(int id, Node) {
|
deba@1820
|
538 |
return Node(id << 1);
|
deba@1820
|
539 |
}
|
deba@1910
|
540 |
int maxANodeId() const {
|
deba@1910
|
541 |
return _aNodeNum;
|
deba@1820
|
542 |
}
|
deba@1820
|
543 |
|
deba@1910
|
544 |
static int bNodeId(const Node& node) {
|
deba@1820
|
545 |
return node.id >> 1;
|
deba@1820
|
546 |
}
|
deba@1910
|
547 |
static Node fromBNodeId(int id) {
|
deba@1820
|
548 |
return Node((id << 1) + 1);
|
deba@1820
|
549 |
}
|
deba@1910
|
550 |
int maxBNodeId() const {
|
deba@1910
|
551 |
return _bNodeNum;
|
deba@1820
|
552 |
}
|
deba@1820
|
553 |
|
deba@1910
|
554 |
Node aNode(const Edge& edge) const {
|
deba@1910
|
555 |
return Node((edge.id / _bNodeNum) << 1);
|
deba@1820
|
556 |
}
|
deba@1910
|
557 |
Node bNode(const Edge& edge) const {
|
deba@1910
|
558 |
return Node(((edge.id % _bNodeNum) << 1) + 1);
|
deba@1820
|
559 |
}
|
deba@1820
|
560 |
|
deba@1910
|
561 |
static bool aNode(const Node& node) {
|
deba@1820
|
562 |
return (node.id & 1) == 0;
|
deba@1820
|
563 |
}
|
deba@1820
|
564 |
|
deba@1910
|
565 |
static bool bNode(const Node& node) {
|
deba@1820
|
566 |
return (node.id & 1) == 1;
|
deba@1820
|
567 |
}
|
deba@1820
|
568 |
|
deba@1910
|
569 |
static Node aNode(int index) {
|
deba@1820
|
570 |
return Node(index << 1);
|
deba@1820
|
571 |
}
|
deba@1820
|
572 |
|
deba@1910
|
573 |
static Node bNode(int index) {
|
deba@1820
|
574 |
return Node((index << 1) + 1);
|
deba@1820
|
575 |
}
|
deba@1820
|
576 |
|
deba@1820
|
577 |
};
|
deba@1820
|
578 |
|
deba@1820
|
579 |
|
deba@1910
|
580 |
typedef StaticMappableBpUGraphExtender<
|
deba@1910
|
581 |
IterableBpUGraphExtender<
|
deba@1910
|
582 |
AlterableBpUGraphExtender<
|
deba@1910
|
583 |
BpUGraphExtender <
|
deba@1910
|
584 |
FullBpUGraphBase> > > >
|
deba@1910
|
585 |
ExtendedFullBpUGraphBase;
|
deba@1820
|
586 |
|
deba@1820
|
587 |
|
deba@1910
|
588 |
/// \ingroup graphs
|
deba@1910
|
589 |
///
|
deba@1910
|
590 |
/// \brief An undirected full bipartite graph class.
|
deba@1910
|
591 |
///
|
deba@1910
|
592 |
/// This is a simple and fast bipartite undirected full graph implementation.
|
deba@1910
|
593 |
/// It is completely static, so you can neither add nor delete either
|
deba@1910
|
594 |
/// edges or nodes.
|
deba@1910
|
595 |
///
|
deba@1910
|
596 |
/// \sa FullGraph
|
deba@1910
|
597 |
///
|
deba@1910
|
598 |
/// \author Balazs Dezso
|
deba@1910
|
599 |
class FullBpUGraph :
|
deba@1910
|
600 |
public ExtendedFullBpUGraphBase {
|
deba@1820
|
601 |
public:
|
deba@1910
|
602 |
typedef ExtendedFullBpUGraphBase Parent;
|
deba@1910
|
603 |
FullBpUGraph(int aNodeNum, int bNodeNum) {
|
deba@1910
|
604 |
Parent::construct(aNodeNum, bNodeNum);
|
deba@1820
|
605 |
}
|
deba@1820
|
606 |
};
|
deba@1820
|
607 |
|
alpar@921
|
608 |
} //namespace lemon
|
alpar@591
|
609 |
|
alpar@591
|
610 |
|
alpar@921
|
611 |
#endif //LEMON_FULL_GRAPH_H
|