lemon/grid_graph.h
author deba
Mon, 12 Sep 2005 11:24:54 +0000
changeset 1681 84e43c7ca1e3
parent 1623 c3defc3590aa
child 1693 269f0cbfbcc8
permissions -rw-r--r--
SubGraphAdaptors with edge checking functionality.

Improved grid_graph_demo
deba@1623
     1
/* -*- C++ -*-
deba@1623
     2
 * lemon/grid_graph.h - Part of LEMON, a generic C++ optimization library
deba@1623
     3
 *
deba@1623
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1623
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1623
     6
 *
deba@1623
     7
 * Permission to use, modify and distribute this software is granted
deba@1623
     8
 * provided that this copyright notice appears in all copies. For
deba@1623
     9
 * precise terms see the accompanying LICENSE file.
deba@1623
    10
 *
deba@1623
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1623
    12
 * express or implied, and with no claim as to its suitability for any
deba@1623
    13
 * purpose.
deba@1623
    14
 *
deba@1623
    15
 */
deba@1623
    16
deba@1623
    17
#ifndef GRID_GRAPH_H
deba@1623
    18
#define GRID_GRAPH_H
deba@1623
    19
deba@1623
    20
#include <iostream>
deba@1623
    21
#include <lemon/invalid.h>
deba@1623
    22
#include <lemon/utility.h>
deba@1623
    23
deba@1623
    24
#include <lemon/bits/iterable_graph_extender.h>
deba@1623
    25
#include <lemon/bits/alteration_notifier.h>
deba@1623
    26
#include <lemon/bits/default_map.h>
deba@1623
    27
deba@1623
    28
#include <lemon/bits/undir_graph_extender.h>
deba@1623
    29
deba@1623
    30
namespace lemon {
deba@1623
    31
deba@1623
    32
  class GridGraphBase {
deba@1623
    33
deba@1623
    34
  public:
deba@1623
    35
deba@1623
    36
    typedef GridGraphBase Graph;
deba@1623
    37
deba@1623
    38
    class Node;
deba@1623
    39
    class Edge;
deba@1623
    40
deba@1623
    41
  public:
deba@1623
    42
deba@1623
    43
    GridGraphBase() {}
deba@1623
    44
deba@1623
    45
  protected:
deba@1623
    46
deba@1623
    47
    /// \brief Creates a grid graph with the given size.
deba@1623
    48
    ///
deba@1623
    49
    /// Creates a grid graph with the given size.
deba@1680
    50
    void construct(int width, int height) {
deba@1623
    51
      _height = height; _width = width;
deba@1623
    52
      _nodeNum = height * width; _edgeNum = 2 * _nodeNum - width - height;
deba@1623
    53
      _edgeLimit = _nodeNum - width;
deba@1623
    54
    }
deba@1623
    55
deba@1680
    56
    /// \brief Gives back the edge goes down from the node.
deba@1680
    57
    ///
deba@1680
    58
    /// Gives back the edge goes down from the node. If there is not
deba@1680
    59
    /// outgoing edge then it gives back INVALID.
deba@1680
    60
    Edge _down(Node n) const {
deba@1680
    61
      if (n.id < _nodeNum - _width) {
deba@1680
    62
	return Edge(n.id);
deba@1680
    63
      } else {
deba@1680
    64
	return INVALID;
deba@1680
    65
      }
deba@1680
    66
    }
deba@1680
    67
deba@1680
    68
    /// \brief Gives back the edge comes from up into the node.
deba@1680
    69
    ///
deba@1680
    70
    /// Gives back the edge comes from up into the node. If there is not
deba@1680
    71
    /// incoming edge then it gives back INVALID.
deba@1680
    72
    Edge _up(Node n) const {
deba@1680
    73
      if (n.id >= _width) {
deba@1680
    74
	return Edge(n.id - _width);
deba@1680
    75
      } else {
deba@1680
    76
	return INVALID;
deba@1680
    77
      }
deba@1680
    78
    }
deba@1680
    79
deba@1680
    80
    /// \brief Gives back the edge goes right from the node.
deba@1680
    81
    ///
deba@1680
    82
    /// Gives back the edge goes right from the node. If there is not
deba@1680
    83
    /// outgoing edge then it gives back INVALID.
deba@1680
    84
    Edge _right(Node n) const {
deba@1680
    85
      if (n.id % _width < _width - 1) {
deba@1680
    86
	return _edgeLimit + n.id % _width + (n.id / _width) * (_width - 1);
deba@1680
    87
      } else {
deba@1680
    88
	return INVALID;
deba@1680
    89
      }
deba@1680
    90
    }
deba@1680
    91
deba@1680
    92
    /// \brief Gives back the edge comes from left into the node.
deba@1680
    93
    ///
deba@1680
    94
    /// Gives back the edge comes left up into the node. If there is not
deba@1680
    95
    /// incoming edge then it gives back INVALID.
deba@1680
    96
    Edge _left(Node n) const {
deba@1680
    97
      if (n.id % _width > 0) {
deba@1680
    98
	return _edgeLimit + n.id % _width + (n.id / _width) * (_width - 1) - 1;
deba@1680
    99
      } else {
deba@1680
   100
	return INVALID;
deba@1680
   101
      }
deba@1680
   102
    }
deba@1680
   103
deba@1680
   104
deba@1623
   105
  public:
deba@1623
   106
    
deba@1623
   107
    /// \brief The node on the given position.
deba@1623
   108
    /// 
deba@1623
   109
    /// Gives back the node on the given position.
deba@1623
   110
    Node operator()(int i, int j) const {
deba@1680
   111
      return Node(i + j * _width);
deba@1623
   112
    }
deba@1623
   113
deba@1623
   114
    /// \brief Gives back the row index of the node.
deba@1623
   115
    ///
deba@1623
   116
    /// Gives back the row index of the node.
deba@1623
   117
    int row(Node n) const {
deba@1623
   118
      return n.id / _width;
deba@1623
   119
    }
deba@1623
   120
    
deba@1623
   121
    /// \brief Gives back the coloumn index of the node.
deba@1623
   122
    ///
deba@1623
   123
    /// Gives back the coloumn index of the node.
deba@1680
   124
    int col(Node n) const {
deba@1623
   125
      return n.id % _width;    
deba@1623
   126
    }
deba@1623
   127
deba@1623
   128
    /// \brief Gives back the width of the graph.
deba@1623
   129
    ///
deba@1623
   130
    /// Gives back the width of the graph.
deba@1623
   131
    int width() const {
deba@1623
   132
      return _width;
deba@1623
   133
    }
deba@1623
   134
deba@1623
   135
    /// \brief Gives back the height of the graph.
deba@1623
   136
    ///
deba@1623
   137
    /// Gives back the height of the graph.
deba@1623
   138
    int height() const {
deba@1623
   139
      return _height;
deba@1623
   140
    }
deba@1623
   141
deba@1623
   142
    typedef True NodeNumTag;
deba@1623
   143
    typedef True EdgeNumTag;
deba@1623
   144
deba@1623
   145
    ///Number of nodes.
deba@1623
   146
    int nodeNum() const { return _nodeNum; }
deba@1623
   147
    ///Number of edges.
deba@1623
   148
    int edgeNum() const { return _edgeNum; }
deba@1623
   149
deba@1623
   150
    /// Maximum node ID.
deba@1623
   151
    
deba@1623
   152
    /// Maximum node ID.
deba@1623
   153
    ///\sa id(Node)
deba@1623
   154
    int maxId(Node = INVALID) const { return nodeNum() - 1; }
deba@1623
   155
    /// Maximum edge ID.
deba@1623
   156
    
deba@1623
   157
    /// Maximum edge ID.
deba@1623
   158
    ///\sa id(Edge)
deba@1623
   159
    int maxId(Edge = INVALID) const { return edgeNum() - 1; }
deba@1623
   160
deba@1623
   161
    /// \brief Gives back the source node of an edge.
deba@1623
   162
    ///    
deba@1623
   163
    /// Gives back the source node of an edge.
deba@1623
   164
    Node source(Edge e) const {
deba@1623
   165
      if (e.id < _edgeLimit) {
deba@1623
   166
	return e.id;
deba@1623
   167
      } else {
deba@1623
   168
	return (e.id - _edgeLimit) % (_width - 1) +
deba@1623
   169
	  (e.id - _edgeLimit) / (_width - 1) * _width;
deba@1623
   170
      }
deba@1623
   171
    }
deba@1623
   172
deba@1623
   173
    /// \brief Gives back the target node of an edge.
deba@1623
   174
    ///    
deba@1623
   175
    /// Gives back the target node of an edge.
deba@1623
   176
    Node target(Edge e) const {
deba@1623
   177
      if (e.id < _edgeLimit) {
deba@1623
   178
	return e.id + _width;
deba@1623
   179
      } else {
deba@1623
   180
	return (e.id - _edgeLimit) % (_width - 1) +
deba@1623
   181
	  (e.id - _edgeLimit) / (_width - 1) * _width + 1;
deba@1623
   182
      }
deba@1623
   183
    }
deba@1623
   184
deba@1623
   185
    /// Node ID.
deba@1623
   186
    
deba@1623
   187
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@1623
   188
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@1623
   189
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@1623
   190
    ///
deba@1623
   191
    /// The ID of the \ref INVALID node is -1.
deba@1623
   192
    ///\return The ID of the node \c v. 
deba@1623
   193
deba@1623
   194
    static int id(Node v) { return v.id; }
deba@1623
   195
    /// Edge ID.
deba@1623
   196
    
deba@1623
   197
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@1623
   198
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@1623
   199
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@1623
   200
    ///
deba@1623
   201
    /// The ID of the \ref INVALID edge is -1.
deba@1623
   202
    ///\return The ID of the edge \c e. 
deba@1623
   203
    static int id(Edge e) { return e.id; }
deba@1623
   204
deba@1623
   205
    static Node fromId(int id, Node) { return Node(id);}
deba@1623
   206
    
deba@1623
   207
    static Edge fromId(int id, Edge) { return Edge(id);}
deba@1623
   208
deba@1623
   209
    typedef True FindEdgeTag;
deba@1623
   210
deba@1623
   211
    /// Finds an edge between two nodes.
deba@1623
   212
    
deba@1623
   213
    /// Finds an edge from node \c u to node \c v.
deba@1623
   214
    ///
deba@1623
   215
    /// If \c prev is \ref INVALID (this is the default value), then
deba@1623
   216
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@1623
   217
    /// the next edge from \c u to \c v after \c prev.
deba@1623
   218
    /// \return The found edge or INVALID if there is no such an edge.
deba@1623
   219
    Edge findEdge(Node u, Node v, Edge prev = INVALID) {
deba@1623
   220
      if (prev != INVALID) return INVALID;
deba@1623
   221
      if (v.id - u.id == _width) return Edge(u.id);
deba@1623
   222
      if (v.id - u.id == 1 && u.id % _width < _width - 1) {
deba@1623
   223
	return Edge(u.id / _width * (_width - 1) +
deba@1623
   224
		    u.id % _width + _edgeLimit);
deba@1623
   225
      }
deba@1623
   226
      return INVALID;
deba@1623
   227
    }
deba@1623
   228
    
deba@1623
   229
      
deba@1623
   230
    class Node {
deba@1623
   231
      friend class GridGraphBase;
deba@1623
   232
deba@1623
   233
    protected:
deba@1623
   234
      int id;
deba@1623
   235
      Node(int _id) { id = _id;}
deba@1623
   236
    public:
deba@1623
   237
      Node() {}
deba@1623
   238
      Node (Invalid) { id = -1; }
deba@1623
   239
      bool operator==(const Node node) const {return id == node.id;}
deba@1623
   240
      bool operator!=(const Node node) const {return id != node.id;}
deba@1623
   241
      bool operator<(const Node node) const {return id < node.id;}
deba@1623
   242
    };
deba@1623
   243
    
deba@1623
   244
deba@1623
   245
deba@1623
   246
    class Edge {
deba@1623
   247
      friend class GridGraphBase;
deba@1623
   248
      
deba@1623
   249
    protected:
deba@1623
   250
      int id; 
deba@1623
   251
deba@1623
   252
      Edge(int _id) : id(_id) {}
deba@1623
   253
deba@1623
   254
    public:
deba@1623
   255
      Edge() { }
deba@1623
   256
      Edge (Invalid) { id = -1; }
deba@1623
   257
      bool operator==(const Edge edge) const {return id == edge.id;}
deba@1623
   258
      bool operator!=(const Edge edge) const {return id != edge.id;}
deba@1623
   259
      bool operator<(const Edge edge) const {return id < edge.id;}
deba@1623
   260
    };
deba@1623
   261
deba@1623
   262
    void first(Node& node) const {
deba@1623
   263
      node.id = nodeNum() - 1;
deba@1623
   264
    }
deba@1623
   265
deba@1623
   266
    static void next(Node& node) {
deba@1623
   267
      --node.id;
deba@1623
   268
    }
deba@1623
   269
deba@1623
   270
    void first(Edge& edge) const {
deba@1623
   271
      edge.id = edgeNum() - 1;
deba@1623
   272
    }
deba@1623
   273
deba@1623
   274
    static void next(Edge& edge) {
deba@1623
   275
      --edge.id;
deba@1623
   276
    }
deba@1623
   277
deba@1623
   278
    void firstOut(Edge& edge, const Node& node) const {
deba@1623
   279
      if (node.id < _nodeNum - _width) {
deba@1623
   280
	edge.id = node.id;
deba@1623
   281
      } else if (node.id % _width < _width - 1) {
deba@1623
   282
	edge.id = _edgeLimit + node.id % _width +
deba@1623
   283
	  (node.id / _width) * (_width - 1);
deba@1623
   284
      } else {
deba@1623
   285
	edge.id = -1;
deba@1623
   286
      }
deba@1623
   287
    }
deba@1623
   288
deba@1623
   289
    void nextOut(Edge& edge) const {
deba@1623
   290
      if (edge.id >= _edgeLimit) {
deba@1623
   291
	edge.id = -1;
deba@1623
   292
      } else if (edge.id % _width < _width - 1) {
deba@1623
   293
	edge.id = _edgeLimit + edge.id % _width +
deba@1623
   294
	  (edge.id / _width) * (_width - 1);
deba@1623
   295
      } else {
deba@1623
   296
	edge.id = -1;
deba@1623
   297
      }
deba@1623
   298
    }
deba@1623
   299
deba@1623
   300
    void firstIn(Edge& edge, const Node& node) const {
deba@1623
   301
      if (node.id >= _width) {
deba@1623
   302
	edge.id = node.id - _width;
deba@1623
   303
      } else if (node.id % _width > 0) {
deba@1623
   304
	edge.id = _edgeLimit + node.id % _width +
deba@1623
   305
	  (node.id / _width) * (_width - 1) - 1;
deba@1623
   306
      } else {
deba@1623
   307
	edge.id = -1;
deba@1623
   308
      }
deba@1623
   309
    }
deba@1623
   310
    
deba@1623
   311
    void nextIn(Edge& edge) const {
deba@1623
   312
      if (edge.id >= _edgeLimit) {
deba@1623
   313
	edge.id = -1;
deba@1623
   314
      } else if (edge.id % _width > 0) {
deba@1623
   315
	edge.id = _edgeLimit + edge.id % _width +
deba@1623
   316
	  (edge.id / _width + 1) * (_width - 1) - 1;
deba@1623
   317
      } else {
deba@1623
   318
	edge.id = -1;
deba@1623
   319
      }
deba@1623
   320
    }
deba@1623
   321
deba@1623
   322
  private:
deba@1623
   323
    int _width, _height;
deba@1623
   324
    int _nodeNum, _edgeNum;
deba@1623
   325
    int _edgeLimit;
deba@1623
   326
  };
deba@1623
   327
deba@1623
   328
deba@1680
   329
  typedef MappableUndirGraphExtender<
deba@1680
   330
    IterableUndirGraphExtender<
deba@1680
   331
    AlterableUndirGraphExtender<
deba@1680
   332
    UndirGraphExtender<GridGraphBase> > > > ExtendedGridGraphBase;
deba@1623
   333
deba@1623
   334
  /// \ingroup graphs
deba@1623
   335
  ///
deba@1623
   336
  /// \brief Grid graph class
deba@1623
   337
  ///
deba@1623
   338
  /// This class implements a special graph type. The nodes of the
deba@1623
   339
  /// graph can be indiced by two integer \c (i,j) value where \c i
deba@1623
   340
  /// is in the \c [0,height) range and j is in the [0, width) range.
deba@1623
   341
  /// Two nodes are connected in the graph if the indices differ only
deba@1623
   342
  /// on one position and only one is the difference. 
deba@1623
   343
  ///
deba@1623
   344
  /// The graph can be indiced in the following way:
deba@1623
   345
  /// \code
deba@1623
   346
  /// GridGraph graph(h, w);
deba@1623
   347
  /// GridGraph::NodeMap<int> val(graph); 
deba@1623
   348
  /// for (int i = 0; i < graph.height(); ++i) {
deba@1623
   349
  ///   for (int j = 0; j < graph.width(); ++j) {
deba@1623
   350
  ///     val[graph(i, j)] = i + j;
deba@1623
   351
  ///   }
deba@1623
   352
  /// }
deba@1623
   353
  /// \endcode
deba@1623
   354
  ///
deba@1623
   355
  /// The graph type is fully conform to the \ref concept::UndirGraph
deba@1623
   356
  /// "Undirected Graph" concept.
deba@1623
   357
  ///
deba@1623
   358
  /// \author Balazs Dezso
deba@1680
   359
  class GridGraph : public ExtendedGridGraphBase {
deba@1623
   360
  public:
deba@1623
   361
    
deba@1680
   362
    GridGraph(int n, int m) { construct(n, m); }
deba@1680
   363
    
deba@1680
   364
    /// \brief Gives back the edge goes down from the node.
deba@1680
   365
    ///
deba@1680
   366
    /// Gives back the edge goes down from the node. If there is not
deba@1680
   367
    /// outgoing edge then it gives back INVALID.
deba@1680
   368
    Edge down(Node n) const {
deba@1680
   369
      UndirEdge ue = _down(n);
deba@1680
   370
      return ue != INVALID ? direct(ue, true) : INVALID;
deba@1680
   371
    }
deba@1680
   372
    
deba@1680
   373
    /// \brief Gives back the edge goes up from the node.
deba@1680
   374
    ///
deba@1680
   375
    /// Gives back the edge goes up from the node. If there is not
deba@1680
   376
    /// outgoing edge then it gives back INVALID.
deba@1680
   377
    Edge up(Node n) const {
deba@1680
   378
      UndirEdge ue = _up(n);
deba@1680
   379
      return ue != INVALID ? direct(ue, false) : INVALID;
deba@1680
   380
    }
deba@1680
   381
deba@1680
   382
    /// \brief Gives back the edge goes right from the node.
deba@1680
   383
    ///
deba@1680
   384
    /// Gives back the edge goes right from the node. If there is not
deba@1680
   385
    /// outgoing edge then it gives back INVALID.
deba@1680
   386
    Edge right(Node n) const {
deba@1680
   387
      UndirEdge ue = _right(n);
deba@1680
   388
      return ue != INVALID ? direct(ue, true) : INVALID;
deba@1680
   389
    }
deba@1680
   390
deba@1680
   391
    /// \brief Gives back the edge goes left from the node.
deba@1680
   392
    ///
deba@1680
   393
    /// Gives back the edge goes left from the node. If there is not
deba@1680
   394
    /// outgoing edge then it gives back INVALID.
deba@1680
   395
    Edge left(Node n) const {
deba@1680
   396
      UndirEdge ue = _left(n);
deba@1680
   397
      return ue != INVALID ? direct(ue, false) : INVALID;
deba@1680
   398
    }
deba@1680
   399
    
deba@1623
   400
  };
deba@1623
   401
}
deba@1623
   402
#endif