kpeter@364: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@364: * kpeter@364: * This file is a part of LEMON, a generic C++ optimization library. kpeter@364: * alpar@440: * Copyright (C) 2003-2009 kpeter@364: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@364: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@364: * kpeter@364: * Permission to use, modify and distribute this software is granted kpeter@364: * provided that this copyright notice appears in all copies. For kpeter@364: * precise terms see the accompanying LICENSE file. kpeter@364: * kpeter@364: * This software is provided "AS IS" with no warranty of any kind, kpeter@364: * express or implied, and with no claim as to its suitability for any kpeter@364: * purpose. kpeter@364: * kpeter@364: */ kpeter@364: kpeter@364: #ifndef HYPERCUBE_GRAPH_H kpeter@364: #define HYPERCUBE_GRAPH_H kpeter@364: kpeter@364: #include kpeter@364: #include kpeter@365: #include kpeter@364: #include kpeter@364: kpeter@364: ///\ingroup graphs kpeter@364: ///\file kpeter@365: ///\brief HypercubeGraph class. kpeter@364: kpeter@364: namespace lemon { kpeter@364: kpeter@365: class HypercubeGraphBase { kpeter@364: kpeter@364: public: kpeter@364: kpeter@365: typedef HypercubeGraphBase Graph; kpeter@364: kpeter@364: class Node; kpeter@365: class Edge; kpeter@364: class Arc; kpeter@364: kpeter@364: public: kpeter@364: kpeter@365: HypercubeGraphBase() {} kpeter@364: kpeter@364: protected: kpeter@364: kpeter@364: void construct(int dim) { kpeter@365: LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1."); kpeter@364: _dim = dim; kpeter@365: _node_num = 1 << dim; alpar@372: _edge_num = dim * (1 << (dim-1)); kpeter@364: } kpeter@364: kpeter@364: public: kpeter@364: kpeter@364: typedef True NodeNumTag; kpeter@365: typedef True EdgeNumTag; kpeter@364: typedef True ArcNumTag; kpeter@364: kpeter@365: int nodeNum() const { return _node_num; } kpeter@365: int edgeNum() const { return _edge_num; } kpeter@365: int arcNum() const { return 2 * _edge_num; } kpeter@364: kpeter@365: int maxNodeId() const { return _node_num - 1; } kpeter@365: int maxEdgeId() const { return _edge_num - 1; } kpeter@365: int maxArcId() const { return 2 * _edge_num - 1; } kpeter@364: kpeter@365: static Node nodeFromId(int id) { return Node(id); } kpeter@365: static Edge edgeFromId(int id) { return Edge(id); } kpeter@365: static Arc arcFromId(int id) { return Arc(id); } kpeter@365: kpeter@365: static int id(Node node) { return node._id; } kpeter@365: static int id(Edge edge) { return edge._id; } kpeter@365: static int id(Arc arc) { return arc._id; } kpeter@365: kpeter@365: Node u(Edge edge) const { alpar@372: int base = edge._id & ((1 << (_dim-1)) - 1); alpar@372: int k = edge._id >> (_dim-1); alpar@372: return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)); kpeter@364: } kpeter@364: kpeter@365: Node v(Edge edge) const { alpar@372: int base = edge._id & ((1 << (_dim-1)) - 1); alpar@372: int k = edge._id >> (_dim-1); alpar@372: return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)) | (1 << k); kpeter@364: } kpeter@364: kpeter@365: Node source(Arc arc) const { kpeter@365: return (arc._id & 1) == 1 ? u(arc) : v(arc); kpeter@365: } kpeter@364: kpeter@365: Node target(Arc arc) const { kpeter@365: return (arc._id & 1) == 1 ? v(arc) : u(arc); kpeter@365: } kpeter@364: kpeter@365: typedef True FindEdgeTag; kpeter@365: typedef True FindArcTag; kpeter@365: kpeter@365: Edge findEdge(Node u, Node v, Edge prev = INVALID) const { kpeter@365: if (prev != INVALID) return INVALID; kpeter@365: int d = u._id ^ v._id; kpeter@365: int k = 0; kpeter@365: if (d == 0) return INVALID; kpeter@365: for ( ; (d & 1) == 0; d >>= 1) ++k; kpeter@365: if (d >> 1 != 0) return INVALID; alpar@372: return (k << (_dim-1)) | ((u._id >> (k+1)) << k) | alpar@372: (u._id & ((1 << k) - 1)); kpeter@365: } kpeter@365: kpeter@365: Arc findArc(Node u, Node v, Arc prev = INVALID) const { kpeter@365: Edge edge = findEdge(u, v, prev); kpeter@365: if (edge == INVALID) return INVALID; alpar@372: int k = edge._id >> (_dim-1); kpeter@365: return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1; kpeter@365: } kpeter@364: kpeter@364: class Node { kpeter@365: friend class HypercubeGraphBase; kpeter@365: kpeter@364: protected: kpeter@365: int _id; kpeter@365: Node(int id) : _id(id) {} kpeter@364: public: kpeter@364: Node() {} kpeter@365: Node (Invalid) : _id(-1) {} kpeter@365: bool operator==(const Node node) const {return _id == node._id;} kpeter@365: bool operator!=(const Node node) const {return _id != node._id;} kpeter@365: bool operator<(const Node node) const {return _id < node._id;} kpeter@365: }; kpeter@365: kpeter@365: class Edge { kpeter@365: friend class HypercubeGraphBase; kpeter@365: friend class Arc; kpeter@365: kpeter@365: protected: kpeter@365: int _id; kpeter@365: kpeter@365: Edge(int id) : _id(id) {} kpeter@365: kpeter@365: public: kpeter@365: Edge() {} kpeter@365: Edge (Invalid) : _id(-1) {} kpeter@365: bool operator==(const Edge edge) const {return _id == edge._id;} kpeter@365: bool operator!=(const Edge edge) const {return _id != edge._id;} kpeter@365: bool operator<(const Edge edge) const {return _id < edge._id;} kpeter@364: }; kpeter@364: kpeter@364: class Arc { kpeter@365: friend class HypercubeGraphBase; kpeter@365: kpeter@364: protected: kpeter@365: int _id; kpeter@365: kpeter@365: Arc(int id) : _id(id) {} kpeter@365: kpeter@364: public: kpeter@365: Arc() {} kpeter@365: Arc (Invalid) : _id(-1) {} kpeter@365: operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; } kpeter@365: bool operator==(const Arc arc) const {return _id == arc._id;} kpeter@365: bool operator!=(const Arc arc) const {return _id != arc._id;} kpeter@365: bool operator<(const Arc arc) const {return _id < arc._id;} kpeter@364: }; kpeter@364: kpeter@364: void first(Node& node) const { kpeter@365: node._id = _node_num - 1; kpeter@364: } kpeter@364: kpeter@364: static void next(Node& node) { kpeter@365: --node._id; kpeter@365: } kpeter@365: kpeter@365: void first(Edge& edge) const { kpeter@365: edge._id = _edge_num - 1; kpeter@365: } kpeter@365: kpeter@365: static void next(Edge& edge) { kpeter@365: --edge._id; kpeter@364: } kpeter@364: kpeter@364: void first(Arc& arc) const { kpeter@365: arc._id = 2 * _edge_num - 1; kpeter@364: } kpeter@364: kpeter@364: static void next(Arc& arc) { kpeter@365: --arc._id; kpeter@365: } kpeter@365: kpeter@365: void firstInc(Edge& edge, bool& dir, const Node& node) const { kpeter@365: edge._id = node._id >> 1; kpeter@365: dir = (node._id & 1) == 0; kpeter@365: } kpeter@365: kpeter@365: void nextInc(Edge& edge, bool& dir) const { kpeter@365: Node n = dir ? u(edge) : v(edge); alpar@372: int k = (edge._id >> (_dim-1)) + 1; kpeter@365: if (k < _dim) { alpar@372: edge._id = (k << (_dim-1)) | alpar@372: ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); kpeter@365: dir = ((n._id >> k) & 1) == 0; kpeter@365: } else { kpeter@365: edge._id = -1; kpeter@365: dir = true; kpeter@365: } kpeter@364: } kpeter@364: kpeter@364: void firstOut(Arc& arc, const Node& node) const { kpeter@365: arc._id = ((node._id >> 1) << 1) | (~node._id & 1); kpeter@364: } kpeter@364: kpeter@364: void nextOut(Arc& arc) const { kpeter@365: Node n = (arc._id & 1) == 1 ? u(arc) : v(arc); kpeter@365: int k = (arc._id >> _dim) + 1; kpeter@365: if (k < _dim) { alpar@372: arc._id = (k << (_dim-1)) | alpar@372: ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); kpeter@365: arc._id = (arc._id << 1) | (~(n._id >> k) & 1); kpeter@365: } else { kpeter@365: arc._id = -1; kpeter@365: } kpeter@364: } kpeter@364: kpeter@364: void firstIn(Arc& arc, const Node& node) const { kpeter@365: arc._id = ((node._id >> 1) << 1) | (node._id & 1); kpeter@364: } kpeter@364: kpeter@364: void nextIn(Arc& arc) const { kpeter@365: Node n = (arc._id & 1) == 1 ? v(arc) : u(arc); kpeter@365: int k = (arc._id >> _dim) + 1; kpeter@365: if (k < _dim) { alpar@372: arc._id = (k << (_dim-1)) | alpar@372: ((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); kpeter@365: arc._id = (arc._id << 1) | ((n._id >> k) & 1); kpeter@364: } else { kpeter@365: arc._id = -1; kpeter@364: } kpeter@364: } kpeter@364: kpeter@365: static bool direction(Arc arc) { kpeter@365: return (arc._id & 1) == 1; kpeter@365: } kpeter@365: kpeter@365: static Arc direct(Edge edge, bool dir) { kpeter@365: return Arc((edge._id << 1) | (dir ? 1 : 0)); kpeter@365: } kpeter@365: kpeter@364: int dimension() const { kpeter@364: return _dim; kpeter@364: } kpeter@364: kpeter@364: bool projection(Node node, int n) const { kpeter@365: return static_cast(node._id & (1 << n)); kpeter@365: } kpeter@365: kpeter@365: int dimension(Edge edge) const { alpar@372: return edge._id >> (_dim-1); kpeter@364: } kpeter@364: kpeter@364: int dimension(Arc arc) const { kpeter@365: return arc._id >> _dim; kpeter@364: } kpeter@364: kpeter@364: int index(Node node) const { kpeter@365: return node._id; kpeter@364: } kpeter@364: kpeter@364: Node operator()(int ix) const { kpeter@364: return Node(ix); kpeter@364: } kpeter@364: kpeter@364: private: kpeter@365: int _dim; kpeter@365: int _node_num, _edge_num; kpeter@364: }; kpeter@364: kpeter@364: kpeter@365: typedef GraphExtender ExtendedHypercubeGraphBase; kpeter@364: kpeter@365: /// \ingroup graphs kpeter@364: /// kpeter@365: /// \brief Hypercube graph class kpeter@364: /// kpeter@735: /// HypercubeGraph implements a special graph type. The nodes of the kpeter@735: /// graph are indexed with integers having at most \c dim binary digits. kpeter@365: /// Two nodes are connected in the graph if and only if their indices kpeter@365: /// differ only on one position in the binary form. kpeter@735: /// This class is completely static and it needs constant memory space. kpeter@786: /// Thus you can neither add nor delete nodes or edges, however, kpeter@737: /// the structure can be resized using resize(). kpeter@735: /// kpeter@735: /// This type fully conforms to the \ref concepts::Graph "Graph concept". kpeter@735: /// Most of its member functions and nested classes are documented kpeter@735: /// only in the concept class. kpeter@364: /// kpeter@365: /// \note The type of the indices is chosen to \c int for efficiency kpeter@365: /// reasons. Thus the maximum dimension of this implementation is 26 kpeter@365: /// (assuming that the size of \c int is 32 bit). kpeter@365: class HypercubeGraph : public ExtendedHypercubeGraphBase { kpeter@617: typedef ExtendedHypercubeGraphBase Parent; kpeter@617: kpeter@364: public: kpeter@364: kpeter@365: /// \brief Constructs a hypercube graph with \c dim dimensions. kpeter@364: /// kpeter@365: /// Constructs a hypercube graph with \c dim dimensions. kpeter@365: HypercubeGraph(int dim) { construct(dim); } kpeter@364: kpeter@737: /// \brief Resizes the graph kpeter@737: /// kpeter@737: /// This function resizes the graph. It fully destroys and kpeter@737: /// rebuilds the structure, therefore the maps of the graph will be kpeter@737: /// reallocated automatically and the previous values will be lost. kpeter@737: void resize(int dim) { kpeter@737: Parent::notifier(Arc()).clear(); kpeter@737: Parent::notifier(Edge()).clear(); kpeter@737: Parent::notifier(Node()).clear(); kpeter@737: construct(dim); kpeter@737: Parent::notifier(Node()).build(); kpeter@737: Parent::notifier(Edge()).build(); kpeter@737: Parent::notifier(Arc()).build(); kpeter@737: } kpeter@737: kpeter@365: /// \brief The number of dimensions. kpeter@364: /// kpeter@365: /// Gives back the number of dimensions. kpeter@364: int dimension() const { kpeter@364: return Parent::dimension(); kpeter@364: } kpeter@364: kpeter@365: /// \brief Returns \c true if the n'th bit of the node is one. kpeter@364: /// kpeter@365: /// Returns \c true if the n'th bit of the node is one. kpeter@364: bool projection(Node node, int n) const { kpeter@364: return Parent::projection(node, n); kpeter@364: } kpeter@364: kpeter@365: /// \brief The dimension id of an edge. kpeter@364: /// kpeter@365: /// Gives back the dimension id of the given edge. kpeter@735: /// It is in the range [0..dim-1]. kpeter@365: int dimension(Edge edge) const { kpeter@365: return Parent::dimension(edge); kpeter@365: } kpeter@365: kpeter@365: /// \brief The dimension id of an arc. kpeter@365: /// kpeter@365: /// Gives back the dimension id of the given arc. kpeter@735: /// It is in the range [0..dim-1]. kpeter@364: int dimension(Arc arc) const { kpeter@364: return Parent::dimension(arc); kpeter@364: } kpeter@364: kpeter@365: /// \brief The index of a node. kpeter@364: /// kpeter@365: /// Gives back the index of the given node. kpeter@365: /// The lower bits of the integer describes the node. kpeter@364: int index(Node node) const { kpeter@364: return Parent::index(node); kpeter@364: } kpeter@364: kpeter@365: /// \brief Gives back a node by its index. kpeter@364: /// kpeter@365: /// Gives back a node by its index. kpeter@364: Node operator()(int ix) const { kpeter@364: return Parent::operator()(ix); kpeter@364: } kpeter@364: kpeter@364: /// \brief Number of nodes. kpeter@364: int nodeNum() const { return Parent::nodeNum(); } kpeter@365: /// \brief Number of edges. kpeter@365: int edgeNum() const { return Parent::edgeNum(); } kpeter@364: /// \brief Number of arcs. kpeter@364: int arcNum() const { return Parent::arcNum(); } kpeter@364: kpeter@364: /// \brief Linear combination map. kpeter@364: /// kpeter@365: /// This map makes possible to give back a linear combination kpeter@365: /// for each node. It works like the \c std::accumulate function, kpeter@365: /// so it accumulates the \c bf binary function with the \c fv first kpeter@365: /// value. The map accumulates only on that positions (dimensions) kpeter@365: /// where the index of the node is one. The values that have to be kpeter@365: /// accumulated should be given by the \c begin and \c end iterators kpeter@365: /// and the length of this range should be equal to the dimension kpeter@365: /// number of the graph. kpeter@364: /// kpeter@364: ///\code kpeter@364: /// const int DIM = 3; kpeter@365: /// HypercubeGraph graph(DIM); kpeter@364: /// dim2::Point base[DIM]; kpeter@364: /// for (int k = 0; k < DIM; ++k) { kpeter@364: /// base[k].x = rnd(); kpeter@364: /// base[k].y = rnd(); kpeter@364: /// } kpeter@365: /// HypercubeGraph::HyperMap > kpeter@365: /// pos(graph, base, base + DIM, dim2::Point(0.0, 0.0)); kpeter@364: ///\endcode kpeter@364: /// kpeter@365: /// \see HypercubeGraph kpeter@364: template > kpeter@364: class HyperMap { kpeter@364: public: kpeter@364: kpeter@365: /// \brief The key type of the map kpeter@364: typedef Node Key; kpeter@365: /// \brief The value type of the map kpeter@364: typedef T Value; kpeter@364: kpeter@364: /// \brief Constructor for HyperMap. kpeter@364: /// kpeter@365: /// Construct a HyperMap for the given graph. The values that have kpeter@365: /// to be accumulated should be given by the \c begin and \c end kpeter@365: /// iterators and the length of this range should be equal to the kpeter@365: /// dimension number of the graph. kpeter@364: /// kpeter@365: /// This map accumulates the \c bf binary function with the \c fv kpeter@365: /// first value on that positions (dimensions) where the index of kpeter@365: /// the node is one. kpeter@364: template kpeter@365: HyperMap(const Graph& graph, It begin, It end, kpeter@365: T fv = 0, const BF& bf = BF()) kpeter@365: : _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf) kpeter@364: { kpeter@365: LEMON_ASSERT(_values.size() == graph.dimension(), kpeter@365: "Wrong size of range"); kpeter@364: } kpeter@364: kpeter@365: /// \brief The partial accumulated value. kpeter@364: /// kpeter@364: /// Gives back the partial accumulated value. kpeter@365: Value operator[](const Key& k) const { kpeter@364: Value val = _first_value; kpeter@364: int id = _graph.index(k); kpeter@364: int n = 0; kpeter@364: while (id != 0) { kpeter@364: if (id & 1) { kpeter@364: val = _bin_func(val, _values[n]); kpeter@364: } kpeter@364: id >>= 1; kpeter@364: ++n; kpeter@364: } kpeter@364: return val; kpeter@364: } kpeter@364: kpeter@364: private: kpeter@365: const Graph& _graph; kpeter@364: std::vector _values; kpeter@364: T _first_value; kpeter@364: BF _bin_func; kpeter@364: }; kpeter@364: kpeter@364: }; kpeter@364: kpeter@364: } kpeter@364: kpeter@364: #endif