3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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
24 #include <lemon/bits/invalid.h>
25 #include <lemon/bits/utility.h>
26 #include <lemon/error.h>
28 #include <lemon/bits/base_extender.h>
29 #include <lemon/bits/graph_extender.h>
33 ///\brief HyperCubeGraph class.
37 class HyperCubeGraphBase {
41 typedef HyperCubeGraphBase Graph;
48 HyperCubeGraphBase() {}
52 void construct(int dim) {
60 typedef True NodeNumTag;
61 typedef True EdgeNumTag;
63 int nodeNum() const { return _nodeNum; }
64 int edgeNum() const { return _nodeNum * _dim; }
66 int maxNodeId() const { return nodeNum() - 1; }
67 int maxEdgeId() const { return edgeNum() - 1; }
69 Node source(Edge e) const {
73 Node target(Edge e) const {
74 return (e.id / _dim) ^ ( 1 << (e.id % _dim));
77 static int id(Node v) { return v.id; }
78 static int id(Edge e) { return e.id; }
80 static Node nodeFromId(int id) { return Node(id);}
82 static Edge edgeFromId(int id) { return Edge(id);}
85 friend class HyperCubeGraphBase;
89 Node(int _id) { id = _id;}
92 Node (Invalid) { id = -1; }
93 bool operator==(const Node node) const {return id == node.id;}
94 bool operator!=(const Node node) const {return id != node.id;}
95 bool operator<(const Node node) const {return id < node.id;}
99 friend class HyperCubeGraphBase;
104 Edge(int _id) : id(_id) {}
108 Edge (Invalid) { id = -1; }
109 bool operator==(const Edge edge) const {return id == edge.id;}
110 bool operator!=(const Edge edge) const {return id != edge.id;}
111 bool operator<(const Edge edge) const {return id < edge.id;}
114 void first(Node& node) const {
115 node.id = nodeNum() - 1;
118 static void next(Node& node) {
122 void first(Edge& edge) const {
123 edge.id = edgeNum() - 1;
126 static void next(Edge& edge) {
130 void firstOut(Edge& edge, const Node& node) const {
131 edge.id = node.id * _dim;
134 void nextOut(Edge& edge) const {
136 if (edge.id % _dim == 0) edge.id = -1;
139 void firstIn(Edge& edge, const Node& node) const {
140 edge.id = (node.id ^ 1) * _dim;
143 void nextIn(Edge& edge) const {
144 int cnt = edge.id % _dim;
145 if ((cnt + 1) % _dim == 0) {
148 edge.id = ((edge.id / _dim) ^ ((1 << cnt) * 3)) * _dim + cnt + 1;
152 int dimension() const {
156 bool projection(Node node, int n) const {
157 return (bool)(node.id & (1 << n));
160 int dimension(Edge edge) const {
161 return edge.id % _dim;
164 int index(Node node) const {
168 Node operator()(int index) const {
177 typedef GraphExtender<HyperCubeGraphBase> ExtendedHyperCubeGraphBase;
181 /// \brief HyperCube graph class
183 /// This class implements a special graph type. The nodes of the
184 /// graph can be indiced with integers with at most \c dim binary length.
185 /// Two nodes are connected in the graph if the indices differ only
186 /// on one position in the binary form.
188 /// \note The type of the \c ids is chosen to \c int because efficiency
189 /// reasons. This way the maximal dimension of this implementation
192 /// The graph type is fully conform to the \ref concepts::Graph
193 /// concept but it does not conform to the \ref concepts::UGraph.
195 /// \author Balazs Dezso
196 class HyperCubeGraph : public ExtendedHyperCubeGraphBase {
199 typedef ExtendedHyperCubeGraphBase Parent;
201 /// \brief Construct a graph with \c dim dimension.
203 /// Construct a graph with \c dim dimension.
204 HyperCubeGraph(int dim) { construct(dim); }
206 /// \brief Gives back the number of the dimensions.
208 /// Gives back the number of the dimensions.
209 int dimension() const {
210 return Parent::dimension();
213 /// \brief Returns true if the n'th bit of the node is one.
215 /// Returns true if the n'th bit of the node is one.
216 bool projection(Node node, int n) const {
217 return Parent::projection(node, n);
220 /// \brief The dimension id of the edge.
222 /// It returns the dimension id of the edge. It can
223 /// be in the \f$ \{0, 1, \dots, dim-1\} \f$ intervall.
224 int dimension(Edge edge) const {
225 return Parent::dimension(edge);
228 /// \brief Gives back the index of the node.
230 /// Gives back the index of the node. The lower bits of the
231 /// integer describes the node.
232 int index(Node node) const {
233 return Parent::index(node);
236 /// \brief Gives back the node by its index.
238 /// Gives back the node by its index.
239 Node operator()(int index) const {
240 return Parent::operator()(index);
243 /// \brief Number of nodes.
244 int nodeNum() const { return Parent::nodeNum(); }
245 /// \brief Number of edges.
246 int edgeNum() const { return Parent::edgeNum(); }
248 /// \brief Linear combination map.
250 /// It makes possible to give back a linear combination
251 /// for each node. This function works like the \c std::accumulate
252 /// so it accumulates the \c bf binary function with the \c fv
253 /// first value. The map accumulates only on that dimensions where
254 /// the node's index is one. The accumulated values should be
255 /// given by the \c begin and \c end iterators and this range's length
256 /// should be the dimension number of the graph.
259 /// const int DIM = 3;
260 /// HyperCubeGraph graph(DIM);
261 /// dim2::Point<double> base[DIM];
262 /// for (int k = 0; k < DIM; ++k) {
263 /// base[k].x = rnd();
264 /// base[k].y = rnd();
266 /// HyperCubeGraph::HyperMap<dim2::Point<double> >
267 /// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
270 /// \see HyperCubeGraph
271 template <typename T, typename BF = std::plus<T> >
279 /// \brief Constructor for HyperMap.
281 /// Construct a HyperMap for the given graph. The accumulated values
282 /// should be given by the \c begin and \c end iterators and this
283 /// range's length should be the dimension number of the graph.
285 /// This function accumulates the \c bf binary function with
286 /// the \c fv first value. The map accumulates only on that dimensions
287 /// where the node's index is one.
288 template <typename It>
289 HyperMap(const Graph& graph, It begin, It end,
290 T fv = 0.0, const BF& bf = BF())
291 : _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf) {
292 LEMON_ASSERT(_values.size() == graph.dimension(),
293 "Wrong size of dimension");
296 /// \brief Gives back the partial accumulated value.
298 /// Gives back the partial accumulated value.
299 Value operator[](Key k) const {
300 Value val = _first_value;
301 int id = _graph.index(k);
305 val = _bin_func(val, _values[n]);
315 std::vector<T> _values;