alpar@948
|
1 |
/* -*- C++ -*-
|
alpar@948
|
2 |
* src/lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
|
alpar@948
|
3 |
*
|
alpar@948
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@948
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@948
|
6 |
*
|
alpar@948
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@948
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@948
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@948
|
10 |
*
|
alpar@948
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@948
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@948
|
13 |
* purpose.
|
alpar@948
|
14 |
*
|
alpar@948
|
15 |
*/
|
alpar@395
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_LIST_GRAPH_H
|
alpar@921
|
18 |
#define LEMON_LIST_GRAPH_H
|
alpar@395
|
19 |
|
alpar@948
|
20 |
///\ingroup graphs
|
alpar@948
|
21 |
///\file
|
alpar@948
|
22 |
///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes.
|
alpar@948
|
23 |
|
klao@946
|
24 |
#include <lemon/erasable_graph_extender.h>
|
klao@946
|
25 |
#include <lemon/clearable_graph_extender.h>
|
klao@946
|
26 |
#include <lemon/extendable_graph_extender.h>
|
alpar@395
|
27 |
|
klao@946
|
28 |
#include <lemon/iterable_graph_extender.h>
|
alpar@395
|
29 |
|
klao@946
|
30 |
#include <lemon/alteration_observer_registry.h>
|
deba@782
|
31 |
|
klao@946
|
32 |
#include <lemon/default_map.h>
|
deba@782
|
33 |
|
deba@782
|
34 |
|
alpar@921
|
35 |
namespace lemon {
|
alpar@395
|
36 |
|
klao@946
|
37 |
class ListGraphBase {
|
alpar@406
|
38 |
|
alpar@949
|
39 |
protected:
|
klao@946
|
40 |
struct NodeT {
|
alpar@397
|
41 |
int first_in,first_out;
|
alpar@397
|
42 |
int prev, next;
|
alpar@395
|
43 |
};
|
klao@946
|
44 |
|
klao@946
|
45 |
struct EdgeT {
|
alpar@986
|
46 |
int target, source;
|
alpar@397
|
47 |
int prev_in, prev_out;
|
alpar@397
|
48 |
int next_in, next_out;
|
alpar@395
|
49 |
};
|
alpar@395
|
50 |
|
alpar@395
|
51 |
std::vector<NodeT> nodes;
|
klao@946
|
52 |
|
alpar@397
|
53 |
int first_node;
|
klao@946
|
54 |
|
alpar@397
|
55 |
int first_free_node;
|
klao@946
|
56 |
|
alpar@395
|
57 |
std::vector<EdgeT> edges;
|
klao@946
|
58 |
|
alpar@397
|
59 |
int first_free_edge;
|
alpar@395
|
60 |
|
deba@782
|
61 |
public:
|
alpar@395
|
62 |
|
klao@946
|
63 |
typedef ListGraphBase Graph;
|
alpar@397
|
64 |
|
klao@946
|
65 |
class Node {
|
marci@975
|
66 |
friend class ListGraphBase;
|
klao@946
|
67 |
protected:
|
alpar@395
|
68 |
|
klao@946
|
69 |
int id;
|
klao@946
|
70 |
Node(int pid) { id = pid;}
|
alpar@395
|
71 |
|
klao@946
|
72 |
public:
|
klao@946
|
73 |
Node() {}
|
klao@946
|
74 |
Node (Invalid) { id = -1; }
|
klao@946
|
75 |
bool operator==(const Node& node) const {return id == node.id;}
|
klao@946
|
76 |
bool operator!=(const Node& node) const {return id != node.id;}
|
klao@946
|
77 |
bool operator<(const Node& node) const {return id < node.id;}
|
klao@946
|
78 |
};
|
deba@782
|
79 |
|
klao@946
|
80 |
class Edge {
|
marci@975
|
81 |
friend class ListGraphBase;
|
klao@946
|
82 |
protected:
|
deba@782
|
83 |
|
klao@946
|
84 |
int id;
|
klao@946
|
85 |
Edge(int pid) { id = pid;}
|
alpar@395
|
86 |
|
klao@946
|
87 |
public:
|
klao@946
|
88 |
Edge() {}
|
klao@946
|
89 |
Edge (Invalid) { id = -1; }
|
klao@946
|
90 |
bool operator==(const Edge& edge) const {return id == edge.id;}
|
klao@946
|
91 |
bool operator!=(const Edge& edge) const {return id != edge.id;}
|
klao@946
|
92 |
bool operator<(const Edge& edge) const {return id < edge.id;}
|
klao@946
|
93 |
};
|
klao@946
|
94 |
|
klao@946
|
95 |
|
klao@946
|
96 |
|
klao@946
|
97 |
ListGraphBase()
|
deba@782
|
98 |
: nodes(), first_node(-1),
|
deba@782
|
99 |
first_free_node(-1), edges(), first_free_edge(-1) {}
|
deba@782
|
100 |
|
alpar@395
|
101 |
|
alpar@813
|
102 |
/// Maximum node ID.
|
alpar@813
|
103 |
|
alpar@813
|
104 |
/// Maximum node ID.
|
alpar@813
|
105 |
///\sa id(Node)
|
deba@980
|
106 |
int maxId(Node = INVALID) const { return nodes.size()-1; }
|
klao@946
|
107 |
|
alpar@813
|
108 |
/// Maximum edge ID.
|
alpar@813
|
109 |
|
alpar@813
|
110 |
/// Maximum edge ID.
|
alpar@813
|
111 |
///\sa id(Edge)
|
deba@980
|
112 |
int maxId(Edge = INVALID) const { return edges.size()-1; }
|
alpar@395
|
113 |
|
alpar@986
|
114 |
Node source(Edge e) const { return edges[e.id].source; }
|
alpar@986
|
115 |
Node target(Edge e) const { return edges[e.id].target; }
|
alpar@395
|
116 |
|
alpar@395
|
117 |
|
klao@946
|
118 |
void first(Node& node) const {
|
klao@946
|
119 |
node.id = first_node;
|
klao@946
|
120 |
}
|
klao@946
|
121 |
|
klao@946
|
122 |
void next(Node& node) const {
|
klao@946
|
123 |
node.id = nodes[node.id].next;
|
klao@946
|
124 |
}
|
klao@946
|
125 |
|
klao@946
|
126 |
|
klao@946
|
127 |
void first(Edge& e) const {
|
klao@946
|
128 |
int n;
|
klao@946
|
129 |
for(n = first_node;
|
klao@946
|
130 |
n!=-1 && nodes[n].first_in == -1;
|
klao@946
|
131 |
n = nodes[n].next);
|
klao@946
|
132 |
e.id = (n == -1) ? -1 : nodes[n].first_in;
|
klao@946
|
133 |
}
|
klao@946
|
134 |
|
klao@946
|
135 |
void next(Edge& edge) const {
|
klao@946
|
136 |
if (edges[edge.id].next_in != -1) {
|
klao@946
|
137 |
edge.id = edges[edge.id].next_in;
|
klao@946
|
138 |
} else {
|
klao@946
|
139 |
int n;
|
alpar@986
|
140 |
for(n = nodes[edges[edge.id].target].next;
|
klao@946
|
141 |
n!=-1 && nodes[n].first_in == -1;
|
klao@946
|
142 |
n = nodes[n].next);
|
klao@946
|
143 |
edge.id = (n == -1) ? -1 : nodes[n].first_in;
|
klao@946
|
144 |
}
|
klao@946
|
145 |
}
|
klao@946
|
146 |
|
klao@946
|
147 |
void firstOut(Edge &e, const Node& v) const {
|
klao@946
|
148 |
e.id = nodes[v.id].first_out;
|
klao@946
|
149 |
}
|
klao@946
|
150 |
void nextOut(Edge &e) const {
|
klao@946
|
151 |
e.id=edges[e.id].next_out;
|
klao@946
|
152 |
}
|
klao@946
|
153 |
|
klao@946
|
154 |
void firstIn(Edge &e, const Node& v) const {
|
klao@946
|
155 |
e.id = nodes[v.id].first_in;
|
klao@946
|
156 |
}
|
klao@946
|
157 |
void nextIn(Edge &e) const {
|
klao@946
|
158 |
e.id=edges[e.id].next_in;
|
klao@946
|
159 |
}
|
klao@946
|
160 |
|
alpar@813
|
161 |
|
klao@946
|
162 |
static int id(Node v) { return v.id; }
|
klao@946
|
163 |
static int id(Edge e) { return e.id; }
|
alpar@395
|
164 |
|
alpar@397
|
165 |
/// Adds a new node to the graph.
|
alpar@397
|
166 |
|
alpar@813
|
167 |
/// \warning It adds the new node to the front of the list.
|
alpar@397
|
168 |
/// (i.e. the lastly added node becomes the first.)
|
klao@946
|
169 |
Node addNode() {
|
alpar@397
|
170 |
int n;
|
alpar@397
|
171 |
|
klao@946
|
172 |
if(first_free_node==-1) {
|
klao@946
|
173 |
n = nodes.size();
|
klao@946
|
174 |
nodes.push_back(NodeT());
|
klao@946
|
175 |
} else {
|
alpar@397
|
176 |
n = first_free_node;
|
alpar@397
|
177 |
first_free_node = nodes[n].next;
|
alpar@397
|
178 |
}
|
alpar@397
|
179 |
|
alpar@397
|
180 |
nodes[n].next = first_node;
|
alpar@397
|
181 |
if(first_node != -1) nodes[first_node].prev = n;
|
alpar@397
|
182 |
first_node = n;
|
alpar@397
|
183 |
nodes[n].prev = -1;
|
alpar@397
|
184 |
|
alpar@397
|
185 |
nodes[n].first_in = nodes[n].first_out = -1;
|
alpar@397
|
186 |
|
klao@946
|
187 |
return Node(n);
|
alpar@395
|
188 |
}
|
alpar@395
|
189 |
|
alpar@395
|
190 |
Edge addEdge(Node u, Node v) {
|
klao@946
|
191 |
int n;
|
klao@946
|
192 |
|
klao@946
|
193 |
if (first_free_edge == -1) {
|
klao@946
|
194 |
n = edges.size();
|
klao@946
|
195 |
edges.push_back(EdgeT());
|
klao@946
|
196 |
} else {
|
alpar@397
|
197 |
n = first_free_edge;
|
alpar@397
|
198 |
first_free_edge = edges[n].next_in;
|
alpar@397
|
199 |
}
|
alpar@397
|
200 |
|
alpar@986
|
201 |
edges[n].source = u.id;
|
alpar@986
|
202 |
edges[n].target = v.id;
|
alpar@395
|
203 |
|
klao@946
|
204 |
edges[n].next_out = nodes[u.id].first_out;
|
klao@946
|
205 |
if(nodes[u.id].first_out != -1) {
|
klao@946
|
206 |
edges[nodes[u.id].first_out].prev_out = n;
|
klao@946
|
207 |
}
|
klao@946
|
208 |
|
klao@946
|
209 |
edges[n].next_in = nodes[v.id].first_in;
|
klao@946
|
210 |
if(nodes[v.id].first_in != -1) {
|
klao@946
|
211 |
edges[nodes[v.id].first_in].prev_in = n;
|
klao@946
|
212 |
}
|
klao@946
|
213 |
|
alpar@397
|
214 |
edges[n].prev_in = edges[n].prev_out = -1;
|
alpar@397
|
215 |
|
klao@946
|
216 |
nodes[u.id].first_out = nodes[v.id].first_in = n;
|
alpar@397
|
217 |
|
klao@946
|
218 |
return Edge(n);
|
alpar@395
|
219 |
}
|
alpar@774
|
220 |
|
klao@946
|
221 |
void erase(const Node& node) {
|
klao@946
|
222 |
int n = node.id;
|
klao@946
|
223 |
|
klao@946
|
224 |
if(nodes[n].next != -1) {
|
klao@946
|
225 |
nodes[nodes[n].next].prev = nodes[n].prev;
|
klao@946
|
226 |
}
|
klao@946
|
227 |
|
klao@946
|
228 |
if(nodes[n].prev != -1) {
|
klao@946
|
229 |
nodes[nodes[n].prev].next = nodes[n].next;
|
klao@946
|
230 |
} else {
|
klao@946
|
231 |
first_node = nodes[n].next;
|
klao@946
|
232 |
}
|
klao@946
|
233 |
|
klao@946
|
234 |
nodes[n].next = first_free_node;
|
klao@946
|
235 |
first_free_node = n;
|
alpar@395
|
236 |
|
alpar@774
|
237 |
}
|
alpar@774
|
238 |
|
klao@946
|
239 |
void erase(const Edge& edge) {
|
klao@946
|
240 |
int n = edge.id;
|
alpar@397
|
241 |
|
klao@946
|
242 |
if(edges[n].next_in!=-1) {
|
alpar@397
|
243 |
edges[edges[n].next_in].prev_in = edges[n].prev_in;
|
klao@946
|
244 |
}
|
klao@946
|
245 |
|
klao@946
|
246 |
if(edges[n].prev_in!=-1) {
|
alpar@397
|
247 |
edges[edges[n].prev_in].next_in = edges[n].next_in;
|
klao@946
|
248 |
} else {
|
alpar@986
|
249 |
nodes[edges[n].target].first_in = edges[n].next_in;
|
klao@946
|
250 |
}
|
klao@946
|
251 |
|
alpar@397
|
252 |
|
klao@946
|
253 |
if(edges[n].next_out!=-1) {
|
alpar@397
|
254 |
edges[edges[n].next_out].prev_out = edges[n].prev_out;
|
klao@946
|
255 |
}
|
klao@946
|
256 |
|
klao@946
|
257 |
if(edges[n].prev_out!=-1) {
|
alpar@397
|
258 |
edges[edges[n].prev_out].next_out = edges[n].next_out;
|
klao@946
|
259 |
} else {
|
alpar@986
|
260 |
nodes[edges[n].source].first_out = edges[n].next_out;
|
klao@946
|
261 |
}
|
alpar@397
|
262 |
|
alpar@397
|
263 |
edges[n].next_in = first_free_edge;
|
alpar@695
|
264 |
first_free_edge = n;
|
alpar@397
|
265 |
|
alpar@397
|
266 |
}
|
alpar@397
|
267 |
|
alpar@397
|
268 |
void clear() {
|
deba@782
|
269 |
edges.clear();
|
deba@782
|
270 |
nodes.clear();
|
klao@946
|
271 |
first_node = first_free_node = first_free_edge = -1;
|
deba@937
|
272 |
}
|
deba@937
|
273 |
|
alpar@949
|
274 |
protected:
|
alpar@986
|
275 |
void _moveTarget(Edge e, Node n)
|
alpar@949
|
276 |
{
|
alpar@949
|
277 |
if(edges[e.id].next_in != -1)
|
alpar@949
|
278 |
edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
|
alpar@949
|
279 |
if(edges[e.id].prev_in != -1)
|
alpar@949
|
280 |
edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
|
alpar@986
|
281 |
else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
|
alpar@986
|
282 |
edges[e.id].target = n.id;
|
alpar@949
|
283 |
edges[e.id].prev_in = -1;
|
alpar@949
|
284 |
edges[e.id].next_in = nodes[n.id].first_in;
|
alpar@949
|
285 |
nodes[n.id].first_in = e.id;
|
alpar@949
|
286 |
}
|
alpar@986
|
287 |
void _moveSource(Edge e, Node n)
|
alpar@949
|
288 |
{
|
alpar@949
|
289 |
if(edges[e.id].next_out != -1)
|
alpar@949
|
290 |
edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
|
alpar@949
|
291 |
if(edges[e.id].prev_out != -1)
|
alpar@949
|
292 |
edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
|
alpar@986
|
293 |
else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
|
alpar@986
|
294 |
edges[e.id].source = n.id;
|
alpar@949
|
295 |
edges[e.id].prev_out = -1;
|
alpar@949
|
296 |
edges[e.id].next_out = nodes[n.id].first_out;
|
alpar@949
|
297 |
nodes[n.id].first_out = e.id;
|
alpar@949
|
298 |
}
|
alpar@949
|
299 |
|
alpar@919
|
300 |
};
|
deba@909
|
301 |
|
klao@946
|
302 |
typedef AlterableGraphExtender<ListGraphBase> AlterableListGraphBase;
|
klao@946
|
303 |
typedef IterableGraphExtender<AlterableListGraphBase> IterableListGraphBase;
|
deba@980
|
304 |
typedef DefaultMappableGraphExtender<IterableListGraphBase> MappableListGraphBase;
|
klao@946
|
305 |
typedef ExtendableGraphExtender<MappableListGraphBase> ExtendableListGraphBase;
|
klao@946
|
306 |
typedef ClearableGraphExtender<ExtendableListGraphBase> ClearableListGraphBase;
|
klao@946
|
307 |
typedef ErasableGraphExtender<ClearableListGraphBase> ErasableListGraphBase;
|
alpar@400
|
308 |
|
alpar@948
|
309 |
/// \addtogroup graphs
|
alpar@948
|
310 |
/// @{
|
alpar@400
|
311 |
|
alpar@948
|
312 |
///A list graph class.
|
alpar@400
|
313 |
|
alpar@948
|
314 |
///This is a simple and fast erasable graph implementation.
|
alpar@948
|
315 |
///
|
alpar@948
|
316 |
///It conforms to the
|
klao@959
|
317 |
///\ref concept::ErasableGraph "ErasableGraph" concept.
|
klao@959
|
318 |
///\sa concept::ErasableGraph.
|
deba@782
|
319 |
|
alpar@948
|
320 |
class ListGraph : public ErasableListGraphBase
|
alpar@948
|
321 |
{
|
alpar@948
|
322 |
public:
|
alpar@986
|
323 |
/// Moves the target of \c e to \c n
|
alpar@948
|
324 |
|
alpar@986
|
325 |
/// Moves the target of \c e to \c n
|
alpar@948
|
326 |
///
|
alpar@986
|
327 |
void moveTarget(Edge e, Node n) { _moveTarget(e,n); }
|
alpar@986
|
328 |
/// Moves the source of \c e to \c n
|
alpar@948
|
329 |
|
alpar@986
|
330 |
/// Moves the source of \c e to \c n
|
alpar@948
|
331 |
///
|
alpar@986
|
332 |
void moveSource(Edge e, Node n) { _moveSource(e,n); }
|
alpar@949
|
333 |
|
alpar@949
|
334 |
///Using this it possible to avoid the superfluous memory allocation.
|
alpar@949
|
335 |
///\todo more docs...
|
alpar@949
|
336 |
void reserveEdge(int n) { edges.reserve(n); };
|
alpar@949
|
337 |
|
alpar@949
|
338 |
};
|
alpar@949
|
339 |
|
alpar@948
|
340 |
/// @}
|
alpar@948
|
341 |
} //namespace lemon
|
klao@946
|
342 |
|
alpar@400
|
343 |
|
klao@946
|
344 |
#endif
|