[948] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/list_graph.h - Part of LEMON, a generic C++ optimization library |
---|
[948] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[948] | 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
[395] | 16 | |
---|
[921] | 17 | #ifndef LEMON_LIST_GRAPH_H |
---|
| 18 | #define LEMON_LIST_GRAPH_H |
---|
[395] | 19 | |
---|
[948] | 20 | ///\ingroup graphs |
---|
| 21 | ///\file |
---|
[1692] | 22 | ///\brief ListGraph, UndirListGraph classes. |
---|
[948] | 23 | |
---|
[1307] | 24 | #include <lemon/bits/erasable_graph_extender.h> |
---|
| 25 | #include <lemon/bits/clearable_graph_extender.h> |
---|
| 26 | #include <lemon/bits/extendable_graph_extender.h> |
---|
| 27 | #include <lemon/bits/iterable_graph_extender.h> |
---|
| 28 | #include <lemon/bits/alteration_notifier.h> |
---|
| 29 | #include <lemon/bits/default_map.h> |
---|
[395] | 30 | |
---|
[1307] | 31 | #include <lemon/bits/undir_graph_extender.h> |
---|
[782] | 32 | |
---|
[1011] | 33 | #include <list> |
---|
[782] | 34 | |
---|
[921] | 35 | namespace lemon { |
---|
[395] | 36 | |
---|
[946] | 37 | class ListGraphBase { |
---|
[406] | 38 | |
---|
[949] | 39 | protected: |
---|
[946] | 40 | struct NodeT { |
---|
[1470] | 41 | int first_in, first_out; |
---|
[397] | 42 | int prev, next; |
---|
[395] | 43 | }; |
---|
[946] | 44 | |
---|
| 45 | struct EdgeT { |
---|
[986] | 46 | int target, source; |
---|
[397] | 47 | int prev_in, prev_out; |
---|
| 48 | int next_in, next_out; |
---|
[395] | 49 | }; |
---|
| 50 | |
---|
| 51 | std::vector<NodeT> nodes; |
---|
[946] | 52 | |
---|
[397] | 53 | int first_node; |
---|
[946] | 54 | |
---|
[397] | 55 | int first_free_node; |
---|
[946] | 56 | |
---|
[395] | 57 | std::vector<EdgeT> edges; |
---|
[946] | 58 | |
---|
[397] | 59 | int first_free_edge; |
---|
[395] | 60 | |
---|
[782] | 61 | public: |
---|
[395] | 62 | |
---|
[946] | 63 | typedef ListGraphBase Graph; |
---|
[397] | 64 | |
---|
[946] | 65 | class Node { |
---|
[975] | 66 | friend class ListGraphBase; |
---|
[946] | 67 | protected: |
---|
[395] | 68 | |
---|
[946] | 69 | int id; |
---|
| 70 | Node(int pid) { id = pid;} |
---|
[395] | 71 | |
---|
[946] | 72 | public: |
---|
| 73 | Node() {} |
---|
| 74 | Node (Invalid) { id = -1; } |
---|
| 75 | bool operator==(const Node& node) const {return id == node.id;} |
---|
| 76 | bool operator!=(const Node& node) const {return id != node.id;} |
---|
| 77 | bool operator<(const Node& node) const {return id < node.id;} |
---|
| 78 | }; |
---|
[782] | 79 | |
---|
[946] | 80 | class Edge { |
---|
[975] | 81 | friend class ListGraphBase; |
---|
[946] | 82 | protected: |
---|
[782] | 83 | |
---|
[946] | 84 | int id; |
---|
| 85 | Edge(int pid) { id = pid;} |
---|
[395] | 86 | |
---|
[946] | 87 | public: |
---|
| 88 | Edge() {} |
---|
| 89 | Edge (Invalid) { id = -1; } |
---|
| 90 | bool operator==(const Edge& edge) const {return id == edge.id;} |
---|
| 91 | bool operator!=(const Edge& edge) const {return id != edge.id;} |
---|
| 92 | bool operator<(const Edge& edge) const {return id < edge.id;} |
---|
| 93 | }; |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | ListGraphBase() |
---|
[782] | 98 | : nodes(), first_node(-1), |
---|
| 99 | first_free_node(-1), edges(), first_free_edge(-1) {} |
---|
| 100 | |
---|
[395] | 101 | |
---|
[813] | 102 | /// Maximum node ID. |
---|
| 103 | |
---|
| 104 | /// Maximum node ID. |
---|
| 105 | ///\sa id(Node) |
---|
[980] | 106 | int maxId(Node = INVALID) const { return nodes.size()-1; } |
---|
[946] | 107 | |
---|
[813] | 108 | /// Maximum edge ID. |
---|
| 109 | |
---|
| 110 | /// Maximum edge ID. |
---|
| 111 | ///\sa id(Edge) |
---|
[980] | 112 | int maxId(Edge = INVALID) const { return edges.size()-1; } |
---|
[395] | 113 | |
---|
[986] | 114 | Node source(Edge e) const { return edges[e.id].source; } |
---|
| 115 | Node target(Edge e) const { return edges[e.id].target; } |
---|
[395] | 116 | |
---|
| 117 | |
---|
[946] | 118 | void first(Node& node) const { |
---|
| 119 | node.id = first_node; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void next(Node& node) const { |
---|
| 123 | node.id = nodes[node.id].next; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | void first(Edge& e) const { |
---|
| 128 | int n; |
---|
| 129 | for(n = first_node; |
---|
| 130 | n!=-1 && nodes[n].first_in == -1; |
---|
| 131 | n = nodes[n].next); |
---|
| 132 | e.id = (n == -1) ? -1 : nodes[n].first_in; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | void next(Edge& edge) const { |
---|
| 136 | if (edges[edge.id].next_in != -1) { |
---|
| 137 | edge.id = edges[edge.id].next_in; |
---|
| 138 | } else { |
---|
| 139 | int n; |
---|
[986] | 140 | for(n = nodes[edges[edge.id].target].next; |
---|
[946] | 141 | n!=-1 && nodes[n].first_in == -1; |
---|
| 142 | n = nodes[n].next); |
---|
| 143 | edge.id = (n == -1) ? -1 : nodes[n].first_in; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void firstOut(Edge &e, const Node& v) const { |
---|
| 148 | e.id = nodes[v.id].first_out; |
---|
| 149 | } |
---|
| 150 | void nextOut(Edge &e) const { |
---|
| 151 | e.id=edges[e.id].next_out; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | void firstIn(Edge &e, const Node& v) const { |
---|
| 155 | e.id = nodes[v.id].first_in; |
---|
| 156 | } |
---|
| 157 | void nextIn(Edge &e) const { |
---|
| 158 | e.id=edges[e.id].next_in; |
---|
| 159 | } |
---|
| 160 | |
---|
[813] | 161 | |
---|
[946] | 162 | static int id(Node v) { return v.id; } |
---|
| 163 | static int id(Edge e) { return e.id; } |
---|
[395] | 164 | |
---|
[1106] | 165 | static Node fromId(int id, Node) { return Node(id);} |
---|
| 166 | static Edge fromId(int id, Edge) { return Edge(id);} |
---|
| 167 | |
---|
[397] | 168 | /// Adds a new node to the graph. |
---|
| 169 | |
---|
[813] | 170 | /// \warning It adds the new node to the front of the list. |
---|
[397] | 171 | /// (i.e. the lastly added node becomes the first.) |
---|
[946] | 172 | Node addNode() { |
---|
[397] | 173 | int n; |
---|
| 174 | |
---|
[946] | 175 | if(first_free_node==-1) { |
---|
| 176 | n = nodes.size(); |
---|
| 177 | nodes.push_back(NodeT()); |
---|
| 178 | } else { |
---|
[397] | 179 | n = first_free_node; |
---|
| 180 | first_free_node = nodes[n].next; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | nodes[n].next = first_node; |
---|
| 184 | if(first_node != -1) nodes[first_node].prev = n; |
---|
| 185 | first_node = n; |
---|
| 186 | nodes[n].prev = -1; |
---|
| 187 | |
---|
| 188 | nodes[n].first_in = nodes[n].first_out = -1; |
---|
| 189 | |
---|
[946] | 190 | return Node(n); |
---|
[395] | 191 | } |
---|
| 192 | |
---|
| 193 | Edge addEdge(Node u, Node v) { |
---|
[946] | 194 | int n; |
---|
| 195 | |
---|
| 196 | if (first_free_edge == -1) { |
---|
| 197 | n = edges.size(); |
---|
| 198 | edges.push_back(EdgeT()); |
---|
| 199 | } else { |
---|
[397] | 200 | n = first_free_edge; |
---|
| 201 | first_free_edge = edges[n].next_in; |
---|
| 202 | } |
---|
| 203 | |
---|
[986] | 204 | edges[n].source = u.id; |
---|
| 205 | edges[n].target = v.id; |
---|
[395] | 206 | |
---|
[946] | 207 | edges[n].next_out = nodes[u.id].first_out; |
---|
| 208 | if(nodes[u.id].first_out != -1) { |
---|
| 209 | edges[nodes[u.id].first_out].prev_out = n; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | edges[n].next_in = nodes[v.id].first_in; |
---|
| 213 | if(nodes[v.id].first_in != -1) { |
---|
| 214 | edges[nodes[v.id].first_in].prev_in = n; |
---|
| 215 | } |
---|
| 216 | |
---|
[397] | 217 | edges[n].prev_in = edges[n].prev_out = -1; |
---|
| 218 | |
---|
[946] | 219 | nodes[u.id].first_out = nodes[v.id].first_in = n; |
---|
[397] | 220 | |
---|
[946] | 221 | return Edge(n); |
---|
[395] | 222 | } |
---|
[774] | 223 | |
---|
[946] | 224 | void erase(const Node& node) { |
---|
| 225 | int n = node.id; |
---|
| 226 | |
---|
| 227 | if(nodes[n].next != -1) { |
---|
| 228 | nodes[nodes[n].next].prev = nodes[n].prev; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | if(nodes[n].prev != -1) { |
---|
| 232 | nodes[nodes[n].prev].next = nodes[n].next; |
---|
| 233 | } else { |
---|
| 234 | first_node = nodes[n].next; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | nodes[n].next = first_free_node; |
---|
| 238 | first_free_node = n; |
---|
[395] | 239 | |
---|
[774] | 240 | } |
---|
| 241 | |
---|
[946] | 242 | void erase(const Edge& edge) { |
---|
| 243 | int n = edge.id; |
---|
[397] | 244 | |
---|
[946] | 245 | if(edges[n].next_in!=-1) { |
---|
[397] | 246 | edges[edges[n].next_in].prev_in = edges[n].prev_in; |
---|
[946] | 247 | } |
---|
| 248 | |
---|
| 249 | if(edges[n].prev_in!=-1) { |
---|
[397] | 250 | edges[edges[n].prev_in].next_in = edges[n].next_in; |
---|
[946] | 251 | } else { |
---|
[986] | 252 | nodes[edges[n].target].first_in = edges[n].next_in; |
---|
[946] | 253 | } |
---|
| 254 | |
---|
[397] | 255 | |
---|
[946] | 256 | if(edges[n].next_out!=-1) { |
---|
[397] | 257 | edges[edges[n].next_out].prev_out = edges[n].prev_out; |
---|
[946] | 258 | } |
---|
| 259 | |
---|
| 260 | if(edges[n].prev_out!=-1) { |
---|
[397] | 261 | edges[edges[n].prev_out].next_out = edges[n].next_out; |
---|
[946] | 262 | } else { |
---|
[986] | 263 | nodes[edges[n].source].first_out = edges[n].next_out; |
---|
[946] | 264 | } |
---|
[397] | 265 | |
---|
| 266 | edges[n].next_in = first_free_edge; |
---|
[695] | 267 | first_free_edge = n; |
---|
[397] | 268 | |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | void clear() { |
---|
[782] | 272 | edges.clear(); |
---|
| 273 | nodes.clear(); |
---|
[946] | 274 | first_node = first_free_node = first_free_edge = -1; |
---|
[937] | 275 | } |
---|
| 276 | |
---|
[949] | 277 | protected: |
---|
[1546] | 278 | void _changeTarget(Edge e, Node n) |
---|
[949] | 279 | { |
---|
| 280 | if(edges[e.id].next_in != -1) |
---|
| 281 | edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in; |
---|
| 282 | if(edges[e.id].prev_in != -1) |
---|
| 283 | edges[edges[e.id].prev_in].next_in = edges[e.id].next_in; |
---|
[986] | 284 | else nodes[edges[e.id].target].first_in = edges[e.id].next_in; |
---|
[1702] | 285 | if (nodes[n.id].first_in != -1) { |
---|
| 286 | edges[nodes[n.id].first_in].prev_in = e.id; |
---|
| 287 | } |
---|
[986] | 288 | edges[e.id].target = n.id; |
---|
[949] | 289 | edges[e.id].prev_in = -1; |
---|
| 290 | edges[e.id].next_in = nodes[n.id].first_in; |
---|
| 291 | nodes[n.id].first_in = e.id; |
---|
| 292 | } |
---|
[1546] | 293 | void _changeSource(Edge e, Node n) |
---|
[949] | 294 | { |
---|
| 295 | if(edges[e.id].next_out != -1) |
---|
| 296 | edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out; |
---|
| 297 | if(edges[e.id].prev_out != -1) |
---|
| 298 | edges[edges[e.id].prev_out].next_out = edges[e.id].next_out; |
---|
[986] | 299 | else nodes[edges[e.id].source].first_out = edges[e.id].next_out; |
---|
[1702] | 300 | if (nodes[n.id].first_out != -1) { |
---|
| 301 | edges[nodes[n.id].first_out].prev_out = e.id; |
---|
| 302 | } |
---|
[986] | 303 | edges[e.id].source = n.id; |
---|
[949] | 304 | edges[e.id].prev_out = -1; |
---|
| 305 | edges[e.id].next_out = nodes[n.id].first_out; |
---|
| 306 | nodes[n.id].first_out = e.id; |
---|
| 307 | } |
---|
| 308 | |
---|
[919] | 309 | }; |
---|
[909] | 310 | |
---|
[1669] | 311 | typedef ErasableGraphExtender< |
---|
| 312 | ClearableGraphExtender< |
---|
| 313 | ExtendableGraphExtender< |
---|
| 314 | MappableGraphExtender< |
---|
| 315 | IterableGraphExtender< |
---|
| 316 | AlterableGraphExtender<ListGraphBase> > > > > > ExtendedListGraphBase; |
---|
[400] | 317 | |
---|
[1718] | 318 | /// \addtogroup graphs |
---|
| 319 | /// @{ |
---|
[400] | 320 | |
---|
[948] | 321 | ///A list graph class. |
---|
[400] | 322 | |
---|
[948] | 323 | ///This is a simple and fast erasable graph implementation. |
---|
| 324 | /// |
---|
[1010] | 325 | ///It addition that it conforms to the |
---|
| 326 | ///\ref concept::ErasableGraph "ErasableGraph" concept, |
---|
| 327 | ///it also provides several additional useful extra functionalities. |
---|
[959] | 328 | ///\sa concept::ErasableGraph. |
---|
[782] | 329 | |
---|
[1669] | 330 | class ListGraph : public ExtendedListGraphBase |
---|
[948] | 331 | { |
---|
| 332 | public: |
---|
[1546] | 333 | /// Changes the target of \c e to \c n |
---|
[948] | 334 | |
---|
[1546] | 335 | /// Changes the target of \c e to \c n |
---|
[948] | 336 | /// |
---|
[1010] | 337 | ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s |
---|
[1546] | 338 | ///referencing the changed edge remain |
---|
[1010] | 339 | ///valid. However <tt>InEdge</tt>'s are invalidated. |
---|
[1718] | 340 | void changeTarget(Edge e, Node n) { |
---|
| 341 | _changeTarget(e,n); |
---|
| 342 | } |
---|
[1546] | 343 | /// Changes the source of \c e to \c n |
---|
[948] | 344 | |
---|
[1546] | 345 | /// Changes the source of \c e to \c n |
---|
[948] | 346 | /// |
---|
[1010] | 347 | ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s |
---|
[1546] | 348 | ///referencing the changed edge remain |
---|
[1010] | 349 | ///valid. However <tt>OutEdge</tt>'s are invalidated. |
---|
[1718] | 350 | void changeSource(Edge e, Node n) { |
---|
| 351 | _changeSource(e,n); |
---|
| 352 | } |
---|
[949] | 353 | |
---|
[1010] | 354 | /// Invert the direction of an edge. |
---|
| 355 | |
---|
| 356 | ///\note The <tt>Edge</tt>'s |
---|
[1546] | 357 | ///referencing the changed edge remain |
---|
[1010] | 358 | ///valid. However <tt>OutEdge</tt>'s and <tt>InEdge</tt>'s are invalidated. |
---|
| 359 | void reverseEdge(Edge e) { |
---|
| 360 | Node t=target(e); |
---|
[1546] | 361 | _changeTarget(e,source(e)); |
---|
| 362 | _changeSource(e,t); |
---|
[1010] | 363 | } |
---|
| 364 | |
---|
| 365 | ///Using this it possible to avoid the superfluous memory allocation. |
---|
| 366 | |
---|
[949] | 367 | ///Using this it possible to avoid the superfluous memory allocation. |
---|
| 368 | ///\todo more docs... |
---|
| 369 | void reserveEdge(int n) { edges.reserve(n); }; |
---|
[1010] | 370 | |
---|
| 371 | ///Contract two nodes. |
---|
| 372 | |
---|
| 373 | ///This function contracts two nodes. |
---|
| 374 | /// |
---|
| 375 | ///Node \p b will be removed but instead of deleting |
---|
| 376 | ///its neighboring edges, they will be joined to \p a. |
---|
| 377 | ///The last parameter \p r controls whether to remove loops. \c true |
---|
| 378 | ///means that loops will be removed. |
---|
| 379 | /// |
---|
| 380 | ///\note The <tt>Edge</tt>s |
---|
[1281] | 381 | ///referencing a moved edge remain |
---|
[1010] | 382 | ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 383 | ///may be invalidated. |
---|
[1718] | 384 | void contract(Node a, Node b, bool r = true) |
---|
[1010] | 385 | { |
---|
| 386 | for(OutEdgeIt e(*this,b);e!=INVALID;) { |
---|
| 387 | OutEdgeIt f=e; |
---|
| 388 | ++f; |
---|
| 389 | if(r && target(e)==a) erase(e); |
---|
[1546] | 390 | else changeSource(e,a); |
---|
[1010] | 391 | e=f; |
---|
| 392 | } |
---|
| 393 | for(InEdgeIt e(*this,b);e!=INVALID;) { |
---|
| 394 | InEdgeIt f=e; |
---|
| 395 | ++f; |
---|
| 396 | if(r && source(e)==a) erase(e); |
---|
[1546] | 397 | else changeTarget(e,a); |
---|
[1010] | 398 | e=f; |
---|
| 399 | } |
---|
| 400 | erase(b); |
---|
| 401 | } |
---|
[1011] | 402 | |
---|
[1281] | 403 | ///Split a node. |
---|
[1011] | 404 | |
---|
[1284] | 405 | ///This function splits a node. First a new node is added to the graph, |
---|
| 406 | ///then the source of each outgoing edge of \c n is moved to this new node. |
---|
[1281] | 407 | ///If \c connect is \c true (this is the default value), then a new edge |
---|
| 408 | ///from \c n to the newly created node is also added. |
---|
| 409 | ///\return The newly created node. |
---|
| 410 | /// |
---|
| 411 | ///\note The <tt>Edge</tt>s |
---|
| 412 | ///referencing a moved edge remain |
---|
| 413 | ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 414 | ///may be invalidated. |
---|
[1284] | 415 | ///\warning This functionality cannot be used together with the SnapShot |
---|
| 416 | ///feature. |
---|
[1281] | 417 | ///\todo It could be implemented in a bit faster way. |
---|
| 418 | Node split(Node n, bool connect = true) |
---|
| 419 | { |
---|
| 420 | Node b = addNode(); |
---|
| 421 | for(OutEdgeIt e(*this,n);e!=INVALID;) { |
---|
| 422 | OutEdgeIt f=e; |
---|
| 423 | ++f; |
---|
[1546] | 424 | changeSource(e,b); |
---|
[1281] | 425 | e=f; |
---|
| 426 | } |
---|
| 427 | if(connect) addEdge(n,b); |
---|
| 428 | return b; |
---|
| 429 | } |
---|
| 430 | |
---|
[1011] | 431 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 432 | |
---|
| 433 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 434 | /// |
---|
| 435 | ///The newly added nodes and edges can be removed using the |
---|
| 436 | ///restore() function. |
---|
| 437 | /// |
---|
| 438 | ///\warning Edge and node deletions cannot be restored. |
---|
| 439 | ///\warning SnapShots cannot be nested. |
---|
[1035] | 440 | ///\todo \c SnapShot or \c Snapshot? |
---|
[1039] | 441 | class SnapShot : protected AlterationNotifier<Node>::ObserverBase, |
---|
| 442 | protected AlterationNotifier<Edge>::ObserverBase |
---|
[1011] | 443 | { |
---|
| 444 | protected: |
---|
| 445 | |
---|
| 446 | ListGraph *g; |
---|
| 447 | std::list<Node> added_nodes; |
---|
| 448 | std::list<Edge> added_edges; |
---|
| 449 | |
---|
| 450 | bool active; |
---|
| 451 | virtual void add(const Node& n) { |
---|
| 452 | added_nodes.push_back(n); |
---|
| 453 | }; |
---|
| 454 | ///\bug Exception... |
---|
| 455 | /// |
---|
| 456 | virtual void erase(const Node&) |
---|
| 457 | { |
---|
| 458 | exit(1); |
---|
| 459 | } |
---|
| 460 | virtual void add(const Edge& n) { |
---|
| 461 | added_edges.push_back(n); |
---|
| 462 | }; |
---|
| 463 | ///\bug Exception... |
---|
| 464 | /// |
---|
| 465 | virtual void erase(const Edge&) |
---|
| 466 | { |
---|
| 467 | exit(1); |
---|
| 468 | } |
---|
| 469 | |
---|
[1457] | 470 | ///\bug What is this used for? |
---|
| 471 | /// |
---|
| 472 | virtual void build() {} |
---|
| 473 | ///\bug What is this used for? |
---|
| 474 | /// |
---|
| 475 | virtual void clear() {} |
---|
| 476 | |
---|
[1011] | 477 | void regist(ListGraph &_g) { |
---|
| 478 | g=&_g; |
---|
[1039] | 479 | AlterationNotifier<Node>::ObserverBase:: |
---|
[1040] | 480 | attach(g->getNotifier(Node())); |
---|
[1039] | 481 | AlterationNotifier<Edge>::ObserverBase:: |
---|
[1040] | 482 | attach(g->getNotifier(Edge())); |
---|
[1011] | 483 | } |
---|
| 484 | |
---|
| 485 | void deregist() { |
---|
[1039] | 486 | AlterationNotifier<Node>::ObserverBase:: |
---|
[1011] | 487 | detach(); |
---|
[1039] | 488 | AlterationNotifier<Edge>::ObserverBase:: |
---|
[1011] | 489 | detach(); |
---|
| 490 | g=0; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | public: |
---|
| 494 | ///Default constructur. |
---|
| 495 | |
---|
| 496 | ///Default constructur. |
---|
| 497 | ///To actually make a snapshot you must call save(). |
---|
| 498 | /// |
---|
| 499 | SnapShot() : g(0) {} |
---|
| 500 | ///Constructor that immediately makes a snapshot. |
---|
| 501 | |
---|
| 502 | ///This constructor immediately makes a snapshot of the graph. |
---|
| 503 | ///\param _g The graph we make a snapshot of. |
---|
| 504 | SnapShot(ListGraph &_g) { |
---|
| 505 | regist(_g); |
---|
| 506 | } |
---|
| 507 | ///\bug Is it necessary? |
---|
| 508 | /// |
---|
| 509 | ~SnapShot() |
---|
| 510 | { |
---|
| 511 | if(g) deregist(); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | ///Make a snapshot. |
---|
| 515 | |
---|
| 516 | ///Make a snapshot of the graph. |
---|
| 517 | /// |
---|
| 518 | ///This function can be called more than once. In case of a repeated |
---|
| 519 | ///call, the previous snapshot gets lost. |
---|
| 520 | ///\param _g The graph we make the snapshot of. |
---|
| 521 | void save(ListGraph &_g) |
---|
| 522 | { |
---|
| 523 | if(g!=&_g) { |
---|
| 524 | if(g) deregist(); |
---|
| 525 | regist(_g); |
---|
| 526 | } |
---|
| 527 | added_nodes.clear(); |
---|
| 528 | added_edges.clear(); |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | ///Undo the changes until the last snapshot. |
---|
| 532 | |
---|
| 533 | ///Undo the changes until last snapshot created by save(). |
---|
| 534 | /// |
---|
| 535 | ///\todo This function might be called undo(). |
---|
| 536 | void restore() { |
---|
[1457] | 537 | ListGraph &old_g=*g; |
---|
[1011] | 538 | deregist(); |
---|
| 539 | while(!added_edges.empty()) { |
---|
[1457] | 540 | old_g.erase(added_edges.front()); |
---|
[1011] | 541 | added_edges.pop_front(); |
---|
| 542 | } |
---|
| 543 | while(!added_nodes.empty()) { |
---|
[1457] | 544 | old_g.erase(added_nodes.front()); |
---|
[1011] | 545 | added_nodes.pop_front(); |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | }; |
---|
| 549 | |
---|
[949] | 550 | }; |
---|
[1034] | 551 | |
---|
[1555] | 552 | ///@} |
---|
[1034] | 553 | |
---|
| 554 | /**************** Undirected List Graph ****************/ |
---|
| 555 | |
---|
| 556 | typedef ErasableUndirGraphExtender< |
---|
| 557 | ClearableUndirGraphExtender< |
---|
| 558 | ExtendableUndirGraphExtender< |
---|
| 559 | MappableUndirGraphExtender< |
---|
| 560 | IterableUndirGraphExtender< |
---|
| 561 | AlterableUndirGraphExtender< |
---|
[1669] | 562 | UndirGraphExtender<ListGraphBase> > > > > > > ExtendedUndirListGraphBase; |
---|
[1034] | 563 | |
---|
[1718] | 564 | /// \addtogroup graphs |
---|
| 565 | /// @{ |
---|
[1555] | 566 | |
---|
[1035] | 567 | ///An undirected list graph class. |
---|
| 568 | |
---|
| 569 | ///This is a simple and fast erasable undirected graph implementation. |
---|
| 570 | /// |
---|
| 571 | ///It conforms to the |
---|
| 572 | ///\ref concept::UndirGraph "UndirGraph" concept. |
---|
| 573 | /// |
---|
| 574 | ///\sa concept::UndirGraph. |
---|
| 575 | /// |
---|
[1546] | 576 | ///\todo SnapShot, reverseEdge(), changeTarget(), changeSource(), contract() |
---|
[1161] | 577 | ///haven't been implemented yet. |
---|
[1035] | 578 | /// |
---|
[1669] | 579 | class UndirListGraph : public ExtendedUndirListGraphBase { |
---|
[1718] | 580 | public: |
---|
| 581 | typedef ExtendedUndirListGraphBase Parent; |
---|
| 582 | /// \brief Changes the target of \c e to \c n |
---|
| 583 | /// |
---|
| 584 | /// Changes the target of \c e to \c n |
---|
| 585 | /// |
---|
| 586 | /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 587 | /// referencing the changed edge remain |
---|
| 588 | /// valid. However <tt>InEdge</tt>'s are invalidated. |
---|
| 589 | void changeTarget(UndirEdge e, Node n) { |
---|
| 590 | _changeTarget(e,n); |
---|
| 591 | } |
---|
| 592 | /// Changes the source of \c e to \c n |
---|
| 593 | /// |
---|
| 594 | /// Changes the source of \c e to \c n |
---|
| 595 | /// |
---|
| 596 | ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s |
---|
| 597 | ///referencing the changed edge remain |
---|
| 598 | ///valid. However <tt>OutEdge</tt>'s are invalidated. |
---|
| 599 | void changeSource(UndirEdge e, Node n) { |
---|
| 600 | _changeSource(e,n); |
---|
| 601 | } |
---|
| 602 | /// \brief Contract two nodes. |
---|
| 603 | /// |
---|
| 604 | /// This function contracts two nodes. |
---|
| 605 | /// |
---|
| 606 | /// Node \p b will be removed but instead of deleting |
---|
| 607 | /// its neighboring edges, they will be joined to \p a. |
---|
| 608 | /// The last parameter \p r controls whether to remove loops. \c true |
---|
| 609 | /// means that loops will be removed. |
---|
| 610 | /// |
---|
| 611 | /// \note The <tt>Edge</tt>s |
---|
| 612 | /// referencing a moved edge remain |
---|
| 613 | /// valid. |
---|
| 614 | void contract(Node a, Node b, bool r = true) { |
---|
| 615 | for(IncEdgeIt e(*this, b); e!=INVALID;) { |
---|
| 616 | IncEdgeIt f = e; ++f; |
---|
| 617 | if (r && runningNode(e) == a) { |
---|
| 618 | erase(e); |
---|
| 619 | } else if (source(e) == b) { |
---|
| 620 | changeSource(e, a); |
---|
| 621 | } else { |
---|
| 622 | changeTarget(e, a); |
---|
| 623 | } |
---|
| 624 | e = f; |
---|
| 625 | } |
---|
| 626 | erase(b); |
---|
| 627 | } |
---|
[1034] | 628 | }; |
---|
| 629 | |
---|
[949] | 630 | |
---|
[948] | 631 | /// @} |
---|
| 632 | } //namespace lemon |
---|
[946] | 633 | |
---|
[400] | 634 | |
---|
[946] | 635 | #endif |
---|