0
21
0
2
2
2
2
10
10
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
namespace lemon { |
20 | 20 |
|
21 | 21 |
/** |
22 | 22 |
\page min_cost_flow Minimum Cost Flow Problem |
23 | 23 |
|
24 | 24 |
\section mcf_def Definition (GEQ form) |
25 | 25 |
|
26 | 26 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
27 | 27 |
minimum total cost from a set of supply nodes to a set of demand nodes |
28 | 28 |
in a network with capacity constraints (lower and upper bounds) |
29 | 29 |
and arc costs. |
30 | 30 |
|
31 | 31 |
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$, |
32 | 32 |
\f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and |
33 | 33 |
upper bounds for the flow values on the arcs, for which |
34 | 34 |
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$, |
35 | 35 |
\f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow |
36 | 36 |
on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the |
37 | 37 |
signed supply values of the nodes. |
38 | 38 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
39 | 39 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
40 | 40 |
\f$-sup(u)\f$ demand. |
41 | 41 |
A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution |
42 | 42 |
of the following optimization problem. |
43 | 43 |
|
44 | 44 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
45 | 45 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq |
46 | 46 |
sup(u) \quad \forall u\in V \f] |
47 | 47 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
48 | 48 |
|
49 | 49 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
50 | 50 |
zero or negative in order to have a feasible solution (since the sum |
51 | 51 |
of the expressions on the left-hand side of the inequalities is zero). |
52 | 52 |
It means that the total demand must be greater or equal to the total |
53 | 53 |
supply and all the supplies have to be carried out from the supply nodes, |
54 | 54 |
but there could be demands that are not satisfied. |
55 | 55 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
56 | 56 |
constraints have to be satisfied with equality, i.e. all demands |
57 | 57 |
have to be satisfied and all supplies have to be used. |
58 | 58 |
|
59 | 59 |
|
60 | 60 |
\section mcf_algs Algorithms |
61 | 61 |
|
62 | 62 |
LEMON contains several algorithms for solving this problem, for more |
63 | 63 |
information see \ref min_cost_flow_algs "Minimum Cost Flow Algorithms". |
64 | 64 |
|
65 | 65 |
A feasible solution for this problem can be found using \ref Circulation. |
66 | 66 |
|
67 | 67 |
|
68 | 68 |
\section mcf_dual Dual Solution |
69 | 69 |
|
70 | 70 |
The dual solution of the minimum cost flow problem is represented by |
71 | 71 |
node potentials \f$\pi: V\rightarrow\mathbf{R}\f$. |
72 | 72 |
An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal |
73 | 73 |
if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials |
74 | 74 |
the following \e complementary \e slackness optimality conditions hold. |
75 | 75 |
|
76 | 76 |
- For all \f$uv\in A\f$ arcs: |
77 | 77 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
78 | 78 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
79 | 79 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
80 | 80 |
- For all \f$u\in V\f$ nodes: |
81 |
- \f$\pi(u) |
|
81 |
- \f$\pi(u)\leq 0\f$; |
|
82 | 82 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
83 | 83 |
then \f$\pi(u)=0\f$. |
84 | 84 |
|
85 | 85 |
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc |
86 | 86 |
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e. |
87 | 87 |
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f] |
88 | 88 |
|
89 | 89 |
All algorithms provide dual solution (node potentials), as well, |
90 | 90 |
if an optimal flow is found. |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
\section mcf_eq Equality Form |
94 | 94 |
|
95 | 95 |
The above \ref mcf_def "definition" is actually more general than the |
96 | 96 |
usual formulation of the minimum cost flow problem, in which strict |
97 | 97 |
equalities are required in the supply/demand contraints. |
98 | 98 |
|
99 | 99 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
100 | 100 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) = |
101 | 101 |
sup(u) \quad \forall u\in V \f] |
102 | 102 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
103 | 103 |
|
104 | 104 |
However if the sum of the supply values is zero, then these two problems |
105 | 105 |
are equivalent. |
106 | 106 |
The \ref min_cost_flow_algs "algorithms" in LEMON support the general |
107 | 107 |
form, so if you need the equality form, you have to ensure this additional |
108 | 108 |
contraint manually. |
109 | 109 |
|
110 | 110 |
|
111 | 111 |
\section mcf_leq Opposite Inequalites (LEQ Form) |
112 | 112 |
|
113 | 113 |
Another possible definition of the minimum cost flow problem is |
114 | 114 |
when there are <em>"less or equal"</em> (LEQ) supply/demand constraints, |
115 | 115 |
instead of the <em>"greater or equal"</em> (GEQ) constraints. |
116 | 116 |
|
117 | 117 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
118 | 118 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq |
119 | 119 |
sup(u) \quad \forall u\in V \f] |
120 | 120 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
121 | 121 |
|
122 | 122 |
It means that the total demand must be less or equal to the |
123 | 123 |
total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or |
124 | 124 |
positive) and all the demands have to be satisfied, but there |
125 | 125 |
could be supplies that are not carried out from the supply |
126 | 126 |
nodes. |
127 | 127 |
The equality form is also a special case of this form, of course. |
128 | 128 |
|
129 | 129 |
You could easily transform this case to the \ref mcf_def "GEQ form" |
130 | 130 |
of the problem by reversing the direction of the arcs and taking the |
131 | 131 |
negative of the supply values (e.g. using \ref ReverseDigraph and |
132 | 132 |
\ref NegMap adaptors). |
133 | 133 |
However \ref NetworkSimplex algorithm also supports this form directly |
134 | 134 |
for the sake of convenience. |
135 | 135 |
|
136 | 136 |
Note that the optimality conditions for this supply constraint type are |
137 | 137 |
slightly differ from the conditions that are discussed for the GEQ form, |
138 | 138 |
namely the potentials have to be non-negative instead of non-positive. |
139 | 139 |
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem |
140 | 140 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ |
141 | 141 |
node potentials the following conditions hold. |
142 | 142 |
|
143 | 143 |
- For all \f$uv\in A\f$ arcs: |
144 | 144 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
145 | 145 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
146 | 146 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
147 | 147 |
- For all \f$u\in V\f$ nodes: |
148 |
- \f$\pi(u) |
|
148 |
- \f$\pi(u)\geq 0\f$; |
|
149 | 149 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
150 | 150 |
then \f$\pi(u)=0\f$. |
151 | 151 |
|
152 | 152 |
*/ |
153 | 153 |
} |
... | ... |
@@ -110,385 +110,385 @@ |
110 | 110 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
111 | 111 |
|
112 | 112 |
/// \brief The type of the map that stores the last arcs of the |
113 | 113 |
/// shortest paths. |
114 | 114 |
/// |
115 | 115 |
/// The type of the map that stores the last |
116 | 116 |
/// arcs of the shortest paths. |
117 | 117 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
118 | 118 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
119 | 119 |
|
120 | 120 |
/// \brief Instantiates a \c PredMap. |
121 | 121 |
/// |
122 | 122 |
/// This function instantiates a \ref PredMap. |
123 | 123 |
/// \param g is the digraph to which we would like to define the |
124 | 124 |
/// \ref PredMap. |
125 | 125 |
static PredMap *createPredMap(const GR& g) { |
126 | 126 |
return new PredMap(g); |
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
/// \brief The type of the map that stores the distances of the nodes. |
130 | 130 |
/// |
131 | 131 |
/// The type of the map that stores the distances of the nodes. |
132 | 132 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
133 | 133 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
134 | 134 |
|
135 | 135 |
/// \brief Instantiates a \c DistMap. |
136 | 136 |
/// |
137 | 137 |
/// This function instantiates a \ref DistMap. |
138 | 138 |
/// \param g is the digraph to which we would like to define the |
139 | 139 |
/// \ref DistMap. |
140 | 140 |
static DistMap *createDistMap(const GR& g) { |
141 | 141 |
return new DistMap(g); |
142 | 142 |
} |
143 | 143 |
|
144 | 144 |
}; |
145 | 145 |
|
146 | 146 |
/// \brief %BellmanFord algorithm class. |
147 | 147 |
/// |
148 | 148 |
/// \ingroup shortest_path |
149 | 149 |
/// This class provides an efficient implementation of the Bellman-Ford |
150 | 150 |
/// algorithm. The maximum time complexity of the algorithm is |
151 | 151 |
/// <tt>O(ne)</tt>. |
152 | 152 |
/// |
153 | 153 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
154 | 154 |
/// problem when the arcs can have negative lengths, but the digraph |
155 | 155 |
/// should not contain directed cycles with negative total length. |
156 | 156 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
157 | 157 |
/// algorithm instead, since it is more efficient. |
158 | 158 |
/// |
159 | 159 |
/// The arc lengths are passed to the algorithm using a |
160 | 160 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
161 | 161 |
/// kind of length. The type of the length values is determined by the |
162 | 162 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
163 | 163 |
/// |
164 | 164 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
165 | 165 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
166 | 166 |
/// it can be used easier. |
167 | 167 |
/// |
168 | 168 |
/// \tparam GR The type of the digraph the algorithm runs on. |
169 | 169 |
/// The default type is \ref ListDigraph. |
170 | 170 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
171 | 171 |
/// the lengths of the arcs. The default map type is |
172 | 172 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
173 | 173 |
#ifdef DOXYGEN |
174 | 174 |
template <typename GR, typename LEN, typename TR> |
175 | 175 |
#else |
176 | 176 |
template <typename GR=ListDigraph, |
177 | 177 |
typename LEN=typename GR::template ArcMap<int>, |
178 | 178 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
179 | 179 |
#endif |
180 | 180 |
class BellmanFord { |
181 | 181 |
public: |
182 | 182 |
|
183 | 183 |
///The type of the underlying digraph. |
184 | 184 |
typedef typename TR::Digraph Digraph; |
185 | 185 |
|
186 | 186 |
/// \brief The type of the arc lengths. |
187 | 187 |
typedef typename TR::LengthMap::Value Value; |
188 | 188 |
/// \brief The type of the map that stores the arc lengths. |
189 | 189 |
typedef typename TR::LengthMap LengthMap; |
190 | 190 |
/// \brief The type of the map that stores the last |
191 | 191 |
/// arcs of the shortest paths. |
192 | 192 |
typedef typename TR::PredMap PredMap; |
193 | 193 |
/// \brief The type of the map that stores the distances of the nodes. |
194 | 194 |
typedef typename TR::DistMap DistMap; |
195 | 195 |
/// The type of the paths. |
196 | 196 |
typedef PredMapPath<Digraph, PredMap> Path; |
197 | 197 |
///\brief The \ref BellmanFordDefaultOperationTraits |
198 | 198 |
/// "operation traits class" of the algorithm. |
199 | 199 |
typedef typename TR::OperationTraits OperationTraits; |
200 | 200 |
|
201 | 201 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
202 | 202 |
typedef TR Traits; |
203 | 203 |
|
204 | 204 |
private: |
205 | 205 |
|
206 | 206 |
typedef typename Digraph::Node Node; |
207 | 207 |
typedef typename Digraph::NodeIt NodeIt; |
208 | 208 |
typedef typename Digraph::Arc Arc; |
209 | 209 |
typedef typename Digraph::OutArcIt OutArcIt; |
210 | 210 |
|
211 | 211 |
// Pointer to the underlying digraph. |
212 | 212 |
const Digraph *_gr; |
213 | 213 |
// Pointer to the length map |
214 | 214 |
const LengthMap *_length; |
215 | 215 |
// Pointer to the map of predecessors arcs. |
216 | 216 |
PredMap *_pred; |
217 | 217 |
// Indicates if _pred is locally allocated (true) or not. |
218 | 218 |
bool _local_pred; |
219 | 219 |
// Pointer to the map of distances. |
220 | 220 |
DistMap *_dist; |
221 | 221 |
// Indicates if _dist is locally allocated (true) or not. |
222 | 222 |
bool _local_dist; |
223 | 223 |
|
224 | 224 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
225 | 225 |
MaskMap *_mask; |
226 | 226 |
|
227 | 227 |
std::vector<Node> _process; |
228 | 228 |
|
229 | 229 |
// Creates the maps if necessary. |
230 | 230 |
void create_maps() { |
231 | 231 |
if(!_pred) { |
232 | 232 |
_local_pred = true; |
233 | 233 |
_pred = Traits::createPredMap(*_gr); |
234 | 234 |
} |
235 | 235 |
if(!_dist) { |
236 | 236 |
_local_dist = true; |
237 | 237 |
_dist = Traits::createDistMap(*_gr); |
238 | 238 |
} |
239 | 239 |
_mask = new MaskMap(*_gr, false); |
240 | 240 |
} |
241 | 241 |
|
242 | 242 |
public : |
243 | 243 |
|
244 | 244 |
typedef BellmanFord Create; |
245 | 245 |
|
246 | 246 |
/// \name Named Template Parameters |
247 | 247 |
|
248 | 248 |
///@{ |
249 | 249 |
|
250 | 250 |
template <class T> |
251 | 251 |
struct SetPredMapTraits : public Traits { |
252 | 252 |
typedef T PredMap; |
253 | 253 |
static PredMap *createPredMap(const Digraph&) { |
254 | 254 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
255 | 255 |
return 0; // ignore warnings |
256 | 256 |
} |
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
/// \brief \ref named-templ-param "Named parameter" for setting |
260 | 260 |
/// \c PredMap type. |
261 | 261 |
/// |
262 | 262 |
/// \ref named-templ-param "Named parameter" for setting |
263 | 263 |
/// \c PredMap type. |
264 | 264 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
265 | 265 |
template <class T> |
266 | 266 |
struct SetPredMap |
267 | 267 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > { |
268 | 268 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
269 | 269 |
}; |
270 | 270 |
|
271 | 271 |
template <class T> |
272 | 272 |
struct SetDistMapTraits : public Traits { |
273 | 273 |
typedef T DistMap; |
274 | 274 |
static DistMap *createDistMap(const Digraph&) { |
275 | 275 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
276 | 276 |
return 0; // ignore warnings |
277 | 277 |
} |
278 | 278 |
}; |
279 | 279 |
|
280 | 280 |
/// \brief \ref named-templ-param "Named parameter" for setting |
281 | 281 |
/// \c DistMap type. |
282 | 282 |
/// |
283 | 283 |
/// \ref named-templ-param "Named parameter" for setting |
284 | 284 |
/// \c DistMap type. |
285 | 285 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
286 | 286 |
template <class T> |
287 | 287 |
struct SetDistMap |
288 | 288 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > { |
289 | 289 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
290 | 290 |
}; |
291 | 291 |
|
292 | 292 |
template <class T> |
293 | 293 |
struct SetOperationTraitsTraits : public Traits { |
294 | 294 |
typedef T OperationTraits; |
295 | 295 |
}; |
296 | 296 |
|
297 | 297 |
/// \brief \ref named-templ-param "Named parameter" for setting |
298 | 298 |
/// \c OperationTraits type. |
299 | 299 |
/// |
300 | 300 |
/// \ref named-templ-param "Named parameter" for setting |
301 | 301 |
/// \c OperationTraits type. |
302 |
/// For more information see \ref BellmanFordDefaultOperationTraits. |
|
302 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
|
303 | 303 |
template <class T> |
304 | 304 |
struct SetOperationTraits |
305 | 305 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
306 | 306 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
307 | 307 |
Create; |
308 | 308 |
}; |
309 | 309 |
|
310 | 310 |
///@} |
311 | 311 |
|
312 | 312 |
protected: |
313 | 313 |
|
314 | 314 |
BellmanFord() {} |
315 | 315 |
|
316 | 316 |
public: |
317 | 317 |
|
318 | 318 |
/// \brief Constructor. |
319 | 319 |
/// |
320 | 320 |
/// Constructor. |
321 | 321 |
/// \param g The digraph the algorithm runs on. |
322 | 322 |
/// \param length The length map used by the algorithm. |
323 | 323 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
324 | 324 |
_gr(&g), _length(&length), |
325 | 325 |
_pred(0), _local_pred(false), |
326 | 326 |
_dist(0), _local_dist(false), _mask(0) {} |
327 | 327 |
|
328 | 328 |
///Destructor. |
329 | 329 |
~BellmanFord() { |
330 | 330 |
if(_local_pred) delete _pred; |
331 | 331 |
if(_local_dist) delete _dist; |
332 | 332 |
if(_mask) delete _mask; |
333 | 333 |
} |
334 | 334 |
|
335 | 335 |
/// \brief Sets the length map. |
336 | 336 |
/// |
337 | 337 |
/// Sets the length map. |
338 | 338 |
/// \return <tt>(*this)</tt> |
339 | 339 |
BellmanFord &lengthMap(const LengthMap &map) { |
340 | 340 |
_length = ↦ |
341 | 341 |
return *this; |
342 | 342 |
} |
343 | 343 |
|
344 | 344 |
/// \brief Sets the map that stores the predecessor arcs. |
345 | 345 |
/// |
346 | 346 |
/// Sets the map that stores the predecessor arcs. |
347 | 347 |
/// If you don't use this function before calling \ref run() |
348 | 348 |
/// or \ref init(), an instance will be allocated automatically. |
349 | 349 |
/// The destructor deallocates this automatically allocated map, |
350 | 350 |
/// of course. |
351 | 351 |
/// \return <tt>(*this)</tt> |
352 | 352 |
BellmanFord &predMap(PredMap &map) { |
353 | 353 |
if(_local_pred) { |
354 | 354 |
delete _pred; |
355 | 355 |
_local_pred=false; |
356 | 356 |
} |
357 | 357 |
_pred = ↦ |
358 | 358 |
return *this; |
359 | 359 |
} |
360 | 360 |
|
361 | 361 |
/// \brief Sets the map that stores the distances of the nodes. |
362 | 362 |
/// |
363 | 363 |
/// Sets the map that stores the distances of the nodes calculated |
364 | 364 |
/// by the algorithm. |
365 | 365 |
/// If you don't use this function before calling \ref run() |
366 | 366 |
/// or \ref init(), an instance will be allocated automatically. |
367 | 367 |
/// The destructor deallocates this automatically allocated map, |
368 | 368 |
/// of course. |
369 | 369 |
/// \return <tt>(*this)</tt> |
370 | 370 |
BellmanFord &distMap(DistMap &map) { |
371 | 371 |
if(_local_dist) { |
372 | 372 |
delete _dist; |
373 | 373 |
_local_dist=false; |
374 | 374 |
} |
375 | 375 |
_dist = ↦ |
376 | 376 |
return *this; |
377 | 377 |
} |
378 | 378 |
|
379 | 379 |
/// \name Execution Control |
380 | 380 |
/// The simplest way to execute the Bellman-Ford algorithm is to use |
381 | 381 |
/// one of the member functions called \ref run().\n |
382 | 382 |
/// If you need better control on the execution, you have to call |
383 | 383 |
/// \ref init() first, then you can add several source nodes |
384 | 384 |
/// with \ref addSource(). Finally the actual path computation can be |
385 | 385 |
/// performed with \ref start(), \ref checkedStart() or |
386 | 386 |
/// \ref limitedStart(). |
387 | 387 |
|
388 | 388 |
///@{ |
389 | 389 |
|
390 | 390 |
/// \brief Initializes the internal data structures. |
391 | 391 |
/// |
392 | 392 |
/// Initializes the internal data structures. The optional parameter |
393 | 393 |
/// is the initial distance of each node. |
394 | 394 |
void init(const Value value = OperationTraits::infinity()) { |
395 | 395 |
create_maps(); |
396 | 396 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
397 | 397 |
_pred->set(it, INVALID); |
398 | 398 |
_dist->set(it, value); |
399 | 399 |
} |
400 | 400 |
_process.clear(); |
401 | 401 |
if (OperationTraits::less(value, OperationTraits::infinity())) { |
402 | 402 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
403 | 403 |
_process.push_back(it); |
404 | 404 |
_mask->set(it, true); |
405 | 405 |
} |
406 | 406 |
} |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
/// \brief Adds a new source node. |
410 | 410 |
/// |
411 | 411 |
/// This function adds a new source node. The optional second parameter |
412 | 412 |
/// is the initial distance of the node. |
413 | 413 |
void addSource(Node source, Value dst = OperationTraits::zero()) { |
414 | 414 |
_dist->set(source, dst); |
415 | 415 |
if (!(*_mask)[source]) { |
416 | 416 |
_process.push_back(source); |
417 | 417 |
_mask->set(source, true); |
418 | 418 |
} |
419 | 419 |
} |
420 | 420 |
|
421 | 421 |
/// \brief Executes one round from the Bellman-Ford algorithm. |
422 | 422 |
/// |
423 | 423 |
/// If the algoritm calculated the distances in the previous round |
424 | 424 |
/// exactly for the paths of at most \c k arcs, then this function |
425 | 425 |
/// will calculate the distances exactly for the paths of at most |
426 | 426 |
/// <tt>k+1</tt> arcs. Performing \c k iterations using this function |
427 | 427 |
/// calculates the shortest path distances exactly for the paths |
428 | 428 |
/// consisting of at most \c k arcs. |
429 | 429 |
/// |
430 | 430 |
/// \warning The paths with limited arc number cannot be retrieved |
431 | 431 |
/// easily with \ref path() or \ref predArc() functions. If you also |
432 | 432 |
/// need the shortest paths and not only the distances, you should |
433 | 433 |
/// store the \ref predMap() "predecessor map" after each iteration |
434 | 434 |
/// and build the path manually. |
435 | 435 |
/// |
436 | 436 |
/// \return \c true when the algorithm have not found more shorter |
437 | 437 |
/// paths. |
438 | 438 |
/// |
439 | 439 |
/// \see ActiveIt |
440 | 440 |
bool processNextRound() { |
441 | 441 |
for (int i = 0; i < int(_process.size()); ++i) { |
442 | 442 |
_mask->set(_process[i], false); |
443 | 443 |
} |
444 | 444 |
std::vector<Node> nextProcess; |
445 | 445 |
std::vector<Value> values(_process.size()); |
446 | 446 |
for (int i = 0; i < int(_process.size()); ++i) { |
447 | 447 |
values[i] = (*_dist)[_process[i]]; |
448 | 448 |
} |
449 | 449 |
for (int i = 0; i < int(_process.size()); ++i) { |
450 | 450 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
451 | 451 |
Node target = _gr->target(it); |
452 | 452 |
Value relaxed = OperationTraits::plus(values[i], (*_length)[it]); |
453 | 453 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
454 | 454 |
_pred->set(target, it); |
455 | 455 |
_dist->set(target, relaxed); |
456 | 456 |
if (!(*_mask)[target]) { |
457 | 457 |
_mask->set(target, true); |
458 | 458 |
nextProcess.push_back(target); |
459 | 459 |
} |
460 | 460 |
} |
461 | 461 |
} |
462 | 462 |
} |
463 | 463 |
_process.swap(nextProcess); |
464 | 464 |
return _process.empty(); |
465 | 465 |
} |
466 | 466 |
|
467 | 467 |
/// \brief Executes one weak round from the Bellman-Ford algorithm. |
468 | 468 |
/// |
469 | 469 |
/// If the algorithm calculated the distances in the previous round |
470 | 470 |
/// at least for the paths of at most \c k arcs, then this function |
471 | 471 |
/// will calculate the distances at least for the paths of at most |
472 | 472 |
/// <tt>k+1</tt> arcs. |
473 | 473 |
/// This function does not make it possible to calculate the shortest |
474 | 474 |
/// path distances exactly for paths consisting of at most \c k arcs, |
475 | 475 |
/// this is why it is called weak round. |
476 | 476 |
/// |
477 | 477 |
/// \return \c true when the algorithm have not found more shorter |
478 | 478 |
/// paths. |
479 | 479 |
/// |
480 | 480 |
/// \see ActiveIt |
481 | 481 |
bool processNextWeakRound() { |
482 | 482 |
for (int i = 0; i < int(_process.size()); ++i) { |
483 | 483 |
_mask->set(_process[i], false); |
484 | 484 |
} |
485 | 485 |
std::vector<Node> nextProcess; |
486 | 486 |
for (int i = 0; i < int(_process.size()); ++i) { |
487 | 487 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
488 | 488 |
Node target = _gr->target(it); |
489 | 489 |
Value relaxed = |
490 | 490 |
OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]); |
491 | 491 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
492 | 492 |
_pred->set(target, it); |
493 | 493 |
_dist->set(target, relaxed); |
494 | 494 |
if (!(*_mask)[target]) { |
... | ... |
@@ -528,400 +528,400 @@ |
528 | 528 |
/// |
529 | 529 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
530 | 530 |
/// in order to compute the shortest path to each node and also checks |
531 | 531 |
/// if the digraph contains cycles with negative total length. |
532 | 532 |
/// |
533 | 533 |
/// The algorithm computes |
534 | 534 |
/// - the shortest path tree (forest), |
535 | 535 |
/// - the distance of each node from the root(s). |
536 | 536 |
/// |
537 | 537 |
/// \return \c false if there is a negative cycle in the digraph. |
538 | 538 |
/// |
539 | 539 |
/// \pre init() must be called and at least one root node should be |
540 | 540 |
/// added with addSource() before using this function. |
541 | 541 |
bool checkedStart() { |
542 | 542 |
int num = countNodes(*_gr); |
543 | 543 |
for (int i = 0; i < num; ++i) { |
544 | 544 |
if (processNextWeakRound()) return true; |
545 | 545 |
} |
546 | 546 |
return _process.empty(); |
547 | 547 |
} |
548 | 548 |
|
549 | 549 |
/// \brief Executes the algorithm with arc number limit. |
550 | 550 |
/// |
551 | 551 |
/// Executes the algorithm with arc number limit. |
552 | 552 |
/// |
553 | 553 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
554 | 554 |
/// in order to compute the shortest path distance for each node |
555 | 555 |
/// using only the paths consisting of at most \c num arcs. |
556 | 556 |
/// |
557 | 557 |
/// The algorithm computes |
558 | 558 |
/// - the limited distance of each node from the root(s), |
559 | 559 |
/// - the predecessor arc for each node. |
560 | 560 |
/// |
561 | 561 |
/// \warning The paths with limited arc number cannot be retrieved |
562 | 562 |
/// easily with \ref path() or \ref predArc() functions. If you also |
563 | 563 |
/// need the shortest paths and not only the distances, you should |
564 | 564 |
/// store the \ref predMap() "predecessor map" after each iteration |
565 | 565 |
/// and build the path manually. |
566 | 566 |
/// |
567 | 567 |
/// \pre init() must be called and at least one root node should be |
568 | 568 |
/// added with addSource() before using this function. |
569 | 569 |
void limitedStart(int num) { |
570 | 570 |
for (int i = 0; i < num; ++i) { |
571 | 571 |
if (processNextRound()) break; |
572 | 572 |
} |
573 | 573 |
} |
574 | 574 |
|
575 | 575 |
/// \brief Runs the algorithm from the given root node. |
576 | 576 |
/// |
577 | 577 |
/// This method runs the Bellman-Ford algorithm from the given root |
578 | 578 |
/// node \c s in order to compute the shortest path to each node. |
579 | 579 |
/// |
580 | 580 |
/// The algorithm computes |
581 | 581 |
/// - the shortest path tree (forest), |
582 | 582 |
/// - the distance of each node from the root(s). |
583 | 583 |
/// |
584 | 584 |
/// \note bf.run(s) is just a shortcut of the following code. |
585 | 585 |
/// \code |
586 | 586 |
/// bf.init(); |
587 | 587 |
/// bf.addSource(s); |
588 | 588 |
/// bf.start(); |
589 | 589 |
/// \endcode |
590 | 590 |
void run(Node s) { |
591 | 591 |
init(); |
592 | 592 |
addSource(s); |
593 | 593 |
start(); |
594 | 594 |
} |
595 | 595 |
|
596 | 596 |
/// \brief Runs the algorithm from the given root node with arc |
597 | 597 |
/// number limit. |
598 | 598 |
/// |
599 | 599 |
/// This method runs the Bellman-Ford algorithm from the given root |
600 | 600 |
/// node \c s in order to compute the shortest path distance for each |
601 | 601 |
/// node using only the paths consisting of at most \c num arcs. |
602 | 602 |
/// |
603 | 603 |
/// The algorithm computes |
604 | 604 |
/// - the limited distance of each node from the root(s), |
605 | 605 |
/// - the predecessor arc for each node. |
606 | 606 |
/// |
607 | 607 |
/// \warning The paths with limited arc number cannot be retrieved |
608 | 608 |
/// easily with \ref path() or \ref predArc() functions. If you also |
609 | 609 |
/// need the shortest paths and not only the distances, you should |
610 | 610 |
/// store the \ref predMap() "predecessor map" after each iteration |
611 | 611 |
/// and build the path manually. |
612 | 612 |
/// |
613 | 613 |
/// \note bf.run(s, num) is just a shortcut of the following code. |
614 | 614 |
/// \code |
615 | 615 |
/// bf.init(); |
616 | 616 |
/// bf.addSource(s); |
617 | 617 |
/// bf.limitedStart(num); |
618 | 618 |
/// \endcode |
619 | 619 |
void run(Node s, int num) { |
620 | 620 |
init(); |
621 | 621 |
addSource(s); |
622 | 622 |
limitedStart(num); |
623 | 623 |
} |
624 | 624 |
|
625 | 625 |
///@} |
626 | 626 |
|
627 | 627 |
/// \brief LEMON iterator for getting the active nodes. |
628 | 628 |
/// |
629 | 629 |
/// This class provides a common style LEMON iterator that traverses |
630 | 630 |
/// the active nodes of the Bellman-Ford algorithm after the last |
631 | 631 |
/// phase. These nodes should be checked in the next phase to |
632 | 632 |
/// find augmenting arcs outgoing from them. |
633 | 633 |
class ActiveIt { |
634 | 634 |
public: |
635 | 635 |
|
636 | 636 |
/// \brief Constructor. |
637 | 637 |
/// |
638 | 638 |
/// Constructor for getting the active nodes of the given BellmanFord |
639 | 639 |
/// instance. |
640 | 640 |
ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
641 | 641 |
{ |
642 | 642 |
_index = _algorithm->_process.size() - 1; |
643 | 643 |
} |
644 | 644 |
|
645 | 645 |
/// \brief Invalid constructor. |
646 | 646 |
/// |
647 | 647 |
/// Invalid constructor. |
648 | 648 |
ActiveIt(Invalid) : _algorithm(0), _index(-1) {} |
649 | 649 |
|
650 | 650 |
/// \brief Conversion to \c Node. |
651 | 651 |
/// |
652 | 652 |
/// Conversion to \c Node. |
653 | 653 |
operator Node() const { |
654 | 654 |
return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
655 | 655 |
} |
656 | 656 |
|
657 | 657 |
/// \brief Increment operator. |
658 | 658 |
/// |
659 | 659 |
/// Increment operator. |
660 | 660 |
ActiveIt& operator++() { |
661 | 661 |
--_index; |
662 | 662 |
return *this; |
663 | 663 |
} |
664 | 664 |
|
665 | 665 |
bool operator==(const ActiveIt& it) const { |
666 | 666 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
667 | 667 |
} |
668 | 668 |
bool operator!=(const ActiveIt& it) const { |
669 | 669 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
670 | 670 |
} |
671 | 671 |
bool operator<(const ActiveIt& it) const { |
672 | 672 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
673 | 673 |
} |
674 | 674 |
|
675 | 675 |
private: |
676 | 676 |
const BellmanFord* _algorithm; |
677 | 677 |
int _index; |
678 | 678 |
}; |
679 | 679 |
|
680 | 680 |
/// \name Query Functions |
681 | 681 |
/// The result of the Bellman-Ford algorithm can be obtained using these |
682 | 682 |
/// functions.\n |
683 | 683 |
/// Either \ref run() or \ref init() should be called before using them. |
684 | 684 |
|
685 | 685 |
///@{ |
686 | 686 |
|
687 | 687 |
/// \brief The shortest path to the given node. |
688 | 688 |
/// |
689 | 689 |
/// Gives back the shortest path to the given node from the root(s). |
690 | 690 |
/// |
691 | 691 |
/// \warning \c t should be reached from the root(s). |
692 | 692 |
/// |
693 | 693 |
/// \pre Either \ref run() or \ref init() must be called before |
694 | 694 |
/// using this function. |
695 | 695 |
Path path(Node t) const |
696 | 696 |
{ |
697 | 697 |
return Path(*_gr, *_pred, t); |
698 | 698 |
} |
699 | 699 |
|
700 | 700 |
/// \brief The distance of the given node from the root(s). |
701 | 701 |
/// |
702 | 702 |
/// Returns the distance of the given node from the root(s). |
703 | 703 |
/// |
704 | 704 |
/// \warning If node \c v is not reached from the root(s), then |
705 | 705 |
/// the return value of this function is undefined. |
706 | 706 |
/// |
707 | 707 |
/// \pre Either \ref run() or \ref init() must be called before |
708 | 708 |
/// using this function. |
709 | 709 |
Value dist(Node v) const { return (*_dist)[v]; } |
710 | 710 |
|
711 | 711 |
/// \brief Returns the 'previous arc' of the shortest path tree for |
712 | 712 |
/// the given node. |
713 | 713 |
/// |
714 | 714 |
/// This function returns the 'previous arc' of the shortest path |
715 | 715 |
/// tree for node \c v, i.e. it returns the last arc of a |
716 | 716 |
/// shortest path from a root to \c v. It is \c INVALID if \c v |
717 | 717 |
/// is not reached from the root(s) or if \c v is a root. |
718 | 718 |
/// |
719 | 719 |
/// The shortest path tree used here is equal to the shortest path |
720 |
/// tree used in \ref predNode() and \predMap(). |
|
720 |
/// tree used in \ref predNode() and \ref predMap(). |
|
721 | 721 |
/// |
722 | 722 |
/// \pre Either \ref run() or \ref init() must be called before |
723 | 723 |
/// using this function. |
724 | 724 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
725 | 725 |
|
726 | 726 |
/// \brief Returns the 'previous node' of the shortest path tree for |
727 | 727 |
/// the given node. |
728 | 728 |
/// |
729 | 729 |
/// This function returns the 'previous node' of the shortest path |
730 | 730 |
/// tree for node \c v, i.e. it returns the last but one node of |
731 | 731 |
/// a shortest path from a root to \c v. It is \c INVALID if \c v |
732 | 732 |
/// is not reached from the root(s) or if \c v is a root. |
733 | 733 |
/// |
734 | 734 |
/// The shortest path tree used here is equal to the shortest path |
735 |
/// tree used in \ref predArc() and \predMap(). |
|
735 |
/// tree used in \ref predArc() and \ref predMap(). |
|
736 | 736 |
/// |
737 | 737 |
/// \pre Either \ref run() or \ref init() must be called before |
738 | 738 |
/// using this function. |
739 | 739 |
Node predNode(Node v) const { |
740 | 740 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
741 | 741 |
} |
742 | 742 |
|
743 | 743 |
/// \brief Returns a const reference to the node map that stores the |
744 | 744 |
/// distances of the nodes. |
745 | 745 |
/// |
746 | 746 |
/// Returns a const reference to the node map that stores the distances |
747 | 747 |
/// of the nodes calculated by the algorithm. |
748 | 748 |
/// |
749 | 749 |
/// \pre Either \ref run() or \ref init() must be called before |
750 | 750 |
/// using this function. |
751 | 751 |
const DistMap &distMap() const { return *_dist;} |
752 | 752 |
|
753 | 753 |
/// \brief Returns a const reference to the node map that stores the |
754 | 754 |
/// predecessor arcs. |
755 | 755 |
/// |
756 | 756 |
/// Returns a const reference to the node map that stores the predecessor |
757 | 757 |
/// arcs, which form the shortest path tree (forest). |
758 | 758 |
/// |
759 | 759 |
/// \pre Either \ref run() or \ref init() must be called before |
760 | 760 |
/// using this function. |
761 | 761 |
const PredMap &predMap() const { return *_pred; } |
762 | 762 |
|
763 | 763 |
/// \brief Checks if a node is reached from the root(s). |
764 | 764 |
/// |
765 | 765 |
/// Returns \c true if \c v is reached from the root(s). |
766 | 766 |
/// |
767 | 767 |
/// \pre Either \ref run() or \ref init() must be called before |
768 | 768 |
/// using this function. |
769 | 769 |
bool reached(Node v) const { |
770 | 770 |
return (*_dist)[v] != OperationTraits::infinity(); |
771 | 771 |
} |
772 | 772 |
|
773 | 773 |
/// \brief Gives back a negative cycle. |
774 | 774 |
/// |
775 | 775 |
/// This function gives back a directed cycle with negative total |
776 | 776 |
/// length if the algorithm has already found one. |
777 | 777 |
/// Otherwise it gives back an empty path. |
778 | 778 |
lemon::Path<Digraph> negativeCycle() { |
779 | 779 |
typename Digraph::template NodeMap<int> state(*_gr, -1); |
780 | 780 |
lemon::Path<Digraph> cycle; |
781 | 781 |
for (int i = 0; i < int(_process.size()); ++i) { |
782 | 782 |
if (state[_process[i]] != -1) continue; |
783 | 783 |
for (Node v = _process[i]; (*_pred)[v] != INVALID; |
784 | 784 |
v = _gr->source((*_pred)[v])) { |
785 | 785 |
if (state[v] == i) { |
786 | 786 |
cycle.addFront((*_pred)[v]); |
787 | 787 |
for (Node u = _gr->source((*_pred)[v]); u != v; |
788 | 788 |
u = _gr->source((*_pred)[u])) { |
789 | 789 |
cycle.addFront((*_pred)[u]); |
790 | 790 |
} |
791 | 791 |
return cycle; |
792 | 792 |
} |
793 | 793 |
else if (state[v] >= 0) { |
794 | 794 |
break; |
795 | 795 |
} |
796 | 796 |
state[v] = i; |
797 | 797 |
} |
798 | 798 |
} |
799 | 799 |
return cycle; |
800 | 800 |
} |
801 | 801 |
|
802 | 802 |
///@} |
803 | 803 |
}; |
804 | 804 |
|
805 | 805 |
/// \brief Default traits class of bellmanFord() function. |
806 | 806 |
/// |
807 | 807 |
/// Default traits class of bellmanFord() function. |
808 | 808 |
/// \tparam GR The type of the digraph. |
809 | 809 |
/// \tparam LEN The type of the length map. |
810 | 810 |
template <typename GR, typename LEN> |
811 | 811 |
struct BellmanFordWizardDefaultTraits { |
812 | 812 |
/// The type of the digraph the algorithm runs on. |
813 | 813 |
typedef GR Digraph; |
814 | 814 |
|
815 | 815 |
/// \brief The type of the map that stores the arc lengths. |
816 | 816 |
/// |
817 | 817 |
/// The type of the map that stores the arc lengths. |
818 | 818 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
819 | 819 |
typedef LEN LengthMap; |
820 | 820 |
|
821 | 821 |
/// The type of the arc lengths. |
822 | 822 |
typedef typename LEN::Value Value; |
823 | 823 |
|
824 | 824 |
/// \brief Operation traits for Bellman-Ford algorithm. |
825 | 825 |
/// |
826 | 826 |
/// It defines the used operations and the infinity value for the |
827 | 827 |
/// given \c Value type. |
828 | 828 |
/// \see BellmanFordDefaultOperationTraits |
829 | 829 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
830 | 830 |
|
831 | 831 |
/// \brief The type of the map that stores the last |
832 | 832 |
/// arcs of the shortest paths. |
833 | 833 |
/// |
834 | 834 |
/// The type of the map that stores the last arcs of the shortest paths. |
835 | 835 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
836 | 836 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
837 | 837 |
|
838 | 838 |
/// \brief Instantiates a \c PredMap. |
839 | 839 |
/// |
840 | 840 |
/// This function instantiates a \ref PredMap. |
841 | 841 |
/// \param g is the digraph to which we would like to define the |
842 | 842 |
/// \ref PredMap. |
843 | 843 |
static PredMap *createPredMap(const GR &g) { |
844 | 844 |
return new PredMap(g); |
845 | 845 |
} |
846 | 846 |
|
847 | 847 |
/// \brief The type of the map that stores the distances of the nodes. |
848 | 848 |
/// |
849 | 849 |
/// The type of the map that stores the distances of the nodes. |
850 | 850 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
851 | 851 |
typedef typename GR::template NodeMap<Value> DistMap; |
852 | 852 |
|
853 | 853 |
/// \brief Instantiates a \c DistMap. |
854 | 854 |
/// |
855 | 855 |
/// This function instantiates a \ref DistMap. |
856 | 856 |
/// \param g is the digraph to which we would like to define the |
857 | 857 |
/// \ref DistMap. |
858 | 858 |
static DistMap *createDistMap(const GR &g) { |
859 | 859 |
return new DistMap(g); |
860 | 860 |
} |
861 | 861 |
|
862 | 862 |
///The type of the shortest paths. |
863 | 863 |
|
864 | 864 |
///The type of the shortest paths. |
865 | 865 |
///It must meet the \ref concepts::Path "Path" concept. |
866 | 866 |
typedef lemon::Path<Digraph> Path; |
867 | 867 |
}; |
868 | 868 |
|
869 | 869 |
/// \brief Default traits class used by BellmanFordWizard. |
870 | 870 |
/// |
871 | 871 |
/// Default traits class used by BellmanFordWizard. |
872 | 872 |
/// \tparam GR The type of the digraph. |
873 | 873 |
/// \tparam LEN The type of the length map. |
874 | 874 |
template <typename GR, typename LEN> |
875 | 875 |
class BellmanFordWizardBase |
876 | 876 |
: public BellmanFordWizardDefaultTraits<GR, LEN> { |
877 | 877 |
|
878 | 878 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
879 | 879 |
protected: |
880 | 880 |
// Type of the nodes in the digraph. |
881 | 881 |
typedef typename Base::Digraph::Node Node; |
882 | 882 |
|
883 | 883 |
// Pointer to the underlying digraph. |
884 | 884 |
void *_graph; |
885 | 885 |
// Pointer to the length map |
886 | 886 |
void *_length; |
887 | 887 |
// Pointer to the map of predecessors arcs. |
888 | 888 |
void *_pred; |
889 | 889 |
// Pointer to the map of distances. |
890 | 890 |
void *_dist; |
891 | 891 |
//Pointer to the shortest path to the target node. |
892 | 892 |
void *_path; |
893 | 893 |
//Pointer to the distance of the target node. |
894 | 894 |
void *_di; |
895 | 895 |
|
896 | 896 |
public: |
897 | 897 |
/// Constructor. |
898 | 898 |
|
899 | 899 |
/// This constructor does not require parameters, it initiates |
900 | 900 |
/// all of the attributes to default values \c 0. |
901 | 901 |
BellmanFordWizardBase() : |
902 | 902 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
903 | 903 |
|
904 | 904 |
/// Constructor. |
905 | 905 |
|
906 | 906 |
/// This constructor requires two parameters, |
907 | 907 |
/// others are initiated to \c 0. |
908 | 908 |
/// \param gr The digraph the algorithm runs on. |
909 | 909 |
/// \param len The length map. |
910 | 910 |
BellmanFordWizardBase(const GR& gr, |
911 | 911 |
const LEN& len) : |
912 | 912 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
913 | 913 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
914 | 914 |
_pred(0), _dist(0), _path(0), _di(0) {} |
915 | 915 |
|
916 | 916 |
}; |
917 | 917 |
|
918 | 918 |
/// \brief Auxiliary class for the function-type interface of the |
919 | 919 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
920 | 920 |
/// |
921 | 921 |
/// This auxiliary class is created to implement the |
922 | 922 |
/// \ref bellmanFord() "function-type interface" of the |
923 | 923 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
924 | 924 |
/// It does not have own \ref run() method, it uses the |
925 | 925 |
/// functions and features of the plain \ref BellmanFord. |
926 | 926 |
/// |
927 | 927 |
/// This class should only be used through the \ref bellmanFord() |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief BFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 | 50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
66 |
///By default it is a NullMap. |
|
66 |
///By default, it is a NullMap. |
|
67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
68 | 68 |
///Instantiates a \c ProcessedMap. |
69 | 69 |
|
70 | 70 |
///This function instantiates a \ref ProcessedMap. |
71 | 71 |
///\param g is the digraph, to which |
72 | 72 |
///we would like to define the \ref ProcessedMap |
73 | 73 |
#ifdef DOXYGEN |
74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
75 | 75 |
#else |
76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
77 | 77 |
#endif |
78 | 78 |
{ |
79 | 79 |
return new ProcessedMap(); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 | 87 |
///Instantiates a \c ReachedMap. |
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref ReachedMap. |
90 | 90 |
///\param g is the digraph, to which |
91 | 91 |
///we would like to define the \ref ReachedMap. |
92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
93 | 93 |
{ |
94 | 94 |
return new ReachedMap(g); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
///The type of the map that stores the distances of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the distances of the nodes. |
100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 | 102 |
///Instantiates a \c DistMap. |
103 | 103 |
|
104 | 104 |
///This function instantiates a \ref DistMap. |
105 | 105 |
///\param g is the digraph, to which we would like to define the |
106 | 106 |
///\ref DistMap. |
107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(g); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%BFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 117 |
/// |
118 | 118 |
///There is also a \ref bfs() "function-type interface" for the BFS |
119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 120 |
///used easier. |
121 | 121 |
/// |
122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 123 |
///The default type is \ref ListDigraph. |
124 | 124 |
#ifdef DOXYGEN |
125 | 125 |
template <typename GR, |
126 | 126 |
typename TR> |
127 | 127 |
#else |
128 | 128 |
template <typename GR=ListDigraph, |
129 | 129 |
typename TR=BfsDefaultTraits<GR> > |
130 | 130 |
#endif |
131 | 131 |
class Bfs { |
132 | 132 |
public: |
133 | 133 |
|
134 | 134 |
///The type of the digraph the algorithm runs on. |
135 | 135 |
typedef typename TR::Digraph Digraph; |
136 | 136 |
|
137 | 137 |
///\brief The type of the map that stores the predecessor arcs of the |
138 | 138 |
///shortest paths. |
139 | 139 |
typedef typename TR::PredMap PredMap; |
140 | 140 |
///The type of the map that stores the distances of the nodes. |
141 | 141 |
typedef typename TR::DistMap DistMap; |
142 | 142 |
///The type of the map that indicates which nodes are reached. |
143 | 143 |
typedef typename TR::ReachedMap ReachedMap; |
144 | 144 |
///The type of the map that indicates which nodes are processed. |
145 | 145 |
typedef typename TR::ProcessedMap ProcessedMap; |
146 | 146 |
///The type of the paths. |
147 | 147 |
typedef PredMapPath<Digraph, PredMap> Path; |
148 | 148 |
|
149 | 149 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
150 | 150 |
typedef TR Traits; |
151 | 151 |
|
152 | 152 |
private: |
153 | 153 |
|
154 | 154 |
typedef typename Digraph::Node Node; |
155 | 155 |
typedef typename Digraph::NodeIt NodeIt; |
156 | 156 |
typedef typename Digraph::Arc Arc; |
157 | 157 |
typedef typename Digraph::OutArcIt OutArcIt; |
158 | 158 |
|
159 | 159 |
//Pointer to the underlying digraph. |
160 | 160 |
const Digraph *G; |
161 | 161 |
//Pointer to the map of predecessor arcs. |
162 | 162 |
PredMap *_pred; |
163 | 163 |
//Indicates if _pred is locally allocated (true) or not. |
164 | 164 |
bool local_pred; |
165 | 165 |
//Pointer to the map of distances. |
166 | 166 |
DistMap *_dist; |
167 | 167 |
//Indicates if _dist is locally allocated (true) or not. |
168 | 168 |
bool local_dist; |
169 | 169 |
//Pointer to the map of reached status of the nodes. |
170 | 170 |
ReachedMap *_reached; |
171 | 171 |
//Indicates if _reached is locally allocated (true) or not. |
172 | 172 |
bool local_reached; |
173 | 173 |
//Pointer to the map of processed status of the nodes. |
174 | 174 |
ProcessedMap *_processed; |
175 | 175 |
//Indicates if _processed is locally allocated (true) or not. |
176 | 176 |
bool local_processed; |
177 | 177 |
|
178 | 178 |
std::vector<typename Digraph::Node> _queue; |
179 | 179 |
int _queue_head,_queue_tail,_queue_next_dist; |
180 | 180 |
int _curr_dist; |
181 | 181 |
|
182 | 182 |
//Creates the maps if necessary. |
183 | 183 |
void create_maps() |
184 | 184 |
{ |
185 | 185 |
if(!_pred) { |
186 | 186 |
local_pred = true; |
187 | 187 |
_pred = Traits::createPredMap(*G); |
188 | 188 |
} |
189 | 189 |
if(!_dist) { |
190 | 190 |
local_dist = true; |
191 | 191 |
_dist = Traits::createDistMap(*G); |
192 | 192 |
} |
193 | 193 |
if(!_reached) { |
194 | 194 |
local_reached = true; |
195 | 195 |
_reached = Traits::createReachedMap(*G); |
196 | 196 |
} |
197 | 197 |
if(!_processed) { |
198 | 198 |
local_processed = true; |
199 | 199 |
_processed = Traits::createProcessedMap(*G); |
200 | 200 |
} |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
protected: |
204 | 204 |
|
205 | 205 |
Bfs() {} |
206 | 206 |
|
207 | 207 |
public: |
208 | 208 |
|
209 | 209 |
typedef Bfs Create; |
210 | 210 |
|
211 | 211 |
///\name Named Template Parameters |
212 | 212 |
|
213 | 213 |
///@{ |
214 | 214 |
|
215 | 215 |
template <class T> |
216 | 216 |
struct SetPredMapTraits : public Traits { |
217 | 217 |
typedef T PredMap; |
218 | 218 |
static PredMap *createPredMap(const Digraph &) |
219 | 219 |
{ |
220 | 220 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
221 | 221 |
return 0; // ignore warnings |
222 | 222 |
} |
223 | 223 |
}; |
224 | 224 |
///\brief \ref named-templ-param "Named parameter" for setting |
225 | 225 |
///\c PredMap type. |
226 | 226 |
/// |
227 | 227 |
///\ref named-templ-param "Named parameter" for setting |
228 | 228 |
///\c PredMap type. |
229 | 229 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
230 | 230 |
template <class T> |
231 | 231 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
232 | 232 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
233 | 233 |
}; |
234 | 234 |
|
235 | 235 |
template <class T> |
236 | 236 |
struct SetDistMapTraits : public Traits { |
237 | 237 |
typedef T DistMap; |
238 | 238 |
static DistMap *createDistMap(const Digraph &) |
239 | 239 |
{ |
240 | 240 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
241 | 241 |
return 0; // ignore warnings |
242 | 242 |
} |
243 | 243 |
}; |
244 | 244 |
///\brief \ref named-templ-param "Named parameter" for setting |
245 | 245 |
///\c DistMap type. |
246 | 246 |
/// |
247 | 247 |
///\ref named-templ-param "Named parameter" for setting |
248 | 248 |
///\c DistMap type. |
249 | 249 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
250 | 250 |
template <class T> |
251 | 251 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
252 | 252 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
253 | 253 |
}; |
254 | 254 |
|
255 | 255 |
template <class T> |
256 | 256 |
struct SetReachedMapTraits : public Traits { |
257 | 257 |
typedef T ReachedMap; |
258 | 258 |
static ReachedMap *createReachedMap(const Digraph &) |
... | ... |
@@ -663,385 +663,385 @@ |
663 | 663 |
/// |
664 | 664 |
///The algorithm computes |
665 | 665 |
///- the shortest path tree, |
666 | 666 |
///- the distance of each node from the root. |
667 | 667 |
/// |
668 | 668 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
669 | 669 |
///\code |
670 | 670 |
/// b.init(); |
671 | 671 |
/// b.addSource(s); |
672 | 672 |
/// b.start(); |
673 | 673 |
///\endcode |
674 | 674 |
void run(Node s) { |
675 | 675 |
init(); |
676 | 676 |
addSource(s); |
677 | 677 |
start(); |
678 | 678 |
} |
679 | 679 |
|
680 | 680 |
///Finds the shortest path between \c s and \c t. |
681 | 681 |
|
682 | 682 |
///This method runs the %BFS algorithm from node \c s |
683 | 683 |
///in order to compute the shortest path to node \c t |
684 | 684 |
///(it stops searching when \c t is processed). |
685 | 685 |
/// |
686 | 686 |
///\return \c true if \c t is reachable form \c s. |
687 | 687 |
/// |
688 | 688 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
689 | 689 |
///shortcut of the following code. |
690 | 690 |
///\code |
691 | 691 |
/// b.init(); |
692 | 692 |
/// b.addSource(s); |
693 | 693 |
/// b.start(t); |
694 | 694 |
///\endcode |
695 | 695 |
bool run(Node s,Node t) { |
696 | 696 |
init(); |
697 | 697 |
addSource(s); |
698 | 698 |
start(t); |
699 | 699 |
return reached(t); |
700 | 700 |
} |
701 | 701 |
|
702 | 702 |
///Runs the algorithm to visit all nodes in the digraph. |
703 | 703 |
|
704 | 704 |
///This method runs the %BFS algorithm in order to |
705 | 705 |
///compute the shortest path to each node. |
706 | 706 |
/// |
707 | 707 |
///The algorithm computes |
708 | 708 |
///- the shortest path tree (forest), |
709 | 709 |
///- the distance of each node from the root(s). |
710 | 710 |
/// |
711 | 711 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
712 | 712 |
///\code |
713 | 713 |
/// b.init(); |
714 | 714 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
715 | 715 |
/// if (!b.reached(n)) { |
716 | 716 |
/// b.addSource(n); |
717 | 717 |
/// b.start(); |
718 | 718 |
/// } |
719 | 719 |
/// } |
720 | 720 |
///\endcode |
721 | 721 |
void run() { |
722 | 722 |
init(); |
723 | 723 |
for (NodeIt n(*G); n != INVALID; ++n) { |
724 | 724 |
if (!reached(n)) { |
725 | 725 |
addSource(n); |
726 | 726 |
start(); |
727 | 727 |
} |
728 | 728 |
} |
729 | 729 |
} |
730 | 730 |
|
731 | 731 |
///@} |
732 | 732 |
|
733 | 733 |
///\name Query Functions |
734 | 734 |
///The results of the BFS algorithm can be obtained using these |
735 | 735 |
///functions.\n |
736 | 736 |
///Either \ref run(Node) "run()" or \ref start() should be called |
737 | 737 |
///before using them. |
738 | 738 |
|
739 | 739 |
///@{ |
740 | 740 |
|
741 | 741 |
///The shortest path to the given node. |
742 | 742 |
|
743 | 743 |
///Returns the shortest path to the given node from the root(s). |
744 | 744 |
/// |
745 | 745 |
///\warning \c t should be reached from the root(s). |
746 | 746 |
/// |
747 | 747 |
///\pre Either \ref run(Node) "run()" or \ref init() |
748 | 748 |
///must be called before using this function. |
749 | 749 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
750 | 750 |
|
751 | 751 |
///The distance of the given node from the root(s). |
752 | 752 |
|
753 | 753 |
///Returns the distance of the given node from the root(s). |
754 | 754 |
/// |
755 | 755 |
///\warning If node \c v is not reached from the root(s), then |
756 | 756 |
///the return value of this function is undefined. |
757 | 757 |
/// |
758 | 758 |
///\pre Either \ref run(Node) "run()" or \ref init() |
759 | 759 |
///must be called before using this function. |
760 | 760 |
int dist(Node v) const { return (*_dist)[v]; } |
761 | 761 |
|
762 | 762 |
///\brief Returns the 'previous arc' of the shortest path tree for |
763 | 763 |
///the given node. |
764 | 764 |
/// |
765 | 765 |
///This function returns the 'previous arc' of the shortest path |
766 | 766 |
///tree for the node \c v, i.e. it returns the last arc of a |
767 | 767 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
768 | 768 |
///is not reached from the root(s) or if \c v is a root. |
769 | 769 |
/// |
770 | 770 |
///The shortest path tree used here is equal to the shortest path |
771 | 771 |
///tree used in \ref predNode() and \ref predMap(). |
772 | 772 |
/// |
773 | 773 |
///\pre Either \ref run(Node) "run()" or \ref init() |
774 | 774 |
///must be called before using this function. |
775 | 775 |
Arc predArc(Node v) const { return (*_pred)[v];} |
776 | 776 |
|
777 | 777 |
///\brief Returns the 'previous node' of the shortest path tree for |
778 | 778 |
///the given node. |
779 | 779 |
/// |
780 | 780 |
///This function returns the 'previous node' of the shortest path |
781 | 781 |
///tree for the node \c v, i.e. it returns the last but one node |
782 | 782 |
///of a shortest path from a root to \c v. It is \c INVALID |
783 | 783 |
///if \c v is not reached from the root(s) or if \c v is a root. |
784 | 784 |
/// |
785 | 785 |
///The shortest path tree used here is equal to the shortest path |
786 | 786 |
///tree used in \ref predArc() and \ref predMap(). |
787 | 787 |
/// |
788 | 788 |
///\pre Either \ref run(Node) "run()" or \ref init() |
789 | 789 |
///must be called before using this function. |
790 | 790 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
791 | 791 |
G->source((*_pred)[v]); } |
792 | 792 |
|
793 | 793 |
///\brief Returns a const reference to the node map that stores the |
794 | 794 |
/// distances of the nodes. |
795 | 795 |
/// |
796 | 796 |
///Returns a const reference to the node map that stores the distances |
797 | 797 |
///of the nodes calculated by the algorithm. |
798 | 798 |
/// |
799 | 799 |
///\pre Either \ref run(Node) "run()" or \ref init() |
800 | 800 |
///must be called before using this function. |
801 | 801 |
const DistMap &distMap() const { return *_dist;} |
802 | 802 |
|
803 | 803 |
///\brief Returns a const reference to the node map that stores the |
804 | 804 |
///predecessor arcs. |
805 | 805 |
/// |
806 | 806 |
///Returns a const reference to the node map that stores the predecessor |
807 | 807 |
///arcs, which form the shortest path tree (forest). |
808 | 808 |
/// |
809 | 809 |
///\pre Either \ref run(Node) "run()" or \ref init() |
810 | 810 |
///must be called before using this function. |
811 | 811 |
const PredMap &predMap() const { return *_pred;} |
812 | 812 |
|
813 | 813 |
///Checks if the given node is reached from the root(s). |
814 | 814 |
|
815 | 815 |
///Returns \c true if \c v is reached from the root(s). |
816 | 816 |
/// |
817 | 817 |
///\pre Either \ref run(Node) "run()" or \ref init() |
818 | 818 |
///must be called before using this function. |
819 | 819 |
bool reached(Node v) const { return (*_reached)[v]; } |
820 | 820 |
|
821 | 821 |
///@} |
822 | 822 |
}; |
823 | 823 |
|
824 | 824 |
///Default traits class of bfs() function. |
825 | 825 |
|
826 | 826 |
///Default traits class of bfs() function. |
827 | 827 |
///\tparam GR Digraph type. |
828 | 828 |
template<class GR> |
829 | 829 |
struct BfsWizardDefaultTraits |
830 | 830 |
{ |
831 | 831 |
///The type of the digraph the algorithm runs on. |
832 | 832 |
typedef GR Digraph; |
833 | 833 |
|
834 | 834 |
///\brief The type of the map that stores the predecessor |
835 | 835 |
///arcs of the shortest paths. |
836 | 836 |
/// |
837 | 837 |
///The type of the map that stores the predecessor |
838 | 838 |
///arcs of the shortest paths. |
839 | 839 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
840 | 840 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
841 | 841 |
///Instantiates a PredMap. |
842 | 842 |
|
843 | 843 |
///This function instantiates a PredMap. |
844 | 844 |
///\param g is the digraph, to which we would like to define the |
845 | 845 |
///PredMap. |
846 | 846 |
static PredMap *createPredMap(const Digraph &g) |
847 | 847 |
{ |
848 | 848 |
return new PredMap(g); |
849 | 849 |
} |
850 | 850 |
|
851 | 851 |
///The type of the map that indicates which nodes are processed. |
852 | 852 |
|
853 | 853 |
///The type of the map that indicates which nodes are processed. |
854 | 854 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
855 |
///By default it is a NullMap. |
|
855 |
///By default, it is a NullMap. |
|
856 | 856 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
857 | 857 |
///Instantiates a ProcessedMap. |
858 | 858 |
|
859 | 859 |
///This function instantiates a ProcessedMap. |
860 | 860 |
///\param g is the digraph, to which |
861 | 861 |
///we would like to define the ProcessedMap. |
862 | 862 |
#ifdef DOXYGEN |
863 | 863 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
864 | 864 |
#else |
865 | 865 |
static ProcessedMap *createProcessedMap(const Digraph &) |
866 | 866 |
#endif |
867 | 867 |
{ |
868 | 868 |
return new ProcessedMap(); |
869 | 869 |
} |
870 | 870 |
|
871 | 871 |
///The type of the map that indicates which nodes are reached. |
872 | 872 |
|
873 | 873 |
///The type of the map that indicates which nodes are reached. |
874 | 874 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
875 | 875 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
876 | 876 |
///Instantiates a ReachedMap. |
877 | 877 |
|
878 | 878 |
///This function instantiates a ReachedMap. |
879 | 879 |
///\param g is the digraph, to which |
880 | 880 |
///we would like to define the ReachedMap. |
881 | 881 |
static ReachedMap *createReachedMap(const Digraph &g) |
882 | 882 |
{ |
883 | 883 |
return new ReachedMap(g); |
884 | 884 |
} |
885 | 885 |
|
886 | 886 |
///The type of the map that stores the distances of the nodes. |
887 | 887 |
|
888 | 888 |
///The type of the map that stores the distances of the nodes. |
889 | 889 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
890 | 890 |
typedef typename Digraph::template NodeMap<int> DistMap; |
891 | 891 |
///Instantiates a DistMap. |
892 | 892 |
|
893 | 893 |
///This function instantiates a DistMap. |
894 | 894 |
///\param g is the digraph, to which we would like to define |
895 | 895 |
///the DistMap |
896 | 896 |
static DistMap *createDistMap(const Digraph &g) |
897 | 897 |
{ |
898 | 898 |
return new DistMap(g); |
899 | 899 |
} |
900 | 900 |
|
901 | 901 |
///The type of the shortest paths. |
902 | 902 |
|
903 | 903 |
///The type of the shortest paths. |
904 | 904 |
///It must conform to the \ref concepts::Path "Path" concept. |
905 | 905 |
typedef lemon::Path<Digraph> Path; |
906 | 906 |
}; |
907 | 907 |
|
908 | 908 |
/// Default traits class used by BfsWizard |
909 | 909 |
|
910 | 910 |
/// Default traits class used by BfsWizard. |
911 | 911 |
/// \tparam GR The type of the digraph. |
912 | 912 |
template<class GR> |
913 | 913 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
914 | 914 |
{ |
915 | 915 |
|
916 | 916 |
typedef BfsWizardDefaultTraits<GR> Base; |
917 | 917 |
protected: |
918 | 918 |
//The type of the nodes in the digraph. |
919 | 919 |
typedef typename Base::Digraph::Node Node; |
920 | 920 |
|
921 | 921 |
//Pointer to the digraph the algorithm runs on. |
922 | 922 |
void *_g; |
923 | 923 |
//Pointer to the map of reached nodes. |
924 | 924 |
void *_reached; |
925 | 925 |
//Pointer to the map of processed nodes. |
926 | 926 |
void *_processed; |
927 | 927 |
//Pointer to the map of predecessors arcs. |
928 | 928 |
void *_pred; |
929 | 929 |
//Pointer to the map of distances. |
930 | 930 |
void *_dist; |
931 | 931 |
//Pointer to the shortest path to the target node. |
932 | 932 |
void *_path; |
933 | 933 |
//Pointer to the distance of the target node. |
934 | 934 |
int *_di; |
935 | 935 |
|
936 | 936 |
public: |
937 | 937 |
/// Constructor. |
938 | 938 |
|
939 | 939 |
/// This constructor does not require parameters, it initiates |
940 | 940 |
/// all of the attributes to \c 0. |
941 | 941 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
942 | 942 |
_dist(0), _path(0), _di(0) {} |
943 | 943 |
|
944 | 944 |
/// Constructor. |
945 | 945 |
|
946 | 946 |
/// This constructor requires one parameter, |
947 | 947 |
/// others are initiated to \c 0. |
948 | 948 |
/// \param g The digraph the algorithm runs on. |
949 | 949 |
BfsWizardBase(const GR &g) : |
950 | 950 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
951 | 951 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
952 | 952 |
|
953 | 953 |
}; |
954 | 954 |
|
955 | 955 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
956 | 956 |
|
957 | 957 |
/// This auxiliary class is created to implement the |
958 | 958 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
959 | 959 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
960 | 960 |
/// functions and features of the plain \ref Bfs. |
961 | 961 |
/// |
962 | 962 |
/// This class should only be used through the \ref bfs() function, |
963 | 963 |
/// which makes it easier to use the algorithm. |
964 | 964 |
template<class TR> |
965 | 965 |
class BfsWizard : public TR |
966 | 966 |
{ |
967 | 967 |
typedef TR Base; |
968 | 968 |
|
969 | 969 |
typedef typename TR::Digraph Digraph; |
970 | 970 |
|
971 | 971 |
typedef typename Digraph::Node Node; |
972 | 972 |
typedef typename Digraph::NodeIt NodeIt; |
973 | 973 |
typedef typename Digraph::Arc Arc; |
974 | 974 |
typedef typename Digraph::OutArcIt OutArcIt; |
975 | 975 |
|
976 | 976 |
typedef typename TR::PredMap PredMap; |
977 | 977 |
typedef typename TR::DistMap DistMap; |
978 | 978 |
typedef typename TR::ReachedMap ReachedMap; |
979 | 979 |
typedef typename TR::ProcessedMap ProcessedMap; |
980 | 980 |
typedef typename TR::Path Path; |
981 | 981 |
|
982 | 982 |
public: |
983 | 983 |
|
984 | 984 |
/// Constructor. |
985 | 985 |
BfsWizard() : TR() {} |
986 | 986 |
|
987 | 987 |
/// Constructor that requires parameters. |
988 | 988 |
|
989 | 989 |
/// Constructor that requires parameters. |
990 | 990 |
/// These parameters will be the default values for the traits class. |
991 | 991 |
/// \param g The digraph the algorithm runs on. |
992 | 992 |
BfsWizard(const Digraph &g) : |
993 | 993 |
TR(g) {} |
994 | 994 |
|
995 | 995 |
///Copy constructor |
996 | 996 |
BfsWizard(const TR &b) : TR(b) {} |
997 | 997 |
|
998 | 998 |
~BfsWizard() {} |
999 | 999 |
|
1000 | 1000 |
///Runs BFS algorithm from the given source node. |
1001 | 1001 |
|
1002 | 1002 |
///This method runs BFS algorithm from node \c s |
1003 | 1003 |
///in order to compute the shortest path to each node. |
1004 | 1004 |
void run(Node s) |
1005 | 1005 |
{ |
1006 | 1006 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1007 | 1007 |
if (Base::_pred) |
1008 | 1008 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1009 | 1009 |
if (Base::_dist) |
1010 | 1010 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1011 | 1011 |
if (Base::_reached) |
1012 | 1012 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1013 | 1013 |
if (Base::_processed) |
1014 | 1014 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1015 | 1015 |
if (s!=INVALID) |
1016 | 1016 |
alg.run(s); |
1017 | 1017 |
else |
1018 | 1018 |
alg.run(); |
1019 | 1019 |
} |
1020 | 1020 |
|
1021 | 1021 |
///Finds the shortest path between \c s and \c t. |
1022 | 1022 |
|
1023 | 1023 |
///This method runs BFS algorithm from node \c s |
1024 | 1024 |
///in order to compute the shortest path to node \c t |
1025 | 1025 |
///(it stops searching when \c t is processed). |
1026 | 1026 |
/// |
1027 | 1027 |
///\return \c true if \c t is reachable form \c s. |
1028 | 1028 |
bool run(Node s, Node t) |
1029 | 1029 |
{ |
1030 | 1030 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1031 | 1031 |
if (Base::_pred) |
1032 | 1032 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1033 | 1033 |
if (Base::_dist) |
1034 | 1034 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1035 | 1035 |
if (Base::_reached) |
1036 | 1036 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1037 | 1037 |
if (Base::_processed) |
1038 | 1038 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1039 | 1039 |
alg.run(s,t); |
1040 | 1040 |
if (Base::_path) |
1041 | 1041 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
1042 | 1042 |
if (Base::_di) |
1043 | 1043 |
*Base::_di = alg.dist(t); |
1044 | 1044 |
return alg.reached(t); |
1045 | 1045 |
} |
1046 | 1046 |
|
1047 | 1047 |
///Runs BFS algorithm to visit all nodes in the digraph. |
... | ... |
@@ -117,385 +117,385 @@ |
117 | 117 |
|
118 | 118 |
/** |
119 | 119 |
\brief Push-relabel algorithm for the network circulation problem. |
120 | 120 |
|
121 | 121 |
\ingroup max_flow |
122 | 122 |
This class implements a push-relabel algorithm for the \e network |
123 | 123 |
\e circulation problem. |
124 | 124 |
It is to find a feasible circulation when lower and upper bounds |
125 | 125 |
are given for the flow values on the arcs and lower bounds are |
126 | 126 |
given for the difference between the outgoing and incoming flow |
127 | 127 |
at the nodes. |
128 | 128 |
|
129 | 129 |
The exact formulation of this problem is the following. |
130 | 130 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$ |
131 | 131 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and |
132 | 132 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
133 | 133 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$ |
134 | 134 |
denotes the signed supply values of the nodes. |
135 | 135 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
136 | 136 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
137 | 137 |
\f$-sup(u)\f$ demand. |
138 | 138 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$ |
139 | 139 |
solution of the following problem. |
140 | 140 |
|
141 | 141 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) |
142 | 142 |
\geq sup(u) \quad \forall u\in V, \f] |
143 | 143 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
144 | 144 |
|
145 | 145 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
146 | 146 |
zero or negative in order to have a feasible solution (since the sum |
147 | 147 |
of the expressions on the left-hand side of the inequalities is zero). |
148 | 148 |
It means that the total demand must be greater or equal to the total |
149 | 149 |
supply and all the supplies have to be carried out from the supply nodes, |
150 | 150 |
but there could be demands that are not satisfied. |
151 | 151 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
152 | 152 |
constraints have to be satisfied with equality, i.e. all demands |
153 | 153 |
have to be satisfied and all supplies have to be used. |
154 | 154 |
|
155 | 155 |
If you need the opposite inequalities in the supply/demand constraints |
156 | 156 |
(i.e. the total demand is less than the total supply and all the demands |
157 | 157 |
have to be satisfied while there could be supplies that are not used), |
158 | 158 |
then you could easily transform the problem to the above form by reversing |
159 | 159 |
the direction of the arcs and taking the negative of the supply values |
160 | 160 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
161 | 161 |
|
162 | 162 |
This algorithm either calculates a feasible circulation, or provides |
163 | 163 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
164 | 164 |
cannot exist. |
165 | 165 |
|
166 | 166 |
Note that this algorithm also provides a feasible solution for the |
167 | 167 |
\ref min_cost_flow "minimum cost flow problem". |
168 | 168 |
|
169 | 169 |
\tparam GR The type of the digraph the algorithm runs on. |
170 | 170 |
\tparam LM The type of the lower bound map. The default |
171 | 171 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
172 | 172 |
\tparam UM The type of the upper bound (capacity) map. |
173 | 173 |
The default map type is \c LM. |
174 | 174 |
\tparam SM The type of the supply map. The default map type is |
175 | 175 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
176 | 176 |
*/ |
177 | 177 |
#ifdef DOXYGEN |
178 | 178 |
template< typename GR, |
179 | 179 |
typename LM, |
180 | 180 |
typename UM, |
181 | 181 |
typename SM, |
182 | 182 |
typename TR > |
183 | 183 |
#else |
184 | 184 |
template< typename GR, |
185 | 185 |
typename LM = typename GR::template ArcMap<int>, |
186 | 186 |
typename UM = LM, |
187 | 187 |
typename SM = typename GR::template NodeMap<typename UM::Value>, |
188 | 188 |
typename TR = CirculationDefaultTraits<GR, LM, UM, SM> > |
189 | 189 |
#endif |
190 | 190 |
class Circulation { |
191 | 191 |
public: |
192 | 192 |
|
193 | 193 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
194 | 194 |
typedef TR Traits; |
195 | 195 |
///The type of the digraph the algorithm runs on. |
196 | 196 |
typedef typename Traits::Digraph Digraph; |
197 | 197 |
///The type of the flow and supply values. |
198 | 198 |
typedef typename Traits::Value Value; |
199 | 199 |
|
200 | 200 |
///The type of the lower bound map. |
201 | 201 |
typedef typename Traits::LowerMap LowerMap; |
202 | 202 |
///The type of the upper bound (capacity) map. |
203 | 203 |
typedef typename Traits::UpperMap UpperMap; |
204 | 204 |
///The type of the supply map. |
205 | 205 |
typedef typename Traits::SupplyMap SupplyMap; |
206 | 206 |
///The type of the flow map. |
207 | 207 |
typedef typename Traits::FlowMap FlowMap; |
208 | 208 |
|
209 | 209 |
///The type of the elevator. |
210 | 210 |
typedef typename Traits::Elevator Elevator; |
211 | 211 |
///The type of the tolerance. |
212 | 212 |
typedef typename Traits::Tolerance Tolerance; |
213 | 213 |
|
214 | 214 |
private: |
215 | 215 |
|
216 | 216 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
217 | 217 |
|
218 | 218 |
const Digraph &_g; |
219 | 219 |
int _node_num; |
220 | 220 |
|
221 | 221 |
const LowerMap *_lo; |
222 | 222 |
const UpperMap *_up; |
223 | 223 |
const SupplyMap *_supply; |
224 | 224 |
|
225 | 225 |
FlowMap *_flow; |
226 | 226 |
bool _local_flow; |
227 | 227 |
|
228 | 228 |
Elevator* _level; |
229 | 229 |
bool _local_level; |
230 | 230 |
|
231 | 231 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
232 | 232 |
ExcessMap* _excess; |
233 | 233 |
|
234 | 234 |
Tolerance _tol; |
235 | 235 |
int _el; |
236 | 236 |
|
237 | 237 |
public: |
238 | 238 |
|
239 | 239 |
typedef Circulation Create; |
240 | 240 |
|
241 | 241 |
///\name Named Template Parameters |
242 | 242 |
|
243 | 243 |
///@{ |
244 | 244 |
|
245 | 245 |
template <typename T> |
246 | 246 |
struct SetFlowMapTraits : public Traits { |
247 | 247 |
typedef T FlowMap; |
248 | 248 |
static FlowMap *createFlowMap(const Digraph&) { |
249 | 249 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
250 | 250 |
return 0; // ignore warnings |
251 | 251 |
} |
252 | 252 |
}; |
253 | 253 |
|
254 | 254 |
/// \brief \ref named-templ-param "Named parameter" for setting |
255 | 255 |
/// FlowMap type |
256 | 256 |
/// |
257 | 257 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
258 | 258 |
/// type. |
259 | 259 |
template <typename T> |
260 | 260 |
struct SetFlowMap |
261 | 261 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
262 | 262 |
SetFlowMapTraits<T> > { |
263 | 263 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
264 | 264 |
SetFlowMapTraits<T> > Create; |
265 | 265 |
}; |
266 | 266 |
|
267 | 267 |
template <typename T> |
268 | 268 |
struct SetElevatorTraits : public Traits { |
269 | 269 |
typedef T Elevator; |
270 | 270 |
static Elevator *createElevator(const Digraph&, int) { |
271 | 271 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
272 | 272 |
return 0; // ignore warnings |
273 | 273 |
} |
274 | 274 |
}; |
275 | 275 |
|
276 | 276 |
/// \brief \ref named-templ-param "Named parameter" for setting |
277 | 277 |
/// Elevator type |
278 | 278 |
/// |
279 | 279 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
280 | 280 |
/// type. If this named parameter is used, then an external |
281 | 281 |
/// elevator object must be passed to the algorithm using the |
282 | 282 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
283 | 283 |
/// \ref run() or \ref init(). |
284 | 284 |
/// \sa SetStandardElevator |
285 | 285 |
template <typename T> |
286 | 286 |
struct SetElevator |
287 | 287 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
288 | 288 |
SetElevatorTraits<T> > { |
289 | 289 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
290 | 290 |
SetElevatorTraits<T> > Create; |
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
template <typename T> |
294 | 294 |
struct SetStandardElevatorTraits : public Traits { |
295 | 295 |
typedef T Elevator; |
296 | 296 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
297 | 297 |
return new Elevator(digraph, max_level); |
298 | 298 |
} |
299 | 299 |
}; |
300 | 300 |
|
301 | 301 |
/// \brief \ref named-templ-param "Named parameter" for setting |
302 | 302 |
/// Elevator type with automatic allocation |
303 | 303 |
/// |
304 | 304 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
305 | 305 |
/// type with automatic allocation. |
306 | 306 |
/// The Elevator should have standard constructor interface to be |
307 | 307 |
/// able to automatically created by the algorithm (i.e. the |
308 | 308 |
/// digraph and the maximum level should be passed to it). |
309 |
/// However an external elevator object could also be passed to the |
|
309 |
/// However, an external elevator object could also be passed to the |
|
310 | 310 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
311 | 311 |
/// before calling \ref run() or \ref init(). |
312 | 312 |
/// \sa SetElevator |
313 | 313 |
template <typename T> |
314 | 314 |
struct SetStandardElevator |
315 | 315 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
316 | 316 |
SetStandardElevatorTraits<T> > { |
317 | 317 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
318 | 318 |
SetStandardElevatorTraits<T> > Create; |
319 | 319 |
}; |
320 | 320 |
|
321 | 321 |
/// @} |
322 | 322 |
|
323 | 323 |
protected: |
324 | 324 |
|
325 | 325 |
Circulation() {} |
326 | 326 |
|
327 | 327 |
public: |
328 | 328 |
|
329 | 329 |
/// Constructor. |
330 | 330 |
|
331 | 331 |
/// The constructor of the class. |
332 | 332 |
/// |
333 | 333 |
/// \param graph The digraph the algorithm runs on. |
334 | 334 |
/// \param lower The lower bounds for the flow values on the arcs. |
335 | 335 |
/// \param upper The upper bounds (capacities) for the flow values |
336 | 336 |
/// on the arcs. |
337 | 337 |
/// \param supply The signed supply values of the nodes. |
338 | 338 |
Circulation(const Digraph &graph, const LowerMap &lower, |
339 | 339 |
const UpperMap &upper, const SupplyMap &supply) |
340 | 340 |
: _g(graph), _lo(&lower), _up(&upper), _supply(&supply), |
341 | 341 |
_flow(NULL), _local_flow(false), _level(NULL), _local_level(false), |
342 | 342 |
_excess(NULL) {} |
343 | 343 |
|
344 | 344 |
/// Destructor. |
345 | 345 |
~Circulation() { |
346 | 346 |
destroyStructures(); |
347 | 347 |
} |
348 | 348 |
|
349 | 349 |
|
350 | 350 |
private: |
351 | 351 |
|
352 | 352 |
bool checkBoundMaps() { |
353 | 353 |
for (ArcIt e(_g);e!=INVALID;++e) { |
354 | 354 |
if (_tol.less((*_up)[e], (*_lo)[e])) return false; |
355 | 355 |
} |
356 | 356 |
return true; |
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
void createStructures() { |
360 | 360 |
_node_num = _el = countNodes(_g); |
361 | 361 |
|
362 | 362 |
if (!_flow) { |
363 | 363 |
_flow = Traits::createFlowMap(_g); |
364 | 364 |
_local_flow = true; |
365 | 365 |
} |
366 | 366 |
if (!_level) { |
367 | 367 |
_level = Traits::createElevator(_g, _node_num); |
368 | 368 |
_local_level = true; |
369 | 369 |
} |
370 | 370 |
if (!_excess) { |
371 | 371 |
_excess = new ExcessMap(_g); |
372 | 372 |
} |
373 | 373 |
} |
374 | 374 |
|
375 | 375 |
void destroyStructures() { |
376 | 376 |
if (_local_flow) { |
377 | 377 |
delete _flow; |
378 | 378 |
} |
379 | 379 |
if (_local_level) { |
380 | 380 |
delete _level; |
381 | 381 |
} |
382 | 382 |
if (_excess) { |
383 | 383 |
delete _excess; |
384 | 384 |
} |
385 | 385 |
} |
386 | 386 |
|
387 | 387 |
public: |
388 | 388 |
|
389 | 389 |
/// Sets the lower bound map. |
390 | 390 |
|
391 | 391 |
/// Sets the lower bound map. |
392 | 392 |
/// \return <tt>(*this)</tt> |
393 | 393 |
Circulation& lowerMap(const LowerMap& map) { |
394 | 394 |
_lo = ↦ |
395 | 395 |
return *this; |
396 | 396 |
} |
397 | 397 |
|
398 | 398 |
/// Sets the upper bound (capacity) map. |
399 | 399 |
|
400 | 400 |
/// Sets the upper bound (capacity) map. |
401 | 401 |
/// \return <tt>(*this)</tt> |
402 | 402 |
Circulation& upperMap(const UpperMap& map) { |
403 | 403 |
_up = ↦ |
404 | 404 |
return *this; |
405 | 405 |
} |
406 | 406 |
|
407 | 407 |
/// Sets the supply map. |
408 | 408 |
|
409 | 409 |
/// Sets the supply map. |
410 | 410 |
/// \return <tt>(*this)</tt> |
411 | 411 |
Circulation& supplyMap(const SupplyMap& map) { |
412 | 412 |
_supply = ↦ |
413 | 413 |
return *this; |
414 | 414 |
} |
415 | 415 |
|
416 | 416 |
/// \brief Sets the flow map. |
417 | 417 |
/// |
418 | 418 |
/// Sets the flow map. |
419 | 419 |
/// If you don't use this function before calling \ref run() or |
420 | 420 |
/// \ref init(), an instance will be allocated automatically. |
421 | 421 |
/// The destructor deallocates this automatically allocated map, |
422 | 422 |
/// of course. |
423 | 423 |
/// \return <tt>(*this)</tt> |
424 | 424 |
Circulation& flowMap(FlowMap& map) { |
425 | 425 |
if (_local_flow) { |
426 | 426 |
delete _flow; |
427 | 427 |
_local_flow = false; |
428 | 428 |
} |
429 | 429 |
_flow = ↦ |
430 | 430 |
return *this; |
431 | 431 |
} |
432 | 432 |
|
433 | 433 |
/// \brief Sets the elevator used by algorithm. |
434 | 434 |
/// |
435 | 435 |
/// Sets the elevator used by algorithm. |
436 | 436 |
/// If you don't use this function before calling \ref run() or |
437 | 437 |
/// \ref init(), an instance will be allocated automatically. |
438 | 438 |
/// The destructor deallocates this automatically allocated elevator, |
439 | 439 |
/// of course. |
440 | 440 |
/// \return <tt>(*this)</tt> |
441 | 441 |
Circulation& elevator(Elevator& elevator) { |
442 | 442 |
if (_local_level) { |
443 | 443 |
delete _level; |
444 | 444 |
_local_level = false; |
445 | 445 |
} |
446 | 446 |
_level = &elevator; |
447 | 447 |
return *this; |
448 | 448 |
} |
449 | 449 |
|
450 | 450 |
/// \brief Returns a const reference to the elevator. |
451 | 451 |
/// |
452 | 452 |
/// Returns a const reference to the elevator. |
453 | 453 |
/// |
454 | 454 |
/// \pre Either \ref run() or \ref init() must be called before |
455 | 455 |
/// using this function. |
456 | 456 |
const Elevator& elevator() const { |
457 | 457 |
return *_level; |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
/// \brief Sets the tolerance used by the algorithm. |
461 | 461 |
/// |
462 | 462 |
/// Sets the tolerance object used by the algorithm. |
463 | 463 |
/// \return <tt>(*this)</tt> |
464 | 464 |
Circulation& tolerance(const Tolerance& tolerance) { |
465 | 465 |
_tol = tolerance; |
466 | 466 |
return *this; |
467 | 467 |
} |
468 | 468 |
|
469 | 469 |
/// \brief Returns a const reference to the tolerance. |
470 | 470 |
/// |
471 | 471 |
/// Returns a const reference to the tolerance object used by |
472 | 472 |
/// the algorithm. |
473 | 473 |
const Tolerance& tolerance() const { |
474 | 474 |
return _tol; |
475 | 475 |
} |
476 | 476 |
|
477 | 477 |
/// \name Execution Control |
478 | 478 |
/// The simplest way to execute the algorithm is to call \ref run().\n |
479 | 479 |
/// If you need better control on the initial solution or the execution, |
480 | 480 |
/// you have to call one of the \ref init() functions first, then |
481 | 481 |
/// the \ref start() function. |
482 | 482 |
|
483 | 483 |
///@{ |
484 | 484 |
|
485 | 485 |
/// Initializes the internal data structures. |
486 | 486 |
|
487 | 487 |
/// Initializes the internal data structures and sets all flow values |
488 | 488 |
/// to the lower bound. |
489 | 489 |
void init() |
490 | 490 |
{ |
491 | 491 |
LEMON_DEBUG(checkBoundMaps(), |
492 | 492 |
"Upper bounds must be greater or equal to the lower bounds"); |
493 | 493 |
|
494 | 494 |
createStructures(); |
495 | 495 |
|
496 | 496 |
for(NodeIt n(_g);n!=INVALID;++n) { |
497 | 497 |
(*_excess)[n] = (*_supply)[n]; |
498 | 498 |
} |
499 | 499 |
|
500 | 500 |
for (ArcIt e(_g);e!=INVALID;++e) { |
501 | 501 |
_flow->set(e, (*_lo)[e]); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONCEPTS_DIGRAPH_H |
20 | 20 |
#define LEMON_CONCEPTS_DIGRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graph_concepts |
23 | 23 |
///\file |
24 | 24 |
///\brief The concept of directed graphs. |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/graph_components.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
namespace concepts { |
33 | 33 |
|
34 | 34 |
/// \ingroup graph_concepts |
35 | 35 |
/// |
36 | 36 |
/// \brief Class describing the concept of directed graphs. |
37 | 37 |
/// |
38 | 38 |
/// This class describes the common interface of all directed |
39 | 39 |
/// graphs (digraphs). |
40 | 40 |
/// |
41 | 41 |
/// Like all concept classes, it only provides an interface |
42 | 42 |
/// without any sensible implementation. So any general algorithm for |
43 | 43 |
/// directed graphs should compile with this class, but it will not |
44 | 44 |
/// run properly, of course. |
45 | 45 |
/// An actual digraph implementation like \ref ListDigraph or |
46 | 46 |
/// \ref SmartDigraph may have additional functionality. |
47 | 47 |
/// |
48 | 48 |
/// \sa Graph |
49 | 49 |
class Digraph { |
50 | 50 |
private: |
51 | 51 |
/// Diraphs are \e not copy constructible. Use DigraphCopy instead. |
52 | 52 |
Digraph(const Digraph &) {} |
53 | 53 |
/// \brief Assignment of a digraph to another one is \e not allowed. |
54 | 54 |
/// Use DigraphCopy instead. |
55 | 55 |
void operator=(const Digraph &) {} |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
/// Default constructor. |
59 | 59 |
Digraph() { } |
60 | 60 |
|
61 | 61 |
/// The node type of the digraph |
62 | 62 |
|
63 | 63 |
/// This class identifies a node of the digraph. It also serves |
64 | 64 |
/// as a base class of the node iterators, |
65 | 65 |
/// thus they convert to this type. |
66 | 66 |
class Node { |
67 | 67 |
public: |
68 | 68 |
/// Default constructor |
69 | 69 |
|
70 | 70 |
/// Default constructor. |
71 | 71 |
/// \warning It sets the object to an undefined value. |
72 | 72 |
Node() { } |
73 | 73 |
/// Copy constructor. |
74 | 74 |
|
75 | 75 |
/// Copy constructor. |
76 | 76 |
/// |
77 | 77 |
Node(const Node&) { } |
78 | 78 |
|
79 | 79 |
/// %Invalid constructor \& conversion. |
80 | 80 |
|
81 | 81 |
/// Initializes the object to be invalid. |
82 | 82 |
/// \sa Invalid for more details. |
83 | 83 |
Node(Invalid) { } |
84 | 84 |
/// Equality operator |
85 | 85 |
|
86 | 86 |
/// Equality operator. |
87 | 87 |
/// |
88 | 88 |
/// Two iterators are equal if and only if they point to the |
89 | 89 |
/// same object or both are \c INVALID. |
90 | 90 |
bool operator==(Node) const { return true; } |
91 | 91 |
|
92 | 92 |
/// Inequality operator |
93 | 93 |
|
94 | 94 |
/// Inequality operator. |
95 | 95 |
bool operator!=(Node) const { return true; } |
96 | 96 |
|
97 | 97 |
/// Artificial ordering operator. |
98 | 98 |
|
99 | 99 |
/// Artificial ordering operator. |
100 | 100 |
/// |
101 | 101 |
/// \note This operator only has to define some strict ordering of |
102 | 102 |
/// the nodes; this order has nothing to do with the iteration |
103 | 103 |
/// ordering of the nodes. |
104 | 104 |
bool operator<(Node) const { return false; } |
105 | 105 |
}; |
106 | 106 |
|
107 | 107 |
/// Iterator class for the nodes. |
108 | 108 |
|
109 | 109 |
/// This iterator goes through each node of the digraph. |
110 |
/// Its usage is quite simple, for example you can count the number |
|
110 |
/// Its usage is quite simple, for example, you can count the number |
|
111 | 111 |
/// of nodes in a digraph \c g of type \c %Digraph like this: |
112 | 112 |
///\code |
113 | 113 |
/// int count=0; |
114 | 114 |
/// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count; |
115 | 115 |
///\endcode |
116 | 116 |
class NodeIt : public Node { |
117 | 117 |
public: |
118 | 118 |
/// Default constructor |
119 | 119 |
|
120 | 120 |
/// Default constructor. |
121 | 121 |
/// \warning It sets the iterator to an undefined value. |
122 | 122 |
NodeIt() { } |
123 | 123 |
/// Copy constructor. |
124 | 124 |
|
125 | 125 |
/// Copy constructor. |
126 | 126 |
/// |
127 | 127 |
NodeIt(const NodeIt& n) : Node(n) { } |
128 | 128 |
/// %Invalid constructor \& conversion. |
129 | 129 |
|
130 | 130 |
/// Initializes the iterator to be invalid. |
131 | 131 |
/// \sa Invalid for more details. |
132 | 132 |
NodeIt(Invalid) { } |
133 | 133 |
/// Sets the iterator to the first node. |
134 | 134 |
|
135 | 135 |
/// Sets the iterator to the first node of the given digraph. |
136 | 136 |
/// |
137 | 137 |
explicit NodeIt(const Digraph&) { } |
138 | 138 |
/// Sets the iterator to the given node. |
139 | 139 |
|
140 | 140 |
/// Sets the iterator to the given node of the given digraph. |
141 | 141 |
/// |
142 | 142 |
NodeIt(const Digraph&, const Node&) { } |
143 | 143 |
/// Next node. |
144 | 144 |
|
145 | 145 |
/// Assign the iterator to the next node. |
146 | 146 |
/// |
147 | 147 |
NodeIt& operator++() { return *this; } |
148 | 148 |
}; |
149 | 149 |
|
150 | 150 |
|
151 | 151 |
/// The arc type of the digraph |
152 | 152 |
|
153 | 153 |
/// This class identifies an arc of the digraph. It also serves |
154 | 154 |
/// as a base class of the arc iterators, |
155 | 155 |
/// thus they will convert to this type. |
156 | 156 |
class Arc { |
157 | 157 |
public: |
158 | 158 |
/// Default constructor |
159 | 159 |
|
160 | 160 |
/// Default constructor. |
161 | 161 |
/// \warning It sets the object to an undefined value. |
162 | 162 |
Arc() { } |
163 | 163 |
/// Copy constructor. |
164 | 164 |
|
165 | 165 |
/// Copy constructor. |
166 | 166 |
/// |
167 | 167 |
Arc(const Arc&) { } |
168 | 168 |
/// %Invalid constructor \& conversion. |
169 | 169 |
|
170 | 170 |
/// Initializes the object to be invalid. |
171 | 171 |
/// \sa Invalid for more details. |
172 | 172 |
Arc(Invalid) { } |
173 | 173 |
/// Equality operator |
174 | 174 |
|
175 | 175 |
/// Equality operator. |
176 | 176 |
/// |
177 | 177 |
/// Two iterators are equal if and only if they point to the |
178 | 178 |
/// same object or both are \c INVALID. |
179 | 179 |
bool operator==(Arc) const { return true; } |
180 | 180 |
/// Inequality operator |
181 | 181 |
|
182 | 182 |
/// Inequality operator. |
183 | 183 |
bool operator!=(Arc) const { return true; } |
184 | 184 |
|
185 | 185 |
/// Artificial ordering operator. |
186 | 186 |
|
187 | 187 |
/// Artificial ordering operator. |
188 | 188 |
/// |
189 | 189 |
/// \note This operator only has to define some strict ordering of |
190 | 190 |
/// the arcs; this order has nothing to do with the iteration |
191 | 191 |
/// ordering of the arcs. |
192 | 192 |
bool operator<(Arc) const { return false; } |
193 | 193 |
}; |
194 | 194 |
|
195 | 195 |
/// Iterator class for the outgoing arcs of a node. |
196 | 196 |
|
197 | 197 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
198 | 198 |
/// of a digraph. |
199 |
/// Its usage is quite simple, for example you can count the number |
|
199 |
/// Its usage is quite simple, for example, you can count the number |
|
200 | 200 |
/// of outgoing arcs of a node \c n |
201 | 201 |
/// in a digraph \c g of type \c %Digraph as follows. |
202 | 202 |
///\code |
203 | 203 |
/// int count=0; |
204 | 204 |
/// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count; |
205 | 205 |
///\endcode |
206 | 206 |
class OutArcIt : public Arc { |
207 | 207 |
public: |
208 | 208 |
/// Default constructor |
209 | 209 |
|
210 | 210 |
/// Default constructor. |
211 | 211 |
/// \warning It sets the iterator to an undefined value. |
212 | 212 |
OutArcIt() { } |
213 | 213 |
/// Copy constructor. |
214 | 214 |
|
215 | 215 |
/// Copy constructor. |
216 | 216 |
/// |
217 | 217 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
218 | 218 |
/// %Invalid constructor \& conversion. |
219 | 219 |
|
220 | 220 |
/// Initializes the iterator to be invalid. |
221 | 221 |
/// \sa Invalid for more details. |
222 | 222 |
OutArcIt(Invalid) { } |
223 | 223 |
/// Sets the iterator to the first outgoing arc. |
224 | 224 |
|
225 | 225 |
/// Sets the iterator to the first outgoing arc of the given node. |
226 | 226 |
/// |
227 | 227 |
OutArcIt(const Digraph&, const Node&) { } |
228 | 228 |
/// Sets the iterator to the given arc. |
229 | 229 |
|
230 | 230 |
/// Sets the iterator to the given arc of the given digraph. |
231 | 231 |
/// |
232 | 232 |
OutArcIt(const Digraph&, const Arc&) { } |
233 | 233 |
/// Next outgoing arc |
234 | 234 |
|
235 | 235 |
/// Assign the iterator to the next |
236 | 236 |
/// outgoing arc of the corresponding node. |
237 | 237 |
OutArcIt& operator++() { return *this; } |
238 | 238 |
}; |
239 | 239 |
|
240 | 240 |
/// Iterator class for the incoming arcs of a node. |
241 | 241 |
|
242 | 242 |
/// This iterator goes trough the \e incoming arcs of a certain node |
243 | 243 |
/// of a digraph. |
244 |
/// Its usage is quite simple, for example you can count the number |
|
244 |
/// Its usage is quite simple, for example, you can count the number |
|
245 | 245 |
/// of incoming arcs of a node \c n |
246 | 246 |
/// in a digraph \c g of type \c %Digraph as follows. |
247 | 247 |
///\code |
248 | 248 |
/// int count=0; |
249 | 249 |
/// for(Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count; |
250 | 250 |
///\endcode |
251 | 251 |
class InArcIt : public Arc { |
252 | 252 |
public: |
253 | 253 |
/// Default constructor |
254 | 254 |
|
255 | 255 |
/// Default constructor. |
256 | 256 |
/// \warning It sets the iterator to an undefined value. |
257 | 257 |
InArcIt() { } |
258 | 258 |
/// Copy constructor. |
259 | 259 |
|
260 | 260 |
/// Copy constructor. |
261 | 261 |
/// |
262 | 262 |
InArcIt(const InArcIt& e) : Arc(e) { } |
263 | 263 |
/// %Invalid constructor \& conversion. |
264 | 264 |
|
265 | 265 |
/// Initializes the iterator to be invalid. |
266 | 266 |
/// \sa Invalid for more details. |
267 | 267 |
InArcIt(Invalid) { } |
268 | 268 |
/// Sets the iterator to the first incoming arc. |
269 | 269 |
|
270 | 270 |
/// Sets the iterator to the first incoming arc of the given node. |
271 | 271 |
/// |
272 | 272 |
InArcIt(const Digraph&, const Node&) { } |
273 | 273 |
/// Sets the iterator to the given arc. |
274 | 274 |
|
275 | 275 |
/// Sets the iterator to the given arc of the given digraph. |
276 | 276 |
/// |
277 | 277 |
InArcIt(const Digraph&, const Arc&) { } |
278 | 278 |
/// Next incoming arc |
279 | 279 |
|
280 | 280 |
/// Assign the iterator to the next |
281 | 281 |
/// incoming arc of the corresponding node. |
282 | 282 |
InArcIt& operator++() { return *this; } |
283 | 283 |
}; |
284 | 284 |
|
285 | 285 |
/// Iterator class for the arcs. |
286 | 286 |
|
287 | 287 |
/// This iterator goes through each arc of the digraph. |
288 |
/// Its usage is quite simple, for example you can count the number |
|
288 |
/// Its usage is quite simple, for example, you can count the number |
|
289 | 289 |
/// of arcs in a digraph \c g of type \c %Digraph as follows: |
290 | 290 |
///\code |
291 | 291 |
/// int count=0; |
292 | 292 |
/// for(Digraph::ArcIt a(g); a!=INVALID; ++a) ++count; |
293 | 293 |
///\endcode |
294 | 294 |
class ArcIt : public Arc { |
295 | 295 |
public: |
296 | 296 |
/// Default constructor |
297 | 297 |
|
298 | 298 |
/// Default constructor. |
299 | 299 |
/// \warning It sets the iterator to an undefined value. |
300 | 300 |
ArcIt() { } |
301 | 301 |
/// Copy constructor. |
302 | 302 |
|
303 | 303 |
/// Copy constructor. |
304 | 304 |
/// |
305 | 305 |
ArcIt(const ArcIt& e) : Arc(e) { } |
306 | 306 |
/// %Invalid constructor \& conversion. |
307 | 307 |
|
308 | 308 |
/// Initializes the iterator to be invalid. |
309 | 309 |
/// \sa Invalid for more details. |
310 | 310 |
ArcIt(Invalid) { } |
311 | 311 |
/// Sets the iterator to the first arc. |
312 | 312 |
|
313 | 313 |
/// Sets the iterator to the first arc of the given digraph. |
314 | 314 |
/// |
315 | 315 |
explicit ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); } |
316 | 316 |
/// Sets the iterator to the given arc. |
317 | 317 |
|
318 | 318 |
/// Sets the iterator to the given arc of the given digraph. |
319 | 319 |
/// |
320 | 320 |
ArcIt(const Digraph&, const Arc&) { } |
321 | 321 |
/// Next arc |
322 | 322 |
|
323 | 323 |
/// Assign the iterator to the next arc. |
324 | 324 |
/// |
325 | 325 |
ArcIt& operator++() { return *this; } |
326 | 326 |
}; |
327 | 327 |
|
328 | 328 |
/// \brief The source node of the arc. |
329 | 329 |
/// |
330 | 330 |
/// Returns the source node of the given arc. |
331 | 331 |
Node source(Arc) const { return INVALID; } |
332 | 332 |
|
333 | 333 |
/// \brief The target node of the arc. |
334 | 334 |
/// |
335 | 335 |
/// Returns the target node of the given arc. |
336 | 336 |
Node target(Arc) const { return INVALID; } |
337 | 337 |
|
338 | 338 |
/// \brief The ID of the node. |
339 | 339 |
/// |
340 | 340 |
/// Returns the ID of the given node. |
341 | 341 |
int id(Node) const { return -1; } |
342 | 342 |
|
343 | 343 |
/// \brief The ID of the arc. |
344 | 344 |
/// |
345 | 345 |
/// Returns the ID of the given arc. |
346 | 346 |
int id(Arc) const { return -1; } |
347 | 347 |
|
348 | 348 |
/// \brief The node with the given ID. |
349 | 349 |
/// |
350 | 350 |
/// Returns the node with the given ID. |
351 | 351 |
/// \pre The argument should be a valid node ID in the digraph. |
352 | 352 |
Node nodeFromId(int) const { return INVALID; } |
353 | 353 |
|
354 | 354 |
/// \brief The arc with the given ID. |
355 | 355 |
/// |
356 | 356 |
/// Returns the arc with the given ID. |
357 | 357 |
/// \pre The argument should be a valid arc ID in the digraph. |
358 | 358 |
Arc arcFromId(int) const { return INVALID; } |
359 | 359 |
|
360 | 360 |
/// \brief An upper bound on the node IDs. |
361 | 361 |
/// |
362 | 362 |
/// Returns an upper bound on the node IDs. |
363 | 363 |
int maxNodeId() const { return -1; } |
364 | 364 |
|
365 | 365 |
/// \brief An upper bound on the arc IDs. |
366 | 366 |
/// |
367 | 367 |
/// Returns an upper bound on the arc IDs. |
368 | 368 |
int maxArcId() const { return -1; } |
369 | 369 |
|
370 | 370 |
void first(Node&) const {} |
371 | 371 |
void next(Node&) const {} |
372 | 372 |
|
373 | 373 |
void first(Arc&) const {} |
374 | 374 |
void next(Arc&) const {} |
375 | 375 |
|
376 | 376 |
|
377 | 377 |
void firstIn(Arc&, const Node&) const {} |
378 | 378 |
void nextIn(Arc&) const {} |
379 | 379 |
|
380 | 380 |
void firstOut(Arc&, const Node&) const {} |
381 | 381 |
void nextOut(Arc&) const {} |
382 | 382 |
|
383 | 383 |
// The second parameter is dummy. |
384 | 384 |
Node fromId(int, Node) const { return INVALID; } |
385 | 385 |
// The second parameter is dummy. |
386 | 386 |
Arc fromId(int, Arc) const { return INVALID; } |
387 | 387 |
|
388 | 388 |
// Dummy parameter. |
389 | 389 |
int maxId(Node) const { return -1; } |
390 | 390 |
// Dummy parameter. |
391 | 391 |
int maxId(Arc) const { return -1; } |
392 | 392 |
|
393 | 393 |
/// \brief The opposite node on the arc. |
394 | 394 |
/// |
395 | 395 |
/// Returns the opposite node on the given arc. |
396 | 396 |
Node oppositeNode(Node, Arc) const { return INVALID; } |
397 | 397 |
|
398 | 398 |
/// \brief The base node of the iterator. |
399 | 399 |
/// |
400 | 400 |
/// Returns the base node of the given outgoing arc iterator |
401 | 401 |
/// (i.e. the source node of the corresponding arc). |
402 | 402 |
Node baseNode(OutArcIt) const { return INVALID; } |
403 | 403 |
|
404 | 404 |
/// \brief The running node of the iterator. |
405 | 405 |
/// |
406 | 406 |
/// Returns the running node of the given outgoing arc iterator |
407 | 407 |
/// (i.e. the target node of the corresponding arc). |
408 | 408 |
Node runningNode(OutArcIt) const { return INVALID; } |
409 | 409 |
|
410 | 410 |
/// \brief The base node of the iterator. |
411 | 411 |
/// |
412 | 412 |
/// Returns the base node of the given incomming arc iterator |
413 | 413 |
/// (i.e. the target node of the corresponding arc). |
414 | 414 |
Node baseNode(InArcIt) const { return INVALID; } |
415 | 415 |
|
416 | 416 |
/// \brief The running node of the iterator. |
417 | 417 |
/// |
418 | 418 |
/// Returns the running node of the given incomming arc iterator |
419 | 419 |
/// (i.e. the source node of the corresponding arc). |
420 | 420 |
Node runningNode(InArcIt) const { return INVALID; } |
421 | 421 |
|
422 | 422 |
/// \brief Standard graph map type for the nodes. |
423 | 423 |
/// |
424 | 424 |
/// Standard graph map type for the nodes. |
425 | 425 |
/// It conforms to the ReferenceMap concept. |
426 | 426 |
template<class T> |
427 | 427 |
class NodeMap : public ReferenceMap<Node, T, T&, const T&> { |
428 | 428 |
public: |
429 | 429 |
|
430 | 430 |
/// Constructor |
431 | 431 |
explicit NodeMap(const Digraph&) { } |
432 | 432 |
/// Constructor with given initial value |
433 | 433 |
NodeMap(const Digraph&, T) { } |
434 | 434 |
|
435 | 435 |
private: |
436 | 436 |
///Copy constructor |
437 | 437 |
NodeMap(const NodeMap& nm) : |
438 | 438 |
ReferenceMap<Node, T, T&, const T&>(nm) { } |
439 | 439 |
///Assignment operator |
440 | 440 |
template <typename CMap> |
441 | 441 |
NodeMap& operator=(const CMap&) { |
442 | 442 |
checkConcept<ReadMap<Node, T>, CMap>(); |
443 | 443 |
return *this; |
444 | 444 |
} |
445 | 445 |
}; |
446 | 446 |
|
447 | 447 |
/// \brief Standard graph map type for the arcs. |
448 | 448 |
/// |
449 | 449 |
/// Standard graph map type for the arcs. |
450 | 450 |
/// It conforms to the ReferenceMap concept. |
451 | 451 |
template<class T> |
452 | 452 |
class ArcMap : public ReferenceMap<Arc, T, T&, const T&> { |
453 | 453 |
public: |
454 | 454 |
|
455 | 455 |
/// Constructor |
456 | 456 |
explicit ArcMap(const Digraph&) { } |
457 | 457 |
/// Constructor with given initial value |
458 | 458 |
ArcMap(const Digraph&, T) { } |
459 | 459 |
|
460 | 460 |
private: |
461 | 461 |
///Copy constructor |
462 | 462 |
ArcMap(const ArcMap& em) : |
463 | 463 |
ReferenceMap<Arc, T, T&, const T&>(em) { } |
464 | 464 |
///Assignment operator |
465 | 465 |
template <typename CMap> |
466 | 466 |
ArcMap& operator=(const CMap&) { |
467 | 467 |
checkConcept<ReadMap<Arc, T>, CMap>(); |
468 | 468 |
return *this; |
469 | 469 |
} |
470 | 470 |
}; |
471 | 471 |
|
472 | 472 |
template <typename _Digraph> |
473 | 473 |
struct Constraints { |
474 | 474 |
void constraints() { |
475 | 475 |
checkConcept<BaseDigraphComponent, _Digraph>(); |
476 | 476 |
checkConcept<IterableDigraphComponent<>, _Digraph>(); |
477 | 477 |
checkConcept<IDableDigraphComponent<>, _Digraph>(); |
478 | 478 |
checkConcept<MappableDigraphComponent<>, _Digraph>(); |
479 | 479 |
} |
480 | 480 |
}; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of undirected graphs. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_H |
24 | 24 |
#define LEMON_CONCEPTS_GRAPH_H |
25 | 25 |
|
26 | 26 |
#include <lemon/concepts/graph_components.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
namespace concepts { |
33 | 33 |
|
34 | 34 |
/// \ingroup graph_concepts |
35 | 35 |
/// |
36 | 36 |
/// \brief Class describing the concept of undirected graphs. |
37 | 37 |
/// |
38 | 38 |
/// This class describes the common interface of all undirected |
39 | 39 |
/// graphs. |
40 | 40 |
/// |
41 | 41 |
/// Like all concept classes, it only provides an interface |
42 | 42 |
/// without any sensible implementation. So any general algorithm for |
43 | 43 |
/// undirected graphs should compile with this class, but it will not |
44 | 44 |
/// run properly, of course. |
45 | 45 |
/// An actual graph implementation like \ref ListGraph or |
46 | 46 |
/// \ref SmartGraph may have additional functionality. |
47 | 47 |
/// |
48 | 48 |
/// The undirected graphs also fulfill the concept of \ref Digraph |
49 | 49 |
/// "directed graphs", since each edge can also be regarded as two |
50 | 50 |
/// oppositely directed arcs. |
51 | 51 |
/// Undirected graphs provide an Edge type for the undirected edges and |
52 | 52 |
/// an Arc type for the directed arcs. The Arc type is convertible to |
53 | 53 |
/// Edge or inherited from it, i.e. the corresponding edge can be |
54 | 54 |
/// obtained from an arc. |
55 | 55 |
/// EdgeIt and EdgeMap classes can be used for the edges, while ArcIt |
56 | 56 |
/// and ArcMap classes can be used for the arcs (just like in digraphs). |
57 | 57 |
/// Both InArcIt and OutArcIt iterates on the same edges but with |
58 | 58 |
/// opposite direction. IncEdgeIt also iterates on the same edges |
59 | 59 |
/// as OutArcIt and InArcIt, but it is not convertible to Arc, |
60 | 60 |
/// only to Edge. |
61 | 61 |
/// |
62 | 62 |
/// In LEMON, each undirected edge has an inherent orientation. |
63 | 63 |
/// Thus it can defined if an arc is forward or backward oriented in |
64 | 64 |
/// an undirected graph with respect to this default oriantation of |
65 | 65 |
/// the represented edge. |
66 | 66 |
/// With the direction() and direct() functions the direction |
67 | 67 |
/// of an arc can be obtained and set, respectively. |
68 | 68 |
/// |
69 | 69 |
/// Only nodes and edges can be added to or removed from an undirected |
70 | 70 |
/// graph and the corresponding arcs are added or removed automatically. |
71 | 71 |
/// |
72 | 72 |
/// \sa Digraph |
73 | 73 |
class Graph { |
74 | 74 |
private: |
75 | 75 |
/// Graphs are \e not copy constructible. Use DigraphCopy instead. |
76 | 76 |
Graph(const Graph&) {} |
77 | 77 |
/// \brief Assignment of a graph to another one is \e not allowed. |
78 | 78 |
/// Use DigraphCopy instead. |
79 | 79 |
void operator=(const Graph&) {} |
80 | 80 |
|
81 | 81 |
public: |
82 | 82 |
/// Default constructor. |
83 | 83 |
Graph() {} |
84 | 84 |
|
85 | 85 |
/// \brief Undirected graphs should be tagged with \c UndirectedTag. |
86 | 86 |
/// |
87 | 87 |
/// Undirected graphs should be tagged with \c UndirectedTag. |
88 | 88 |
/// |
89 | 89 |
/// This tag helps the \c enable_if technics to make compile time |
90 | 90 |
/// specializations for undirected graphs. |
91 | 91 |
typedef True UndirectedTag; |
92 | 92 |
|
93 | 93 |
/// The node type of the graph |
94 | 94 |
|
95 | 95 |
/// This class identifies a node of the graph. It also serves |
96 | 96 |
/// as a base class of the node iterators, |
97 | 97 |
/// thus they convert to this type. |
98 | 98 |
class Node { |
99 | 99 |
public: |
100 | 100 |
/// Default constructor |
101 | 101 |
|
102 | 102 |
/// Default constructor. |
103 | 103 |
/// \warning It sets the object to an undefined value. |
104 | 104 |
Node() { } |
105 | 105 |
/// Copy constructor. |
106 | 106 |
|
107 | 107 |
/// Copy constructor. |
108 | 108 |
/// |
109 | 109 |
Node(const Node&) { } |
110 | 110 |
|
111 | 111 |
/// %Invalid constructor \& conversion. |
112 | 112 |
|
113 | 113 |
/// Initializes the object to be invalid. |
114 | 114 |
/// \sa Invalid for more details. |
115 | 115 |
Node(Invalid) { } |
116 | 116 |
/// Equality operator |
117 | 117 |
|
118 | 118 |
/// Equality operator. |
119 | 119 |
/// |
120 | 120 |
/// Two iterators are equal if and only if they point to the |
121 | 121 |
/// same object or both are \c INVALID. |
122 | 122 |
bool operator==(Node) const { return true; } |
123 | 123 |
|
124 | 124 |
/// Inequality operator |
125 | 125 |
|
126 | 126 |
/// Inequality operator. |
127 | 127 |
bool operator!=(Node) const { return true; } |
128 | 128 |
|
129 | 129 |
/// Artificial ordering operator. |
130 | 130 |
|
131 | 131 |
/// Artificial ordering operator. |
132 | 132 |
/// |
133 | 133 |
/// \note This operator only has to define some strict ordering of |
134 | 134 |
/// the items; this order has nothing to do with the iteration |
135 | 135 |
/// ordering of the items. |
136 | 136 |
bool operator<(Node) const { return false; } |
137 | 137 |
|
138 | 138 |
}; |
139 | 139 |
|
140 | 140 |
/// Iterator class for the nodes. |
141 | 141 |
|
142 | 142 |
/// This iterator goes through each node of the graph. |
143 |
/// Its usage is quite simple, for example you can count the number |
|
143 |
/// Its usage is quite simple, for example, you can count the number |
|
144 | 144 |
/// of nodes in a graph \c g of type \c %Graph like this: |
145 | 145 |
///\code |
146 | 146 |
/// int count=0; |
147 | 147 |
/// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; |
148 | 148 |
///\endcode |
149 | 149 |
class NodeIt : public Node { |
150 | 150 |
public: |
151 | 151 |
/// Default constructor |
152 | 152 |
|
153 | 153 |
/// Default constructor. |
154 | 154 |
/// \warning It sets the iterator to an undefined value. |
155 | 155 |
NodeIt() { } |
156 | 156 |
/// Copy constructor. |
157 | 157 |
|
158 | 158 |
/// Copy constructor. |
159 | 159 |
/// |
160 | 160 |
NodeIt(const NodeIt& n) : Node(n) { } |
161 | 161 |
/// %Invalid constructor \& conversion. |
162 | 162 |
|
163 | 163 |
/// Initializes the iterator to be invalid. |
164 | 164 |
/// \sa Invalid for more details. |
165 | 165 |
NodeIt(Invalid) { } |
166 | 166 |
/// Sets the iterator to the first node. |
167 | 167 |
|
168 | 168 |
/// Sets the iterator to the first node of the given digraph. |
169 | 169 |
/// |
170 | 170 |
explicit NodeIt(const Graph&) { } |
171 | 171 |
/// Sets the iterator to the given node. |
172 | 172 |
|
173 | 173 |
/// Sets the iterator to the given node of the given digraph. |
174 | 174 |
/// |
175 | 175 |
NodeIt(const Graph&, const Node&) { } |
176 | 176 |
/// Next node. |
177 | 177 |
|
178 | 178 |
/// Assign the iterator to the next node. |
179 | 179 |
/// |
180 | 180 |
NodeIt& operator++() { return *this; } |
181 | 181 |
}; |
182 | 182 |
|
183 | 183 |
|
184 | 184 |
/// The edge type of the graph |
185 | 185 |
|
186 | 186 |
/// This class identifies an edge of the graph. It also serves |
187 | 187 |
/// as a base class of the edge iterators, |
188 | 188 |
/// thus they will convert to this type. |
189 | 189 |
class Edge { |
190 | 190 |
public: |
191 | 191 |
/// Default constructor |
192 | 192 |
|
193 | 193 |
/// Default constructor. |
194 | 194 |
/// \warning It sets the object to an undefined value. |
195 | 195 |
Edge() { } |
196 | 196 |
/// Copy constructor. |
197 | 197 |
|
198 | 198 |
/// Copy constructor. |
199 | 199 |
/// |
200 | 200 |
Edge(const Edge&) { } |
201 | 201 |
/// %Invalid constructor \& conversion. |
202 | 202 |
|
203 | 203 |
/// Initializes the object to be invalid. |
204 | 204 |
/// \sa Invalid for more details. |
205 | 205 |
Edge(Invalid) { } |
206 | 206 |
/// Equality operator |
207 | 207 |
|
208 | 208 |
/// Equality operator. |
209 | 209 |
/// |
210 | 210 |
/// Two iterators are equal if and only if they point to the |
211 | 211 |
/// same object or both are \c INVALID. |
212 | 212 |
bool operator==(Edge) const { return true; } |
213 | 213 |
/// Inequality operator |
214 | 214 |
|
215 | 215 |
/// Inequality operator. |
216 | 216 |
bool operator!=(Edge) const { return true; } |
217 | 217 |
|
218 | 218 |
/// Artificial ordering operator. |
219 | 219 |
|
220 | 220 |
/// Artificial ordering operator. |
221 | 221 |
/// |
222 | 222 |
/// \note This operator only has to define some strict ordering of |
223 | 223 |
/// the edges; this order has nothing to do with the iteration |
224 | 224 |
/// ordering of the edges. |
225 | 225 |
bool operator<(Edge) const { return false; } |
226 | 226 |
}; |
227 | 227 |
|
228 | 228 |
/// Iterator class for the edges. |
229 | 229 |
|
230 | 230 |
/// This iterator goes through each edge of the graph. |
231 |
/// Its usage is quite simple, for example you can count the number |
|
231 |
/// Its usage is quite simple, for example, you can count the number |
|
232 | 232 |
/// of edges in a graph \c g of type \c %Graph as follows: |
233 | 233 |
///\code |
234 | 234 |
/// int count=0; |
235 | 235 |
/// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count; |
236 | 236 |
///\endcode |
237 | 237 |
class EdgeIt : public Edge { |
238 | 238 |
public: |
239 | 239 |
/// Default constructor |
240 | 240 |
|
241 | 241 |
/// Default constructor. |
242 | 242 |
/// \warning It sets the iterator to an undefined value. |
243 | 243 |
EdgeIt() { } |
244 | 244 |
/// Copy constructor. |
245 | 245 |
|
246 | 246 |
/// Copy constructor. |
247 | 247 |
/// |
248 | 248 |
EdgeIt(const EdgeIt& e) : Edge(e) { } |
249 | 249 |
/// %Invalid constructor \& conversion. |
250 | 250 |
|
251 | 251 |
/// Initializes the iterator to be invalid. |
252 | 252 |
/// \sa Invalid for more details. |
253 | 253 |
EdgeIt(Invalid) { } |
254 | 254 |
/// Sets the iterator to the first edge. |
255 | 255 |
|
256 | 256 |
/// Sets the iterator to the first edge of the given graph. |
257 | 257 |
/// |
258 | 258 |
explicit EdgeIt(const Graph&) { } |
259 | 259 |
/// Sets the iterator to the given edge. |
260 | 260 |
|
261 | 261 |
/// Sets the iterator to the given edge of the given graph. |
262 | 262 |
/// |
263 | 263 |
EdgeIt(const Graph&, const Edge&) { } |
264 | 264 |
/// Next edge |
265 | 265 |
|
266 | 266 |
/// Assign the iterator to the next edge. |
267 | 267 |
/// |
268 | 268 |
EdgeIt& operator++() { return *this; } |
269 | 269 |
}; |
270 | 270 |
|
271 | 271 |
/// Iterator class for the incident edges of a node. |
272 | 272 |
|
273 | 273 |
/// This iterator goes trough the incident undirected edges |
274 | 274 |
/// of a certain node of a graph. |
275 |
/// Its usage is quite simple, for example you can compute the |
|
275 |
/// Its usage is quite simple, for example, you can compute the |
|
276 | 276 |
/// degree (i.e. the number of incident edges) of a node \c n |
277 | 277 |
/// in a graph \c g of type \c %Graph as follows. |
278 | 278 |
/// |
279 | 279 |
///\code |
280 | 280 |
/// int count=0; |
281 | 281 |
/// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count; |
282 | 282 |
///\endcode |
283 | 283 |
/// |
284 | 284 |
/// \warning Loop edges will be iterated twice. |
285 | 285 |
class IncEdgeIt : public Edge { |
286 | 286 |
public: |
287 | 287 |
/// Default constructor |
288 | 288 |
|
289 | 289 |
/// Default constructor. |
290 | 290 |
/// \warning It sets the iterator to an undefined value. |
291 | 291 |
IncEdgeIt() { } |
292 | 292 |
/// Copy constructor. |
293 | 293 |
|
294 | 294 |
/// Copy constructor. |
295 | 295 |
/// |
296 | 296 |
IncEdgeIt(const IncEdgeIt& e) : Edge(e) { } |
297 | 297 |
/// %Invalid constructor \& conversion. |
298 | 298 |
|
299 | 299 |
/// Initializes the iterator to be invalid. |
300 | 300 |
/// \sa Invalid for more details. |
301 | 301 |
IncEdgeIt(Invalid) { } |
302 | 302 |
/// Sets the iterator to the first incident edge. |
303 | 303 |
|
304 | 304 |
/// Sets the iterator to the first incident edge of the given node. |
305 | 305 |
/// |
306 | 306 |
IncEdgeIt(const Graph&, const Node&) { } |
307 | 307 |
/// Sets the iterator to the given edge. |
308 | 308 |
|
309 | 309 |
/// Sets the iterator to the given edge of the given graph. |
310 | 310 |
/// |
311 | 311 |
IncEdgeIt(const Graph&, const Edge&) { } |
312 | 312 |
/// Next incident edge |
313 | 313 |
|
314 | 314 |
/// Assign the iterator to the next incident edge |
315 | 315 |
/// of the corresponding node. |
316 | 316 |
IncEdgeIt& operator++() { return *this; } |
317 | 317 |
}; |
318 | 318 |
|
319 | 319 |
/// The arc type of the graph |
320 | 320 |
|
321 | 321 |
/// This class identifies a directed arc of the graph. It also serves |
322 | 322 |
/// as a base class of the arc iterators, |
323 | 323 |
/// thus they will convert to this type. |
324 | 324 |
class Arc { |
325 | 325 |
public: |
326 | 326 |
/// Default constructor |
327 | 327 |
|
328 | 328 |
/// Default constructor. |
329 | 329 |
/// \warning It sets the object to an undefined value. |
330 | 330 |
Arc() { } |
331 | 331 |
/// Copy constructor. |
332 | 332 |
|
333 | 333 |
/// Copy constructor. |
334 | 334 |
/// |
335 | 335 |
Arc(const Arc&) { } |
336 | 336 |
/// %Invalid constructor \& conversion. |
337 | 337 |
|
338 | 338 |
/// Initializes the object to be invalid. |
339 | 339 |
/// \sa Invalid for more details. |
340 | 340 |
Arc(Invalid) { } |
341 | 341 |
/// Equality operator |
342 | 342 |
|
343 | 343 |
/// Equality operator. |
344 | 344 |
/// |
345 | 345 |
/// Two iterators are equal if and only if they point to the |
346 | 346 |
/// same object or both are \c INVALID. |
347 | 347 |
bool operator==(Arc) const { return true; } |
348 | 348 |
/// Inequality operator |
349 | 349 |
|
350 | 350 |
/// Inequality operator. |
351 | 351 |
bool operator!=(Arc) const { return true; } |
352 | 352 |
|
353 | 353 |
/// Artificial ordering operator. |
354 | 354 |
|
355 | 355 |
/// Artificial ordering operator. |
356 | 356 |
/// |
357 | 357 |
/// \note This operator only has to define some strict ordering of |
358 | 358 |
/// the arcs; this order has nothing to do with the iteration |
359 | 359 |
/// ordering of the arcs. |
360 | 360 |
bool operator<(Arc) const { return false; } |
361 | 361 |
|
362 | 362 |
/// Converison to \c Edge |
363 | 363 |
|
364 | 364 |
/// Converison to \c Edge. |
365 | 365 |
/// |
366 | 366 |
operator Edge() const { return Edge(); } |
367 | 367 |
}; |
368 | 368 |
|
369 | 369 |
/// Iterator class for the arcs. |
370 | 370 |
|
371 | 371 |
/// This iterator goes through each directed arc of the graph. |
372 |
/// Its usage is quite simple, for example you can count the number |
|
372 |
/// Its usage is quite simple, for example, you can count the number |
|
373 | 373 |
/// of arcs in a graph \c g of type \c %Graph as follows: |
374 | 374 |
///\code |
375 | 375 |
/// int count=0; |
376 | 376 |
/// for(Graph::ArcIt a(g); a!=INVALID; ++a) ++count; |
377 | 377 |
///\endcode |
378 | 378 |
class ArcIt : public Arc { |
379 | 379 |
public: |
380 | 380 |
/// Default constructor |
381 | 381 |
|
382 | 382 |
/// Default constructor. |
383 | 383 |
/// \warning It sets the iterator to an undefined value. |
384 | 384 |
ArcIt() { } |
385 | 385 |
/// Copy constructor. |
386 | 386 |
|
387 | 387 |
/// Copy constructor. |
388 | 388 |
/// |
389 | 389 |
ArcIt(const ArcIt& e) : Arc(e) { } |
390 | 390 |
/// %Invalid constructor \& conversion. |
391 | 391 |
|
392 | 392 |
/// Initializes the iterator to be invalid. |
393 | 393 |
/// \sa Invalid for more details. |
394 | 394 |
ArcIt(Invalid) { } |
395 | 395 |
/// Sets the iterator to the first arc. |
396 | 396 |
|
397 | 397 |
/// Sets the iterator to the first arc of the given graph. |
398 | 398 |
/// |
399 | 399 |
explicit ArcIt(const Graph &g) { ignore_unused_variable_warning(g); } |
400 | 400 |
/// Sets the iterator to the given arc. |
401 | 401 |
|
402 | 402 |
/// Sets the iterator to the given arc of the given graph. |
403 | 403 |
/// |
404 | 404 |
ArcIt(const Graph&, const Arc&) { } |
405 | 405 |
/// Next arc |
406 | 406 |
|
407 | 407 |
/// Assign the iterator to the next arc. |
408 | 408 |
/// |
409 | 409 |
ArcIt& operator++() { return *this; } |
410 | 410 |
}; |
411 | 411 |
|
412 | 412 |
/// Iterator class for the outgoing arcs of a node. |
413 | 413 |
|
414 | 414 |
/// This iterator goes trough the \e outgoing directed arcs of a |
415 | 415 |
/// certain node of a graph. |
416 |
/// Its usage is quite simple, for example you can count the number |
|
416 |
/// Its usage is quite simple, for example, you can count the number |
|
417 | 417 |
/// of outgoing arcs of a node \c n |
418 | 418 |
/// in a graph \c g of type \c %Graph as follows. |
419 | 419 |
///\code |
420 | 420 |
/// int count=0; |
421 | 421 |
/// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count; |
422 | 422 |
///\endcode |
423 | 423 |
class OutArcIt : public Arc { |
424 | 424 |
public: |
425 | 425 |
/// Default constructor |
426 | 426 |
|
427 | 427 |
/// Default constructor. |
428 | 428 |
/// \warning It sets the iterator to an undefined value. |
429 | 429 |
OutArcIt() { } |
430 | 430 |
/// Copy constructor. |
431 | 431 |
|
432 | 432 |
/// Copy constructor. |
433 | 433 |
/// |
434 | 434 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
435 | 435 |
/// %Invalid constructor \& conversion. |
436 | 436 |
|
437 | 437 |
/// Initializes the iterator to be invalid. |
438 | 438 |
/// \sa Invalid for more details. |
439 | 439 |
OutArcIt(Invalid) { } |
440 | 440 |
/// Sets the iterator to the first outgoing arc. |
441 | 441 |
|
442 | 442 |
/// Sets the iterator to the first outgoing arc of the given node. |
443 | 443 |
/// |
444 | 444 |
OutArcIt(const Graph& n, const Node& g) { |
445 | 445 |
ignore_unused_variable_warning(n); |
446 | 446 |
ignore_unused_variable_warning(g); |
447 | 447 |
} |
448 | 448 |
/// Sets the iterator to the given arc. |
449 | 449 |
|
450 | 450 |
/// Sets the iterator to the given arc of the given graph. |
451 | 451 |
/// |
452 | 452 |
OutArcIt(const Graph&, const Arc&) { } |
453 | 453 |
/// Next outgoing arc |
454 | 454 |
|
455 | 455 |
/// Assign the iterator to the next |
456 | 456 |
/// outgoing arc of the corresponding node. |
457 | 457 |
OutArcIt& operator++() { return *this; } |
458 | 458 |
}; |
459 | 459 |
|
460 | 460 |
/// Iterator class for the incoming arcs of a node. |
461 | 461 |
|
462 | 462 |
/// This iterator goes trough the \e incoming directed arcs of a |
463 | 463 |
/// certain node of a graph. |
464 |
/// Its usage is quite simple, for example you can count the number |
|
464 |
/// Its usage is quite simple, for example, you can count the number |
|
465 | 465 |
/// of incoming arcs of a node \c n |
466 | 466 |
/// in a graph \c g of type \c %Graph as follows. |
467 | 467 |
///\code |
468 | 468 |
/// int count=0; |
469 | 469 |
/// for (Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count; |
470 | 470 |
///\endcode |
471 | 471 |
class InArcIt : public Arc { |
472 | 472 |
public: |
473 | 473 |
/// Default constructor |
474 | 474 |
|
475 | 475 |
/// Default constructor. |
476 | 476 |
/// \warning It sets the iterator to an undefined value. |
477 | 477 |
InArcIt() { } |
478 | 478 |
/// Copy constructor. |
479 | 479 |
|
480 | 480 |
/// Copy constructor. |
481 | 481 |
/// |
482 | 482 |
InArcIt(const InArcIt& e) : Arc(e) { } |
483 | 483 |
/// %Invalid constructor \& conversion. |
484 | 484 |
|
485 | 485 |
/// Initializes the iterator to be invalid. |
486 | 486 |
/// \sa Invalid for more details. |
487 | 487 |
InArcIt(Invalid) { } |
488 | 488 |
/// Sets the iterator to the first incoming arc. |
489 | 489 |
|
490 | 490 |
/// Sets the iterator to the first incoming arc of the given node. |
491 | 491 |
/// |
492 | 492 |
InArcIt(const Graph& g, const Node& n) { |
493 | 493 |
ignore_unused_variable_warning(n); |
494 | 494 |
ignore_unused_variable_warning(g); |
495 | 495 |
} |
496 | 496 |
/// Sets the iterator to the given arc. |
497 | 497 |
|
498 | 498 |
/// Sets the iterator to the given arc of the given graph. |
499 | 499 |
/// |
500 | 500 |
InArcIt(const Graph&, const Arc&) { } |
501 | 501 |
/// Next incoming arc |
502 | 502 |
|
503 | 503 |
/// Assign the iterator to the next |
504 | 504 |
/// incoming arc of the corresponding node. |
505 | 505 |
InArcIt& operator++() { return *this; } |
506 | 506 |
}; |
507 | 507 |
|
508 | 508 |
/// \brief Standard graph map type for the nodes. |
509 | 509 |
/// |
510 | 510 |
/// Standard graph map type for the nodes. |
511 | 511 |
/// It conforms to the ReferenceMap concept. |
512 | 512 |
template<class T> |
513 | 513 |
class NodeMap : public ReferenceMap<Node, T, T&, const T&> |
514 | 514 |
{ |
515 | 515 |
public: |
516 | 516 |
|
517 | 517 |
/// Constructor |
518 | 518 |
explicit NodeMap(const Graph&) { } |
519 | 519 |
/// Constructor with given initial value |
520 | 520 |
NodeMap(const Graph&, T) { } |
521 | 521 |
|
522 | 522 |
private: |
523 | 523 |
///Copy constructor |
524 | 524 |
NodeMap(const NodeMap& nm) : |
525 | 525 |
ReferenceMap<Node, T, T&, const T&>(nm) { } |
526 | 526 |
///Assignment operator |
527 | 527 |
template <typename CMap> |
528 | 528 |
NodeMap& operator=(const CMap&) { |
529 | 529 |
checkConcept<ReadMap<Node, T>, CMap>(); |
530 | 530 |
return *this; |
531 | 531 |
} |
532 | 532 |
}; |
533 | 533 |
|
534 | 534 |
/// \brief Standard graph map type for the arcs. |
535 | 535 |
/// |
536 | 536 |
/// Standard graph map type for the arcs. |
537 | 537 |
/// It conforms to the ReferenceMap concept. |
538 | 538 |
template<class T> |
539 | 539 |
class ArcMap : public ReferenceMap<Arc, T, T&, const T&> |
540 | 540 |
{ |
541 | 541 |
public: |
542 | 542 |
|
543 | 543 |
/// Constructor |
544 | 544 |
explicit ArcMap(const Graph&) { } |
545 | 545 |
/// Constructor with given initial value |
546 | 546 |
ArcMap(const Graph&, T) { } |
547 | 547 |
|
548 | 548 |
private: |
549 | 549 |
///Copy constructor |
550 | 550 |
ArcMap(const ArcMap& em) : |
551 | 551 |
ReferenceMap<Arc, T, T&, const T&>(em) { } |
552 | 552 |
///Assignment operator |
553 | 553 |
template <typename CMap> |
554 | 554 |
ArcMap& operator=(const CMap&) { |
555 | 555 |
checkConcept<ReadMap<Arc, T>, CMap>(); |
556 | 556 |
return *this; |
557 | 557 |
} |
558 | 558 |
}; |
559 | 559 |
|
560 | 560 |
/// \brief Standard graph map type for the edges. |
561 | 561 |
/// |
562 | 562 |
/// Standard graph map type for the edges. |
563 | 563 |
/// It conforms to the ReferenceMap concept. |
564 | 564 |
template<class T> |
565 | 565 |
class EdgeMap : public ReferenceMap<Edge, T, T&, const T&> |
566 | 566 |
{ |
567 | 567 |
public: |
568 | 568 |
|
569 | 569 |
/// Constructor |
570 | 570 |
explicit EdgeMap(const Graph&) { } |
571 | 571 |
/// Constructor with given initial value |
572 | 572 |
EdgeMap(const Graph&, T) { } |
573 | 573 |
|
574 | 574 |
private: |
575 | 575 |
///Copy constructor |
576 | 576 |
EdgeMap(const EdgeMap& em) : |
577 | 577 |
ReferenceMap<Edge, T, T&, const T&>(em) {} |
578 | 578 |
///Assignment operator |
579 | 579 |
template <typename CMap> |
580 | 580 |
EdgeMap& operator=(const CMap&) { |
581 | 581 |
checkConcept<ReadMap<Edge, T>, CMap>(); |
582 | 582 |
return *this; |
583 | 583 |
} |
584 | 584 |
}; |
585 | 585 |
|
586 | 586 |
/// \brief The first node of the edge. |
587 | 587 |
/// |
588 | 588 |
/// Returns the first node of the given edge. |
589 | 589 |
/// |
590 |
/// Edges don't have source and target nodes, however methods |
|
590 |
/// Edges don't have source and target nodes, however, methods |
|
591 | 591 |
/// u() and v() are used to query the two end-nodes of an edge. |
592 | 592 |
/// The orientation of an edge that arises this way is called |
593 | 593 |
/// the inherent direction, it is used to define the default |
594 | 594 |
/// direction for the corresponding arcs. |
595 | 595 |
/// \sa v() |
596 | 596 |
/// \sa direction() |
597 | 597 |
Node u(Edge) const { return INVALID; } |
598 | 598 |
|
599 | 599 |
/// \brief The second node of the edge. |
600 | 600 |
/// |
601 | 601 |
/// Returns the second node of the given edge. |
602 | 602 |
/// |
603 |
/// Edges don't have source and target nodes, however methods |
|
603 |
/// Edges don't have source and target nodes, however, methods |
|
604 | 604 |
/// u() and v() are used to query the two end-nodes of an edge. |
605 | 605 |
/// The orientation of an edge that arises this way is called |
606 | 606 |
/// the inherent direction, it is used to define the default |
607 | 607 |
/// direction for the corresponding arcs. |
608 | 608 |
/// \sa u() |
609 | 609 |
/// \sa direction() |
610 | 610 |
Node v(Edge) const { return INVALID; } |
611 | 611 |
|
612 | 612 |
/// \brief The source node of the arc. |
613 | 613 |
/// |
614 | 614 |
/// Returns the source node of the given arc. |
615 | 615 |
Node source(Arc) const { return INVALID; } |
616 | 616 |
|
617 | 617 |
/// \brief The target node of the arc. |
618 | 618 |
/// |
619 | 619 |
/// Returns the target node of the given arc. |
620 | 620 |
Node target(Arc) const { return INVALID; } |
621 | 621 |
|
622 | 622 |
/// \brief The ID of the node. |
623 | 623 |
/// |
624 | 624 |
/// Returns the ID of the given node. |
625 | 625 |
int id(Node) const { return -1; } |
626 | 626 |
|
627 | 627 |
/// \brief The ID of the edge. |
628 | 628 |
/// |
629 | 629 |
/// Returns the ID of the given edge. |
630 | 630 |
int id(Edge) const { return -1; } |
631 | 631 |
|
632 | 632 |
/// \brief The ID of the arc. |
633 | 633 |
/// |
634 | 634 |
/// Returns the ID of the given arc. |
635 | 635 |
int id(Arc) const { return -1; } |
636 | 636 |
|
637 | 637 |
/// \brief The node with the given ID. |
638 | 638 |
/// |
639 | 639 |
/// Returns the node with the given ID. |
640 | 640 |
/// \pre The argument should be a valid node ID in the graph. |
641 | 641 |
Node nodeFromId(int) const { return INVALID; } |
642 | 642 |
|
643 | 643 |
/// \brief The edge with the given ID. |
644 | 644 |
/// |
645 | 645 |
/// Returns the edge with the given ID. |
646 | 646 |
/// \pre The argument should be a valid edge ID in the graph. |
647 | 647 |
Edge edgeFromId(int) const { return INVALID; } |
648 | 648 |
|
649 | 649 |
/// \brief The arc with the given ID. |
650 | 650 |
/// |
651 | 651 |
/// Returns the arc with the given ID. |
652 | 652 |
/// \pre The argument should be a valid arc ID in the graph. |
653 | 653 |
Arc arcFromId(int) const { return INVALID; } |
654 | 654 |
|
655 | 655 |
/// \brief An upper bound on the node IDs. |
656 | 656 |
/// |
657 | 657 |
/// Returns an upper bound on the node IDs. |
658 | 658 |
int maxNodeId() const { return -1; } |
659 | 659 |
|
660 | 660 |
/// \brief An upper bound on the edge IDs. |
661 | 661 |
/// |
662 | 662 |
/// Returns an upper bound on the edge IDs. |
663 | 663 |
int maxEdgeId() const { return -1; } |
664 | 664 |
|
665 | 665 |
/// \brief An upper bound on the arc IDs. |
666 | 666 |
/// |
667 | 667 |
/// Returns an upper bound on the arc IDs. |
668 | 668 |
int maxArcId() const { return -1; } |
669 | 669 |
|
670 | 670 |
/// \brief The direction of the arc. |
671 | 671 |
/// |
672 | 672 |
/// Returns \c true if the direction of the given arc is the same as |
673 | 673 |
/// the inherent orientation of the represented edge. |
674 | 674 |
bool direction(Arc) const { return true; } |
675 | 675 |
|
676 | 676 |
/// \brief Direct the edge. |
677 | 677 |
/// |
678 | 678 |
/// Direct the given edge. The returned arc |
679 | 679 |
/// represents the given edge and its direction comes |
680 | 680 |
/// from the bool parameter. If it is \c true, then the direction |
681 | 681 |
/// of the arc is the same as the inherent orientation of the edge. |
682 | 682 |
Arc direct(Edge, bool) const { |
683 | 683 |
return INVALID; |
684 | 684 |
} |
685 | 685 |
|
686 | 686 |
/// \brief Direct the edge. |
687 | 687 |
/// |
688 | 688 |
/// Direct the given edge. The returned arc represents the given |
689 | 689 |
/// edge and its source node is the given node. |
690 | 690 |
Arc direct(Edge, Node) const { |
691 | 691 |
return INVALID; |
692 | 692 |
} |
693 | 693 |
|
694 | 694 |
/// \brief The oppositely directed arc. |
695 | 695 |
/// |
696 | 696 |
/// Returns the oppositely directed arc representing the same edge. |
697 | 697 |
Arc oppositeArc(Arc) const { return INVALID; } |
698 | 698 |
|
699 | 699 |
/// \brief The opposite node on the edge. |
700 | 700 |
/// |
701 | 701 |
/// Returns the opposite node on the given edge. |
702 | 702 |
Node oppositeNode(Node, Edge) const { return INVALID; } |
703 | 703 |
|
704 | 704 |
void first(Node&) const {} |
705 | 705 |
void next(Node&) const {} |
706 | 706 |
|
707 | 707 |
void first(Edge&) const {} |
708 | 708 |
void next(Edge&) const {} |
709 | 709 |
|
710 | 710 |
void first(Arc&) const {} |
711 | 711 |
void next(Arc&) const {} |
712 | 712 |
|
713 | 713 |
void firstOut(Arc&, Node) const {} |
714 | 714 |
void nextOut(Arc&) const {} |
715 | 715 |
|
716 | 716 |
void firstIn(Arc&, Node) const {} |
717 | 717 |
void nextIn(Arc&) const {} |
718 | 718 |
|
719 | 719 |
void firstInc(Edge &, bool &, const Node &) const {} |
720 | 720 |
void nextInc(Edge &, bool &) const {} |
721 | 721 |
|
722 | 722 |
// The second parameter is dummy. |
723 | 723 |
Node fromId(int, Node) const { return INVALID; } |
724 | 724 |
// The second parameter is dummy. |
725 | 725 |
Edge fromId(int, Edge) const { return INVALID; } |
726 | 726 |
// The second parameter is dummy. |
727 | 727 |
Arc fromId(int, Arc) const { return INVALID; } |
728 | 728 |
|
729 | 729 |
// Dummy parameter. |
730 | 730 |
int maxId(Node) const { return -1; } |
731 | 731 |
// Dummy parameter. |
732 | 732 |
int maxId(Edge) const { return -1; } |
733 | 733 |
// Dummy parameter. |
734 | 734 |
int maxId(Arc) const { return -1; } |
735 | 735 |
|
736 | 736 |
/// \brief The base node of the iterator. |
737 | 737 |
/// |
738 | 738 |
/// Returns the base node of the given incident edge iterator. |
739 | 739 |
Node baseNode(IncEdgeIt) const { return INVALID; } |
740 | 740 |
|
741 | 741 |
/// \brief The running node of the iterator. |
742 | 742 |
/// |
743 | 743 |
/// Returns the running node of the given incident edge iterator. |
744 | 744 |
Node runningNode(IncEdgeIt) const { return INVALID; } |
745 | 745 |
|
746 | 746 |
/// \brief The base node of the iterator. |
747 | 747 |
/// |
748 | 748 |
/// Returns the base node of the given outgoing arc iterator |
749 | 749 |
/// (i.e. the source node of the corresponding arc). |
750 | 750 |
Node baseNode(OutArcIt) const { return INVALID; } |
751 | 751 |
|
752 | 752 |
/// \brief The running node of the iterator. |
753 | 753 |
/// |
754 | 754 |
/// Returns the running node of the given outgoing arc iterator |
755 | 755 |
/// (i.e. the target node of the corresponding arc). |
756 | 756 |
Node runningNode(OutArcIt) const { return INVALID; } |
757 | 757 |
|
758 | 758 |
/// \brief The base node of the iterator. |
759 | 759 |
/// |
760 | 760 |
/// Returns the base node of the given incomming arc iterator |
761 | 761 |
/// (i.e. the target node of the corresponding arc). |
762 | 762 |
Node baseNode(InArcIt) const { return INVALID; } |
763 | 763 |
|
764 | 764 |
/// \brief The running node of the iterator. |
765 | 765 |
/// |
766 | 766 |
/// Returns the running node of the given incomming arc iterator |
767 | 767 |
/// (i.e. the source node of the corresponding arc). |
768 | 768 |
Node runningNode(InArcIt) const { return INVALID; } |
769 | 769 |
|
770 | 770 |
template <typename _Graph> |
771 | 771 |
struct Constraints { |
772 | 772 |
void constraints() { |
773 | 773 |
checkConcept<BaseGraphComponent, _Graph>(); |
774 | 774 |
checkConcept<IterableGraphComponent<>, _Graph>(); |
775 | 775 |
checkConcept<IDableGraphComponent<>, _Graph>(); |
776 | 776 |
checkConcept<MappableGraphComponent<>, _Graph>(); |
777 | 777 |
} |
778 | 778 |
}; |
779 | 779 |
|
780 | 780 |
}; |
781 | 781 |
|
782 | 782 |
} |
783 | 783 |
|
784 | 784 |
} |
785 | 785 |
|
786 | 786 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 |
///\brief The |
|
21 |
///\brief The concepts of graph components. |
|
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
24 | 24 |
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
#include <lemon/bits/alteration_notifier.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
namespace concepts { |
33 | 33 |
|
34 | 34 |
/// \brief Concept class for \c Node, \c Arc and \c Edge types. |
35 | 35 |
/// |
36 | 36 |
/// This class describes the concept of \c Node, \c Arc and \c Edge |
37 | 37 |
/// subtypes of digraph and graph types. |
38 | 38 |
/// |
39 | 39 |
/// \note This class is a template class so that we can use it to |
40 | 40 |
/// create graph skeleton classes. The reason for this is that \c Node |
41 | 41 |
/// and \c Arc (or \c Edge) types should \e not derive from the same |
42 | 42 |
/// base class. For \c Node you should instantiate it with character |
43 | 43 |
/// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'. |
44 | 44 |
#ifndef DOXYGEN |
45 | 45 |
template <char sel = '0'> |
46 | 46 |
#endif |
47 | 47 |
class GraphItem { |
48 | 48 |
public: |
49 | 49 |
/// \brief Default constructor. |
50 | 50 |
/// |
51 | 51 |
/// Default constructor. |
52 | 52 |
/// \warning The default constructor is not required to set |
53 | 53 |
/// the item to some well-defined value. So you should consider it |
54 | 54 |
/// as uninitialized. |
55 | 55 |
GraphItem() {} |
56 | 56 |
|
57 | 57 |
/// \brief Copy constructor. |
58 | 58 |
/// |
59 | 59 |
/// Copy constructor. |
60 | 60 |
GraphItem(const GraphItem &) {} |
61 | 61 |
|
62 | 62 |
/// \brief Constructor for conversion from \c INVALID. |
63 | 63 |
/// |
64 | 64 |
/// Constructor for conversion from \c INVALID. |
65 | 65 |
/// It initializes the item to be invalid. |
66 | 66 |
/// \sa Invalid for more details. |
67 | 67 |
GraphItem(Invalid) {} |
68 | 68 |
|
69 | 69 |
/// \brief Assignment operator. |
70 | 70 |
/// |
71 | 71 |
/// Assignment operator for the item. |
72 | 72 |
GraphItem& operator=(const GraphItem&) { return *this; } |
73 | 73 |
|
74 | 74 |
/// \brief Assignment operator for INVALID. |
75 | 75 |
/// |
76 | 76 |
/// This operator makes the item invalid. |
77 | 77 |
GraphItem& operator=(Invalid) { return *this; } |
78 | 78 |
|
79 | 79 |
/// \brief Equality operator. |
80 | 80 |
/// |
81 | 81 |
/// Equality operator. |
82 | 82 |
bool operator==(const GraphItem&) const { return false; } |
83 | 83 |
|
84 | 84 |
/// \brief Inequality operator. |
85 | 85 |
/// |
86 | 86 |
/// Inequality operator. |
87 | 87 |
bool operator!=(const GraphItem&) const { return false; } |
88 | 88 |
|
89 | 89 |
/// \brief Ordering operator. |
90 | 90 |
/// |
91 | 91 |
/// This operator defines an ordering of the items. |
92 | 92 |
/// It makes possible to use graph item types as key types in |
93 | 93 |
/// associative containers (e.g. \c std::map). |
94 | 94 |
/// |
95 | 95 |
/// \note This operator only has to define some strict ordering of |
96 | 96 |
/// the items; this order has nothing to do with the iteration |
97 | 97 |
/// ordering of the items. |
98 | 98 |
bool operator<(const GraphItem&) const { return false; } |
99 | 99 |
|
100 | 100 |
template<typename _GraphItem> |
101 | 101 |
struct Constraints { |
102 | 102 |
void constraints() { |
103 | 103 |
_GraphItem i1; |
104 | 104 |
i1=INVALID; |
105 | 105 |
_GraphItem i2 = i1; |
106 | 106 |
_GraphItem i3 = INVALID; |
107 | 107 |
|
108 | 108 |
i1 = i2 = i3; |
109 | 109 |
|
110 | 110 |
bool b; |
111 | 111 |
b = (ia == ib) && (ia != ib); |
112 | 112 |
b = (ia == INVALID) && (ib != INVALID); |
113 | 113 |
b = (ia < ib); |
114 | 114 |
} |
115 | 115 |
|
116 | 116 |
const _GraphItem &ia; |
117 | 117 |
const _GraphItem &ib; |
118 | 118 |
}; |
119 | 119 |
}; |
120 | 120 |
|
121 | 121 |
/// \brief Base skeleton class for directed graphs. |
122 | 122 |
/// |
123 | 123 |
/// This class describes the base interface of directed graph types. |
124 | 124 |
/// All digraph %concepts have to conform to this class. |
125 | 125 |
/// It just provides types for nodes and arcs and functions |
126 | 126 |
/// to get the source and the target nodes of arcs. |
127 | 127 |
class BaseDigraphComponent { |
128 | 128 |
public: |
129 | 129 |
|
130 | 130 |
typedef BaseDigraphComponent Digraph; |
131 | 131 |
|
132 | 132 |
/// \brief Node class of the digraph. |
133 | 133 |
/// |
134 | 134 |
/// This class represents the nodes of the digraph. |
135 | 135 |
typedef GraphItem<'n'> Node; |
136 | 136 |
|
137 | 137 |
/// \brief Arc class of the digraph. |
138 | 138 |
/// |
139 | 139 |
/// This class represents the arcs of the digraph. |
140 | 140 |
typedef GraphItem<'a'> Arc; |
141 | 141 |
|
142 | 142 |
/// \brief Return the source node of an arc. |
143 | 143 |
/// |
144 | 144 |
/// This function returns the source node of an arc. |
145 | 145 |
Node source(const Arc&) const { return INVALID; } |
146 | 146 |
|
147 | 147 |
/// \brief Return the target node of an arc. |
148 | 148 |
/// |
149 | 149 |
/// This function returns the target node of an arc. |
150 | 150 |
Node target(const Arc&) const { return INVALID; } |
151 | 151 |
|
152 | 152 |
/// \brief Return the opposite node on the given arc. |
153 | 153 |
/// |
154 | 154 |
/// This function returns the opposite node on the given arc. |
155 | 155 |
Node oppositeNode(const Node&, const Arc&) const { |
156 | 156 |
return INVALID; |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
template <typename _Digraph> |
160 | 160 |
struct Constraints { |
161 | 161 |
typedef typename _Digraph::Node Node; |
162 | 162 |
typedef typename _Digraph::Arc Arc; |
163 | 163 |
|
164 | 164 |
void constraints() { |
165 | 165 |
checkConcept<GraphItem<'n'>, Node>(); |
166 | 166 |
checkConcept<GraphItem<'a'>, Arc>(); |
167 | 167 |
{ |
168 | 168 |
Node n; |
169 | 169 |
Arc e(INVALID); |
170 | 170 |
n = digraph.source(e); |
171 | 171 |
n = digraph.target(e); |
172 | 172 |
n = digraph.oppositeNode(n, e); |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
const _Digraph& digraph; |
177 | 177 |
}; |
178 | 178 |
}; |
179 | 179 |
|
180 | 180 |
/// \brief Base skeleton class for undirected graphs. |
181 | 181 |
/// |
182 | 182 |
/// This class describes the base interface of undirected graph types. |
183 | 183 |
/// All graph %concepts have to conform to this class. |
184 | 184 |
/// It extends the interface of \ref BaseDigraphComponent with an |
185 | 185 |
/// \c Edge type and functions to get the end nodes of edges, |
186 | 186 |
/// to convert from arcs to edges and to get both direction of edges. |
187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent { |
188 | 188 |
public: |
189 | 189 |
|
190 | 190 |
typedef BaseGraphComponent Graph; |
191 | 191 |
|
192 | 192 |
typedef BaseDigraphComponent::Node Node; |
193 | 193 |
typedef BaseDigraphComponent::Arc Arc; |
194 | 194 |
|
195 | 195 |
/// \brief Undirected edge class of the graph. |
196 | 196 |
/// |
197 | 197 |
/// This class represents the undirected edges of the graph. |
198 | 198 |
/// Undirected graphs can be used as directed graphs, each edge is |
199 | 199 |
/// represented by two opposite directed arcs. |
200 | 200 |
class Edge : public GraphItem<'e'> { |
201 | 201 |
typedef GraphItem<'e'> Parent; |
202 | 202 |
|
203 | 203 |
public: |
204 | 204 |
/// \brief Default constructor. |
205 | 205 |
/// |
206 | 206 |
/// Default constructor. |
207 | 207 |
/// \warning The default constructor is not required to set |
208 | 208 |
/// the item to some well-defined value. So you should consider it |
209 | 209 |
/// as uninitialized. |
210 | 210 |
Edge() {} |
211 | 211 |
|
212 | 212 |
/// \brief Copy constructor. |
213 | 213 |
/// |
... | ... |
@@ -23,227 +23,227 @@ |
23 | 23 |
#include <iostream> |
24 | 24 |
|
25 | 25 |
///\ingroup timecount |
26 | 26 |
///\file |
27 | 27 |
///\brief Tools for counting steps and events |
28 | 28 |
|
29 | 29 |
namespace lemon |
30 | 30 |
{ |
31 | 31 |
|
32 | 32 |
template<class P> class _NoSubCounter; |
33 | 33 |
|
34 | 34 |
template<class P> |
35 | 35 |
class _SubCounter |
36 | 36 |
{ |
37 | 37 |
P &_parent; |
38 | 38 |
std::string _title; |
39 | 39 |
std::ostream &_os; |
40 | 40 |
int count; |
41 | 41 |
public: |
42 | 42 |
|
43 | 43 |
typedef _SubCounter<_SubCounter<P> > SubCounter; |
44 | 44 |
typedef _NoSubCounter<_SubCounter<P> > NoSubCounter; |
45 | 45 |
|
46 | 46 |
_SubCounter(P &parent) |
47 | 47 |
: _parent(parent), _title(), _os(std::cerr), count(0) {} |
48 | 48 |
_SubCounter(P &parent,std::string title,std::ostream &os=std::cerr) |
49 | 49 |
: _parent(parent), _title(title), _os(os), count(0) {} |
50 | 50 |
_SubCounter(P &parent,const char *title,std::ostream &os=std::cerr) |
51 | 51 |
: _parent(parent), _title(title), _os(os), count(0) {} |
52 | 52 |
~_SubCounter() { |
53 | 53 |
_os << _title << count <<std::endl; |
54 | 54 |
_parent+=count; |
55 | 55 |
} |
56 | 56 |
_SubCounter &operator++() { count++; return *this;} |
57 | 57 |
int operator++(int) { return count++; } |
58 | 58 |
_SubCounter &operator--() { count--; return *this;} |
59 | 59 |
int operator--(int) { return count--; } |
60 | 60 |
_SubCounter &operator+=(int c) { count+=c; return *this;} |
61 | 61 |
_SubCounter &operator-=(int c) { count-=c; return *this;} |
62 | 62 |
operator int() {return count;} |
63 | 63 |
}; |
64 | 64 |
|
65 | 65 |
template<class P> |
66 | 66 |
class _NoSubCounter |
67 | 67 |
{ |
68 | 68 |
P &_parent; |
69 | 69 |
public: |
70 | 70 |
typedef _NoSubCounter<_NoSubCounter<P> > SubCounter; |
71 | 71 |
typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter; |
72 | 72 |
|
73 | 73 |
_NoSubCounter(P &parent) :_parent(parent) {} |
74 | 74 |
_NoSubCounter(P &parent,std::string,std::ostream &) |
75 | 75 |
:_parent(parent) {} |
76 | 76 |
_NoSubCounter(P &parent,std::string) |
77 | 77 |
:_parent(parent) {} |
78 | 78 |
_NoSubCounter(P &parent,const char *,std::ostream &) |
79 | 79 |
:_parent(parent) {} |
80 | 80 |
_NoSubCounter(P &parent,const char *) |
81 | 81 |
:_parent(parent) {} |
82 | 82 |
~_NoSubCounter() {} |
83 | 83 |
_NoSubCounter &operator++() { ++_parent; return *this;} |
84 | 84 |
int operator++(int) { _parent++; return 0;} |
85 | 85 |
_NoSubCounter &operator--() { --_parent; return *this;} |
86 | 86 |
int operator--(int) { _parent--; return 0;} |
87 | 87 |
_NoSubCounter &operator+=(int c) { _parent+=c; return *this;} |
88 | 88 |
_NoSubCounter &operator-=(int c) { _parent-=c; return *this;} |
89 | 89 |
operator int() {return 0;} |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup timecount |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// A counter class |
97 | 97 |
|
98 | 98 |
/// This class makes it easier to count certain events (e.g. for debug |
99 | 99 |
/// reasons). |
100 | 100 |
/// You can increment or decrement the counter using \c operator++, |
101 | 101 |
/// \c operator--, \c operator+= and \c operator-=. You can also |
102 | 102 |
/// define subcounters for the different phases of the algorithm or |
103 | 103 |
/// for different types of operations. |
104 | 104 |
/// A report containing the given title and the value of the counter |
105 | 105 |
/// is automatically printed on destruction. |
106 | 106 |
/// |
107 | 107 |
/// The following example shows the usage of counters and subcounters. |
108 | 108 |
/// \code |
109 | 109 |
/// // Bubble sort |
110 | 110 |
/// std::vector<T> v; |
111 | 111 |
/// ... |
112 | 112 |
/// Counter op("Operations: "); |
113 | 113 |
/// Counter::SubCounter as(op, "Assignments: "); |
114 | 114 |
/// Counter::SubCounter co(op, "Comparisons: "); |
115 | 115 |
/// for (int i = v.size()-1; i > 0; --i) { |
116 | 116 |
/// for (int j = 0; j < i; ++j) { |
117 | 117 |
/// if (v[j] > v[j+1]) { |
118 | 118 |
/// T tmp = v[j]; |
119 | 119 |
/// v[j] = v[j+1]; |
120 | 120 |
/// v[j+1] = tmp; |
121 | 121 |
/// as += 3; // three assignments |
122 | 122 |
/// } |
123 | 123 |
/// ++co; // one comparison |
124 | 124 |
/// } |
125 | 125 |
/// } |
126 | 126 |
/// \endcode |
127 | 127 |
/// |
128 | 128 |
/// This code prints out something like that: |
129 | 129 |
/// \code |
130 | 130 |
/// Comparisons: 45 |
131 | 131 |
/// Assignments: 57 |
132 | 132 |
/// Operations: 102 |
133 | 133 |
/// \endcode |
134 | 134 |
/// |
135 | 135 |
/// \sa NoCounter |
136 | 136 |
class Counter |
137 | 137 |
{ |
138 | 138 |
std::string _title; |
139 | 139 |
std::ostream &_os; |
140 | 140 |
int count; |
141 | 141 |
public: |
142 | 142 |
|
143 | 143 |
/// SubCounter class |
144 | 144 |
|
145 | 145 |
/// This class can be used to setup subcounters for a \ref Counter |
146 | 146 |
/// to have finer reports. A subcounter provides exactly the same |
147 | 147 |
/// operations as the main \ref Counter, but it also increments and |
148 | 148 |
/// decrements the value of its parent. |
149 | 149 |
/// Subcounters can also have subcounters. |
150 | 150 |
/// |
151 | 151 |
/// The parent counter must be given as the first parameter of the |
152 | 152 |
/// constructor. Apart from that a title and an \c ostream object |
153 | 153 |
/// can also be given just like for the main \ref Counter. |
154 | 154 |
/// |
155 | 155 |
/// A report containing the given title and the value of the |
156 | 156 |
/// subcounter is automatically printed on destruction. If you |
157 | 157 |
/// would like to turn off this report, use \ref NoSubCounter |
158 | 158 |
/// instead. |
159 | 159 |
/// |
160 | 160 |
/// \sa NoSubCounter |
161 | 161 |
typedef _SubCounter<Counter> SubCounter; |
162 | 162 |
|
163 | 163 |
/// SubCounter class without printing report on destruction |
164 | 164 |
|
165 | 165 |
/// This class can be used to setup subcounters for a \ref Counter. |
166 | 166 |
/// It is the same as \ref SubCounter but it does not print report |
167 | 167 |
/// on destruction. (It modifies the value of its parent, so 'No' |
168 | 168 |
/// only means 'do not print'.) |
169 | 169 |
/// |
170 | 170 |
/// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter |
171 | 171 |
/// "NoSubCounter"s makes it possible to turn off reporting |
172 | 172 |
/// subcounter values without actually removing the definitions |
173 | 173 |
/// and the increment or decrement operators. |
174 | 174 |
/// |
175 | 175 |
/// \sa SubCounter |
176 | 176 |
typedef _NoSubCounter<Counter> NoSubCounter; |
177 | 177 |
|
178 | 178 |
/// Constructor. |
179 | 179 |
Counter() : _title(), _os(std::cerr), count(0) {} |
180 | 180 |
/// Constructor. |
181 | 181 |
Counter(std::string title,std::ostream &os=std::cerr) |
182 | 182 |
: _title(title), _os(os), count(0) {} |
183 | 183 |
/// Constructor. |
184 | 184 |
Counter(const char *title,std::ostream &os=std::cerr) |
185 | 185 |
: _title(title), _os(os), count(0) {} |
186 | 186 |
/// Destructor. Prints the given title and the value of the counter. |
187 | 187 |
~Counter() { |
188 | 188 |
_os << _title << count <<std::endl; |
189 | 189 |
} |
190 | 190 |
///\e |
191 | 191 |
Counter &operator++() { count++; return *this;} |
192 | 192 |
///\e |
193 | 193 |
int operator++(int) { return count++;} |
194 | 194 |
///\e |
195 | 195 |
Counter &operator--() { count--; return *this;} |
196 | 196 |
///\e |
197 | 197 |
int operator--(int) { return count--;} |
198 | 198 |
///\e |
199 | 199 |
Counter &operator+=(int c) { count+=c; return *this;} |
200 | 200 |
///\e |
201 | 201 |
Counter &operator-=(int c) { count-=c; return *this;} |
202 | 202 |
/// Resets the counter to the given value. |
203 | 203 |
|
204 | 204 |
/// Resets the counter to the given value. |
205 | 205 |
/// \note This function does not reset the values of |
206 | 206 |
/// \ref SubCounter "SubCounter"s but it resets \ref NoSubCounter |
207 | 207 |
/// "NoSubCounter"s along with the main counter. |
208 | 208 |
void reset(int c=0) {count=c;} |
209 | 209 |
/// Returns the value of the counter. |
210 | 210 |
operator int() {return count;} |
211 | 211 |
}; |
212 | 212 |
|
213 | 213 |
/// 'Do nothing' version of Counter. |
214 | 214 |
|
215 |
/// This class can be used in the same way as \ref Counter |
|
215 |
/// This class can be used in the same way as \ref Counter, but it |
|
216 | 216 |
/// does not count at all and does not print report on destruction. |
217 | 217 |
/// |
218 | 218 |
/// Replacing a \ref Counter with a \ref NoCounter makes it possible |
219 | 219 |
/// to turn off all counting and reporting (SubCounters should also |
220 | 220 |
/// be replaced with NoSubCounters), so it does not affect the |
221 | 221 |
/// efficiency of the program at all. |
222 | 222 |
/// |
223 | 223 |
/// \sa Counter |
224 | 224 |
class NoCounter |
225 | 225 |
{ |
226 | 226 |
public: |
227 | 227 |
typedef _NoSubCounter<NoCounter> SubCounter; |
228 | 228 |
typedef _NoSubCounter<NoCounter> NoSubCounter; |
229 | 229 |
|
230 | 230 |
NoCounter() {} |
231 | 231 |
NoCounter(std::string,std::ostream &) {} |
232 | 232 |
NoCounter(const char *,std::ostream &) {} |
233 | 233 |
NoCounter(std::string) {} |
234 | 234 |
NoCounter(const char *) {} |
235 | 235 |
NoCounter &operator++() { return *this; } |
236 | 236 |
int operator++(int) { return 0; } |
237 | 237 |
NoCounter &operator--() { return *this; } |
238 | 238 |
int operator--(int) { return 0; } |
239 | 239 |
NoCounter &operator+=(int) { return *this;} |
240 | 240 |
NoCounter &operator-=(int) { return *this;} |
241 | 241 |
void reset(int) {} |
242 | 242 |
void reset() {} |
243 | 243 |
operator int() {return 0;} |
244 | 244 |
}; |
245 | 245 |
|
246 | 246 |
///@} |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
66 |
///By default it is a NullMap. |
|
66 |
///By default, it is a NullMap. |
|
67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
68 | 68 |
///Instantiates a \c ProcessedMap. |
69 | 69 |
|
70 | 70 |
///This function instantiates a \ref ProcessedMap. |
71 | 71 |
///\param g is the digraph, to which |
72 | 72 |
///we would like to define the \ref ProcessedMap. |
73 | 73 |
#ifdef DOXYGEN |
74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
75 | 75 |
#else |
76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
77 | 77 |
#endif |
78 | 78 |
{ |
79 | 79 |
return new ProcessedMap(); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 | 87 |
///Instantiates a \c ReachedMap. |
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref ReachedMap. |
90 | 90 |
///\param g is the digraph, to which |
91 | 91 |
///we would like to define the \ref ReachedMap. |
92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
93 | 93 |
{ |
94 | 94 |
return new ReachedMap(g); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
///The type of the map that stores the distances of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the distances of the nodes. |
100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 | 102 |
///Instantiates a \c DistMap. |
103 | 103 |
|
104 | 104 |
///This function instantiates a \ref DistMap. |
105 | 105 |
///\param g is the digraph, to which we would like to define the |
106 | 106 |
///\ref DistMap. |
107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(g); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%DFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %DFS algorithm. |
117 | 117 |
/// |
118 | 118 |
///There is also a \ref dfs() "function-type interface" for the DFS |
119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 120 |
///used easier. |
121 | 121 |
/// |
122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 123 |
///The default type is \ref ListDigraph. |
124 | 124 |
#ifdef DOXYGEN |
125 | 125 |
template <typename GR, |
126 | 126 |
typename TR> |
127 | 127 |
#else |
128 | 128 |
template <typename GR=ListDigraph, |
129 | 129 |
typename TR=DfsDefaultTraits<GR> > |
130 | 130 |
#endif |
131 | 131 |
class Dfs { |
132 | 132 |
public: |
133 | 133 |
|
134 | 134 |
///The type of the digraph the algorithm runs on. |
135 | 135 |
typedef typename TR::Digraph Digraph; |
136 | 136 |
|
137 | 137 |
///\brief The type of the map that stores the predecessor arcs of the |
138 | 138 |
///DFS paths. |
139 | 139 |
typedef typename TR::PredMap PredMap; |
140 | 140 |
///The type of the map that stores the distances of the nodes. |
141 | 141 |
typedef typename TR::DistMap DistMap; |
142 | 142 |
///The type of the map that indicates which nodes are reached. |
143 | 143 |
typedef typename TR::ReachedMap ReachedMap; |
144 | 144 |
///The type of the map that indicates which nodes are processed. |
145 | 145 |
typedef typename TR::ProcessedMap ProcessedMap; |
146 | 146 |
///The type of the paths. |
147 | 147 |
typedef PredMapPath<Digraph, PredMap> Path; |
148 | 148 |
|
149 | 149 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
150 | 150 |
typedef TR Traits; |
151 | 151 |
|
152 | 152 |
private: |
153 | 153 |
|
154 | 154 |
typedef typename Digraph::Node Node; |
155 | 155 |
typedef typename Digraph::NodeIt NodeIt; |
156 | 156 |
typedef typename Digraph::Arc Arc; |
157 | 157 |
typedef typename Digraph::OutArcIt OutArcIt; |
158 | 158 |
|
159 | 159 |
//Pointer to the underlying digraph. |
160 | 160 |
const Digraph *G; |
161 | 161 |
//Pointer to the map of predecessor arcs. |
162 | 162 |
PredMap *_pred; |
163 | 163 |
//Indicates if _pred is locally allocated (true) or not. |
164 | 164 |
bool local_pred; |
165 | 165 |
//Pointer to the map of distances. |
166 | 166 |
DistMap *_dist; |
167 | 167 |
//Indicates if _dist is locally allocated (true) or not. |
168 | 168 |
bool local_dist; |
169 | 169 |
//Pointer to the map of reached status of the nodes. |
170 | 170 |
ReachedMap *_reached; |
171 | 171 |
//Indicates if _reached is locally allocated (true) or not. |
172 | 172 |
bool local_reached; |
173 | 173 |
//Pointer to the map of processed status of the nodes. |
174 | 174 |
ProcessedMap *_processed; |
175 | 175 |
//Indicates if _processed is locally allocated (true) or not. |
176 | 176 |
bool local_processed; |
177 | 177 |
|
178 | 178 |
std::vector<typename Digraph::OutArcIt> _stack; |
179 | 179 |
int _stack_head; |
180 | 180 |
|
181 | 181 |
//Creates the maps if necessary. |
182 | 182 |
void create_maps() |
183 | 183 |
{ |
184 | 184 |
if(!_pred) { |
185 | 185 |
local_pred = true; |
186 | 186 |
_pred = Traits::createPredMap(*G); |
187 | 187 |
} |
188 | 188 |
if(!_dist) { |
189 | 189 |
local_dist = true; |
190 | 190 |
_dist = Traits::createDistMap(*G); |
191 | 191 |
} |
192 | 192 |
if(!_reached) { |
193 | 193 |
local_reached = true; |
194 | 194 |
_reached = Traits::createReachedMap(*G); |
195 | 195 |
} |
196 | 196 |
if(!_processed) { |
197 | 197 |
local_processed = true; |
198 | 198 |
_processed = Traits::createProcessedMap(*G); |
199 | 199 |
} |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
protected: |
203 | 203 |
|
204 | 204 |
Dfs() {} |
205 | 205 |
|
206 | 206 |
public: |
207 | 207 |
|
208 | 208 |
typedef Dfs Create; |
209 | 209 |
|
210 | 210 |
///\name Named Template Parameters |
211 | 211 |
|
212 | 212 |
///@{ |
213 | 213 |
|
214 | 214 |
template <class T> |
215 | 215 |
struct SetPredMapTraits : public Traits { |
216 | 216 |
typedef T PredMap; |
217 | 217 |
static PredMap *createPredMap(const Digraph &) |
218 | 218 |
{ |
219 | 219 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
220 | 220 |
return 0; // ignore warnings |
221 | 221 |
} |
222 | 222 |
}; |
223 | 223 |
///\brief \ref named-templ-param "Named parameter" for setting |
224 | 224 |
///\c PredMap type. |
225 | 225 |
/// |
226 | 226 |
///\ref named-templ-param "Named parameter" for setting |
227 | 227 |
///\c PredMap type. |
228 | 228 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
229 | 229 |
template <class T> |
230 | 230 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
231 | 231 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
232 | 232 |
}; |
233 | 233 |
|
234 | 234 |
template <class T> |
235 | 235 |
struct SetDistMapTraits : public Traits { |
236 | 236 |
typedef T DistMap; |
237 | 237 |
static DistMap *createDistMap(const Digraph &) |
238 | 238 |
{ |
239 | 239 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
240 | 240 |
return 0; // ignore warnings |
241 | 241 |
} |
242 | 242 |
}; |
243 | 243 |
///\brief \ref named-templ-param "Named parameter" for setting |
244 | 244 |
///\c DistMap type. |
245 | 245 |
/// |
246 | 246 |
///\ref named-templ-param "Named parameter" for setting |
247 | 247 |
///\c DistMap type. |
248 | 248 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
249 | 249 |
template <class T> |
250 | 250 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
251 | 251 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
252 | 252 |
}; |
253 | 253 |
|
254 | 254 |
template <class T> |
255 | 255 |
struct SetReachedMapTraits : public Traits { |
256 | 256 |
typedef T ReachedMap; |
257 | 257 |
static ReachedMap *createReachedMap(const Digraph &) |
258 | 258 |
{ |
... | ... |
@@ -593,385 +593,385 @@ |
593 | 593 |
///This method runs the %DFS algorithm from node \c s |
594 | 594 |
///in order to compute the DFS path to each node. |
595 | 595 |
/// |
596 | 596 |
///The algorithm computes |
597 | 597 |
///- the %DFS tree, |
598 | 598 |
///- the distance of each node from the root in the %DFS tree. |
599 | 599 |
/// |
600 | 600 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
601 | 601 |
///\code |
602 | 602 |
/// d.init(); |
603 | 603 |
/// d.addSource(s); |
604 | 604 |
/// d.start(); |
605 | 605 |
///\endcode |
606 | 606 |
void run(Node s) { |
607 | 607 |
init(); |
608 | 608 |
addSource(s); |
609 | 609 |
start(); |
610 | 610 |
} |
611 | 611 |
|
612 | 612 |
///Finds the %DFS path between \c s and \c t. |
613 | 613 |
|
614 | 614 |
///This method runs the %DFS algorithm from node \c s |
615 | 615 |
///in order to compute the DFS path to node \c t |
616 | 616 |
///(it stops searching when \c t is processed) |
617 | 617 |
/// |
618 | 618 |
///\return \c true if \c t is reachable form \c s. |
619 | 619 |
/// |
620 | 620 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
621 | 621 |
///just a shortcut of the following code. |
622 | 622 |
///\code |
623 | 623 |
/// d.init(); |
624 | 624 |
/// d.addSource(s); |
625 | 625 |
/// d.start(t); |
626 | 626 |
///\endcode |
627 | 627 |
bool run(Node s,Node t) { |
628 | 628 |
init(); |
629 | 629 |
addSource(s); |
630 | 630 |
start(t); |
631 | 631 |
return reached(t); |
632 | 632 |
} |
633 | 633 |
|
634 | 634 |
///Runs the algorithm to visit all nodes in the digraph. |
635 | 635 |
|
636 | 636 |
///This method runs the %DFS algorithm in order to compute the |
637 | 637 |
///%DFS path to each node. |
638 | 638 |
/// |
639 | 639 |
///The algorithm computes |
640 | 640 |
///- the %DFS tree (forest), |
641 | 641 |
///- the distance of each node from the root(s) in the %DFS tree. |
642 | 642 |
/// |
643 | 643 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
644 | 644 |
///\code |
645 | 645 |
/// d.init(); |
646 | 646 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
647 | 647 |
/// if (!d.reached(n)) { |
648 | 648 |
/// d.addSource(n); |
649 | 649 |
/// d.start(); |
650 | 650 |
/// } |
651 | 651 |
/// } |
652 | 652 |
///\endcode |
653 | 653 |
void run() { |
654 | 654 |
init(); |
655 | 655 |
for (NodeIt it(*G); it != INVALID; ++it) { |
656 | 656 |
if (!reached(it)) { |
657 | 657 |
addSource(it); |
658 | 658 |
start(); |
659 | 659 |
} |
660 | 660 |
} |
661 | 661 |
} |
662 | 662 |
|
663 | 663 |
///@} |
664 | 664 |
|
665 | 665 |
///\name Query Functions |
666 | 666 |
///The results of the DFS algorithm can be obtained using these |
667 | 667 |
///functions.\n |
668 | 668 |
///Either \ref run(Node) "run()" or \ref start() should be called |
669 | 669 |
///before using them. |
670 | 670 |
|
671 | 671 |
///@{ |
672 | 672 |
|
673 | 673 |
///The DFS path to the given node. |
674 | 674 |
|
675 | 675 |
///Returns the DFS path to the given node from the root(s). |
676 | 676 |
/// |
677 | 677 |
///\warning \c t should be reached from the root(s). |
678 | 678 |
/// |
679 | 679 |
///\pre Either \ref run(Node) "run()" or \ref init() |
680 | 680 |
///must be called before using this function. |
681 | 681 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
682 | 682 |
|
683 | 683 |
///The distance of the given node from the root(s). |
684 | 684 |
|
685 | 685 |
///Returns the distance of the given node from the root(s). |
686 | 686 |
/// |
687 | 687 |
///\warning If node \c v is not reached from the root(s), then |
688 | 688 |
///the return value of this function is undefined. |
689 | 689 |
/// |
690 | 690 |
///\pre Either \ref run(Node) "run()" or \ref init() |
691 | 691 |
///must be called before using this function. |
692 | 692 |
int dist(Node v) const { return (*_dist)[v]; } |
693 | 693 |
|
694 | 694 |
///Returns the 'previous arc' of the %DFS tree for the given node. |
695 | 695 |
|
696 | 696 |
///This function returns the 'previous arc' of the %DFS tree for the |
697 | 697 |
///node \c v, i.e. it returns the last arc of a %DFS path from a |
698 | 698 |
///root to \c v. It is \c INVALID if \c v is not reached from the |
699 | 699 |
///root(s) or if \c v is a root. |
700 | 700 |
/// |
701 | 701 |
///The %DFS tree used here is equal to the %DFS tree used in |
702 | 702 |
///\ref predNode() and \ref predMap(). |
703 | 703 |
/// |
704 | 704 |
///\pre Either \ref run(Node) "run()" or \ref init() |
705 | 705 |
///must be called before using this function. |
706 | 706 |
Arc predArc(Node v) const { return (*_pred)[v];} |
707 | 707 |
|
708 | 708 |
///Returns the 'previous node' of the %DFS tree for the given node. |
709 | 709 |
|
710 | 710 |
///This function returns the 'previous node' of the %DFS |
711 | 711 |
///tree for the node \c v, i.e. it returns the last but one node |
712 | 712 |
///of a %DFS path from a root to \c v. It is \c INVALID |
713 | 713 |
///if \c v is not reached from the root(s) or if \c v is a root. |
714 | 714 |
/// |
715 | 715 |
///The %DFS tree used here is equal to the %DFS tree used in |
716 | 716 |
///\ref predArc() and \ref predMap(). |
717 | 717 |
/// |
718 | 718 |
///\pre Either \ref run(Node) "run()" or \ref init() |
719 | 719 |
///must be called before using this function. |
720 | 720 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
721 | 721 |
G->source((*_pred)[v]); } |
722 | 722 |
|
723 | 723 |
///\brief Returns a const reference to the node map that stores the |
724 | 724 |
///distances of the nodes. |
725 | 725 |
/// |
726 | 726 |
///Returns a const reference to the node map that stores the |
727 | 727 |
///distances of the nodes calculated by the algorithm. |
728 | 728 |
/// |
729 | 729 |
///\pre Either \ref run(Node) "run()" or \ref init() |
730 | 730 |
///must be called before using this function. |
731 | 731 |
const DistMap &distMap() const { return *_dist;} |
732 | 732 |
|
733 | 733 |
///\brief Returns a const reference to the node map that stores the |
734 | 734 |
///predecessor arcs. |
735 | 735 |
/// |
736 | 736 |
///Returns a const reference to the node map that stores the predecessor |
737 | 737 |
///arcs, which form the DFS tree (forest). |
738 | 738 |
/// |
739 | 739 |
///\pre Either \ref run(Node) "run()" or \ref init() |
740 | 740 |
///must be called before using this function. |
741 | 741 |
const PredMap &predMap() const { return *_pred;} |
742 | 742 |
|
743 | 743 |
///Checks if the given node. node is reached from the root(s). |
744 | 744 |
|
745 | 745 |
///Returns \c true if \c v is reached from the root(s). |
746 | 746 |
/// |
747 | 747 |
///\pre Either \ref run(Node) "run()" or \ref init() |
748 | 748 |
///must be called before using this function. |
749 | 749 |
bool reached(Node v) const { return (*_reached)[v]; } |
750 | 750 |
|
751 | 751 |
///@} |
752 | 752 |
}; |
753 | 753 |
|
754 | 754 |
///Default traits class of dfs() function. |
755 | 755 |
|
756 | 756 |
///Default traits class of dfs() function. |
757 | 757 |
///\tparam GR Digraph type. |
758 | 758 |
template<class GR> |
759 | 759 |
struct DfsWizardDefaultTraits |
760 | 760 |
{ |
761 | 761 |
///The type of the digraph the algorithm runs on. |
762 | 762 |
typedef GR Digraph; |
763 | 763 |
|
764 | 764 |
///\brief The type of the map that stores the predecessor |
765 | 765 |
///arcs of the %DFS paths. |
766 | 766 |
/// |
767 | 767 |
///The type of the map that stores the predecessor |
768 | 768 |
///arcs of the %DFS paths. |
769 | 769 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
770 | 770 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
771 | 771 |
///Instantiates a PredMap. |
772 | 772 |
|
773 | 773 |
///This function instantiates a PredMap. |
774 | 774 |
///\param g is the digraph, to which we would like to define the |
775 | 775 |
///PredMap. |
776 | 776 |
static PredMap *createPredMap(const Digraph &g) |
777 | 777 |
{ |
778 | 778 |
return new PredMap(g); |
779 | 779 |
} |
780 | 780 |
|
781 | 781 |
///The type of the map that indicates which nodes are processed. |
782 | 782 |
|
783 | 783 |
///The type of the map that indicates which nodes are processed. |
784 | 784 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
785 |
///By default it is a NullMap. |
|
785 |
///By default, it is a NullMap. |
|
786 | 786 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
787 | 787 |
///Instantiates a ProcessedMap. |
788 | 788 |
|
789 | 789 |
///This function instantiates a ProcessedMap. |
790 | 790 |
///\param g is the digraph, to which |
791 | 791 |
///we would like to define the ProcessedMap. |
792 | 792 |
#ifdef DOXYGEN |
793 | 793 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
794 | 794 |
#else |
795 | 795 |
static ProcessedMap *createProcessedMap(const Digraph &) |
796 | 796 |
#endif |
797 | 797 |
{ |
798 | 798 |
return new ProcessedMap(); |
799 | 799 |
} |
800 | 800 |
|
801 | 801 |
///The type of the map that indicates which nodes are reached. |
802 | 802 |
|
803 | 803 |
///The type of the map that indicates which nodes are reached. |
804 | 804 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
805 | 805 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
806 | 806 |
///Instantiates a ReachedMap. |
807 | 807 |
|
808 | 808 |
///This function instantiates a ReachedMap. |
809 | 809 |
///\param g is the digraph, to which |
810 | 810 |
///we would like to define the ReachedMap. |
811 | 811 |
static ReachedMap *createReachedMap(const Digraph &g) |
812 | 812 |
{ |
813 | 813 |
return new ReachedMap(g); |
814 | 814 |
} |
815 | 815 |
|
816 | 816 |
///The type of the map that stores the distances of the nodes. |
817 | 817 |
|
818 | 818 |
///The type of the map that stores the distances of the nodes. |
819 | 819 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
820 | 820 |
typedef typename Digraph::template NodeMap<int> DistMap; |
821 | 821 |
///Instantiates a DistMap. |
822 | 822 |
|
823 | 823 |
///This function instantiates a DistMap. |
824 | 824 |
///\param g is the digraph, to which we would like to define |
825 | 825 |
///the DistMap |
826 | 826 |
static DistMap *createDistMap(const Digraph &g) |
827 | 827 |
{ |
828 | 828 |
return new DistMap(g); |
829 | 829 |
} |
830 | 830 |
|
831 | 831 |
///The type of the DFS paths. |
832 | 832 |
|
833 | 833 |
///The type of the DFS paths. |
834 | 834 |
///It must conform to the \ref concepts::Path "Path" concept. |
835 | 835 |
typedef lemon::Path<Digraph> Path; |
836 | 836 |
}; |
837 | 837 |
|
838 | 838 |
/// Default traits class used by DfsWizard |
839 | 839 |
|
840 | 840 |
/// Default traits class used by DfsWizard. |
841 | 841 |
/// \tparam GR The type of the digraph. |
842 | 842 |
template<class GR> |
843 | 843 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
844 | 844 |
{ |
845 | 845 |
|
846 | 846 |
typedef DfsWizardDefaultTraits<GR> Base; |
847 | 847 |
protected: |
848 | 848 |
//The type of the nodes in the digraph. |
849 | 849 |
typedef typename Base::Digraph::Node Node; |
850 | 850 |
|
851 | 851 |
//Pointer to the digraph the algorithm runs on. |
852 | 852 |
void *_g; |
853 | 853 |
//Pointer to the map of reached nodes. |
854 | 854 |
void *_reached; |
855 | 855 |
//Pointer to the map of processed nodes. |
856 | 856 |
void *_processed; |
857 | 857 |
//Pointer to the map of predecessors arcs. |
858 | 858 |
void *_pred; |
859 | 859 |
//Pointer to the map of distances. |
860 | 860 |
void *_dist; |
861 | 861 |
//Pointer to the DFS path to the target node. |
862 | 862 |
void *_path; |
863 | 863 |
//Pointer to the distance of the target node. |
864 | 864 |
int *_di; |
865 | 865 |
|
866 | 866 |
public: |
867 | 867 |
/// Constructor. |
868 | 868 |
|
869 | 869 |
/// This constructor does not require parameters, it initiates |
870 | 870 |
/// all of the attributes to \c 0. |
871 | 871 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
872 | 872 |
_dist(0), _path(0), _di(0) {} |
873 | 873 |
|
874 | 874 |
/// Constructor. |
875 | 875 |
|
876 | 876 |
/// This constructor requires one parameter, |
877 | 877 |
/// others are initiated to \c 0. |
878 | 878 |
/// \param g The digraph the algorithm runs on. |
879 | 879 |
DfsWizardBase(const GR &g) : |
880 | 880 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
881 | 881 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
882 | 882 |
|
883 | 883 |
}; |
884 | 884 |
|
885 | 885 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
886 | 886 |
|
887 | 887 |
/// This auxiliary class is created to implement the |
888 | 888 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
889 | 889 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
890 | 890 |
/// functions and features of the plain \ref Dfs. |
891 | 891 |
/// |
892 | 892 |
/// This class should only be used through the \ref dfs() function, |
893 | 893 |
/// which makes it easier to use the algorithm. |
894 | 894 |
template<class TR> |
895 | 895 |
class DfsWizard : public TR |
896 | 896 |
{ |
897 | 897 |
typedef TR Base; |
898 | 898 |
|
899 | 899 |
typedef typename TR::Digraph Digraph; |
900 | 900 |
|
901 | 901 |
typedef typename Digraph::Node Node; |
902 | 902 |
typedef typename Digraph::NodeIt NodeIt; |
903 | 903 |
typedef typename Digraph::Arc Arc; |
904 | 904 |
typedef typename Digraph::OutArcIt OutArcIt; |
905 | 905 |
|
906 | 906 |
typedef typename TR::PredMap PredMap; |
907 | 907 |
typedef typename TR::DistMap DistMap; |
908 | 908 |
typedef typename TR::ReachedMap ReachedMap; |
909 | 909 |
typedef typename TR::ProcessedMap ProcessedMap; |
910 | 910 |
typedef typename TR::Path Path; |
911 | 911 |
|
912 | 912 |
public: |
913 | 913 |
|
914 | 914 |
/// Constructor. |
915 | 915 |
DfsWizard() : TR() {} |
916 | 916 |
|
917 | 917 |
/// Constructor that requires parameters. |
918 | 918 |
|
919 | 919 |
/// Constructor that requires parameters. |
920 | 920 |
/// These parameters will be the default values for the traits class. |
921 | 921 |
/// \param g The digraph the algorithm runs on. |
922 | 922 |
DfsWizard(const Digraph &g) : |
923 | 923 |
TR(g) {} |
924 | 924 |
|
925 | 925 |
///Copy constructor |
926 | 926 |
DfsWizard(const TR &b) : TR(b) {} |
927 | 927 |
|
928 | 928 |
~DfsWizard() {} |
929 | 929 |
|
930 | 930 |
///Runs DFS algorithm from the given source node. |
931 | 931 |
|
932 | 932 |
///This method runs DFS algorithm from node \c s |
933 | 933 |
///in order to compute the DFS path to each node. |
934 | 934 |
void run(Node s) |
935 | 935 |
{ |
936 | 936 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
937 | 937 |
if (Base::_pred) |
938 | 938 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
939 | 939 |
if (Base::_dist) |
940 | 940 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
941 | 941 |
if (Base::_reached) |
942 | 942 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
943 | 943 |
if (Base::_processed) |
944 | 944 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
945 | 945 |
if (s!=INVALID) |
946 | 946 |
alg.run(s); |
947 | 947 |
else |
948 | 948 |
alg.run(); |
949 | 949 |
} |
950 | 950 |
|
951 | 951 |
///Finds the DFS path between \c s and \c t. |
952 | 952 |
|
953 | 953 |
///This method runs DFS algorithm from node \c s |
954 | 954 |
///in order to compute the DFS path to node \c t |
955 | 955 |
///(it stops searching when \c t is processed). |
956 | 956 |
/// |
957 | 957 |
///\return \c true if \c t is reachable form \c s. |
958 | 958 |
bool run(Node s, Node t) |
959 | 959 |
{ |
960 | 960 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
961 | 961 |
if (Base::_pred) |
962 | 962 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
963 | 963 |
if (Base::_dist) |
964 | 964 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
965 | 965 |
if (Base::_reached) |
966 | 966 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
967 | 967 |
if (Base::_processed) |
968 | 968 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
969 | 969 |
alg.run(s,t); |
970 | 970 |
if (Base::_path) |
971 | 971 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
972 | 972 |
if (Base::_di) |
973 | 973 |
*Base::_di = alg.dist(t); |
974 | 974 |
return alg.reached(t); |
975 | 975 |
} |
976 | 976 |
|
977 | 977 |
///Runs DFS algorithm to visit all nodes in the digraph. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIJKSTRA_H |
20 | 20 |
#define LEMON_DIJKSTRA_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief Dijkstra algorithm. |
25 | 25 |
|
26 | 26 |
#include <limits> |
27 | 27 |
#include <lemon/list_graph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
#include <lemon/path.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
38 | 38 |
/// |
39 | 39 |
/// This operation traits class defines all computational operations and |
40 | 40 |
/// constants which are used in the Dijkstra algorithm. |
41 | 41 |
template <typename V> |
42 | 42 |
struct DijkstraDefaultOperationTraits { |
43 | 43 |
/// \e |
44 | 44 |
typedef V Value; |
45 | 45 |
/// \brief Gives back the zero value of the type. |
46 | 46 |
static Value zero() { |
47 | 47 |
return static_cast<Value>(0); |
48 | 48 |
} |
49 | 49 |
/// \brief Gives back the sum of the given two elements. |
50 | 50 |
static Value plus(const Value& left, const Value& right) { |
51 | 51 |
return left + right; |
52 | 52 |
} |
53 | 53 |
/// \brief Gives back true only if the first value is less than the second. |
54 | 54 |
static bool less(const Value& left, const Value& right) { |
55 | 55 |
return left < right; |
56 | 56 |
} |
57 | 57 |
}; |
58 | 58 |
|
59 | 59 |
///Default traits class of Dijkstra class. |
60 | 60 |
|
61 | 61 |
///Default traits class of Dijkstra class. |
62 | 62 |
///\tparam GR The type of the digraph. |
63 | 63 |
///\tparam LEN The type of the length map. |
64 | 64 |
template<typename GR, typename LEN> |
65 | 65 |
struct DijkstraDefaultTraits |
66 | 66 |
{ |
67 | 67 |
///The type of the digraph the algorithm runs on. |
68 | 68 |
typedef GR Digraph; |
69 | 69 |
|
70 | 70 |
///The type of the map that stores the arc lengths. |
71 | 71 |
|
72 | 72 |
///The type of the map that stores the arc lengths. |
73 | 73 |
///It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
74 | 74 |
typedef LEN LengthMap; |
75 | 75 |
///The type of the arc lengths. |
76 | 76 |
typedef typename LEN::Value Value; |
77 | 77 |
|
78 | 78 |
/// Operation traits for %Dijkstra algorithm. |
79 | 79 |
|
80 | 80 |
/// This class defines the operations that are used in the algorithm. |
81 | 81 |
/// \see DijkstraDefaultOperationTraits |
82 | 82 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
83 | 83 |
|
84 | 84 |
/// The cross reference type used by the heap. |
85 | 85 |
|
86 | 86 |
/// The cross reference type used by the heap. |
87 | 87 |
/// Usually it is \c Digraph::NodeMap<int>. |
88 | 88 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
89 | 89 |
///Instantiates a \c HeapCrossRef. |
90 | 90 |
|
91 | 91 |
///This function instantiates a \ref HeapCrossRef. |
92 | 92 |
/// \param g is the digraph, to which we would like to define the |
93 | 93 |
/// \ref HeapCrossRef. |
94 | 94 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
95 | 95 |
{ |
96 | 96 |
return new HeapCrossRef(g); |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
///The heap type used by the %Dijkstra algorithm. |
100 | 100 |
|
101 | 101 |
///The heap type used by the Dijkstra algorithm. |
102 | 102 |
/// |
103 | 103 |
///\sa BinHeap |
104 | 104 |
///\sa Dijkstra |
105 | 105 |
typedef BinHeap<typename LEN::Value, HeapCrossRef, std::less<Value> > Heap; |
106 | 106 |
///Instantiates a \c Heap. |
107 | 107 |
|
108 | 108 |
///This function instantiates a \ref Heap. |
109 | 109 |
static Heap *createHeap(HeapCrossRef& r) |
110 | 110 |
{ |
111 | 111 |
return new Heap(r); |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
///\brief The type of the map that stores the predecessor |
115 | 115 |
///arcs of the shortest paths. |
116 | 116 |
/// |
117 | 117 |
///The type of the map that stores the predecessor |
118 | 118 |
///arcs of the shortest paths. |
119 | 119 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
120 | 120 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
121 | 121 |
///Instantiates a \c PredMap. |
122 | 122 |
|
123 | 123 |
///This function instantiates a \ref PredMap. |
124 | 124 |
///\param g is the digraph, to which we would like to define the |
125 | 125 |
///\ref PredMap. |
126 | 126 |
static PredMap *createPredMap(const Digraph &g) |
127 | 127 |
{ |
128 | 128 |
return new PredMap(g); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
///The type of the map that indicates which nodes are processed. |
132 | 132 |
|
133 | 133 |
///The type of the map that indicates which nodes are processed. |
134 | 134 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
135 |
///By default it is a NullMap. |
|
135 |
///By default, it is a NullMap. |
|
136 | 136 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
137 | 137 |
///Instantiates a \c ProcessedMap. |
138 | 138 |
|
139 | 139 |
///This function instantiates a \ref ProcessedMap. |
140 | 140 |
///\param g is the digraph, to which |
141 | 141 |
///we would like to define the \ref ProcessedMap. |
142 | 142 |
#ifdef DOXYGEN |
143 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
144 | 144 |
#else |
145 | 145 |
static ProcessedMap *createProcessedMap(const Digraph &) |
146 | 146 |
#endif |
147 | 147 |
{ |
148 | 148 |
return new ProcessedMap(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
///The type of the map that stores the distances of the nodes. |
152 | 152 |
|
153 | 153 |
///The type of the map that stores the distances of the nodes. |
154 | 154 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
155 | 155 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
156 | 156 |
///Instantiates a \c DistMap. |
157 | 157 |
|
158 | 158 |
///This function instantiates a \ref DistMap. |
159 | 159 |
///\param g is the digraph, to which we would like to define |
160 | 160 |
///the \ref DistMap. |
161 | 161 |
static DistMap *createDistMap(const Digraph &g) |
162 | 162 |
{ |
163 | 163 |
return new DistMap(g); |
164 | 164 |
} |
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
///%Dijkstra algorithm class. |
168 | 168 |
|
169 | 169 |
/// \ingroup shortest_path |
170 | 170 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
171 | 171 |
/// |
172 | 172 |
///The %Dijkstra algorithm solves the single-source shortest path problem |
173 | 173 |
///when all arc lengths are non-negative. If there are negative lengths, |
174 | 174 |
///the BellmanFord algorithm should be used instead. |
175 | 175 |
/// |
176 | 176 |
///The arc lengths are passed to the algorithm using a |
177 | 177 |
///\ref concepts::ReadMap "ReadMap", |
178 | 178 |
///so it is easy to change it to any kind of length. |
179 | 179 |
///The type of the length is determined by the |
180 | 180 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
181 | 181 |
///It is also possible to change the underlying priority heap. |
182 | 182 |
/// |
183 | 183 |
///There is also a \ref dijkstra() "function-type interface" for the |
184 | 184 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
185 | 185 |
///it can be used easier. |
186 | 186 |
/// |
187 | 187 |
///\tparam GR The type of the digraph the algorithm runs on. |
188 | 188 |
///The default type is \ref ListDigraph. |
189 | 189 |
///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
190 | 190 |
///the lengths of the arcs. |
191 | 191 |
///It is read once for each arc, so the map may involve in |
192 | 192 |
///relatively time consuming process to compute the arc lengths if |
193 | 193 |
///it is necessary. The default map type is \ref |
194 | 194 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
195 | 195 |
#ifdef DOXYGEN |
196 | 196 |
template <typename GR, typename LEN, typename TR> |
197 | 197 |
#else |
198 | 198 |
template <typename GR=ListDigraph, |
199 | 199 |
typename LEN=typename GR::template ArcMap<int>, |
200 | 200 |
typename TR=DijkstraDefaultTraits<GR,LEN> > |
201 | 201 |
#endif |
202 | 202 |
class Dijkstra { |
203 | 203 |
public: |
204 | 204 |
|
205 | 205 |
///The type of the digraph the algorithm runs on. |
206 | 206 |
typedef typename TR::Digraph Digraph; |
207 | 207 |
|
208 | 208 |
///The type of the arc lengths. |
209 | 209 |
typedef typename TR::LengthMap::Value Value; |
210 | 210 |
///The type of the map that stores the arc lengths. |
211 | 211 |
typedef typename TR::LengthMap LengthMap; |
212 | 212 |
///\brief The type of the map that stores the predecessor arcs of the |
213 | 213 |
///shortest paths. |
214 | 214 |
typedef typename TR::PredMap PredMap; |
215 | 215 |
///The type of the map that stores the distances of the nodes. |
216 | 216 |
typedef typename TR::DistMap DistMap; |
217 | 217 |
///The type of the map that indicates which nodes are processed. |
218 | 218 |
typedef typename TR::ProcessedMap ProcessedMap; |
219 | 219 |
///The type of the paths. |
220 | 220 |
typedef PredMapPath<Digraph, PredMap> Path; |
221 | 221 |
///The cross reference type used for the current heap. |
222 | 222 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
223 | 223 |
///The heap type used by the algorithm. |
224 | 224 |
typedef typename TR::Heap Heap; |
225 | 225 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
226 | 226 |
///of the algorithm. |
227 | 227 |
typedef typename TR::OperationTraits OperationTraits; |
228 | 228 |
|
229 | 229 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
230 | 230 |
typedef TR Traits; |
231 | 231 |
|
232 | 232 |
private: |
233 | 233 |
|
234 | 234 |
typedef typename Digraph::Node Node; |
235 | 235 |
typedef typename Digraph::NodeIt NodeIt; |
236 | 236 |
typedef typename Digraph::Arc Arc; |
237 | 237 |
typedef typename Digraph::OutArcIt OutArcIt; |
238 | 238 |
|
239 | 239 |
//Pointer to the underlying digraph. |
240 | 240 |
const Digraph *G; |
241 | 241 |
//Pointer to the length map. |
242 | 242 |
const LengthMap *_length; |
243 | 243 |
//Pointer to the map of predecessors arcs. |
244 | 244 |
PredMap *_pred; |
245 | 245 |
//Indicates if _pred is locally allocated (true) or not. |
246 | 246 |
bool local_pred; |
247 | 247 |
//Pointer to the map of distances. |
248 | 248 |
DistMap *_dist; |
249 | 249 |
//Indicates if _dist is locally allocated (true) or not. |
250 | 250 |
bool local_dist; |
251 | 251 |
//Pointer to the map of processed status of the nodes. |
252 | 252 |
ProcessedMap *_processed; |
253 | 253 |
//Indicates if _processed is locally allocated (true) or not. |
254 | 254 |
bool local_processed; |
255 | 255 |
//Pointer to the heap cross references. |
256 | 256 |
HeapCrossRef *_heap_cross_ref; |
257 | 257 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
258 | 258 |
bool local_heap_cross_ref; |
259 | 259 |
//Pointer to the heap. |
260 | 260 |
Heap *_heap; |
261 | 261 |
//Indicates if _heap is locally allocated (true) or not. |
262 | 262 |
bool local_heap; |
263 | 263 |
|
264 | 264 |
//Creates the maps if necessary. |
265 | 265 |
void create_maps() |
266 | 266 |
{ |
267 | 267 |
if(!_pred) { |
268 | 268 |
local_pred = true; |
269 | 269 |
_pred = Traits::createPredMap(*G); |
270 | 270 |
} |
271 | 271 |
if(!_dist) { |
272 | 272 |
local_dist = true; |
273 | 273 |
_dist = Traits::createDistMap(*G); |
274 | 274 |
} |
275 | 275 |
if(!_processed) { |
276 | 276 |
local_processed = true; |
277 | 277 |
_processed = Traits::createProcessedMap(*G); |
278 | 278 |
} |
279 | 279 |
if (!_heap_cross_ref) { |
280 | 280 |
local_heap_cross_ref = true; |
281 | 281 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
282 | 282 |
} |
283 | 283 |
if (!_heap) { |
284 | 284 |
local_heap = true; |
285 | 285 |
_heap = Traits::createHeap(*_heap_cross_ref); |
286 | 286 |
} |
287 | 287 |
} |
288 | 288 |
|
289 | 289 |
public: |
290 | 290 |
|
291 | 291 |
typedef Dijkstra Create; |
292 | 292 |
|
293 | 293 |
///\name Named Template Parameters |
294 | 294 |
|
295 | 295 |
///@{ |
296 | 296 |
|
297 | 297 |
template <class T> |
298 | 298 |
struct SetPredMapTraits : public Traits { |
299 | 299 |
typedef T PredMap; |
300 | 300 |
static PredMap *createPredMap(const Digraph &) |
301 | 301 |
{ |
302 | 302 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
303 | 303 |
return 0; // ignore warnings |
304 | 304 |
} |
305 | 305 |
}; |
306 | 306 |
///\brief \ref named-templ-param "Named parameter" for setting |
307 | 307 |
///\c PredMap type. |
308 | 308 |
/// |
309 | 309 |
///\ref named-templ-param "Named parameter" for setting |
310 | 310 |
///\c PredMap type. |
311 | 311 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
312 | 312 |
template <class T> |
313 | 313 |
struct SetPredMap |
314 | 314 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
315 | 315 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
316 | 316 |
}; |
317 | 317 |
|
318 | 318 |
template <class T> |
319 | 319 |
struct SetDistMapTraits : public Traits { |
320 | 320 |
typedef T DistMap; |
321 | 321 |
static DistMap *createDistMap(const Digraph &) |
322 | 322 |
{ |
323 | 323 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
324 | 324 |
return 0; // ignore warnings |
325 | 325 |
} |
326 | 326 |
}; |
327 | 327 |
///\brief \ref named-templ-param "Named parameter" for setting |
328 | 328 |
///\c DistMap type. |
329 | 329 |
/// |
330 | 330 |
///\ref named-templ-param "Named parameter" for setting |
331 | 331 |
///\c DistMap type. |
332 | 332 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
333 | 333 |
template <class T> |
334 | 334 |
struct SetDistMap |
335 | 335 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
336 | 336 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
337 | 337 |
}; |
338 | 338 |
|
339 | 339 |
template <class T> |
340 | 340 |
struct SetProcessedMapTraits : public Traits { |
341 | 341 |
typedef T ProcessedMap; |
342 | 342 |
static ProcessedMap *createProcessedMap(const Digraph &) |
343 | 343 |
{ |
344 | 344 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
345 | 345 |
return 0; // ignore warnings |
346 | 346 |
} |
347 | 347 |
}; |
348 | 348 |
///\brief \ref named-templ-param "Named parameter" for setting |
349 | 349 |
///\c ProcessedMap type. |
350 | 350 |
/// |
351 | 351 |
///\ref named-templ-param "Named parameter" for setting |
352 | 352 |
///\c ProcessedMap type. |
353 | 353 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
354 | 354 |
template <class T> |
355 | 355 |
struct SetProcessedMap |
356 | 356 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
357 | 357 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
358 | 358 |
}; |
359 | 359 |
|
360 | 360 |
struct SetStandardProcessedMapTraits : public Traits { |
361 | 361 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
362 | 362 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
363 | 363 |
{ |
364 | 364 |
return new ProcessedMap(g); |
365 | 365 |
} |
366 | 366 |
}; |
367 | 367 |
///\brief \ref named-templ-param "Named parameter" for setting |
368 | 368 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
369 | 369 |
/// |
370 | 370 |
///\ref named-templ-param "Named parameter" for setting |
371 | 371 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
372 | 372 |
///If you don't set it explicitly, it will be automatically allocated. |
373 | 373 |
struct SetStandardProcessedMap |
374 | 374 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
375 | 375 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
376 | 376 |
Create; |
377 | 377 |
}; |
378 | 378 |
|
379 | 379 |
template <class H, class CR> |
380 | 380 |
struct SetHeapTraits : public Traits { |
381 | 381 |
typedef CR HeapCrossRef; |
382 | 382 |
typedef H Heap; |
383 | 383 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
384 | 384 |
LEMON_ASSERT(false, "HeapCrossRef is not initialized"); |
385 | 385 |
return 0; // ignore warnings |
386 | 386 |
} |
387 | 387 |
static Heap *createHeap(HeapCrossRef &) |
388 | 388 |
{ |
389 | 389 |
LEMON_ASSERT(false, "Heap is not initialized"); |
390 | 390 |
return 0; // ignore warnings |
391 | 391 |
} |
392 | 392 |
}; |
393 | 393 |
///\brief \ref named-templ-param "Named parameter" for setting |
394 | 394 |
///heap and cross reference types |
395 | 395 |
/// |
396 | 396 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
397 | 397 |
///reference types. If this named parameter is used, then external |
398 | 398 |
///heap and cross reference objects must be passed to the algorithm |
399 | 399 |
///using the \ref heap() function before calling \ref run(Node) "run()" |
400 | 400 |
///or \ref init(). |
401 | 401 |
///\sa SetStandardHeap |
402 | 402 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
403 | 403 |
struct SetHeap |
404 | 404 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > { |
405 | 405 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
406 | 406 |
}; |
407 | 407 |
|
408 | 408 |
template <class H, class CR> |
409 | 409 |
struct SetStandardHeapTraits : public Traits { |
410 | 410 |
typedef CR HeapCrossRef; |
411 | 411 |
typedef H Heap; |
412 | 412 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
413 | 413 |
return new HeapCrossRef(G); |
414 | 414 |
} |
415 | 415 |
static Heap *createHeap(HeapCrossRef &R) |
416 | 416 |
{ |
417 | 417 |
return new Heap(R); |
418 | 418 |
} |
419 | 419 |
}; |
420 | 420 |
///\brief \ref named-templ-param "Named parameter" for setting |
421 | 421 |
///heap and cross reference types with automatic allocation |
422 | 422 |
/// |
423 | 423 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
424 | 424 |
///reference types with automatic allocation. |
425 | 425 |
///They should have standard constructor interfaces to be able to |
426 | 426 |
///automatically created by the algorithm (i.e. the digraph should be |
427 | 427 |
///passed to the constructor of the cross reference and the cross |
428 | 428 |
///reference should be passed to the constructor of the heap). |
429 |
///However external heap and cross reference objects could also be |
|
429 |
///However, external heap and cross reference objects could also be |
|
430 | 430 |
///passed to the algorithm using the \ref heap() function before |
431 | 431 |
///calling \ref run(Node) "run()" or \ref init(). |
432 | 432 |
///\sa SetHeap |
433 | 433 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
434 | 434 |
struct SetStandardHeap |
435 | 435 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
436 | 436 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
437 | 437 |
Create; |
438 | 438 |
}; |
439 | 439 |
|
440 | 440 |
template <class T> |
441 | 441 |
struct SetOperationTraitsTraits : public Traits { |
442 | 442 |
typedef T OperationTraits; |
443 | 443 |
}; |
444 | 444 |
|
445 | 445 |
/// \brief \ref named-templ-param "Named parameter" for setting |
446 | 446 |
///\c OperationTraits type |
447 | 447 |
/// |
448 | 448 |
///\ref named-templ-param "Named parameter" for setting |
449 | 449 |
///\c OperationTraits type. |
450 |
/// For more information see \ref DijkstraDefaultOperationTraits. |
|
450 |
/// For more information, see \ref DijkstraDefaultOperationTraits. |
|
451 | 451 |
template <class T> |
452 | 452 |
struct SetOperationTraits |
453 | 453 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
454 | 454 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
455 | 455 |
Create; |
456 | 456 |
}; |
457 | 457 |
|
458 | 458 |
///@} |
459 | 459 |
|
460 | 460 |
protected: |
461 | 461 |
|
462 | 462 |
Dijkstra() {} |
463 | 463 |
|
464 | 464 |
public: |
465 | 465 |
|
466 | 466 |
///Constructor. |
467 | 467 |
|
468 | 468 |
///Constructor. |
469 | 469 |
///\param g The digraph the algorithm runs on. |
470 | 470 |
///\param length The length map used by the algorithm. |
471 | 471 |
Dijkstra(const Digraph& g, const LengthMap& length) : |
472 | 472 |
G(&g), _length(&length), |
473 | 473 |
_pred(NULL), local_pred(false), |
474 | 474 |
_dist(NULL), local_dist(false), |
475 | 475 |
_processed(NULL), local_processed(false), |
476 | 476 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
477 | 477 |
_heap(NULL), local_heap(false) |
478 | 478 |
{ } |
479 | 479 |
|
480 | 480 |
///Destructor. |
481 | 481 |
~Dijkstra() |
482 | 482 |
{ |
483 | 483 |
if(local_pred) delete _pred; |
484 | 484 |
if(local_dist) delete _dist; |
485 | 485 |
if(local_processed) delete _processed; |
486 | 486 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
487 | 487 |
if(local_heap) delete _heap; |
488 | 488 |
} |
489 | 489 |
|
490 | 490 |
///Sets the length map. |
491 | 491 |
|
492 | 492 |
///Sets the length map. |
493 | 493 |
///\return <tt> (*this) </tt> |
494 | 494 |
Dijkstra &lengthMap(const LengthMap &m) |
495 | 495 |
{ |
496 | 496 |
_length = &m; |
497 | 497 |
return *this; |
498 | 498 |
} |
499 | 499 |
|
500 | 500 |
///Sets the map that stores the predecessor arcs. |
501 | 501 |
|
502 | 502 |
///Sets the map that stores the predecessor arcs. |
503 | 503 |
///If you don't use this function before calling \ref run(Node) "run()" |
504 | 504 |
///or \ref init(), an instance will be allocated automatically. |
505 | 505 |
///The destructor deallocates this automatically allocated map, |
506 | 506 |
///of course. |
507 | 507 |
///\return <tt> (*this) </tt> |
508 | 508 |
Dijkstra &predMap(PredMap &m) |
509 | 509 |
{ |
510 | 510 |
if(local_pred) { |
511 | 511 |
delete _pred; |
512 | 512 |
local_pred=false; |
513 | 513 |
} |
514 | 514 |
_pred = &m; |
515 | 515 |
return *this; |
516 | 516 |
} |
517 | 517 |
|
518 | 518 |
///Sets the map that indicates which nodes are processed. |
519 | 519 |
|
520 | 520 |
///Sets the map that indicates which nodes are processed. |
521 | 521 |
///If you don't use this function before calling \ref run(Node) "run()" |
522 | 522 |
///or \ref init(), an instance will be allocated automatically. |
523 | 523 |
///The destructor deallocates this automatically allocated map, |
524 | 524 |
///of course. |
525 | 525 |
///\return <tt> (*this) </tt> |
526 | 526 |
Dijkstra &processedMap(ProcessedMap &m) |
527 | 527 |
{ |
528 | 528 |
if(local_processed) { |
529 | 529 |
delete _processed; |
530 | 530 |
local_processed=false; |
531 | 531 |
} |
532 | 532 |
_processed = &m; |
533 | 533 |
return *this; |
534 | 534 |
} |
535 | 535 |
|
536 | 536 |
///Sets the map that stores the distances of the nodes. |
537 | 537 |
|
538 | 538 |
///Sets the map that stores the distances of the nodes calculated by the |
539 | 539 |
///algorithm. |
540 | 540 |
///If you don't use this function before calling \ref run(Node) "run()" |
541 | 541 |
///or \ref init(), an instance will be allocated automatically. |
542 | 542 |
///The destructor deallocates this automatically allocated map, |
543 | 543 |
///of course. |
544 | 544 |
///\return <tt> (*this) </tt> |
545 | 545 |
Dijkstra &distMap(DistMap &m) |
546 | 546 |
{ |
547 | 547 |
if(local_dist) { |
548 | 548 |
delete _dist; |
549 | 549 |
local_dist=false; |
550 | 550 |
} |
551 | 551 |
_dist = &m; |
552 | 552 |
return *this; |
553 | 553 |
} |
554 | 554 |
|
555 | 555 |
///Sets the heap and the cross reference used by algorithm. |
556 | 556 |
|
557 | 557 |
///Sets the heap and the cross reference used by algorithm. |
558 | 558 |
///If you don't use this function before calling \ref run(Node) "run()" |
559 | 559 |
///or \ref init(), heap and cross reference instances will be |
560 | 560 |
///allocated automatically. |
561 | 561 |
///The destructor deallocates these automatically allocated objects, |
562 | 562 |
///of course. |
563 | 563 |
///\return <tt> (*this) </tt> |
564 | 564 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
565 | 565 |
{ |
566 | 566 |
if(local_heap_cross_ref) { |
567 | 567 |
delete _heap_cross_ref; |
568 | 568 |
local_heap_cross_ref=false; |
569 | 569 |
} |
570 | 570 |
_heap_cross_ref = &cr; |
571 | 571 |
if(local_heap) { |
572 | 572 |
delete _heap; |
573 | 573 |
local_heap=false; |
574 | 574 |
} |
575 | 575 |
_heap = &hp; |
576 | 576 |
return *this; |
577 | 577 |
} |
578 | 578 |
|
579 | 579 |
private: |
580 | 580 |
|
581 | 581 |
void finalizeNodeData(Node v,Value dst) |
582 | 582 |
{ |
583 | 583 |
_processed->set(v,true); |
584 | 584 |
_dist->set(v, dst); |
585 | 585 |
} |
586 | 586 |
|
587 | 587 |
public: |
588 | 588 |
|
589 | 589 |
///\name Execution Control |
590 | 590 |
///The simplest way to execute the %Dijkstra algorithm is to use |
591 | 591 |
///one of the member functions called \ref run(Node) "run()".\n |
592 | 592 |
///If you need better control on the execution, you have to call |
593 | 593 |
///\ref init() first, then you can add several source nodes with |
594 | 594 |
///\ref addSource(). Finally the actual path computation can be |
595 | 595 |
///performed with one of the \ref start() functions. |
596 | 596 |
|
597 | 597 |
///@{ |
598 | 598 |
|
599 | 599 |
///\brief Initializes the internal data structures. |
600 | 600 |
/// |
601 | 601 |
///Initializes the internal data structures. |
602 | 602 |
void init() |
603 | 603 |
{ |
604 | 604 |
create_maps(); |
605 | 605 |
_heap->clear(); |
606 | 606 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
607 | 607 |
_pred->set(u,INVALID); |
608 | 608 |
_processed->set(u,false); |
609 | 609 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
610 | 610 |
} |
611 | 611 |
} |
612 | 612 |
|
613 | 613 |
///Adds a new source node. |
614 | 614 |
|
615 | 615 |
///Adds a new source node to the priority heap. |
616 | 616 |
///The optional second parameter is the initial distance of the node. |
617 | 617 |
/// |
618 | 618 |
///The function checks if the node has already been added to the heap and |
619 | 619 |
///it is pushed to the heap only if either it was not in the heap |
620 | 620 |
///or the shortest path found till then is shorter than \c dst. |
621 | 621 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
622 | 622 |
{ |
623 | 623 |
if(_heap->state(s) != Heap::IN_HEAP) { |
624 | 624 |
_heap->push(s,dst); |
625 | 625 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
626 | 626 |
_heap->set(s,dst); |
627 | 627 |
_pred->set(s,INVALID); |
628 | 628 |
} |
629 | 629 |
} |
630 | 630 |
|
631 | 631 |
///Processes the next node in the priority heap |
632 | 632 |
|
633 | 633 |
///Processes the next node in the priority heap. |
634 | 634 |
/// |
635 | 635 |
///\return The processed node. |
636 | 636 |
/// |
637 | 637 |
///\warning The priority heap must not be empty. |
638 | 638 |
Node processNextNode() |
639 | 639 |
{ |
640 | 640 |
Node v=_heap->top(); |
641 | 641 |
Value oldvalue=_heap->prio(); |
642 | 642 |
_heap->pop(); |
... | ... |
@@ -807,385 +807,385 @@ |
807 | 807 |
///The results of the %Dijkstra algorithm can be obtained using these |
808 | 808 |
///functions.\n |
809 | 809 |
///Either \ref run(Node) "run()" or \ref init() should be called |
810 | 810 |
///before using them. |
811 | 811 |
|
812 | 812 |
///@{ |
813 | 813 |
|
814 | 814 |
///The shortest path to the given node. |
815 | 815 |
|
816 | 816 |
///Returns the shortest path to the given node from the root(s). |
817 | 817 |
/// |
818 | 818 |
///\warning \c t should be reached from the root(s). |
819 | 819 |
/// |
820 | 820 |
///\pre Either \ref run(Node) "run()" or \ref init() |
821 | 821 |
///must be called before using this function. |
822 | 822 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
823 | 823 |
|
824 | 824 |
///The distance of the given node from the root(s). |
825 | 825 |
|
826 | 826 |
///Returns the distance of the given node from the root(s). |
827 | 827 |
/// |
828 | 828 |
///\warning If node \c v is not reached from the root(s), then |
829 | 829 |
///the return value of this function is undefined. |
830 | 830 |
/// |
831 | 831 |
///\pre Either \ref run(Node) "run()" or \ref init() |
832 | 832 |
///must be called before using this function. |
833 | 833 |
Value dist(Node v) const { return (*_dist)[v]; } |
834 | 834 |
|
835 | 835 |
///\brief Returns the 'previous arc' of the shortest path tree for |
836 | 836 |
///the given node. |
837 | 837 |
/// |
838 | 838 |
///This function returns the 'previous arc' of the shortest path |
839 | 839 |
///tree for the node \c v, i.e. it returns the last arc of a |
840 | 840 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
841 | 841 |
///is not reached from the root(s) or if \c v is a root. |
842 | 842 |
/// |
843 | 843 |
///The shortest path tree used here is equal to the shortest path |
844 | 844 |
///tree used in \ref predNode() and \ref predMap(). |
845 | 845 |
/// |
846 | 846 |
///\pre Either \ref run(Node) "run()" or \ref init() |
847 | 847 |
///must be called before using this function. |
848 | 848 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
849 | 849 |
|
850 | 850 |
///\brief Returns the 'previous node' of the shortest path tree for |
851 | 851 |
///the given node. |
852 | 852 |
/// |
853 | 853 |
///This function returns the 'previous node' of the shortest path |
854 | 854 |
///tree for the node \c v, i.e. it returns the last but one node |
855 | 855 |
///of a shortest path from a root to \c v. It is \c INVALID |
856 | 856 |
///if \c v is not reached from the root(s) or if \c v is a root. |
857 | 857 |
/// |
858 | 858 |
///The shortest path tree used here is equal to the shortest path |
859 | 859 |
///tree used in \ref predArc() and \ref predMap(). |
860 | 860 |
/// |
861 | 861 |
///\pre Either \ref run(Node) "run()" or \ref init() |
862 | 862 |
///must be called before using this function. |
863 | 863 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
864 | 864 |
G->source((*_pred)[v]); } |
865 | 865 |
|
866 | 866 |
///\brief Returns a const reference to the node map that stores the |
867 | 867 |
///distances of the nodes. |
868 | 868 |
/// |
869 | 869 |
///Returns a const reference to the node map that stores the distances |
870 | 870 |
///of the nodes calculated by the algorithm. |
871 | 871 |
/// |
872 | 872 |
///\pre Either \ref run(Node) "run()" or \ref init() |
873 | 873 |
///must be called before using this function. |
874 | 874 |
const DistMap &distMap() const { return *_dist;} |
875 | 875 |
|
876 | 876 |
///\brief Returns a const reference to the node map that stores the |
877 | 877 |
///predecessor arcs. |
878 | 878 |
/// |
879 | 879 |
///Returns a const reference to the node map that stores the predecessor |
880 | 880 |
///arcs, which form the shortest path tree (forest). |
881 | 881 |
/// |
882 | 882 |
///\pre Either \ref run(Node) "run()" or \ref init() |
883 | 883 |
///must be called before using this function. |
884 | 884 |
const PredMap &predMap() const { return *_pred;} |
885 | 885 |
|
886 | 886 |
///Checks if the given node is reached from the root(s). |
887 | 887 |
|
888 | 888 |
///Returns \c true if \c v is reached from the root(s). |
889 | 889 |
/// |
890 | 890 |
///\pre Either \ref run(Node) "run()" or \ref init() |
891 | 891 |
///must be called before using this function. |
892 | 892 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
893 | 893 |
Heap::PRE_HEAP; } |
894 | 894 |
|
895 | 895 |
///Checks if a node is processed. |
896 | 896 |
|
897 | 897 |
///Returns \c true if \c v is processed, i.e. the shortest |
898 | 898 |
///path to \c v has already found. |
899 | 899 |
/// |
900 | 900 |
///\pre Either \ref run(Node) "run()" or \ref init() |
901 | 901 |
///must be called before using this function. |
902 | 902 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
903 | 903 |
Heap::POST_HEAP; } |
904 | 904 |
|
905 | 905 |
///The current distance of the given node from the root(s). |
906 | 906 |
|
907 | 907 |
///Returns the current distance of the given node from the root(s). |
908 | 908 |
///It may be decreased in the following processes. |
909 | 909 |
/// |
910 | 910 |
///\pre Either \ref run(Node) "run()" or \ref init() |
911 | 911 |
///must be called before using this function and |
912 | 912 |
///node \c v must be reached but not necessarily processed. |
913 | 913 |
Value currentDist(Node v) const { |
914 | 914 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
915 | 915 |
} |
916 | 916 |
|
917 | 917 |
///@} |
918 | 918 |
}; |
919 | 919 |
|
920 | 920 |
|
921 | 921 |
///Default traits class of dijkstra() function. |
922 | 922 |
|
923 | 923 |
///Default traits class of dijkstra() function. |
924 | 924 |
///\tparam GR The type of the digraph. |
925 | 925 |
///\tparam LEN The type of the length map. |
926 | 926 |
template<class GR, class LEN> |
927 | 927 |
struct DijkstraWizardDefaultTraits |
928 | 928 |
{ |
929 | 929 |
///The type of the digraph the algorithm runs on. |
930 | 930 |
typedef GR Digraph; |
931 | 931 |
///The type of the map that stores the arc lengths. |
932 | 932 |
|
933 | 933 |
///The type of the map that stores the arc lengths. |
934 | 934 |
///It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
935 | 935 |
typedef LEN LengthMap; |
936 | 936 |
///The type of the arc lengths. |
937 | 937 |
typedef typename LEN::Value Value; |
938 | 938 |
|
939 | 939 |
/// Operation traits for Dijkstra algorithm. |
940 | 940 |
|
941 | 941 |
/// This class defines the operations that are used in the algorithm. |
942 | 942 |
/// \see DijkstraDefaultOperationTraits |
943 | 943 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
944 | 944 |
|
945 | 945 |
/// The cross reference type used by the heap. |
946 | 946 |
|
947 | 947 |
/// The cross reference type used by the heap. |
948 | 948 |
/// Usually it is \c Digraph::NodeMap<int>. |
949 | 949 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
950 | 950 |
///Instantiates a \ref HeapCrossRef. |
951 | 951 |
|
952 | 952 |
///This function instantiates a \ref HeapCrossRef. |
953 | 953 |
/// \param g is the digraph, to which we would like to define the |
954 | 954 |
/// HeapCrossRef. |
955 | 955 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
956 | 956 |
{ |
957 | 957 |
return new HeapCrossRef(g); |
958 | 958 |
} |
959 | 959 |
|
960 | 960 |
///The heap type used by the Dijkstra algorithm. |
961 | 961 |
|
962 | 962 |
///The heap type used by the Dijkstra algorithm. |
963 | 963 |
/// |
964 | 964 |
///\sa BinHeap |
965 | 965 |
///\sa Dijkstra |
966 | 966 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
967 | 967 |
std::less<Value> > Heap; |
968 | 968 |
|
969 | 969 |
///Instantiates a \ref Heap. |
970 | 970 |
|
971 | 971 |
///This function instantiates a \ref Heap. |
972 | 972 |
/// \param r is the HeapCrossRef which is used. |
973 | 973 |
static Heap *createHeap(HeapCrossRef& r) |
974 | 974 |
{ |
975 | 975 |
return new Heap(r); |
976 | 976 |
} |
977 | 977 |
|
978 | 978 |
///\brief The type of the map that stores the predecessor |
979 | 979 |
///arcs of the shortest paths. |
980 | 980 |
/// |
981 | 981 |
///The type of the map that stores the predecessor |
982 | 982 |
///arcs of the shortest paths. |
983 | 983 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
984 | 984 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
985 | 985 |
///Instantiates a PredMap. |
986 | 986 |
|
987 | 987 |
///This function instantiates a PredMap. |
988 | 988 |
///\param g is the digraph, to which we would like to define the |
989 | 989 |
///PredMap. |
990 | 990 |
static PredMap *createPredMap(const Digraph &g) |
991 | 991 |
{ |
992 | 992 |
return new PredMap(g); |
993 | 993 |
} |
994 | 994 |
|
995 | 995 |
///The type of the map that indicates which nodes are processed. |
996 | 996 |
|
997 | 997 |
///The type of the map that indicates which nodes are processed. |
998 | 998 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
999 |
///By default it is a NullMap. |
|
999 |
///By default, it is a NullMap. |
|
1000 | 1000 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1001 | 1001 |
///Instantiates a ProcessedMap. |
1002 | 1002 |
|
1003 | 1003 |
///This function instantiates a ProcessedMap. |
1004 | 1004 |
///\param g is the digraph, to which |
1005 | 1005 |
///we would like to define the ProcessedMap. |
1006 | 1006 |
#ifdef DOXYGEN |
1007 | 1007 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1008 | 1008 |
#else |
1009 | 1009 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1010 | 1010 |
#endif |
1011 | 1011 |
{ |
1012 | 1012 |
return new ProcessedMap(); |
1013 | 1013 |
} |
1014 | 1014 |
|
1015 | 1015 |
///The type of the map that stores the distances of the nodes. |
1016 | 1016 |
|
1017 | 1017 |
///The type of the map that stores the distances of the nodes. |
1018 | 1018 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
1019 | 1019 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
1020 | 1020 |
///Instantiates a DistMap. |
1021 | 1021 |
|
1022 | 1022 |
///This function instantiates a DistMap. |
1023 | 1023 |
///\param g is the digraph, to which we would like to define |
1024 | 1024 |
///the DistMap |
1025 | 1025 |
static DistMap *createDistMap(const Digraph &g) |
1026 | 1026 |
{ |
1027 | 1027 |
return new DistMap(g); |
1028 | 1028 |
} |
1029 | 1029 |
|
1030 | 1030 |
///The type of the shortest paths. |
1031 | 1031 |
|
1032 | 1032 |
///The type of the shortest paths. |
1033 | 1033 |
///It must conform to the \ref concepts::Path "Path" concept. |
1034 | 1034 |
typedef lemon::Path<Digraph> Path; |
1035 | 1035 |
}; |
1036 | 1036 |
|
1037 | 1037 |
/// Default traits class used by DijkstraWizard |
1038 | 1038 |
|
1039 | 1039 |
/// Default traits class used by DijkstraWizard. |
1040 | 1040 |
/// \tparam GR The type of the digraph. |
1041 | 1041 |
/// \tparam LEN The type of the length map. |
1042 | 1042 |
template<typename GR, typename LEN> |
1043 | 1043 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LEN> |
1044 | 1044 |
{ |
1045 | 1045 |
typedef DijkstraWizardDefaultTraits<GR,LEN> Base; |
1046 | 1046 |
protected: |
1047 | 1047 |
//The type of the nodes in the digraph. |
1048 | 1048 |
typedef typename Base::Digraph::Node Node; |
1049 | 1049 |
|
1050 | 1050 |
//Pointer to the digraph the algorithm runs on. |
1051 | 1051 |
void *_g; |
1052 | 1052 |
//Pointer to the length map. |
1053 | 1053 |
void *_length; |
1054 | 1054 |
//Pointer to the map of processed nodes. |
1055 | 1055 |
void *_processed; |
1056 | 1056 |
//Pointer to the map of predecessors arcs. |
1057 | 1057 |
void *_pred; |
1058 | 1058 |
//Pointer to the map of distances. |
1059 | 1059 |
void *_dist; |
1060 | 1060 |
//Pointer to the shortest path to the target node. |
1061 | 1061 |
void *_path; |
1062 | 1062 |
//Pointer to the distance of the target node. |
1063 | 1063 |
void *_di; |
1064 | 1064 |
|
1065 | 1065 |
public: |
1066 | 1066 |
/// Constructor. |
1067 | 1067 |
|
1068 | 1068 |
/// This constructor does not require parameters, therefore it initiates |
1069 | 1069 |
/// all of the attributes to \c 0. |
1070 | 1070 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1071 | 1071 |
_dist(0), _path(0), _di(0) {} |
1072 | 1072 |
|
1073 | 1073 |
/// Constructor. |
1074 | 1074 |
|
1075 | 1075 |
/// This constructor requires two parameters, |
1076 | 1076 |
/// others are initiated to \c 0. |
1077 | 1077 |
/// \param g The digraph the algorithm runs on. |
1078 | 1078 |
/// \param l The length map. |
1079 | 1079 |
DijkstraWizardBase(const GR &g,const LEN &l) : |
1080 | 1080 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1081 | 1081 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&l))), |
1082 | 1082 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1083 | 1083 |
|
1084 | 1084 |
}; |
1085 | 1085 |
|
1086 | 1086 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1087 | 1087 |
|
1088 | 1088 |
/// This auxiliary class is created to implement the |
1089 | 1089 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1090 | 1090 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
1091 | 1091 |
/// functions and features of the plain \ref Dijkstra. |
1092 | 1092 |
/// |
1093 | 1093 |
/// This class should only be used through the \ref dijkstra() function, |
1094 | 1094 |
/// which makes it easier to use the algorithm. |
1095 | 1095 |
template<class TR> |
1096 | 1096 |
class DijkstraWizard : public TR |
1097 | 1097 |
{ |
1098 | 1098 |
typedef TR Base; |
1099 | 1099 |
|
1100 | 1100 |
typedef typename TR::Digraph Digraph; |
1101 | 1101 |
|
1102 | 1102 |
typedef typename Digraph::Node Node; |
1103 | 1103 |
typedef typename Digraph::NodeIt NodeIt; |
1104 | 1104 |
typedef typename Digraph::Arc Arc; |
1105 | 1105 |
typedef typename Digraph::OutArcIt OutArcIt; |
1106 | 1106 |
|
1107 | 1107 |
typedef typename TR::LengthMap LengthMap; |
1108 | 1108 |
typedef typename LengthMap::Value Value; |
1109 | 1109 |
typedef typename TR::PredMap PredMap; |
1110 | 1110 |
typedef typename TR::DistMap DistMap; |
1111 | 1111 |
typedef typename TR::ProcessedMap ProcessedMap; |
1112 | 1112 |
typedef typename TR::Path Path; |
1113 | 1113 |
typedef typename TR::Heap Heap; |
1114 | 1114 |
|
1115 | 1115 |
public: |
1116 | 1116 |
|
1117 | 1117 |
/// Constructor. |
1118 | 1118 |
DijkstraWizard() : TR() {} |
1119 | 1119 |
|
1120 | 1120 |
/// Constructor that requires parameters. |
1121 | 1121 |
|
1122 | 1122 |
/// Constructor that requires parameters. |
1123 | 1123 |
/// These parameters will be the default values for the traits class. |
1124 | 1124 |
/// \param g The digraph the algorithm runs on. |
1125 | 1125 |
/// \param l The length map. |
1126 | 1126 |
DijkstraWizard(const Digraph &g, const LengthMap &l) : |
1127 | 1127 |
TR(g,l) {} |
1128 | 1128 |
|
1129 | 1129 |
///Copy constructor |
1130 | 1130 |
DijkstraWizard(const TR &b) : TR(b) {} |
1131 | 1131 |
|
1132 | 1132 |
~DijkstraWizard() {} |
1133 | 1133 |
|
1134 | 1134 |
///Runs Dijkstra algorithm from the given source node. |
1135 | 1135 |
|
1136 | 1136 |
///This method runs %Dijkstra algorithm from the given source node |
1137 | 1137 |
///in order to compute the shortest path to each node. |
1138 | 1138 |
void run(Node s) |
1139 | 1139 |
{ |
1140 | 1140 |
Dijkstra<Digraph,LengthMap,TR> |
1141 | 1141 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1142 | 1142 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1143 | 1143 |
if (Base::_pred) |
1144 | 1144 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1145 | 1145 |
if (Base::_dist) |
1146 | 1146 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1147 | 1147 |
if (Base::_processed) |
1148 | 1148 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1149 | 1149 |
dijk.run(s); |
1150 | 1150 |
} |
1151 | 1151 |
|
1152 | 1152 |
///Finds the shortest path between \c s and \c t. |
1153 | 1153 |
|
1154 | 1154 |
///This method runs the %Dijkstra algorithm from node \c s |
1155 | 1155 |
///in order to compute the shortest path to node \c t |
1156 | 1156 |
///(it stops searching when \c t is processed). |
1157 | 1157 |
/// |
1158 | 1158 |
///\return \c true if \c t is reachable form \c s. |
1159 | 1159 |
bool run(Node s, Node t) |
1160 | 1160 |
{ |
1161 | 1161 |
Dijkstra<Digraph,LengthMap,TR> |
1162 | 1162 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1163 | 1163 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1164 | 1164 |
if (Base::_pred) |
1165 | 1165 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1166 | 1166 |
if (Base::_dist) |
1167 | 1167 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1168 | 1168 |
if (Base::_processed) |
1169 | 1169 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1170 | 1170 |
dijk.run(s,t); |
1171 | 1171 |
if (Base::_path) |
1172 | 1172 |
*reinterpret_cast<Path*>(Base::_path) = dijk.path(t); |
1173 | 1173 |
if (Base::_di) |
1174 | 1174 |
*reinterpret_cast<Value*>(Base::_di) = dijk.dist(t); |
1175 | 1175 |
return dijk.reached(t); |
1176 | 1176 |
} |
1177 | 1177 |
|
1178 | 1178 |
template<class T> |
1179 | 1179 |
struct SetPredMapBase : public Base { |
1180 | 1180 |
typedef T PredMap; |
1181 | 1181 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1182 | 1182 |
SetPredMapBase(const TR &b) : TR(b) {} |
1183 | 1183 |
}; |
1184 | 1184 |
|
1185 | 1185 |
///\brief \ref named-templ-param "Named parameter" for setting |
1186 | 1186 |
///the predecessor map. |
1187 | 1187 |
/// |
1188 | 1188 |
///\ref named-templ-param "Named parameter" function for setting |
1189 | 1189 |
///the map that stores the predecessor arcs of the nodes. |
1190 | 1190 |
template<class T> |
1191 | 1191 |
DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
... | ... |
@@ -105,466 +105,464 @@ |
105 | 105 |
} |
106 | 106 |
if (_weight) { |
107 | 107 |
delete _weight; |
108 | 108 |
} |
109 | 109 |
if (_order) { |
110 | 110 |
delete _order; |
111 | 111 |
} |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
public: |
115 | 115 |
|
116 | 116 |
/// \brief Constructor |
117 | 117 |
/// |
118 | 118 |
/// Constructor. |
119 | 119 |
/// \param graph The undirected graph the algorithm runs on. |
120 | 120 |
/// \param capacity The edge capacity map. |
121 | 121 |
GomoryHu(const Graph& graph, const Capacity& capacity) |
122 | 122 |
: _graph(graph), _capacity(capacity), |
123 | 123 |
_pred(0), _weight(0), _order(0) |
124 | 124 |
{ |
125 | 125 |
checkConcept<concepts::ReadMap<Edge, Value>, Capacity>(); |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
|
129 | 129 |
/// \brief Destructor |
130 | 130 |
/// |
131 | 131 |
/// Destructor. |
132 | 132 |
~GomoryHu() { |
133 | 133 |
destroyStructures(); |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
private: |
137 | 137 |
|
138 | 138 |
// Initialize the internal data structures |
139 | 139 |
void init() { |
140 | 140 |
createStructures(); |
141 | 141 |
|
142 | 142 |
_root = NodeIt(_graph); |
143 | 143 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
144 | 144 |
(*_pred)[n] = _root; |
145 | 145 |
(*_order)[n] = -1; |
146 | 146 |
} |
147 | 147 |
(*_pred)[_root] = INVALID; |
148 | 148 |
(*_weight)[_root] = std::numeric_limits<Value>::max(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
|
152 | 152 |
// Start the algorithm |
153 | 153 |
void start() { |
154 | 154 |
Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID); |
155 | 155 |
|
156 | 156 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
157 | 157 |
if (n == _root) continue; |
158 | 158 |
|
159 | 159 |
Node pn = (*_pred)[n]; |
160 | 160 |
fa.source(n); |
161 | 161 |
fa.target(pn); |
162 | 162 |
|
163 | 163 |
fa.runMinCut(); |
164 | 164 |
|
165 | 165 |
(*_weight)[n] = fa.flowValue(); |
166 | 166 |
|
167 | 167 |
for (NodeIt nn(_graph); nn != INVALID; ++nn) { |
168 | 168 |
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) { |
169 | 169 |
(*_pred)[nn] = n; |
170 | 170 |
} |
171 | 171 |
} |
172 | 172 |
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) { |
173 | 173 |
(*_pred)[n] = (*_pred)[pn]; |
174 | 174 |
(*_pred)[pn] = n; |
175 | 175 |
(*_weight)[n] = (*_weight)[pn]; |
176 | 176 |
(*_weight)[pn] = fa.flowValue(); |
177 | 177 |
} |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
(*_order)[_root] = 0; |
181 | 181 |
int index = 1; |
182 | 182 |
|
183 | 183 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
184 | 184 |
std::vector<Node> st; |
185 | 185 |
Node nn = n; |
186 | 186 |
while ((*_order)[nn] == -1) { |
187 | 187 |
st.push_back(nn); |
188 | 188 |
nn = (*_pred)[nn]; |
189 | 189 |
} |
190 | 190 |
while (!st.empty()) { |
191 | 191 |
(*_order)[st.back()] = index++; |
192 | 192 |
st.pop_back(); |
193 | 193 |
} |
194 | 194 |
} |
195 | 195 |
} |
196 | 196 |
|
197 | 197 |
public: |
198 | 198 |
|
199 | 199 |
///\name Execution Control |
200 | 200 |
|
201 | 201 |
///@{ |
202 | 202 |
|
203 | 203 |
/// \brief Run the Gomory-Hu algorithm. |
204 | 204 |
/// |
205 | 205 |
/// This function runs the Gomory-Hu algorithm. |
206 | 206 |
void run() { |
207 | 207 |
init(); |
208 | 208 |
start(); |
209 | 209 |
} |
210 | 210 |
|
211 | 211 |
/// @} |
212 | 212 |
|
213 | 213 |
///\name Query Functions |
214 | 214 |
///The results of the algorithm can be obtained using these |
215 | 215 |
///functions.\n |
216 | 216 |
///\ref run() should be called before using them.\n |
217 | 217 |
///See also \ref MinCutNodeIt and \ref MinCutEdgeIt. |
218 | 218 |
|
219 | 219 |
///@{ |
220 | 220 |
|
221 | 221 |
/// \brief Return the predecessor node in the Gomory-Hu tree. |
222 | 222 |
/// |
223 | 223 |
/// This function returns the predecessor node of the given node |
224 | 224 |
/// in the Gomory-Hu tree. |
225 | 225 |
/// If \c node is the root of the tree, then it returns \c INVALID. |
226 | 226 |
/// |
227 | 227 |
/// \pre \ref run() must be called before using this function. |
228 | 228 |
Node predNode(const Node& node) const { |
229 | 229 |
return (*_pred)[node]; |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
/// \brief Return the weight of the predecessor edge in the |
233 | 233 |
/// Gomory-Hu tree. |
234 | 234 |
/// |
235 | 235 |
/// This function returns the weight of the predecessor edge of the |
236 | 236 |
/// given node in the Gomory-Hu tree. |
237 | 237 |
/// If \c node is the root of the tree, the result is undefined. |
238 | 238 |
/// |
239 | 239 |
/// \pre \ref run() must be called before using this function. |
240 | 240 |
Value predValue(const Node& node) const { |
241 | 241 |
return (*_weight)[node]; |
242 | 242 |
} |
243 | 243 |
|
244 | 244 |
/// \brief Return the distance from the root node in the Gomory-Hu tree. |
245 | 245 |
/// |
246 | 246 |
/// This function returns the distance of the given node from the root |
247 | 247 |
/// node in the Gomory-Hu tree. |
248 | 248 |
/// |
249 | 249 |
/// \pre \ref run() must be called before using this function. |
250 | 250 |
int rootDist(const Node& node) const { |
251 | 251 |
return (*_order)[node]; |
252 | 252 |
} |
253 | 253 |
|
254 | 254 |
/// \brief Return the minimum cut value between two nodes |
255 | 255 |
/// |
256 | 256 |
/// This function returns the minimum cut value between the nodes |
257 | 257 |
/// \c s and \c t. |
258 | 258 |
/// It finds the nearest common ancestor of the given nodes in the |
259 | 259 |
/// Gomory-Hu tree and calculates the minimum weight edge on the |
260 | 260 |
/// paths to the ancestor. |
261 | 261 |
/// |
262 | 262 |
/// \pre \ref run() must be called before using this function. |
263 | 263 |
Value minCutValue(const Node& s, const Node& t) const { |
264 | 264 |
Node sn = s, tn = t; |
265 | 265 |
Value value = std::numeric_limits<Value>::max(); |
266 | 266 |
|
267 | 267 |
while (sn != tn) { |
268 | 268 |
if ((*_order)[sn] < (*_order)[tn]) { |
269 | 269 |
if ((*_weight)[tn] <= value) value = (*_weight)[tn]; |
270 | 270 |
tn = (*_pred)[tn]; |
271 | 271 |
} else { |
272 | 272 |
if ((*_weight)[sn] <= value) value = (*_weight)[sn]; |
273 | 273 |
sn = (*_pred)[sn]; |
274 | 274 |
} |
275 | 275 |
} |
276 | 276 |
return value; |
277 | 277 |
} |
278 | 278 |
|
279 | 279 |
/// \brief Return the minimum cut between two nodes |
280 | 280 |
/// |
281 | 281 |
/// This function returns the minimum cut between the nodes \c s and \c t |
282 | 282 |
/// in the \c cutMap parameter by setting the nodes in the component of |
283 | 283 |
/// \c s to \c true and the other nodes to \c false. |
284 | 284 |
/// |
285 | 285 |
/// For higher level interfaces see MinCutNodeIt and MinCutEdgeIt. |
286 | 286 |
/// |
287 | 287 |
/// \param s The base node. |
288 | 288 |
/// \param t The node you want to separate from node \c s. |
289 | 289 |
/// \param cutMap The cut will be returned in this map. |
290 | 290 |
/// It must be a \c bool (or convertible) \ref concepts::ReadWriteMap |
291 | 291 |
/// "ReadWriteMap" on the graph nodes. |
292 | 292 |
/// |
293 | 293 |
/// \return The value of the minimum cut between \c s and \c t. |
294 | 294 |
/// |
295 | 295 |
/// \pre \ref run() must be called before using this function. |
296 | 296 |
template <typename CutMap> |
297 |
Value minCutMap(const Node& s, |
|
297 |
Value minCutMap(const Node& s, |
|
298 | 298 |
const Node& t, |
299 |
///< |
|
300 | 299 |
CutMap& cutMap |
301 |
///< |
|
302 | 300 |
) const { |
303 | 301 |
Node sn = s, tn = t; |
304 | 302 |
bool s_root=false; |
305 | 303 |
Node rn = INVALID; |
306 | 304 |
Value value = std::numeric_limits<Value>::max(); |
307 | 305 |
|
308 | 306 |
while (sn != tn) { |
309 | 307 |
if ((*_order)[sn] < (*_order)[tn]) { |
310 | 308 |
if ((*_weight)[tn] <= value) { |
311 | 309 |
rn = tn; |
312 | 310 |
s_root = false; |
313 | 311 |
value = (*_weight)[tn]; |
314 | 312 |
} |
315 | 313 |
tn = (*_pred)[tn]; |
316 | 314 |
} else { |
317 | 315 |
if ((*_weight)[sn] <= value) { |
318 | 316 |
rn = sn; |
319 | 317 |
s_root = true; |
320 | 318 |
value = (*_weight)[sn]; |
321 | 319 |
} |
322 | 320 |
sn = (*_pred)[sn]; |
323 | 321 |
} |
324 | 322 |
} |
325 | 323 |
|
326 | 324 |
typename Graph::template NodeMap<bool> reached(_graph, false); |
327 | 325 |
reached[_root] = true; |
328 | 326 |
cutMap.set(_root, !s_root); |
329 | 327 |
reached[rn] = true; |
330 | 328 |
cutMap.set(rn, s_root); |
331 | 329 |
|
332 | 330 |
std::vector<Node> st; |
333 | 331 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
334 | 332 |
st.clear(); |
335 | 333 |
Node nn = n; |
336 | 334 |
while (!reached[nn]) { |
337 | 335 |
st.push_back(nn); |
338 | 336 |
nn = (*_pred)[nn]; |
339 | 337 |
} |
340 | 338 |
while (!st.empty()) { |
341 | 339 |
cutMap.set(st.back(), cutMap[nn]); |
342 | 340 |
st.pop_back(); |
343 | 341 |
} |
344 | 342 |
} |
345 | 343 |
|
346 | 344 |
return value; |
347 | 345 |
} |
348 | 346 |
|
349 | 347 |
///@} |
350 | 348 |
|
351 | 349 |
friend class MinCutNodeIt; |
352 | 350 |
|
353 | 351 |
/// Iterate on the nodes of a minimum cut |
354 | 352 |
|
355 | 353 |
/// This iterator class lists the nodes of a minimum cut found by |
356 | 354 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class |
357 | 355 |
/// and call its \ref GomoryHu::run() "run()" method. |
358 | 356 |
/// |
359 | 357 |
/// This example counts the nodes in the minimum cut separating \c s from |
360 | 358 |
/// \c t. |
361 | 359 |
/// \code |
362 | 360 |
/// GomoryHu<Graph> gom(g, capacities); |
363 | 361 |
/// gom.run(); |
364 | 362 |
/// int cnt=0; |
365 | 363 |
/// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt; |
366 | 364 |
/// \endcode |
367 | 365 |
class MinCutNodeIt |
368 | 366 |
{ |
369 | 367 |
bool _side; |
370 | 368 |
typename Graph::NodeIt _node_it; |
371 | 369 |
typename Graph::template NodeMap<bool> _cut; |
372 | 370 |
public: |
373 | 371 |
/// Constructor |
374 | 372 |
|
375 | 373 |
/// Constructor. |
376 | 374 |
/// |
377 | 375 |
MinCutNodeIt(GomoryHu const &gomory, |
378 | 376 |
///< The GomoryHu class. You must call its |
379 | 377 |
/// run() method |
380 | 378 |
/// before initializing this iterator. |
381 | 379 |
const Node& s, ///< The base node. |
382 | 380 |
const Node& t, |
383 | 381 |
///< The node you want to separate from node \c s. |
384 | 382 |
bool side=true |
385 | 383 |
///< If it is \c true (default) then the iterator lists |
386 | 384 |
/// the nodes of the component containing \c s, |
387 | 385 |
/// otherwise it lists the other component. |
388 | 386 |
/// \note As the minimum cut is not always unique, |
389 | 387 |
/// \code |
390 | 388 |
/// MinCutNodeIt(gomory, s, t, true); |
391 | 389 |
/// \endcode |
392 | 390 |
/// and |
393 | 391 |
/// \code |
394 | 392 |
/// MinCutNodeIt(gomory, t, s, false); |
395 | 393 |
/// \endcode |
396 | 394 |
/// does not necessarily give the same set of nodes. |
397 |
/// However it is ensured that |
|
395 |
/// However, it is ensured that |
|
398 | 396 |
/// \code |
399 | 397 |
/// MinCutNodeIt(gomory, s, t, true); |
400 | 398 |
/// \endcode |
401 | 399 |
/// and |
402 | 400 |
/// \code |
403 | 401 |
/// MinCutNodeIt(gomory, s, t, false); |
404 | 402 |
/// \endcode |
405 | 403 |
/// together list each node exactly once. |
406 | 404 |
) |
407 | 405 |
: _side(side), _cut(gomory._graph) |
408 | 406 |
{ |
409 | 407 |
gomory.minCutMap(s,t,_cut); |
410 | 408 |
for(_node_it=typename Graph::NodeIt(gomory._graph); |
411 | 409 |
_node_it!=INVALID && _cut[_node_it]!=_side; |
412 | 410 |
++_node_it) {} |
413 | 411 |
} |
414 | 412 |
/// Conversion to \c Node |
415 | 413 |
|
416 | 414 |
/// Conversion to \c Node. |
417 | 415 |
/// |
418 | 416 |
operator typename Graph::Node() const |
419 | 417 |
{ |
420 | 418 |
return _node_it; |
421 | 419 |
} |
422 | 420 |
bool operator==(Invalid) { return _node_it==INVALID; } |
423 | 421 |
bool operator!=(Invalid) { return _node_it!=INVALID; } |
424 | 422 |
/// Next node |
425 | 423 |
|
426 | 424 |
/// Next node. |
427 | 425 |
/// |
428 | 426 |
MinCutNodeIt &operator++() |
429 | 427 |
{ |
430 | 428 |
for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {} |
431 | 429 |
return *this; |
432 | 430 |
} |
433 | 431 |
/// Postfix incrementation |
434 | 432 |
|
435 | 433 |
/// Postfix incrementation. |
436 | 434 |
/// |
437 | 435 |
/// \warning This incrementation |
438 | 436 |
/// returns a \c Node, not a \c MinCutNodeIt, as one may |
439 | 437 |
/// expect. |
440 | 438 |
typename Graph::Node operator++(int) |
441 | 439 |
{ |
442 | 440 |
typename Graph::Node n=*this; |
443 | 441 |
++(*this); |
444 | 442 |
return n; |
445 | 443 |
} |
446 | 444 |
}; |
447 | 445 |
|
448 | 446 |
friend class MinCutEdgeIt; |
449 | 447 |
|
450 | 448 |
/// Iterate on the edges of a minimum cut |
451 | 449 |
|
452 | 450 |
/// This iterator class lists the edges of a minimum cut found by |
453 | 451 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class |
454 | 452 |
/// and call its \ref GomoryHu::run() "run()" method. |
455 | 453 |
/// |
456 | 454 |
/// This example computes the value of the minimum cut separating \c s from |
457 | 455 |
/// \c t. |
458 | 456 |
/// \code |
459 | 457 |
/// GomoryHu<Graph> gom(g, capacities); |
460 | 458 |
/// gom.run(); |
461 | 459 |
/// int value=0; |
462 | 460 |
/// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e) |
463 | 461 |
/// value+=capacities[e]; |
464 | 462 |
/// \endcode |
465 | 463 |
/// The result will be the same as the value returned by |
466 | 464 |
/// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)". |
467 | 465 |
class MinCutEdgeIt |
468 | 466 |
{ |
469 | 467 |
bool _side; |
470 | 468 |
const Graph &_graph; |
471 | 469 |
typename Graph::NodeIt _node_it; |
472 | 470 |
typename Graph::OutArcIt _arc_it; |
473 | 471 |
typename Graph::template NodeMap<bool> _cut; |
474 | 472 |
void step() |
475 | 473 |
{ |
476 | 474 |
++_arc_it; |
477 | 475 |
while(_node_it!=INVALID && _arc_it==INVALID) |
478 | 476 |
{ |
479 | 477 |
for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {} |
480 | 478 |
if(_node_it!=INVALID) |
481 | 479 |
_arc_it=typename Graph::OutArcIt(_graph,_node_it); |
482 | 480 |
} |
483 | 481 |
} |
484 | 482 |
|
485 | 483 |
public: |
486 | 484 |
/// Constructor |
487 | 485 |
|
488 | 486 |
/// Constructor. |
489 | 487 |
/// |
490 | 488 |
MinCutEdgeIt(GomoryHu const &gomory, |
491 | 489 |
///< The GomoryHu class. You must call its |
492 | 490 |
/// run() method |
493 | 491 |
/// before initializing this iterator. |
494 | 492 |
const Node& s, ///< The base node. |
495 | 493 |
const Node& t, |
496 | 494 |
///< The node you want to separate from node \c s. |
497 | 495 |
bool side=true |
498 | 496 |
///< If it is \c true (default) then the listed arcs |
499 | 497 |
/// will be oriented from the |
500 | 498 |
/// nodes of the component containing \c s, |
501 | 499 |
/// otherwise they will be oriented in the opposite |
502 | 500 |
/// direction. |
503 | 501 |
) |
504 | 502 |
: _graph(gomory._graph), _cut(_graph) |
505 | 503 |
{ |
506 | 504 |
gomory.minCutMap(s,t,_cut); |
507 | 505 |
if(!side) |
508 | 506 |
for(typename Graph::NodeIt n(_graph);n!=INVALID;++n) |
509 | 507 |
_cut[n]=!_cut[n]; |
510 | 508 |
|
511 | 509 |
for(_node_it=typename Graph::NodeIt(_graph); |
512 | 510 |
_node_it!=INVALID && !_cut[_node_it]; |
513 | 511 |
++_node_it) {} |
514 | 512 |
_arc_it = _node_it!=INVALID ? |
515 | 513 |
typename Graph::OutArcIt(_graph,_node_it) : INVALID; |
516 | 514 |
while(_node_it!=INVALID && _arc_it == INVALID) |
517 | 515 |
{ |
518 | 516 |
for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {} |
519 | 517 |
if(_node_it!=INVALID) |
520 | 518 |
_arc_it= typename Graph::OutArcIt(_graph,_node_it); |
521 | 519 |
} |
522 | 520 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step(); |
523 | 521 |
} |
524 | 522 |
/// Conversion to \c Arc |
525 | 523 |
|
526 | 524 |
/// Conversion to \c Arc. |
527 | 525 |
/// |
528 | 526 |
operator typename Graph::Arc() const |
529 | 527 |
{ |
530 | 528 |
return _arc_it; |
531 | 529 |
} |
532 | 530 |
/// Conversion to \c Edge |
533 | 531 |
|
534 | 532 |
/// Conversion to \c Edge. |
535 | 533 |
/// |
536 | 534 |
operator typename Graph::Edge() const |
537 | 535 |
{ |
538 | 536 |
return _arc_it; |
539 | 537 |
} |
540 | 538 |
bool operator==(Invalid) { return _node_it==INVALID; } |
541 | 539 |
bool operator!=(Invalid) { return _node_it!=INVALID; } |
542 | 540 |
/// Next edge |
543 | 541 |
|
544 | 542 |
/// Next edge. |
545 | 543 |
/// |
546 | 544 |
MinCutEdgeIt &operator++() |
547 | 545 |
{ |
548 | 546 |
step(); |
549 | 547 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step(); |
550 | 548 |
return *this; |
551 | 549 |
} |
552 | 550 |
/// Postfix incrementation |
553 | 551 |
|
554 | 552 |
/// Postfix incrementation. |
555 | 553 |
/// |
556 | 554 |
/// \warning This incrementation |
557 | 555 |
/// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect. |
558 | 556 |
typename Graph::Arc operator++(int) |
559 | 557 |
{ |
560 | 558 |
typename Graph::Arc e=*this; |
561 | 559 |
++(*this); |
562 | 560 |
return e; |
563 | 561 |
} |
564 | 562 |
}; |
565 | 563 |
|
566 | 564 |
}; |
567 | 565 |
|
568 | 566 |
} |
569 | 567 |
|
570 | 568 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#include<lemon/bits/windows.h> |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
#include<lemon/core.h> |
37 | 37 |
#include<lemon/dim2.h> |
38 | 38 |
#include<lemon/maps.h> |
39 | 39 |
#include<lemon/color.h> |
40 | 40 |
#include<lemon/bits/bezier.h> |
41 | 41 |
#include<lemon/error.h> |
42 | 42 |
|
43 | 43 |
|
44 | 44 |
///\ingroup eps_io |
45 | 45 |
///\file |
46 | 46 |
///\brief A well configurable tool for visualizing graphs |
47 | 47 |
|
48 | 48 |
namespace lemon { |
49 | 49 |
|
50 | 50 |
namespace _graph_to_eps_bits { |
51 | 51 |
template<class MT> |
52 | 52 |
class _NegY { |
53 | 53 |
public: |
54 | 54 |
typedef typename MT::Key Key; |
55 | 55 |
typedef typename MT::Value Value; |
56 | 56 |
const MT ↦ |
57 | 57 |
int yscale; |
58 | 58 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
59 | 59 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
60 | 60 |
}; |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
///Default traits class of GraphToEps |
64 | 64 |
|
65 | 65 |
///Default traits class of \ref GraphToEps. |
66 | 66 |
/// |
67 | 67 |
///\param GR is the type of the underlying graph. |
68 | 68 |
template<class GR> |
69 | 69 |
struct DefaultGraphToEpsTraits |
70 | 70 |
{ |
71 | 71 |
typedef GR Graph; |
72 | 72 |
typedef GR Digraph; |
73 | 73 |
typedef typename Graph::Node Node; |
74 | 74 |
typedef typename Graph::NodeIt NodeIt; |
75 | 75 |
typedef typename Graph::Arc Arc; |
76 | 76 |
typedef typename Graph::ArcIt ArcIt; |
77 | 77 |
typedef typename Graph::InArcIt InArcIt; |
78 | 78 |
typedef typename Graph::OutArcIt OutArcIt; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
const Graph &g; |
82 | 82 |
|
83 | 83 |
std::ostream& os; |
84 | 84 |
|
85 | 85 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
86 | 86 |
CoordsMapType _coords; |
87 | 87 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
88 | 88 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
89 | 89 |
|
90 | 90 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
91 | 91 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
92 | 92 |
|
93 | 93 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
94 | 94 |
|
95 | 95 |
double _arcWidthScale; |
96 | 96 |
|
97 | 97 |
double _nodeScale; |
98 | 98 |
double _xBorder, _yBorder; |
99 | 99 |
double _scale; |
100 | 100 |
double _nodeBorderQuotient; |
101 | 101 |
|
102 | 102 |
bool _drawArrows; |
103 | 103 |
double _arrowLength, _arrowWidth; |
104 | 104 |
|
105 | 105 |
bool _showNodes, _showArcs; |
106 | 106 |
|
107 | 107 |
bool _enableParallel; |
108 | 108 |
double _parArcDist; |
109 | 109 |
|
110 | 110 |
bool _showNodeText; |
111 | 111 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
112 | 112 |
double _nodeTextSize; |
113 | 113 |
|
114 | 114 |
bool _showNodePsText; |
115 | 115 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
116 | 116 |
char *_nodePsTextsPreamble; |
117 | 117 |
|
118 | 118 |
bool _undirected; |
119 | 119 |
|
120 | 120 |
bool _pleaseRemoveOsStream; |
121 | 121 |
|
122 | 122 |
bool _scaleToA4; |
123 | 123 |
|
124 | 124 |
std::string _title; |
125 | 125 |
std::string _copyright; |
126 | 126 |
|
127 | 127 |
enum NodeTextColorType |
128 | 128 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
129 | 129 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
130 | 130 |
|
131 | 131 |
bool _autoNodeScale; |
132 | 132 |
bool _autoArcWidthScale; |
133 | 133 |
|
134 | 134 |
bool _absoluteNodeSizes; |
135 | 135 |
bool _absoluteArcWidths; |
136 | 136 |
|
137 | 137 |
bool _negY; |
138 | 138 |
|
139 | 139 |
bool _preScale; |
140 | 140 |
///Constructor |
141 | 141 |
|
142 | 142 |
///Constructor |
143 | 143 |
///\param gr Reference to the graph to be printed. |
144 | 144 |
///\param ost Reference to the output stream. |
145 |
///By default it is <tt>std::cout</tt>. |
|
145 |
///By default, it is <tt>std::cout</tt>. |
|
146 | 146 |
///\param pros If it is \c true, then the \c ostream referenced by \c os |
147 | 147 |
///will be explicitly deallocated by the destructor. |
148 | 148 |
DefaultGraphToEpsTraits(const GR &gr, std::ostream& ost = std::cout, |
149 | 149 |
bool pros = false) : |
150 | 150 |
g(gr), os(ost), |
151 | 151 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
152 | 152 |
_nodeColors(WHITE), _arcColors(BLACK), |
153 | 153 |
_arcWidths(1.0), _arcWidthScale(0.003), |
154 | 154 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
155 | 155 |
_nodeBorderQuotient(.1), |
156 | 156 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
157 | 157 |
_showNodes(true), _showArcs(true), |
158 | 158 |
_enableParallel(false), _parArcDist(1), |
159 | 159 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
160 | 160 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
161 | 161 |
_undirected(lemon::UndirectedTagIndicator<GR>::value), |
162 | 162 |
_pleaseRemoveOsStream(pros), _scaleToA4(false), |
163 | 163 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
164 | 164 |
_autoNodeScale(false), |
165 | 165 |
_autoArcWidthScale(false), |
166 | 166 |
_absoluteNodeSizes(false), |
167 | 167 |
_absoluteArcWidths(false), |
168 | 168 |
_negY(false), |
169 | 169 |
_preScale(true) |
170 | 170 |
{} |
171 | 171 |
}; |
172 | 172 |
|
173 | 173 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
174 | 174 |
|
175 | 175 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
176 | 176 |
/// |
177 | 177 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
178 | 178 |
template<class T> class GraphToEps : public T |
179 | 179 |
{ |
180 | 180 |
// Can't believe it is required by the C++ standard |
181 | 181 |
using T::g; |
182 | 182 |
using T::os; |
183 | 183 |
|
184 | 184 |
using T::_coords; |
185 | 185 |
using T::_nodeSizes; |
186 | 186 |
using T::_nodeShapes; |
187 | 187 |
using T::_nodeColors; |
188 | 188 |
using T::_arcColors; |
189 | 189 |
using T::_arcWidths; |
190 | 190 |
|
191 | 191 |
using T::_arcWidthScale; |
192 | 192 |
using T::_nodeScale; |
193 | 193 |
using T::_xBorder; |
194 | 194 |
using T::_yBorder; |
195 | 195 |
using T::_scale; |
196 | 196 |
using T::_nodeBorderQuotient; |
197 | 197 |
|
198 | 198 |
using T::_drawArrows; |
199 | 199 |
using T::_arrowLength; |
200 | 200 |
using T::_arrowWidth; |
201 | 201 |
|
202 | 202 |
using T::_showNodes; |
203 | 203 |
using T::_showArcs; |
204 | 204 |
|
205 | 205 |
using T::_enableParallel; |
206 | 206 |
using T::_parArcDist; |
207 | 207 |
|
208 | 208 |
using T::_showNodeText; |
209 | 209 |
using T::_nodeTexts; |
210 | 210 |
using T::_nodeTextSize; |
211 | 211 |
|
212 | 212 |
using T::_showNodePsText; |
213 | 213 |
using T::_nodePsTexts; |
214 | 214 |
using T::_nodePsTextsPreamble; |
215 | 215 |
|
216 | 216 |
using T::_undirected; |
217 | 217 |
|
218 | 218 |
using T::_pleaseRemoveOsStream; |
219 | 219 |
|
220 | 220 |
using T::_scaleToA4; |
221 | 221 |
|
222 | 222 |
using T::_title; |
223 | 223 |
using T::_copyright; |
224 | 224 |
|
225 | 225 |
using T::NodeTextColorType; |
226 | 226 |
using T::CUST_COL; |
227 | 227 |
using T::DIST_COL; |
228 | 228 |
using T::DIST_BW; |
229 | 229 |
using T::_nodeTextColorType; |
230 | 230 |
using T::_nodeTextColors; |
231 | 231 |
|
232 | 232 |
using T::_autoNodeScale; |
233 | 233 |
using T::_autoArcWidthScale; |
234 | 234 |
|
235 | 235 |
using T::_absoluteNodeSizes; |
236 | 236 |
using T::_absoluteArcWidths; |
237 | 237 |
|
238 | 238 |
|
239 | 239 |
using T::_negY; |
240 | 240 |
using T::_preScale; |
241 | 241 |
|
242 | 242 |
// dradnats ++C eht yb deriuqer si ti eveileb t'naC |
243 | 243 |
|
244 | 244 |
typedef typename T::Graph Graph; |
245 | 245 |
typedef typename T::Digraph Digraph; |
246 | 246 |
typedef typename Graph::Node Node; |
247 | 247 |
typedef typename Graph::NodeIt NodeIt; |
248 | 248 |
typedef typename Graph::Arc Arc; |
249 | 249 |
typedef typename Graph::ArcIt ArcIt; |
250 | 250 |
typedef typename Graph::InArcIt InArcIt; |
251 | 251 |
typedef typename Graph::OutArcIt OutArcIt; |
252 | 252 |
|
253 | 253 |
static const int INTERPOL_PREC; |
254 | 254 |
static const double A4HEIGHT; |
255 | 255 |
static const double A4WIDTH; |
256 | 256 |
static const double A4BORDER; |
257 | 257 |
|
258 | 258 |
bool dontPrint; |
259 | 259 |
|
260 | 260 |
public: |
261 | 261 |
///Node shapes |
262 | 262 |
|
263 | 263 |
///Node shapes. |
264 | 264 |
/// |
265 | 265 |
enum NodeShapes { |
266 | 266 |
/// = 0 |
267 | 267 |
///\image html nodeshape_0.png |
268 | 268 |
///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
269 | 269 |
CIRCLE=0, |
270 | 270 |
/// = 1 |
271 | 271 |
///\image html nodeshape_1.png |
272 | 272 |
///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
273 | 273 |
SQUARE=1, |
274 | 274 |
/// = 2 |
275 | 275 |
///\image html nodeshape_2.png |
276 | 276 |
///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
277 | 277 |
DIAMOND=2, |
278 | 278 |
/// = 3 |
279 | 279 |
///\image html nodeshape_3.png |
280 | 280 |
///\image latex nodeshape_3.eps "MALE shape (3)" width=2cm |
281 | 281 |
MALE=3, |
282 | 282 |
/// = 4 |
283 | 283 |
///\image html nodeshape_4.png |
284 | 284 |
///\image latex nodeshape_4.eps "FEMALE shape (4)" width=2cm |
285 | 285 |
FEMALE=4 |
286 | 286 |
}; |
287 | 287 |
|
288 | 288 |
private: |
289 | 289 |
class arcLess { |
290 | 290 |
const Graph &g; |
291 | 291 |
public: |
292 | 292 |
arcLess(const Graph &_g) : g(_g) {} |
293 | 293 |
bool operator()(Arc a,Arc b) const |
294 | 294 |
{ |
295 | 295 |
Node ai=std::min(g.source(a),g.target(a)); |
296 | 296 |
Node aa=std::max(g.source(a),g.target(a)); |
297 | 297 |
Node bi=std::min(g.source(b),g.target(b)); |
298 | 298 |
Node ba=std::max(g.source(b),g.target(b)); |
299 | 299 |
return ai<bi || |
300 | 300 |
(ai==bi && (aa < ba || |
301 | 301 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
302 | 302 |
} |
303 | 303 |
}; |
304 | 304 |
bool isParallel(Arc e,Arc f) const |
305 | 305 |
{ |
306 | 306 |
return (g.source(e)==g.source(f)&& |
307 | 307 |
g.target(e)==g.target(f)) || |
308 | 308 |
(g.source(e)==g.target(f)&& |
309 | 309 |
g.target(e)==g.source(f)); |
310 | 310 |
} |
311 | 311 |
template<class TT> |
312 | 312 |
static std::string psOut(const dim2::Point<TT> &p) |
313 | 313 |
{ |
314 | 314 |
std::ostringstream os; |
315 | 315 |
os << p.x << ' ' << p.y; |
316 | 316 |
return os.str(); |
317 | 317 |
} |
318 | 318 |
static std::string psOut(const Color &c) |
319 | 319 |
{ |
320 | 320 |
std::ostringstream os; |
321 | 321 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
322 | 322 |
return os.str(); |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
public: |
326 | 326 |
GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
327 | 327 |
|
328 | 328 |
template<class X> struct CoordsTraits : public T { |
329 | 329 |
typedef X CoordsMapType; |
330 | 330 |
const X &_coords; |
331 | 331 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
332 | 332 |
}; |
333 | 333 |
///Sets the map of the node coordinates |
334 | 334 |
|
335 | 335 |
///Sets the map of the node coordinates. |
336 | 336 |
///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or |
337 | 337 |
///\ref dim2::Point "dim2::Point<int>" values. |
338 | 338 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
339 | 339 |
dontPrint=true; |
340 | 340 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
341 | 341 |
} |
342 | 342 |
template<class X> struct NodeSizesTraits : public T { |
343 | 343 |
const X &_nodeSizes; |
344 | 344 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
345 | 345 |
}; |
346 | 346 |
///Sets the map of the node sizes |
347 | 347 |
|
348 | 348 |
///Sets the map of the node sizes. |
349 | 349 |
///\param x must be a node map with \c double (or convertible) values. |
350 | 350 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
351 | 351 |
{ |
352 | 352 |
dontPrint=true; |
353 | 353 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
354 | 354 |
} |
355 | 355 |
template<class X> struct NodeShapesTraits : public T { |
356 | 356 |
const X &_nodeShapes; |
357 | 357 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
358 | 358 |
}; |
359 | 359 |
///Sets the map of the node shapes |
360 | 360 |
|
361 | 361 |
///Sets the map of the node shapes. |
362 | 362 |
///The available shape values |
363 | 363 |
///can be found in \ref NodeShapes "enum NodeShapes". |
364 | 364 |
///\param x must be a node map with \c int (or convertible) values. |
365 | 365 |
///\sa NodeShapes |
366 | 366 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
367 | 367 |
{ |
368 | 368 |
dontPrint=true; |
369 | 369 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
370 | 370 |
} |
371 | 371 |
template<class X> struct NodeTextsTraits : public T { |
372 | 372 |
const X &_nodeTexts; |
373 | 373 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
374 | 374 |
}; |
375 | 375 |
///Sets the text printed on the nodes |
376 | 376 |
|
377 | 377 |
///Sets the text printed on the nodes. |
378 | 378 |
///\param x must be a node map with type that can be pushed to a standard |
379 | 379 |
///\c ostream. |
380 | 380 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
381 | 381 |
{ |
382 | 382 |
dontPrint=true; |
383 | 383 |
_showNodeText=true; |
384 | 384 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
385 | 385 |
} |
386 | 386 |
template<class X> struct NodePsTextsTraits : public T { |
387 | 387 |
const X &_nodePsTexts; |
388 | 388 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
389 | 389 |
}; |
390 | 390 |
///Inserts a PostScript block to the nodes |
391 | 391 |
|
392 | 392 |
///With this command it is possible to insert a verbatim PostScript |
393 | 393 |
///block to the nodes. |
394 | 394 |
///The PS current point will be moved to the center of the node before |
395 | 395 |
///the PostScript block inserted. |
396 | 396 |
/// |
397 | 397 |
///Before and after the block a newline character is inserted so you |
398 | 398 |
///don't have to bother with the separators. |
399 | 399 |
/// |
400 | 400 |
///\param x must be a node map with type that can be pushed to a standard |
401 | 401 |
///\c ostream. |
402 | 402 |
/// |
403 | 403 |
///\sa nodePsTextsPreamble() |
404 | 404 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
405 | 405 |
{ |
406 | 406 |
dontPrint=true; |
407 | 407 |
_showNodePsText=true; |
408 | 408 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
409 | 409 |
} |
410 | 410 |
template<class X> struct ArcWidthsTraits : public T { |
411 | 411 |
const X &_arcWidths; |
412 | 412 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
413 | 413 |
}; |
414 | 414 |
///Sets the map of the arc widths |
415 | 415 |
|
416 | 416 |
///Sets the map of the arc widths. |
417 | 417 |
///\param x must be an arc map with \c double (or convertible) values. |
418 | 418 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
419 | 419 |
{ |
420 | 420 |
dontPrint=true; |
421 | 421 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
422 | 422 |
} |
423 | 423 |
|
424 | 424 |
template<class X> struct NodeColorsTraits : public T { |
425 | 425 |
const X &_nodeColors; |
426 | 426 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
427 | 427 |
}; |
428 | 428 |
///Sets the map of the node colors |
429 | 429 |
|
430 | 430 |
///Sets the map of the node colors. |
431 | 431 |
///\param x must be a node map with \ref Color values. |
432 | 432 |
/// |
433 | 433 |
///\sa Palette |
434 | 434 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
435 | 435 |
nodeColors(const X &x) |
436 | 436 |
{ |
437 | 437 |
dontPrint=true; |
438 | 438 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
439 | 439 |
} |
440 | 440 |
template<class X> struct NodeTextColorsTraits : public T { |
441 | 441 |
const X &_nodeTextColors; |
442 | 442 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
443 | 443 |
}; |
444 | 444 |
///Sets the map of the node text colors |
445 | 445 |
|
446 | 446 |
///Sets the map of the node text colors. |
447 | 447 |
///\param x must be a node map with \ref Color values. |
448 | 448 |
/// |
449 | 449 |
///\sa Palette |
450 | 450 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
451 | 451 |
nodeTextColors(const X &x) |
452 | 452 |
{ |
453 | 453 |
dontPrint=true; |
454 | 454 |
_nodeTextColorType=CUST_COL; |
455 | 455 |
return GraphToEps<NodeTextColorsTraits<X> > |
456 | 456 |
(NodeTextColorsTraits<X>(*this,x)); |
457 | 457 |
} |
458 | 458 |
template<class X> struct ArcColorsTraits : public T { |
459 | 459 |
const X &_arcColors; |
460 | 460 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
461 | 461 |
}; |
462 | 462 |
///Sets the map of the arc colors |
463 | 463 |
|
464 | 464 |
///Sets the map of the arc colors. |
465 | 465 |
///\param x must be an arc map with \ref Color values. |
466 | 466 |
/// |
467 | 467 |
///\sa Palette |
468 | 468 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
469 | 469 |
arcColors(const X &x) |
470 | 470 |
{ |
471 | 471 |
dontPrint=true; |
472 | 472 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
473 | 473 |
} |
474 | 474 |
///Sets a global scale factor for node sizes |
475 | 475 |
|
476 | 476 |
///Sets a global scale factor for node sizes. |
477 | 477 |
/// |
478 | 478 |
/// If nodeSizes() is not given, this function simply sets the node |
479 | 479 |
/// sizes to \c d. If nodeSizes() is given, but |
480 | 480 |
/// autoNodeScale() is not, then the node size given by |
481 | 481 |
/// nodeSizes() will be multiplied by the value \c d. |
482 | 482 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
483 | 483 |
/// node sizes will be scaled in such a way that the greatest size will be |
484 | 484 |
/// equal to \c d. |
485 | 485 |
/// \sa nodeSizes() |
486 | 486 |
/// \sa autoNodeScale() |
487 | 487 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
488 | 488 |
///Turns on/off the automatic node size scaling. |
489 | 489 |
|
490 | 490 |
///Turns on/off the automatic node size scaling. |
491 | 491 |
/// |
492 | 492 |
///\sa nodeScale() |
493 | 493 |
/// |
494 | 494 |
GraphToEps<T> &autoNodeScale(bool b=true) { |
495 | 495 |
_autoNodeScale=b;return *this; |
496 | 496 |
} |
497 | 497 |
|
498 | 498 |
///Turns on/off the absolutematic node size scaling. |
499 | 499 |
|
500 | 500 |
///Turns on/off the absolutematic node size scaling. |
501 | 501 |
/// |
502 | 502 |
///\sa nodeScale() |
503 | 503 |
/// |
504 | 504 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
505 | 505 |
_absoluteNodeSizes=b;return *this; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
///Negates the Y coordinates. |
509 | 509 |
GraphToEps<T> &negateY(bool b=true) { |
510 | 510 |
_negY=b;return *this; |
511 | 511 |
} |
512 | 512 |
|
513 | 513 |
///Turn on/off pre-scaling |
514 | 514 |
|
515 |
///By default graphToEps() rescales the whole image in order to avoid |
|
515 |
///By default, graphToEps() rescales the whole image in order to avoid |
|
516 | 516 |
///very big or very small bounding boxes. |
517 | 517 |
/// |
518 | 518 |
///This (p)rescaling can be turned off with this function. |
519 | 519 |
/// |
520 | 520 |
GraphToEps<T> &preScale(bool b=true) { |
521 | 521 |
_preScale=b;return *this; |
522 | 522 |
} |
523 | 523 |
|
524 | 524 |
///Sets a global scale factor for arc widths |
525 | 525 |
|
526 | 526 |
/// Sets a global scale factor for arc widths. |
527 | 527 |
/// |
528 | 528 |
/// If arcWidths() is not given, this function simply sets the arc |
529 | 529 |
/// widths to \c d. If arcWidths() is given, but |
530 | 530 |
/// autoArcWidthScale() is not, then the arc withs given by |
531 | 531 |
/// arcWidths() will be multiplied by the value \c d. |
532 | 532 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
533 | 533 |
/// arc withs will be scaled in such a way that the greatest width will be |
534 | 534 |
/// equal to \c d. |
535 | 535 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
536 | 536 |
///Turns on/off the automatic arc width scaling. |
537 | 537 |
|
538 | 538 |
///Turns on/off the automatic arc width scaling. |
539 | 539 |
/// |
540 | 540 |
///\sa arcWidthScale() |
541 | 541 |
/// |
542 | 542 |
GraphToEps<T> &autoArcWidthScale(bool b=true) { |
543 | 543 |
_autoArcWidthScale=b;return *this; |
544 | 544 |
} |
545 | 545 |
///Turns on/off the absolutematic arc width scaling. |
546 | 546 |
|
547 | 547 |
///Turns on/off the absolutematic arc width scaling. |
548 | 548 |
/// |
549 | 549 |
///\sa arcWidthScale() |
550 | 550 |
/// |
551 | 551 |
GraphToEps<T> &absoluteArcWidths(bool b=true) { |
552 | 552 |
_absoluteArcWidths=b;return *this; |
553 | 553 |
} |
554 | 554 |
///Sets a global scale factor for the whole picture |
555 | 555 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
556 | 556 |
///Sets the width of the border around the picture |
557 | 557 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;} |
558 | 558 |
///Sets the width of the border around the picture |
559 | 559 |
GraphToEps<T> &border(double x, double y) { |
560 | 560 |
_xBorder=x;_yBorder=y;return *this; |
561 | 561 |
} |
562 | 562 |
///Sets whether to draw arrows |
563 | 563 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
564 | 564 |
///Sets the length of the arrowheads |
565 | 565 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} |
566 | 566 |
///Sets the width of the arrowheads |
567 | 567 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} |
568 | 568 |
|
569 | 569 |
///Scales the drawing to fit to A4 page |
570 | 570 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
571 | 571 |
|
572 | 572 |
///Enables parallel arcs |
573 | 573 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
574 | 574 |
|
575 | 575 |
///Sets the distance between parallel arcs |
576 | 576 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
577 | 577 |
|
578 | 578 |
///Hides the arcs |
579 | 579 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
580 | 580 |
///Hides the nodes |
581 | 581 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
582 | 582 |
|
583 | 583 |
///Sets the size of the node texts |
584 | 584 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
585 | 585 |
|
586 | 586 |
///Sets the color of the node texts to be different from the node color |
587 | 587 |
|
588 | 588 |
///Sets the color of the node texts to be as different from the node color |
589 | 589 |
///as it is possible. |
590 | 590 |
GraphToEps<T> &distantColorNodeTexts() |
591 | 591 |
{_nodeTextColorType=DIST_COL;return *this;} |
592 | 592 |
///Sets the color of the node texts to be black or white and always visible. |
593 | 593 |
|
594 | 594 |
///Sets the color of the node texts to be black or white according to |
595 | 595 |
///which is more different from the node color. |
596 | 596 |
GraphToEps<T> &distantBWNodeTexts() |
597 | 597 |
{_nodeTextColorType=DIST_BW;return *this;} |
598 | 598 |
|
599 | 599 |
///Gives a preamble block for node Postscript block. |
600 | 600 |
|
601 | 601 |
///Gives a preamble block for node Postscript block. |
602 | 602 |
/// |
603 | 603 |
///\sa nodePsTexts() |
604 | 604 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
605 | 605 |
_nodePsTextsPreamble=str ;return *this; |
606 | 606 |
} |
607 | 607 |
///Sets whether the graph is undirected |
608 | 608 |
|
609 | 609 |
///Sets whether the graph is undirected. |
610 | 610 |
/// |
611 | 611 |
///This setting is the default for undirected graphs. |
612 | 612 |
/// |
613 | 613 |
///\sa directed() |
614 | 614 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
615 | 615 |
|
616 | 616 |
///Sets whether the graph is directed |
617 | 617 |
|
618 | 618 |
///Sets whether the graph is directed. |
619 | 619 |
///Use it to show the edges as a pair of directed ones. |
620 | 620 |
/// |
621 | 621 |
///This setting is the default for digraphs. |
622 | 622 |
/// |
623 | 623 |
///\sa undirected() |
624 | 624 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
625 | 625 |
|
626 | 626 |
///Sets the title. |
627 | 627 |
|
628 | 628 |
///Sets the title of the generated image, |
629 | 629 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
630 | 630 |
///the EPS file. |
631 | 631 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
632 | 632 |
///Sets the copyright statement. |
633 | 633 |
|
634 | 634 |
///Sets the copyright statement of the generated image, |
635 | 635 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
636 | 636 |
///the EPS file. |
637 | 637 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
638 | 638 |
|
639 | 639 |
protected: |
640 | 640 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
641 | 641 |
{ |
642 | 642 |
switch(t) { |
643 | 643 |
case CIRCLE: |
644 | 644 |
case MALE: |
645 | 645 |
case FEMALE: |
646 | 646 |
return p.normSquare()<=r*r; |
647 | 647 |
case SQUARE: |
648 | 648 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
649 | 649 |
case DIAMOND: |
650 | 650 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
651 | 651 |
} |
652 | 652 |
return false; |
653 | 653 |
} |
654 | 654 |
|
655 | 655 |
public: |
656 | 656 |
~GraphToEps() { } |
657 | 657 |
|
658 | 658 |
///Draws the graph. |
659 | 659 |
|
660 | 660 |
///Like other functions using |
661 | 661 |
///\ref named-templ-func-param "named template parameters", |
662 | 662 |
///this function calls the algorithm itself, i.e. in this case |
663 | 663 |
///it draws the graph. |
664 | 664 |
void run() { |
665 | 665 |
const double EPSILON=1e-9; |
666 | 666 |
if(dontPrint) return; |
667 | 667 |
|
668 | 668 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
669 | 669 |
mycoords(_coords,_negY); |
670 | 670 |
|
671 | 671 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
672 | 672 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
673 | 673 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
674 | 674 |
os << "%%Creator: LEMON, graphToEps()\n"; |
675 | 675 |
|
676 | 676 |
{ |
677 | 677 |
os << "%%CreationDate: "; |
678 | 678 |
#ifndef WIN32 |
679 | 679 |
timeval tv; |
680 | 680 |
gettimeofday(&tv, 0); |
681 | 681 |
|
682 | 682 |
char cbuf[26]; |
683 | 683 |
ctime_r(&tv.tv_sec,cbuf); |
684 | 684 |
os << cbuf; |
685 | 685 |
#else |
686 | 686 |
os << bits::getWinFormattedDate(); |
687 | 687 |
#endif |
688 | 688 |
} |
689 | 689 |
os << std::endl; |
690 | 690 |
|
691 | 691 |
if (_autoArcWidthScale) { |
692 | 692 |
double max_w=0; |
693 | 693 |
for(ArcIt e(g);e!=INVALID;++e) |
694 | 694 |
max_w=std::max(double(_arcWidths[e]),max_w); |
695 | 695 |
if(max_w>EPSILON) { |
696 | 696 |
_arcWidthScale/=max_w; |
697 | 697 |
} |
698 | 698 |
} |
699 | 699 |
|
700 | 700 |
if (_autoNodeScale) { |
701 | 701 |
double max_s=0; |
702 | 702 |
for(NodeIt n(g);n!=INVALID;++n) |
703 | 703 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
704 | 704 |
if(max_s>EPSILON) { |
705 | 705 |
_nodeScale/=max_s; |
706 | 706 |
} |
707 | 707 |
} |
... | ... |
@@ -925,263 +925,263 @@ |
925 | 925 |
bez=bez.before((t1+t2)/2); |
926 | 926 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
927 | 927 |
// node_shape=_nodeShapes[g.source(*e)]; |
928 | 928 |
// t1=0;t2=1; |
929 | 929 |
// for(int i=0;i<INTERPOL_PREC;++i) |
930 | 930 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
931 | 931 |
// t1=(t1+t2)/2; |
932 | 932 |
// else t2=(t1+t2)/2; |
933 | 933 |
// bez=bez.after((t1+t2)/2); |
934 | 934 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
935 | 935 |
<< _arcColors[*e].red() << ' ' |
936 | 936 |
<< _arcColors[*e].green() << ' ' |
937 | 937 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
938 | 938 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
939 | 939 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
940 | 940 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
941 | 941 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
942 | 942 |
dim2::Point<double> dd(rot90(linend-apoint)); |
943 | 943 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
944 | 944 |
std::sqrt(dd.normSquare()); |
945 | 945 |
os << "newpath " << psOut(apoint) << " moveto " |
946 | 946 |
<< psOut(linend+dd) << " lineto " |
947 | 947 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
948 | 948 |
} |
949 | 949 |
else { |
950 | 950 |
os << mycoords[g.source(*e)].x << ' ' |
951 | 951 |
<< mycoords[g.source(*e)].y << ' ' |
952 | 952 |
<< mm.x << ' ' << mm.y << ' ' |
953 | 953 |
<< mycoords[g.target(*e)].x << ' ' |
954 | 954 |
<< mycoords[g.target(*e)].y << ' ' |
955 | 955 |
<< _arcColors[*e].red() << ' ' |
956 | 956 |
<< _arcColors[*e].green() << ' ' |
957 | 957 |
<< _arcColors[*e].blue() << ' ' |
958 | 958 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
959 | 959 |
} |
960 | 960 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
961 | 961 |
} |
962 | 962 |
} |
963 | 963 |
} |
964 | 964 |
else for(ArcIt e(g);e!=INVALID;++e) |
965 | 965 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
966 | 966 |
&&g.source(e)!=g.target(e)) { |
967 | 967 |
if(_drawArrows) { |
968 | 968 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
969 | 969 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
970 | 970 |
int node_shape=_nodeShapes[g.target(e)]; |
971 | 971 |
double t1=0,t2=1; |
972 | 972 |
for(int i=0;i<INTERPOL_PREC;++i) |
973 | 973 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
974 | 974 |
else t2=(t1+t2)/2; |
975 | 975 |
double l=std::sqrt(d.normSquare()); |
976 | 976 |
d/=l; |
977 | 977 |
|
978 | 978 |
os << l*(1-(t1+t2)/2) << ' ' |
979 | 979 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
980 | 980 |
<< d.x << ' ' << d.y << ' ' |
981 | 981 |
<< mycoords[g.source(e)].x << ' ' |
982 | 982 |
<< mycoords[g.source(e)].y << ' ' |
983 | 983 |
<< _arcColors[e].red() << ' ' |
984 | 984 |
<< _arcColors[e].green() << ' ' |
985 | 985 |
<< _arcColors[e].blue() << " arr\n"; |
986 | 986 |
} |
987 | 987 |
else os << mycoords[g.source(e)].x << ' ' |
988 | 988 |
<< mycoords[g.source(e)].y << ' ' |
989 | 989 |
<< mycoords[g.target(e)].x << ' ' |
990 | 990 |
<< mycoords[g.target(e)].y << ' ' |
991 | 991 |
<< _arcColors[e].red() << ' ' |
992 | 992 |
<< _arcColors[e].green() << ' ' |
993 | 993 |
<< _arcColors[e].blue() << ' ' |
994 | 994 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
995 | 995 |
} |
996 | 996 |
os << "grestore\n"; |
997 | 997 |
} |
998 | 998 |
if(_showNodes) { |
999 | 999 |
os << "%Nodes:\ngsave\n"; |
1000 | 1000 |
for(NodeIt n(g);n!=INVALID;++n) { |
1001 | 1001 |
os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
1002 | 1002 |
<< _nodeSizes[n]*_nodeScale << ' ' |
1003 | 1003 |
<< _nodeColors[n].red() << ' ' |
1004 | 1004 |
<< _nodeColors[n].green() << ' ' |
1005 | 1005 |
<< _nodeColors[n].blue() << ' '; |
1006 | 1006 |
switch(_nodeShapes[n]) { |
1007 | 1007 |
case CIRCLE: |
1008 | 1008 |
os<< "nc";break; |
1009 | 1009 |
case SQUARE: |
1010 | 1010 |
os<< "nsq";break; |
1011 | 1011 |
case DIAMOND: |
1012 | 1012 |
os<< "ndi";break; |
1013 | 1013 |
case MALE: |
1014 | 1014 |
os<< "nmale";break; |
1015 | 1015 |
case FEMALE: |
1016 | 1016 |
os<< "nfemale";break; |
1017 | 1017 |
} |
1018 | 1018 |
os<<'\n'; |
1019 | 1019 |
} |
1020 | 1020 |
os << "grestore\n"; |
1021 | 1021 |
} |
1022 | 1022 |
if(_showNodeText) { |
1023 | 1023 |
os << "%Node texts:\ngsave\n"; |
1024 | 1024 |
os << "/fosi " << _nodeTextSize << " def\n"; |
1025 | 1025 |
os << "(Helvetica) findfont fosi scalefont setfont\n"; |
1026 | 1026 |
for(NodeIt n(g);n!=INVALID;++n) { |
1027 | 1027 |
switch(_nodeTextColorType) { |
1028 | 1028 |
case DIST_COL: |
1029 | 1029 |
os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; |
1030 | 1030 |
break; |
1031 | 1031 |
case DIST_BW: |
1032 | 1032 |
os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; |
1033 | 1033 |
break; |
1034 | 1034 |
case CUST_COL: |
1035 | 1035 |
os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; |
1036 | 1036 |
break; |
1037 | 1037 |
default: |
1038 | 1038 |
os << "0 0 0 setrgbcolor\n"; |
1039 | 1039 |
} |
1040 | 1040 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1041 | 1041 |
<< " (" << _nodeTexts[n] << ") cshow\n"; |
1042 | 1042 |
} |
1043 | 1043 |
os << "grestore\n"; |
1044 | 1044 |
} |
1045 | 1045 |
if(_showNodePsText) { |
1046 | 1046 |
os << "%Node PS blocks:\ngsave\n"; |
1047 | 1047 |
for(NodeIt n(g);n!=INVALID;++n) |
1048 | 1048 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1049 | 1049 |
<< " moveto\n" << _nodePsTexts[n] << "\n"; |
1050 | 1050 |
os << "grestore\n"; |
1051 | 1051 |
} |
1052 | 1052 |
|
1053 | 1053 |
os << "grestore\nshowpage\n"; |
1054 | 1054 |
|
1055 | 1055 |
//CleanUp: |
1056 | 1056 |
if(_pleaseRemoveOsStream) {delete &os;} |
1057 | 1057 |
} |
1058 | 1058 |
|
1059 | 1059 |
///\name Aliases |
1060 | 1060 |
///These are just some aliases to other parameter setting functions. |
1061 | 1061 |
|
1062 | 1062 |
///@{ |
1063 | 1063 |
|
1064 | 1064 |
///An alias for arcWidths() |
1065 | 1065 |
template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x) |
1066 | 1066 |
{ |
1067 | 1067 |
return arcWidths(x); |
1068 | 1068 |
} |
1069 | 1069 |
|
1070 | 1070 |
///An alias for arcColors() |
1071 | 1071 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
1072 | 1072 |
edgeColors(const X &x) |
1073 | 1073 |
{ |
1074 | 1074 |
return arcColors(x); |
1075 | 1075 |
} |
1076 | 1076 |
|
1077 | 1077 |
///An alias for arcWidthScale() |
1078 | 1078 |
GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);} |
1079 | 1079 |
|
1080 | 1080 |
///An alias for autoArcWidthScale() |
1081 | 1081 |
GraphToEps<T> &autoEdgeWidthScale(bool b=true) |
1082 | 1082 |
{ |
1083 | 1083 |
return autoArcWidthScale(b); |
1084 | 1084 |
} |
1085 | 1085 |
|
1086 | 1086 |
///An alias for absoluteArcWidths() |
1087 | 1087 |
GraphToEps<T> &absoluteEdgeWidths(bool b=true) |
1088 | 1088 |
{ |
1089 | 1089 |
return absoluteArcWidths(b); |
1090 | 1090 |
} |
1091 | 1091 |
|
1092 | 1092 |
///An alias for parArcDist() |
1093 | 1093 |
GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);} |
1094 | 1094 |
|
1095 | 1095 |
///An alias for hideArcs() |
1096 | 1096 |
GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);} |
1097 | 1097 |
|
1098 | 1098 |
///@} |
1099 | 1099 |
}; |
1100 | 1100 |
|
1101 | 1101 |
template<class T> |
1102 | 1102 |
const int GraphToEps<T>::INTERPOL_PREC = 20; |
1103 | 1103 |
template<class T> |
1104 | 1104 |
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276; |
1105 | 1105 |
template<class T> |
1106 | 1106 |
const double GraphToEps<T>::A4WIDTH = 595.275590551181; |
1107 | 1107 |
template<class T> |
1108 | 1108 |
const double GraphToEps<T>::A4BORDER = 15; |
1109 | 1109 |
|
1110 | 1110 |
|
1111 | 1111 |
///Generates an EPS file from a graph |
1112 | 1112 |
|
1113 | 1113 |
///\ingroup eps_io |
1114 | 1114 |
///Generates an EPS file from a graph. |
1115 | 1115 |
///\param g Reference to the graph to be printed. |
1116 | 1116 |
///\param os Reference to the output stream. |
1117 |
///By default it is <tt>std::cout</tt>. |
|
1117 |
///By default, it is <tt>std::cout</tt>. |
|
1118 | 1118 |
/// |
1119 | 1119 |
///This function also has a lot of |
1120 | 1120 |
///\ref named-templ-func-param "named parameters", |
1121 | 1121 |
///they are declared as the members of class \ref GraphToEps. The following |
1122 | 1122 |
///example shows how to use these parameters. |
1123 | 1123 |
///\code |
1124 | 1124 |
/// graphToEps(g,os).scale(10).coords(coords) |
1125 | 1125 |
/// .nodeScale(2).nodeSizes(sizes) |
1126 | 1126 |
/// .arcWidthScale(.4).run(); |
1127 | 1127 |
///\endcode |
1128 | 1128 |
/// |
1129 |
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file. |
|
1129 |
///For more detailed examples, see the \ref graph_to_eps_demo.cc demo file. |
|
1130 | 1130 |
/// |
1131 | 1131 |
///\warning Don't forget to put the \ref GraphToEps::run() "run()" |
1132 | 1132 |
///to the end of the parameter list. |
1133 | 1133 |
///\sa GraphToEps |
1134 | 1134 |
///\sa graphToEps(GR &g, const char *file_name) |
1135 | 1135 |
template<class GR> |
1136 | 1136 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
1137 | 1137 |
graphToEps(GR &g, std::ostream& os=std::cout) |
1138 | 1138 |
{ |
1139 | 1139 |
return |
1140 | 1140 |
GraphToEps<DefaultGraphToEpsTraits<GR> >(DefaultGraphToEpsTraits<GR>(g,os)); |
1141 | 1141 |
} |
1142 | 1142 |
|
1143 | 1143 |
///Generates an EPS file from a graph |
1144 | 1144 |
|
1145 | 1145 |
///\ingroup eps_io |
1146 | 1146 |
///This function does the same as |
1147 | 1147 |
///\ref graphToEps(GR &g,std::ostream& os) |
1148 | 1148 |
///but it writes its output into the file \c file_name |
1149 | 1149 |
///instead of a stream. |
1150 | 1150 |
///\sa graphToEps(GR &g, std::ostream& os) |
1151 | 1151 |
template<class GR> |
1152 | 1152 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
1153 | 1153 |
graphToEps(GR &g,const char *file_name) |
1154 | 1154 |
{ |
1155 | 1155 |
std::ostream* os = new std::ofstream(file_name); |
1156 | 1156 |
if (!(*os)) { |
1157 | 1157 |
delete os; |
1158 | 1158 |
throw IoError("Cannot write file", file_name); |
1159 | 1159 |
} |
1160 | 1160 |
return GraphToEps<DefaultGraphToEpsTraits<GR> > |
1161 | 1161 |
(DefaultGraphToEpsTraits<GR>(g,*os,true)); |
1162 | 1162 |
} |
1163 | 1163 |
|
1164 | 1164 |
///Generates an EPS file from a graph |
1165 | 1165 |
|
1166 | 1166 |
///\ingroup eps_io |
1167 | 1167 |
///This function does the same as |
1168 | 1168 |
///\ref graphToEps(GR &g,std::ostream& os) |
1169 | 1169 |
///but it writes its output into the file \c file_name |
1170 | 1170 |
///instead of a stream. |
1171 | 1171 |
///\sa graphToEps(GR &g, std::ostream& os) |
1172 | 1172 |
template<class GR> |
1173 | 1173 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
1174 | 1174 |
graphToEps(GR &g,const std::string& file_name) |
1175 | 1175 |
{ |
1176 | 1176 |
std::ostream* os = new std::ofstream(file_name.c_str()); |
1177 | 1177 |
if (!(*os)) { |
1178 | 1178 |
delete os; |
1179 | 1179 |
throw IoError("Cannot write file", file_name); |
1180 | 1180 |
} |
1181 | 1181 |
return GraphToEps<DefaultGraphToEpsTraits<GR> > |
1182 | 1182 |
(DefaultGraphToEpsTraits<GR>(g,*os,true)); |
1183 | 1183 |
} |
1184 | 1184 |
|
1185 | 1185 |
} //END OF NAMESPACE LEMON |
1186 | 1186 |
|
1187 | 1187 |
#endif // LEMON_GRAPH_TO_EPS_H |
... | ... |
@@ -98,360 +98,360 @@ |
98 | 98 |
typedef True FindEdgeTag; |
99 | 99 |
typedef True FindArcTag; |
100 | 100 |
|
101 | 101 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const { |
102 | 102 |
if (prev != INVALID) return INVALID; |
103 | 103 |
int d = u._id ^ v._id; |
104 | 104 |
int k = 0; |
105 | 105 |
if (d == 0) return INVALID; |
106 | 106 |
for ( ; (d & 1) == 0; d >>= 1) ++k; |
107 | 107 |
if (d >> 1 != 0) return INVALID; |
108 | 108 |
return (k << (_dim-1)) | ((u._id >> (k+1)) << k) | |
109 | 109 |
(u._id & ((1 << k) - 1)); |
110 | 110 |
} |
111 | 111 |
|
112 | 112 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const { |
113 | 113 |
Edge edge = findEdge(u, v, prev); |
114 | 114 |
if (edge == INVALID) return INVALID; |
115 | 115 |
int k = edge._id >> (_dim-1); |
116 | 116 |
return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1; |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
class Node { |
120 | 120 |
friend class HypercubeGraphBase; |
121 | 121 |
|
122 | 122 |
protected: |
123 | 123 |
int _id; |
124 | 124 |
Node(int id) : _id(id) {} |
125 | 125 |
public: |
126 | 126 |
Node() {} |
127 | 127 |
Node (Invalid) : _id(-1) {} |
128 | 128 |
bool operator==(const Node node) const {return _id == node._id;} |
129 | 129 |
bool operator!=(const Node node) const {return _id != node._id;} |
130 | 130 |
bool operator<(const Node node) const {return _id < node._id;} |
131 | 131 |
}; |
132 | 132 |
|
133 | 133 |
class Edge { |
134 | 134 |
friend class HypercubeGraphBase; |
135 | 135 |
friend class Arc; |
136 | 136 |
|
137 | 137 |
protected: |
138 | 138 |
int _id; |
139 | 139 |
|
140 | 140 |
Edge(int id) : _id(id) {} |
141 | 141 |
|
142 | 142 |
public: |
143 | 143 |
Edge() {} |
144 | 144 |
Edge (Invalid) : _id(-1) {} |
145 | 145 |
bool operator==(const Edge edge) const {return _id == edge._id;} |
146 | 146 |
bool operator!=(const Edge edge) const {return _id != edge._id;} |
147 | 147 |
bool operator<(const Edge edge) const {return _id < edge._id;} |
148 | 148 |
}; |
149 | 149 |
|
150 | 150 |
class Arc { |
151 | 151 |
friend class HypercubeGraphBase; |
152 | 152 |
|
153 | 153 |
protected: |
154 | 154 |
int _id; |
155 | 155 |
|
156 | 156 |
Arc(int id) : _id(id) {} |
157 | 157 |
|
158 | 158 |
public: |
159 | 159 |
Arc() {} |
160 | 160 |
Arc (Invalid) : _id(-1) {} |
161 | 161 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; } |
162 | 162 |
bool operator==(const Arc arc) const {return _id == arc._id;} |
163 | 163 |
bool operator!=(const Arc arc) const {return _id != arc._id;} |
164 | 164 |
bool operator<(const Arc arc) const {return _id < arc._id;} |
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
void first(Node& node) const { |
168 | 168 |
node._id = _node_num - 1; |
169 | 169 |
} |
170 | 170 |
|
171 | 171 |
static void next(Node& node) { |
172 | 172 |
--node._id; |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
void first(Edge& edge) const { |
176 | 176 |
edge._id = _edge_num - 1; |
177 | 177 |
} |
178 | 178 |
|
179 | 179 |
static void next(Edge& edge) { |
180 | 180 |
--edge._id; |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
void first(Arc& arc) const { |
184 | 184 |
arc._id = 2 * _edge_num - 1; |
185 | 185 |
} |
186 | 186 |
|
187 | 187 |
static void next(Arc& arc) { |
188 | 188 |
--arc._id; |
189 | 189 |
} |
190 | 190 |
|
191 | 191 |
void firstInc(Edge& edge, bool& dir, const Node& node) const { |
192 | 192 |
edge._id = node._id >> 1; |
193 | 193 |
dir = (node._id & 1) == 0; |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
void nextInc(Edge& edge, bool& dir) const { |
197 | 197 |
Node n = dir ? u(edge) : v(edge); |
198 | 198 |
int k = (edge._id >> (_dim-1)) + 1; |
199 | 199 |
if (k < _dim) { |
200 | 200 |
edge._id = (k << (_dim-1)) | |
201 | 201 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
202 | 202 |
dir = ((n._id >> k) & 1) == 0; |
203 | 203 |
} else { |
204 | 204 |
edge._id = -1; |
205 | 205 |
dir = true; |
206 | 206 |
} |
207 | 207 |
} |
208 | 208 |
|
209 | 209 |
void firstOut(Arc& arc, const Node& node) const { |
210 | 210 |
arc._id = ((node._id >> 1) << 1) | (~node._id & 1); |
211 | 211 |
} |
212 | 212 |
|
213 | 213 |
void nextOut(Arc& arc) const { |
214 | 214 |
Node n = (arc._id & 1) == 1 ? u(arc) : v(arc); |
215 | 215 |
int k = (arc._id >> _dim) + 1; |
216 | 216 |
if (k < _dim) { |
217 | 217 |
arc._id = (k << (_dim-1)) | |
218 | 218 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
219 | 219 |
arc._id = (arc._id << 1) | (~(n._id >> k) & 1); |
220 | 220 |
} else { |
221 | 221 |
arc._id = -1; |
222 | 222 |
} |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
void firstIn(Arc& arc, const Node& node) const { |
226 | 226 |
arc._id = ((node._id >> 1) << 1) | (node._id & 1); |
227 | 227 |
} |
228 | 228 |
|
229 | 229 |
void nextIn(Arc& arc) const { |
230 | 230 |
Node n = (arc._id & 1) == 1 ? v(arc) : u(arc); |
231 | 231 |
int k = (arc._id >> _dim) + 1; |
232 | 232 |
if (k < _dim) { |
233 | 233 |
arc._id = (k << (_dim-1)) | |
234 | 234 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
235 | 235 |
arc._id = (arc._id << 1) | ((n._id >> k) & 1); |
236 | 236 |
} else { |
237 | 237 |
arc._id = -1; |
238 | 238 |
} |
239 | 239 |
} |
240 | 240 |
|
241 | 241 |
static bool direction(Arc arc) { |
242 | 242 |
return (arc._id & 1) == 1; |
243 | 243 |
} |
244 | 244 |
|
245 | 245 |
static Arc direct(Edge edge, bool dir) { |
246 | 246 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
int dimension() const { |
250 | 250 |
return _dim; |
251 | 251 |
} |
252 | 252 |
|
253 | 253 |
bool projection(Node node, int n) const { |
254 | 254 |
return static_cast<bool>(node._id & (1 << n)); |
255 | 255 |
} |
256 | 256 |
|
257 | 257 |
int dimension(Edge edge) const { |
258 | 258 |
return edge._id >> (_dim-1); |
259 | 259 |
} |
260 | 260 |
|
261 | 261 |
int dimension(Arc arc) const { |
262 | 262 |
return arc._id >> _dim; |
263 | 263 |
} |
264 | 264 |
|
265 | 265 |
int index(Node node) const { |
266 | 266 |
return node._id; |
267 | 267 |
} |
268 | 268 |
|
269 | 269 |
Node operator()(int ix) const { |
270 | 270 |
return Node(ix); |
271 | 271 |
} |
272 | 272 |
|
273 | 273 |
private: |
274 | 274 |
int _dim; |
275 | 275 |
int _node_num, _edge_num; |
276 | 276 |
}; |
277 | 277 |
|
278 | 278 |
|
279 | 279 |
typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase; |
280 | 280 |
|
281 | 281 |
/// \ingroup graphs |
282 | 282 |
/// |
283 | 283 |
/// \brief Hypercube graph class |
284 | 284 |
/// |
285 | 285 |
/// HypercubeGraph implements a special graph type. The nodes of the |
286 | 286 |
/// graph are indexed with integers having at most \c dim binary digits. |
287 | 287 |
/// Two nodes are connected in the graph if and only if their indices |
288 | 288 |
/// differ only on one position in the binary form. |
289 | 289 |
/// This class is completely static and it needs constant memory space. |
290 |
/// Thus you can neither add nor delete nodes or edges, however |
|
290 |
/// Thus you can neither add nor delete nodes or edges, however, |
|
291 | 291 |
/// the structure can be resized using resize(). |
292 | 292 |
/// |
293 | 293 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept". |
294 | 294 |
/// Most of its member functions and nested classes are documented |
295 | 295 |
/// only in the concept class. |
296 | 296 |
/// |
297 | 297 |
/// \note The type of the indices is chosen to \c int for efficiency |
298 | 298 |
/// reasons. Thus the maximum dimension of this implementation is 26 |
299 | 299 |
/// (assuming that the size of \c int is 32 bit). |
300 | 300 |
class HypercubeGraph : public ExtendedHypercubeGraphBase { |
301 | 301 |
typedef ExtendedHypercubeGraphBase Parent; |
302 | 302 |
|
303 | 303 |
public: |
304 | 304 |
|
305 | 305 |
/// \brief Constructs a hypercube graph with \c dim dimensions. |
306 | 306 |
/// |
307 | 307 |
/// Constructs a hypercube graph with \c dim dimensions. |
308 | 308 |
HypercubeGraph(int dim) { construct(dim); } |
309 | 309 |
|
310 | 310 |
/// \brief Resizes the graph |
311 | 311 |
/// |
312 | 312 |
/// This function resizes the graph. It fully destroys and |
313 | 313 |
/// rebuilds the structure, therefore the maps of the graph will be |
314 | 314 |
/// reallocated automatically and the previous values will be lost. |
315 | 315 |
void resize(int dim) { |
316 | 316 |
Parent::notifier(Arc()).clear(); |
317 | 317 |
Parent::notifier(Edge()).clear(); |
318 | 318 |
Parent::notifier(Node()).clear(); |
319 | 319 |
construct(dim); |
320 | 320 |
Parent::notifier(Node()).build(); |
321 | 321 |
Parent::notifier(Edge()).build(); |
322 | 322 |
Parent::notifier(Arc()).build(); |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
/// \brief The number of dimensions. |
326 | 326 |
/// |
327 | 327 |
/// Gives back the number of dimensions. |
328 | 328 |
int dimension() const { |
329 | 329 |
return Parent::dimension(); |
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
/// \brief Returns \c true if the n'th bit of the node is one. |
333 | 333 |
/// |
334 | 334 |
/// Returns \c true if the n'th bit of the node is one. |
335 | 335 |
bool projection(Node node, int n) const { |
336 | 336 |
return Parent::projection(node, n); |
337 | 337 |
} |
338 | 338 |
|
339 | 339 |
/// \brief The dimension id of an edge. |
340 | 340 |
/// |
341 | 341 |
/// Gives back the dimension id of the given edge. |
342 | 342 |
/// It is in the range <tt>[0..dim-1]</tt>. |
343 | 343 |
int dimension(Edge edge) const { |
344 | 344 |
return Parent::dimension(edge); |
345 | 345 |
} |
346 | 346 |
|
347 | 347 |
/// \brief The dimension id of an arc. |
348 | 348 |
/// |
349 | 349 |
/// Gives back the dimension id of the given arc. |
350 | 350 |
/// It is in the range <tt>[0..dim-1]</tt>. |
351 | 351 |
int dimension(Arc arc) const { |
352 | 352 |
return Parent::dimension(arc); |
353 | 353 |
} |
354 | 354 |
|
355 | 355 |
/// \brief The index of a node. |
356 | 356 |
/// |
357 | 357 |
/// Gives back the index of the given node. |
358 | 358 |
/// The lower bits of the integer describes the node. |
359 | 359 |
int index(Node node) const { |
360 | 360 |
return Parent::index(node); |
361 | 361 |
} |
362 | 362 |
|
363 | 363 |
/// \brief Gives back a node by its index. |
364 | 364 |
/// |
365 | 365 |
/// Gives back a node by its index. |
366 | 366 |
Node operator()(int ix) const { |
367 | 367 |
return Parent::operator()(ix); |
368 | 368 |
} |
369 | 369 |
|
370 | 370 |
/// \brief Number of nodes. |
371 | 371 |
int nodeNum() const { return Parent::nodeNum(); } |
372 | 372 |
/// \brief Number of edges. |
373 | 373 |
int edgeNum() const { return Parent::edgeNum(); } |
374 | 374 |
/// \brief Number of arcs. |
375 | 375 |
int arcNum() const { return Parent::arcNum(); } |
376 | 376 |
|
377 | 377 |
/// \brief Linear combination map. |
378 | 378 |
/// |
379 | 379 |
/// This map makes possible to give back a linear combination |
380 | 380 |
/// for each node. It works like the \c std::accumulate function, |
381 | 381 |
/// so it accumulates the \c bf binary function with the \c fv first |
382 | 382 |
/// value. The map accumulates only on that positions (dimensions) |
383 | 383 |
/// where the index of the node is one. The values that have to be |
384 | 384 |
/// accumulated should be given by the \c begin and \c end iterators |
385 | 385 |
/// and the length of this range should be equal to the dimension |
386 | 386 |
/// number of the graph. |
387 | 387 |
/// |
388 | 388 |
///\code |
389 | 389 |
/// const int DIM = 3; |
390 | 390 |
/// HypercubeGraph graph(DIM); |
391 | 391 |
/// dim2::Point<double> base[DIM]; |
392 | 392 |
/// for (int k = 0; k < DIM; ++k) { |
393 | 393 |
/// base[k].x = rnd(); |
394 | 394 |
/// base[k].y = rnd(); |
395 | 395 |
/// } |
396 | 396 |
/// HypercubeGraph::HyperMap<dim2::Point<double> > |
397 | 397 |
/// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0)); |
398 | 398 |
///\endcode |
399 | 399 |
/// |
400 | 400 |
/// \see HypercubeGraph |
401 | 401 |
template <typename T, typename BF = std::plus<T> > |
402 | 402 |
class HyperMap { |
403 | 403 |
public: |
404 | 404 |
|
405 | 405 |
/// \brief The key type of the map |
406 | 406 |
typedef Node Key; |
407 | 407 |
/// \brief The value type of the map |
408 | 408 |
typedef T Value; |
409 | 409 |
|
410 | 410 |
/// \brief Constructor for HyperMap. |
411 | 411 |
/// |
412 | 412 |
/// Construct a HyperMap for the given graph. The values that have |
413 | 413 |
/// to be accumulated should be given by the \c begin and \c end |
414 | 414 |
/// iterators and the length of this range should be equal to the |
415 | 415 |
/// dimension number of the graph. |
416 | 416 |
/// |
417 | 417 |
/// This map accumulates the \c bf binary function with the \c fv |
418 | 418 |
/// first value on that positions (dimensions) where the index of |
419 | 419 |
/// the node is one. |
420 | 420 |
template <typename It> |
421 | 421 |
HyperMap(const Graph& graph, It begin, It end, |
422 | 422 |
T fv = 0, const BF& bf = BF()) |
423 | 423 |
: _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf) |
424 | 424 |
{ |
425 | 425 |
LEMON_ASSERT(_values.size() == graph.dimension(), |
426 | 426 |
"Wrong size of range"); |
427 | 427 |
} |
428 | 428 |
|
429 | 429 |
/// \brief The partial accumulated value. |
430 | 430 |
/// |
431 | 431 |
/// Gives back the partial accumulated value. |
432 | 432 |
Value operator[](const Key& k) const { |
433 | 433 |
Value val = _first_value; |
434 | 434 |
int id = _graph.index(k); |
435 | 435 |
int n = 0; |
436 | 436 |
while (id != 0) { |
437 | 437 |
if (id & 1) { |
438 | 438 |
val = _bin_func(val, _values[n]); |
439 | 439 |
} |
440 | 440 |
id >>= 1; |
441 | 441 |
++n; |
442 | 442 |
} |
443 | 443 |
return val; |
444 | 444 |
} |
445 | 445 |
|
446 | 446 |
private: |
447 | 447 |
const Graph& _graph; |
448 | 448 |
std::vector<T> _values; |
449 | 449 |
T _first_value; |
450 | 450 |
BF _bin_func; |
451 | 451 |
}; |
452 | 452 |
|
453 | 453 |
}; |
454 | 454 |
|
455 | 455 |
} |
456 | 456 |
|
457 | 457 |
#endif |
... | ... |
@@ -238,385 +238,385 @@ |
238 | 238 |
if (!is.get(c)) |
239 | 239 |
throw FormatError("Escape format error"); |
240 | 240 |
|
241 | 241 |
switch (c) { |
242 | 242 |
case '\\': |
243 | 243 |
return '\\'; |
244 | 244 |
case '\"': |
245 | 245 |
return '\"'; |
246 | 246 |
case '\'': |
247 | 247 |
return '\''; |
248 | 248 |
case '\?': |
249 | 249 |
return '\?'; |
250 | 250 |
case 'a': |
251 | 251 |
return '\a'; |
252 | 252 |
case 'b': |
253 | 253 |
return '\b'; |
254 | 254 |
case 'f': |
255 | 255 |
return '\f'; |
256 | 256 |
case 'n': |
257 | 257 |
return '\n'; |
258 | 258 |
case 'r': |
259 | 259 |
return '\r'; |
260 | 260 |
case 't': |
261 | 261 |
return '\t'; |
262 | 262 |
case 'v': |
263 | 263 |
return '\v'; |
264 | 264 |
case 'x': |
265 | 265 |
{ |
266 | 266 |
int code; |
267 | 267 |
if (!is.get(c) || !isHex(c)) |
268 | 268 |
throw FormatError("Escape format error"); |
269 | 269 |
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); |
270 | 270 |
else code = code * 16 + valueHex(c); |
271 | 271 |
return code; |
272 | 272 |
} |
273 | 273 |
default: |
274 | 274 |
{ |
275 | 275 |
int code; |
276 | 276 |
if (!isOct(c)) |
277 | 277 |
throw FormatError("Escape format error"); |
278 | 278 |
else if (code = valueOct(c), !is.get(c) || !isOct(c)) |
279 | 279 |
is.putback(c); |
280 | 280 |
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) |
281 | 281 |
is.putback(c); |
282 | 282 |
else code = code * 8 + valueOct(c); |
283 | 283 |
return code; |
284 | 284 |
} |
285 | 285 |
} |
286 | 286 |
} |
287 | 287 |
|
288 | 288 |
inline std::istream& readToken(std::istream& is, std::string& str) { |
289 | 289 |
std::ostringstream os; |
290 | 290 |
|
291 | 291 |
char c; |
292 | 292 |
is >> std::ws; |
293 | 293 |
|
294 | 294 |
if (!is.get(c)) |
295 | 295 |
return is; |
296 | 296 |
|
297 | 297 |
if (c == '\"') { |
298 | 298 |
while (is.get(c) && c != '\"') { |
299 | 299 |
if (c == '\\') |
300 | 300 |
c = readEscape(is); |
301 | 301 |
os << c; |
302 | 302 |
} |
303 | 303 |
if (!is) |
304 | 304 |
throw FormatError("Quoted format error"); |
305 | 305 |
} else { |
306 | 306 |
is.putback(c); |
307 | 307 |
while (is.get(c) && !isWhiteSpace(c)) { |
308 | 308 |
if (c == '\\') |
309 | 309 |
c = readEscape(is); |
310 | 310 |
os << c; |
311 | 311 |
} |
312 | 312 |
if (!is) { |
313 | 313 |
is.clear(); |
314 | 314 |
} else { |
315 | 315 |
is.putback(c); |
316 | 316 |
} |
317 | 317 |
} |
318 | 318 |
str = os.str(); |
319 | 319 |
return is; |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
class Section { |
323 | 323 |
public: |
324 | 324 |
virtual ~Section() {} |
325 | 325 |
virtual void process(std::istream& is, int& line_num) = 0; |
326 | 326 |
}; |
327 | 327 |
|
328 | 328 |
template <typename Functor> |
329 | 329 |
class LineSection : public Section { |
330 | 330 |
private: |
331 | 331 |
|
332 | 332 |
Functor _functor; |
333 | 333 |
|
334 | 334 |
public: |
335 | 335 |
|
336 | 336 |
LineSection(const Functor& functor) : _functor(functor) {} |
337 | 337 |
virtual ~LineSection() {} |
338 | 338 |
|
339 | 339 |
virtual void process(std::istream& is, int& line_num) { |
340 | 340 |
char c; |
341 | 341 |
std::string line; |
342 | 342 |
while (is.get(c) && c != '@') { |
343 | 343 |
if (c == '\n') { |
344 | 344 |
++line_num; |
345 | 345 |
} else if (c == '#') { |
346 | 346 |
getline(is, line); |
347 | 347 |
++line_num; |
348 | 348 |
} else if (!isWhiteSpace(c)) { |
349 | 349 |
is.putback(c); |
350 | 350 |
getline(is, line); |
351 | 351 |
_functor(line); |
352 | 352 |
++line_num; |
353 | 353 |
} |
354 | 354 |
} |
355 | 355 |
if (is) is.putback(c); |
356 | 356 |
else if (is.eof()) is.clear(); |
357 | 357 |
} |
358 | 358 |
}; |
359 | 359 |
|
360 | 360 |
template <typename Functor> |
361 | 361 |
class StreamSection : public Section { |
362 | 362 |
private: |
363 | 363 |
|
364 | 364 |
Functor _functor; |
365 | 365 |
|
366 | 366 |
public: |
367 | 367 |
|
368 | 368 |
StreamSection(const Functor& functor) : _functor(functor) {} |
369 | 369 |
virtual ~StreamSection() {} |
370 | 370 |
|
371 | 371 |
virtual void process(std::istream& is, int& line_num) { |
372 | 372 |
_functor(is, line_num); |
373 | 373 |
char c; |
374 | 374 |
std::string line; |
375 | 375 |
while (is.get(c) && c != '@') { |
376 | 376 |
if (c == '\n') { |
377 | 377 |
++line_num; |
378 | 378 |
} else if (!isWhiteSpace(c)) { |
379 | 379 |
getline(is, line); |
380 | 380 |
++line_num; |
381 | 381 |
} |
382 | 382 |
} |
383 | 383 |
if (is) is.putback(c); |
384 | 384 |
else if (is.eof()) is.clear(); |
385 | 385 |
} |
386 | 386 |
}; |
387 | 387 |
|
388 | 388 |
} |
389 | 389 |
|
390 | 390 |
template <typename DGR> |
391 | 391 |
class DigraphReader; |
392 | 392 |
|
393 | 393 |
template <typename TDGR> |
394 | 394 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is = std::cin); |
395 | 395 |
template <typename TDGR> |
396 | 396 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn); |
397 | 397 |
template <typename TDGR> |
398 | 398 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn); |
399 | 399 |
|
400 | 400 |
/// \ingroup lemon_io |
401 | 401 |
/// |
402 | 402 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
403 | 403 |
/// |
404 | 404 |
/// This utility reads an \ref lgf-format "LGF" file. |
405 | 405 |
/// |
406 | 406 |
/// The reading method does a batch processing. The user creates a |
407 | 407 |
/// reader object, then various reading rules can be added to the |
408 | 408 |
/// reader, and eventually the reading is executed with the \c run() |
409 | 409 |
/// member function. A map reading rule can be added to the reader |
410 | 410 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
411 | 411 |
/// converter parameter can also be added as a standard functor |
412 | 412 |
/// converting from \c std::string to the value type of the map. If it |
413 | 413 |
/// is set, it will determine how the tokens in the file should be |
414 | 414 |
/// converted to the value type of the map. If the functor is not set, |
415 | 415 |
/// then a default conversion will be used. One map can be read into |
416 | 416 |
/// multiple map objects at the same time. The \c attribute(), \c |
417 | 417 |
/// node() and \c arc() functions are used to add attribute reading |
418 | 418 |
/// rules. |
419 | 419 |
/// |
420 | 420 |
///\code |
421 | 421 |
/// DigraphReader<DGR>(digraph, std::cin). |
422 | 422 |
/// nodeMap("coordinates", coord_map). |
423 | 423 |
/// arcMap("capacity", cap_map). |
424 | 424 |
/// node("source", src). |
425 | 425 |
/// node("target", trg). |
426 | 426 |
/// attribute("caption", caption). |
427 | 427 |
/// run(); |
428 | 428 |
///\endcode |
429 | 429 |
/// |
430 |
/// By default the reader uses the first section in the file of the |
|
430 |
/// By default, the reader uses the first section in the file of the |
|
431 | 431 |
/// proper type. If a section has an optional name, then it can be |
432 | 432 |
/// selected for reading by giving an optional name parameter to the |
433 | 433 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
434 | 434 |
/// |
435 | 435 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
436 | 436 |
/// that the nodes or arcs should not be constructed (added to the |
437 | 437 |
/// graph) during the reading, but instead the label map of the items |
438 | 438 |
/// are given as a parameter of these functions. An |
439 | 439 |
/// application of these functions is multipass reading, which is |
440 | 440 |
/// important if two \c \@arcs sections must be read from the |
441 | 441 |
/// file. In this case the first phase would read the node set and one |
442 | 442 |
/// of the arc sets, while the second phase would read the second arc |
443 | 443 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
444 | 444 |
/// The previously read label node map should be passed to the \c |
445 | 445 |
/// useNodes() functions. Another application of multipass reading when |
446 | 446 |
/// paths are given as a node map or an arc map. |
447 | 447 |
/// It is impossible to read this in |
448 | 448 |
/// a single pass, because the arcs are not constructed when the node |
449 | 449 |
/// maps are read. |
450 | 450 |
template <typename DGR> |
451 | 451 |
class DigraphReader { |
452 | 452 |
public: |
453 | 453 |
|
454 | 454 |
typedef DGR Digraph; |
455 | 455 |
|
456 | 456 |
private: |
457 | 457 |
|
458 | 458 |
TEMPLATE_DIGRAPH_TYPEDEFS(DGR); |
459 | 459 |
|
460 | 460 |
std::istream* _is; |
461 | 461 |
bool local_is; |
462 | 462 |
std::string _filename; |
463 | 463 |
|
464 | 464 |
DGR& _digraph; |
465 | 465 |
|
466 | 466 |
std::string _nodes_caption; |
467 | 467 |
std::string _arcs_caption; |
468 | 468 |
std::string _attributes_caption; |
469 | 469 |
|
470 | 470 |
typedef std::map<std::string, Node> NodeIndex; |
471 | 471 |
NodeIndex _node_index; |
472 | 472 |
typedef std::map<std::string, Arc> ArcIndex; |
473 | 473 |
ArcIndex _arc_index; |
474 | 474 |
|
475 | 475 |
typedef std::vector<std::pair<std::string, |
476 | 476 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
477 | 477 |
NodeMaps _node_maps; |
478 | 478 |
|
479 | 479 |
typedef std::vector<std::pair<std::string, |
480 | 480 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
481 | 481 |
ArcMaps _arc_maps; |
482 | 482 |
|
483 | 483 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
484 | 484 |
Attributes; |
485 | 485 |
Attributes _attributes; |
486 | 486 |
|
487 | 487 |
bool _use_nodes; |
488 | 488 |
bool _use_arcs; |
489 | 489 |
|
490 | 490 |
bool _skip_nodes; |
491 | 491 |
bool _skip_arcs; |
492 | 492 |
|
493 | 493 |
int line_num; |
494 | 494 |
std::istringstream line; |
495 | 495 |
|
496 | 496 |
public: |
497 | 497 |
|
498 | 498 |
/// \brief Constructor |
499 | 499 |
/// |
500 | 500 |
/// Construct a directed graph reader, which reads from the given |
501 | 501 |
/// input stream. |
502 | 502 |
DigraphReader(DGR& digraph, std::istream& is = std::cin) |
503 | 503 |
: _is(&is), local_is(false), _digraph(digraph), |
504 | 504 |
_use_nodes(false), _use_arcs(false), |
505 | 505 |
_skip_nodes(false), _skip_arcs(false) {} |
506 | 506 |
|
507 | 507 |
/// \brief Constructor |
508 | 508 |
/// |
509 | 509 |
/// Construct a directed graph reader, which reads from the given |
510 | 510 |
/// file. |
511 | 511 |
DigraphReader(DGR& digraph, const std::string& fn) |
512 | 512 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
513 | 513 |
_filename(fn), _digraph(digraph), |
514 | 514 |
_use_nodes(false), _use_arcs(false), |
515 | 515 |
_skip_nodes(false), _skip_arcs(false) { |
516 | 516 |
if (!(*_is)) { |
517 | 517 |
delete _is; |
518 | 518 |
throw IoError("Cannot open file", fn); |
519 | 519 |
} |
520 | 520 |
} |
521 | 521 |
|
522 | 522 |
/// \brief Constructor |
523 | 523 |
/// |
524 | 524 |
/// Construct a directed graph reader, which reads from the given |
525 | 525 |
/// file. |
526 | 526 |
DigraphReader(DGR& digraph, const char* fn) |
527 | 527 |
: _is(new std::ifstream(fn)), local_is(true), |
528 | 528 |
_filename(fn), _digraph(digraph), |
529 | 529 |
_use_nodes(false), _use_arcs(false), |
530 | 530 |
_skip_nodes(false), _skip_arcs(false) { |
531 | 531 |
if (!(*_is)) { |
532 | 532 |
delete _is; |
533 | 533 |
throw IoError("Cannot open file", fn); |
534 | 534 |
} |
535 | 535 |
} |
536 | 536 |
|
537 | 537 |
/// \brief Destructor |
538 | 538 |
~DigraphReader() { |
539 | 539 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
540 | 540 |
it != _node_maps.end(); ++it) { |
541 | 541 |
delete it->second; |
542 | 542 |
} |
543 | 543 |
|
544 | 544 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
545 | 545 |
it != _arc_maps.end(); ++it) { |
546 | 546 |
delete it->second; |
547 | 547 |
} |
548 | 548 |
|
549 | 549 |
for (typename Attributes::iterator it = _attributes.begin(); |
550 | 550 |
it != _attributes.end(); ++it) { |
551 | 551 |
delete it->second; |
552 | 552 |
} |
553 | 553 |
|
554 | 554 |
if (local_is) { |
555 | 555 |
delete _is; |
556 | 556 |
} |
557 | 557 |
|
558 | 558 |
} |
559 | 559 |
|
560 | 560 |
private: |
561 | 561 |
|
562 | 562 |
template <typename TDGR> |
563 | 563 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is); |
564 | 564 |
template <typename TDGR> |
565 | 565 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, |
566 | 566 |
const std::string& fn); |
567 | 567 |
template <typename TDGR> |
568 | 568 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn); |
569 | 569 |
|
570 | 570 |
DigraphReader(DigraphReader& other) |
571 | 571 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
572 | 572 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
573 | 573 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { |
574 | 574 |
|
575 | 575 |
other._is = 0; |
576 | 576 |
other.local_is = false; |
577 | 577 |
|
578 | 578 |
_node_index.swap(other._node_index); |
579 | 579 |
_arc_index.swap(other._arc_index); |
580 | 580 |
|
581 | 581 |
_node_maps.swap(other._node_maps); |
582 | 582 |
_arc_maps.swap(other._arc_maps); |
583 | 583 |
_attributes.swap(other._attributes); |
584 | 584 |
|
585 | 585 |
_nodes_caption = other._nodes_caption; |
586 | 586 |
_arcs_caption = other._arcs_caption; |
587 | 587 |
_attributes_caption = other._attributes_caption; |
588 | 588 |
|
589 | 589 |
} |
590 | 590 |
|
591 | 591 |
DigraphReader& operator=(const DigraphReader&); |
592 | 592 |
|
593 | 593 |
public: |
594 | 594 |
|
595 | 595 |
/// \name Reading Rules |
596 | 596 |
/// @{ |
597 | 597 |
|
598 | 598 |
/// \brief Node map reading rule |
599 | 599 |
/// |
600 | 600 |
/// Add a node map reading rule to the reader. |
601 | 601 |
template <typename Map> |
602 | 602 |
DigraphReader& nodeMap(const std::string& caption, Map& map) { |
603 | 603 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
604 | 604 |
_reader_bits::MapStorageBase<Node>* storage = |
605 | 605 |
new _reader_bits::MapStorage<Node, Map>(map); |
606 | 606 |
_node_maps.push_back(std::make_pair(caption, storage)); |
607 | 607 |
return *this; |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
/// \brief Node map reading rule |
611 | 611 |
/// |
612 | 612 |
/// Add a node map reading rule with specialized converter to the |
613 | 613 |
/// reader. |
614 | 614 |
template <typename Map, typename Converter> |
615 | 615 |
DigraphReader& nodeMap(const std::string& caption, Map& map, |
616 | 616 |
const Converter& converter = Converter()) { |
617 | 617 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
618 | 618 |
_reader_bits::MapStorageBase<Node>* storage = |
619 | 619 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
620 | 620 |
_node_maps.push_back(std::make_pair(caption, storage)); |
621 | 621 |
return *this; |
622 | 622 |
} |
... | ... |
@@ -2032,385 +2032,385 @@ |
2032 | 2032 |
} |
2033 | 2033 |
} else { |
2034 | 2034 |
readLine(); |
2035 | 2035 |
skipSection(); |
2036 | 2036 |
} |
2037 | 2037 |
} catch (FormatError& error) { |
2038 | 2038 |
error.line(line_num); |
2039 | 2039 |
error.file(_filename); |
2040 | 2040 |
throw; |
2041 | 2041 |
} |
2042 | 2042 |
} |
2043 | 2043 |
|
2044 | 2044 |
if (!nodes_done) { |
2045 | 2045 |
throw FormatError("Section @nodes not found"); |
2046 | 2046 |
} |
2047 | 2047 |
|
2048 | 2048 |
if (!edges_done) { |
2049 | 2049 |
throw FormatError("Section @edges not found"); |
2050 | 2050 |
} |
2051 | 2051 |
|
2052 | 2052 |
if (!attributes_done && !_attributes.empty()) { |
2053 | 2053 |
throw FormatError("Section @attributes not found"); |
2054 | 2054 |
} |
2055 | 2055 |
|
2056 | 2056 |
} |
2057 | 2057 |
|
2058 | 2058 |
/// @} |
2059 | 2059 |
|
2060 | 2060 |
}; |
2061 | 2061 |
|
2062 | 2062 |
/// \ingroup lemon_io |
2063 | 2063 |
/// |
2064 | 2064 |
/// \brief Return a \ref GraphReader class |
2065 | 2065 |
/// |
2066 | 2066 |
/// This function just returns a \ref GraphReader class. |
2067 | 2067 |
/// |
2068 | 2068 |
/// With this function a graph can be read from an |
2069 | 2069 |
/// \ref lgf-format "LGF" file or input stream with several maps and |
2070 | 2070 |
/// attributes. For example, there is weighted matching problem on a |
2071 | 2071 |
/// graph, i.e. a graph with a \e weight map on the edges. This |
2072 | 2072 |
/// graph can be read with the following code: |
2073 | 2073 |
/// |
2074 | 2074 |
///\code |
2075 | 2075 |
///ListGraph graph; |
2076 | 2076 |
///ListGraph::EdgeMap<int> weight(graph); |
2077 | 2077 |
///graphReader(graph, std::cin). |
2078 | 2078 |
/// edgeMap("weight", weight). |
2079 | 2079 |
/// run(); |
2080 | 2080 |
///\endcode |
2081 | 2081 |
/// |
2082 | 2082 |
/// For a complete documentation, please see the \ref GraphReader |
2083 | 2083 |
/// class documentation. |
2084 | 2084 |
/// \warning Don't forget to put the \ref GraphReader::run() "run()" |
2085 | 2085 |
/// to the end of the parameter list. |
2086 | 2086 |
/// \relates GraphReader |
2087 | 2087 |
/// \sa graphReader(TGR& graph, const std::string& fn) |
2088 | 2088 |
/// \sa graphReader(TGR& graph, const char* fn) |
2089 | 2089 |
template <typename TGR> |
2090 | 2090 |
GraphReader<TGR> graphReader(TGR& graph, std::istream& is) { |
2091 | 2091 |
GraphReader<TGR> tmp(graph, is); |
2092 | 2092 |
return tmp; |
2093 | 2093 |
} |
2094 | 2094 |
|
2095 | 2095 |
/// \brief Return a \ref GraphReader class |
2096 | 2096 |
/// |
2097 | 2097 |
/// This function just returns a \ref GraphReader class. |
2098 | 2098 |
/// \relates GraphReader |
2099 | 2099 |
/// \sa graphReader(TGR& graph, std::istream& is) |
2100 | 2100 |
template <typename TGR> |
2101 | 2101 |
GraphReader<TGR> graphReader(TGR& graph, const std::string& fn) { |
2102 | 2102 |
GraphReader<TGR> tmp(graph, fn); |
2103 | 2103 |
return tmp; |
2104 | 2104 |
} |
2105 | 2105 |
|
2106 | 2106 |
/// \brief Return a \ref GraphReader class |
2107 | 2107 |
/// |
2108 | 2108 |
/// This function just returns a \ref GraphReader class. |
2109 | 2109 |
/// \relates GraphReader |
2110 | 2110 |
/// \sa graphReader(TGR& graph, std::istream& is) |
2111 | 2111 |
template <typename TGR> |
2112 | 2112 |
GraphReader<TGR> graphReader(TGR& graph, const char* fn) { |
2113 | 2113 |
GraphReader<TGR> tmp(graph, fn); |
2114 | 2114 |
return tmp; |
2115 | 2115 |
} |
2116 | 2116 |
|
2117 | 2117 |
class SectionReader; |
2118 | 2118 |
|
2119 | 2119 |
SectionReader sectionReader(std::istream& is); |
2120 | 2120 |
SectionReader sectionReader(const std::string& fn); |
2121 | 2121 |
SectionReader sectionReader(const char* fn); |
2122 | 2122 |
|
2123 | 2123 |
/// \ingroup lemon_io |
2124 | 2124 |
/// |
2125 | 2125 |
/// \brief Section reader class |
2126 | 2126 |
/// |
2127 | 2127 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
2128 | 2128 |
/// which contain any data in arbitrary format. Such sections can be |
2129 | 2129 |
/// read with this class. A reading rule can be added to the class |
2130 | 2130 |
/// with two different functions. With the \c sectionLines() function a |
2131 | 2131 |
/// functor can process the section line-by-line, while with the \c |
2132 | 2132 |
/// sectionStream() member the section can be read from an input |
2133 | 2133 |
/// stream. |
2134 | 2134 |
class SectionReader { |
2135 | 2135 |
private: |
2136 | 2136 |
|
2137 | 2137 |
std::istream* _is; |
2138 | 2138 |
bool local_is; |
2139 | 2139 |
std::string _filename; |
2140 | 2140 |
|
2141 | 2141 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
2142 | 2142 |
Sections _sections; |
2143 | 2143 |
|
2144 | 2144 |
int line_num; |
2145 | 2145 |
std::istringstream line; |
2146 | 2146 |
|
2147 | 2147 |
public: |
2148 | 2148 |
|
2149 | 2149 |
/// \brief Constructor |
2150 | 2150 |
/// |
2151 | 2151 |
/// Construct a section reader, which reads from the given input |
2152 | 2152 |
/// stream. |
2153 | 2153 |
SectionReader(std::istream& is) |
2154 | 2154 |
: _is(&is), local_is(false) {} |
2155 | 2155 |
|
2156 | 2156 |
/// \brief Constructor |
2157 | 2157 |
/// |
2158 | 2158 |
/// Construct a section reader, which reads from the given file. |
2159 | 2159 |
SectionReader(const std::string& fn) |
2160 | 2160 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
2161 | 2161 |
_filename(fn) { |
2162 | 2162 |
if (!(*_is)) { |
2163 | 2163 |
delete _is; |
2164 | 2164 |
throw IoError("Cannot open file", fn); |
2165 | 2165 |
} |
2166 | 2166 |
} |
2167 | 2167 |
|
2168 | 2168 |
/// \brief Constructor |
2169 | 2169 |
/// |
2170 | 2170 |
/// Construct a section reader, which reads from the given file. |
2171 | 2171 |
SectionReader(const char* fn) |
2172 | 2172 |
: _is(new std::ifstream(fn)), local_is(true), |
2173 | 2173 |
_filename(fn) { |
2174 | 2174 |
if (!(*_is)) { |
2175 | 2175 |
delete _is; |
2176 | 2176 |
throw IoError("Cannot open file", fn); |
2177 | 2177 |
} |
2178 | 2178 |
} |
2179 | 2179 |
|
2180 | 2180 |
/// \brief Destructor |
2181 | 2181 |
~SectionReader() { |
2182 | 2182 |
for (Sections::iterator it = _sections.begin(); |
2183 | 2183 |
it != _sections.end(); ++it) { |
2184 | 2184 |
delete it->second; |
2185 | 2185 |
} |
2186 | 2186 |
|
2187 | 2187 |
if (local_is) { |
2188 | 2188 |
delete _is; |
2189 | 2189 |
} |
2190 | 2190 |
|
2191 | 2191 |
} |
2192 | 2192 |
|
2193 | 2193 |
private: |
2194 | 2194 |
|
2195 | 2195 |
friend SectionReader sectionReader(std::istream& is); |
2196 | 2196 |
friend SectionReader sectionReader(const std::string& fn); |
2197 | 2197 |
friend SectionReader sectionReader(const char* fn); |
2198 | 2198 |
|
2199 | 2199 |
SectionReader(SectionReader& other) |
2200 | 2200 |
: _is(other._is), local_is(other.local_is) { |
2201 | 2201 |
|
2202 | 2202 |
other._is = 0; |
2203 | 2203 |
other.local_is = false; |
2204 | 2204 |
|
2205 | 2205 |
_sections.swap(other._sections); |
2206 | 2206 |
} |
2207 | 2207 |
|
2208 | 2208 |
SectionReader& operator=(const SectionReader&); |
2209 | 2209 |
|
2210 | 2210 |
public: |
2211 | 2211 |
|
2212 | 2212 |
/// \name Section Readers |
2213 | 2213 |
/// @{ |
2214 | 2214 |
|
2215 | 2215 |
/// \brief Add a section processor with line oriented reading |
2216 | 2216 |
/// |
2217 | 2217 |
/// The first parameter is the type descriptor of the section, the |
2218 | 2218 |
/// second is a functor, which takes just one \c std::string |
2219 | 2219 |
/// parameter. At the reading process, each line of the section |
2220 | 2220 |
/// will be given to the functor object. However, the empty lines |
2221 | 2221 |
/// and the comment lines are filtered out, and the leading |
2222 | 2222 |
/// whitespaces are trimmed from each processed string. |
2223 | 2223 |
/// |
2224 |
/// For example let's see a section, which contain several |
|
2224 |
/// For example, let's see a section, which contain several |
|
2225 | 2225 |
/// integers, which should be inserted into a vector. |
2226 | 2226 |
///\code |
2227 | 2227 |
/// @numbers |
2228 | 2228 |
/// 12 45 23 |
2229 | 2229 |
/// 4 |
2230 | 2230 |
/// 23 6 |
2231 | 2231 |
///\endcode |
2232 | 2232 |
/// |
2233 | 2233 |
/// The functor is implemented as a struct: |
2234 | 2234 |
///\code |
2235 | 2235 |
/// struct NumberSection { |
2236 | 2236 |
/// std::vector<int>& _data; |
2237 | 2237 |
/// NumberSection(std::vector<int>& data) : _data(data) {} |
2238 | 2238 |
/// void operator()(const std::string& line) { |
2239 | 2239 |
/// std::istringstream ls(line); |
2240 | 2240 |
/// int value; |
2241 | 2241 |
/// while (ls >> value) _data.push_back(value); |
2242 | 2242 |
/// } |
2243 | 2243 |
/// }; |
2244 | 2244 |
/// |
2245 | 2245 |
/// // ... |
2246 | 2246 |
/// |
2247 | 2247 |
/// reader.sectionLines("numbers", NumberSection(vec)); |
2248 | 2248 |
///\endcode |
2249 | 2249 |
template <typename Functor> |
2250 | 2250 |
SectionReader& sectionLines(const std::string& type, Functor functor) { |
2251 | 2251 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
2252 | 2252 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
2253 | 2253 |
"Multiple reading of section."); |
2254 | 2254 |
_sections.insert(std::make_pair(type, |
2255 | 2255 |
new _reader_bits::LineSection<Functor>(functor))); |
2256 | 2256 |
return *this; |
2257 | 2257 |
} |
2258 | 2258 |
|
2259 | 2259 |
|
2260 | 2260 |
/// \brief Add a section processor with stream oriented reading |
2261 | 2261 |
/// |
2262 | 2262 |
/// The first parameter is the type of the section, the second is |
2263 | 2263 |
/// a functor, which takes an \c std::istream& and an \c int& |
2264 | 2264 |
/// parameter, the latter regard to the line number of stream. The |
2265 | 2265 |
/// functor can read the input while the section go on, and the |
2266 | 2266 |
/// line number should be modified accordingly. |
2267 | 2267 |
template <typename Functor> |
2268 | 2268 |
SectionReader& sectionStream(const std::string& type, Functor functor) { |
2269 | 2269 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
2270 | 2270 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
2271 | 2271 |
"Multiple reading of section."); |
2272 | 2272 |
_sections.insert(std::make_pair(type, |
2273 | 2273 |
new _reader_bits::StreamSection<Functor>(functor))); |
2274 | 2274 |
return *this; |
2275 | 2275 |
} |
2276 | 2276 |
|
2277 | 2277 |
/// @} |
2278 | 2278 |
|
2279 | 2279 |
private: |
2280 | 2280 |
|
2281 | 2281 |
bool readLine() { |
2282 | 2282 |
std::string str; |
2283 | 2283 |
while(++line_num, std::getline(*_is, str)) { |
2284 | 2284 |
line.clear(); line.str(str); |
2285 | 2285 |
char c; |
2286 | 2286 |
if (line >> std::ws >> c && c != '#') { |
2287 | 2287 |
line.putback(c); |
2288 | 2288 |
return true; |
2289 | 2289 |
} |
2290 | 2290 |
} |
2291 | 2291 |
return false; |
2292 | 2292 |
} |
2293 | 2293 |
|
2294 | 2294 |
bool readSuccess() { |
2295 | 2295 |
return static_cast<bool>(*_is); |
2296 | 2296 |
} |
2297 | 2297 |
|
2298 | 2298 |
void skipSection() { |
2299 | 2299 |
char c; |
2300 | 2300 |
while (readSuccess() && line >> c && c != '@') { |
2301 | 2301 |
readLine(); |
2302 | 2302 |
} |
2303 | 2303 |
if (readSuccess()) { |
2304 | 2304 |
line.putback(c); |
2305 | 2305 |
} |
2306 | 2306 |
} |
2307 | 2307 |
|
2308 | 2308 |
public: |
2309 | 2309 |
|
2310 | 2310 |
|
2311 | 2311 |
/// \name Execution of the Reader |
2312 | 2312 |
/// @{ |
2313 | 2313 |
|
2314 | 2314 |
/// \brief Start the batch processing |
2315 | 2315 |
/// |
2316 | 2316 |
/// This function starts the batch processing. |
2317 | 2317 |
void run() { |
2318 | 2318 |
|
2319 | 2319 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
2320 | 2320 |
|
2321 | 2321 |
std::set<std::string> extra_sections; |
2322 | 2322 |
|
2323 | 2323 |
line_num = 0; |
2324 | 2324 |
readLine(); |
2325 | 2325 |
skipSection(); |
2326 | 2326 |
|
2327 | 2327 |
while (readSuccess()) { |
2328 | 2328 |
try { |
2329 | 2329 |
char c; |
2330 | 2330 |
std::string section, caption; |
2331 | 2331 |
line >> c; |
2332 | 2332 |
_reader_bits::readToken(line, section); |
2333 | 2333 |
_reader_bits::readToken(line, caption); |
2334 | 2334 |
|
2335 | 2335 |
if (line >> c) |
2336 | 2336 |
throw FormatError("Extra character at the end of line"); |
2337 | 2337 |
|
2338 | 2338 |
if (extra_sections.find(section) != extra_sections.end()) { |
2339 | 2339 |
std::ostringstream msg; |
2340 | 2340 |
msg << "Multiple occurence of section: " << section; |
2341 | 2341 |
throw FormatError(msg.str()); |
2342 | 2342 |
} |
2343 | 2343 |
Sections::iterator it = _sections.find(section); |
2344 | 2344 |
if (it != _sections.end()) { |
2345 | 2345 |
extra_sections.insert(section); |
2346 | 2346 |
it->second->process(*_is, line_num); |
2347 | 2347 |
} |
2348 | 2348 |
readLine(); |
2349 | 2349 |
skipSection(); |
2350 | 2350 |
} catch (FormatError& error) { |
2351 | 2351 |
error.line(line_num); |
2352 | 2352 |
error.file(_filename); |
2353 | 2353 |
throw; |
2354 | 2354 |
} |
2355 | 2355 |
} |
2356 | 2356 |
for (Sections::iterator it = _sections.begin(); |
2357 | 2357 |
it != _sections.end(); ++it) { |
2358 | 2358 |
if (extra_sections.find(it->first) == extra_sections.end()) { |
2359 | 2359 |
std::ostringstream os; |
2360 | 2360 |
os << "Cannot find section: " << it->first; |
2361 | 2361 |
throw FormatError(os.str()); |
2362 | 2362 |
} |
2363 | 2363 |
} |
2364 | 2364 |
} |
2365 | 2365 |
|
2366 | 2366 |
/// @} |
2367 | 2367 |
|
2368 | 2368 |
}; |
2369 | 2369 |
|
2370 | 2370 |
/// \ingroup lemon_io |
2371 | 2371 |
/// |
2372 | 2372 |
/// \brief Return a \ref SectionReader class |
2373 | 2373 |
/// |
2374 | 2374 |
/// This function just returns a \ref SectionReader class. |
2375 | 2375 |
/// |
2376 | 2376 |
/// Please see SectionReader documentation about the custom section |
2377 | 2377 |
/// input. |
2378 | 2378 |
/// |
2379 | 2379 |
/// \relates SectionReader |
2380 | 2380 |
/// \sa sectionReader(const std::string& fn) |
2381 | 2381 |
/// \sa sectionReader(const char *fn) |
2382 | 2382 |
inline SectionReader sectionReader(std::istream& is) { |
2383 | 2383 |
SectionReader tmp(is); |
2384 | 2384 |
return tmp; |
2385 | 2385 |
} |
2386 | 2386 |
|
2387 | 2387 |
/// \brief Return a \ref SectionReader class |
2388 | 2388 |
/// |
2389 | 2389 |
/// This function just returns a \ref SectionReader class. |
2390 | 2390 |
/// \relates SectionReader |
2391 | 2391 |
/// \sa sectionReader(std::istream& is) |
2392 | 2392 |
inline SectionReader sectionReader(const std::string& fn) { |
2393 | 2393 |
SectionReader tmp(fn); |
2394 | 2394 |
return tmp; |
2395 | 2395 |
} |
2396 | 2396 |
|
2397 | 2397 |
/// \brief Return a \ref SectionReader class |
2398 | 2398 |
/// |
2399 | 2399 |
/// This function just returns a \ref SectionReader class. |
2400 | 2400 |
/// \relates SectionReader |
2401 | 2401 |
/// \sa sectionReader(std::istream& is) |
2402 | 2402 |
inline SectionReader sectionReader(const char* fn) { |
2403 | 2403 |
SectionReader tmp(fn); |
2404 | 2404 |
return tmp; |
2405 | 2405 |
} |
2406 | 2406 |
|
2407 | 2407 |
/// \ingroup lemon_io |
2408 | 2408 |
/// |
2409 | 2409 |
/// \brief Reader for the contents of the \ref lgf-format "LGF" file |
2410 | 2410 |
/// |
2411 | 2411 |
/// This class can be used to read the sections, the map names and |
2412 | 2412 |
/// the attributes from a file. Usually, the LEMON programs know |
2413 | 2413 |
/// that, which type of graph, which maps and which attributes |
2414 | 2414 |
/// should be read from a file, but in general tools (like glemon) |
2415 | 2415 |
/// the contents of an LGF file should be guessed somehow. This class |
2416 | 2416 |
/// reads the graph and stores the appropriate information for |
... | ... |
@@ -202,543 +202,543 @@ |
202 | 202 |
|
203 | 203 |
arcs[n].source = u.id; |
204 | 204 |
arcs[n].target = v.id; |
205 | 205 |
|
206 | 206 |
arcs[n].next_out = nodes[u.id].first_out; |
207 | 207 |
if(nodes[u.id].first_out != -1) { |
208 | 208 |
arcs[nodes[u.id].first_out].prev_out = n; |
209 | 209 |
} |
210 | 210 |
|
211 | 211 |
arcs[n].next_in = nodes[v.id].first_in; |
212 | 212 |
if(nodes[v.id].first_in != -1) { |
213 | 213 |
arcs[nodes[v.id].first_in].prev_in = n; |
214 | 214 |
} |
215 | 215 |
|
216 | 216 |
arcs[n].prev_in = arcs[n].prev_out = -1; |
217 | 217 |
|
218 | 218 |
nodes[u.id].first_out = nodes[v.id].first_in = n; |
219 | 219 |
|
220 | 220 |
return Arc(n); |
221 | 221 |
} |
222 | 222 |
|
223 | 223 |
void erase(const Node& node) { |
224 | 224 |
int n = node.id; |
225 | 225 |
|
226 | 226 |
if(nodes[n].next != -1) { |
227 | 227 |
nodes[nodes[n].next].prev = nodes[n].prev; |
228 | 228 |
} |
229 | 229 |
|
230 | 230 |
if(nodes[n].prev != -1) { |
231 | 231 |
nodes[nodes[n].prev].next = nodes[n].next; |
232 | 232 |
} else { |
233 | 233 |
first_node = nodes[n].next; |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
nodes[n].next = first_free_node; |
237 | 237 |
first_free_node = n; |
238 | 238 |
nodes[n].prev = -2; |
239 | 239 |
|
240 | 240 |
} |
241 | 241 |
|
242 | 242 |
void erase(const Arc& arc) { |
243 | 243 |
int n = arc.id; |
244 | 244 |
|
245 | 245 |
if(arcs[n].next_in!=-1) { |
246 | 246 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
if(arcs[n].prev_in!=-1) { |
250 | 250 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
251 | 251 |
} else { |
252 | 252 |
nodes[arcs[n].target].first_in = arcs[n].next_in; |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
|
256 | 256 |
if(arcs[n].next_out!=-1) { |
257 | 257 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
if(arcs[n].prev_out!=-1) { |
261 | 261 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
262 | 262 |
} else { |
263 | 263 |
nodes[arcs[n].source].first_out = arcs[n].next_out; |
264 | 264 |
} |
265 | 265 |
|
266 | 266 |
arcs[n].next_in = first_free_arc; |
267 | 267 |
first_free_arc = n; |
268 | 268 |
arcs[n].prev_in = -2; |
269 | 269 |
} |
270 | 270 |
|
271 | 271 |
void clear() { |
272 | 272 |
arcs.clear(); |
273 | 273 |
nodes.clear(); |
274 | 274 |
first_node = first_free_node = first_free_arc = -1; |
275 | 275 |
} |
276 | 276 |
|
277 | 277 |
protected: |
278 | 278 |
void changeTarget(Arc e, Node n) |
279 | 279 |
{ |
280 | 280 |
if(arcs[e.id].next_in != -1) |
281 | 281 |
arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in; |
282 | 282 |
if(arcs[e.id].prev_in != -1) |
283 | 283 |
arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in; |
284 | 284 |
else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in; |
285 | 285 |
if (nodes[n.id].first_in != -1) { |
286 | 286 |
arcs[nodes[n.id].first_in].prev_in = e.id; |
287 | 287 |
} |
288 | 288 |
arcs[e.id].target = n.id; |
289 | 289 |
arcs[e.id].prev_in = -1; |
290 | 290 |
arcs[e.id].next_in = nodes[n.id].first_in; |
291 | 291 |
nodes[n.id].first_in = e.id; |
292 | 292 |
} |
293 | 293 |
void changeSource(Arc e, Node n) |
294 | 294 |
{ |
295 | 295 |
if(arcs[e.id].next_out != -1) |
296 | 296 |
arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
297 | 297 |
if(arcs[e.id].prev_out != -1) |
298 | 298 |
arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
299 | 299 |
else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
300 | 300 |
if (nodes[n.id].first_out != -1) { |
301 | 301 |
arcs[nodes[n.id].first_out].prev_out = e.id; |
302 | 302 |
} |
303 | 303 |
arcs[e.id].source = n.id; |
304 | 304 |
arcs[e.id].prev_out = -1; |
305 | 305 |
arcs[e.id].next_out = nodes[n.id].first_out; |
306 | 306 |
nodes[n.id].first_out = e.id; |
307 | 307 |
} |
308 | 308 |
|
309 | 309 |
}; |
310 | 310 |
|
311 | 311 |
typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
312 | 312 |
|
313 | 313 |
/// \addtogroup graphs |
314 | 314 |
/// @{ |
315 | 315 |
|
316 | 316 |
///A general directed graph structure. |
317 | 317 |
|
318 | 318 |
///\ref ListDigraph is a versatile and fast directed graph |
319 | 319 |
///implementation based on linked lists that are stored in |
320 | 320 |
///\c std::vector structures. |
321 | 321 |
/// |
322 | 322 |
///This type fully conforms to the \ref concepts::Digraph "Digraph concept" |
323 | 323 |
///and it also provides several useful additional functionalities. |
324 | 324 |
///Most of its member functions and nested classes are documented |
325 | 325 |
///only in the concept class. |
326 | 326 |
/// |
327 | 327 |
///\sa concepts::Digraph |
328 | 328 |
///\sa ListGraph |
329 | 329 |
class ListDigraph : public ExtendedListDigraphBase { |
330 | 330 |
typedef ExtendedListDigraphBase Parent; |
331 | 331 |
|
332 | 332 |
private: |
333 | 333 |
/// Digraphs are \e not copy constructible. Use DigraphCopy instead. |
334 | 334 |
ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {}; |
335 | 335 |
/// \brief Assignment of a digraph to another one is \e not allowed. |
336 | 336 |
/// Use DigraphCopy instead. |
337 | 337 |
void operator=(const ListDigraph &) {} |
338 | 338 |
public: |
339 | 339 |
|
340 | 340 |
/// Constructor |
341 | 341 |
|
342 | 342 |
/// Constructor. |
343 | 343 |
/// |
344 | 344 |
ListDigraph() {} |
345 | 345 |
|
346 | 346 |
///Add a new node to the digraph. |
347 | 347 |
|
348 | 348 |
///This function adds a new node to the digraph. |
349 | 349 |
///\return The new node. |
350 | 350 |
Node addNode() { return Parent::addNode(); } |
351 | 351 |
|
352 | 352 |
///Add a new arc to the digraph. |
353 | 353 |
|
354 | 354 |
///This function adds a new arc to the digraph with source node \c s |
355 | 355 |
///and target node \c t. |
356 | 356 |
///\return The new arc. |
357 | 357 |
Arc addArc(Node s, Node t) { |
358 | 358 |
return Parent::addArc(s, t); |
359 | 359 |
} |
360 | 360 |
|
361 | 361 |
///\brief Erase a node from the digraph. |
362 | 362 |
/// |
363 | 363 |
///This function erases the given node from the digraph. |
364 | 364 |
void erase(Node n) { Parent::erase(n); } |
365 | 365 |
|
366 | 366 |
///\brief Erase an arc from the digraph. |
367 | 367 |
/// |
368 | 368 |
///This function erases the given arc from the digraph. |
369 | 369 |
void erase(Arc a) { Parent::erase(a); } |
370 | 370 |
|
371 | 371 |
/// Node validity check |
372 | 372 |
|
373 | 373 |
/// This function gives back \c true if the given node is valid, |
374 | 374 |
/// i.e. it is a real node of the digraph. |
375 | 375 |
/// |
376 | 376 |
/// \warning A removed node could become valid again if new nodes are |
377 | 377 |
/// added to the digraph. |
378 | 378 |
bool valid(Node n) const { return Parent::valid(n); } |
379 | 379 |
|
380 | 380 |
/// Arc validity check |
381 | 381 |
|
382 | 382 |
/// This function gives back \c true if the given arc is valid, |
383 | 383 |
/// i.e. it is a real arc of the digraph. |
384 | 384 |
/// |
385 | 385 |
/// \warning A removed arc could become valid again if new arcs are |
386 | 386 |
/// added to the digraph. |
387 | 387 |
bool valid(Arc a) const { return Parent::valid(a); } |
388 | 388 |
|
389 | 389 |
/// Change the target node of an arc |
390 | 390 |
|
391 | 391 |
/// This function changes the target node of the given arc \c a to \c n. |
392 | 392 |
/// |
393 | 393 |
///\note \c ArcIt and \c OutArcIt iterators referencing the changed |
394 |
///arc remain valid, |
|
394 |
///arc remain valid, but \c InArcIt iterators are invalidated. |
|
395 | 395 |
/// |
396 | 396 |
///\warning This functionality cannot be used together with the Snapshot |
397 | 397 |
///feature. |
398 | 398 |
void changeTarget(Arc a, Node n) { |
399 | 399 |
Parent::changeTarget(a,n); |
400 | 400 |
} |
401 | 401 |
/// Change the source node of an arc |
402 | 402 |
|
403 | 403 |
/// This function changes the source node of the given arc \c a to \c n. |
404 | 404 |
/// |
405 | 405 |
///\note \c InArcIt iterators referencing the changed arc remain |
406 |
///valid, |
|
406 |
///valid, but \c ArcIt and \c OutArcIt iterators are invalidated. |
|
407 | 407 |
/// |
408 | 408 |
///\warning This functionality cannot be used together with the Snapshot |
409 | 409 |
///feature. |
410 | 410 |
void changeSource(Arc a, Node n) { |
411 | 411 |
Parent::changeSource(a,n); |
412 | 412 |
} |
413 | 413 |
|
414 | 414 |
/// Reverse the direction of an arc. |
415 | 415 |
|
416 | 416 |
/// This function reverses the direction of the given arc. |
417 | 417 |
///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing |
418 | 418 |
///the changed arc are invalidated. |
419 | 419 |
/// |
420 | 420 |
///\warning This functionality cannot be used together with the Snapshot |
421 | 421 |
///feature. |
422 | 422 |
void reverseArc(Arc a) { |
423 | 423 |
Node t=target(a); |
424 | 424 |
changeTarget(a,source(a)); |
425 | 425 |
changeSource(a,t); |
426 | 426 |
} |
427 | 427 |
|
428 | 428 |
///Contract two nodes. |
429 | 429 |
|
430 | 430 |
///This function contracts the given two nodes. |
431 | 431 |
///Node \c v is removed, but instead of deleting its |
432 | 432 |
///incident arcs, they are joined to node \c u. |
433 | 433 |
///If the last parameter \c r is \c true (this is the default value), |
434 | 434 |
///then the newly created loops are removed. |
435 | 435 |
/// |
436 | 436 |
///\note The moved arcs are joined to node \c u using changeSource() |
437 | 437 |
///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are |
438 | 438 |
///invalidated for the outgoing arcs of node \c v and \c InArcIt |
439 | 439 |
///iterators are invalidated for the incomming arcs of \c v. |
440 | 440 |
///Moreover all iterators referencing node \c v or the removed |
441 | 441 |
///loops are also invalidated. Other iterators remain valid. |
442 | 442 |
/// |
443 | 443 |
///\warning This functionality cannot be used together with the Snapshot |
444 | 444 |
///feature. |
445 | 445 |
void contract(Node u, Node v, bool r = true) |
446 | 446 |
{ |
447 | 447 |
for(OutArcIt e(*this,v);e!=INVALID;) { |
448 | 448 |
OutArcIt f=e; |
449 | 449 |
++f; |
450 | 450 |
if(r && target(e)==u) erase(e); |
451 | 451 |
else changeSource(e,u); |
452 | 452 |
e=f; |
453 | 453 |
} |
454 | 454 |
for(InArcIt e(*this,v);e!=INVALID;) { |
455 | 455 |
InArcIt f=e; |
456 | 456 |
++f; |
457 | 457 |
if(r && source(e)==u) erase(e); |
458 | 458 |
else changeTarget(e,u); |
459 | 459 |
e=f; |
460 | 460 |
} |
461 | 461 |
erase(v); |
462 | 462 |
} |
463 | 463 |
|
464 | 464 |
///Split a node. |
465 | 465 |
|
466 | 466 |
///This function splits the given node. First, a new node is added |
467 | 467 |
///to the digraph, then the source of each outgoing arc of node \c n |
468 | 468 |
///is moved to this new node. |
469 | 469 |
///If the second parameter \c connect is \c true (this is the default |
470 | 470 |
///value), then a new arc from node \c n to the newly created node |
471 | 471 |
///is also added. |
472 | 472 |
///\return The newly created node. |
473 | 473 |
/// |
474 | 474 |
///\note All iterators remain valid. |
475 | 475 |
/// |
476 | 476 |
///\warning This functionality cannot be used together with the |
477 | 477 |
///Snapshot feature. |
478 | 478 |
Node split(Node n, bool connect = true) { |
479 | 479 |
Node b = addNode(); |
480 | 480 |
nodes[b.id].first_out=nodes[n.id].first_out; |
481 | 481 |
nodes[n.id].first_out=-1; |
482 | 482 |
for(int i=nodes[b.id].first_out; i!=-1; i=arcs[i].next_out) { |
483 | 483 |
arcs[i].source=b.id; |
484 | 484 |
} |
485 | 485 |
if (connect) addArc(n,b); |
486 | 486 |
return b; |
487 | 487 |
} |
488 | 488 |
|
489 | 489 |
///Split an arc. |
490 | 490 |
|
491 | 491 |
///This function splits the given arc. First, a new node \c v is |
492 | 492 |
///added to the digraph, then the target node of the original arc |
493 | 493 |
///is set to \c v. Finally, an arc from \c v to the original target |
494 | 494 |
///is added. |
495 | 495 |
///\return The newly created node. |
496 | 496 |
/// |
497 | 497 |
///\note \c InArcIt iterators referencing the original arc are |
498 | 498 |
///invalidated. Other iterators remain valid. |
499 | 499 |
/// |
500 | 500 |
///\warning This functionality cannot be used together with the |
501 | 501 |
///Snapshot feature. |
502 | 502 |
Node split(Arc a) { |
503 | 503 |
Node v = addNode(); |
504 | 504 |
addArc(v,target(a)); |
505 | 505 |
changeTarget(a,v); |
506 | 506 |
return v; |
507 | 507 |
} |
508 | 508 |
|
509 | 509 |
///Clear the digraph. |
510 | 510 |
|
511 | 511 |
///This function erases all nodes and arcs from the digraph. |
512 | 512 |
/// |
513 | 513 |
void clear() { |
514 | 514 |
Parent::clear(); |
515 | 515 |
} |
516 | 516 |
|
517 | 517 |
/// Reserve memory for nodes. |
518 | 518 |
|
519 | 519 |
/// Using this function, it is possible to avoid superfluous memory |
520 | 520 |
/// allocation: if you know that the digraph you want to build will |
521 | 521 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
522 | 522 |
/// then it is worth reserving space for this amount before starting |
523 | 523 |
/// to build the digraph. |
524 | 524 |
/// \sa reserveArc() |
525 | 525 |
void reserveNode(int n) { nodes.reserve(n); }; |
526 | 526 |
|
527 | 527 |
/// Reserve memory for arcs. |
528 | 528 |
|
529 | 529 |
/// Using this function, it is possible to avoid superfluous memory |
530 | 530 |
/// allocation: if you know that the digraph you want to build will |
531 | 531 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
532 | 532 |
/// then it is worth reserving space for this amount before starting |
533 | 533 |
/// to build the digraph. |
534 | 534 |
/// \sa reserveNode() |
535 | 535 |
void reserveArc(int m) { arcs.reserve(m); }; |
536 | 536 |
|
537 | 537 |
/// \brief Class to make a snapshot of the digraph and restore |
538 | 538 |
/// it later. |
539 | 539 |
/// |
540 | 540 |
/// Class to make a snapshot of the digraph and restore it later. |
541 | 541 |
/// |
542 | 542 |
/// The newly added nodes and arcs can be removed using the |
543 | 543 |
/// restore() function. |
544 | 544 |
/// |
545 | 545 |
/// \note After a state is restored, you cannot restore a later state, |
546 | 546 |
/// i.e. you cannot add the removed nodes and arcs again using |
547 | 547 |
/// another Snapshot instance. |
548 | 548 |
/// |
549 | 549 |
/// \warning Node and arc deletions and other modifications (e.g. |
550 | 550 |
/// reversing, contracting, splitting arcs or nodes) cannot be |
551 | 551 |
/// restored. These events invalidate the snapshot. |
552 |
/// However the arcs and nodes that were added to the digraph after |
|
552 |
/// However, the arcs and nodes that were added to the digraph after |
|
553 | 553 |
/// making the current snapshot can be removed without invalidating it. |
554 | 554 |
class Snapshot { |
555 | 555 |
protected: |
556 | 556 |
|
557 | 557 |
typedef Parent::NodeNotifier NodeNotifier; |
558 | 558 |
|
559 | 559 |
class NodeObserverProxy : public NodeNotifier::ObserverBase { |
560 | 560 |
public: |
561 | 561 |
|
562 | 562 |
NodeObserverProxy(Snapshot& _snapshot) |
563 | 563 |
: snapshot(_snapshot) {} |
564 | 564 |
|
565 | 565 |
using NodeNotifier::ObserverBase::attach; |
566 | 566 |
using NodeNotifier::ObserverBase::detach; |
567 | 567 |
using NodeNotifier::ObserverBase::attached; |
568 | 568 |
|
569 | 569 |
protected: |
570 | 570 |
|
571 | 571 |
virtual void add(const Node& node) { |
572 | 572 |
snapshot.addNode(node); |
573 | 573 |
} |
574 | 574 |
virtual void add(const std::vector<Node>& nodes) { |
575 | 575 |
for (int i = nodes.size() - 1; i >= 0; ++i) { |
576 | 576 |
snapshot.addNode(nodes[i]); |
577 | 577 |
} |
578 | 578 |
} |
579 | 579 |
virtual void erase(const Node& node) { |
580 | 580 |
snapshot.eraseNode(node); |
581 | 581 |
} |
582 | 582 |
virtual void erase(const std::vector<Node>& nodes) { |
583 | 583 |
for (int i = 0; i < int(nodes.size()); ++i) { |
584 | 584 |
snapshot.eraseNode(nodes[i]); |
585 | 585 |
} |
586 | 586 |
} |
587 | 587 |
virtual void build() { |
588 | 588 |
Node node; |
589 | 589 |
std::vector<Node> nodes; |
590 | 590 |
for (notifier()->first(node); node != INVALID; |
591 | 591 |
notifier()->next(node)) { |
592 | 592 |
nodes.push_back(node); |
593 | 593 |
} |
594 | 594 |
for (int i = nodes.size() - 1; i >= 0; --i) { |
595 | 595 |
snapshot.addNode(nodes[i]); |
596 | 596 |
} |
597 | 597 |
} |
598 | 598 |
virtual void clear() { |
599 | 599 |
Node node; |
600 | 600 |
for (notifier()->first(node); node != INVALID; |
601 | 601 |
notifier()->next(node)) { |
602 | 602 |
snapshot.eraseNode(node); |
603 | 603 |
} |
604 | 604 |
} |
605 | 605 |
|
606 | 606 |
Snapshot& snapshot; |
607 | 607 |
}; |
608 | 608 |
|
609 | 609 |
class ArcObserverProxy : public ArcNotifier::ObserverBase { |
610 | 610 |
public: |
611 | 611 |
|
612 | 612 |
ArcObserverProxy(Snapshot& _snapshot) |
613 | 613 |
: snapshot(_snapshot) {} |
614 | 614 |
|
615 | 615 |
using ArcNotifier::ObserverBase::attach; |
616 | 616 |
using ArcNotifier::ObserverBase::detach; |
617 | 617 |
using ArcNotifier::ObserverBase::attached; |
618 | 618 |
|
619 | 619 |
protected: |
620 | 620 |
|
621 | 621 |
virtual void add(const Arc& arc) { |
622 | 622 |
snapshot.addArc(arc); |
623 | 623 |
} |
624 | 624 |
virtual void add(const std::vector<Arc>& arcs) { |
625 | 625 |
for (int i = arcs.size() - 1; i >= 0; ++i) { |
626 | 626 |
snapshot.addArc(arcs[i]); |
627 | 627 |
} |
628 | 628 |
} |
629 | 629 |
virtual void erase(const Arc& arc) { |
630 | 630 |
snapshot.eraseArc(arc); |
631 | 631 |
} |
632 | 632 |
virtual void erase(const std::vector<Arc>& arcs) { |
633 | 633 |
for (int i = 0; i < int(arcs.size()); ++i) { |
634 | 634 |
snapshot.eraseArc(arcs[i]); |
635 | 635 |
} |
636 | 636 |
} |
637 | 637 |
virtual void build() { |
638 | 638 |
Arc arc; |
639 | 639 |
std::vector<Arc> arcs; |
640 | 640 |
for (notifier()->first(arc); arc != INVALID; |
641 | 641 |
notifier()->next(arc)) { |
642 | 642 |
arcs.push_back(arc); |
643 | 643 |
} |
644 | 644 |
for (int i = arcs.size() - 1; i >= 0; --i) { |
645 | 645 |
snapshot.addArc(arcs[i]); |
646 | 646 |
} |
647 | 647 |
} |
648 | 648 |
virtual void clear() { |
649 | 649 |
Arc arc; |
650 | 650 |
for (notifier()->first(arc); arc != INVALID; |
651 | 651 |
notifier()->next(arc)) { |
652 | 652 |
snapshot.eraseArc(arc); |
653 | 653 |
} |
654 | 654 |
} |
655 | 655 |
|
656 | 656 |
Snapshot& snapshot; |
657 | 657 |
}; |
658 | 658 |
|
659 | 659 |
ListDigraph *digraph; |
660 | 660 |
|
661 | 661 |
NodeObserverProxy node_observer_proxy; |
662 | 662 |
ArcObserverProxy arc_observer_proxy; |
663 | 663 |
|
664 | 664 |
std::list<Node> added_nodes; |
665 | 665 |
std::list<Arc> added_arcs; |
666 | 666 |
|
667 | 667 |
|
668 | 668 |
void addNode(const Node& node) { |
669 | 669 |
added_nodes.push_front(node); |
670 | 670 |
} |
671 | 671 |
void eraseNode(const Node& node) { |
672 | 672 |
std::list<Node>::iterator it = |
673 | 673 |
std::find(added_nodes.begin(), added_nodes.end(), node); |
674 | 674 |
if (it == added_nodes.end()) { |
675 | 675 |
clear(); |
676 | 676 |
arc_observer_proxy.detach(); |
677 | 677 |
throw NodeNotifier::ImmediateDetach(); |
678 | 678 |
} else { |
679 | 679 |
added_nodes.erase(it); |
680 | 680 |
} |
681 | 681 |
} |
682 | 682 |
|
683 | 683 |
void addArc(const Arc& arc) { |
684 | 684 |
added_arcs.push_front(arc); |
685 | 685 |
} |
686 | 686 |
void eraseArc(const Arc& arc) { |
687 | 687 |
std::list<Arc>::iterator it = |
688 | 688 |
std::find(added_arcs.begin(), added_arcs.end(), arc); |
689 | 689 |
if (it == added_arcs.end()) { |
690 | 690 |
clear(); |
691 | 691 |
node_observer_proxy.detach(); |
692 | 692 |
throw ArcNotifier::ImmediateDetach(); |
693 | 693 |
} else { |
694 | 694 |
added_arcs.erase(it); |
695 | 695 |
} |
696 | 696 |
} |
697 | 697 |
|
698 | 698 |
void attach(ListDigraph &_digraph) { |
699 | 699 |
digraph = &_digraph; |
700 | 700 |
node_observer_proxy.attach(digraph->notifier(Node())); |
701 | 701 |
arc_observer_proxy.attach(digraph->notifier(Arc())); |
702 | 702 |
} |
703 | 703 |
|
704 | 704 |
void detach() { |
705 | 705 |
node_observer_proxy.detach(); |
706 | 706 |
arc_observer_proxy.detach(); |
707 | 707 |
} |
708 | 708 |
|
709 | 709 |
bool attached() const { |
710 | 710 |
return node_observer_proxy.attached(); |
711 | 711 |
} |
712 | 712 |
|
713 | 713 |
void clear() { |
714 | 714 |
added_nodes.clear(); |
715 | 715 |
added_arcs.clear(); |
716 | 716 |
} |
717 | 717 |
|
718 | 718 |
public: |
719 | 719 |
|
720 | 720 |
/// \brief Default constructor. |
721 | 721 |
/// |
722 | 722 |
/// Default constructor. |
723 | 723 |
/// You have to call save() to actually make a snapshot. |
724 | 724 |
Snapshot() |
725 | 725 |
: digraph(0), node_observer_proxy(*this), |
726 | 726 |
arc_observer_proxy(*this) {} |
727 | 727 |
|
728 | 728 |
/// \brief Constructor that immediately makes a snapshot. |
729 | 729 |
/// |
730 | 730 |
/// This constructor immediately makes a snapshot of the given digraph. |
731 | 731 |
Snapshot(ListDigraph &gr) |
732 | 732 |
: node_observer_proxy(*this), |
733 | 733 |
arc_observer_proxy(*this) { |
734 | 734 |
attach(gr); |
735 | 735 |
} |
736 | 736 |
|
737 | 737 |
/// \brief Make a snapshot. |
738 | 738 |
/// |
739 | 739 |
/// This function makes a snapshot of the given digraph. |
740 | 740 |
/// It can be called more than once. In case of a repeated |
741 | 741 |
/// call, the previous snapshot gets lost. |
742 | 742 |
void save(ListDigraph &gr) { |
743 | 743 |
if (attached()) { |
744 | 744 |
detach(); |
... | ... |
@@ -1078,469 +1078,469 @@ |
1078 | 1078 |
first_free_node = n; |
1079 | 1079 |
nodes[n].prev = -2; |
1080 | 1080 |
} |
1081 | 1081 |
|
1082 | 1082 |
void erase(const Edge& edge) { |
1083 | 1083 |
int n = edge.id * 2; |
1084 | 1084 |
|
1085 | 1085 |
if (arcs[n].next_out != -1) { |
1086 | 1086 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
1087 | 1087 |
} |
1088 | 1088 |
|
1089 | 1089 |
if (arcs[n].prev_out != -1) { |
1090 | 1090 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
1091 | 1091 |
} else { |
1092 | 1092 |
nodes[arcs[n | 1].target].first_out = arcs[n].next_out; |
1093 | 1093 |
} |
1094 | 1094 |
|
1095 | 1095 |
if (arcs[n | 1].next_out != -1) { |
1096 | 1096 |
arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
1097 | 1097 |
} |
1098 | 1098 |
|
1099 | 1099 |
if (arcs[n | 1].prev_out != -1) { |
1100 | 1100 |
arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
1101 | 1101 |
} else { |
1102 | 1102 |
nodes[arcs[n].target].first_out = arcs[n | 1].next_out; |
1103 | 1103 |
} |
1104 | 1104 |
|
1105 | 1105 |
arcs[n].next_out = first_free_arc; |
1106 | 1106 |
first_free_arc = n; |
1107 | 1107 |
arcs[n].prev_out = -2; |
1108 | 1108 |
arcs[n | 1].prev_out = -2; |
1109 | 1109 |
|
1110 | 1110 |
} |
1111 | 1111 |
|
1112 | 1112 |
void clear() { |
1113 | 1113 |
arcs.clear(); |
1114 | 1114 |
nodes.clear(); |
1115 | 1115 |
first_node = first_free_node = first_free_arc = -1; |
1116 | 1116 |
} |
1117 | 1117 |
|
1118 | 1118 |
protected: |
1119 | 1119 |
|
1120 | 1120 |
void changeV(Edge e, Node n) { |
1121 | 1121 |
if(arcs[2 * e.id].next_out != -1) { |
1122 | 1122 |
arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out; |
1123 | 1123 |
} |
1124 | 1124 |
if(arcs[2 * e.id].prev_out != -1) { |
1125 | 1125 |
arcs[arcs[2 * e.id].prev_out].next_out = |
1126 | 1126 |
arcs[2 * e.id].next_out; |
1127 | 1127 |
} else { |
1128 | 1128 |
nodes[arcs[(2 * e.id) | 1].target].first_out = |
1129 | 1129 |
arcs[2 * e.id].next_out; |
1130 | 1130 |
} |
1131 | 1131 |
|
1132 | 1132 |
if (nodes[n.id].first_out != -1) { |
1133 | 1133 |
arcs[nodes[n.id].first_out].prev_out = 2 * e.id; |
1134 | 1134 |
} |
1135 | 1135 |
arcs[(2 * e.id) | 1].target = n.id; |
1136 | 1136 |
arcs[2 * e.id].prev_out = -1; |
1137 | 1137 |
arcs[2 * e.id].next_out = nodes[n.id].first_out; |
1138 | 1138 |
nodes[n.id].first_out = 2 * e.id; |
1139 | 1139 |
} |
1140 | 1140 |
|
1141 | 1141 |
void changeU(Edge e, Node n) { |
1142 | 1142 |
if(arcs[(2 * e.id) | 1].next_out != -1) { |
1143 | 1143 |
arcs[arcs[(2 * e.id) | 1].next_out].prev_out = |
1144 | 1144 |
arcs[(2 * e.id) | 1].prev_out; |
1145 | 1145 |
} |
1146 | 1146 |
if(arcs[(2 * e.id) | 1].prev_out != -1) { |
1147 | 1147 |
arcs[arcs[(2 * e.id) | 1].prev_out].next_out = |
1148 | 1148 |
arcs[(2 * e.id) | 1].next_out; |
1149 | 1149 |
} else { |
1150 | 1150 |
nodes[arcs[2 * e.id].target].first_out = |
1151 | 1151 |
arcs[(2 * e.id) | 1].next_out; |
1152 | 1152 |
} |
1153 | 1153 |
|
1154 | 1154 |
if (nodes[n.id].first_out != -1) { |
1155 | 1155 |
arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
1156 | 1156 |
} |
1157 | 1157 |
arcs[2 * e.id].target = n.id; |
1158 | 1158 |
arcs[(2 * e.id) | 1].prev_out = -1; |
1159 | 1159 |
arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out; |
1160 | 1160 |
nodes[n.id].first_out = ((2 * e.id) | 1); |
1161 | 1161 |
} |
1162 | 1162 |
|
1163 | 1163 |
}; |
1164 | 1164 |
|
1165 | 1165 |
typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
1166 | 1166 |
|
1167 | 1167 |
|
1168 | 1168 |
/// \addtogroup graphs |
1169 | 1169 |
/// @{ |
1170 | 1170 |
|
1171 | 1171 |
///A general undirected graph structure. |
1172 | 1172 |
|
1173 | 1173 |
///\ref ListGraph is a versatile and fast undirected graph |
1174 | 1174 |
///implementation based on linked lists that are stored in |
1175 | 1175 |
///\c std::vector structures. |
1176 | 1176 |
/// |
1177 | 1177 |
///This type fully conforms to the \ref concepts::Graph "Graph concept" |
1178 | 1178 |
///and it also provides several useful additional functionalities. |
1179 | 1179 |
///Most of its member functions and nested classes are documented |
1180 | 1180 |
///only in the concept class. |
1181 | 1181 |
/// |
1182 | 1182 |
///\sa concepts::Graph |
1183 | 1183 |
///\sa ListDigraph |
1184 | 1184 |
class ListGraph : public ExtendedListGraphBase { |
1185 | 1185 |
typedef ExtendedListGraphBase Parent; |
1186 | 1186 |
|
1187 | 1187 |
private: |
1188 | 1188 |
/// Graphs are \e not copy constructible. Use GraphCopy instead. |
1189 | 1189 |
ListGraph(const ListGraph &) :ExtendedListGraphBase() {}; |
1190 | 1190 |
/// \brief Assignment of a graph to another one is \e not allowed. |
1191 | 1191 |
/// Use GraphCopy instead. |
1192 | 1192 |
void operator=(const ListGraph &) {} |
1193 | 1193 |
public: |
1194 | 1194 |
/// Constructor |
1195 | 1195 |
|
1196 | 1196 |
/// Constructor. |
1197 | 1197 |
/// |
1198 | 1198 |
ListGraph() {} |
1199 | 1199 |
|
1200 | 1200 |
typedef Parent::OutArcIt IncEdgeIt; |
1201 | 1201 |
|
1202 | 1202 |
/// \brief Add a new node to the graph. |
1203 | 1203 |
/// |
1204 | 1204 |
/// This function adds a new node to the graph. |
1205 | 1205 |
/// \return The new node. |
1206 | 1206 |
Node addNode() { return Parent::addNode(); } |
1207 | 1207 |
|
1208 | 1208 |
/// \brief Add a new edge to the graph. |
1209 | 1209 |
/// |
1210 | 1210 |
/// This function adds a new edge to the graph between nodes |
1211 | 1211 |
/// \c u and \c v with inherent orientation from node \c u to |
1212 | 1212 |
/// node \c v. |
1213 | 1213 |
/// \return The new edge. |
1214 | 1214 |
Edge addEdge(Node u, Node v) { |
1215 | 1215 |
return Parent::addEdge(u, v); |
1216 | 1216 |
} |
1217 | 1217 |
|
1218 | 1218 |
///\brief Erase a node from the graph. |
1219 | 1219 |
/// |
1220 | 1220 |
/// This function erases the given node from the graph. |
1221 | 1221 |
void erase(Node n) { Parent::erase(n); } |
1222 | 1222 |
|
1223 | 1223 |
///\brief Erase an edge from the graph. |
1224 | 1224 |
/// |
1225 | 1225 |
/// This function erases the given edge from the graph. |
1226 | 1226 |
void erase(Edge e) { Parent::erase(e); } |
1227 | 1227 |
/// Node validity check |
1228 | 1228 |
|
1229 | 1229 |
/// This function gives back \c true if the given node is valid, |
1230 | 1230 |
/// i.e. it is a real node of the graph. |
1231 | 1231 |
/// |
1232 | 1232 |
/// \warning A removed node could become valid again if new nodes are |
1233 | 1233 |
/// added to the graph. |
1234 | 1234 |
bool valid(Node n) const { return Parent::valid(n); } |
1235 | 1235 |
/// Edge validity check |
1236 | 1236 |
|
1237 | 1237 |
/// This function gives back \c true if the given edge is valid, |
1238 | 1238 |
/// i.e. it is a real edge of the graph. |
1239 | 1239 |
/// |
1240 | 1240 |
/// \warning A removed edge could become valid again if new edges are |
1241 | 1241 |
/// added to the graph. |
1242 | 1242 |
bool valid(Edge e) const { return Parent::valid(e); } |
1243 | 1243 |
/// Arc validity check |
1244 | 1244 |
|
1245 | 1245 |
/// This function gives back \c true if the given arc is valid, |
1246 | 1246 |
/// i.e. it is a real arc of the graph. |
1247 | 1247 |
/// |
1248 | 1248 |
/// \warning A removed arc could become valid again if new edges are |
1249 | 1249 |
/// added to the graph. |
1250 | 1250 |
bool valid(Arc a) const { return Parent::valid(a); } |
1251 | 1251 |
|
1252 | 1252 |
/// \brief Change the first node of an edge. |
1253 | 1253 |
/// |
1254 | 1254 |
/// This function changes the first node of the given edge \c e to \c n. |
1255 | 1255 |
/// |
1256 | 1256 |
///\note \c EdgeIt and \c ArcIt iterators referencing the |
1257 | 1257 |
///changed edge are invalidated and all other iterators whose |
1258 | 1258 |
///base node is the changed node are also invalidated. |
1259 | 1259 |
/// |
1260 | 1260 |
///\warning This functionality cannot be used together with the |
1261 | 1261 |
///Snapshot feature. |
1262 | 1262 |
void changeU(Edge e, Node n) { |
1263 | 1263 |
Parent::changeU(e,n); |
1264 | 1264 |
} |
1265 | 1265 |
/// \brief Change the second node of an edge. |
1266 | 1266 |
/// |
1267 | 1267 |
/// This function changes the second node of the given edge \c e to \c n. |
1268 | 1268 |
/// |
1269 | 1269 |
///\note \c EdgeIt iterators referencing the changed edge remain |
1270 |
///valid, |
|
1270 |
///valid, but \c ArcIt iterators referencing the changed edge and |
|
1271 | 1271 |
///all other iterators whose base node is the changed node are also |
1272 | 1272 |
///invalidated. |
1273 | 1273 |
/// |
1274 | 1274 |
///\warning This functionality cannot be used together with the |
1275 | 1275 |
///Snapshot feature. |
1276 | 1276 |
void changeV(Edge e, Node n) { |
1277 | 1277 |
Parent::changeV(e,n); |
1278 | 1278 |
} |
1279 | 1279 |
|
1280 | 1280 |
/// \brief Contract two nodes. |
1281 | 1281 |
/// |
1282 | 1282 |
/// This function contracts the given two nodes. |
1283 | 1283 |
/// Node \c b is removed, but instead of deleting |
1284 | 1284 |
/// its incident edges, they are joined to node \c a. |
1285 | 1285 |
/// If the last parameter \c r is \c true (this is the default value), |
1286 | 1286 |
/// then the newly created loops are removed. |
1287 | 1287 |
/// |
1288 | 1288 |
/// \note The moved edges are joined to node \c a using changeU() |
1289 | 1289 |
/// or changeV(), thus all edge and arc iterators whose base node is |
1290 | 1290 |
/// \c b are invalidated. |
1291 | 1291 |
/// Moreover all iterators referencing node \c b or the removed |
1292 | 1292 |
/// loops are also invalidated. Other iterators remain valid. |
1293 | 1293 |
/// |
1294 | 1294 |
///\warning This functionality cannot be used together with the |
1295 | 1295 |
///Snapshot feature. |
1296 | 1296 |
void contract(Node a, Node b, bool r = true) { |
1297 | 1297 |
for(IncEdgeIt e(*this, b); e!=INVALID;) { |
1298 | 1298 |
IncEdgeIt f = e; ++f; |
1299 | 1299 |
if (r && runningNode(e) == a) { |
1300 | 1300 |
erase(e); |
1301 | 1301 |
} else if (u(e) == b) { |
1302 | 1302 |
changeU(e, a); |
1303 | 1303 |
} else { |
1304 | 1304 |
changeV(e, a); |
1305 | 1305 |
} |
1306 | 1306 |
e = f; |
1307 | 1307 |
} |
1308 | 1308 |
erase(b); |
1309 | 1309 |
} |
1310 | 1310 |
|
1311 | 1311 |
///Clear the graph. |
1312 | 1312 |
|
1313 | 1313 |
///This function erases all nodes and arcs from the graph. |
1314 | 1314 |
/// |
1315 | 1315 |
void clear() { |
1316 | 1316 |
Parent::clear(); |
1317 | 1317 |
} |
1318 | 1318 |
|
1319 | 1319 |
/// Reserve memory for nodes. |
1320 | 1320 |
|
1321 | 1321 |
/// Using this function, it is possible to avoid superfluous memory |
1322 | 1322 |
/// allocation: if you know that the graph you want to build will |
1323 | 1323 |
/// be large (e.g. it will contain millions of nodes and/or edges), |
1324 | 1324 |
/// then it is worth reserving space for this amount before starting |
1325 | 1325 |
/// to build the graph. |
1326 | 1326 |
/// \sa reserveEdge() |
1327 | 1327 |
void reserveNode(int n) { nodes.reserve(n); }; |
1328 | 1328 |
|
1329 | 1329 |
/// Reserve memory for edges. |
1330 | 1330 |
|
1331 | 1331 |
/// Using this function, it is possible to avoid superfluous memory |
1332 | 1332 |
/// allocation: if you know that the graph you want to build will |
1333 | 1333 |
/// be large (e.g. it will contain millions of nodes and/or edges), |
1334 | 1334 |
/// then it is worth reserving space for this amount before starting |
1335 | 1335 |
/// to build the graph. |
1336 | 1336 |
/// \sa reserveNode() |
1337 | 1337 |
void reserveEdge(int m) { arcs.reserve(2 * m); }; |
1338 | 1338 |
|
1339 | 1339 |
/// \brief Class to make a snapshot of the graph and restore |
1340 | 1340 |
/// it later. |
1341 | 1341 |
/// |
1342 | 1342 |
/// Class to make a snapshot of the graph and restore it later. |
1343 | 1343 |
/// |
1344 | 1344 |
/// The newly added nodes and edges can be removed |
1345 | 1345 |
/// using the restore() function. |
1346 | 1346 |
/// |
1347 | 1347 |
/// \note After a state is restored, you cannot restore a later state, |
1348 | 1348 |
/// i.e. you cannot add the removed nodes and edges again using |
1349 | 1349 |
/// another Snapshot instance. |
1350 | 1350 |
/// |
1351 | 1351 |
/// \warning Node and edge deletions and other modifications |
1352 | 1352 |
/// (e.g. changing the end-nodes of edges or contracting nodes) |
1353 | 1353 |
/// cannot be restored. These events invalidate the snapshot. |
1354 |
/// However the edges and nodes that were added to the graph after |
|
1354 |
/// However, the edges and nodes that were added to the graph after |
|
1355 | 1355 |
/// making the current snapshot can be removed without invalidating it. |
1356 | 1356 |
class Snapshot { |
1357 | 1357 |
protected: |
1358 | 1358 |
|
1359 | 1359 |
typedef Parent::NodeNotifier NodeNotifier; |
1360 | 1360 |
|
1361 | 1361 |
class NodeObserverProxy : public NodeNotifier::ObserverBase { |
1362 | 1362 |
public: |
1363 | 1363 |
|
1364 | 1364 |
NodeObserverProxy(Snapshot& _snapshot) |
1365 | 1365 |
: snapshot(_snapshot) {} |
1366 | 1366 |
|
1367 | 1367 |
using NodeNotifier::ObserverBase::attach; |
1368 | 1368 |
using NodeNotifier::ObserverBase::detach; |
1369 | 1369 |
using NodeNotifier::ObserverBase::attached; |
1370 | 1370 |
|
1371 | 1371 |
protected: |
1372 | 1372 |
|
1373 | 1373 |
virtual void add(const Node& node) { |
1374 | 1374 |
snapshot.addNode(node); |
1375 | 1375 |
} |
1376 | 1376 |
virtual void add(const std::vector<Node>& nodes) { |
1377 | 1377 |
for (int i = nodes.size() - 1; i >= 0; ++i) { |
1378 | 1378 |
snapshot.addNode(nodes[i]); |
1379 | 1379 |
} |
1380 | 1380 |
} |
1381 | 1381 |
virtual void erase(const Node& node) { |
1382 | 1382 |
snapshot.eraseNode(node); |
1383 | 1383 |
} |
1384 | 1384 |
virtual void erase(const std::vector<Node>& nodes) { |
1385 | 1385 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1386 | 1386 |
snapshot.eraseNode(nodes[i]); |
1387 | 1387 |
} |
1388 | 1388 |
} |
1389 | 1389 |
virtual void build() { |
1390 | 1390 |
Node node; |
1391 | 1391 |
std::vector<Node> nodes; |
1392 | 1392 |
for (notifier()->first(node); node != INVALID; |
1393 | 1393 |
notifier()->next(node)) { |
1394 | 1394 |
nodes.push_back(node); |
1395 | 1395 |
} |
1396 | 1396 |
for (int i = nodes.size() - 1; i >= 0; --i) { |
1397 | 1397 |
snapshot.addNode(nodes[i]); |
1398 | 1398 |
} |
1399 | 1399 |
} |
1400 | 1400 |
virtual void clear() { |
1401 | 1401 |
Node node; |
1402 | 1402 |
for (notifier()->first(node); node != INVALID; |
1403 | 1403 |
notifier()->next(node)) { |
1404 | 1404 |
snapshot.eraseNode(node); |
1405 | 1405 |
} |
1406 | 1406 |
} |
1407 | 1407 |
|
1408 | 1408 |
Snapshot& snapshot; |
1409 | 1409 |
}; |
1410 | 1410 |
|
1411 | 1411 |
class EdgeObserverProxy : public EdgeNotifier::ObserverBase { |
1412 | 1412 |
public: |
1413 | 1413 |
|
1414 | 1414 |
EdgeObserverProxy(Snapshot& _snapshot) |
1415 | 1415 |
: snapshot(_snapshot) {} |
1416 | 1416 |
|
1417 | 1417 |
using EdgeNotifier::ObserverBase::attach; |
1418 | 1418 |
using EdgeNotifier::ObserverBase::detach; |
1419 | 1419 |
using EdgeNotifier::ObserverBase::attached; |
1420 | 1420 |
|
1421 | 1421 |
protected: |
1422 | 1422 |
|
1423 | 1423 |
virtual void add(const Edge& edge) { |
1424 | 1424 |
snapshot.addEdge(edge); |
1425 | 1425 |
} |
1426 | 1426 |
virtual void add(const std::vector<Edge>& edges) { |
1427 | 1427 |
for (int i = edges.size() - 1; i >= 0; ++i) { |
1428 | 1428 |
snapshot.addEdge(edges[i]); |
1429 | 1429 |
} |
1430 | 1430 |
} |
1431 | 1431 |
virtual void erase(const Edge& edge) { |
1432 | 1432 |
snapshot.eraseEdge(edge); |
1433 | 1433 |
} |
1434 | 1434 |
virtual void erase(const std::vector<Edge>& edges) { |
1435 | 1435 |
for (int i = 0; i < int(edges.size()); ++i) { |
1436 | 1436 |
snapshot.eraseEdge(edges[i]); |
1437 | 1437 |
} |
1438 | 1438 |
} |
1439 | 1439 |
virtual void build() { |
1440 | 1440 |
Edge edge; |
1441 | 1441 |
std::vector<Edge> edges; |
1442 | 1442 |
for (notifier()->first(edge); edge != INVALID; |
1443 | 1443 |
notifier()->next(edge)) { |
1444 | 1444 |
edges.push_back(edge); |
1445 | 1445 |
} |
1446 | 1446 |
for (int i = edges.size() - 1; i >= 0; --i) { |
1447 | 1447 |
snapshot.addEdge(edges[i]); |
1448 | 1448 |
} |
1449 | 1449 |
} |
1450 | 1450 |
virtual void clear() { |
1451 | 1451 |
Edge edge; |
1452 | 1452 |
for (notifier()->first(edge); edge != INVALID; |
1453 | 1453 |
notifier()->next(edge)) { |
1454 | 1454 |
snapshot.eraseEdge(edge); |
1455 | 1455 |
} |
1456 | 1456 |
} |
1457 | 1457 |
|
1458 | 1458 |
Snapshot& snapshot; |
1459 | 1459 |
}; |
1460 | 1460 |
|
1461 | 1461 |
ListGraph *graph; |
1462 | 1462 |
|
1463 | 1463 |
NodeObserverProxy node_observer_proxy; |
1464 | 1464 |
EdgeObserverProxy edge_observer_proxy; |
1465 | 1465 |
|
1466 | 1466 |
std::list<Node> added_nodes; |
1467 | 1467 |
std::list<Edge> added_edges; |
1468 | 1468 |
|
1469 | 1469 |
|
1470 | 1470 |
void addNode(const Node& node) { |
1471 | 1471 |
added_nodes.push_front(node); |
1472 | 1472 |
} |
1473 | 1473 |
void eraseNode(const Node& node) { |
1474 | 1474 |
std::list<Node>::iterator it = |
1475 | 1475 |
std::find(added_nodes.begin(), added_nodes.end(), node); |
1476 | 1476 |
if (it == added_nodes.end()) { |
1477 | 1477 |
clear(); |
1478 | 1478 |
edge_observer_proxy.detach(); |
1479 | 1479 |
throw NodeNotifier::ImmediateDetach(); |
1480 | 1480 |
} else { |
1481 | 1481 |
added_nodes.erase(it); |
1482 | 1482 |
} |
1483 | 1483 |
} |
1484 | 1484 |
|
1485 | 1485 |
void addEdge(const Edge& edge) { |
1486 | 1486 |
added_edges.push_front(edge); |
1487 | 1487 |
} |
1488 | 1488 |
void eraseEdge(const Edge& edge) { |
1489 | 1489 |
std::list<Edge>::iterator it = |
1490 | 1490 |
std::find(added_edges.begin(), added_edges.end(), edge); |
1491 | 1491 |
if (it == added_edges.end()) { |
1492 | 1492 |
clear(); |
1493 | 1493 |
node_observer_proxy.detach(); |
1494 | 1494 |
throw EdgeNotifier::ImmediateDetach(); |
1495 | 1495 |
} else { |
1496 | 1496 |
added_edges.erase(it); |
1497 | 1497 |
} |
1498 | 1498 |
} |
1499 | 1499 |
|
1500 | 1500 |
void attach(ListGraph &_graph) { |
1501 | 1501 |
graph = &_graph; |
1502 | 1502 |
node_observer_proxy.attach(graph->notifier(Node())); |
1503 | 1503 |
edge_observer_proxy.attach(graph->notifier(Edge())); |
1504 | 1504 |
} |
1505 | 1505 |
|
1506 | 1506 |
void detach() { |
1507 | 1507 |
node_observer_proxy.detach(); |
1508 | 1508 |
edge_observer_proxy.detach(); |
1509 | 1509 |
} |
1510 | 1510 |
|
1511 | 1511 |
bool attached() const { |
1512 | 1512 |
return node_observer_proxy.attached(); |
1513 | 1513 |
} |
1514 | 1514 |
|
1515 | 1515 |
void clear() { |
1516 | 1516 |
added_nodes.clear(); |
1517 | 1517 |
added_edges.clear(); |
1518 | 1518 |
} |
1519 | 1519 |
|
1520 | 1520 |
public: |
1521 | 1521 |
|
1522 | 1522 |
/// \brief Default constructor. |
1523 | 1523 |
/// |
1524 | 1524 |
/// Default constructor. |
1525 | 1525 |
/// You have to call save() to actually make a snapshot. |
1526 | 1526 |
Snapshot() |
1527 | 1527 |
: graph(0), node_observer_proxy(*this), |
1528 | 1528 |
edge_observer_proxy(*this) {} |
1529 | 1529 |
|
1530 | 1530 |
/// \brief Constructor that immediately makes a snapshot. |
1531 | 1531 |
/// |
1532 | 1532 |
/// This constructor immediately makes a snapshot of the given graph. |
1533 | 1533 |
Snapshot(ListGraph &gr) |
1534 | 1534 |
: node_observer_proxy(*this), |
1535 | 1535 |
edge_observer_proxy(*this) { |
1536 | 1536 |
attach(gr); |
1537 | 1537 |
} |
1538 | 1538 |
|
1539 | 1539 |
/// \brief Make a snapshot. |
1540 | 1540 |
/// |
1541 | 1541 |
/// This function makes a snapshot of the given graph. |
1542 | 1542 |
/// It can be called more than once. In case of a repeated |
1543 | 1543 |
/// call, the previous snapshot gets lost. |
1544 | 1544 |
void save(ListGraph &gr) { |
1545 | 1545 |
if (attached()) { |
1546 | 1546 |
detach(); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_LP_BASE_H |
20 | 20 |
#define LEMON_LP_BASE_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<vector> |
24 | 24 |
#include<map> |
25 | 25 |
#include<limits> |
26 | 26 |
#include<lemon/math.h> |
27 | 27 |
|
28 | 28 |
#include<lemon/error.h> |
29 | 29 |
#include<lemon/assert.h> |
30 | 30 |
|
31 | 31 |
#include<lemon/core.h> |
32 | 32 |
#include<lemon/bits/solver_bits.h> |
33 | 33 |
|
34 | 34 |
///\file |
35 | 35 |
///\brief The interface of the LP solver interface. |
36 | 36 |
///\ingroup lp_group |
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
///Common base class for LP and MIP solvers |
40 | 40 |
|
41 | 41 |
///Usually this class is not used directly, please use one of the concrete |
42 | 42 |
///implementations of the solver interface. |
43 | 43 |
///\ingroup lp_group |
44 | 44 |
class LpBase { |
45 | 45 |
|
46 | 46 |
protected: |
47 | 47 |
|
48 | 48 |
_solver_bits::VarIndex rows; |
49 | 49 |
_solver_bits::VarIndex cols; |
50 | 50 |
|
51 | 51 |
public: |
52 | 52 |
|
53 | 53 |
///Possible outcomes of an LP solving procedure |
54 | 54 |
enum SolveExitStatus { |
55 | 55 |
/// = 0. It means that the problem has been successfully solved: either |
56 | 56 |
///an optimal solution has been found or infeasibility/unboundedness |
57 | 57 |
///has been proved. |
58 | 58 |
SOLVED = 0, |
59 | 59 |
/// = 1. Any other case (including the case when some user specified |
60 | 60 |
///limit has been exceeded). |
61 | 61 |
UNSOLVED = 1 |
62 | 62 |
}; |
63 | 63 |
|
64 | 64 |
///Direction of the optimization |
65 | 65 |
enum Sense { |
66 | 66 |
/// Minimization |
67 | 67 |
MIN, |
68 | 68 |
/// Maximization |
69 | 69 |
MAX |
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
///Enum for \c messageLevel() parameter |
73 | 73 |
enum MessageLevel { |
74 | 74 |
/// No output (default value). |
75 | 75 |
MESSAGE_NOTHING, |
76 | 76 |
/// Error messages only. |
77 | 77 |
MESSAGE_ERROR, |
78 | 78 |
/// Warnings. |
79 | 79 |
MESSAGE_WARNING, |
80 | 80 |
/// Normal output. |
81 | 81 |
MESSAGE_NORMAL, |
82 | 82 |
/// Verbose output. |
83 | 83 |
MESSAGE_VERBOSE |
84 | 84 |
}; |
85 | 85 |
|
86 | 86 |
|
87 | 87 |
///The floating point type used by the solver |
88 | 88 |
typedef double Value; |
89 | 89 |
///The infinity constant |
90 | 90 |
static const Value INF; |
91 | 91 |
///The not a number constant |
92 | 92 |
static const Value NaN; |
93 | 93 |
|
94 | 94 |
friend class Col; |
95 | 95 |
friend class ColIt; |
96 | 96 |
friend class Row; |
97 | 97 |
friend class RowIt; |
98 | 98 |
|
99 | 99 |
///Refer to a column of the LP. |
100 | 100 |
|
101 | 101 |
///This type is used to refer to a column of the LP. |
102 | 102 |
/// |
103 | 103 |
///Its value remains valid and correct even after the addition or erase of |
104 | 104 |
///other columns. |
105 | 105 |
/// |
106 | 106 |
///\note This class is similar to other Item types in LEMON, like |
107 | 107 |
///Node and Arc types in digraph. |
108 | 108 |
class Col { |
109 | 109 |
friend class LpBase; |
110 | 110 |
protected: |
111 | 111 |
int _id; |
112 | 112 |
explicit Col(int id) : _id(id) {} |
113 | 113 |
public: |
114 | 114 |
typedef Value ExprValue; |
115 | 115 |
typedef True LpCol; |
116 | 116 |
/// Default constructor |
117 | 117 |
|
118 | 118 |
/// \warning The default constructor sets the Col to an |
119 | 119 |
/// undefined value. |
120 | 120 |
Col() {} |
121 | 121 |
/// Invalid constructor \& conversion. |
122 | 122 |
|
123 | 123 |
/// This constructor initializes the Col to be invalid. |
124 | 124 |
/// \sa Invalid for more details. |
125 | 125 |
Col(const Invalid&) : _id(-1) {} |
126 | 126 |
/// Equality operator |
127 | 127 |
|
128 | 128 |
/// Two \ref Col "Col"s are equal if and only if they point to |
129 | 129 |
/// the same LP column or both are invalid. |
130 | 130 |
bool operator==(Col c) const {return _id == c._id;} |
131 | 131 |
/// Inequality operator |
132 | 132 |
|
133 | 133 |
/// \sa operator==(Col c) |
134 | 134 |
/// |
135 | 135 |
bool operator!=(Col c) const {return _id != c._id;} |
136 | 136 |
/// Artificial ordering operator. |
137 | 137 |
|
138 | 138 |
/// To allow the use of this object in std::map or similar |
139 | 139 |
/// associative container we require this. |
140 | 140 |
/// |
141 | 141 |
/// \note This operator only have to define some strict ordering of |
142 | 142 |
/// the items; this order has nothing to do with the iteration |
143 | 143 |
/// ordering of the items. |
144 | 144 |
bool operator<(Col c) const {return _id < c._id;} |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
///Iterator for iterate over the columns of an LP problem |
148 | 148 |
|
149 |
/// Its usage is quite simple, for example you can count the number |
|
149 |
/// Its usage is quite simple, for example, you can count the number |
|
150 | 150 |
/// of columns in an LP \c lp: |
151 | 151 |
///\code |
152 | 152 |
/// int count=0; |
153 | 153 |
/// for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count; |
154 | 154 |
///\endcode |
155 | 155 |
class ColIt : public Col { |
156 | 156 |
const LpBase *_solver; |
157 | 157 |
public: |
158 | 158 |
/// Default constructor |
159 | 159 |
|
160 | 160 |
/// \warning The default constructor sets the iterator |
161 | 161 |
/// to an undefined value. |
162 | 162 |
ColIt() {} |
163 | 163 |
/// Sets the iterator to the first Col |
164 | 164 |
|
165 | 165 |
/// Sets the iterator to the first Col. |
166 | 166 |
/// |
167 | 167 |
ColIt(const LpBase &solver) : _solver(&solver) |
168 | 168 |
{ |
169 | 169 |
_solver->cols.firstItem(_id); |
170 | 170 |
} |
171 | 171 |
/// Invalid constructor \& conversion |
172 | 172 |
|
173 | 173 |
/// Initialize the iterator to be invalid. |
174 | 174 |
/// \sa Invalid for more details. |
175 | 175 |
ColIt(const Invalid&) : Col(INVALID) {} |
176 | 176 |
/// Next column |
177 | 177 |
|
178 | 178 |
/// Assign the iterator to the next column. |
179 | 179 |
/// |
180 | 180 |
ColIt &operator++() |
181 | 181 |
{ |
182 | 182 |
_solver->cols.nextItem(_id); |
183 | 183 |
return *this; |
184 | 184 |
} |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
/// \brief Returns the ID of the column. |
188 | 188 |
static int id(const Col& col) { return col._id; } |
189 | 189 |
/// \brief Returns the column with the given ID. |
190 | 190 |
/// |
191 | 191 |
/// \pre The argument should be a valid column ID in the LP problem. |
192 | 192 |
static Col colFromId(int id) { return Col(id); } |
193 | 193 |
|
194 | 194 |
///Refer to a row of the LP. |
195 | 195 |
|
196 | 196 |
///This type is used to refer to a row of the LP. |
197 | 197 |
/// |
198 | 198 |
///Its value remains valid and correct even after the addition or erase of |
199 | 199 |
///other rows. |
200 | 200 |
/// |
201 | 201 |
///\note This class is similar to other Item types in LEMON, like |
202 | 202 |
///Node and Arc types in digraph. |
203 | 203 |
class Row { |
204 | 204 |
friend class LpBase; |
205 | 205 |
protected: |
206 | 206 |
int _id; |
207 | 207 |
explicit Row(int id) : _id(id) {} |
208 | 208 |
public: |
209 | 209 |
typedef Value ExprValue; |
210 | 210 |
typedef True LpRow; |
211 | 211 |
/// Default constructor |
212 | 212 |
|
213 | 213 |
/// \warning The default constructor sets the Row to an |
214 | 214 |
/// undefined value. |
215 | 215 |
Row() {} |
216 | 216 |
/// Invalid constructor \& conversion. |
217 | 217 |
|
218 | 218 |
/// This constructor initializes the Row to be invalid. |
219 | 219 |
/// \sa Invalid for more details. |
220 | 220 |
Row(const Invalid&) : _id(-1) {} |
221 | 221 |
/// Equality operator |
222 | 222 |
|
223 | 223 |
/// Two \ref Row "Row"s are equal if and only if they point to |
224 | 224 |
/// the same LP row or both are invalid. |
225 | 225 |
bool operator==(Row r) const {return _id == r._id;} |
226 | 226 |
/// Inequality operator |
227 | 227 |
|
228 | 228 |
/// \sa operator==(Row r) |
229 | 229 |
/// |
230 | 230 |
bool operator!=(Row r) const {return _id != r._id;} |
231 | 231 |
/// Artificial ordering operator. |
232 | 232 |
|
233 | 233 |
/// To allow the use of this object in std::map or similar |
234 | 234 |
/// associative container we require this. |
235 | 235 |
/// |
236 | 236 |
/// \note This operator only have to define some strict ordering of |
237 | 237 |
/// the items; this order has nothing to do with the iteration |
238 | 238 |
/// ordering of the items. |
239 | 239 |
bool operator<(Row r) const {return _id < r._id;} |
240 | 240 |
}; |
241 | 241 |
|
242 | 242 |
///Iterator for iterate over the rows of an LP problem |
243 | 243 |
|
244 |
/// Its usage is quite simple, for example you can count the number |
|
244 |
/// Its usage is quite simple, for example, you can count the number |
|
245 | 245 |
/// of rows in an LP \c lp: |
246 | 246 |
///\code |
247 | 247 |
/// int count=0; |
248 | 248 |
/// for (LpBase::RowIt c(lp); c!=INVALID; ++c) ++count; |
249 | 249 |
///\endcode |
250 | 250 |
class RowIt : public Row { |
251 | 251 |
const LpBase *_solver; |
252 | 252 |
public: |
253 | 253 |
/// Default constructor |
254 | 254 |
|
255 | 255 |
/// \warning The default constructor sets the iterator |
256 | 256 |
/// to an undefined value. |
257 | 257 |
RowIt() {} |
258 | 258 |
/// Sets the iterator to the first Row |
259 | 259 |
|
260 | 260 |
/// Sets the iterator to the first Row. |
261 | 261 |
/// |
262 | 262 |
RowIt(const LpBase &solver) : _solver(&solver) |
263 | 263 |
{ |
264 | 264 |
_solver->rows.firstItem(_id); |
265 | 265 |
} |
266 | 266 |
/// Invalid constructor \& conversion |
267 | 267 |
|
268 | 268 |
/// Initialize the iterator to be invalid. |
269 | 269 |
/// \sa Invalid for more details. |
270 | 270 |
RowIt(const Invalid&) : Row(INVALID) {} |
271 | 271 |
/// Next row |
272 | 272 |
|
273 | 273 |
/// Assign the iterator to the next row. |
274 | 274 |
/// |
275 | 275 |
RowIt &operator++() |
276 | 276 |
{ |
277 | 277 |
_solver->rows.nextItem(_id); |
278 | 278 |
return *this; |
279 | 279 |
} |
280 | 280 |
}; |
281 | 281 |
|
282 | 282 |
/// \brief Returns the ID of the row. |
283 | 283 |
static int id(const Row& row) { return row._id; } |
284 | 284 |
/// \brief Returns the row with the given ID. |
285 | 285 |
/// |
286 | 286 |
/// \pre The argument should be a valid row ID in the LP problem. |
287 | 287 |
static Row rowFromId(int id) { return Row(id); } |
288 | 288 |
|
289 | 289 |
public: |
290 | 290 |
|
291 | 291 |
///Linear expression of variables and a constant component |
292 | 292 |
|
293 | 293 |
///This data structure stores a linear expression of the variables |
294 | 294 |
///(\ref Col "Col"s) and also has a constant component. |
295 | 295 |
/// |
296 | 296 |
///There are several ways to access and modify the contents of this |
297 | 297 |
///container. |
298 | 298 |
///\code |
299 | 299 |
///e[v]=5; |
300 | 300 |
///e[v]+=12; |
301 | 301 |
///e.erase(v); |
302 | 302 |
///\endcode |
303 | 303 |
///or you can also iterate through its elements. |
304 | 304 |
///\code |
305 | 305 |
///double s=0; |
306 | 306 |
///for(LpBase::Expr::ConstCoeffIt i(e);i!=INVALID;++i) |
307 | 307 |
/// s+=*i * primal(i); |
308 | 308 |
///\endcode |
309 | 309 |
///(This code computes the primal value of the expression). |
310 | 310 |
///- Numbers (<tt>double</tt>'s) |
311 | 311 |
///and variables (\ref Col "Col"s) directly convert to an |
312 | 312 |
///\ref Expr and the usual linear operations are defined, so |
313 | 313 |
///\code |
314 | 314 |
///v+w |
315 | 315 |
///2*v-3.12*(v-w/2)+2 |
316 | 316 |
///v*2.1+(3*v+(v*12+w+6)*3)/2 |
317 | 317 |
///\endcode |
318 | 318 |
///are valid expressions. |
319 | 319 |
///The usual assignment operations are also defined. |
320 | 320 |
///\code |
321 | 321 |
///e=v+w; |
322 | 322 |
///e+=2*v-3.12*(v-w/2)+2; |
323 | 323 |
///e*=3.4; |
324 | 324 |
///e/=5; |
325 | 325 |
///\endcode |
326 | 326 |
///- The constant member can be set and read by dereference |
327 | 327 |
/// operator (unary *) |
328 | 328 |
/// |
329 | 329 |
///\code |
330 | 330 |
///*e=12; |
331 | 331 |
///double c=*e; |
332 | 332 |
///\endcode |
333 | 333 |
/// |
334 | 334 |
///\sa Constr |
335 | 335 |
class Expr { |
336 | 336 |
friend class LpBase; |
337 | 337 |
public: |
338 | 338 |
/// The key type of the expression |
339 | 339 |
typedef LpBase::Col Key; |
340 | 340 |
/// The value type of the expression |
341 | 341 |
typedef LpBase::Value Value; |
342 | 342 |
|
343 | 343 |
protected: |
344 | 344 |
Value const_comp; |
345 | 345 |
std::map<int, Value> comps; |
346 | 346 |
|
347 | 347 |
public: |
348 | 348 |
typedef True SolverExpr; |
349 | 349 |
/// Default constructor |
350 | 350 |
|
351 | 351 |
/// Construct an empty expression, the coefficients and |
352 | 352 |
/// the constant component are initialized to zero. |
353 | 353 |
Expr() : const_comp(0) {} |
354 | 354 |
/// Construct an expression from a column |
355 | 355 |
|
356 | 356 |
/// Construct an expression, which has a term with \c c variable |
357 | 357 |
/// and 1.0 coefficient. |
358 | 358 |
Expr(const Col &c) : const_comp(0) { |
359 | 359 |
typedef std::map<int, Value>::value_type pair_type; |
360 | 360 |
comps.insert(pair_type(id(c), 1)); |
361 | 361 |
} |
362 | 362 |
/// Construct an expression from a constant |
363 | 363 |
|
364 | 364 |
/// Construct an expression, which's constant component is \c v. |
365 | 365 |
/// |
366 | 366 |
Expr(const Value &v) : const_comp(v) {} |
367 | 367 |
/// Returns the coefficient of the column |
368 | 368 |
Value operator[](const Col& c) const { |
369 | 369 |
std::map<int, Value>::const_iterator it=comps.find(id(c)); |
370 | 370 |
if (it != comps.end()) { |
371 | 371 |
return it->second; |
372 | 372 |
} else { |
373 | 373 |
return 0; |
374 | 374 |
} |
375 | 375 |
} |
376 | 376 |
/// Returns the coefficient of the column |
377 | 377 |
Value& operator[](const Col& c) { |
378 | 378 |
return comps[id(c)]; |
379 | 379 |
} |
380 | 380 |
/// Sets the coefficient of the column |
381 | 381 |
void set(const Col &c, const Value &v) { |
382 | 382 |
if (v != 0.0) { |
383 | 383 |
typedef std::map<int, Value>::value_type pair_type; |
384 | 384 |
comps.insert(pair_type(id(c), v)); |
385 | 385 |
} else { |
386 | 386 |
comps.erase(id(c)); |
387 | 387 |
} |
388 | 388 |
} |
389 | 389 |
/// Returns the constant component of the expression |
390 | 390 |
Value& operator*() { return const_comp; } |
391 | 391 |
/// Returns the constant component of the expression |
392 | 392 |
const Value& operator*() const { return const_comp; } |
393 | 393 |
/// \brief Removes the coefficients which's absolute value does |
394 | 394 |
/// not exceed \c epsilon. It also sets to zero the constant |
395 | 395 |
/// component, if it does not exceed epsilon in absolute value. |
396 | 396 |
void simplify(Value epsilon = 0.0) { |
397 | 397 |
std::map<int, Value>::iterator it=comps.begin(); |
398 | 398 |
while (it != comps.end()) { |
399 | 399 |
std::map<int, Value>::iterator jt=it; |
400 | 400 |
++jt; |
401 | 401 |
if (std::fabs((*it).second) <= epsilon) comps.erase(it); |
402 | 402 |
it=jt; |
403 | 403 |
} |
404 | 404 |
if (std::fabs(const_comp) <= epsilon) const_comp = 0; |
405 | 405 |
} |
406 | 406 |
|
407 | 407 |
void simplify(Value epsilon = 0.0) const { |
408 | 408 |
const_cast<Expr*>(this)->simplify(epsilon); |
409 | 409 |
} |
410 | 410 |
|
411 | 411 |
///Sets all coefficients and the constant component to 0. |
412 | 412 |
void clear() { |
413 | 413 |
comps.clear(); |
414 | 414 |
const_comp=0; |
415 | 415 |
} |
416 | 416 |
|
417 | 417 |
///Compound assignment |
418 | 418 |
Expr &operator+=(const Expr &e) { |
419 | 419 |
for (std::map<int, Value>::const_iterator it=e.comps.begin(); |
420 | 420 |
it!=e.comps.end(); ++it) |
421 | 421 |
comps[it->first]+=it->second; |
422 | 422 |
const_comp+=e.const_comp; |
423 | 423 |
return *this; |
424 | 424 |
} |
425 | 425 |
///Compound assignment |
426 | 426 |
Expr &operator-=(const Expr &e) { |
427 | 427 |
for (std::map<int, Value>::const_iterator it=e.comps.begin(); |
428 | 428 |
it!=e.comps.end(); ++it) |
429 | 429 |
comps[it->first]-=it->second; |
430 | 430 |
const_comp-=e.const_comp; |
431 | 431 |
return *this; |
432 | 432 |
} |
433 | 433 |
///Multiply with a constant |
434 | 434 |
Expr &operator*=(const Value &v) { |
435 | 435 |
for (std::map<int, Value>::iterator it=comps.begin(); |
436 | 436 |
it!=comps.end(); ++it) |
... | ... |
@@ -41,505 +41,505 @@ |
41 | 41 |
/// required by the map %concepts. |
42 | 42 |
template<typename K, typename V> |
43 | 43 |
class MapBase { |
44 | 44 |
public: |
45 | 45 |
/// \brief The key type of the map. |
46 | 46 |
typedef K Key; |
47 | 47 |
/// \brief The value type of the map. |
48 | 48 |
/// (The type of objects associated with the keys). |
49 | 49 |
typedef V Value; |
50 | 50 |
}; |
51 | 51 |
|
52 | 52 |
|
53 | 53 |
/// Null map. (a.k.a. DoNothingMap) |
54 | 54 |
|
55 | 55 |
/// This map can be used if you have to provide a map only for |
56 | 56 |
/// its type definitions, or if you have to provide a writable map, |
57 | 57 |
/// but data written to it is not required (i.e. it will be sent to |
58 | 58 |
/// <tt>/dev/null</tt>). |
59 | 59 |
/// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
60 | 60 |
/// |
61 | 61 |
/// \sa ConstMap |
62 | 62 |
template<typename K, typename V> |
63 | 63 |
class NullMap : public MapBase<K, V> { |
64 | 64 |
public: |
65 | 65 |
///\e |
66 | 66 |
typedef K Key; |
67 | 67 |
///\e |
68 | 68 |
typedef V Value; |
69 | 69 |
|
70 | 70 |
/// Gives back a default constructed element. |
71 | 71 |
Value operator[](const Key&) const { return Value(); } |
72 | 72 |
/// Absorbs the value. |
73 | 73 |
void set(const Key&, const Value&) {} |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
/// Returns a \c NullMap class |
77 | 77 |
|
78 | 78 |
/// This function just returns a \c NullMap class. |
79 | 79 |
/// \relates NullMap |
80 | 80 |
template <typename K, typename V> |
81 | 81 |
NullMap<K, V> nullMap() { |
82 | 82 |
return NullMap<K, V>(); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// Constant map. |
87 | 87 |
|
88 | 88 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
89 | 89 |
/// value to each key. |
90 | 90 |
/// |
91 | 91 |
/// In other aspects it is equivalent to \c NullMap. |
92 | 92 |
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" |
93 | 93 |
/// concept, but it absorbs the data written to it. |
94 | 94 |
/// |
95 | 95 |
/// The simplest way of using this map is through the constMap() |
96 | 96 |
/// function. |
97 | 97 |
/// |
98 | 98 |
/// \sa NullMap |
99 | 99 |
/// \sa IdentityMap |
100 | 100 |
template<typename K, typename V> |
101 | 101 |
class ConstMap : public MapBase<K, V> { |
102 | 102 |
private: |
103 | 103 |
V _value; |
104 | 104 |
public: |
105 | 105 |
///\e |
106 | 106 |
typedef K Key; |
107 | 107 |
///\e |
108 | 108 |
typedef V Value; |
109 | 109 |
|
110 | 110 |
/// Default constructor |
111 | 111 |
|
112 | 112 |
/// Default constructor. |
113 | 113 |
/// The value of the map will be default constructed. |
114 | 114 |
ConstMap() {} |
115 | 115 |
|
116 | 116 |
/// Constructor with specified initial value |
117 | 117 |
|
118 | 118 |
/// Constructor with specified initial value. |
119 | 119 |
/// \param v The initial value of the map. |
120 | 120 |
ConstMap(const Value &v) : _value(v) {} |
121 | 121 |
|
122 | 122 |
/// Gives back the specified value. |
123 | 123 |
Value operator[](const Key&) const { return _value; } |
124 | 124 |
|
125 | 125 |
/// Absorbs the value. |
126 | 126 |
void set(const Key&, const Value&) {} |
127 | 127 |
|
128 | 128 |
/// Sets the value that is assigned to each key. |
129 | 129 |
void setAll(const Value &v) { |
130 | 130 |
_value = v; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
template<typename V1> |
134 | 134 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {} |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
/// Returns a \c ConstMap class |
138 | 138 |
|
139 | 139 |
/// This function just returns a \c ConstMap class. |
140 | 140 |
/// \relates ConstMap |
141 | 141 |
template<typename K, typename V> |
142 | 142 |
inline ConstMap<K, V> constMap(const V &v) { |
143 | 143 |
return ConstMap<K, V>(v); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
template<typename K, typename V> |
147 | 147 |
inline ConstMap<K, V> constMap() { |
148 | 148 |
return ConstMap<K, V>(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
|
152 | 152 |
template<typename T, T v> |
153 | 153 |
struct Const {}; |
154 | 154 |
|
155 | 155 |
/// Constant map with inlined constant value. |
156 | 156 |
|
157 | 157 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
158 | 158 |
/// value to each key. |
159 | 159 |
/// |
160 | 160 |
/// In other aspects it is equivalent to \c NullMap. |
161 | 161 |
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" |
162 | 162 |
/// concept, but it absorbs the data written to it. |
163 | 163 |
/// |
164 | 164 |
/// The simplest way of using this map is through the constMap() |
165 | 165 |
/// function. |
166 | 166 |
/// |
167 | 167 |
/// \sa NullMap |
168 | 168 |
/// \sa IdentityMap |
169 | 169 |
template<typename K, typename V, V v> |
170 | 170 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
171 | 171 |
public: |
172 | 172 |
///\e |
173 | 173 |
typedef K Key; |
174 | 174 |
///\e |
175 | 175 |
typedef V Value; |
176 | 176 |
|
177 | 177 |
/// Constructor. |
178 | 178 |
ConstMap() {} |
179 | 179 |
|
180 | 180 |
/// Gives back the specified value. |
181 | 181 |
Value operator[](const Key&) const { return v; } |
182 | 182 |
|
183 | 183 |
/// Absorbs the value. |
184 | 184 |
void set(const Key&, const Value&) {} |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
/// Returns a \c ConstMap class with inlined constant value |
188 | 188 |
|
189 | 189 |
/// This function just returns a \c ConstMap class with inlined |
190 | 190 |
/// constant value. |
191 | 191 |
/// \relates ConstMap |
192 | 192 |
template<typename K, typename V, V v> |
193 | 193 |
inline ConstMap<K, Const<V, v> > constMap() { |
194 | 194 |
return ConstMap<K, Const<V, v> >(); |
195 | 195 |
} |
196 | 196 |
|
197 | 197 |
|
198 | 198 |
/// Identity map. |
199 | 199 |
|
200 | 200 |
/// This \ref concepts::ReadMap "read-only map" gives back the given |
201 | 201 |
/// key as value without any modification. |
202 | 202 |
/// |
203 | 203 |
/// \sa ConstMap |
204 | 204 |
template <typename T> |
205 | 205 |
class IdentityMap : public MapBase<T, T> { |
206 | 206 |
public: |
207 | 207 |
///\e |
208 | 208 |
typedef T Key; |
209 | 209 |
///\e |
210 | 210 |
typedef T Value; |
211 | 211 |
|
212 | 212 |
/// Gives back the given value without any modification. |
213 | 213 |
Value operator[](const Key &k) const { |
214 | 214 |
return k; |
215 | 215 |
} |
216 | 216 |
}; |
217 | 217 |
|
218 | 218 |
/// Returns an \c IdentityMap class |
219 | 219 |
|
220 | 220 |
/// This function just returns an \c IdentityMap class. |
221 | 221 |
/// \relates IdentityMap |
222 | 222 |
template<typename T> |
223 | 223 |
inline IdentityMap<T> identityMap() { |
224 | 224 |
return IdentityMap<T>(); |
225 | 225 |
} |
226 | 226 |
|
227 | 227 |
|
228 | 228 |
/// \brief Map for storing values for integer keys from the range |
229 | 229 |
/// <tt>[0..size-1]</tt>. |
230 | 230 |
/// |
231 | 231 |
/// This map is essentially a wrapper for \c std::vector. It assigns |
232 | 232 |
/// values to integer keys from the range <tt>[0..size-1]</tt>. |
233 |
/// It can be used with some data structures, for example |
|
234 |
/// \c UnionFind, \c BinHeap, when the used items are small |
|
233 |
/// It can be used together with some data structures, e.g. |
|
234 |
/// heap types and \c UnionFind, when the used items are small |
|
235 | 235 |
/// integers. This map conforms to the \ref concepts::ReferenceMap |
236 |
/// "ReferenceMap" concept. |
|
236 |
/// "ReferenceMap" concept. |
|
237 | 237 |
/// |
238 | 238 |
/// The simplest way of using this map is through the rangeMap() |
239 | 239 |
/// function. |
240 | 240 |
template <typename V> |
241 | 241 |
class RangeMap : public MapBase<int, V> { |
242 | 242 |
template <typename V1> |
243 | 243 |
friend class RangeMap; |
244 | 244 |
private: |
245 | 245 |
|
246 | 246 |
typedef std::vector<V> Vector; |
247 | 247 |
Vector _vector; |
248 | 248 |
|
249 | 249 |
public: |
250 | 250 |
|
251 | 251 |
/// Key type |
252 | 252 |
typedef int Key; |
253 | 253 |
/// Value type |
254 | 254 |
typedef V Value; |
255 | 255 |
/// Reference type |
256 | 256 |
typedef typename Vector::reference Reference; |
257 | 257 |
/// Const reference type |
258 | 258 |
typedef typename Vector::const_reference ConstReference; |
259 | 259 |
|
260 | 260 |
typedef True ReferenceMapTag; |
261 | 261 |
|
262 | 262 |
public: |
263 | 263 |
|
264 | 264 |
/// Constructor with specified default value. |
265 | 265 |
RangeMap(int size = 0, const Value &value = Value()) |
266 | 266 |
: _vector(size, value) {} |
267 | 267 |
|
268 | 268 |
/// Constructs the map from an appropriate \c std::vector. |
269 | 269 |
template <typename V1> |
270 | 270 |
RangeMap(const std::vector<V1>& vector) |
271 | 271 |
: _vector(vector.begin(), vector.end()) {} |
272 | 272 |
|
273 | 273 |
/// Constructs the map from another \c RangeMap. |
274 | 274 |
template <typename V1> |
275 | 275 |
RangeMap(const RangeMap<V1> &c) |
276 | 276 |
: _vector(c._vector.begin(), c._vector.end()) {} |
277 | 277 |
|
278 | 278 |
/// Returns the size of the map. |
279 | 279 |
int size() { |
280 | 280 |
return _vector.size(); |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
/// Resizes the map. |
284 | 284 |
|
285 | 285 |
/// Resizes the underlying \c std::vector container, so changes the |
286 | 286 |
/// keyset of the map. |
287 | 287 |
/// \param size The new size of the map. The new keyset will be the |
288 | 288 |
/// range <tt>[0..size-1]</tt>. |
289 | 289 |
/// \param value The default value to assign to the new keys. |
290 | 290 |
void resize(int size, const Value &value = Value()) { |
291 | 291 |
_vector.resize(size, value); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
private: |
295 | 295 |
|
296 | 296 |
RangeMap& operator=(const RangeMap&); |
297 | 297 |
|
298 | 298 |
public: |
299 | 299 |
|
300 | 300 |
///\e |
301 | 301 |
Reference operator[](const Key &k) { |
302 | 302 |
return _vector[k]; |
303 | 303 |
} |
304 | 304 |
|
305 | 305 |
///\e |
306 | 306 |
ConstReference operator[](const Key &k) const { |
307 | 307 |
return _vector[k]; |
308 | 308 |
} |
309 | 309 |
|
310 | 310 |
///\e |
311 | 311 |
void set(const Key &k, const Value &v) { |
312 | 312 |
_vector[k] = v; |
313 | 313 |
} |
314 | 314 |
}; |
315 | 315 |
|
316 | 316 |
/// Returns a \c RangeMap class |
317 | 317 |
|
318 | 318 |
/// This function just returns a \c RangeMap class. |
319 | 319 |
/// \relates RangeMap |
320 | 320 |
template<typename V> |
321 | 321 |
inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) { |
322 | 322 |
return RangeMap<V>(size, value); |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
/// \brief Returns a \c RangeMap class created from an appropriate |
326 | 326 |
/// \c std::vector |
327 | 327 |
|
328 | 328 |
/// This function just returns a \c RangeMap class created from an |
329 | 329 |
/// appropriate \c std::vector. |
330 | 330 |
/// \relates RangeMap |
331 | 331 |
template<typename V> |
332 | 332 |
inline RangeMap<V> rangeMap(const std::vector<V> &vector) { |
333 | 333 |
return RangeMap<V>(vector); |
334 | 334 |
} |
335 | 335 |
|
336 | 336 |
|
337 | 337 |
/// Map type based on \c std::map |
338 | 338 |
|
339 | 339 |
/// This map is essentially a wrapper for \c std::map with addition |
340 | 340 |
/// that you can specify a default value for the keys that are not |
341 | 341 |
/// stored actually. This value can be different from the default |
342 | 342 |
/// contructed value (i.e. \c %Value()). |
343 | 343 |
/// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap" |
344 | 344 |
/// concept. |
345 | 345 |
/// |
346 | 346 |
/// This map is useful if a default value should be assigned to most of |
347 | 347 |
/// the keys and different values should be assigned only to a few |
348 | 348 |
/// keys (i.e. the map is "sparse"). |
349 | 349 |
/// The name of this type also refers to this important usage. |
350 | 350 |
/// |
351 |
/// Apart form that this map can be used in many other cases since it |
|
351 |
/// Apart form that, this map can be used in many other cases since it |
|
352 | 352 |
/// is based on \c std::map, which is a general associative container. |
353 |
/// However keep in mind that it is usually not as efficient as other |
|
353 |
/// However, keep in mind that it is usually not as efficient as other |
|
354 | 354 |
/// maps. |
355 | 355 |
/// |
356 | 356 |
/// The simplest way of using this map is through the sparseMap() |
357 | 357 |
/// function. |
358 | 358 |
template <typename K, typename V, typename Comp = std::less<K> > |
359 | 359 |
class SparseMap : public MapBase<K, V> { |
360 | 360 |
template <typename K1, typename V1, typename C1> |
361 | 361 |
friend class SparseMap; |
362 | 362 |
public: |
363 | 363 |
|
364 | 364 |
/// Key type |
365 | 365 |
typedef K Key; |
366 | 366 |
/// Value type |
367 | 367 |
typedef V Value; |
368 | 368 |
/// Reference type |
369 | 369 |
typedef Value& Reference; |
370 | 370 |
/// Const reference type |
371 | 371 |
typedef const Value& ConstReference; |
372 | 372 |
|
373 | 373 |
typedef True ReferenceMapTag; |
374 | 374 |
|
375 | 375 |
private: |
376 | 376 |
|
377 | 377 |
typedef std::map<K, V, Comp> Map; |
378 | 378 |
Map _map; |
379 | 379 |
Value _value; |
380 | 380 |
|
381 | 381 |
public: |
382 | 382 |
|
383 | 383 |
/// \brief Constructor with specified default value. |
384 | 384 |
SparseMap(const Value &value = Value()) : _value(value) {} |
385 | 385 |
/// \brief Constructs the map from an appropriate \c std::map, and |
386 | 386 |
/// explicitly specifies a default value. |
387 | 387 |
template <typename V1, typename Comp1> |
388 | 388 |
SparseMap(const std::map<Key, V1, Comp1> &map, |
389 | 389 |
const Value &value = Value()) |
390 | 390 |
: _map(map.begin(), map.end()), _value(value) {} |
391 | 391 |
|
392 | 392 |
/// \brief Constructs the map from another \c SparseMap. |
393 | 393 |
template<typename V1, typename Comp1> |
394 | 394 |
SparseMap(const SparseMap<Key, V1, Comp1> &c) |
395 | 395 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {} |
396 | 396 |
|
397 | 397 |
private: |
398 | 398 |
|
399 | 399 |
SparseMap& operator=(const SparseMap&); |
400 | 400 |
|
401 | 401 |
public: |
402 | 402 |
|
403 | 403 |
///\e |
404 | 404 |
Reference operator[](const Key &k) { |
405 | 405 |
typename Map::iterator it = _map.lower_bound(k); |
406 | 406 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
407 | 407 |
return it->second; |
408 | 408 |
else |
409 | 409 |
return _map.insert(it, std::make_pair(k, _value))->second; |
410 | 410 |
} |
411 | 411 |
|
412 | 412 |
///\e |
413 | 413 |
ConstReference operator[](const Key &k) const { |
414 | 414 |
typename Map::const_iterator it = _map.find(k); |
415 | 415 |
if (it != _map.end()) |
416 | 416 |
return it->second; |
417 | 417 |
else |
418 | 418 |
return _value; |
419 | 419 |
} |
420 | 420 |
|
421 | 421 |
///\e |
422 | 422 |
void set(const Key &k, const Value &v) { |
423 | 423 |
typename Map::iterator it = _map.lower_bound(k); |
424 | 424 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
425 | 425 |
it->second = v; |
426 | 426 |
else |
427 | 427 |
_map.insert(it, std::make_pair(k, v)); |
428 | 428 |
} |
429 | 429 |
|
430 | 430 |
///\e |
431 | 431 |
void setAll(const Value &v) { |
432 | 432 |
_value = v; |
433 | 433 |
_map.clear(); |
434 | 434 |
} |
435 | 435 |
}; |
436 | 436 |
|
437 | 437 |
/// Returns a \c SparseMap class |
438 | 438 |
|
439 | 439 |
/// This function just returns a \c SparseMap class with specified |
440 | 440 |
/// default value. |
441 | 441 |
/// \relates SparseMap |
442 | 442 |
template<typename K, typename V, typename Compare> |
443 | 443 |
inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) { |
444 | 444 |
return SparseMap<K, V, Compare>(value); |
445 | 445 |
} |
446 | 446 |
|
447 | 447 |
template<typename K, typename V> |
448 | 448 |
inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) { |
449 | 449 |
return SparseMap<K, V, std::less<K> >(value); |
450 | 450 |
} |
451 | 451 |
|
452 | 452 |
/// \brief Returns a \c SparseMap class created from an appropriate |
453 | 453 |
/// \c std::map |
454 | 454 |
|
455 | 455 |
/// This function just returns a \c SparseMap class created from an |
456 | 456 |
/// appropriate \c std::map. |
457 | 457 |
/// \relates SparseMap |
458 | 458 |
template<typename K, typename V, typename Compare> |
459 | 459 |
inline SparseMap<K, V, Compare> |
460 | 460 |
sparseMap(const std::map<K, V, Compare> &map, const V& value = V()) |
461 | 461 |
{ |
462 | 462 |
return SparseMap<K, V, Compare>(map, value); |
463 | 463 |
} |
464 | 464 |
|
465 | 465 |
/// @} |
466 | 466 |
|
467 | 467 |
/// \addtogroup map_adaptors |
468 | 468 |
/// @{ |
469 | 469 |
|
470 | 470 |
/// Composition of two maps |
471 | 471 |
|
472 | 472 |
/// This \ref concepts::ReadMap "read-only map" returns the |
473 | 473 |
/// composition of two given maps. That is to say, if \c m1 is of |
474 | 474 |
/// type \c M1 and \c m2 is of \c M2, then for |
475 | 475 |
/// \code |
476 | 476 |
/// ComposeMap<M1, M2> cm(m1,m2); |
477 | 477 |
/// \endcode |
478 | 478 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
479 | 479 |
/// |
480 | 480 |
/// The \c Key type of the map is inherited from \c M2 and the |
481 | 481 |
/// \c Value type is from \c M1. |
482 | 482 |
/// \c M2::Value must be convertible to \c M1::Key. |
483 | 483 |
/// |
484 | 484 |
/// The simplest way of using this map is through the composeMap() |
485 | 485 |
/// function. |
486 | 486 |
/// |
487 | 487 |
/// \sa CombineMap |
488 | 488 |
template <typename M1, typename M2> |
489 | 489 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
490 | 490 |
const M1 &_m1; |
491 | 491 |
const M2 &_m2; |
492 | 492 |
public: |
493 | 493 |
///\e |
494 | 494 |
typedef typename M2::Key Key; |
495 | 495 |
///\e |
496 | 496 |
typedef typename M1::Value Value; |
497 | 497 |
|
498 | 498 |
/// Constructor |
499 | 499 |
ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
500 | 500 |
|
501 | 501 |
///\e |
502 | 502 |
typename MapTraits<M1>::ConstReturnValue |
503 | 503 |
operator[](const Key &k) const { return _m1[_m2[k]]; } |
504 | 504 |
}; |
505 | 505 |
|
506 | 506 |
/// Returns a \c ComposeMap class |
507 | 507 |
|
508 | 508 |
/// This function just returns a \c ComposeMap class. |
509 | 509 |
/// |
510 | 510 |
/// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is |
511 | 511 |
/// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt> |
512 | 512 |
/// will be equal to <tt>m1[m2[x]]</tt>. |
513 | 513 |
/// |
514 | 514 |
/// \relates ComposeMap |
515 | 515 |
template <typename M1, typename M2> |
516 | 516 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) { |
517 | 517 |
return ComposeMap<M1, M2>(m1, m2); |
518 | 518 |
} |
519 | 519 |
|
520 | 520 |
|
521 | 521 |
/// Combination of two maps using an STL (binary) functor. |
522 | 522 |
|
523 | 523 |
/// This \ref concepts::ReadMap "read-only map" takes two maps and a |
524 | 524 |
/// binary functor and returns the combination of the two given maps |
525 | 525 |
/// using the functor. |
526 | 526 |
/// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2 |
527 | 527 |
/// and \c f is of \c F, then for |
528 | 528 |
/// \code |
529 | 529 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
530 | 530 |
/// \endcode |
531 | 531 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>. |
532 | 532 |
/// |
533 | 533 |
/// The \c Key type of the map is inherited from \c M1 (\c M1::Key |
534 | 534 |
/// must be convertible to \c M2::Key) and the \c Value type is \c V. |
535 | 535 |
/// \c M2::Value and \c M1::Value must be convertible to the |
536 | 536 |
/// corresponding input parameter of \c F and the return type of \c F |
537 | 537 |
/// must be convertible to \c V. |
538 | 538 |
/// |
539 | 539 |
/// The simplest way of using this map is through the combineMap() |
540 | 540 |
/// function. |
541 | 541 |
/// |
542 | 542 |
/// \sa ComposeMap |
543 | 543 |
template<typename M1, typename M2, typename F, |
544 | 544 |
typename V = typename F::result_type> |
545 | 545 |
class CombineMap : public MapBase<typename M1::Key, V> { |
... | ... |
@@ -1596,522 +1596,522 @@ |
1596 | 1596 |
} |
1597 | 1597 |
|
1598 | 1598 |
|
1599 | 1599 |
/// Combination of two maps using the \c == operator |
1600 | 1600 |
|
1601 | 1601 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1602 | 1602 |
/// the keys for which the corresponding values of the two maps are |
1603 | 1603 |
/// equal. |
1604 | 1604 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1605 | 1605 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1606 | 1606 |
/// |
1607 | 1607 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1608 | 1608 |
/// \code |
1609 | 1609 |
/// EqualMap<M1,M2> em(m1,m2); |
1610 | 1610 |
/// \endcode |
1611 | 1611 |
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>. |
1612 | 1612 |
/// |
1613 | 1613 |
/// The simplest way of using this map is through the equalMap() |
1614 | 1614 |
/// function. |
1615 | 1615 |
/// |
1616 | 1616 |
/// \sa LessMap |
1617 | 1617 |
template<typename M1, typename M2> |
1618 | 1618 |
class EqualMap : public MapBase<typename M1::Key, bool> { |
1619 | 1619 |
const M1 &_m1; |
1620 | 1620 |
const M2 &_m2; |
1621 | 1621 |
public: |
1622 | 1622 |
///\e |
1623 | 1623 |
typedef typename M1::Key Key; |
1624 | 1624 |
///\e |
1625 | 1625 |
typedef bool Value; |
1626 | 1626 |
|
1627 | 1627 |
/// Constructor |
1628 | 1628 |
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1629 | 1629 |
///\e |
1630 | 1630 |
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; } |
1631 | 1631 |
}; |
1632 | 1632 |
|
1633 | 1633 |
/// Returns an \c EqualMap class |
1634 | 1634 |
|
1635 | 1635 |
/// This function just returns an \c EqualMap class. |
1636 | 1636 |
/// |
1637 | 1637 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
1638 | 1638 |
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to |
1639 | 1639 |
/// <tt>m1[x]==m2[x]</tt>. |
1640 | 1640 |
/// |
1641 | 1641 |
/// \relates EqualMap |
1642 | 1642 |
template<typename M1, typename M2> |
1643 | 1643 |
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) { |
1644 | 1644 |
return EqualMap<M1, M2>(m1,m2); |
1645 | 1645 |
} |
1646 | 1646 |
|
1647 | 1647 |
|
1648 | 1648 |
/// Combination of two maps using the \c < operator |
1649 | 1649 |
|
1650 | 1650 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1651 | 1651 |
/// the keys for which the corresponding value of the first map is |
1652 | 1652 |
/// less then the value of the second map. |
1653 | 1653 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1654 | 1654 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1655 | 1655 |
/// |
1656 | 1656 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1657 | 1657 |
/// \code |
1658 | 1658 |
/// LessMap<M1,M2> lm(m1,m2); |
1659 | 1659 |
/// \endcode |
1660 | 1660 |
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>. |
1661 | 1661 |
/// |
1662 | 1662 |
/// The simplest way of using this map is through the lessMap() |
1663 | 1663 |
/// function. |
1664 | 1664 |
/// |
1665 | 1665 |
/// \sa EqualMap |
1666 | 1666 |
template<typename M1, typename M2> |
1667 | 1667 |
class LessMap : public MapBase<typename M1::Key, bool> { |
1668 | 1668 |
const M1 &_m1; |
1669 | 1669 |
const M2 &_m2; |
1670 | 1670 |
public: |
1671 | 1671 |
///\e |
1672 | 1672 |
typedef typename M1::Key Key; |
1673 | 1673 |
///\e |
1674 | 1674 |
typedef bool Value; |
1675 | 1675 |
|
1676 | 1676 |
/// Constructor |
1677 | 1677 |
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1678 | 1678 |
///\e |
1679 | 1679 |
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; } |
1680 | 1680 |
}; |
1681 | 1681 |
|
1682 | 1682 |
/// Returns an \c LessMap class |
1683 | 1683 |
|
1684 | 1684 |
/// This function just returns an \c LessMap class. |
1685 | 1685 |
/// |
1686 | 1686 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
1687 | 1687 |
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to |
1688 | 1688 |
/// <tt>m1[x]<m2[x]</tt>. |
1689 | 1689 |
/// |
1690 | 1690 |
/// \relates LessMap |
1691 | 1691 |
template<typename M1, typename M2> |
1692 | 1692 |
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) { |
1693 | 1693 |
return LessMap<M1, M2>(m1,m2); |
1694 | 1694 |
} |
1695 | 1695 |
|
1696 | 1696 |
namespace _maps_bits { |
1697 | 1697 |
|
1698 | 1698 |
template <typename _Iterator, typename Enable = void> |
1699 | 1699 |
struct IteratorTraits { |
1700 | 1700 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
1701 | 1701 |
}; |
1702 | 1702 |
|
1703 | 1703 |
template <typename _Iterator> |
1704 | 1704 |
struct IteratorTraits<_Iterator, |
1705 | 1705 |
typename exists<typename _Iterator::container_type>::type> |
1706 | 1706 |
{ |
1707 | 1707 |
typedef typename _Iterator::container_type::value_type Value; |
1708 | 1708 |
}; |
1709 | 1709 |
|
1710 | 1710 |
} |
1711 | 1711 |
|
1712 | 1712 |
/// @} |
1713 | 1713 |
|
1714 | 1714 |
/// \addtogroup maps |
1715 | 1715 |
/// @{ |
1716 | 1716 |
|
1717 | 1717 |
/// \brief Writable bool map for logging each \c true assigned element |
1718 | 1718 |
/// |
1719 | 1719 |
/// A \ref concepts::WriteMap "writable" bool map for logging |
1720 | 1720 |
/// each \c true assigned element, i.e it copies subsequently each |
1721 | 1721 |
/// keys set to \c true to the given iterator. |
1722 | 1722 |
/// The most important usage of it is storing certain nodes or arcs |
1723 | 1723 |
/// that were marked \c true by an algorithm. |
1724 | 1724 |
/// |
1725 | 1725 |
/// There are several algorithms that provide solutions through bool |
1726 | 1726 |
/// maps and most of them assign \c true at most once for each key. |
1727 | 1727 |
/// In these cases it is a natural request to store each \c true |
1728 | 1728 |
/// assigned elements (in order of the assignment), which can be |
1729 | 1729 |
/// easily done with LoggerBoolMap. |
1730 | 1730 |
/// |
1731 | 1731 |
/// The simplest way of using this map is through the loggerBoolMap() |
1732 | 1732 |
/// function. |
1733 | 1733 |
/// |
1734 | 1734 |
/// \tparam IT The type of the iterator. |
1735 | 1735 |
/// \tparam KEY The key type of the map. The default value set |
1736 | 1736 |
/// according to the iterator type should work in most cases. |
1737 | 1737 |
/// |
1738 | 1738 |
/// \note The container of the iterator must contain enough space |
1739 | 1739 |
/// for the elements or the iterator should be an inserter iterator. |
1740 | 1740 |
#ifdef DOXYGEN |
1741 | 1741 |
template <typename IT, typename KEY> |
1742 | 1742 |
#else |
1743 | 1743 |
template <typename IT, |
1744 | 1744 |
typename KEY = typename _maps_bits::IteratorTraits<IT>::Value> |
1745 | 1745 |
#endif |
1746 | 1746 |
class LoggerBoolMap : public MapBase<KEY, bool> { |
1747 | 1747 |
public: |
1748 | 1748 |
|
1749 | 1749 |
///\e |
1750 | 1750 |
typedef KEY Key; |
1751 | 1751 |
///\e |
1752 | 1752 |
typedef bool Value; |
1753 | 1753 |
///\e |
1754 | 1754 |
typedef IT Iterator; |
1755 | 1755 |
|
1756 | 1756 |
/// Constructor |
1757 | 1757 |
LoggerBoolMap(Iterator it) |
1758 | 1758 |
: _begin(it), _end(it) {} |
1759 | 1759 |
|
1760 | 1760 |
/// Gives back the given iterator set for the first key |
1761 | 1761 |
Iterator begin() const { |
1762 | 1762 |
return _begin; |
1763 | 1763 |
} |
1764 | 1764 |
|
1765 | 1765 |
/// Gives back the the 'after the last' iterator |
1766 | 1766 |
Iterator end() const { |
1767 | 1767 |
return _end; |
1768 | 1768 |
} |
1769 | 1769 |
|
1770 | 1770 |
/// The set function of the map |
1771 | 1771 |
void set(const Key& key, Value value) { |
1772 | 1772 |
if (value) { |
1773 | 1773 |
*_end++ = key; |
1774 | 1774 |
} |
1775 | 1775 |
} |
1776 | 1776 |
|
1777 | 1777 |
private: |
1778 | 1778 |
Iterator _begin; |
1779 | 1779 |
Iterator _end; |
1780 | 1780 |
}; |
1781 | 1781 |
|
1782 | 1782 |
/// Returns a \c LoggerBoolMap class |
1783 | 1783 |
|
1784 | 1784 |
/// This function just returns a \c LoggerBoolMap class. |
1785 | 1785 |
/// |
1786 | 1786 |
/// The most important usage of it is storing certain nodes or arcs |
1787 | 1787 |
/// that were marked \c true by an algorithm. |
1788 |
/// For example it makes easier to store the nodes in the processing |
|
1788 |
/// For example, it makes easier to store the nodes in the processing |
|
1789 | 1789 |
/// order of Dfs algorithm, as the following examples show. |
1790 | 1790 |
/// \code |
1791 | 1791 |
/// std::vector<Node> v; |
1792 | 1792 |
/// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s); |
1793 | 1793 |
/// \endcode |
1794 | 1794 |
/// \code |
1795 | 1795 |
/// std::vector<Node> v(countNodes(g)); |
1796 | 1796 |
/// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s); |
1797 | 1797 |
/// \endcode |
1798 | 1798 |
/// |
1799 | 1799 |
/// \note The container of the iterator must contain enough space |
1800 | 1800 |
/// for the elements or the iterator should be an inserter iterator. |
1801 | 1801 |
/// |
1802 | 1802 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so |
1803 |
/// it cannot be used when a readable map is needed, for example as |
|
1803 |
/// it cannot be used when a readable map is needed, for example, as |
|
1804 | 1804 |
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms. |
1805 | 1805 |
/// |
1806 | 1806 |
/// \relates LoggerBoolMap |
1807 | 1807 |
template<typename Iterator> |
1808 | 1808 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) { |
1809 | 1809 |
return LoggerBoolMap<Iterator>(it); |
1810 | 1810 |
} |
1811 | 1811 |
|
1812 | 1812 |
/// @} |
1813 | 1813 |
|
1814 | 1814 |
/// \addtogroup graph_maps |
1815 | 1815 |
/// @{ |
1816 | 1816 |
|
1817 | 1817 |
/// \brief Provides an immutable and unique id for each item in a graph. |
1818 | 1818 |
/// |
1819 | 1819 |
/// IdMap provides a unique and immutable id for each item of the |
1820 | 1820 |
/// same type (\c Node, \c Arc or \c Edge) in a graph. This id is |
1821 | 1821 |
/// - \b unique: different items get different ids, |
1822 | 1822 |
/// - \b immutable: the id of an item does not change (even if you |
1823 | 1823 |
/// delete other nodes). |
1824 | 1824 |
/// |
1825 | 1825 |
/// Using this map you get access (i.e. can read) the inner id values of |
1826 | 1826 |
/// the items stored in the graph, which is returned by the \c id() |
1827 | 1827 |
/// function of the graph. This map can be inverted with its member |
1828 | 1828 |
/// class \c InverseMap or with the \c operator()() member. |
1829 | 1829 |
/// |
1830 | 1830 |
/// \tparam GR The graph type. |
1831 | 1831 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
1832 | 1832 |
/// \c GR::Edge). |
1833 | 1833 |
/// |
1834 | 1834 |
/// \see RangeIdMap |
1835 | 1835 |
template <typename GR, typename K> |
1836 | 1836 |
class IdMap : public MapBase<K, int> { |
1837 | 1837 |
public: |
1838 | 1838 |
/// The graph type of IdMap. |
1839 | 1839 |
typedef GR Graph; |
1840 | 1840 |
typedef GR Digraph; |
1841 | 1841 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge). |
1842 | 1842 |
typedef K Item; |
1843 | 1843 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge). |
1844 | 1844 |
typedef K Key; |
1845 | 1845 |
/// The value type of IdMap. |
1846 | 1846 |
typedef int Value; |
1847 | 1847 |
|
1848 | 1848 |
/// \brief Constructor. |
1849 | 1849 |
/// |
1850 | 1850 |
/// Constructor of the map. |
1851 | 1851 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
1852 | 1852 |
|
1853 | 1853 |
/// \brief Gives back the \e id of the item. |
1854 | 1854 |
/// |
1855 | 1855 |
/// Gives back the immutable and unique \e id of the item. |
1856 | 1856 |
int operator[](const Item& item) const { return _graph->id(item);} |
1857 | 1857 |
|
1858 | 1858 |
/// \brief Gives back the \e item by its id. |
1859 | 1859 |
/// |
1860 | 1860 |
/// Gives back the \e item by its id. |
1861 | 1861 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
1862 | 1862 |
|
1863 | 1863 |
private: |
1864 | 1864 |
const Graph* _graph; |
1865 | 1865 |
|
1866 | 1866 |
public: |
1867 | 1867 |
|
1868 | 1868 |
/// \brief The inverse map type of IdMap. |
1869 | 1869 |
/// |
1870 | 1870 |
/// The inverse map type of IdMap. The subscript operator gives back |
1871 | 1871 |
/// an item by its id. |
1872 | 1872 |
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. |
1873 | 1873 |
/// \see inverse() |
1874 | 1874 |
class InverseMap { |
1875 | 1875 |
public: |
1876 | 1876 |
|
1877 | 1877 |
/// \brief Constructor. |
1878 | 1878 |
/// |
1879 | 1879 |
/// Constructor for creating an id-to-item map. |
1880 | 1880 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
1881 | 1881 |
|
1882 | 1882 |
/// \brief Constructor. |
1883 | 1883 |
/// |
1884 | 1884 |
/// Constructor for creating an id-to-item map. |
1885 | 1885 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
1886 | 1886 |
|
1887 | 1887 |
/// \brief Gives back an item by its id. |
1888 | 1888 |
/// |
1889 | 1889 |
/// Gives back an item by its id. |
1890 | 1890 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
1891 | 1891 |
|
1892 | 1892 |
private: |
1893 | 1893 |
const Graph* _graph; |
1894 | 1894 |
}; |
1895 | 1895 |
|
1896 | 1896 |
/// \brief Gives back the inverse of the map. |
1897 | 1897 |
/// |
1898 | 1898 |
/// Gives back the inverse of the IdMap. |
1899 | 1899 |
InverseMap inverse() const { return InverseMap(*_graph);} |
1900 | 1900 |
}; |
1901 | 1901 |
|
1902 | 1902 |
/// \brief Returns an \c IdMap class. |
1903 | 1903 |
/// |
1904 | 1904 |
/// This function just returns an \c IdMap class. |
1905 | 1905 |
/// \relates IdMap |
1906 | 1906 |
template <typename K, typename GR> |
1907 | 1907 |
inline IdMap<GR, K> idMap(const GR& graph) { |
1908 | 1908 |
return IdMap<GR, K>(graph); |
1909 | 1909 |
} |
1910 | 1910 |
|
1911 | 1911 |
/// \brief General cross reference graph map type. |
1912 | 1912 |
|
1913 | 1913 |
/// This class provides simple invertable graph maps. |
1914 | 1914 |
/// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap) |
1915 | 1915 |
/// and if a key is set to a new value, then stores it in the inverse map. |
1916 | 1916 |
/// The graph items can be accessed by their values either using |
1917 | 1917 |
/// \c InverseMap or \c operator()(), and the values of the map can be |
1918 | 1918 |
/// accessed with an STL compatible forward iterator (\c ValueIt). |
1919 | 1919 |
/// |
1920 | 1920 |
/// This map is intended to be used when all associated values are |
1921 | 1921 |
/// different (the map is actually invertable) or there are only a few |
1922 | 1922 |
/// items with the same value. |
1923 | 1923 |
/// Otherwise consider to use \c IterableValueMap, which is more |
1924 | 1924 |
/// suitable and more efficient for such cases. It provides iterators |
1925 |
/// to traverse the items with the same associated value, |
|
1925 |
/// to traverse the items with the same associated value, but |
|
1926 | 1926 |
/// it does not have \c InverseMap. |
1927 | 1927 |
/// |
1928 | 1928 |
/// This type is not reference map, so it cannot be modified with |
1929 | 1929 |
/// the subscript operator. |
1930 | 1930 |
/// |
1931 | 1931 |
/// \tparam GR The graph type. |
1932 | 1932 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
1933 | 1933 |
/// \c GR::Edge). |
1934 | 1934 |
/// \tparam V The value type of the map. |
1935 | 1935 |
/// |
1936 | 1936 |
/// \see IterableValueMap |
1937 | 1937 |
template <typename GR, typename K, typename V> |
1938 | 1938 |
class CrossRefMap |
1939 | 1939 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type { |
1940 | 1940 |
private: |
1941 | 1941 |
|
1942 | 1942 |
typedef typename ItemSetTraits<GR, K>:: |
1943 | 1943 |
template Map<V>::Type Map; |
1944 | 1944 |
|
1945 | 1945 |
typedef std::multimap<V, K> Container; |
1946 | 1946 |
Container _inv_map; |
1947 | 1947 |
|
1948 | 1948 |
public: |
1949 | 1949 |
|
1950 | 1950 |
/// The graph type of CrossRefMap. |
1951 | 1951 |
typedef GR Graph; |
1952 | 1952 |
typedef GR Digraph; |
1953 | 1953 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
1954 | 1954 |
typedef K Item; |
1955 | 1955 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
1956 | 1956 |
typedef K Key; |
1957 | 1957 |
/// The value type of CrossRefMap. |
1958 | 1958 |
typedef V Value; |
1959 | 1959 |
|
1960 | 1960 |
/// \brief Constructor. |
1961 | 1961 |
/// |
1962 | 1962 |
/// Construct a new CrossRefMap for the given graph. |
1963 | 1963 |
explicit CrossRefMap(const Graph& graph) : Map(graph) {} |
1964 | 1964 |
|
1965 | 1965 |
/// \brief Forward iterator for values. |
1966 | 1966 |
/// |
1967 | 1967 |
/// This iterator is an STL compatible forward |
1968 | 1968 |
/// iterator on the values of the map. The values can |
1969 | 1969 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range. |
1970 | 1970 |
/// They are considered with multiplicity, so each value is |
1971 | 1971 |
/// traversed for each item it is assigned to. |
1972 | 1972 |
class ValueIt |
1973 | 1973 |
: public std::iterator<std::forward_iterator_tag, Value> { |
1974 | 1974 |
friend class CrossRefMap; |
1975 | 1975 |
private: |
1976 | 1976 |
ValueIt(typename Container::const_iterator _it) |
1977 | 1977 |
: it(_it) {} |
1978 | 1978 |
public: |
1979 | 1979 |
|
1980 | 1980 |
/// Constructor |
1981 | 1981 |
ValueIt() {} |
1982 | 1982 |
|
1983 | 1983 |
/// \e |
1984 | 1984 |
ValueIt& operator++() { ++it; return *this; } |
1985 | 1985 |
/// \e |
1986 | 1986 |
ValueIt operator++(int) { |
1987 | 1987 |
ValueIt tmp(*this); |
1988 | 1988 |
operator++(); |
1989 | 1989 |
return tmp; |
1990 | 1990 |
} |
1991 | 1991 |
|
1992 | 1992 |
/// \e |
1993 | 1993 |
const Value& operator*() const { return it->first; } |
1994 | 1994 |
/// \e |
1995 | 1995 |
const Value* operator->() const { return &(it->first); } |
1996 | 1996 |
|
1997 | 1997 |
/// \e |
1998 | 1998 |
bool operator==(ValueIt jt) const { return it == jt.it; } |
1999 | 1999 |
/// \e |
2000 | 2000 |
bool operator!=(ValueIt jt) const { return it != jt.it; } |
2001 | 2001 |
|
2002 | 2002 |
private: |
2003 | 2003 |
typename Container::const_iterator it; |
2004 | 2004 |
}; |
2005 | 2005 |
|
2006 | 2006 |
/// Alias for \c ValueIt |
2007 | 2007 |
typedef ValueIt ValueIterator; |
2008 | 2008 |
|
2009 | 2009 |
/// \brief Returns an iterator to the first value. |
2010 | 2010 |
/// |
2011 | 2011 |
/// Returns an STL compatible iterator to the |
2012 | 2012 |
/// first value of the map. The values of the |
2013 | 2013 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
2014 | 2014 |
/// range. |
2015 | 2015 |
ValueIt beginValue() const { |
2016 | 2016 |
return ValueIt(_inv_map.begin()); |
2017 | 2017 |
} |
2018 | 2018 |
|
2019 | 2019 |
/// \brief Returns an iterator after the last value. |
2020 | 2020 |
/// |
2021 | 2021 |
/// Returns an STL compatible iterator after the |
2022 | 2022 |
/// last value of the map. The values of the |
2023 | 2023 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
2024 | 2024 |
/// range. |
2025 | 2025 |
ValueIt endValue() const { |
2026 | 2026 |
return ValueIt(_inv_map.end()); |
2027 | 2027 |
} |
2028 | 2028 |
|
2029 | 2029 |
/// \brief Sets the value associated with the given key. |
2030 | 2030 |
/// |
2031 | 2031 |
/// Sets the value associated with the given key. |
2032 | 2032 |
void set(const Key& key, const Value& val) { |
2033 | 2033 |
Value oldval = Map::operator[](key); |
2034 | 2034 |
typename Container::iterator it; |
2035 | 2035 |
for (it = _inv_map.equal_range(oldval).first; |
2036 | 2036 |
it != _inv_map.equal_range(oldval).second; ++it) { |
2037 | 2037 |
if (it->second == key) { |
2038 | 2038 |
_inv_map.erase(it); |
2039 | 2039 |
break; |
2040 | 2040 |
} |
2041 | 2041 |
} |
2042 | 2042 |
_inv_map.insert(std::make_pair(val, key)); |
2043 | 2043 |
Map::set(key, val); |
2044 | 2044 |
} |
2045 | 2045 |
|
2046 | 2046 |
/// \brief Returns the value associated with the given key. |
2047 | 2047 |
/// |
2048 | 2048 |
/// Returns the value associated with the given key. |
2049 | 2049 |
typename MapTraits<Map>::ConstReturnValue |
2050 | 2050 |
operator[](const Key& key) const { |
2051 | 2051 |
return Map::operator[](key); |
2052 | 2052 |
} |
2053 | 2053 |
|
2054 | 2054 |
/// \brief Gives back an item by its value. |
2055 | 2055 |
/// |
2056 | 2056 |
/// This function gives back an item that is assigned to |
2057 | 2057 |
/// the given value or \c INVALID if no such item exists. |
2058 | 2058 |
/// If there are more items with the same associated value, |
2059 | 2059 |
/// only one of them is returned. |
2060 | 2060 |
Key operator()(const Value& val) const { |
2061 | 2061 |
typename Container::const_iterator it = _inv_map.find(val); |
2062 | 2062 |
return it != _inv_map.end() ? it->second : INVALID; |
2063 | 2063 |
} |
2064 | 2064 |
|
2065 | 2065 |
/// \brief Returns the number of items with the given value. |
2066 | 2066 |
/// |
2067 | 2067 |
/// This function returns the number of items with the given value |
2068 | 2068 |
/// associated with it. |
2069 | 2069 |
int count(const Value &val) const { |
2070 | 2070 |
return _inv_map.count(val); |
2071 | 2071 |
} |
2072 | 2072 |
|
2073 | 2073 |
protected: |
2074 | 2074 |
|
2075 | 2075 |
/// \brief Erase the key from the map and the inverse map. |
2076 | 2076 |
/// |
2077 | 2077 |
/// Erase the key from the map and the inverse map. It is called by the |
2078 | 2078 |
/// \c AlterationNotifier. |
2079 | 2079 |
virtual void erase(const Key& key) { |
2080 | 2080 |
Value val = Map::operator[](key); |
2081 | 2081 |
typename Container::iterator it; |
2082 | 2082 |
for (it = _inv_map.equal_range(val).first; |
2083 | 2083 |
it != _inv_map.equal_range(val).second; ++it) { |
2084 | 2084 |
if (it->second == key) { |
2085 | 2085 |
_inv_map.erase(it); |
2086 | 2086 |
break; |
2087 | 2087 |
} |
2088 | 2088 |
} |
2089 | 2089 |
Map::erase(key); |
2090 | 2090 |
} |
2091 | 2091 |
|
2092 | 2092 |
/// \brief Erase more keys from the map and the inverse map. |
2093 | 2093 |
/// |
2094 | 2094 |
/// Erase more keys from the map and the inverse map. It is called by the |
2095 | 2095 |
/// \c AlterationNotifier. |
2096 | 2096 |
virtual void erase(const std::vector<Key>& keys) { |
2097 | 2097 |
for (int i = 0; i < int(keys.size()); ++i) { |
2098 | 2098 |
Value val = Map::operator[](keys[i]); |
2099 | 2099 |
typename Container::iterator it; |
2100 | 2100 |
for (it = _inv_map.equal_range(val).first; |
2101 | 2101 |
it != _inv_map.equal_range(val).second; ++it) { |
2102 | 2102 |
if (it->second == keys[i]) { |
2103 | 2103 |
_inv_map.erase(it); |
2104 | 2104 |
break; |
2105 | 2105 |
} |
2106 | 2106 |
} |
2107 | 2107 |
} |
2108 | 2108 |
Map::erase(keys); |
2109 | 2109 |
} |
2110 | 2110 |
|
2111 | 2111 |
/// \brief Clear the keys from the map and the inverse map. |
2112 | 2112 |
/// |
2113 | 2113 |
/// Clear the keys from the map and the inverse map. It is called by the |
2114 | 2114 |
/// \c AlterationNotifier. |
2115 | 2115 |
virtual void clear() { |
2116 | 2116 |
_inv_map.clear(); |
2117 | 2117 |
Map::clear(); |
... | ... |
@@ -3277,494 +3277,494 @@ |
3277 | 3277 |
virtual void build() { |
3278 | 3278 |
Parent::build(); |
3279 | 3279 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { |
3280 | 3280 |
lace(it); |
3281 | 3281 |
} |
3282 | 3282 |
} |
3283 | 3283 |
|
3284 | 3284 |
virtual void clear() { |
3285 | 3285 |
_first.clear(); |
3286 | 3286 |
Parent::clear(); |
3287 | 3287 |
} |
3288 | 3288 |
|
3289 | 3289 |
private: |
3290 | 3290 |
std::map<Value, Key> _first; |
3291 | 3291 |
}; |
3292 | 3292 |
|
3293 | 3293 |
/// \brief Map of the source nodes of arcs in a digraph. |
3294 | 3294 |
/// |
3295 | 3295 |
/// SourceMap provides access for the source node of each arc in a digraph, |
3296 | 3296 |
/// which is returned by the \c source() function of the digraph. |
3297 | 3297 |
/// \tparam GR The digraph type. |
3298 | 3298 |
/// \see TargetMap |
3299 | 3299 |
template <typename GR> |
3300 | 3300 |
class SourceMap { |
3301 | 3301 |
public: |
3302 | 3302 |
|
3303 | 3303 |
/// The key type (the \c Arc type of the digraph). |
3304 | 3304 |
typedef typename GR::Arc Key; |
3305 | 3305 |
/// The value type (the \c Node type of the digraph). |
3306 | 3306 |
typedef typename GR::Node Value; |
3307 | 3307 |
|
3308 | 3308 |
/// \brief Constructor |
3309 | 3309 |
/// |
3310 | 3310 |
/// Constructor. |
3311 | 3311 |
/// \param digraph The digraph that the map belongs to. |
3312 | 3312 |
explicit SourceMap(const GR& digraph) : _graph(digraph) {} |
3313 | 3313 |
|
3314 | 3314 |
/// \brief Returns the source node of the given arc. |
3315 | 3315 |
/// |
3316 | 3316 |
/// Returns the source node of the given arc. |
3317 | 3317 |
Value operator[](const Key& arc) const { |
3318 | 3318 |
return _graph.source(arc); |
3319 | 3319 |
} |
3320 | 3320 |
|
3321 | 3321 |
private: |
3322 | 3322 |
const GR& _graph; |
3323 | 3323 |
}; |
3324 | 3324 |
|
3325 | 3325 |
/// \brief Returns a \c SourceMap class. |
3326 | 3326 |
/// |
3327 | 3327 |
/// This function just returns an \c SourceMap class. |
3328 | 3328 |
/// \relates SourceMap |
3329 | 3329 |
template <typename GR> |
3330 | 3330 |
inline SourceMap<GR> sourceMap(const GR& graph) { |
3331 | 3331 |
return SourceMap<GR>(graph); |
3332 | 3332 |
} |
3333 | 3333 |
|
3334 | 3334 |
/// \brief Map of the target nodes of arcs in a digraph. |
3335 | 3335 |
/// |
3336 | 3336 |
/// TargetMap provides access for the target node of each arc in a digraph, |
3337 | 3337 |
/// which is returned by the \c target() function of the digraph. |
3338 | 3338 |
/// \tparam GR The digraph type. |
3339 | 3339 |
/// \see SourceMap |
3340 | 3340 |
template <typename GR> |
3341 | 3341 |
class TargetMap { |
3342 | 3342 |
public: |
3343 | 3343 |
|
3344 | 3344 |
/// The key type (the \c Arc type of the digraph). |
3345 | 3345 |
typedef typename GR::Arc Key; |
3346 | 3346 |
/// The value type (the \c Node type of the digraph). |
3347 | 3347 |
typedef typename GR::Node Value; |
3348 | 3348 |
|
3349 | 3349 |
/// \brief Constructor |
3350 | 3350 |
/// |
3351 | 3351 |
/// Constructor. |
3352 | 3352 |
/// \param digraph The digraph that the map belongs to. |
3353 | 3353 |
explicit TargetMap(const GR& digraph) : _graph(digraph) {} |
3354 | 3354 |
|
3355 | 3355 |
/// \brief Returns the target node of the given arc. |
3356 | 3356 |
/// |
3357 | 3357 |
/// Returns the target node of the given arc. |
3358 | 3358 |
Value operator[](const Key& e) const { |
3359 | 3359 |
return _graph.target(e); |
3360 | 3360 |
} |
3361 | 3361 |
|
3362 | 3362 |
private: |
3363 | 3363 |
const GR& _graph; |
3364 | 3364 |
}; |
3365 | 3365 |
|
3366 | 3366 |
/// \brief Returns a \c TargetMap class. |
3367 | 3367 |
/// |
3368 | 3368 |
/// This function just returns a \c TargetMap class. |
3369 | 3369 |
/// \relates TargetMap |
3370 | 3370 |
template <typename GR> |
3371 | 3371 |
inline TargetMap<GR> targetMap(const GR& graph) { |
3372 | 3372 |
return TargetMap<GR>(graph); |
3373 | 3373 |
} |
3374 | 3374 |
|
3375 | 3375 |
/// \brief Map of the "forward" directed arc view of edges in a graph. |
3376 | 3376 |
/// |
3377 | 3377 |
/// ForwardMap provides access for the "forward" directed arc view of |
3378 | 3378 |
/// each edge in a graph, which is returned by the \c direct() function |
3379 | 3379 |
/// of the graph with \c true parameter. |
3380 | 3380 |
/// \tparam GR The graph type. |
3381 | 3381 |
/// \see BackwardMap |
3382 | 3382 |
template <typename GR> |
3383 | 3383 |
class ForwardMap { |
3384 | 3384 |
public: |
3385 | 3385 |
|
3386 | 3386 |
/// The key type (the \c Edge type of the digraph). |
3387 | 3387 |
typedef typename GR::Edge Key; |
3388 | 3388 |
/// The value type (the \c Arc type of the digraph). |
3389 | 3389 |
typedef typename GR::Arc Value; |
3390 | 3390 |
|
3391 | 3391 |
/// \brief Constructor |
3392 | 3392 |
/// |
3393 | 3393 |
/// Constructor. |
3394 | 3394 |
/// \param graph The graph that the map belongs to. |
3395 | 3395 |
explicit ForwardMap(const GR& graph) : _graph(graph) {} |
3396 | 3396 |
|
3397 | 3397 |
/// \brief Returns the "forward" directed arc view of the given edge. |
3398 | 3398 |
/// |
3399 | 3399 |
/// Returns the "forward" directed arc view of the given edge. |
3400 | 3400 |
Value operator[](const Key& key) const { |
3401 | 3401 |
return _graph.direct(key, true); |
3402 | 3402 |
} |
3403 | 3403 |
|
3404 | 3404 |
private: |
3405 | 3405 |
const GR& _graph; |
3406 | 3406 |
}; |
3407 | 3407 |
|
3408 | 3408 |
/// \brief Returns a \c ForwardMap class. |
3409 | 3409 |
/// |
3410 | 3410 |
/// This function just returns an \c ForwardMap class. |
3411 | 3411 |
/// \relates ForwardMap |
3412 | 3412 |
template <typename GR> |
3413 | 3413 |
inline ForwardMap<GR> forwardMap(const GR& graph) { |
3414 | 3414 |
return ForwardMap<GR>(graph); |
3415 | 3415 |
} |
3416 | 3416 |
|
3417 | 3417 |
/// \brief Map of the "backward" directed arc view of edges in a graph. |
3418 | 3418 |
/// |
3419 | 3419 |
/// BackwardMap provides access for the "backward" directed arc view of |
3420 | 3420 |
/// each edge in a graph, which is returned by the \c direct() function |
3421 | 3421 |
/// of the graph with \c false parameter. |
3422 | 3422 |
/// \tparam GR The graph type. |
3423 | 3423 |
/// \see ForwardMap |
3424 | 3424 |
template <typename GR> |
3425 | 3425 |
class BackwardMap { |
3426 | 3426 |
public: |
3427 | 3427 |
|
3428 | 3428 |
/// The key type (the \c Edge type of the digraph). |
3429 | 3429 |
typedef typename GR::Edge Key; |
3430 | 3430 |
/// The value type (the \c Arc type of the digraph). |
3431 | 3431 |
typedef typename GR::Arc Value; |
3432 | 3432 |
|
3433 | 3433 |
/// \brief Constructor |
3434 | 3434 |
/// |
3435 | 3435 |
/// Constructor. |
3436 | 3436 |
/// \param graph The graph that the map belongs to. |
3437 | 3437 |
explicit BackwardMap(const GR& graph) : _graph(graph) {} |
3438 | 3438 |
|
3439 | 3439 |
/// \brief Returns the "backward" directed arc view of the given edge. |
3440 | 3440 |
/// |
3441 | 3441 |
/// Returns the "backward" directed arc view of the given edge. |
3442 | 3442 |
Value operator[](const Key& key) const { |
3443 | 3443 |
return _graph.direct(key, false); |
3444 | 3444 |
} |
3445 | 3445 |
|
3446 | 3446 |
private: |
3447 | 3447 |
const GR& _graph; |
3448 | 3448 |
}; |
3449 | 3449 |
|
3450 | 3450 |
/// \brief Returns a \c BackwardMap class |
3451 | 3451 |
|
3452 | 3452 |
/// This function just returns a \c BackwardMap class. |
3453 | 3453 |
/// \relates BackwardMap |
3454 | 3454 |
template <typename GR> |
3455 | 3455 |
inline BackwardMap<GR> backwardMap(const GR& graph) { |
3456 | 3456 |
return BackwardMap<GR>(graph); |
3457 | 3457 |
} |
3458 | 3458 |
|
3459 | 3459 |
/// \brief Map of the in-degrees of nodes in a digraph. |
3460 | 3460 |
/// |
3461 | 3461 |
/// This map returns the in-degree of a node. Once it is constructed, |
3462 | 3462 |
/// the degrees are stored in a standard \c NodeMap, so each query is done |
3463 | 3463 |
/// in constant time. On the other hand, the values are updated automatically |
3464 | 3464 |
/// whenever the digraph changes. |
3465 | 3465 |
/// |
3466 | 3466 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure |
3467 | 3467 |
/// may provide alternative ways to modify the digraph. |
3468 | 3468 |
/// The correct behavior of InDegMap is not guarantied if these additional |
3469 |
/// features are used. For example the functions |
|
3469 |
/// features are used. For example, the functions |
|
3470 | 3470 |
/// \ref ListDigraph::changeSource() "changeSource()", |
3471 | 3471 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
3472 | 3472 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
3473 | 3473 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
3474 | 3474 |
/// |
3475 | 3475 |
/// \sa OutDegMap |
3476 | 3476 |
template <typename GR> |
3477 | 3477 |
class InDegMap |
3478 | 3478 |
: protected ItemSetTraits<GR, typename GR::Arc> |
3479 | 3479 |
::ItemNotifier::ObserverBase { |
3480 | 3480 |
|
3481 | 3481 |
public: |
3482 | 3482 |
|
3483 | 3483 |
/// The graph type of InDegMap |
3484 | 3484 |
typedef GR Graph; |
3485 | 3485 |
typedef GR Digraph; |
3486 | 3486 |
/// The key type |
3487 | 3487 |
typedef typename Digraph::Node Key; |
3488 | 3488 |
/// The value type |
3489 | 3489 |
typedef int Value; |
3490 | 3490 |
|
3491 | 3491 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
3492 | 3492 |
::ItemNotifier::ObserverBase Parent; |
3493 | 3493 |
|
3494 | 3494 |
private: |
3495 | 3495 |
|
3496 | 3496 |
class AutoNodeMap |
3497 | 3497 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
3498 | 3498 |
public: |
3499 | 3499 |
|
3500 | 3500 |
typedef typename ItemSetTraits<Digraph, Key>:: |
3501 | 3501 |
template Map<int>::Type Parent; |
3502 | 3502 |
|
3503 | 3503 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
3504 | 3504 |
|
3505 | 3505 |
virtual void add(const Key& key) { |
3506 | 3506 |
Parent::add(key); |
3507 | 3507 |
Parent::set(key, 0); |
3508 | 3508 |
} |
3509 | 3509 |
|
3510 | 3510 |
virtual void add(const std::vector<Key>& keys) { |
3511 | 3511 |
Parent::add(keys); |
3512 | 3512 |
for (int i = 0; i < int(keys.size()); ++i) { |
3513 | 3513 |
Parent::set(keys[i], 0); |
3514 | 3514 |
} |
3515 | 3515 |
} |
3516 | 3516 |
|
3517 | 3517 |
virtual void build() { |
3518 | 3518 |
Parent::build(); |
3519 | 3519 |
Key it; |
3520 | 3520 |
typename Parent::Notifier* nf = Parent::notifier(); |
3521 | 3521 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
3522 | 3522 |
Parent::set(it, 0); |
3523 | 3523 |
} |
3524 | 3524 |
} |
3525 | 3525 |
}; |
3526 | 3526 |
|
3527 | 3527 |
public: |
3528 | 3528 |
|
3529 | 3529 |
/// \brief Constructor. |
3530 | 3530 |
/// |
3531 | 3531 |
/// Constructor for creating an in-degree map. |
3532 | 3532 |
explicit InDegMap(const Digraph& graph) |
3533 | 3533 |
: _digraph(graph), _deg(graph) { |
3534 | 3534 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
3535 | 3535 |
|
3536 | 3536 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3537 | 3537 |
_deg[it] = countInArcs(_digraph, it); |
3538 | 3538 |
} |
3539 | 3539 |
} |
3540 | 3540 |
|
3541 | 3541 |
/// \brief Gives back the in-degree of a Node. |
3542 | 3542 |
/// |
3543 | 3543 |
/// Gives back the in-degree of a Node. |
3544 | 3544 |
int operator[](const Key& key) const { |
3545 | 3545 |
return _deg[key]; |
3546 | 3546 |
} |
3547 | 3547 |
|
3548 | 3548 |
protected: |
3549 | 3549 |
|
3550 | 3550 |
typedef typename Digraph::Arc Arc; |
3551 | 3551 |
|
3552 | 3552 |
virtual void add(const Arc& arc) { |
3553 | 3553 |
++_deg[_digraph.target(arc)]; |
3554 | 3554 |
} |
3555 | 3555 |
|
3556 | 3556 |
virtual void add(const std::vector<Arc>& arcs) { |
3557 | 3557 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3558 | 3558 |
++_deg[_digraph.target(arcs[i])]; |
3559 | 3559 |
} |
3560 | 3560 |
} |
3561 | 3561 |
|
3562 | 3562 |
virtual void erase(const Arc& arc) { |
3563 | 3563 |
--_deg[_digraph.target(arc)]; |
3564 | 3564 |
} |
3565 | 3565 |
|
3566 | 3566 |
virtual void erase(const std::vector<Arc>& arcs) { |
3567 | 3567 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3568 | 3568 |
--_deg[_digraph.target(arcs[i])]; |
3569 | 3569 |
} |
3570 | 3570 |
} |
3571 | 3571 |
|
3572 | 3572 |
virtual void build() { |
3573 | 3573 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3574 | 3574 |
_deg[it] = countInArcs(_digraph, it); |
3575 | 3575 |
} |
3576 | 3576 |
} |
3577 | 3577 |
|
3578 | 3578 |
virtual void clear() { |
3579 | 3579 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3580 | 3580 |
_deg[it] = 0; |
3581 | 3581 |
} |
3582 | 3582 |
} |
3583 | 3583 |
private: |
3584 | 3584 |
|
3585 | 3585 |
const Digraph& _digraph; |
3586 | 3586 |
AutoNodeMap _deg; |
3587 | 3587 |
}; |
3588 | 3588 |
|
3589 | 3589 |
/// \brief Map of the out-degrees of nodes in a digraph. |
3590 | 3590 |
/// |
3591 | 3591 |
/// This map returns the out-degree of a node. Once it is constructed, |
3592 | 3592 |
/// the degrees are stored in a standard \c NodeMap, so each query is done |
3593 | 3593 |
/// in constant time. On the other hand, the values are updated automatically |
3594 | 3594 |
/// whenever the digraph changes. |
3595 | 3595 |
/// |
3596 | 3596 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure |
3597 | 3597 |
/// may provide alternative ways to modify the digraph. |
3598 | 3598 |
/// The correct behavior of OutDegMap is not guarantied if these additional |
3599 |
/// features are used. For example the functions |
|
3599 |
/// features are used. For example, the functions |
|
3600 | 3600 |
/// \ref ListDigraph::changeSource() "changeSource()", |
3601 | 3601 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
3602 | 3602 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
3603 | 3603 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
3604 | 3604 |
/// |
3605 | 3605 |
/// \sa InDegMap |
3606 | 3606 |
template <typename GR> |
3607 | 3607 |
class OutDegMap |
3608 | 3608 |
: protected ItemSetTraits<GR, typename GR::Arc> |
3609 | 3609 |
::ItemNotifier::ObserverBase { |
3610 | 3610 |
|
3611 | 3611 |
public: |
3612 | 3612 |
|
3613 | 3613 |
/// The graph type of OutDegMap |
3614 | 3614 |
typedef GR Graph; |
3615 | 3615 |
typedef GR Digraph; |
3616 | 3616 |
/// The key type |
3617 | 3617 |
typedef typename Digraph::Node Key; |
3618 | 3618 |
/// The value type |
3619 | 3619 |
typedef int Value; |
3620 | 3620 |
|
3621 | 3621 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
3622 | 3622 |
::ItemNotifier::ObserverBase Parent; |
3623 | 3623 |
|
3624 | 3624 |
private: |
3625 | 3625 |
|
3626 | 3626 |
class AutoNodeMap |
3627 | 3627 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
3628 | 3628 |
public: |
3629 | 3629 |
|
3630 | 3630 |
typedef typename ItemSetTraits<Digraph, Key>:: |
3631 | 3631 |
template Map<int>::Type Parent; |
3632 | 3632 |
|
3633 | 3633 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
3634 | 3634 |
|
3635 | 3635 |
virtual void add(const Key& key) { |
3636 | 3636 |
Parent::add(key); |
3637 | 3637 |
Parent::set(key, 0); |
3638 | 3638 |
} |
3639 | 3639 |
virtual void add(const std::vector<Key>& keys) { |
3640 | 3640 |
Parent::add(keys); |
3641 | 3641 |
for (int i = 0; i < int(keys.size()); ++i) { |
3642 | 3642 |
Parent::set(keys[i], 0); |
3643 | 3643 |
} |
3644 | 3644 |
} |
3645 | 3645 |
virtual void build() { |
3646 | 3646 |
Parent::build(); |
3647 | 3647 |
Key it; |
3648 | 3648 |
typename Parent::Notifier* nf = Parent::notifier(); |
3649 | 3649 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
3650 | 3650 |
Parent::set(it, 0); |
3651 | 3651 |
} |
3652 | 3652 |
} |
3653 | 3653 |
}; |
3654 | 3654 |
|
3655 | 3655 |
public: |
3656 | 3656 |
|
3657 | 3657 |
/// \brief Constructor. |
3658 | 3658 |
/// |
3659 | 3659 |
/// Constructor for creating an out-degree map. |
3660 | 3660 |
explicit OutDegMap(const Digraph& graph) |
3661 | 3661 |
: _digraph(graph), _deg(graph) { |
3662 | 3662 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
3663 | 3663 |
|
3664 | 3664 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3665 | 3665 |
_deg[it] = countOutArcs(_digraph, it); |
3666 | 3666 |
} |
3667 | 3667 |
} |
3668 | 3668 |
|
3669 | 3669 |
/// \brief Gives back the out-degree of a Node. |
3670 | 3670 |
/// |
3671 | 3671 |
/// Gives back the out-degree of a Node. |
3672 | 3672 |
int operator[](const Key& key) const { |
3673 | 3673 |
return _deg[key]; |
3674 | 3674 |
} |
3675 | 3675 |
|
3676 | 3676 |
protected: |
3677 | 3677 |
|
3678 | 3678 |
typedef typename Digraph::Arc Arc; |
3679 | 3679 |
|
3680 | 3680 |
virtual void add(const Arc& arc) { |
3681 | 3681 |
++_deg[_digraph.source(arc)]; |
3682 | 3682 |
} |
3683 | 3683 |
|
3684 | 3684 |
virtual void add(const std::vector<Arc>& arcs) { |
3685 | 3685 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3686 | 3686 |
++_deg[_digraph.source(arcs[i])]; |
3687 | 3687 |
} |
3688 | 3688 |
} |
3689 | 3689 |
|
3690 | 3690 |
virtual void erase(const Arc& arc) { |
3691 | 3691 |
--_deg[_digraph.source(arc)]; |
3692 | 3692 |
} |
3693 | 3693 |
|
3694 | 3694 |
virtual void erase(const std::vector<Arc>& arcs) { |
3695 | 3695 |
for (int i = 0; i < int(arcs.size()); ++i) { |
3696 | 3696 |
--_deg[_digraph.source(arcs[i])]; |
3697 | 3697 |
} |
3698 | 3698 |
} |
3699 | 3699 |
|
3700 | 3700 |
virtual void build() { |
3701 | 3701 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3702 | 3702 |
_deg[it] = countOutArcs(_digraph, it); |
3703 | 3703 |
} |
3704 | 3704 |
} |
3705 | 3705 |
|
3706 | 3706 |
virtual void clear() { |
3707 | 3707 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
3708 | 3708 |
_deg[it] = 0; |
3709 | 3709 |
} |
3710 | 3710 |
} |
3711 | 3711 |
private: |
3712 | 3712 |
|
3713 | 3713 |
const Digraph& _digraph; |
3714 | 3714 |
AutoNodeMap _deg; |
3715 | 3715 |
}; |
3716 | 3716 |
|
3717 | 3717 |
/// \brief Potential difference map |
3718 | 3718 |
/// |
3719 | 3719 |
/// PotentialDifferenceMap returns the difference between the potentials of |
3720 | 3720 |
/// the source and target nodes of each arc in a digraph, i.e. it returns |
3721 | 3721 |
/// \code |
3722 | 3722 |
/// potential[gr.target(arc)] - potential[gr.source(arc)]. |
3723 | 3723 |
/// \endcode |
3724 | 3724 |
/// \tparam GR The digraph type. |
3725 | 3725 |
/// \tparam POT A node map storing the potentials. |
3726 | 3726 |
template <typename GR, typename POT> |
3727 | 3727 |
class PotentialDifferenceMap { |
3728 | 3728 |
public: |
3729 | 3729 |
/// Key type |
3730 | 3730 |
typedef typename GR::Arc Key; |
3731 | 3731 |
/// Value type |
3732 | 3732 |
typedef typename POT::Value Value; |
3733 | 3733 |
|
3734 | 3734 |
/// \brief Constructor |
3735 | 3735 |
/// |
3736 | 3736 |
/// Contructor of the map. |
3737 | 3737 |
explicit PotentialDifferenceMap(const GR& gr, |
3738 | 3738 |
const POT& potential) |
3739 | 3739 |
: _digraph(gr), _potential(potential) {} |
3740 | 3740 |
|
3741 | 3741 |
/// \brief Returns the potential difference for the given arc. |
3742 | 3742 |
/// |
3743 | 3743 |
/// Returns the potential difference for the given arc, i.e. |
3744 | 3744 |
/// \code |
3745 | 3745 |
/// potential[gr.target(arc)] - potential[gr.source(arc)]. |
3746 | 3746 |
/// \endcode |
3747 | 3747 |
Value operator[](const Key& arc) const { |
3748 | 3748 |
return _potential[_digraph.target(arc)] - |
3749 | 3749 |
_potential[_digraph.source(arc)]; |
3750 | 3750 |
} |
3751 | 3751 |
|
3752 | 3752 |
private: |
3753 | 3753 |
const GR& _digraph; |
3754 | 3754 |
const POT& _potential; |
3755 | 3755 |
}; |
3756 | 3756 |
|
3757 | 3757 |
/// \brief Returns a PotentialDifferenceMap. |
3758 | 3758 |
/// |
3759 | 3759 |
/// This function just returns a PotentialDifferenceMap. |
3760 | 3760 |
/// \relates PotentialDifferenceMap |
3761 | 3761 |
template <typename GR, typename POT> |
3762 | 3762 |
PotentialDifferenceMap<GR, POT> |
3763 | 3763 |
potentialDifferenceMap(const GR& gr, const POT& potential) { |
3764 | 3764 |
return PotentialDifferenceMap<GR, POT>(gr, potential); |
3765 | 3765 |
} |
3766 | 3766 |
|
3767 | 3767 |
/// @} |
3768 | 3768 |
} |
3769 | 3769 |
|
3770 | 3770 |
#endif // LEMON_MAPS_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_NETWORK_SIMPLEX_H |
20 | 20 |
#define LEMON_NETWORK_SIMPLEX_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Network Simplex algorithm for finding a minimum cost flow. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <algorithm> |
30 | 30 |
|
31 | 31 |
#include <lemon/core.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup min_cost_flow_algs |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
/// \brief Implementation of the primal Network Simplex algorithm |
40 | 40 |
/// for finding a \ref min_cost_flow "minimum cost flow". |
41 | 41 |
/// |
42 | 42 |
/// \ref NetworkSimplex implements the primal Network Simplex algorithm |
43 | 43 |
/// for finding a \ref min_cost_flow "minimum cost flow". |
44 | 44 |
/// This algorithm is a specialized version of the linear programming |
45 | 45 |
/// simplex method directly for the minimum cost flow problem. |
46 | 46 |
/// It is one of the most efficient solution methods. |
47 | 47 |
/// |
48 | 48 |
/// In general this class is the fastest implementation available |
49 | 49 |
/// in LEMON for the minimum cost flow problem. |
50 | 50 |
/// Moreover it supports both directions of the supply/demand inequality |
51 |
/// constraints. For more information see \ref SupplyType. |
|
51 |
/// constraints. For more information, see \ref SupplyType. |
|
52 | 52 |
/// |
53 | 53 |
/// Most of the parameters of the problem (except for the digraph) |
54 | 54 |
/// can be given using separate functions, and the algorithm can be |
55 | 55 |
/// executed using the \ref run() function. If some parameters are not |
56 | 56 |
/// specified, then default values will be used. |
57 | 57 |
/// |
58 | 58 |
/// \tparam GR The digraph type the algorithm runs on. |
59 | 59 |
/// \tparam V The value type used for flow amounts, capacity bounds |
60 |
/// and supply values in the algorithm. By default it is \c int. |
|
60 |
/// and supply values in the algorithm. By default, it is \c int. |
|
61 | 61 |
/// \tparam C The value type used for costs and potentials in the |
62 |
/// algorithm. By default it is the same as \c V. |
|
62 |
/// algorithm. By default, it is the same as \c V. |
|
63 | 63 |
/// |
64 | 64 |
/// \warning Both value types must be signed and all input data must |
65 | 65 |
/// be integer. |
66 | 66 |
/// |
67 | 67 |
/// \note %NetworkSimplex provides five different pivot rule |
68 | 68 |
/// implementations, from which the most efficient one is used |
69 |
/// by default. For more information see \ref PivotRule. |
|
69 |
/// by default. For more information, see \ref PivotRule. |
|
70 | 70 |
template <typename GR, typename V = int, typename C = V> |
71 | 71 |
class NetworkSimplex |
72 | 72 |
{ |
73 | 73 |
public: |
74 | 74 |
|
75 | 75 |
/// The type of the flow amounts, capacity bounds and supply values |
76 | 76 |
typedef V Value; |
77 | 77 |
/// The type of the arc costs |
78 | 78 |
typedef C Cost; |
79 | 79 |
|
80 | 80 |
public: |
81 | 81 |
|
82 | 82 |
/// \brief Problem type constants for the \c run() function. |
83 | 83 |
/// |
84 | 84 |
/// Enum type containing the problem type constants that can be |
85 | 85 |
/// returned by the \ref run() function of the algorithm. |
86 | 86 |
enum ProblemType { |
87 | 87 |
/// The problem has no feasible solution (flow). |
88 | 88 |
INFEASIBLE, |
89 | 89 |
/// The problem has optimal solution (i.e. it is feasible and |
90 | 90 |
/// bounded), and the algorithm has found optimal flow and node |
91 | 91 |
/// potentials (primal and dual solutions). |
92 | 92 |
OPTIMAL, |
93 | 93 |
/// The objective function of the problem is unbounded, i.e. |
94 | 94 |
/// there is a directed cycle having negative total cost and |
95 | 95 |
/// infinite upper bound. |
96 | 96 |
UNBOUNDED |
97 | 97 |
}; |
98 | 98 |
|
99 | 99 |
/// \brief Constants for selecting the type of the supply constraints. |
100 | 100 |
/// |
101 | 101 |
/// Enum type containing constants for selecting the supply type, |
102 | 102 |
/// i.e. the direction of the inequalities in the supply/demand |
103 | 103 |
/// constraints of the \ref min_cost_flow "minimum cost flow problem". |
104 | 104 |
/// |
105 | 105 |
/// The default supply type is \c GEQ, the \c LEQ type can be |
106 | 106 |
/// selected using \ref supplyType(). |
107 | 107 |
/// The equality form is a special case of both supply types. |
108 | 108 |
enum SupplyType { |
109 | 109 |
/// This option means that there are <em>"greater or equal"</em> |
110 | 110 |
/// supply/demand constraints in the definition of the problem. |
111 | 111 |
GEQ, |
112 | 112 |
/// This option means that there are <em>"less or equal"</em> |
113 | 113 |
/// supply/demand constraints in the definition of the problem. |
114 | 114 |
LEQ |
115 | 115 |
}; |
116 | 116 |
|
117 | 117 |
/// \brief Constants for selecting the pivot rule. |
118 | 118 |
/// |
119 | 119 |
/// Enum type containing constants for selecting the pivot rule for |
120 | 120 |
/// the \ref run() function. |
121 | 121 |
/// |
122 | 122 |
/// \ref NetworkSimplex provides five different pivot rule |
123 | 123 |
/// implementations that significantly affect the running time |
124 | 124 |
/// of the algorithm. |
125 |
/// By default \ref BLOCK_SEARCH "Block Search" is used, which |
|
125 |
/// By default, \ref BLOCK_SEARCH "Block Search" is used, which |
|
126 | 126 |
/// proved to be the most efficient and the most robust on various |
127 | 127 |
/// test inputs according to our benchmark tests. |
128 |
/// However another pivot rule can be selected using the \ref run() |
|
128 |
/// However, another pivot rule can be selected using the \ref run() |
|
129 | 129 |
/// function with the proper parameter. |
130 | 130 |
enum PivotRule { |
131 | 131 |
|
132 |
/// The First Eligible pivot rule. |
|
132 |
/// The \e First \e Eligible pivot rule. |
|
133 | 133 |
/// The next eligible arc is selected in a wraparound fashion |
134 | 134 |
/// in every iteration. |
135 | 135 |
FIRST_ELIGIBLE, |
136 | 136 |
|
137 |
/// The Best Eligible pivot rule. |
|
137 |
/// The \e Best \e Eligible pivot rule. |
|
138 | 138 |
/// The best eligible arc is selected in every iteration. |
139 | 139 |
BEST_ELIGIBLE, |
140 | 140 |
|
141 |
/// The Block Search pivot rule. |
|
141 |
/// The \e Block \e Search pivot rule. |
|
142 | 142 |
/// A specified number of arcs are examined in every iteration |
143 | 143 |
/// in a wraparound fashion and the best eligible arc is selected |
144 | 144 |
/// from this block. |
145 | 145 |
BLOCK_SEARCH, |
146 | 146 |
|
147 |
/// The Candidate List pivot rule. |
|
147 |
/// The \e Candidate \e List pivot rule. |
|
148 | 148 |
/// In a major iteration a candidate list is built from eligible arcs |
149 | 149 |
/// in a wraparound fashion and in the following minor iterations |
150 | 150 |
/// the best eligible arc is selected from this list. |
151 | 151 |
CANDIDATE_LIST, |
152 | 152 |
|
153 |
/// The Altering Candidate List pivot rule. |
|
153 |
/// The \e Altering \e Candidate \e List pivot rule. |
|
154 | 154 |
/// It is a modified version of the Candidate List method. |
155 | 155 |
/// It keeps only the several best eligible arcs from the former |
156 | 156 |
/// candidate list and extends this list in every iteration. |
157 | 157 |
ALTERING_LIST |
158 | 158 |
}; |
159 | 159 |
|
160 | 160 |
private: |
161 | 161 |
|
162 | 162 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
163 | 163 |
|
164 | 164 |
typedef std::vector<int> IntVector; |
165 | 165 |
typedef std::vector<bool> BoolVector; |
166 | 166 |
typedef std::vector<Value> ValueVector; |
167 | 167 |
typedef std::vector<Cost> CostVector; |
168 | 168 |
|
169 | 169 |
// State constants for arcs |
170 | 170 |
enum ArcStateEnum { |
171 | 171 |
STATE_UPPER = -1, |
172 | 172 |
STATE_TREE = 0, |
173 | 173 |
STATE_LOWER = 1 |
174 | 174 |
}; |
175 | 175 |
|
176 | 176 |
private: |
177 | 177 |
|
178 | 178 |
// Data related to the underlying digraph |
179 | 179 |
const GR &_graph; |
180 | 180 |
int _node_num; |
181 | 181 |
int _arc_num; |
182 | 182 |
int _all_arc_num; |
183 | 183 |
int _search_arc_num; |
184 | 184 |
|
185 | 185 |
// Parameters of the problem |
186 | 186 |
bool _have_lower; |
187 | 187 |
SupplyType _stype; |
188 | 188 |
Value _sum_supply; |
189 | 189 |
|
190 | 190 |
// Data structures for storing the digraph |
191 | 191 |
IntNodeMap _node_id; |
192 | 192 |
IntArcMap _arc_id; |
193 | 193 |
IntVector _source; |
194 | 194 |
IntVector _target; |
195 | 195 |
|
196 | 196 |
// Node and arc data |
197 | 197 |
ValueVector _lower; |
198 | 198 |
ValueVector _upper; |
199 | 199 |
ValueVector _cap; |
200 | 200 |
CostVector _cost; |
201 | 201 |
ValueVector _supply; |
202 | 202 |
ValueVector _flow; |
203 | 203 |
CostVector _pi; |
204 | 204 |
|
205 | 205 |
// Data for storing the spanning tree structure |
206 | 206 |
IntVector _parent; |
207 | 207 |
IntVector _pred; |
208 | 208 |
IntVector _thread; |
209 | 209 |
IntVector _rev_thread; |
210 | 210 |
IntVector _succ_num; |
211 | 211 |
IntVector _last_succ; |
212 | 212 |
IntVector _dirty_revs; |
213 | 213 |
BoolVector _forward; |
214 | 214 |
IntVector _state; |
215 | 215 |
int _root; |
216 | 216 |
|
217 | 217 |
// Temporary data used in the current pivot iteration |
218 | 218 |
int in_arc, join, u_in, v_in, u_out, v_out; |
219 | 219 |
int first, second, right, last; |
220 | 220 |
int stem, par_stem, new_stem; |
221 | 221 |
Value delta; |
222 | 222 |
|
223 | 223 |
public: |
224 | 224 |
|
225 | 225 |
/// \brief Constant for infinite upper bounds (capacities). |
226 | 226 |
/// |
227 | 227 |
/// Constant for infinite upper bounds (capacities). |
228 | 228 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
229 | 229 |
/// \c std::numeric_limits<Value>::max() otherwise. |
230 | 230 |
const Value INF; |
231 | 231 |
|
232 | 232 |
private: |
233 | 233 |
|
234 | 234 |
// Implementation of the First Eligible pivot rule |
235 | 235 |
class FirstEligiblePivotRule |
236 | 236 |
{ |
237 | 237 |
private: |
238 | 238 |
|
239 | 239 |
// References to the NetworkSimplex class |
240 | 240 |
const IntVector &_source; |
241 | 241 |
const IntVector &_target; |
242 | 242 |
const CostVector &_cost; |
243 | 243 |
const IntVector &_state; |
244 | 244 |
const CostVector &_pi; |
245 | 245 |
int &_in_arc; |
246 | 246 |
int _search_arc_num; |
247 | 247 |
|
248 | 248 |
// Pivot rule data |
249 | 249 |
int _next_arc; |
250 | 250 |
|
251 | 251 |
public: |
252 | 252 |
|
253 | 253 |
// Constructor |
254 | 254 |
FirstEligiblePivotRule(NetworkSimplex &ns) : |
255 | 255 |
_source(ns._source), _target(ns._target), |
256 | 256 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
257 | 257 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
258 | 258 |
_next_arc(0) |
259 | 259 |
{} |
260 | 260 |
|
261 | 261 |
// Find next entering arc |
262 | 262 |
bool findEnteringArc() { |
263 | 263 |
Cost c; |
264 | 264 |
for (int e = _next_arc; e < _search_arc_num; ++e) { |
265 | 265 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
266 | 266 |
if (c < 0) { |
267 | 267 |
_in_arc = e; |
268 | 268 |
_next_arc = e + 1; |
269 | 269 |
return true; |
270 | 270 |
} |
271 | 271 |
} |
272 | 272 |
for (int e = 0; e < _next_arc; ++e) { |
273 | 273 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
274 | 274 |
if (c < 0) { |
275 | 275 |
_in_arc = e; |
276 | 276 |
_next_arc = e + 1; |
277 | 277 |
return true; |
278 | 278 |
} |
279 | 279 |
} |
280 | 280 |
return false; |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
}; //class FirstEligiblePivotRule |
284 | 284 |
|
285 | 285 |
|
286 | 286 |
// Implementation of the Best Eligible pivot rule |
287 | 287 |
class BestEligiblePivotRule |
288 | 288 |
{ |
289 | 289 |
private: |
290 | 290 |
|
291 | 291 |
// References to the NetworkSimplex class |
292 | 292 |
const IntVector &_source; |
293 | 293 |
const IntVector &_target; |
294 | 294 |
const CostVector &_cost; |
295 | 295 |
const IntVector &_state; |
296 | 296 |
const CostVector &_pi; |
297 | 297 |
int &_in_arc; |
298 | 298 |
int _search_arc_num; |
299 | 299 |
|
300 | 300 |
public: |
301 | 301 |
|
302 | 302 |
// Constructor |
303 | 303 |
BestEligiblePivotRule(NetworkSimplex &ns) : |
304 | 304 |
_source(ns._source), _target(ns._target), |
305 | 305 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
306 | 306 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num) |
307 | 307 |
{} |
308 | 308 |
|
309 | 309 |
// Find next entering arc |
310 | 310 |
bool findEnteringArc() { |
311 | 311 |
Cost c, min = 0; |
312 | 312 |
for (int e = 0; e < _search_arc_num; ++e) { |
313 | 313 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
314 | 314 |
if (c < min) { |
315 | 315 |
min = c; |
316 | 316 |
_in_arc = e; |
317 | 317 |
} |
318 | 318 |
} |
319 | 319 |
return min < 0; |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
}; //class BestEligiblePivotRule |
323 | 323 |
|
324 | 324 |
|
325 | 325 |
// Implementation of the Block Search pivot rule |
326 | 326 |
class BlockSearchPivotRule |
327 | 327 |
{ |
328 | 328 |
private: |
329 | 329 |
|
330 | 330 |
// References to the NetworkSimplex class |
331 | 331 |
const IntVector &_source; |
332 | 332 |
const IntVector &_target; |
333 | 333 |
const CostVector &_cost; |
334 | 334 |
const IntVector &_state; |
335 | 335 |
const CostVector &_pi; |
336 | 336 |
int &_in_arc; |
337 | 337 |
int _search_arc_num; |
338 | 338 |
|
339 | 339 |
// Pivot rule data |
340 | 340 |
int _block_size; |
341 | 341 |
int _next_arc; |
342 | 342 |
|
343 | 343 |
public: |
344 | 344 |
|
345 | 345 |
// Constructor |
... | ... |
@@ -621,446 +621,446 @@ |
621 | 621 |
/// \brief Constructor. |
622 | 622 |
/// |
623 | 623 |
/// The constructor of the class. |
624 | 624 |
/// |
625 | 625 |
/// \param graph The digraph the algorithm runs on. |
626 | 626 |
/// \param arc_mixing Indicate if the arcs have to be stored in a |
627 | 627 |
/// mixed order in the internal data structure. |
628 | 628 |
/// In special cases, it could lead to better overall performance, |
629 | 629 |
/// but it is usually slower. Therefore it is disabled by default. |
630 | 630 |
NetworkSimplex(const GR& graph, bool arc_mixing = false) : |
631 | 631 |
_graph(graph), _node_id(graph), _arc_id(graph), |
632 | 632 |
INF(std::numeric_limits<Value>::has_infinity ? |
633 | 633 |
std::numeric_limits<Value>::infinity() : |
634 | 634 |
std::numeric_limits<Value>::max()) |
635 | 635 |
{ |
636 | 636 |
// Check the value types |
637 | 637 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
638 | 638 |
"The flow type of NetworkSimplex must be signed"); |
639 | 639 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
640 | 640 |
"The cost type of NetworkSimplex must be signed"); |
641 | 641 |
|
642 | 642 |
// Resize vectors |
643 | 643 |
_node_num = countNodes(_graph); |
644 | 644 |
_arc_num = countArcs(_graph); |
645 | 645 |
int all_node_num = _node_num + 1; |
646 | 646 |
int max_arc_num = _arc_num + 2 * _node_num; |
647 | 647 |
|
648 | 648 |
_source.resize(max_arc_num); |
649 | 649 |
_target.resize(max_arc_num); |
650 | 650 |
|
651 | 651 |
_lower.resize(_arc_num); |
652 | 652 |
_upper.resize(_arc_num); |
653 | 653 |
_cap.resize(max_arc_num); |
654 | 654 |
_cost.resize(max_arc_num); |
655 | 655 |
_supply.resize(all_node_num); |
656 | 656 |
_flow.resize(max_arc_num); |
657 | 657 |
_pi.resize(all_node_num); |
658 | 658 |
|
659 | 659 |
_parent.resize(all_node_num); |
660 | 660 |
_pred.resize(all_node_num); |
661 | 661 |
_forward.resize(all_node_num); |
662 | 662 |
_thread.resize(all_node_num); |
663 | 663 |
_rev_thread.resize(all_node_num); |
664 | 664 |
_succ_num.resize(all_node_num); |
665 | 665 |
_last_succ.resize(all_node_num); |
666 | 666 |
_state.resize(max_arc_num); |
667 | 667 |
|
668 | 668 |
// Copy the graph |
669 | 669 |
int i = 0; |
670 | 670 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
671 | 671 |
_node_id[n] = i; |
672 | 672 |
} |
673 | 673 |
if (arc_mixing) { |
674 | 674 |
// Store the arcs in a mixed order |
675 | 675 |
int k = std::max(int(std::sqrt(double(_arc_num))), 10); |
676 | 676 |
int i = 0, j = 0; |
677 | 677 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
678 | 678 |
_arc_id[a] = i; |
679 | 679 |
_source[i] = _node_id[_graph.source(a)]; |
680 | 680 |
_target[i] = _node_id[_graph.target(a)]; |
681 | 681 |
if ((i += k) >= _arc_num) i = ++j; |
682 | 682 |
} |
683 | 683 |
} else { |
684 | 684 |
// Store the arcs in the original order |
685 | 685 |
int i = 0; |
686 | 686 |
for (ArcIt a(_graph); a != INVALID; ++a, ++i) { |
687 | 687 |
_arc_id[a] = i; |
688 | 688 |
_source[i] = _node_id[_graph.source(a)]; |
689 | 689 |
_target[i] = _node_id[_graph.target(a)]; |
690 | 690 |
} |
691 | 691 |
} |
692 | 692 |
|
693 | 693 |
// Reset parameters |
694 | 694 |
reset(); |
695 | 695 |
} |
696 | 696 |
|
697 | 697 |
/// \name Parameters |
698 | 698 |
/// The parameters of the algorithm can be specified using these |
699 | 699 |
/// functions. |
700 | 700 |
|
701 | 701 |
/// @{ |
702 | 702 |
|
703 | 703 |
/// \brief Set the lower bounds on the arcs. |
704 | 704 |
/// |
705 | 705 |
/// This function sets the lower bounds on the arcs. |
706 | 706 |
/// If it is not used before calling \ref run(), the lower bounds |
707 | 707 |
/// will be set to zero on all arcs. |
708 | 708 |
/// |
709 | 709 |
/// \param map An arc map storing the lower bounds. |
710 | 710 |
/// Its \c Value type must be convertible to the \c Value type |
711 | 711 |
/// of the algorithm. |
712 | 712 |
/// |
713 | 713 |
/// \return <tt>(*this)</tt> |
714 | 714 |
template <typename LowerMap> |
715 | 715 |
NetworkSimplex& lowerMap(const LowerMap& map) { |
716 | 716 |
_have_lower = true; |
717 | 717 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
718 | 718 |
_lower[_arc_id[a]] = map[a]; |
719 | 719 |
} |
720 | 720 |
return *this; |
721 | 721 |
} |
722 | 722 |
|
723 | 723 |
/// \brief Set the upper bounds (capacities) on the arcs. |
724 | 724 |
/// |
725 | 725 |
/// This function sets the upper bounds (capacities) on the arcs. |
726 | 726 |
/// If it is not used before calling \ref run(), the upper bounds |
727 | 727 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
728 | 728 |
/// unbounded from above on each arc). |
729 | 729 |
/// |
730 | 730 |
/// \param map An arc map storing the upper bounds. |
731 | 731 |
/// Its \c Value type must be convertible to the \c Value type |
732 | 732 |
/// of the algorithm. |
733 | 733 |
/// |
734 | 734 |
/// \return <tt>(*this)</tt> |
735 | 735 |
template<typename UpperMap> |
736 | 736 |
NetworkSimplex& upperMap(const UpperMap& map) { |
737 | 737 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
738 | 738 |
_upper[_arc_id[a]] = map[a]; |
739 | 739 |
} |
740 | 740 |
return *this; |
741 | 741 |
} |
742 | 742 |
|
743 | 743 |
/// \brief Set the costs of the arcs. |
744 | 744 |
/// |
745 | 745 |
/// This function sets the costs of the arcs. |
746 | 746 |
/// If it is not used before calling \ref run(), the costs |
747 | 747 |
/// will be set to \c 1 on all arcs. |
748 | 748 |
/// |
749 | 749 |
/// \param map An arc map storing the costs. |
750 | 750 |
/// Its \c Value type must be convertible to the \c Cost type |
751 | 751 |
/// of the algorithm. |
752 | 752 |
/// |
753 | 753 |
/// \return <tt>(*this)</tt> |
754 | 754 |
template<typename CostMap> |
755 | 755 |
NetworkSimplex& costMap(const CostMap& map) { |
756 | 756 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
757 | 757 |
_cost[_arc_id[a]] = map[a]; |
758 | 758 |
} |
759 | 759 |
return *this; |
760 | 760 |
} |
761 | 761 |
|
762 | 762 |
/// \brief Set the supply values of the nodes. |
763 | 763 |
/// |
764 | 764 |
/// This function sets the supply values of the nodes. |
765 | 765 |
/// If neither this function nor \ref stSupply() is used before |
766 | 766 |
/// calling \ref run(), the supply of each node will be set to zero. |
767 | 767 |
/// |
768 | 768 |
/// \param map A node map storing the supply values. |
769 | 769 |
/// Its \c Value type must be convertible to the \c Value type |
770 | 770 |
/// of the algorithm. |
771 | 771 |
/// |
772 | 772 |
/// \return <tt>(*this)</tt> |
773 | 773 |
template<typename SupplyMap> |
774 | 774 |
NetworkSimplex& supplyMap(const SupplyMap& map) { |
775 | 775 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
776 | 776 |
_supply[_node_id[n]] = map[n]; |
777 | 777 |
} |
778 | 778 |
return *this; |
779 | 779 |
} |
780 | 780 |
|
781 | 781 |
/// \brief Set single source and target nodes and a supply value. |
782 | 782 |
/// |
783 | 783 |
/// This function sets a single source node and a single target node |
784 | 784 |
/// and the required flow value. |
785 | 785 |
/// If neither this function nor \ref supplyMap() is used before |
786 | 786 |
/// calling \ref run(), the supply of each node will be set to zero. |
787 | 787 |
/// |
788 | 788 |
/// Using this function has the same effect as using \ref supplyMap() |
789 | 789 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
790 | 790 |
/// assigned to \c t and all other nodes have zero supply value. |
791 | 791 |
/// |
792 | 792 |
/// \param s The source node. |
793 | 793 |
/// \param t The target node. |
794 | 794 |
/// \param k The required amount of flow from node \c s to node \c t |
795 | 795 |
/// (i.e. the supply of \c s and the demand of \c t). |
796 | 796 |
/// |
797 | 797 |
/// \return <tt>(*this)</tt> |
798 | 798 |
NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) { |
799 | 799 |
for (int i = 0; i != _node_num; ++i) { |
800 | 800 |
_supply[i] = 0; |
801 | 801 |
} |
802 | 802 |
_supply[_node_id[s]] = k; |
803 | 803 |
_supply[_node_id[t]] = -k; |
804 | 804 |
return *this; |
805 | 805 |
} |
806 | 806 |
|
807 | 807 |
/// \brief Set the type of the supply constraints. |
808 | 808 |
/// |
809 | 809 |
/// This function sets the type of the supply/demand constraints. |
810 | 810 |
/// If it is not used before calling \ref run(), the \ref GEQ supply |
811 | 811 |
/// type will be used. |
812 | 812 |
/// |
813 |
/// For more information see \ref SupplyType. |
|
813 |
/// For more information, see \ref SupplyType. |
|
814 | 814 |
/// |
815 | 815 |
/// \return <tt>(*this)</tt> |
816 | 816 |
NetworkSimplex& supplyType(SupplyType supply_type) { |
817 | 817 |
_stype = supply_type; |
818 | 818 |
return *this; |
819 | 819 |
} |
820 | 820 |
|
821 | 821 |
/// @} |
822 | 822 |
|
823 | 823 |
/// \name Execution Control |
824 | 824 |
/// The algorithm can be executed using \ref run(). |
825 | 825 |
|
826 | 826 |
/// @{ |
827 | 827 |
|
828 | 828 |
/// \brief Run the algorithm. |
829 | 829 |
/// |
830 | 830 |
/// This function runs the algorithm. |
831 | 831 |
/// The paramters can be specified using functions \ref lowerMap(), |
832 | 832 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
833 | 833 |
/// \ref supplyType(). |
834 | 834 |
/// For example, |
835 | 835 |
/// \code |
836 | 836 |
/// NetworkSimplex<ListDigraph> ns(graph); |
837 | 837 |
/// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
838 | 838 |
/// .supplyMap(sup).run(); |
839 | 839 |
/// \endcode |
840 | 840 |
/// |
841 | 841 |
/// This function can be called more than once. All the parameters |
842 | 842 |
/// that have been given are kept for the next call, unless |
843 | 843 |
/// \ref reset() is called, thus only the modified parameters |
844 | 844 |
/// have to be set again. See \ref reset() for examples. |
845 |
/// However the underlying digraph must not be modified after this |
|
845 |
/// However, the underlying digraph must not be modified after this |
|
846 | 846 |
/// class have been constructed, since it copies and extends the graph. |
847 | 847 |
/// |
848 | 848 |
/// \param pivot_rule The pivot rule that will be used during the |
849 |
/// algorithm. For more information see \ref PivotRule. |
|
849 |
/// algorithm. For more information, see \ref PivotRule. |
|
850 | 850 |
/// |
851 | 851 |
/// \return \c INFEASIBLE if no feasible flow exists, |
852 | 852 |
/// \n \c OPTIMAL if the problem has optimal solution |
853 | 853 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
854 | 854 |
/// optimal flow and node potentials (primal and dual solutions), |
855 | 855 |
/// \n \c UNBOUNDED if the objective function of the problem is |
856 | 856 |
/// unbounded, i.e. there is a directed cycle having negative total |
857 | 857 |
/// cost and infinite upper bound. |
858 | 858 |
/// |
859 | 859 |
/// \see ProblemType, PivotRule |
860 | 860 |
ProblemType run(PivotRule pivot_rule = BLOCK_SEARCH) { |
861 | 861 |
if (!init()) return INFEASIBLE; |
862 | 862 |
return start(pivot_rule); |
863 | 863 |
} |
864 | 864 |
|
865 | 865 |
/// \brief Reset all the parameters that have been given before. |
866 | 866 |
/// |
867 | 867 |
/// This function resets all the paramaters that have been given |
868 | 868 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
869 | 869 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(), \ref supplyType(). |
870 | 870 |
/// |
871 | 871 |
/// It is useful for multiple run() calls. If this function is not |
872 | 872 |
/// used, all the parameters given before are kept for the next |
873 | 873 |
/// \ref run() call. |
874 |
/// However the underlying digraph must not be modified after this |
|
874 |
/// However, the underlying digraph must not be modified after this |
|
875 | 875 |
/// class have been constructed, since it copies and extends the graph. |
876 | 876 |
/// |
877 | 877 |
/// For example, |
878 | 878 |
/// \code |
879 | 879 |
/// NetworkSimplex<ListDigraph> ns(graph); |
880 | 880 |
/// |
881 | 881 |
/// // First run |
882 | 882 |
/// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
883 | 883 |
/// .supplyMap(sup).run(); |
884 | 884 |
/// |
885 | 885 |
/// // Run again with modified cost map (reset() is not called, |
886 | 886 |
/// // so only the cost map have to be set again) |
887 | 887 |
/// cost[e] += 100; |
888 | 888 |
/// ns.costMap(cost).run(); |
889 | 889 |
/// |
890 | 890 |
/// // Run again from scratch using reset() |
891 | 891 |
/// // (the lower bounds will be set to zero on all arcs) |
892 | 892 |
/// ns.reset(); |
893 | 893 |
/// ns.upperMap(capacity).costMap(cost) |
894 | 894 |
/// .supplyMap(sup).run(); |
895 | 895 |
/// \endcode |
896 | 896 |
/// |
897 | 897 |
/// \return <tt>(*this)</tt> |
898 | 898 |
NetworkSimplex& reset() { |
899 | 899 |
for (int i = 0; i != _node_num; ++i) { |
900 | 900 |
_supply[i] = 0; |
901 | 901 |
} |
902 | 902 |
for (int i = 0; i != _arc_num; ++i) { |
903 | 903 |
_lower[i] = 0; |
904 | 904 |
_upper[i] = INF; |
905 | 905 |
_cost[i] = 1; |
906 | 906 |
} |
907 | 907 |
_have_lower = false; |
908 | 908 |
_stype = GEQ; |
909 | 909 |
return *this; |
910 | 910 |
} |
911 | 911 |
|
912 | 912 |
/// @} |
913 | 913 |
|
914 | 914 |
/// \name Query Functions |
915 | 915 |
/// The results of the algorithm can be obtained using these |
916 | 916 |
/// functions.\n |
917 | 917 |
/// The \ref run() function must be called before using them. |
918 | 918 |
|
919 | 919 |
/// @{ |
920 | 920 |
|
921 | 921 |
/// \brief Return the total cost of the found flow. |
922 | 922 |
/// |
923 | 923 |
/// This function returns the total cost of the found flow. |
924 | 924 |
/// Its complexity is O(e). |
925 | 925 |
/// |
926 | 926 |
/// \note The return type of the function can be specified as a |
927 | 927 |
/// template parameter. For example, |
928 | 928 |
/// \code |
929 | 929 |
/// ns.totalCost<double>(); |
930 | 930 |
/// \endcode |
931 | 931 |
/// It is useful if the total cost cannot be stored in the \c Cost |
932 | 932 |
/// type of the algorithm, which is the default return type of the |
933 | 933 |
/// function. |
934 | 934 |
/// |
935 | 935 |
/// \pre \ref run() must be called before using this function. |
936 | 936 |
template <typename Number> |
937 | 937 |
Number totalCost() const { |
938 | 938 |
Number c = 0; |
939 | 939 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
940 | 940 |
int i = _arc_id[a]; |
941 | 941 |
c += Number(_flow[i]) * Number(_cost[i]); |
942 | 942 |
} |
943 | 943 |
return c; |
944 | 944 |
} |
945 | 945 |
|
946 | 946 |
#ifndef DOXYGEN |
947 | 947 |
Cost totalCost() const { |
948 | 948 |
return totalCost<Cost>(); |
949 | 949 |
} |
950 | 950 |
#endif |
951 | 951 |
|
952 | 952 |
/// \brief Return the flow on the given arc. |
953 | 953 |
/// |
954 | 954 |
/// This function returns the flow on the given arc. |
955 | 955 |
/// |
956 | 956 |
/// \pre \ref run() must be called before using this function. |
957 | 957 |
Value flow(const Arc& a) const { |
958 | 958 |
return _flow[_arc_id[a]]; |
959 | 959 |
} |
960 | 960 |
|
961 | 961 |
/// \brief Return the flow map (the primal solution). |
962 | 962 |
/// |
963 | 963 |
/// This function copies the flow value on each arc into the given |
964 | 964 |
/// map. The \c Value type of the algorithm must be convertible to |
965 | 965 |
/// the \c Value type of the map. |
966 | 966 |
/// |
967 | 967 |
/// \pre \ref run() must be called before using this function. |
968 | 968 |
template <typename FlowMap> |
969 | 969 |
void flowMap(FlowMap &map) const { |
970 | 970 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
971 | 971 |
map.set(a, _flow[_arc_id[a]]); |
972 | 972 |
} |
973 | 973 |
} |
974 | 974 |
|
975 | 975 |
/// \brief Return the potential (dual value) of the given node. |
976 | 976 |
/// |
977 | 977 |
/// This function returns the potential (dual value) of the |
978 | 978 |
/// given node. |
979 | 979 |
/// |
980 | 980 |
/// \pre \ref run() must be called before using this function. |
981 | 981 |
Cost potential(const Node& n) const { |
982 | 982 |
return _pi[_node_id[n]]; |
983 | 983 |
} |
984 | 984 |
|
985 | 985 |
/// \brief Return the potential map (the dual solution). |
986 | 986 |
/// |
987 | 987 |
/// This function copies the potential (dual value) of each node |
988 | 988 |
/// into the given map. |
989 | 989 |
/// The \c Cost type of the algorithm must be convertible to the |
990 | 990 |
/// \c Value type of the map. |
991 | 991 |
/// |
992 | 992 |
/// \pre \ref run() must be called before using this function. |
993 | 993 |
template <typename PotentialMap> |
994 | 994 |
void potentialMap(PotentialMap &map) const { |
995 | 995 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
996 | 996 |
map.set(n, _pi[_node_id[n]]); |
997 | 997 |
} |
998 | 998 |
} |
999 | 999 |
|
1000 | 1000 |
/// @} |
1001 | 1001 |
|
1002 | 1002 |
private: |
1003 | 1003 |
|
1004 | 1004 |
// Initialize internal data structures |
1005 | 1005 |
bool init() { |
1006 | 1006 |
if (_node_num == 0) return false; |
1007 | 1007 |
|
1008 | 1008 |
// Check the sum of supply values |
1009 | 1009 |
_sum_supply = 0; |
1010 | 1010 |
for (int i = 0; i != _node_num; ++i) { |
1011 | 1011 |
_sum_supply += _supply[i]; |
1012 | 1012 |
} |
1013 | 1013 |
if ( !((_stype == GEQ && _sum_supply <= 0) || |
1014 | 1014 |
(_stype == LEQ && _sum_supply >= 0)) ) return false; |
1015 | 1015 |
|
1016 | 1016 |
// Remove non-zero lower bounds |
1017 | 1017 |
if (_have_lower) { |
1018 | 1018 |
for (int i = 0; i != _arc_num; ++i) { |
1019 | 1019 |
Value c = _lower[i]; |
1020 | 1020 |
if (c >= 0) { |
1021 | 1021 |
_cap[i] = _upper[i] < INF ? _upper[i] - c : INF; |
1022 | 1022 |
} else { |
1023 | 1023 |
_cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF; |
1024 | 1024 |
} |
1025 | 1025 |
_supply[_source[i]] -= c; |
1026 | 1026 |
_supply[_target[i]] += c; |
1027 | 1027 |
} |
1028 | 1028 |
} else { |
1029 | 1029 |
for (int i = 0; i != _arc_num; ++i) { |
1030 | 1030 |
_cap[i] = _upper[i]; |
1031 | 1031 |
} |
1032 | 1032 |
} |
1033 | 1033 |
|
1034 | 1034 |
// Initialize artifical cost |
1035 | 1035 |
Cost ART_COST; |
1036 | 1036 |
if (std::numeric_limits<Cost>::is_exact) { |
1037 | 1037 |
ART_COST = std::numeric_limits<Cost>::max() / 2 + 1; |
1038 | 1038 |
} else { |
1039 | 1039 |
ART_COST = std::numeric_limits<Cost>::min(); |
1040 | 1040 |
for (int i = 0; i != _arc_num; ++i) { |
1041 | 1041 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
1042 | 1042 |
} |
1043 | 1043 |
ART_COST = (ART_COST + 1) * _node_num; |
1044 | 1044 |
} |
1045 | 1045 |
|
1046 | 1046 |
// Initialize arc maps |
1047 | 1047 |
for (int i = 0; i != _arc_num; ++i) { |
1048 | 1048 |
_flow[i] = 0; |
1049 | 1049 |
_state[i] = STATE_LOWER; |
1050 | 1050 |
} |
1051 | 1051 |
|
1052 | 1052 |
// Set data for the artificial root node |
1053 | 1053 |
_root = _node_num; |
1054 | 1054 |
_parent[_root] = -1; |
1055 | 1055 |
_pred[_root] = -1; |
1056 | 1056 |
_thread[_root] = 0; |
1057 | 1057 |
_rev_thread[0] = _root; |
1058 | 1058 |
_succ_num[_root] = _node_num + 1; |
1059 | 1059 |
_last_succ[_root] = _root - 1; |
1060 | 1060 |
_supply[_root] = -_sum_supply; |
1061 | 1061 |
_pi[_root] = 0; |
1062 | 1062 |
|
1063 | 1063 |
// Add artificial arcs and initialize the spanning tree data structure |
1064 | 1064 |
if (_sum_supply == 0) { |
1065 | 1065 |
// EQ supply constraints |
1066 | 1066 |
_search_arc_num = _arc_num; |
... | ... |
@@ -75,385 +75,385 @@ |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
77 | 77 |
#else |
78 | 78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
79 | 79 |
#endif |
80 | 80 |
|
81 | 81 |
/// \brief Instantiates an Elevator. |
82 | 82 |
/// |
83 | 83 |
/// This function instantiates an \ref Elevator. |
84 | 84 |
/// \param digraph The digraph for which we would like to define |
85 | 85 |
/// the elevator. |
86 | 86 |
/// \param max_level The maximum level of the elevator. |
87 | 87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
88 | 88 |
return new Elevator(digraph, max_level); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
/// \brief The tolerance used by the algorithm |
92 | 92 |
/// |
93 | 93 |
/// The tolerance used by the algorithm to handle inexact computation. |
94 | 94 |
typedef lemon::Tolerance<Value> Tolerance; |
95 | 95 |
|
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
|
99 | 99 |
/// \ingroup max_flow |
100 | 100 |
/// |
101 | 101 |
/// \brief %Preflow algorithm class. |
102 | 102 |
/// |
103 | 103 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
104 | 104 |
/// \e push-relabel algorithm producing a \ref max_flow |
105 | 105 |
/// "flow of maximum value" in a digraph. |
106 | 106 |
/// The preflow algorithms are the fastest known maximum |
107 | 107 |
/// flow algorithms. The current implementation uses a mixture of the |
108 | 108 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
109 | 109 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
110 | 110 |
/// |
111 | 111 |
/// The algorithm consists of two phases. After the first phase |
112 | 112 |
/// the maximum flow value and the minimum cut is obtained. The |
113 | 113 |
/// second phase constructs a feasible maximum flow on each arc. |
114 | 114 |
/// |
115 | 115 |
/// \tparam GR The type of the digraph the algorithm runs on. |
116 | 116 |
/// \tparam CAP The type of the capacity map. The default map |
117 | 117 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
118 | 118 |
#ifdef DOXYGEN |
119 | 119 |
template <typename GR, typename CAP, typename TR> |
120 | 120 |
#else |
121 | 121 |
template <typename GR, |
122 | 122 |
typename CAP = typename GR::template ArcMap<int>, |
123 | 123 |
typename TR = PreflowDefaultTraits<GR, CAP> > |
124 | 124 |
#endif |
125 | 125 |
class Preflow { |
126 | 126 |
public: |
127 | 127 |
|
128 | 128 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
129 | 129 |
typedef TR Traits; |
130 | 130 |
///The type of the digraph the algorithm runs on. |
131 | 131 |
typedef typename Traits::Digraph Digraph; |
132 | 132 |
///The type of the capacity map. |
133 | 133 |
typedef typename Traits::CapacityMap CapacityMap; |
134 | 134 |
///The type of the flow values. |
135 | 135 |
typedef typename Traits::Value Value; |
136 | 136 |
|
137 | 137 |
///The type of the flow map. |
138 | 138 |
typedef typename Traits::FlowMap FlowMap; |
139 | 139 |
///The type of the elevator. |
140 | 140 |
typedef typename Traits::Elevator Elevator; |
141 | 141 |
///The type of the tolerance. |
142 | 142 |
typedef typename Traits::Tolerance Tolerance; |
143 | 143 |
|
144 | 144 |
private: |
145 | 145 |
|
146 | 146 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
147 | 147 |
|
148 | 148 |
const Digraph& _graph; |
149 | 149 |
const CapacityMap* _capacity; |
150 | 150 |
|
151 | 151 |
int _node_num; |
152 | 152 |
|
153 | 153 |
Node _source, _target; |
154 | 154 |
|
155 | 155 |
FlowMap* _flow; |
156 | 156 |
bool _local_flow; |
157 | 157 |
|
158 | 158 |
Elevator* _level; |
159 | 159 |
bool _local_level; |
160 | 160 |
|
161 | 161 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
162 | 162 |
ExcessMap* _excess; |
163 | 163 |
|
164 | 164 |
Tolerance _tolerance; |
165 | 165 |
|
166 | 166 |
bool _phase; |
167 | 167 |
|
168 | 168 |
|
169 | 169 |
void createStructures() { |
170 | 170 |
_node_num = countNodes(_graph); |
171 | 171 |
|
172 | 172 |
if (!_flow) { |
173 | 173 |
_flow = Traits::createFlowMap(_graph); |
174 | 174 |
_local_flow = true; |
175 | 175 |
} |
176 | 176 |
if (!_level) { |
177 | 177 |
_level = Traits::createElevator(_graph, _node_num); |
178 | 178 |
_local_level = true; |
179 | 179 |
} |
180 | 180 |
if (!_excess) { |
181 | 181 |
_excess = new ExcessMap(_graph); |
182 | 182 |
} |
183 | 183 |
} |
184 | 184 |
|
185 | 185 |
void destroyStructures() { |
186 | 186 |
if (_local_flow) { |
187 | 187 |
delete _flow; |
188 | 188 |
} |
189 | 189 |
if (_local_level) { |
190 | 190 |
delete _level; |
191 | 191 |
} |
192 | 192 |
if (_excess) { |
193 | 193 |
delete _excess; |
194 | 194 |
} |
195 | 195 |
} |
196 | 196 |
|
197 | 197 |
public: |
198 | 198 |
|
199 | 199 |
typedef Preflow Create; |
200 | 200 |
|
201 | 201 |
///\name Named Template Parameters |
202 | 202 |
|
203 | 203 |
///@{ |
204 | 204 |
|
205 | 205 |
template <typename T> |
206 | 206 |
struct SetFlowMapTraits : public Traits { |
207 | 207 |
typedef T FlowMap; |
208 | 208 |
static FlowMap *createFlowMap(const Digraph&) { |
209 | 209 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
210 | 210 |
return 0; // ignore warnings |
211 | 211 |
} |
212 | 212 |
}; |
213 | 213 |
|
214 | 214 |
/// \brief \ref named-templ-param "Named parameter" for setting |
215 | 215 |
/// FlowMap type |
216 | 216 |
/// |
217 | 217 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
218 | 218 |
/// type. |
219 | 219 |
template <typename T> |
220 | 220 |
struct SetFlowMap |
221 | 221 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > { |
222 | 222 |
typedef Preflow<Digraph, CapacityMap, |
223 | 223 |
SetFlowMapTraits<T> > Create; |
224 | 224 |
}; |
225 | 225 |
|
226 | 226 |
template <typename T> |
227 | 227 |
struct SetElevatorTraits : public Traits { |
228 | 228 |
typedef T Elevator; |
229 | 229 |
static Elevator *createElevator(const Digraph&, int) { |
230 | 230 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
231 | 231 |
return 0; // ignore warnings |
232 | 232 |
} |
233 | 233 |
}; |
234 | 234 |
|
235 | 235 |
/// \brief \ref named-templ-param "Named parameter" for setting |
236 | 236 |
/// Elevator type |
237 | 237 |
/// |
238 | 238 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
239 | 239 |
/// type. If this named parameter is used, then an external |
240 | 240 |
/// elevator object must be passed to the algorithm using the |
241 | 241 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
242 | 242 |
/// \ref run() or \ref init(). |
243 | 243 |
/// \sa SetStandardElevator |
244 | 244 |
template <typename T> |
245 | 245 |
struct SetElevator |
246 | 246 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > { |
247 | 247 |
typedef Preflow<Digraph, CapacityMap, |
248 | 248 |
SetElevatorTraits<T> > Create; |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
template <typename T> |
252 | 252 |
struct SetStandardElevatorTraits : public Traits { |
253 | 253 |
typedef T Elevator; |
254 | 254 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
255 | 255 |
return new Elevator(digraph, max_level); |
256 | 256 |
} |
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
/// \brief \ref named-templ-param "Named parameter" for setting |
260 | 260 |
/// Elevator type with automatic allocation |
261 | 261 |
/// |
262 | 262 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
263 | 263 |
/// type with automatic allocation. |
264 | 264 |
/// The Elevator should have standard constructor interface to be |
265 | 265 |
/// able to automatically created by the algorithm (i.e. the |
266 | 266 |
/// digraph and the maximum level should be passed to it). |
267 |
/// However an external elevator object could also be passed to the |
|
267 |
/// However, an external elevator object could also be passed to the |
|
268 | 268 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
269 | 269 |
/// before calling \ref run() or \ref init(). |
270 | 270 |
/// \sa SetElevator |
271 | 271 |
template <typename T> |
272 | 272 |
struct SetStandardElevator |
273 | 273 |
: public Preflow<Digraph, CapacityMap, |
274 | 274 |
SetStandardElevatorTraits<T> > { |
275 | 275 |
typedef Preflow<Digraph, CapacityMap, |
276 | 276 |
SetStandardElevatorTraits<T> > Create; |
277 | 277 |
}; |
278 | 278 |
|
279 | 279 |
/// @} |
280 | 280 |
|
281 | 281 |
protected: |
282 | 282 |
|
283 | 283 |
Preflow() {} |
284 | 284 |
|
285 | 285 |
public: |
286 | 286 |
|
287 | 287 |
|
288 | 288 |
/// \brief The constructor of the class. |
289 | 289 |
/// |
290 | 290 |
/// The constructor of the class. |
291 | 291 |
/// \param digraph The digraph the algorithm runs on. |
292 | 292 |
/// \param capacity The capacity of the arcs. |
293 | 293 |
/// \param source The source node. |
294 | 294 |
/// \param target The target node. |
295 | 295 |
Preflow(const Digraph& digraph, const CapacityMap& capacity, |
296 | 296 |
Node source, Node target) |
297 | 297 |
: _graph(digraph), _capacity(&capacity), |
298 | 298 |
_node_num(0), _source(source), _target(target), |
299 | 299 |
_flow(0), _local_flow(false), |
300 | 300 |
_level(0), _local_level(false), |
301 | 301 |
_excess(0), _tolerance(), _phase() {} |
302 | 302 |
|
303 | 303 |
/// \brief Destructor. |
304 | 304 |
/// |
305 | 305 |
/// Destructor. |
306 | 306 |
~Preflow() { |
307 | 307 |
destroyStructures(); |
308 | 308 |
} |
309 | 309 |
|
310 | 310 |
/// \brief Sets the capacity map. |
311 | 311 |
/// |
312 | 312 |
/// Sets the capacity map. |
313 | 313 |
/// \return <tt>(*this)</tt> |
314 | 314 |
Preflow& capacityMap(const CapacityMap& map) { |
315 | 315 |
_capacity = ↦ |
316 | 316 |
return *this; |
317 | 317 |
} |
318 | 318 |
|
319 | 319 |
/// \brief Sets the flow map. |
320 | 320 |
/// |
321 | 321 |
/// Sets the flow map. |
322 | 322 |
/// If you don't use this function before calling \ref run() or |
323 | 323 |
/// \ref init(), an instance will be allocated automatically. |
324 | 324 |
/// The destructor deallocates this automatically allocated map, |
325 | 325 |
/// of course. |
326 | 326 |
/// \return <tt>(*this)</tt> |
327 | 327 |
Preflow& flowMap(FlowMap& map) { |
328 | 328 |
if (_local_flow) { |
329 | 329 |
delete _flow; |
330 | 330 |
_local_flow = false; |
331 | 331 |
} |
332 | 332 |
_flow = ↦ |
333 | 333 |
return *this; |
334 | 334 |
} |
335 | 335 |
|
336 | 336 |
/// \brief Sets the source node. |
337 | 337 |
/// |
338 | 338 |
/// Sets the source node. |
339 | 339 |
/// \return <tt>(*this)</tt> |
340 | 340 |
Preflow& source(const Node& node) { |
341 | 341 |
_source = node; |
342 | 342 |
return *this; |
343 | 343 |
} |
344 | 344 |
|
345 | 345 |
/// \brief Sets the target node. |
346 | 346 |
/// |
347 | 347 |
/// Sets the target node. |
348 | 348 |
/// \return <tt>(*this)</tt> |
349 | 349 |
Preflow& target(const Node& node) { |
350 | 350 |
_target = node; |
351 | 351 |
return *this; |
352 | 352 |
} |
353 | 353 |
|
354 | 354 |
/// \brief Sets the elevator used by algorithm. |
355 | 355 |
/// |
356 | 356 |
/// Sets the elevator used by algorithm. |
357 | 357 |
/// If you don't use this function before calling \ref run() or |
358 | 358 |
/// \ref init(), an instance will be allocated automatically. |
359 | 359 |
/// The destructor deallocates this automatically allocated elevator, |
360 | 360 |
/// of course. |
361 | 361 |
/// \return <tt>(*this)</tt> |
362 | 362 |
Preflow& elevator(Elevator& elevator) { |
363 | 363 |
if (_local_level) { |
364 | 364 |
delete _level; |
365 | 365 |
_local_level = false; |
366 | 366 |
} |
367 | 367 |
_level = &elevator; |
368 | 368 |
return *this; |
369 | 369 |
} |
370 | 370 |
|
371 | 371 |
/// \brief Returns a const reference to the elevator. |
372 | 372 |
/// |
373 | 373 |
/// Returns a const reference to the elevator. |
374 | 374 |
/// |
375 | 375 |
/// \pre Either \ref run() or \ref init() must be called before |
376 | 376 |
/// using this function. |
377 | 377 |
const Elevator& elevator() const { |
378 | 378 |
return *_level; |
379 | 379 |
} |
380 | 380 |
|
381 | 381 |
/// \brief Sets the tolerance used by the algorithm. |
382 | 382 |
/// |
383 | 383 |
/// Sets the tolerance object used by the algorithm. |
384 | 384 |
/// \return <tt>(*this)</tt> |
385 | 385 |
Preflow& tolerance(const Tolerance& tolerance) { |
386 | 386 |
_tolerance = tolerance; |
387 | 387 |
return *this; |
388 | 388 |
} |
389 | 389 |
|
390 | 390 |
/// \brief Returns a const reference to the tolerance. |
391 | 391 |
/// |
392 | 392 |
/// Returns a const reference to the tolerance object used by |
393 | 393 |
/// the algorithm. |
394 | 394 |
const Tolerance& tolerance() const { |
395 | 395 |
return _tolerance; |
396 | 396 |
} |
397 | 397 |
|
398 | 398 |
/// \name Execution Control |
399 | 399 |
/// The simplest way to execute the preflow algorithm is to use |
400 | 400 |
/// \ref run() or \ref runMinCut().\n |
401 | 401 |
/// If you need better control on the initial solution or the execution, |
402 | 402 |
/// you have to call one of the \ref init() functions first, then |
403 | 403 |
/// \ref startFirstPhase() and if you need it \ref startSecondPhase(). |
404 | 404 |
|
405 | 405 |
///@{ |
406 | 406 |
|
407 | 407 |
/// \brief Initializes the internal data structures. |
408 | 408 |
/// |
409 | 409 |
/// Initializes the internal data structures and sets the initial |
410 | 410 |
/// flow to zero on each arc. |
411 | 411 |
void init() { |
412 | 412 |
createStructures(); |
413 | 413 |
|
414 | 414 |
_phase = true; |
415 | 415 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
416 | 416 |
(*_excess)[n] = 0; |
417 | 417 |
} |
418 | 418 |
|
419 | 419 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
420 | 420 |
_flow->set(e, 0); |
421 | 421 |
} |
422 | 422 |
|
423 | 423 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
424 | 424 |
|
425 | 425 |
_level->initStart(); |
426 | 426 |
_level->initAddItem(_target); |
427 | 427 |
|
428 | 428 |
std::vector<Node> queue; |
429 | 429 |
reached[_source] = true; |
430 | 430 |
|
431 | 431 |
queue.push_back(_target); |
432 | 432 |
reached[_target] = true; |
433 | 433 |
while (!queue.empty()) { |
434 | 434 |
_level->initNewLevel(); |
435 | 435 |
std::vector<Node> nqueue; |
436 | 436 |
for (int i = 0; i < int(queue.size()); ++i) { |
437 | 437 |
Node n = queue[i]; |
438 | 438 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
439 | 439 |
Node u = _graph.source(e); |
440 | 440 |
if (!reached[u] && _tolerance.positive((*_capacity)[e])) { |
441 | 441 |
reached[u] = true; |
442 | 442 |
_level->initAddItem(u); |
443 | 443 |
nqueue.push_back(u); |
444 | 444 |
} |
445 | 445 |
} |
446 | 446 |
} |
447 | 447 |
queue.swap(nqueue); |
448 | 448 |
} |
449 | 449 |
_level->initFinish(); |
450 | 450 |
|
451 | 451 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) { |
452 | 452 |
if (_tolerance.positive((*_capacity)[e])) { |
453 | 453 |
Node u = _graph.target(e); |
454 | 454 |
if ((*_level)[u] == _level->maxLevel()) continue; |
455 | 455 |
_flow->set(e, (*_capacity)[e]); |
456 | 456 |
(*_excess)[u] += (*_capacity)[e]; |
457 | 457 |
if (u != _target && !_level->active(u)) { |
458 | 458 |
_level->activate(u); |
459 | 459 |
} |
... | ... |
@@ -186,367 +186,367 @@ |
186 | 186 |
|
187 | 187 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
188 | 188 |
/// |
189 | 189 |
double cUserTime() const |
190 | 190 |
{ |
191 | 191 |
return cutime; |
192 | 192 |
} |
193 | 193 |
///Gives back the user time of the process' children |
194 | 194 |
|
195 | 195 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
196 | 196 |
/// |
197 | 197 |
double cSystemTime() const |
198 | 198 |
{ |
199 | 199 |
return cstime; |
200 | 200 |
} |
201 | 201 |
///Gives back the real time |
202 | 202 |
double realTime() const {return rtime;} |
203 | 203 |
}; |
204 | 204 |
|
205 | 205 |
inline TimeStamp operator*(double b,const TimeStamp &t) |
206 | 206 |
{ |
207 | 207 |
return t*b; |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
///Prints the time counters |
211 | 211 |
|
212 | 212 |
///Prints the time counters in the following form: |
213 | 213 |
/// |
214 | 214 |
/// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
215 | 215 |
/// |
216 | 216 |
/// where the values are the |
217 | 217 |
/// \li \c u: user cpu time, |
218 | 218 |
/// \li \c s: system cpu time, |
219 | 219 |
/// \li \c cu: user cpu time of children, |
220 | 220 |
/// \li \c cs: system cpu time of children, |
221 | 221 |
/// \li \c real: real time. |
222 | 222 |
/// \relates TimeStamp |
223 | 223 |
/// \note On <tt>WIN32</tt> platform the cummulative values are not |
224 | 224 |
/// calculated. |
225 | 225 |
inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
226 | 226 |
{ |
227 | 227 |
os << "u: " << t.userTime() << |
228 | 228 |
"s, s: " << t.systemTime() << |
229 | 229 |
"s, cu: " << t.cUserTime() << |
230 | 230 |
"s, cs: " << t.cSystemTime() << |
231 | 231 |
"s, real: " << t.realTime() << "s"; |
232 | 232 |
return os; |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
///Class for measuring the cpu time and real time usage of the process |
236 | 236 |
|
237 | 237 |
///Class for measuring the cpu time and real time usage of the process. |
238 | 238 |
///It is quite easy-to-use, here is a short example. |
239 | 239 |
///\code |
240 | 240 |
/// #include<lemon/time_measure.h> |
241 | 241 |
/// #include<iostream> |
242 | 242 |
/// |
243 | 243 |
/// int main() |
244 | 244 |
/// { |
245 | 245 |
/// |
246 | 246 |
/// ... |
247 | 247 |
/// |
248 | 248 |
/// Timer t; |
249 | 249 |
/// doSomething(); |
250 | 250 |
/// std::cout << t << '\n'; |
251 | 251 |
/// t.restart(); |
252 | 252 |
/// doSomethingElse(); |
253 | 253 |
/// std::cout << t << '\n'; |
254 | 254 |
/// |
255 | 255 |
/// ... |
256 | 256 |
/// |
257 | 257 |
/// } |
258 | 258 |
///\endcode |
259 | 259 |
/// |
260 | 260 |
///The \ref Timer can also be \ref stop() "stopped" and |
261 | 261 |
///\ref start() "started" again, so it is possible to compute collected |
262 | 262 |
///running times. |
263 | 263 |
/// |
264 | 264 |
///\warning Depending on the operation system and its actual configuration |
265 | 265 |
///the time counters have a certain (10ms on a typical Linux system) |
266 | 266 |
///granularity. |
267 | 267 |
///Therefore this tool is not appropriate to measure very short times. |
268 | 268 |
///Also, if you start and stop the timer very frequently, it could lead to |
269 | 269 |
///distorted results. |
270 | 270 |
/// |
271 | 271 |
///\note If you want to measure the running time of the execution of a certain |
272 | 272 |
///function, consider the usage of \ref TimeReport instead. |
273 | 273 |
/// |
274 | 274 |
///\sa TimeReport |
275 | 275 |
class Timer |
276 | 276 |
{ |
277 | 277 |
int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
278 | 278 |
TimeStamp start_time; //This is the relativ start-time if the timer |
279 | 279 |
//is _running, the collected _running time otherwise. |
280 | 280 |
|
281 | 281 |
void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
282 | 282 |
|
283 | 283 |
public: |
284 | 284 |
///Constructor. |
285 | 285 |
|
286 | 286 |
///\param run indicates whether or not the timer starts immediately. |
287 | 287 |
/// |
288 | 288 |
Timer(bool run=true) :_running(run) {_reset();} |
289 | 289 |
|
290 | 290 |
///\name Control the State of the Timer |
291 | 291 |
///Basically a Timer can be either running or stopped, |
292 | 292 |
///but it provides a bit finer control on the execution. |
293 | 293 |
///The \ref lemon::Timer "Timer" also counts the number of |
294 | 294 |
///\ref lemon::Timer::start() "start()" executions, and it stops |
295 | 295 |
///only after the same amount (or more) \ref lemon::Timer::stop() |
296 | 296 |
///"stop()"s. This can be useful e.g. to compute the running time |
297 | 297 |
///of recursive functions. |
298 | 298 |
|
299 | 299 |
///@{ |
300 | 300 |
|
301 | 301 |
///Reset and stop the time counters |
302 | 302 |
|
303 | 303 |
///This function resets and stops the time counters |
304 | 304 |
///\sa restart() |
305 | 305 |
void reset() |
306 | 306 |
{ |
307 | 307 |
_running=0; |
308 | 308 |
_reset(); |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
///Start the time counters |
312 | 312 |
|
313 | 313 |
///This function starts the time counters. |
314 | 314 |
/// |
315 | 315 |
///If the timer is started more than ones, it will remain running |
316 | 316 |
///until the same amount of \ref stop() is called. |
317 | 317 |
///\sa stop() |
318 | 318 |
void start() |
319 | 319 |
{ |
320 | 320 |
if(_running) _running++; |
321 | 321 |
else { |
322 | 322 |
_running=1; |
323 | 323 |
TimeStamp t; |
324 | 324 |
t.stamp(); |
325 | 325 |
start_time=t-start_time; |
326 | 326 |
} |
327 | 327 |
} |
328 | 328 |
|
329 | 329 |
|
330 | 330 |
///Stop the time counters |
331 | 331 |
|
332 | 332 |
///This function stops the time counters. If start() was executed more than |
333 | 333 |
///once, then the same number of stop() execution is necessary the really |
334 | 334 |
///stop the timer. |
335 | 335 |
/// |
336 | 336 |
///\sa halt() |
337 | 337 |
///\sa start() |
338 | 338 |
///\sa restart() |
339 | 339 |
///\sa reset() |
340 | 340 |
|
341 | 341 |
void stop() |
342 | 342 |
{ |
343 | 343 |
if(_running && !--_running) { |
344 | 344 |
TimeStamp t; |
345 | 345 |
t.stamp(); |
346 | 346 |
start_time=t-start_time; |
347 | 347 |
} |
348 | 348 |
} |
349 | 349 |
|
350 | 350 |
///Halt (i.e stop immediately) the time counters |
351 | 351 |
|
352 | 352 |
///This function stops immediately the time counters, i.e. <tt>t.halt()</tt> |
353 | 353 |
///is a faster |
354 | 354 |
///equivalent of the following. |
355 | 355 |
///\code |
356 | 356 |
/// while(t.running()) t.stop() |
357 | 357 |
///\endcode |
358 | 358 |
/// |
359 | 359 |
/// |
360 | 360 |
///\sa stop() |
361 | 361 |
///\sa restart() |
362 | 362 |
///\sa reset() |
363 | 363 |
|
364 | 364 |
void halt() |
365 | 365 |
{ |
366 | 366 |
if(_running) { |
367 | 367 |
_running=0; |
368 | 368 |
TimeStamp t; |
369 | 369 |
t.stamp(); |
370 | 370 |
start_time=t-start_time; |
371 | 371 |
} |
372 | 372 |
} |
373 | 373 |
|
374 | 374 |
///Returns the running state of the timer |
375 | 375 |
|
376 | 376 |
///This function returns the number of stop() exections that is |
377 | 377 |
///necessary to really stop the timer. |
378 |
///For example the timer |
|
378 |
///For example, the timer |
|
379 | 379 |
///is running if and only if the return value is \c true |
380 | 380 |
///(i.e. greater than |
381 | 381 |
///zero). |
382 | 382 |
int running() { return _running; } |
383 | 383 |
|
384 | 384 |
|
385 | 385 |
///Restart the time counters |
386 | 386 |
|
387 | 387 |
///This function is a shorthand for |
388 | 388 |
///a reset() and a start() calls. |
389 | 389 |
/// |
390 | 390 |
void restart() |
391 | 391 |
{ |
392 | 392 |
reset(); |
393 | 393 |
start(); |
394 | 394 |
} |
395 | 395 |
|
396 | 396 |
///@} |
397 | 397 |
|
398 | 398 |
///\name Query Functions for the Ellapsed Time |
399 | 399 |
|
400 | 400 |
///@{ |
401 | 401 |
|
402 | 402 |
///Gives back the ellapsed user time of the process |
403 | 403 |
double userTime() const |
404 | 404 |
{ |
405 | 405 |
return operator TimeStamp().userTime(); |
406 | 406 |
} |
407 | 407 |
///Gives back the ellapsed system time of the process |
408 | 408 |
double systemTime() const |
409 | 409 |
{ |
410 | 410 |
return operator TimeStamp().systemTime(); |
411 | 411 |
} |
412 | 412 |
///Gives back the ellapsed user time of the process' children |
413 | 413 |
|
414 | 414 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
415 | 415 |
/// |
416 | 416 |
double cUserTime() const |
417 | 417 |
{ |
418 | 418 |
return operator TimeStamp().cUserTime(); |
419 | 419 |
} |
420 | 420 |
///Gives back the ellapsed user time of the process' children |
421 | 421 |
|
422 | 422 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
423 | 423 |
/// |
424 | 424 |
double cSystemTime() const |
425 | 425 |
{ |
426 | 426 |
return operator TimeStamp().cSystemTime(); |
427 | 427 |
} |
428 | 428 |
///Gives back the ellapsed real time |
429 | 429 |
double realTime() const |
430 | 430 |
{ |
431 | 431 |
return operator TimeStamp().realTime(); |
432 | 432 |
} |
433 | 433 |
///Computes the ellapsed time |
434 | 434 |
|
435 | 435 |
///This conversion computes the ellapsed time, therefore you can print |
436 | 436 |
///the ellapsed time like this. |
437 | 437 |
///\code |
438 | 438 |
/// Timer t; |
439 | 439 |
/// doSomething(); |
440 | 440 |
/// std::cout << t << '\n'; |
441 | 441 |
///\endcode |
442 | 442 |
operator TimeStamp () const |
443 | 443 |
{ |
444 | 444 |
TimeStamp t; |
445 | 445 |
t.stamp(); |
446 | 446 |
return _running?t-start_time:start_time; |
447 | 447 |
} |
448 | 448 |
|
449 | 449 |
|
450 | 450 |
///@} |
451 | 451 |
}; |
452 | 452 |
|
453 | 453 |
///Same as Timer but prints a report on destruction. |
454 | 454 |
|
455 | 455 |
///Same as \ref Timer but prints a report on destruction. |
456 | 456 |
///This example shows its usage. |
457 | 457 |
///\code |
458 | 458 |
/// void myAlg(ListGraph &g,int n) |
459 | 459 |
/// { |
460 | 460 |
/// TimeReport tr("Running time of myAlg: "); |
461 | 461 |
/// ... //Here comes the algorithm |
462 | 462 |
/// } |
463 | 463 |
///\endcode |
464 | 464 |
/// |
465 | 465 |
///\sa Timer |
466 | 466 |
///\sa NoTimeReport |
467 | 467 |
class TimeReport : public Timer |
468 | 468 |
{ |
469 | 469 |
std::string _title; |
470 | 470 |
std::ostream &_os; |
471 | 471 |
public: |
472 | 472 |
///Constructor |
473 | 473 |
|
474 | 474 |
///Constructor. |
475 | 475 |
///\param title This text will be printed before the ellapsed time. |
476 | 476 |
///\param os The stream to print the report to. |
477 | 477 |
///\param run Sets whether the timer should start immediately. |
478 | 478 |
TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
479 | 479 |
: Timer(run), _title(title), _os(os){} |
480 | 480 |
///Destructor that prints the ellapsed time |
481 | 481 |
~TimeReport() |
482 | 482 |
{ |
483 | 483 |
_os << _title << *this << std::endl; |
484 | 484 |
} |
485 | 485 |
}; |
486 | 486 |
|
487 | 487 |
///'Do nothing' version of TimeReport |
488 | 488 |
|
489 | 489 |
///\sa TimeReport |
490 | 490 |
/// |
491 | 491 |
class NoTimeReport |
492 | 492 |
{ |
493 | 493 |
public: |
494 | 494 |
///\e |
495 | 495 |
NoTimeReport(std::string,std::ostream &,bool) {} |
496 | 496 |
///\e |
497 | 497 |
NoTimeReport(std::string,std::ostream &) {} |
498 | 498 |
///\e |
499 | 499 |
NoTimeReport(std::string) {} |
500 | 500 |
///\e Do nothing. |
501 | 501 |
~NoTimeReport() {} |
502 | 502 |
|
503 | 503 |
operator TimeStamp () const { return TimeStamp(); } |
504 | 504 |
void reset() {} |
505 | 505 |
void start() {} |
506 | 506 |
void stop() {} |
507 | 507 |
void halt() {} |
508 | 508 |
int running() { return 0; } |
509 | 509 |
void restart() {} |
510 | 510 |
double userTime() const { return 0; } |
511 | 511 |
double systemTime() const { return 0; } |
512 | 512 |
double cUserTime() const { return 0; } |
513 | 513 |
double cSystemTime() const { return 0; } |
514 | 514 |
double realTime() const { return 0; } |
515 | 515 |
}; |
516 | 516 |
|
517 | 517 |
///Tool to measure the running time more exactly. |
518 | 518 |
|
519 | 519 |
///This function calls \c f several times and returns the average |
520 | 520 |
///running time. The number of the executions will be choosen in such a way |
521 | 521 |
///that the full real running time will be roughly between \c min_time |
522 | 522 |
///and <tt>2*min_time</tt>. |
523 | 523 |
///\param f the function object to be measured. |
524 | 524 |
///\param min_time the minimum total running time. |
525 | 525 |
///\retval num if it is not \c NULL, then the actual |
526 | 526 |
/// number of execution of \c f will be written into <tt>*num</tt>. |
527 | 527 |
///\retval full_time if it is not \c NULL, then the actual |
528 | 528 |
/// total running time will be written into <tt>*full_time</tt>. |
529 | 529 |
///\return The average running time of \c f. |
530 | 530 |
|
531 | 531 |
template<class F> |
532 | 532 |
TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL, |
533 | 533 |
TimeStamp *full_time=NULL) |
534 | 534 |
{ |
535 | 535 |
TimeStamp full; |
536 | 536 |
unsigned int total=0; |
537 | 537 |
Timer t; |
538 | 538 |
for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) { |
539 | 539 |
for(;total<tn;total++) f(); |
540 | 540 |
full=t; |
541 | 541 |
} |
542 | 542 |
if(num) *num=total; |
543 | 543 |
if(full_time) *full_time=full; |
544 | 544 |
return full/total; |
545 | 545 |
} |
546 | 546 |
|
547 | 547 |
/// @} |
548 | 548 |
|
549 | 549 |
|
550 | 550 |
} //namespace lemon |
551 | 551 |
|
552 | 552 |
#endif //LEMON_TIME_MEASURE_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_UNION_FIND_H |
20 | 20 |
#define LEMON_UNION_FIND_H |
21 | 21 |
|
22 | 22 |
//!\ingroup auxdat |
23 | 23 |
//!\file |
24 | 24 |
//!\brief Union-Find data structures. |
25 | 25 |
//! |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <list> |
29 | 29 |
#include <utility> |
30 | 30 |
#include <algorithm> |
31 | 31 |
#include <functional> |
32 | 32 |
|
33 | 33 |
#include <lemon/core.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \ingroup auxdat |
38 | 38 |
/// |
39 | 39 |
/// \brief A \e Union-Find data structure implementation |
40 | 40 |
/// |
41 | 41 |
/// The class implements the \e Union-Find data structure. |
42 | 42 |
/// The union operation uses rank heuristic, while |
43 | 43 |
/// the find operation uses path compression. |
44 | 44 |
/// This is a very simple but efficient implementation, providing |
45 | 45 |
/// only four methods: join (union), find, insert and size. |
46 |
/// For more features see the \ref UnionFindEnum class. |
|
46 |
/// For more features, see the \ref UnionFindEnum class. |
|
47 | 47 |
/// |
48 | 48 |
/// It is primarily used in Kruskal algorithm for finding minimal |
49 | 49 |
/// cost spanning tree in a graph. |
50 | 50 |
/// \sa kruskal() |
51 | 51 |
/// |
52 | 52 |
/// \pre You need to add all the elements by the \ref insert() |
53 | 53 |
/// method. |
54 | 54 |
template <typename IM> |
55 | 55 |
class UnionFind { |
56 | 56 |
public: |
57 | 57 |
|
58 | 58 |
///\e |
59 | 59 |
typedef IM ItemIntMap; |
60 | 60 |
///\e |
61 | 61 |
typedef typename ItemIntMap::Key Item; |
62 | 62 |
|
63 | 63 |
private: |
64 | 64 |
// If the items vector stores negative value for an item then |
65 | 65 |
// that item is root item and it has -items[it] component size. |
66 | 66 |
// Else the items[it] contains the index of the parent. |
67 | 67 |
std::vector<int> items; |
68 | 68 |
ItemIntMap& index; |
69 | 69 |
|
70 | 70 |
bool rep(int idx) const { |
71 | 71 |
return items[idx] < 0; |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
int repIndex(int idx) const { |
75 | 75 |
int k = idx; |
76 | 76 |
while (!rep(k)) { |
77 | 77 |
k = items[k] ; |
78 | 78 |
} |
79 | 79 |
while (idx != k) { |
80 | 80 |
int next = items[idx]; |
81 | 81 |
const_cast<int&>(items[idx]) = k; |
82 | 82 |
idx = next; |
83 | 83 |
} |
84 | 84 |
return k; |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
public: |
88 | 88 |
|
89 | 89 |
/// \brief Constructor |
90 | 90 |
/// |
91 | 91 |
/// Constructor of the UnionFind class. You should give an item to |
92 | 92 |
/// integer map which will be used from the data structure. If you |
93 | 93 |
/// modify directly this map that may cause segmentation fault, |
94 | 94 |
/// invalid data structure, or infinite loop when you use again |
95 | 95 |
/// the union-find. |
96 | 96 |
UnionFind(ItemIntMap& m) : index(m) {} |
97 | 97 |
|
98 | 98 |
/// \brief Returns the index of the element's component. |
99 | 99 |
/// |
100 | 100 |
/// The method returns the index of the element's component. |
101 | 101 |
/// This is an integer between zero and the number of inserted elements. |
102 | 102 |
/// |
103 | 103 |
int find(const Item& a) { |
104 | 104 |
return repIndex(index[a]); |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
/// \brief Clears the union-find data structure |
108 | 108 |
/// |
109 | 109 |
/// Erase each item from the data structure. |
110 | 110 |
void clear() { |
111 | 111 |
items.clear(); |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
/// \brief Inserts a new element into the structure. |
115 | 115 |
/// |
116 | 116 |
/// This method inserts a new element into the data structure. |
117 | 117 |
/// |
118 | 118 |
/// The method returns the index of the new component. |
119 | 119 |
int insert(const Item& a) { |
120 | 120 |
int n = items.size(); |
121 | 121 |
items.push_back(-1); |
122 | 122 |
index.set(a,n); |
123 | 123 |
return n; |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
/// \brief Joining the components of element \e a and element \e b. |
127 | 127 |
/// |
128 | 128 |
/// This is the \e union operation of the Union-Find structure. |
129 | 129 |
/// Joins the component of element \e a and component of |
130 | 130 |
/// element \e b. If \e a and \e b are in the same component then |
131 | 131 |
/// it returns false otherwise it returns true. |
132 | 132 |
bool join(const Item& a, const Item& b) { |
133 | 133 |
int ka = repIndex(index[a]); |
134 | 134 |
int kb = repIndex(index[b]); |
135 | 135 |
|
136 | 136 |
if ( ka == kb ) |
137 | 137 |
return false; |
138 | 138 |
|
139 | 139 |
if (items[ka] < items[kb]) { |
140 | 140 |
items[ka] += items[kb]; |
141 | 141 |
items[kb] = ka; |
142 | 142 |
} else { |
143 | 143 |
items[kb] += items[ka]; |
144 | 144 |
items[ka] = kb; |
145 | 145 |
} |
146 | 146 |
return true; |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
/// \brief Returns the size of the component of element \e a. |
150 | 150 |
/// |
151 | 151 |
/// Returns the size of the component of element \e a. |
152 | 152 |
int size(const Item& a) { |
153 | 153 |
int k = repIndex(index[a]); |
154 | 154 |
return - items[k]; |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
/// \ingroup auxdat |
160 | 160 |
/// |
161 | 161 |
/// \brief A \e Union-Find data structure implementation which |
162 | 162 |
/// is able to enumerate the components. |
163 | 163 |
/// |
164 | 164 |
/// The class implements a \e Union-Find data structure |
165 | 165 |
/// which is able to enumerate the components and the items in |
166 | 166 |
/// a component. If you don't need this feature then perhaps it's |
167 | 167 |
/// better to use the \ref UnionFind class which is more efficient. |
168 | 168 |
/// |
169 | 169 |
/// The union operation uses rank heuristic, while |
170 | 170 |
/// the find operation uses path compression. |
171 | 171 |
/// |
172 | 172 |
/// \pre You need to add all the elements by the \ref insert() |
173 | 173 |
/// method. |
174 | 174 |
/// |
175 | 175 |
template <typename IM> |
176 | 176 |
class UnionFindEnum { |
177 | 177 |
public: |
178 | 178 |
|
179 | 179 |
///\e |
180 | 180 |
typedef IM ItemIntMap; |
181 | 181 |
///\e |
182 | 182 |
typedef typename ItemIntMap::Key Item; |
183 | 183 |
|
184 | 184 |
private: |
185 | 185 |
|
186 | 186 |
ItemIntMap& index; |
187 | 187 |
|
188 | 188 |
// If the parent stores negative value for an item then that item |
189 | 189 |
// is root item and it has ~(items[it].parent) component id. Else |
190 | 190 |
// the items[it].parent contains the index of the parent. |
191 | 191 |
// |
192 | 192 |
// The \c next and \c prev provides the double-linked |
193 | 193 |
// cyclic list of one component's items. |
194 | 194 |
struct ItemT { |
195 | 195 |
int parent; |
196 | 196 |
Item item; |
197 | 197 |
|
198 | 198 |
int next, prev; |
199 | 199 |
}; |
200 | 200 |
|
201 | 201 |
std::vector<ItemT> items; |
202 | 202 |
int firstFreeItem; |
203 | 203 |
|
204 | 204 |
struct ClassT { |
205 | 205 |
int size; |
206 | 206 |
int firstItem; |
207 | 207 |
int next, prev; |
208 | 208 |
}; |
209 | 209 |
|
210 | 210 |
std::vector<ClassT> classes; |
211 | 211 |
int firstClass, firstFreeClass; |
212 | 212 |
|
213 | 213 |
int newClass() { |
214 | 214 |
if (firstFreeClass == -1) { |
215 | 215 |
int cdx = classes.size(); |
216 | 216 |
classes.push_back(ClassT()); |
217 | 217 |
return cdx; |
218 | 218 |
} else { |
219 | 219 |
int cdx = firstFreeClass; |
220 | 220 |
firstFreeClass = classes[firstFreeClass].next; |
221 | 221 |
return cdx; |
222 | 222 |
} |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
int newItem() { |
226 | 226 |
if (firstFreeItem == -1) { |
227 | 227 |
int idx = items.size(); |
228 | 228 |
items.push_back(ItemT()); |
229 | 229 |
return idx; |
230 | 230 |
} else { |
231 | 231 |
int idx = firstFreeItem; |
232 | 232 |
firstFreeItem = items[firstFreeItem].next; |
233 | 233 |
return idx; |
234 | 234 |
} |
235 | 235 |
} |
236 | 236 |
|
237 | 237 |
|
238 | 238 |
bool rep(int idx) const { |
0 comments (0 inline)