Matrix graph renamed to grid graph
authordeba
Thu, 11 Aug 2005 13:20:52 +0000
changeset 1623c3defc3590aa
parent 1622 9c98841eda96
child 1624 61cc647dac99
Matrix graph renamed to grid graph
Some usefull function and documentation
lemon/grid_graph.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/grid_graph.h	Thu Aug 11 13:20:52 2005 +0000
     1.3 @@ -0,0 +1,320 @@
     1.4 +/* -*- C++ -*-
     1.5 + * lemon/grid_graph.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef GRID_GRAPH_H
    1.21 +#define GRID_GRAPH_H
    1.22 +
    1.23 +#include <iostream>
    1.24 +#include <lemon/invalid.h>
    1.25 +#include <lemon/utility.h>
    1.26 +
    1.27 +#include <lemon/bits/iterable_graph_extender.h>
    1.28 +#include <lemon/bits/alteration_notifier.h>
    1.29 +#include <lemon/bits/default_map.h>
    1.30 +
    1.31 +#include <lemon/bits/undir_graph_extender.h>
    1.32 +
    1.33 +namespace lemon {
    1.34 +
    1.35 +  class GridGraphBase {
    1.36 +
    1.37 +  public:
    1.38 +
    1.39 +    typedef GridGraphBase Graph;
    1.40 +
    1.41 +    class Node;
    1.42 +    class Edge;
    1.43 +
    1.44 +  public:
    1.45 +
    1.46 +    GridGraphBase() {}
    1.47 +
    1.48 +  protected:
    1.49 +
    1.50 +    /// \brief Creates a grid graph with the given size.
    1.51 +    ///
    1.52 +    /// Creates a grid graph with the given size.
    1.53 +    void construct(int height, int width) {
    1.54 +      _height = height; _width = width;
    1.55 +      _nodeNum = height * width; _edgeNum = 2 * _nodeNum - width - height;
    1.56 +      _edgeLimit = _nodeNum - width;
    1.57 +    }
    1.58 +
    1.59 +  public:
    1.60 +    
    1.61 +    /// \brief The node on the given position.
    1.62 +    /// 
    1.63 +    /// Gives back the node on the given position.
    1.64 +    Node operator()(int i, int j) const {
    1.65 +      return Node(i * _width + j);
    1.66 +    }
    1.67 +
    1.68 +    /// \brief Gives back the row index of the node.
    1.69 +    ///
    1.70 +    /// Gives back the row index of the node.
    1.71 +    int row(Node n) const {
    1.72 +      return n.id / _width;
    1.73 +    }
    1.74 +    
    1.75 +    /// \brief Gives back the coloumn index of the node.
    1.76 +    ///
    1.77 +    /// Gives back the coloumn index of the node.
    1.78 +    int col(Node node) const {
    1.79 +      return n.id % _width;    
    1.80 +    }
    1.81 +
    1.82 +    /// \brief Gives back the width of the graph.
    1.83 +    ///
    1.84 +    /// Gives back the width of the graph.
    1.85 +    int width() const {
    1.86 +      return _width;
    1.87 +    }
    1.88 +
    1.89 +    /// \brief Gives back the height of the graph.
    1.90 +    ///
    1.91 +    /// Gives back the height of the graph.
    1.92 +    int height() const {
    1.93 +      return _height;
    1.94 +    }
    1.95 +
    1.96 +    typedef True NodeNumTag;
    1.97 +    typedef True EdgeNumTag;
    1.98 +
    1.99 +    ///Number of nodes.
   1.100 +    int nodeNum() const { return _nodeNum; }
   1.101 +    ///Number of edges.
   1.102 +    int edgeNum() const { return _edgeNum; }
   1.103 +
   1.104 +    /// Maximum node ID.
   1.105 +    
   1.106 +    /// Maximum node ID.
   1.107 +    ///\sa id(Node)
   1.108 +    int maxId(Node = INVALID) const { return nodeNum() - 1; }
   1.109 +    /// Maximum edge ID.
   1.110 +    
   1.111 +    /// Maximum edge ID.
   1.112 +    ///\sa id(Edge)
   1.113 +    int maxId(Edge = INVALID) const { return edgeNum() - 1; }
   1.114 +
   1.115 +    /// \brief Gives back the source node of an edge.
   1.116 +    ///    
   1.117 +    /// Gives back the source node of an edge.
   1.118 +    Node source(Edge e) const {
   1.119 +      if (e.id < _edgeLimit) {
   1.120 +	return e.id;
   1.121 +      } else {
   1.122 +	return (e.id - _edgeLimit) % (_width - 1) +
   1.123 +	  (e.id - _edgeLimit) / (_width - 1) * _width;
   1.124 +      }
   1.125 +    }
   1.126 +
   1.127 +    /// \brief Gives back the target node of an edge.
   1.128 +    ///    
   1.129 +    /// Gives back the target node of an edge.
   1.130 +    Node target(Edge e) const {
   1.131 +      if (e.id < _edgeLimit) {
   1.132 +	return e.id + _width;
   1.133 +      } else {
   1.134 +	return (e.id - _edgeLimit) % (_width - 1) +
   1.135 +	  (e.id - _edgeLimit) / (_width - 1) * _width + 1;
   1.136 +      }
   1.137 +    }
   1.138 +
   1.139 +    /// Node ID.
   1.140 +    
   1.141 +    /// The ID of a valid Node is a nonnegative integer not greater than
   1.142 +    /// \ref maxNodeId(). The range of the ID's is not surely continuous
   1.143 +    /// and the greatest node ID can be actually less then \ref maxNodeId().
   1.144 +    ///
   1.145 +    /// The ID of the \ref INVALID node is -1.
   1.146 +    ///\return The ID of the node \c v. 
   1.147 +
   1.148 +    static int id(Node v) { return v.id; }
   1.149 +    /// Edge ID.
   1.150 +    
   1.151 +    /// The ID of a valid Edge is a nonnegative integer not greater than
   1.152 +    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
   1.153 +    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   1.154 +    ///
   1.155 +    /// The ID of the \ref INVALID edge is -1.
   1.156 +    ///\return The ID of the edge \c e. 
   1.157 +    static int id(Edge e) { return e.id; }
   1.158 +
   1.159 +    static Node fromId(int id, Node) { return Node(id);}
   1.160 +    
   1.161 +    static Edge fromId(int id, Edge) { return Edge(id);}
   1.162 +
   1.163 +    typedef True FindEdgeTag;
   1.164 +
   1.165 +    /// Finds an edge between two nodes.
   1.166 +    
   1.167 +    /// Finds an edge from node \c u to node \c v.
   1.168 +    ///
   1.169 +    /// If \c prev is \ref INVALID (this is the default value), then
   1.170 +    /// It finds the first edge from \c u to \c v. Otherwise it looks for
   1.171 +    /// the next edge from \c u to \c v after \c prev.
   1.172 +    /// \return The found edge or INVALID if there is no such an edge.
   1.173 +    Edge findEdge(Node u, Node v, Edge prev = INVALID) {
   1.174 +      if (prev != INVALID) return INVALID;
   1.175 +      if (v.id - u.id == _width) return Edge(u.id);
   1.176 +      if (v.id - u.id == 1 && u.id % _width < _width - 1) {
   1.177 +	return Edge(u.id / _width * (_width - 1) +
   1.178 +		    u.id % _width + _edgeLimit);
   1.179 +      }
   1.180 +      return INVALID;
   1.181 +    }
   1.182 +    
   1.183 +      
   1.184 +    class Node {
   1.185 +      friend class GridGraphBase;
   1.186 +
   1.187 +    protected:
   1.188 +      int id;
   1.189 +      Node(int _id) { id = _id;}
   1.190 +    public:
   1.191 +      Node() {}
   1.192 +      Node (Invalid) { id = -1; }
   1.193 +      bool operator==(const Node node) const {return id == node.id;}
   1.194 +      bool operator!=(const Node node) const {return id != node.id;}
   1.195 +      bool operator<(const Node node) const {return id < node.id;}
   1.196 +    };
   1.197 +    
   1.198 +
   1.199 +
   1.200 +    class Edge {
   1.201 +      friend class GridGraphBase;
   1.202 +      
   1.203 +    protected:
   1.204 +      int id; 
   1.205 +
   1.206 +      Edge(int _id) : id(_id) {}
   1.207 +
   1.208 +    public:
   1.209 +      Edge() { }
   1.210 +      Edge (Invalid) { id = -1; }
   1.211 +      bool operator==(const Edge edge) const {return id == edge.id;}
   1.212 +      bool operator!=(const Edge edge) const {return id != edge.id;}
   1.213 +      bool operator<(const Edge edge) const {return id < edge.id;}
   1.214 +    };
   1.215 +
   1.216 +    void first(Node& node) const {
   1.217 +      node.id = nodeNum() - 1;
   1.218 +    }
   1.219 +
   1.220 +    static void next(Node& node) {
   1.221 +      --node.id;
   1.222 +    }
   1.223 +
   1.224 +    void first(Edge& edge) const {
   1.225 +      edge.id = edgeNum() - 1;
   1.226 +    }
   1.227 +
   1.228 +    static void next(Edge& edge) {
   1.229 +      --edge.id;
   1.230 +    }
   1.231 +
   1.232 +    void firstOut(Edge& edge, const Node& node) const {
   1.233 +      if (node.id < _nodeNum - _width) {
   1.234 +	edge.id = node.id;
   1.235 +      } else if (node.id % _width < _width - 1) {
   1.236 +	edge.id = _edgeLimit + node.id % _width +
   1.237 +	  (node.id / _width) * (_width - 1);
   1.238 +      } else {
   1.239 +	edge.id = -1;
   1.240 +      }
   1.241 +    }
   1.242 +
   1.243 +    void nextOut(Edge& edge) const {
   1.244 +      if (edge.id >= _edgeLimit) {
   1.245 +	edge.id = -1;
   1.246 +      } else if (edge.id % _width < _width - 1) {
   1.247 +	edge.id = _edgeLimit + edge.id % _width +
   1.248 +	  (edge.id / _width) * (_width - 1);
   1.249 +      } else {
   1.250 +	edge.id = -1;
   1.251 +      }
   1.252 +    }
   1.253 +
   1.254 +    void firstIn(Edge& edge, const Node& node) const {
   1.255 +      if (node.id >= _width) {
   1.256 +	edge.id = node.id - _width;
   1.257 +      } else if (node.id % _width > 0) {
   1.258 +	edge.id = _edgeLimit + node.id % _width +
   1.259 +	  (node.id / _width) * (_width - 1) - 1;
   1.260 +      } else {
   1.261 +	edge.id = -1;
   1.262 +      }
   1.263 +    }
   1.264 +    
   1.265 +    void nextIn(Edge& edge) const {
   1.266 +      if (edge.id >= _edgeLimit) {
   1.267 +	edge.id = -1;
   1.268 +      } else if (edge.id % _width > 0) {
   1.269 +	edge.id = _edgeLimit + edge.id % _width +
   1.270 +	  (edge.id / _width + 1) * (_width - 1) - 1;
   1.271 +      } else {
   1.272 +	edge.id = -1;
   1.273 +      }
   1.274 +    }
   1.275 +
   1.276 +  private:
   1.277 +    int _width, _height;
   1.278 +    int _nodeNum, _edgeNum;
   1.279 +    int _edgeLimit;
   1.280 +  };
   1.281 +
   1.282 +
   1.283 +  typedef UndirGraphExtender<GridGraphBase>
   1.284 +  UndirGridGraphBase;
   1.285 +  typedef AlterableUndirGraphExtender<UndirGridGraphBase> 
   1.286 +  AlterableGridGraphBase;
   1.287 +  typedef IterableUndirGraphExtender<AlterableGridGraphBase> 
   1.288 +  IterableGridGraphBase;
   1.289 +  typedef MappableUndirGraphExtender<IterableGridGraphBase> 
   1.290 +  MappableGridGraphBase;
   1.291 +
   1.292 +  /// \ingroup graphs
   1.293 +  ///
   1.294 +  /// \brief Grid graph class
   1.295 +  ///
   1.296 +  /// This class implements a special graph type. The nodes of the
   1.297 +  /// graph can be indiced by two integer \c (i,j) value where \c i
   1.298 +  /// is in the \c [0,height) range and j is in the [0, width) range.
   1.299 +  /// Two nodes are connected in the graph if the indices differ only
   1.300 +  /// on one position and only one is the difference. 
   1.301 +  ///
   1.302 +  /// The graph can be indiced in the following way:
   1.303 +  /// \code
   1.304 +  /// GridGraph graph(h, w);
   1.305 +  /// GridGraph::NodeMap<int> val(graph); 
   1.306 +  /// for (int i = 0; i < graph.height(); ++i) {
   1.307 +  ///   for (int j = 0; j < graph.width(); ++j) {
   1.308 +  ///     val[graph(i, j)] = i + j;
   1.309 +  ///   }
   1.310 +  /// }
   1.311 +  /// \endcode
   1.312 +  ///
   1.313 +  /// The graph type is fully conform to the \ref concept::UndirGraph
   1.314 +  /// "Undirected Graph" concept.
   1.315 +  ///
   1.316 +  /// \author Balazs Dezso
   1.317 +  class GridGraph : public MappableGridGraphBase {
   1.318 +  public:
   1.319 +    
   1.320 +    GridGraph(int m, int n) { construct(m, n); }
   1.321 +  };
   1.322 +}
   1.323 +#endif