deba@1912
|
1 |
/* -*- C++ -*-
|
deba@1912
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@1956
|
5 |
* Copyright (C) 2003-2006
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@1912
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@1912
|
8 |
*
|
deba@1912
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@1912
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@1912
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@1912
|
12 |
*
|
deba@1912
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@1912
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@1912
|
15 |
* purpose.
|
deba@1912
|
16 |
*
|
deba@1912
|
17 |
*/
|
deba@1912
|
18 |
|
deba@1912
|
19 |
#ifndef LEMON_DAG_SHORTEST_PATH_H
|
deba@1912
|
20 |
#define LEMON_DAG_SHORTEST_PATH_H
|
deba@1912
|
21 |
|
deba@1912
|
22 |
///\ingroup flowalgs
|
deba@1912
|
23 |
/// \file
|
deba@1912
|
24 |
/// \brief DagShortestPath algorithm.
|
deba@1912
|
25 |
///
|
deba@1912
|
26 |
|
deba@1912
|
27 |
#include <lemon/list_graph.h>
|
deba@1993
|
28 |
#include <lemon/bits/invalid.h>
|
deba@1912
|
29 |
#include <lemon/error.h>
|
deba@1912
|
30 |
#include <lemon/maps.h>
|
deba@1912
|
31 |
#include <lemon/topology.h>
|
deba@1912
|
32 |
|
deba@1912
|
33 |
#include <limits>
|
deba@1912
|
34 |
|
deba@1912
|
35 |
namespace lemon {
|
deba@1912
|
36 |
|
deba@1912
|
37 |
/// \brief Default OperationTraits for the DagShortestPath algorithm class.
|
deba@1912
|
38 |
///
|
deba@1912
|
39 |
/// It defines all computational operations and constants which are
|
deba@1912
|
40 |
/// used in the dag shortest path algorithm. The default implementation
|
deba@1912
|
41 |
/// is based on the numeric_limits class. If the numeric type does not
|
deba@1912
|
42 |
/// have infinity value then the maximum value is used as extremal
|
deba@1912
|
43 |
/// infinity value.
|
deba@1912
|
44 |
template <
|
deba@1912
|
45 |
typename Value,
|
deba@1912
|
46 |
bool has_infinity = std::numeric_limits<Value>::has_infinity>
|
deba@1912
|
47 |
struct DagShortestPathDefaultOperationTraits {
|
deba@1912
|
48 |
/// \brief Gives back the zero value of the type.
|
deba@1912
|
49 |
static Value zero() {
|
deba@1912
|
50 |
return static_cast<Value>(0);
|
deba@1912
|
51 |
}
|
deba@1912
|
52 |
/// \brief Gives back the positive infinity value of the type.
|
deba@1912
|
53 |
static Value infinity() {
|
deba@1912
|
54 |
return std::numeric_limits<Value>::infinity();
|
deba@1912
|
55 |
}
|
deba@1912
|
56 |
/// \brief Gives back the sum of the given two elements.
|
deba@1912
|
57 |
static Value plus(const Value& left, const Value& right) {
|
deba@1912
|
58 |
return left + right;
|
deba@1912
|
59 |
}
|
deba@1912
|
60 |
/// \brief Gives back true only if the first value less than the second.
|
deba@1912
|
61 |
static bool less(const Value& left, const Value& right) {
|
deba@1912
|
62 |
return left < right;
|
deba@1912
|
63 |
}
|
deba@1912
|
64 |
};
|
deba@1912
|
65 |
|
deba@1912
|
66 |
template <typename Value>
|
deba@1912
|
67 |
struct DagShortestPathDefaultOperationTraits<Value, false> {
|
deba@1912
|
68 |
static Value zero() {
|
deba@1912
|
69 |
return static_cast<Value>(0);
|
deba@1912
|
70 |
}
|
deba@1912
|
71 |
static Value infinity() {
|
deba@1912
|
72 |
return std::numeric_limits<Value>::max();
|
deba@1912
|
73 |
}
|
deba@1912
|
74 |
static Value plus(const Value& left, const Value& right) {
|
deba@1912
|
75 |
if (left == infinity() || right == infinity()) return infinity();
|
deba@1912
|
76 |
return left + right;
|
deba@1912
|
77 |
}
|
deba@1912
|
78 |
static bool less(const Value& left, const Value& right) {
|
deba@1912
|
79 |
return left < right;
|
deba@1912
|
80 |
}
|
deba@1912
|
81 |
};
|
deba@1912
|
82 |
|
deba@1912
|
83 |
/// \brief Default traits class of DagShortestPath class.
|
deba@1912
|
84 |
///
|
deba@1912
|
85 |
/// Default traits class of DagShortestPath class.
|
deba@1912
|
86 |
/// \param _Graph Graph type.
|
deba@1912
|
87 |
/// \param _LegthMap Type of length map.
|
deba@1912
|
88 |
template<class _Graph, class _LengthMap>
|
deba@1912
|
89 |
struct DagShortestPathDefaultTraits {
|
deba@1912
|
90 |
/// The graph type the algorithm runs on.
|
deba@1912
|
91 |
typedef _Graph Graph;
|
deba@1912
|
92 |
|
deba@1912
|
93 |
/// \brief The type of the map that stores the edge lengths.
|
deba@1912
|
94 |
///
|
deba@1912
|
95 |
/// The type of the map that stores the edge lengths.
|
alpar@2260
|
96 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
deba@1912
|
97 |
typedef _LengthMap LengthMap;
|
deba@1912
|
98 |
|
deba@1912
|
99 |
// The type of the length of the edges.
|
deba@1912
|
100 |
typedef typename _LengthMap::Value Value;
|
deba@1912
|
101 |
|
deba@1912
|
102 |
/// \brief Operation traits for dag shortest path algorithm.
|
deba@1912
|
103 |
///
|
deba@1912
|
104 |
/// It defines the infinity type on the given Value type
|
deba@1912
|
105 |
/// and the used operation.
|
deba@1912
|
106 |
/// \see DagShortestPathDefaultOperationTraits
|
deba@1912
|
107 |
typedef DagShortestPathDefaultOperationTraits<Value> OperationTraits;
|
deba@1912
|
108 |
|
deba@1912
|
109 |
/// \brief The type of the map that stores the last edges of the
|
deba@1912
|
110 |
/// shortest paths.
|
deba@1912
|
111 |
///
|
deba@1912
|
112 |
/// The type of the map that stores the last
|
deba@1912
|
113 |
/// edges of the shortest paths.
|
alpar@2260
|
114 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
deba@1912
|
115 |
///
|
deba@1912
|
116 |
typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
|
deba@1912
|
117 |
|
deba@1912
|
118 |
/// \brief Instantiates a PredMap.
|
deba@1912
|
119 |
///
|
deba@1912
|
120 |
/// This function instantiates a \ref PredMap.
|
alpar@1946
|
121 |
/// \param graph is the graph, to which we would
|
alpar@1946
|
122 |
/// like to define the PredMap.
|
deba@1912
|
123 |
/// \todo The graph alone may be insufficient for the initialization
|
deba@1912
|
124 |
static PredMap *createPredMap(const _Graph& graph) {
|
deba@1912
|
125 |
return new PredMap(graph);
|
deba@1912
|
126 |
}
|
deba@1912
|
127 |
|
deba@1912
|
128 |
/// \brief The type of the map that stores the dists of the nodes.
|
deba@1912
|
129 |
///
|
deba@1912
|
130 |
/// The type of the map that stores the dists of the nodes.
|
alpar@2260
|
131 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
deba@1912
|
132 |
///
|
deba@1912
|
133 |
typedef typename Graph::template NodeMap<typename _LengthMap::Value>
|
deba@1912
|
134 |
DistMap;
|
deba@1912
|
135 |
|
deba@1912
|
136 |
/// \brief Instantiates a DistMap.
|
deba@1912
|
137 |
///
|
deba@1912
|
138 |
/// This function instantiates a \ref DistMap.
|
alpar@1946
|
139 |
/// \param graph is the graph, to which we would like to define the
|
deba@1912
|
140 |
/// \ref DistMap
|
deba@1912
|
141 |
static DistMap *createDistMap(const _Graph& graph) {
|
deba@1912
|
142 |
return new DistMap(graph);
|
deba@1912
|
143 |
}
|
deba@1912
|
144 |
|
deba@1912
|
145 |
};
|
deba@1912
|
146 |
|
deba@1912
|
147 |
/// \brief Inverse OperationTraits for the DagShortestPath algorithm class.
|
deba@1912
|
148 |
///
|
deba@1912
|
149 |
/// It defines all computational operations and constants which are
|
deba@1912
|
150 |
/// used in the dag shortest path algorithm. It is the inverse of
|
deba@1912
|
151 |
/// \ref DagShortestPathDefaultOperationTraits, so it can be used to
|
deba@1912
|
152 |
/// calculate the longest path. The default implementation
|
deba@1912
|
153 |
/// is based on the numeric_limits class. If the numeric type does not
|
deba@1912
|
154 |
/// have infinity value then the minimum value is used as extremal
|
deba@1912
|
155 |
/// infinity value.
|
deba@1912
|
156 |
template <
|
deba@1912
|
157 |
typename Value,
|
deba@1912
|
158 |
bool has_infinity = std::numeric_limits<Value>::has_infinity>
|
deba@1912
|
159 |
struct DagLongestPathOperationTraits {
|
deba@1912
|
160 |
/// \brief Gives back the zero value of the type.
|
deba@1912
|
161 |
static Value zero() {
|
deba@1912
|
162 |
return static_cast<Value>(0);
|
deba@1912
|
163 |
}
|
deba@1912
|
164 |
/// \brief Gives back the negative infinity value of the type.
|
deba@1912
|
165 |
static Value infinity() {
|
deba@1912
|
166 |
return -(std::numeric_limits<Value>::infinity());
|
deba@1912
|
167 |
}
|
deba@1912
|
168 |
/// \brief Gives back the sum of the given two elements.
|
deba@1912
|
169 |
static Value plus(const Value& left, const Value& right) {
|
deba@1912
|
170 |
return left + right;
|
deba@1912
|
171 |
}
|
deba@1912
|
172 |
/// \brief Gives back true only if the first value less than the second.
|
deba@1912
|
173 |
static bool less(const Value& left, const Value& right) {
|
deba@1912
|
174 |
return left > right;
|
deba@1912
|
175 |
}
|
deba@1912
|
176 |
};
|
deba@1912
|
177 |
|
deba@1912
|
178 |
template <typename Value>
|
deba@1912
|
179 |
struct DagLongestPathOperationTraits<Value, false> {
|
deba@1912
|
180 |
static Value zero() {
|
deba@1912
|
181 |
return static_cast<Value>(0);
|
deba@1912
|
182 |
}
|
deba@1912
|
183 |
static Value infinity() {
|
deba@1912
|
184 |
return std::numeric_limits<Value>::min();
|
deba@1912
|
185 |
}
|
deba@1912
|
186 |
static Value plus(const Value& left, const Value& right) {
|
deba@1912
|
187 |
if (left == infinity() || right == infinity()) return infinity();
|
deba@1912
|
188 |
return left + right;
|
deba@1912
|
189 |
}
|
deba@1912
|
190 |
static bool less(const Value& left, const Value& right) {
|
deba@1912
|
191 |
return left > right;
|
deba@1912
|
192 |
}
|
deba@1912
|
193 |
};
|
deba@1912
|
194 |
|
deba@1912
|
195 |
/// \brief Inverse traits class of DagShortestPath class.
|
deba@1912
|
196 |
///
|
deba@1912
|
197 |
/// Inverse traits class of DagShortestPath class.
|
deba@1912
|
198 |
/// \param _Graph Graph type.
|
deba@1912
|
199 |
/// \param _LegthMap Type of length map.
|
deba@1912
|
200 |
template<class _Graph, class _LengthMap>
|
deba@1912
|
201 |
struct DagLongestPathTraits {
|
deba@1912
|
202 |
/// The graph type the algorithm runs on.
|
deba@1912
|
203 |
typedef _Graph Graph;
|
deba@1912
|
204 |
|
deba@1912
|
205 |
/// \brief The type of the map that stores the edge lengths.
|
deba@1912
|
206 |
///
|
deba@1912
|
207 |
/// The type of the map that stores the edge lengths.
|
alpar@2260
|
208 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
deba@1912
|
209 |
typedef _LengthMap LengthMap;
|
deba@1912
|
210 |
|
deba@1912
|
211 |
// The type of the length of the edges.
|
deba@1912
|
212 |
typedef typename _LengthMap::Value Value;
|
deba@1912
|
213 |
|
deba@1912
|
214 |
/// \brief Inverse operation traits for dag shortest path algorithm.
|
deba@1912
|
215 |
///
|
deba@1912
|
216 |
/// It defines the infinity type on the given Value type
|
deba@1912
|
217 |
/// and the used operation.
|
deba@1912
|
218 |
/// \see DagLongestPathOperationTraits
|
deba@1912
|
219 |
typedef DagLongestPathOperationTraits<Value> OperationTraits;
|
deba@1912
|
220 |
|
deba@1912
|
221 |
/// \brief The type of the map that stores the last edges of the
|
deba@1912
|
222 |
/// longest paths.
|
deba@1912
|
223 |
///
|
deba@1912
|
224 |
/// The type of the map that stores the last
|
deba@1912
|
225 |
/// edges of the longest paths.
|
alpar@2260
|
226 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
deba@1912
|
227 |
///
|
deba@1912
|
228 |
typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
|
deba@1912
|
229 |
|
deba@1912
|
230 |
/// \brief Instantiates a PredMap.
|
deba@1912
|
231 |
///
|
deba@1912
|
232 |
/// This function instantiates a \ref PredMap.
|
alpar@1946
|
233 |
/// \param graph is the graph,
|
alpar@1946
|
234 |
/// to which we would like to define the PredMap.
|
deba@1912
|
235 |
/// \todo The graph alone may be insufficient for the initialization
|
deba@1912
|
236 |
static PredMap *createPredMap(const _Graph& graph) {
|
deba@1912
|
237 |
return new PredMap(graph);
|
deba@1912
|
238 |
}
|
deba@1912
|
239 |
|
deba@1912
|
240 |
/// \brief The type of the map that stores the dists of the nodes.
|
deba@1912
|
241 |
///
|
deba@1912
|
242 |
/// The type of the map that stores the dists of the nodes.
|
alpar@2260
|
243 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
deba@1912
|
244 |
///
|
deba@1912
|
245 |
typedef typename Graph::template NodeMap<typename _LengthMap::Value>
|
deba@1912
|
246 |
DistMap;
|
deba@1912
|
247 |
|
deba@1912
|
248 |
/// \brief Instantiates a DistMap.
|
deba@1912
|
249 |
///
|
deba@1912
|
250 |
/// This function instantiates a \ref DistMap.
|
alpar@1946
|
251 |
/// \param graph is the graph, to which we would like to define the
|
deba@1912
|
252 |
/// \ref DistMap
|
deba@1912
|
253 |
static DistMap *createDistMap(const _Graph& graph) {
|
deba@1912
|
254 |
return new DistMap(graph);
|
deba@1912
|
255 |
}
|
deba@1912
|
256 |
|
deba@1912
|
257 |
};
|
deba@1912
|
258 |
|
deba@1912
|
259 |
|
deba@1912
|
260 |
/// \brief %DagShortestPath algorithm class.
|
deba@1912
|
261 |
///
|
deba@1912
|
262 |
/// \ingroup flowalgs
|
deba@1912
|
263 |
/// This class provides an efficient implementation of a Dag sortest path
|
deba@1912
|
264 |
/// searching algorithm. The edge lengths are passed to the algorithm
|
alpar@2260
|
265 |
/// using a \ref concepts::ReadMap "ReadMap", so it is easy to change it
|
deba@1912
|
266 |
/// to any kind of length.
|
deba@1912
|
267 |
///
|
deba@1912
|
268 |
/// The complexity of the algorithm is O(n + e).
|
deba@1912
|
269 |
///
|
deba@1912
|
270 |
/// The type of the length is determined by the
|
alpar@2260
|
271 |
/// \ref concepts::ReadMap::Value "Value" of the length map.
|
deba@1912
|
272 |
///
|
deba@1912
|
273 |
/// \param _Graph The graph type the algorithm runs on. The default value
|
deba@1912
|
274 |
/// is \ref ListGraph. The value of _Graph is not used directly by
|
deba@1912
|
275 |
/// DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits.
|
deba@1912
|
276 |
/// \param _LengthMap This read-only EdgeMap determines the lengths of the
|
alpar@2260
|
277 |
/// edges. The default map type is \ref concepts::Graph::EdgeMap
|
deba@1912
|
278 |
/// "Graph::EdgeMap<int>". The value of _LengthMap is not used directly
|
deba@1912
|
279 |
/// by DagShortestPath, it is only passed to \ref DagShortestPathDefaultTraits.
|
deba@1912
|
280 |
/// \param _Traits Traits class to set various data types used by the
|
deba@1912
|
281 |
/// algorithm. The default traits class is \ref DagShortestPathDefaultTraits
|
deba@1912
|
282 |
/// "DagShortestPathDefaultTraits<_Graph,_LengthMap>". See \ref
|
deba@1912
|
283 |
/// DagShortestPathDefaultTraits for the documentation of a DagShortestPath traits
|
deba@1912
|
284 |
/// class.
|
deba@1912
|
285 |
///
|
deba@1912
|
286 |
/// \author Balazs Attila Mihaly (based on Balazs Dezso's work)
|
deba@1912
|
287 |
|
deba@1912
|
288 |
#ifdef DOXYGEN
|
deba@1912
|
289 |
template <typename _Graph, typename _LengthMap, typename _Traits>
|
deba@1912
|
290 |
#else
|
deba@1912
|
291 |
template <typename _Graph=ListGraph,
|
deba@1912
|
292 |
typename _LengthMap=typename _Graph::template EdgeMap<int>,
|
deba@1912
|
293 |
typename _Traits=DagShortestPathDefaultTraits<_Graph,_LengthMap> >
|
deba@1912
|
294 |
#endif
|
deba@1912
|
295 |
class DagShortestPath {
|
deba@1912
|
296 |
public:
|
deba@1912
|
297 |
|
deba@1912
|
298 |
/// \brief \ref Exception for uninitialized parameters.
|
deba@1912
|
299 |
///
|
deba@1912
|
300 |
/// This error represents problems in the initialization
|
deba@1912
|
301 |
/// of the parameters of the algorithms.
|
deba@1912
|
302 |
|
deba@1912
|
303 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
deba@1912
|
304 |
public:
|
alpar@2151
|
305 |
virtual const char* what() const throw() {
|
deba@1912
|
306 |
return "lemon::DagShortestPath::UninitializedParameter";
|
deba@1912
|
307 |
}
|
deba@1912
|
308 |
};
|
deba@1912
|
309 |
|
deba@1912
|
310 |
typedef _Traits Traits;
|
deba@1912
|
311 |
///The type of the underlying graph.
|
deba@1912
|
312 |
typedef typename _Traits::Graph Graph;
|
deba@1912
|
313 |
|
deba@1912
|
314 |
typedef typename Graph::Node Node;
|
deba@1912
|
315 |
typedef typename Graph::NodeIt NodeIt;
|
deba@1912
|
316 |
typedef typename Graph::Edge Edge;
|
deba@1912
|
317 |
typedef typename Graph::EdgeIt EdgeIt;
|
deba@1912
|
318 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
deba@1912
|
319 |
|
deba@1912
|
320 |
/// \brief The type of the length of the edges.
|
deba@1912
|
321 |
typedef typename _Traits::LengthMap::Value Value;
|
deba@1912
|
322 |
/// \brief The type of the map that stores the edge lengths.
|
deba@1912
|
323 |
typedef typename _Traits::LengthMap LengthMap;
|
deba@1912
|
324 |
/// \brief The type of the map that stores the last
|
deba@1912
|
325 |
/// edges of the shortest paths.
|
deba@1912
|
326 |
typedef typename _Traits::PredMap PredMap;
|
deba@1912
|
327 |
/// \brief The type of the map that stores the dists of the nodes.
|
deba@1912
|
328 |
typedef typename _Traits::DistMap DistMap;
|
deba@1912
|
329 |
/// \brief The operation traits.
|
deba@1912
|
330 |
typedef typename _Traits::OperationTraits OperationTraits;
|
deba@1912
|
331 |
/// \brief The Node weight map.
|
deba@1999
|
332 |
typedef typename Graph::template NodeMap<Value> WeightMap;
|
deba@1912
|
333 |
private:
|
deba@1912
|
334 |
/// Pointer to the underlying graph
|
deba@1912
|
335 |
const Graph *graph;
|
deba@1912
|
336 |
/// Pointer to the length map
|
deba@1912
|
337 |
const LengthMap *length;
|
deba@1912
|
338 |
///Pointer to the map of predecessors edges
|
deba@1912
|
339 |
PredMap *_pred;
|
deba@1912
|
340 |
///Indicates if \ref _pred is locally allocated (\c true) or not
|
deba@1912
|
341 |
bool local_pred;
|
deba@1912
|
342 |
///Pointer to the map of distances
|
deba@1912
|
343 |
DistMap *_dist;
|
deba@1912
|
344 |
///Indicates if \ref _dist is locally allocated (\c true) or not
|
deba@1912
|
345 |
bool local_dist;
|
deba@1912
|
346 |
///Process step counter
|
deba@1912
|
347 |
unsigned int _process_step;
|
deba@1912
|
348 |
|
deba@1912
|
349 |
std::vector<Node> _node_order;
|
deba@1912
|
350 |
|
deba@1912
|
351 |
/// Creates the maps if necessary.
|
deba@1912
|
352 |
void create_maps() {
|
deba@1912
|
353 |
if(!_pred) {
|
deba@1912
|
354 |
local_pred = true;
|
deba@1912
|
355 |
_pred = Traits::createPredMap(*graph);
|
deba@1912
|
356 |
}
|
deba@1912
|
357 |
if(!_dist) {
|
deba@1912
|
358 |
local_dist = true;
|
deba@1912
|
359 |
_dist = Traits::createDistMap(*graph);
|
deba@1912
|
360 |
}
|
deba@1912
|
361 |
}
|
deba@1912
|
362 |
|
deba@1912
|
363 |
public :
|
deba@1912
|
364 |
|
deba@1912
|
365 |
typedef DagShortestPath Create;
|
deba@1912
|
366 |
|
deba@1912
|
367 |
/// \name Named template parameters
|
deba@1912
|
368 |
|
deba@1912
|
369 |
///@{
|
deba@1912
|
370 |
|
deba@1912
|
371 |
template <class T>
|
deba@1912
|
372 |
struct DefPredMapTraits : public Traits {
|
deba@1912
|
373 |
typedef T PredMap;
|
deba@1912
|
374 |
static PredMap *createPredMap(const Graph&) {
|
deba@1912
|
375 |
throw UninitializedParameter();
|
deba@1912
|
376 |
}
|
deba@1912
|
377 |
};
|
deba@1912
|
378 |
|
deba@1912
|
379 |
/// \brief \ref named-templ-param "Named parameter" for setting PredMap
|
deba@1912
|
380 |
/// type
|
deba@1912
|
381 |
/// \ref named-templ-param "Named parameter" for setting PredMap type
|
deba@1912
|
382 |
///
|
deba@1912
|
383 |
template <class T>
|
deba@1912
|
384 |
struct DefPredMap {
|
deba@1912
|
385 |
typedef DagShortestPath< Graph, LengthMap, DefPredMapTraits<T> > Create;
|
deba@1912
|
386 |
};
|
deba@1912
|
387 |
|
deba@1912
|
388 |
template <class T>
|
deba@1912
|
389 |
struct DefDistMapTraits : public Traits {
|
deba@1912
|
390 |
typedef T DistMap;
|
deba@1912
|
391 |
static DistMap *createDistMap(const Graph& graph) {
|
deba@1912
|
392 |
throw UninitializedParameter();
|
deba@1912
|
393 |
}
|
deba@1912
|
394 |
};
|
deba@1912
|
395 |
|
deba@1912
|
396 |
/// \brief \ref named-templ-param "Named parameter" for setting DistMap
|
deba@1912
|
397 |
/// type
|
deba@1912
|
398 |
///
|
deba@1912
|
399 |
/// \ref named-templ-param "Named parameter" for setting DistMap type
|
deba@1912
|
400 |
///
|
deba@1912
|
401 |
template <class T>
|
deba@1912
|
402 |
struct DefDistMap
|
deba@1912
|
403 |
: public DagShortestPath< Graph, LengthMap, DefDistMapTraits<T> > {
|
deba@1912
|
404 |
typedef DagShortestPath< Graph, LengthMap, DefDistMapTraits<T> > Create;
|
deba@1912
|
405 |
};
|
deba@1912
|
406 |
|
deba@1912
|
407 |
template <class T>
|
deba@1912
|
408 |
struct DefOperationTraitsTraits : public Traits {
|
deba@1912
|
409 |
typedef T OperationTraits;
|
deba@1912
|
410 |
};
|
deba@1912
|
411 |
|
deba@1912
|
412 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
deba@1912
|
413 |
/// OperationTraits type
|
deba@1912
|
414 |
///
|
deba@1912
|
415 |
/// \ref named-templ-param "Named parameter" for setting OperationTraits
|
deba@1912
|
416 |
/// type
|
deba@1912
|
417 |
template <class T>
|
deba@1912
|
418 |
struct DefOperationTraits
|
deba@1912
|
419 |
: public DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits<T> > {
|
deba@1912
|
420 |
typedef DagShortestPath< Graph, LengthMap, DefOperationTraitsTraits<T> >
|
deba@1912
|
421 |
Create;
|
deba@1912
|
422 |
};
|
deba@1912
|
423 |
|
deba@1912
|
424 |
///@}
|
deba@1912
|
425 |
|
deba@1912
|
426 |
protected:
|
deba@1912
|
427 |
|
deba@1912
|
428 |
DagShortestPath() {}
|
deba@1912
|
429 |
|
deba@1912
|
430 |
public:
|
deba@1912
|
431 |
|
deba@1912
|
432 |
/// \brief Constructor.
|
deba@1912
|
433 |
///
|
deba@1912
|
434 |
/// \param _graph the graph the algorithm will run on.
|
deba@1912
|
435 |
/// \param _length the length map used by the algorithm.
|
deba@1912
|
436 |
DagShortestPath(const Graph& _graph, const LengthMap& _length) :
|
deba@1912
|
437 |
graph(&_graph), length(&_length),
|
deba@1912
|
438 |
_pred(0), local_pred(false),
|
deba@1912
|
439 |
_dist(0), local_dist(false){}
|
deba@1912
|
440 |
|
deba@1912
|
441 |
/// \brief Constructor with node weight map.
|
deba@1912
|
442 |
///
|
deba@1912
|
443 |
/// \param _graph the graph the algorithm will run on.
|
deba@1912
|
444 |
/// \param _length the length map used by the algorithm.
|
deba@1912
|
445 |
/// The NodeMap _length will be used as the weight map.
|
deba@1912
|
446 |
/// Each edge will have the weight of its target node.
|
deba@1912
|
447 |
DagShortestPath(const Graph& _graph, const WeightMap& _length) :
|
deba@1912
|
448 |
graph(&_graph),
|
deba@1912
|
449 |
_pred(0), local_pred(false),
|
deba@1912
|
450 |
_dist(0), local_dist(false){
|
deba@1912
|
451 |
length=new LengthMap(_graph);
|
deba@1912
|
452 |
_dist=new DistMap(_graph);
|
deba@1912
|
453 |
for(EdgeIt eit(_graph);eit!=INVALID;++eit)
|
deba@1912
|
454 |
(const_cast<LengthMap*>(length))->set(eit,_length[_graph.target(eit)]);
|
deba@1912
|
455 |
}
|
deba@1912
|
456 |
|
deba@1912
|
457 |
///Destructor.
|
deba@1912
|
458 |
~DagShortestPath() {
|
deba@1912
|
459 |
if(local_pred) delete _pred;
|
deba@1912
|
460 |
if(local_dist) delete _dist;
|
deba@1912
|
461 |
}
|
deba@1912
|
462 |
|
deba@1912
|
463 |
/// \brief Sets the length map.
|
deba@1912
|
464 |
///
|
deba@1912
|
465 |
/// Sets the length map.
|
deba@1912
|
466 |
/// \return \c (*this)
|
deba@1912
|
467 |
DagShortestPath &lengthMap(const LengthMap &m) {
|
deba@1912
|
468 |
length = &m;
|
deba@1912
|
469 |
return *this;
|
deba@1912
|
470 |
}
|
deba@1912
|
471 |
|
deba@1912
|
472 |
/// \brief Sets the map storing the predecessor edges.
|
deba@1912
|
473 |
///
|
deba@1912
|
474 |
/// Sets the map storing the predecessor edges.
|
deba@1912
|
475 |
/// If you don't use this function before calling \ref run(),
|
deba@1912
|
476 |
/// it will allocate one. The destuctor deallocates this
|
deba@1912
|
477 |
/// automatically allocated map, of course.
|
deba@1912
|
478 |
/// \return \c (*this)
|
deba@1912
|
479 |
DagShortestPath &predMap(PredMap &m) {
|
deba@1912
|
480 |
if(local_pred) {
|
deba@1912
|
481 |
delete _pred;
|
deba@1912
|
482 |
local_pred=false;
|
deba@1912
|
483 |
}
|
deba@1912
|
484 |
_pred = &m;
|
deba@1912
|
485 |
return *this;
|
deba@1912
|
486 |
}
|
deba@1912
|
487 |
|
deba@1912
|
488 |
/// \brief Sets the map storing the distances calculated by the algorithm.
|
deba@1912
|
489 |
///
|
deba@1912
|
490 |
/// Sets the map storing the distances calculated by the algorithm.
|
deba@1912
|
491 |
/// If you don't use this function before calling \ref run(),
|
deba@1912
|
492 |
/// it will allocate one. The destuctor deallocates this
|
deba@1912
|
493 |
/// automatically allocated map, of course.
|
deba@1912
|
494 |
/// \return \c (*this)
|
deba@1912
|
495 |
DagShortestPath &distMap(DistMap &m) {
|
deba@1912
|
496 |
if(local_dist) {
|
deba@1912
|
497 |
delete _dist;
|
deba@1912
|
498 |
local_dist=false;
|
deba@1912
|
499 |
}
|
deba@1912
|
500 |
_dist = &m;
|
deba@1912
|
501 |
return *this;
|
deba@1912
|
502 |
}
|
deba@1912
|
503 |
|
deba@1912
|
504 |
/// \name Execution control
|
deba@1912
|
505 |
/// The simplest way to execute the algorithm is to use
|
deba@1912
|
506 |
/// one of the member functions called \c run(...)
|
deba@1912
|
507 |
/// \n
|
deba@1912
|
508 |
/// If you need more control on the execution,
|
deba@1912
|
509 |
/// first you must call \ref init(...), then you can add several source
|
deba@1912
|
510 |
/// nodes with \ref addSource().
|
deba@1912
|
511 |
/// Finally \ref start() will perform the actual path computation.
|
deba@1912
|
512 |
/// Some functions have an alternative form (\ref checkedInit(...),
|
deba@1912
|
513 |
/// \ref checkedRun(...)) which also verifies if the graph given in the
|
deba@1912
|
514 |
/// constructor is a dag.
|
deba@1912
|
515 |
|
deba@1912
|
516 |
///@{
|
deba@1912
|
517 |
|
deba@1912
|
518 |
/// \brief Initializes the internal data structures.
|
deba@1912
|
519 |
///
|
deba@1912
|
520 |
/// Initializes the internal data structures.
|
deba@1912
|
521 |
void init(const Value value = OperationTraits::infinity()) {
|
deba@1912
|
522 |
typedef typename Graph::template NodeMap<int> NodeOrderMap;
|
deba@1912
|
523 |
_process_step=0;
|
deba@1912
|
524 |
NodeOrderMap node_order(*graph);
|
deba@1912
|
525 |
topologicalSort(*graph,node_order);
|
deba@1912
|
526 |
_node_order.resize(countNodes(*graph),INVALID);
|
deba@1912
|
527 |
create_maps();
|
deba@1912
|
528 |
for (NodeIt it(*graph); it != INVALID; ++it) {
|
deba@1912
|
529 |
_node_order[node_order[it]]=it;
|
deba@1912
|
530 |
_pred->set(it, INVALID);
|
deba@1912
|
531 |
_dist->set(it, value);
|
deba@1912
|
532 |
}
|
deba@1912
|
533 |
}
|
deba@1912
|
534 |
|
deba@1912
|
535 |
/// \brief Initializes the internal data structures
|
deba@1912
|
536 |
/// with a given topological sort (NodeMap).
|
deba@1912
|
537 |
///
|
deba@1912
|
538 |
/// Initializes the internal data structures
|
deba@1912
|
539 |
/// with a given topological sort (NodeMap).
|
deba@1912
|
540 |
void init(const typename Graph::template NodeMap<int>& node_order,
|
deba@1912
|
541 |
const Value value = OperationTraits::infinity()) {
|
deba@1912
|
542 |
_process_step=0;
|
deba@1912
|
543 |
_node_order.resize(countNodes(*graph),INVALID);
|
deba@1912
|
544 |
create_maps();
|
deba@1912
|
545 |
for (NodeIt it(*graph); it != INVALID; ++it) {
|
deba@1912
|
546 |
_node_order[node_order[it]]=it;
|
deba@1912
|
547 |
_pred->set(it, INVALID);
|
deba@1912
|
548 |
_dist->set(it, value);
|
deba@1912
|
549 |
}
|
deba@1912
|
550 |
}
|
deba@1912
|
551 |
|
deba@1912
|
552 |
/// \brief Initializes the internal data structures
|
deba@1912
|
553 |
/// with a given topological sort (std::vector).
|
deba@1912
|
554 |
///
|
deba@1912
|
555 |
/// Initializes the internal data structures
|
deba@1912
|
556 |
/// with a given topological sort (std::vector).
|
deba@1912
|
557 |
void init(const std::vector<Node>& node_order,
|
deba@1912
|
558 |
const Value value = OperationTraits::infinity()) {
|
deba@1912
|
559 |
_process_step=0;
|
deba@1912
|
560 |
_node_order=node_order;
|
deba@1912
|
561 |
create_maps();
|
deba@1912
|
562 |
for (NodeIt it(*graph); it != INVALID; ++it) {
|
deba@1912
|
563 |
_pred->set(it, INVALID);
|
deba@1912
|
564 |
_dist->set(it, value);
|
deba@1912
|
565 |
}
|
deba@1912
|
566 |
}
|
deba@1912
|
567 |
|
deba@1912
|
568 |
/// \brief Initializes the internal data structures. It also checks if the graph is dag.
|
deba@1912
|
569 |
///
|
deba@1912
|
570 |
/// Initializes the internal data structures. It also checks if the graph is dag.
|
deba@1912
|
571 |
/// \return true if the graph (given in the constructor) is dag, false otherwise.
|
deba@1912
|
572 |
bool checkedInit(const Value value = OperationTraits::infinity()) {
|
deba@1912
|
573 |
typedef typename Graph::template NodeMap<int> NodeOrderMap;
|
deba@1912
|
574 |
NodeOrderMap node_order(*graph);
|
deba@1912
|
575 |
if(!checkedTopologicalSort(*graph,node_order))return false;
|
deba@1912
|
576 |
init(node_order,value);
|
deba@1912
|
577 |
return true;
|
deba@1912
|
578 |
}
|
deba@1912
|
579 |
|
deba@1912
|
580 |
/// \brief Initializes the internal data structures with a given
|
deba@1912
|
581 |
/// topological sort (NodeMap). It also checks if the graph is dag.
|
deba@1912
|
582 |
///
|
deba@1912
|
583 |
/// Initializes the internal data structures with a given
|
deba@1912
|
584 |
/// topological sort (NodeMap). It also checks if the graph is dag.
|
deba@1912
|
585 |
/// \return true if the graph (given in the constructor) is dag, false otherwise.
|
deba@1912
|
586 |
bool checkedInit(const typename Graph::template NodeMap<int>& node_order,
|
deba@1912
|
587 |
const Value value = OperationTraits::infinity()) {
|
deba@1912
|
588 |
for(NodeIt it(*graph);it!=INVALID;++it){
|
deba@1912
|
589 |
for(OutEdgeIt oeit(*graph,it);oeit!=INVALID;++oeit){
|
deba@1912
|
590 |
if(node_order[graph->target(oeit)]<node_order[it])return false;
|
deba@1912
|
591 |
}
|
deba@1912
|
592 |
}
|
deba@1912
|
593 |
init(node_order,value);
|
deba@1912
|
594 |
return true;
|
deba@1912
|
595 |
}
|
deba@1912
|
596 |
|
deba@1912
|
597 |
/// \brief Initializes the internal data structures with a given
|
deba@1912
|
598 |
/// topological sort (std::vector). It also checks if the graph is dag.
|
deba@1912
|
599 |
///
|
deba@1912
|
600 |
/// Initializes the internal data structures with a given
|
deba@1912
|
601 |
/// topological sort (std::vector). It also checks if the graph is dag.
|
deba@1912
|
602 |
/// \return true if the graph (given in the constructor) is dag, false otherwise.
|
deba@1912
|
603 |
bool checkedInit(const std::vector<Node>& node_order,
|
deba@1912
|
604 |
const Value value = OperationTraits::infinity()) {
|
deba@1912
|
605 |
typedef typename Graph::template NodeMap<bool> BoolNodeMap;
|
deba@1912
|
606 |
BoolNodeMap _processed(*graph,false);
|
deba@1912
|
607 |
for(unsigned int i=0;i<_node_order.size();++i){
|
deba@1912
|
608 |
_processed[node_order[i]]=true;
|
deba@1912
|
609 |
for(OutEdgeIt oeit(*graph,node_order[i]);oeit!=INVALID;++oeit){
|
deba@1912
|
610 |
if(_processed[graph->target(oeit)])return false;
|
deba@1912
|
611 |
}
|
deba@1912
|
612 |
}
|
deba@1912
|
613 |
init(node_order,value);
|
deba@1912
|
614 |
return true;
|
deba@1912
|
615 |
}
|
deba@1912
|
616 |
|
deba@1912
|
617 |
/// \brief Adds a new source node.
|
deba@1912
|
618 |
///
|
deba@1912
|
619 |
/// The optional second parameter is the initial distance of the node.
|
deba@1912
|
620 |
/// It just sets the distance of the node to the given value.
|
deba@1912
|
621 |
void addSource(Node source, Value dst = OperationTraits::zero()) {
|
deba@1912
|
622 |
if((*_dist)[source] != dst){
|
deba@1912
|
623 |
_dist->set(source, dst);
|
deba@1912
|
624 |
}
|
deba@1912
|
625 |
}
|
deba@1912
|
626 |
|
deba@1912
|
627 |
/// \brief Executes one step from the dag shortest path algorithm.
|
deba@1912
|
628 |
///
|
deba@1912
|
629 |
/// If the algoritm calculated the distances in the previous step
|
deba@1912
|
630 |
/// strictly for all at most k length paths then it will calculate the
|
deba@1912
|
631 |
/// distances strictly for all at most k + 1 length paths. With k
|
deba@1912
|
632 |
/// iteration this function calculates the at most k length paths.
|
deba@1912
|
633 |
///\pre the queue is not empty
|
deba@1912
|
634 |
///\return the currently processed node
|
deba@1912
|
635 |
Node processNextNode() {
|
deba@1912
|
636 |
if(reached(_node_order[_process_step])){
|
deba@1912
|
637 |
for (OutEdgeIt it(*graph, _node_order[_process_step]); it != INVALID; ++it) {
|
deba@1912
|
638 |
Node target = graph->target(it);
|
deba@1912
|
639 |
Value relaxed =
|
deba@1912
|
640 |
OperationTraits::plus((*_dist)[_node_order[_process_step]], (*length)[it]);
|
deba@1912
|
641 |
if (OperationTraits::less(relaxed, (*_dist)[target])) {
|
deba@1912
|
642 |
_pred->set(target, it);
|
deba@1912
|
643 |
_dist->set(target, relaxed);
|
deba@1912
|
644 |
}
|
deba@1912
|
645 |
}
|
deba@1912
|
646 |
}
|
deba@1912
|
647 |
++_process_step;
|
deba@1912
|
648 |
return _node_order[_process_step-1];
|
deba@1912
|
649 |
}
|
deba@1912
|
650 |
|
deba@1912
|
651 |
///\brief Returns \c false if there are nodes
|
deba@1912
|
652 |
///to be processed in the queue
|
deba@1912
|
653 |
///
|
deba@1912
|
654 |
///Returns \c false if there are nodes
|
deba@1912
|
655 |
///to be processed in the queue
|
deba@1912
|
656 |
bool emptyQueue() { return _node_order.size()-1==_process_step; }
|
deba@1912
|
657 |
|
deba@1912
|
658 |
///\brief Returns the number of the nodes to be processed.
|
deba@1912
|
659 |
///
|
deba@1912
|
660 |
///Returns the number of the nodes to be processed in the queue.
|
deba@1912
|
661 |
int queueSize() { return _node_order.size()-1-_process_step; }
|
deba@1912
|
662 |
|
deba@1912
|
663 |
/// \brief Executes the algorithm.
|
deba@1912
|
664 |
///
|
deba@1912
|
665 |
/// \pre init() must be called and at least one node should be added
|
deba@1912
|
666 |
/// with addSource() before using this function.
|
deba@1912
|
667 |
///
|
deba@1912
|
668 |
/// This method runs the %DagShortestPath algorithm from the root node(s)
|
deba@1912
|
669 |
/// in order to compute the shortest path to each node. The algorithm
|
deba@1912
|
670 |
/// computes
|
deba@1912
|
671 |
/// - The shortest path tree.
|
deba@1912
|
672 |
/// - The distance of each node from the root(s).
|
deba@1912
|
673 |
void start() {
|
deba@1912
|
674 |
while(!emptyQueue()) {
|
deba@1912
|
675 |
processNextNode();
|
deba@1912
|
676 |
}
|
deba@1912
|
677 |
}
|
deba@1912
|
678 |
|
deba@1912
|
679 |
/// \brief Runs %DagShortestPath algorithm from node \c s.
|
deba@1912
|
680 |
///
|
deba@1912
|
681 |
/// This method runs the %DagShortestPath algorithm from a root node \c s
|
deba@1912
|
682 |
/// in order to compute the shortest path to each node. The algorithm
|
deba@1912
|
683 |
/// computes
|
deba@1912
|
684 |
/// - The shortest path tree.
|
deba@1912
|
685 |
/// - The distance of each node from the root.
|
deba@1912
|
686 |
///
|
deba@1912
|
687 |
/// \note d.run(s) is just a shortcut of the following code.
|
alpar@1946
|
688 |
///\code
|
deba@1912
|
689 |
/// d.init();
|
deba@1912
|
690 |
/// d.addSource(s);
|
deba@1912
|
691 |
/// d.start();
|
alpar@1946
|
692 |
///\endcode
|
deba@1912
|
693 |
void run(Node s) {
|
deba@1912
|
694 |
init();
|
deba@1912
|
695 |
addSource(s);
|
deba@1912
|
696 |
start();
|
deba@1912
|
697 |
}
|
deba@1912
|
698 |
|
deba@1912
|
699 |
/// \brief Runs %DagShortestPath algorithm from node \c s.
|
deba@1912
|
700 |
/// It also checks if the graph is a dag.
|
deba@1912
|
701 |
///
|
deba@1912
|
702 |
/// This method runs the %DagShortestPath algorithm from a root node \c s
|
deba@1912
|
703 |
/// in order to compute the shortest path to each node. The algorithm
|
deba@1912
|
704 |
/// computes
|
deba@1912
|
705 |
/// - The shortest path tree.
|
deba@1912
|
706 |
/// - The distance of each node from the root.
|
deba@1912
|
707 |
/// The algorithm checks if the graph given int the constructor is a dag.
|
deba@1912
|
708 |
bool checkedRun(Node s) {
|
deba@1912
|
709 |
if(!checkedInit())return false;
|
deba@1912
|
710 |
addSource(s);
|
deba@1912
|
711 |
start();
|
deba@1912
|
712 |
return true;
|
deba@1912
|
713 |
}
|
deba@1912
|
714 |
|
deba@1912
|
715 |
///@}
|
deba@1912
|
716 |
|
deba@1912
|
717 |
/// \name Query Functions
|
deba@1912
|
718 |
/// The result of the %DagShortestPath algorithm can be obtained using these
|
deba@1912
|
719 |
/// functions.\n
|
deba@1912
|
720 |
/// Before the use of these functions,
|
deba@1912
|
721 |
/// either run() or start() must be called.
|
deba@1912
|
722 |
|
deba@1912
|
723 |
///@{
|
deba@1912
|
724 |
|
deba@1912
|
725 |
/// \brief Copies the shortest path to \c t into \c p
|
deba@1912
|
726 |
///
|
deba@1912
|
727 |
/// This function copies the shortest path to \c t into \c p.
|
deba@1912
|
728 |
/// If it \c t is a source itself or unreachable, then it does not
|
deba@1912
|
729 |
/// alter \c p.
|
deba@1912
|
730 |
///
|
deba@1912
|
731 |
/// \return Returns \c true if a path to \c t was actually copied to \c p,
|
deba@1912
|
732 |
/// \c false otherwise.
|
deba@1912
|
733 |
/// \sa DirPath
|
deba@1912
|
734 |
template <typename Path>
|
deba@1912
|
735 |
bool getPath(Path &p, Node t) {
|
deba@1912
|
736 |
if(reached(t)) {
|
deba@1912
|
737 |
p.clear();
|
deba@1912
|
738 |
typename Path::Builder b(p);
|
deba@1912
|
739 |
for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t))
|
deba@1912
|
740 |
b.pushFront(predEdge(t));
|
deba@1912
|
741 |
b.commit();
|
deba@1912
|
742 |
return true;
|
deba@1912
|
743 |
}
|
deba@1912
|
744 |
return false;
|
deba@1912
|
745 |
}
|
deba@1912
|
746 |
|
deba@1912
|
747 |
/// \brief The distance of a node from the root.
|
deba@1912
|
748 |
///
|
deba@1912
|
749 |
/// Returns the distance of a node from the root.
|
deba@1912
|
750 |
/// \pre \ref run() must be called before using this function.
|
deba@1912
|
751 |
/// \warning If node \c v in unreachable from the root the return value
|
deba@1912
|
752 |
/// of this funcion is undefined.
|
deba@1912
|
753 |
Value dist(Node v) const { return (*_dist)[v]; }
|
deba@1912
|
754 |
|
deba@1912
|
755 |
/// \brief Returns the 'previous edge' of the shortest path tree.
|
deba@1912
|
756 |
///
|
deba@1912
|
757 |
/// For a node \c v it returns the 'previous edge' of the shortest path
|
deba@1912
|
758 |
/// tree, i.e. it returns the last edge of a shortest path from the root
|
deba@1912
|
759 |
/// to \c v. It is \ref INVALID if \c v is unreachable from the root or
|
deba@1912
|
760 |
/// if \c v=s. The shortest path tree used here is equal to the shortest
|
deba@1912
|
761 |
/// path tree used in \ref predNode().
|
deba@1912
|
762 |
/// \pre \ref run() must be called before using
|
deba@1912
|
763 |
/// this function.
|
deba@1912
|
764 |
Edge predEdge(Node v) const { return (*_pred)[v]; }
|
deba@1912
|
765 |
|
deba@1912
|
766 |
/// \brief Returns the 'previous node' of the shortest path tree.
|
deba@1912
|
767 |
///
|
deba@1912
|
768 |
/// For a node \c v it returns the 'previous node' of the shortest path
|
deba@1912
|
769 |
/// tree, i.e. it returns the last but one node from a shortest path from
|
deba@1912
|
770 |
/// the root to \c /v. It is INVALID if \c v is unreachable from the root
|
deba@1912
|
771 |
/// or if \c v=s. The shortest path tree used here is equal to the
|
deba@1912
|
772 |
/// shortest path tree used in \ref predEdge(). \pre \ref run() must be
|
deba@1912
|
773 |
/// called before using this function.
|
deba@1912
|
774 |
Node predNode(Node v) const {
|
deba@1912
|
775 |
return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]);
|
deba@1912
|
776 |
}
|
deba@1912
|
777 |
|
deba@1912
|
778 |
/// \brief Returns a reference to the NodeMap of distances.
|
deba@1912
|
779 |
///
|
deba@1912
|
780 |
/// Returns a reference to the NodeMap of distances. \pre \ref run() must
|
deba@1912
|
781 |
/// be called before using this function.
|
deba@1912
|
782 |
const DistMap &distMap() const { return *_dist;}
|
deba@1912
|
783 |
|
deba@1912
|
784 |
/// \brief Returns a reference to the shortest path tree map.
|
deba@1912
|
785 |
///
|
deba@1912
|
786 |
/// Returns a reference to the NodeMap of the edges of the
|
deba@1912
|
787 |
/// shortest path tree.
|
deba@1912
|
788 |
/// \pre \ref run() must be called before using this function.
|
deba@1912
|
789 |
const PredMap &predMap() const { return *_pred; }
|
deba@1912
|
790 |
|
deba@1912
|
791 |
/// \brief Checks if a node is reachable from the root.
|
deba@1912
|
792 |
///
|
deba@1912
|
793 |
/// Returns \c true if \c v is reachable from the root.
|
deba@1912
|
794 |
/// \pre \ref run() must be called before using this function.
|
deba@1912
|
795 |
///
|
deba@1912
|
796 |
bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); }
|
deba@1912
|
797 |
|
deba@1912
|
798 |
///@}
|
deba@1912
|
799 |
};
|
deba@1912
|
800 |
|
deba@1912
|
801 |
/// \brief Default traits class of DagShortestPath function.
|
deba@1912
|
802 |
///
|
deba@1912
|
803 |
/// Default traits class of DagShortestPath function.
|
deba@1912
|
804 |
/// \param _Graph Graph type.
|
deba@1912
|
805 |
/// \param _LengthMap Type of length map.
|
deba@1912
|
806 |
template <typename _Graph, typename _LengthMap>
|
deba@1912
|
807 |
struct DagShortestPathWizardDefaultTraits {
|
deba@1912
|
808 |
/// \brief The graph type the algorithm runs on.
|
deba@1912
|
809 |
typedef _Graph Graph;
|
deba@1912
|
810 |
|
deba@1912
|
811 |
/// \brief The type of the map that stores the edge lengths.
|
deba@1912
|
812 |
///
|
deba@1912
|
813 |
/// The type of the map that stores the edge lengths.
|
alpar@2260
|
814 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
deba@1912
|
815 |
typedef _LengthMap LengthMap;
|
deba@1912
|
816 |
|
deba@1912
|
817 |
/// \brief The value type of the length map.
|
deba@1912
|
818 |
typedef typename _LengthMap::Value Value;
|
deba@1912
|
819 |
|
deba@1912
|
820 |
/// \brief Operation traits for dag shortest path algorithm.
|
deba@1912
|
821 |
///
|
deba@1912
|
822 |
/// It defines the infinity type on the given Value type
|
deba@1912
|
823 |
/// and the used operation.
|
deba@1912
|
824 |
/// \see DagShortestPathDefaultOperationTraits
|
deba@1912
|
825 |
typedef DagShortestPathDefaultOperationTraits<Value> OperationTraits;
|
deba@1912
|
826 |
|
deba@1912
|
827 |
/// \brief The type of the map that stores the last
|
deba@1912
|
828 |
/// edges of the shortest paths.
|
deba@1912
|
829 |
///
|
deba@1912
|
830 |
/// The type of the map that stores the last
|
deba@1912
|
831 |
/// edges of the shortest paths.
|
alpar@2260
|
832 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
deba@1912
|
833 |
typedef NullMap <typename _Graph::Node,typename _Graph::Edge> PredMap;
|
deba@1912
|
834 |
|
deba@1912
|
835 |
/// \brief Instantiates a PredMap.
|
deba@1912
|
836 |
///
|
deba@1912
|
837 |
/// This function instantiates a \ref PredMap.
|
deba@1912
|
838 |
static PredMap *createPredMap(const _Graph &) {
|
deba@1912
|
839 |
return new PredMap();
|
deba@1912
|
840 |
}
|
deba@1912
|
841 |
/// \brief The type of the map that stores the dists of the nodes.
|
deba@1912
|
842 |
///
|
deba@1912
|
843 |
/// The type of the map that stores the dists of the nodes.
|
alpar@2260
|
844 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
deba@1912
|
845 |
typedef NullMap<typename Graph::Node, Value> DistMap;
|
deba@1912
|
846 |
/// \brief Instantiates a DistMap.
|
deba@1912
|
847 |
///
|
deba@1912
|
848 |
/// This function instantiates a \ref DistMap.
|
deba@1912
|
849 |
static DistMap *createDistMap(const _Graph &) {
|
deba@1912
|
850 |
return new DistMap();
|
deba@1912
|
851 |
}
|
deba@1912
|
852 |
};
|
deba@1912
|
853 |
|
deba@1912
|
854 |
/// \brief Default traits used by \ref DagShortestPathWizard
|
deba@1912
|
855 |
///
|
deba@1912
|
856 |
/// To make it easier to use DagShortestPath algorithm
|
deba@1912
|
857 |
/// we have created a wizard class.
|
deba@1912
|
858 |
/// This \ref DagShortestPathWizard class needs default traits,
|
deba@1912
|
859 |
/// as well as the \ref DagShortestPath class.
|
deba@1912
|
860 |
/// The \ref DagShortestPathWizardBase is a class to be the default traits of the
|
deba@1912
|
861 |
/// \ref DagShortestPathWizard class.
|
deba@1912
|
862 |
/// \todo More named parameters are required...
|
deba@1912
|
863 |
template<class _Graph,class _LengthMap>
|
deba@1912
|
864 |
class DagShortestPathWizardBase
|
deba@1912
|
865 |
: public DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> {
|
deba@1912
|
866 |
|
deba@1912
|
867 |
typedef DagShortestPathWizardDefaultTraits<_Graph,_LengthMap> Base;
|
deba@1912
|
868 |
protected:
|
deba@1912
|
869 |
/// Type of the nodes in the graph.
|
deba@1912
|
870 |
typedef typename Base::Graph::Node Node;
|
deba@1912
|
871 |
|
deba@1912
|
872 |
/// Pointer to the underlying graph.
|
deba@1912
|
873 |
void *_graph;
|
deba@1912
|
874 |
/// Pointer to the length map
|
deba@1912
|
875 |
void *_length;
|
deba@1912
|
876 |
///Pointer to the map of predecessors edges.
|
deba@1912
|
877 |
void *_pred;
|
deba@1912
|
878 |
///Pointer to the map of distances.
|
deba@1912
|
879 |
void *_dist;
|
deba@1912
|
880 |
///Pointer to the source node.
|
deba@1912
|
881 |
Node _source;
|
deba@1912
|
882 |
|
deba@1912
|
883 |
public:
|
deba@1912
|
884 |
/// Constructor.
|
deba@1912
|
885 |
|
deba@1912
|
886 |
/// This constructor does not require parameters, therefore it initiates
|
deba@1912
|
887 |
/// all of the attributes to default values (0, INVALID).
|
deba@1912
|
888 |
DagShortestPathWizardBase() : _graph(0), _length(0), _pred(0),
|
deba@1912
|
889 |
_dist(0), _source(INVALID) {}
|
deba@1912
|
890 |
|
deba@1912
|
891 |
/// Constructor.
|
deba@1912
|
892 |
|
deba@1912
|
893 |
/// This constructor requires some parameters,
|
deba@1912
|
894 |
/// listed in the parameters list.
|
deba@1912
|
895 |
/// Others are initiated to 0.
|
deba@1912
|
896 |
/// \param graph is the initial value of \ref _graph
|
deba@1912
|
897 |
/// \param length is the initial value of \ref _length
|
deba@1912
|
898 |
/// \param source is the initial value of \ref _source
|
deba@1912
|
899 |
DagShortestPathWizardBase(const _Graph& graph,
|
deba@1912
|
900 |
const _LengthMap& length,
|
deba@1912
|
901 |
Node source = INVALID) :
|
deba@1912
|
902 |
_graph((void *)&graph), _length((void *)&length), _pred(0),
|
deba@1912
|
903 |
_dist(0), _source(source) {}
|
deba@1912
|
904 |
|
deba@1912
|
905 |
};
|
deba@1912
|
906 |
|
deba@1912
|
907 |
/// A class to make the usage of DagShortestPath algorithm easier
|
deba@1912
|
908 |
|
deba@1912
|
909 |
/// This class is created to make it easier to use DagShortestPath algorithm.
|
deba@1912
|
910 |
/// It uses the functions and features of the plain \ref DagShortestPath,
|
deba@1912
|
911 |
/// but it is much simpler to use it.
|
deba@1912
|
912 |
///
|
deba@1912
|
913 |
/// Simplicity means that the way to change the types defined
|
deba@1912
|
914 |
/// in the traits class is based on functions that returns the new class
|
deba@1912
|
915 |
/// and not on templatable built-in classes.
|
deba@1912
|
916 |
/// When using the plain \ref DagShortestPath
|
deba@1912
|
917 |
/// the new class with the modified type comes from
|
deba@1912
|
918 |
/// the original class by using the ::
|
deba@1912
|
919 |
/// operator. In the case of \ref DagShortestPathWizard only
|
deba@1912
|
920 |
/// a function have to be called and it will
|
deba@1912
|
921 |
/// return the needed class.
|
deba@1912
|
922 |
///
|
deba@1912
|
923 |
/// It does not have own \ref run method. When its \ref run method is called
|
deba@1912
|
924 |
/// it initiates a plain \ref DagShortestPath class, and calls the \ref
|
deba@1912
|
925 |
/// DagShortestPath::run() method of it.
|
deba@1912
|
926 |
template<class _Traits>
|
deba@1912
|
927 |
class DagShortestPathWizard : public _Traits {
|
deba@1912
|
928 |
typedef _Traits Base;
|
deba@1912
|
929 |
|
deba@1912
|
930 |
///The type of the underlying graph.
|
deba@1912
|
931 |
typedef typename _Traits::Graph Graph;
|
deba@1912
|
932 |
|
deba@1912
|
933 |
typedef typename Graph::Node Node;
|
deba@1912
|
934 |
typedef typename Graph::NodeIt NodeIt;
|
deba@1912
|
935 |
typedef typename Graph::Edge Edge;
|
deba@1912
|
936 |
typedef typename Graph::OutEdgeIt EdgeIt;
|
deba@1912
|
937 |
|
deba@1912
|
938 |
///The type of the map that stores the edge lengths.
|
deba@1912
|
939 |
typedef typename _Traits::LengthMap LengthMap;
|
deba@1912
|
940 |
|
deba@1912
|
941 |
///The type of the length of the edges.
|
deba@1912
|
942 |
typedef typename LengthMap::Value Value;
|
deba@1912
|
943 |
|
deba@1912
|
944 |
///\brief The type of the map that stores the last
|
deba@1912
|
945 |
///edges of the shortest paths.
|
deba@1912
|
946 |
typedef typename _Traits::PredMap PredMap;
|
deba@1912
|
947 |
|
deba@1912
|
948 |
///The type of the map that stores the dists of the nodes.
|
deba@1912
|
949 |
typedef typename _Traits::DistMap DistMap;
|
deba@1912
|
950 |
|
deba@1912
|
951 |
public:
|
deba@1912
|
952 |
/// Constructor.
|
deba@1912
|
953 |
DagShortestPathWizard() : _Traits() {}
|
deba@1912
|
954 |
|
deba@1912
|
955 |
/// \brief Constructor that requires parameters.
|
deba@1912
|
956 |
///
|
deba@1912
|
957 |
/// Constructor that requires parameters.
|
deba@1912
|
958 |
/// These parameters will be the default values for the traits class.
|
deba@1912
|
959 |
DagShortestPathWizard(const Graph& graph, const LengthMap& length,
|
deba@1912
|
960 |
Node source = INVALID)
|
deba@1912
|
961 |
: _Traits(graph, length, source) {}
|
deba@1912
|
962 |
|
deba@1912
|
963 |
/// \brief Copy constructor
|
deba@1912
|
964 |
DagShortestPathWizard(const _Traits &b) : _Traits(b) {}
|
deba@1912
|
965 |
|
deba@1912
|
966 |
~DagShortestPathWizard() {}
|
deba@1912
|
967 |
|
deba@1912
|
968 |
/// \brief Runs DagShortestPath algorithm from a given node.
|
deba@1912
|
969 |
///
|
deba@1912
|
970 |
/// Runs DagShortestPath algorithm from a given node.
|
deba@1912
|
971 |
/// The node can be given by the \ref source function.
|
deba@1912
|
972 |
void run() {
|
deba@1912
|
973 |
if(Base::_source == INVALID) throw UninitializedParameter();
|
deba@1912
|
974 |
DagShortestPath<Graph,LengthMap,_Traits>
|
deba@1912
|
975 |
bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length);
|
deba@1912
|
976 |
if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred);
|
deba@1912
|
977 |
if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist);
|
deba@1912
|
978 |
bf.run(Base::_source);
|
deba@1912
|
979 |
}
|
deba@1912
|
980 |
|
deba@1912
|
981 |
/// \brief Runs DagShortestPath algorithm from the given node.
|
deba@1912
|
982 |
///
|
deba@1912
|
983 |
/// Runs DagShortestPath algorithm from the given node.
|
alpar@1946
|
984 |
/// \param source is the given source.
|
deba@1912
|
985 |
void run(Node source) {
|
deba@1912
|
986 |
Base::_source = source;
|
deba@1912
|
987 |
run();
|
deba@1912
|
988 |
}
|
deba@1912
|
989 |
|
deba@1912
|
990 |
template<class T>
|
deba@1912
|
991 |
struct DefPredMapBase : public Base {
|
deba@1912
|
992 |
typedef T PredMap;
|
deba@1912
|
993 |
static PredMap *createPredMap(const Graph &) { return 0; };
|
deba@1912
|
994 |
DefPredMapBase(const _Traits &b) : _Traits(b) {}
|
deba@1912
|
995 |
};
|
deba@1912
|
996 |
|
deba@1912
|
997 |
///\brief \ref named-templ-param "Named parameter"
|
deba@1912
|
998 |
///function for setting PredMap type
|
deba@1912
|
999 |
///
|
deba@1912
|
1000 |
/// \ref named-templ-param "Named parameter"
|
deba@1912
|
1001 |
///function for setting PredMap type
|
deba@1912
|
1002 |
///
|
deba@1912
|
1003 |
template<class T>
|
deba@1912
|
1004 |
DagShortestPathWizard<DefPredMapBase<T> > predMap(const T &t)
|
deba@1912
|
1005 |
{
|
deba@1912
|
1006 |
Base::_pred=(void *)&t;
|
deba@1912
|
1007 |
return DagShortestPathWizard<DefPredMapBase<T> >(*this);
|
deba@1912
|
1008 |
}
|
deba@1912
|
1009 |
|
deba@1912
|
1010 |
template<class T>
|
deba@1912
|
1011 |
struct DefDistMapBase : public Base {
|
deba@1912
|
1012 |
typedef T DistMap;
|
deba@1912
|
1013 |
static DistMap *createDistMap(const Graph &) { return 0; };
|
deba@1912
|
1014 |
DefDistMapBase(const _Traits &b) : _Traits(b) {}
|
deba@1912
|
1015 |
};
|
deba@1912
|
1016 |
|
deba@1912
|
1017 |
///\brief \ref named-templ-param "Named parameter"
|
deba@1912
|
1018 |
///function for setting DistMap type
|
deba@1912
|
1019 |
///
|
deba@1912
|
1020 |
/// \ref named-templ-param "Named parameter"
|
deba@1912
|
1021 |
///function for setting DistMap type
|
deba@1912
|
1022 |
///
|
deba@1912
|
1023 |
template<class T>
|
deba@1912
|
1024 |
DagShortestPathWizard<DefDistMapBase<T> > distMap(const T &t) {
|
deba@1912
|
1025 |
Base::_dist=(void *)&t;
|
deba@1912
|
1026 |
return DagShortestPathWizard<DefDistMapBase<T> >(*this);
|
deba@1912
|
1027 |
}
|
deba@1912
|
1028 |
|
deba@1912
|
1029 |
template<class T>
|
deba@1912
|
1030 |
struct DefOperationTraitsBase : public Base {
|
deba@1912
|
1031 |
typedef T OperationTraits;
|
deba@1912
|
1032 |
DefOperationTraitsBase(const _Traits &b) : _Traits(b) {}
|
deba@1912
|
1033 |
};
|
deba@1912
|
1034 |
|
deba@1912
|
1035 |
///\brief \ref named-templ-param "Named parameter"
|
deba@1912
|
1036 |
///function for setting OperationTraits type
|
deba@1912
|
1037 |
///
|
deba@1912
|
1038 |
/// \ref named-templ-param "Named parameter"
|
deba@1912
|
1039 |
///function for setting OperationTraits type
|
deba@1912
|
1040 |
///
|
deba@1912
|
1041 |
template<class T>
|
deba@1912
|
1042 |
DagShortestPathWizard<DefOperationTraitsBase<T> > distMap() {
|
deba@1912
|
1043 |
return DagShortestPathWizard<DefDistMapBase<T> >(*this);
|
deba@1912
|
1044 |
}
|
deba@1912
|
1045 |
|
deba@1912
|
1046 |
/// \brief Sets the source node, from which the DagShortestPath algorithm runs.
|
deba@1912
|
1047 |
///
|
deba@1912
|
1048 |
/// Sets the source node, from which the DagShortestPath algorithm runs.
|
alpar@1946
|
1049 |
/// \param source is the source node.
|
deba@1912
|
1050 |
DagShortestPathWizard<_Traits>& source(Node source) {
|
deba@1912
|
1051 |
Base::_source = source;
|
deba@1912
|
1052 |
return *this;
|
deba@1912
|
1053 |
}
|
deba@1912
|
1054 |
|
deba@1912
|
1055 |
};
|
deba@1912
|
1056 |
|
deba@1912
|
1057 |
/// \brief Function type interface for DagShortestPath algorithm.
|
deba@1912
|
1058 |
///
|
deba@1912
|
1059 |
/// \ingroup flowalgs
|
deba@1912
|
1060 |
/// Function type interface for DagShortestPath algorithm.
|
deba@1912
|
1061 |
///
|
deba@1912
|
1062 |
/// This function also has several \ref named-templ-func-param
|
deba@1912
|
1063 |
/// "named parameters", they are declared as the members of class
|
deba@1912
|
1064 |
/// \ref DagShortestPathWizard.
|
deba@1912
|
1065 |
/// The following
|
deba@1912
|
1066 |
/// example shows how to use these parameters.
|
alpar@1946
|
1067 |
///\code
|
deba@1912
|
1068 |
/// dagShortestPath(g,length,source).predMap(preds).run();
|
alpar@1946
|
1069 |
///\endcode
|
deba@1912
|
1070 |
/// \warning Don't forget to put the \ref DagShortestPathWizard::run() "run()"
|
deba@1912
|
1071 |
/// to the end of the parameter list.
|
deba@1912
|
1072 |
/// \sa DagShortestPathWizard
|
deba@1912
|
1073 |
/// \sa DagShortestPath
|
deba@1912
|
1074 |
template<class _Graph, class _LengthMap>
|
deba@1912
|
1075 |
DagShortestPathWizard<DagShortestPathWizardBase<_Graph,_LengthMap> >
|
deba@1912
|
1076 |
dagShortestPath(const _Graph& graph,
|
deba@1912
|
1077 |
const _LengthMap& length,
|
deba@1912
|
1078 |
typename _Graph::Node source = INVALID) {
|
deba@1912
|
1079 |
return DagShortestPathWizard<DagShortestPathWizardBase<_Graph,_LengthMap> >
|
deba@1912
|
1080 |
(graph, length, source);
|
deba@1912
|
1081 |
}
|
deba@1912
|
1082 |
|
deba@1912
|
1083 |
} //END OF NAMESPACE LEMON
|
deba@1912
|
1084 |
|
deba@1912
|
1085 |
#endif
|
deba@1912
|
1086 |
|