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