lemon/hypercube_graph.h
changeset 366 efbd0ab50a77
parent 364 b4a01426c0d9
child 372 7b6466ed488a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/hypercube_graph.h	Thu Nov 06 14:40:32 2008 +0000
     1.3 @@ -0,0 +1,439 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef HYPERCUBE_GRAPH_H
    1.23 +#define HYPERCUBE_GRAPH_H
    1.24 +
    1.25 +#include <vector>
    1.26 +#include <lemon/core.h>
    1.27 +#include <lemon/assert.h>
    1.28 +#include <lemon/bits/graph_extender.h>
    1.29 +
    1.30 +///\ingroup graphs
    1.31 +///\file
    1.32 +///\brief HypercubeGraph class.
    1.33 +
    1.34 +namespace lemon {
    1.35 +
    1.36 +  class HypercubeGraphBase {
    1.37 +
    1.38 +  public:
    1.39 +
    1.40 +    typedef HypercubeGraphBase Graph;
    1.41 +
    1.42 +    class Node;
    1.43 +    class Edge;
    1.44 +    class Arc;
    1.45 +
    1.46 +  public:
    1.47 +
    1.48 +    HypercubeGraphBase() {}
    1.49 +
    1.50 +  protected:
    1.51 +
    1.52 +    void construct(int dim) {
    1.53 +      LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1.");
    1.54 +      _dim = dim;
    1.55 +      _node_num = 1 << dim;
    1.56 +      _edge_num = dim * (1 << dim-1);
    1.57 +    }
    1.58 +
    1.59 +  public:
    1.60 +
    1.61 +    typedef True NodeNumTag;
    1.62 +    typedef True EdgeNumTag;
    1.63 +    typedef True ArcNumTag;
    1.64 +
    1.65 +    int nodeNum() const { return _node_num; }
    1.66 +    int edgeNum() const { return _edge_num; }
    1.67 +    int arcNum() const { return 2 * _edge_num; }
    1.68 +
    1.69 +    int maxNodeId() const { return _node_num - 1; }
    1.70 +    int maxEdgeId() const { return _edge_num - 1; }
    1.71 +    int maxArcId() const { return 2 * _edge_num - 1; }
    1.72 +
    1.73 +    static Node nodeFromId(int id) { return Node(id); }
    1.74 +    static Edge edgeFromId(int id) { return Edge(id); }
    1.75 +    static Arc arcFromId(int id) { return Arc(id); }
    1.76 +
    1.77 +    static int id(Node node) { return node._id; }
    1.78 +    static int id(Edge edge) { return edge._id; }
    1.79 +    static int id(Arc arc) { return arc._id; }
    1.80 +
    1.81 +    Node u(Edge edge) const {
    1.82 +      int base = edge._id & ((1 << _dim-1) - 1);
    1.83 +      int k = edge._id >> _dim-1;
    1.84 +      return ((base >> k) << k+1) | (base & ((1 << k) - 1));
    1.85 +    }
    1.86 +
    1.87 +    Node v(Edge edge) const {
    1.88 +      int base = edge._id & ((1 << _dim-1) - 1);
    1.89 +      int k = edge._id >> _dim-1;
    1.90 +      return ((base >> k) << k+1) | (base & ((1 << k) - 1)) | (1 << k);
    1.91 +    }
    1.92 +
    1.93 +    Node source(Arc arc) const {
    1.94 +      return (arc._id & 1) == 1 ? u(arc) : v(arc);
    1.95 +    }
    1.96 +
    1.97 +    Node target(Arc arc) const {
    1.98 +      return (arc._id & 1) == 1 ? v(arc) : u(arc);
    1.99 +    }
   1.100 +
   1.101 +    typedef True FindEdgeTag;
   1.102 +    typedef True FindArcTag;
   1.103 +
   1.104 +    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
   1.105 +      if (prev != INVALID) return INVALID;
   1.106 +      int d = u._id ^ v._id;
   1.107 +      int k = 0;
   1.108 +      if (d == 0) return INVALID;
   1.109 +      for ( ; (d & 1) == 0; d >>= 1) ++k;
   1.110 +      if (d >> 1 != 0) return INVALID;
   1.111 +      return (k << _dim-1) | ((u._id >> k+1) << k) | (u._id & ((1 << k) - 1));
   1.112 +    }
   1.113 +
   1.114 +    Arc findArc(Node u, Node v, Arc prev = INVALID) const {
   1.115 +      Edge edge = findEdge(u, v, prev);
   1.116 +      if (edge == INVALID) return INVALID;
   1.117 +      int k = edge._id >> _dim-1;
   1.118 +      return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1;
   1.119 +    }
   1.120 +
   1.121 +    class Node {
   1.122 +      friend class HypercubeGraphBase;
   1.123 +
   1.124 +    protected:
   1.125 +      int _id;
   1.126 +      Node(int id) : _id(id) {}
   1.127 +    public:
   1.128 +      Node() {}
   1.129 +      Node (Invalid) : _id(-1) {}
   1.130 +      bool operator==(const Node node) const {return _id == node._id;}
   1.131 +      bool operator!=(const Node node) const {return _id != node._id;}
   1.132 +      bool operator<(const Node node) const {return _id < node._id;}
   1.133 +    };
   1.134 +
   1.135 +    class Edge {
   1.136 +      friend class HypercubeGraphBase;
   1.137 +      friend class Arc;
   1.138 +
   1.139 +    protected:
   1.140 +      int _id;
   1.141 +
   1.142 +      Edge(int id) : _id(id) {}
   1.143 +
   1.144 +    public:
   1.145 +      Edge() {}
   1.146 +      Edge (Invalid) : _id(-1) {}
   1.147 +      bool operator==(const Edge edge) const {return _id == edge._id;}
   1.148 +      bool operator!=(const Edge edge) const {return _id != edge._id;}
   1.149 +      bool operator<(const Edge edge) const {return _id < edge._id;}
   1.150 +    };
   1.151 +
   1.152 +    class Arc {
   1.153 +      friend class HypercubeGraphBase;
   1.154 +
   1.155 +    protected:
   1.156 +      int _id;
   1.157 +
   1.158 +      Arc(int id) : _id(id) {}
   1.159 +
   1.160 +    public:
   1.161 +      Arc() {}
   1.162 +      Arc (Invalid) : _id(-1) {}
   1.163 +      operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
   1.164 +      bool operator==(const Arc arc) const {return _id == arc._id;}
   1.165 +      bool operator!=(const Arc arc) const {return _id != arc._id;}
   1.166 +      bool operator<(const Arc arc) const {return _id < arc._id;}
   1.167 +    };
   1.168 +
   1.169 +    void first(Node& node) const {
   1.170 +      node._id = _node_num - 1;
   1.171 +    }
   1.172 +
   1.173 +    static void next(Node& node) {
   1.174 +      --node._id;
   1.175 +    }
   1.176 +
   1.177 +    void first(Edge& edge) const {
   1.178 +      edge._id = _edge_num - 1;
   1.179 +    }
   1.180 +
   1.181 +    static void next(Edge& edge) {
   1.182 +      --edge._id;
   1.183 +    }
   1.184 +
   1.185 +    void first(Arc& arc) const {
   1.186 +      arc._id = 2 * _edge_num - 1;
   1.187 +    }
   1.188 +
   1.189 +    static void next(Arc& arc) {
   1.190 +      --arc._id;
   1.191 +    }
   1.192 +
   1.193 +    void firstInc(Edge& edge, bool& dir, const Node& node) const {
   1.194 +      edge._id = node._id >> 1;
   1.195 +      dir = (node._id & 1) == 0;
   1.196 +    }
   1.197 +
   1.198 +    void nextInc(Edge& edge, bool& dir) const {
   1.199 +      Node n = dir ? u(edge) : v(edge);
   1.200 +      int k = (edge._id >> _dim-1) + 1;
   1.201 +      if (k < _dim) {
   1.202 +        edge._id = (k << _dim-1) |
   1.203 +                   ((n._id >> k+1) << k) | (n._id & ((1 << k) - 1));
   1.204 +        dir = ((n._id >> k) & 1) == 0;
   1.205 +      } else {
   1.206 +        edge._id = -1;
   1.207 +        dir = true;
   1.208 +      }
   1.209 +    }
   1.210 +
   1.211 +    void firstOut(Arc& arc, const Node& node) const {
   1.212 +      arc._id = ((node._id >> 1) << 1) | (~node._id & 1);
   1.213 +    }
   1.214 +
   1.215 +    void nextOut(Arc& arc) const {
   1.216 +      Node n = (arc._id & 1) == 1 ? u(arc) : v(arc);
   1.217 +      int k = (arc._id >> _dim) + 1;
   1.218 +      if (k < _dim) {
   1.219 +        arc._id = (k << _dim-1) |
   1.220 +                  ((n._id >> k+1) << k) | (n._id & ((1 << k) - 1));
   1.221 +        arc._id = (arc._id << 1) | (~(n._id >> k) & 1);
   1.222 +      } else {
   1.223 +        arc._id = -1;
   1.224 +      }
   1.225 +    }
   1.226 +
   1.227 +    void firstIn(Arc& arc, const Node& node) const {
   1.228 +      arc._id = ((node._id >> 1) << 1) | (node._id & 1);
   1.229 +    }
   1.230 +
   1.231 +    void nextIn(Arc& arc) const {
   1.232 +      Node n = (arc._id & 1) == 1 ? v(arc) : u(arc);
   1.233 +      int k = (arc._id >> _dim) + 1;
   1.234 +      if (k < _dim) {
   1.235 +        arc._id = (k << _dim-1) |
   1.236 +                  ((n._id >> k+1) << k) | (n._id & ((1 << k) - 1));
   1.237 +        arc._id = (arc._id << 1) | ((n._id >> k) & 1);
   1.238 +      } else {
   1.239 +        arc._id = -1;
   1.240 +      }
   1.241 +    }
   1.242 +
   1.243 +    static bool direction(Arc arc) {
   1.244 +      return (arc._id & 1) == 1;
   1.245 +    }
   1.246 +
   1.247 +    static Arc direct(Edge edge, bool dir) {
   1.248 +      return Arc((edge._id << 1) | (dir ? 1 : 0));
   1.249 +    }
   1.250 +
   1.251 +    int dimension() const {
   1.252 +      return _dim;
   1.253 +    }
   1.254 +
   1.255 +    bool projection(Node node, int n) const {
   1.256 +      return static_cast<bool>(node._id & (1 << n));
   1.257 +    }
   1.258 +
   1.259 +    int dimension(Edge edge) const {
   1.260 +      return edge._id >> _dim-1;
   1.261 +    }
   1.262 +
   1.263 +    int dimension(Arc arc) const {
   1.264 +      return arc._id >> _dim;
   1.265 +    }
   1.266 +
   1.267 +    int index(Node node) const {
   1.268 +      return node._id;
   1.269 +    }
   1.270 +
   1.271 +    Node operator()(int ix) const {
   1.272 +      return Node(ix);
   1.273 +    }
   1.274 +
   1.275 +  private:
   1.276 +    int _dim;
   1.277 +    int _node_num, _edge_num;
   1.278 +  };
   1.279 +
   1.280 +
   1.281 +  typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase;
   1.282 +
   1.283 +  /// \ingroup graphs
   1.284 +  ///
   1.285 +  /// \brief Hypercube graph class
   1.286 +  ///
   1.287 +  /// This class implements a special graph type. The nodes of the graph
   1.288 +  /// are indiced with integers with at most \c dim binary digits.
   1.289 +  /// Two nodes are connected in the graph if and only if their indices
   1.290 +  /// differ only on one position in the binary form.
   1.291 +  ///
   1.292 +  /// \note The type of the indices is chosen to \c int for efficiency
   1.293 +  /// reasons. Thus the maximum dimension of this implementation is 26
   1.294 +  /// (assuming that the size of \c int is 32 bit).
   1.295 +  ///
   1.296 +  /// This graph type is fully conform to the \ref concepts::Graph
   1.297 +  /// "Graph" concept, and it also has an important extra feature
   1.298 +  /// that its maps are real \ref concepts::ReferenceMap
   1.299 +  /// "reference map"s.
   1.300 +  class HypercubeGraph : public ExtendedHypercubeGraphBase {
   1.301 +  public:
   1.302 +
   1.303 +    typedef ExtendedHypercubeGraphBase Parent;
   1.304 +
   1.305 +    /// \brief Constructs a hypercube graph with \c dim dimensions.
   1.306 +    ///
   1.307 +    /// Constructs a hypercube graph with \c dim dimensions.
   1.308 +    HypercubeGraph(int dim) { construct(dim); }
   1.309 +
   1.310 +    /// \brief The number of dimensions.
   1.311 +    ///
   1.312 +    /// Gives back the number of dimensions.
   1.313 +    int dimension() const {
   1.314 +      return Parent::dimension();
   1.315 +    }
   1.316 +
   1.317 +    /// \brief Returns \c true if the n'th bit of the node is one.
   1.318 +    ///
   1.319 +    /// Returns \c true if the n'th bit of the node is one.
   1.320 +    bool projection(Node node, int n) const {
   1.321 +      return Parent::projection(node, n);
   1.322 +    }
   1.323 +
   1.324 +    /// \brief The dimension id of an edge.
   1.325 +    ///
   1.326 +    /// Gives back the dimension id of the given edge.
   1.327 +    /// It is in the [0..dim-1] range.
   1.328 +    int dimension(Edge edge) const {
   1.329 +      return Parent::dimension(edge);
   1.330 +    }
   1.331 +
   1.332 +    /// \brief The dimension id of an arc.
   1.333 +    ///
   1.334 +    /// Gives back the dimension id of the given arc.
   1.335 +    /// It is in the [0..dim-1] range.
   1.336 +    int dimension(Arc arc) const {
   1.337 +      return Parent::dimension(arc);
   1.338 +    }
   1.339 +
   1.340 +    /// \brief The index of a node.
   1.341 +    ///
   1.342 +    /// Gives back the index of the given node.
   1.343 +    /// The lower bits of the integer describes the node.
   1.344 +    int index(Node node) const {
   1.345 +      return Parent::index(node);
   1.346 +    }
   1.347 +
   1.348 +    /// \brief Gives back a node by its index.
   1.349 +    ///
   1.350 +    /// Gives back a node by its index.
   1.351 +    Node operator()(int ix) const {
   1.352 +      return Parent::operator()(ix);
   1.353 +    }
   1.354 +
   1.355 +    /// \brief Number of nodes.
   1.356 +    int nodeNum() const { return Parent::nodeNum(); }
   1.357 +    /// \brief Number of edges.
   1.358 +    int edgeNum() const { return Parent::edgeNum(); }
   1.359 +    /// \brief Number of arcs.
   1.360 +    int arcNum() const { return Parent::arcNum(); }
   1.361 +
   1.362 +    /// \brief Linear combination map.
   1.363 +    ///
   1.364 +    /// This map makes possible to give back a linear combination
   1.365 +    /// for each node. It works like the \c std::accumulate function,
   1.366 +    /// so it accumulates the \c bf binary function with the \c fv first
   1.367 +    /// value. The map accumulates only on that positions (dimensions)
   1.368 +    /// where the index of the node is one. The values that have to be
   1.369 +    /// accumulated should be given by the \c begin and \c end iterators
   1.370 +    /// and the length of this range should be equal to the dimension
   1.371 +    /// number of the graph.
   1.372 +    ///
   1.373 +    ///\code
   1.374 +    /// const int DIM = 3;
   1.375 +    /// HypercubeGraph graph(DIM);
   1.376 +    /// dim2::Point<double> base[DIM];
   1.377 +    /// for (int k = 0; k < DIM; ++k) {
   1.378 +    ///   base[k].x = rnd();
   1.379 +    ///   base[k].y = rnd();
   1.380 +    /// }
   1.381 +    /// HypercubeGraph::HyperMap<dim2::Point<double> >
   1.382 +    ///   pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
   1.383 +    ///\endcode
   1.384 +    ///
   1.385 +    /// \see HypercubeGraph
   1.386 +    template <typename T, typename BF = std::plus<T> >
   1.387 +    class HyperMap {
   1.388 +    public:
   1.389 +
   1.390 +      /// \brief The key type of the map
   1.391 +      typedef Node Key;
   1.392 +      /// \brief The value type of the map
   1.393 +      typedef T Value;
   1.394 +
   1.395 +      /// \brief Constructor for HyperMap.
   1.396 +      ///
   1.397 +      /// Construct a HyperMap for the given graph. The values that have
   1.398 +      /// to be accumulated should be given by the \c begin and \c end
   1.399 +      /// iterators and the length of this range should be equal to the
   1.400 +      /// dimension number of the graph.
   1.401 +      ///
   1.402 +      /// This map accumulates the \c bf binary function with the \c fv
   1.403 +      /// first value on that positions (dimensions) where the index of
   1.404 +      /// the node is one.
   1.405 +      template <typename It>
   1.406 +      HyperMap(const Graph& graph, It begin, It end,
   1.407 +               T fv = 0, const BF& bf = BF())
   1.408 +        : _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf)
   1.409 +      {
   1.410 +        LEMON_ASSERT(_values.size() == graph.dimension(),
   1.411 +                     "Wrong size of range");
   1.412 +      }
   1.413 +
   1.414 +      /// \brief The partial accumulated value.
   1.415 +      ///
   1.416 +      /// Gives back the partial accumulated value.
   1.417 +      Value operator[](const Key& k) const {
   1.418 +        Value val = _first_value;
   1.419 +        int id = _graph.index(k);
   1.420 +        int n = 0;
   1.421 +        while (id != 0) {
   1.422 +          if (id & 1) {
   1.423 +            val = _bin_func(val, _values[n]);
   1.424 +          }
   1.425 +          id >>= 1;
   1.426 +          ++n;
   1.427 +        }
   1.428 +        return val;
   1.429 +      }
   1.430 +
   1.431 +    private:
   1.432 +      const Graph& _graph;
   1.433 +      std::vector<T> _values;
   1.434 +      T _first_value;
   1.435 +      BF _bin_func;
   1.436 +    };
   1.437 +
   1.438 +  };
   1.439 +
   1.440 +}
   1.441 +
   1.442 +#endif