[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[109] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[109] | 4 | * |
---|
[440] | 5 | * Copyright (C) 2003-2009 |
---|
[109] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 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 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_SMART_GRAPH_H |
---|
| 20 | #define LEMON_SMART_GRAPH_H |
---|
| 21 | |
---|
| 22 | ///\ingroup graphs |
---|
| 23 | ///\file |
---|
| 24 | ///\brief SmartDigraph and SmartGraph classes. |
---|
| 25 | |
---|
| 26 | #include <vector> |
---|
| 27 | |
---|
[220] | 28 | #include <lemon/core.h> |
---|
[109] | 29 | #include <lemon/error.h> |
---|
| 30 | #include <lemon/bits/graph_extender.h> |
---|
| 31 | |
---|
| 32 | namespace lemon { |
---|
| 33 | |
---|
| 34 | class SmartDigraph; |
---|
| 35 | |
---|
| 36 | class SmartDigraphBase { |
---|
| 37 | protected: |
---|
| 38 | |
---|
[209] | 39 | struct NodeT |
---|
[109] | 40 | { |
---|
[209] | 41 | int first_in, first_out; |
---|
[109] | 42 | NodeT() {} |
---|
| 43 | }; |
---|
[209] | 44 | struct ArcT |
---|
[109] | 45 | { |
---|
[209] | 46 | int target, source, next_in, next_out; |
---|
| 47 | ArcT() {} |
---|
[109] | 48 | }; |
---|
| 49 | |
---|
| 50 | std::vector<NodeT> nodes; |
---|
| 51 | std::vector<ArcT> arcs; |
---|
[209] | 52 | |
---|
[109] | 53 | public: |
---|
| 54 | |
---|
[617] | 55 | typedef SmartDigraphBase Digraph; |
---|
[109] | 56 | |
---|
| 57 | class Node; |
---|
| 58 | class Arc; |
---|
| 59 | |
---|
| 60 | public: |
---|
| 61 | |
---|
| 62 | SmartDigraphBase() : nodes(), arcs() { } |
---|
[209] | 63 | SmartDigraphBase(const SmartDigraphBase &_g) |
---|
[109] | 64 | : nodes(_g.nodes), arcs(_g.arcs) { } |
---|
[209] | 65 | |
---|
[109] | 66 | typedef True NodeNumTag; |
---|
[360] | 67 | typedef True ArcNumTag; |
---|
[109] | 68 | |
---|
| 69 | int nodeNum() const { return nodes.size(); } |
---|
| 70 | int arcNum() const { return arcs.size(); } |
---|
| 71 | |
---|
| 72 | int maxNodeId() const { return nodes.size()-1; } |
---|
| 73 | int maxArcId() const { return arcs.size()-1; } |
---|
| 74 | |
---|
| 75 | Node addNode() { |
---|
[209] | 76 | int n = nodes.size(); |
---|
[109] | 77 | nodes.push_back(NodeT()); |
---|
| 78 | nodes[n].first_in = -1; |
---|
| 79 | nodes[n].first_out = -1; |
---|
| 80 | return Node(n); |
---|
| 81 | } |
---|
[209] | 82 | |
---|
[109] | 83 | Arc addArc(Node u, Node v) { |
---|
[209] | 84 | int n = arcs.size(); |
---|
[109] | 85 | arcs.push_back(ArcT()); |
---|
[209] | 86 | arcs[n].source = u._id; |
---|
[109] | 87 | arcs[n].target = v._id; |
---|
| 88 | arcs[n].next_out = nodes[u._id].first_out; |
---|
| 89 | arcs[n].next_in = nodes[v._id].first_in; |
---|
| 90 | nodes[u._id].first_out = nodes[v._id].first_in = n; |
---|
| 91 | |
---|
| 92 | return Arc(n); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void clear() { |
---|
| 96 | arcs.clear(); |
---|
| 97 | nodes.clear(); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | Node source(Arc a) const { return Node(arcs[a._id].source); } |
---|
| 101 | Node target(Arc a) const { return Node(arcs[a._id].target); } |
---|
| 102 | |
---|
| 103 | static int id(Node v) { return v._id; } |
---|
| 104 | static int id(Arc a) { return a._id; } |
---|
| 105 | |
---|
| 106 | static Node nodeFromId(int id) { return Node(id);} |
---|
| 107 | static Arc arcFromId(int id) { return Arc(id);} |
---|
| 108 | |
---|
[209] | 109 | bool valid(Node n) const { |
---|
| 110 | return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
---|
[149] | 111 | } |
---|
[209] | 112 | bool valid(Arc a) const { |
---|
| 113 | return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
---|
[149] | 114 | } |
---|
| 115 | |
---|
[109] | 116 | class Node { |
---|
| 117 | friend class SmartDigraphBase; |
---|
| 118 | friend class SmartDigraph; |
---|
| 119 | |
---|
| 120 | protected: |
---|
| 121 | int _id; |
---|
| 122 | explicit Node(int id) : _id(id) {} |
---|
| 123 | public: |
---|
| 124 | Node() {} |
---|
| 125 | Node (Invalid) : _id(-1) {} |
---|
| 126 | bool operator==(const Node i) const {return _id == i._id;} |
---|
| 127 | bool operator!=(const Node i) const {return _id != i._id;} |
---|
| 128 | bool operator<(const Node i) const {return _id < i._id;} |
---|
| 129 | }; |
---|
[209] | 130 | |
---|
[109] | 131 | |
---|
| 132 | class Arc { |
---|
| 133 | friend class SmartDigraphBase; |
---|
| 134 | friend class SmartDigraph; |
---|
| 135 | |
---|
| 136 | protected: |
---|
| 137 | int _id; |
---|
| 138 | explicit Arc(int id) : _id(id) {} |
---|
| 139 | public: |
---|
| 140 | Arc() { } |
---|
| 141 | Arc (Invalid) : _id(-1) {} |
---|
| 142 | bool operator==(const Arc i) const {return _id == i._id;} |
---|
| 143 | bool operator!=(const Arc i) const {return _id != i._id;} |
---|
| 144 | bool operator<(const Arc i) const {return _id < i._id;} |
---|
| 145 | }; |
---|
| 146 | |
---|
| 147 | void first(Node& node) const { |
---|
| 148 | node._id = nodes.size() - 1; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | static void next(Node& node) { |
---|
| 152 | --node._id; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | void first(Arc& arc) const { |
---|
| 156 | arc._id = arcs.size() - 1; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | static void next(Arc& arc) { |
---|
| 160 | --arc._id; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void firstOut(Arc& arc, const Node& node) const { |
---|
| 164 | arc._id = nodes[node._id].first_out; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | void nextOut(Arc& arc) const { |
---|
| 168 | arc._id = arcs[arc._id].next_out; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void firstIn(Arc& arc, const Node& node) const { |
---|
| 172 | arc._id = nodes[node._id].first_in; |
---|
| 173 | } |
---|
[209] | 174 | |
---|
[109] | 175 | void nextIn(Arc& arc) const { |
---|
| 176 | arc._id = arcs[arc._id].next_in; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | }; |
---|
| 180 | |
---|
| 181 | typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
---|
| 182 | |
---|
| 183 | ///\ingroup graphs |
---|
| 184 | /// |
---|
| 185 | ///\brief A smart directed graph class. |
---|
| 186 | /// |
---|
[735] | 187 | ///\ref SmartDigraph is a simple and fast digraph implementation. |
---|
| 188 | ///It is also quite memory efficient but at the price |
---|
| 189 | ///that it does not support node and arc deletion |
---|
| 190 | ///(except for the Snapshot feature). |
---|
[109] | 191 | /// |
---|
[735] | 192 | ///This type fully conforms to the \ref concepts::Digraph "Digraph concept" |
---|
| 193 | ///and it also provides some additional functionalities. |
---|
| 194 | ///Most of its member functions and nested classes are documented |
---|
| 195 | ///only in the concept class. |
---|
| 196 | /// |
---|
| 197 | ///\sa concepts::Digraph |
---|
| 198 | ///\sa SmartGraph |
---|
[109] | 199 | class SmartDigraph : public ExtendedSmartDigraphBase { |
---|
| 200 | typedef ExtendedSmartDigraphBase Parent; |
---|
| 201 | |
---|
| 202 | private: |
---|
[735] | 203 | /// Digraphs are \e not copy constructible. Use DigraphCopy instead. |
---|
[109] | 204 | SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {}; |
---|
[735] | 205 | /// \brief Assignment of a digraph to another one is \e not allowed. |
---|
| 206 | /// Use DigraphCopy instead. |
---|
[109] | 207 | void operator=(const SmartDigraph &) {} |
---|
| 208 | |
---|
| 209 | public: |
---|
[209] | 210 | |
---|
[109] | 211 | /// Constructor |
---|
[209] | 212 | |
---|
[109] | 213 | /// Constructor. |
---|
| 214 | /// |
---|
| 215 | SmartDigraph() {}; |
---|
[209] | 216 | |
---|
[109] | 217 | ///Add a new node to the digraph. |
---|
[209] | 218 | |
---|
[735] | 219 | ///This function adds a new node to the digraph. |
---|
| 220 | ///\return The new node. |
---|
[109] | 221 | Node addNode() { return Parent::addNode(); } |
---|
[209] | 222 | |
---|
[109] | 223 | ///Add a new arc to the digraph. |
---|
[209] | 224 | |
---|
[735] | 225 | ///This function adds a new arc to the digraph with source node \c s |
---|
[109] | 226 | ///and target node \c t. |
---|
[559] | 227 | ///\return The new arc. |
---|
[735] | 228 | Arc addArc(Node s, Node t) { |
---|
[209] | 229 | return Parent::addArc(s, t); |
---|
[109] | 230 | } |
---|
| 231 | |
---|
[149] | 232 | /// \brief Node validity check |
---|
| 233 | /// |
---|
[735] | 234 | /// This function gives back \c true if the given node is valid, |
---|
| 235 | /// i.e. it is a real node of the digraph. |
---|
[149] | 236 | /// |
---|
| 237 | /// \warning A removed node (using Snapshot) could become valid again |
---|
[735] | 238 | /// if new nodes are added to the digraph. |
---|
[149] | 239 | bool valid(Node n) const { return Parent::valid(n); } |
---|
| 240 | |
---|
| 241 | /// \brief Arc validity check |
---|
| 242 | /// |
---|
[735] | 243 | /// This function gives back \c true if the given arc is valid, |
---|
| 244 | /// i.e. it is a real arc of the digraph. |
---|
[149] | 245 | /// |
---|
| 246 | /// \warning A removed arc (using Snapshot) could become valid again |
---|
[735] | 247 | /// if new arcs are added to the graph. |
---|
[149] | 248 | bool valid(Arc a) const { return Parent::valid(a); } |
---|
| 249 | |
---|
[109] | 250 | ///Split a node. |
---|
[209] | 251 | |
---|
[735] | 252 | ///This function splits the given node. First, a new node is added |
---|
| 253 | ///to the digraph, then the source of each outgoing arc of node \c n |
---|
| 254 | ///is moved to this new node. |
---|
| 255 | ///If the second parameter \c connect is \c true (this is the default |
---|
| 256 | ///value), then a new arc from node \c n to the newly created node |
---|
| 257 | ///is also added. |
---|
[109] | 258 | ///\return The newly created node. |
---|
| 259 | /// |
---|
[735] | 260 | ///\note All iterators remain valid. |
---|
| 261 | /// |
---|
[109] | 262 | ///\warning This functionality cannot be used together with the Snapshot |
---|
| 263 | ///feature. |
---|
| 264 | Node split(Node n, bool connect = true) |
---|
| 265 | { |
---|
| 266 | Node b = addNode(); |
---|
| 267 | nodes[b._id].first_out=nodes[n._id].first_out; |
---|
| 268 | nodes[n._id].first_out=-1; |
---|
[370] | 269 | for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) { |
---|
| 270 | arcs[i].source=b._id; |
---|
| 271 | } |
---|
[109] | 272 | if(connect) addArc(n,b); |
---|
| 273 | return b; |
---|
| 274 | } |
---|
| 275 | |
---|
[735] | 276 | ///Clear the digraph. |
---|
| 277 | |
---|
| 278 | ///This function erases all nodes and arcs from the digraph. |
---|
| 279 | /// |
---|
| 280 | void clear() { |
---|
| 281 | Parent::clear(); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /// Reserve memory for nodes. |
---|
| 285 | |
---|
| 286 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 287 | /// allocation: if you know that the digraph you want to build will |
---|
| 288 | /// be large (e.g. it will contain millions of nodes and/or arcs), |
---|
| 289 | /// then it is worth reserving space for this amount before starting |
---|
| 290 | /// to build the digraph. |
---|
| 291 | /// \sa reserveArc() |
---|
| 292 | void reserveNode(int n) { nodes.reserve(n); }; |
---|
| 293 | |
---|
| 294 | /// Reserve memory for arcs. |
---|
| 295 | |
---|
| 296 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 297 | /// allocation: if you know that the digraph you want to build will |
---|
| 298 | /// be large (e.g. it will contain millions of nodes and/or arcs), |
---|
| 299 | /// then it is worth reserving space for this amount before starting |
---|
| 300 | /// to build the digraph. |
---|
| 301 | /// \sa reserveNode() |
---|
| 302 | void reserveArc(int m) { arcs.reserve(m); }; |
---|
| 303 | |
---|
[109] | 304 | public: |
---|
[209] | 305 | |
---|
[109] | 306 | class Snapshot; |
---|
| 307 | |
---|
| 308 | protected: |
---|
| 309 | |
---|
| 310 | void restoreSnapshot(const Snapshot &s) |
---|
| 311 | { |
---|
| 312 | while(s.arc_num<arcs.size()) { |
---|
| 313 | Arc arc = arcFromId(arcs.size()-1); |
---|
[209] | 314 | Parent::notifier(Arc()).erase(arc); |
---|
| 315 | nodes[arcs.back().source].first_out=arcs.back().next_out; |
---|
| 316 | nodes[arcs.back().target].first_in=arcs.back().next_in; |
---|
| 317 | arcs.pop_back(); |
---|
[109] | 318 | } |
---|
| 319 | while(s.node_num<nodes.size()) { |
---|
| 320 | Node node = nodeFromId(nodes.size()-1); |
---|
[209] | 321 | Parent::notifier(Node()).erase(node); |
---|
| 322 | nodes.pop_back(); |
---|
[109] | 323 | } |
---|
[209] | 324 | } |
---|
[109] | 325 | |
---|
| 326 | public: |
---|
| 327 | |
---|
[735] | 328 | ///Class to make a snapshot of the digraph and to restore it later. |
---|
[109] | 329 | |
---|
[735] | 330 | ///Class to make a snapshot of the digraph and to restore it later. |
---|
[109] | 331 | /// |
---|
| 332 | ///The newly added nodes and arcs can be removed using the |
---|
[735] | 333 | ///restore() function. This is the only way for deleting nodes and/or |
---|
| 334 | ///arcs from a SmartDigraph structure. |
---|
[109] | 335 | /// |
---|
[735] | 336 | ///\note After a state is restored, you cannot restore a later state, |
---|
| 337 | ///i.e. you cannot add the removed nodes and arcs again using |
---|
| 338 | ///another Snapshot instance. |
---|
| 339 | /// |
---|
| 340 | ///\warning Node splitting cannot be restored. |
---|
| 341 | ///\warning The validity of the snapshot is not stored due to |
---|
| 342 | ///performance reasons. If you do not use the snapshot correctly, |
---|
| 343 | ///it can cause broken program, invalid or not restored state of |
---|
| 344 | ///the digraph or no change. |
---|
[209] | 345 | class Snapshot |
---|
[109] | 346 | { |
---|
| 347 | SmartDigraph *_graph; |
---|
| 348 | protected: |
---|
| 349 | friend class SmartDigraph; |
---|
| 350 | unsigned int node_num; |
---|
| 351 | unsigned int arc_num; |
---|
| 352 | public: |
---|
| 353 | ///Default constructor. |
---|
[209] | 354 | |
---|
[109] | 355 | ///Default constructor. |
---|
[735] | 356 | ///You have to call save() to actually make a snapshot. |
---|
[109] | 357 | Snapshot() : _graph(0) {} |
---|
| 358 | ///Constructor that immediately makes a snapshot |
---|
[209] | 359 | |
---|
[735] | 360 | ///This constructor immediately makes a snapshot of the given digraph. |
---|
| 361 | /// |
---|
| 362 | Snapshot(SmartDigraph &gr) : _graph(&gr) { |
---|
[209] | 363 | node_num=_graph->nodes.size(); |
---|
| 364 | arc_num=_graph->arcs.size(); |
---|
[109] | 365 | } |
---|
| 366 | |
---|
| 367 | ///Make a snapshot. |
---|
| 368 | |
---|
[735] | 369 | ///This function makes a snapshot of the given digraph. |
---|
| 370 | ///It can be called more than once. In case of a repeated |
---|
[109] | 371 | ///call, the previous snapshot gets lost. |
---|
[735] | 372 | void save(SmartDigraph &gr) { |
---|
| 373 | _graph=&gr; |
---|
[209] | 374 | node_num=_graph->nodes.size(); |
---|
| 375 | arc_num=_graph->arcs.size(); |
---|
[109] | 376 | } |
---|
| 377 | |
---|
| 378 | ///Undo the changes until a snapshot. |
---|
[209] | 379 | |
---|
[735] | 380 | ///This function undos the changes until the last snapshot |
---|
| 381 | ///created by save() or Snapshot(SmartDigraph&). |
---|
[109] | 382 | void restore() |
---|
| 383 | { |
---|
[209] | 384 | _graph->restoreSnapshot(*this); |
---|
[109] | 385 | } |
---|
| 386 | }; |
---|
| 387 | }; |
---|
| 388 | |
---|
| 389 | |
---|
| 390 | class SmartGraphBase { |
---|
| 391 | |
---|
| 392 | protected: |
---|
| 393 | |
---|
| 394 | struct NodeT { |
---|
| 395 | int first_out; |
---|
| 396 | }; |
---|
[209] | 397 | |
---|
[109] | 398 | struct ArcT { |
---|
| 399 | int target; |
---|
| 400 | int next_out; |
---|
| 401 | }; |
---|
| 402 | |
---|
| 403 | std::vector<NodeT> nodes; |
---|
| 404 | std::vector<ArcT> arcs; |
---|
| 405 | |
---|
| 406 | int first_free_arc; |
---|
[209] | 407 | |
---|
[109] | 408 | public: |
---|
[209] | 409 | |
---|
[617] | 410 | typedef SmartGraphBase Graph; |
---|
[109] | 411 | |
---|
| 412 | class Node; |
---|
| 413 | class Arc; |
---|
| 414 | class Edge; |
---|
[209] | 415 | |
---|
[109] | 416 | class Node { |
---|
| 417 | friend class SmartGraphBase; |
---|
| 418 | protected: |
---|
| 419 | |
---|
| 420 | int _id; |
---|
| 421 | explicit Node(int id) { _id = id;} |
---|
| 422 | |
---|
| 423 | public: |
---|
| 424 | Node() {} |
---|
| 425 | Node (Invalid) { _id = -1; } |
---|
| 426 | bool operator==(const Node& node) const {return _id == node._id;} |
---|
| 427 | bool operator!=(const Node& node) const {return _id != node._id;} |
---|
| 428 | bool operator<(const Node& node) const {return _id < node._id;} |
---|
| 429 | }; |
---|
| 430 | |
---|
| 431 | class Edge { |
---|
| 432 | friend class SmartGraphBase; |
---|
| 433 | protected: |
---|
| 434 | |
---|
| 435 | int _id; |
---|
| 436 | explicit Edge(int id) { _id = id;} |
---|
| 437 | |
---|
| 438 | public: |
---|
| 439 | Edge() {} |
---|
| 440 | Edge (Invalid) { _id = -1; } |
---|
| 441 | bool operator==(const Edge& arc) const {return _id == arc._id;} |
---|
| 442 | bool operator!=(const Edge& arc) const {return _id != arc._id;} |
---|
| 443 | bool operator<(const Edge& arc) const {return _id < arc._id;} |
---|
| 444 | }; |
---|
| 445 | |
---|
| 446 | class Arc { |
---|
| 447 | friend class SmartGraphBase; |
---|
| 448 | protected: |
---|
| 449 | |
---|
| 450 | int _id; |
---|
| 451 | explicit Arc(int id) { _id = id;} |
---|
| 452 | |
---|
| 453 | public: |
---|
[329] | 454 | operator Edge() const { |
---|
| 455 | return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
---|
[238] | 456 | } |
---|
[109] | 457 | |
---|
| 458 | Arc() {} |
---|
| 459 | Arc (Invalid) { _id = -1; } |
---|
| 460 | bool operator==(const Arc& arc) const {return _id == arc._id;} |
---|
| 461 | bool operator!=(const Arc& arc) const {return _id != arc._id;} |
---|
| 462 | bool operator<(const Arc& arc) const {return _id < arc._id;} |
---|
| 463 | }; |
---|
| 464 | |
---|
| 465 | |
---|
| 466 | |
---|
| 467 | SmartGraphBase() |
---|
| 468 | : nodes(), arcs() {} |
---|
| 469 | |
---|
[368] | 470 | typedef True NodeNumTag; |
---|
| 471 | typedef True EdgeNumTag; |
---|
| 472 | typedef True ArcNumTag; |
---|
| 473 | |
---|
| 474 | int nodeNum() const { return nodes.size(); } |
---|
| 475 | int edgeNum() const { return arcs.size() / 2; } |
---|
| 476 | int arcNum() const { return arcs.size(); } |
---|
[209] | 477 | |
---|
| 478 | int maxNodeId() const { return nodes.size()-1; } |
---|
[109] | 479 | int maxEdgeId() const { return arcs.size() / 2 - 1; } |
---|
| 480 | int maxArcId() const { return arcs.size()-1; } |
---|
| 481 | |
---|
| 482 | Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); } |
---|
| 483 | Node target(Arc e) const { return Node(arcs[e._id].target); } |
---|
| 484 | |
---|
[125] | 485 | Node u(Edge e) const { return Node(arcs[2 * e._id].target); } |
---|
| 486 | Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); } |
---|
[109] | 487 | |
---|
| 488 | static bool direction(Arc e) { |
---|
| 489 | return (e._id & 1) == 1; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | static Arc direct(Edge e, bool d) { |
---|
| 493 | return Arc(e._id * 2 + (d ? 1 : 0)); |
---|
| 494 | } |
---|
| 495 | |
---|
[209] | 496 | void first(Node& node) const { |
---|
[109] | 497 | node._id = nodes.size() - 1; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | void next(Node& node) const { |
---|
| 501 | --node._id; |
---|
| 502 | } |
---|
| 503 | |
---|
[209] | 504 | void first(Arc& arc) const { |
---|
[109] | 505 | arc._id = arcs.size() - 1; |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | void next(Arc& arc) const { |
---|
| 509 | --arc._id; |
---|
| 510 | } |
---|
| 511 | |
---|
[209] | 512 | void first(Edge& arc) const { |
---|
[109] | 513 | arc._id = arcs.size() / 2 - 1; |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | void next(Edge& arc) const { |
---|
| 517 | --arc._id; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | void firstOut(Arc &arc, const Node& v) const { |
---|
| 521 | arc._id = nodes[v._id].first_out; |
---|
| 522 | } |
---|
| 523 | void nextOut(Arc &arc) const { |
---|
| 524 | arc._id = arcs[arc._id].next_out; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | void firstIn(Arc &arc, const Node& v) const { |
---|
| 528 | arc._id = ((nodes[v._id].first_out) ^ 1); |
---|
| 529 | if (arc._id == -2) arc._id = -1; |
---|
| 530 | } |
---|
| 531 | void nextIn(Arc &arc) const { |
---|
| 532 | arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
---|
| 533 | if (arc._id == -2) arc._id = -1; |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | void firstInc(Edge &arc, bool& d, const Node& v) const { |
---|
| 537 | int de = nodes[v._id].first_out; |
---|
| 538 | if (de != -1) { |
---|
| 539 | arc._id = de / 2; |
---|
| 540 | d = ((de & 1) == 1); |
---|
| 541 | } else { |
---|
| 542 | arc._id = -1; |
---|
| 543 | d = true; |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | void nextInc(Edge &arc, bool& d) const { |
---|
| 547 | int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
---|
| 548 | if (de != -1) { |
---|
| 549 | arc._id = de / 2; |
---|
| 550 | d = ((de & 1) == 1); |
---|
| 551 | } else { |
---|
| 552 | arc._id = -1; |
---|
[209] | 553 | d = true; |
---|
[109] | 554 | } |
---|
| 555 | } |
---|
[209] | 556 | |
---|
[109] | 557 | static int id(Node v) { return v._id; } |
---|
| 558 | static int id(Arc e) { return e._id; } |
---|
| 559 | static int id(Edge e) { return e._id; } |
---|
| 560 | |
---|
| 561 | static Node nodeFromId(int id) { return Node(id);} |
---|
| 562 | static Arc arcFromId(int id) { return Arc(id);} |
---|
| 563 | static Edge edgeFromId(int id) { return Edge(id);} |
---|
| 564 | |
---|
[209] | 565 | bool valid(Node n) const { |
---|
| 566 | return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
---|
[149] | 567 | } |
---|
[209] | 568 | bool valid(Arc a) const { |
---|
[149] | 569 | return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
---|
| 570 | } |
---|
[209] | 571 | bool valid(Edge e) const { |
---|
| 572 | return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
---|
[149] | 573 | } |
---|
| 574 | |
---|
[209] | 575 | Node addNode() { |
---|
[109] | 576 | int n = nodes.size(); |
---|
| 577 | nodes.push_back(NodeT()); |
---|
| 578 | nodes[n].first_out = -1; |
---|
[209] | 579 | |
---|
[109] | 580 | return Node(n); |
---|
| 581 | } |
---|
[209] | 582 | |
---|
[138] | 583 | Edge addEdge(Node u, Node v) { |
---|
[109] | 584 | int n = arcs.size(); |
---|
| 585 | arcs.push_back(ArcT()); |
---|
| 586 | arcs.push_back(ArcT()); |
---|
[209] | 587 | |
---|
[109] | 588 | arcs[n].target = u._id; |
---|
| 589 | arcs[n | 1].target = v._id; |
---|
| 590 | |
---|
| 591 | arcs[n].next_out = nodes[v._id].first_out; |
---|
| 592 | nodes[v._id].first_out = n; |
---|
| 593 | |
---|
[209] | 594 | arcs[n | 1].next_out = nodes[u._id].first_out; |
---|
[109] | 595 | nodes[u._id].first_out = (n | 1); |
---|
| 596 | |
---|
| 597 | return Edge(n / 2); |
---|
| 598 | } |
---|
[209] | 599 | |
---|
[109] | 600 | void clear() { |
---|
| 601 | arcs.clear(); |
---|
| 602 | nodes.clear(); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | }; |
---|
| 606 | |
---|
| 607 | typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
---|
| 608 | |
---|
| 609 | /// \ingroup graphs |
---|
| 610 | /// |
---|
| 611 | /// \brief A smart undirected graph class. |
---|
| 612 | /// |
---|
[735] | 613 | /// \ref SmartGraph is a simple and fast graph implementation. |
---|
| 614 | /// It is also quite memory efficient but at the price |
---|
| 615 | /// that it does not support node and edge deletion |
---|
| 616 | /// (except for the Snapshot feature). |
---|
[109] | 617 | /// |
---|
[735] | 618 | /// This type fully conforms to the \ref concepts::Graph "Graph concept" |
---|
| 619 | /// and it also provides some additional functionalities. |
---|
| 620 | /// Most of its member functions and nested classes are documented |
---|
| 621 | /// only in the concept class. |
---|
| 622 | /// |
---|
| 623 | /// \sa concepts::Graph |
---|
| 624 | /// \sa SmartDigraph |
---|
[109] | 625 | class SmartGraph : public ExtendedSmartGraphBase { |
---|
[617] | 626 | typedef ExtendedSmartGraphBase Parent; |
---|
| 627 | |
---|
[109] | 628 | private: |
---|
[735] | 629 | /// Graphs are \e not copy constructible. Use GraphCopy instead. |
---|
[109] | 630 | SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {}; |
---|
[735] | 631 | /// \brief Assignment of a graph to another one is \e not allowed. |
---|
| 632 | /// Use GraphCopy instead. |
---|
[109] | 633 | void operator=(const SmartGraph &) {} |
---|
| 634 | |
---|
| 635 | public: |
---|
| 636 | |
---|
| 637 | /// Constructor |
---|
[209] | 638 | |
---|
[109] | 639 | /// Constructor. |
---|
| 640 | /// |
---|
| 641 | SmartGraph() {} |
---|
| 642 | |
---|
[735] | 643 | /// \brief Add a new node to the graph. |
---|
| 644 | /// |
---|
| 645 | /// This function adds a new node to the graph. |
---|
[559] | 646 | /// \return The new node. |
---|
[109] | 647 | Node addNode() { return Parent::addNode(); } |
---|
[209] | 648 | |
---|
[735] | 649 | /// \brief Add a new edge to the graph. |
---|
| 650 | /// |
---|
| 651 | /// This function adds a new edge to the graph between nodes |
---|
| 652 | /// \c u and \c v with inherent orientation from node \c u to |
---|
| 653 | /// node \c v. |
---|
| 654 | /// \return The new edge. |
---|
| 655 | Edge addEdge(Node u, Node v) { |
---|
| 656 | return Parent::addEdge(u, v); |
---|
[109] | 657 | } |
---|
| 658 | |
---|
[149] | 659 | /// \brief Node validity check |
---|
| 660 | /// |
---|
[735] | 661 | /// This function gives back \c true if the given node is valid, |
---|
| 662 | /// i.e. it is a real node of the graph. |
---|
[149] | 663 | /// |
---|
| 664 | /// \warning A removed node (using Snapshot) could become valid again |
---|
[735] | 665 | /// if new nodes are added to the graph. |
---|
[149] | 666 | bool valid(Node n) const { return Parent::valid(n); } |
---|
| 667 | |
---|
[735] | 668 | /// \brief Edge validity check |
---|
| 669 | /// |
---|
| 670 | /// This function gives back \c true if the given edge is valid, |
---|
| 671 | /// i.e. it is a real edge of the graph. |
---|
| 672 | /// |
---|
| 673 | /// \warning A removed edge (using Snapshot) could become valid again |
---|
| 674 | /// if new edges are added to the graph. |
---|
| 675 | bool valid(Edge e) const { return Parent::valid(e); } |
---|
| 676 | |
---|
[149] | 677 | /// \brief Arc validity check |
---|
| 678 | /// |
---|
[735] | 679 | /// This function gives back \c true if the given arc is valid, |
---|
| 680 | /// i.e. it is a real arc of the graph. |
---|
[149] | 681 | /// |
---|
| 682 | /// \warning A removed arc (using Snapshot) could become valid again |
---|
[735] | 683 | /// if new edges are added to the graph. |
---|
[149] | 684 | bool valid(Arc a) const { return Parent::valid(a); } |
---|
| 685 | |
---|
[109] | 686 | ///Clear the graph. |
---|
[209] | 687 | |
---|
[735] | 688 | ///This function erases all nodes and arcs from the graph. |
---|
[109] | 689 | /// |
---|
| 690 | void clear() { |
---|
| 691 | Parent::clear(); |
---|
| 692 | } |
---|
| 693 | |
---|
[736] | 694 | /// Reserve memory for nodes. |
---|
| 695 | |
---|
| 696 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 697 | /// allocation: if you know that the graph you want to build will |
---|
| 698 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 699 | /// then it is worth reserving space for this amount before starting |
---|
| 700 | /// to build the graph. |
---|
| 701 | /// \sa reserveEdge() |
---|
| 702 | void reserveNode(int n) { nodes.reserve(n); }; |
---|
| 703 | |
---|
| 704 | /// Reserve memory for edges. |
---|
| 705 | |
---|
| 706 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 707 | /// allocation: if you know that the graph you want to build will |
---|
| 708 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 709 | /// then it is worth reserving space for this amount before starting |
---|
| 710 | /// to build the graph. |
---|
| 711 | /// \sa reserveNode() |
---|
| 712 | void reserveEdge(int m) { arcs.reserve(2 * m); }; |
---|
| 713 | |
---|
[109] | 714 | public: |
---|
[209] | 715 | |
---|
[109] | 716 | class Snapshot; |
---|
| 717 | |
---|
| 718 | protected: |
---|
| 719 | |
---|
| 720 | void saveSnapshot(Snapshot &s) |
---|
| 721 | { |
---|
| 722 | s._graph = this; |
---|
| 723 | s.node_num = nodes.size(); |
---|
| 724 | s.arc_num = arcs.size(); |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | void restoreSnapshot(const Snapshot &s) |
---|
| 728 | { |
---|
| 729 | while(s.arc_num<arcs.size()) { |
---|
| 730 | int n=arcs.size()-1; |
---|
| 731 | Edge arc=edgeFromId(n/2); |
---|
[209] | 732 | Parent::notifier(Edge()).erase(arc); |
---|
[109] | 733 | std::vector<Arc> dir; |
---|
| 734 | dir.push_back(arcFromId(n)); |
---|
| 735 | dir.push_back(arcFromId(n-1)); |
---|
[209] | 736 | Parent::notifier(Arc()).erase(dir); |
---|
[373] | 737 | nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
---|
| 738 | nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
---|
[209] | 739 | arcs.pop_back(); |
---|
| 740 | arcs.pop_back(); |
---|
[109] | 741 | } |
---|
| 742 | while(s.node_num<nodes.size()) { |
---|
| 743 | int n=nodes.size()-1; |
---|
| 744 | Node node = nodeFromId(n); |
---|
[209] | 745 | Parent::notifier(Node()).erase(node); |
---|
| 746 | nodes.pop_back(); |
---|
[109] | 747 | } |
---|
[209] | 748 | } |
---|
[109] | 749 | |
---|
| 750 | public: |
---|
| 751 | |
---|
[735] | 752 | ///Class to make a snapshot of the graph and to restore it later. |
---|
[109] | 753 | |
---|
[735] | 754 | ///Class to make a snapshot of the graph and to restore it later. |
---|
[109] | 755 | /// |
---|
[735] | 756 | ///The newly added nodes and edges can be removed using the |
---|
| 757 | ///restore() function. This is the only way for deleting nodes and/or |
---|
| 758 | ///edges from a SmartGraph structure. |
---|
[109] | 759 | /// |
---|
[735] | 760 | ///\note After a state is restored, you cannot restore a later state, |
---|
| 761 | ///i.e. you cannot add the removed nodes and edges again using |
---|
| 762 | ///another Snapshot instance. |
---|
[109] | 763 | /// |
---|
[735] | 764 | ///\warning The validity of the snapshot is not stored due to |
---|
| 765 | ///performance reasons. If you do not use the snapshot correctly, |
---|
| 766 | ///it can cause broken program, invalid or not restored state of |
---|
| 767 | ///the graph or no change. |
---|
[209] | 768 | class Snapshot |
---|
[109] | 769 | { |
---|
| 770 | SmartGraph *_graph; |
---|
| 771 | protected: |
---|
| 772 | friend class SmartGraph; |
---|
| 773 | unsigned int node_num; |
---|
| 774 | unsigned int arc_num; |
---|
| 775 | public: |
---|
| 776 | ///Default constructor. |
---|
[209] | 777 | |
---|
[109] | 778 | ///Default constructor. |
---|
[735] | 779 | ///You have to call save() to actually make a snapshot. |
---|
[109] | 780 | Snapshot() : _graph(0) {} |
---|
| 781 | ///Constructor that immediately makes a snapshot |
---|
[209] | 782 | |
---|
[735] | 783 | /// This constructor immediately makes a snapshot of the given graph. |
---|
| 784 | /// |
---|
| 785 | Snapshot(SmartGraph &gr) { |
---|
| 786 | gr.saveSnapshot(*this); |
---|
[109] | 787 | } |
---|
| 788 | |
---|
| 789 | ///Make a snapshot. |
---|
| 790 | |
---|
[735] | 791 | ///This function makes a snapshot of the given graph. |
---|
| 792 | ///It can be called more than once. In case of a repeated |
---|
[109] | 793 | ///call, the previous snapshot gets lost. |
---|
[735] | 794 | void save(SmartGraph &gr) |
---|
[109] | 795 | { |
---|
[735] | 796 | gr.saveSnapshot(*this); |
---|
[109] | 797 | } |
---|
| 798 | |
---|
[735] | 799 | ///Undo the changes until the last snapshot. |
---|
[209] | 800 | |
---|
[735] | 801 | ///This function undos the changes until the last snapshot |
---|
| 802 | ///created by save() or Snapshot(SmartGraph&). |
---|
[109] | 803 | void restore() |
---|
| 804 | { |
---|
| 805 | _graph->restoreSnapshot(*this); |
---|
| 806 | } |
---|
| 807 | }; |
---|
| 808 | }; |
---|
[209] | 809 | |
---|
[109] | 810 | } //namespace lemon |
---|
| 811 | |
---|
| 812 | |
---|
| 813 | #endif //LEMON_SMART_GRAPH_H |
---|