3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_SMART_GRAPH_H
20 #define LEMON_SMART_GRAPH_H
24 ///\brief SmartGraph class.
28 #include <lemon/bits/invalid.h>
30 #include <lemon/bits/graph_extender.h>
32 #include <lemon/bits/utility.h>
34 #include <lemon/bits/graph_extender.h>
43 class SmartGraphBase {
45 friend class SmatGraph;
50 int first_in,first_out;
51 NodeT() : first_in(-1), first_out(-1) {}
55 int target, source, next_in, next_out;
56 //FIXME: is this necessary?
57 EdgeT() : next_in(-1), next_out(-1) {}
60 std::vector<NodeT> nodes;
62 std::vector<EdgeT> edges;
67 typedef SmartGraphBase Graph;
75 SmartGraphBase() : nodes(), edges() { }
76 SmartGraphBase(const SmartGraphBase &_g)
77 : nodes(_g.nodes), edges(_g.edges) { }
79 typedef True NodeNumTag;
80 typedef True EdgeNumTag;
83 int nodeNum() const { return nodes.size(); }
85 int edgeNum() const { return edges.size(); }
91 int maxNodeId() const { return nodes.size()-1; }
96 int maxEdgeId() const { return edges.size()-1; }
98 Node source(Edge e) const { return edges[e.n].source; }
99 Node target(Edge e) const { return edges[e.n].target; }
103 /// The ID of a valid Node is a nonnegative integer not greater than
104 /// \ref maxNodeId(). The range of the ID's is not surely continuous
105 /// and the greatest node ID can be actually less then \ref maxNodeId().
107 /// The ID of the \ref INVALID node is -1.
108 ///\return The ID of the node \c v.
109 static int id(Node v) { return v.n; }
112 /// The ID of a valid Edge is a nonnegative integer not greater than
113 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
114 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
116 /// The ID of the \ref INVALID edge is -1.
117 ///\return The ID of the edge \c e.
118 static int id(Edge e) { return e.n; }
120 /// \brief Returns the node from its \c id.
122 /// Returns the node from its \c id. If there is not node
123 /// with the given id the effect of the function is undefinied.
124 static Node nodeFromId(int id) { return Node(id);}
126 /// \brief Returns the edge from its \c id.
128 /// Returns the edge from its \c id. If there is not edge
129 /// with the given id the effect of the function is undefinied.
130 static Edge edgeFromId(int id) { return Edge(id);}
133 Node n; n.n=nodes.size();
134 nodes.push_back(NodeT()); //FIXME: Hmmm...
138 Edge addEdge(Node u, Node v) {
139 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
140 edges[e.n].source=u.n; edges[e.n].target=v.n;
141 edges[e.n].next_out=nodes[u.n].first_out;
142 edges[e.n].next_in=nodes[v.n].first_in;
143 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
155 friend class SmartGraphBase;
156 friend class SmartGraph;
163 Node (Invalid) { n=-1; }
164 bool operator==(const Node i) const {return n==i.n;}
165 bool operator!=(const Node i) const {return n!=i.n;}
166 bool operator<(const Node i) const {return n<i.n;}
171 friend class SmartGraphBase;
172 friend class SmartGraph;
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;}
185 void first(Node& node) const {
186 node.n = nodes.size() - 1;
189 static void next(Node& node) {
193 void first(Edge& edge) const {
194 edge.n = edges.size() - 1;
197 static void next(Edge& edge) {
201 void firstOut(Edge& edge, const Node& node) const {
202 edge.n = nodes[node.n].first_out;
205 void nextOut(Edge& edge) const {
206 edge.n = edges[edge.n].next_out;
209 void firstIn(Edge& edge, const Node& node) const {
210 edge.n = nodes[node.n].first_in;
213 void nextIn(Edge& edge) const {
214 edge.n = edges[edge.n].next_in;
217 Node _split(Node n, bool connect = true)
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);
229 typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
233 ///A smart graph class.
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>.
240 ///the \ref concept::Graph "Graph" concept.
241 ///\sa concept::Graph.
243 ///\author Alpar Juttner
244 class SmartGraph : public ExtendedSmartGraphBase {
247 typedef ExtendedSmartGraphBase Parent;
250 friend class Snapshot;
253 void restoreSnapshot(const Snapshot &s)
255 while(s.edge_num<edges.size()) {
256 Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
257 nodes[edges.back().target].first_in=edges.back().next_in;
258 nodes[edges.back().source].first_out=edges.back().next_out;
261 //nodes.resize(s.nodes_num);
262 while(s.node_num<nodes.size()) {
263 Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
272 ///This function splits a node. First a new node is added to the graph,
273 ///then the source of each outgoing edge of \c n is moved to this new node.
274 ///If \c connect is \c true (this is the default value), then a new edge
275 ///from \c n to the newly created node is also added.
276 ///\return The newly created node.
278 ///\note The <tt>Edge</tt>s
279 ///referencing a moved edge remain
280 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
281 ///may be invalidated.
282 ///\warning This functionality cannot be used together with the Snapshot
284 ///\todo It could be implemented in a bit faster way.
285 Node split(Node n, bool connect = true)
287 Node b = _split(n,connect);
292 ///Class to make a snapshot of the graph and to restrore to it later.
294 ///Class to make a snapshot of the graph and to restrore to it later.
296 ///The newly added nodes and edges can be removed using the
297 ///restore() function.
298 ///\note After you restore a state, you cannot restore
299 ///a later state, in other word you cannot add again the edges deleted
300 ///by restore() using another Snapshot instance.
306 friend class SmartGraph;
307 unsigned int node_num;
308 unsigned int edge_num;
310 ///Default constructor.
312 ///Default constructor.
313 ///To actually make a snapshot you must call save().
316 ///Constructor that immediately makes a snapshot
318 ///This constructor immediately makes a snapshot of the graph.
319 ///\param _g The graph we make a snapshot of.
320 Snapshot(SmartGraph &_g) :g(&_g) {
321 node_num=g->nodes.size();
322 edge_num=g->edges.size();
327 ///Make a snapshot of the graph.
329 ///This function can be called more than once. In case of a repeated
330 ///call, the previous snapshot gets lost.
331 ///\param _g The graph we make the snapshot of.
332 void save(SmartGraph &_g)
335 node_num=g->nodes.size();
336 edge_num=g->edges.size();
339 ///Undo the changes until a snapshot.
341 ///Undo the changes until a snapshot created by save().
343 ///\note After you restored a state, you cannot restore
344 ///a later state, in other word you cannot add again the edges deleted
347 ///\todo This function might be called undo().
351 g->restoreSnapshot(*this);
360 #endif //LEMON_SMART_GRAPH_H