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