[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> |
---|
| 25 | #include <lemon/bits/default_map.h> |
---|
[946] | 26 | |
---|
[977] | 27 | #include <lemon/invalid.h> |
---|
| 28 | #include <lemon/utility.h> |
---|
| 29 | |
---|
| 30 | |
---|
[591] | 31 | ///\ingroup graphs |
---|
| 32 | ///\file |
---|
| 33 | ///\brief FullGraph and SymFullGraph classes. |
---|
| 34 | |
---|
| 35 | |
---|
[921] | 36 | namespace lemon { |
---|
[591] | 37 | |
---|
[946] | 38 | class FullGraphBase { |
---|
[591] | 39 | int NodeNum; |
---|
| 40 | int EdgeNum; |
---|
| 41 | public: |
---|
[782] | 42 | |
---|
[946] | 43 | typedef FullGraphBase Graph; |
---|
[591] | 44 | |
---|
| 45 | class Node; |
---|
| 46 | class Edge; |
---|
[782] | 47 | |
---|
[591] | 48 | public: |
---|
| 49 | |
---|
[946] | 50 | FullGraphBase() {} |
---|
| 51 | |
---|
| 52 | |
---|
[591] | 53 | ///Creates a full graph with \c n nodes. |
---|
[946] | 54 | void construct(int n) { NodeNum = n; EdgeNum = n * n; } |
---|
[591] | 55 | /// |
---|
[946] | 56 | // FullGraphBase(const FullGraphBase &_g) |
---|
| 57 | // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { } |
---|
[591] | 58 | |
---|
[977] | 59 | typedef True NodeNumTag; |
---|
| 60 | typedef True EdgeNumTag; |
---|
| 61 | |
---|
[813] | 62 | ///Number of nodes. |
---|
| 63 | int nodeNum() const { return NodeNum; } |
---|
| 64 | ///Number of edges. |
---|
| 65 | int edgeNum() const { return EdgeNum; } |
---|
[591] | 66 | |
---|
[813] | 67 | /// Maximum node ID. |
---|
| 68 | |
---|
| 69 | /// Maximum node ID. |
---|
| 70 | ///\sa id(Node) |
---|
[980] | 71 | int maxId(Node = INVALID) const { return NodeNum-1; } |
---|
[813] | 72 | /// Maximum edge ID. |
---|
| 73 | |
---|
| 74 | /// Maximum edge ID. |
---|
| 75 | ///\sa id(Edge) |
---|
[980] | 76 | int maxId(Edge = INVALID) const { return EdgeNum-1; } |
---|
[591] | 77 | |
---|
[986] | 78 | Node source(Edge e) const { return e.id % NodeNum; } |
---|
| 79 | Node target(Edge e) const { return e.id / NodeNum; } |
---|
[591] | 80 | |
---|
| 81 | |
---|
[813] | 82 | /// Node ID. |
---|
| 83 | |
---|
| 84 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 85 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 86 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 87 | /// |
---|
| 88 | /// The ID of the \ref INVALID node is -1. |
---|
| 89 | ///\return The ID of the node \c v. |
---|
[946] | 90 | |
---|
| 91 | static int id(Node v) { return v.id; } |
---|
[813] | 92 | /// Edge ID. |
---|
| 93 | |
---|
| 94 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 95 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 96 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 97 | /// |
---|
| 98 | /// The ID of the \ref INVALID edge is -1. |
---|
| 99 | ///\return The ID of the edge \c e. |
---|
[946] | 100 | static int id(Edge e) { return e.id; } |
---|
[591] | 101 | |
---|
[1106] | 102 | static Node fromId(int id, Node) { return Node(id);} |
---|
| 103 | |
---|
| 104 | static Edge fromId(int id, Edge) { return Edge(id);} |
---|
| 105 | |
---|
[774] | 106 | /// Finds an edge between two nodes. |
---|
| 107 | |
---|
| 108 | /// Finds an edge from node \c u to node \c v. |
---|
| 109 | /// |
---|
| 110 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 111 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 112 | /// the next edge from \c u to \c v after \c prev. |
---|
| 113 | /// \return The found edge or INVALID if there is no such an edge. |
---|
| 114 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 115 | { |
---|
[946] | 116 | return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; |
---|
[774] | 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
[591] | 120 | class Node { |
---|
[946] | 121 | friend class FullGraphBase; |
---|
[591] | 122 | |
---|
| 123 | protected: |
---|
[946] | 124 | int id; |
---|
| 125 | Node(int _id) { id = _id;} |
---|
[591] | 126 | public: |
---|
| 127 | Node() {} |
---|
[946] | 128 | Node (Invalid) { id = -1; } |
---|
| 129 | bool operator==(const Node node) const {return id == node.id;} |
---|
| 130 | bool operator!=(const Node node) const {return id != node.id;} |
---|
| 131 | bool operator<(const Node node) const {return id < node.id;} |
---|
[591] | 132 | }; |
---|
| 133 | |
---|
[946] | 134 | |
---|
| 135 | |
---|
| 136 | class Edge { |
---|
| 137 | friend class FullGraphBase; |
---|
| 138 | |
---|
| 139 | protected: |
---|
[986] | 140 | int id; // NodeNum * target + source; |
---|
[946] | 141 | |
---|
| 142 | Edge(int _id) : id(_id) {} |
---|
| 143 | |
---|
[986] | 144 | Edge(const FullGraphBase& _graph, int source, int target) |
---|
| 145 | : id(_graph.NodeNum * target+source) {} |
---|
[591] | 146 | public: |
---|
[946] | 147 | Edge() { } |
---|
| 148 | Edge (Invalid) { id = -1; } |
---|
| 149 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
| 150 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
| 151 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
[591] | 152 | }; |
---|
| 153 | |
---|
[946] | 154 | void first(Node& node) const { |
---|
| 155 | node.id = NodeNum-1; |
---|
| 156 | } |
---|
[591] | 157 | |
---|
[946] | 158 | static void next(Node& node) { |
---|
| 159 | --node.id; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | void first(Edge& edge) const { |
---|
| 163 | edge.id = EdgeNum-1; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | static void next(Edge& edge) { |
---|
| 167 | --edge.id; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | void firstOut(Edge& edge, const Node& node) const { |
---|
| 171 | edge.id = EdgeNum + node.id - NodeNum; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | void nextOut(Edge& edge) const { |
---|
| 175 | edge.id -= NodeNum; |
---|
| 176 | if (edge.id < 0) edge.id = -1; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | void firstIn(Edge& edge, const Node& node) const { |
---|
| 180 | edge.id = node.id * NodeNum; |
---|
| 181 | } |
---|
[591] | 182 | |
---|
[946] | 183 | void nextIn(Edge& edge) const { |
---|
| 184 | ++edge.id; |
---|
| 185 | if (edge.id % NodeNum == 0) edge.id = -1; |
---|
| 186 | } |
---|
[591] | 187 | |
---|
| 188 | }; |
---|
| 189 | |
---|
[946] | 190 | |
---|
| 191 | typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase; |
---|
| 192 | typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase; |
---|
[980] | 193 | typedef DefaultMappableGraphExtender<IterableFullGraphBase> MappableFullGraphBase; |
---|
[946] | 194 | |
---|
[1555] | 195 | /// \addtogroup graphs |
---|
| 196 | /// @{ |
---|
| 197 | |
---|
[951] | 198 | ///A full graph class. |
---|
| 199 | |
---|
| 200 | ///This is a simple and fast directed full graph implementation. |
---|
| 201 | ///It is completely static, so you can neither add nor delete either |
---|
| 202 | ///edges or nodes. |
---|
| 203 | ///Thus it conforms to |
---|
[959] | 204 | ///the \ref concept::StaticGraph "StaticGraph" concept |
---|
| 205 | ///\sa concept::StaticGraph. |
---|
[951] | 206 | /// |
---|
| 207 | ///\author Alpar Juttner |
---|
[946] | 208 | class FullGraph : public MappableFullGraphBase { |
---|
| 209 | public: |
---|
| 210 | |
---|
| 211 | FullGraph(int n) { construct(n); } |
---|
| 212 | }; |
---|
| 213 | |
---|
[1555] | 214 | ///@} |
---|
[983] | 215 | |
---|
[1161] | 216 | // Base graph class for UndirFullGraph. |
---|
[983] | 217 | class UndirFullGraphBase { |
---|
| 218 | int NodeNum; |
---|
| 219 | int EdgeNum; |
---|
| 220 | public: |
---|
| 221 | |
---|
[984] | 222 | typedef UndirFullGraphBase Graph; |
---|
[983] | 223 | |
---|
| 224 | class Node; |
---|
| 225 | class Edge; |
---|
| 226 | |
---|
| 227 | public: |
---|
| 228 | |
---|
[984] | 229 | UndirFullGraphBase() {} |
---|
[983] | 230 | |
---|
| 231 | |
---|
| 232 | ///Creates a full graph with \c n nodes. |
---|
| 233 | void construct(int n) { NodeNum = n; EdgeNum = n * (n - 1) / 2; } |
---|
| 234 | /// |
---|
| 235 | // FullGraphBase(const FullGraphBase &_g) |
---|
| 236 | // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { } |
---|
| 237 | |
---|
| 238 | typedef True NodeNumTag; |
---|
| 239 | typedef True EdgeNumTag; |
---|
| 240 | |
---|
| 241 | ///Number of nodes. |
---|
| 242 | int nodeNum() const { return NodeNum; } |
---|
| 243 | ///Number of edges. |
---|
| 244 | int edgeNum() const { return EdgeNum; } |
---|
| 245 | |
---|
| 246 | /// Maximum node ID. |
---|
| 247 | |
---|
| 248 | /// Maximum node ID. |
---|
| 249 | ///\sa id(Node) |
---|
| 250 | int maxId(Node = INVALID) const { return NodeNum-1; } |
---|
| 251 | /// Maximum edge ID. |
---|
| 252 | |
---|
| 253 | /// Maximum edge ID. |
---|
| 254 | ///\sa id(Edge) |
---|
| 255 | int maxId(Edge = INVALID) const { return EdgeNum-1; } |
---|
| 256 | |
---|
[986] | 257 | Node source(Edge e) const { |
---|
[983] | 258 | /// \todo we may do it faster |
---|
| 259 | return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2; |
---|
| 260 | } |
---|
| 261 | |
---|
[986] | 262 | Node target(Edge e) const { |
---|
| 263 | int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;; |
---|
| 264 | return e.id - (source) * (source - 1) / 2; |
---|
[983] | 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | /// Node ID. |
---|
| 269 | |
---|
| 270 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 271 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 272 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 273 | /// |
---|
| 274 | /// The ID of the \ref INVALID node is -1. |
---|
| 275 | ///\return The ID of the node \c v. |
---|
| 276 | |
---|
| 277 | static int id(Node v) { return v.id; } |
---|
| 278 | /// Edge ID. |
---|
| 279 | |
---|
| 280 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 281 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 282 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 283 | /// |
---|
| 284 | /// The ID of the \ref INVALID edge is -1. |
---|
| 285 | ///\return The ID of the edge \c e. |
---|
| 286 | static int id(Edge e) { return e.id; } |
---|
| 287 | |
---|
| 288 | /// Finds an edge between two nodes. |
---|
| 289 | |
---|
| 290 | /// Finds an edge from node \c u to node \c v. |
---|
| 291 | /// |
---|
| 292 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 293 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 294 | /// the next edge from \c u to \c v after \c prev. |
---|
| 295 | /// \return The found edge or INVALID if there is no such an edge. |
---|
| 296 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 297 | { |
---|
| 298 | return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | class Node { |
---|
[985] | 303 | friend class UndirFullGraphBase; |
---|
[983] | 304 | |
---|
| 305 | protected: |
---|
| 306 | int id; |
---|
| 307 | Node(int _id) { id = _id;} |
---|
| 308 | public: |
---|
| 309 | Node() {} |
---|
| 310 | Node (Invalid) { id = -1; } |
---|
| 311 | bool operator==(const Node node) const {return id == node.id;} |
---|
| 312 | bool operator!=(const Node node) const {return id != node.id;} |
---|
| 313 | bool operator<(const Node node) const {return id < node.id;} |
---|
| 314 | }; |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | class Edge { |
---|
[985] | 319 | friend class UndirFullGraphBase; |
---|
[983] | 320 | |
---|
| 321 | protected: |
---|
[986] | 322 | int id; // NodeNum * target + source; |
---|
[983] | 323 | |
---|
| 324 | Edge(int _id) : id(_id) {} |
---|
| 325 | |
---|
[986] | 326 | Edge(const UndirFullGraphBase& _graph, int source, int target) |
---|
| 327 | : id(_graph.NodeNum * target+source) {} |
---|
[983] | 328 | public: |
---|
| 329 | Edge() { } |
---|
| 330 | Edge (Invalid) { id = -1; } |
---|
| 331 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
| 332 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
| 333 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
| 334 | }; |
---|
| 335 | |
---|
| 336 | void first(Node& node) const { |
---|
| 337 | node.id = NodeNum-1; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | static void next(Node& node) { |
---|
| 341 | --node.id; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | void first(Edge& edge) const { |
---|
| 345 | edge.id = EdgeNum-1; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | static void next(Edge& edge) { |
---|
| 349 | --edge.id; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | void firstOut(Edge& edge, const Node& node) const { |
---|
| 353 | edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | /// \todo with specialized iterators we can make faster iterating |
---|
[985] | 357 | void nextOut(Edge& e) const { |
---|
[986] | 358 | int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;; |
---|
| 359 | int target = e.id - (source) * (source - 1) / 2; |
---|
| 360 | ++target; |
---|
| 361 | e.id = target < source ? source * (source - 1) / 2 + target : -1; |
---|
[983] | 362 | } |
---|
| 363 | |
---|
| 364 | void firstIn(Edge& edge, const Node& node) const { |
---|
| 365 | edge.id = node.id * (node.id + 1) / 2 - 1; |
---|
| 366 | } |
---|
| 367 | |
---|
[985] | 368 | void nextIn(Edge& e) const { |
---|
[986] | 369 | int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;; |
---|
| 370 | int target = e.id - (source) * (source - 1) / 2; ++target; |
---|
| 371 | ++source; |
---|
| 372 | e.id = source < NodeNum ? source * (source - 1) / 2 + target : -1; |
---|
[983] | 373 | } |
---|
| 374 | |
---|
| 375 | }; |
---|
| 376 | |
---|
[1555] | 377 | /// \addtogroup graphs |
---|
| 378 | /// @{ |
---|
| 379 | |
---|
| 380 | |
---|
[983] | 381 | /// \todo UndirFullGraph from the UndirFullGraphBase |
---|
| 382 | |
---|
| 383 | |
---|
[591] | 384 | /// @} |
---|
| 385 | |
---|
[921] | 386 | } //namespace lemon |
---|
[591] | 387 | |
---|
| 388 | |
---|
[921] | 389 | #endif //LEMON_FULL_GRAPH_H |
---|