2 * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
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.
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
17 #ifndef LEMON_SMART_GRAPH_H
18 #define LEMON_SMART_GRAPH_H
22 ///\brief SmartGraph and SymSmartGraph classes.
26 #include <lemon/invalid.h>
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>
34 #include <lemon/undir_graph_extender.h>
36 #include <lemon/utility.h>
40 /// \addtogroup graphs
48 class SmartGraphBase {
50 friend class SmatGraph;
55 int first_in,first_out;
56 NodeT() : first_in(-1), first_out(-1) {}
60 int target, source, next_in, next_out;
61 //FIXME: is this necessary?
62 EdgeT() : next_in(-1), next_out(-1) {}
65 std::vector<NodeT> nodes;
67 std::vector<EdgeT> edges;
72 typedef SmartGraphBase Graph;
80 SmartGraphBase() : nodes(), edges() { }
81 SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { }
83 typedef True NodeNumTag;
84 typedef True EdgeNumTag;
87 int nodeNum() const { return nodes.size(); }
89 int edgeNum() const { return edges.size(); }
95 int maxId(Node = INVALID) const { return nodes.size()-1; }
100 int maxId(Edge = INVALID) const { return edges.size()-1; }
102 Node source(Edge e) const { return edges[e.n].source; }
103 Node target(Edge e) const { return edges[e.n].target; }
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().
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; }
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().
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; }
125 Node n; n.n=nodes.size();
126 nodes.push_back(NodeT()); //FIXME: Hmmm...
130 Edge addEdge(Node u, Node v) {
131 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
132 edges[e.n].source=u.n; edges[e.n].target=v.n;
133 edges[e.n].next_out=nodes[u.n].first_out;
134 edges[e.n].next_in=nodes[v.n].first_in;
135 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
147 friend class SmartGraphBase;
148 friend class SmartGraph;
152 ///\todo It should be removed (or at least define a setToId() instead).
157 Node (Invalid) { n=-1; }
158 bool operator==(const Node i) const {return n==i.n;}
159 bool operator!=(const Node i) const {return n!=i.n;}
160 bool operator<(const Node i) const {return n<i.n;}
165 friend class SmartGraphBase;
166 friend class SmartGraph;
170 ///\todo It should be removed (or at least define a setToId() instead).
175 Edge (Invalid) { n=-1; }
176 bool operator==(const Edge i) const {return n==i.n;}
177 bool operator!=(const Edge i) const {return n!=i.n;}
178 bool operator<(const Edge i) const {return n<i.n;}
181 void first(Node& node) const {
182 node.n = nodes.size() - 1;
185 static void next(Node& node) {
189 void first(Edge& edge) const {
190 edge.n = edges.size() - 1;
193 static void next(Edge& edge) {
197 void firstOut(Edge& edge, const Node& node) const {
198 edge.n = nodes[node.n].first_out;
201 void nextOut(Edge& edge) const {
202 edge.n = edges[edge.n].next_out;
205 void firstIn(Edge& edge, const Node& node) const {
206 edge.n = nodes[node.n].first_in;
209 void nextIn(Edge& edge) const {
210 edge.n = edges[edge.n].next_in;
213 Edge _findEdge(Node u,Node v, Edge prev = INVALID)
215 int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
216 while(e!=-1 && edges[e].source!=v.n) e = edges[e].next_out;
223 typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
224 typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
225 typedef DefaultMappableGraphExtender<IterableSmartGraphBase> MappableSmartGraphBase;
226 typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
227 typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
229 ///A smart graph class.
231 ///This is a simple and fast graph implementation.
232 ///It is also quite memory efficient, but at the price
233 ///that <b> it does support only limited (only stack-like)
234 ///node and edge deletions</b>.
236 ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
237 ///\sa concept::ExtendableGraph.
239 ///\author Alpar Juttner
240 class SmartGraph : public ClearableSmartGraphBase {
242 /// Finds an edge between two nodes.
244 /// Finds an edge from node \c u to node \c v.
246 /// If \c prev is \ref INVALID (this is the default value), then
247 /// it finds the first edge from \c u to \c v. Otherwise it looks for
248 /// the next edge from \c u to \c v after \c prev.
249 /// \return The found edge or \ref INVALID if there is no such an edge.
251 /// Thus you can iterate through each edge from \c u to \c v as it follows.
253 /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
257 /// \todo Possibly it should be a global function.
258 Edge findEdge(Node u,Node v, Edge prev = INVALID)
260 return _findEdge(u,v,prev);
264 friend class SnapShot;
267 void restoreSnapShot(const SnapShot &s)
269 while(s.edge_num>edges.size()) {
270 Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
271 nodes[edges.back().target].first_in=edges.back().next_in;
272 nodes[edges.back().source].first_out=edges.back().next_out;
275 //nodes.resize(s.nodes_num);
276 while(s.node_num>nodes.size()) {
277 Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
283 ///Class to make a snapshot of the graph and to restrore to it later.
285 ///Class to make a snapshot of the graph and to restrore to it later.
287 ///The newly added nodes and edges can be removed using the
288 ///restore() function.
289 ///\note After you restore a state, you cannot restore
290 ///a later state, in other word you cannot add again the edges deleted
291 ///by restore() using another SnapShot instance.
298 friend class SmartGraph;
299 unsigned int node_num;
300 unsigned int edge_num;
302 ///Default constructur.
304 ///Default constructur.
305 ///To actually make a snapshot you must call save().
308 ///Constructor that immediately makes a snapshot
310 ///This constructor immediately makes a snapshot of the graph.
311 ///\param _g The graph we make a snapshot of.
312 SnapShot(SmartGraph &_g) :g(&_g) {
313 node_num=g->nodes.size();
314 edge_num=g->edges.size();
319 ///Make a snapshot of the graph.
321 ///This function can be called more than once. In case of a repeated
322 ///call, the previous snapshot gets lost.
323 ///\param _g The graph we make the snapshot of.
324 void save(SmartGraph &_g)
327 node_num=g->nodes.size();
328 edge_num=g->edges.size();
331 ///Undo the changes until a snapshot.
333 ///Undo the changes until a snapshot created by save().
335 ///\param s an internal stucture given back by save()
336 ///\note After you restored a state, you cannot restore
337 ///a later state, in other word you cannot add again the edges deleted
340 ///\todo This function might be called undo().
344 g->restoreSnapShot(*this);
350 /**************** Undirected List Graph ****************/
352 typedef ClearableUndirGraphExtender<
353 ExtendableUndirGraphExtender<
354 MappableUndirGraphExtender<
355 IterableUndirGraphExtender<
356 AlterableUndirGraphExtender<
357 UndirGraphExtender<SmartGraphBase> > > > > > UndirSmartGraphBase;
359 ///A smart undirected graph class.
361 ///This is a simple and fast undirected graph implementation.
362 ///It is also quite memory efficient, but at the price
363 ///that <b> it does support only limited (only stack-like)
364 ///node and edge deletions</b>.
365 ///Except from this it conforms to
366 ///the \ref concept::UndirGraph "UndirGraph" concept.
367 ///\sa concept::UndirGraph.
369 ///\todo SnapShot hasn't been implemented yet.
371 class UndirSmartGraph : public UndirSmartGraphBase {
379 #endif //LEMON_SMART_GRAPH_H