2 * lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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 UndirSmartGraph classes.
26 #include <lemon/invalid.h>
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 #include <lemon/bits/graph_extender.h>
35 #include <lemon/utility.h>
44 class SmartGraphBase {
46 friend class SmatGraph;
51 int first_in,first_out;
52 NodeT() : first_in(-1), first_out(-1) {}
56 int target, source, next_in, next_out;
57 //FIXME: is this necessary?
58 EdgeT() : next_in(-1), next_out(-1) {}
61 std::vector<NodeT> nodes;
63 std::vector<EdgeT> edges;
68 typedef SmartGraphBase Graph;
76 SmartGraphBase() : nodes(), edges() { }
77 SmartGraphBase(const SmartGraphBase &_g)
78 : nodes(_g.nodes), edges(_g.edges) { }
80 typedef True NodeNumTag;
81 typedef True EdgeNumTag;
84 int nodeNum() const { return nodes.size(); }
86 int edgeNum() const { return edges.size(); }
92 int maxNodeId() const { return nodes.size()-1; }
97 int maxEdgeId() const { return edges.size()-1; }
99 Node source(Edge e) const { return edges[e.n].source; }
100 Node target(Edge e) const { return edges[e.n].target; }
104 /// The ID of a valid Node is a nonnegative integer not greater than
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().
108 /// The ID of the \ref INVALID node is -1.
109 ///\return The ID of the node \c v.
110 static int id(Node v) { return v.n; }
113 /// The ID of a valid Edge is a nonnegative integer not greater than
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().
117 /// The ID of the \ref INVALID edge is -1.
118 ///\return The ID of the edge \c e.
119 static int id(Edge e) { return e.n; }
121 static Node nodeFromId(int id) { return Node(id);}
123 static Edge edgeFromId(int id) { return Edge(id);}
126 Node n; n.n=nodes.size();
127 nodes.push_back(NodeT()); //FIXME: Hmmm...
131 Edge addEdge(Node u, Node v) {
132 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
133 edges[e.n].source=u.n; edges[e.n].target=v.n;
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;
148 friend class SmartGraphBase;
149 friend class SmartGraph;
156 Node (Invalid) { n=-1; }
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;}
164 friend class SmartGraphBase;
165 friend class SmartGraph;
172 Edge (Invalid) { n=-1; }
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;}
178 void first(Node& node) const {
179 node.n = nodes.size() - 1;
182 static void next(Node& node) {
186 void first(Edge& edge) const {
187 edge.n = edges.size() - 1;
190 static void next(Edge& edge) {
194 void firstOut(Edge& edge, const Node& node) const {
195 edge.n = nodes[node.n].first_out;
198 void nextOut(Edge& edge) const {
199 edge.n = edges[edge.n].next_out;
202 void firstIn(Edge& edge, const Node& node) const {
203 edge.n = nodes[node.n].first_in;
206 void nextIn(Edge& edge) const {
207 edge.n = edges[edge.n].next_in;
210 Node _split(Node n, bool connect = true)
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);
222 typedef ClearableGraphExtender<
223 ExtendableGraphExtender<
224 MappableGraphExtender<
225 IterableGraphExtender<
226 AlterableGraphExtender<
227 GraphExtender<SmartGraphBase> > > > > > ExtendedSmartGraphBase;
231 ///A smart graph class.
233 ///This is a simple and fast graph implementation.
234 ///It is also quite memory efficient, but at the price
235 ///that <b> it does support only limited (only stack-like)
236 ///node and edge deletions</b>.
238 ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
239 ///\sa concept::ExtendableGraph.
241 ///\author Alpar Juttner
242 class SmartGraph : public ExtendedSmartGraphBase {
246 friend class Snapshot;
249 void restoreSnapshot(const Snapshot &s)
251 while(s.edge_num<edges.size()) {
252 Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
253 nodes[edges.back().target].first_in=edges.back().next_in;
254 nodes[edges.back().source].first_out=edges.back().next_out;
257 //nodes.resize(s.nodes_num);
258 while(s.node_num<nodes.size()) {
259 Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
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.
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.
278 ///\warning This functionality cannot be used together with the Snapshot
280 ///\todo It could be implemented in a bit faster way.
281 Node split(Node n, bool connect = true)
283 Node b = _split(n,connect);
288 ///Class to make a snapshot of the graph and to restrore to it later.
290 ///Class to make a snapshot of the graph and to restrore to it later.
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
296 ///by restore() using another Snapshot instance.
302 friend class SmartGraph;
303 unsigned int node_num;
304 unsigned int edge_num;
306 ///Default constructor.
308 ///Default constructor.
309 ///To actually make a snapshot you must call save().
312 ///Constructor that immediately makes a snapshot
314 ///This constructor immediately makes a snapshot of the graph.
315 ///\param _g The graph we make a snapshot of.
316 Snapshot(SmartGraph &_g) :g(&_g) {
317 node_num=g->nodes.size();
318 edge_num=g->edges.size();
323 ///Make a snapshot of the graph.
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)
331 node_num=g->nodes.size();
332 edge_num=g->edges.size();
335 ///Undo the changes until a snapshot.
337 ///Undo the changes until a snapshot created 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
343 ///\todo This function might be called undo().
347 g->restoreSnapshot(*this);
353 /**************** Undirected List Graph ****************/
355 typedef ClearableUndirGraphExtender<
356 ExtendableUndirGraphExtender<
357 MappableUndirGraphExtender<
358 IterableUndirGraphExtender<
359 AlterableUndirGraphExtender<
360 UndirGraphExtender<SmartGraphBase> > > > > > ExtendedUndirSmartGraphBase;
362 ///A smart undirected graph class.
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.
372 ///\todo Snapshot hasn't been implemented yet.
374 class UndirSmartGraph : public ExtendedUndirSmartGraphBase {
382 #endif //LEMON_SMART_GRAPH_H