1.1 --- a/lemon/grid_graph.h Wed Feb 22 12:45:59 2006 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,522 +0,0 @@
1.4 -/* -*- C++ -*-
1.5 - *
1.6 - * This file is a part of LEMON, a generic C++ optimization library
1.7 - *
1.8 - * Copyright (C) 2003-2006
1.9 - * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 - * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 - *
1.12 - * Permission to use, modify and distribute this software is granted
1.13 - * provided that this copyright notice appears in all copies. For
1.14 - * precise terms see the accompanying LICENSE file.
1.15 - *
1.16 - * This software is provided "AS IS" with no warranty of any kind,
1.17 - * express or implied, and with no claim as to its suitability for any
1.18 - * purpose.
1.19 - *
1.20 - */
1.21 -
1.22 -#ifndef GRID_GRAPH_H
1.23 -#define GRID_GRAPH_H
1.24 -
1.25 -#include <iostream>
1.26 -#include <lemon/invalid.h>
1.27 -#include <lemon/utility.h>
1.28 -
1.29 -#include <lemon/bits/iterable_graph_extender.h>
1.30 -#include <lemon/bits/alteration_notifier.h>
1.31 -#include <lemon/bits/static_map.h>
1.32 -#include <lemon/bits/graph_extender.h>
1.33 -
1.34 -#include <lemon/xy.h>
1.35 -
1.36 -///\ingroup graphs
1.37 -///\file
1.38 -///\brief GridGraph class.
1.39 -
1.40 -namespace lemon {
1.41 -
1.42 - /// \brief Base graph for GridGraph.
1.43 - ///
1.44 - /// Base graph for grid graph. It describes some member functions
1.45 - /// which can be used in the GridGraph.
1.46 - ///
1.47 - /// \warning Always use the GridGraph instead of this.
1.48 - /// \see GridGraph
1.49 - class GridGraphBase {
1.50 -
1.51 - public:
1.52 -
1.53 - typedef GridGraphBase Graph;
1.54 -
1.55 - class Node;
1.56 - class Edge;
1.57 -
1.58 - public:
1.59 -
1.60 - GridGraphBase() {}
1.61 -
1.62 - protected:
1.63 -
1.64 - /// \brief Creates a grid graph with the given size.
1.65 - ///
1.66 - /// Creates a grid graph with the given size.
1.67 - void construct(int width, int height) {
1.68 - _height = height; _width = width;
1.69 - _nodeNum = height * width; _edgeNum = 2 * _nodeNum - width - height;
1.70 - _edgeLimit = _nodeNum - width;
1.71 - }
1.72 -
1.73 - /// \brief Gives back the edge goes down from the node.
1.74 - ///
1.75 - /// Gives back the edge goes down from the node. If there is not
1.76 - /// outgoing edge then it gives back INVALID.
1.77 - Edge _down(Node n) const {
1.78 - if (n.id < _nodeNum - _width) {
1.79 - return Edge(n.id);
1.80 - } else {
1.81 - return INVALID;
1.82 - }
1.83 - }
1.84 -
1.85 - /// \brief Gives back the edge comes from up into the node.
1.86 - ///
1.87 - /// Gives back the edge comes from up into the node. If there is not
1.88 - /// incoming edge then it gives back INVALID.
1.89 - Edge _up(Node n) const {
1.90 - if (n.id >= _width) {
1.91 - return Edge(n.id - _width);
1.92 - } else {
1.93 - return INVALID;
1.94 - }
1.95 - }
1.96 -
1.97 - /// \brief Gives back the edge goes right from the node.
1.98 - ///
1.99 - /// Gives back the edge goes right from the node. If there is not
1.100 - /// outgoing edge then it gives back INVALID.
1.101 - Edge _right(Node n) const {
1.102 - if (n.id % _width < _width - 1) {
1.103 - return _edgeLimit + n.id % _width + (n.id / _width) * (_width - 1);
1.104 - } else {
1.105 - return INVALID;
1.106 - }
1.107 - }
1.108 -
1.109 - /// \brief Gives back the edge comes from left into the node.
1.110 - ///
1.111 - /// Gives back the edge comes left up into the node. If there is not
1.112 - /// incoming edge then it gives back INVALID.
1.113 - Edge _left(Node n) const {
1.114 - if (n.id % _width > 0) {
1.115 - return _edgeLimit + n.id % _width + (n.id / _width) * (_width - 1) - 1;
1.116 - } else {
1.117 - return INVALID;
1.118 - }
1.119 - }
1.120 -
1.121 -
1.122 - public:
1.123 -
1.124 - /// \brief The node on the given position.
1.125 - ///
1.126 - /// Gives back the node on the given position.
1.127 - Node operator()(int i, int j) const {
1.128 - return Node(i + j * _width);
1.129 - }
1.130 -
1.131 - /// \brief Gives back the row index of the node.
1.132 - ///
1.133 - /// Gives back the row index of the node.
1.134 - int row(Node n) const {
1.135 - return n.id / _width;
1.136 - }
1.137 -
1.138 - /// \brief Gives back the coloumn index of the node.
1.139 - ///
1.140 - /// Gives back the coloumn index of the node.
1.141 - int col(Node n) const {
1.142 - return n.id % _width;
1.143 - }
1.144 -
1.145 - /// \brief Gives back the width of the graph.
1.146 - ///
1.147 - /// Gives back the width of the graph.
1.148 - int width() const {
1.149 - return _width;
1.150 - }
1.151 -
1.152 - /// \brief Gives back the height of the graph.
1.153 - ///
1.154 - /// Gives back the height of the graph.
1.155 - int height() const {
1.156 - return _height;
1.157 - }
1.158 -
1.159 - typedef True NodeNumTag;
1.160 - typedef True EdgeNumTag;
1.161 -
1.162 - ///Number of nodes.
1.163 - int nodeNum() const { return _nodeNum; }
1.164 - ///Number of edges.
1.165 - int edgeNum() const { return _edgeNum; }
1.166 -
1.167 - /// Maximum node ID.
1.168 -
1.169 - /// Maximum node ID.
1.170 - ///\sa id(Node)
1.171 - int maxNodeId() const { return nodeNum() - 1; }
1.172 - /// Maximum edge ID.
1.173 -
1.174 - /// Maximum edge ID.
1.175 - ///\sa id(Edge)
1.176 - int maxEdgeId() const { return edgeNum() - 1; }
1.177 -
1.178 - /// \brief Gives back the source node of an edge.
1.179 - ///
1.180 - /// Gives back the source node of an edge.
1.181 - Node source(Edge e) const {
1.182 - if (e.id < _edgeLimit) {
1.183 - return e.id;
1.184 - } else {
1.185 - return (e.id - _edgeLimit) % (_width - 1) +
1.186 - (e.id - _edgeLimit) / (_width - 1) * _width;
1.187 - }
1.188 - }
1.189 -
1.190 - /// \brief Gives back the target node of an edge.
1.191 - ///
1.192 - /// Gives back the target node of an edge.
1.193 - Node target(Edge e) const {
1.194 - if (e.id < _edgeLimit) {
1.195 - return e.id + _width;
1.196 - } else {
1.197 - return (e.id - _edgeLimit) % (_width - 1) +
1.198 - (e.id - _edgeLimit) / (_width - 1) * _width + 1;
1.199 - }
1.200 - }
1.201 -
1.202 - /// Node ID.
1.203 -
1.204 - /// The ID of a valid Node is a nonnegative integer not greater than
1.205 - /// \ref maxNodeId(). The range of the ID's is not surely continuous
1.206 - /// and the greatest node ID can be actually less then \ref maxNodeId().
1.207 - ///
1.208 - /// The ID of the \ref INVALID node is -1.
1.209 - ///\return The ID of the node \c v.
1.210 -
1.211 - static int id(Node v) { return v.id; }
1.212 - /// Edge ID.
1.213 -
1.214 - /// The ID of a valid Edge is a nonnegative integer not greater than
1.215 - /// \ref maxEdgeId(). The range of the ID's is not surely continuous
1.216 - /// and the greatest edge ID can be actually less then \ref maxEdgeId().
1.217 - ///
1.218 - /// The ID of the \ref INVALID edge is -1.
1.219 - ///\return The ID of the edge \c e.
1.220 - static int id(Edge e) { return e.id; }
1.221 -
1.222 - static Node nodeFromId(int id) { return Node(id);}
1.223 -
1.224 - static Edge edgeFromId(int id) { return Edge(id);}
1.225 -
1.226 - typedef True FindEdgeTag;
1.227 -
1.228 - /// Finds an edge between two nodes.
1.229 -
1.230 - /// Finds an edge from node \c u to node \c v.
1.231 - ///
1.232 - /// If \c prev is \ref INVALID (this is the default value), then
1.233 - /// It finds the first edge from \c u to \c v. Otherwise it looks for
1.234 - /// the next edge from \c u to \c v after \c prev.
1.235 - /// \return The found edge or INVALID if there is no such an edge.
1.236 - Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
1.237 - if (prev != INVALID) return INVALID;
1.238 - if (v.id - u.id == _width) return Edge(u.id);
1.239 - if (v.id - u.id == 1 && u.id % _width < _width - 1) {
1.240 - return Edge(u.id / _width * (_width - 1) +
1.241 - u.id % _width + _edgeLimit);
1.242 - }
1.243 - return INVALID;
1.244 - }
1.245 -
1.246 -
1.247 - class Node {
1.248 - friend class GridGraphBase;
1.249 -
1.250 - protected:
1.251 - int id;
1.252 - Node(int _id) { id = _id;}
1.253 - public:
1.254 - Node() {}
1.255 - Node (Invalid) { id = -1; }
1.256 - bool operator==(const Node node) const {return id == node.id;}
1.257 - bool operator!=(const Node node) const {return id != node.id;}
1.258 - bool operator<(const Node node) const {return id < node.id;}
1.259 - };
1.260 -
1.261 -
1.262 -
1.263 - class Edge {
1.264 - friend class GridGraphBase;
1.265 -
1.266 - protected:
1.267 - int id;
1.268 -
1.269 - Edge(int _id) : id(_id) {}
1.270 -
1.271 - public:
1.272 - Edge() { }
1.273 - Edge (Invalid) { id = -1; }
1.274 - bool operator==(const Edge edge) const {return id == edge.id;}
1.275 - bool operator!=(const Edge edge) const {return id != edge.id;}
1.276 - bool operator<(const Edge edge) const {return id < edge.id;}
1.277 - };
1.278 -
1.279 - void first(Node& node) const {
1.280 - node.id = nodeNum() - 1;
1.281 - }
1.282 -
1.283 - static void next(Node& node) {
1.284 - --node.id;
1.285 - }
1.286 -
1.287 - void first(Edge& edge) const {
1.288 - edge.id = edgeNum() - 1;
1.289 - }
1.290 -
1.291 - static void next(Edge& edge) {
1.292 - --edge.id;
1.293 - }
1.294 -
1.295 - void firstOut(Edge& edge, const Node& node) const {
1.296 - if (node.id < _nodeNum - _width) {
1.297 - edge.id = node.id;
1.298 - } else if (node.id % _width < _width - 1) {
1.299 - edge.id = _edgeLimit + node.id % _width +
1.300 - (node.id / _width) * (_width - 1);
1.301 - } else {
1.302 - edge.id = -1;
1.303 - }
1.304 - }
1.305 -
1.306 - void nextOut(Edge& edge) const {
1.307 - if (edge.id >= _edgeLimit) {
1.308 - edge.id = -1;
1.309 - } else if (edge.id % _width < _width - 1) {
1.310 - edge.id = _edgeLimit + edge.id % _width +
1.311 - (edge.id / _width) * (_width - 1);
1.312 - } else {
1.313 - edge.id = -1;
1.314 - }
1.315 - }
1.316 -
1.317 - void firstIn(Edge& edge, const Node& node) const {
1.318 - if (node.id >= _width) {
1.319 - edge.id = node.id - _width;
1.320 - } else if (node.id % _width > 0) {
1.321 - edge.id = _edgeLimit + node.id % _width +
1.322 - (node.id / _width) * (_width - 1) - 1;
1.323 - } else {
1.324 - edge.id = -1;
1.325 - }
1.326 - }
1.327 -
1.328 - void nextIn(Edge& edge) const {
1.329 - if (edge.id >= _edgeLimit) {
1.330 - edge.id = -1;
1.331 - } else if (edge.id % _width > 0) {
1.332 - edge.id = _edgeLimit + edge.id % _width +
1.333 - (edge.id / _width + 1) * (_width - 1) - 1;
1.334 - } else {
1.335 - edge.id = -1;
1.336 - }
1.337 - }
1.338 -
1.339 - private:
1.340 - int _width, _height;
1.341 - int _nodeNum, _edgeNum;
1.342 - int _edgeLimit;
1.343 - };
1.344 -
1.345 -
1.346 - typedef StaticMappableUGraphExtender<
1.347 - IterableUGraphExtender<
1.348 - AlterableUGraphExtender<
1.349 - UGraphExtender<GridGraphBase> > > > ExtendedGridGraphBase;
1.350 -
1.351 - /// \ingroup graphs
1.352 - ///
1.353 - /// \brief Grid graph class
1.354 - ///
1.355 - /// This class implements a special graph type. The nodes of the
1.356 - /// graph can be indiced by two integer \c (i,j) value where \c i
1.357 - /// is in the \c [0,width) range and j is in the [0, height) range.
1.358 - /// Two nodes are connected in the graph if the indices differ only
1.359 - /// on one position and only one is the difference.
1.360 - ///
1.361 - /// The graph can be indiced in the following way:
1.362 - ///\code
1.363 - /// GridGraph graph(w, h);
1.364 - /// GridGraph::NodeMap<int> val(graph);
1.365 - /// for (int i = 0; i < graph.width(); ++i) {
1.366 - /// for (int j = 0; j < graph.height(); ++j) {
1.367 - /// val[graph(i, j)] = i + j;
1.368 - /// }
1.369 - /// }
1.370 - ///\endcode
1.371 - ///
1.372 - /// The graph type is fully conform to the \ref concept::UGraph
1.373 - /// "Undirected Graph" concept.
1.374 - ///
1.375 - /// \author Balazs Dezso
1.376 - /// \see GridGraphBase
1.377 - class GridGraph : public ExtendedGridGraphBase {
1.378 - public:
1.379 -
1.380 - /// \brief Map to get the indices of the nodes as xy<int>.
1.381 - ///
1.382 - /// Map to get the indices of the nodes as xy<int>.
1.383 - class IndexMap {
1.384 - public:
1.385 - typedef True NeedCopy;
1.386 - /// \brief The key type of the map
1.387 - typedef GridGraph::Node Key;
1.388 - /// \brief The value type of the map
1.389 - typedef xy<int> Value;
1.390 -
1.391 - /// \brief Constructor
1.392 - ///
1.393 - /// Constructor
1.394 - IndexMap(const GridGraph& _graph) : graph(_graph) {}
1.395 -
1.396 - /// \brief The subscript operator
1.397 - ///
1.398 - /// The subscript operator.
1.399 - Value operator[](Key key) const {
1.400 - return xy<int>(graph.row(key), graph.col(key));
1.401 - }
1.402 -
1.403 - private:
1.404 - const GridGraph& graph;
1.405 - };
1.406 -
1.407 - /// \brief Map to get the row of the nodes.
1.408 - ///
1.409 - /// Map to get the row of the nodes.
1.410 - class RowMap {
1.411 - public:
1.412 - typedef True NeedCopy;
1.413 - /// \brief The key type of the map
1.414 - typedef GridGraph::Node Key;
1.415 - /// \brief The value type of the map
1.416 - typedef int Value;
1.417 -
1.418 - /// \brief Constructor
1.419 - ///
1.420 - /// Constructor
1.421 - RowMap(const GridGraph& _graph) : graph(_graph) {}
1.422 -
1.423 - /// \brief The subscript operator
1.424 - ///
1.425 - /// The subscript operator.
1.426 - Value operator[](Key key) const {
1.427 - return graph.row(key);
1.428 - }
1.429 -
1.430 - private:
1.431 - const GridGraph& graph;
1.432 - };
1.433 -
1.434 - /// \brief Map to get the column of the nodes.
1.435 - ///
1.436 - /// Map to get the column of the nodes.
1.437 - class ColMap {
1.438 - public:
1.439 - typedef True NeedCopy;
1.440 - /// \brief The key type of the map
1.441 - typedef GridGraph::Node Key;
1.442 - /// \brief The value type of the map
1.443 - typedef int Value;
1.444 -
1.445 - /// \brief Constructor
1.446 - ///
1.447 - /// Constructor
1.448 - ColMap(const GridGraph& _graph) : graph(_graph) {}
1.449 -
1.450 - /// \brief The subscript operator
1.451 - ///
1.452 - /// The subscript operator.
1.453 - Value operator[](Key key) const {
1.454 - return graph.col(key);
1.455 - }
1.456 -
1.457 - private:
1.458 - const GridGraph& graph;
1.459 - };
1.460 -
1.461 - /// \brief Constructor
1.462 - ///
1.463 - ///
1.464 - GridGraph(int n, int m) { construct(n, m); }
1.465 -
1.466 - /// \brief Gives back the edge goes down from the node.
1.467 - ///
1.468 - /// Gives back the edge goes down from the node. If there is not
1.469 - /// outgoing edge then it gives back INVALID.
1.470 - Edge down(Node n) const {
1.471 - UEdge ue = _down(n);
1.472 - return ue != INVALID ? direct(ue, true) : INVALID;
1.473 - }
1.474 -
1.475 - /// \brief Gives back the edge goes up from the node.
1.476 - ///
1.477 - /// Gives back the edge goes up from the node. If there is not
1.478 - /// outgoing edge then it gives back INVALID.
1.479 - Edge up(Node n) const {
1.480 - UEdge ue = _up(n);
1.481 - return ue != INVALID ? direct(ue, false) : INVALID;
1.482 - }
1.483 -
1.484 - /// \brief Gives back the edge goes right from the node.
1.485 - ///
1.486 - /// Gives back the edge goes right from the node. If there is not
1.487 - /// outgoing edge then it gives back INVALID.
1.488 - Edge right(Node n) const {
1.489 - UEdge ue = _right(n);
1.490 - return ue != INVALID ? direct(ue, true) : INVALID;
1.491 - }
1.492 -
1.493 - /// \brief Gives back the edge goes left from the node.
1.494 - ///
1.495 - /// Gives back the edge goes left from the node. If there is not
1.496 - /// outgoing edge then it gives back INVALID.
1.497 - Edge left(Node n) const {
1.498 - UEdge ue = _left(n);
1.499 - return ue != INVALID ? direct(ue, false) : INVALID;
1.500 - }
1.501 -
1.502 - };
1.503 -
1.504 - /// \brief Index map of the grid graph
1.505 - ///
1.506 - /// Just returns an IndexMap for the grid graph.
1.507 - GridGraph::IndexMap indexMap(const GridGraph& graph) {
1.508 - return GridGraph::IndexMap(graph);
1.509 - }
1.510 -
1.511 - /// \brief Row map of the grid graph
1.512 - ///
1.513 - /// Just returns a RowMap for the grid graph.
1.514 - GridGraph::RowMap rowMap(const GridGraph& graph) {
1.515 - return GridGraph::RowMap(graph);
1.516 - }
1.517 -
1.518 - /// \brief Column map of the grid graph
1.519 - ///
1.520 - /// Just returns a ColMap for the grid graph.
1.521 - GridGraph::ColMap colMap(const GridGraph& graph) {
1.522 - return GridGraph::ColMap(graph);
1.523 - }
1.524 -}
1.525 -#endif