[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 | * |
---|
[877] | 5 | * Copyright (C) 2003-2010 |
---|
[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 |
---|
[877] | 189 | ///that it does not support node and arc deletion |
---|
[735] | 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 | /// |
---|
[787] | 197 | ///This class provides constant time counting for nodes and arcs. |
---|
| 198 | /// |
---|
[735] | 199 | ///\sa concepts::Digraph |
---|
| 200 | ///\sa SmartGraph |
---|
[109] | 201 | class SmartDigraph : public ExtendedSmartDigraphBase { |
---|
| 202 | typedef ExtendedSmartDigraphBase Parent; |
---|
| 203 | |
---|
| 204 | private: |
---|
[735] | 205 | /// Digraphs are \e not copy constructible. Use DigraphCopy instead. |
---|
[109] | 206 | SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {}; |
---|
[735] | 207 | /// \brief Assignment of a digraph to another one is \e not allowed. |
---|
| 208 | /// Use DigraphCopy instead. |
---|
[109] | 209 | void operator=(const SmartDigraph &) {} |
---|
| 210 | |
---|
| 211 | public: |
---|
[209] | 212 | |
---|
[109] | 213 | /// Constructor |
---|
[209] | 214 | |
---|
[109] | 215 | /// Constructor. |
---|
| 216 | /// |
---|
| 217 | SmartDigraph() {}; |
---|
[209] | 218 | |
---|
[109] | 219 | ///Add a new node to the digraph. |
---|
[209] | 220 | |
---|
[735] | 221 | ///This function adds a new node to the digraph. |
---|
| 222 | ///\return The new node. |
---|
[109] | 223 | Node addNode() { return Parent::addNode(); } |
---|
[209] | 224 | |
---|
[109] | 225 | ///Add a new arc to the digraph. |
---|
[209] | 226 | |
---|
[735] | 227 | ///This function adds a new arc to the digraph with source node \c s |
---|
[109] | 228 | ///and target node \c t. |
---|
[559] | 229 | ///\return The new arc. |
---|
[735] | 230 | Arc addArc(Node s, Node t) { |
---|
[209] | 231 | return Parent::addArc(s, t); |
---|
[109] | 232 | } |
---|
| 233 | |
---|
[149] | 234 | /// \brief Node validity check |
---|
| 235 | /// |
---|
[735] | 236 | /// This function gives back \c true if the given node is valid, |
---|
| 237 | /// i.e. it is a real node of the digraph. |
---|
[149] | 238 | /// |
---|
| 239 | /// \warning A removed node (using Snapshot) could become valid again |
---|
[735] | 240 | /// if new nodes are added to the digraph. |
---|
[149] | 241 | bool valid(Node n) const { return Parent::valid(n); } |
---|
| 242 | |
---|
| 243 | /// \brief Arc validity check |
---|
| 244 | /// |
---|
[735] | 245 | /// This function gives back \c true if the given arc is valid, |
---|
| 246 | /// i.e. it is a real arc of the digraph. |
---|
[149] | 247 | /// |
---|
| 248 | /// \warning A removed arc (using Snapshot) could become valid again |
---|
[735] | 249 | /// if new arcs are added to the graph. |
---|
[149] | 250 | bool valid(Arc a) const { return Parent::valid(a); } |
---|
| 251 | |
---|
[109] | 252 | ///Split a node. |
---|
[209] | 253 | |
---|
[735] | 254 | ///This function splits the given node. First, a new node is added |
---|
| 255 | ///to the digraph, then the source of each outgoing arc of node \c n |
---|
| 256 | ///is moved to this new node. |
---|
| 257 | ///If the second parameter \c connect is \c true (this is the default |
---|
| 258 | ///value), then a new arc from node \c n to the newly created node |
---|
| 259 | ///is also added. |
---|
[109] | 260 | ///\return The newly created node. |
---|
| 261 | /// |
---|
[735] | 262 | ///\note All iterators remain valid. |
---|
| 263 | /// |
---|
[109] | 264 | ///\warning This functionality cannot be used together with the Snapshot |
---|
| 265 | ///feature. |
---|
| 266 | Node split(Node n, bool connect = true) |
---|
| 267 | { |
---|
| 268 | Node b = addNode(); |
---|
| 269 | nodes[b._id].first_out=nodes[n._id].first_out; |
---|
| 270 | nodes[n._id].first_out=-1; |
---|
[370] | 271 | for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) { |
---|
| 272 | arcs[i].source=b._id; |
---|
| 273 | } |
---|
[109] | 274 | if(connect) addArc(n,b); |
---|
| 275 | return b; |
---|
| 276 | } |
---|
| 277 | |
---|
[735] | 278 | ///Clear the digraph. |
---|
| 279 | |
---|
| 280 | ///This function erases all nodes and arcs from the digraph. |
---|
| 281 | /// |
---|
| 282 | void clear() { |
---|
| 283 | Parent::clear(); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | /// Reserve memory for nodes. |
---|
| 287 | |
---|
| 288 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 289 | /// allocation: if you know that the digraph you want to build will |
---|
| 290 | /// be large (e.g. it will contain millions of nodes and/or arcs), |
---|
| 291 | /// then it is worth reserving space for this amount before starting |
---|
| 292 | /// to build the digraph. |
---|
| 293 | /// \sa reserveArc() |
---|
| 294 | void reserveNode(int n) { nodes.reserve(n); }; |
---|
| 295 | |
---|
| 296 | /// Reserve memory for arcs. |
---|
| 297 | |
---|
| 298 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 299 | /// allocation: if you know that the digraph you want to build will |
---|
| 300 | /// be large (e.g. it will contain millions of nodes and/or arcs), |
---|
| 301 | /// then it is worth reserving space for this amount before starting |
---|
| 302 | /// to build the digraph. |
---|
| 303 | /// \sa reserveNode() |
---|
| 304 | void reserveArc(int m) { arcs.reserve(m); }; |
---|
| 305 | |
---|
[109] | 306 | public: |
---|
[209] | 307 | |
---|
[109] | 308 | class Snapshot; |
---|
| 309 | |
---|
| 310 | protected: |
---|
| 311 | |
---|
| 312 | void restoreSnapshot(const Snapshot &s) |
---|
| 313 | { |
---|
| 314 | while(s.arc_num<arcs.size()) { |
---|
| 315 | Arc arc = arcFromId(arcs.size()-1); |
---|
[209] | 316 | Parent::notifier(Arc()).erase(arc); |
---|
| 317 | nodes[arcs.back().source].first_out=arcs.back().next_out; |
---|
| 318 | nodes[arcs.back().target].first_in=arcs.back().next_in; |
---|
| 319 | arcs.pop_back(); |
---|
[109] | 320 | } |
---|
| 321 | while(s.node_num<nodes.size()) { |
---|
| 322 | Node node = nodeFromId(nodes.size()-1); |
---|
[209] | 323 | Parent::notifier(Node()).erase(node); |
---|
| 324 | nodes.pop_back(); |
---|
[109] | 325 | } |
---|
[209] | 326 | } |
---|
[109] | 327 | |
---|
| 328 | public: |
---|
| 329 | |
---|
[735] | 330 | ///Class to make a snapshot of the digraph and to restore it later. |
---|
[109] | 331 | |
---|
[735] | 332 | ///Class to make a snapshot of the digraph and to restore it later. |
---|
[109] | 333 | /// |
---|
| 334 | ///The newly added nodes and arcs can be removed using the |
---|
[735] | 335 | ///restore() function. This is the only way for deleting nodes and/or |
---|
| 336 | ///arcs from a SmartDigraph structure. |
---|
[109] | 337 | /// |
---|
[877] | 338 | ///\note After a state is restored, you cannot restore a later state, |
---|
[735] | 339 | ///i.e. you cannot add the removed nodes and arcs again using |
---|
| 340 | ///another Snapshot instance. |
---|
| 341 | /// |
---|
| 342 | ///\warning Node splitting cannot be restored. |
---|
| 343 | ///\warning The validity of the snapshot is not stored due to |
---|
| 344 | ///performance reasons. If you do not use the snapshot correctly, |
---|
| 345 | ///it can cause broken program, invalid or not restored state of |
---|
| 346 | ///the digraph or no change. |
---|
[209] | 347 | class Snapshot |
---|
[109] | 348 | { |
---|
| 349 | SmartDigraph *_graph; |
---|
| 350 | protected: |
---|
| 351 | friend class SmartDigraph; |
---|
| 352 | unsigned int node_num; |
---|
| 353 | unsigned int arc_num; |
---|
| 354 | public: |
---|
| 355 | ///Default constructor. |
---|
[209] | 356 | |
---|
[109] | 357 | ///Default constructor. |
---|
[735] | 358 | ///You have to call save() to actually make a snapshot. |
---|
[109] | 359 | Snapshot() : _graph(0) {} |
---|
| 360 | ///Constructor that immediately makes a snapshot |
---|
[209] | 361 | |
---|
[735] | 362 | ///This constructor immediately makes a snapshot of the given digraph. |
---|
| 363 | /// |
---|
| 364 | Snapshot(SmartDigraph &gr) : _graph(&gr) { |
---|
[209] | 365 | node_num=_graph->nodes.size(); |
---|
| 366 | arc_num=_graph->arcs.size(); |
---|
[109] | 367 | } |
---|
| 368 | |
---|
| 369 | ///Make a snapshot. |
---|
| 370 | |
---|
[735] | 371 | ///This function makes a snapshot of the given digraph. |
---|
| 372 | ///It can be called more than once. In case of a repeated |
---|
[109] | 373 | ///call, the previous snapshot gets lost. |
---|
[735] | 374 | void save(SmartDigraph &gr) { |
---|
| 375 | _graph=&gr; |
---|
[209] | 376 | node_num=_graph->nodes.size(); |
---|
| 377 | arc_num=_graph->arcs.size(); |
---|
[109] | 378 | } |
---|
| 379 | |
---|
| 380 | ///Undo the changes until a snapshot. |
---|
[209] | 381 | |
---|
[735] | 382 | ///This function undos the changes until the last snapshot |
---|
| 383 | ///created by save() or Snapshot(SmartDigraph&). |
---|
[109] | 384 | void restore() |
---|
| 385 | { |
---|
[209] | 386 | _graph->restoreSnapshot(*this); |
---|
[109] | 387 | } |
---|
| 388 | }; |
---|
| 389 | }; |
---|
| 390 | |
---|
| 391 | |
---|
| 392 | class SmartGraphBase { |
---|
| 393 | |
---|
| 394 | protected: |
---|
| 395 | |
---|
| 396 | struct NodeT { |
---|
| 397 | int first_out; |
---|
| 398 | }; |
---|
[209] | 399 | |
---|
[109] | 400 | struct ArcT { |
---|
| 401 | int target; |
---|
| 402 | int next_out; |
---|
| 403 | }; |
---|
| 404 | |
---|
| 405 | std::vector<NodeT> nodes; |
---|
| 406 | std::vector<ArcT> arcs; |
---|
| 407 | |
---|
| 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 | |
---|
[778] | 500 | static void next(Node& node) { |
---|
[109] | 501 | --node._id; |
---|
| 502 | } |
---|
| 503 | |
---|
[209] | 504 | void first(Arc& arc) const { |
---|
[109] | 505 | arc._id = arcs.size() - 1; |
---|
| 506 | } |
---|
| 507 | |
---|
[778] | 508 | static void next(Arc& arc) { |
---|
[109] | 509 | --arc._id; |
---|
| 510 | } |
---|
| 511 | |
---|
[209] | 512 | void first(Edge& arc) const { |
---|
[109] | 513 | arc._id = arcs.size() / 2 - 1; |
---|
| 514 | } |
---|
| 515 | |
---|
[778] | 516 | static void next(Edge& arc) { |
---|
[109] | 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 |
---|
[877] | 615 | /// that it does not support node and edge deletion |
---|
[735] | 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 | /// |
---|
[787] | 623 | /// This class provides constant time counting for nodes, edges and arcs. |
---|
| 624 | /// |
---|
[735] | 625 | /// \sa concepts::Graph |
---|
| 626 | /// \sa SmartDigraph |
---|
[109] | 627 | class SmartGraph : public ExtendedSmartGraphBase { |
---|
[617] | 628 | typedef ExtendedSmartGraphBase Parent; |
---|
| 629 | |
---|
[109] | 630 | private: |
---|
[735] | 631 | /// Graphs are \e not copy constructible. Use GraphCopy instead. |
---|
[109] | 632 | SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {}; |
---|
[735] | 633 | /// \brief Assignment of a graph to another one is \e not allowed. |
---|
| 634 | /// Use GraphCopy instead. |
---|
[109] | 635 | void operator=(const SmartGraph &) {} |
---|
| 636 | |
---|
| 637 | public: |
---|
| 638 | |
---|
| 639 | /// Constructor |
---|
[209] | 640 | |
---|
[109] | 641 | /// Constructor. |
---|
| 642 | /// |
---|
| 643 | SmartGraph() {} |
---|
| 644 | |
---|
[735] | 645 | /// \brief Add a new node to the graph. |
---|
| 646 | /// |
---|
| 647 | /// This function adds a new node to the graph. |
---|
[559] | 648 | /// \return The new node. |
---|
[109] | 649 | Node addNode() { return Parent::addNode(); } |
---|
[209] | 650 | |
---|
[735] | 651 | /// \brief Add a new edge to the graph. |
---|
| 652 | /// |
---|
| 653 | /// This function adds a new edge to the graph between nodes |
---|
| 654 | /// \c u and \c v with inherent orientation from node \c u to |
---|
| 655 | /// node \c v. |
---|
| 656 | /// \return The new edge. |
---|
| 657 | Edge addEdge(Node u, Node v) { |
---|
| 658 | return Parent::addEdge(u, v); |
---|
[109] | 659 | } |
---|
| 660 | |
---|
[149] | 661 | /// \brief Node validity check |
---|
| 662 | /// |
---|
[735] | 663 | /// This function gives back \c true if the given node is valid, |
---|
| 664 | /// i.e. it is a real node of the graph. |
---|
[149] | 665 | /// |
---|
| 666 | /// \warning A removed node (using Snapshot) could become valid again |
---|
[735] | 667 | /// if new nodes are added to the graph. |
---|
[149] | 668 | bool valid(Node n) const { return Parent::valid(n); } |
---|
| 669 | |
---|
[735] | 670 | /// \brief Edge validity check |
---|
| 671 | /// |
---|
| 672 | /// This function gives back \c true if the given edge is valid, |
---|
| 673 | /// i.e. it is a real edge of the graph. |
---|
| 674 | /// |
---|
| 675 | /// \warning A removed edge (using Snapshot) could become valid again |
---|
| 676 | /// if new edges are added to the graph. |
---|
| 677 | bool valid(Edge e) const { return Parent::valid(e); } |
---|
| 678 | |
---|
[149] | 679 | /// \brief Arc validity check |
---|
| 680 | /// |
---|
[735] | 681 | /// This function gives back \c true if the given arc is valid, |
---|
| 682 | /// i.e. it is a real arc of the graph. |
---|
[149] | 683 | /// |
---|
| 684 | /// \warning A removed arc (using Snapshot) could become valid again |
---|
[735] | 685 | /// if new edges are added to the graph. |
---|
[149] | 686 | bool valid(Arc a) const { return Parent::valid(a); } |
---|
| 687 | |
---|
[109] | 688 | ///Clear the graph. |
---|
[209] | 689 | |
---|
[735] | 690 | ///This function erases all nodes and arcs from the graph. |
---|
[109] | 691 | /// |
---|
| 692 | void clear() { |
---|
| 693 | Parent::clear(); |
---|
| 694 | } |
---|
| 695 | |
---|
[736] | 696 | /// Reserve memory for nodes. |
---|
| 697 | |
---|
| 698 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 699 | /// allocation: if you know that the graph you want to build will |
---|
| 700 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 701 | /// then it is worth reserving space for this amount before starting |
---|
| 702 | /// to build the graph. |
---|
| 703 | /// \sa reserveEdge() |
---|
| 704 | void reserveNode(int n) { nodes.reserve(n); }; |
---|
| 705 | |
---|
| 706 | /// Reserve memory for edges. |
---|
| 707 | |
---|
| 708 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 709 | /// allocation: if you know that the graph you want to build will |
---|
| 710 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 711 | /// then it is worth reserving space for this amount before starting |
---|
| 712 | /// to build the graph. |
---|
| 713 | /// \sa reserveNode() |
---|
| 714 | void reserveEdge(int m) { arcs.reserve(2 * m); }; |
---|
| 715 | |
---|
[109] | 716 | public: |
---|
[209] | 717 | |
---|
[109] | 718 | class Snapshot; |
---|
| 719 | |
---|
| 720 | protected: |
---|
| 721 | |
---|
| 722 | void saveSnapshot(Snapshot &s) |
---|
| 723 | { |
---|
| 724 | s._graph = this; |
---|
| 725 | s.node_num = nodes.size(); |
---|
| 726 | s.arc_num = arcs.size(); |
---|
| 727 | } |
---|
| 728 | |
---|
| 729 | void restoreSnapshot(const Snapshot &s) |
---|
| 730 | { |
---|
| 731 | while(s.arc_num<arcs.size()) { |
---|
| 732 | int n=arcs.size()-1; |
---|
| 733 | Edge arc=edgeFromId(n/2); |
---|
[209] | 734 | Parent::notifier(Edge()).erase(arc); |
---|
[109] | 735 | std::vector<Arc> dir; |
---|
| 736 | dir.push_back(arcFromId(n)); |
---|
| 737 | dir.push_back(arcFromId(n-1)); |
---|
[209] | 738 | Parent::notifier(Arc()).erase(dir); |
---|
[373] | 739 | nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
---|
| 740 | nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
---|
[209] | 741 | arcs.pop_back(); |
---|
| 742 | arcs.pop_back(); |
---|
[109] | 743 | } |
---|
| 744 | while(s.node_num<nodes.size()) { |
---|
| 745 | int n=nodes.size()-1; |
---|
| 746 | Node node = nodeFromId(n); |
---|
[209] | 747 | Parent::notifier(Node()).erase(node); |
---|
| 748 | nodes.pop_back(); |
---|
[109] | 749 | } |
---|
[209] | 750 | } |
---|
[109] | 751 | |
---|
| 752 | public: |
---|
| 753 | |
---|
[735] | 754 | ///Class to make a snapshot of the graph and to restore it later. |
---|
[109] | 755 | |
---|
[735] | 756 | ///Class to make a snapshot of the graph and to restore it later. |
---|
[109] | 757 | /// |
---|
[735] | 758 | ///The newly added nodes and edges can be removed using the |
---|
| 759 | ///restore() function. This is the only way for deleting nodes and/or |
---|
| 760 | ///edges from a SmartGraph structure. |
---|
[109] | 761 | /// |
---|
[877] | 762 | ///\note After a state is restored, you cannot restore a later state, |
---|
[735] | 763 | ///i.e. you cannot add the removed nodes and edges again using |
---|
| 764 | ///another Snapshot instance. |
---|
[109] | 765 | /// |
---|
[735] | 766 | ///\warning The validity of the snapshot is not stored due to |
---|
| 767 | ///performance reasons. If you do not use the snapshot correctly, |
---|
| 768 | ///it can cause broken program, invalid or not restored state of |
---|
| 769 | ///the graph or no change. |
---|
[209] | 770 | class Snapshot |
---|
[109] | 771 | { |
---|
| 772 | SmartGraph *_graph; |
---|
| 773 | protected: |
---|
| 774 | friend class SmartGraph; |
---|
| 775 | unsigned int node_num; |
---|
| 776 | unsigned int arc_num; |
---|
| 777 | public: |
---|
| 778 | ///Default constructor. |
---|
[209] | 779 | |
---|
[109] | 780 | ///Default constructor. |
---|
[735] | 781 | ///You have to call save() to actually make a snapshot. |
---|
[109] | 782 | Snapshot() : _graph(0) {} |
---|
| 783 | ///Constructor that immediately makes a snapshot |
---|
[209] | 784 | |
---|
[735] | 785 | /// This constructor immediately makes a snapshot of the given graph. |
---|
| 786 | /// |
---|
| 787 | Snapshot(SmartGraph &gr) { |
---|
| 788 | gr.saveSnapshot(*this); |
---|
[109] | 789 | } |
---|
| 790 | |
---|
| 791 | ///Make a snapshot. |
---|
| 792 | |
---|
[735] | 793 | ///This function makes a snapshot of the given graph. |
---|
| 794 | ///It can be called more than once. In case of a repeated |
---|
[109] | 795 | ///call, the previous snapshot gets lost. |
---|
[735] | 796 | void save(SmartGraph &gr) |
---|
[109] | 797 | { |
---|
[735] | 798 | gr.saveSnapshot(*this); |
---|
[109] | 799 | } |
---|
| 800 | |
---|
[735] | 801 | ///Undo the changes until the last snapshot. |
---|
[209] | 802 | |
---|
[735] | 803 | ///This function undos the changes until the last snapshot |
---|
| 804 | ///created by save() or Snapshot(SmartGraph&). |
---|
[109] | 805 | void restore() |
---|
| 806 | { |
---|
| 807 | _graph->restoreSnapshot(*this); |
---|
| 808 | } |
---|
| 809 | }; |
---|
| 810 | }; |
---|
[209] | 811 | |
---|
[1019] | 812 | class SmartBpGraphBase { |
---|
| 813 | |
---|
| 814 | protected: |
---|
| 815 | |
---|
| 816 | struct NodeT { |
---|
| 817 | int first_out; |
---|
| 818 | int partition_next; |
---|
| 819 | int partition_index; |
---|
| 820 | bool red; |
---|
| 821 | }; |
---|
| 822 | |
---|
| 823 | struct ArcT { |
---|
| 824 | int target; |
---|
| 825 | int next_out; |
---|
| 826 | }; |
---|
| 827 | |
---|
| 828 | std::vector<NodeT> nodes; |
---|
| 829 | std::vector<ArcT> arcs; |
---|
| 830 | |
---|
| 831 | int first_red, first_blue; |
---|
[1023] | 832 | int max_red, max_blue; |
---|
[1019] | 833 | |
---|
| 834 | public: |
---|
| 835 | |
---|
| 836 | typedef SmartBpGraphBase Graph; |
---|
| 837 | |
---|
| 838 | class Node; |
---|
| 839 | class Arc; |
---|
| 840 | class Edge; |
---|
| 841 | |
---|
| 842 | class Node { |
---|
| 843 | friend class SmartBpGraphBase; |
---|
| 844 | protected: |
---|
| 845 | |
---|
| 846 | int _id; |
---|
| 847 | explicit Node(int id) { _id = id;} |
---|
| 848 | |
---|
| 849 | public: |
---|
| 850 | Node() {} |
---|
| 851 | Node (Invalid) { _id = -1; } |
---|
| 852 | bool operator==(const Node& node) const {return _id == node._id;} |
---|
| 853 | bool operator!=(const Node& node) const {return _id != node._id;} |
---|
| 854 | bool operator<(const Node& node) const {return _id < node._id;} |
---|
| 855 | }; |
---|
| 856 | |
---|
| 857 | class Edge { |
---|
| 858 | friend class SmartBpGraphBase; |
---|
| 859 | protected: |
---|
| 860 | |
---|
| 861 | int _id; |
---|
| 862 | explicit Edge(int id) { _id = id;} |
---|
| 863 | |
---|
| 864 | public: |
---|
| 865 | Edge() {} |
---|
| 866 | Edge (Invalid) { _id = -1; } |
---|
| 867 | bool operator==(const Edge& arc) const {return _id == arc._id;} |
---|
| 868 | bool operator!=(const Edge& arc) const {return _id != arc._id;} |
---|
| 869 | bool operator<(const Edge& arc) const {return _id < arc._id;} |
---|
| 870 | }; |
---|
| 871 | |
---|
| 872 | class Arc { |
---|
| 873 | friend class SmartBpGraphBase; |
---|
| 874 | protected: |
---|
| 875 | |
---|
| 876 | int _id; |
---|
| 877 | explicit Arc(int id) { _id = id;} |
---|
| 878 | |
---|
| 879 | public: |
---|
| 880 | operator Edge() const { |
---|
| 881 | return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
---|
| 882 | } |
---|
| 883 | |
---|
| 884 | Arc() {} |
---|
| 885 | Arc (Invalid) { _id = -1; } |
---|
| 886 | bool operator==(const Arc& arc) const {return _id == arc._id;} |
---|
| 887 | bool operator!=(const Arc& arc) const {return _id != arc._id;} |
---|
| 888 | bool operator<(const Arc& arc) const {return _id < arc._id;} |
---|
| 889 | }; |
---|
| 890 | |
---|
| 891 | |
---|
| 892 | |
---|
| 893 | SmartBpGraphBase() |
---|
[1023] | 894 | : nodes(), arcs(), first_red(-1), first_blue(-1), |
---|
| 895 | max_red(-1), max_blue(-1) {} |
---|
[1019] | 896 | |
---|
| 897 | typedef True NodeNumTag; |
---|
| 898 | typedef True EdgeNumTag; |
---|
| 899 | typedef True ArcNumTag; |
---|
| 900 | |
---|
| 901 | int nodeNum() const { return nodes.size(); } |
---|
[1023] | 902 | int redNum() const { return max_red + 1; } |
---|
| 903 | int blueNum() const { return max_blue + 1; } |
---|
[1019] | 904 | int edgeNum() const { return arcs.size() / 2; } |
---|
| 905 | int arcNum() const { return arcs.size(); } |
---|
| 906 | |
---|
| 907 | int maxNodeId() const { return nodes.size()-1; } |
---|
[1023] | 908 | int maxRedId() const { return max_red; } |
---|
| 909 | int maxBlueId() const { return max_blue; } |
---|
[1019] | 910 | int maxEdgeId() const { return arcs.size() / 2 - 1; } |
---|
| 911 | int maxArcId() const { return arcs.size()-1; } |
---|
| 912 | |
---|
| 913 | bool red(Node n) const { return nodes[n._id].red; } |
---|
| 914 | bool blue(Node n) const { return !nodes[n._id].red; } |
---|
| 915 | |
---|
| 916 | Node source(Arc a) const { return Node(arcs[a._id ^ 1].target); } |
---|
| 917 | Node target(Arc a) const { return Node(arcs[a._id].target); } |
---|
| 918 | |
---|
| 919 | Node redNode(Edge e) const { return Node(arcs[2 * e._id].target); } |
---|
| 920 | Node blueNode(Edge e) const { return Node(arcs[2 * e._id + 1].target); } |
---|
| 921 | |
---|
| 922 | static bool direction(Arc a) { |
---|
| 923 | return (a._id & 1) == 1; |
---|
| 924 | } |
---|
| 925 | |
---|
| 926 | static Arc direct(Edge e, bool d) { |
---|
| 927 | return Arc(e._id * 2 + (d ? 1 : 0)); |
---|
| 928 | } |
---|
| 929 | |
---|
| 930 | void first(Node& node) const { |
---|
| 931 | node._id = nodes.size() - 1; |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | static void next(Node& node) { |
---|
| 935 | --node._id; |
---|
| 936 | } |
---|
| 937 | |
---|
| 938 | void firstRed(Node& node) const { |
---|
| 939 | node._id = first_red; |
---|
| 940 | } |
---|
| 941 | |
---|
| 942 | void nextRed(Node& node) const { |
---|
| 943 | node._id = nodes[node._id].partition_next; |
---|
| 944 | } |
---|
| 945 | |
---|
| 946 | void firstBlue(Node& node) const { |
---|
| 947 | node._id = first_blue; |
---|
| 948 | } |
---|
| 949 | |
---|
| 950 | void nextBlue(Node& node) const { |
---|
| 951 | node._id = nodes[node._id].partition_next; |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | void first(Arc& arc) const { |
---|
| 955 | arc._id = arcs.size() - 1; |
---|
| 956 | } |
---|
| 957 | |
---|
| 958 | static void next(Arc& arc) { |
---|
| 959 | --arc._id; |
---|
| 960 | } |
---|
| 961 | |
---|
| 962 | void first(Edge& arc) const { |
---|
| 963 | arc._id = arcs.size() / 2 - 1; |
---|
| 964 | } |
---|
| 965 | |
---|
| 966 | static void next(Edge& arc) { |
---|
| 967 | --arc._id; |
---|
| 968 | } |
---|
| 969 | |
---|
| 970 | void firstOut(Arc &arc, const Node& v) const { |
---|
| 971 | arc._id = nodes[v._id].first_out; |
---|
| 972 | } |
---|
| 973 | void nextOut(Arc &arc) const { |
---|
| 974 | arc._id = arcs[arc._id].next_out; |
---|
| 975 | } |
---|
| 976 | |
---|
| 977 | void firstIn(Arc &arc, const Node& v) const { |
---|
| 978 | arc._id = ((nodes[v._id].first_out) ^ 1); |
---|
| 979 | if (arc._id == -2) arc._id = -1; |
---|
| 980 | } |
---|
| 981 | void nextIn(Arc &arc) const { |
---|
| 982 | arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
---|
| 983 | if (arc._id == -2) arc._id = -1; |
---|
| 984 | } |
---|
| 985 | |
---|
| 986 | void firstInc(Edge &arc, bool& d, const Node& v) const { |
---|
| 987 | int de = nodes[v._id].first_out; |
---|
| 988 | if (de != -1) { |
---|
| 989 | arc._id = de / 2; |
---|
| 990 | d = ((de & 1) == 1); |
---|
| 991 | } else { |
---|
| 992 | arc._id = -1; |
---|
| 993 | d = true; |
---|
| 994 | } |
---|
| 995 | } |
---|
| 996 | void nextInc(Edge &arc, bool& d) const { |
---|
| 997 | int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
---|
| 998 | if (de != -1) { |
---|
| 999 | arc._id = de / 2; |
---|
| 1000 | d = ((de & 1) == 1); |
---|
| 1001 | } else { |
---|
| 1002 | arc._id = -1; |
---|
| 1003 | d = true; |
---|
| 1004 | } |
---|
| 1005 | } |
---|
| 1006 | |
---|
| 1007 | static int id(Node v) { return v._id; } |
---|
| 1008 | int redId(Node v) const { |
---|
| 1009 | LEMON_DEBUG(nodes[v._id].red, "Node has to be red"); |
---|
| 1010 | return nodes[v._id].partition_index; |
---|
| 1011 | } |
---|
| 1012 | int blueId(Node v) const { |
---|
[1021] | 1013 | LEMON_DEBUG(!nodes[v._id].red, "Node has to be blue"); |
---|
[1019] | 1014 | return nodes[v._id].partition_index; |
---|
| 1015 | } |
---|
| 1016 | static int id(Arc e) { return e._id; } |
---|
| 1017 | static int id(Edge e) { return e._id; } |
---|
| 1018 | |
---|
| 1019 | static Node nodeFromId(int id) { return Node(id);} |
---|
| 1020 | static Arc arcFromId(int id) { return Arc(id);} |
---|
| 1021 | static Edge edgeFromId(int id) { return Edge(id);} |
---|
| 1022 | |
---|
| 1023 | bool valid(Node n) const { |
---|
| 1024 | return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
---|
| 1025 | } |
---|
| 1026 | bool valid(Arc a) const { |
---|
| 1027 | return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
---|
| 1028 | } |
---|
| 1029 | bool valid(Edge e) const { |
---|
| 1030 | return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | Node addRedNode() { |
---|
| 1034 | int n = nodes.size(); |
---|
| 1035 | nodes.push_back(NodeT()); |
---|
| 1036 | nodes[n].first_out = -1; |
---|
| 1037 | nodes[n].red = true; |
---|
[1023] | 1038 | nodes[n].partition_index = ++max_red; |
---|
[1019] | 1039 | nodes[n].partition_next = first_red; |
---|
| 1040 | first_red = n; |
---|
| 1041 | |
---|
| 1042 | return Node(n); |
---|
| 1043 | } |
---|
| 1044 | |
---|
| 1045 | Node addBlueNode() { |
---|
| 1046 | int n = nodes.size(); |
---|
| 1047 | nodes.push_back(NodeT()); |
---|
| 1048 | nodes[n].first_out = -1; |
---|
| 1049 | nodes[n].red = false; |
---|
[1023] | 1050 | nodes[n].partition_index = ++max_blue; |
---|
[1019] | 1051 | nodes[n].partition_next = first_blue; |
---|
| 1052 | first_blue = n; |
---|
| 1053 | |
---|
| 1054 | return Node(n); |
---|
| 1055 | } |
---|
| 1056 | |
---|
| 1057 | Edge addEdge(Node u, Node v) { |
---|
| 1058 | int n = arcs.size(); |
---|
| 1059 | arcs.push_back(ArcT()); |
---|
| 1060 | arcs.push_back(ArcT()); |
---|
| 1061 | |
---|
| 1062 | arcs[n].target = u._id; |
---|
| 1063 | arcs[n | 1].target = v._id; |
---|
| 1064 | |
---|
| 1065 | arcs[n].next_out = nodes[v._id].first_out; |
---|
| 1066 | nodes[v._id].first_out = n; |
---|
| 1067 | |
---|
| 1068 | arcs[n | 1].next_out = nodes[u._id].first_out; |
---|
| 1069 | nodes[u._id].first_out = (n | 1); |
---|
| 1070 | |
---|
| 1071 | return Edge(n / 2); |
---|
| 1072 | } |
---|
| 1073 | |
---|
| 1074 | void clear() { |
---|
| 1075 | arcs.clear(); |
---|
| 1076 | nodes.clear(); |
---|
| 1077 | first_red = -1; |
---|
| 1078 | first_blue = -1; |
---|
[1023] | 1079 | max_blue = -1; |
---|
| 1080 | max_red = -1; |
---|
[1019] | 1081 | } |
---|
| 1082 | |
---|
| 1083 | }; |
---|
| 1084 | |
---|
| 1085 | typedef BpGraphExtender<SmartBpGraphBase> ExtendedSmartBpGraphBase; |
---|
| 1086 | |
---|
| 1087 | /// \ingroup graphs |
---|
| 1088 | /// |
---|
[1020] | 1089 | /// \brief A smart undirected bipartite graph class. |
---|
[1019] | 1090 | /// |
---|
[1020] | 1091 | /// \ref SmartBpGraph is a simple and fast bipartite graph implementation. |
---|
[1019] | 1092 | /// It is also quite memory efficient but at the price |
---|
| 1093 | /// that it does not support node and edge deletion |
---|
| 1094 | /// (except for the Snapshot feature). |
---|
| 1095 | /// |
---|
[1020] | 1096 | /// This type fully conforms to the \ref concepts::BpGraph "BpGraph concept" |
---|
[1019] | 1097 | /// and it also provides some additional functionalities. |
---|
| 1098 | /// Most of its member functions and nested classes are documented |
---|
| 1099 | /// only in the concept class. |
---|
| 1100 | /// |
---|
| 1101 | /// This class provides constant time counting for nodes, edges and arcs. |
---|
| 1102 | /// |
---|
[1020] | 1103 | /// \sa concepts::BpGraph |
---|
| 1104 | /// \sa SmartGraph |
---|
[1019] | 1105 | class SmartBpGraph : public ExtendedSmartBpGraphBase { |
---|
| 1106 | typedef ExtendedSmartBpGraphBase Parent; |
---|
| 1107 | |
---|
| 1108 | private: |
---|
| 1109 | /// Graphs are \e not copy constructible. Use GraphCopy instead. |
---|
| 1110 | SmartBpGraph(const SmartBpGraph &) : ExtendedSmartBpGraphBase() {}; |
---|
| 1111 | /// \brief Assignment of a graph to another one is \e not allowed. |
---|
| 1112 | /// Use GraphCopy instead. |
---|
| 1113 | void operator=(const SmartBpGraph &) {} |
---|
| 1114 | |
---|
| 1115 | public: |
---|
| 1116 | |
---|
| 1117 | /// Constructor |
---|
| 1118 | |
---|
| 1119 | /// Constructor. |
---|
| 1120 | /// |
---|
| 1121 | SmartBpGraph() {} |
---|
| 1122 | |
---|
| 1123 | /// \brief Add a new red node to the graph. |
---|
| 1124 | /// |
---|
| 1125 | /// This function adds a red new node to the graph. |
---|
| 1126 | /// \return The new node. |
---|
| 1127 | Node addRedNode() { return Parent::addRedNode(); } |
---|
| 1128 | |
---|
| 1129 | /// \brief Add a new blue node to the graph. |
---|
| 1130 | /// |
---|
| 1131 | /// This function adds a blue new node to the graph. |
---|
| 1132 | /// \return The new node. |
---|
| 1133 | Node addBlueNode() { return Parent::addBlueNode(); } |
---|
| 1134 | |
---|
| 1135 | /// \brief Add a new edge to the graph. |
---|
| 1136 | /// |
---|
| 1137 | /// This function adds a new edge to the graph between nodes |
---|
| 1138 | /// \c u and \c v with inherent orientation from node \c u to |
---|
| 1139 | /// node \c v. |
---|
| 1140 | /// \return The new edge. |
---|
| 1141 | Edge addEdge(Node red, Node blue) { |
---|
| 1142 | LEMON_DEBUG(Parent::red(red) && Parent::blue(blue), |
---|
| 1143 | "Edge has to be formed by a red and a blue nodes"); |
---|
| 1144 | return Parent::addEdge(red, blue); |
---|
| 1145 | } |
---|
| 1146 | |
---|
| 1147 | /// \brief Node validity check |
---|
| 1148 | /// |
---|
| 1149 | /// This function gives back \c true if the given node is valid, |
---|
| 1150 | /// i.e. it is a real node of the graph. |
---|
| 1151 | /// |
---|
| 1152 | /// \warning A removed node (using Snapshot) could become valid again |
---|
| 1153 | /// if new nodes are added to the graph. |
---|
| 1154 | bool valid(Node n) const { return Parent::valid(n); } |
---|
| 1155 | |
---|
| 1156 | /// \brief Edge validity check |
---|
| 1157 | /// |
---|
| 1158 | /// This function gives back \c true if the given edge is valid, |
---|
| 1159 | /// i.e. it is a real edge of the graph. |
---|
| 1160 | /// |
---|
| 1161 | /// \warning A removed edge (using Snapshot) could become valid again |
---|
| 1162 | /// if new edges are added to the graph. |
---|
| 1163 | bool valid(Edge e) const { return Parent::valid(e); } |
---|
| 1164 | |
---|
| 1165 | /// \brief Arc validity check |
---|
| 1166 | /// |
---|
| 1167 | /// This function gives back \c true if the given arc is valid, |
---|
| 1168 | /// i.e. it is a real arc of the graph. |
---|
| 1169 | /// |
---|
| 1170 | /// \warning A removed arc (using Snapshot) could become valid again |
---|
| 1171 | /// if new edges are added to the graph. |
---|
| 1172 | bool valid(Arc a) const { return Parent::valid(a); } |
---|
| 1173 | |
---|
| 1174 | ///Clear the graph. |
---|
| 1175 | |
---|
| 1176 | ///This function erases all nodes and arcs from the graph. |
---|
| 1177 | /// |
---|
| 1178 | void clear() { |
---|
| 1179 | Parent::clear(); |
---|
| 1180 | } |
---|
| 1181 | |
---|
| 1182 | /// Reserve memory for nodes. |
---|
| 1183 | |
---|
| 1184 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 1185 | /// allocation: if you know that the graph you want to build will |
---|
| 1186 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 1187 | /// then it is worth reserving space for this amount before starting |
---|
| 1188 | /// to build the graph. |
---|
| 1189 | /// \sa reserveEdge() |
---|
| 1190 | void reserveNode(int n) { nodes.reserve(n); }; |
---|
| 1191 | |
---|
| 1192 | /// Reserve memory for edges. |
---|
| 1193 | |
---|
| 1194 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 1195 | /// allocation: if you know that the graph you want to build will |
---|
| 1196 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 1197 | /// then it is worth reserving space for this amount before starting |
---|
| 1198 | /// to build the graph. |
---|
| 1199 | /// \sa reserveNode() |
---|
| 1200 | void reserveEdge(int m) { arcs.reserve(2 * m); }; |
---|
| 1201 | |
---|
| 1202 | public: |
---|
| 1203 | |
---|
| 1204 | class Snapshot; |
---|
| 1205 | |
---|
| 1206 | protected: |
---|
| 1207 | |
---|
| 1208 | void saveSnapshot(Snapshot &s) |
---|
| 1209 | { |
---|
| 1210 | s._graph = this; |
---|
| 1211 | s.node_num = nodes.size(); |
---|
| 1212 | s.arc_num = arcs.size(); |
---|
| 1213 | } |
---|
| 1214 | |
---|
| 1215 | void restoreSnapshot(const Snapshot &s) |
---|
| 1216 | { |
---|
| 1217 | while(s.arc_num<arcs.size()) { |
---|
| 1218 | int n=arcs.size()-1; |
---|
| 1219 | Edge arc=edgeFromId(n/2); |
---|
| 1220 | Parent::notifier(Edge()).erase(arc); |
---|
| 1221 | std::vector<Arc> dir; |
---|
| 1222 | dir.push_back(arcFromId(n)); |
---|
| 1223 | dir.push_back(arcFromId(n-1)); |
---|
| 1224 | Parent::notifier(Arc()).erase(dir); |
---|
| 1225 | nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
---|
| 1226 | nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
---|
| 1227 | arcs.pop_back(); |
---|
| 1228 | arcs.pop_back(); |
---|
| 1229 | } |
---|
| 1230 | while(s.node_num<nodes.size()) { |
---|
| 1231 | int n=nodes.size()-1; |
---|
| 1232 | Node node = nodeFromId(n); |
---|
| 1233 | if (Parent::red(node)) { |
---|
| 1234 | first_red = nodes[n].partition_next; |
---|
[1023] | 1235 | if (first_red != -1) { |
---|
| 1236 | max_red = nodes[first_red].partition_index; |
---|
| 1237 | } else { |
---|
| 1238 | max_red = -1; |
---|
| 1239 | } |
---|
[1019] | 1240 | Parent::notifier(RedNode()).erase(node); |
---|
| 1241 | } else { |
---|
| 1242 | first_blue = nodes[n].partition_next; |
---|
[1023] | 1243 | if (first_blue != -1) { |
---|
| 1244 | max_blue = nodes[first_blue].partition_index; |
---|
| 1245 | } else { |
---|
| 1246 | max_blue = -1; |
---|
| 1247 | } |
---|
[1019] | 1248 | Parent::notifier(BlueNode()).erase(node); |
---|
| 1249 | } |
---|
| 1250 | Parent::notifier(Node()).erase(node); |
---|
| 1251 | nodes.pop_back(); |
---|
| 1252 | } |
---|
| 1253 | } |
---|
| 1254 | |
---|
| 1255 | public: |
---|
| 1256 | |
---|
| 1257 | ///Class to make a snapshot of the graph and to restore it later. |
---|
| 1258 | |
---|
| 1259 | ///Class to make a snapshot of the graph and to restore it later. |
---|
| 1260 | /// |
---|
| 1261 | ///The newly added nodes and edges can be removed using the |
---|
| 1262 | ///restore() function. This is the only way for deleting nodes and/or |
---|
| 1263 | ///edges from a SmartBpGraph structure. |
---|
| 1264 | /// |
---|
| 1265 | ///\note After a state is restored, you cannot restore a later state, |
---|
| 1266 | ///i.e. you cannot add the removed nodes and edges again using |
---|
| 1267 | ///another Snapshot instance. |
---|
| 1268 | /// |
---|
| 1269 | ///\warning The validity of the snapshot is not stored due to |
---|
| 1270 | ///performance reasons. If you do not use the snapshot correctly, |
---|
| 1271 | ///it can cause broken program, invalid or not restored state of |
---|
| 1272 | ///the graph or no change. |
---|
| 1273 | class Snapshot |
---|
| 1274 | { |
---|
| 1275 | SmartBpGraph *_graph; |
---|
| 1276 | protected: |
---|
| 1277 | friend class SmartBpGraph; |
---|
| 1278 | unsigned int node_num; |
---|
| 1279 | unsigned int arc_num; |
---|
| 1280 | public: |
---|
| 1281 | ///Default constructor. |
---|
| 1282 | |
---|
| 1283 | ///Default constructor. |
---|
| 1284 | ///You have to call save() to actually make a snapshot. |
---|
| 1285 | Snapshot() : _graph(0) {} |
---|
| 1286 | ///Constructor that immediately makes a snapshot |
---|
| 1287 | |
---|
| 1288 | /// This constructor immediately makes a snapshot of the given graph. |
---|
| 1289 | /// |
---|
| 1290 | Snapshot(SmartBpGraph &gr) { |
---|
| 1291 | gr.saveSnapshot(*this); |
---|
| 1292 | } |
---|
| 1293 | |
---|
| 1294 | ///Make a snapshot. |
---|
| 1295 | |
---|
| 1296 | ///This function makes a snapshot of the given graph. |
---|
| 1297 | ///It can be called more than once. In case of a repeated |
---|
| 1298 | ///call, the previous snapshot gets lost. |
---|
| 1299 | void save(SmartBpGraph &gr) |
---|
| 1300 | { |
---|
| 1301 | gr.saveSnapshot(*this); |
---|
| 1302 | } |
---|
| 1303 | |
---|
| 1304 | ///Undo the changes until the last snapshot. |
---|
| 1305 | |
---|
| 1306 | ///This function undos the changes until the last snapshot |
---|
| 1307 | ///created by save() or Snapshot(SmartBpGraph&). |
---|
| 1308 | void restore() |
---|
| 1309 | { |
---|
| 1310 | _graph->restoreSnapshot(*this); |
---|
| 1311 | } |
---|
| 1312 | }; |
---|
| 1313 | }; |
---|
| 1314 | |
---|
[109] | 1315 | } //namespace lemon |
---|
| 1316 | |
---|
| 1317 | |
---|
| 1318 | #endif //LEMON_SMART_GRAPH_H |
---|