[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
[105] | 18 | |
---|
[921] | 19 | #ifndef LEMON_SMART_GRAPH_H |
---|
| 20 | #define LEMON_SMART_GRAPH_H |
---|
[104] | 21 | |
---|
[491] | 22 | ///\ingroup graphs |
---|
[242] | 23 | ///\file |
---|
[1909] | 24 | ///\brief SmartGraph and SmartUGraph classes. |
---|
[242] | 25 | |
---|
[104] | 26 | #include <vector> |
---|
| 27 | |
---|
[1993] | 28 | #include <lemon/bits/invalid.h> |
---|
[157] | 29 | |
---|
[1791] | 30 | #include <lemon/bits/graph_extender.h> |
---|
[1034] | 31 | |
---|
[1993] | 32 | #include <lemon/bits/utility.h> |
---|
[1820] | 33 | #include <lemon/error.h> |
---|
[782] | 34 | |
---|
[1979] | 35 | #include <lemon/bits/graph_extender.h> |
---|
| 36 | |
---|
[921] | 37 | namespace lemon { |
---|
[104] | 38 | |
---|
[973] | 39 | class SmartGraph; |
---|
[969] | 40 | ///Base of SmartGraph |
---|
| 41 | |
---|
| 42 | ///Base of SmartGraph |
---|
| 43 | /// |
---|
[946] | 44 | class SmartGraphBase { |
---|
[104] | 45 | |
---|
[973] | 46 | friend class SmatGraph; |
---|
| 47 | |
---|
| 48 | protected: |
---|
[104] | 49 | struct NodeT |
---|
| 50 | { |
---|
| 51 | int first_in,first_out; |
---|
[157] | 52 | NodeT() : first_in(-1), first_out(-1) {} |
---|
[104] | 53 | }; |
---|
| 54 | struct EdgeT |
---|
| 55 | { |
---|
[986] | 56 | int target, source, next_in, next_out; |
---|
[104] | 57 | //FIXME: is this necessary? |
---|
[157] | 58 | EdgeT() : next_in(-1), next_out(-1) {} |
---|
[104] | 59 | }; |
---|
| 60 | |
---|
| 61 | std::vector<NodeT> nodes; |
---|
[129] | 62 | |
---|
[104] | 63 | std::vector<EdgeT> edges; |
---|
| 64 | |
---|
[185] | 65 | |
---|
[104] | 66 | public: |
---|
[782] | 67 | |
---|
[946] | 68 | typedef SmartGraphBase Graph; |
---|
[104] | 69 | |
---|
[164] | 70 | class Node; |
---|
| 71 | class Edge; |
---|
[108] | 72 | |
---|
[104] | 73 | |
---|
| 74 | public: |
---|
| 75 | |
---|
[946] | 76 | SmartGraphBase() : nodes(), edges() { } |
---|
[1718] | 77 | SmartGraphBase(const SmartGraphBase &_g) |
---|
| 78 | : nodes(_g.nodes), edges(_g.edges) { } |
---|
[104] | 79 | |
---|
[977] | 80 | typedef True NodeNumTag; |
---|
| 81 | typedef True EdgeNumTag; |
---|
| 82 | |
---|
[813] | 83 | ///Number of nodes. |
---|
| 84 | int nodeNum() const { return nodes.size(); } |
---|
| 85 | ///Number of edges. |
---|
| 86 | int edgeNum() const { return edges.size(); } |
---|
[104] | 87 | |
---|
[813] | 88 | /// Maximum node ID. |
---|
| 89 | |
---|
| 90 | /// Maximum node ID. |
---|
| 91 | ///\sa id(Node) |
---|
[1791] | 92 | int maxNodeId() const { return nodes.size()-1; } |
---|
[813] | 93 | /// Maximum edge ID. |
---|
| 94 | |
---|
| 95 | /// Maximum edge ID. |
---|
| 96 | ///\sa id(Edge) |
---|
[1791] | 97 | int maxEdgeId() const { return edges.size()-1; } |
---|
[108] | 98 | |
---|
[986] | 99 | Node source(Edge e) const { return edges[e.n].source; } |
---|
| 100 | Node target(Edge e) const { return edges[e.n].target; } |
---|
[104] | 101 | |
---|
[813] | 102 | /// Node ID. |
---|
| 103 | |
---|
| 104 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
[1791] | 105 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 106 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
[813] | 107 | /// |
---|
| 108 | /// The ID of the \ref INVALID node is -1. |
---|
| 109 | ///\return The ID of the node \c v. |
---|
[713] | 110 | static int id(Node v) { return v.n; } |
---|
[813] | 111 | /// Edge ID. |
---|
| 112 | |
---|
| 113 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
[1791] | 114 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 115 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
[813] | 116 | /// |
---|
| 117 | /// The ID of the \ref INVALID edge is -1. |
---|
| 118 | ///\return The ID of the edge \c e. |
---|
[713] | 119 | static int id(Edge e) { return e.n; } |
---|
[104] | 120 | |
---|
[1791] | 121 | static Node nodeFromId(int id) { return Node(id);} |
---|
[1106] | 122 | |
---|
[1791] | 123 | static Edge edgeFromId(int id) { return Edge(id);} |
---|
[1106] | 124 | |
---|
[164] | 125 | Node addNode() { |
---|
| 126 | Node n; n.n=nodes.size(); |
---|
[104] | 127 | nodes.push_back(NodeT()); //FIXME: Hmmm... |
---|
| 128 | return n; |
---|
| 129 | } |
---|
[108] | 130 | |
---|
[164] | 131 | Edge addEdge(Node u, Node v) { |
---|
| 132 | Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... |
---|
[986] | 133 | edges[e.n].source=u.n; edges[e.n].target=v.n; |
---|
[104] | 134 | edges[e.n].next_out=nodes[u.n].first_out; |
---|
| 135 | edges[e.n].next_in=nodes[v.n].first_in; |
---|
| 136 | nodes[u.n].first_out=nodes[v.n].first_in=e.n; |
---|
[108] | 137 | |
---|
[104] | 138 | return e; |
---|
| 139 | } |
---|
| 140 | |
---|
[782] | 141 | void clear() { |
---|
| 142 | edges.clear(); |
---|
| 143 | nodes.clear(); |
---|
| 144 | } |
---|
[104] | 145 | |
---|
[946] | 146 | |
---|
[164] | 147 | class Node { |
---|
[946] | 148 | friend class SmartGraphBase; |
---|
[973] | 149 | friend class SmartGraph; |
---|
[104] | 150 | |
---|
| 151 | protected: |
---|
| 152 | int n; |
---|
[164] | 153 | Node(int nn) {n=nn;} |
---|
[104] | 154 | public: |
---|
[164] | 155 | Node() {} |
---|
[503] | 156 | Node (Invalid) { n=-1; } |
---|
[164] | 157 | bool operator==(const Node i) const {return n==i.n;} |
---|
| 158 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
| 159 | bool operator<(const Node i) const {return n<i.n;} |
---|
[104] | 160 | }; |
---|
| 161 | |
---|
| 162 | |
---|
[164] | 163 | class Edge { |
---|
[946] | 164 | friend class SmartGraphBase; |
---|
[973] | 165 | friend class SmartGraph; |
---|
[185] | 166 | |
---|
[104] | 167 | protected: |
---|
| 168 | int n; |
---|
[905] | 169 | Edge(int nn) {n=nn;} |
---|
[706] | 170 | public: |
---|
[164] | 171 | Edge() { } |
---|
[174] | 172 | Edge (Invalid) { n=-1; } |
---|
[164] | 173 | bool operator==(const Edge i) const {return n==i.n;} |
---|
| 174 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
| 175 | bool operator<(const Edge i) const {return n<i.n;} |
---|
[946] | 176 | }; |
---|
[905] | 177 | |
---|
[946] | 178 | void first(Node& node) const { |
---|
| 179 | node.n = nodes.size() - 1; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | static void next(Node& node) { |
---|
| 183 | --node.n; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void first(Edge& edge) const { |
---|
| 187 | edge.n = edges.size() - 1; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | static void next(Edge& edge) { |
---|
| 191 | --edge.n; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | void firstOut(Edge& edge, const Node& node) const { |
---|
| 195 | edge.n = nodes[node.n].first_out; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | void nextOut(Edge& edge) const { |
---|
| 199 | edge.n = edges[edge.n].next_out; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | void firstIn(Edge& edge, const Node& node) const { |
---|
| 203 | edge.n = nodes[node.n].first_in; |
---|
| 204 | } |
---|
[104] | 205 | |
---|
[946] | 206 | void nextIn(Edge& edge) const { |
---|
| 207 | edge.n = edges[edge.n].next_in; |
---|
| 208 | } |
---|
[105] | 209 | |
---|
[1284] | 210 | Node _split(Node n, bool connect = true) |
---|
| 211 | { |
---|
| 212 | Node b = addNode(); |
---|
| 213 | nodes[b.n].first_out=nodes[n.n].first_out; |
---|
| 214 | nodes[n.n].first_out=-1; |
---|
| 215 | for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n; |
---|
| 216 | if(connect) addEdge(n,b); |
---|
| 217 | return b; |
---|
| 218 | } |
---|
| 219 | |
---|
[104] | 220 | }; |
---|
[185] | 221 | |
---|
[1979] | 222 | typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
---|
[937] | 223 | |
---|
[1791] | 224 | /// \ingroup graphs |
---|
[1161] | 225 | |
---|
[950] | 226 | ///A smart graph class. |
---|
[937] | 227 | |
---|
[950] | 228 | ///This is a simple and fast graph implementation. |
---|
| 229 | ///It is also quite memory efficient, but at the price |
---|
[974] | 230 | ///that <b> it does support only limited (only stack-like) |
---|
| 231 | ///node and edge deletions</b>. |
---|
[950] | 232 | ///It conforms to |
---|
[959] | 233 | ///the \ref concept::ExtendableGraph "ExtendableGraph" concept. |
---|
| 234 | ///\sa concept::ExtendableGraph. |
---|
[950] | 235 | /// |
---|
| 236 | ///\author Alpar Juttner |
---|
[1669] | 237 | class SmartGraph : public ExtendedSmartGraphBase { |
---|
[969] | 238 | public: |
---|
[1979] | 239 | |
---|
| 240 | typedef ExtendedSmartGraphBase Parent; |
---|
| 241 | |
---|
[1770] | 242 | class Snapshot; |
---|
| 243 | friend class Snapshot; |
---|
[973] | 244 | |
---|
[1011] | 245 | protected: |
---|
[1770] | 246 | void restoreSnapshot(const Snapshot &s) |
---|
[973] | 247 | { |
---|
[1457] | 248 | while(s.edge_num<edges.size()) { |
---|
[1040] | 249 | Parent::getNotifier(Edge()).erase(Edge(edges.size()-1)); |
---|
[986] | 250 | nodes[edges.back().target].first_in=edges.back().next_in; |
---|
| 251 | nodes[edges.back().source].first_out=edges.back().next_out; |
---|
[973] | 252 | edges.pop_back(); |
---|
| 253 | } |
---|
| 254 | //nodes.resize(s.nodes_num); |
---|
[1457] | 255 | while(s.node_num<nodes.size()) { |
---|
[1040] | 256 | Parent::getNotifier(Node()).erase(Node(nodes.size()-1)); |
---|
[973] | 257 | nodes.pop_back(); |
---|
| 258 | } |
---|
[1011] | 259 | } |
---|
| 260 | |
---|
| 261 | public: |
---|
[1284] | 262 | |
---|
| 263 | ///Split a node. |
---|
| 264 | |
---|
| 265 | ///This function splits a node. First a new node is added to the graph, |
---|
| 266 | ///then the source of each outgoing edge of \c n is moved to this new node. |
---|
| 267 | ///If \c connect is \c true (this is the default value), then a new edge |
---|
| 268 | ///from \c n to the newly created node is also added. |
---|
| 269 | ///\return The newly created node. |
---|
| 270 | /// |
---|
| 271 | ///\note The <tt>Edge</tt>s |
---|
| 272 | ///referencing a moved edge remain |
---|
| 273 | ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 274 | ///may be invalidated. |
---|
[1770] | 275 | ///\warning This functionality cannot be used together with the Snapshot |
---|
[1284] | 276 | ///feature. |
---|
| 277 | ///\todo It could be implemented in a bit faster way. |
---|
| 278 | Node split(Node n, bool connect = true) |
---|
| 279 | { |
---|
[1718] | 280 | Node b = _split(n,connect); |
---|
| 281 | return b; |
---|
[1284] | 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
[1011] | 285 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 286 | |
---|
| 287 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 288 | /// |
---|
| 289 | ///The newly added nodes and edges can be removed using the |
---|
| 290 | ///restore() function. |
---|
| 291 | ///\note After you restore a state, you cannot restore |
---|
| 292 | ///a later state, in other word you cannot add again the edges deleted |
---|
[1770] | 293 | ///by restore() using another Snapshot instance. |
---|
[1011] | 294 | /// |
---|
[1770] | 295 | class Snapshot |
---|
[1011] | 296 | { |
---|
| 297 | SmartGraph *g; |
---|
| 298 | protected: |
---|
| 299 | friend class SmartGraph; |
---|
| 300 | unsigned int node_num; |
---|
| 301 | unsigned int edge_num; |
---|
| 302 | public: |
---|
[1274] | 303 | ///Default constructor. |
---|
[1011] | 304 | |
---|
[1274] | 305 | ///Default constructor. |
---|
[1011] | 306 | ///To actually make a snapshot you must call save(). |
---|
| 307 | /// |
---|
[1770] | 308 | Snapshot() : g(0) {} |
---|
[1011] | 309 | ///Constructor that immediately makes a snapshot |
---|
| 310 | |
---|
| 311 | ///This constructor immediately makes a snapshot of the graph. |
---|
| 312 | ///\param _g The graph we make a snapshot of. |
---|
[1770] | 313 | Snapshot(SmartGraph &_g) :g(&_g) { |
---|
[1011] | 314 | node_num=g->nodes.size(); |
---|
| 315 | edge_num=g->edges.size(); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | ///Make a snapshot. |
---|
| 319 | |
---|
| 320 | ///Make a snapshot of the graph. |
---|
| 321 | /// |
---|
| 322 | ///This function can be called more than once. In case of a repeated |
---|
| 323 | ///call, the previous snapshot gets lost. |
---|
| 324 | ///\param _g The graph we make the snapshot of. |
---|
| 325 | void save(SmartGraph &_g) |
---|
| 326 | { |
---|
| 327 | g=&_g; |
---|
| 328 | node_num=g->nodes.size(); |
---|
| 329 | edge_num=g->edges.size(); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | ///Undo the changes until a snapshot. |
---|
| 333 | |
---|
| 334 | ///Undo the changes until a snapshot created by save(). |
---|
| 335 | /// |
---|
| 336 | ///\note After you restored a state, you cannot restore |
---|
| 337 | ///a later state, in other word you cannot add again the edges deleted |
---|
| 338 | ///by restore(). |
---|
| 339 | /// |
---|
| 340 | ///\todo This function might be called undo(). |
---|
| 341 | |
---|
| 342 | void restore() |
---|
| 343 | { |
---|
[1770] | 344 | g->restoreSnapshot(*this); |
---|
[1011] | 345 | } |
---|
| 346 | }; |
---|
[973] | 347 | }; |
---|
[1034] | 348 | |
---|
| 349 | |
---|
| 350 | /**************** Undirected List Graph ****************/ |
---|
| 351 | |
---|
[1979] | 352 | typedef UGraphExtender<UGraphBaseExtender<SmartGraphBase> > |
---|
| 353 | ExtendedSmartUGraphBase; |
---|
[1034] | 354 | |
---|
[1909] | 355 | /// \ingroup graphs |
---|
[1035] | 356 | /// |
---|
[1909] | 357 | /// \brief A smart undirected graph class. |
---|
[1035] | 358 | /// |
---|
[1909] | 359 | /// This is a simple and fast undirected graph implementation. |
---|
| 360 | /// It is also quite memory efficient, but at the price |
---|
| 361 | /// that <b> it does support only limited (only stack-like) |
---|
| 362 | /// node and edge deletions</b>. |
---|
| 363 | /// Except from this it conforms to |
---|
| 364 | /// the \ref concept::UGraph "UGraph" concept. |
---|
| 365 | /// \sa concept::UGraph. |
---|
| 366 | /// |
---|
| 367 | /// \todo Snapshot hasn't been implemented yet. |
---|
| 368 | /// |
---|
| 369 | class SmartUGraph : public ExtendedSmartUGraphBase { |
---|
[1034] | 370 | }; |
---|
| 371 | |
---|
[1820] | 372 | |
---|
[1910] | 373 | class SmartBpUGraphBase { |
---|
[1820] | 374 | public: |
---|
| 375 | |
---|
| 376 | class NodeSetError : public LogicError { |
---|
| 377 | virtual const char* exceptionName() const { |
---|
[1910] | 378 | return "lemon::SmartBpUGraph::NodeSetError"; |
---|
[1820] | 379 | } |
---|
| 380 | }; |
---|
| 381 | |
---|
| 382 | protected: |
---|
| 383 | |
---|
| 384 | struct NodeT { |
---|
| 385 | int first; |
---|
| 386 | NodeT() {} |
---|
| 387 | NodeT(int _first) : first(_first) {} |
---|
| 388 | }; |
---|
| 389 | |
---|
| 390 | struct EdgeT { |
---|
[1910] | 391 | int aNode, next_out; |
---|
| 392 | int bNode, next_in; |
---|
[1820] | 393 | }; |
---|
| 394 | |
---|
[1910] | 395 | std::vector<NodeT> aNodes; |
---|
| 396 | std::vector<NodeT> bNodes; |
---|
[1820] | 397 | |
---|
| 398 | std::vector<EdgeT> edges; |
---|
| 399 | |
---|
| 400 | public: |
---|
| 401 | |
---|
| 402 | class Node { |
---|
[1910] | 403 | friend class SmartBpUGraphBase; |
---|
[1820] | 404 | protected: |
---|
| 405 | int id; |
---|
| 406 | |
---|
| 407 | Node(int _id) : id(_id) {} |
---|
| 408 | public: |
---|
| 409 | Node() {} |
---|
| 410 | Node(Invalid) { id = -1; } |
---|
| 411 | bool operator==(const Node i) const {return id==i.id;} |
---|
| 412 | bool operator!=(const Node i) const {return id!=i.id;} |
---|
| 413 | bool operator<(const Node i) const {return id<i.id;} |
---|
| 414 | }; |
---|
| 415 | |
---|
| 416 | class Edge { |
---|
[1910] | 417 | friend class SmartBpUGraphBase; |
---|
[1820] | 418 | protected: |
---|
| 419 | int id; |
---|
| 420 | |
---|
| 421 | Edge(int _id) { id = _id;} |
---|
| 422 | public: |
---|
| 423 | Edge() {} |
---|
| 424 | Edge (Invalid) { id = -1; } |
---|
| 425 | bool operator==(const Edge i) const {return id==i.id;} |
---|
| 426 | bool operator!=(const Edge i) const {return id!=i.id;} |
---|
| 427 | bool operator<(const Edge i) const {return id<i.id;} |
---|
| 428 | }; |
---|
| 429 | |
---|
[1910] | 430 | void firstANode(Node& node) const { |
---|
| 431 | node.id = 2 * aNodes.size() - 2; |
---|
[1820] | 432 | if (node.id < 0) node.id = -1; |
---|
| 433 | } |
---|
[1910] | 434 | void nextANode(Node& node) const { |
---|
[1820] | 435 | node.id -= 2; |
---|
| 436 | if (node.id < 0) node.id = -1; |
---|
| 437 | } |
---|
| 438 | |
---|
[1910] | 439 | void firstBNode(Node& node) const { |
---|
| 440 | node.id = 2 * bNodes.size() - 1; |
---|
[1820] | 441 | } |
---|
[1910] | 442 | void nextBNode(Node& node) const { |
---|
[1820] | 443 | node.id -= 2; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | void first(Node& node) const { |
---|
[1910] | 447 | if (aNodes.size() > 0) { |
---|
| 448 | node.id = 2 * aNodes.size() - 2; |
---|
[1820] | 449 | } else { |
---|
[1910] | 450 | node.id = 2 * bNodes.size() - 1; |
---|
[1820] | 451 | } |
---|
| 452 | } |
---|
| 453 | void next(Node& node) const { |
---|
| 454 | node.id -= 2; |
---|
| 455 | if (node.id == -2) { |
---|
[1910] | 456 | node.id = 2 * bNodes.size() - 1; |
---|
[1820] | 457 | } |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | void first(Edge& edge) const { |
---|
| 461 | edge.id = edges.size() - 1; |
---|
| 462 | } |
---|
| 463 | void next(Edge& edge) const { |
---|
| 464 | --edge.id; |
---|
| 465 | } |
---|
| 466 | |
---|
[1910] | 467 | void firstOut(Edge& edge, const Node& node) const { |
---|
[1820] | 468 | LEMON_ASSERT((node.id & 1) == 0, NodeSetError()); |
---|
[1910] | 469 | edge.id = aNodes[node.id >> 1].first; |
---|
[1820] | 470 | } |
---|
[1910] | 471 | void nextOut(Edge& edge) const { |
---|
| 472 | edge.id = edges[edge.id].next_out; |
---|
[1820] | 473 | } |
---|
| 474 | |
---|
[1910] | 475 | void firstIn(Edge& edge, const Node& node) const { |
---|
[1820] | 476 | LEMON_ASSERT((node.id & 1) == 1, NodeSetError()); |
---|
[1910] | 477 | edge.id = bNodes[node.id >> 1].first; |
---|
[1820] | 478 | } |
---|
[1910] | 479 | void nextIn(Edge& edge) const { |
---|
| 480 | edge.id = edges[edge.id].next_in; |
---|
[1820] | 481 | } |
---|
| 482 | |
---|
| 483 | static int id(const Node& node) { |
---|
| 484 | return node.id; |
---|
| 485 | } |
---|
| 486 | static Node nodeFromId(int id) { |
---|
| 487 | return Node(id); |
---|
| 488 | } |
---|
| 489 | int maxNodeId() const { |
---|
[1910] | 490 | return aNodes.size() > bNodes.size() ? |
---|
| 491 | aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1; |
---|
[1820] | 492 | } |
---|
| 493 | |
---|
| 494 | static int id(const Edge& edge) { |
---|
| 495 | return edge.id; |
---|
| 496 | } |
---|
| 497 | static Edge edgeFromId(int id) { |
---|
| 498 | return Edge(id); |
---|
| 499 | } |
---|
| 500 | int maxEdgeId() const { |
---|
| 501 | return edges.size(); |
---|
| 502 | } |
---|
| 503 | |
---|
[1910] | 504 | static int aNodeId(const Node& node) { |
---|
[1820] | 505 | return node.id >> 1; |
---|
| 506 | } |
---|
[1995] | 507 | static Node fromANodeId(int id) { |
---|
[1820] | 508 | return Node(id << 1); |
---|
| 509 | } |
---|
[1910] | 510 | int maxANodeId() const { |
---|
| 511 | return aNodes.size(); |
---|
[1820] | 512 | } |
---|
| 513 | |
---|
[1910] | 514 | static int bNodeId(const Node& node) { |
---|
[1820] | 515 | return node.id >> 1; |
---|
| 516 | } |
---|
[1910] | 517 | static Node fromBNodeId(int id) { |
---|
[1820] | 518 | return Node((id << 1) + 1); |
---|
| 519 | } |
---|
[1910] | 520 | int maxBNodeId() const { |
---|
| 521 | return bNodes.size(); |
---|
[1820] | 522 | } |
---|
| 523 | |
---|
[1910] | 524 | Node aNode(const Edge& edge) const { |
---|
| 525 | return Node(edges[edge.id].aNode); |
---|
[1820] | 526 | } |
---|
[1910] | 527 | Node bNode(const Edge& edge) const { |
---|
| 528 | return Node(edges[edge.id].bNode); |
---|
[1820] | 529 | } |
---|
| 530 | |
---|
[1910] | 531 | static bool aNode(const Node& node) { |
---|
[1820] | 532 | return (node.id & 1) == 0; |
---|
| 533 | } |
---|
| 534 | |
---|
[1910] | 535 | static bool bNode(const Node& node) { |
---|
[1820] | 536 | return (node.id & 1) == 1; |
---|
| 537 | } |
---|
| 538 | |
---|
[1910] | 539 | Node addANode() { |
---|
[1820] | 540 | NodeT nodeT; |
---|
| 541 | nodeT.first = -1; |
---|
[1910] | 542 | aNodes.push_back(nodeT); |
---|
| 543 | return Node(aNodes.size() * 2 - 2); |
---|
[1820] | 544 | } |
---|
| 545 | |
---|
[1910] | 546 | Node addBNode() { |
---|
[1820] | 547 | NodeT nodeT; |
---|
| 548 | nodeT.first = -1; |
---|
[1910] | 549 | bNodes.push_back(nodeT); |
---|
| 550 | return Node(bNodes.size() * 2 - 1); |
---|
[1820] | 551 | } |
---|
| 552 | |
---|
| 553 | Edge addEdge(const Node& source, const Node& target) { |
---|
| 554 | LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError()); |
---|
| 555 | EdgeT edgeT; |
---|
| 556 | if ((source.id & 1) == 0) { |
---|
[1910] | 557 | edgeT.aNode = source.id; |
---|
| 558 | edgeT.bNode = target.id; |
---|
[1820] | 559 | } else { |
---|
[1910] | 560 | edgeT.aNode = target.id; |
---|
| 561 | edgeT.bNode = source.id; |
---|
[1820] | 562 | } |
---|
[1910] | 563 | edgeT.next_out = aNodes[edgeT.aNode >> 1].first; |
---|
| 564 | aNodes[edgeT.aNode >> 1].first = edges.size(); |
---|
| 565 | edgeT.next_in = bNodes[edgeT.bNode >> 1].first; |
---|
| 566 | bNodes[edgeT.bNode >> 1].first = edges.size(); |
---|
[1820] | 567 | edges.push_back(edgeT); |
---|
| 568 | return Edge(edges.size() - 1); |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | void clear() { |
---|
[1910] | 572 | aNodes.clear(); |
---|
| 573 | bNodes.clear(); |
---|
[1820] | 574 | edges.clear(); |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | }; |
---|
| 578 | |
---|
| 579 | |
---|
[1979] | 580 | typedef BpUGraphExtender< BpUGraphBaseExtender< |
---|
| 581 | SmartBpUGraphBase> > ExtendedSmartBpUGraphBase; |
---|
[1820] | 582 | |
---|
[1910] | 583 | /// \ingroup graphs |
---|
| 584 | /// |
---|
| 585 | /// \brief A smart bipartite undirected graph class. |
---|
| 586 | /// |
---|
| 587 | /// This is a simple and fast bipartite undirected graph implementation. |
---|
| 588 | /// It is also quite memory efficient, but at the price |
---|
| 589 | /// that <b> it does not support node and edge deletions</b>. |
---|
| 590 | /// Except from this it conforms to |
---|
| 591 | /// the \ref concept::BpUGraph "BpUGraph" concept. |
---|
| 592 | /// \sa concept::BpUGraph. |
---|
| 593 | /// |
---|
| 594 | class SmartBpUGraph : public ExtendedSmartBpUGraphBase {}; |
---|
[1820] | 595 | |
---|
[950] | 596 | |
---|
[407] | 597 | /// @} |
---|
[921] | 598 | } //namespace lemon |
---|
[104] | 599 | |
---|
[157] | 600 | |
---|
[921] | 601 | #endif //LEMON_SMART_GRAPH_H |
---|