COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/smart_graph.h @ 1791:62e7d237e1fb

Last change on this file since 1791:62e7d237e1fb was 1791:62e7d237e1fb, checked in by Balazs Dezso, 18 years ago

Modification on the base graph concept
The extended interface does not changed

File size: 10.0 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>
[1791]33#include <lemon/bits/graph_extender.h>
[1034]34
[977]35#include <lemon/utility.h>
[782]36
[921]37namespace lemon {
[104]38
[973]39  class SmartGraph;
[969]40  ///Base of SmartGraph
41
42  ///Base of SmartGraph
43  ///
[946]44  class SmartGraphBase {
[104]45
[973]46    friend class SmatGraph;
47
48  protected:
[104]49    struct NodeT
50    {
51      int first_in,first_out;     
[157]52      NodeT() : first_in(-1), first_out(-1) {}
[104]53    };
54    struct EdgeT
55    {
[986]56      int target, source, next_in, next_out;     
[104]57      //FIXME: is this necessary?
[157]58      EdgeT() : next_in(-1), next_out(-1) {} 
[104]59    };
60
61    std::vector<NodeT> nodes;
[129]62
[104]63    std::vector<EdgeT> edges;
64   
[185]65   
[104]66  public:
[782]67
[946]68    typedef SmartGraphBase Graph;
[104]69
[164]70    class Node;
71    class Edge;
[108]72
[104]73   
74  public:
75
[946]76    SmartGraphBase() : nodes(), edges() { }
[1718]77    SmartGraphBase(const SmartGraphBase &_g)
78      : 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)
[1791]92    int maxNodeId() const { return nodes.size()-1; }
[813]93    /// Maximum edge ID.
94   
95    /// Maximum edge ID.
96    ///\sa id(Edge)
[1791]97    int maxEdgeId() 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
[1791]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().
[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
[1791]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().
[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
[1791]121    static Node nodeFromId(int id) { return Node(id);}
[1106]122
[1791]123    static Edge edgeFromId(int id) { return Edge(id);}
[1106]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;
[164]153      Node(int nn) {n=nn;}
[104]154    public:
[164]155      Node() {}
[503]156      Node (Invalid) { n=-1; }
[164]157      bool operator==(const Node i) const {return n==i.n;}
158      bool operator!=(const Node i) const {return n!=i.n;}
159      bool operator<(const Node i) const {return n<i.n;}
[104]160    };
161   
162
[164]163    class Edge {
[946]164      friend class SmartGraphBase;
[973]165      friend class SmartGraph;
[185]166
[104]167    protected:
168      int n;
[905]169      Edge(int nn) {n=nn;}
[706]170    public:
[164]171      Edge() { }
[174]172      Edge (Invalid) { n=-1; }
[164]173      bool operator==(const Edge i) const {return n==i.n;}
174      bool operator!=(const Edge i) const {return n!=i.n;}
175      bool operator<(const Edge i) const {return n<i.n;}
[946]176    };
[905]177
[946]178    void first(Node& node) const {
179      node.n = nodes.size() - 1;
180    }
181
182    static void next(Node& node) {
183      --node.n;
184    }
185
186    void first(Edge& edge) const {
187      edge.n = edges.size() - 1;
188    }
189
190    static void next(Edge& edge) {
191      --edge.n;
192    }
193
194    void firstOut(Edge& edge, const Node& node) const {
195      edge.n = nodes[node.n].first_out;
196    }
197
198    void nextOut(Edge& edge) const {
199      edge.n = edges[edge.n].next_out;
200    }
201
202    void firstIn(Edge& edge, const Node& node) const {
203      edge.n = nodes[node.n].first_in;
204    }
[104]205   
[946]206    void nextIn(Edge& edge) const {
207      edge.n = edges[edge.n].next_in;
208    }
[105]209
[1284]210    Node _split(Node n, bool connect = true)
211    {
212      Node b = addNode();
213      nodes[b.n].first_out=nodes[n.n].first_out;
214      nodes[n.n].first_out=-1;
215      for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
216      if(connect) addEdge(n,b);
217      return b;
218    }
219
[104]220  };
[185]221
[1669]222  typedef ClearableGraphExtender<
223    ExtendableGraphExtender<
224    MappableGraphExtender<
225    IterableGraphExtender<
[1791]226    AlterableGraphExtender<
227    GraphExtender<SmartGraphBase> > > > > > ExtendedSmartGraphBase;
[937]228
[1791]229  /// \ingroup graphs
[1161]230
[950]231  ///A smart graph class.
[937]232
[950]233  ///This is a simple and fast graph implementation.
234  ///It is also quite memory efficient, but at the price
[974]235  ///that <b> it does support only limited (only stack-like)
236  ///node and edge deletions</b>.
[950]237  ///It conforms to
[959]238  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
239  ///\sa concept::ExtendableGraph.
[950]240  ///
241  ///\author Alpar Juttner
[1669]242  class SmartGraph : public ExtendedSmartGraphBase {
[969]243  public:
[973]244   
[1770]245    class Snapshot;
246    friend class Snapshot;
[973]247
[1011]248  protected:
[1770]249    void restoreSnapshot(const Snapshot &s)
[973]250    {
[1457]251      while(s.edge_num<edges.size()) {
[1040]252        Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
[986]253        nodes[edges.back().target].first_in=edges.back().next_in;
254        nodes[edges.back().source].first_out=edges.back().next_out;
[973]255        edges.pop_back();
256      }
257      //nodes.resize(s.nodes_num);
[1457]258      while(s.node_num<nodes.size()) {
[1040]259        Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
[973]260        nodes.pop_back();
261      }
[1011]262    }   
263
264  public:
[1284]265
266    ///Split a node.
267   
268    ///This function splits a node. First a new node is added to the graph,
269    ///then the source of each outgoing edge of \c n is moved to this new node.
270    ///If \c connect is \c true (this is the default value), then a new edge
271    ///from \c n to the newly created node is also added.
272    ///\return The newly created node.
273    ///
274    ///\note The <tt>Edge</tt>s
275    ///referencing a moved edge remain
276    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
277    ///may be invalidated.
[1770]278    ///\warning This functionality cannot be used together with the Snapshot
[1284]279    ///feature.
280    ///\todo It could be implemented in a bit faster way.
281    Node split(Node n, bool connect = true)
282    {
[1718]283      Node b = _split(n,connect);
284      return b;
[1284]285    }
286 
287
[1011]288    ///Class to make a snapshot of the graph and to restrore to it later.
289
290    ///Class to make a snapshot of the graph and to restrore to it later.
291    ///
292    ///The newly added nodes and edges can be removed using the
293    ///restore() function.
294    ///\note After you restore a state, you cannot restore
295    ///a later state, in other word you cannot add again the edges deleted
[1770]296    ///by restore() using another Snapshot instance.
[1011]297    ///
[1770]298    class Snapshot
[1011]299    {
300      SmartGraph *g;
301    protected:
302      friend class SmartGraph;
303      unsigned int node_num;
304      unsigned int edge_num;
305    public:
[1274]306      ///Default constructor.
[1011]307     
[1274]308      ///Default constructor.
[1011]309      ///To actually make a snapshot you must call save().
310      ///
[1770]311      Snapshot() : g(0) {}
[1011]312      ///Constructor that immediately makes a snapshot
313     
314      ///This constructor immediately makes a snapshot of the graph.
315      ///\param _g The graph we make a snapshot of.
[1770]316      Snapshot(SmartGraph &_g) :g(&_g) {
[1011]317        node_num=g->nodes.size();
318        edge_num=g->edges.size();
319      }
320
321      ///Make a snapshot.
322
323      ///Make a snapshot of the graph.
324      ///
325      ///This function can be called more than once. In case of a repeated
326      ///call, the previous snapshot gets lost.
327      ///\param _g The graph we make the snapshot of.
328      void save(SmartGraph &_g)
329      {
330        g=&_g;
331        node_num=g->nodes.size();
332        edge_num=g->edges.size();
333      }
334
335      ///Undo the changes until a snapshot.
336     
337      ///Undo the changes until a snapshot created by save().
338      ///
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      {
[1770]347        g->restoreSnapshot(*this);
[1011]348      }
349    };
[973]350  };
[1034]351
352
353  /**************** Undirected List Graph ****************/
354
355  typedef ClearableUndirGraphExtender<
356    ExtendableUndirGraphExtender<
357    MappableUndirGraphExtender<
358    IterableUndirGraphExtender<
359    AlterableUndirGraphExtender<
[1669]360    UndirGraphExtender<SmartGraphBase> > > > > > ExtendedUndirSmartGraphBase;
[1034]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  ///
[1770]372  ///\todo Snapshot hasn't been implemented yet.
[1035]373  ///
[1669]374  class UndirSmartGraph : public ExtendedUndirSmartGraphBase {
[1034]375  };
376
[950]377 
[407]378  /// @} 
[921]379} //namespace lemon
[104]380
[157]381
[921]382#endif //LEMON_SMART_GRAPH_H
Note: See TracBrowser for help on using the repository browser.