3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
23 #include <lemon/bits/invalid.h>
24 #include <lemon/bits/utility.h>
26 #include <lemon/bits/base_extender.h>
27 #include <lemon/bits/graph_extender.h>
29 #include <lemon/dim2.h>
33 ///\brief GridUGraph class.
37 class GridUGraphBase {
41 typedef GridUGraphBase UGraph;
52 void construct(int width, int height) {
53 _height = height; _width = width;
54 _nodeNum = height * width; _edgeNum = 2 * _nodeNum - width - height;
55 _edgeLimit = _nodeNum - width;
58 Edge _down(Node n) const {
59 if (n.id < _nodeNum - _width) {
66 Edge _up(Node n) const {
68 return Edge(n.id - _width);
74 Edge _right(Node n) const {
75 if (n.id % _width < _width - 1) {
76 return _edgeLimit + n.id % _width + (n.id / _width) * (_width - 1);
82 Edge _left(Node n) const {
83 if (n.id % _width > 0) {
84 return _edgeLimit + n.id % _width + (n.id / _width) * (_width - 1) - 1;
92 class IndexError : public RuntimeError {
94 virtual const char* what() const throw() {
95 return "lemon::GridUGraph::IndexError";
100 Node operator()(int i, int j) const {
101 LEMON_ASSERT(0 <= i && i < width() && 0 <= j &&
102 j < height(), IndexError());
103 return Node(i + j * _width);
106 int row(Node n) const {
107 return n.id / _width;
110 int col(Node n) const {
111 return n.id % _width;
122 typedef True NodeNumTag;
123 typedef True EdgeNumTag;
125 int nodeNum() const { return _nodeNum; }
126 int edgeNum() const { return _edgeNum; }
128 int maxNodeId() const { return nodeNum() - 1; }
129 int maxEdgeId() const { return edgeNum() - 1; }
131 Node source(Edge e) const {
132 if (e.id < _edgeLimit) {
135 return (e.id - _edgeLimit) % (_width - 1) +
136 (e.id - _edgeLimit) / (_width - 1) * _width;
140 Node target(Edge e) const {
141 if (e.id < _edgeLimit) {
142 return e.id + _width;
144 return (e.id - _edgeLimit) % (_width - 1) +
145 (e.id - _edgeLimit) / (_width - 1) * _width + 1;
149 static int id(Node v) { return v.id; }
150 static int id(Edge e) { return e.id; }
152 static Node nodeFromId(int id) { return Node(id);}
154 static Edge edgeFromId(int id) { return Edge(id);}
156 typedef True FindEdgeTag;
158 Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
159 if (prev != INVALID) return INVALID;
160 if (v.id - u.id == _width) return Edge(u.id);
161 if (v.id - u.id == 1 && u.id % _width < _width - 1) {
162 return Edge(u.id / _width * (_width - 1) +
163 u.id % _width + _edgeLimit);
170 friend class GridUGraphBase;
174 Node(int _id) { id = _id;}
177 Node (Invalid) { id = -1; }
178 bool operator==(const Node node) const {return id == node.id;}
179 bool operator!=(const Node node) const {return id != node.id;}
180 bool operator<(const Node node) const {return id < node.id;}
186 friend class GridUGraphBase;
191 Edge(int _id) : id(_id) {}
195 Edge (Invalid) { id = -1; }
196 bool operator==(const Edge edge) const {return id == edge.id;}
197 bool operator!=(const Edge edge) const {return id != edge.id;}
198 bool operator<(const Edge edge) const {return id < edge.id;}
201 void first(Node& node) const {
202 node.id = nodeNum() - 1;
205 static void next(Node& node) {
209 void first(Edge& edge) const {
210 edge.id = edgeNum() - 1;
213 static void next(Edge& edge) {
217 void firstOut(Edge& edge, const Node& node) const {
218 if (node.id < _nodeNum - _width) {
220 } else if (node.id % _width < _width - 1) {
221 edge.id = _edgeLimit + node.id % _width +
222 (node.id / _width) * (_width - 1);
228 void nextOut(Edge& edge) const {
229 if (edge.id >= _edgeLimit) {
231 } else if (edge.id % _width < _width - 1) {
232 edge.id = _edgeLimit + edge.id % _width +
233 (edge.id / _width) * (_width - 1);
239 void firstIn(Edge& edge, const Node& node) const {
240 if (node.id >= _width) {
241 edge.id = node.id - _width;
242 } else if (node.id % _width > 0) {
243 edge.id = _edgeLimit + node.id % _width +
244 (node.id / _width) * (_width - 1) - 1;
250 void nextIn(Edge& edge) const {
251 if (edge.id >= _edgeLimit) {
253 } else if (edge.id % _width > 0) {
254 edge.id = _edgeLimit + edge.id % _width +
255 (edge.id / _width + 1) * (_width - 1) - 1;
263 int _nodeNum, _edgeNum;
268 typedef UGraphExtender<UndirGraphExtender<GridUGraphBase> >
269 ExtendedGridUGraphBase;
273 /// \brief Grid graph class
275 /// This class implements a special graph type. The nodes of the
276 /// graph can be indiced by two integer \c (i,j) value where \c i
277 /// is in the \c [0,width) range and j is in the [0, height) range.
278 /// Two nodes are connected in the graph if the indices differ only
279 /// on one position and only one is the difference.
281 /// \image html grid_ugraph.png
282 /// \image latex grid_ugraph.eps "Grid graph" width=\textwidth
284 /// The graph can be indiced in the following way:
286 /// GridUGraph graph(w, h);
287 /// GridUGraph::NodeMap<int> val(graph);
288 /// for (int i = 0; i < graph.width(); ++i) {
289 /// for (int j = 0; j < graph.height(); ++j) {
290 /// val[graph(i, j)] = i + j;
295 /// The graph type is fully conform to the \ref concept::UGraph
296 /// "Undirected Graph" concept.
298 /// \author Balazs Dezso
299 class GridUGraph : public ExtendedGridUGraphBase {
302 typedef ExtendedGridUGraphBase Parent;
304 /// \brief Map to get the indices of the nodes as dim2::Point<int>.
306 /// Map to get the indices of the nodes as dim2::Point<int>.
309 /// \brief The key type of the map
310 typedef GridUGraph::Node Key;
311 /// \brief The value type of the map
312 typedef dim2::Point<int> Value;
314 /// \brief Constructor
317 IndexMap(const GridUGraph& _graph) : graph(_graph) {}
319 /// \brief The subscript operator
321 /// The subscript operator.
322 Value operator[](Key key) const {
323 return dim2::Point<int>(graph.row(key), graph.col(key));
327 const GridUGraph& graph;
330 /// \brief Map to get the row of the nodes.
332 /// Map to get the row of the nodes.
335 /// \brief The key type of the map
336 typedef GridUGraph::Node Key;
337 /// \brief The value type of the map
340 /// \brief Constructor
343 RowMap(const GridUGraph& _graph) : graph(_graph) {}
345 /// \brief The subscript operator
347 /// The subscript operator.
348 Value operator[](Key key) const {
349 return graph.row(key);
353 const GridUGraph& graph;
356 /// \brief Map to get the column of the nodes.
358 /// Map to get the column of the nodes.
361 /// \brief The key type of the map
362 typedef GridUGraph::Node Key;
363 /// \brief The value type of the map
366 /// \brief Constructor
369 ColMap(const GridUGraph& _graph) : graph(_graph) {}
371 /// \brief The subscript operator
373 /// The subscript operator.
374 Value operator[](Key key) const {
375 return graph.col(key);
379 const GridUGraph& graph;
382 /// \brief Constructor
385 GridUGraph(int n, int m) { construct(n, m); }
387 /// \brief Resize the graph
389 void resize(int n, int m) {
390 Parent::getNotifier(Edge()).clear();
391 Parent::getNotifier(UEdge()).clear();
392 Parent::getNotifier(Node()).clear();
394 Parent::getNotifier(Node()).build();
395 Parent::getNotifier(UEdge()).build();
396 Parent::getNotifier(Edge()).build();
399 /// \brief The node on the given position.
401 /// Gives back the node on the given position.
402 Node operator()(int i, int j) const {
403 return Parent::operator()(i, j);
406 /// \brief Gives back the row index of the node.
408 /// Gives back the row index of the node.
409 int row(Node n) const {
410 return Parent::row(n);
413 /// \brief Gives back the coloumn index of the node.
415 /// Gives back the coloumn index of the node.
416 int col(Node n) const {
417 return Parent::col(n);
420 /// \brief Gives back the width of the graph.
422 /// Gives back the width of the graph.
424 return Parent::width();
427 /// \brief Gives back the height of the graph.
429 /// Gives back the height of the graph.
431 return Parent::height();
434 /// \brief Gives back the edge goes down from the node.
436 /// Gives back the edge goes down from the node. If there is not
437 /// outgoing edge then it gives back INVALID.
438 Edge down(Node n) const {
440 return ue != INVALID ? direct(ue, true) : INVALID;
443 /// \brief Gives back the edge goes up from the node.
445 /// Gives back the edge goes up from the node. If there is not
446 /// outgoing edge then it gives back INVALID.
447 Edge up(Node n) const {
449 return ue != INVALID ? direct(ue, false) : INVALID;
452 /// \brief Gives back the edge goes right from the node.
454 /// Gives back the edge goes right from the node. If there is not
455 /// outgoing edge then it gives back INVALID.
456 Edge right(Node n) const {
457 UEdge ue = _right(n);
458 return ue != INVALID ? direct(ue, true) : INVALID;
461 /// \brief Gives back the edge goes left from the node.
463 /// Gives back the edge goes left from the node. If there is not
464 /// outgoing edge then it gives back INVALID.
465 Edge left(Node n) const {
467 return ue != INVALID ? direct(ue, false) : INVALID;
472 /// \brief Index map of the grid graph
474 /// Just returns an IndexMap for the grid graph.
475 GridUGraph::IndexMap indexMap(const GridUGraph& graph) {
476 return GridUGraph::IndexMap(graph);
479 /// \brief Row map of the grid graph
481 /// Just returns a RowMap for the grid graph.
482 GridUGraph::RowMap rowMap(const GridUGraph& graph) {
483 return GridUGraph::RowMap(graph);
486 /// \brief Column map of the grid graph
488 /// Just returns a ColMap for the grid graph.
489 GridUGraph::ColMap colMap(const GridUGraph& graph) {
490 return GridUGraph::ColMap(graph);