Matrix graph renamed -> Grid graph
authordeba
Thu, 11 Aug 2005 15:56:21 +0000
changeset 1628191264dc6925
parent 1627 3fd1ba6e9872
child 1629 a245eff568a6
Matrix graph renamed -> Grid graph
lemon/matrix_graph.h
     1.1 --- a/lemon/matrix_graph.h	Thu Aug 11 15:55:17 2005 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,285 +0,0 @@
     1.4 -/* -*- C++ -*-
     1.5 - * lemon/matrix_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 MATRIX_GRAPH_H
    1.21 -#define MATRIX_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 MatrixGraphBase {
    1.36 -
    1.37 -  public:
    1.38 -
    1.39 -    typedef MatrixGraphBase Graph;
    1.40 -
    1.41 -    class Node;
    1.42 -    class Edge;
    1.43 -
    1.44 -  public:
    1.45 -
    1.46 -    MatrixGraphBase() {}
    1.47 -
    1.48 -    ///Creates a full graph with \c n nodes.
    1.49 -    void construct(int height, int width) {
    1.50 -      _height = height; _width = width;
    1.51 -      _nodeNum = height * width; _edgeNum = 2 * _nodeNum - width - height;
    1.52 -      _edgeLimit = _nodeNum - width;
    1.53 -    }
    1.54 -    
    1.55 -    Node operator()(int i, int j) const {
    1.56 -      return Node(i * _width + j);
    1.57 -    }
    1.58 -
    1.59 -    int width() const {
    1.60 -      return _width;
    1.61 -    }
    1.62 -
    1.63 -    int height() const {
    1.64 -      return _height;
    1.65 -    }
    1.66 -
    1.67 -    typedef True NodeNumTag;
    1.68 -    typedef True EdgeNumTag;
    1.69 -
    1.70 -    ///Number of nodes.
    1.71 -    int nodeNum() const { return _nodeNum; }
    1.72 -    ///Number of edges.
    1.73 -    int edgeNum() const { return _edgeNum; }
    1.74 -
    1.75 -    /// Maximum node ID.
    1.76 -    
    1.77 -    /// Maximum node ID.
    1.78 -    ///\sa id(Node)
    1.79 -    int maxId(Node = INVALID) const { return nodeNum() - 1; }
    1.80 -    /// Maximum edge ID.
    1.81 -    
    1.82 -    /// Maximum edge ID.
    1.83 -    ///\sa id(Edge)
    1.84 -    int maxId(Edge = INVALID) const { return edgeNum() - 1; }
    1.85 -
    1.86 -    Node source(Edge e) const {
    1.87 -      if (e.id < _edgeLimit) {
    1.88 -	return e.id;
    1.89 -      } else {
    1.90 -	return (e.id - _edgeLimit) % (_width - 1) +
    1.91 -	  (e.id - _edgeLimit) / (_width - 1) * _width;
    1.92 -      }
    1.93 -    }
    1.94 -
    1.95 -    Node target(Edge e) const {
    1.96 -      if (e.id < _edgeLimit) {
    1.97 -	return e.id + _width;
    1.98 -      } else {
    1.99 -	return (e.id - _edgeLimit) % (_width - 1) +
   1.100 -	  (e.id - _edgeLimit) / (_width - 1) * _width + 1;
   1.101 -      }
   1.102 -    }
   1.103 -
   1.104 -    /// Node ID.
   1.105 -    
   1.106 -    /// The ID of a valid Node is a nonnegative integer not greater than
   1.107 -    /// \ref maxNodeId(). The range of the ID's is not surely continuous
   1.108 -    /// and the greatest node ID can be actually less then \ref maxNodeId().
   1.109 -    ///
   1.110 -    /// The ID of the \ref INVALID node is -1.
   1.111 -    ///\return The ID of the node \c v. 
   1.112 -
   1.113 -    static int id(Node v) { return v.id; }
   1.114 -    /// Edge ID.
   1.115 -    
   1.116 -    /// The ID of a valid Edge is a nonnegative integer not greater than
   1.117 -    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
   1.118 -    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   1.119 -    ///
   1.120 -    /// The ID of the \ref INVALID edge is -1.
   1.121 -    ///\return The ID of the edge \c e. 
   1.122 -    static int id(Edge e) { return e.id; }
   1.123 -
   1.124 -    static Node fromId(int id, Node) { return Node(id);}
   1.125 -    
   1.126 -    static Edge fromId(int id, Edge) { return Edge(id);}
   1.127 -
   1.128 -    typedef True FindEdgeTag;
   1.129 -
   1.130 -    /// Finds an edge between two nodes.
   1.131 -    
   1.132 -    /// Finds an edge from node \c u to node \c v.
   1.133 -    ///
   1.134 -    /// If \c prev is \ref INVALID (this is the default value), then
   1.135 -    /// It finds the first edge from \c u to \c v. Otherwise it looks for
   1.136 -    /// the next edge from \c u to \c v after \c prev.
   1.137 -    /// \return The found edge or INVALID if there is no such an edge.
   1.138 -    Edge findEdge(Node u, Node v, Edge prev = INVALID) {
   1.139 -      if (prev != INVALID) return INVALID;
   1.140 -      if (v.id - u.id == _width) return Edge(u.id);
   1.141 -      if (v.id - u.id == 1 && u.id % _width < _width - 1) {
   1.142 -	return Edge(u.id / _width * (_width - 1) +
   1.143 -		    u.id % _width + _edgeLimit);
   1.144 -      }
   1.145 -      return INVALID;
   1.146 -    }
   1.147 -    
   1.148 -      
   1.149 -    class Node {
   1.150 -      friend class MatrixGraphBase;
   1.151 -
   1.152 -    protected:
   1.153 -      int id;
   1.154 -      Node(int _id) { id = _id;}
   1.155 -    public:
   1.156 -      Node() {}
   1.157 -      Node (Invalid) { id = -1; }
   1.158 -      bool operator==(const Node node) const {return id == node.id;}
   1.159 -      bool operator!=(const Node node) const {return id != node.id;}
   1.160 -      bool operator<(const Node node) const {return id < node.id;}
   1.161 -    };
   1.162 -    
   1.163 -
   1.164 -
   1.165 -    class Edge {
   1.166 -      friend class MatrixGraphBase;
   1.167 -      
   1.168 -    protected:
   1.169 -      int id; 
   1.170 -
   1.171 -      Edge(int _id) : id(_id) {}
   1.172 -
   1.173 -    public:
   1.174 -      Edge() { }
   1.175 -      Edge (Invalid) { id = -1; }
   1.176 -      bool operator==(const Edge edge) const {return id == edge.id;}
   1.177 -      bool operator!=(const Edge edge) const {return id != edge.id;}
   1.178 -      bool operator<(const Edge edge) const {return id < edge.id;}
   1.179 -    };
   1.180 -
   1.181 -    void first(Node& node) const {
   1.182 -      node.id = nodeNum() - 1;
   1.183 -    }
   1.184 -
   1.185 -    static void next(Node& node) {
   1.186 -      --node.id;
   1.187 -    }
   1.188 -
   1.189 -    void first(Edge& edge) const {
   1.190 -      edge.id = edgeNum() - 1;
   1.191 -    }
   1.192 -
   1.193 -    static void next(Edge& edge) {
   1.194 -      --edge.id;
   1.195 -    }
   1.196 -
   1.197 -    void firstOut(Edge& edge, const Node& node) const {
   1.198 -      if (node.id < _nodeNum - _width) {
   1.199 -	edge.id = node.id;
   1.200 -      } else if (node.id % _width < _width - 1) {
   1.201 -	edge.id = _edgeLimit + node.id % _width +
   1.202 -	  (node.id / _width) * (_width - 1);
   1.203 -      } else {
   1.204 -	edge.id = -1;
   1.205 -      }
   1.206 -    }
   1.207 -
   1.208 -    void nextOut(Edge& edge) const {
   1.209 -      if (edge.id >= _edgeLimit) {
   1.210 -	edge.id = -1;
   1.211 -      } else if (edge.id % _width < _width - 1) {
   1.212 -	edge.id = _edgeLimit + edge.id % _width +
   1.213 -	  (edge.id / _width) * (_width - 1);
   1.214 -      } else {
   1.215 -	edge.id = -1;
   1.216 -      }
   1.217 -    }
   1.218 -
   1.219 -    void firstIn(Edge& edge, const Node& node) const {
   1.220 -      if (node.id >= _width) {
   1.221 -	edge.id = node.id - _width;
   1.222 -      } else if (node.id % _width > 0) {
   1.223 -	edge.id = _edgeLimit + node.id % _width +
   1.224 -	  (node.id / _width) * (_width - 1) - 1;
   1.225 -      } else {
   1.226 -	edge.id = -1;
   1.227 -      }
   1.228 -    }
   1.229 -    
   1.230 -    void nextIn(Edge& edge) const {
   1.231 -      if (edge.id >= _edgeLimit) {
   1.232 -	edge.id = -1;
   1.233 -      } else if (edge.id % _width > 0) {
   1.234 -	edge.id = _edgeLimit + edge.id % _width +
   1.235 -	  (edge.id / _width + 1) * (_width - 1) - 1;
   1.236 -      } else {
   1.237 -	edge.id = -1;
   1.238 -      }
   1.239 -    }
   1.240 -
   1.241 -  private:
   1.242 -    int _width, _height;
   1.243 -    int _nodeNum, _edgeNum;
   1.244 -    int _edgeLimit;
   1.245 -  };
   1.246 -
   1.247 -
   1.248 -  typedef UndirGraphExtender<MatrixGraphBase>
   1.249 -  UndirMatrixGraphBase;
   1.250 -  typedef AlterableUndirGraphExtender<UndirMatrixGraphBase> 
   1.251 -  AlterableMatrixGraphBase;
   1.252 -  typedef IterableUndirGraphExtender<AlterableMatrixGraphBase> 
   1.253 -  IterableMatrixGraphBase;
   1.254 -  typedef MappableUndirGraphExtender<IterableMatrixGraphBase> 
   1.255 -  MappableMatrixGraphBase;
   1.256 -
   1.257 -  /// \ingroup graphs
   1.258 -  ///
   1.259 -  /// \brief Matrix graph class
   1.260 -  ///
   1.261 -  /// This class implements a special graph type. The nodes of the
   1.262 -  /// graph can be indiced by two integer \c (i,j) value where \c i
   1.263 -  /// is in the \c [0,height) range and j is in the [0, width) range.
   1.264 -  /// Two nodes are connected in the graph if the indices differ only
   1.265 -  /// on one position and only one is the difference. 
   1.266 -  ///
   1.267 -  /// The graph can be indiced in the following way:
   1.268 -  /// \code
   1.269 -  /// MatrixGraph graph(h, w);
   1.270 -  /// MatrixGraph::NodeMap<int> val(graph); 
   1.271 -  /// for (int i = 0; i < graph.height(); ++i) {
   1.272 -  ///   for (int j = 0; j < graph.width(); ++j) {
   1.273 -  ///     val[graph(i, j)] = i + j;
   1.274 -  ///   }
   1.275 -  /// }
   1.276 -  /// \endcode
   1.277 -  ///
   1.278 -  /// The graph type is fully conform to the \ref concept::UndirGraph
   1.279 -  /// "Undirected Graph" concept.
   1.280 -  ///
   1.281 -  /// \author Balazs Dezso
   1.282 -  class MatrixGraph : public MappableMatrixGraphBase {
   1.283 -  public:
   1.284 -    
   1.285 -    MatrixGraph(int m, int n) { construct(m, n); }
   1.286 -  };
   1.287 -}
   1.288 -#endif