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/idmappable_graph_extender.h>
|
alpar@395
|
29 |
|
klao@946
|
30 |
#include <lemon/iterable_graph_extender.h>
|
alpar@395
|
31 |
|
klao@946
|
32 |
#include <lemon/alteration_observer_registry.h>
|
deba@782
|
33 |
|
klao@946
|
34 |
#include <lemon/default_map.h>
|
deba@782
|
35 |
|
deba@782
|
36 |
|
alpar@921
|
37 |
namespace lemon {
|
alpar@395
|
38 |
|
klao@946
|
39 |
class ListGraphBase {
|
alpar@406
|
40 |
|
alpar@949
|
41 |
protected:
|
klao@946
|
42 |
struct NodeT {
|
alpar@397
|
43 |
int first_in,first_out;
|
alpar@397
|
44 |
int prev, next;
|
alpar@395
|
45 |
};
|
klao@946
|
46 |
|
klao@946
|
47 |
struct EdgeT {
|
alpar@397
|
48 |
int head, tail;
|
alpar@397
|
49 |
int prev_in, prev_out;
|
alpar@397
|
50 |
int next_in, next_out;
|
alpar@395
|
51 |
};
|
alpar@395
|
52 |
|
alpar@395
|
53 |
std::vector<NodeT> nodes;
|
klao@946
|
54 |
|
alpar@397
|
55 |
int first_node;
|
klao@946
|
56 |
|
alpar@397
|
57 |
int first_free_node;
|
klao@946
|
58 |
|
alpar@395
|
59 |
std::vector<EdgeT> edges;
|
klao@946
|
60 |
|
alpar@397
|
61 |
int first_free_edge;
|
alpar@395
|
62 |
|
deba@782
|
63 |
public:
|
alpar@395
|
64 |
|
klao@946
|
65 |
typedef ListGraphBase Graph;
|
alpar@397
|
66 |
|
klao@946
|
67 |
class Node {
|
klao@946
|
68 |
friend class Graph;
|
klao@946
|
69 |
protected:
|
alpar@395
|
70 |
|
klao@946
|
71 |
int id;
|
klao@946
|
72 |
Node(int pid) { id = pid;}
|
alpar@395
|
73 |
|
klao@946
|
74 |
public:
|
klao@946
|
75 |
Node() {}
|
klao@946
|
76 |
Node (Invalid) { id = -1; }
|
klao@946
|
77 |
bool operator==(const Node& node) const {return id == node.id;}
|
klao@946
|
78 |
bool operator!=(const Node& node) const {return id != node.id;}
|
klao@946
|
79 |
bool operator<(const Node& node) const {return id < node.id;}
|
klao@946
|
80 |
};
|
deba@782
|
81 |
|
klao@946
|
82 |
class Edge {
|
klao@946
|
83 |
friend class Graph;
|
klao@946
|
84 |
protected:
|
deba@782
|
85 |
|
klao@946
|
86 |
int id;
|
klao@946
|
87 |
Edge(int pid) { id = pid;}
|
alpar@395
|
88 |
|
klao@946
|
89 |
public:
|
klao@946
|
90 |
Edge() {}
|
klao@946
|
91 |
Edge (Invalid) { id = -1; }
|
klao@946
|
92 |
bool operator==(const Edge& edge) const {return id == edge.id;}
|
klao@946
|
93 |
bool operator!=(const Edge& edge) const {return id != edge.id;}
|
klao@946
|
94 |
bool operator<(const Edge& edge) const {return id < edge.id;}
|
klao@946
|
95 |
};
|
klao@946
|
96 |
|
klao@946
|
97 |
|
klao@946
|
98 |
|
klao@946
|
99 |
ListGraphBase()
|
deba@782
|
100 |
: nodes(), first_node(-1),
|
deba@782
|
101 |
first_free_node(-1), edges(), first_free_edge(-1) {}
|
deba@782
|
102 |
|
alpar@395
|
103 |
|
alpar@813
|
104 |
/// Maximum node ID.
|
alpar@813
|
105 |
|
alpar@813
|
106 |
/// Maximum node ID.
|
alpar@813
|
107 |
///\sa id(Node)
|
alpar@813
|
108 |
int maxNodeId() const { return nodes.size()-1; }
|
klao@946
|
109 |
|
alpar@813
|
110 |
/// Maximum edge ID.
|
alpar@813
|
111 |
|
alpar@813
|
112 |
/// Maximum edge ID.
|
alpar@813
|
113 |
///\sa id(Edge)
|
alpar@813
|
114 |
int maxEdgeId() const { return edges.size()-1; }
|
alpar@395
|
115 |
|
klao@946
|
116 |
Node tail(Edge e) const { return edges[e.id].tail; }
|
klao@946
|
117 |
Node head(Edge e) const { return edges[e.id].head; }
|
alpar@395
|
118 |
|
alpar@395
|
119 |
|
klao@946
|
120 |
void first(Node& node) const {
|
klao@946
|
121 |
node.id = first_node;
|
klao@946
|
122 |
}
|
klao@946
|
123 |
|
klao@946
|
124 |
void next(Node& node) const {
|
klao@946
|
125 |
node.id = nodes[node.id].next;
|
klao@946
|
126 |
}
|
klao@946
|
127 |
|
klao@946
|
128 |
|
klao@946
|
129 |
void first(Edge& e) const {
|
klao@946
|
130 |
int n;
|
klao@946
|
131 |
for(n = first_node;
|
klao@946
|
132 |
n!=-1 && nodes[n].first_in == -1;
|
klao@946
|
133 |
n = nodes[n].next);
|
klao@946
|
134 |
e.id = (n == -1) ? -1 : nodes[n].first_in;
|
klao@946
|
135 |
}
|
klao@946
|
136 |
|
klao@946
|
137 |
void next(Edge& edge) const {
|
klao@946
|
138 |
if (edges[edge.id].next_in != -1) {
|
klao@946
|
139 |
edge.id = edges[edge.id].next_in;
|
klao@946
|
140 |
} else {
|
klao@946
|
141 |
int n;
|
klao@946
|
142 |
for(n = nodes[edges[edge.id].head].next;
|
klao@946
|
143 |
n!=-1 && nodes[n].first_in == -1;
|
klao@946
|
144 |
n = nodes[n].next);
|
klao@946
|
145 |
edge.id = (n == -1) ? -1 : nodes[n].first_in;
|
klao@946
|
146 |
}
|
klao@946
|
147 |
}
|
klao@946
|
148 |
|
klao@946
|
149 |
void firstOut(Edge &e, const Node& v) const {
|
klao@946
|
150 |
e.id = nodes[v.id].first_out;
|
klao@946
|
151 |
}
|
klao@946
|
152 |
void nextOut(Edge &e) const {
|
klao@946
|
153 |
e.id=edges[e.id].next_out;
|
klao@946
|
154 |
}
|
klao@946
|
155 |
|
klao@946
|
156 |
void firstIn(Edge &e, const Node& v) const {
|
klao@946
|
157 |
e.id = nodes[v.id].first_in;
|
klao@946
|
158 |
}
|
klao@946
|
159 |
void nextIn(Edge &e) const {
|
klao@946
|
160 |
e.id=edges[e.id].next_in;
|
klao@946
|
161 |
}
|
klao@946
|
162 |
|
alpar@813
|
163 |
|
klao@946
|
164 |
static int id(Node v) { return v.id; }
|
klao@946
|
165 |
static int id(Edge e) { return e.id; }
|
alpar@395
|
166 |
|
alpar@397
|
167 |
/// Adds a new node to the graph.
|
alpar@397
|
168 |
|
alpar@813
|
169 |
/// \warning It adds the new node to the front of the list.
|
alpar@397
|
170 |
/// (i.e. the lastly added node becomes the first.)
|
klao@946
|
171 |
Node addNode() {
|
alpar@397
|
172 |
int n;
|
alpar@397
|
173 |
|
klao@946
|
174 |
if(first_free_node==-1) {
|
klao@946
|
175 |
n = nodes.size();
|
klao@946
|
176 |
nodes.push_back(NodeT());
|
klao@946
|
177 |
} else {
|
alpar@397
|
178 |
n = first_free_node;
|
alpar@397
|
179 |
first_free_node = nodes[n].next;
|
alpar@397
|
180 |
}
|
alpar@397
|
181 |
|
alpar@397
|
182 |
nodes[n].next = first_node;
|
alpar@397
|
183 |
if(first_node != -1) nodes[first_node].prev = n;
|
alpar@397
|
184 |
first_node = n;
|
alpar@397
|
185 |
nodes[n].prev = -1;
|
alpar@397
|
186 |
|
alpar@397
|
187 |
nodes[n].first_in = nodes[n].first_out = -1;
|
alpar@397
|
188 |
|
klao@946
|
189 |
return Node(n);
|
alpar@395
|
190 |
}
|
alpar@395
|
191 |
|
alpar@395
|
192 |
Edge addEdge(Node u, Node v) {
|
klao@946
|
193 |
int n;
|
klao@946
|
194 |
|
klao@946
|
195 |
if (first_free_edge == -1) {
|
klao@946
|
196 |
n = edges.size();
|
klao@946
|
197 |
edges.push_back(EdgeT());
|
klao@946
|
198 |
} else {
|
alpar@397
|
199 |
n = first_free_edge;
|
alpar@397
|
200 |
first_free_edge = edges[n].next_in;
|
alpar@397
|
201 |
}
|
alpar@397
|
202 |
|
klao@946
|
203 |
edges[n].tail = u.id;
|
klao@946
|
204 |
edges[n].head = v.id;
|
alpar@395
|
205 |
|
klao@946
|
206 |
edges[n].next_out = nodes[u.id].first_out;
|
klao@946
|
207 |
if(nodes[u.id].first_out != -1) {
|
klao@946
|
208 |
edges[nodes[u.id].first_out].prev_out = n;
|
klao@946
|
209 |
}
|
klao@946
|
210 |
|
klao@946
|
211 |
edges[n].next_in = nodes[v.id].first_in;
|
klao@946
|
212 |
if(nodes[v.id].first_in != -1) {
|
klao@946
|
213 |
edges[nodes[v.id].first_in].prev_in = n;
|
klao@946
|
214 |
}
|
klao@946
|
215 |
|
alpar@397
|
216 |
edges[n].prev_in = edges[n].prev_out = -1;
|
alpar@397
|
217 |
|
klao@946
|
218 |
nodes[u.id].first_out = nodes[v.id].first_in = n;
|
alpar@397
|
219 |
|
klao@946
|
220 |
return Edge(n);
|
alpar@395
|
221 |
}
|
alpar@774
|
222 |
|
klao@946
|
223 |
void erase(const Node& node) {
|
klao@946
|
224 |
int n = node.id;
|
klao@946
|
225 |
|
klao@946
|
226 |
if(nodes[n].next != -1) {
|
klao@946
|
227 |
nodes[nodes[n].next].prev = nodes[n].prev;
|
klao@946
|
228 |
}
|
klao@946
|
229 |
|
klao@946
|
230 |
if(nodes[n].prev != -1) {
|
klao@946
|
231 |
nodes[nodes[n].prev].next = nodes[n].next;
|
klao@946
|
232 |
} else {
|
klao@946
|
233 |
first_node = nodes[n].next;
|
klao@946
|
234 |
}
|
klao@946
|
235 |
|
klao@946
|
236 |
nodes[n].next = first_free_node;
|
klao@946
|
237 |
first_free_node = n;
|
alpar@395
|
238 |
|
alpar@774
|
239 |
}
|
alpar@774
|
240 |
|
klao@946
|
241 |
void erase(const Edge& edge) {
|
klao@946
|
242 |
int n = edge.id;
|
alpar@397
|
243 |
|
klao@946
|
244 |
if(edges[n].next_in!=-1) {
|
alpar@397
|
245 |
edges[edges[n].next_in].prev_in = edges[n].prev_in;
|
klao@946
|
246 |
}
|
klao@946
|
247 |
|
klao@946
|
248 |
if(edges[n].prev_in!=-1) {
|
alpar@397
|
249 |
edges[edges[n].prev_in].next_in = edges[n].next_in;
|
klao@946
|
250 |
} else {
|
klao@946
|
251 |
nodes[edges[n].head].first_in = edges[n].next_in;
|
klao@946
|
252 |
}
|
klao@946
|
253 |
|
alpar@397
|
254 |
|
klao@946
|
255 |
if(edges[n].next_out!=-1) {
|
alpar@397
|
256 |
edges[edges[n].next_out].prev_out = edges[n].prev_out;
|
klao@946
|
257 |
}
|
klao@946
|
258 |
|
klao@946
|
259 |
if(edges[n].prev_out!=-1) {
|
alpar@397
|
260 |
edges[edges[n].prev_out].next_out = edges[n].next_out;
|
klao@946
|
261 |
} else {
|
klao@946
|
262 |
nodes[edges[n].tail].first_out = edges[n].next_out;
|
klao@946
|
263 |
}
|
alpar@397
|
264 |
|
alpar@397
|
265 |
edges[n].next_in = first_free_edge;
|
alpar@695
|
266 |
first_free_edge = n;
|
alpar@397
|
267 |
|
alpar@397
|
268 |
}
|
alpar@397
|
269 |
|
alpar@397
|
270 |
void clear() {
|
deba@782
|
271 |
edges.clear();
|
deba@782
|
272 |
nodes.clear();
|
klao@946
|
273 |
first_node = first_free_node = first_free_edge = -1;
|
deba@937
|
274 |
}
|
deba@937
|
275 |
|
alpar@949
|
276 |
protected:
|
alpar@949
|
277 |
void _moveHead(Edge e, Node n)
|
alpar@949
|
278 |
{
|
alpar@949
|
279 |
if(edges[e.id].next_in != -1)
|
alpar@949
|
280 |
edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
|
alpar@949
|
281 |
if(edges[e.id].prev_in != -1)
|
alpar@949
|
282 |
edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
|
alpar@949
|
283 |
else nodes[edges[e.id].head].first_in = edges[e.id].next_in;
|
alpar@949
|
284 |
edges[e.id].head = n.id;
|
alpar@949
|
285 |
edges[e.id].prev_in = -1;
|
alpar@949
|
286 |
edges[e.id].next_in = nodes[n.id].first_in;
|
alpar@949
|
287 |
nodes[n.id].first_in = e.id;
|
alpar@949
|
288 |
}
|
alpar@949
|
289 |
void _moveTail(Edge e, Node n)
|
alpar@949
|
290 |
{
|
alpar@949
|
291 |
if(edges[e.id].next_out != -1)
|
alpar@949
|
292 |
edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
|
alpar@949
|
293 |
if(edges[e.id].prev_out != -1)
|
alpar@949
|
294 |
edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
|
alpar@949
|
295 |
else nodes[edges[e.id].tail].first_out = edges[e.id].next_out;
|
alpar@949
|
296 |
edges[e.id].tail = n.id;
|
alpar@949
|
297 |
edges[e.id].prev_out = -1;
|
alpar@949
|
298 |
edges[e.id].next_out = nodes[n.id].first_out;
|
alpar@949
|
299 |
nodes[n.id].first_out = e.id;
|
alpar@949
|
300 |
}
|
alpar@949
|
301 |
|
alpar@919
|
302 |
};
|
deba@909
|
303 |
|
klao@946
|
304 |
typedef AlterableGraphExtender<ListGraphBase> AlterableListGraphBase;
|
klao@946
|
305 |
typedef IterableGraphExtender<AlterableListGraphBase> IterableListGraphBase;
|
klao@946
|
306 |
typedef IdMappableGraphExtender<IterableListGraphBase> IdMappableListGraphBase;
|
klao@946
|
307 |
typedef DefaultMappableGraphExtender<IdMappableListGraphBase> MappableListGraphBase;
|
klao@946
|
308 |
typedef ExtendableGraphExtender<MappableListGraphBase> ExtendableListGraphBase;
|
klao@946
|
309 |
typedef ClearableGraphExtender<ExtendableListGraphBase> ClearableListGraphBase;
|
klao@946
|
310 |
typedef ErasableGraphExtender<ClearableListGraphBase> ErasableListGraphBase;
|
alpar@400
|
311 |
|
alpar@948
|
312 |
/// \addtogroup graphs
|
alpar@948
|
313 |
/// @{
|
alpar@400
|
314 |
|
alpar@948
|
315 |
///A list graph class.
|
alpar@400
|
316 |
|
alpar@948
|
317 |
///This is a simple and fast erasable graph implementation.
|
alpar@948
|
318 |
///
|
alpar@948
|
319 |
///It conforms to the
|
klao@959
|
320 |
///\ref concept::ErasableGraph "ErasableGraph" concept.
|
klao@959
|
321 |
///\sa concept::ErasableGraph.
|
deba@782
|
322 |
|
alpar@948
|
323 |
class ListGraph : public ErasableListGraphBase
|
alpar@948
|
324 |
{
|
alpar@948
|
325 |
public:
|
alpar@948
|
326 |
/// Moves the head of \c e to \c n
|
alpar@948
|
327 |
|
alpar@948
|
328 |
/// Moves the head of \c e to \c n
|
alpar@948
|
329 |
///
|
alpar@949
|
330 |
void moveHead(Edge e, Node n) { _moveHead(e,n); }
|
alpar@948
|
331 |
/// Moves the tail of \c e to \c n
|
alpar@948
|
332 |
|
alpar@948
|
333 |
/// Moves the tail of \c e to \c n
|
alpar@948
|
334 |
///
|
alpar@949
|
335 |
void moveTail(Edge e, Node n) { _moveTail(e,n); }
|
alpar@949
|
336 |
|
alpar@949
|
337 |
///Using this it possible to avoid the superfluous memory allocation.
|
alpar@949
|
338 |
///\todo more docs...
|
alpar@949
|
339 |
void reserveEdge(int n) { edges.reserve(n); };
|
alpar@949
|
340 |
|
alpar@949
|
341 |
};
|
alpar@949
|
342 |
|
alpar@948
|
343 |
/// @}
|
alpar@948
|
344 |
} //namespace lemon
|
klao@946
|
345 |
|
alpar@400
|
346 |
|
klao@946
|
347 |
#endif
|