1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2008
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    22 #include <lemon/core.h>
 
    23 #include <lemon/bits/graph_extender.h>
 
    24 #include <lemon/dim2.h>
 
    25 #include <lemon/assert.h>
 
    29 ///\brief GridGraph class.
 
    37     typedef GridGraphBase Graph;
 
    49     void construct(int width, int height) {
 
    50        _width = width; _height = height;
 
    51       _node_num = width * height;
 
    52       _edge_num = 2 * _node_num - width - height;
 
    53       _edge_limit = _node_num - _width;
 
    58     Node operator()(int i, int j) const {
 
    59       LEMON_DEBUG(0 <= i && i < _width &&
 
    60                   0 <= j  && j < _height, "Index out of range");
 
    61       return Node(i + j * _width);
 
    64     int col(Node n) const {
 
    65       return n._id % _width;
 
    68     int row(Node n) const {
 
    69       return n._id / _width;
 
    72     dim2::Point<int> pos(Node n) const {
 
    73       return dim2::Point<int>(col(n), row(n));
 
    84     typedef True NodeNumTag;
 
    85     typedef True EdgeNumTag;
 
    86     typedef True ArcNumTag;
 
    88     int nodeNum() const { return _node_num; }
 
    89     int edgeNum() const { return _edge_num; }
 
    90     int arcNum() const { return 2 * _edge_num; }
 
    92     Node u(Edge edge) const {
 
    93       if (edge._id < _edge_limit) {
 
    96         return (edge._id - _edge_limit) % (_width - 1) +
 
    97           (edge._id - _edge_limit) / (_width - 1) * _width;
 
   101     Node v(Edge edge) const {
 
   102       if (edge._id < _edge_limit) {
 
   103         return edge._id + _width;
 
   105         return (edge._id - _edge_limit) % (_width - 1) +
 
   106           (edge._id - _edge_limit) / (_width - 1) * _width + 1;
 
   110     Node source(Arc arc) const {
 
   111       return (arc._id & 1) == 1 ? u(arc) : v(arc);
 
   114     Node target(Arc arc) const {
 
   115       return (arc._id & 1) == 1 ? v(arc) : u(arc);
 
   118     static int id(Node node) { return node._id; }
 
   119     static int id(Edge edge) { return edge._id; }
 
   120     static int id(Arc arc) { return arc._id; }
 
   122     int maxNodeId() const { return _node_num - 1; }
 
   123     int maxEdgeId() const { return _edge_num - 1; }
 
   124     int maxArcId() const { return 2 * _edge_num - 1; }
 
   126     static Node nodeFromId(int id) { return Node(id);}
 
   127     static Edge edgeFromId(int id) { return Edge(id);}
 
   128     static Arc arcFromId(int id) { return Arc(id);}
 
   130     typedef True FindEdgeTag;
 
   131     typedef True FindArcTag;
 
   133     Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
 
   134       if (prev != INVALID) return INVALID;
 
   136         if (v._id - u._id == _width)
 
   138         if (v._id - u._id == 1 && u._id % _width < _width - 1) {
 
   139           return Edge(u._id / _width * (_width - 1) +
 
   140                       u._id % _width + _edge_limit);
 
   143         if (u._id - v._id == _width)
 
   145         if (u._id - v._id == 1 && v._id % _width < _width - 1) {
 
   146           return Edge(v._id / _width * (_width - 1) +
 
   147                       v._id % _width + _edge_limit);
 
   153     Arc findArc(Node u, Node v, Arc prev = INVALID) const {
 
   154       if (prev != INVALID) return INVALID;
 
   156         if (v._id - u._id == _width)
 
   157           return Arc((u._id << 1) | 1);
 
   158         if (v._id - u._id == 1 && u._id % _width < _width - 1) {
 
   159           return Arc(((u._id / _width * (_width - 1) +
 
   160                        u._id % _width + _edge_limit) << 1) | 1);
 
   163         if (u._id - v._id == _width)
 
   164           return Arc(v._id << 1);
 
   165         if (u._id - v._id == 1 && v._id % _width < _width - 1) {
 
   166           return Arc((v._id / _width * (_width - 1) +
 
   167                        v._id % _width + _edge_limit) << 1);
 
   174       friend class GridGraphBase;
 
   178       Node(int id) : _id(id) {}
 
   181       Node (Invalid) : _id(-1) {}
 
   182       bool operator==(const Node node) const {return _id == node._id;}
 
   183       bool operator!=(const Node node) const {return _id != node._id;}
 
   184       bool operator<(const Node node) const {return _id < node._id;}
 
   188       friend class GridGraphBase;
 
   194       Edge(int id) : _id(id) {}
 
   198       Edge (Invalid) : _id(-1) {}
 
   199       bool operator==(const Edge edge) const {return _id == edge._id;}
 
   200       bool operator!=(const Edge edge) const {return _id != edge._id;}
 
   201       bool operator<(const Edge edge) const {return _id < edge._id;}
 
   205       friend class GridGraphBase;
 
   210       Arc(int id) : _id(id) {}
 
   214       Arc (Invalid) : _id(-1) {}
 
   215       operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
 
   216       bool operator==(const Arc arc) const {return _id == arc._id;}
 
   217       bool operator!=(const Arc arc) const {return _id != arc._id;}
 
   218       bool operator<(const Arc arc) const {return _id < arc._id;}
 
   221     static bool direction(Arc arc) {
 
   222       return (arc._id & 1) == 1;
 
   225     static Arc direct(Edge edge, bool dir) {
 
   226       return Arc((edge._id << 1) | (dir ? 1 : 0));
 
   229     void first(Node& node) const {
 
   230       node._id = _node_num - 1;
 
   233     static void next(Node& node) {
 
   237     void first(Edge& edge) const {
 
   238       edge._id = _edge_num - 1;
 
   241     static void next(Edge& edge) {
 
   245     void first(Arc& arc) const {
 
   246       arc._id = 2 * _edge_num - 1;
 
   249     static void next(Arc& arc) {
 
   253     void firstOut(Arc& arc, const Node& node) const {
 
   254       if (node._id % _width < _width - 1) {
 
   255         arc._id = (_edge_limit + node._id % _width +
 
   256                    (node._id / _width) * (_width - 1)) << 1 | 1;
 
   259       if (node._id < _node_num - _width) {
 
   260         arc._id = node._id << 1 | 1;
 
   263       if (node._id % _width > 0) {
 
   264         arc._id = (_edge_limit + node._id % _width +
 
   265                    (node._id / _width) * (_width - 1) - 1) << 1;
 
   268       if (node._id >= _width) {
 
   269         arc._id = (node._id - _width) << 1;
 
   275     void nextOut(Arc& arc) const {
 
   276       int nid = arc._id >> 1;
 
   277       if ((arc._id & 1) == 1) {
 
   278         if (nid >= _edge_limit) {
 
   279           nid = (nid - _edge_limit) % (_width - 1) +
 
   280             (nid - _edge_limit) / (_width - 1) * _width;
 
   281           if (nid < _node_num - _width) {
 
   282             arc._id = nid << 1 | 1;
 
   286         if (nid % _width > 0) {
 
   287           arc._id = (_edge_limit + nid % _width +
 
   288                      (nid / _width) * (_width - 1) - 1) << 1;
 
   292           arc._id = (nid - _width) << 1;
 
   296         if (nid >= _edge_limit) {
 
   297           nid = (nid - _edge_limit) % (_width - 1) +
 
   298             (nid - _edge_limit) / (_width - 1) * _width + 1;
 
   300             arc._id = (nid - _width) << 1;
 
   308     void firstIn(Arc& arc, const Node& node) const {
 
   309       if (node._id % _width < _width - 1) {
 
   310         arc._id = (_edge_limit + node._id % _width +
 
   311                    (node._id / _width) * (_width - 1)) << 1;
 
   314       if (node._id < _node_num - _width) {
 
   315         arc._id = node._id << 1;
 
   318       if (node._id % _width > 0) {
 
   319         arc._id = (_edge_limit + node._id % _width +
 
   320                    (node._id / _width) * (_width - 1) - 1) << 1 | 1;
 
   323       if (node._id >= _width) {
 
   324         arc._id = (node._id - _width) << 1 | 1;
 
   330     void nextIn(Arc& arc) const {
 
   331       int nid = arc._id >> 1;
 
   332       if ((arc._id & 1) == 0) {
 
   333         if (nid >= _edge_limit) {
 
   334           nid = (nid - _edge_limit) % (_width - 1) +
 
   335             (nid - _edge_limit) / (_width - 1) * _width;
 
   336           if (nid < _node_num - _width) {
 
   341         if (nid % _width > 0) {
 
   342           arc._id = (_edge_limit + nid % _width +
 
   343                      (nid / _width) * (_width - 1) - 1) << 1 | 1;
 
   347           arc._id = (nid - _width) << 1 | 1;
 
   351         if (nid >= _edge_limit) {
 
   352           nid = (nid - _edge_limit) % (_width - 1) +
 
   353             (nid - _edge_limit) / (_width - 1) * _width + 1;
 
   355             arc._id = (nid - _width) << 1 | 1;
 
   363     void firstInc(Edge& edge, bool& dir, const Node& node) const {
 
   364       if (node._id % _width < _width - 1) {
 
   365         edge._id = _edge_limit + node._id % _width +
 
   366           (node._id / _width) * (_width - 1);
 
   370       if (node._id < _node_num - _width) {
 
   375       if (node._id % _width > 0) {
 
   376         edge._id = _edge_limit + node._id % _width +
 
   377           (node._id / _width) * (_width - 1) - 1;
 
   381       if (node._id >= _width) {
 
   382         edge._id = node._id - _width;
 
   390     void nextInc(Edge& edge, bool& dir) const {
 
   393         if (nid >= _edge_limit) {
 
   394           nid = (nid - _edge_limit) % (_width - 1) +
 
   395             (nid - _edge_limit) / (_width - 1) * _width;
 
   396           if (nid < _node_num - _width) {
 
   401         if (nid % _width > 0) {
 
   402           edge._id = _edge_limit + nid % _width +
 
   403             (nid / _width) * (_width - 1) - 1;
 
   408           edge._id = nid - _width;
 
   413         if (nid >= _edge_limit) {
 
   414           nid = (nid - _edge_limit) % (_width - 1) +
 
   415             (nid - _edge_limit) / (_width - 1) * _width + 1;
 
   417             edge._id = nid - _width;
 
   426     Arc right(Node n) const {
 
   427       if (n._id % _width < _width - 1) {
 
   428         return Arc(((_edge_limit + n._id % _width +
 
   429                     (n._id / _width) * (_width - 1)) << 1) | 1);
 
   435     Arc left(Node n) const {
 
   436       if (n._id % _width > 0) {
 
   437         return Arc((_edge_limit + n._id % _width +
 
   438                      (n._id / _width) * (_width - 1) - 1) << 1);
 
   444     Arc up(Node n) const {
 
   445       if (n._id < _edge_limit) {
 
   446         return Arc((n._id << 1) | 1);
 
   452     Arc down(Node n) const {
 
   453       if (n._id >= _width) {
 
   454         return Arc((n._id - _width) << 1);
 
   462     int _node_num, _edge_num;
 
   467   typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase;
 
   471   /// \brief Grid graph class
 
   473   /// This class implements a special graph type. The nodes of the
 
   474   /// graph can be indexed by two integer \c (i,j) value where \c i is
 
   475   /// in the \c [0..width()-1] range and j is in the \c
 
   476   /// [0..height()-1] range.  Two nodes are connected in the graph if
 
   477   /// the indexes differ exactly on one position and exactly one is
 
   478   /// the difference. The nodes of the graph can be indexed by position
 
   479   /// with the \c operator()() function. The positions of the nodes can be
 
   480   /// get with \c pos(), \c col() and \c row() members. The outgoing
 
   481   /// arcs can be retrieved with the \c right(), \c up(), \c left()
 
   482   /// and \c down() functions, where the bottom-left corner is the
 
   485   /// \image html grid_graph.png
 
   486   /// \image latex grid_graph.eps "Grid graph" width=\textwidth
 
   488   /// A short example about the basic usage:
 
   490   /// GridGraph graph(rows, cols);
 
   491   /// GridGraph::NodeMap<int> val(graph);
 
   492   /// for (int i = 0; i < graph.width(); ++i) {
 
   493   ///   for (int j = 0; j < graph.height(); ++j) {
 
   494   ///     val[graph(i, j)] = i + j;
 
   499   /// This graph type is fully conform to the \ref concepts::Graph
 
   500   /// "Graph" concept, and it also has an important extra feature
 
   501   /// that its maps are real \ref concepts::ReferenceMap
 
   502   /// "reference map"s.
 
   503   class GridGraph : public ExtendedGridGraphBase {
 
   506     typedef ExtendedGridGraphBase Parent;
 
   508     /// \brief Map to get the indices of the nodes as dim2::Point<int>.
 
   510     /// Map to get the indices of the nodes as dim2::Point<int>.
 
   513       /// \brief The key type of the map
 
   514       typedef GridGraph::Node Key;
 
   515       /// \brief The value type of the map
 
   516       typedef dim2::Point<int> Value;
 
   518       /// \brief Constructor
 
   521       IndexMap(const GridGraph& graph) : _graph(graph) {}
 
   523       /// \brief The subscript operator
 
   525       /// The subscript operator.
 
   526       Value operator[](Key key) const {
 
   527         return _graph.pos(key);
 
   531       const GridGraph& _graph;
 
   534     /// \brief Map to get the column of the nodes.
 
   536     /// Map to get the column of the nodes.
 
   539       /// \brief The key type of the map
 
   540       typedef GridGraph::Node Key;
 
   541       /// \brief The value type of the map
 
   544       /// \brief Constructor
 
   547       ColMap(const GridGraph& graph) : _graph(graph) {}
 
   549       /// \brief The subscript operator
 
   551       /// The subscript operator.
 
   552       Value operator[](Key key) const {
 
   553         return _graph.col(key);
 
   557       const GridGraph& _graph;
 
   560     /// \brief Map to get the row of the nodes.
 
   562     /// Map to get the row of the nodes.
 
   565       /// \brief The key type of the map
 
   566       typedef GridGraph::Node Key;
 
   567       /// \brief The value type of the map
 
   570       /// \brief Constructor
 
   573       RowMap(const GridGraph& graph) : _graph(graph) {}
 
   575       /// \brief The subscript operator
 
   577       /// The subscript operator.
 
   578       Value operator[](Key key) const {
 
   579         return _graph.row(key);
 
   583       const GridGraph& _graph;
 
   586     /// \brief Constructor
 
   588     /// Construct a grid graph with given size.
 
   589     GridGraph(int width, int height) { construct(width, height); }
 
   591     /// \brief Resize the graph
 
   593     /// Resize the graph. The function will fully destroy and rebuild
 
   594     /// the graph.  This cause that the maps of the graph will
 
   595     /// reallocated automatically and the previous values will be
 
   597     void resize(int width, int height) {
 
   598       Parent::notifier(Arc()).clear();
 
   599       Parent::notifier(Edge()).clear();
 
   600       Parent::notifier(Node()).clear();
 
   601       construct(width, height);
 
   602       Parent::notifier(Node()).build();
 
   603       Parent::notifier(Edge()).build();
 
   604       Parent::notifier(Arc()).build();
 
   607     /// \brief The node on the given position.
 
   609     /// Gives back the node on the given position.
 
   610     Node operator()(int i, int j) const {
 
   611       return Parent::operator()(i, j);
 
   614     /// \brief Gives back the column index of the node.
 
   616     /// Gives back the column index of the node.
 
   617     int col(Node n) const {
 
   618       return Parent::col(n);
 
   621     /// \brief Gives back the row index of the node.
 
   623     /// Gives back the row index of the node.
 
   624     int row(Node n) const {
 
   625       return Parent::row(n);
 
   628     /// \brief Gives back the position of the node.
 
   630     /// Gives back the position of the node, ie. the <tt>(col,row)</tt> pair.
 
   631     dim2::Point<int> pos(Node n) const {
 
   632       return Parent::pos(n);
 
   635     /// \brief Gives back the number of the columns.
 
   637     /// Gives back the number of the columns.
 
   639       return Parent::width();
 
   642     /// \brief Gives back the number of the rows.
 
   644     /// Gives back the number of the rows.
 
   646       return Parent::height();
 
   649     /// \brief Gives back the arc goes right from the node.
 
   651     /// Gives back the arc goes right from the node. If there is not
 
   652     /// outgoing arc then it gives back INVALID.
 
   653     Arc right(Node n) const {
 
   654       return Parent::right(n);
 
   657     /// \brief Gives back the arc goes left from the node.
 
   659     /// Gives back the arc goes left from the node. If there is not
 
   660     /// outgoing arc then it gives back INVALID.
 
   661     Arc left(Node n) const {
 
   662       return Parent::left(n);
 
   665     /// \brief Gives back the arc goes up from the node.
 
   667     /// Gives back the arc goes up from the node. If there is not
 
   668     /// outgoing arc then it gives back INVALID.
 
   669     Arc up(Node n) const {
 
   670       return Parent::up(n);
 
   673     /// \brief Gives back the arc goes down from the node.
 
   675     /// Gives back the arc goes down from the node. If there is not
 
   676     /// outgoing arc then it gives back INVALID.
 
   677     Arc down(Node n) const {
 
   678       return Parent::down(n);
 
   681     /// \brief Index map of the grid graph
 
   683     /// Just returns an IndexMap for the grid graph.
 
   684     IndexMap indexMap() const {
 
   685       return IndexMap(*this);
 
   688     /// \brief Row map of the grid graph
 
   690     /// Just returns a RowMap for the grid graph.
 
   691     RowMap rowMap() const {
 
   692       return RowMap(*this);
 
   695     /// \brief Column map of the grid graph
 
   697     /// Just returns a ColMap for the grid graph.
 
   698     ColMap colMap() const {
 
   699       return ColMap(*this);