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