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 \ref amo93networkflows. |
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 |
} |
... | ... |
@@ -111,385 +111,385 @@ |
111 | 111 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
112 | 112 |
|
113 | 113 |
/// \brief The type of the map that stores the last arcs of the |
114 | 114 |
/// shortest paths. |
115 | 115 |
/// |
116 | 116 |
/// The type of the map that stores the last |
117 | 117 |
/// arcs of the shortest paths. |
118 | 118 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
119 | 119 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
120 | 120 |
|
121 | 121 |
/// \brief 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 GR& g) { |
127 | 127 |
return new PredMap(g); |
128 | 128 |
} |
129 | 129 |
|
130 | 130 |
/// \brief The type of the map that stores the distances of the nodes. |
131 | 131 |
/// |
132 | 132 |
/// The type of the map that stores the distances of the nodes. |
133 | 133 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
134 | 134 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
135 | 135 |
|
136 | 136 |
/// \brief Instantiates a \c DistMap. |
137 | 137 |
/// |
138 | 138 |
/// This function instantiates a \ref DistMap. |
139 | 139 |
/// \param g is the digraph to which we would like to define the |
140 | 140 |
/// \ref DistMap. |
141 | 141 |
static DistMap *createDistMap(const GR& g) { |
142 | 142 |
return new DistMap(g); |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
/// \brief %BellmanFord algorithm class. |
148 | 148 |
/// |
149 | 149 |
/// \ingroup shortest_path |
150 | 150 |
/// This class provides an efficient implementation of the Bellman-Ford |
151 | 151 |
/// algorithm. The maximum time complexity of the algorithm is |
152 | 152 |
/// <tt>O(ne)</tt>. |
153 | 153 |
/// |
154 | 154 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
155 | 155 |
/// problem when the arcs can have negative lengths, but the digraph |
156 | 156 |
/// should not contain directed cycles with negative total length. |
157 | 157 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
158 | 158 |
/// algorithm instead, since it is more efficient. |
159 | 159 |
/// |
160 | 160 |
/// The arc lengths are passed to the algorithm using a |
161 | 161 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
162 | 162 |
/// kind of length. The type of the length values is determined by the |
163 | 163 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
164 | 164 |
/// |
165 | 165 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
166 | 166 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
167 | 167 |
/// it can be used easier. |
168 | 168 |
/// |
169 | 169 |
/// \tparam GR The type of the digraph the algorithm runs on. |
170 | 170 |
/// The default type is \ref ListDigraph. |
171 | 171 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
172 | 172 |
/// the lengths of the arcs. The default map type is |
173 | 173 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
174 | 174 |
#ifdef DOXYGEN |
175 | 175 |
template <typename GR, typename LEN, typename TR> |
176 | 176 |
#else |
177 | 177 |
template <typename GR=ListDigraph, |
178 | 178 |
typename LEN=typename GR::template ArcMap<int>, |
179 | 179 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
180 | 180 |
#endif |
181 | 181 |
class BellmanFord { |
182 | 182 |
public: |
183 | 183 |
|
184 | 184 |
///The type of the underlying digraph. |
185 | 185 |
typedef typename TR::Digraph Digraph; |
186 | 186 |
|
187 | 187 |
/// \brief The type of the arc lengths. |
188 | 188 |
typedef typename TR::LengthMap::Value Value; |
189 | 189 |
/// \brief The type of the map that stores the arc lengths. |
190 | 190 |
typedef typename TR::LengthMap LengthMap; |
191 | 191 |
/// \brief The type of the map that stores the last |
192 | 192 |
/// arcs of the shortest paths. |
193 | 193 |
typedef typename TR::PredMap PredMap; |
194 | 194 |
/// \brief The type of the map that stores the distances of the nodes. |
195 | 195 |
typedef typename TR::DistMap DistMap; |
196 | 196 |
/// The type of the paths. |
197 | 197 |
typedef PredMapPath<Digraph, PredMap> Path; |
198 | 198 |
///\brief The \ref BellmanFordDefaultOperationTraits |
199 | 199 |
/// "operation traits class" of the algorithm. |
200 | 200 |
typedef typename TR::OperationTraits OperationTraits; |
201 | 201 |
|
202 | 202 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
203 | 203 |
typedef TR Traits; |
204 | 204 |
|
205 | 205 |
private: |
206 | 206 |
|
207 | 207 |
typedef typename Digraph::Node Node; |
208 | 208 |
typedef typename Digraph::NodeIt NodeIt; |
209 | 209 |
typedef typename Digraph::Arc Arc; |
210 | 210 |
typedef typename Digraph::OutArcIt OutArcIt; |
211 | 211 |
|
212 | 212 |
// Pointer to the underlying digraph. |
213 | 213 |
const Digraph *_gr; |
214 | 214 |
// Pointer to the length map |
215 | 215 |
const LengthMap *_length; |
216 | 216 |
// Pointer to the map of predecessors arcs. |
217 | 217 |
PredMap *_pred; |
218 | 218 |
// Indicates if _pred is locally allocated (true) or not. |
219 | 219 |
bool _local_pred; |
220 | 220 |
// Pointer to the map of distances. |
221 | 221 |
DistMap *_dist; |
222 | 222 |
// Indicates if _dist is locally allocated (true) or not. |
223 | 223 |
bool _local_dist; |
224 | 224 |
|
225 | 225 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
226 | 226 |
MaskMap *_mask; |
227 | 227 |
|
228 | 228 |
std::vector<Node> _process; |
229 | 229 |
|
230 | 230 |
// Creates the maps if necessary. |
231 | 231 |
void create_maps() { |
232 | 232 |
if(!_pred) { |
233 | 233 |
_local_pred = true; |
234 | 234 |
_pred = Traits::createPredMap(*_gr); |
235 | 235 |
} |
236 | 236 |
if(!_dist) { |
237 | 237 |
_local_dist = true; |
238 | 238 |
_dist = Traits::createDistMap(*_gr); |
239 | 239 |
} |
240 | 240 |
_mask = new MaskMap(*_gr, false); |
241 | 241 |
} |
242 | 242 |
|
243 | 243 |
public : |
244 | 244 |
|
245 | 245 |
typedef BellmanFord Create; |
246 | 246 |
|
247 | 247 |
/// \name Named Template Parameters |
248 | 248 |
|
249 | 249 |
///@{ |
250 | 250 |
|
251 | 251 |
template <class T> |
252 | 252 |
struct SetPredMapTraits : public Traits { |
253 | 253 |
typedef T PredMap; |
254 | 254 |
static PredMap *createPredMap(const Digraph&) { |
255 | 255 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
256 | 256 |
return 0; // ignore warnings |
257 | 257 |
} |
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
/// \brief \ref named-templ-param "Named parameter" for setting |
261 | 261 |
/// \c PredMap type. |
262 | 262 |
/// |
263 | 263 |
/// \ref named-templ-param "Named parameter" for setting |
264 | 264 |
/// \c PredMap type. |
265 | 265 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
266 | 266 |
template <class T> |
267 | 267 |
struct SetPredMap |
268 | 268 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > { |
269 | 269 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
270 | 270 |
}; |
271 | 271 |
|
272 | 272 |
template <class T> |
273 | 273 |
struct SetDistMapTraits : public Traits { |
274 | 274 |
typedef T DistMap; |
275 | 275 |
static DistMap *createDistMap(const Digraph&) { |
276 | 276 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
277 | 277 |
return 0; // ignore warnings |
278 | 278 |
} |
279 | 279 |
}; |
280 | 280 |
|
281 | 281 |
/// \brief \ref named-templ-param "Named parameter" for setting |
282 | 282 |
/// \c DistMap type. |
283 | 283 |
/// |
284 | 284 |
/// \ref named-templ-param "Named parameter" for setting |
285 | 285 |
/// \c DistMap type. |
286 | 286 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
287 | 287 |
template <class T> |
288 | 288 |
struct SetDistMap |
289 | 289 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > { |
290 | 290 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
template <class T> |
294 | 294 |
struct SetOperationTraitsTraits : public Traits { |
295 | 295 |
typedef T OperationTraits; |
296 | 296 |
}; |
297 | 297 |
|
298 | 298 |
/// \brief \ref named-templ-param "Named parameter" for setting |
299 | 299 |
/// \c OperationTraits type. |
300 | 300 |
/// |
301 | 301 |
/// \ref named-templ-param "Named parameter" for setting |
302 | 302 |
/// \c OperationTraits type. |
303 |
/// For more information see \ref BellmanFordDefaultOperationTraits. |
|
303 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
|
304 | 304 |
template <class T> |
305 | 305 |
struct SetOperationTraits |
306 | 306 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
307 | 307 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
308 | 308 |
Create; |
309 | 309 |
}; |
310 | 310 |
|
311 | 311 |
///@} |
312 | 312 |
|
313 | 313 |
protected: |
314 | 314 |
|
315 | 315 |
BellmanFord() {} |
316 | 316 |
|
317 | 317 |
public: |
318 | 318 |
|
319 | 319 |
/// \brief Constructor. |
320 | 320 |
/// |
321 | 321 |
/// Constructor. |
322 | 322 |
/// \param g The digraph the algorithm runs on. |
323 | 323 |
/// \param length The length map used by the algorithm. |
324 | 324 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
325 | 325 |
_gr(&g), _length(&length), |
326 | 326 |
_pred(0), _local_pred(false), |
327 | 327 |
_dist(0), _local_dist(false), _mask(0) {} |
328 | 328 |
|
329 | 329 |
///Destructor. |
330 | 330 |
~BellmanFord() { |
331 | 331 |
if(_local_pred) delete _pred; |
332 | 332 |
if(_local_dist) delete _dist; |
333 | 333 |
if(_mask) delete _mask; |
334 | 334 |
} |
335 | 335 |
|
336 | 336 |
/// \brief Sets the length map. |
337 | 337 |
/// |
338 | 338 |
/// Sets the length map. |
339 | 339 |
/// \return <tt>(*this)</tt> |
340 | 340 |
BellmanFord &lengthMap(const LengthMap &map) { |
341 | 341 |
_length = ↦ |
342 | 342 |
return *this; |
343 | 343 |
} |
344 | 344 |
|
345 | 345 |
/// \brief Sets the map that stores the predecessor arcs. |
346 | 346 |
/// |
347 | 347 |
/// Sets the map that stores the predecessor arcs. |
348 | 348 |
/// If you don't use this function before calling \ref run() |
349 | 349 |
/// or \ref init(), an instance will be allocated automatically. |
350 | 350 |
/// The destructor deallocates this automatically allocated map, |
351 | 351 |
/// of course. |
352 | 352 |
/// \return <tt>(*this)</tt> |
353 | 353 |
BellmanFord &predMap(PredMap &map) { |
354 | 354 |
if(_local_pred) { |
355 | 355 |
delete _pred; |
356 | 356 |
_local_pred=false; |
357 | 357 |
} |
358 | 358 |
_pred = ↦ |
359 | 359 |
return *this; |
360 | 360 |
} |
361 | 361 |
|
362 | 362 |
/// \brief Sets the map that stores the distances of the nodes. |
363 | 363 |
/// |
364 | 364 |
/// Sets the map that stores the distances of the nodes calculated |
365 | 365 |
/// by the algorithm. |
366 | 366 |
/// If you don't use this function before calling \ref run() |
367 | 367 |
/// or \ref init(), an instance will be allocated automatically. |
368 | 368 |
/// The destructor deallocates this automatically allocated map, |
369 | 369 |
/// of course. |
370 | 370 |
/// \return <tt>(*this)</tt> |
371 | 371 |
BellmanFord &distMap(DistMap &map) { |
372 | 372 |
if(_local_dist) { |
373 | 373 |
delete _dist; |
374 | 374 |
_local_dist=false; |
375 | 375 |
} |
376 | 376 |
_dist = ↦ |
377 | 377 |
return *this; |
378 | 378 |
} |
379 | 379 |
|
380 | 380 |
/// \name Execution Control |
381 | 381 |
/// The simplest way to execute the Bellman-Ford algorithm is to use |
382 | 382 |
/// one of the member functions called \ref run().\n |
383 | 383 |
/// If you need better control on the execution, you have to call |
384 | 384 |
/// \ref init() first, then you can add several source nodes |
385 | 385 |
/// with \ref addSource(). Finally the actual path computation can be |
386 | 386 |
/// performed with \ref start(), \ref checkedStart() or |
387 | 387 |
/// \ref limitedStart(). |
388 | 388 |
|
389 | 389 |
///@{ |
390 | 390 |
|
391 | 391 |
/// \brief Initializes the internal data structures. |
392 | 392 |
/// |
393 | 393 |
/// Initializes the internal data structures. The optional parameter |
394 | 394 |
/// is the initial distance of each node. |
395 | 395 |
void init(const Value value = OperationTraits::infinity()) { |
396 | 396 |
create_maps(); |
397 | 397 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
398 | 398 |
_pred->set(it, INVALID); |
399 | 399 |
_dist->set(it, value); |
400 | 400 |
} |
401 | 401 |
_process.clear(); |
402 | 402 |
if (OperationTraits::less(value, OperationTraits::infinity())) { |
403 | 403 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
404 | 404 |
_process.push_back(it); |
405 | 405 |
_mask->set(it, true); |
406 | 406 |
} |
407 | 407 |
} |
408 | 408 |
} |
409 | 409 |
|
410 | 410 |
/// \brief Adds a new source node. |
411 | 411 |
/// |
412 | 412 |
/// This function adds a new source node. The optional second parameter |
413 | 413 |
/// is the initial distance of the node. |
414 | 414 |
void addSource(Node source, Value dst = OperationTraits::zero()) { |
415 | 415 |
_dist->set(source, dst); |
416 | 416 |
if (!(*_mask)[source]) { |
417 | 417 |
_process.push_back(source); |
418 | 418 |
_mask->set(source, true); |
419 | 419 |
} |
420 | 420 |
} |
421 | 421 |
|
422 | 422 |
/// \brief Executes one round from the Bellman-Ford algorithm. |
423 | 423 |
/// |
424 | 424 |
/// If the algoritm calculated the distances in the previous round |
425 | 425 |
/// exactly for the paths of at most \c k arcs, then this function |
426 | 426 |
/// will calculate the distances exactly for the paths of at most |
427 | 427 |
/// <tt>k+1</tt> arcs. Performing \c k iterations using this function |
428 | 428 |
/// calculates the shortest path distances exactly for the paths |
429 | 429 |
/// consisting of at most \c k arcs. |
430 | 430 |
/// |
431 | 431 |
/// \warning The paths with limited arc number cannot be retrieved |
432 | 432 |
/// easily with \ref path() or \ref predArc() functions. If you also |
433 | 433 |
/// need the shortest paths and not only the distances, you should |
434 | 434 |
/// store the \ref predMap() "predecessor map" after each iteration |
435 | 435 |
/// and build the path manually. |
436 | 436 |
/// |
437 | 437 |
/// \return \c true when the algorithm have not found more shorter |
438 | 438 |
/// paths. |
439 | 439 |
/// |
440 | 440 |
/// \see ActiveIt |
441 | 441 |
bool processNextRound() { |
442 | 442 |
for (int i = 0; i < int(_process.size()); ++i) { |
443 | 443 |
_mask->set(_process[i], false); |
444 | 444 |
} |
445 | 445 |
std::vector<Node> nextProcess; |
446 | 446 |
std::vector<Value> values(_process.size()); |
447 | 447 |
for (int i = 0; i < int(_process.size()); ++i) { |
448 | 448 |
values[i] = (*_dist)[_process[i]]; |
449 | 449 |
} |
450 | 450 |
for (int i = 0; i < int(_process.size()); ++i) { |
451 | 451 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
452 | 452 |
Node target = _gr->target(it); |
453 | 453 |
Value relaxed = OperationTraits::plus(values[i], (*_length)[it]); |
454 | 454 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
455 | 455 |
_pred->set(target, it); |
456 | 456 |
_dist->set(target, relaxed); |
457 | 457 |
if (!(*_mask)[target]) { |
458 | 458 |
_mask->set(target, true); |
459 | 459 |
nextProcess.push_back(target); |
460 | 460 |
} |
461 | 461 |
} |
462 | 462 |
} |
463 | 463 |
} |
464 | 464 |
_process.swap(nextProcess); |
465 | 465 |
return _process.empty(); |
466 | 466 |
} |
467 | 467 |
|
468 | 468 |
/// \brief Executes one weak round from the Bellman-Ford algorithm. |
469 | 469 |
/// |
470 | 470 |
/// If the algorithm calculated the distances in the previous round |
471 | 471 |
/// at least for the paths of at most \c k arcs, then this function |
472 | 472 |
/// will calculate the distances at least for the paths of at most |
473 | 473 |
/// <tt>k+1</tt> arcs. |
474 | 474 |
/// This function does not make it possible to calculate the shortest |
475 | 475 |
/// path distances exactly for paths consisting of at most \c k arcs, |
476 | 476 |
/// this is why it is called weak round. |
477 | 477 |
/// |
478 | 478 |
/// \return \c true when the algorithm have not found more shorter |
479 | 479 |
/// paths. |
480 | 480 |
/// |
481 | 481 |
/// \see ActiveIt |
482 | 482 |
bool processNextWeakRound() { |
483 | 483 |
for (int i = 0; i < int(_process.size()); ++i) { |
484 | 484 |
_mask->set(_process[i], false); |
485 | 485 |
} |
486 | 486 |
std::vector<Node> nextProcess; |
487 | 487 |
for (int i = 0; i < int(_process.size()); ++i) { |
488 | 488 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
489 | 489 |
Node target = _gr->target(it); |
490 | 490 |
Value relaxed = |
491 | 491 |
OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]); |
492 | 492 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
493 | 493 |
_pred->set(target, it); |
494 | 494 |
_dist->set(target, relaxed); |
495 | 495 |
if (!(*_mask)[target]) { |
... | ... |
@@ -529,400 +529,400 @@ |
529 | 529 |
/// |
530 | 530 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
531 | 531 |
/// in order to compute the shortest path to each node and also checks |
532 | 532 |
/// if the digraph contains cycles with negative total length. |
533 | 533 |
/// |
534 | 534 |
/// The algorithm computes |
535 | 535 |
/// - the shortest path tree (forest), |
536 | 536 |
/// - the distance of each node from the root(s). |
537 | 537 |
/// |
538 | 538 |
/// \return \c false if there is a negative cycle in the digraph. |
539 | 539 |
/// |
540 | 540 |
/// \pre init() must be called and at least one root node should be |
541 | 541 |
/// added with addSource() before using this function. |
542 | 542 |
bool checkedStart() { |
543 | 543 |
int num = countNodes(*_gr); |
544 | 544 |
for (int i = 0; i < num; ++i) { |
545 | 545 |
if (processNextWeakRound()) return true; |
546 | 546 |
} |
547 | 547 |
return _process.empty(); |
548 | 548 |
} |
549 | 549 |
|
550 | 550 |
/// \brief Executes the algorithm with arc number limit. |
551 | 551 |
/// |
552 | 552 |
/// Executes the algorithm with arc number limit. |
553 | 553 |
/// |
554 | 554 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
555 | 555 |
/// in order to compute the shortest path distance for each node |
556 | 556 |
/// using only the paths consisting of at most \c num arcs. |
557 | 557 |
/// |
558 | 558 |
/// The algorithm computes |
559 | 559 |
/// - the limited distance of each node from the root(s), |
560 | 560 |
/// - the predecessor arc for each node. |
561 | 561 |
/// |
562 | 562 |
/// \warning The paths with limited arc number cannot be retrieved |
563 | 563 |
/// easily with \ref path() or \ref predArc() functions. If you also |
564 | 564 |
/// need the shortest paths and not only the distances, you should |
565 | 565 |
/// store the \ref predMap() "predecessor map" after each iteration |
566 | 566 |
/// and build the path manually. |
567 | 567 |
/// |
568 | 568 |
/// \pre init() must be called and at least one root node should be |
569 | 569 |
/// added with addSource() before using this function. |
570 | 570 |
void limitedStart(int num) { |
571 | 571 |
for (int i = 0; i < num; ++i) { |
572 | 572 |
if (processNextRound()) break; |
573 | 573 |
} |
574 | 574 |
} |
575 | 575 |
|
576 | 576 |
/// \brief Runs the algorithm from the given root node. |
577 | 577 |
/// |
578 | 578 |
/// This method runs the Bellman-Ford algorithm from the given root |
579 | 579 |
/// node \c s in order to compute the shortest path to each node. |
580 | 580 |
/// |
581 | 581 |
/// The algorithm computes |
582 | 582 |
/// - the shortest path tree (forest), |
583 | 583 |
/// - the distance of each node from the root(s). |
584 | 584 |
/// |
585 | 585 |
/// \note bf.run(s) is just a shortcut of the following code. |
586 | 586 |
/// \code |
587 | 587 |
/// bf.init(); |
588 | 588 |
/// bf.addSource(s); |
589 | 589 |
/// bf.start(); |
590 | 590 |
/// \endcode |
591 | 591 |
void run(Node s) { |
592 | 592 |
init(); |
593 | 593 |
addSource(s); |
594 | 594 |
start(); |
595 | 595 |
} |
596 | 596 |
|
597 | 597 |
/// \brief Runs the algorithm from the given root node with arc |
598 | 598 |
/// number limit. |
599 | 599 |
/// |
600 | 600 |
/// This method runs the Bellman-Ford algorithm from the given root |
601 | 601 |
/// node \c s in order to compute the shortest path distance for each |
602 | 602 |
/// node using only the paths consisting of at most \c num arcs. |
603 | 603 |
/// |
604 | 604 |
/// The algorithm computes |
605 | 605 |
/// - the limited distance of each node from the root(s), |
606 | 606 |
/// - the predecessor arc for each node. |
607 | 607 |
/// |
608 | 608 |
/// \warning The paths with limited arc number cannot be retrieved |
609 | 609 |
/// easily with \ref path() or \ref predArc() functions. If you also |
610 | 610 |
/// need the shortest paths and not only the distances, you should |
611 | 611 |
/// store the \ref predMap() "predecessor map" after each iteration |
612 | 612 |
/// and build the path manually. |
613 | 613 |
/// |
614 | 614 |
/// \note bf.run(s, num) is just a shortcut of the following code. |
615 | 615 |
/// \code |
616 | 616 |
/// bf.init(); |
617 | 617 |
/// bf.addSource(s); |
618 | 618 |
/// bf.limitedStart(num); |
619 | 619 |
/// \endcode |
620 | 620 |
void run(Node s, int num) { |
621 | 621 |
init(); |
622 | 622 |
addSource(s); |
623 | 623 |
limitedStart(num); |
624 | 624 |
} |
625 | 625 |
|
626 | 626 |
///@} |
627 | 627 |
|
628 | 628 |
/// \brief LEMON iterator for getting the active nodes. |
629 | 629 |
/// |
630 | 630 |
/// This class provides a common style LEMON iterator that traverses |
631 | 631 |
/// the active nodes of the Bellman-Ford algorithm after the last |
632 | 632 |
/// phase. These nodes should be checked in the next phase to |
633 | 633 |
/// find augmenting arcs outgoing from them. |
634 | 634 |
class ActiveIt { |
635 | 635 |
public: |
636 | 636 |
|
637 | 637 |
/// \brief Constructor. |
638 | 638 |
/// |
639 | 639 |
/// Constructor for getting the active nodes of the given BellmanFord |
640 | 640 |
/// instance. |
641 | 641 |
ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
642 | 642 |
{ |
643 | 643 |
_index = _algorithm->_process.size() - 1; |
644 | 644 |
} |
645 | 645 |
|
646 | 646 |
/// \brief Invalid constructor. |
647 | 647 |
/// |
648 | 648 |
/// Invalid constructor. |
649 | 649 |
ActiveIt(Invalid) : _algorithm(0), _index(-1) {} |
650 | 650 |
|
651 | 651 |
/// \brief Conversion to \c Node. |
652 | 652 |
/// |
653 | 653 |
/// Conversion to \c Node. |
654 | 654 |
operator Node() const { |
655 | 655 |
return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
656 | 656 |
} |
657 | 657 |
|
658 | 658 |
/// \brief Increment operator. |
659 | 659 |
/// |
660 | 660 |
/// Increment operator. |
661 | 661 |
ActiveIt& operator++() { |
662 | 662 |
--_index; |
663 | 663 |
return *this; |
664 | 664 |
} |
665 | 665 |
|
666 | 666 |
bool operator==(const ActiveIt& it) const { |
667 | 667 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
668 | 668 |
} |
669 | 669 |
bool operator!=(const ActiveIt& it) const { |
670 | 670 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
671 | 671 |
} |
672 | 672 |
bool operator<(const ActiveIt& it) const { |
673 | 673 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
674 | 674 |
} |
675 | 675 |
|
676 | 676 |
private: |
677 | 677 |
const BellmanFord* _algorithm; |
678 | 678 |
int _index; |
679 | 679 |
}; |
680 | 680 |
|
681 | 681 |
/// \name Query Functions |
682 | 682 |
/// The result of the Bellman-Ford algorithm can be obtained using these |
683 | 683 |
/// functions.\n |
684 | 684 |
/// Either \ref run() or \ref init() should be called before using them. |
685 | 685 |
|
686 | 686 |
///@{ |
687 | 687 |
|
688 | 688 |
/// \brief The shortest path to the given node. |
689 | 689 |
/// |
690 | 690 |
/// Gives back the shortest path to the given node from the root(s). |
691 | 691 |
/// |
692 | 692 |
/// \warning \c t should be reached from the root(s). |
693 | 693 |
/// |
694 | 694 |
/// \pre Either \ref run() or \ref init() must be called before |
695 | 695 |
/// using this function. |
696 | 696 |
Path path(Node t) const |
697 | 697 |
{ |
698 | 698 |
return Path(*_gr, *_pred, t); |
699 | 699 |
} |
700 | 700 |
|
701 | 701 |
/// \brief The distance of the given node from the root(s). |
702 | 702 |
/// |
703 | 703 |
/// Returns the distance of the given node from the root(s). |
704 | 704 |
/// |
705 | 705 |
/// \warning If node \c v is not reached from the root(s), then |
706 | 706 |
/// the return value of this function is undefined. |
707 | 707 |
/// |
708 | 708 |
/// \pre Either \ref run() or \ref init() must be called before |
709 | 709 |
/// using this function. |
710 | 710 |
Value dist(Node v) const { return (*_dist)[v]; } |
711 | 711 |
|
712 | 712 |
/// \brief Returns the 'previous arc' of the shortest path tree for |
713 | 713 |
/// the given node. |
714 | 714 |
/// |
715 | 715 |
/// This function returns the 'previous arc' of the shortest path |
716 | 716 |
/// tree for node \c v, i.e. it returns the last arc of a |
717 | 717 |
/// shortest path from a root to \c v. It is \c INVALID if \c v |
718 | 718 |
/// is not reached from the root(s) or if \c v is a root. |
719 | 719 |
/// |
720 | 720 |
/// The shortest path tree used here is equal to the shortest path |
721 |
/// tree used in \ref predNode() and \predMap(). |
|
721 |
/// tree used in \ref predNode() and \ref predMap(). |
|
722 | 722 |
/// |
723 | 723 |
/// \pre Either \ref run() or \ref init() must be called before |
724 | 724 |
/// using this function. |
725 | 725 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
726 | 726 |
|
727 | 727 |
/// \brief Returns the 'previous node' of the shortest path tree for |
728 | 728 |
/// the given node. |
729 | 729 |
/// |
730 | 730 |
/// This function returns the 'previous node' of the shortest path |
731 | 731 |
/// tree for node \c v, i.e. it returns the last but one node of |
732 | 732 |
/// a shortest path from a root to \c v. It is \c INVALID if \c v |
733 | 733 |
/// is not reached from the root(s) or if \c v is a root. |
734 | 734 |
/// |
735 | 735 |
/// The shortest path tree used here is equal to the shortest path |
736 |
/// tree used in \ref predArc() and \predMap(). |
|
736 |
/// tree used in \ref predArc() and \ref predMap(). |
|
737 | 737 |
/// |
738 | 738 |
/// \pre Either \ref run() or \ref init() must be called before |
739 | 739 |
/// using this function. |
740 | 740 |
Node predNode(Node v) const { |
741 | 741 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
742 | 742 |
} |
743 | 743 |
|
744 | 744 |
/// \brief Returns a const reference to the node map that stores the |
745 | 745 |
/// distances of the nodes. |
746 | 746 |
/// |
747 | 747 |
/// Returns a const reference to the node map that stores the distances |
748 | 748 |
/// of the nodes calculated by the algorithm. |
749 | 749 |
/// |
750 | 750 |
/// \pre Either \ref run() or \ref init() must be called before |
751 | 751 |
/// using this function. |
752 | 752 |
const DistMap &distMap() const { return *_dist;} |
753 | 753 |
|
754 | 754 |
/// \brief Returns a const reference to the node map that stores the |
755 | 755 |
/// predecessor arcs. |
756 | 756 |
/// |
757 | 757 |
/// Returns a const reference to the node map that stores the predecessor |
758 | 758 |
/// arcs, which form the shortest path tree (forest). |
759 | 759 |
/// |
760 | 760 |
/// \pre Either \ref run() or \ref init() must be called before |
761 | 761 |
/// using this function. |
762 | 762 |
const PredMap &predMap() const { return *_pred; } |
763 | 763 |
|
764 | 764 |
/// \brief Checks if a node is reached from the root(s). |
765 | 765 |
/// |
766 | 766 |
/// Returns \c true if \c v is reached from the root(s). |
767 | 767 |
/// |
768 | 768 |
/// \pre Either \ref run() or \ref init() must be called before |
769 | 769 |
/// using this function. |
770 | 770 |
bool reached(Node v) const { |
771 | 771 |
return (*_dist)[v] != OperationTraits::infinity(); |
772 | 772 |
} |
773 | 773 |
|
774 | 774 |
/// \brief Gives back a negative cycle. |
775 | 775 |
/// |
776 | 776 |
/// This function gives back a directed cycle with negative total |
777 | 777 |
/// length if the algorithm has already found one. |
778 | 778 |
/// Otherwise it gives back an empty path. |
779 | 779 |
lemon::Path<Digraph> negativeCycle() const { |
780 | 780 |
typename Digraph::template NodeMap<int> state(*_gr, -1); |
781 | 781 |
lemon::Path<Digraph> cycle; |
782 | 782 |
for (int i = 0; i < int(_process.size()); ++i) { |
783 | 783 |
if (state[_process[i]] != -1) continue; |
784 | 784 |
for (Node v = _process[i]; (*_pred)[v] != INVALID; |
785 | 785 |
v = _gr->source((*_pred)[v])) { |
786 | 786 |
if (state[v] == i) { |
787 | 787 |
cycle.addFront((*_pred)[v]); |
788 | 788 |
for (Node u = _gr->source((*_pred)[v]); u != v; |
789 | 789 |
u = _gr->source((*_pred)[u])) { |
790 | 790 |
cycle.addFront((*_pred)[u]); |
791 | 791 |
} |
792 | 792 |
return cycle; |
793 | 793 |
} |
794 | 794 |
else if (state[v] >= 0) { |
795 | 795 |
break; |
796 | 796 |
} |
797 | 797 |
state[v] = i; |
798 | 798 |
} |
799 | 799 |
} |
800 | 800 |
return cycle; |
801 | 801 |
} |
802 | 802 |
|
803 | 803 |
///@} |
804 | 804 |
}; |
805 | 805 |
|
806 | 806 |
/// \brief Default traits class of bellmanFord() function. |
807 | 807 |
/// |
808 | 808 |
/// Default traits class of bellmanFord() function. |
809 | 809 |
/// \tparam GR The type of the digraph. |
810 | 810 |
/// \tparam LEN The type of the length map. |
811 | 811 |
template <typename GR, typename LEN> |
812 | 812 |
struct BellmanFordWizardDefaultTraits { |
813 | 813 |
/// The type of the digraph the algorithm runs on. |
814 | 814 |
typedef GR Digraph; |
815 | 815 |
|
816 | 816 |
/// \brief The type of the map that stores the arc lengths. |
817 | 817 |
/// |
818 | 818 |
/// The type of the map that stores the arc lengths. |
819 | 819 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
820 | 820 |
typedef LEN LengthMap; |
821 | 821 |
|
822 | 822 |
/// The type of the arc lengths. |
823 | 823 |
typedef typename LEN::Value Value; |
824 | 824 |
|
825 | 825 |
/// \brief Operation traits for Bellman-Ford algorithm. |
826 | 826 |
/// |
827 | 827 |
/// It defines the used operations and the infinity value for the |
828 | 828 |
/// given \c Value type. |
829 | 829 |
/// \see BellmanFordDefaultOperationTraits |
830 | 830 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
831 | 831 |
|
832 | 832 |
/// \brief The type of the map that stores the last |
833 | 833 |
/// arcs of the shortest paths. |
834 | 834 |
/// |
835 | 835 |
/// The type of the map that stores the last arcs of the shortest paths. |
836 | 836 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
837 | 837 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
838 | 838 |
|
839 | 839 |
/// \brief Instantiates a \c PredMap. |
840 | 840 |
/// |
841 | 841 |
/// This function instantiates a \ref PredMap. |
842 | 842 |
/// \param g is the digraph to which we would like to define the |
843 | 843 |
/// \ref PredMap. |
844 | 844 |
static PredMap *createPredMap(const GR &g) { |
845 | 845 |
return new PredMap(g); |
846 | 846 |
} |
847 | 847 |
|
848 | 848 |
/// \brief The type of the map that stores the distances of the nodes. |
849 | 849 |
/// |
850 | 850 |
/// The type of the map that stores the distances of the nodes. |
851 | 851 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
852 | 852 |
typedef typename GR::template NodeMap<Value> DistMap; |
853 | 853 |
|
854 | 854 |
/// \brief Instantiates a \c DistMap. |
855 | 855 |
/// |
856 | 856 |
/// This function instantiates a \ref DistMap. |
857 | 857 |
/// \param g is the digraph to which we would like to define the |
858 | 858 |
/// \ref DistMap. |
859 | 859 |
static DistMap *createDistMap(const GR &g) { |
860 | 860 |
return new DistMap(g); |
861 | 861 |
} |
862 | 862 |
|
863 | 863 |
///The type of the shortest paths. |
864 | 864 |
|
865 | 865 |
///The type of the shortest paths. |
866 | 866 |
///It must meet the \ref concepts::Path "Path" concept. |
867 | 867 |
typedef lemon::Path<Digraph> Path; |
868 | 868 |
}; |
869 | 869 |
|
870 | 870 |
/// \brief Default traits class used by BellmanFordWizard. |
871 | 871 |
/// |
872 | 872 |
/// Default traits class used by BellmanFordWizard. |
873 | 873 |
/// \tparam GR The type of the digraph. |
874 | 874 |
/// \tparam LEN The type of the length map. |
875 | 875 |
template <typename GR, typename LEN> |
876 | 876 |
class BellmanFordWizardBase |
877 | 877 |
: public BellmanFordWizardDefaultTraits<GR, LEN> { |
878 | 878 |
|
879 | 879 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
880 | 880 |
protected: |
881 | 881 |
// Type of the nodes in the digraph. |
882 | 882 |
typedef typename Base::Digraph::Node Node; |
883 | 883 |
|
884 | 884 |
// Pointer to the underlying digraph. |
885 | 885 |
void *_graph; |
886 | 886 |
// Pointer to the length map |
887 | 887 |
void *_length; |
888 | 888 |
// Pointer to the map of predecessors arcs. |
889 | 889 |
void *_pred; |
890 | 890 |
// Pointer to the map of distances. |
891 | 891 |
void *_dist; |
892 | 892 |
//Pointer to the shortest path to the target node. |
893 | 893 |
void *_path; |
894 | 894 |
//Pointer to the distance of the target node. |
895 | 895 |
void *_di; |
896 | 896 |
|
897 | 897 |
public: |
898 | 898 |
/// Constructor. |
899 | 899 |
|
900 | 900 |
/// This constructor does not require parameters, it initiates |
901 | 901 |
/// all of the attributes to default values \c 0. |
902 | 902 |
BellmanFordWizardBase() : |
903 | 903 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
904 | 904 |
|
905 | 905 |
/// Constructor. |
906 | 906 |
|
907 | 907 |
/// This constructor requires two parameters, |
908 | 908 |
/// others are initiated to \c 0. |
909 | 909 |
/// \param gr The digraph the algorithm runs on. |
910 | 910 |
/// \param len The length map. |
911 | 911 |
BellmanFordWizardBase(const GR& gr, |
912 | 912 |
const LEN& len) : |
913 | 913 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
914 | 914 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
915 | 915 |
_pred(0), _dist(0), _path(0), _di(0) {} |
916 | 916 |
|
917 | 917 |
}; |
918 | 918 |
|
919 | 919 |
/// \brief Auxiliary class for the function-type interface of the |
920 | 920 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
921 | 921 |
/// |
922 | 922 |
/// This auxiliary class is created to implement the |
923 | 923 |
/// \ref bellmanFord() "function-type interface" of the |
924 | 924 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
925 | 925 |
/// It does not have own \ref run() method, it uses the |
926 | 926 |
/// functions and features of the plain \ref BellmanFord. |
927 | 927 |
/// |
928 | 928 |
/// 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 &) |
... | ... |
@@ -659,385 +659,385 @@ |
659 | 659 |
///Runs the algorithm from the given source node. |
660 | 660 |
|
661 | 661 |
///This method runs the %BFS algorithm from node \c s |
662 | 662 |
///in order to compute the shortest path to each node. |
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 visit all nodes |
705 | 705 |
///in the digraph. |
706 | 706 |
/// |
707 | 707 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
708 | 708 |
///\code |
709 | 709 |
/// b.init(); |
710 | 710 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
711 | 711 |
/// if (!b.reached(n)) { |
712 | 712 |
/// b.addSource(n); |
713 | 713 |
/// b.start(); |
714 | 714 |
/// } |
715 | 715 |
/// } |
716 | 716 |
///\endcode |
717 | 717 |
void run() { |
718 | 718 |
init(); |
719 | 719 |
for (NodeIt n(*G); n != INVALID; ++n) { |
720 | 720 |
if (!reached(n)) { |
721 | 721 |
addSource(n); |
722 | 722 |
start(); |
723 | 723 |
} |
724 | 724 |
} |
725 | 725 |
} |
726 | 726 |
|
727 | 727 |
///@} |
728 | 728 |
|
729 | 729 |
///\name Query Functions |
730 | 730 |
///The results of the BFS algorithm can be obtained using these |
731 | 731 |
///functions.\n |
732 | 732 |
///Either \ref run(Node) "run()" or \ref start() should be called |
733 | 733 |
///before using them. |
734 | 734 |
|
735 | 735 |
///@{ |
736 | 736 |
|
737 | 737 |
///The shortest path to the given node. |
738 | 738 |
|
739 | 739 |
///Returns the shortest path to the given node from the root(s). |
740 | 740 |
/// |
741 | 741 |
///\warning \c t should be reached from the root(s). |
742 | 742 |
/// |
743 | 743 |
///\pre Either \ref run(Node) "run()" or \ref init() |
744 | 744 |
///must be called before using this function. |
745 | 745 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
746 | 746 |
|
747 | 747 |
///The distance of the given node from the root(s). |
748 | 748 |
|
749 | 749 |
///Returns the distance of the given node from the root(s). |
750 | 750 |
/// |
751 | 751 |
///\warning If node \c v is not reached from the root(s), then |
752 | 752 |
///the return value of this function is undefined. |
753 | 753 |
/// |
754 | 754 |
///\pre Either \ref run(Node) "run()" or \ref init() |
755 | 755 |
///must be called before using this function. |
756 | 756 |
int dist(Node v) const { return (*_dist)[v]; } |
757 | 757 |
|
758 | 758 |
///\brief Returns the 'previous arc' of the shortest path tree for |
759 | 759 |
///the given node. |
760 | 760 |
/// |
761 | 761 |
///This function returns the 'previous arc' of the shortest path |
762 | 762 |
///tree for the node \c v, i.e. it returns the last arc of a |
763 | 763 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
764 | 764 |
///is not reached from the root(s) or if \c v is a root. |
765 | 765 |
/// |
766 | 766 |
///The shortest path tree used here is equal to the shortest path |
767 | 767 |
///tree used in \ref predNode() and \ref predMap(). |
768 | 768 |
/// |
769 | 769 |
///\pre Either \ref run(Node) "run()" or \ref init() |
770 | 770 |
///must be called before using this function. |
771 | 771 |
Arc predArc(Node v) const { return (*_pred)[v];} |
772 | 772 |
|
773 | 773 |
///\brief Returns the 'previous node' of the shortest path tree for |
774 | 774 |
///the given node. |
775 | 775 |
/// |
776 | 776 |
///This function returns the 'previous node' of the shortest path |
777 | 777 |
///tree for the node \c v, i.e. it returns the last but one node |
778 | 778 |
///of a shortest path from a root to \c v. It is \c INVALID |
779 | 779 |
///if \c v is not reached from the root(s) or if \c v is a root. |
780 | 780 |
/// |
781 | 781 |
///The shortest path tree used here is equal to the shortest path |
782 | 782 |
///tree used in \ref predArc() and \ref predMap(). |
783 | 783 |
/// |
784 | 784 |
///\pre Either \ref run(Node) "run()" or \ref init() |
785 | 785 |
///must be called before using this function. |
786 | 786 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
787 | 787 |
G->source((*_pred)[v]); } |
788 | 788 |
|
789 | 789 |
///\brief Returns a const reference to the node map that stores the |
790 | 790 |
/// distances of the nodes. |
791 | 791 |
/// |
792 | 792 |
///Returns a const reference to the node map that stores the distances |
793 | 793 |
///of the nodes calculated by the algorithm. |
794 | 794 |
/// |
795 | 795 |
///\pre Either \ref run(Node) "run()" or \ref init() |
796 | 796 |
///must be called before using this function. |
797 | 797 |
const DistMap &distMap() const { return *_dist;} |
798 | 798 |
|
799 | 799 |
///\brief Returns a const reference to the node map that stores the |
800 | 800 |
///predecessor arcs. |
801 | 801 |
/// |
802 | 802 |
///Returns a const reference to the node map that stores the predecessor |
803 | 803 |
///arcs, which form the shortest path tree (forest). |
804 | 804 |
/// |
805 | 805 |
///\pre Either \ref run(Node) "run()" or \ref init() |
806 | 806 |
///must be called before using this function. |
807 | 807 |
const PredMap &predMap() const { return *_pred;} |
808 | 808 |
|
809 | 809 |
///Checks if the given node is reached from the root(s). |
810 | 810 |
|
811 | 811 |
///Returns \c true if \c v is reached from the root(s). |
812 | 812 |
/// |
813 | 813 |
///\pre Either \ref run(Node) "run()" or \ref init() |
814 | 814 |
///must be called before using this function. |
815 | 815 |
bool reached(Node v) const { return (*_reached)[v]; } |
816 | 816 |
|
817 | 817 |
///@} |
818 | 818 |
}; |
819 | 819 |
|
820 | 820 |
///Default traits class of bfs() function. |
821 | 821 |
|
822 | 822 |
///Default traits class of bfs() function. |
823 | 823 |
///\tparam GR Digraph type. |
824 | 824 |
template<class GR> |
825 | 825 |
struct BfsWizardDefaultTraits |
826 | 826 |
{ |
827 | 827 |
///The type of the digraph the algorithm runs on. |
828 | 828 |
typedef GR Digraph; |
829 | 829 |
|
830 | 830 |
///\brief The type of the map that stores the predecessor |
831 | 831 |
///arcs of the shortest paths. |
832 | 832 |
/// |
833 | 833 |
///The type of the map that stores the predecessor |
834 | 834 |
///arcs of the shortest paths. |
835 | 835 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
836 | 836 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
837 | 837 |
///Instantiates a PredMap. |
838 | 838 |
|
839 | 839 |
///This function instantiates a PredMap. |
840 | 840 |
///\param g is the digraph, to which we would like to define the |
841 | 841 |
///PredMap. |
842 | 842 |
static PredMap *createPredMap(const Digraph &g) |
843 | 843 |
{ |
844 | 844 |
return new PredMap(g); |
845 | 845 |
} |
846 | 846 |
|
847 | 847 |
///The type of the map that indicates which nodes are processed. |
848 | 848 |
|
849 | 849 |
///The type of the map that indicates which nodes are processed. |
850 | 850 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
851 |
///By default it is a NullMap. |
|
851 |
///By default, it is a NullMap. |
|
852 | 852 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
853 | 853 |
///Instantiates a ProcessedMap. |
854 | 854 |
|
855 | 855 |
///This function instantiates a ProcessedMap. |
856 | 856 |
///\param g is the digraph, to which |
857 | 857 |
///we would like to define the ProcessedMap. |
858 | 858 |
#ifdef DOXYGEN |
859 | 859 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
860 | 860 |
#else |
861 | 861 |
static ProcessedMap *createProcessedMap(const Digraph &) |
862 | 862 |
#endif |
863 | 863 |
{ |
864 | 864 |
return new ProcessedMap(); |
865 | 865 |
} |
866 | 866 |
|
867 | 867 |
///The type of the map that indicates which nodes are reached. |
868 | 868 |
|
869 | 869 |
///The type of the map that indicates which nodes are reached. |
870 | 870 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
871 | 871 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
872 | 872 |
///Instantiates a ReachedMap. |
873 | 873 |
|
874 | 874 |
///This function instantiates a ReachedMap. |
875 | 875 |
///\param g is the digraph, to which |
876 | 876 |
///we would like to define the ReachedMap. |
877 | 877 |
static ReachedMap *createReachedMap(const Digraph &g) |
878 | 878 |
{ |
879 | 879 |
return new ReachedMap(g); |
880 | 880 |
} |
881 | 881 |
|
882 | 882 |
///The type of the map that stores the distances of the nodes. |
883 | 883 |
|
884 | 884 |
///The type of the map that stores the distances of the nodes. |
885 | 885 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
886 | 886 |
typedef typename Digraph::template NodeMap<int> DistMap; |
887 | 887 |
///Instantiates a DistMap. |
888 | 888 |
|
889 | 889 |
///This function instantiates a DistMap. |
890 | 890 |
///\param g is the digraph, to which we would like to define |
891 | 891 |
///the DistMap |
892 | 892 |
static DistMap *createDistMap(const Digraph &g) |
893 | 893 |
{ |
894 | 894 |
return new DistMap(g); |
895 | 895 |
} |
896 | 896 |
|
897 | 897 |
///The type of the shortest paths. |
898 | 898 |
|
899 | 899 |
///The type of the shortest paths. |
900 | 900 |
///It must conform to the \ref concepts::Path "Path" concept. |
901 | 901 |
typedef lemon::Path<Digraph> Path; |
902 | 902 |
}; |
903 | 903 |
|
904 | 904 |
/// Default traits class used by BfsWizard |
905 | 905 |
|
906 | 906 |
/// Default traits class used by BfsWizard. |
907 | 907 |
/// \tparam GR The type of the digraph. |
908 | 908 |
template<class GR> |
909 | 909 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
910 | 910 |
{ |
911 | 911 |
|
912 | 912 |
typedef BfsWizardDefaultTraits<GR> Base; |
913 | 913 |
protected: |
914 | 914 |
//The type of the nodes in the digraph. |
915 | 915 |
typedef typename Base::Digraph::Node Node; |
916 | 916 |
|
917 | 917 |
//Pointer to the digraph the algorithm runs on. |
918 | 918 |
void *_g; |
919 | 919 |
//Pointer to the map of reached nodes. |
920 | 920 |
void *_reached; |
921 | 921 |
//Pointer to the map of processed nodes. |
922 | 922 |
void *_processed; |
923 | 923 |
//Pointer to the map of predecessors arcs. |
924 | 924 |
void *_pred; |
925 | 925 |
//Pointer to the map of distances. |
926 | 926 |
void *_dist; |
927 | 927 |
//Pointer to the shortest path to the target node. |
928 | 928 |
void *_path; |
929 | 929 |
//Pointer to the distance of the target node. |
930 | 930 |
int *_di; |
931 | 931 |
|
932 | 932 |
public: |
933 | 933 |
/// Constructor. |
934 | 934 |
|
935 | 935 |
/// This constructor does not require parameters, it initiates |
936 | 936 |
/// all of the attributes to \c 0. |
937 | 937 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
938 | 938 |
_dist(0), _path(0), _di(0) {} |
939 | 939 |
|
940 | 940 |
/// Constructor. |
941 | 941 |
|
942 | 942 |
/// This constructor requires one parameter, |
943 | 943 |
/// others are initiated to \c 0. |
944 | 944 |
/// \param g The digraph the algorithm runs on. |
945 | 945 |
BfsWizardBase(const GR &g) : |
946 | 946 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
947 | 947 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
948 | 948 |
|
949 | 949 |
}; |
950 | 950 |
|
951 | 951 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
952 | 952 |
|
953 | 953 |
/// This auxiliary class is created to implement the |
954 | 954 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
955 | 955 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
956 | 956 |
/// functions and features of the plain \ref Bfs. |
957 | 957 |
/// |
958 | 958 |
/// This class should only be used through the \ref bfs() function, |
959 | 959 |
/// which makes it easier to use the algorithm. |
960 | 960 |
template<class TR> |
961 | 961 |
class BfsWizard : public TR |
962 | 962 |
{ |
963 | 963 |
typedef TR Base; |
964 | 964 |
|
965 | 965 |
typedef typename TR::Digraph Digraph; |
966 | 966 |
|
967 | 967 |
typedef typename Digraph::Node Node; |
968 | 968 |
typedef typename Digraph::NodeIt NodeIt; |
969 | 969 |
typedef typename Digraph::Arc Arc; |
970 | 970 |
typedef typename Digraph::OutArcIt OutArcIt; |
971 | 971 |
|
972 | 972 |
typedef typename TR::PredMap PredMap; |
973 | 973 |
typedef typename TR::DistMap DistMap; |
974 | 974 |
typedef typename TR::ReachedMap ReachedMap; |
975 | 975 |
typedef typename TR::ProcessedMap ProcessedMap; |
976 | 976 |
typedef typename TR::Path Path; |
977 | 977 |
|
978 | 978 |
public: |
979 | 979 |
|
980 | 980 |
/// Constructor. |
981 | 981 |
BfsWizard() : TR() {} |
982 | 982 |
|
983 | 983 |
/// Constructor that requires parameters. |
984 | 984 |
|
985 | 985 |
/// Constructor that requires parameters. |
986 | 986 |
/// These parameters will be the default values for the traits class. |
987 | 987 |
/// \param g The digraph the algorithm runs on. |
988 | 988 |
BfsWizard(const Digraph &g) : |
989 | 989 |
TR(g) {} |
990 | 990 |
|
991 | 991 |
///Copy constructor |
992 | 992 |
BfsWizard(const TR &b) : TR(b) {} |
993 | 993 |
|
994 | 994 |
~BfsWizard() {} |
995 | 995 |
|
996 | 996 |
///Runs BFS algorithm from the given source node. |
997 | 997 |
|
998 | 998 |
///This method runs BFS algorithm from node \c s |
999 | 999 |
///in order to compute the shortest path to each node. |
1000 | 1000 |
void run(Node s) |
1001 | 1001 |
{ |
1002 | 1002 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1003 | 1003 |
if (Base::_pred) |
1004 | 1004 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1005 | 1005 |
if (Base::_dist) |
1006 | 1006 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1007 | 1007 |
if (Base::_reached) |
1008 | 1008 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1009 | 1009 |
if (Base::_processed) |
1010 | 1010 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1011 | 1011 |
if (s!=INVALID) |
1012 | 1012 |
alg.run(s); |
1013 | 1013 |
else |
1014 | 1014 |
alg.run(); |
1015 | 1015 |
} |
1016 | 1016 |
|
1017 | 1017 |
///Finds the shortest path between \c s and \c t. |
1018 | 1018 |
|
1019 | 1019 |
///This method runs BFS algorithm from node \c s |
1020 | 1020 |
///in order to compute the shortest path to node \c t |
1021 | 1021 |
///(it stops searching when \c t is processed). |
1022 | 1022 |
/// |
1023 | 1023 |
///\return \c true if \c t is reachable form \c s. |
1024 | 1024 |
bool run(Node s, Node t) |
1025 | 1025 |
{ |
1026 | 1026 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1027 | 1027 |
if (Base::_pred) |
1028 | 1028 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1029 | 1029 |
if (Base::_dist) |
1030 | 1030 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1031 | 1031 |
if (Base::_reached) |
1032 | 1032 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1033 | 1033 |
if (Base::_processed) |
1034 | 1034 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1035 | 1035 |
alg.run(s,t); |
1036 | 1036 |
if (Base::_path) |
1037 | 1037 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
1038 | 1038 |
if (Base::_di) |
1039 | 1039 |
*Base::_di = alg.dist(t); |
1040 | 1040 |
return alg.reached(t); |
1041 | 1041 |
} |
1042 | 1042 |
|
1043 | 1043 |
///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 |
/// |
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 concept |
20 | 20 |
///\file |
21 |
///\brief |
|
21 |
///\brief The concept of paths |
|
22 | 22 |
/// |
23 | 23 |
|
24 | 24 |
#ifndef LEMON_CONCEPTS_PATH_H |
25 | 25 |
#define LEMON_CONCEPTS_PATH_H |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// \brief A skeleton structure for representing directed paths in |
37 | 37 |
/// a digraph. |
38 | 38 |
/// |
39 | 39 |
/// A skeleton structure for representing directed paths in a |
40 | 40 |
/// digraph. |
41 |
/// In a sense, a path can be treated as a list of arcs. |
|
42 |
/// LEMON path types just store this list. As a consequence, they cannot |
|
43 |
/// enumerate the nodes on the path directly and a zero length path |
|
44 |
/// cannot store its source node. |
|
45 |
/// |
|
46 |
/// The arcs of a path should be stored in the order of their directions, |
|
47 |
/// i.e. the target node of each arc should be the same as the source |
|
48 |
/// node of the next arc. This consistency could be checked using |
|
49 |
/// \ref checkPath(). |
|
50 |
/// The source and target nodes of a (consistent) path can be obtained |
|
51 |
/// using \ref pathSource() and \ref pathTarget(). |
|
52 |
/// |
|
53 |
/// A path can be constructed from another path of any type using the |
|
54 |
/// copy constructor or the assignment operator. |
|
55 |
/// |
|
41 | 56 |
/// \tparam GR The digraph type in which the path is. |
42 |
/// |
|
43 |
/// In a sense, the path can be treated as a list of arcs. The |
|
44 |
/// lemon path type stores just this list. As a consequence it |
|
45 |
/// cannot enumerate the nodes in the path and the zero length |
|
46 |
/// paths cannot store the source. |
|
47 |
/// |
|
48 | 57 |
template <typename GR> |
49 | 58 |
class Path { |
50 | 59 |
public: |
51 | 60 |
|
52 | 61 |
/// Type of the underlying digraph. |
53 | 62 |
typedef GR Digraph; |
54 | 63 |
/// Arc type of the underlying digraph. |
55 | 64 |
typedef typename Digraph::Arc Arc; |
56 | 65 |
|
57 | 66 |
class ArcIt; |
58 | 67 |
|
59 | 68 |
/// \brief Default constructor |
60 | 69 |
Path() {} |
61 | 70 |
|
62 |
/// \brief Template constructor |
|
71 |
/// \brief Template copy constructor |
|
63 | 72 |
template <typename CPath> |
64 | 73 |
Path(const CPath& cpath) {} |
65 | 74 |
|
66 |
/// \brief Template assigment |
|
75 |
/// \brief Template assigment operator |
|
67 | 76 |
template <typename CPath> |
68 | 77 |
Path& operator=(const CPath& cpath) { |
69 | 78 |
ignore_unused_variable_warning(cpath); |
70 | 79 |
return *this; |
71 | 80 |
} |
72 | 81 |
|
73 |
/// Length of the path |
|
82 |
/// Length of the path, i.e. the number of arcs on the path. |
|
74 | 83 |
int length() const { return 0;} |
75 | 84 |
|
76 | 85 |
/// Returns whether the path is empty. |
77 | 86 |
bool empty() const { return true;} |
78 | 87 |
|
79 | 88 |
/// Resets the path to an empty path. |
80 | 89 |
void clear() {} |
81 | 90 |
|
82 |
/// \brief LEMON style iterator for |
|
91 |
/// \brief LEMON style iterator for enumerating the arcs of a path. |
|
83 | 92 |
/// |
84 |
/// |
|
93 |
/// LEMON style iterator class for enumerating the arcs of a path. |
|
85 | 94 |
class ArcIt { |
86 | 95 |
public: |
87 | 96 |
/// Default constructor |
88 | 97 |
ArcIt() {} |
89 | 98 |
/// Invalid constructor |
90 | 99 |
ArcIt(Invalid) {} |
91 |
/// |
|
100 |
/// Sets the iterator to the first arc of the given path |
|
92 | 101 |
ArcIt(const Path &) {} |
93 | 102 |
|
94 |
/// Conversion to Arc |
|
103 |
/// Conversion to \c Arc |
|
95 | 104 |
operator Arc() const { return INVALID; } |
96 | 105 |
|
97 | 106 |
/// Next arc |
98 | 107 |
ArcIt& operator++() {return *this;} |
99 | 108 |
|
100 | 109 |
/// Comparison operator |
101 | 110 |
bool operator==(const ArcIt&) const {return true;} |
102 | 111 |
/// Comparison operator |
103 | 112 |
bool operator!=(const ArcIt&) const {return true;} |
104 | 113 |
/// Comparison operator |
105 | 114 |
bool operator<(const ArcIt&) const {return false;} |
106 | 115 |
|
107 | 116 |
}; |
108 | 117 |
|
109 | 118 |
template <typename _Path> |
110 | 119 |
struct Constraints { |
111 | 120 |
void constraints() { |
112 | 121 |
Path<Digraph> pc; |
113 | 122 |
_Path p, pp(pc); |
114 | 123 |
int l = p.length(); |
115 | 124 |
int e = p.empty(); |
116 | 125 |
p.clear(); |
117 | 126 |
|
118 | 127 |
p = pc; |
119 | 128 |
|
120 | 129 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
121 | 130 |
|
122 | 131 |
++i; |
123 | 132 |
typename Digraph::Arc ed = i; |
124 | 133 |
|
125 | 134 |
e = (i == ii); |
126 | 135 |
e = (i != ii); |
127 | 136 |
e = (i < ii); |
128 | 137 |
|
129 | 138 |
ignore_unused_variable_warning(l); |
130 | 139 |
ignore_unused_variable_warning(pp); |
131 | 140 |
ignore_unused_variable_warning(e); |
132 | 141 |
ignore_unused_variable_warning(id); |
133 | 142 |
ignore_unused_variable_warning(ii); |
134 | 143 |
ignore_unused_variable_warning(ed); |
135 | 144 |
} |
136 | 145 |
}; |
137 | 146 |
|
138 | 147 |
}; |
139 | 148 |
|
140 | 149 |
namespace _path_bits { |
141 | 150 |
|
142 | 151 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
143 | 152 |
struct PathDumperConstraints { |
144 | 153 |
void constraints() { |
145 | 154 |
int l = p.length(); |
146 | 155 |
int e = p.empty(); |
147 | 156 |
|
148 | 157 |
typename _Path::ArcIt id, i(p); |
149 | 158 |
|
150 | 159 |
++i; |
151 | 160 |
typename _Digraph::Arc ed = i; |
152 | 161 |
|
153 | 162 |
e = (i == INVALID); |
154 | 163 |
e = (i != INVALID); |
155 | 164 |
|
156 | 165 |
ignore_unused_variable_warning(l); |
157 | 166 |
ignore_unused_variable_warning(e); |
158 | 167 |
ignore_unused_variable_warning(id); |
159 | 168 |
ignore_unused_variable_warning(ed); |
160 | 169 |
} |
161 | 170 |
_Path& p; |
162 | 171 |
}; |
163 | 172 |
|
164 | 173 |
template <typename _Digraph, typename _Path> |
165 | 174 |
struct PathDumperConstraints< |
166 | 175 |
_Digraph, _Path, |
167 | 176 |
typename enable_if<typename _Path::RevPathTag, void>::type |
168 | 177 |
> { |
169 | 178 |
void constraints() { |
170 | 179 |
int l = p.length(); |
171 | 180 |
int e = p.empty(); |
172 | 181 |
|
173 | 182 |
typename _Path::RevArcIt id, i(p); |
174 | 183 |
|
175 | 184 |
++i; |
176 | 185 |
typename _Digraph::Arc ed = i; |
177 | 186 |
|
178 | 187 |
e = (i == INVALID); |
179 | 188 |
e = (i != INVALID); |
180 | 189 |
|
181 | 190 |
ignore_unused_variable_warning(l); |
182 | 191 |
ignore_unused_variable_warning(e); |
183 | 192 |
ignore_unused_variable_warning(id); |
184 | 193 |
ignore_unused_variable_warning(ed); |
185 | 194 |
} |
186 | 195 |
_Path& p; |
187 | 196 |
}; |
188 | 197 |
|
189 | 198 |
} |
190 | 199 |
|
191 | 200 |
|
192 | 201 |
/// \brief A skeleton structure for path dumpers. |
193 | 202 |
/// |
194 | 203 |
/// A skeleton structure for path dumpers. The path dumpers are |
195 |
/// the generalization of the paths. The path dumpers can |
|
196 |
/// enumerate the arcs of the path wheter in forward or in |
|
197 |
/// backward order. In most time these classes are not used |
|
198 |
/// directly rather it used to assign a dumped class to a real |
|
199 |
/// |
|
204 |
/// the generalization of the paths, they can enumerate the arcs |
|
205 |
/// of the path either in forward or in backward order. |
|
206 |
/// These classes are typically not used directly, they are rather |
|
207 |
/// used to be assigned to a real path type. |
|
200 | 208 |
/// |
201 | 209 |
/// The main purpose of this concept is that the shortest path |
202 |
/// algorithms can enumerate easily the arcs in reverse order. |
|
203 |
/// If we would like to give back a real path from these |
|
204 |
/// algorithms then we should create a temporarly path object. In |
|
205 |
/// LEMON such algorithms gives back a path dumper what can |
|
206 |
/// |
|
210 |
/// algorithms can enumerate the arcs easily in reverse order. |
|
211 |
/// In LEMON, such algorithms give back a (reverse) path dumper that |
|
212 |
/// can be assigned to a real path. The dumpers can be implemented as |
|
207 | 213 |
/// an adaptor class to the predecessor map. |
208 | 214 |
/// |
209 | 215 |
/// \tparam GR The digraph type in which the path is. |
210 |
/// |
|
211 |
/// The paths can be constructed from any path type by a |
|
212 |
/// template constructor or a template assignment operator. |
|
213 | 216 |
template <typename GR> |
214 | 217 |
class PathDumper { |
215 | 218 |
public: |
216 | 219 |
|
217 | 220 |
/// Type of the underlying digraph. |
218 | 221 |
typedef GR Digraph; |
219 | 222 |
/// Arc type of the underlying digraph. |
220 | 223 |
typedef typename Digraph::Arc Arc; |
221 | 224 |
|
222 |
/// Length of the path |
|
225 |
/// Length of the path, i.e. the number of arcs on the path. |
|
223 | 226 |
int length() const { return 0;} |
224 | 227 |
|
225 | 228 |
/// Returns whether the path is empty. |
226 | 229 |
bool empty() const { return true;} |
227 | 230 |
|
228 | 231 |
/// \brief Forward or reverse dumping |
229 | 232 |
/// |
230 |
/// If the RevPathTag is defined and true then reverse dumping |
|
231 |
/// is provided in the path dumper. In this case instead of the |
|
232 |
/// ArcIt the RevArcIt iterator should be implemented in the |
|
233 |
/// dumper. |
|
233 |
/// If this tag is defined to be \c True, then reverse dumping |
|
234 |
/// is provided in the path dumper. In this case, \c RevArcIt |
|
235 |
/// iterator should be implemented instead of \c ArcIt iterator. |
|
234 | 236 |
typedef False RevPathTag; |
235 | 237 |
|
236 |
/// \brief LEMON style iterator for |
|
238 |
/// \brief LEMON style iterator for enumerating the arcs of a path. |
|
237 | 239 |
/// |
238 |
/// |
|
240 |
/// LEMON style iterator class for enumerating the arcs of a path. |
|
239 | 241 |
class ArcIt { |
240 | 242 |
public: |
241 | 243 |
/// Default constructor |
242 | 244 |
ArcIt() {} |
243 | 245 |
/// Invalid constructor |
244 | 246 |
ArcIt(Invalid) {} |
245 |
/// |
|
247 |
/// Sets the iterator to the first arc of the given path |
|
246 | 248 |
ArcIt(const PathDumper&) {} |
247 | 249 |
|
248 |
/// Conversion to Arc |
|
250 |
/// Conversion to \c Arc |
|
249 | 251 |
operator Arc() const { return INVALID; } |
250 | 252 |
|
251 | 253 |
/// Next arc |
252 | 254 |
ArcIt& operator++() {return *this;} |
253 | 255 |
|
254 | 256 |
/// Comparison operator |
255 | 257 |
bool operator==(const ArcIt&) const {return true;} |
256 | 258 |
/// Comparison operator |
257 | 259 |
bool operator!=(const ArcIt&) const {return true;} |
258 | 260 |
/// Comparison operator |
259 | 261 |
bool operator<(const ArcIt&) const {return false;} |
260 | 262 |
|
261 | 263 |
}; |
262 | 264 |
|
263 |
/// \brief LEMON style iterator for |
|
265 |
/// \brief LEMON style iterator for enumerating the arcs of a path |
|
266 |
/// in reverse direction. |
|
264 | 267 |
/// |
265 |
/// This class is used to iterate on the arcs of the paths in |
|
266 |
/// reverse direction. |
|
268 |
/// LEMON style iterator class for enumerating the arcs of a path |
|
269 |
/// in reverse direction. |
|
267 | 270 |
class RevArcIt { |
268 | 271 |
public: |
269 | 272 |
/// Default constructor |
270 | 273 |
RevArcIt() {} |
271 | 274 |
/// Invalid constructor |
272 | 275 |
RevArcIt(Invalid) {} |
273 |
/// |
|
276 |
/// Sets the iterator to the last arc of the given path |
|
274 | 277 |
RevArcIt(const PathDumper &) {} |
275 | 278 |
|
276 |
/// Conversion to Arc |
|
279 |
/// Conversion to \c Arc |
|
277 | 280 |
operator Arc() const { return INVALID; } |
278 | 281 |
|
279 | 282 |
/// Next arc |
280 | 283 |
RevArcIt& operator++() {return *this;} |
281 | 284 |
|
282 | 285 |
/// Comparison operator |
283 | 286 |
bool operator==(const RevArcIt&) const {return true;} |
284 | 287 |
/// Comparison operator |
285 | 288 |
bool operator!=(const RevArcIt&) const {return true;} |
286 | 289 |
/// Comparison operator |
287 | 290 |
bool operator<(const RevArcIt&) const {return false;} |
288 | 291 |
|
289 | 292 |
}; |
290 | 293 |
|
291 | 294 |
template <typename _Path> |
292 | 295 |
struct Constraints { |
293 | 296 |
void constraints() { |
294 | 297 |
function_requires<_path_bits:: |
295 | 298 |
PathDumperConstraints<Digraph, _Path> >(); |
296 | 299 |
} |
297 | 300 |
}; |
298 | 301 |
|
299 | 302 |
}; |
300 | 303 |
|
301 | 304 |
|
302 | 305 |
///@} |
303 | 306 |
} |
304 | 307 |
|
305 | 308 |
} // namespace lemon |
306 | 309 |
|
307 | 310 |
#endif |
... | ... |
@@ -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 |
{ |
... | ... |
@@ -589,385 +589,385 @@ |
589 | 589 |
} |
590 | 590 |
|
591 | 591 |
///Runs the algorithm from the given source node. |
592 | 592 |
|
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 visit all nodes |
637 | 637 |
///in the digraph. |
638 | 638 |
/// |
639 | 639 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
640 | 640 |
///\code |
641 | 641 |
/// d.init(); |
642 | 642 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
643 | 643 |
/// if (!d.reached(n)) { |
644 | 644 |
/// d.addSource(n); |
645 | 645 |
/// d.start(); |
646 | 646 |
/// } |
647 | 647 |
/// } |
648 | 648 |
///\endcode |
649 | 649 |
void run() { |
650 | 650 |
init(); |
651 | 651 |
for (NodeIt it(*G); it != INVALID; ++it) { |
652 | 652 |
if (!reached(it)) { |
653 | 653 |
addSource(it); |
654 | 654 |
start(); |
655 | 655 |
} |
656 | 656 |
} |
657 | 657 |
} |
658 | 658 |
|
659 | 659 |
///@} |
660 | 660 |
|
661 | 661 |
///\name Query Functions |
662 | 662 |
///The results of the DFS algorithm can be obtained using these |
663 | 663 |
///functions.\n |
664 | 664 |
///Either \ref run(Node) "run()" or \ref start() should be called |
665 | 665 |
///before using them. |
666 | 666 |
|
667 | 667 |
///@{ |
668 | 668 |
|
669 | 669 |
///The DFS path to the given node. |
670 | 670 |
|
671 | 671 |
///Returns the DFS path to the given node from the root(s). |
672 | 672 |
/// |
673 | 673 |
///\warning \c t should be reached from the root(s). |
674 | 674 |
/// |
675 | 675 |
///\pre Either \ref run(Node) "run()" or \ref init() |
676 | 676 |
///must be called before using this function. |
677 | 677 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
678 | 678 |
|
679 | 679 |
///The distance of the given node from the root(s). |
680 | 680 |
|
681 | 681 |
///Returns the distance of the given node from the root(s). |
682 | 682 |
/// |
683 | 683 |
///\warning If node \c v is not reached from the root(s), then |
684 | 684 |
///the return value of this function is undefined. |
685 | 685 |
/// |
686 | 686 |
///\pre Either \ref run(Node) "run()" or \ref init() |
687 | 687 |
///must be called before using this function. |
688 | 688 |
int dist(Node v) const { return (*_dist)[v]; } |
689 | 689 |
|
690 | 690 |
///Returns the 'previous arc' of the %DFS tree for the given node. |
691 | 691 |
|
692 | 692 |
///This function returns the 'previous arc' of the %DFS tree for the |
693 | 693 |
///node \c v, i.e. it returns the last arc of a %DFS path from a |
694 | 694 |
///root to \c v. It is \c INVALID if \c v is not reached from the |
695 | 695 |
///root(s) or if \c v is a root. |
696 | 696 |
/// |
697 | 697 |
///The %DFS tree used here is equal to the %DFS tree used in |
698 | 698 |
///\ref predNode() and \ref predMap(). |
699 | 699 |
/// |
700 | 700 |
///\pre Either \ref run(Node) "run()" or \ref init() |
701 | 701 |
///must be called before using this function. |
702 | 702 |
Arc predArc(Node v) const { return (*_pred)[v];} |
703 | 703 |
|
704 | 704 |
///Returns the 'previous node' of the %DFS tree for the given node. |
705 | 705 |
|
706 | 706 |
///This function returns the 'previous node' of the %DFS |
707 | 707 |
///tree for the node \c v, i.e. it returns the last but one node |
708 | 708 |
///of a %DFS path from a root to \c v. It is \c INVALID |
709 | 709 |
///if \c v is not reached from the root(s) or if \c v is a root. |
710 | 710 |
/// |
711 | 711 |
///The %DFS tree used here is equal to the %DFS tree used in |
712 | 712 |
///\ref predArc() and \ref predMap(). |
713 | 713 |
/// |
714 | 714 |
///\pre Either \ref run(Node) "run()" or \ref init() |
715 | 715 |
///must be called before using this function. |
716 | 716 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
717 | 717 |
G->source((*_pred)[v]); } |
718 | 718 |
|
719 | 719 |
///\brief Returns a const reference to the node map that stores the |
720 | 720 |
///distances of the nodes. |
721 | 721 |
/// |
722 | 722 |
///Returns a const reference to the node map that stores the |
723 | 723 |
///distances of the nodes calculated by the algorithm. |
724 | 724 |
/// |
725 | 725 |
///\pre Either \ref run(Node) "run()" or \ref init() |
726 | 726 |
///must be called before using this function. |
727 | 727 |
const DistMap &distMap() const { return *_dist;} |
728 | 728 |
|
729 | 729 |
///\brief Returns a const reference to the node map that stores the |
730 | 730 |
///predecessor arcs. |
731 | 731 |
/// |
732 | 732 |
///Returns a const reference to the node map that stores the predecessor |
733 | 733 |
///arcs, which form the DFS tree (forest). |
734 | 734 |
/// |
735 | 735 |
///\pre Either \ref run(Node) "run()" or \ref init() |
736 | 736 |
///must be called before using this function. |
737 | 737 |
const PredMap &predMap() const { return *_pred;} |
738 | 738 |
|
739 | 739 |
///Checks if the given node. node is reached from the root(s). |
740 | 740 |
|
741 | 741 |
///Returns \c true if \c v is reached from the root(s). |
742 | 742 |
/// |
743 | 743 |
///\pre Either \ref run(Node) "run()" or \ref init() |
744 | 744 |
///must be called before using this function. |
745 | 745 |
bool reached(Node v) const { return (*_reached)[v]; } |
746 | 746 |
|
747 | 747 |
///@} |
748 | 748 |
}; |
749 | 749 |
|
750 | 750 |
///Default traits class of dfs() function. |
751 | 751 |
|
752 | 752 |
///Default traits class of dfs() function. |
753 | 753 |
///\tparam GR Digraph type. |
754 | 754 |
template<class GR> |
755 | 755 |
struct DfsWizardDefaultTraits |
756 | 756 |
{ |
757 | 757 |
///The type of the digraph the algorithm runs on. |
758 | 758 |
typedef GR Digraph; |
759 | 759 |
|
760 | 760 |
///\brief The type of the map that stores the predecessor |
761 | 761 |
///arcs of the %DFS paths. |
762 | 762 |
/// |
763 | 763 |
///The type of the map that stores the predecessor |
764 | 764 |
///arcs of the %DFS paths. |
765 | 765 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
766 | 766 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
767 | 767 |
///Instantiates a PredMap. |
768 | 768 |
|
769 | 769 |
///This function instantiates a PredMap. |
770 | 770 |
///\param g is the digraph, to which we would like to define the |
771 | 771 |
///PredMap. |
772 | 772 |
static PredMap *createPredMap(const Digraph &g) |
773 | 773 |
{ |
774 | 774 |
return new PredMap(g); |
775 | 775 |
} |
776 | 776 |
|
777 | 777 |
///The type of the map that indicates which nodes are processed. |
778 | 778 |
|
779 | 779 |
///The type of the map that indicates which nodes are processed. |
780 | 780 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
781 |
///By default it is a NullMap. |
|
781 |
///By default, it is a NullMap. |
|
782 | 782 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
783 | 783 |
///Instantiates a ProcessedMap. |
784 | 784 |
|
785 | 785 |
///This function instantiates a ProcessedMap. |
786 | 786 |
///\param g is the digraph, to which |
787 | 787 |
///we would like to define the ProcessedMap. |
788 | 788 |
#ifdef DOXYGEN |
789 | 789 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
790 | 790 |
#else |
791 | 791 |
static ProcessedMap *createProcessedMap(const Digraph &) |
792 | 792 |
#endif |
793 | 793 |
{ |
794 | 794 |
return new ProcessedMap(); |
795 | 795 |
} |
796 | 796 |
|
797 | 797 |
///The type of the map that indicates which nodes are reached. |
798 | 798 |
|
799 | 799 |
///The type of the map that indicates which nodes are reached. |
800 | 800 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
801 | 801 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
802 | 802 |
///Instantiates a ReachedMap. |
803 | 803 |
|
804 | 804 |
///This function instantiates a ReachedMap. |
805 | 805 |
///\param g is the digraph, to which |
806 | 806 |
///we would like to define the ReachedMap. |
807 | 807 |
static ReachedMap *createReachedMap(const Digraph &g) |
808 | 808 |
{ |
809 | 809 |
return new ReachedMap(g); |
810 | 810 |
} |
811 | 811 |
|
812 | 812 |
///The type of the map that stores the distances of the nodes. |
813 | 813 |
|
814 | 814 |
///The type of the map that stores the distances of the nodes. |
815 | 815 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
816 | 816 |
typedef typename Digraph::template NodeMap<int> DistMap; |
817 | 817 |
///Instantiates a DistMap. |
818 | 818 |
|
819 | 819 |
///This function instantiates a DistMap. |
820 | 820 |
///\param g is the digraph, to which we would like to define |
821 | 821 |
///the DistMap |
822 | 822 |
static DistMap *createDistMap(const Digraph &g) |
823 | 823 |
{ |
824 | 824 |
return new DistMap(g); |
825 | 825 |
} |
826 | 826 |
|
827 | 827 |
///The type of the DFS paths. |
828 | 828 |
|
829 | 829 |
///The type of the DFS paths. |
830 | 830 |
///It must conform to the \ref concepts::Path "Path" concept. |
831 | 831 |
typedef lemon::Path<Digraph> Path; |
832 | 832 |
}; |
833 | 833 |
|
834 | 834 |
/// Default traits class used by DfsWizard |
835 | 835 |
|
836 | 836 |
/// Default traits class used by DfsWizard. |
837 | 837 |
/// \tparam GR The type of the digraph. |
838 | 838 |
template<class GR> |
839 | 839 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
840 | 840 |
{ |
841 | 841 |
|
842 | 842 |
typedef DfsWizardDefaultTraits<GR> Base; |
843 | 843 |
protected: |
844 | 844 |
//The type of the nodes in the digraph. |
845 | 845 |
typedef typename Base::Digraph::Node Node; |
846 | 846 |
|
847 | 847 |
//Pointer to the digraph the algorithm runs on. |
848 | 848 |
void *_g; |
849 | 849 |
//Pointer to the map of reached nodes. |
850 | 850 |
void *_reached; |
851 | 851 |
//Pointer to the map of processed nodes. |
852 | 852 |
void *_processed; |
853 | 853 |
//Pointer to the map of predecessors arcs. |
854 | 854 |
void *_pred; |
855 | 855 |
//Pointer to the map of distances. |
856 | 856 |
void *_dist; |
857 | 857 |
//Pointer to the DFS path to the target node. |
858 | 858 |
void *_path; |
859 | 859 |
//Pointer to the distance of the target node. |
860 | 860 |
int *_di; |
861 | 861 |
|
862 | 862 |
public: |
863 | 863 |
/// Constructor. |
864 | 864 |
|
865 | 865 |
/// This constructor does not require parameters, it initiates |
866 | 866 |
/// all of the attributes to \c 0. |
867 | 867 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
868 | 868 |
_dist(0), _path(0), _di(0) {} |
869 | 869 |
|
870 | 870 |
/// Constructor. |
871 | 871 |
|
872 | 872 |
/// This constructor requires one parameter, |
873 | 873 |
/// others are initiated to \c 0. |
874 | 874 |
/// \param g The digraph the algorithm runs on. |
875 | 875 |
DfsWizardBase(const GR &g) : |
876 | 876 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
877 | 877 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
878 | 878 |
|
879 | 879 |
}; |
880 | 880 |
|
881 | 881 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
882 | 882 |
|
883 | 883 |
/// This auxiliary class is created to implement the |
884 | 884 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
885 | 885 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
886 | 886 |
/// functions and features of the plain \ref Dfs. |
887 | 887 |
/// |
888 | 888 |
/// This class should only be used through the \ref dfs() function, |
889 | 889 |
/// which makes it easier to use the algorithm. |
890 | 890 |
template<class TR> |
891 | 891 |
class DfsWizard : public TR |
892 | 892 |
{ |
893 | 893 |
typedef TR Base; |
894 | 894 |
|
895 | 895 |
typedef typename TR::Digraph Digraph; |
896 | 896 |
|
897 | 897 |
typedef typename Digraph::Node Node; |
898 | 898 |
typedef typename Digraph::NodeIt NodeIt; |
899 | 899 |
typedef typename Digraph::Arc Arc; |
900 | 900 |
typedef typename Digraph::OutArcIt OutArcIt; |
901 | 901 |
|
902 | 902 |
typedef typename TR::PredMap PredMap; |
903 | 903 |
typedef typename TR::DistMap DistMap; |
904 | 904 |
typedef typename TR::ReachedMap ReachedMap; |
905 | 905 |
typedef typename TR::ProcessedMap ProcessedMap; |
906 | 906 |
typedef typename TR::Path Path; |
907 | 907 |
|
908 | 908 |
public: |
909 | 909 |
|
910 | 910 |
/// Constructor. |
911 | 911 |
DfsWizard() : TR() {} |
912 | 912 |
|
913 | 913 |
/// Constructor that requires parameters. |
914 | 914 |
|
915 | 915 |
/// Constructor that requires parameters. |
916 | 916 |
/// These parameters will be the default values for the traits class. |
917 | 917 |
/// \param g The digraph the algorithm runs on. |
918 | 918 |
DfsWizard(const Digraph &g) : |
919 | 919 |
TR(g) {} |
920 | 920 |
|
921 | 921 |
///Copy constructor |
922 | 922 |
DfsWizard(const TR &b) : TR(b) {} |
923 | 923 |
|
924 | 924 |
~DfsWizard() {} |
925 | 925 |
|
926 | 926 |
///Runs DFS algorithm from the given source node. |
927 | 927 |
|
928 | 928 |
///This method runs DFS algorithm from node \c s |
929 | 929 |
///in order to compute the DFS path to each node. |
930 | 930 |
void run(Node s) |
931 | 931 |
{ |
932 | 932 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
933 | 933 |
if (Base::_pred) |
934 | 934 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
935 | 935 |
if (Base::_dist) |
936 | 936 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
937 | 937 |
if (Base::_reached) |
938 | 938 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
939 | 939 |
if (Base::_processed) |
940 | 940 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
941 | 941 |
if (s!=INVALID) |
942 | 942 |
alg.run(s); |
943 | 943 |
else |
944 | 944 |
alg.run(); |
945 | 945 |
} |
946 | 946 |
|
947 | 947 |
///Finds the DFS path between \c s and \c t. |
948 | 948 |
|
949 | 949 |
///This method runs DFS algorithm from node \c s |
950 | 950 |
///in order to compute the DFS path to node \c t |
951 | 951 |
///(it stops searching when \c t is processed). |
952 | 952 |
/// |
953 | 953 |
///\return \c true if \c t is reachable form \c s. |
954 | 954 |
bool run(Node s, Node t) |
955 | 955 |
{ |
956 | 956 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
957 | 957 |
if (Base::_pred) |
958 | 958 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
959 | 959 |
if (Base::_dist) |
960 | 960 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
961 | 961 |
if (Base::_reached) |
962 | 962 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
963 | 963 |
if (Base::_processed) |
964 | 964 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
965 | 965 |
alg.run(s,t); |
966 | 966 |
if (Base::_path) |
967 | 967 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
968 | 968 |
if (Base::_di) |
969 | 969 |
*Base::_di = alg.dist(t); |
970 | 970 |
return alg.reached(t); |
971 | 971 |
} |
972 | 972 |
|
973 | 973 |
///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::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,362 +98,362 @@ |
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 |
static int index(Node node) { |
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 |
/// This class provides constant time counting for nodes, edges and arcs. |
298 | 298 |
/// |
299 | 299 |
/// \note The type of the indices is chosen to \c int for efficiency |
300 | 300 |
/// reasons. Thus the maximum dimension of this implementation is 26 |
301 | 301 |
/// (assuming that the size of \c int is 32 bit). |
302 | 302 |
class HypercubeGraph : public ExtendedHypercubeGraphBase { |
303 | 303 |
typedef ExtendedHypercubeGraphBase Parent; |
304 | 304 |
|
305 | 305 |
public: |
306 | 306 |
|
307 | 307 |
/// \brief Constructs a hypercube graph with \c dim dimensions. |
308 | 308 |
/// |
309 | 309 |
/// Constructs a hypercube graph with \c dim dimensions. |
310 | 310 |
HypercubeGraph(int dim) { construct(dim); } |
311 | 311 |
|
312 | 312 |
/// \brief Resizes the graph |
313 | 313 |
/// |
314 | 314 |
/// This function resizes the graph. It fully destroys and |
315 | 315 |
/// rebuilds the structure, therefore the maps of the graph will be |
316 | 316 |
/// reallocated automatically and the previous values will be lost. |
317 | 317 |
void resize(int dim) { |
318 | 318 |
Parent::notifier(Arc()).clear(); |
319 | 319 |
Parent::notifier(Edge()).clear(); |
320 | 320 |
Parent::notifier(Node()).clear(); |
321 | 321 |
construct(dim); |
322 | 322 |
Parent::notifier(Node()).build(); |
323 | 323 |
Parent::notifier(Edge()).build(); |
324 | 324 |
Parent::notifier(Arc()).build(); |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
/// \brief The number of dimensions. |
328 | 328 |
/// |
329 | 329 |
/// Gives back the number of dimensions. |
330 | 330 |
int dimension() const { |
331 | 331 |
return Parent::dimension(); |
332 | 332 |
} |
333 | 333 |
|
334 | 334 |
/// \brief Returns \c true if the n'th bit of the node is one. |
335 | 335 |
/// |
336 | 336 |
/// Returns \c true if the n'th bit of the node is one. |
337 | 337 |
bool projection(Node node, int n) const { |
338 | 338 |
return Parent::projection(node, n); |
339 | 339 |
} |
340 | 340 |
|
341 | 341 |
/// \brief The dimension id of an edge. |
342 | 342 |
/// |
343 | 343 |
/// Gives back the dimension id of the given edge. |
344 | 344 |
/// It is in the range <tt>[0..dim-1]</tt>. |
345 | 345 |
int dimension(Edge edge) const { |
346 | 346 |
return Parent::dimension(edge); |
347 | 347 |
} |
348 | 348 |
|
349 | 349 |
/// \brief The dimension id of an arc. |
350 | 350 |
/// |
351 | 351 |
/// Gives back the dimension id of the given arc. |
352 | 352 |
/// It is in the range <tt>[0..dim-1]</tt>. |
353 | 353 |
int dimension(Arc arc) const { |
354 | 354 |
return Parent::dimension(arc); |
355 | 355 |
} |
356 | 356 |
|
357 | 357 |
/// \brief The index of a node. |
358 | 358 |
/// |
359 | 359 |
/// Gives back the index of the given node. |
360 | 360 |
/// The lower bits of the integer describes the node. |
361 | 361 |
static int index(Node node) { |
362 | 362 |
return Parent::index(node); |
363 | 363 |
} |
364 | 364 |
|
365 | 365 |
/// \brief Gives back a node by its index. |
366 | 366 |
/// |
367 | 367 |
/// Gives back a node by its index. |
368 | 368 |
Node operator()(int ix) const { |
369 | 369 |
return Parent::operator()(ix); |
370 | 370 |
} |
371 | 371 |
|
372 | 372 |
/// \brief Number of nodes. |
373 | 373 |
int nodeNum() const { return Parent::nodeNum(); } |
374 | 374 |
/// \brief Number of edges. |
375 | 375 |
int edgeNum() const { return Parent::edgeNum(); } |
376 | 376 |
/// \brief Number of arcs. |
377 | 377 |
int arcNum() const { return Parent::arcNum(); } |
378 | 378 |
|
379 | 379 |
/// \brief Linear combination map. |
380 | 380 |
/// |
381 | 381 |
/// This map makes possible to give back a linear combination |
382 | 382 |
/// for each node. It works like the \c std::accumulate function, |
383 | 383 |
/// so it accumulates the \c bf binary function with the \c fv first |
384 | 384 |
/// value. The map accumulates only on that positions (dimensions) |
385 | 385 |
/// where the index of the node is one. The values that have to be |
386 | 386 |
/// accumulated should be given by the \c begin and \c end iterators |
387 | 387 |
/// and the length of this range should be equal to the dimension |
388 | 388 |
/// number of the graph. |
389 | 389 |
/// |
390 | 390 |
///\code |
391 | 391 |
/// const int DIM = 3; |
392 | 392 |
/// HypercubeGraph graph(DIM); |
393 | 393 |
/// dim2::Point<double> base[DIM]; |
394 | 394 |
/// for (int k = 0; k < DIM; ++k) { |
395 | 395 |
/// base[k].x = rnd(); |
396 | 396 |
/// base[k].y = rnd(); |
397 | 397 |
/// } |
398 | 398 |
/// HypercubeGraph::HyperMap<dim2::Point<double> > |
399 | 399 |
/// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0)); |
400 | 400 |
///\endcode |
401 | 401 |
/// |
402 | 402 |
/// \see HypercubeGraph |
403 | 403 |
template <typename T, typename BF = std::plus<T> > |
404 | 404 |
class HyperMap { |
405 | 405 |
public: |
406 | 406 |
|
407 | 407 |
/// \brief The key type of the map |
408 | 408 |
typedef Node Key; |
409 | 409 |
/// \brief The value type of the map |
410 | 410 |
typedef T Value; |
411 | 411 |
|
412 | 412 |
/// \brief Constructor for HyperMap. |
413 | 413 |
/// |
414 | 414 |
/// Construct a HyperMap for the given graph. The values that have |
415 | 415 |
/// to be accumulated should be given by the \c begin and \c end |
416 | 416 |
/// iterators and the length of this range should be equal to the |
417 | 417 |
/// dimension number of the graph. |
418 | 418 |
/// |
419 | 419 |
/// This map accumulates the \c bf binary function with the \c fv |
420 | 420 |
/// first value on that positions (dimensions) where the index of |
421 | 421 |
/// the node is one. |
422 | 422 |
template <typename It> |
423 | 423 |
HyperMap(const Graph& graph, It begin, It end, |
424 | 424 |
T fv = 0, const BF& bf = BF()) |
425 | 425 |
: _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf) |
426 | 426 |
{ |
427 | 427 |
LEMON_ASSERT(_values.size() == graph.dimension(), |
428 | 428 |
"Wrong size of range"); |
429 | 429 |
} |
430 | 430 |
|
431 | 431 |
/// \brief The partial accumulated value. |
432 | 432 |
/// |
433 | 433 |
/// Gives back the partial accumulated value. |
434 | 434 |
Value operator[](const Key& k) const { |
435 | 435 |
Value val = _first_value; |
436 | 436 |
int id = _graph.index(k); |
437 | 437 |
int n = 0; |
438 | 438 |
while (id != 0) { |
439 | 439 |
if (id & 1) { |
440 | 440 |
val = _bin_func(val, _values[n]); |
441 | 441 |
} |
442 | 442 |
id >>= 1; |
443 | 443 |
++n; |
444 | 444 |
} |
445 | 445 |
return val; |
446 | 446 |
} |
447 | 447 |
|
448 | 448 |
private: |
449 | 449 |
const Graph& _graph; |
450 | 450 |
std::vector<T> _values; |
451 | 451 |
T _first_value; |
452 | 452 |
BF _bin_func; |
453 | 453 |
}; |
454 | 454 |
|
455 | 455 |
}; |
456 | 456 |
|
457 | 457 |
} |
458 | 458 |
|
459 | 459 |
#endif |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)