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