alpar@906
|
1 |
/* -*- C++ -*-
|
ladanyi@1435
|
2 |
* lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
5 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@105
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_SMART_GRAPH_H
|
alpar@921
|
18 |
#define LEMON_SMART_GRAPH_H
|
alpar@104
|
19 |
|
klao@491
|
20 |
///\ingroup graphs
|
alpar@242
|
21 |
///\file
|
deba@1692
|
22 |
///\brief SmartGraph and UndirSmartGraph classes.
|
alpar@242
|
23 |
|
alpar@104
|
24 |
#include <vector>
|
alpar@104
|
25 |
|
alpar@921
|
26 |
#include <lemon/invalid.h>
|
alpar@157
|
27 |
|
deba@1307
|
28 |
#include <lemon/bits/clearable_graph_extender.h>
|
deba@1307
|
29 |
#include <lemon/bits/extendable_graph_extender.h>
|
deba@1307
|
30 |
#include <lemon/bits/iterable_graph_extender.h>
|
deba@1307
|
31 |
#include <lemon/bits/alteration_notifier.h>
|
deba@1307
|
32 |
#include <lemon/bits/default_map.h>
|
klao@946
|
33 |
|
deba@1307
|
34 |
#include <lemon/bits/undir_graph_extender.h>
|
klao@1034
|
35 |
|
klao@977
|
36 |
#include <lemon/utility.h>
|
deba@782
|
37 |
|
alpar@921
|
38 |
namespace lemon {
|
alpar@104
|
39 |
|
alpar@973
|
40 |
class SmartGraph;
|
alpar@969
|
41 |
///Base of SmartGraph
|
alpar@969
|
42 |
|
alpar@969
|
43 |
///Base of SmartGraph
|
alpar@969
|
44 |
///
|
klao@946
|
45 |
class SmartGraphBase {
|
alpar@104
|
46 |
|
alpar@973
|
47 |
friend class SmatGraph;
|
alpar@973
|
48 |
|
alpar@973
|
49 |
protected:
|
alpar@104
|
50 |
struct NodeT
|
alpar@104
|
51 |
{
|
alpar@104
|
52 |
int first_in,first_out;
|
alpar@157
|
53 |
NodeT() : first_in(-1), first_out(-1) {}
|
alpar@104
|
54 |
};
|
alpar@104
|
55 |
struct EdgeT
|
alpar@104
|
56 |
{
|
alpar@986
|
57 |
int target, source, next_in, next_out;
|
alpar@104
|
58 |
//FIXME: is this necessary?
|
alpar@157
|
59 |
EdgeT() : next_in(-1), next_out(-1) {}
|
alpar@104
|
60 |
};
|
alpar@104
|
61 |
|
alpar@104
|
62 |
std::vector<NodeT> nodes;
|
alpar@129
|
63 |
|
alpar@104
|
64 |
std::vector<EdgeT> edges;
|
alpar@104
|
65 |
|
alpar@185
|
66 |
|
alpar@104
|
67 |
public:
|
deba@782
|
68 |
|
klao@946
|
69 |
typedef SmartGraphBase Graph;
|
alpar@104
|
70 |
|
alpar@164
|
71 |
class Node;
|
alpar@164
|
72 |
class Edge;
|
alpar@108
|
73 |
|
alpar@104
|
74 |
|
alpar@104
|
75 |
public:
|
alpar@104
|
76 |
|
klao@946
|
77 |
SmartGraphBase() : nodes(), edges() { }
|
deba@1718
|
78 |
SmartGraphBase(const SmartGraphBase &_g)
|
deba@1718
|
79 |
: nodes(_g.nodes), edges(_g.edges) { }
|
alpar@104
|
80 |
|
klao@977
|
81 |
typedef True NodeNumTag;
|
klao@977
|
82 |
typedef True EdgeNumTag;
|
klao@977
|
83 |
|
alpar@813
|
84 |
///Number of nodes.
|
alpar@813
|
85 |
int nodeNum() const { return nodes.size(); }
|
alpar@813
|
86 |
///Number of edges.
|
alpar@813
|
87 |
int edgeNum() const { return edges.size(); }
|
alpar@104
|
88 |
|
alpar@813
|
89 |
/// Maximum node ID.
|
alpar@813
|
90 |
|
alpar@813
|
91 |
/// Maximum node ID.
|
alpar@813
|
92 |
///\sa id(Node)
|
alpar@1631
|
93 |
int maxId(Node) const { return nodes.size()-1; }
|
alpar@813
|
94 |
/// Maximum edge ID.
|
alpar@813
|
95 |
|
alpar@813
|
96 |
/// Maximum edge ID.
|
alpar@813
|
97 |
///\sa id(Edge)
|
alpar@1631
|
98 |
int maxId(Edge) const { return edges.size()-1; }
|
alpar@108
|
99 |
|
alpar@986
|
100 |
Node source(Edge e) const { return edges[e.n].source; }
|
alpar@986
|
101 |
Node target(Edge e) const { return edges[e.n].target; }
|
alpar@104
|
102 |
|
alpar@813
|
103 |
/// Node ID.
|
alpar@813
|
104 |
|
alpar@813
|
105 |
/// The ID of a valid Node is a nonnegative integer not greater than
|
alpar@1631
|
106 |
/// \ref maxId(Node). The range of the ID's is not surely continuous
|
alpar@1631
|
107 |
/// and the greatest node ID can be actually less then \ref maxId(Node).
|
alpar@813
|
108 |
///
|
alpar@813
|
109 |
/// The ID of the \ref INVALID node is -1.
|
alpar@813
|
110 |
///\return The ID of the node \c v.
|
alpar@713
|
111 |
static int id(Node v) { return v.n; }
|
alpar@813
|
112 |
/// Edge ID.
|
alpar@813
|
113 |
|
alpar@813
|
114 |
/// The ID of a valid Edge is a nonnegative integer not greater than
|
alpar@1631
|
115 |
/// \ref maxId(Edge). The range of the ID's is not surely continuous
|
alpar@1631
|
116 |
/// and the greatest edge ID can be actually less then \ref maxId(Edge).
|
alpar@813
|
117 |
///
|
alpar@813
|
118 |
/// The ID of the \ref INVALID edge is -1.
|
alpar@813
|
119 |
///\return The ID of the edge \c e.
|
alpar@713
|
120 |
static int id(Edge e) { return e.n; }
|
alpar@104
|
121 |
|
deba@1106
|
122 |
static Node fromId(int id, Node) { return Node(id);}
|
deba@1106
|
123 |
|
deba@1106
|
124 |
static Edge fromId(int id, Edge) { return Edge(id);}
|
deba@1106
|
125 |
|
alpar@164
|
126 |
Node addNode() {
|
alpar@164
|
127 |
Node n; n.n=nodes.size();
|
alpar@104
|
128 |
nodes.push_back(NodeT()); //FIXME: Hmmm...
|
alpar@104
|
129 |
return n;
|
alpar@104
|
130 |
}
|
alpar@108
|
131 |
|
alpar@164
|
132 |
Edge addEdge(Node u, Node v) {
|
alpar@164
|
133 |
Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
|
alpar@986
|
134 |
edges[e.n].source=u.n; edges[e.n].target=v.n;
|
alpar@104
|
135 |
edges[e.n].next_out=nodes[u.n].first_out;
|
alpar@104
|
136 |
edges[e.n].next_in=nodes[v.n].first_in;
|
alpar@104
|
137 |
nodes[u.n].first_out=nodes[v.n].first_in=e.n;
|
alpar@108
|
138 |
|
alpar@104
|
139 |
return e;
|
alpar@104
|
140 |
}
|
alpar@104
|
141 |
|
deba@782
|
142 |
void clear() {
|
deba@782
|
143 |
edges.clear();
|
deba@782
|
144 |
nodes.clear();
|
deba@782
|
145 |
}
|
alpar@104
|
146 |
|
klao@946
|
147 |
|
alpar@164
|
148 |
class Node {
|
klao@946
|
149 |
friend class SmartGraphBase;
|
alpar@973
|
150 |
friend class SmartGraph;
|
alpar@104
|
151 |
|
alpar@104
|
152 |
protected:
|
alpar@104
|
153 |
int n;
|
alpar@1641
|
154 |
///\e
|
alpar@1641
|
155 |
|
alpar@973
|
156 |
///\todo It should be removed (or at least define a setToId() instead).
|
alpar@973
|
157 |
///
|
alpar@164
|
158 |
Node(int nn) {n=nn;}
|
alpar@104
|
159 |
public:
|
alpar@164
|
160 |
Node() {}
|
alpar@503
|
161 |
Node (Invalid) { n=-1; }
|
alpar@164
|
162 |
bool operator==(const Node i) const {return n==i.n;}
|
alpar@164
|
163 |
bool operator!=(const Node i) const {return n!=i.n;}
|
alpar@164
|
164 |
bool operator<(const Node i) const {return n<i.n;}
|
alpar@104
|
165 |
};
|
alpar@104
|
166 |
|
alpar@104
|
167 |
|
alpar@164
|
168 |
class Edge {
|
klao@946
|
169 |
friend class SmartGraphBase;
|
alpar@973
|
170 |
friend class SmartGraph;
|
alpar@185
|
171 |
|
alpar@104
|
172 |
protected:
|
alpar@104
|
173 |
int n;
|
alpar@973
|
174 |
///\todo It should be removed (or at least define a setToId() instead).
|
alpar@973
|
175 |
///
|
alpar@905
|
176 |
Edge(int nn) {n=nn;}
|
alpar@706
|
177 |
public:
|
alpar@164
|
178 |
Edge() { }
|
marci@174
|
179 |
Edge (Invalid) { n=-1; }
|
alpar@164
|
180 |
bool operator==(const Edge i) const {return n==i.n;}
|
alpar@164
|
181 |
bool operator!=(const Edge i) const {return n!=i.n;}
|
alpar@164
|
182 |
bool operator<(const Edge i) const {return n<i.n;}
|
klao@946
|
183 |
};
|
alpar@905
|
184 |
|
klao@946
|
185 |
void first(Node& node) const {
|
klao@946
|
186 |
node.n = nodes.size() - 1;
|
klao@946
|
187 |
}
|
klao@946
|
188 |
|
klao@946
|
189 |
static void next(Node& node) {
|
klao@946
|
190 |
--node.n;
|
klao@946
|
191 |
}
|
klao@946
|
192 |
|
klao@946
|
193 |
void first(Edge& edge) const {
|
klao@946
|
194 |
edge.n = edges.size() - 1;
|
klao@946
|
195 |
}
|
klao@946
|
196 |
|
klao@946
|
197 |
static void next(Edge& edge) {
|
klao@946
|
198 |
--edge.n;
|
klao@946
|
199 |
}
|
klao@946
|
200 |
|
klao@946
|
201 |
void firstOut(Edge& edge, const Node& node) const {
|
klao@946
|
202 |
edge.n = nodes[node.n].first_out;
|
klao@946
|
203 |
}
|
klao@946
|
204 |
|
klao@946
|
205 |
void nextOut(Edge& edge) const {
|
klao@946
|
206 |
edge.n = edges[edge.n].next_out;
|
klao@946
|
207 |
}
|
klao@946
|
208 |
|
klao@946
|
209 |
void firstIn(Edge& edge, const Node& node) const {
|
klao@946
|
210 |
edge.n = nodes[node.n].first_in;
|
klao@946
|
211 |
}
|
alpar@104
|
212 |
|
klao@946
|
213 |
void nextIn(Edge& edge) const {
|
klao@946
|
214 |
edge.n = edges[edge.n].next_in;
|
klao@946
|
215 |
}
|
alpar@105
|
216 |
|
alpar@1284
|
217 |
Node _split(Node n, bool connect = true)
|
alpar@1284
|
218 |
{
|
alpar@1284
|
219 |
Node b = addNode();
|
alpar@1284
|
220 |
nodes[b.n].first_out=nodes[n.n].first_out;
|
alpar@1284
|
221 |
nodes[n.n].first_out=-1;
|
alpar@1284
|
222 |
for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
|
alpar@1284
|
223 |
if(connect) addEdge(n,b);
|
alpar@1284
|
224 |
return b;
|
alpar@1284
|
225 |
}
|
alpar@1284
|
226 |
|
alpar@104
|
227 |
};
|
alpar@185
|
228 |
|
deba@1669
|
229 |
typedef ClearableGraphExtender<
|
deba@1669
|
230 |
ExtendableGraphExtender<
|
deba@1669
|
231 |
MappableGraphExtender<
|
deba@1669
|
232 |
IterableGraphExtender<
|
deba@1669
|
233 |
AlterableGraphExtender<SmartGraphBase> > > > > ExtendedSmartGraphBase;
|
deba@937
|
234 |
|
alpar@1161
|
235 |
/// \addtogroup graphs
|
alpar@1161
|
236 |
/// @{
|
alpar@1161
|
237 |
|
alpar@950
|
238 |
///A smart graph class.
|
deba@937
|
239 |
|
alpar@950
|
240 |
///This is a simple and fast graph implementation.
|
alpar@950
|
241 |
///It is also quite memory efficient, but at the price
|
alpar@974
|
242 |
///that <b> it does support only limited (only stack-like)
|
alpar@974
|
243 |
///node and edge deletions</b>.
|
alpar@950
|
244 |
///It conforms to
|
klao@959
|
245 |
///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
|
klao@959
|
246 |
///\sa concept::ExtendableGraph.
|
alpar@950
|
247 |
///
|
alpar@950
|
248 |
///\author Alpar Juttner
|
deba@1669
|
249 |
class SmartGraph : public ExtendedSmartGraphBase {
|
alpar@969
|
250 |
public:
|
alpar@973
|
251 |
|
alpar@1770
|
252 |
class Snapshot;
|
alpar@1770
|
253 |
friend class Snapshot;
|
alpar@973
|
254 |
|
alpar@1011
|
255 |
protected:
|
alpar@1770
|
256 |
void restoreSnapshot(const Snapshot &s)
|
alpar@973
|
257 |
{
|
alpar@1457
|
258 |
while(s.edge_num<edges.size()) {
|
deba@1040
|
259 |
Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
|
alpar@986
|
260 |
nodes[edges.back().target].first_in=edges.back().next_in;
|
alpar@986
|
261 |
nodes[edges.back().source].first_out=edges.back().next_out;
|
alpar@973
|
262 |
edges.pop_back();
|
alpar@973
|
263 |
}
|
alpar@973
|
264 |
//nodes.resize(s.nodes_num);
|
alpar@1457
|
265 |
while(s.node_num<nodes.size()) {
|
deba@1040
|
266 |
Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
|
alpar@973
|
267 |
nodes.pop_back();
|
alpar@973
|
268 |
}
|
alpar@1011
|
269 |
}
|
alpar@1011
|
270 |
|
alpar@1011
|
271 |
public:
|
alpar@1284
|
272 |
|
alpar@1284
|
273 |
///Split a node.
|
alpar@1284
|
274 |
|
alpar@1284
|
275 |
///This function splits a node. First a new node is added to the graph,
|
alpar@1284
|
276 |
///then the source of each outgoing edge of \c n is moved to this new node.
|
alpar@1284
|
277 |
///If \c connect is \c true (this is the default value), then a new edge
|
alpar@1284
|
278 |
///from \c n to the newly created node is also added.
|
alpar@1284
|
279 |
///\return The newly created node.
|
alpar@1284
|
280 |
///
|
alpar@1284
|
281 |
///\note The <tt>Edge</tt>s
|
alpar@1284
|
282 |
///referencing a moved edge remain
|
alpar@1284
|
283 |
///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
|
alpar@1284
|
284 |
///may be invalidated.
|
alpar@1770
|
285 |
///\warning This functionality cannot be used together with the Snapshot
|
alpar@1284
|
286 |
///feature.
|
alpar@1284
|
287 |
///\todo It could be implemented in a bit faster way.
|
alpar@1284
|
288 |
Node split(Node n, bool connect = true)
|
alpar@1284
|
289 |
{
|
deba@1718
|
290 |
Node b = _split(n,connect);
|
deba@1718
|
291 |
return b;
|
alpar@1284
|
292 |
}
|
alpar@1284
|
293 |
|
alpar@1284
|
294 |
|
alpar@1011
|
295 |
///Class to make a snapshot of the graph and to restrore to it later.
|
alpar@1011
|
296 |
|
alpar@1011
|
297 |
///Class to make a snapshot of the graph and to restrore to it later.
|
alpar@1011
|
298 |
///
|
alpar@1011
|
299 |
///The newly added nodes and edges can be removed using the
|
alpar@1011
|
300 |
///restore() function.
|
alpar@1011
|
301 |
///\note After you restore a state, you cannot restore
|
alpar@1011
|
302 |
///a later state, in other word you cannot add again the edges deleted
|
alpar@1770
|
303 |
///by restore() using another Snapshot instance.
|
alpar@1011
|
304 |
///
|
alpar@1770
|
305 |
class Snapshot
|
alpar@1011
|
306 |
{
|
alpar@1011
|
307 |
SmartGraph *g;
|
alpar@1011
|
308 |
protected:
|
alpar@1011
|
309 |
friend class SmartGraph;
|
alpar@1011
|
310 |
unsigned int node_num;
|
alpar@1011
|
311 |
unsigned int edge_num;
|
alpar@1011
|
312 |
public:
|
zsuzska@1274
|
313 |
///Default constructor.
|
alpar@1011
|
314 |
|
zsuzska@1274
|
315 |
///Default constructor.
|
alpar@1011
|
316 |
///To actually make a snapshot you must call save().
|
alpar@1011
|
317 |
///
|
alpar@1770
|
318 |
Snapshot() : g(0) {}
|
alpar@1011
|
319 |
///Constructor that immediately makes a snapshot
|
alpar@1011
|
320 |
|
alpar@1011
|
321 |
///This constructor immediately makes a snapshot of the graph.
|
alpar@1011
|
322 |
///\param _g The graph we make a snapshot of.
|
alpar@1770
|
323 |
Snapshot(SmartGraph &_g) :g(&_g) {
|
alpar@1011
|
324 |
node_num=g->nodes.size();
|
alpar@1011
|
325 |
edge_num=g->edges.size();
|
alpar@1011
|
326 |
}
|
alpar@1011
|
327 |
|
alpar@1011
|
328 |
///Make a snapshot.
|
alpar@1011
|
329 |
|
alpar@1011
|
330 |
///Make a snapshot of the graph.
|
alpar@1011
|
331 |
///
|
alpar@1011
|
332 |
///This function can be called more than once. In case of a repeated
|
alpar@1011
|
333 |
///call, the previous snapshot gets lost.
|
alpar@1011
|
334 |
///\param _g The graph we make the snapshot of.
|
alpar@1011
|
335 |
void save(SmartGraph &_g)
|
alpar@1011
|
336 |
{
|
alpar@1011
|
337 |
g=&_g;
|
alpar@1011
|
338 |
node_num=g->nodes.size();
|
alpar@1011
|
339 |
edge_num=g->edges.size();
|
alpar@1011
|
340 |
}
|
alpar@1011
|
341 |
|
alpar@1011
|
342 |
///Undo the changes until a snapshot.
|
alpar@1011
|
343 |
|
alpar@1011
|
344 |
///Undo the changes until a snapshot created by save().
|
alpar@1011
|
345 |
///
|
alpar@1011
|
346 |
///\note After you restored a state, you cannot restore
|
alpar@1011
|
347 |
///a later state, in other word you cannot add again the edges deleted
|
alpar@1011
|
348 |
///by restore().
|
alpar@1011
|
349 |
///
|
alpar@1011
|
350 |
///\todo This function might be called undo().
|
alpar@1011
|
351 |
|
alpar@1011
|
352 |
void restore()
|
alpar@1011
|
353 |
{
|
alpar@1770
|
354 |
g->restoreSnapshot(*this);
|
alpar@1011
|
355 |
}
|
alpar@1011
|
356 |
};
|
alpar@973
|
357 |
};
|
klao@1034
|
358 |
|
klao@1034
|
359 |
|
klao@1034
|
360 |
/**************** Undirected List Graph ****************/
|
klao@1034
|
361 |
|
klao@1034
|
362 |
typedef ClearableUndirGraphExtender<
|
klao@1034
|
363 |
ExtendableUndirGraphExtender<
|
klao@1034
|
364 |
MappableUndirGraphExtender<
|
klao@1034
|
365 |
IterableUndirGraphExtender<
|
klao@1034
|
366 |
AlterableUndirGraphExtender<
|
deba@1669
|
367 |
UndirGraphExtender<SmartGraphBase> > > > > > ExtendedUndirSmartGraphBase;
|
klao@1034
|
368 |
|
alpar@1035
|
369 |
///A smart undirected graph class.
|
alpar@1035
|
370 |
|
alpar@1035
|
371 |
///This is a simple and fast undirected graph implementation.
|
alpar@1035
|
372 |
///It is also quite memory efficient, but at the price
|
alpar@1035
|
373 |
///that <b> it does support only limited (only stack-like)
|
alpar@1035
|
374 |
///node and edge deletions</b>.
|
alpar@1035
|
375 |
///Except from this it conforms to
|
alpar@1035
|
376 |
///the \ref concept::UndirGraph "UndirGraph" concept.
|
alpar@1035
|
377 |
///\sa concept::UndirGraph.
|
alpar@1035
|
378 |
///
|
alpar@1770
|
379 |
///\todo Snapshot hasn't been implemented yet.
|
alpar@1035
|
380 |
///
|
deba@1669
|
381 |
class UndirSmartGraph : public ExtendedUndirSmartGraphBase {
|
klao@1034
|
382 |
};
|
klao@1034
|
383 |
|
alpar@950
|
384 |
|
alpar@407
|
385 |
/// @}
|
alpar@921
|
386 |
} //namespace lemon
|
alpar@104
|
387 |
|
alpar@157
|
388 |
|
alpar@921
|
389 |
#endif //LEMON_SMART_GRAPH_H
|