COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/smart_graph.h @ 1695:e6f99fe1723f

Last change on this file since 1695:e6f99fe1723f was 1692:a34203867181, checked in by Balazs Dezso, 19 years ago

Correcting the names in the \files documentation.

File size: 11.1 KB
RevLine 
[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]38namespace 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)
[1631]92    int maxId(Node) const { return nodes.size()-1; }
[813]93    /// Maximum edge ID.
94   
95    /// Maximum edge ID.
96    ///\sa id(Edge)
[1631]97    int maxId(Edge) 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
[1631]105    /// \ref maxId(Node). The range of the ID's is not surely continuous
106    /// and the greatest node ID can be actually less then \ref maxId(Node).
[813]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
[1631]114    /// \ref maxId(Edge). The range of the ID's is not surely continuous
115    /// and the greatest edge ID can be actually less then \ref maxId(Edge).
[813]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;
[1641]153      ///\e
154
[973]155      ///\todo It should be removed (or at least define a setToId() instead).
156      ///
[164]157      Node(int nn) {n=nn;}
[104]158    public:
[164]159      Node() {}
[503]160      Node (Invalid) { n=-1; }
[164]161      bool operator==(const Node i) const {return n==i.n;}
162      bool operator!=(const Node i) const {return n!=i.n;}
163      bool operator<(const Node i) const {return n<i.n;}
[104]164    };
165   
166
[164]167    class Edge {
[946]168      friend class SmartGraphBase;
[973]169      friend class SmartGraph;
[185]170
[104]171    protected:
172      int n;
[973]173      ///\todo It should be removed (or at least define a setToId() instead).
174      ///
[905]175      Edge(int nn) {n=nn;}
[706]176    public:
[164]177      Edge() { }
[174]178      Edge (Invalid) { n=-1; }
[164]179      bool operator==(const Edge i) const {return n==i.n;}
180      bool operator!=(const Edge i) const {return n!=i.n;}
181      bool operator<(const Edge i) const {return n<i.n;}
[946]182    };
[905]183
[946]184    void first(Node& node) const {
185      node.n = nodes.size() - 1;
186    }
187
188    static void next(Node& node) {
189      --node.n;
190    }
191
192    void first(Edge& edge) const {
193      edge.n = edges.size() - 1;
194    }
195
196    static void next(Edge& edge) {
197      --edge.n;
198    }
199
200    void firstOut(Edge& edge, const Node& node) const {
201      edge.n = nodes[node.n].first_out;
202    }
203
204    void nextOut(Edge& edge) const {
205      edge.n = edges[edge.n].next_out;
206    }
207
208    void firstIn(Edge& edge, const Node& node) const {
209      edge.n = nodes[node.n].first_in;
210    }
[104]211   
[946]212    void nextIn(Edge& edge) const {
213      edge.n = edges[edge.n].next_in;
214    }
[105]215
[969]216    Edge _findEdge(Node u,Node v, Edge prev = INVALID)
217    {
218      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
[1082]219      while(e!=-1 && edges[e].target!=v.n) e = edges[e].next_out;
[969]220      prev.n=e;
221      return prev;
222    }
[973]223
[1284]224    Node _split(Node n, bool connect = true)
225    {
226      Node b = addNode();
227      nodes[b.n].first_out=nodes[n.n].first_out;
228      nodes[n.n].first_out=-1;
229      for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
230      if(connect) addEdge(n,b);
231      return b;
232    }
233
[104]234  };
[185]235
[1669]236  typedef ClearableGraphExtender<
237    ExtendableGraphExtender<
238    MappableGraphExtender<
239    IterableGraphExtender<
240    AlterableGraphExtender<SmartGraphBase> > > > > ExtendedSmartGraphBase;
[937]241
[1161]242  /// \addtogroup graphs
243  /// @{
244
[950]245  ///A smart graph class.
[937]246
[950]247  ///This is a simple and fast graph implementation.
248  ///It is also quite memory efficient, but at the price
[974]249  ///that <b> it does support only limited (only stack-like)
250  ///node and edge deletions</b>.
[950]251  ///It conforms to
[959]252  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
253  ///\sa concept::ExtendableGraph.
[950]254  ///
255  ///\author Alpar Juttner
[1669]256  class SmartGraph : public ExtendedSmartGraphBase {
[969]257  public:
258    /// Finds an edge between two nodes.
[973]259   
[969]260    /// Finds an edge from node \c u to node \c v.
261    ///
262    /// If \c prev is \ref INVALID (this is the default value), then
263    /// it finds the first edge from \c u to \c v. Otherwise it looks for
264    /// the next edge from \c u to \c v after \c prev.
265    /// \return The found edge or \ref INVALID if there is no such an edge.
266    ///
267    /// Thus you can iterate through each edge from \c u to \c v as it follows.
268    /// \code
269    /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
270    ///   ...
271    /// }
272    /// \endcode
273    /// \todo Possibly it should be a global function.
274    Edge findEdge(Node u,Node v, Edge prev = INVALID)
275    {
276      return _findEdge(u,v,prev);
277    }
[973]278   
[1011]279    class SnapShot;
280    friend class SnapShot;
[973]281
[1011]282  protected:
283    void restoreSnapShot(const SnapShot &s)
[973]284    {
[1457]285      while(s.edge_num<edges.size()) {
[1040]286        Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
[986]287        nodes[edges.back().target].first_in=edges.back().next_in;
288        nodes[edges.back().source].first_out=edges.back().next_out;
[973]289        edges.pop_back();
290      }
291      //nodes.resize(s.nodes_num);
[1457]292      while(s.node_num<nodes.size()) {
[1040]293        Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
[973]294        nodes.pop_back();
295      }
[1011]296    }   
297
298  public:
[1284]299
300    ///Split a node.
301   
302    ///This function splits a node. First a new node is added to the graph,
303    ///then the source of each outgoing edge of \c n is moved to this new node.
304    ///If \c connect is \c true (this is the default value), then a new edge
305    ///from \c n to the newly created node is also added.
306    ///\return The newly created node.
307    ///
308    ///\note The <tt>Edge</tt>s
309    ///referencing a moved edge remain
310    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
311    ///may be invalidated.
312    ///\warning This functionality cannot be used together with the SnapShot
313    ///feature.
314    ///\todo It could be implemented in a bit faster way.
315    Node split(Node n, bool connect = true)
316    {
317      return _split(n,connect);
318    }
319 
320
[1011]321    ///Class to make a snapshot of the graph and to restrore to it later.
322
323    ///Class to make a snapshot of the graph and to restrore to it later.
324    ///
325    ///The newly added nodes and edges can be removed using the
326    ///restore() function.
327    ///\note After you restore a state, you cannot restore
328    ///a later state, in other word you cannot add again the edges deleted
329    ///by restore() using another SnapShot instance.
330    ///
331    class SnapShot
332    {
333      SmartGraph *g;
334    protected:
335      friend class SmartGraph;
336      unsigned int node_num;
337      unsigned int edge_num;
338    public:
[1274]339      ///Default constructor.
[1011]340     
[1274]341      ///Default constructor.
[1011]342      ///To actually make a snapshot you must call save().
343      ///
344      SnapShot() : g(0) {}
345      ///Constructor that immediately makes a snapshot
346     
347      ///This constructor immediately makes a snapshot of the graph.
348      ///\param _g The graph we make a snapshot of.
349      SnapShot(SmartGraph &_g) :g(&_g) {
350        node_num=g->nodes.size();
351        edge_num=g->edges.size();
352      }
353
354      ///Make a snapshot.
355
356      ///Make a snapshot of the graph.
357      ///
358      ///This function can be called more than once. In case of a repeated
359      ///call, the previous snapshot gets lost.
360      ///\param _g The graph we make the snapshot of.
361      void save(SmartGraph &_g)
362      {
363        g=&_g;
364        node_num=g->nodes.size();
365        edge_num=g->edges.size();
366      }
367
368      ///Undo the changes until a snapshot.
369     
370      ///Undo the changes until a snapshot created by save().
371      ///
372      ///\note After you restored a state, you cannot restore
373      ///a later state, in other word you cannot add again the edges deleted
374      ///by restore().
375      ///
376      ///\todo This function might be called undo().
377     
378      void restore()
379      {
380        g->restoreSnapShot(*this);
381      }
382    };
[973]383  };
[1034]384
385
386  /**************** Undirected List Graph ****************/
387
388  typedef ClearableUndirGraphExtender<
389    ExtendableUndirGraphExtender<
390    MappableUndirGraphExtender<
391    IterableUndirGraphExtender<
392    AlterableUndirGraphExtender<
[1669]393    UndirGraphExtender<SmartGraphBase> > > > > > ExtendedUndirSmartGraphBase;
[1034]394
[1035]395  ///A smart undirected graph class.
396
397  ///This is a simple and fast undirected graph implementation.
398  ///It is also quite memory efficient, but at the price
399  ///that <b> it does support only limited (only stack-like)
400  ///node and edge deletions</b>.
401  ///Except from this it conforms to
402  ///the \ref concept::UndirGraph "UndirGraph" concept.
403  ///\sa concept::UndirGraph.
404  ///
405  ///\todo SnapShot hasn't been implemented yet.
406  ///
[1669]407  class UndirSmartGraph : public ExtendedUndirSmartGraphBase {
[1034]408  };
409
[950]410 
[407]411  /// @} 
[921]412} //namespace lemon
[104]413
[157]414
[921]415#endif //LEMON_SMART_GRAPH_H
Note: See TracBrowser for help on using the repository browser.