1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef HYPERCUBE_GRAPH_H
20 #define HYPERCUBE_GRAPH_H
23 #include <lemon/core.h>
24 #include <lemon/assert.h>
25 #include <lemon/bits/graph_extender.h>
29 ///\brief HypercubeGraph class.
33 class HypercubeGraphBase {
37 typedef HypercubeGraphBase Graph;
45 HypercubeGraphBase() {}
49 void construct(int dim) {
50 LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1.");
53 _edge_num = dim * (1 << (dim-1));
58 typedef True NodeNumTag;
59 typedef True EdgeNumTag;
60 typedef True ArcNumTag;
62 int nodeNum() const { return _node_num; }
63 int edgeNum() const { return _edge_num; }
64 int arcNum() const { return 2 * _edge_num; }
66 int maxNodeId() const { return _node_num - 1; }
67 int maxEdgeId() const { return _edge_num - 1; }
68 int maxArcId() const { return 2 * _edge_num - 1; }
70 static Node nodeFromId(int id) { return Node(id); }
71 static Edge edgeFromId(int id) { return Edge(id); }
72 static Arc arcFromId(int id) { return Arc(id); }
74 static int id(Node node) { return node._id; }
75 static int id(Edge edge) { return edge._id; }
76 static int id(Arc arc) { return arc._id; }
78 Node u(Edge edge) const {
79 int base = edge._id & ((1 << (_dim-1)) - 1);
80 int k = edge._id >> (_dim-1);
81 return ((base >> k) << (k+1)) | (base & ((1 << k) - 1));
84 Node v(Edge edge) const {
85 int base = edge._id & ((1 << (_dim-1)) - 1);
86 int k = edge._id >> (_dim-1);
87 return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)) | (1 << k);
90 Node source(Arc arc) const {
91 return (arc._id & 1) == 1 ? u(arc) : v(arc);
94 Node target(Arc arc) const {
95 return (arc._id & 1) == 1 ? v(arc) : u(arc);
98 typedef True FindEdgeTag;
99 typedef True FindArcTag;
101 Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
102 if (prev != INVALID) return INVALID;
103 int d = u._id ^ v._id;
105 if (d == 0) return INVALID;
106 for ( ; (d & 1) == 0; d >>= 1) ++k;
107 if (d >> 1 != 0) return INVALID;
108 return (k << (_dim-1)) | ((u._id >> (k+1)) << k) |
109 (u._id & ((1 << k) - 1));
112 Arc findArc(Node u, Node v, Arc prev = INVALID) const {
113 Edge edge = findEdge(u, v, prev);
114 if (edge == INVALID) return INVALID;
115 int k = edge._id >> (_dim-1);
116 return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1;
120 friend class HypercubeGraphBase;
124 Node(int id) : _id(id) {}
127 Node (Invalid) : _id(-1) {}
128 bool operator==(const Node node) const {return _id == node._id;}
129 bool operator!=(const Node node) const {return _id != node._id;}
130 bool operator<(const Node node) const {return _id < node._id;}
134 friend class HypercubeGraphBase;
140 Edge(int id) : _id(id) {}
144 Edge (Invalid) : _id(-1) {}
145 bool operator==(const Edge edge) const {return _id == edge._id;}
146 bool operator!=(const Edge edge) const {return _id != edge._id;}
147 bool operator<(const Edge edge) const {return _id < edge._id;}
151 friend class HypercubeGraphBase;
156 Arc(int id) : _id(id) {}
160 Arc (Invalid) : _id(-1) {}
161 operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
162 bool operator==(const Arc arc) const {return _id == arc._id;}
163 bool operator!=(const Arc arc) const {return _id != arc._id;}
164 bool operator<(const Arc arc) const {return _id < arc._id;}
167 void first(Node& node) const {
168 node._id = _node_num - 1;
171 static void next(Node& node) {
175 void first(Edge& edge) const {
176 edge._id = _edge_num - 1;
179 static void next(Edge& edge) {
183 void first(Arc& arc) const {
184 arc._id = 2 * _edge_num - 1;
187 static void next(Arc& arc) {
191 void firstInc(Edge& edge, bool& dir, const Node& node) const {
192 edge._id = node._id >> 1;
193 dir = (node._id & 1) == 0;
196 void nextInc(Edge& edge, bool& dir) const {
197 Node n = dir ? u(edge) : v(edge);
198 int k = (edge._id >> (_dim-1)) + 1;
200 edge._id = (k << (_dim-1)) |
201 ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
202 dir = ((n._id >> k) & 1) == 0;
209 void firstOut(Arc& arc, const Node& node) const {
210 arc._id = ((node._id >> 1) << 1) | (~node._id & 1);
213 void nextOut(Arc& arc) const {
214 Node n = (arc._id & 1) == 1 ? u(arc) : v(arc);
215 int k = (arc._id >> _dim) + 1;
217 arc._id = (k << (_dim-1)) |
218 ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
219 arc._id = (arc._id << 1) | (~(n._id >> k) & 1);
225 void firstIn(Arc& arc, const Node& node) const {
226 arc._id = ((node._id >> 1) << 1) | (node._id & 1);
229 void nextIn(Arc& arc) const {
230 Node n = (arc._id & 1) == 1 ? v(arc) : u(arc);
231 int k = (arc._id >> _dim) + 1;
233 arc._id = (k << (_dim-1)) |
234 ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
235 arc._id = (arc._id << 1) | ((n._id >> k) & 1);
241 static bool direction(Arc arc) {
242 return (arc._id & 1) == 1;
245 static Arc direct(Edge edge, bool dir) {
246 return Arc((edge._id << 1) | (dir ? 1 : 0));
249 int dimension() const {
253 bool projection(Node node, int n) const {
254 return static_cast<bool>(node._id & (1 << n));
257 int dimension(Edge edge) const {
258 return edge._id >> (_dim-1);
261 int dimension(Arc arc) const {
262 return arc._id >> _dim;
265 static int index(Node node) {
269 Node operator()(int ix) const {
275 int _node_num, _edge_num;
279 typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase;
283 /// \brief Hypercube graph class
285 /// HypercubeGraph implements a special graph type. The nodes of the
286 /// graph are indexed with integers having at most \c dim binary digits.
287 /// Two nodes are connected in the graph if and only if their indices
288 /// differ only on one position in the binary form.
289 /// This class is completely static and it needs constant memory space.
290 /// Thus you can neither add nor delete nodes or edges, however
291 /// the structure can be resized using resize().
293 /// This type fully conforms to the \ref concepts::Graph "Graph concept".
294 /// Most of its member functions and nested classes are documented
295 /// only in the concept class.
297 /// \note The type of the indices is chosen to \c int for efficiency
298 /// reasons. Thus the maximum dimension of this implementation is 26
299 /// (assuming that the size of \c int is 32 bit).
300 class HypercubeGraph : public ExtendedHypercubeGraphBase {
301 typedef ExtendedHypercubeGraphBase Parent;
305 /// \brief Constructs a hypercube graph with \c dim dimensions.
307 /// Constructs a hypercube graph with \c dim dimensions.
308 HypercubeGraph(int dim) { construct(dim); }
310 /// \brief Resizes the graph
312 /// This function resizes the graph. It fully destroys and
313 /// rebuilds the structure, therefore the maps of the graph will be
314 /// reallocated automatically and the previous values will be lost.
315 void resize(int dim) {
316 Parent::notifier(Arc()).clear();
317 Parent::notifier(Edge()).clear();
318 Parent::notifier(Node()).clear();
320 Parent::notifier(Node()).build();
321 Parent::notifier(Edge()).build();
322 Parent::notifier(Arc()).build();
325 /// \brief The number of dimensions.
327 /// Gives back the number of dimensions.
328 int dimension() const {
329 return Parent::dimension();
332 /// \brief Returns \c true if the n'th bit of the node is one.
334 /// Returns \c true if the n'th bit of the node is one.
335 bool projection(Node node, int n) const {
336 return Parent::projection(node, n);
339 /// \brief The dimension id of an edge.
341 /// Gives back the dimension id of the given edge.
342 /// It is in the range <tt>[0..dim-1]</tt>.
343 int dimension(Edge edge) const {
344 return Parent::dimension(edge);
347 /// \brief The dimension id of an arc.
349 /// Gives back the dimension id of the given arc.
350 /// It is in the range <tt>[0..dim-1]</tt>.
351 int dimension(Arc arc) const {
352 return Parent::dimension(arc);
355 /// \brief The index of a node.
357 /// Gives back the index of the given node.
358 /// The lower bits of the integer describes the node.
359 static int index(Node node) {
360 return Parent::index(node);
363 /// \brief Gives back a node by its index.
365 /// Gives back a node by its index.
366 Node operator()(int ix) const {
367 return Parent::operator()(ix);
370 /// \brief Number of nodes.
371 int nodeNum() const { return Parent::nodeNum(); }
372 /// \brief Number of edges.
373 int edgeNum() const { return Parent::edgeNum(); }
374 /// \brief Number of arcs.
375 int arcNum() const { return Parent::arcNum(); }
377 /// \brief Linear combination map.
379 /// This map makes possible to give back a linear combination
380 /// for each node. It works like the \c std::accumulate function,
381 /// so it accumulates the \c bf binary function with the \c fv first
382 /// value. The map accumulates only on that positions (dimensions)
383 /// where the index of the node is one. The values that have to be
384 /// accumulated should be given by the \c begin and \c end iterators
385 /// and the length of this range should be equal to the dimension
386 /// number of the graph.
389 /// const int DIM = 3;
390 /// HypercubeGraph graph(DIM);
391 /// dim2::Point<double> base[DIM];
392 /// for (int k = 0; k < DIM; ++k) {
393 /// base[k].x = rnd();
394 /// base[k].y = rnd();
396 /// HypercubeGraph::HyperMap<dim2::Point<double> >
397 /// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
400 /// \see HypercubeGraph
401 template <typename T, typename BF = std::plus<T> >
405 /// \brief The key type of the map
407 /// \brief The value type of the map
410 /// \brief Constructor for HyperMap.
412 /// Construct a HyperMap for the given graph. The values that have
413 /// to be accumulated should be given by the \c begin and \c end
414 /// iterators and the length of this range should be equal to the
415 /// dimension number of the graph.
417 /// This map accumulates the \c bf binary function with the \c fv
418 /// first value on that positions (dimensions) where the index of
420 template <typename It>
421 HyperMap(const Graph& graph, It begin, It end,
422 T fv = 0, const BF& bf = BF())
423 : _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf)
425 LEMON_ASSERT(_values.size() == graph.dimension(),
426 "Wrong size of range");
429 /// \brief The partial accumulated value.
431 /// Gives back the partial accumulated value.
432 Value operator[](const Key& k) const {
433 Value val = _first_value;
434 int id = _graph.index(k);
438 val = _bin_func(val, _values[n]);
448 std::vector<T> _values;