alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@921
|
2 |
* src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, 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
|
alpar@242
|
22 |
///\brief SmartGraph and SymSmartGraph classes.
|
alpar@242
|
23 |
|
alpar@104
|
24 |
#include <vector>
|
alpar@104
|
25 |
|
alpar@921
|
26 |
#include <lemon/invalid.h>
|
alpar@157
|
27 |
|
klao@946
|
28 |
#include <lemon/erasable_graph_extender.h>
|
klao@946
|
29 |
#include <lemon/clearable_graph_extender.h>
|
klao@946
|
30 |
#include <lemon/extendable_graph_extender.h>
|
deba@937
|
31 |
|
klao@946
|
32 |
#include <lemon/idmappable_graph_extender.h>
|
alpar@919
|
33 |
|
klao@946
|
34 |
#include <lemon/iterable_graph_extender.h>
|
deba@782
|
35 |
|
klao@946
|
36 |
#include <lemon/alteration_observer_registry.h>
|
klao@946
|
37 |
#include <lemon/default_map.h>
|
klao@946
|
38 |
|
klao@946
|
39 |
|
klao@946
|
40 |
#include <lemon/graph_utils.h>
|
klao@946
|
41 |
|
deba@782
|
42 |
|
alpar@921
|
43 |
namespace lemon {
|
alpar@104
|
44 |
|
klao@946
|
45 |
/// \addtogroup graphs
|
klao@946
|
46 |
/// @{
|
alpar@185
|
47 |
|
alpar@969
|
48 |
///Base of SmartGraph
|
alpar@969
|
49 |
|
alpar@969
|
50 |
///Base of SmartGraph
|
alpar@969
|
51 |
///
|
klao@946
|
52 |
class SmartGraphBase {
|
alpar@104
|
53 |
|
alpar@104
|
54 |
struct NodeT
|
alpar@104
|
55 |
{
|
alpar@104
|
56 |
int first_in,first_out;
|
alpar@157
|
57 |
NodeT() : first_in(-1), first_out(-1) {}
|
alpar@104
|
58 |
};
|
alpar@104
|
59 |
struct EdgeT
|
alpar@104
|
60 |
{
|
alpar@104
|
61 |
int head, tail, next_in, next_out;
|
alpar@104
|
62 |
//FIXME: is this necessary?
|
alpar@157
|
63 |
EdgeT() : next_in(-1), next_out(-1) {}
|
alpar@104
|
64 |
};
|
alpar@104
|
65 |
|
alpar@104
|
66 |
std::vector<NodeT> nodes;
|
alpar@129
|
67 |
|
alpar@104
|
68 |
std::vector<EdgeT> edges;
|
alpar@104
|
69 |
|
alpar@185
|
70 |
|
alpar@104
|
71 |
public:
|
deba@782
|
72 |
|
klao@946
|
73 |
typedef SmartGraphBase Graph;
|
alpar@104
|
74 |
|
alpar@164
|
75 |
class Node;
|
alpar@164
|
76 |
class Edge;
|
alpar@108
|
77 |
|
alpar@104
|
78 |
|
alpar@104
|
79 |
public:
|
alpar@104
|
80 |
|
klao@946
|
81 |
SmartGraphBase() : nodes(), edges() { }
|
klao@946
|
82 |
SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { }
|
alpar@104
|
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@813
|
93 |
int maxNodeId() 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@813
|
98 |
int maxEdgeId() const { return edges.size()-1; }
|
alpar@108
|
99 |
|
alpar@164
|
100 |
Node tail(Edge e) const { return edges[e.n].tail; }
|
alpar@164
|
101 |
Node head(Edge e) const { return edges[e.n].head; }
|
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@813
|
106 |
/// \ref maxNodeId(). The range of the ID's is not surely continuous
|
alpar@813
|
107 |
/// and the greatest node ID can be actually less then \ref maxNodeId().
|
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@813
|
115 |
/// \ref maxEdgeId(). The range of the ID's is not surely continuous
|
alpar@813
|
116 |
/// and the greatest edge ID can be actually less then \ref maxEdgeId().
|
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 |
|
alpar@164
|
122 |
Node addNode() {
|
alpar@164
|
123 |
Node n; n.n=nodes.size();
|
alpar@104
|
124 |
nodes.push_back(NodeT()); //FIXME: Hmmm...
|
alpar@104
|
125 |
return n;
|
alpar@104
|
126 |
}
|
alpar@108
|
127 |
|
alpar@164
|
128 |
Edge addEdge(Node u, Node v) {
|
alpar@164
|
129 |
Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
|
alpar@104
|
130 |
edges[e.n].tail=u.n; edges[e.n].head=v.n;
|
alpar@104
|
131 |
edges[e.n].next_out=nodes[u.n].first_out;
|
alpar@104
|
132 |
edges[e.n].next_in=nodes[v.n].first_in;
|
alpar@104
|
133 |
nodes[u.n].first_out=nodes[v.n].first_in=e.n;
|
alpar@108
|
134 |
|
alpar@104
|
135 |
return e;
|
alpar@104
|
136 |
}
|
alpar@104
|
137 |
|
deba@782
|
138 |
void clear() {
|
deba@782
|
139 |
edges.clear();
|
deba@782
|
140 |
nodes.clear();
|
deba@782
|
141 |
}
|
alpar@104
|
142 |
|
klao@946
|
143 |
|
alpar@164
|
144 |
class Node {
|
klao@946
|
145 |
friend class SmartGraphBase;
|
alpar@104
|
146 |
|
alpar@104
|
147 |
protected:
|
alpar@104
|
148 |
int n;
|
alpar@164
|
149 |
Node(int nn) {n=nn;}
|
alpar@104
|
150 |
public:
|
alpar@164
|
151 |
Node() {}
|
alpar@503
|
152 |
Node (Invalid) { n=-1; }
|
alpar@164
|
153 |
bool operator==(const Node i) const {return n==i.n;}
|
alpar@164
|
154 |
bool operator!=(const Node i) const {return n!=i.n;}
|
alpar@164
|
155 |
bool operator<(const Node i) const {return n<i.n;}
|
alpar@104
|
156 |
};
|
alpar@104
|
157 |
|
alpar@104
|
158 |
|
alpar@164
|
159 |
class Edge {
|
klao@946
|
160 |
friend class SmartGraphBase;
|
alpar@185
|
161 |
|
alpar@104
|
162 |
protected:
|
alpar@104
|
163 |
int n;
|
alpar@905
|
164 |
Edge(int nn) {n=nn;}
|
alpar@706
|
165 |
public:
|
alpar@164
|
166 |
Edge() { }
|
marci@174
|
167 |
Edge (Invalid) { n=-1; }
|
alpar@164
|
168 |
bool operator==(const Edge i) const {return n==i.n;}
|
alpar@164
|
169 |
bool operator!=(const Edge i) const {return n!=i.n;}
|
alpar@164
|
170 |
bool operator<(const Edge i) const {return n<i.n;}
|
klao@946
|
171 |
};
|
alpar@905
|
172 |
|
klao@946
|
173 |
void first(Node& node) const {
|
klao@946
|
174 |
node.n = nodes.size() - 1;
|
klao@946
|
175 |
}
|
klao@946
|
176 |
|
klao@946
|
177 |
static void next(Node& node) {
|
klao@946
|
178 |
--node.n;
|
klao@946
|
179 |
}
|
klao@946
|
180 |
|
klao@946
|
181 |
void first(Edge& edge) const {
|
klao@946
|
182 |
edge.n = edges.size() - 1;
|
klao@946
|
183 |
}
|
klao@946
|
184 |
|
klao@946
|
185 |
static void next(Edge& edge) {
|
klao@946
|
186 |
--edge.n;
|
klao@946
|
187 |
}
|
klao@946
|
188 |
|
klao@946
|
189 |
void firstOut(Edge& edge, const Node& node) const {
|
klao@946
|
190 |
edge.n = nodes[node.n].first_out;
|
klao@946
|
191 |
}
|
klao@946
|
192 |
|
klao@946
|
193 |
void nextOut(Edge& edge) const {
|
klao@946
|
194 |
edge.n = edges[edge.n].next_out;
|
klao@946
|
195 |
}
|
klao@946
|
196 |
|
klao@946
|
197 |
void firstIn(Edge& edge, const Node& node) const {
|
klao@946
|
198 |
edge.n = nodes[node.n].first_in;
|
klao@946
|
199 |
}
|
alpar@104
|
200 |
|
klao@946
|
201 |
void nextIn(Edge& edge) const {
|
klao@946
|
202 |
edge.n = edges[edge.n].next_in;
|
klao@946
|
203 |
}
|
alpar@105
|
204 |
|
alpar@969
|
205 |
Edge _findEdge(Node u,Node v, Edge prev = INVALID)
|
alpar@969
|
206 |
{
|
alpar@969
|
207 |
int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
|
alpar@969
|
208 |
while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
|
alpar@969
|
209 |
prev.n=e;
|
alpar@969
|
210 |
return prev;
|
alpar@969
|
211 |
}
|
alpar@104
|
212 |
};
|
alpar@185
|
213 |
|
klao@946
|
214 |
typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
|
klao@946
|
215 |
typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
|
klao@946
|
216 |
typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase;
|
klao@946
|
217 |
typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase;
|
klao@946
|
218 |
typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
|
klao@946
|
219 |
typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
|
deba@937
|
220 |
|
alpar@950
|
221 |
///A smart graph class.
|
deba@937
|
222 |
|
alpar@950
|
223 |
///This is a simple and fast graph implementation.
|
alpar@950
|
224 |
///It is also quite memory efficient, but at the price
|
alpar@950
|
225 |
///that <b> it does not support node and edge deletion</b>.
|
alpar@950
|
226 |
///It conforms to
|
klao@959
|
227 |
///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
|
klao@959
|
228 |
///\sa concept::ExtendableGraph.
|
alpar@950
|
229 |
///
|
alpar@950
|
230 |
///\todo Some member functions could be \c static.
|
alpar@950
|
231 |
///
|
alpar@950
|
232 |
///\todo A possibly useful functionality: a function saveState()
|
alpar@950
|
233 |
///(or snapshot() ) would
|
alpar@950
|
234 |
///give back a data sturcture X and then the function restoreState(X)
|
alpar@950
|
235 |
///(or rollBack() )
|
alpar@950
|
236 |
///would remove the nodes and edges added after the call of saveState().
|
alpar@950
|
237 |
///Of course it should be used as a stack. (Maybe X is not necessary.)
|
alpar@950
|
238 |
///
|
alpar@950
|
239 |
///\author Alpar Juttner
|
alpar@969
|
240 |
class SmartGraph :public ClearableSmartGraphBase {
|
alpar@969
|
241 |
public:
|
alpar@969
|
242 |
/// Finds an edge between two nodes.
|
alpar@969
|
243 |
|
alpar@969
|
244 |
/// Finds an edge from node \c u to node \c v.
|
alpar@969
|
245 |
///
|
alpar@969
|
246 |
/// If \c prev is \ref INVALID (this is the default value), then
|
alpar@969
|
247 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for
|
alpar@969
|
248 |
/// the next edge from \c u to \c v after \c prev.
|
alpar@969
|
249 |
/// \return The found edge or \ref INVALID if there is no such an edge.
|
alpar@969
|
250 |
///
|
alpar@969
|
251 |
/// Thus you can iterate through each edge from \c u to \c v as it follows.
|
alpar@969
|
252 |
/// \code
|
alpar@969
|
253 |
/// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
|
alpar@969
|
254 |
/// ...
|
alpar@969
|
255 |
/// }
|
alpar@969
|
256 |
/// \endcode
|
alpar@969
|
257 |
/// \todo Possibly it should be a global function.
|
alpar@969
|
258 |
Edge findEdge(Node u,Node v, Edge prev = INVALID)
|
alpar@969
|
259 |
{
|
alpar@969
|
260 |
return _findEdge(u,v,prev);
|
alpar@969
|
261 |
}
|
alpar@969
|
262 |
};
|
alpar@950
|
263 |
|
klao@946
|
264 |
template <>
|
klao@946
|
265 |
int countNodes<SmartGraph>(const SmartGraph& graph) {
|
klao@946
|
266 |
return graph.nodeNum();
|
klao@946
|
267 |
}
|
deba@937
|
268 |
|
klao@946
|
269 |
template <>
|
klao@946
|
270 |
int countEdges<SmartGraph>(const SmartGraph& graph) {
|
klao@946
|
271 |
return graph.edgeNum();
|
klao@946
|
272 |
}
|
deba@937
|
273 |
|
alpar@407
|
274 |
/// @}
|
alpar@921
|
275 |
} //namespace lemon
|
alpar@104
|
276 |
|
alpar@157
|
277 |
|
alpar@157
|
278 |
|
alpar@157
|
279 |
|
alpar@921
|
280 |
#endif //LEMON_SMART_GRAPH_H
|