COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/smart_graph.h @ 1768:1e2e0238e7c8

Last change on this file since 1768:1e2e0238e7c8 was 1768:1e2e0238e7c8, checked in by Balazs Dezso, 18 years ago

Removing findEdge

File size: 10.2 KB
Line 
1/* -*- C++ -*-
2 * lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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 */
16
17#ifndef LEMON_SMART_GRAPH_H
18#define LEMON_SMART_GRAPH_H
19
20///\ingroup graphs
21///\file
22///\brief SmartGraph and UndirSmartGraph classes.
23
24#include <vector>
25
26#include <lemon/invalid.h>
27
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>
33
34#include <lemon/bits/undir_graph_extender.h>
35
36#include <lemon/utility.h>
37
38namespace lemon {
39
40  class SmartGraph;
41  ///Base of SmartGraph
42
43  ///Base of SmartGraph
44  ///
45  class SmartGraphBase {
46
47    friend class SmatGraph;
48
49  protected:
50    struct NodeT
51    {
52      int first_in,first_out;     
53      NodeT() : first_in(-1), first_out(-1) {}
54    };
55    struct EdgeT
56    {
57      int target, source, next_in, next_out;     
58      //FIXME: is this necessary?
59      EdgeT() : next_in(-1), next_out(-1) {} 
60    };
61
62    std::vector<NodeT> nodes;
63
64    std::vector<EdgeT> edges;
65   
66   
67  public:
68
69    typedef SmartGraphBase Graph;
70
71    class Node;
72    class Edge;
73
74   
75  public:
76
77    SmartGraphBase() : nodes(), edges() { }
78    SmartGraphBase(const SmartGraphBase &_g)
79      : nodes(_g.nodes), edges(_g.edges) { }
80   
81    typedef True NodeNumTag;
82    typedef True EdgeNumTag;
83
84    ///Number of nodes.
85    int nodeNum() const { return nodes.size(); }
86    ///Number of edges.
87    int edgeNum() const { return edges.size(); }
88
89    /// Maximum node ID.
90   
91    /// Maximum node ID.
92    ///\sa id(Node)
93    int maxId(Node) const { return nodes.size()-1; }
94    /// Maximum edge ID.
95   
96    /// Maximum edge ID.
97    ///\sa id(Edge)
98    int maxId(Edge) const { return edges.size()-1; }
99
100    Node source(Edge e) const { return edges[e.n].source; }
101    Node target(Edge e) const { return edges[e.n].target; }
102
103    /// Node ID.
104   
105    /// The ID of a valid Node is a nonnegative integer not greater than
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).
108    ///
109    /// The ID of the \ref INVALID node is -1.
110    ///\return The ID of the node \c v.
111    static int id(Node v) { return v.n; }
112    /// Edge ID.
113   
114    /// The ID of a valid Edge is a nonnegative integer not greater than
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).
117    ///
118    /// The ID of the \ref INVALID edge is -1.
119    ///\return The ID of the edge \c e.
120    static int id(Edge e) { return e.n; }
121
122    static Node fromId(int id, Node) { return Node(id);}
123
124    static Edge fromId(int id, Edge) { return Edge(id);}
125
126    Node addNode() {
127      Node n; n.n=nodes.size();
128      nodes.push_back(NodeT()); //FIXME: Hmmm...
129      return n;
130    }
131   
132    Edge addEdge(Node u, Node v) {
133      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
134      edges[e.n].source=u.n; edges[e.n].target=v.n;
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;
138
139      return e;
140    }
141
142    void clear() {
143      edges.clear();
144      nodes.clear();
145    }
146
147
148    class Node {
149      friend class SmartGraphBase;
150      friend class SmartGraph;
151
152    protected:
153      int n;
154      ///\e
155
156      ///\todo It should be removed (or at least define a setToId() instead).
157      ///
158      Node(int nn) {n=nn;}
159    public:
160      Node() {}
161      Node (Invalid) { n=-1; }
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;}
165    };
166   
167
168    class Edge {
169      friend class SmartGraphBase;
170      friend class SmartGraph;
171
172    protected:
173      int n;
174      ///\todo It should be removed (or at least define a setToId() instead).
175      ///
176      Edge(int nn) {n=nn;}
177    public:
178      Edge() { }
179      Edge (Invalid) { n=-1; }
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;}
183    };
184
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    }
212   
213    void nextIn(Edge& edge) const {
214      edge.n = edges[edge.n].next_in;
215    }
216
217    Node _split(Node n, bool connect = true)
218    {
219      Node b = addNode();
220      nodes[b.n].first_out=nodes[n.n].first_out;
221      nodes[n.n].first_out=-1;
222      for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
223      if(connect) addEdge(n,b);
224      return b;
225    }
226
227  };
228
229  typedef ClearableGraphExtender<
230    ExtendableGraphExtender<
231    MappableGraphExtender<
232    IterableGraphExtender<
233    AlterableGraphExtender<SmartGraphBase> > > > > ExtendedSmartGraphBase;
234
235  /// \addtogroup graphs
236  /// @{
237
238  ///A smart graph class.
239
240  ///This is a simple and fast graph implementation.
241  ///It is also quite memory efficient, but at the price
242  ///that <b> it does support only limited (only stack-like)
243  ///node and edge deletions</b>.
244  ///It conforms to
245  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
246  ///\sa concept::ExtendableGraph.
247  ///
248  ///\author Alpar Juttner
249  class SmartGraph : public ExtendedSmartGraphBase {
250  public:
251   
252    class SnapShot;
253    friend class SnapShot;
254
255  protected:
256    void restoreSnapShot(const SnapShot &s)
257    {
258      while(s.edge_num<edges.size()) {
259        Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
260        nodes[edges.back().target].first_in=edges.back().next_in;
261        nodes[edges.back().source].first_out=edges.back().next_out;
262        edges.pop_back();
263      }
264      //nodes.resize(s.nodes_num);
265      while(s.node_num<nodes.size()) {
266        Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
267        nodes.pop_back();
268      }
269    }   
270
271  public:
272
273    ///Split a node.
274   
275    ///This function splits a node. First a new node is added to the graph,
276    ///then the source of each outgoing edge of \c n is moved to this new node.
277    ///If \c connect is \c true (this is the default value), then a new edge
278    ///from \c n to the newly created node is also added.
279    ///\return The newly created node.
280    ///
281    ///\note The <tt>Edge</tt>s
282    ///referencing a moved edge remain
283    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
284    ///may be invalidated.
285    ///\warning This functionality cannot be used together with the SnapShot
286    ///feature.
287    ///\todo It could be implemented in a bit faster way.
288    Node split(Node n, bool connect = true)
289    {
290      Node b = _split(n,connect);
291      return b;
292    }
293 
294
295    ///Class to make a snapshot of the graph and to restrore to it later.
296
297    ///Class to make a snapshot of the graph and to restrore to it later.
298    ///
299    ///The newly added nodes and edges can be removed using the
300    ///restore() function.
301    ///\note After you restore a state, you cannot restore
302    ///a later state, in other word you cannot add again the edges deleted
303    ///by restore() using another SnapShot instance.
304    ///
305    class SnapShot
306    {
307      SmartGraph *g;
308    protected:
309      friend class SmartGraph;
310      unsigned int node_num;
311      unsigned int edge_num;
312    public:
313      ///Default constructor.
314     
315      ///Default constructor.
316      ///To actually make a snapshot you must call save().
317      ///
318      SnapShot() : g(0) {}
319      ///Constructor that immediately makes a snapshot
320     
321      ///This constructor immediately makes a snapshot of the graph.
322      ///\param _g The graph we make a snapshot of.
323      SnapShot(SmartGraph &_g) :g(&_g) {
324        node_num=g->nodes.size();
325        edge_num=g->edges.size();
326      }
327
328      ///Make a snapshot.
329
330      ///Make a snapshot of the graph.
331      ///
332      ///This function can be called more than once. In case of a repeated
333      ///call, the previous snapshot gets lost.
334      ///\param _g The graph we make the snapshot of.
335      void save(SmartGraph &_g)
336      {
337        g=&_g;
338        node_num=g->nodes.size();
339        edge_num=g->edges.size();
340      }
341
342      ///Undo the changes until a snapshot.
343     
344      ///Undo the changes until a snapshot created by save().
345      ///
346      ///\note After you restored a state, you cannot restore
347      ///a later state, in other word you cannot add again the edges deleted
348      ///by restore().
349      ///
350      ///\todo This function might be called undo().
351     
352      void restore()
353      {
354        g->restoreSnapShot(*this);
355      }
356    };
357  };
358
359
360  /**************** Undirected List Graph ****************/
361
362  typedef ClearableUndirGraphExtender<
363    ExtendableUndirGraphExtender<
364    MappableUndirGraphExtender<
365    IterableUndirGraphExtender<
366    AlterableUndirGraphExtender<
367    UndirGraphExtender<SmartGraphBase> > > > > > ExtendedUndirSmartGraphBase;
368
369  ///A smart undirected graph class.
370
371  ///This is a simple and fast undirected graph implementation.
372  ///It is also quite memory efficient, but at the price
373  ///that <b> it does support only limited (only stack-like)
374  ///node and edge deletions</b>.
375  ///Except from this it conforms to
376  ///the \ref concept::UndirGraph "UndirGraph" concept.
377  ///\sa concept::UndirGraph.
378  ///
379  ///\todo SnapShot hasn't been implemented yet.
380  ///
381  class UndirSmartGraph : public ExtendedUndirSmartGraphBase {
382  };
383
384 
385  /// @} 
386} //namespace lemon
387
388
389#endif //LEMON_SMART_GRAPH_H
Note: See TracBrowser for help on using the repository browser.