deba@2115
|
1 |
/* -*- C++ -*-
|
deba@2115
|
2 |
*
|
deba@2115
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@2115
|
4 |
*
|
deba@2115
|
5 |
* Copyright (C) 2003-2006
|
deba@2115
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@2115
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@2115
|
8 |
*
|
deba@2115
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@2115
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@2115
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@2115
|
12 |
*
|
deba@2115
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@2115
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@2115
|
15 |
* purpose.
|
deba@2115
|
16 |
*
|
deba@2115
|
17 |
*/
|
deba@2115
|
18 |
|
deba@2115
|
19 |
#ifndef LEMON_FULL_UGRAPH_H
|
deba@2115
|
20 |
#define LEMON_FULL_UGRAPH_H
|
deba@2115
|
21 |
|
deba@2115
|
22 |
#include <cmath>
|
deba@2115
|
23 |
|
deba@2115
|
24 |
#include <lemon/bits/base_extender.h>
|
deba@2115
|
25 |
#include <lemon/bits/ugraph_extender.h>
|
deba@2115
|
26 |
|
deba@2115
|
27 |
#include <lemon/bits/invalid.h>
|
deba@2115
|
28 |
#include <lemon/bits/utility.h>
|
deba@2115
|
29 |
|
deba@2115
|
30 |
|
deba@2115
|
31 |
///\ingroup graphs
|
deba@2115
|
32 |
///\file
|
deba@2115
|
33 |
///\brief FullUGraph classes.
|
deba@2115
|
34 |
|
deba@2115
|
35 |
|
deba@2115
|
36 |
namespace lemon {
|
deba@2115
|
37 |
|
deba@2115
|
38 |
/// \brief Base of the FullUGrpah.
|
deba@2115
|
39 |
///
|
deba@2115
|
40 |
/// Base of the FullUGrpah.
|
deba@2115
|
41 |
class FullUGraphBase {
|
deba@2115
|
42 |
int _nodeNum;
|
deba@2115
|
43 |
int _edgeNum;
|
deba@2115
|
44 |
public:
|
deba@2115
|
45 |
|
deba@2115
|
46 |
typedef FullUGraphBase Graph;
|
deba@2115
|
47 |
|
deba@2115
|
48 |
class Node;
|
deba@2115
|
49 |
class Edge;
|
deba@2115
|
50 |
|
deba@2115
|
51 |
public:
|
deba@2115
|
52 |
|
deba@2115
|
53 |
FullUGraphBase() {}
|
deba@2115
|
54 |
|
deba@2115
|
55 |
|
deba@2115
|
56 |
///Creates a full graph with \c n nodes.
|
deba@2115
|
57 |
void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
|
deba@2115
|
58 |
|
deba@2115
|
59 |
/// \brief Returns the node with the given index.
|
deba@2115
|
60 |
///
|
deba@2115
|
61 |
/// Returns the node with the given index. Because it is a
|
deba@2115
|
62 |
/// static size graph the node's of the graph can be indiced
|
deba@2115
|
63 |
/// by the range from 0 to \e nodeNum()-1 and the index of
|
deba@2115
|
64 |
/// the node can accessed by the \e index() member.
|
deba@2115
|
65 |
Node operator()(int index) const { return Node(index); }
|
deba@2115
|
66 |
|
deba@2115
|
67 |
/// \brief Returns the index of the node.
|
deba@2115
|
68 |
///
|
deba@2115
|
69 |
/// Returns the index of the node. Because it is a
|
deba@2115
|
70 |
/// static size graph the node's of the graph can be indiced
|
deba@2115
|
71 |
/// by the range from 0 to \e nodeNum()-1 and the index of
|
deba@2115
|
72 |
/// the node can accessed by the \e index() member.
|
deba@2115
|
73 |
int index(const Node& node) const { return node.id; }
|
deba@2115
|
74 |
|
deba@2115
|
75 |
typedef True NodeNumTag;
|
deba@2115
|
76 |
typedef True EdgeNumTag;
|
deba@2115
|
77 |
|
deba@2115
|
78 |
///Number of nodes.
|
deba@2115
|
79 |
int nodeNum() const { return _nodeNum; }
|
deba@2115
|
80 |
///Number of edges.
|
deba@2115
|
81 |
int edgeNum() const { return _edgeNum; }
|
deba@2115
|
82 |
|
deba@2115
|
83 |
/// Maximum node ID.
|
deba@2115
|
84 |
|
deba@2115
|
85 |
/// Maximum node ID.
|
deba@2115
|
86 |
///\sa id(Node)
|
deba@2115
|
87 |
int maxNodeId() const { return _nodeNum-1; }
|
deba@2115
|
88 |
/// Maximum edge ID.
|
deba@2115
|
89 |
|
deba@2115
|
90 |
/// Maximum edge ID.
|
deba@2115
|
91 |
///\sa id(Edge)
|
deba@2115
|
92 |
int maxEdgeId() const { return _edgeNum-1; }
|
deba@2115
|
93 |
|
deba@2115
|
94 |
/// \brief Returns the node from its \c id.
|
deba@2115
|
95 |
///
|
deba@2115
|
96 |
/// Returns the node from its \c id. If there is not node
|
deba@2115
|
97 |
/// with the given id the effect of the function is undefinied.
|
deba@2115
|
98 |
static Node nodeFromId(int id) { return Node(id);}
|
deba@2115
|
99 |
|
deba@2115
|
100 |
/// \brief Returns the edge from its \c id.
|
deba@2115
|
101 |
///
|
deba@2115
|
102 |
/// Returns the edge from its \c id. If there is not edge
|
deba@2115
|
103 |
/// with the given id the effect of the function is undefinied.
|
deba@2115
|
104 |
static Edge edgeFromId(int id) { return Edge(id);}
|
deba@2115
|
105 |
|
deba@2115
|
106 |
Node source(Edge e) const {
|
deba@2115
|
107 |
/// \todo we may do it faster
|
deba@2115
|
108 |
return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
|
deba@2115
|
109 |
}
|
deba@2115
|
110 |
|
deba@2115
|
111 |
Node target(Edge e) const {
|
deba@2115
|
112 |
int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
|
deba@2115
|
113 |
return Node(e.id - (source) * (source - 1) / 2);
|
deba@2115
|
114 |
}
|
deba@2115
|
115 |
|
deba@2115
|
116 |
|
deba@2115
|
117 |
/// \brief Node ID.
|
deba@2115
|
118 |
///
|
deba@2115
|
119 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
deba@2115
|
120 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
deba@2115
|
121 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
deba@2115
|
122 |
///
|
deba@2115
|
123 |
/// The ID of the \ref INVALID node is -1.
|
deba@2115
|
124 |
/// \return The ID of the node \c v.
|
deba@2115
|
125 |
|
deba@2115
|
126 |
static int id(Node v) { return v.id; }
|
deba@2115
|
127 |
|
deba@2115
|
128 |
/// \brief Edge ID.
|
deba@2115
|
129 |
///
|
deba@2115
|
130 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
deba@2115
|
131 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
deba@2115
|
132 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
deba@2115
|
133 |
///
|
deba@2115
|
134 |
/// The ID of the \ref INVALID edge is -1.
|
deba@2115
|
135 |
///\return The ID of the edge \c e.
|
deba@2115
|
136 |
static int id(Edge e) { return e.id; }
|
deba@2115
|
137 |
|
deba@2115
|
138 |
/// \brief Finds an edge between two nodes.
|
deba@2115
|
139 |
///
|
deba@2115
|
140 |
/// Finds an edge from node \c u to node \c v.
|
deba@2115
|
141 |
///
|
deba@2115
|
142 |
/// If \c prev is \ref INVALID (this is the default value), then
|
deba@2115
|
143 |
/// It finds the first edge from \c u to \c v. Otherwise it looks for
|
deba@2115
|
144 |
/// the next edge from \c u to \c v after \c prev.
|
deba@2115
|
145 |
/// \return The found edge or INVALID if there is no such an edge.
|
deba@2115
|
146 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
deba@2115
|
147 |
if (prev.id != -1 || u.id <= v.id) return Edge(-1);
|
deba@2115
|
148 |
return Edge(u.id * (u.id - 1) / 2 + v.id);
|
deba@2115
|
149 |
}
|
deba@2115
|
150 |
|
deba@2115
|
151 |
typedef True FindEdgeTag;
|
deba@2115
|
152 |
|
deba@2115
|
153 |
|
deba@2115
|
154 |
class Node {
|
deba@2115
|
155 |
friend class FullUGraphBase;
|
deba@2115
|
156 |
|
deba@2115
|
157 |
protected:
|
deba@2115
|
158 |
int id;
|
deba@2115
|
159 |
Node(int _id) { id = _id;}
|
deba@2115
|
160 |
public:
|
deba@2115
|
161 |
Node() {}
|
deba@2115
|
162 |
Node (Invalid) { id = -1; }
|
deba@2115
|
163 |
bool operator==(const Node node) const {return id == node.id;}
|
deba@2115
|
164 |
bool operator!=(const Node node) const {return id != node.id;}
|
deba@2115
|
165 |
bool operator<(const Node node) const {return id < node.id;}
|
deba@2115
|
166 |
};
|
deba@2115
|
167 |
|
deba@2115
|
168 |
|
deba@2115
|
169 |
|
deba@2115
|
170 |
class Edge {
|
deba@2115
|
171 |
friend class FullUGraphBase;
|
deba@2115
|
172 |
|
deba@2115
|
173 |
protected:
|
deba@2115
|
174 |
int id; // _nodeNum * target + source;
|
deba@2115
|
175 |
|
deba@2115
|
176 |
Edge(int _id) : id(_id) {}
|
deba@2115
|
177 |
|
deba@2115
|
178 |
public:
|
deba@2115
|
179 |
Edge() { }
|
deba@2115
|
180 |
Edge (Invalid) { id = -1; }
|
deba@2115
|
181 |
bool operator==(const Edge edge) const {return id == edge.id;}
|
deba@2115
|
182 |
bool operator!=(const Edge edge) const {return id != edge.id;}
|
deba@2115
|
183 |
bool operator<(const Edge edge) const {return id < edge.id;}
|
deba@2115
|
184 |
};
|
deba@2115
|
185 |
|
deba@2115
|
186 |
void first(Node& node) const {
|
deba@2115
|
187 |
node.id = _nodeNum - 1;
|
deba@2115
|
188 |
}
|
deba@2115
|
189 |
|
deba@2115
|
190 |
static void next(Node& node) {
|
deba@2115
|
191 |
--node.id;
|
deba@2115
|
192 |
}
|
deba@2115
|
193 |
|
deba@2115
|
194 |
void first(Edge& edge) const {
|
deba@2115
|
195 |
edge.id = _edgeNum - 1;
|
deba@2115
|
196 |
}
|
deba@2115
|
197 |
|
deba@2115
|
198 |
static void next(Edge& edge) {
|
deba@2115
|
199 |
--edge.id;
|
deba@2115
|
200 |
}
|
deba@2115
|
201 |
|
deba@2115
|
202 |
void firstOut(Edge& edge, const Node& node) const {
|
deba@2115
|
203 |
int src = node.id;
|
deba@2115
|
204 |
int trg = 0;
|
deba@2115
|
205 |
edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
|
deba@2115
|
206 |
}
|
deba@2115
|
207 |
|
deba@2115
|
208 |
/// \todo with specialized iterators we can make faster iterating
|
deba@2115
|
209 |
void nextOut(Edge& edge) const {
|
deba@2115
|
210 |
int src = source(edge).id;
|
deba@2115
|
211 |
int trg = target(edge).id;
|
deba@2115
|
212 |
++trg;
|
deba@2115
|
213 |
edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
|
deba@2115
|
214 |
}
|
deba@2115
|
215 |
|
deba@2115
|
216 |
void firstIn(Edge& edge, const Node& node) const {
|
deba@2115
|
217 |
int src = node.id + 1;
|
deba@2115
|
218 |
int trg = node.id;
|
deba@2115
|
219 |
edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
|
deba@2115
|
220 |
}
|
deba@2115
|
221 |
|
deba@2115
|
222 |
void nextIn(Edge& edge) const {
|
deba@2115
|
223 |
int src = source(edge).id;
|
deba@2115
|
224 |
int trg = target(edge).id;
|
deba@2115
|
225 |
++src;
|
deba@2115
|
226 |
edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
|
deba@2115
|
227 |
}
|
deba@2115
|
228 |
|
deba@2115
|
229 |
};
|
deba@2115
|
230 |
|
deba@2115
|
231 |
typedef UGraphExtender<UndirGraphExtender<FullUGraphBase> >
|
deba@2115
|
232 |
ExtendedFullUGraphBase;
|
deba@2115
|
233 |
|
deba@2115
|
234 |
/// \ingroup graphs
|
deba@2115
|
235 |
///
|
deba@2115
|
236 |
/// \brief An undirected full graph class.
|
deba@2115
|
237 |
///
|
deba@2115
|
238 |
/// This is a simple and fast undirected full graph implementation.
|
deba@2115
|
239 |
/// It is completely static, so you can neither add nor delete either
|
deba@2115
|
240 |
/// edges or nodes.
|
deba@2115
|
241 |
///
|
deba@2115
|
242 |
/// The main difference beetween the \e FullGraph and \e FullUGraph class
|
deba@2115
|
243 |
/// is that this class conforms to the undirected graph concept and
|
deba@2115
|
244 |
/// it does not contain the loop edges.
|
deba@2115
|
245 |
///
|
deba@2115
|
246 |
/// \sa FullUGraphBase
|
deba@2115
|
247 |
/// \sa FullGraph
|
deba@2115
|
248 |
///
|
deba@2115
|
249 |
/// \author Balazs Dezso
|
deba@2115
|
250 |
class FullUGraph : public ExtendedFullUGraphBase {
|
deba@2115
|
251 |
public:
|
deba@2115
|
252 |
|
deba@2115
|
253 |
typedef ExtendedFullUGraphBase Parent;
|
deba@2115
|
254 |
|
deba@2115
|
255 |
/// \brief Constructor
|
deba@2115
|
256 |
FullUGraph() { construct(0); }
|
deba@2115
|
257 |
|
deba@2115
|
258 |
/// \brief Constructor
|
deba@2115
|
259 |
FullUGraph(int n) { construct(n); }
|
deba@2115
|
260 |
|
deba@2115
|
261 |
/// \brief Resize the graph
|
deba@2115
|
262 |
///
|
deba@2115
|
263 |
/// Resize the graph. The function will fully destroy and build the graph.
|
deba@2115
|
264 |
/// This cause that the maps of the graph will reallocated
|
deba@2115
|
265 |
/// automatically and the previous values will be lost.
|
deba@2115
|
266 |
void resize(int n) {
|
deba@2115
|
267 |
Parent::getNotifier(Edge()).clear();
|
deba@2115
|
268 |
Parent::getNotifier(UEdge()).clear();
|
deba@2115
|
269 |
Parent::getNotifier(Node()).clear();
|
deba@2115
|
270 |
construct(n);
|
deba@2115
|
271 |
Parent::getNotifier(Node()).build();
|
deba@2115
|
272 |
Parent::getNotifier(UEdge()).build();
|
deba@2115
|
273 |
Parent::getNotifier(Edge()).build();
|
deba@2115
|
274 |
}
|
deba@2115
|
275 |
};
|
deba@2115
|
276 |
|
deba@2115
|
277 |
} //namespace lemon
|
deba@2115
|
278 |
|
deba@2115
|
279 |
|
deba@2115
|
280 |
#endif //LEMON_FULL_GRAPH_H
|