COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/smart_graph.h @ 1137:83a48cfd8553

Last change on this file since 1137:83a48cfd8553 was 1106:0a7d604a9763, checked in by Balazs Dezso, 19 years ago

Concept modification to resolve the item by its ID.

File size: 10.4 KB
Line 
1/* -*- C++ -*-
2 * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
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 */
16
17#ifndef LEMON_SMART_GRAPH_H
18#define LEMON_SMART_GRAPH_H
19
20///\ingroup graphs
21///\file
22///\brief SmartGraph and SymSmartGraph classes.
23
24#include <vector>
25
26#include <lemon/invalid.h>
27
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_notifier.h>
32#include <lemon/default_map.h>
33
34#include <lemon/undir_graph_extender.h>
35
36#include <lemon/utility.h>
37
38namespace lemon {
39
40  /// \addtogroup graphs
41  /// @{
42
43  class SmartGraph;
44  ///Base of SmartGraph
45
46  ///Base of SmartGraph
47  ///
48  class SmartGraphBase {
49
50    friend class SmatGraph;
51
52  protected:
53    struct NodeT
54    {
55      int first_in,first_out;     
56      NodeT() : first_in(-1), first_out(-1) {}
57    };
58    struct EdgeT
59    {
60      int target, source, next_in, next_out;     
61      //FIXME: is this necessary?
62      EdgeT() : next_in(-1), next_out(-1) {} 
63    };
64
65    std::vector<NodeT> nodes;
66
67    std::vector<EdgeT> edges;
68   
69   
70  public:
71
72    typedef SmartGraphBase Graph;
73
74    class Node;
75    class Edge;
76
77   
78  public:
79
80    SmartGraphBase() : nodes(), edges() { }
81    SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { }
82   
83    typedef True NodeNumTag;
84    typedef True EdgeNumTag;
85
86    ///Number of nodes.
87    int nodeNum() const { return nodes.size(); }
88    ///Number of edges.
89    int edgeNum() const { return edges.size(); }
90
91    /// Maximum node ID.
92   
93    /// Maximum node ID.
94    ///\sa id(Node)
95    int maxId(Node = INVALID) const { return nodes.size()-1; }
96    /// Maximum edge ID.
97   
98    /// Maximum edge ID.
99    ///\sa id(Edge)
100    int maxId(Edge = INVALID) const { return edges.size()-1; }
101
102    Node source(Edge e) const { return edges[e.n].source; }
103    Node target(Edge e) const { return edges[e.n].target; }
104
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.
113    static int id(Node v) { return v.n; }
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.
122    static int id(Edge e) { return e.n; }
123
124    static Node fromId(int id, Node) { return Node(id);}
125
126    static Edge fromId(int id, Edge) { return Edge(id);}
127
128    Node addNode() {
129      Node n; n.n=nodes.size();
130      nodes.push_back(NodeT()); //FIXME: Hmmm...
131      return n;
132    }
133   
134    Edge addEdge(Node u, Node v) {
135      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
136      edges[e.n].source=u.n; edges[e.n].target=v.n;
137      edges[e.n].next_out=nodes[u.n].first_out;
138      edges[e.n].next_in=nodes[v.n].first_in;
139      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
140
141      return e;
142    }
143
144    void clear() {
145      edges.clear();
146      nodes.clear();
147    }
148
149
150    class Node {
151      friend class SmartGraphBase;
152      friend class SmartGraph;
153
154    protected:
155      int n;
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    Edge _findEdge(Node u,Node v, Edge prev = INVALID)
218    {
219      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
220      while(e!=-1 && edges[e].target!=v.n) e = edges[e].next_out;
221      prev.n=e;
222      return prev;
223    }
224
225  };
226
227  typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
228  typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
229  typedef DefaultMappableGraphExtender<IterableSmartGraphBase> MappableSmartGraphBase;
230  typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
231  typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
232
233  ///A smart graph class.
234
235  ///This is a simple and fast graph implementation.
236  ///It is also quite memory efficient, but at the price
237  ///that <b> it does support only limited (only stack-like)
238  ///node and edge deletions</b>.
239  ///It conforms to
240  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
241  ///\sa concept::ExtendableGraph.
242  ///
243  ///\author Alpar Juttner
244  class SmartGraph : public ClearableSmartGraphBase {
245  public:
246    /// Finds an edge between two nodes.
247   
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    }
266   
267    class SnapShot;
268    friend class SnapShot;
269
270  protected:
271    void restoreSnapShot(const SnapShot &s)
272    {
273      while(s.edge_num>edges.size()) {
274        Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
275        nodes[edges.back().target].first_in=edges.back().next_in;
276        nodes[edges.back().source].first_out=edges.back().next_out;
277        edges.pop_back();
278      }
279      //nodes.resize(s.nodes_num);
280      while(s.node_num>nodes.size()) {
281        Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
282        nodes.pop_back();
283      }
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:
305      ///Default constructur.
306     
307      ///Default constructur.
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    };
350  };
351
352
353  /**************** Undirected List Graph ****************/
354
355  typedef ClearableUndirGraphExtender<
356    ExtendableUndirGraphExtender<
357    MappableUndirGraphExtender<
358    IterableUndirGraphExtender<
359    AlterableUndirGraphExtender<
360    UndirGraphExtender<SmartGraphBase> > > > > > UndirSmartGraphBase;
361
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  ///
374  class UndirSmartGraph : public UndirSmartGraphBase {
375  };
376
377 
378  /// @} 
379} //namespace lemon
380
381
382#endif //LEMON_SMART_GRAPH_H
Note: See TracBrowser for help on using the repository browser.