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_observer_registry.h> |
---|
32 | #include <lemon/default_map.h> |
---|
33 | |
---|
34 | #include <lemon/utility.h> |
---|
35 | |
---|
36 | namespace lemon { |
---|
37 | |
---|
38 | /// \addtogroup graphs |
---|
39 | /// @{ |
---|
40 | |
---|
41 | class SmartGraph; |
---|
42 | ///Base of SmartGraph |
---|
43 | |
---|
44 | ///Base of SmartGraph |
---|
45 | /// |
---|
46 | class SmartGraphBase { |
---|
47 | |
---|
48 | friend class SmatGraph; |
---|
49 | |
---|
50 | protected: |
---|
51 | struct NodeT |
---|
52 | { |
---|
53 | int first_in,first_out; |
---|
54 | NodeT() : first_in(-1), first_out(-1) {} |
---|
55 | }; |
---|
56 | struct EdgeT |
---|
57 | { |
---|
58 | int head, tail, next_in, next_out; |
---|
59 | //FIXME: is this necessary? |
---|
60 | EdgeT() : next_in(-1), next_out(-1) {} |
---|
61 | }; |
---|
62 | |
---|
63 | std::vector<NodeT> nodes; |
---|
64 | |
---|
65 | std::vector<EdgeT> edges; |
---|
66 | |
---|
67 | |
---|
68 | public: |
---|
69 | |
---|
70 | typedef SmartGraphBase Graph; |
---|
71 | |
---|
72 | class Node; |
---|
73 | class Edge; |
---|
74 | |
---|
75 | |
---|
76 | public: |
---|
77 | |
---|
78 | SmartGraphBase() : nodes(), edges() { } |
---|
79 | SmartGraphBase(const SmartGraphBase &_g) : 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 = INVALID) const { return nodes.size()-1; } |
---|
94 | /// Maximum edge ID. |
---|
95 | |
---|
96 | /// Maximum edge ID. |
---|
97 | ///\sa id(Edge) |
---|
98 | int maxId(Edge = INVALID) const { return edges.size()-1; } |
---|
99 | |
---|
100 | Node tail(Edge e) const { return edges[e.n].tail; } |
---|
101 | Node head(Edge e) const { return edges[e.n].head; } |
---|
102 | |
---|
103 | /// Node ID. |
---|
104 | |
---|
105 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
106 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
107 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
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 maxEdgeId(). The range of the ID's is not surely continuous |
---|
116 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
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 | Node addNode() { |
---|
123 | Node n; n.n=nodes.size(); |
---|
124 | nodes.push_back(NodeT()); //FIXME: Hmmm... |
---|
125 | return n; |
---|
126 | } |
---|
127 | |
---|
128 | Edge addEdge(Node u, Node v) { |
---|
129 | Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... |
---|
130 | edges[e.n].tail=u.n; edges[e.n].head=v.n; |
---|
131 | edges[e.n].next_out=nodes[u.n].first_out; |
---|
132 | edges[e.n].next_in=nodes[v.n].first_in; |
---|
133 | nodes[u.n].first_out=nodes[v.n].first_in=e.n; |
---|
134 | |
---|
135 | return e; |
---|
136 | } |
---|
137 | |
---|
138 | void clear() { |
---|
139 | edges.clear(); |
---|
140 | nodes.clear(); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | class Node { |
---|
145 | friend class SmartGraphBase; |
---|
146 | friend class SmartGraph; |
---|
147 | |
---|
148 | protected: |
---|
149 | int n; |
---|
150 | ///\todo It should be removed (or at least define a setToId() instead). |
---|
151 | /// |
---|
152 | Node(int nn) {n=nn;} |
---|
153 | public: |
---|
154 | Node() {} |
---|
155 | Node (Invalid) { n=-1; } |
---|
156 | bool operator==(const Node i) const {return n==i.n;} |
---|
157 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
158 | bool operator<(const Node i) const {return n<i.n;} |
---|
159 | }; |
---|
160 | |
---|
161 | |
---|
162 | class Edge { |
---|
163 | friend class SmartGraphBase; |
---|
164 | friend class SmartGraph; |
---|
165 | |
---|
166 | protected: |
---|
167 | int n; |
---|
168 | ///\todo It should be removed (or at least define a setToId() instead). |
---|
169 | /// |
---|
170 | Edge(int nn) {n=nn;} |
---|
171 | public: |
---|
172 | Edge() { } |
---|
173 | Edge (Invalid) { n=-1; } |
---|
174 | bool operator==(const Edge i) const {return n==i.n;} |
---|
175 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
176 | bool operator<(const Edge i) const {return n<i.n;} |
---|
177 | }; |
---|
178 | |
---|
179 | void first(Node& node) const { |
---|
180 | node.n = nodes.size() - 1; |
---|
181 | } |
---|
182 | |
---|
183 | static void next(Node& node) { |
---|
184 | --node.n; |
---|
185 | } |
---|
186 | |
---|
187 | void first(Edge& edge) const { |
---|
188 | edge.n = edges.size() - 1; |
---|
189 | } |
---|
190 | |
---|
191 | static void next(Edge& edge) { |
---|
192 | --edge.n; |
---|
193 | } |
---|
194 | |
---|
195 | void firstOut(Edge& edge, const Node& node) const { |
---|
196 | edge.n = nodes[node.n].first_out; |
---|
197 | } |
---|
198 | |
---|
199 | void nextOut(Edge& edge) const { |
---|
200 | edge.n = edges[edge.n].next_out; |
---|
201 | } |
---|
202 | |
---|
203 | void firstIn(Edge& edge, const Node& node) const { |
---|
204 | edge.n = nodes[node.n].first_in; |
---|
205 | } |
---|
206 | |
---|
207 | void nextIn(Edge& edge) const { |
---|
208 | edge.n = edges[edge.n].next_in; |
---|
209 | } |
---|
210 | |
---|
211 | Edge _findEdge(Node u,Node v, Edge prev = INVALID) |
---|
212 | { |
---|
213 | int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; |
---|
214 | while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out; |
---|
215 | prev.n=e; |
---|
216 | return prev; |
---|
217 | } |
---|
218 | |
---|
219 | }; |
---|
220 | |
---|
221 | typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase; |
---|
222 | typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase; |
---|
223 | typedef DefaultMappableGraphExtender<IterableSmartGraphBase> MappableSmartGraphBase; |
---|
224 | typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase; |
---|
225 | typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase; |
---|
226 | |
---|
227 | ///A smart graph class. |
---|
228 | |
---|
229 | ///This is a simple and fast graph implementation. |
---|
230 | ///It is also quite memory efficient, but at the price |
---|
231 | ///that <b> it does support only limited (only stack-like) |
---|
232 | ///node and edge deletions</b>. |
---|
233 | ///It conforms to |
---|
234 | ///the \ref concept::ExtendableGraph "ExtendableGraph" concept. |
---|
235 | ///\sa concept::ExtendableGraph. |
---|
236 | /// |
---|
237 | ///\todo Some member functions could be \c static. |
---|
238 | /// |
---|
239 | ///\author Alpar Juttner |
---|
240 | class SmartGraph : public ClearableSmartGraphBase { |
---|
241 | public: |
---|
242 | /// Finds an edge between two nodes. |
---|
243 | |
---|
244 | /// Finds an edge from node \c u to node \c v. |
---|
245 | /// |
---|
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. |
---|
250 | /// |
---|
251 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
252 | /// \code |
---|
253 | /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) { |
---|
254 | /// ... |
---|
255 | /// } |
---|
256 | /// \endcode |
---|
257 | /// \todo Possibly it should be a global function. |
---|
258 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
259 | { |
---|
260 | return _findEdge(u,v,prev); |
---|
261 | } |
---|
262 | |
---|
263 | ///Internal data structure to store snapshots |
---|
264 | |
---|
265 | ///\ingroup graphs |
---|
266 | ///\sa makeSnapShot() |
---|
267 | ///\sa rollBack() |
---|
268 | struct SnapShot |
---|
269 | { |
---|
270 | unsigned int node_num; |
---|
271 | unsigned int edge_num; |
---|
272 | }; |
---|
273 | |
---|
274 | ///Make a snapshot of the graph. |
---|
275 | |
---|
276 | ///Make a snapshot of the graph. |
---|
277 | /// |
---|
278 | ///The newly added nodes and edges can be removed using the |
---|
279 | ///rollBack() function. |
---|
280 | /// |
---|
281 | ///\return An stucture SnapShot describing the pesent state of the |
---|
282 | ///graph. |
---|
283 | ///\note After you rolled back to a state, you cannot roll "back" to |
---|
284 | ///a later state, in other word you cannot add again the edges deleted |
---|
285 | ///by rollBack(). |
---|
286 | ///\todo This function might be called saveState() or getState(). |
---|
287 | SnapShot makeSnapShot() |
---|
288 | { |
---|
289 | SnapShot s; |
---|
290 | s.node_num=nodes.size(); |
---|
291 | s.edge_num=edges.size(); |
---|
292 | return s; |
---|
293 | } |
---|
294 | |
---|
295 | ///Undo the changes until a snapshot. |
---|
296 | |
---|
297 | ///Undo the changes until a snapshot created by makeSnapShot(). |
---|
298 | /// |
---|
299 | ///\param s an internal stucture given back by makeSnapShot() |
---|
300 | ///\note After you rolled back to a state, you cannot "roll forward" to |
---|
301 | ///a later state, in other word you cannot add again the edges deleted |
---|
302 | ///by rollBack(). |
---|
303 | /// |
---|
304 | ///\todo This function might be called undo(). |
---|
305 | |
---|
306 | void rollBack(const SnapShot &s) |
---|
307 | { |
---|
308 | while(s.edge_num>edges.size()) { |
---|
309 | edge_observers.erase(Edge(edges.size()-1)); |
---|
310 | nodes[edges.back().head].first_in=edges.back().next_in; |
---|
311 | nodes[edges.back().tail].first_out=edges.back().next_out; |
---|
312 | edges.pop_back(); |
---|
313 | } |
---|
314 | //nodes.resize(s.nodes_num); |
---|
315 | while(s.node_num>nodes.size()) { |
---|
316 | node_observers.erase(Node(nodes.size()-1)); |
---|
317 | nodes.pop_back(); |
---|
318 | } |
---|
319 | } |
---|
320 | }; |
---|
321 | |
---|
322 | /// @} |
---|
323 | } //namespace lemon |
---|
324 | |
---|
325 | |
---|
326 | #endif //LEMON_SMART_GRAPH_H |
---|