kpeter@334: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@334: * kpeter@334: * This file is a part of LEMON, a generic C++ optimization library. kpeter@334: * kpeter@334: * Copyright (C) 2003-2008 kpeter@334: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@334: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@334: * kpeter@334: * Permission to use, modify and distribute this software is granted kpeter@334: * provided that this copyright notice appears in all copies. For kpeter@334: * precise terms see the accompanying LICENSE file. kpeter@334: * kpeter@334: * This software is provided "AS IS" with no warranty of any kind, kpeter@334: * express or implied, and with no claim as to its suitability for any kpeter@334: * purpose. kpeter@334: * kpeter@334: */ kpeter@334: kpeter@334: #ifndef GRID_GRAPH_H kpeter@334: #define GRID_GRAPH_H kpeter@334: kpeter@334: #include kpeter@334: #include kpeter@334: #include kpeter@334: kpeter@334: #include kpeter@334: #include kpeter@334: kpeter@334: #include kpeter@334: kpeter@334: ///\ingroup graphs kpeter@334: ///\file kpeter@334: ///\brief GridGraph class. kpeter@334: kpeter@334: namespace lemon { kpeter@334: kpeter@334: class GridGraphBase { kpeter@334: kpeter@334: public: kpeter@334: kpeter@334: typedef GridGraphBase Graph; kpeter@334: kpeter@334: class Node; kpeter@334: class Arc; kpeter@334: kpeter@334: public: kpeter@334: kpeter@334: GridGraphBase() {} kpeter@334: kpeter@334: protected: kpeter@334: kpeter@334: void construct(int w, int h) { kpeter@334: _height = h; _width = w; kpeter@334: _nodeNum = h * w; _arcNum = 2 * _nodeNum - w - h; kpeter@334: _arcLimit = _nodeNum - w; kpeter@334: } kpeter@334: kpeter@334: Arc _down(Node n) const { kpeter@334: if (n.id < _nodeNum - _width) { kpeter@334: return Arc(n.id); kpeter@334: } else { kpeter@334: return INVALID; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: Arc _up(Node n) const { kpeter@334: if (n.id >= _width) { kpeter@334: return Arc(n.id - _width); kpeter@334: } else { kpeter@334: return INVALID; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: Arc _right(Node n) const { kpeter@334: if (n.id % _width < _width - 1) { kpeter@334: return _arcLimit + n.id % _width + (n.id / _width) * (_width - 1); kpeter@334: } else { kpeter@334: return INVALID; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: Arc _left(Node n) const { kpeter@334: if (n.id % _width > 0) { kpeter@334: return _arcLimit + n.id % _width + (n.id / _width) * (_width - 1) - 1; kpeter@334: } else { kpeter@334: return INVALID; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: public: kpeter@334: kpeter@334: Node operator()(int i, int j) const { kpeter@334: LEMON_ASSERT(0 <= i && i < width() && kpeter@334: 0 <= j && j < height(), "lemon::GridGraph::IndexError"); kpeter@334: return Node(i + j * _width); kpeter@334: } kpeter@334: kpeter@334: int row(Node n) const { kpeter@334: return n.id / _width; kpeter@334: } kpeter@334: kpeter@334: int col(Node n) const { kpeter@334: return n.id % _width; kpeter@334: } kpeter@334: kpeter@334: int width() const { kpeter@334: return _width; kpeter@334: } kpeter@334: kpeter@334: int height() const { kpeter@334: return _height; kpeter@334: } kpeter@334: kpeter@334: typedef True NodeNumTag; kpeter@334: typedef True ArcNumTag; kpeter@334: kpeter@334: int nodeNum() const { return _nodeNum; } kpeter@334: int arcNum() const { return _arcNum; } kpeter@334: kpeter@334: int maxNodeId() const { return nodeNum() - 1; } kpeter@334: int maxArcId() const { return arcNum() - 1; } kpeter@334: kpeter@334: Node source(Arc e) const { kpeter@334: if (e.id < _arcLimit) { kpeter@334: return e.id; kpeter@334: } else { kpeter@334: return (e.id - _arcLimit) % (_width - 1) + kpeter@334: (e.id - _arcLimit) / (_width - 1) * _width; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: Node target(Arc e) const { kpeter@334: if (e.id < _arcLimit) { kpeter@334: return e.id + _width; kpeter@334: } else { kpeter@334: return (e.id - _arcLimit) % (_width - 1) + kpeter@334: (e.id - _arcLimit) / (_width - 1) * _width + 1; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: static int id(Node v) { return v.id; } kpeter@334: static int id(Arc e) { return e.id; } kpeter@334: kpeter@334: static Node nodeFromId(int id) { return Node(id);} kpeter@334: kpeter@334: static Arc arcFromId(int id) { return Arc(id);} kpeter@334: kpeter@334: typedef True FindArcTag; kpeter@334: kpeter@334: Arc findArc(Node u, Node v, Arc prev = INVALID) const { kpeter@334: if (prev != INVALID) return INVALID; kpeter@334: if (v.id - u.id == _width) return Arc(u.id); kpeter@334: if (v.id - u.id == 1 && u.id % _width < _width - 1) { kpeter@334: return Arc(u.id / _width * (_width - 1) + kpeter@334: u.id % _width + _arcLimit); kpeter@334: } kpeter@334: return INVALID; kpeter@334: } kpeter@334: kpeter@334: class Node { kpeter@334: friend class GridGraphBase; kpeter@334: kpeter@334: protected: kpeter@334: int id; kpeter@334: Node(int _id) : id(_id) {} kpeter@334: public: kpeter@334: Node() {} kpeter@334: Node (Invalid) { id = -1; } kpeter@334: bool operator==(const Node node) const { return id == node.id; } kpeter@334: bool operator!=(const Node node) const { return id != node.id; } kpeter@334: bool operator<(const Node node) const { return id < node.id; } kpeter@334: }; kpeter@334: kpeter@334: class Arc { kpeter@334: friend class GridGraphBase; kpeter@334: kpeter@334: protected: kpeter@334: int id; kpeter@334: Arc(int _id) : id(_id) {} kpeter@334: public: kpeter@334: Arc() {} kpeter@334: Arc (Invalid) { id = -1; } kpeter@334: bool operator==(const Arc arc) const { return id == arc.id; } kpeter@334: bool operator!=(const Arc arc) const { return id != arc.id; } kpeter@334: bool operator<(const Arc arc) const { return id < arc.id; } kpeter@334: }; kpeter@334: kpeter@334: void first(Node& node) const { kpeter@334: node.id = nodeNum() - 1; kpeter@334: } kpeter@334: kpeter@334: static void next(Node& node) { kpeter@334: --node.id; kpeter@334: } kpeter@334: kpeter@334: void first(Arc& arc) const { kpeter@334: arc.id = arcNum() - 1; kpeter@334: } kpeter@334: kpeter@334: static void next(Arc& arc) { kpeter@334: --arc.id; kpeter@334: } kpeter@334: kpeter@334: void firstOut(Arc& arc, const Node& node) const { kpeter@334: if (node.id < _nodeNum - _width) { kpeter@334: arc.id = node.id; kpeter@334: } else if (node.id % _width < _width - 1) { kpeter@334: arc.id = _arcLimit + node.id % _width + kpeter@334: (node.id / _width) * (_width - 1); kpeter@334: } else { kpeter@334: arc.id = -1; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: void nextOut(Arc& arc) const { kpeter@334: if (arc.id >= _arcLimit) { kpeter@334: arc.id = -1; kpeter@334: } else if (arc.id % _width < _width - 1) { kpeter@334: arc.id = _arcLimit + arc.id % _width + kpeter@334: (arc.id / _width) * (_width - 1); kpeter@334: } else { kpeter@334: arc.id = -1; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: void firstIn(Arc& arc, const Node& node) const { kpeter@334: if (node.id >= _width) { kpeter@334: arc.id = node.id - _width; kpeter@334: } else if (node.id % _width > 0) { kpeter@334: arc.id = _arcLimit + node.id % _width + kpeter@334: (node.id / _width) * (_width - 1) - 1; kpeter@334: } else { kpeter@334: arc.id = -1; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: void nextIn(Arc& arc) const { kpeter@334: if (arc.id >= _arcLimit) { kpeter@334: arc.id = -1; kpeter@334: } else if (arc.id % _width > 0) { kpeter@334: arc.id = _arcLimit + arc.id % _width + kpeter@334: (arc.id / _width + 1) * (_width - 1) - 1; kpeter@334: } else { kpeter@334: arc.id = -1; kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: private: kpeter@334: int _width, _height; kpeter@334: int _nodeNum, _arcNum; kpeter@334: int _arcLimit; kpeter@334: }; kpeter@334: kpeter@334: typedef GraphExtender > kpeter@334: ExtendedGridGraphBase; kpeter@334: kpeter@334: /// \ingroup graphs kpeter@334: /// kpeter@334: /// \brief Grid graph class kpeter@334: /// kpeter@334: /// This class implements a special graph type. The nodes of the kpeter@334: /// graph can be indiced by two integer \c (i,j) value where \c i kpeter@334: /// is in the \c [0,width) range and j is in the [0, height) range. kpeter@334: /// Two nodes are connected in the graph if the indices differ only kpeter@334: /// on one position and only one is the difference. kpeter@334: /// kpeter@334: /// \image html grid_graph.png kpeter@334: /// \image latex grid_graph.eps "Grid graph" width=\textwidth kpeter@334: /// kpeter@334: /// The graph can be indiced in the following way: kpeter@334: ///\code kpeter@334: /// GridGraph gr(w, h); kpeter@334: /// GridGraph::NodeMap val(gr); kpeter@334: /// for (int i = 0; i < gr.width(); ++i) { kpeter@334: /// for (int j = 0; j < gr.height(); ++j) { kpeter@334: /// val[gr(i, j)] = i + j; kpeter@334: /// } kpeter@334: /// } kpeter@334: ///\endcode kpeter@334: /// kpeter@334: /// This graph type is fully conform to the \ref concepts::Graph kpeter@334: /// "Undirected Graph" concept, and it also has an important extra kpeter@334: /// feature that its maps are real \ref concepts::ReferenceMap kpeter@334: /// "reference map"s. kpeter@334: class GridGraph : public ExtendedGridGraphBase { kpeter@334: public: kpeter@334: kpeter@334: typedef ExtendedGridGraphBase Parent; kpeter@334: kpeter@334: /// \brief Map to get the indices of the nodes as dim2::Point. kpeter@334: /// kpeter@334: /// Map to get the indices of the nodes as dim2::Point. kpeter@334: class IndexMap { kpeter@334: public: kpeter@334: /// The key type of the map kpeter@334: typedef GridGraph::Node Key; kpeter@334: /// The value type of the map kpeter@334: typedef dim2::Point Value; kpeter@334: kpeter@334: /// Constructor kpeter@334: IndexMap(const GridGraph& graph) : _graph(graph) {} kpeter@334: kpeter@334: /// The subscript operator kpeter@334: Value operator[](const Key& key) const { kpeter@334: return dim2::Point(_graph.row(key), _graph.col(key)); kpeter@334: } kpeter@334: kpeter@334: private: kpeter@334: const GridGraph& _graph; kpeter@334: }; kpeter@334: kpeter@334: /// \brief Map to get the row of the nodes. kpeter@334: /// kpeter@334: /// Map to get the row of the nodes. kpeter@334: class RowMap { kpeter@334: public: kpeter@334: /// The key type of the map kpeter@334: typedef GridGraph::Node Key; kpeter@334: /// The value type of the map kpeter@334: typedef int Value; kpeter@334: kpeter@334: /// Constructor kpeter@334: RowMap(const GridGraph& graph) : _graph(graph) {} kpeter@334: kpeter@334: /// The subscript operator kpeter@334: Value operator[](const Key& key) const { kpeter@334: return _graph.row(key); kpeter@334: } kpeter@334: kpeter@334: private: kpeter@334: const GridGraph& _graph; kpeter@334: }; kpeter@334: kpeter@334: /// \brief Map to get the column of the nodes. kpeter@334: /// kpeter@334: /// Map to get the column of the nodes. kpeter@334: class ColMap { kpeter@334: public: kpeter@334: /// The key type of the map kpeter@334: typedef GridGraph::Node Key; kpeter@334: /// The value type of the map kpeter@334: typedef int Value; kpeter@334: kpeter@334: /// Constructor kpeter@334: ColMap(const GridGraph& graph) : _graph(graph) {} kpeter@334: kpeter@334: /// The subscript operator kpeter@334: Value operator[](const Key& key) const { kpeter@334: return _graph.col(key); kpeter@334: } kpeter@334: kpeter@334: private: kpeter@334: const GridGraph& _graph; kpeter@334: }; kpeter@334: kpeter@334: /// \brief Constructor kpeter@334: /// kpeter@334: /// Constructor. kpeter@334: /// \param width The width of the grid. kpeter@334: /// \param height The height of the grid. kpeter@334: GridGraph(int width, int height) { construct(width, height); } kpeter@334: kpeter@334: /// \brief Resize the graph kpeter@334: /// kpeter@334: /// Resize the grid. kpeter@334: void resize(int width, int height) { kpeter@334: Parent::notifier(Arc()).clear(); kpeter@334: Parent::notifier(Edge()).clear(); kpeter@334: Parent::notifier(Node()).clear(); kpeter@334: construct(width, height); kpeter@334: Parent::notifier(Node()).build(); kpeter@334: Parent::notifier(Edge()).build(); kpeter@334: Parent::notifier(Arc()).build(); kpeter@334: } kpeter@334: kpeter@334: /// \brief The node on the given position. kpeter@334: /// kpeter@334: /// Gives back the node on the given position. kpeter@334: Node operator()(int i, int j) const { kpeter@334: return Parent::operator()(i, j); kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the row index of the node. kpeter@334: /// kpeter@334: /// Gives back the row index of the node. kpeter@334: int row(Node n) const { kpeter@334: return Parent::row(n); kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the column index of the node. kpeter@334: /// kpeter@334: /// Gives back the column index of the node. kpeter@334: int col(Node n) const { kpeter@334: return Parent::col(n); kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the width of the grid. kpeter@334: /// kpeter@334: /// Gives back the width of the grid. kpeter@334: int width() const { kpeter@334: return Parent::width(); kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the height of the grid. kpeter@334: /// kpeter@334: /// Gives back the height of the grid. kpeter@334: int height() const { kpeter@334: return Parent::height(); kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the arc goes down from the node. kpeter@334: /// kpeter@334: /// Gives back the arc goes down from the node. If there is not kpeter@334: /// outgoing arc then it gives back \c INVALID. kpeter@334: Arc down(Node n) const { kpeter@334: Edge e = _down(n); kpeter@334: return e != INVALID ? direct(e, true) : INVALID; kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the arc goes up from the node. kpeter@334: /// kpeter@334: /// Gives back the arc goes up from the node. If there is not kpeter@334: /// outgoing arc then it gives back \c INVALID. kpeter@334: Arc up(Node n) const { kpeter@334: Edge e = _up(n); kpeter@334: return e != INVALID ? direct(e, false) : INVALID; kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the arc goes right from the node. kpeter@334: /// kpeter@334: /// Gives back the arc goes right from the node. If there is not kpeter@334: /// outgoing arc then it gives back \c INVALID. kpeter@334: Arc right(Node n) const { kpeter@334: Edge e = _right(n); kpeter@334: return e != INVALID ? direct(e, true) : INVALID; kpeter@334: } kpeter@334: kpeter@334: /// \brief Gives back the arc goes left from the node. kpeter@334: /// kpeter@334: /// Gives back the arc goes left from the node. If there is not kpeter@334: /// outgoing arc then it gives back \c INVALID. kpeter@334: Arc left(Node n) const { kpeter@334: Edge e = _left(n); kpeter@334: return e != INVALID ? direct(e, false) : INVALID; kpeter@334: } kpeter@334: kpeter@334: }; // class GridGraph kpeter@334: kpeter@334: /// \brief Index map of the grid graph kpeter@334: /// kpeter@334: /// Just returns an IndexMap for the grid graph. kpeter@334: inline GridGraph::IndexMap indexMap(const GridGraph& graph) { kpeter@334: return GridGraph::IndexMap(graph); kpeter@334: } kpeter@334: kpeter@334: /// \brief Row map of the grid graph kpeter@334: /// kpeter@334: /// Just returns a RowMap for the grid graph. kpeter@334: inline GridGraph::RowMap rowMap(const GridGraph& graph) { kpeter@334: return GridGraph::RowMap(graph); kpeter@334: } kpeter@334: kpeter@334: /// \brief Column map of the grid graph kpeter@334: /// kpeter@334: /// Just returns a ColMap for the grid graph. kpeter@334: inline GridGraph::ColMap colMap(const GridGraph& graph) { kpeter@334: return GridGraph::ColMap(graph); kpeter@334: } kpeter@334: } kpeter@334: kpeter@334: #endif // GRID_GRAPH_H