[906] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/full_graph.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
[591] | 16 | |
---|
[921] | 17 | #ifndef LEMON_FULL_GRAPH_H |
---|
| 18 | #define LEMON_FULL_GRAPH_H |
---|
[591] | 19 | |
---|
[983] | 20 | #include <cmath> |
---|
| 21 | |
---|
[946] | 22 | |
---|
[1307] | 23 | #include <lemon/bits/iterable_graph_extender.h> |
---|
| 24 | #include <lemon/bits/alteration_notifier.h> |
---|
[1703] | 25 | #include <lemon/bits/static_map.h> |
---|
[946] | 26 | |
---|
[1566] | 27 | #include <lemon/bits/undir_graph_extender.h> |
---|
| 28 | |
---|
[977] | 29 | #include <lemon/invalid.h> |
---|
| 30 | #include <lemon/utility.h> |
---|
| 31 | |
---|
| 32 | |
---|
[591] | 33 | ///\ingroup graphs |
---|
| 34 | ///\file |
---|
[1692] | 35 | ///\brief FullGraph and UndirFullGraph classes. |
---|
[591] | 36 | |
---|
| 37 | |
---|
[921] | 38 | namespace lemon { |
---|
[591] | 39 | |
---|
[946] | 40 | class FullGraphBase { |
---|
[1566] | 41 | int _nodeNum; |
---|
| 42 | int _edgeNum; |
---|
[591] | 43 | public: |
---|
[782] | 44 | |
---|
[946] | 45 | typedef FullGraphBase Graph; |
---|
[591] | 46 | |
---|
| 47 | class Node; |
---|
| 48 | class Edge; |
---|
[782] | 49 | |
---|
[591] | 50 | public: |
---|
| 51 | |
---|
[946] | 52 | FullGraphBase() {} |
---|
| 53 | |
---|
| 54 | |
---|
[591] | 55 | ///Creates a full graph with \c n nodes. |
---|
[1566] | 56 | void construct(int n) { _nodeNum = n; _edgeNum = n * n; } |
---|
[591] | 57 | /// |
---|
[946] | 58 | // FullGraphBase(const FullGraphBase &_g) |
---|
[1566] | 59 | // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { } |
---|
[591] | 60 | |
---|
[977] | 61 | typedef True NodeNumTag; |
---|
| 62 | typedef True EdgeNumTag; |
---|
| 63 | |
---|
[813] | 64 | ///Number of nodes. |
---|
[1566] | 65 | int nodeNum() const { return _nodeNum; } |
---|
[813] | 66 | ///Number of edges. |
---|
[1566] | 67 | int edgeNum() const { return _edgeNum; } |
---|
[591] | 68 | |
---|
[813] | 69 | /// Maximum node ID. |
---|
| 70 | |
---|
| 71 | /// Maximum node ID. |
---|
| 72 | ///\sa id(Node) |
---|
[1566] | 73 | int maxId(Node = INVALID) const { return _nodeNum-1; } |
---|
[813] | 74 | /// Maximum edge ID. |
---|
| 75 | |
---|
| 76 | /// Maximum edge ID. |
---|
| 77 | ///\sa id(Edge) |
---|
[1566] | 78 | int maxId(Edge = INVALID) const { return _edgeNum-1; } |
---|
[591] | 79 | |
---|
[1566] | 80 | Node source(Edge e) const { return e.id % _nodeNum; } |
---|
| 81 | Node target(Edge e) const { return e.id / _nodeNum; } |
---|
[591] | 82 | |
---|
| 83 | |
---|
[813] | 84 | /// Node ID. |
---|
| 85 | |
---|
| 86 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 87 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 88 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 89 | /// |
---|
| 90 | /// The ID of the \ref INVALID node is -1. |
---|
| 91 | ///\return The ID of the node \c v. |
---|
[946] | 92 | |
---|
| 93 | static int id(Node v) { return v.id; } |
---|
[813] | 94 | /// Edge ID. |
---|
| 95 | |
---|
| 96 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 97 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 98 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 99 | /// |
---|
| 100 | /// The ID of the \ref INVALID edge is -1. |
---|
| 101 | ///\return The ID of the edge \c e. |
---|
[946] | 102 | static int id(Edge e) { return e.id; } |
---|
[591] | 103 | |
---|
[1106] | 104 | static Node fromId(int id, Node) { return Node(id);} |
---|
| 105 | |
---|
| 106 | static Edge fromId(int id, Edge) { return Edge(id);} |
---|
| 107 | |
---|
[1566] | 108 | typedef True FindEdgeTag; |
---|
| 109 | |
---|
[774] | 110 | /// Finds an edge between two nodes. |
---|
| 111 | |
---|
| 112 | /// Finds an edge from node \c u to node \c v. |
---|
| 113 | /// |
---|
| 114 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 115 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 116 | /// the next edge from \c u to \c v after \c prev. |
---|
| 117 | /// \return The found edge or INVALID if there is no such an edge. |
---|
[1566] | 118 | Edge findEdge(Node u,Node v, Edge prev = INVALID) const { |
---|
[946] | 119 | return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; |
---|
[774] | 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[591] | 123 | class Node { |
---|
[946] | 124 | friend class FullGraphBase; |
---|
[591] | 125 | |
---|
| 126 | protected: |
---|
[946] | 127 | int id; |
---|
[1643] | 128 | Node(int _id) : id(_id) {} |
---|
[591] | 129 | public: |
---|
| 130 | Node() {} |
---|
[1643] | 131 | Node (Invalid) : id(-1) {} |
---|
[946] | 132 | bool operator==(const Node node) const {return id == node.id;} |
---|
| 133 | bool operator!=(const Node node) const {return id != node.id;} |
---|
| 134 | bool operator<(const Node node) const {return id < node.id;} |
---|
[591] | 135 | }; |
---|
| 136 | |
---|
[946] | 137 | |
---|
| 138 | |
---|
| 139 | class Edge { |
---|
| 140 | friend class FullGraphBase; |
---|
| 141 | |
---|
| 142 | protected: |
---|
[1566] | 143 | int id; // _nodeNum * target + source; |
---|
[946] | 144 | |
---|
| 145 | Edge(int _id) : id(_id) {} |
---|
| 146 | |
---|
[986] | 147 | Edge(const FullGraphBase& _graph, int source, int target) |
---|
[1566] | 148 | : id(_graph._nodeNum * target+source) {} |
---|
[591] | 149 | public: |
---|
[946] | 150 | Edge() { } |
---|
| 151 | Edge (Invalid) { id = -1; } |
---|
| 152 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
| 153 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
| 154 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
[591] | 155 | }; |
---|
| 156 | |
---|
[946] | 157 | void first(Node& node) const { |
---|
[1566] | 158 | node.id = _nodeNum-1; |
---|
[946] | 159 | } |
---|
[591] | 160 | |
---|
[946] | 161 | static void next(Node& node) { |
---|
| 162 | --node.id; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void first(Edge& edge) const { |
---|
[1566] | 166 | edge.id = _edgeNum-1; |
---|
[946] | 167 | } |
---|
| 168 | |
---|
| 169 | static void next(Edge& edge) { |
---|
| 170 | --edge.id; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void firstOut(Edge& edge, const Node& node) const { |
---|
[1566] | 174 | edge.id = _edgeNum + node.id - _nodeNum; |
---|
[946] | 175 | } |
---|
| 176 | |
---|
| 177 | void nextOut(Edge& edge) const { |
---|
[1566] | 178 | edge.id -= _nodeNum; |
---|
[946] | 179 | if (edge.id < 0) edge.id = -1; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | void firstIn(Edge& edge, const Node& node) const { |
---|
[1566] | 183 | edge.id = node.id * _nodeNum; |
---|
[946] | 184 | } |
---|
[591] | 185 | |
---|
[946] | 186 | void nextIn(Edge& edge) const { |
---|
| 187 | ++edge.id; |
---|
[1566] | 188 | if (edge.id % _nodeNum == 0) edge.id = -1; |
---|
[946] | 189 | } |
---|
[591] | 190 | |
---|
| 191 | }; |
---|
| 192 | |
---|
[946] | 193 | |
---|
[1566] | 194 | typedef AlterableGraphExtender<FullGraphBase> |
---|
| 195 | AlterableFullGraphBase; |
---|
| 196 | typedef IterableGraphExtender<AlterableFullGraphBase> |
---|
| 197 | IterableFullGraphBase; |
---|
[1703] | 198 | typedef StaticMappableGraphExtender< |
---|
[1669] | 199 | IterableGraphExtender< |
---|
| 200 | AlterableGraphExtender<FullGraphBase> > > ExtendedFullGraphBase; |
---|
[946] | 201 | |
---|
[1566] | 202 | /// \ingroup graphs |
---|
[951] | 203 | /// |
---|
[1566] | 204 | /// \brief A full graph class. |
---|
| 205 | /// |
---|
| 206 | /// This is a simple and fast directed full graph implementation. |
---|
| 207 | /// It is completely static, so you can neither add nor delete either |
---|
| 208 | /// edges or nodes. |
---|
| 209 | /// Thus it conforms to |
---|
| 210 | /// the \ref concept::StaticGraph "StaticGraph" concept |
---|
| 211 | /// \sa concept::StaticGraph. |
---|
| 212 | /// |
---|
| 213 | /// \author Alpar Juttner |
---|
[1669] | 214 | class FullGraph : public ExtendedFullGraphBase { |
---|
[946] | 215 | public: |
---|
| 216 | |
---|
| 217 | FullGraph(int n) { construct(n); } |
---|
| 218 | }; |
---|
| 219 | |
---|
[1555] | 220 | ///@} |
---|
[983] | 221 | |
---|
| 222 | class UndirFullGraphBase { |
---|
[1566] | 223 | int _nodeNum; |
---|
| 224 | int _edgeNum; |
---|
[983] | 225 | public: |
---|
| 226 | |
---|
[984] | 227 | typedef UndirFullGraphBase Graph; |
---|
[983] | 228 | |
---|
| 229 | class Node; |
---|
| 230 | class Edge; |
---|
| 231 | |
---|
| 232 | public: |
---|
| 233 | |
---|
[984] | 234 | UndirFullGraphBase() {} |
---|
[983] | 235 | |
---|
| 236 | |
---|
| 237 | ///Creates a full graph with \c n nodes. |
---|
[1566] | 238 | void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; } |
---|
[983] | 239 | /// |
---|
| 240 | // FullGraphBase(const FullGraphBase &_g) |
---|
[1566] | 241 | // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { } |
---|
[983] | 242 | |
---|
| 243 | typedef True NodeNumTag; |
---|
| 244 | typedef True EdgeNumTag; |
---|
| 245 | |
---|
| 246 | ///Number of nodes. |
---|
[1566] | 247 | int nodeNum() const { return _nodeNum; } |
---|
[983] | 248 | ///Number of edges. |
---|
[1566] | 249 | int edgeNum() const { return _edgeNum; } |
---|
[983] | 250 | |
---|
| 251 | /// Maximum node ID. |
---|
| 252 | |
---|
| 253 | /// Maximum node ID. |
---|
| 254 | ///\sa id(Node) |
---|
[1566] | 255 | int maxId(Node = INVALID) const { return _nodeNum-1; } |
---|
[983] | 256 | /// Maximum edge ID. |
---|
| 257 | |
---|
| 258 | /// Maximum edge ID. |
---|
| 259 | ///\sa id(Edge) |
---|
[1566] | 260 | int maxId(Edge = INVALID) const { return _edgeNum-1; } |
---|
[983] | 261 | |
---|
[986] | 262 | Node source(Edge e) const { |
---|
[983] | 263 | /// \todo we may do it faster |
---|
[1643] | 264 | return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2); |
---|
[983] | 265 | } |
---|
| 266 | |
---|
[986] | 267 | Node target(Edge e) const { |
---|
| 268 | int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;; |
---|
[1643] | 269 | return Node(e.id - (source) * (source - 1) / 2); |
---|
[983] | 270 | } |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | /// Node ID. |
---|
| 274 | |
---|
| 275 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 276 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 277 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 278 | /// |
---|
| 279 | /// The ID of the \ref INVALID node is -1. |
---|
| 280 | ///\return The ID of the node \c v. |
---|
| 281 | |
---|
| 282 | static int id(Node v) { return v.id; } |
---|
| 283 | /// Edge ID. |
---|
| 284 | |
---|
| 285 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 286 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 287 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 288 | /// |
---|
| 289 | /// The ID of the \ref INVALID edge is -1. |
---|
| 290 | ///\return The ID of the edge \c e. |
---|
| 291 | static int id(Edge e) { return e.id; } |
---|
| 292 | |
---|
| 293 | /// Finds an edge between two nodes. |
---|
| 294 | |
---|
| 295 | /// Finds an edge from node \c u to node \c v. |
---|
| 296 | /// |
---|
| 297 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 298 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 299 | /// the next edge from \c u to \c v after \c prev. |
---|
| 300 | /// \return The found edge or INVALID if there is no such an edge. |
---|
[1703] | 301 | Edge findEdge(Node u, Node v, Edge prev = INVALID) const { |
---|
| 302 | if (prev.id != -1 || u.id <= v.id) return -1; |
---|
| 303 | return Edge(u.id * (u.id - 1) / 2 + v.id); |
---|
[983] | 304 | } |
---|
[1703] | 305 | |
---|
| 306 | typedef True FindEdgeTag; |
---|
[983] | 307 | |
---|
| 308 | |
---|
| 309 | class Node { |
---|
[985] | 310 | friend class UndirFullGraphBase; |
---|
[983] | 311 | |
---|
| 312 | protected: |
---|
| 313 | int id; |
---|
| 314 | Node(int _id) { id = _id;} |
---|
| 315 | public: |
---|
| 316 | Node() {} |
---|
| 317 | Node (Invalid) { id = -1; } |
---|
| 318 | bool operator==(const Node node) const {return id == node.id;} |
---|
| 319 | bool operator!=(const Node node) const {return id != node.id;} |
---|
| 320 | bool operator<(const Node node) const {return id < node.id;} |
---|
| 321 | }; |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | class Edge { |
---|
[985] | 326 | friend class UndirFullGraphBase; |
---|
[983] | 327 | |
---|
| 328 | protected: |
---|
[1566] | 329 | int id; // _nodeNum * target + source; |
---|
[983] | 330 | |
---|
| 331 | Edge(int _id) : id(_id) {} |
---|
| 332 | |
---|
| 333 | public: |
---|
| 334 | Edge() { } |
---|
| 335 | Edge (Invalid) { id = -1; } |
---|
| 336 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
| 337 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
| 338 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
| 339 | }; |
---|
| 340 | |
---|
| 341 | void first(Node& node) const { |
---|
[1703] | 342 | node.id = _nodeNum - 1; |
---|
[983] | 343 | } |
---|
| 344 | |
---|
| 345 | static void next(Node& node) { |
---|
| 346 | --node.id; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | void first(Edge& edge) const { |
---|
[1703] | 350 | edge.id = _edgeNum - 1; |
---|
[983] | 351 | } |
---|
| 352 | |
---|
| 353 | static void next(Edge& edge) { |
---|
| 354 | --edge.id; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | void firstOut(Edge& edge, const Node& node) const { |
---|
[1703] | 358 | int src = node.id; |
---|
| 359 | int trg = 0; |
---|
| 360 | edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 361 | } |
---|
| 362 | |
---|
| 363 | /// \todo with specialized iterators we can make faster iterating |
---|
[1703] | 364 | void nextOut(Edge& edge) const { |
---|
| 365 | int src = source(edge).id; |
---|
| 366 | int trg = target(edge).id; |
---|
| 367 | ++trg; |
---|
| 368 | edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 369 | } |
---|
| 370 | |
---|
| 371 | void firstIn(Edge& edge, const Node& node) const { |
---|
[1703] | 372 | int src = node.id + 1; |
---|
| 373 | int trg = node.id; |
---|
| 374 | edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 375 | } |
---|
| 376 | |
---|
[1703] | 377 | void nextIn(Edge& edge) const { |
---|
| 378 | int src = source(edge).id; |
---|
| 379 | int trg = target(edge).id; |
---|
| 380 | ++src; |
---|
| 381 | edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 382 | } |
---|
| 383 | |
---|
| 384 | }; |
---|
| 385 | |
---|
[1703] | 386 | typedef StaticMappableUndirGraphExtender< |
---|
[1669] | 387 | IterableUndirGraphExtender< |
---|
| 388 | AlterableUndirGraphExtender< |
---|
| 389 | UndirGraphExtender<UndirFullGraphBase> > > > ExtendedUndirFullGraphBase; |
---|
[1555] | 390 | |
---|
[1566] | 391 | /// \ingroup graphs |
---|
| 392 | /// |
---|
| 393 | /// \brief An undirected full graph class. |
---|
| 394 | /// |
---|
| 395 | /// This is a simple and fast directed full graph implementation. |
---|
| 396 | /// It is completely static, so you can neither add nor delete either |
---|
| 397 | /// edges or nodes. |
---|
| 398 | /// |
---|
| 399 | /// The main difference beetween the \e FullGraph and \e UndirFullGraph class |
---|
| 400 | /// is that this class conforms to the undirected graph concept and |
---|
| 401 | /// it does not contain the hook edges. |
---|
| 402 | /// |
---|
| 403 | /// \sa FullGraph |
---|
| 404 | /// |
---|
| 405 | /// \author Balazs Dezso |
---|
[1669] | 406 | class UndirFullGraph : public ExtendedUndirFullGraphBase { |
---|
[1566] | 407 | public: |
---|
| 408 | UndirFullGraph(int n) { construct(n); } |
---|
| 409 | }; |
---|
[591] | 410 | |
---|
[921] | 411 | } //namespace lemon |
---|
[591] | 412 | |
---|
| 413 | |
---|
[921] | 414 | #endif //LEMON_FULL_GRAPH_H |
---|