Location: LEMON/LEMON-main/lemon/hypercube_graph.h - annotation
Load file history
Port hypercube digraph structure from SVN 3503 (#57)
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 | r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 r364:b4a01426c0d9 | /* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2008
* 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 <iostream>
#include <vector>
#include <lemon/core.h>
#include <lemon/error.h>
#include <lemon/bits/base_extender.h>
#include <lemon/bits/graph_extender.h>
///\ingroup graphs
///\file
///\brief HypercubeDigraph class.
namespace lemon {
class HypercubeDigraphBase {
public:
typedef HypercubeDigraphBase Digraph;
class Node;
class Arc;
public:
HypercubeDigraphBase() {}
protected:
void construct(int dim) {
_dim = dim;
_nodeNum = 1 << dim;
}
public:
typedef True NodeNumTag;
typedef True ArcNumTag;
int nodeNum() const { return _nodeNum; }
int arcNum() const { return _nodeNum * _dim; }
int maxNodeId() const { return nodeNum() - 1; }
int maxArcId() const { return arcNum() - 1; }
Node source(Arc e) const {
return e.id / _dim;
}
Node target(Arc e) const {
return (e.id / _dim) ^ (1 << (e.id % _dim));
}
static int id(Node v) { return v.id; }
static int id(Arc e) { return e.id; }
static Node nodeFromId(int id) { return Node(id); }
static Arc arcFromId(int id) { return Arc(id); }
class Node {
friend class HypercubeDigraphBase;
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 Arc {
friend class HypercubeDigraphBase;
protected:
int id;
Arc(int _id) : id(_id) {}
public:
Arc() { }
Arc (Invalid) { id = -1; }
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 = nodeNum() - 1;
}
static void next(Node& node) {
--node.id;
}
void first(Arc& arc) const {
arc.id = arcNum() - 1;
}
static void next(Arc& arc) {
--arc.id;
}
void firstOut(Arc& arc, const Node& node) const {
arc.id = node.id * _dim;
}
void nextOut(Arc& arc) const {
++arc.id;
if (arc.id % _dim == 0) arc.id = -1;
}
void firstIn(Arc& arc, const Node& node) const {
arc.id = (node.id ^ 1) * _dim;
}
void nextIn(Arc& arc) const {
int cnt = arc.id % _dim;
if ((cnt + 1) % _dim == 0) {
arc.id = -1;
} else {
arc.id = ((arc.id / _dim) ^ ((1 << cnt) * 3)) * _dim + cnt + 1;
}
}
int dimension() const {
return _dim;
}
bool projection(Node node, int n) const {
return static_cast<bool>(node.id & (1 << n));
}
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, _nodeNum;
};
typedef DigraphExtender<HypercubeDigraphBase> ExtendedHypercubeDigraphBase;
/// \ingroup digraphs
///
/// \brief Hypercube digraph class
///
/// This class implements a special digraph type. The nodes of the
/// digraph are indiced with integers with at most \c dim binary digits.
/// Two nodes are connected in the digraph if the indices differ only
/// on one position in the binary form.
///
/// \note The type of the \c ids is chosen to \c int because efficiency
/// reasons. Thus the maximum dimension of this implementation is 26.
///
/// The digraph type is fully conform to the \ref concepts::Digraph
/// concept but it does not conform to \ref concepts::Graph.
class HypercubeDigraph : public ExtendedHypercubeDigraphBase {
public:
typedef ExtendedHypercubeDigraphBase Parent;
/// \brief Construct a hypercube digraph with \c dim dimension.
///
/// Construct a hypercube digraph with \c dim dimension.
HypercubeDigraph(int dim) { construct(dim); }
/// \brief Gives back the number of the dimensions.
///
/// Gives back the number of the dimensions.
int dimension() const {
return Parent::dimension();
}
/// \brief Returns true if the n'th bit of the node is one.
///
/// Returns 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 the arc.
///
/// It returns the dimension id of the arc. It can
/// be in the \f$ \{0, 1, \dots, dim-1\} \f$ interval.
int dimension(Arc arc) const {
return Parent::dimension(arc);
}
/// \brief Gives back the index of the node.
///
/// Gives back the index of the node. The lower bits of the
/// integer describes the node.
int index(Node node) const {
return Parent::index(node);
}
/// \brief Gives back the node by its index.
///
/// Gives back the 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 arcs.
int arcNum() const { return Parent::arcNum(); }
/// \brief Linear combination map.
///
/// It makes possible to give back a linear combination
/// for each node. This function works like the \c std::accumulate
/// so it accumulates the \c bf binary function with the \c fv
/// first value. The map accumulates only on that dimensions where
/// the node's index is one. The accumulated values 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 digraph.
///
///\code
/// const int DIM = 3;
/// HypercubeDigraph digraph(DIM);
/// dim2::Point<double> base[DIM];
/// for (int k = 0; k < DIM; ++k) {
/// base[k].x = rnd();
/// base[k].y = rnd();
/// }
/// HypercubeDigraph::HyperMap<dim2::Point<double> >
/// pos(digraph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
///\endcode
///
/// \see HypercubeDigraph
template <typename T, typename BF = std::plus<T> >
class HyperMap {
public:
typedef Node Key;
typedef T Value;
/// \brief Constructor for HyperMap.
///
/// Construct a HyperMap for the given digraph. The accumulated values
/// 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 digraph.
///
/// This function accumulates the \c bf binary function with
/// the \c fv first value. The map accumulates only on that dimensions
/// where the node's index is one.
template <typename It>
HyperMap(const Digraph& digraph, It begin, It end,
T fv = 0.0, const BF& bf = BF())
: _graph(digraph), _values(begin, end), _first_value(fv), _bin_func(bf)
{
LEMON_ASSERT(_values.size() == digraph.dimension(),
"Wrong size of dimension");
}
/// \brief Gives back the partial accumulated value.
///
/// Gives back the partial accumulated value.
Value operator[](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 Digraph& _graph;
std::vector<T> _values;
T _first_value;
BF _bin_func;
};
};
}
#endif
|