New undirected graph type
authordeba
Mon, 18 Jul 2005 15:09:37 +0000
changeset 15673ea28f39218b
parent 1566 12a3101cf3ab
child 1568 f694f75de683
New undirected graph type
Represent a two dimensional undirected grid
lemon/Makefile.am
lemon/matrix_graph.h
     1.1 --- a/lemon/Makefile.am	Mon Jul 18 15:08:18 2005 +0000
     1.2 +++ b/lemon/Makefile.am	Mon Jul 18 15:09:37 2005 +0000
     1.3 @@ -30,6 +30,7 @@
     1.4  	error.h \
     1.5  	fib_heap.h \
     1.6  	full_graph.h \
     1.7 +	matrix_graph.h \
     1.8  	graph_adaptor.h \
     1.9  	graph_utils.h \
    1.10  	graph_to_eps.h \
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/lemon/matrix_graph.h	Mon Jul 18 15:09:37 2005 +0000
     2.3 @@ -0,0 +1,285 @@
     2.4 +/* -*- C++ -*-
     2.5 + * lemon/matrix_graph.h - Part of LEMON, a generic C++ optimization library
     2.6 + *
     2.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     2.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     2.9 + *
    2.10 + * Permission to use, modify and distribute this software is granted
    2.11 + * provided that this copyright notice appears in all copies. For
    2.12 + * precise terms see the accompanying LICENSE file.
    2.13 + *
    2.14 + * This software is provided "AS IS" with no warranty of any kind,
    2.15 + * express or implied, and with no claim as to its suitability for any
    2.16 + * purpose.
    2.17 + *
    2.18 + */
    2.19 +
    2.20 +#ifndef MATRIX_GRAPH_H
    2.21 +#define MATRIX_GRAPH_H
    2.22 +
    2.23 +#include <iostream>
    2.24 +#include <lemon/invalid.h>
    2.25 +#include <lemon/utility.h>
    2.26 +
    2.27 +#include <lemon/bits/iterable_graph_extender.h>
    2.28 +#include <lemon/bits/alteration_notifier.h>
    2.29 +#include <lemon/bits/default_map.h>
    2.30 +
    2.31 +#include <lemon/bits/undir_graph_extender.h>
    2.32 +
    2.33 +namespace lemon {
    2.34 +
    2.35 +  class MatrixGraphBase {
    2.36 +
    2.37 +  public:
    2.38 +
    2.39 +    typedef MatrixGraphBase Graph;
    2.40 +
    2.41 +    class Node;
    2.42 +    class Edge;
    2.43 +
    2.44 +  public:
    2.45 +
    2.46 +    MatrixGraphBase() {}
    2.47 +
    2.48 +    ///Creates a full graph with \c n nodes.
    2.49 +    void construct(int height, int width) {
    2.50 +      _height = height; _width = width;
    2.51 +      _nodeNum = height * width; _edgeNum = 2 * _nodeNum - width - height;
    2.52 +      _edgeLimit = _nodeNum - width;
    2.53 +    }
    2.54 +    
    2.55 +    Node operator()(int i, int j) const {
    2.56 +      return Node(i * _width + j);
    2.57 +    }
    2.58 +
    2.59 +    int width() const {
    2.60 +      return _width;
    2.61 +    }
    2.62 +
    2.63 +    int height() const {
    2.64 +      return _height;
    2.65 +    }
    2.66 +
    2.67 +    typedef True NodeNumTag;
    2.68 +    typedef True EdgeNumTag;
    2.69 +
    2.70 +    ///Number of nodes.
    2.71 +    int nodeNum() const { return _nodeNum; }
    2.72 +    ///Number of edges.
    2.73 +    int edgeNum() const { return _edgeNum; }
    2.74 +
    2.75 +    /// Maximum node ID.
    2.76 +    
    2.77 +    /// Maximum node ID.
    2.78 +    ///\sa id(Node)
    2.79 +    int maxId(Node = INVALID) const { return nodeNum() - 1; }
    2.80 +    /// Maximum edge ID.
    2.81 +    
    2.82 +    /// Maximum edge ID.
    2.83 +    ///\sa id(Edge)
    2.84 +    int maxId(Edge = INVALID) const { return edgeNum() - 1; }
    2.85 +
    2.86 +    Node source(Edge e) const {
    2.87 +      if (e.id < _edgeLimit) {
    2.88 +	return e.id;
    2.89 +      } else {
    2.90 +	return (e.id - _edgeLimit) % (_width - 1) +
    2.91 +	  (e.id - _edgeLimit) / (_width - 1) * _width;
    2.92 +      }
    2.93 +    }
    2.94 +
    2.95 +    Node target(Edge e) const {
    2.96 +      if (e.id < _edgeLimit) {
    2.97 +	return e.id + _width;
    2.98 +      } else {
    2.99 +	return (e.id - _edgeLimit) % (_width - 1) +
   2.100 +	  (e.id - _edgeLimit) / (_width - 1) * _width + 1;
   2.101 +      }
   2.102 +    }
   2.103 +
   2.104 +    /// Node ID.
   2.105 +    
   2.106 +    /// The ID of a valid Node is a nonnegative integer not greater than
   2.107 +    /// \ref maxNodeId(). The range of the ID's is not surely continuous
   2.108 +    /// and the greatest node ID can be actually less then \ref maxNodeId().
   2.109 +    ///
   2.110 +    /// The ID of the \ref INVALID node is -1.
   2.111 +    ///\return The ID of the node \c v. 
   2.112 +
   2.113 +    static int id(Node v) { return v.id; }
   2.114 +    /// Edge ID.
   2.115 +    
   2.116 +    /// The ID of a valid Edge is a nonnegative integer not greater than
   2.117 +    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
   2.118 +    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   2.119 +    ///
   2.120 +    /// The ID of the \ref INVALID edge is -1.
   2.121 +    ///\return The ID of the edge \c e. 
   2.122 +    static int id(Edge e) { return e.id; }
   2.123 +
   2.124 +    static Node fromId(int id, Node) { return Node(id);}
   2.125 +    
   2.126 +    static Edge fromId(int id, Edge) { return Edge(id);}
   2.127 +
   2.128 +    typedef True FindEdgeTag;
   2.129 +
   2.130 +    /// Finds an edge between two nodes.
   2.131 +    
   2.132 +    /// Finds an edge from node \c u to node \c v.
   2.133 +    ///
   2.134 +    /// If \c prev is \ref INVALID (this is the default value), then
   2.135 +    /// It finds the first edge from \c u to \c v. Otherwise it looks for
   2.136 +    /// the next edge from \c u to \c v after \c prev.
   2.137 +    /// \return The found edge or INVALID if there is no such an edge.
   2.138 +    Edge findEdge(Node u, Node v, Edge prev = INVALID) {
   2.139 +      if (prev != INVALID) return INVALID;
   2.140 +      if (v.id - u.id == _width) return Edge(u.id);
   2.141 +      if (v.id - u.id == 1 && u.id % _width < _width - 1) {
   2.142 +	return Edge(u.id / _width * (_width - 1) +
   2.143 +		    u.id % _width + _edgeLimit);
   2.144 +      }
   2.145 +      return INVALID;
   2.146 +    }
   2.147 +    
   2.148 +      
   2.149 +    class Node {
   2.150 +      friend class MatrixGraphBase;
   2.151 +
   2.152 +    protected:
   2.153 +      int id;
   2.154 +      Node(int _id) { id = _id;}
   2.155 +    public:
   2.156 +      Node() {}
   2.157 +      Node (Invalid) { id = -1; }
   2.158 +      bool operator==(const Node node) const {return id == node.id;}
   2.159 +      bool operator!=(const Node node) const {return id != node.id;}
   2.160 +      bool operator<(const Node node) const {return id < node.id;}
   2.161 +    };
   2.162 +    
   2.163 +
   2.164 +
   2.165 +    class Edge {
   2.166 +      friend class MatrixGraphBase;
   2.167 +      
   2.168 +    protected:
   2.169 +      int id; 
   2.170 +
   2.171 +      Edge(int _id) : id(_id) {}
   2.172 +
   2.173 +    public:
   2.174 +      Edge() { }
   2.175 +      Edge (Invalid) { id = -1; }
   2.176 +      bool operator==(const Edge edge) const {return id == edge.id;}
   2.177 +      bool operator!=(const Edge edge) const {return id != edge.id;}
   2.178 +      bool operator<(const Edge edge) const {return id < edge.id;}
   2.179 +    };
   2.180 +
   2.181 +    void first(Node& node) const {
   2.182 +      node.id = nodeNum() - 1;
   2.183 +    }
   2.184 +
   2.185 +    static void next(Node& node) {
   2.186 +      --node.id;
   2.187 +    }
   2.188 +
   2.189 +    void first(Edge& edge) const {
   2.190 +      edge.id = edgeNum() - 1;
   2.191 +    }
   2.192 +
   2.193 +    static void next(Edge& edge) {
   2.194 +      --edge.id;
   2.195 +    }
   2.196 +
   2.197 +    void firstOut(Edge& edge, const Node& node) const {
   2.198 +      if (node.id < _nodeNum - _width) {
   2.199 +	edge.id = node.id;
   2.200 +      } else if (node.id % _width < _width - 1) {
   2.201 +	edge.id = _edgeLimit + node.id % _width +
   2.202 +	  (node.id / _width) * (_width - 1);
   2.203 +      } else {
   2.204 +	edge.id = -1;
   2.205 +      }
   2.206 +    }
   2.207 +
   2.208 +    void nextOut(Edge& edge) const {
   2.209 +      if (edge.id >= _edgeLimit) {
   2.210 +	edge.id = -1;
   2.211 +      } else if (edge.id % _width < _width - 1) {
   2.212 +	edge.id = _edgeLimit + edge.id % _width +
   2.213 +	  (edge.id / _width) * (_width - 1);
   2.214 +      } else {
   2.215 +	edge.id = -1;
   2.216 +      }
   2.217 +    }
   2.218 +
   2.219 +    void firstIn(Edge& edge, const Node& node) const {
   2.220 +      if (node.id >= _width) {
   2.221 +	edge.id = node.id - _width;
   2.222 +      } else if (node.id % _width > 0) {
   2.223 +	edge.id = _edgeLimit + node.id % _width +
   2.224 +	  (node.id / _width) * (_width - 1) - 1;
   2.225 +      } else {
   2.226 +	edge.id = -1;
   2.227 +      }
   2.228 +    }
   2.229 +    
   2.230 +    void nextIn(Edge& edge) const {
   2.231 +      if (edge.id >= _edgeLimit) {
   2.232 +	edge.id = -1;
   2.233 +      } else if (edge.id % _width > 0) {
   2.234 +	edge.id = _edgeLimit + edge.id % _width +
   2.235 +	  (edge.id / _width + 1) * (_width - 1) - 1;
   2.236 +      } else {
   2.237 +	edge.id = -1;
   2.238 +      }
   2.239 +    }
   2.240 +
   2.241 +  private:
   2.242 +    int _width, _height;
   2.243 +    int _nodeNum, _edgeNum;
   2.244 +    int _edgeLimit;
   2.245 +  };
   2.246 +
   2.247 +
   2.248 +  typedef UndirGraphExtender<MatrixGraphBase>
   2.249 +  UndirMatrixGraphBase;
   2.250 +  typedef AlterableUndirGraphExtender<UndirMatrixGraphBase> 
   2.251 +  AlterableMatrixGraphBase;
   2.252 +  typedef IterableUndirGraphExtender<AlterableMatrixGraphBase> 
   2.253 +  IterableMatrixGraphBase;
   2.254 +  typedef MappableUndirGraphExtender<IterableMatrixGraphBase> 
   2.255 +  MappableMatrixGraphBase;
   2.256 +
   2.257 +  /// \ingroup graphs
   2.258 +  ///
   2.259 +  /// \brief Matrix graph class
   2.260 +  ///
   2.261 +  /// This class implements a special graph type. The nodes of the
   2.262 +  /// graph can be indiced by two integer \c (i,j) value where \c i
   2.263 +  /// is in the \c [0,height) range and j is in the [0, width) range.
   2.264 +  /// Two nodes are connected in the graph if the indices differ only
   2.265 +  /// on one position and only one is the difference. 
   2.266 +  ///
   2.267 +  /// The graph can be indiced in the next way:
   2.268 +  /// \code
   2.269 +  /// MatrixGraph graph(h, w);
   2.270 +  /// MatrixGraph::NodeMap<int> val(graph); 
   2.271 +  /// for (int i = 0; i < graph.height(); ++i) {
   2.272 +  ///   for (int j = 0; j < graph.width(); ++j) {
   2.273 +  ///     val[graph(i, j)] = i + j;
   2.274 +  ///   }
   2.275 +  /// }
   2.276 +  /// \endcode
   2.277 +  ///
   2.278 +  /// The graph type is fully conform to the \ref concept::UndirGraph
   2.279 +  /// "Undirected Graph" concept.
   2.280 +  ///
   2.281 +  /// \author Balazs Dezso
   2.282 +  class MatrixGraph : public MappableMatrixGraphBase {
   2.283 +  public:
   2.284 +    
   2.285 +    MatrixGraph(int m, int n) { construct(m, n); }
   2.286 +  };
   2.287 +}
   2.288 +#endif