[906] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 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 | */ |
---|
[105] | 16 | |
---|
[921] | 17 | #ifndef LEMON_SMART_GRAPH_H |
---|
| 18 | #define LEMON_SMART_GRAPH_H |
---|
[104] | 19 | |
---|
[491] | 20 | ///\ingroup graphs |
---|
[242] | 21 | ///\file |
---|
[1692] | 22 | ///\brief SmartGraph and UndirSmartGraph classes. |
---|
[242] | 23 | |
---|
[104] | 24 | #include <vector> |
---|
| 25 | |
---|
[921] | 26 | #include <lemon/invalid.h> |
---|
[157] | 27 | |
---|
[1307] | 28 | #include <lemon/bits/clearable_graph_extender.h> |
---|
| 29 | #include <lemon/bits/extendable_graph_extender.h> |
---|
| 30 | #include <lemon/bits/iterable_graph_extender.h> |
---|
| 31 | #include <lemon/bits/alteration_notifier.h> |
---|
| 32 | #include <lemon/bits/default_map.h> |
---|
[946] | 33 | |
---|
[1307] | 34 | #include <lemon/bits/undir_graph_extender.h> |
---|
[1034] | 35 | |
---|
[977] | 36 | #include <lemon/utility.h> |
---|
[782] | 37 | |
---|
[921] | 38 | namespace lemon { |
---|
[104] | 39 | |
---|
[973] | 40 | class SmartGraph; |
---|
[969] | 41 | ///Base of SmartGraph |
---|
| 42 | |
---|
| 43 | ///Base of SmartGraph |
---|
| 44 | /// |
---|
[946] | 45 | class SmartGraphBase { |
---|
[104] | 46 | |
---|
[973] | 47 | friend class SmatGraph; |
---|
| 48 | |
---|
| 49 | protected: |
---|
[104] | 50 | struct NodeT |
---|
| 51 | { |
---|
| 52 | int first_in,first_out; |
---|
[157] | 53 | NodeT() : first_in(-1), first_out(-1) {} |
---|
[104] | 54 | }; |
---|
| 55 | struct EdgeT |
---|
| 56 | { |
---|
[986] | 57 | int target, source, next_in, next_out; |
---|
[104] | 58 | //FIXME: is this necessary? |
---|
[157] | 59 | EdgeT() : next_in(-1), next_out(-1) {} |
---|
[104] | 60 | }; |
---|
| 61 | |
---|
| 62 | std::vector<NodeT> nodes; |
---|
[129] | 63 | |
---|
[104] | 64 | std::vector<EdgeT> edges; |
---|
| 65 | |
---|
[185] | 66 | |
---|
[104] | 67 | public: |
---|
[782] | 68 | |
---|
[946] | 69 | typedef SmartGraphBase Graph; |
---|
[104] | 70 | |
---|
[164] | 71 | class Node; |
---|
| 72 | class Edge; |
---|
[108] | 73 | |
---|
[104] | 74 | |
---|
| 75 | public: |
---|
| 76 | |
---|
[946] | 77 | SmartGraphBase() : nodes(), edges() { } |
---|
[1718] | 78 | SmartGraphBase(const SmartGraphBase &_g) |
---|
| 79 | : nodes(_g.nodes), edges(_g.edges) { } |
---|
[104] | 80 | |
---|
[977] | 81 | typedef True NodeNumTag; |
---|
| 82 | typedef True EdgeNumTag; |
---|
| 83 | |
---|
[813] | 84 | ///Number of nodes. |
---|
| 85 | int nodeNum() const { return nodes.size(); } |
---|
| 86 | ///Number of edges. |
---|
| 87 | int edgeNum() const { return edges.size(); } |
---|
[104] | 88 | |
---|
[813] | 89 | /// Maximum node ID. |
---|
| 90 | |
---|
| 91 | /// Maximum node ID. |
---|
| 92 | ///\sa id(Node) |
---|
[1631] | 93 | int maxId(Node) const { return nodes.size()-1; } |
---|
[813] | 94 | /// Maximum edge ID. |
---|
| 95 | |
---|
| 96 | /// Maximum edge ID. |
---|
| 97 | ///\sa id(Edge) |
---|
[1631] | 98 | int maxId(Edge) const { return edges.size()-1; } |
---|
[108] | 99 | |
---|
[986] | 100 | Node source(Edge e) const { return edges[e.n].source; } |
---|
| 101 | Node target(Edge e) const { return edges[e.n].target; } |
---|
[104] | 102 | |
---|
[813] | 103 | /// Node ID. |
---|
| 104 | |
---|
| 105 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
[1631] | 106 | /// \ref maxId(Node). The range of the ID's is not surely continuous |
---|
| 107 | /// and the greatest node ID can be actually less then \ref maxId(Node). |
---|
[813] | 108 | /// |
---|
| 109 | /// The ID of the \ref INVALID node is -1. |
---|
| 110 | ///\return The ID of the node \c v. |
---|
[713] | 111 | static int id(Node v) { return v.n; } |
---|
[813] | 112 | /// Edge ID. |
---|
| 113 | |
---|
| 114 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
[1631] | 115 | /// \ref maxId(Edge). The range of the ID's is not surely continuous |
---|
| 116 | /// and the greatest edge ID can be actually less then \ref maxId(Edge). |
---|
[813] | 117 | /// |
---|
| 118 | /// The ID of the \ref INVALID edge is -1. |
---|
| 119 | ///\return The ID of the edge \c e. |
---|
[713] | 120 | static int id(Edge e) { return e.n; } |
---|
[104] | 121 | |
---|
[1106] | 122 | static Node fromId(int id, Node) { return Node(id);} |
---|
| 123 | |
---|
| 124 | static Edge fromId(int id, Edge) { return Edge(id);} |
---|
| 125 | |
---|
[164] | 126 | Node addNode() { |
---|
| 127 | Node n; n.n=nodes.size(); |
---|
[104] | 128 | nodes.push_back(NodeT()); //FIXME: Hmmm... |
---|
| 129 | return n; |
---|
| 130 | } |
---|
[108] | 131 | |
---|
[164] | 132 | Edge addEdge(Node u, Node v) { |
---|
| 133 | Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... |
---|
[986] | 134 | edges[e.n].source=u.n; edges[e.n].target=v.n; |
---|
[104] | 135 | edges[e.n].next_out=nodes[u.n].first_out; |
---|
| 136 | edges[e.n].next_in=nodes[v.n].first_in; |
---|
| 137 | nodes[u.n].first_out=nodes[v.n].first_in=e.n; |
---|
[108] | 138 | |
---|
[104] | 139 | return e; |
---|
| 140 | } |
---|
| 141 | |
---|
[782] | 142 | void clear() { |
---|
| 143 | edges.clear(); |
---|
| 144 | nodes.clear(); |
---|
| 145 | } |
---|
[104] | 146 | |
---|
[946] | 147 | |
---|
[164] | 148 | class Node { |
---|
[946] | 149 | friend class SmartGraphBase; |
---|
[973] | 150 | friend class SmartGraph; |
---|
[104] | 151 | |
---|
| 152 | protected: |
---|
| 153 | int n; |
---|
[1641] | 154 | ///\e |
---|
| 155 | |
---|
[973] | 156 | ///\todo It should be removed (or at least define a setToId() instead). |
---|
| 157 | /// |
---|
[164] | 158 | Node(int nn) {n=nn;} |
---|
[104] | 159 | public: |
---|
[164] | 160 | Node() {} |
---|
[503] | 161 | Node (Invalid) { n=-1; } |
---|
[164] | 162 | bool operator==(const Node i) const {return n==i.n;} |
---|
| 163 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
| 164 | bool operator<(const Node i) const {return n<i.n;} |
---|
[104] | 165 | }; |
---|
| 166 | |
---|
| 167 | |
---|
[164] | 168 | class Edge { |
---|
[946] | 169 | friend class SmartGraphBase; |
---|
[973] | 170 | friend class SmartGraph; |
---|
[185] | 171 | |
---|
[104] | 172 | protected: |
---|
| 173 | int n; |
---|
[973] | 174 | ///\todo It should be removed (or at least define a setToId() instead). |
---|
| 175 | /// |
---|
[905] | 176 | Edge(int nn) {n=nn;} |
---|
[706] | 177 | public: |
---|
[164] | 178 | Edge() { } |
---|
[174] | 179 | Edge (Invalid) { n=-1; } |
---|
[164] | 180 | bool operator==(const Edge i) const {return n==i.n;} |
---|
| 181 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
| 182 | bool operator<(const Edge i) const {return n<i.n;} |
---|
[946] | 183 | }; |
---|
[905] | 184 | |
---|
[946] | 185 | void first(Node& node) const { |
---|
| 186 | node.n = nodes.size() - 1; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | static void next(Node& node) { |
---|
| 190 | --node.n; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | void first(Edge& edge) const { |
---|
| 194 | edge.n = edges.size() - 1; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | static void next(Edge& edge) { |
---|
| 198 | --edge.n; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | void firstOut(Edge& edge, const Node& node) const { |
---|
| 202 | edge.n = nodes[node.n].first_out; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void nextOut(Edge& edge) const { |
---|
| 206 | edge.n = edges[edge.n].next_out; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | void firstIn(Edge& edge, const Node& node) const { |
---|
| 210 | edge.n = nodes[node.n].first_in; |
---|
| 211 | } |
---|
[104] | 212 | |
---|
[946] | 213 | void nextIn(Edge& edge) const { |
---|
| 214 | edge.n = edges[edge.n].next_in; |
---|
| 215 | } |
---|
[105] | 216 | |
---|
[969] | 217 | Edge _findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 218 | { |
---|
| 219 | int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; |
---|
[1082] | 220 | while(e!=-1 && edges[e].target!=v.n) e = edges[e].next_out; |
---|
[969] | 221 | prev.n=e; |
---|
| 222 | return prev; |
---|
| 223 | } |
---|
[973] | 224 | |
---|
[1284] | 225 | Node _split(Node n, bool connect = true) |
---|
| 226 | { |
---|
| 227 | Node b = addNode(); |
---|
| 228 | nodes[b.n].first_out=nodes[n.n].first_out; |
---|
| 229 | nodes[n.n].first_out=-1; |
---|
| 230 | for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n; |
---|
| 231 | if(connect) addEdge(n,b); |
---|
| 232 | return b; |
---|
| 233 | } |
---|
| 234 | |
---|
[104] | 235 | }; |
---|
[185] | 236 | |
---|
[1669] | 237 | typedef ClearableGraphExtender< |
---|
| 238 | ExtendableGraphExtender< |
---|
| 239 | MappableGraphExtender< |
---|
| 240 | IterableGraphExtender< |
---|
| 241 | AlterableGraphExtender<SmartGraphBase> > > > > ExtendedSmartGraphBase; |
---|
[937] | 242 | |
---|
[1161] | 243 | /// \addtogroup graphs |
---|
| 244 | /// @{ |
---|
| 245 | |
---|
[950] | 246 | ///A smart graph class. |
---|
[937] | 247 | |
---|
[950] | 248 | ///This is a simple and fast graph implementation. |
---|
| 249 | ///It is also quite memory efficient, but at the price |
---|
[974] | 250 | ///that <b> it does support only limited (only stack-like) |
---|
| 251 | ///node and edge deletions</b>. |
---|
[950] | 252 | ///It conforms to |
---|
[959] | 253 | ///the \ref concept::ExtendableGraph "ExtendableGraph" concept. |
---|
| 254 | ///\sa concept::ExtendableGraph. |
---|
[950] | 255 | /// |
---|
| 256 | ///\author Alpar Juttner |
---|
[1669] | 257 | class SmartGraph : public ExtendedSmartGraphBase { |
---|
[969] | 258 | public: |
---|
| 259 | /// Finds an edge between two nodes. |
---|
[973] | 260 | |
---|
[969] | 261 | /// Finds an edge from node \c u to node \c v. |
---|
| 262 | /// |
---|
| 263 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 264 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 265 | /// the next edge from \c u to \c v after \c prev. |
---|
| 266 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
| 267 | /// |
---|
| 268 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
| 269 | /// \code |
---|
| 270 | /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) { |
---|
| 271 | /// ... |
---|
| 272 | /// } |
---|
| 273 | /// \endcode |
---|
| 274 | /// \todo Possibly it should be a global function. |
---|
| 275 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 276 | { |
---|
| 277 | return _findEdge(u,v,prev); |
---|
| 278 | } |
---|
[973] | 279 | |
---|
[1011] | 280 | class SnapShot; |
---|
| 281 | friend class SnapShot; |
---|
[973] | 282 | |
---|
[1011] | 283 | protected: |
---|
| 284 | void restoreSnapShot(const SnapShot &s) |
---|
[973] | 285 | { |
---|
[1457] | 286 | while(s.edge_num<edges.size()) { |
---|
[1040] | 287 | Parent::getNotifier(Edge()).erase(Edge(edges.size()-1)); |
---|
[986] | 288 | nodes[edges.back().target].first_in=edges.back().next_in; |
---|
| 289 | nodes[edges.back().source].first_out=edges.back().next_out; |
---|
[973] | 290 | edges.pop_back(); |
---|
| 291 | } |
---|
| 292 | //nodes.resize(s.nodes_num); |
---|
[1457] | 293 | while(s.node_num<nodes.size()) { |
---|
[1040] | 294 | Parent::getNotifier(Node()).erase(Node(nodes.size()-1)); |
---|
[973] | 295 | nodes.pop_back(); |
---|
| 296 | } |
---|
[1011] | 297 | } |
---|
| 298 | |
---|
| 299 | public: |
---|
[1284] | 300 | |
---|
| 301 | ///Split a node. |
---|
| 302 | |
---|
| 303 | ///This function splits a node. First a new node is added to the graph, |
---|
| 304 | ///then the source of each outgoing edge of \c n is moved to this new node. |
---|
| 305 | ///If \c connect is \c true (this is the default value), then a new edge |
---|
| 306 | ///from \c n to the newly created node is also added. |
---|
| 307 | ///\return The newly created node. |
---|
| 308 | /// |
---|
| 309 | ///\note The <tt>Edge</tt>s |
---|
| 310 | ///referencing a moved edge remain |
---|
| 311 | ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s |
---|
| 312 | ///may be invalidated. |
---|
| 313 | ///\warning This functionality cannot be used together with the SnapShot |
---|
| 314 | ///feature. |
---|
| 315 | ///\todo It could be implemented in a bit faster way. |
---|
| 316 | Node split(Node n, bool connect = true) |
---|
| 317 | { |
---|
[1718] | 318 | for (OutEdgeIt it(*this, n); it != INVALID; ++it) { |
---|
| 319 | getNotifier(Edge()).signalChange(it); |
---|
| 320 | } |
---|
| 321 | Node b = _split(n,connect); |
---|
| 322 | for (OutEdgeIt it(*this, b); it != INVALID; ++it) { |
---|
| 323 | getNotifier(Edge()).commitChange(it); |
---|
| 324 | } |
---|
| 325 | return b; |
---|
[1284] | 326 | } |
---|
| 327 | |
---|
| 328 | |
---|
[1011] | 329 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 330 | |
---|
| 331 | ///Class to make a snapshot of the graph and to restrore to it later. |
---|
| 332 | /// |
---|
| 333 | ///The newly added nodes and edges can be removed using the |
---|
| 334 | ///restore() function. |
---|
| 335 | ///\note After you restore a state, you cannot restore |
---|
| 336 | ///a later state, in other word you cannot add again the edges deleted |
---|
| 337 | ///by restore() using another SnapShot instance. |
---|
| 338 | /// |
---|
| 339 | class SnapShot |
---|
| 340 | { |
---|
| 341 | SmartGraph *g; |
---|
| 342 | protected: |
---|
| 343 | friend class SmartGraph; |
---|
| 344 | unsigned int node_num; |
---|
| 345 | unsigned int edge_num; |
---|
| 346 | public: |
---|
[1274] | 347 | ///Default constructor. |
---|
[1011] | 348 | |
---|
[1274] | 349 | ///Default constructor. |
---|
[1011] | 350 | ///To actually make a snapshot you must call save(). |
---|
| 351 | /// |
---|
| 352 | SnapShot() : g(0) {} |
---|
| 353 | ///Constructor that immediately makes a snapshot |
---|
| 354 | |
---|
| 355 | ///This constructor immediately makes a snapshot of the graph. |
---|
| 356 | ///\param _g The graph we make a snapshot of. |
---|
| 357 | SnapShot(SmartGraph &_g) :g(&_g) { |
---|
| 358 | node_num=g->nodes.size(); |
---|
| 359 | edge_num=g->edges.size(); |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | ///Make a snapshot. |
---|
| 363 | |
---|
| 364 | ///Make a snapshot of the graph. |
---|
| 365 | /// |
---|
| 366 | ///This function can be called more than once. In case of a repeated |
---|
| 367 | ///call, the previous snapshot gets lost. |
---|
| 368 | ///\param _g The graph we make the snapshot of. |
---|
| 369 | void save(SmartGraph &_g) |
---|
| 370 | { |
---|
| 371 | g=&_g; |
---|
| 372 | node_num=g->nodes.size(); |
---|
| 373 | edge_num=g->edges.size(); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | ///Undo the changes until a snapshot. |
---|
| 377 | |
---|
| 378 | ///Undo the changes until a snapshot created by save(). |
---|
| 379 | /// |
---|
| 380 | ///\note After you restored a state, you cannot restore |
---|
| 381 | ///a later state, in other word you cannot add again the edges deleted |
---|
| 382 | ///by restore(). |
---|
| 383 | /// |
---|
| 384 | ///\todo This function might be called undo(). |
---|
| 385 | |
---|
| 386 | void restore() |
---|
| 387 | { |
---|
| 388 | g->restoreSnapShot(*this); |
---|
| 389 | } |
---|
| 390 | }; |
---|
[973] | 391 | }; |
---|
[1034] | 392 | |
---|
| 393 | |
---|
| 394 | /**************** Undirected List Graph ****************/ |
---|
| 395 | |
---|
| 396 | typedef ClearableUndirGraphExtender< |
---|
| 397 | ExtendableUndirGraphExtender< |
---|
| 398 | MappableUndirGraphExtender< |
---|
| 399 | IterableUndirGraphExtender< |
---|
| 400 | AlterableUndirGraphExtender< |
---|
[1669] | 401 | UndirGraphExtender<SmartGraphBase> > > > > > ExtendedUndirSmartGraphBase; |
---|
[1034] | 402 | |
---|
[1035] | 403 | ///A smart undirected graph class. |
---|
| 404 | |
---|
| 405 | ///This is a simple and fast undirected graph implementation. |
---|
| 406 | ///It is also quite memory efficient, but at the price |
---|
| 407 | ///that <b> it does support only limited (only stack-like) |
---|
| 408 | ///node and edge deletions</b>. |
---|
| 409 | ///Except from this it conforms to |
---|
| 410 | ///the \ref concept::UndirGraph "UndirGraph" concept. |
---|
| 411 | ///\sa concept::UndirGraph. |
---|
| 412 | /// |
---|
| 413 | ///\todo SnapShot hasn't been implemented yet. |
---|
| 414 | /// |
---|
[1669] | 415 | class UndirSmartGraph : public ExtendedUndirSmartGraphBase { |
---|
[1034] | 416 | }; |
---|
| 417 | |
---|
[950] | 418 | |
---|
[407] | 419 | /// @} |
---|
[921] | 420 | } //namespace lemon |
---|
[104] | 421 | |
---|
[157] | 422 | |
---|
[921] | 423 | #endif //LEMON_SMART_GRAPH_H |
---|