Location: LEMON/LEMON-official/lemon/hypercube_graph.h

Load file history
gravatar
kpeter (Peter Kovacs)
Various doc improvements (#248) - Rename all the ugly template parameters (too long and/or starting with an underscore). - Rename function parameters starting with an underscore. - Extend the doc for many classes. - Use LaTeX-style O(...) expressions only for the complicated ones. - A lot of small unification changes. - Small fixes. - Some other improvements.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#ifndef HYPERCUBE_GRAPH_H
#define HYPERCUBE_GRAPH_H
#include <vector>
#include <lemon/core.h>
#include <lemon/assert.h>
#include <lemon/bits/graph_extender.h>
///\ingroup graphs
///\file
///\brief HypercubeGraph class.
namespace lemon {
class HypercubeGraphBase {
public:
typedef HypercubeGraphBase Graph;
class Node;
class Edge;
class Arc;
public:
HypercubeGraphBase() {}
protected:
void construct(int dim) {
LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1.");
_dim = dim;
_node_num = 1 << dim;
_edge_num = dim * (1 << (dim-1));
}
public:
typedef True NodeNumTag;
typedef True EdgeNumTag;
typedef True ArcNumTag;
int nodeNum() const { return _node_num; }
int edgeNum() const { return _edge_num; }
int arcNum() const { return 2 * _edge_num; }
int maxNodeId() const { return _node_num - 1; }
int maxEdgeId() const { return _edge_num - 1; }
int maxArcId() const { return 2 * _edge_num - 1; }
static Node nodeFromId(int id) { return Node(id); }
static Edge edgeFromId(int id) { return Edge(id); }
static Arc arcFromId(int id) { return Arc(id); }
static int id(Node node) { return node._id; }
static int id(Edge edge) { return edge._id; }
static int id(Arc arc) { return arc._id; }
Node u(Edge edge) const {
int base = edge._id & ((1 << (_dim-1)) - 1);
int k = edge._id >> (_dim-1);
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1));
}
Node v(Edge edge) const {
int base = edge._id & ((1 << (_dim-1)) - 1);
int k = edge._id >> (_dim-1);
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)) | (1 << k);
}
Node source(Arc arc) const {
return (arc._id & 1) == 1 ? u(arc) : v(arc);
}
Node target(Arc arc) const {
return (arc._id & 1) == 1 ? v(arc) : u(arc);
}
typedef True FindEdgeTag;
typedef True FindArcTag;
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
if (prev != INVALID) return INVALID;
int d = u._id ^ v._id;
int k = 0;
if (d == 0) return INVALID;
for ( ; (d & 1) == 0; d >>= 1) ++k;
if (d >> 1 != 0) return INVALID;
return (k << (_dim-1)) | ((u._id >> (k+1)) << k) |
(u._id & ((1 << k) - 1));
}
Arc findArc(Node u, Node v, Arc prev = INVALID) const {
Edge edge = findEdge(u, v, prev);
if (edge == INVALID) return INVALID;
int k = edge._id >> (_dim-1);
return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1;
}
class Node {
friend class HypercubeGraphBase;
protected:
int _id;
Node(int id) : _id(id) {}
public:
Node() {}
Node (Invalid) : _id(-1) {}
bool operator==(const Node node) const {return _id == node._id;}
bool operator!=(const Node node) const {return _id != node._id;}
bool operator<(const Node node) const {return _id < node._id;}
};
class Edge {
friend class HypercubeGraphBase;
friend class Arc;
protected:
int _id;
Edge(int id) : _id(id) {}
public:
Edge() {}
Edge (Invalid) : _id(-1) {}
bool operator==(const Edge edge) const {return _id == edge._id;}
bool operator!=(const Edge edge) const {return _id != edge._id;}
bool operator<(const Edge edge) const {return _id < edge._id;}
};
class Arc {
friend class HypercubeGraphBase;
protected:
int _id;
Arc(int id) : _id(id) {}
public:
Arc() {}
Arc (Invalid) : _id(-1) {}
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
bool operator==(const Arc arc) const {return _id == arc._id;}
bool operator!=(const Arc arc) const {return _id != arc._id;}
bool operator<(const Arc arc) const {return _id < arc._id;}
};
void first(Node& node) const {
node._id = _node_num - 1;
}
static void next(Node& node) {
--node._id;
}
void first(Edge& edge) const {
edge._id = _edge_num - 1;
}
static void next(Edge& edge) {
--edge._id;
}
void first(Arc& arc) const {
arc._id = 2 * _edge_num - 1;
}
static void next(Arc& arc) {
--arc._id;
}
void firstInc(Edge& edge, bool& dir, const Node& node) const {
edge._id = node._id >> 1;
dir = (node._id & 1) == 0;
}
void nextInc(Edge& edge, bool& dir) const {
Node n = dir ? u(edge) : v(edge);
int k = (edge._id >> (_dim-1)) + 1;
if (k < _dim) {
edge._id = (k << (_dim-1)) |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
dir = ((n._id >> k) & 1) == 0;
} else {
edge._id = -1;
dir = true;
}
}
void firstOut(Arc& arc, const Node& node) const {
arc._id = ((node._id >> 1) << 1) | (~node._id & 1);
}
void nextOut(Arc& arc) const {
Node n = (arc._id & 1) == 1 ? u(arc) : v(arc);
int k = (arc._id >> _dim) + 1;
if (k < _dim) {
arc._id = (k << (_dim-1)) |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
arc._id = (arc._id << 1) | (~(n._id >> k) & 1);
} else {
arc._id = -1;
}
}
void firstIn(Arc& arc, const Node& node) const {
arc._id = ((node._id >> 1) << 1) | (node._id & 1);
}
void nextIn(Arc& arc) const {
Node n = (arc._id & 1) == 1 ? v(arc) : u(arc);
int k = (arc._id >> _dim) + 1;
if (k < _dim) {
arc._id = (k << (_dim-1)) |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
arc._id = (arc._id << 1) | ((n._id >> k) & 1);
} else {
arc._id = -1;
}
}
static bool direction(Arc arc) {
return (arc._id & 1) == 1;
}
static Arc direct(Edge edge, bool dir) {
return Arc((edge._id << 1) | (dir ? 1 : 0));
}
int dimension() const {
return _dim;
}
bool projection(Node node, int n) const {
return static_cast<bool>(node._id & (1 << n));
}
int dimension(Edge edge) const {
return edge._id >> (_dim-1);
}
int dimension(Arc arc) const {
return arc._id >> _dim;
}
int index(Node node) const {
return node._id;
}
Node operator()(int ix) const {
return Node(ix);
}
private:
int _dim;
int _node_num, _edge_num;
};
typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase;
/// \ingroup graphs
///
/// \brief Hypercube graph class
///
/// This class implements a special graph type. The nodes of the graph
/// are indiced with integers with at most \c dim binary digits.
/// Two nodes are connected in the graph if and only if their indices
/// differ only on one position in the binary form.
///
/// \note The type of the indices is chosen to \c int for efficiency
/// reasons. Thus the maximum dimension of this implementation is 26
/// (assuming that the size of \c int is 32 bit).
///
/// This graph type fully conforms to the \ref concepts::Graph
/// "Graph" concept, and it also has an important extra feature
/// that its maps are real \ref concepts::ReferenceMap
/// "reference map"s.
class HypercubeGraph : public ExtendedHypercubeGraphBase {
public:
typedef ExtendedHypercubeGraphBase Parent;
/// \brief Constructs a hypercube graph with \c dim dimensions.
///
/// Constructs a hypercube graph with \c dim dimensions.
HypercubeGraph(int dim) { construct(dim); }
/// \brief The number of dimensions.
///
/// Gives back the number of dimensions.
int dimension() const {
return Parent::dimension();
}
/// \brief Returns \c true if the n'th bit of the node is one.
///
/// Returns \c true if the n'th bit of the node is one.
bool projection(Node node, int n) const {
return Parent::projection(node, n);
}
/// \brief The dimension id of an edge.
///
/// Gives back the dimension id of the given edge.
/// It is in the [0..dim-1] range.
int dimension(Edge edge) const {
return Parent::dimension(edge);
}
/// \brief The dimension id of an arc.
///
/// Gives back the dimension id of the given arc.
/// It is in the [0..dim-1] range.
int dimension(Arc arc) const {
return Parent::dimension(arc);
}
/// \brief The index of a node.
///
/// Gives back the index of the given node.
/// The lower bits of the integer describes the node.
int index(Node node) const {
return Parent::index(node);
}
/// \brief Gives back a node by its index.
///
/// Gives back a node by its index.
Node operator()(int ix) const {
return Parent::operator()(ix);
}
/// \brief Number of nodes.
int nodeNum() const { return Parent::nodeNum(); }
/// \brief Number of edges.
int edgeNum() const { return Parent::edgeNum(); }
/// \brief Number of arcs.
int arcNum() const { return Parent::arcNum(); }
/// \brief Linear combination map.
///
/// This map makes possible to give back a linear combination
/// for each node. It works like the \c std::accumulate function,
/// so it accumulates the \c bf binary function with the \c fv first
/// value. The map accumulates only on that positions (dimensions)
/// where the index of the node is one. The values that have to be
/// accumulated should be given by the \c begin and \c end iterators
/// and the length of this range should be equal to the dimension
/// number of the graph.
///
///\code
/// const int DIM = 3;
/// HypercubeGraph graph(DIM);
/// dim2::Point<double> base[DIM];
/// for (int k = 0; k < DIM; ++k) {
/// base[k].x = rnd();
/// base[k].y = rnd();
/// }
/// HypercubeGraph::HyperMap<dim2::Point<double> >
/// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
///\endcode
///
/// \see HypercubeGraph
template <typename T, typename BF = std::plus<T> >
class HyperMap {
public:
/// \brief The key type of the map
typedef Node Key;
/// \brief The value type of the map
typedef T Value;
/// \brief Constructor for HyperMap.
///
/// Construct a HyperMap for the given graph. The values that have
/// to be accumulated should be given by the \c begin and \c end
/// iterators and the length of this range should be equal to the
/// dimension number of the graph.
///
/// This map accumulates the \c bf binary function with the \c fv
/// first value on that positions (dimensions) where the index of
/// the node is one.
template <typename It>
HyperMap(const Graph& graph, It begin, It end,
T fv = 0, const BF& bf = BF())
: _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf)
{
LEMON_ASSERT(_values.size() == graph.dimension(),
"Wrong size of range");
}
/// \brief The partial accumulated value.
///
/// Gives back the partial accumulated value.
Value operator[](const Key& k) const {
Value val = _first_value;
int id = _graph.index(k);
int n = 0;
while (id != 0) {
if (id & 1) {
val = _bin_func(val, _values[n]);
}
id >>= 1;
++n;
}
return val;
}
private:
const Graph& _graph;
std::vector<T> _values;
T _first_value;
BF _bin_func;
};
};
}
#endif