[948] | 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). |
---|
[948] | 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 | */ |
---|
[395] | 18 | |
---|
[921] | 19 | #ifndef LEMON_LIST_GRAPH_H |
---|
| 20 | #define LEMON_LIST_GRAPH_H |
---|
[395] | 21 | |
---|
[948] | 22 | ///\ingroup graphs |
---|
| 23 | ///\file |
---|
[1909] | 24 | ///\brief ListGraph, ListUGraph classes. |
---|
[948] | 25 | |
---|
[1791] | 26 | #include <lemon/bits/graph_extender.h> |
---|
[782] | 27 | |
---|
[1774] | 28 | #include <lemon/error.h> |
---|
| 29 | |
---|
[1979] | 30 | #include <vector> |
---|
[1011] | 31 | #include <list> |
---|
[782] | 32 | |
---|
[921] | 33 | namespace lemon { |
---|
[395] | 34 | |
---|
[946] | 35 | class ListGraphBase { |
---|
[406] | 36 | |
---|
[949] | 37 | protected: |
---|
[946] | 38 | struct NodeT { |
---|
[1470] | 39 | int first_in, first_out; |
---|
[397] | 40 | int prev, next; |
---|
[395] | 41 | }; |
---|
[946] | 42 | |
---|
| 43 | struct EdgeT { |
---|
[986] | 44 | int target, source; |
---|
[397] | 45 | int prev_in, prev_out; |
---|
| 46 | int next_in, next_out; |
---|
[395] | 47 | }; |
---|
| 48 | |
---|
| 49 | std::vector<NodeT> nodes; |
---|
[946] | 50 | |
---|
[397] | 51 | int first_node; |
---|
[946] | 52 | |
---|
[397] | 53 | int first_free_node; |
---|
[946] | 54 | |
---|
[395] | 55 | std::vector<EdgeT> edges; |
---|
[946] | 56 | |
---|
[397] | 57 | int first_free_edge; |
---|
[395] | 58 | |
---|
[782] | 59 | public: |
---|
[395] | 60 | |
---|
[946] | 61 | typedef ListGraphBase Graph; |
---|
[397] | 62 | |
---|
[946] | 63 | class Node { |
---|
[975] | 64 | friend class ListGraphBase; |
---|
[946] | 65 | protected: |
---|
[395] | 66 | |
---|
[946] | 67 | int id; |
---|
| 68 | Node(int pid) { id = pid;} |
---|
[395] | 69 | |
---|
[946] | 70 | public: |
---|
| 71 | Node() {} |
---|
| 72 | Node (Invalid) { id = -1; } |
---|
| 73 | bool operator==(const Node& node) const {return id == node.id;} |
---|
| 74 | bool operator!=(const Node& node) const {return id != node.id;} |
---|
| 75 | bool operator<(const Node& node) const {return id < node.id;} |
---|
| 76 | }; |
---|
[782] | 77 | |
---|
[946] | 78 | class Edge { |
---|
[975] | 79 | friend class ListGraphBase; |
---|
[946] | 80 | protected: |
---|
[782] | 81 | |
---|
[946] | 82 | int id; |
---|
| 83 | Edge(int pid) { id = pid;} |
---|
[395] | 84 | |
---|
[946] | 85 | public: |
---|
| 86 | Edge() {} |
---|
| 87 | Edge (Invalid) { id = -1; } |
---|
| 88 | bool operator==(const Edge& edge) const {return id == edge.id;} |
---|
| 89 | bool operator!=(const Edge& edge) const {return id != edge.id;} |
---|
| 90 | bool operator<(const Edge& edge) const {return id < edge.id;} |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | ListGraphBase() |
---|
[782] | 96 | : nodes(), first_node(-1), |
---|
| 97 | first_free_node(-1), edges(), first_free_edge(-1) {} |
---|
| 98 | |
---|
[395] | 99 | |
---|
[813] | 100 | /// Maximum node ID. |
---|
| 101 | |
---|
| 102 | /// Maximum node ID. |
---|
| 103 | ///\sa id(Node) |
---|
[1791] | 104 | int maxNodeId() const { return nodes.size()-1; } |
---|
[946] | 105 | |
---|
[813] | 106 | /// Maximum edge ID. |
---|
| 107 | |
---|
| 108 | /// Maximum edge ID. |
---|
| 109 | ///\sa id(Edge) |
---|
[1791] | 110 | int maxEdgeId() const { return edges.size()-1; } |
---|
[395] | 111 | |
---|
[986] | 112 | Node source(Edge e) const { return edges[e.id].source; } |
---|
| 113 | Node target(Edge e) const { return edges[e.id].target; } |
---|
[395] | 114 | |
---|
| 115 | |
---|
[946] | 116 | void first(Node& node) const { |
---|
| 117 | node.id = first_node; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void next(Node& node) const { |
---|
| 121 | node.id = nodes[node.id].next; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | void first(Edge& e) const { |
---|
| 126 | int n; |
---|
| 127 | for(n = first_node; |
---|
| 128 | n!=-1 && nodes[n].first_in == -1; |
---|
| 129 | n = nodes[n].next); |
---|
| 130 | e.id = (n == -1) ? -1 : nodes[n].first_in; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void next(Edge& edge) const { |
---|
| 134 | if (edges[edge.id].next_in != -1) { |
---|
| 135 | edge.id = edges[edge.id].next_in; |
---|
| 136 | } else { |
---|
| 137 | int n; |
---|
[986] | 138 | for(n = nodes[edges[edge.id].target].next; |
---|
[946] | 139 | n!=-1 && nodes[n].first_in == -1; |
---|
| 140 | n = nodes[n].next); |
---|
| 141 | edge.id = (n == -1) ? -1 : nodes[n].first_in; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void firstOut(Edge &e, const Node& v) const { |
---|
| 146 | e.id = nodes[v.id].first_out; |
---|
| 147 | } |
---|
| 148 | void nextOut(Edge &e) const { |
---|
| 149 | e.id=edges[e.id].next_out; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void firstIn(Edge &e, const Node& v) const { |
---|
| 153 | e.id = nodes[v.id].first_in; |
---|
| 154 | } |
---|
| 155 | void nextIn(Edge &e) const { |
---|
| 156 | e.id=edges[e.id].next_in; |
---|
| 157 | } |
---|
| 158 | |
---|
[813] | 159 | |
---|
[946] | 160 | static int id(Node v) { return v.id; } |
---|
| 161 | static int id(Edge e) { return e.id; } |
---|
[395] | 162 | |
---|
[1791] | 163 | static Node nodeFromId(int id) { return Node(id);} |
---|
| 164 | static Edge edgeFromId(int id) { return Edge(id);} |
---|
[1106] | 165 | |
---|
[397] | 166 | /// Adds a new node to the graph. |
---|
| 167 | |
---|
[813] | 168 | /// \warning It adds the new node to the front of the list. |
---|
[397] | 169 | /// (i.e. the lastly added node becomes the first.) |
---|
[946] | 170 | Node addNode() { |
---|
[397] | 171 | int n; |
---|
| 172 | |
---|
[946] | 173 | if(first_free_node==-1) { |
---|
| 174 | n = nodes.size(); |
---|
| 175 | nodes.push_back(NodeT()); |
---|
| 176 | } else { |
---|
[397] | 177 | n = first_free_node; |
---|
| 178 | first_free_node = nodes[n].next; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | nodes[n].next = first_node; |
---|
| 182 | if(first_node != -1) nodes[first_node].prev = n; |
---|
| 183 | first_node = n; |
---|
| 184 | nodes[n].prev = -1; |
---|
| 185 | |
---|
| 186 | nodes[n].first_in = nodes[n].first_out = -1; |
---|
| 187 | |
---|
[946] | 188 | return Node(n); |
---|
[395] | 189 | } |
---|
| 190 | |
---|
| 191 | Edge addEdge(Node u, Node v) { |
---|
[946] | 192 | int n; |
---|
| 193 | |
---|
| 194 | if (first_free_edge == -1) { |
---|
| 195 | n = edges.size(); |
---|
| 196 | edges.push_back(EdgeT()); |
---|
| 197 | } else { |
---|
[397] | 198 | n = first_free_edge; |
---|
| 199 | first_free_edge = edges[n].next_in; |
---|
| 200 | } |
---|
| 201 | |
---|
[986] | 202 | edges[n].source = u.id; |
---|
| 203 | edges[n].target = v.id; |
---|
[395] | 204 | |
---|
[946] | 205 | edges[n].next_out = nodes[u.id].first_out; |
---|
| 206 | if(nodes[u.id].first_out != -1) { |
---|
| 207 | edges[nodes[u.id].first_out].prev_out = n; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | edges[n].next_in = nodes[v.id].first_in; |
---|
| 211 | if(nodes[v.id].first_in != -1) { |
---|
| 212 | edges[nodes[v.id].first_in].prev_in = n; |
---|
| 213 | } |
---|
| 214 | |
---|
[397] | 215 | edges[n].prev_in = edges[n].prev_out = -1; |
---|
| 216 | |
---|
[946] | 217 | nodes[u.id].first_out = nodes[v.id].first_in = n; |
---|
[397] | 218 | |
---|
[946] | 219 | return Edge(n); |
---|
[395] | 220 | } |
---|
[774] | 221 | |
---|
[946] | 222 | void erase(const Node& node) { |
---|
| 223 | int n = node.id; |
---|
| 224 | |
---|
| 225 | if(nodes[n].next != -1) { |
---|
| 226 | nodes[nodes[n].next].prev = nodes[n].prev; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | if(nodes[n].prev != -1) { |
---|
| 230 | nodes[nodes[n].prev].next = nodes[n].next; |
---|
| 231 | } else { |
---|
| 232 | first_node = nodes[n].next; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | nodes[n].next = first_free_node; |
---|
| 236 | first_free_node = n; |
---|
[395] | 237 | |
---|
[774] | 238 | } |
---|
| 239 | |
---|
[946] | 240 | void erase(const Edge& edge) { |
---|
| 241 | int n = edge.id; |
---|
[397] | 242 | |
---|
[946] | 243 | if(edges[n].next_in!=-1) { |
---|
[397] | 244 | edges[edges[n].next_in].prev_in = edges[n].prev_in; |
---|
[946] | 245 | } |
---|
| 246 | |
---|
| 247 | if(edges[n].prev_in!=-1) { |
---|
[397] | 248 | edges[edges[n].prev_in].next_in = edges[n].next_in; |
---|
[946] | 249 | } else { |
---|
[986] | 250 | nodes[edges[n].target].first_in = edges[n].next_in; |
---|
[946] | 251 | } |
---|
| 252 | |
---|
[397] | 253 | |
---|
[946] | 254 | if(edges[n].next_out!=-1) { |
---|
[397] | 255 | edges[edges[n].next_out].prev_out = edges[n].prev_out; |
---|
[946] | 256 | } |
---|
| 257 | |
---|
| 258 | if(edges[n].prev_out!=-1) { |
---|
[397] | 259 | edges[edges[n].prev_out].next_out = edges[n].next_out; |
---|
[946] | 260 | } else { |
---|
[986] | 261 | nodes[edges[n].source].first_out = edges[n].next_out; |
---|
[946] | 262 | } |
---|
[397] | 263 | |
---|
| 264 | edges[n].next_in = first_free_edge; |
---|
[695] | 265 | first_free_edge = n; |
---|
[397] | 266 | |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | void clear() { |
---|
[782] | 270 | edges.clear(); |
---|
| 271 | nodes.clear(); |
---|
[946] | 272 | first_node = first_free_node = first_free_edge = -1; |
---|
[937] | 273 | } |
---|
| 274 | |
---|
[949] | 275 | protected: |
---|
[1546] | 276 | void _changeTarget(Edge e, Node n) |
---|
[949] | 277 | { |
---|
| 278 | if(edges[e.id].next_in != -1) |
---|
| 279 | edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in; |
---|
| 280 | if(edges[e.id].prev_in != -1) |
---|
| 281 | edges[edges[e.id].prev_in].next_in = edges[e.id].next_in; |
---|
[986] | 282 | else nodes[edges[e.id].target].first_in = edges[e.id].next_in; |
---|
[1702] | 283 | if (nodes[n.id].first_in != -1) { |
---|
| 284 | edges[nodes[n.id].first_in].prev_in = e.id; |
---|
| 285 | } |
---|
[986] | 286 | edges[e.id].target = n.id; |
---|
[949] | 287 | edges[e.id].prev_in = -1; |
---|
| 288 | edges[e.id].next_in = nodes[n.id].first_in; |
---|
| 289 | nodes[n.id].first_in = e.id; |
---|
| 290 | } |
---|
[1546] | 291 | void _changeSource(Edge e, Node n) |
---|
[949] | 292 | { |
---|
| 293 | if(edges[e.id].next_out != -1) |
---|
| 294 | edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out; |
---|
| 295 | if(edges[e.id].prev_out != -1) |
---|
| 296 | edges[edges[e.id].prev_out].next_out = edges[e.id].next_out; |
---|
[986] | 297 | else nodes[edges[e.id].source].first_out = edges[e.id].next_out; |
---|
[1702] | 298 | if (nodes[n.id].first_out != -1) { |
---|
| 299 | edges[nodes[n.id].first_out].prev_out = e.id; |
---|
| 300 | } |
---|
[986] | 301 | edges[e.id].source = n.id; |
---|
[949] | 302 | edges[e.id].prev_out = -1; |
---|
| 303 | edges[e.id].next_out = nodes[n.id].first_out; |
---|
| 304 | nodes[n.id].first_out = e.id; |
---|
| 305 | } |
---|
| 306 | |
---|
[919] | 307 | }; |
---|
[909] | 308 | |
---|
[1979] | 309 | typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
---|
[400] | 310 | |
---|
[1718] | 311 | /// \addtogroup graphs |
---|
| 312 | /// @{ |
---|
[400] | 313 | |
---|
[948] | 314 | ///A list graph class. |
---|
[400] | 315 | |
---|
[948] | 316 | ///This is a simple and fast erasable graph implementation. |
---|
| 317 | /// |
---|
[1010] | 318 | ///It addition that it conforms to the |
---|
| 319 | ///\ref concept::ErasableGraph "ErasableGraph" concept, |
---|
| 320 | ///it also provides several additional useful extra functionalities. |
---|
[959] | 321 | ///\sa concept::ErasableGraph. |
---|
[782] | 322 | |
---|
[1669] | 323 | class ListGraph : public ExtendedListGraphBase |
---|
[948] | 324 | { |
---|
| 325 | public: |
---|
[1546] | 326 | /// Changes the target of \c e to \c n |
---|
[948] | 327 | |
---|
[1546] | 328 | /// Changes the target of \c e to \c n |
---|
[948] | 329 | /// |
---|
[1010] | 330 | ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s |
---|
[1546] | 331 | ///referencing the changed edge remain |
---|
[1010] | 332 | ///valid. However <tt>InEdge</tt>'s are invalidated. |
---|
[1718] | 333 | void changeTarget(Edge e, Node n) { |
---|
| 334 | _changeTarget(e,n); |
---|
| 335 | } |
---|
[1546] | 336 | /// Changes the source of \c e to \c n |
---|
[948] | 337 | |
---|
[1546] | 338 | /// Changes the source of \c e to \c n |
---|
[948] | 339 | /// |
---|
[1010] | 340 | ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s |
---|
[1546] | 341 | ///referencing the changed edge remain |
---|
[1010] | 342 | ///valid. However <tt>OutEdge</tt>'s are invalidated. |
---|
[1718] | 343 | void changeSource(Edge e, Node n) { |
---|
| 344 | _changeSource(e,n); |
---|
| 345 | } |
---|
[949] | 346 | |
---|
[1010] | 347 | /// Invert the direction of an edge. |
---|
| 348 | |
---|
| 349 | ///\note The <tt>Edge</tt>'s |
---|
[1546] | 350 | ///referencing the changed edge remain |
---|
[1010] | 351 | ///valid. However <tt>OutEdge</tt>'s and <tt>InEdge</tt>'s are invalidated. |
---|
| 352 | void reverseEdge(Edge e) { |
---|
| 353 | Node t=target(e); |
---|
[1546] | 354 | _changeTarget(e,source(e)); |
---|
| 355 | _changeSource(e,t); |
---|
[1010] | 356 | } |
---|
| 357 | |
---|
| 358 | ///Using this it possible to avoid the superfluous memory allocation. |
---|
| 359 | |
---|
[949] | 360 | ///Using this it possible to avoid the superfluous memory allocation. |
---|
| 361 | ///\todo more docs... |
---|
| 362 | void reserveEdge(int n) { edges.reserve(n); }; |
---|
[1010] | 363 | |
---|
| 364 | ///Contract two nodes. |
---|
| 365 | |
---|
| 366 | ///This function contracts two nodes. |
---|
| 367 | /// |
---|
| 368 | ///Node \p b will be removed but instead of deleting |
---|
| 369 | ///its neighboring edges, they will be joined to \p a. |
---|
| 370 | ///The last parameter \p r controls whether to remove loops. \c true |
---|
| 371 | ///means that loops will be removed. |
---|
| 372 | /// |
---|
| 373 | ///\note The <tt>Edge</tt>s |
---|
[1281] | 374 | ///referencing a moved edge remain |
---|
[1010] | 375 | ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 376 | ///may be invalidated. |
---|
[1718] | 377 | void contract(Node a, Node b, bool r = true) |
---|
[1010] | 378 | { |
---|
| 379 | for(OutEdgeIt e(*this,b);e!=INVALID;) { |
---|
| 380 | OutEdgeIt f=e; |
---|
| 381 | ++f; |
---|
| 382 | if(r && target(e)==a) erase(e); |
---|
[1546] | 383 | else changeSource(e,a); |
---|
[1010] | 384 | e=f; |
---|
| 385 | } |
---|
| 386 | for(InEdgeIt e(*this,b);e!=INVALID;) { |
---|
| 387 | InEdgeIt f=e; |
---|
| 388 | ++f; |
---|
| 389 | if(r && source(e)==a) erase(e); |
---|
[1546] | 390 | else changeTarget(e,a); |
---|
[1010] | 391 | e=f; |
---|
| 392 | } |
---|
| 393 | erase(b); |
---|
| 394 | } |
---|
[1011] | 395 | |
---|
[1281] | 396 | ///Split a node. |
---|
[1011] | 397 | |
---|
[1284] | 398 | ///This function splits a node. First a new node is added to the graph, |
---|
| 399 | ///then the source of each outgoing edge of \c n is moved to this new node. |
---|
[1281] | 400 | ///If \c connect is \c true (this is the default value), then a new edge |
---|
| 401 | ///from \c n to the newly created node is also added. |
---|
| 402 | ///\return The newly created node. |
---|
| 403 | /// |
---|
| 404 | ///\note The <tt>Edge</tt>s |
---|
| 405 | ///referencing a moved edge remain |
---|
| 406 | ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 407 | ///may be invalidated. |
---|
[1770] | 408 | ///\warning This functionality cannot be used together with the Snapshot |
---|
[1284] | 409 | ///feature. |
---|
[1281] | 410 | ///\todo It could be implemented in a bit faster way. |
---|
| 411 | Node split(Node n, bool connect = true) |
---|
| 412 | { |
---|
| 413 | Node b = addNode(); |
---|
| 414 | for(OutEdgeIt e(*this,n);e!=INVALID;) { |
---|
| 415 | OutEdgeIt f=e; |
---|
| 416 | ++f; |
---|
[1546] | 417 | changeSource(e,b); |
---|
[1281] | 418 | e=f; |
---|
| 419 | } |
---|
| 420 | if(connect) addEdge(n,b); |
---|
| 421 | return b; |
---|
| 422 | } |
---|
| 423 | |
---|
[1812] | 424 | ///Split an edge. |
---|
| 425 | |
---|
| 426 | ///This function splits an edge. First a new node \c b is added to the graph, |
---|
| 427 | ///then the original edge is re-targetes to \c b. Finally an edge |
---|
| 428 | ///from \c b to the original target is added. |
---|
| 429 | ///\return The newly created node. |
---|
| 430 | ///\warning This functionality cannot be used together with the Snapshot |
---|
| 431 | ///feature. |
---|
| 432 | Node split(Edge e) |
---|
| 433 | { |
---|
| 434 | Node b = addNode(); |
---|
| 435 | addEdge(b,target(e)); |
---|
| 436 | changeTarget(e,b); |
---|
| 437 | return b; |
---|
| 438 | } |
---|
| 439 | |
---|
[1011] | 440 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 441 | |
---|
| 442 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 443 | /// |
---|
| 444 | ///The newly added nodes and edges can be removed using the |
---|
| 445 | ///restore() function. |
---|
| 446 | /// |
---|
| 447 | ///\warning Edge and node deletions cannot be restored. |
---|
[1770] | 448 | ///\warning Snapshots cannot be nested. |
---|
| 449 | class Snapshot : protected AlterationNotifier<Node>::ObserverBase, |
---|
[1039] | 450 | protected AlterationNotifier<Edge>::ObserverBase |
---|
[1011] | 451 | { |
---|
[1774] | 452 | public: |
---|
| 453 | |
---|
| 454 | class UnsupportedOperation : public LogicError { |
---|
| 455 | public: |
---|
| 456 | virtual const char* exceptionName() const { |
---|
| 457 | return "lemon::ListGraph::Snapshot::UnsupportedOperation"; |
---|
| 458 | } |
---|
| 459 | }; |
---|
| 460 | |
---|
| 461 | |
---|
[1011] | 462 | protected: |
---|
| 463 | |
---|
| 464 | ListGraph *g; |
---|
| 465 | std::list<Node> added_nodes; |
---|
| 466 | std::list<Edge> added_edges; |
---|
| 467 | |
---|
| 468 | bool active; |
---|
| 469 | virtual void add(const Node& n) { |
---|
| 470 | added_nodes.push_back(n); |
---|
| 471 | }; |
---|
| 472 | virtual void erase(const Node&) |
---|
| 473 | { |
---|
[1774] | 474 | throw UnsupportedOperation(); |
---|
[1011] | 475 | } |
---|
| 476 | virtual void add(const Edge& n) { |
---|
| 477 | added_edges.push_back(n); |
---|
| 478 | }; |
---|
| 479 | virtual void erase(const Edge&) |
---|
| 480 | { |
---|
[1774] | 481 | throw UnsupportedOperation(); |
---|
[1011] | 482 | } |
---|
| 483 | |
---|
[1457] | 484 | ///\bug What is this used for? |
---|
| 485 | /// |
---|
| 486 | virtual void build() {} |
---|
| 487 | ///\bug What is this used for? |
---|
| 488 | /// |
---|
| 489 | virtual void clear() {} |
---|
| 490 | |
---|
[1011] | 491 | void regist(ListGraph &_g) { |
---|
| 492 | g=&_g; |
---|
[1039] | 493 | AlterationNotifier<Node>::ObserverBase:: |
---|
[1040] | 494 | attach(g->getNotifier(Node())); |
---|
[1039] | 495 | AlterationNotifier<Edge>::ObserverBase:: |
---|
[1040] | 496 | attach(g->getNotifier(Edge())); |
---|
[1011] | 497 | } |
---|
| 498 | |
---|
| 499 | void deregist() { |
---|
[1039] | 500 | AlterationNotifier<Node>::ObserverBase:: |
---|
[1011] | 501 | detach(); |
---|
[1039] | 502 | AlterationNotifier<Edge>::ObserverBase:: |
---|
[1011] | 503 | detach(); |
---|
| 504 | g=0; |
---|
| 505 | } |
---|
[1774] | 506 | |
---|
[1011] | 507 | public: |
---|
| 508 | ///Default constructur. |
---|
| 509 | |
---|
| 510 | ///Default constructur. |
---|
| 511 | ///To actually make a snapshot you must call save(). |
---|
| 512 | /// |
---|
[1770] | 513 | Snapshot() : g(0) {} |
---|
[1011] | 514 | ///Constructor that immediately makes a snapshot. |
---|
| 515 | |
---|
| 516 | ///This constructor immediately makes a snapshot of the graph. |
---|
| 517 | ///\param _g The graph we make a snapshot of. |
---|
[1770] | 518 | Snapshot(ListGraph &_g) { |
---|
[1011] | 519 | regist(_g); |
---|
| 520 | } |
---|
| 521 | ///\bug Is it necessary? |
---|
| 522 | /// |
---|
[1770] | 523 | ~Snapshot() |
---|
[1011] | 524 | { |
---|
| 525 | if(g) deregist(); |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | ///Make a snapshot. |
---|
| 529 | |
---|
| 530 | ///Make a snapshot of the graph. |
---|
| 531 | /// |
---|
| 532 | ///This function can be called more than once. In case of a repeated |
---|
| 533 | ///call, the previous snapshot gets lost. |
---|
| 534 | ///\param _g The graph we make the snapshot of. |
---|
| 535 | void save(ListGraph &_g) |
---|
| 536 | { |
---|
| 537 | if(g!=&_g) { |
---|
| 538 | if(g) deregist(); |
---|
| 539 | regist(_g); |
---|
| 540 | } |
---|
| 541 | added_nodes.clear(); |
---|
| 542 | added_edges.clear(); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | ///Undo the changes until the last snapshot. |
---|
| 546 | |
---|
| 547 | ///Undo the changes until last snapshot created by save(). |
---|
| 548 | /// |
---|
| 549 | ///\todo This function might be called undo(). |
---|
| 550 | void restore() { |
---|
[1457] | 551 | ListGraph &old_g=*g; |
---|
[1011] | 552 | deregist(); |
---|
| 553 | while(!added_edges.empty()) { |
---|
[1457] | 554 | old_g.erase(added_edges.front()); |
---|
[1011] | 555 | added_edges.pop_front(); |
---|
| 556 | } |
---|
| 557 | while(!added_nodes.empty()) { |
---|
[1457] | 558 | old_g.erase(added_nodes.front()); |
---|
[1011] | 559 | added_nodes.pop_front(); |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | }; |
---|
| 563 | |
---|
[949] | 564 | }; |
---|
[1034] | 565 | |
---|
[1555] | 566 | ///@} |
---|
[1034] | 567 | |
---|
| 568 | /**************** Undirected List Graph ****************/ |
---|
| 569 | |
---|
[1979] | 570 | typedef UGraphExtender<UGraphBaseExtender< |
---|
| 571 | ListGraphBase> > ExtendedListUGraphBase; |
---|
[1034] | 572 | |
---|
[1718] | 573 | /// \addtogroup graphs |
---|
| 574 | /// @{ |
---|
[1555] | 575 | |
---|
[1035] | 576 | ///An undirected list graph class. |
---|
| 577 | |
---|
| 578 | ///This is a simple and fast erasable undirected graph implementation. |
---|
| 579 | /// |
---|
| 580 | ///It conforms to the |
---|
[1909] | 581 | ///\ref concept::UGraph "UGraph" concept. |
---|
[1035] | 582 | /// |
---|
[1909] | 583 | ///\sa concept::UGraph. |
---|
[1035] | 584 | /// |
---|
[1770] | 585 | ///\todo Snapshot, reverseEdge(), changeTarget(), changeSource(), contract() |
---|
[1161] | 586 | ///haven't been implemented yet. |
---|
[1035] | 587 | /// |
---|
[1909] | 588 | class ListUGraph : public ExtendedListUGraphBase { |
---|
[1718] | 589 | public: |
---|
[1909] | 590 | typedef ExtendedListUGraphBase Parent; |
---|
[1718] | 591 | /// \brief Changes the target of \c e to \c n |
---|
| 592 | /// |
---|
| 593 | /// Changes the target of \c e to \c n |
---|
| 594 | /// |
---|
| 595 | /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 596 | /// referencing the changed edge remain |
---|
| 597 | /// valid. However <tt>InEdge</tt>'s are invalidated. |
---|
[1909] | 598 | void changeTarget(UEdge e, Node n) { |
---|
[1718] | 599 | _changeTarget(e,n); |
---|
| 600 | } |
---|
| 601 | /// Changes the source of \c e to \c n |
---|
| 602 | /// |
---|
| 603 | /// Changes the source of \c e to \c n |
---|
| 604 | /// |
---|
| 605 | ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s |
---|
| 606 | ///referencing the changed edge remain |
---|
| 607 | ///valid. However <tt>OutEdge</tt>'s are invalidated. |
---|
[1909] | 608 | void changeSource(UEdge e, Node n) { |
---|
[1718] | 609 | _changeSource(e,n); |
---|
| 610 | } |
---|
| 611 | /// \brief Contract two nodes. |
---|
| 612 | /// |
---|
| 613 | /// This function contracts two nodes. |
---|
| 614 | /// |
---|
| 615 | /// Node \p b will be removed but instead of deleting |
---|
| 616 | /// its neighboring edges, they will be joined to \p a. |
---|
| 617 | /// The last parameter \p r controls whether to remove loops. \c true |
---|
| 618 | /// means that loops will be removed. |
---|
| 619 | /// |
---|
| 620 | /// \note The <tt>Edge</tt>s |
---|
| 621 | /// referencing a moved edge remain |
---|
| 622 | /// valid. |
---|
| 623 | void contract(Node a, Node b, bool r = true) { |
---|
| 624 | for(IncEdgeIt e(*this, b); e!=INVALID;) { |
---|
| 625 | IncEdgeIt f = e; ++f; |
---|
| 626 | if (r && runningNode(e) == a) { |
---|
| 627 | erase(e); |
---|
| 628 | } else if (source(e) == b) { |
---|
| 629 | changeSource(e, a); |
---|
| 630 | } else { |
---|
| 631 | changeTarget(e, a); |
---|
| 632 | } |
---|
| 633 | e = f; |
---|
| 634 | } |
---|
| 635 | erase(b); |
---|
| 636 | } |
---|
[1034] | 637 | }; |
---|
| 638 | |
---|
[1982] | 639 | |
---|
| 640 | class ListBpUGraphBase { |
---|
| 641 | public: |
---|
| 642 | |
---|
| 643 | class NodeSetError : public LogicError { |
---|
| 644 | virtual const char* exceptionName() const { |
---|
| 645 | return "lemon::ListBpUGraph::NodeSetError"; |
---|
| 646 | } |
---|
| 647 | }; |
---|
| 648 | |
---|
| 649 | protected: |
---|
| 650 | |
---|
| 651 | struct NodeT { |
---|
| 652 | int first_edge, next_node; |
---|
| 653 | }; |
---|
| 654 | |
---|
| 655 | struct EdgeT { |
---|
| 656 | int aNode, prev_out, next_out; |
---|
| 657 | int bNode, prev_in, next_in; |
---|
| 658 | }; |
---|
| 659 | |
---|
| 660 | std::vector<NodeT> aNodes; |
---|
| 661 | std::vector<NodeT> bNodes; |
---|
| 662 | |
---|
| 663 | std::vector<EdgeT> edges; |
---|
| 664 | |
---|
| 665 | int first_anode; |
---|
| 666 | int first_free_anode; |
---|
| 667 | |
---|
| 668 | int first_bnode; |
---|
| 669 | int first_free_bnode; |
---|
| 670 | |
---|
| 671 | int first_free_edge; |
---|
| 672 | |
---|
| 673 | public: |
---|
| 674 | |
---|
| 675 | class Node { |
---|
| 676 | friend class ListBpUGraphBase; |
---|
| 677 | protected: |
---|
| 678 | int id; |
---|
| 679 | |
---|
| 680 | Node(int _id) : id(_id) {} |
---|
| 681 | public: |
---|
| 682 | Node() {} |
---|
| 683 | Node(Invalid) { id = -1; } |
---|
| 684 | bool operator==(const Node i) const {return id==i.id;} |
---|
| 685 | bool operator!=(const Node i) const {return id!=i.id;} |
---|
| 686 | bool operator<(const Node i) const {return id<i.id;} |
---|
| 687 | }; |
---|
| 688 | |
---|
| 689 | class Edge { |
---|
| 690 | friend class ListBpUGraphBase; |
---|
| 691 | protected: |
---|
| 692 | int id; |
---|
| 693 | |
---|
| 694 | Edge(int _id) { id = _id;} |
---|
| 695 | public: |
---|
| 696 | Edge() {} |
---|
| 697 | Edge (Invalid) { id = -1; } |
---|
| 698 | bool operator==(const Edge i) const {return id==i.id;} |
---|
| 699 | bool operator!=(const Edge i) const {return id!=i.id;} |
---|
| 700 | bool operator<(const Edge i) const {return id<i.id;} |
---|
| 701 | }; |
---|
| 702 | |
---|
| 703 | ListBpUGraphBase() |
---|
| 704 | : first_anode(-1), first_free_anode(-1), |
---|
| 705 | first_bnode(-1), first_free_bnode(-1), |
---|
| 706 | first_free_edge(-1) {} |
---|
| 707 | |
---|
| 708 | void firstANode(Node& node) const { |
---|
| 709 | node.id = first_anode != -1 ? (first_anode << 1) : -1; |
---|
| 710 | } |
---|
| 711 | void nextANode(Node& node) const { |
---|
| 712 | node.id = aNodes[node.id >> 1].next_node; |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | void firstBNode(Node& node) const { |
---|
| 716 | node.id = first_bnode != -1 ? (first_bnode << 1) + 1 : -1; |
---|
| 717 | } |
---|
| 718 | void nextBNode(Node& node) const { |
---|
[1984] | 719 | node.id = bNodes[node.id >> 1].next_node; |
---|
[1982] | 720 | } |
---|
| 721 | |
---|
| 722 | void first(Node& node) const { |
---|
| 723 | if (first_anode != -1) { |
---|
| 724 | node.id = (first_anode << 1); |
---|
| 725 | } else if (first_bnode != -1) { |
---|
| 726 | node.id = (first_bnode << 1) + 1; |
---|
| 727 | } else { |
---|
| 728 | node.id = -1; |
---|
| 729 | } |
---|
| 730 | } |
---|
| 731 | void next(Node& node) const { |
---|
| 732 | if (aNode(node)) { |
---|
| 733 | node.id = aNodes[node.id >> 1].next_node; |
---|
| 734 | if (node.id == -1) { |
---|
| 735 | if (first_bnode != -1) { |
---|
| 736 | node.id = (first_bnode << 1) + 1; |
---|
| 737 | } |
---|
| 738 | } |
---|
| 739 | } else { |
---|
| 740 | node.id = bNodes[node.id >> 1].next_node; |
---|
| 741 | } |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | void first(Edge& edge) const { |
---|
| 745 | int aNodeId = first_anode; |
---|
| 746 | while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) { |
---|
| 747 | aNodeId = aNodes[aNodeId].next_node != -1 ? |
---|
| 748 | aNodes[aNodeId].next_node >> 1 : -1; |
---|
| 749 | } |
---|
| 750 | if (aNodeId != -1) { |
---|
| 751 | edge.id = aNodes[aNodeId].first_edge; |
---|
| 752 | } else { |
---|
| 753 | edge.id = -1; |
---|
| 754 | } |
---|
| 755 | } |
---|
| 756 | void next(Edge& edge) const { |
---|
| 757 | int aNodeId = edges[edge.id].aNode >> 1; |
---|
| 758 | edge.id = edges[edge.id].next_out; |
---|
| 759 | if (edge.id == -1) { |
---|
| 760 | aNodeId = aNodes[aNodeId].next_node != -1 ? |
---|
| 761 | aNodes[aNodeId].next_node >> 1 : -1; |
---|
| 762 | while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) { |
---|
| 763 | aNodeId = aNodes[aNodeId].next_node != -1 ? |
---|
| 764 | aNodes[aNodeId].next_node >> 1 : -1; |
---|
| 765 | } |
---|
| 766 | if (aNodeId != -1) { |
---|
| 767 | edge.id = aNodes[aNodeId].first_edge; |
---|
| 768 | } else { |
---|
| 769 | edge.id = -1; |
---|
| 770 | } |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | void firstOut(Edge& edge, const Node& node) const { |
---|
| 775 | LEMON_ASSERT((node.id & 1) == 0, NodeSetError()); |
---|
| 776 | edge.id = aNodes[node.id >> 1].first_edge; |
---|
| 777 | } |
---|
| 778 | void nextOut(Edge& edge) const { |
---|
| 779 | edge.id = edges[edge.id].next_out; |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | void firstIn(Edge& edge, const Node& node) const { |
---|
| 783 | LEMON_ASSERT((node.id & 1) == 1, NodeSetError()); |
---|
| 784 | edge.id = bNodes[node.id >> 1].first_edge; |
---|
| 785 | } |
---|
| 786 | void nextIn(Edge& edge) const { |
---|
| 787 | edge.id = edges[edge.id].next_in; |
---|
| 788 | } |
---|
| 789 | |
---|
| 790 | static int id(const Node& node) { |
---|
| 791 | return node.id; |
---|
| 792 | } |
---|
| 793 | static Node nodeFromId(int id) { |
---|
| 794 | return Node(id); |
---|
| 795 | } |
---|
| 796 | int maxNodeId() const { |
---|
| 797 | return aNodes.size() > bNodes.size() ? |
---|
| 798 | aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1; |
---|
| 799 | } |
---|
| 800 | |
---|
| 801 | static int id(const Edge& edge) { |
---|
| 802 | return edge.id; |
---|
| 803 | } |
---|
| 804 | static Edge edgeFromId(int id) { |
---|
| 805 | return Edge(id); |
---|
| 806 | } |
---|
| 807 | int maxEdgeId() const { |
---|
| 808 | return edges.size(); |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | static int aNodeId(const Node& node) { |
---|
| 812 | return node.id >> 1; |
---|
| 813 | } |
---|
[1995] | 814 | static Node fromANodeId(int id) { |
---|
[1982] | 815 | return Node(id << 1); |
---|
| 816 | } |
---|
| 817 | int maxANodeId() const { |
---|
| 818 | return aNodes.size(); |
---|
| 819 | } |
---|
| 820 | |
---|
| 821 | static int bNodeId(const Node& node) { |
---|
| 822 | return node.id >> 1; |
---|
| 823 | } |
---|
| 824 | static Node fromBNodeId(int id) { |
---|
| 825 | return Node((id << 1) + 1); |
---|
| 826 | } |
---|
| 827 | int maxBNodeId() const { |
---|
| 828 | return bNodes.size(); |
---|
| 829 | } |
---|
| 830 | |
---|
| 831 | Node aNode(const Edge& edge) const { |
---|
| 832 | return Node(edges[edge.id].aNode); |
---|
| 833 | } |
---|
| 834 | Node bNode(const Edge& edge) const { |
---|
| 835 | return Node(edges[edge.id].bNode); |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | static bool aNode(const Node& node) { |
---|
| 839 | return (node.id & 1) == 0; |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | static bool bNode(const Node& node) { |
---|
| 843 | return (node.id & 1) == 1; |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | Node addANode() { |
---|
| 847 | int aNodeId; |
---|
| 848 | if (first_free_anode == -1) { |
---|
| 849 | aNodeId = aNodes.size(); |
---|
| 850 | aNodes.push_back(NodeT()); |
---|
| 851 | } else { |
---|
| 852 | aNodeId = first_free_anode; |
---|
| 853 | first_free_anode = aNodes[first_free_anode].next_node; |
---|
| 854 | } |
---|
| 855 | aNodes[aNodeId].next_node = |
---|
| 856 | first_anode != -1 ? (first_anode << 1) : -1; |
---|
| 857 | first_anode = aNodeId; |
---|
| 858 | aNodes[aNodeId].first_edge = -1; |
---|
| 859 | return Node(aNodeId << 1); |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | Node addBNode() { |
---|
| 863 | int bNodeId; |
---|
[1984] | 864 | if (first_free_bnode == -1) { |
---|
[1982] | 865 | bNodeId = bNodes.size(); |
---|
| 866 | bNodes.push_back(NodeT()); |
---|
| 867 | } else { |
---|
| 868 | bNodeId = first_free_bnode; |
---|
| 869 | first_free_bnode = bNodes[first_free_bnode].next_node; |
---|
| 870 | } |
---|
| 871 | bNodes[bNodeId].next_node = |
---|
| 872 | first_bnode != -1 ? (first_bnode << 1) + 1 : -1; |
---|
| 873 | first_bnode = bNodeId; |
---|
| 874 | bNodes[bNodeId].first_edge = -1; |
---|
| 875 | return Node((bNodeId << 1) + 1); |
---|
| 876 | } |
---|
| 877 | |
---|
| 878 | Edge addEdge(const Node& source, const Node& target) { |
---|
| 879 | LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError()); |
---|
| 880 | int edgeId; |
---|
| 881 | if (first_free_edge != -1) { |
---|
| 882 | edgeId = first_free_edge; |
---|
| 883 | first_free_edge = edges[edgeId].next_out; |
---|
| 884 | } else { |
---|
| 885 | edgeId = edges.size(); |
---|
| 886 | edges.push_back(EdgeT()); |
---|
| 887 | } |
---|
| 888 | if ((source.id & 1) == 0) { |
---|
| 889 | edges[edgeId].aNode = source.id; |
---|
| 890 | edges[edgeId].bNode = target.id; |
---|
| 891 | } else { |
---|
| 892 | edges[edgeId].aNode = target.id; |
---|
| 893 | edges[edgeId].bNode = source.id; |
---|
| 894 | } |
---|
| 895 | edges[edgeId].next_out = aNodes[edges[edgeId].aNode >> 1].first_edge; |
---|
| 896 | edges[edgeId].prev_out = -1; |
---|
| 897 | if (aNodes[edges[edgeId].aNode >> 1].first_edge != -1) { |
---|
| 898 | edges[aNodes[edges[edgeId].aNode >> 1].first_edge].prev_out = edgeId; |
---|
| 899 | } |
---|
| 900 | aNodes[edges[edgeId].aNode >> 1].first_edge = edgeId; |
---|
| 901 | edges[edgeId].next_in = bNodes[edges[edgeId].bNode >> 1].first_edge; |
---|
| 902 | edges[edgeId].prev_in = -1; |
---|
| 903 | if (bNodes[edges[edgeId].bNode >> 1].first_edge != -1) { |
---|
| 904 | edges[bNodes[edges[edgeId].bNode >> 1].first_edge].prev_in = edgeId; |
---|
| 905 | } |
---|
| 906 | bNodes[edges[edgeId].bNode >> 1].first_edge = edgeId; |
---|
| 907 | return Edge(edgeId); |
---|
| 908 | } |
---|
| 909 | |
---|
| 910 | void erase(const Node& node) { |
---|
| 911 | if (aNode(node)) { |
---|
| 912 | int aNodeId = node.id >> 1; |
---|
| 913 | aNodes[aNodeId].next_node = first_free_anode; |
---|
| 914 | first_free_anode = aNodeId; |
---|
| 915 | } else { |
---|
| 916 | int bNodeId = node.id >> 1; |
---|
| 917 | bNodes[bNodeId].next_node = first_free_bnode; |
---|
| 918 | first_free_bnode = bNodeId; |
---|
| 919 | } |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | void erase(const Edge& edge) { |
---|
| 923 | if (edges[edge.id].prev_out != -1) { |
---|
| 924 | edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out; |
---|
| 925 | } else { |
---|
| 926 | aNodes[edges[edge.id].aNode].first_edge = edges[edge.id].next_out; |
---|
| 927 | } |
---|
| 928 | if (edges[edge.id].next_out != -1) { |
---|
| 929 | edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out; |
---|
| 930 | } |
---|
| 931 | if (edges[edge.id].prev_in != -1) { |
---|
| 932 | edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in; |
---|
| 933 | } else { |
---|
| 934 | bNodes[edges[edge.id].bNode].first_edge = edges[edge.id].next_in; |
---|
| 935 | } |
---|
| 936 | if (edges[edge.id].next_in != -1) { |
---|
| 937 | edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in; |
---|
| 938 | } |
---|
| 939 | edges[edge.id].next_out = first_free_edge; |
---|
| 940 | first_free_edge = edge.id; |
---|
| 941 | } |
---|
| 942 | |
---|
| 943 | void clear() { |
---|
| 944 | aNodes.clear(); |
---|
| 945 | bNodes.clear(); |
---|
| 946 | edges.clear(); |
---|
| 947 | first_anode = -1; |
---|
| 948 | first_free_anode = -1; |
---|
| 949 | first_bnode = -1; |
---|
| 950 | first_free_bnode = -1; |
---|
| 951 | first_free_edge = -1; |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | }; |
---|
| 955 | |
---|
| 956 | |
---|
| 957 | typedef BpUGraphExtender< BpUGraphBaseExtender< |
---|
| 958 | ListBpUGraphBase> > ExtendedListBpUGraphBase; |
---|
| 959 | |
---|
| 960 | /// \ingroup graphs |
---|
| 961 | /// |
---|
| 962 | /// \brief A smart bipartite undirected graph class. |
---|
| 963 | /// |
---|
| 964 | /// This is a bipartite undirected graph implementation. |
---|
[1991] | 965 | /// It is conforms to the \ref concept::ErasableBpUGraph "ErasableBpUGraph" |
---|
| 966 | /// concept. |
---|
[1982] | 967 | /// \sa concept::BpUGraph. |
---|
| 968 | /// |
---|
| 969 | class ListBpUGraph : public ExtendedListBpUGraphBase {}; |
---|
| 970 | |
---|
[949] | 971 | |
---|
[948] | 972 | /// @} |
---|
| 973 | } //namespace lemon |
---|
[946] | 974 | |
---|
[400] | 975 | |
---|
[946] | 976 | #endif |
---|