[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 8 | * |
---|
| 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. |
---|
| 12 | * |
---|
| 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 |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
[591] | 18 | |
---|
[921] | 19 | #ifndef LEMON_FULL_GRAPH_H |
---|
| 20 | #define LEMON_FULL_GRAPH_H |
---|
[591] | 21 | |
---|
[983] | 22 | #include <cmath> |
---|
| 23 | |
---|
[946] | 24 | |
---|
[1791] | 25 | #include <lemon/bits/graph_extender.h> |
---|
[1566] | 26 | |
---|
[1979] | 27 | |
---|
[977] | 28 | #include <lemon/invalid.h> |
---|
| 29 | #include <lemon/utility.h> |
---|
| 30 | |
---|
| 31 | |
---|
[591] | 32 | ///\ingroup graphs |
---|
| 33 | ///\file |
---|
[1909] | 34 | ///\brief FullGraph and FullUGraph classes. |
---|
[591] | 35 | |
---|
| 36 | |
---|
[921] | 37 | namespace lemon { |
---|
[591] | 38 | |
---|
[1986] | 39 | /// \brief Base of the FullGrpah. |
---|
| 40 | /// |
---|
| 41 | /// Base of the FullGrpah. |
---|
[946] | 42 | class FullGraphBase { |
---|
[1566] | 43 | int _nodeNum; |
---|
| 44 | int _edgeNum; |
---|
[591] | 45 | public: |
---|
[782] | 46 | |
---|
[946] | 47 | typedef FullGraphBase Graph; |
---|
[591] | 48 | |
---|
| 49 | class Node; |
---|
| 50 | class Edge; |
---|
[782] | 51 | |
---|
[591] | 52 | public: |
---|
| 53 | |
---|
[946] | 54 | FullGraphBase() {} |
---|
| 55 | |
---|
| 56 | |
---|
[591] | 57 | ///Creates a full graph with \c n nodes. |
---|
[1566] | 58 | void construct(int n) { _nodeNum = n; _edgeNum = n * n; } |
---|
[591] | 59 | |
---|
[977] | 60 | typedef True NodeNumTag; |
---|
| 61 | typedef True EdgeNumTag; |
---|
| 62 | |
---|
[1986] | 63 | /// \brief Returns the node with the given index. |
---|
| 64 | /// |
---|
| 65 | /// Returns the node with the given index. Because it is a |
---|
| 66 | /// static size graph the node's of the graph can be indiced |
---|
| 67 | /// by the range from 0 to \e nodeNum()-1 and the index of |
---|
| 68 | /// the node can accessed by the \e index() member. |
---|
| 69 | Node operator()(int index) const { return Node(index); } |
---|
| 70 | |
---|
| 71 | /// \brief Returns the index of the node. |
---|
| 72 | /// |
---|
| 73 | /// Returns the index of the node. Because it is a |
---|
| 74 | /// static size graph the node's of the graph can be indiced |
---|
| 75 | /// by the range from 0 to \e nodeNum()-1 and the index of |
---|
| 76 | /// the node can accessed by the \e index() member. |
---|
| 77 | int index(const Node& node) const { return node.id; } |
---|
| 78 | |
---|
[813] | 79 | ///Number of nodes. |
---|
[1566] | 80 | int nodeNum() const { return _nodeNum; } |
---|
[813] | 81 | ///Number of edges. |
---|
[1566] | 82 | int edgeNum() const { return _edgeNum; } |
---|
[591] | 83 | |
---|
[813] | 84 | /// Maximum node ID. |
---|
| 85 | |
---|
| 86 | /// Maximum node ID. |
---|
| 87 | ///\sa id(Node) |
---|
[1791] | 88 | int maxNodeId() const { return _nodeNum-1; } |
---|
[813] | 89 | /// Maximum edge ID. |
---|
| 90 | |
---|
| 91 | /// Maximum edge ID. |
---|
| 92 | ///\sa id(Edge) |
---|
[1791] | 93 | int maxEdgeId() const { return _edgeNum-1; } |
---|
[591] | 94 | |
---|
[1566] | 95 | Node source(Edge e) const { return e.id % _nodeNum; } |
---|
| 96 | Node target(Edge e) const { return e.id / _nodeNum; } |
---|
[591] | 97 | |
---|
| 98 | |
---|
[813] | 99 | /// Node ID. |
---|
| 100 | |
---|
| 101 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 102 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 103 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 104 | /// |
---|
| 105 | /// The ID of the \ref INVALID node is -1. |
---|
| 106 | ///\return The ID of the node \c v. |
---|
[946] | 107 | |
---|
| 108 | static int id(Node v) { return v.id; } |
---|
[813] | 109 | /// Edge ID. |
---|
| 110 | |
---|
| 111 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 112 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 113 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 114 | /// |
---|
| 115 | /// The ID of the \ref INVALID edge is -1. |
---|
| 116 | ///\return The ID of the edge \c e. |
---|
[946] | 117 | static int id(Edge e) { return e.id; } |
---|
[591] | 118 | |
---|
[1791] | 119 | static Node nodeFromId(int id) { return Node(id);} |
---|
[1106] | 120 | |
---|
[1791] | 121 | static Edge edgeFromId(int id) { return Edge(id);} |
---|
[1106] | 122 | |
---|
[1566] | 123 | typedef True FindEdgeTag; |
---|
| 124 | |
---|
[774] | 125 | /// Finds an edge between two nodes. |
---|
| 126 | |
---|
| 127 | /// Finds an edge from node \c u to node \c v. |
---|
| 128 | /// |
---|
| 129 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 130 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 131 | /// the next edge from \c u to \c v after \c prev. |
---|
| 132 | /// \return The found edge or INVALID if there is no such an edge. |
---|
[1566] | 133 | Edge findEdge(Node u,Node v, Edge prev = INVALID) const { |
---|
[946] | 134 | return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; |
---|
[774] | 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
[591] | 138 | class Node { |
---|
[946] | 139 | friend class FullGraphBase; |
---|
[591] | 140 | |
---|
| 141 | protected: |
---|
[946] | 142 | int id; |
---|
[1643] | 143 | Node(int _id) : id(_id) {} |
---|
[591] | 144 | public: |
---|
| 145 | Node() {} |
---|
[1643] | 146 | Node (Invalid) : id(-1) {} |
---|
[946] | 147 | bool operator==(const Node node) const {return id == node.id;} |
---|
| 148 | bool operator!=(const Node node) const {return id != node.id;} |
---|
| 149 | bool operator<(const Node node) const {return id < node.id;} |
---|
[591] | 150 | }; |
---|
| 151 | |
---|
[946] | 152 | |
---|
| 153 | |
---|
| 154 | class Edge { |
---|
| 155 | friend class FullGraphBase; |
---|
| 156 | |
---|
| 157 | protected: |
---|
[1566] | 158 | int id; // _nodeNum * target + source; |
---|
[946] | 159 | |
---|
| 160 | Edge(int _id) : id(_id) {} |
---|
| 161 | |
---|
[986] | 162 | Edge(const FullGraphBase& _graph, int source, int target) |
---|
[1566] | 163 | : id(_graph._nodeNum * target+source) {} |
---|
[591] | 164 | public: |
---|
[946] | 165 | Edge() { } |
---|
| 166 | Edge (Invalid) { id = -1; } |
---|
| 167 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
| 168 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
| 169 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
[591] | 170 | }; |
---|
| 171 | |
---|
[946] | 172 | void first(Node& node) const { |
---|
[1566] | 173 | node.id = _nodeNum-1; |
---|
[946] | 174 | } |
---|
[591] | 175 | |
---|
[946] | 176 | static void next(Node& node) { |
---|
| 177 | --node.id; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | void first(Edge& edge) const { |
---|
[1566] | 181 | edge.id = _edgeNum-1; |
---|
[946] | 182 | } |
---|
| 183 | |
---|
| 184 | static void next(Edge& edge) { |
---|
| 185 | --edge.id; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void firstOut(Edge& edge, const Node& node) const { |
---|
[1566] | 189 | edge.id = _edgeNum + node.id - _nodeNum; |
---|
[946] | 190 | } |
---|
| 191 | |
---|
| 192 | void nextOut(Edge& edge) const { |
---|
[1566] | 193 | edge.id -= _nodeNum; |
---|
[946] | 194 | if (edge.id < 0) edge.id = -1; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | void firstIn(Edge& edge, const Node& node) const { |
---|
[1566] | 198 | edge.id = node.id * _nodeNum; |
---|
[946] | 199 | } |
---|
[591] | 200 | |
---|
[946] | 201 | void nextIn(Edge& edge) const { |
---|
| 202 | ++edge.id; |
---|
[1566] | 203 | if (edge.id % _nodeNum == 0) edge.id = -1; |
---|
[946] | 204 | } |
---|
[591] | 205 | |
---|
| 206 | }; |
---|
| 207 | |
---|
[1979] | 208 | typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase; |
---|
[946] | 209 | |
---|
[1566] | 210 | /// \ingroup graphs |
---|
[951] | 211 | /// |
---|
[1566] | 212 | /// \brief A full graph class. |
---|
| 213 | /// |
---|
| 214 | /// This is a simple and fast directed full graph implementation. |
---|
| 215 | /// It is completely static, so you can neither add nor delete either |
---|
| 216 | /// edges or nodes. |
---|
| 217 | /// Thus it conforms to |
---|
| 218 | /// the \ref concept::StaticGraph "StaticGraph" concept |
---|
| 219 | /// \sa concept::StaticGraph. |
---|
| 220 | /// |
---|
[1986] | 221 | /// \sa FullGraphBase |
---|
| 222 | /// \sa FullUGraph |
---|
| 223 | /// |
---|
[1566] | 224 | /// \author Alpar Juttner |
---|
[1669] | 225 | class FullGraph : public ExtendedFullGraphBase { |
---|
[946] | 226 | public: |
---|
| 227 | |
---|
[1979] | 228 | typedef ExtendedFullGraphBase Parent; |
---|
| 229 | |
---|
| 230 | /// \brief Constructor |
---|
| 231 | /// |
---|
[946] | 232 | FullGraph(int n) { construct(n); } |
---|
[1979] | 233 | |
---|
| 234 | /// \brief Resize the graph |
---|
| 235 | /// |
---|
[1986] | 236 | /// Resize the graph. The function will fully destroy and build the graph. |
---|
| 237 | /// This cause that the maps of the graph will reallocated |
---|
| 238 | /// automatically and the previous values will be lost. |
---|
[1979] | 239 | void resize(int n) { |
---|
| 240 | Parent::getNotifier(Edge()).clear(); |
---|
| 241 | Parent::getNotifier(Node()).clear(); |
---|
| 242 | construct(n); |
---|
| 243 | Parent::getNotifier(Node()).build(); |
---|
| 244 | Parent::getNotifier(Edge()).build(); |
---|
| 245 | } |
---|
[946] | 246 | }; |
---|
| 247 | |
---|
[983] | 248 | |
---|
[1986] | 249 | /// \brief Base of the FullUGrpah. |
---|
| 250 | /// |
---|
| 251 | /// Base of the FullUGrpah. |
---|
[1909] | 252 | class FullUGraphBase { |
---|
[1566] | 253 | int _nodeNum; |
---|
| 254 | int _edgeNum; |
---|
[983] | 255 | public: |
---|
| 256 | |
---|
[1909] | 257 | typedef FullUGraphBase Graph; |
---|
[983] | 258 | |
---|
| 259 | class Node; |
---|
| 260 | class Edge; |
---|
| 261 | |
---|
| 262 | public: |
---|
| 263 | |
---|
[1909] | 264 | FullUGraphBase() {} |
---|
[983] | 265 | |
---|
| 266 | |
---|
| 267 | ///Creates a full graph with \c n nodes. |
---|
[1566] | 268 | void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; } |
---|
[1986] | 269 | |
---|
| 270 | /// \brief Returns the node with the given index. |
---|
[983] | 271 | /// |
---|
[1986] | 272 | /// Returns the node with the given index. Because it is a |
---|
| 273 | /// static size graph the node's of the graph can be indiced |
---|
| 274 | /// by the range from 0 to \e nodeNum()-1 and the index of |
---|
| 275 | /// the node can accessed by the \e index() member. |
---|
| 276 | Node operator()(int index) const { return Node(index); } |
---|
| 277 | |
---|
| 278 | /// \brief Returns the index of the node. |
---|
| 279 | /// |
---|
| 280 | /// Returns the index of the node. Because it is a |
---|
| 281 | /// static size graph the node's of the graph can be indiced |
---|
| 282 | /// by the range from 0 to \e nodeNum()-1 and the index of |
---|
| 283 | /// the node can accessed by the \e index() member. |
---|
| 284 | int index(const Node& node) const { return node.id; } |
---|
| 285 | |
---|
[983] | 286 | typedef True NodeNumTag; |
---|
| 287 | typedef True EdgeNumTag; |
---|
| 288 | |
---|
| 289 | ///Number of nodes. |
---|
[1566] | 290 | int nodeNum() const { return _nodeNum; } |
---|
[983] | 291 | ///Number of edges. |
---|
[1566] | 292 | int edgeNum() const { return _edgeNum; } |
---|
[983] | 293 | |
---|
| 294 | /// Maximum node ID. |
---|
| 295 | |
---|
| 296 | /// Maximum node ID. |
---|
| 297 | ///\sa id(Node) |
---|
[1791] | 298 | int maxNodeId() const { return _nodeNum-1; } |
---|
[983] | 299 | /// Maximum edge ID. |
---|
| 300 | |
---|
| 301 | /// Maximum edge ID. |
---|
| 302 | ///\sa id(Edge) |
---|
[1791] | 303 | int maxEdgeId() const { return _edgeNum-1; } |
---|
[983] | 304 | |
---|
[986] | 305 | Node source(Edge e) const { |
---|
[983] | 306 | /// \todo we may do it faster |
---|
[1643] | 307 | return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2); |
---|
[983] | 308 | } |
---|
| 309 | |
---|
[986] | 310 | Node target(Edge e) const { |
---|
| 311 | int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;; |
---|
[1643] | 312 | return Node(e.id - (source) * (source - 1) / 2); |
---|
[983] | 313 | } |
---|
| 314 | |
---|
| 315 | |
---|
[1986] | 316 | /// \brief Node ID. |
---|
| 317 | /// |
---|
[983] | 318 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 319 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 320 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 321 | /// |
---|
| 322 | /// The ID of the \ref INVALID node is -1. |
---|
[1986] | 323 | /// \return The ID of the node \c v. |
---|
[983] | 324 | |
---|
| 325 | static int id(Node v) { return v.id; } |
---|
[1986] | 326 | |
---|
| 327 | /// \brief Edge ID. |
---|
| 328 | /// |
---|
[983] | 329 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 330 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 331 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 332 | /// |
---|
| 333 | /// The ID of the \ref INVALID edge is -1. |
---|
| 334 | ///\return The ID of the edge \c e. |
---|
| 335 | static int id(Edge e) { return e.id; } |
---|
| 336 | |
---|
[1986] | 337 | /// \brief Finds an edge between two nodes. |
---|
| 338 | /// |
---|
[983] | 339 | /// Finds an edge from node \c u to node \c v. |
---|
| 340 | /// |
---|
| 341 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 342 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 343 | /// the next edge from \c u to \c v after \c prev. |
---|
| 344 | /// \return The found edge or INVALID if there is no such an edge. |
---|
[1703] | 345 | Edge findEdge(Node u, Node v, Edge prev = INVALID) const { |
---|
[1986] | 346 | if (prev.id != -1 || u.id <= v.id) return Edge(-1); |
---|
[1703] | 347 | return Edge(u.id * (u.id - 1) / 2 + v.id); |
---|
[983] | 348 | } |
---|
[1703] | 349 | |
---|
| 350 | typedef True FindEdgeTag; |
---|
[983] | 351 | |
---|
| 352 | |
---|
| 353 | class Node { |
---|
[1909] | 354 | friend class FullUGraphBase; |
---|
[983] | 355 | |
---|
| 356 | protected: |
---|
| 357 | int id; |
---|
| 358 | Node(int _id) { id = _id;} |
---|
| 359 | public: |
---|
| 360 | Node() {} |
---|
| 361 | Node (Invalid) { id = -1; } |
---|
| 362 | bool operator==(const Node node) const {return id == node.id;} |
---|
| 363 | bool operator!=(const Node node) const {return id != node.id;} |
---|
| 364 | bool operator<(const Node node) const {return id < node.id;} |
---|
| 365 | }; |
---|
| 366 | |
---|
| 367 | |
---|
| 368 | |
---|
| 369 | class Edge { |
---|
[1909] | 370 | friend class FullUGraphBase; |
---|
[983] | 371 | |
---|
| 372 | protected: |
---|
[1566] | 373 | int id; // _nodeNum * target + source; |
---|
[983] | 374 | |
---|
| 375 | Edge(int _id) : id(_id) {} |
---|
| 376 | |
---|
| 377 | public: |
---|
| 378 | Edge() { } |
---|
| 379 | Edge (Invalid) { id = -1; } |
---|
| 380 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
| 381 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
| 382 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
| 383 | }; |
---|
| 384 | |
---|
| 385 | void first(Node& node) const { |
---|
[1703] | 386 | node.id = _nodeNum - 1; |
---|
[983] | 387 | } |
---|
| 388 | |
---|
| 389 | static void next(Node& node) { |
---|
| 390 | --node.id; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | void first(Edge& edge) const { |
---|
[1703] | 394 | edge.id = _edgeNum - 1; |
---|
[983] | 395 | } |
---|
| 396 | |
---|
| 397 | static void next(Edge& edge) { |
---|
| 398 | --edge.id; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | void firstOut(Edge& edge, const Node& node) const { |
---|
[1703] | 402 | int src = node.id; |
---|
| 403 | int trg = 0; |
---|
| 404 | edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 405 | } |
---|
| 406 | |
---|
| 407 | /// \todo with specialized iterators we can make faster iterating |
---|
[1703] | 408 | void nextOut(Edge& edge) const { |
---|
| 409 | int src = source(edge).id; |
---|
| 410 | int trg = target(edge).id; |
---|
| 411 | ++trg; |
---|
| 412 | edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 413 | } |
---|
| 414 | |
---|
| 415 | void firstIn(Edge& edge, const Node& node) const { |
---|
[1703] | 416 | int src = node.id + 1; |
---|
| 417 | int trg = node.id; |
---|
| 418 | edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 419 | } |
---|
| 420 | |
---|
[1703] | 421 | void nextIn(Edge& edge) const { |
---|
| 422 | int src = source(edge).id; |
---|
| 423 | int trg = target(edge).id; |
---|
| 424 | ++src; |
---|
| 425 | edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1); |
---|
[983] | 426 | } |
---|
| 427 | |
---|
| 428 | }; |
---|
| 429 | |
---|
[1979] | 430 | typedef UGraphExtender<UGraphBaseExtender<FullUGraphBase> > |
---|
| 431 | ExtendedFullUGraphBase; |
---|
[1555] | 432 | |
---|
[1566] | 433 | /// \ingroup graphs |
---|
| 434 | /// |
---|
| 435 | /// \brief An undirected full graph class. |
---|
| 436 | /// |
---|
[1726] | 437 | /// This is a simple and fast undirected full graph implementation. |
---|
[1566] | 438 | /// It is completely static, so you can neither add nor delete either |
---|
| 439 | /// edges or nodes. |
---|
| 440 | /// |
---|
[1909] | 441 | /// The main difference beetween the \e FullGraph and \e FullUGraph class |
---|
[1566] | 442 | /// is that this class conforms to the undirected graph concept and |
---|
[1726] | 443 | /// it does not contain the loop edges. |
---|
[1566] | 444 | /// |
---|
[1986] | 445 | /// \sa FullUGraphBase |
---|
[1566] | 446 | /// \sa FullGraph |
---|
| 447 | /// |
---|
| 448 | /// \author Balazs Dezso |
---|
[1909] | 449 | class FullUGraph : public ExtendedFullUGraphBase { |
---|
[1566] | 450 | public: |
---|
[1979] | 451 | |
---|
| 452 | typedef ExtendedFullUGraphBase Parent; |
---|
| 453 | |
---|
| 454 | /// \brief Constructor |
---|
[1909] | 455 | FullUGraph(int n) { construct(n); } |
---|
[1979] | 456 | |
---|
| 457 | /// \brief Resize the graph |
---|
| 458 | /// |
---|
[1986] | 459 | /// Resize the graph. The function will fully destroy and build the graph. |
---|
| 460 | /// This cause that the maps of the graph will reallocated |
---|
| 461 | /// automatically and the previous values will be lost. |
---|
[1979] | 462 | void resize(int n) { |
---|
| 463 | Parent::getNotifier(Edge()).clear(); |
---|
| 464 | Parent::getNotifier(UEdge()).clear(); |
---|
| 465 | Parent::getNotifier(Node()).clear(); |
---|
| 466 | construct(n); |
---|
| 467 | Parent::getNotifier(Node()).build(); |
---|
| 468 | Parent::getNotifier(UEdge()).build(); |
---|
| 469 | Parent::getNotifier(Edge()).build(); |
---|
| 470 | } |
---|
[1566] | 471 | }; |
---|
[591] | 472 | |
---|
[1820] | 473 | |
---|
[1910] | 474 | class FullBpUGraphBase { |
---|
[1820] | 475 | protected: |
---|
| 476 | |
---|
[1910] | 477 | int _aNodeNum; |
---|
| 478 | int _bNodeNum; |
---|
[1820] | 479 | |
---|
| 480 | int _edgeNum; |
---|
| 481 | |
---|
| 482 | public: |
---|
| 483 | |
---|
| 484 | class NodeSetError : public LogicError { |
---|
| 485 | virtual const char* exceptionName() const { |
---|
[1910] | 486 | return "lemon::FullBpUGraph::NodeSetError"; |
---|
[1820] | 487 | } |
---|
| 488 | }; |
---|
| 489 | |
---|
| 490 | class Node { |
---|
[1910] | 491 | friend class FullBpUGraphBase; |
---|
[1820] | 492 | protected: |
---|
| 493 | int id; |
---|
| 494 | |
---|
| 495 | Node(int _id) : id(_id) {} |
---|
| 496 | public: |
---|
| 497 | Node() {} |
---|
| 498 | Node(Invalid) { id = -1; } |
---|
| 499 | bool operator==(const Node i) const {return id==i.id;} |
---|
| 500 | bool operator!=(const Node i) const {return id!=i.id;} |
---|
| 501 | bool operator<(const Node i) const {return id<i.id;} |
---|
| 502 | }; |
---|
| 503 | |
---|
| 504 | class Edge { |
---|
[1910] | 505 | friend class FullBpUGraphBase; |
---|
[1820] | 506 | protected: |
---|
| 507 | int id; |
---|
| 508 | |
---|
| 509 | Edge(int _id) { id = _id;} |
---|
| 510 | public: |
---|
| 511 | Edge() {} |
---|
| 512 | Edge (Invalid) { id = -1; } |
---|
| 513 | bool operator==(const Edge i) const {return id==i.id;} |
---|
| 514 | bool operator!=(const Edge i) const {return id!=i.id;} |
---|
| 515 | bool operator<(const Edge i) const {return id<i.id;} |
---|
| 516 | }; |
---|
| 517 | |
---|
[1910] | 518 | void construct(int aNodeNum, int bNodeNum) { |
---|
| 519 | _aNodeNum = aNodeNum; |
---|
| 520 | _bNodeNum = bNodeNum; |
---|
| 521 | _edgeNum = aNodeNum * bNodeNum; |
---|
[1820] | 522 | } |
---|
| 523 | |
---|
[1910] | 524 | void firstANode(Node& node) const { |
---|
| 525 | node.id = 2 * _aNodeNum - 2; |
---|
[1820] | 526 | if (node.id < 0) node.id = -1; |
---|
| 527 | } |
---|
[1910] | 528 | void nextANode(Node& node) const { |
---|
[1820] | 529 | node.id -= 2; |
---|
| 530 | if (node.id < 0) node.id = -1; |
---|
| 531 | } |
---|
| 532 | |
---|
[1910] | 533 | void firstBNode(Node& node) const { |
---|
| 534 | node.id = 2 * _bNodeNum - 1; |
---|
[1820] | 535 | } |
---|
[1910] | 536 | void nextBNode(Node& node) const { |
---|
[1820] | 537 | node.id -= 2; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | void first(Node& node) const { |
---|
[1910] | 541 | if (_aNodeNum > 0) { |
---|
| 542 | node.id = 2 * _aNodeNum - 2; |
---|
[1820] | 543 | } else { |
---|
[1910] | 544 | node.id = 2 * _bNodeNum - 1; |
---|
[1820] | 545 | } |
---|
| 546 | } |
---|
| 547 | void next(Node& node) const { |
---|
| 548 | node.id -= 2; |
---|
| 549 | if (node.id == -2) { |
---|
[1910] | 550 | node.id = 2 * _bNodeNum - 1; |
---|
[1820] | 551 | } |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | void first(Edge& edge) const { |
---|
| 555 | edge.id = _edgeNum - 1; |
---|
| 556 | } |
---|
| 557 | void next(Edge& edge) const { |
---|
| 558 | --edge.id; |
---|
| 559 | } |
---|
| 560 | |
---|
[1910] | 561 | void firstOut(Edge& edge, const Node& node) const { |
---|
[1820] | 562 | LEMON_ASSERT((node.id & 1) == 0, NodeSetError()); |
---|
[1910] | 563 | edge.id = (node.id >> 1) * _bNodeNum; |
---|
[1820] | 564 | } |
---|
[1910] | 565 | void nextOut(Edge& edge) const { |
---|
[1820] | 566 | ++(edge.id); |
---|
[1910] | 567 | if (edge.id % _bNodeNum == 0) edge.id = -1; |
---|
[1820] | 568 | } |
---|
| 569 | |
---|
[1910] | 570 | void firstIn(Edge& edge, const Node& node) const { |
---|
[1820] | 571 | LEMON_ASSERT((node.id & 1) == 1, NodeSetError()); |
---|
| 572 | edge.id = (node.id >> 1); |
---|
| 573 | } |
---|
[1910] | 574 | void nextIn(Edge& edge) const { |
---|
| 575 | edge.id += _bNodeNum; |
---|
[1820] | 576 | if (edge.id >= _edgeNum) edge.id = -1; |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | static int id(const Node& node) { |
---|
| 580 | return node.id; |
---|
| 581 | } |
---|
| 582 | static Node nodeFromId(int id) { |
---|
| 583 | return Node(id); |
---|
| 584 | } |
---|
| 585 | int maxNodeId() const { |
---|
[1910] | 586 | return _aNodeNum > _bNodeNum ? |
---|
| 587 | _aNodeNum * 2 - 2 : _bNodeNum * 2 - 1; |
---|
[1820] | 588 | } |
---|
| 589 | |
---|
| 590 | static int id(const Edge& edge) { |
---|
| 591 | return edge.id; |
---|
| 592 | } |
---|
| 593 | static Edge edgeFromId(int id) { |
---|
| 594 | return Edge(id); |
---|
| 595 | } |
---|
| 596 | int maxEdgeId() const { |
---|
| 597 | return _edgeNum - 1; |
---|
| 598 | } |
---|
| 599 | |
---|
[1910] | 600 | static int aNodeId(const Node& node) { |
---|
[1820] | 601 | return node.id >> 1; |
---|
| 602 | } |
---|
[1910] | 603 | static Node fromANodeId(int id, Node) { |
---|
[1820] | 604 | return Node(id << 1); |
---|
| 605 | } |
---|
[1910] | 606 | int maxANodeId() const { |
---|
| 607 | return _aNodeNum; |
---|
[1820] | 608 | } |
---|
| 609 | |
---|
[1910] | 610 | static int bNodeId(const Node& node) { |
---|
[1820] | 611 | return node.id >> 1; |
---|
| 612 | } |
---|
[1910] | 613 | static Node fromBNodeId(int id) { |
---|
[1820] | 614 | return Node((id << 1) + 1); |
---|
| 615 | } |
---|
[1910] | 616 | int maxBNodeId() const { |
---|
| 617 | return _bNodeNum; |
---|
[1820] | 618 | } |
---|
| 619 | |
---|
[1910] | 620 | Node aNode(const Edge& edge) const { |
---|
| 621 | return Node((edge.id / _bNodeNum) << 1); |
---|
[1820] | 622 | } |
---|
[1910] | 623 | Node bNode(const Edge& edge) const { |
---|
| 624 | return Node(((edge.id % _bNodeNum) << 1) + 1); |
---|
[1820] | 625 | } |
---|
| 626 | |
---|
[1910] | 627 | static bool aNode(const Node& node) { |
---|
[1820] | 628 | return (node.id & 1) == 0; |
---|
| 629 | } |
---|
| 630 | |
---|
[1910] | 631 | static bool bNode(const Node& node) { |
---|
[1820] | 632 | return (node.id & 1) == 1; |
---|
| 633 | } |
---|
| 634 | |
---|
[1910] | 635 | static Node aNode(int index) { |
---|
[1820] | 636 | return Node(index << 1); |
---|
| 637 | } |
---|
| 638 | |
---|
[1910] | 639 | static Node bNode(int index) { |
---|
[1820] | 640 | return Node((index << 1) + 1); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | }; |
---|
| 644 | |
---|
| 645 | |
---|
[1979] | 646 | typedef BpUGraphExtender< BpUGraphBaseExtender< |
---|
| 647 | FullBpUGraphBase> > ExtendedFullBpUGraphBase; |
---|
[1820] | 648 | |
---|
| 649 | |
---|
[1910] | 650 | /// \ingroup graphs |
---|
| 651 | /// |
---|
| 652 | /// \brief An undirected full bipartite graph class. |
---|
| 653 | /// |
---|
| 654 | /// This is a simple and fast bipartite undirected full graph implementation. |
---|
| 655 | /// It is completely static, so you can neither add nor delete either |
---|
| 656 | /// edges or nodes. |
---|
| 657 | /// |
---|
[1986] | 658 | /// \sa FullUGraphBase |
---|
[1910] | 659 | /// \sa FullGraph |
---|
| 660 | /// |
---|
| 661 | /// \author Balazs Dezso |
---|
| 662 | class FullBpUGraph : |
---|
| 663 | public ExtendedFullBpUGraphBase { |
---|
[1820] | 664 | public: |
---|
[1979] | 665 | |
---|
[1910] | 666 | typedef ExtendedFullBpUGraphBase Parent; |
---|
[1979] | 667 | |
---|
[1910] | 668 | FullBpUGraph(int aNodeNum, int bNodeNum) { |
---|
| 669 | Parent::construct(aNodeNum, bNodeNum); |
---|
[1820] | 670 | } |
---|
[1979] | 671 | /// \brief Resize the graph |
---|
| 672 | /// |
---|
| 673 | void resize(int n, int m) { |
---|
| 674 | Parent::getNotifier(Edge()).clear(); |
---|
| 675 | Parent::getNotifier(UEdge()).clear(); |
---|
| 676 | Parent::getNotifier(Node()).clear(); |
---|
| 677 | construct(n, m); |
---|
| 678 | Parent::getNotifier(Node()).build(); |
---|
| 679 | Parent::getNotifier(UEdge()).build(); |
---|
| 680 | Parent::getNotifier(Edge()).build(); |
---|
| 681 | } |
---|
[1820] | 682 | }; |
---|
| 683 | |
---|
[921] | 684 | } //namespace lemon |
---|
[591] | 685 | |
---|
| 686 | |
---|
[921] | 687 | #endif //LEMON_FULL_GRAPH_H |
---|