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