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 |
|
deba@2116
|
24 |
#include <lemon/bits/base_extender.h>
|
deba@1791
|
25 |
#include <lemon/bits/graph_extender.h>
|
deba@1566
|
26 |
|
deba@1993
|
27 |
#include <lemon/bits/invalid.h>
|
deba@1993
|
28 |
#include <lemon/bits/utility.h>
|
klao@977
|
29 |
|
klao@977
|
30 |
|
alpar@591
|
31 |
///\ingroup graphs
|
alpar@591
|
32 |
///\file
|
deba@2116
|
33 |
///\brief FullGraph and FullUGraph classes.
|
alpar@591
|
34 |
|
alpar@591
|
35 |
|
alpar@921
|
36 |
namespace lemon {
|
alpar@591
|
37 |
|
deba@1986
|
38 |
/// \brief Base of the FullGrpah.
|
deba@1986
|
39 |
///
|
deba@1986
|
40 |
/// Base of the FullGrpah.
|
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@977
|
59 |
typedef True NodeNumTag;
|
klao@977
|
60 |
typedef True EdgeNumTag;
|
klao@977
|
61 |
|
deba@1986
|
62 |
/// \brief Returns the node with the given index.
|
deba@1986
|
63 |
///
|
deba@1986
|
64 |
/// Returns the node with the given index. Because it is a
|
deba@1986
|
65 |
/// static size graph the node's of the graph can be indiced
|
deba@1986
|
66 |
/// by the range from 0 to \e nodeNum()-1 and the index of
|
deba@1986
|
67 |
/// the node can accessed by the \e index() member.
|
deba@1986
|
68 |
Node operator()(int index) const { return Node(index); }
|
deba@1986
|
69 |
|
deba@1986
|
70 |
/// \brief Returns the index of the node.
|
deba@1986
|
71 |
///
|
deba@1986
|
72 |
/// Returns the index of the node. Because it is a
|
deba@1986
|
73 |
/// static size graph the node's of the graph can be indiced
|
deba@1986
|
74 |
/// by the range from 0 to \e nodeNum()-1 and the index of
|
deba@1986
|
75 |
/// the node can accessed by the \e index() member.
|
deba@1986
|
76 |
int index(const Node& node) const { return node.id; }
|
deba@1986
|
77 |
|
alpar@813
|
78 |
///Number of nodes.
|
deba@1566
|
79 |
int nodeNum() const { return _nodeNum; }
|
alpar@813
|
80 |
///Number of edges.
|
deba@1566
|
81 |
int edgeNum() const { return _edgeNum; }
|
alpar@591
|
82 |
|
alpar@813
|
83 |
/// Maximum node ID.
|
alpar@813
|
84 |
|
alpar@813
|
85 |
/// Maximum node ID.
|
alpar@813
|
86 |
///\sa id(Node)
|
deba@1791
|
87 |
int maxNodeId() const { return _nodeNum-1; }
|
alpar@813
|
88 |
/// Maximum edge ID.
|
alpar@813
|
89 |
|
alpar@813
|
90 |
/// Maximum edge ID.
|
alpar@813
|
91 |
///\sa id(Edge)
|
deba@1791
|
92 |
int maxEdgeId() const { return _edgeNum-1; }
|
alpar@591
|
93 |
|
deba@1566
|
94 |
Node source(Edge e) const { return e.id % _nodeNum; }
|
deba@1566
|
95 |
Node target(Edge e) const { return e.id / _nodeNum; }
|
alpar@591
|
96 |
|
alpar@591
|
97 |
|
alpar@813
|
98 |
/// Node ID.
|
alpar@813
|
99 |
|
alpar@813
|
100 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
alpar@813
|
101 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
alpar@813
|
102 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
alpar@813
|
103 |
///
|
alpar@813
|
104 |
/// The ID of the \ref INVALID node is -1.
|
alpar@813
|
105 |
///\return The ID of the node \c v.
|
klao@946
|
106 |
|
klao@946
|
107 |
static int id(Node v) { return v.id; }
|
alpar@813
|
108 |
/// Edge ID.
|
alpar@813
|
109 |
|
alpar@813
|
110 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
alpar@813
|
111 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
alpar@813
|
112 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
alpar@813
|
113 |
///
|
alpar@813
|
114 |
/// The ID of the \ref INVALID edge is -1.
|
alpar@813
|
115 |
///\return The ID of the edge \c e.
|
klao@946
|
116 |
static int id(Edge e) { return e.id; }
|
alpar@591
|
117 |
|
deba@1791
|
118 |
static Node nodeFromId(int id) { return Node(id);}
|
deba@1106
|
119 |
|
deba@1791
|
120 |
static Edge edgeFromId(int id) { return Edge(id);}
|
deba@1106
|
121 |
|
deba@1566
|
122 |
typedef True FindEdgeTag;
|
deba@1566
|
123 |
|
alpar@774
|
124 |
/// Finds an edge between two nodes.
|
alpar@774
|
125 |
|
alpar@774
|
126 |
/// Finds an edge from node \c u to node \c v.
|
alpar@774
|
127 |
///
|
alpar@774
|
128 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@774
|
129 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@774
|
130 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@774
|
131 |
/// \return The found edge or INVALID if there is no such an edge.
|
deba@1566
|
132 |
Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
|
klao@946
|
133 |
return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
|
alpar@774
|
134 |
}
|
alpar@774
|
135 |
|
alpar@774
|
136 |
|
alpar@591
|
137 |
class Node {
|
klao@946
|
138 |
friend class FullGraphBase;
|
alpar@591
|
139 |
|
alpar@591
|
140 |
protected:
|
klao@946
|
141 |
int id;
|
alpar@1643
|
142 |
Node(int _id) : id(_id) {}
|
alpar@591
|
143 |
public:
|
alpar@591
|
144 |
Node() {}
|
alpar@1643
|
145 |
Node (Invalid) : id(-1) {}
|
klao@946
|
146 |
bool operator==(const Node node) const {return id == node.id;}
|
klao@946
|
147 |
bool operator!=(const Node node) const {return id != node.id;}
|
klao@946
|
148 |
bool operator<(const Node node) const {return id < node.id;}
|
alpar@591
|
149 |
};
|
alpar@591
|
150 |
|
klao@946
|
151 |
|
klao@946
|
152 |
|
klao@946
|
153 |
class Edge {
|
klao@946
|
154 |
friend class FullGraphBase;
|
klao@946
|
155 |
|
klao@946
|
156 |
protected:
|
deba@1566
|
157 |
int id; // _nodeNum * target + source;
|
klao@946
|
158 |
|
klao@946
|
159 |
Edge(int _id) : id(_id) {}
|
klao@946
|
160 |
|
alpar@986
|
161 |
Edge(const FullGraphBase& _graph, int source, int target)
|
deba@1566
|
162 |
: id(_graph._nodeNum * target+source) {}
|
alpar@591
|
163 |
public:
|
klao@946
|
164 |
Edge() { }
|
klao@946
|
165 |
Edge (Invalid) { id = -1; }
|
klao@946
|
166 |
bool operator==(const Edge edge) const {return id == edge.id;}
|
klao@946
|
167 |
bool operator!=(const Edge edge) const {return id != edge.id;}
|
klao@946
|
168 |
bool operator<(const Edge edge) const {return id < edge.id;}
|
alpar@591
|
169 |
};
|
alpar@591
|
170 |
|
klao@946
|
171 |
void first(Node& node) const {
|
deba@1566
|
172 |
node.id = _nodeNum-1;
|
klao@946
|
173 |
}
|
alpar@591
|
174 |
|
klao@946
|
175 |
static void next(Node& node) {
|
klao@946
|
176 |
--node.id;
|
klao@946
|
177 |
}
|
klao@946
|
178 |
|
klao@946
|
179 |
void first(Edge& edge) const {
|
deba@1566
|
180 |
edge.id = _edgeNum-1;
|
klao@946
|
181 |
}
|
klao@946
|
182 |
|
klao@946
|
183 |
static void next(Edge& edge) {
|
klao@946
|
184 |
--edge.id;
|
klao@946
|
185 |
}
|
klao@946
|
186 |
|
klao@946
|
187 |
void firstOut(Edge& edge, const Node& node) const {
|
deba@1566
|
188 |
edge.id = _edgeNum + node.id - _nodeNum;
|
klao@946
|
189 |
}
|
klao@946
|
190 |
|
klao@946
|
191 |
void nextOut(Edge& edge) const {
|
deba@1566
|
192 |
edge.id -= _nodeNum;
|
klao@946
|
193 |
if (edge.id < 0) edge.id = -1;
|
klao@946
|
194 |
}
|
klao@946
|
195 |
|
klao@946
|
196 |
void firstIn(Edge& edge, const Node& node) const {
|
deba@1566
|
197 |
edge.id = node.id * _nodeNum;
|
klao@946
|
198 |
}
|
alpar@591
|
199 |
|
klao@946
|
200 |
void nextIn(Edge& edge) const {
|
klao@946
|
201 |
++edge.id;
|
deba@1566
|
202 |
if (edge.id % _nodeNum == 0) edge.id = -1;
|
klao@946
|
203 |
}
|
alpar@591
|
204 |
|
alpar@591
|
205 |
};
|
alpar@591
|
206 |
|
deba@1979
|
207 |
typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
|
klao@946
|
208 |
|
deba@1566
|
209 |
/// \ingroup graphs
|
alpar@951
|
210 |
///
|
deba@1566
|
211 |
/// \brief A full graph class.
|
deba@1566
|
212 |
///
|
deba@1566
|
213 |
/// This is a simple and fast directed full graph implementation.
|
deba@1566
|
214 |
/// It is completely static, so you can neither add nor delete either
|
deba@1566
|
215 |
/// edges or nodes.
|
deba@1566
|
216 |
/// Thus it conforms to
|
deba@2111
|
217 |
/// the \ref concept::Graph "Graph" concept
|
deba@2111
|
218 |
/// \sa concept::Graph.
|
deba@1566
|
219 |
///
|
deba@1986
|
220 |
/// \sa FullGraphBase
|
deba@1986
|
221 |
/// \sa FullUGraph
|
deba@1986
|
222 |
///
|
deba@1566
|
223 |
/// \author Alpar Juttner
|
deba@1669
|
224 |
class FullGraph : public ExtendedFullGraphBase {
|
klao@946
|
225 |
public:
|
klao@946
|
226 |
|
deba@1979
|
227 |
typedef ExtendedFullGraphBase Parent;
|
deba@1979
|
228 |
|
deba@1979
|
229 |
/// \brief Constructor
|
deba@1987
|
230 |
FullGraph() { construct(0); }
|
deba@1987
|
231 |
|
deba@1987
|
232 |
/// \brief Constructor
|
deba@1979
|
233 |
///
|
klao@946
|
234 |
FullGraph(int n) { construct(n); }
|
deba@1979
|
235 |
|
deba@1979
|
236 |
/// \brief Resize the graph
|
deba@1979
|
237 |
///
|
deba@1986
|
238 |
/// Resize the graph. The function will fully destroy and build the graph.
|
deba@1986
|
239 |
/// This cause that the maps of the graph will reallocated
|
deba@1986
|
240 |
/// automatically and the previous values will be lost.
|
deba@1979
|
241 |
void resize(int n) {
|
deba@1979
|
242 |
Parent::getNotifier(Edge()).clear();
|
deba@1979
|
243 |
Parent::getNotifier(Node()).clear();
|
deba@1979
|
244 |
construct(n);
|
deba@1979
|
245 |
Parent::getNotifier(Node()).build();
|
deba@1979
|
246 |
Parent::getNotifier(Edge()).build();
|
deba@1979
|
247 |
}
|
klao@946
|
248 |
};
|
klao@946
|
249 |
|
deba@2116
|
250 |
|
deba@2116
|
251 |
/// \brief Base of the FullUGrpah.
|
deba@2116
|
252 |
///
|
deba@2116
|
253 |
/// Base of the FullUGrpah.
|
deba@2116
|
254 |
class FullUGraphBase {
|
deba@2116
|
255 |
int _nodeNum;
|
deba@2116
|
256 |
int _edgeNum;
|
deba@2116
|
257 |
public:
|
deba@2116
|
258 |
|
deba@2116
|
259 |
typedef FullUGraphBase Graph;
|
deba@2116
|
260 |
|
deba@2116
|
261 |
class Node;
|
deba@2116
|
262 |
class Edge;
|
deba@2116
|
263 |
|
deba@2116
|
264 |
public:
|
deba@2116
|
265 |
|
deba@2116
|
266 |
FullUGraphBase() {}
|
deba@2116
|
267 |
|
deba@2116
|
268 |
|
deba@2116
|
269 |
///Creates a full graph with \c n nodes.
|
deba@2116
|
270 |
void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
|
deba@2116
|
271 |
|
deba@2116
|
272 |
/// \brief Returns the node with the given index.
|
deba@2116
|
273 |
///
|
deba@2116
|
274 |
/// Returns the node with the given index. Because it is a
|
deba@2116
|
275 |
/// static size graph the node's of the graph can be indiced
|
deba@2116
|
276 |
/// by the range from 0 to \e nodeNum()-1 and the index of
|
deba@2116
|
277 |
/// the node can accessed by the \e index() member.
|
deba@2116
|
278 |
Node operator()(int index) const { return Node(index); }
|
deba@2116
|
279 |
|
deba@2116
|
280 |
/// \brief Returns the index of the node.
|
deba@2116
|
281 |
///
|
deba@2116
|
282 |
/// Returns the index of the node. Because it is a
|
deba@2116
|
283 |
/// static size graph the node's of the graph can be indiced
|
deba@2116
|
284 |
/// by the range from 0 to \e nodeNum()-1 and the index of
|
deba@2116
|
285 |
/// the node can accessed by the \e index() member.
|
deba@2116
|
286 |
int index(const Node& node) const { return node.id; }
|
deba@2116
|
287 |
|
deba@2116
|
288 |
typedef True NodeNumTag;
|
deba@2116
|
289 |
typedef True EdgeNumTag;
|
deba@2116
|
290 |
|
deba@2116
|
291 |
///Number of nodes.
|
deba@2116
|
292 |
int nodeNum() const { return _nodeNum; }
|
deba@2116
|
293 |
///Number of edges.
|
deba@2116
|
294 |
int edgeNum() const { return _edgeNum; }
|
deba@2116
|
295 |
|
deba@2116
|
296 |
/// Maximum node ID.
|
deba@2116
|
297 |
|
deba@2116
|
298 |
/// Maximum node ID.
|
deba@2116
|
299 |
///\sa id(Node)
|
deba@2116
|
300 |
int maxNodeId() const { return _nodeNum-1; }
|
deba@2116
|
301 |
/// Maximum edge ID.
|
deba@2116
|
302 |
|
deba@2116
|
303 |
/// Maximum edge ID.
|
deba@2116
|
304 |
///\sa id(Edge)
|
deba@2116
|
305 |
int maxEdgeId() const { return _edgeNum-1; }
|
deba@2116
|
306 |
|
deba@2116
|
307 |
/// \brief Returns the node from its \c id.
|
deba@2116
|
308 |
///
|
deba@2116
|
309 |
/// Returns the node from its \c id. If there is not node
|
deba@2116
|
310 |
/// with the given id the effect of the function is undefinied.
|
deba@2116
|
311 |
static Node nodeFromId(int id) { return Node(id);}
|
deba@2116
|
312 |
|
deba@2116
|
313 |
/// \brief Returns the edge from its \c id.
|
deba@2116
|
314 |
///
|
deba@2116
|
315 |
/// Returns the edge from its \c id. If there is not edge
|
deba@2116
|
316 |
/// with the given id the effect of the function is undefinied.
|
deba@2116
|
317 |
static Edge edgeFromId(int id) { return Edge(id);}
|
deba@2116
|
318 |
|
deba@2116
|
319 |
Node source(Edge e) const {
|
deba@2116
|
320 |
/// \todo we may do it faster
|
deba@2116
|
321 |
return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
|
deba@2116
|
322 |
}
|
deba@2116
|
323 |
|
deba@2116
|
324 |
Node target(Edge e) const {
|
deba@2116
|
325 |
int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
|
deba@2116
|
326 |
return Node(e.id - (source) * (source - 1) / 2);
|
deba@2116
|
327 |
}
|
deba@2116
|
328 |
|
deba@2116
|
329 |
|
deba@2116
|
330 |
/// \brief Node ID.
|
deba@2116
|
331 |
///
|
deba@2116
|
332 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
deba@2116
|
333 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
deba@2116
|
334 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
deba@2116
|
335 |
///
|
deba@2116
|
336 |
/// The ID of the \ref INVALID node is -1.
|
deba@2116
|
337 |
/// \return The ID of the node \c v.
|
deba@2116
|
338 |
|
deba@2116
|
339 |
static int id(Node v) { return v.id; }
|
deba@2116
|
340 |
|
deba@2116
|
341 |
/// \brief Edge ID.
|
deba@2116
|
342 |
///
|
deba@2116
|
343 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
deba@2116
|
344 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
deba@2116
|
345 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
deba@2116
|
346 |
///
|
deba@2116
|
347 |
/// The ID of the \ref INVALID edge is -1.
|
deba@2116
|
348 |
///\return The ID of the edge \c e.
|
deba@2116
|
349 |
static int id(Edge e) { return e.id; }
|
deba@2116
|
350 |
|
deba@2116
|
351 |
/// \brief Finds an edge between two nodes.
|
deba@2116
|
352 |
///
|
deba@2116
|
353 |
/// Finds an edge from node \c u to node \c v.
|
deba@2116
|
354 |
///
|
deba@2116
|
355 |
/// If \c prev is \ref INVALID (this is the default value), then
|
deba@2116
|
356 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
deba@2116
|
357 |
/// the next edge from \c u to \c v after \c prev.
|
deba@2116
|
358 |
/// \return The found edge or INVALID if there is no such an edge.
|
deba@2116
|
359 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
deba@2116
|
360 |
if (prev.id != -1 || u.id <= v.id) return Edge(-1);
|
deba@2116
|
361 |
return Edge(u.id * (u.id - 1) / 2 + v.id);
|
deba@2116
|
362 |
}
|
deba@2116
|
363 |
|
deba@2116
|
364 |
typedef True FindEdgeTag;
|
deba@2116
|
365 |
|
deba@2116
|
366 |
|
deba@2116
|
367 |
class Node {
|
deba@2116
|
368 |
friend class FullUGraphBase;
|
deba@2116
|
369 |
|
deba@2116
|
370 |
protected:
|
deba@2116
|
371 |
int id;
|
deba@2116
|
372 |
Node(int _id) { id = _id;}
|
deba@2116
|
373 |
public:
|
deba@2116
|
374 |
Node() {}
|
deba@2116
|
375 |
Node (Invalid) { id = -1; }
|
deba@2116
|
376 |
bool operator==(const Node node) const {return id == node.id;}
|
deba@2116
|
377 |
bool operator!=(const Node node) const {return id != node.id;}
|
deba@2116
|
378 |
bool operator<(const Node node) const {return id < node.id;}
|
deba@2116
|
379 |
};
|
deba@2116
|
380 |
|
deba@2116
|
381 |
|
deba@2116
|
382 |
|
deba@2116
|
383 |
class Edge {
|
deba@2116
|
384 |
friend class FullUGraphBase;
|
deba@2116
|
385 |
|
deba@2116
|
386 |
protected:
|
deba@2116
|
387 |
int id; // _nodeNum * target + source;
|
deba@2116
|
388 |
|
deba@2116
|
389 |
Edge(int _id) : id(_id) {}
|
deba@2116
|
390 |
|
deba@2116
|
391 |
public:
|
deba@2116
|
392 |
Edge() { }
|
deba@2116
|
393 |
Edge (Invalid) { id = -1; }
|
deba@2116
|
394 |
bool operator==(const Edge edge) const {return id == edge.id;}
|
deba@2116
|
395 |
bool operator!=(const Edge edge) const {return id != edge.id;}
|
deba@2116
|
396 |
bool operator<(const Edge edge) const {return id < edge.id;}
|
deba@2116
|
397 |
};
|
deba@2116
|
398 |
|
deba@2116
|
399 |
void first(Node& node) const {
|
deba@2116
|
400 |
node.id = _nodeNum - 1;
|
deba@2116
|
401 |
}
|
deba@2116
|
402 |
|
deba@2116
|
403 |
static void next(Node& node) {
|
deba@2116
|
404 |
--node.id;
|
deba@2116
|
405 |
}
|
deba@2116
|
406 |
|
deba@2116
|
407 |
void first(Edge& edge) const {
|
deba@2116
|
408 |
edge.id = _edgeNum - 1;
|
deba@2116
|
409 |
}
|
deba@2116
|
410 |
|
deba@2116
|
411 |
static void next(Edge& edge) {
|
deba@2116
|
412 |
--edge.id;
|
deba@2116
|
413 |
}
|
deba@2116
|
414 |
|
deba@2116
|
415 |
void firstOut(Edge& edge, const Node& node) const {
|
deba@2116
|
416 |
int src = node.id;
|
deba@2116
|
417 |
int trg = 0;
|
deba@2116
|
418 |
edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
|
deba@2116
|
419 |
}
|
deba@2116
|
420 |
|
deba@2116
|
421 |
/// \todo with specialized iterators we can make faster iterating
|
deba@2116
|
422 |
void nextOut(Edge& edge) const {
|
deba@2116
|
423 |
int src = source(edge).id;
|
deba@2116
|
424 |
int trg = target(edge).id;
|
deba@2116
|
425 |
++trg;
|
deba@2116
|
426 |
edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
|
deba@2116
|
427 |
}
|
deba@2116
|
428 |
|
deba@2116
|
429 |
void firstIn(Edge& edge, const Node& node) const {
|
deba@2116
|
430 |
int src = node.id + 1;
|
deba@2116
|
431 |
int trg = node.id;
|
deba@2116
|
432 |
edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
|
deba@2116
|
433 |
}
|
deba@2116
|
434 |
|
deba@2116
|
435 |
void nextIn(Edge& edge) const {
|
deba@2116
|
436 |
int src = source(edge).id;
|
deba@2116
|
437 |
int trg = target(edge).id;
|
deba@2116
|
438 |
++src;
|
deba@2116
|
439 |
edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
|
deba@2116
|
440 |
}
|
deba@2116
|
441 |
|
deba@2116
|
442 |
};
|
deba@2116
|
443 |
|
deba@2116
|
444 |
typedef UGraphExtender<UndirGraphExtender<FullUGraphBase> >
|
deba@2116
|
445 |
ExtendedFullUGraphBase;
|
deba@2116
|
446 |
|
deba@2116
|
447 |
/// \ingroup graphs
|
deba@2116
|
448 |
///
|
deba@2116
|
449 |
/// \brief An undirected full graph class.
|
deba@2116
|
450 |
///
|
deba@2116
|
451 |
/// This is a simple and fast undirected full graph implementation.
|
deba@2116
|
452 |
/// It is completely static, so you can neither add nor delete either
|
deba@2116
|
453 |
/// edges or nodes.
|
deba@2116
|
454 |
///
|
deba@2116
|
455 |
/// The main difference beetween the \e FullGraph and \e FullUGraph class
|
deba@2116
|
456 |
/// is that this class conforms to the undirected graph concept and
|
deba@2116
|
457 |
/// it does not contain the loop edges.
|
deba@2116
|
458 |
///
|
deba@2116
|
459 |
/// \sa FullUGraphBase
|
deba@2116
|
460 |
/// \sa FullGraph
|
deba@2116
|
461 |
///
|
deba@2116
|
462 |
/// \author Balazs Dezso
|
deba@2116
|
463 |
class FullUGraph : public ExtendedFullUGraphBase {
|
deba@2116
|
464 |
public:
|
deba@2116
|
465 |
|
deba@2116
|
466 |
typedef ExtendedFullUGraphBase Parent;
|
deba@2116
|
467 |
|
deba@2116
|
468 |
/// \brief Constructor
|
deba@2116
|
469 |
FullUGraph() { construct(0); }
|
deba@2116
|
470 |
|
deba@2116
|
471 |
/// \brief Constructor
|
deba@2116
|
472 |
FullUGraph(int n) { construct(n); }
|
deba@2116
|
473 |
|
deba@2116
|
474 |
/// \brief Resize the graph
|
deba@2116
|
475 |
///
|
deba@2116
|
476 |
/// Resize the graph. The function will fully destroy and build the graph.
|
deba@2116
|
477 |
/// This cause that the maps of the graph will reallocated
|
deba@2116
|
478 |
/// automatically and the previous values will be lost.
|
deba@2116
|
479 |
void resize(int n) {
|
deba@2116
|
480 |
Parent::getNotifier(Edge()).clear();
|
deba@2116
|
481 |
Parent::getNotifier(UEdge()).clear();
|
deba@2116
|
482 |
Parent::getNotifier(Node()).clear();
|
deba@2116
|
483 |
construct(n);
|
deba@2116
|
484 |
Parent::getNotifier(Node()).build();
|
deba@2116
|
485 |
Parent::getNotifier(UEdge()).build();
|
deba@2116
|
486 |
Parent::getNotifier(Edge()).build();
|
deba@2116
|
487 |
}
|
deba@2116
|
488 |
};
|
deba@2116
|
489 |
|
deba@2116
|
490 |
|
deba@2116
|
491 |
class FullBpUGraphBase {
|
deba@2116
|
492 |
protected:
|
deba@2116
|
493 |
|
deba@2116
|
494 |
int _aNodeNum;
|
deba@2116
|
495 |
int _bNodeNum;
|
deba@2116
|
496 |
|
deba@2116
|
497 |
int _edgeNum;
|
deba@2116
|
498 |
|
deba@2116
|
499 |
public:
|
deba@2116
|
500 |
|
deba@2116
|
501 |
class NodeSetError : public LogicError {
|
alpar@2151
|
502 |
virtual const char* what() const throw() {
|
deba@2116
|
503 |
return "lemon::FullBpUGraph::NodeSetError";
|
deba@2116
|
504 |
}
|
deba@2116
|
505 |
};
|
deba@2116
|
506 |
|
deba@2116
|
507 |
class Node {
|
deba@2116
|
508 |
friend class FullBpUGraphBase;
|
deba@2116
|
509 |
protected:
|
deba@2116
|
510 |
int id;
|
deba@2116
|
511 |
|
deba@2116
|
512 |
Node(int _id) : id(_id) {}
|
deba@2116
|
513 |
public:
|
deba@2116
|
514 |
Node() {}
|
deba@2116
|
515 |
Node(Invalid) { id = -1; }
|
deba@2116
|
516 |
bool operator==(const Node i) const {return id==i.id;}
|
deba@2116
|
517 |
bool operator!=(const Node i) const {return id!=i.id;}
|
deba@2116
|
518 |
bool operator<(const Node i) const {return id<i.id;}
|
deba@2116
|
519 |
};
|
deba@2116
|
520 |
|
deba@2116
|
521 |
class UEdge {
|
deba@2116
|
522 |
friend class FullBpUGraphBase;
|
deba@2116
|
523 |
protected:
|
deba@2116
|
524 |
int id;
|
deba@2116
|
525 |
|
deba@2116
|
526 |
UEdge(int _id) { id = _id;}
|
deba@2116
|
527 |
public:
|
deba@2116
|
528 |
UEdge() {}
|
deba@2116
|
529 |
UEdge (Invalid) { id = -1; }
|
deba@2116
|
530 |
bool operator==(const UEdge i) const {return id==i.id;}
|
deba@2116
|
531 |
bool operator!=(const UEdge i) const {return id!=i.id;}
|
deba@2116
|
532 |
bool operator<(const UEdge i) const {return id<i.id;}
|
deba@2116
|
533 |
};
|
deba@2116
|
534 |
|
deba@2116
|
535 |
void construct(int aNodeNum, int bNodeNum) {
|
deba@2116
|
536 |
_aNodeNum = aNodeNum;
|
deba@2116
|
537 |
_bNodeNum = bNodeNum;
|
deba@2116
|
538 |
_edgeNum = aNodeNum * bNodeNum;
|
deba@2116
|
539 |
}
|
deba@2116
|
540 |
|
deba@2116
|
541 |
void firstANode(Node& node) const {
|
deba@2116
|
542 |
node.id = 2 * _aNodeNum - 2;
|
deba@2116
|
543 |
if (node.id < 0) node.id = -1;
|
deba@2116
|
544 |
}
|
deba@2116
|
545 |
void nextANode(Node& node) const {
|
deba@2116
|
546 |
node.id -= 2;
|
deba@2116
|
547 |
if (node.id < 0) node.id = -1;
|
deba@2116
|
548 |
}
|
deba@2116
|
549 |
|
deba@2116
|
550 |
void firstBNode(Node& node) const {
|
deba@2116
|
551 |
node.id = 2 * _bNodeNum - 1;
|
deba@2116
|
552 |
}
|
deba@2116
|
553 |
void nextBNode(Node& node) const {
|
deba@2116
|
554 |
node.id -= 2;
|
deba@2116
|
555 |
}
|
deba@2116
|
556 |
|
deba@2116
|
557 |
void first(Node& node) const {
|
deba@2116
|
558 |
if (_aNodeNum > 0) {
|
deba@2116
|
559 |
node.id = 2 * _aNodeNum - 2;
|
deba@2116
|
560 |
} else {
|
deba@2116
|
561 |
node.id = 2 * _bNodeNum - 1;
|
deba@2116
|
562 |
}
|
deba@2116
|
563 |
}
|
deba@2116
|
564 |
void next(Node& node) const {
|
deba@2116
|
565 |
node.id -= 2;
|
deba@2116
|
566 |
if (node.id == -2) {
|
deba@2116
|
567 |
node.id = 2 * _bNodeNum - 1;
|
deba@2116
|
568 |
}
|
deba@2116
|
569 |
}
|
deba@2116
|
570 |
|
deba@2116
|
571 |
void first(UEdge& edge) const {
|
deba@2116
|
572 |
edge.id = _edgeNum - 1;
|
deba@2116
|
573 |
}
|
deba@2116
|
574 |
void next(UEdge& edge) const {
|
deba@2116
|
575 |
--edge.id;
|
deba@2116
|
576 |
}
|
deba@2116
|
577 |
|
deba@2116
|
578 |
void firstFromANode(UEdge& edge, const Node& node) const {
|
deba@2116
|
579 |
LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
|
deba@2116
|
580 |
edge.id = (node.id >> 1) * _bNodeNum;
|
deba@2116
|
581 |
}
|
deba@2116
|
582 |
void nextFromANode(UEdge& edge) const {
|
deba@2116
|
583 |
++(edge.id);
|
deba@2116
|
584 |
if (edge.id % _bNodeNum == 0) edge.id = -1;
|
deba@2116
|
585 |
}
|
deba@2116
|
586 |
|
deba@2116
|
587 |
void firstFromBNode(UEdge& edge, const Node& node) const {
|
deba@2116
|
588 |
LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
|
deba@2116
|
589 |
edge.id = (node.id >> 1);
|
deba@2116
|
590 |
}
|
deba@2116
|
591 |
void nextFromBNode(UEdge& edge) const {
|
deba@2116
|
592 |
edge.id += _bNodeNum;
|
deba@2116
|
593 |
if (edge.id >= _edgeNum) edge.id = -1;
|
deba@2116
|
594 |
}
|
deba@2116
|
595 |
|
deba@2116
|
596 |
static int id(const Node& node) {
|
deba@2116
|
597 |
return node.id;
|
deba@2116
|
598 |
}
|
deba@2116
|
599 |
static Node nodeFromId(int id) {
|
deba@2116
|
600 |
return Node(id);
|
deba@2116
|
601 |
}
|
deba@2116
|
602 |
int maxNodeId() const {
|
deba@2116
|
603 |
return _aNodeNum > _bNodeNum ?
|
deba@2116
|
604 |
_aNodeNum * 2 - 2 : _bNodeNum * 2 - 1;
|
deba@2116
|
605 |
}
|
deba@2116
|
606 |
|
deba@2116
|
607 |
static int id(const UEdge& edge) {
|
deba@2116
|
608 |
return edge.id;
|
deba@2116
|
609 |
}
|
deba@2116
|
610 |
static UEdge uEdgeFromId(int id) {
|
deba@2116
|
611 |
return UEdge(id);
|
deba@2116
|
612 |
}
|
deba@2116
|
613 |
int maxUEdgeId() const {
|
deba@2116
|
614 |
return _edgeNum - 1;
|
deba@2116
|
615 |
}
|
deba@2116
|
616 |
|
deba@2116
|
617 |
static int aNodeId(const Node& node) {
|
deba@2116
|
618 |
return node.id >> 1;
|
deba@2116
|
619 |
}
|
deba@2116
|
620 |
static Node fromANodeId(int id) {
|
deba@2116
|
621 |
return Node(id << 1);
|
deba@2116
|
622 |
}
|
deba@2116
|
623 |
int maxANodeId() const {
|
deba@2116
|
624 |
return _aNodeNum;
|
deba@2116
|
625 |
}
|
deba@2116
|
626 |
|
deba@2116
|
627 |
static int bNodeId(const Node& node) {
|
deba@2116
|
628 |
return node.id >> 1;
|
deba@2116
|
629 |
}
|
deba@2116
|
630 |
static Node fromBNodeId(int id) {
|
deba@2116
|
631 |
return Node((id << 1) + 1);
|
deba@2116
|
632 |
}
|
deba@2116
|
633 |
int maxBNodeId() const {
|
deba@2116
|
634 |
return _bNodeNum;
|
deba@2116
|
635 |
}
|
deba@2116
|
636 |
|
deba@2116
|
637 |
Node aNode(const UEdge& edge) const {
|
deba@2116
|
638 |
return Node((edge.id / _bNodeNum) << 1);
|
deba@2116
|
639 |
}
|
deba@2116
|
640 |
Node bNode(const UEdge& edge) const {
|
deba@2116
|
641 |
return Node(((edge.id % _bNodeNum) << 1) + 1);
|
deba@2116
|
642 |
}
|
deba@2116
|
643 |
|
deba@2116
|
644 |
static bool aNode(const Node& node) {
|
deba@2116
|
645 |
return (node.id & 1) == 0;
|
deba@2116
|
646 |
}
|
deba@2116
|
647 |
|
deba@2116
|
648 |
static bool bNode(const Node& node) {
|
deba@2116
|
649 |
return (node.id & 1) == 1;
|
deba@2116
|
650 |
}
|
deba@2116
|
651 |
|
deba@2116
|
652 |
static Node aNode(int index) {
|
deba@2116
|
653 |
return Node(index << 1);
|
deba@2116
|
654 |
}
|
deba@2116
|
655 |
|
deba@2116
|
656 |
static Node bNode(int index) {
|
deba@2116
|
657 |
return Node((index << 1) + 1);
|
deba@2116
|
658 |
}
|
deba@2116
|
659 |
|
deba@2116
|
660 |
typedef True NodeNumTag;
|
deba@2116
|
661 |
int nodeNum() const { return _aNodeNum + _bNodeNum; }
|
deba@2116
|
662 |
int aNodeNum() const { return _aNodeNum; }
|
deba@2116
|
663 |
int bNodeNum() const { return _bNodeNum; }
|
deba@2116
|
664 |
|
deba@2116
|
665 |
typedef True EdgeNumTag;
|
deba@2116
|
666 |
int uEdgeNum() const { return _edgeNum; }
|
deba@2116
|
667 |
|
deba@2116
|
668 |
};
|
deba@2116
|
669 |
|
deba@2116
|
670 |
|
deba@2116
|
671 |
typedef BpUGraphExtender<FullBpUGraphBase> ExtendedFullBpUGraphBase;
|
deba@2116
|
672 |
|
deba@2116
|
673 |
|
deba@2116
|
674 |
/// \ingroup graphs
|
deba@2116
|
675 |
///
|
deba@2116
|
676 |
/// \brief An undirected full bipartite graph class.
|
deba@2116
|
677 |
///
|
deba@2116
|
678 |
/// This is a simple and fast bipartite undirected full graph implementation.
|
deba@2116
|
679 |
/// It is completely static, so you can neither add nor delete either
|
deba@2116
|
680 |
/// edges or nodes.
|
deba@2116
|
681 |
///
|
deba@2116
|
682 |
/// \sa FullUGraphBase
|
deba@2116
|
683 |
/// \sa FullGraph
|
deba@2116
|
684 |
///
|
deba@2116
|
685 |
/// \author Balazs Dezso
|
deba@2116
|
686 |
class FullBpUGraph :
|
deba@2116
|
687 |
public ExtendedFullBpUGraphBase {
|
deba@2116
|
688 |
public:
|
deba@2116
|
689 |
|
deba@2116
|
690 |
typedef ExtendedFullBpUGraphBase Parent;
|
deba@2116
|
691 |
|
deba@2116
|
692 |
FullBpUGraph() {
|
deba@2116
|
693 |
Parent::construct(0, 0);
|
deba@2116
|
694 |
}
|
deba@2116
|
695 |
|
deba@2116
|
696 |
FullBpUGraph(int aNodeNum, int bNodeNum) {
|
deba@2116
|
697 |
Parent::construct(aNodeNum, bNodeNum);
|
deba@2116
|
698 |
}
|
deba@2116
|
699 |
|
deba@2116
|
700 |
/// \brief Resize the graph
|
deba@2116
|
701 |
///
|
deba@2116
|
702 |
void resize(int n, int m) {
|
deba@2116
|
703 |
Parent::getNotifier(Edge()).clear();
|
deba@2116
|
704 |
Parent::getNotifier(UEdge()).clear();
|
deba@2116
|
705 |
Parent::getNotifier(Node()).clear();
|
deba@2116
|
706 |
Parent::getNotifier(ANode()).clear();
|
deba@2116
|
707 |
Parent::getNotifier(BNode()).clear();
|
deba@2116
|
708 |
construct(n, m);
|
deba@2116
|
709 |
Parent::getNotifier(ANode()).build();
|
deba@2116
|
710 |
Parent::getNotifier(BNode()).build();
|
deba@2116
|
711 |
Parent::getNotifier(Node()).build();
|
deba@2116
|
712 |
Parent::getNotifier(UEdge()).build();
|
deba@2116
|
713 |
Parent::getNotifier(Edge()).build();
|
deba@2116
|
714 |
}
|
deba@2116
|
715 |
};
|
deba@2116
|
716 |
|
alpar@921
|
717 |
} //namespace lemon
|
alpar@591
|
718 |
|
alpar@591
|
719 |
|
alpar@921
|
720 |
#endif //LEMON_FULL_GRAPH_H
|