Mapselector widget reached its first release, but there are still work to do on it, I know...
2 * lemon/hypercube_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef HYPERCUBE_GRAPH_H
18 #define HYPERCUBE_GRAPH_H
22 #include <lemon/invalid.h>
23 #include <lemon/utility.h>
25 #include <lemon/bits/iterable_graph_extender.h>
26 #include <lemon/bits/alteration_notifier.h>
27 #include <lemon/bits/default_map.h>
31 ///\brief HyperCubeGraph class.
35 /// \brief Base graph for HyperGraph.
37 /// Base graph for hyper-cube graph. It describes some member functions
38 /// which can be used in the HyperCubeGraph.
40 /// \warning Always use the HyperCubeGraph instead of this.
41 /// \see HyperCubeGraph
42 class HyperCubeGraphBase {
46 typedef HyperCubeGraphBase Graph;
53 HyperCubeGraphBase() {}
57 /// \brief Creates a hypercube graph with the given size.
59 /// Creates a hypercube graph with the given size.
60 void construct(int dim) {
68 typedef True NodeNumTag;
69 typedef True EdgeNumTag;
72 int nodeNum() const { return _nodeNum; }
74 int edgeNum() const { return _nodeNum * _dim; }
80 int maxId(Node = INVALID) const { return nodeNum() - 1; }
85 int maxId(Edge = INVALID) const { return edgeNum() - 1; }
87 /// \brief Gives back the source node of an edge.
89 /// Gives back the source node of an edge.
90 Node source(Edge e) const {
94 /// \brief Gives back the target node of an edge.
96 /// Gives back the target node of an edge.
97 Node target(Edge e) const {
98 return (e.id / _dim) ^ ( 1 << (e.id % _dim));
103 /// The ID of a valid Node is a nonnegative integer not greater than
104 /// \ref maxNodeId(). The range of the ID's is not surely continuous
105 /// and the greatest node ID can be actually less then \ref maxNodeId().
107 /// The ID of the \ref INVALID node is -1.
108 ///\return The ID of the node \c v.
110 static int id(Node v) { return v.id; }
113 /// The ID of a valid Edge is a nonnegative integer not greater than
114 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
115 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
117 /// The ID of the \ref INVALID edge is -1.
118 ///\return The ID of the edge \c e.
119 static int id(Edge e) { return e.id; }
121 static Node fromId(int id, Node) { return Node(id);}
123 static Edge fromId(int id, Edge) { return Edge(id);}
126 friend class HyperCubeGraphBase;
130 Node(int _id) { id = _id;}
133 Node (Invalid) { id = -1; }
134 bool operator==(const Node node) const {return id == node.id;}
135 bool operator!=(const Node node) const {return id != node.id;}
136 bool operator<(const Node node) const {return id < node.id;}
140 friend class HyperCubeGraphBase;
145 Edge(int _id) : id(_id) {}
149 Edge (Invalid) { id = -1; }
150 bool operator==(const Edge edge) const {return id == edge.id;}
151 bool operator!=(const Edge edge) const {return id != edge.id;}
152 bool operator<(const Edge edge) const {return id < edge.id;}
155 void first(Node& node) const {
156 node.id = nodeNum() - 1;
159 static void next(Node& node) {
163 void first(Edge& edge) const {
164 edge.id = edgeNum() - 1;
167 static void next(Edge& edge) {
171 void firstOut(Edge& edge, const Node& node) const {
172 edge.id = node.id * _dim;
175 void nextOut(Edge& edge) const {
177 if (edge.id % _dim == 0) edge.id = -1;
180 void firstIn(Edge& edge, const Node& node) const {
181 edge.id = (node.id ^ 1) * _dim;
184 void nextIn(Edge& edge) const {
185 int cnt = edge.id % _dim;
186 if ((cnt + 1) % _dim == 0) {
189 edge.id = ((edge.id / _dim) ^ ((1 << cnt) * 3)) * _dim + cnt + 1;
193 /// \brief Gives back the number of the dimensions.
195 /// Gives back the number of the dimensions.
196 int dimension() const {
200 /// \brief Returns true if the n'th bit of the node is one.
202 /// Returns true if the n'th bit of the node is one.
203 bool projection(Node node, int n) const {
204 return (bool)(node.id & (1 << n));
207 /// \brief The dimension id of the edge.
209 /// It returns the dimension id of the edge. It can
210 /// be in the ${0, 1, dim-1}$ intervall.
211 int dimension(Edge edge) const {
212 return edge.id % _dim;
215 /// \brief Gives back the index of the node.
217 /// Gives back the index of the node. The lower bits of the
218 /// integer describe the node.
219 int index(Node node) const {
223 /// \brief Gives back the node by its index.
225 /// Gives back the node by its index.
226 Node node(int index) const {
235 typedef StaticMappableGraphExtender<
236 IterableGraphExtender<
237 AlterableGraphExtender<
238 HyperCubeGraphBase > > > ExtendedHyperCubeGraphBase;
242 /// \brief HyperCube graph class
244 /// This class implements a special graph type. The nodes of the
245 /// graph can be indiced with integers with at most \c dim binary length.
246 /// Two nodes are connected in the graph if the indices differ only
247 /// on one position in the binary form.
249 /// \note The type of the \c ids is chosen to \c int because efficiency
250 /// reasons. This way the maximal dimension of this implementation
253 /// The graph type is fully conform to the \ref concept::StaticGraph
254 /// concept but it does not conform to the \ref concept::UndirGraph.
256 /// \see HyperCubeGraphBase
257 /// \author Balazs Dezso
258 class HyperCubeGraph : public ExtendedHyperCubeGraphBase {
261 /// \brief Construct a graph with \c dim dimension.
263 /// Construct a graph with \c dim dimension.
264 HyperCubeGraph(int dim) { construct(dim); }
266 /// \brief Linear combination map.
268 /// It makes possible to give back a linear combination
269 /// for each node. This function works like the \c std::accumulate
270 /// so it accumulates the \c bf binary function with the \c fv
271 /// first value. The map accumulates only on that dimensions where
272 /// the node's index is one. The accumulated values should be
273 /// given by the \c begin and \c end iterators and this range's length
274 /// should be the dimension number of the graph.
277 /// const int DIM = 3;
278 /// HyperCubeGraph graph(DIM);
279 /// xy<double> base[DIM];
280 /// for (int k = 0; k < DIM; ++k) {
281 /// base[k].x = rand() / (RAND_MAX + 1.0);
282 /// base[k].y = rand() / (RAND_MAX + 1.0);
284 /// HyperCubeGraph::HyperMap<xy<double> >
285 /// pos(graph, base, base + DIM, xy<double>(0.0, 0.0));
288 /// \see HyperCubeGraph
289 template <typename T, typename BF = std::plus<T> >
297 /// \brief Constructor for HyperMap.
299 /// Construct a HyperMap for the given graph. The accumulated values
300 /// should be given by the \c begin and \c end iterators and this
301 /// range's length should be the dimension number of the graph.
303 /// This function accumulates the \c bf binary function with
304 /// the \c fv first value. The map accumulates only on that dimensions
305 /// where the node's index is one.
306 template <typename It>
307 HyperMap(const Graph& graph, It begin, It end,
308 T fv = 0.0, const BF& bf = BF())
309 : _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf) {
310 if (_values.size() != graph.dimension()) {}
313 /// \brief Gives back the partial accumulated value.
315 /// Gives back the partial accumulated value.
316 Value operator[](Key k) const {
317 Value val = _first_value;
318 int id = _graph.index(k);
322 val = _bin_func(_values[n], _first_value);
332 std::vector<T> _values;