deba@1699
|
1 |
/* -*- C++ -*-
|
deba@1699
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@2553
|
5 |
* Copyright (C) 2003-2008
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@1699
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@1699
|
8 |
*
|
deba@1699
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@1699
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@1699
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@1699
|
12 |
*
|
deba@1699
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@1699
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@1699
|
15 |
* purpose.
|
deba@1699
|
16 |
*
|
deba@1699
|
17 |
*/
|
deba@1699
|
18 |
|
deba@1699
|
19 |
#ifndef LEMON_FLOYD_WARSHALL_H
|
deba@1699
|
20 |
#define LEMON_FLOYD_WARSHALL_H
|
deba@1699
|
21 |
|
deba@2376
|
22 |
///\ingroup shortest_path
|
deba@1699
|
23 |
/// \file
|
deba@1699
|
24 |
/// \brief FloydWarshall algorithm.
|
deba@1699
|
25 |
///
|
deba@1699
|
26 |
|
deba@1699
|
27 |
#include <lemon/list_graph.h>
|
deba@1699
|
28 |
#include <lemon/graph_utils.h>
|
deba@2335
|
29 |
#include <lemon/bits/path_dump.h>
|
deba@1993
|
30 |
#include <lemon/bits/invalid.h>
|
deba@1699
|
31 |
#include <lemon/error.h>
|
deba@1723
|
32 |
#include <lemon/matrix_maps.h>
|
deba@1699
|
33 |
#include <lemon/maps.h>
|
deba@1699
|
34 |
|
deba@1699
|
35 |
#include <limits>
|
deba@1699
|
36 |
|
deba@1699
|
37 |
namespace lemon {
|
deba@1699
|
38 |
|
deba@1699
|
39 |
/// \brief Default OperationTraits for the FloydWarshall algorithm class.
|
deba@1699
|
40 |
///
|
deba@1699
|
41 |
/// It defines all computational operations and constants which are
|
deba@1699
|
42 |
/// used in the Floyd-Warshall algorithm. The default implementation
|
deba@1699
|
43 |
/// is based on the numeric_limits class. If the numeric type does not
|
deba@1699
|
44 |
/// have infinity value then the maximum value is used as extremal
|
deba@1699
|
45 |
/// infinity value.
|
deba@1699
|
46 |
template <
|
deba@1699
|
47 |
typename Value,
|
deba@1699
|
48 |
bool has_infinity = std::numeric_limits<Value>::has_infinity>
|
deba@1699
|
49 |
struct FloydWarshallDefaultOperationTraits {
|
deba@1699
|
50 |
/// \brief Gives back the zero value of the type.
|
deba@1699
|
51 |
static Value zero() {
|
deba@1699
|
52 |
return static_cast<Value>(0);
|
deba@1699
|
53 |
}
|
deba@1699
|
54 |
/// \brief Gives back the positive infinity value of the type.
|
deba@1699
|
55 |
static Value infinity() {
|
deba@1699
|
56 |
return std::numeric_limits<Value>::infinity();
|
deba@1699
|
57 |
}
|
deba@1699
|
58 |
/// \brief Gives back the sum of the given two elements.
|
deba@1699
|
59 |
static Value plus(const Value& left, const Value& right) {
|
deba@1699
|
60 |
return left + right;
|
deba@1699
|
61 |
}
|
deba@1699
|
62 |
/// \brief Gives back true only if the first value less than the second.
|
deba@1699
|
63 |
static bool less(const Value& left, const Value& right) {
|
deba@1699
|
64 |
return left < right;
|
deba@1699
|
65 |
}
|
deba@1699
|
66 |
};
|
deba@1699
|
67 |
|
deba@1699
|
68 |
template <typename Value>
|
deba@1699
|
69 |
struct FloydWarshallDefaultOperationTraits<Value, false> {
|
deba@1699
|
70 |
static Value zero() {
|
deba@1699
|
71 |
return static_cast<Value>(0);
|
deba@1699
|
72 |
}
|
deba@1699
|
73 |
static Value infinity() {
|
deba@1699
|
74 |
return std::numeric_limits<Value>::max();
|
deba@1699
|
75 |
}
|
deba@1699
|
76 |
static Value plus(const Value& left, const Value& right) {
|
deba@1699
|
77 |
if (left == infinity() || right == infinity()) return infinity();
|
deba@1699
|
78 |
return left + right;
|
deba@1699
|
79 |
}
|
deba@1699
|
80 |
static bool less(const Value& left, const Value& right) {
|
deba@1699
|
81 |
return left < right;
|
deba@1699
|
82 |
}
|
deba@1699
|
83 |
};
|
deba@1699
|
84 |
|
deba@1699
|
85 |
/// \brief Default traits class of FloydWarshall class.
|
deba@1699
|
86 |
///
|
deba@1699
|
87 |
/// Default traits class of FloydWarshall class.
|
deba@1699
|
88 |
/// \param _Graph Graph type.
|
deba@1699
|
89 |
/// \param _LegthMap Type of length map.
|
deba@1699
|
90 |
template<class _Graph, class _LengthMap>
|
deba@1699
|
91 |
struct FloydWarshallDefaultTraits {
|
deba@1699
|
92 |
/// The graph type the algorithm runs on.
|
deba@1699
|
93 |
typedef _Graph Graph;
|
deba@1699
|
94 |
|
deba@1699
|
95 |
/// \brief The type of the map that stores the edge lengths.
|
deba@1699
|
96 |
///
|
deba@1699
|
97 |
/// The type of the map that stores the edge lengths.
|
alpar@2260
|
98 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
deba@1699
|
99 |
typedef _LengthMap LengthMap;
|
deba@1699
|
100 |
|
deba@1699
|
101 |
// The type of the length of the edges.
|
deba@1699
|
102 |
typedef typename _LengthMap::Value Value;
|
deba@1699
|
103 |
|
deba@1865
|
104 |
/// \brief Operation traits for floyd-warshall algorithm.
|
deba@1699
|
105 |
///
|
deba@1699
|
106 |
/// It defines the infinity type on the given Value type
|
deba@1699
|
107 |
/// and the used operation.
|
deba@1699
|
108 |
/// \see FloydWarshallDefaultOperationTraits
|
deba@1699
|
109 |
typedef FloydWarshallDefaultOperationTraits<Value> OperationTraits;
|
deba@1699
|
110 |
|
deba@1723
|
111 |
/// \brief The type of the matrix map that stores the last edges of the
|
deba@1699
|
112 |
/// shortest paths.
|
deba@1699
|
113 |
///
|
deba@1723
|
114 |
/// The type of the map that stores the last edges of the shortest paths.
|
deba@1699
|
115 |
/// It must be a matrix map with \c Graph::Edge value type.
|
deba@1699
|
116 |
///
|
deba@1723
|
117 |
typedef DynamicMatrixMap<Graph, typename Graph::Node,
|
deba@1723
|
118 |
typename Graph::Edge> PredMap;
|
deba@1699
|
119 |
|
deba@1699
|
120 |
/// \brief Instantiates a PredMap.
|
deba@1699
|
121 |
///
|
deba@1699
|
122 |
/// This function instantiates a \ref PredMap.
|
alpar@1946
|
123 |
/// \param graph is the graph,
|
alpar@1946
|
124 |
/// to which we would like to define the PredMap.
|
deba@1699
|
125 |
/// \todo The graph alone may be insufficient for the initialization
|
deba@1699
|
126 |
static PredMap *createPredMap(const _Graph& graph) {
|
deba@1699
|
127 |
return new PredMap(graph);
|
deba@1699
|
128 |
}
|
deba@1699
|
129 |
|
deba@1699
|
130 |
/// \brief The type of the map that stores the dists of the nodes.
|
deba@1699
|
131 |
///
|
deba@1699
|
132 |
/// The type of the map that stores the dists of the nodes.
|
alpar@2260
|
133 |
/// It must meet the \ref concepts::WriteMatrixMap "WriteMatrixMap" concept.
|
deba@1699
|
134 |
///
|
deba@1723
|
135 |
typedef DynamicMatrixMap<Graph, typename Graph::Node, Value> DistMap;
|
deba@1699
|
136 |
|
deba@1699
|
137 |
/// \brief Instantiates a DistMap.
|
deba@1699
|
138 |
///
|
deba@1699
|
139 |
/// This function instantiates a \ref DistMap.
|
alpar@1946
|
140 |
/// \param graph is the graph, to which we would like to define the
|
deba@1699
|
141 |
/// \ref DistMap
|
deba@1699
|
142 |
static DistMap *createDistMap(const _Graph& graph) {
|
deba@1699
|
143 |
return new DistMap(graph);
|
deba@1699
|
144 |
}
|
deba@1699
|
145 |
|
deba@1699
|
146 |
};
|
deba@1699
|
147 |
|
deba@1754
|
148 |
/// \brief %FloydWarshall algorithm class.
|
deba@1699
|
149 |
///
|
deba@2376
|
150 |
/// \ingroup shortest_path
|
deba@1754
|
151 |
/// This class provides an efficient implementation of \c Floyd-Warshall
|
deba@1699
|
152 |
/// algorithm. The edge lengths are passed to the algorithm using a
|
alpar@2260
|
153 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any
|
deba@1699
|
154 |
/// kind of length.
|
deba@1699
|
155 |
///
|
alpar@1757
|
156 |
/// The algorithm solves the shortest path problem for each pair
|
deba@1723
|
157 |
/// of node when the edges can have negative length but the graph should
|
deba@1754
|
158 |
/// not contain cycles with negative sum of length. If we can assume
|
deba@1723
|
159 |
/// that all edge is non-negative in the graph then the dijkstra algorithm
|
deba@1723
|
160 |
/// should be used from each node rather and if the graph is sparse and
|
deba@1754
|
161 |
/// there are negative circles then the johnson algorithm.
|
deba@1723
|
162 |
///
|
deba@2042
|
163 |
/// The complexity of this algorithm is \f$ O(n^3+e) \f$.
|
deba@1723
|
164 |
///
|
deba@1699
|
165 |
/// The type of the length is determined by the
|
alpar@2260
|
166 |
/// \ref concepts::ReadMap::Value "Value" of the length map.
|
deba@1699
|
167 |
///
|
deba@1699
|
168 |
/// \param _Graph The graph type the algorithm runs on. The default value
|
deba@1699
|
169 |
/// is \ref ListGraph. The value of _Graph is not used directly by
|
deba@1699
|
170 |
/// FloydWarshall, it is only passed to \ref FloydWarshallDefaultTraits.
|
deba@1699
|
171 |
/// \param _LengthMap This read-only EdgeMap determines the lengths of the
|
deba@1699
|
172 |
/// edges. It is read once for each edge, so the map may involve in
|
deba@1699
|
173 |
/// relatively time consuming process to compute the edge length if
|
deba@1699
|
174 |
/// it is necessary. The default map type is \ref
|
alpar@2260
|
175 |
/// concepts::Graph::EdgeMap "Graph::EdgeMap<int>". The value
|
deba@1699
|
176 |
/// of _LengthMap is not used directly by FloydWarshall, it is only passed
|
deba@1699
|
177 |
/// to \ref FloydWarshallDefaultTraits. \param _Traits Traits class to set
|
deba@1699
|
178 |
/// various data types used by the algorithm. The default traits
|
deba@1699
|
179 |
/// class is \ref FloydWarshallDefaultTraits
|
deba@1699
|
180 |
/// "FloydWarshallDefaultTraits<_Graph,_LengthMap>". See \ref
|
deba@1699
|
181 |
/// FloydWarshallDefaultTraits for the documentation of a FloydWarshall
|
deba@1699
|
182 |
/// traits class.
|
deba@1699
|
183 |
///
|
deba@1699
|
184 |
/// \author Balazs Dezso
|
alpar@2184
|
185 |
/// \todo A function type interface would be nice.
|
alpar@2184
|
186 |
/// \todo Implement \c nextNode() and \c nextEdge()
|
deba@1710
|
187 |
#ifdef DOXYGEN
|
alpar@2184
|
188 |
template <typename _Graph, typename _LengthMap, typename _Traits >
|
deba@1710
|
189 |
#else
|
deba@1699
|
190 |
template <typename _Graph=ListGraph,
|
deba@1699
|
191 |
typename _LengthMap=typename _Graph::template EdgeMap<int>,
|
deba@1699
|
192 |
typename _Traits=FloydWarshallDefaultTraits<_Graph,_LengthMap> >
|
deba@1710
|
193 |
#endif
|
deba@1699
|
194 |
class FloydWarshall {
|
deba@1699
|
195 |
public:
|
deba@1699
|
196 |
|
deba@1699
|
197 |
/// \brief \ref Exception for uninitialized parameters.
|
deba@1699
|
198 |
///
|
deba@1699
|
199 |
/// This error represents problems in the initialization
|
deba@1699
|
200 |
/// of the parameters of the algorithms.
|
deba@1699
|
201 |
|
deba@1699
|
202 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
deba@1699
|
203 |
public:
|
alpar@2151
|
204 |
virtual const char* what() const throw() {
|
deba@1699
|
205 |
return "lemon::FloydWarshall::UninitializedParameter";
|
deba@1699
|
206 |
}
|
deba@1699
|
207 |
};
|
deba@1699
|
208 |
|
deba@1699
|
209 |
typedef _Traits Traits;
|
deba@1699
|
210 |
///The type of the underlying graph.
|
deba@1699
|
211 |
typedef typename _Traits::Graph Graph;
|
deba@1699
|
212 |
|
deba@1699
|
213 |
typedef typename Graph::Node Node;
|
deba@1699
|
214 |
typedef typename Graph::NodeIt NodeIt;
|
deba@1699
|
215 |
typedef typename Graph::Edge Edge;
|
deba@1699
|
216 |
typedef typename Graph::EdgeIt EdgeIt;
|
deba@1699
|
217 |
|
deba@1699
|
218 |
/// \brief The type of the length of the edges.
|
deba@1699
|
219 |
typedef typename _Traits::LengthMap::Value Value;
|
deba@1699
|
220 |
/// \brief The type of the map that stores the edge lengths.
|
deba@1699
|
221 |
typedef typename _Traits::LengthMap LengthMap;
|
deba@1699
|
222 |
/// \brief The type of the map that stores the last
|
deba@1699
|
223 |
/// edges of the shortest paths. The type of the PredMap
|
deba@1699
|
224 |
/// is a matrix map for Edges
|
deba@1699
|
225 |
typedef typename _Traits::PredMap PredMap;
|
deba@1699
|
226 |
/// \brief The type of the map that stores the dists of the nodes.
|
deba@1699
|
227 |
/// The type of the DistMap is a matrix map for Values
|
alpar@2184
|
228 |
///
|
alpar@2184
|
229 |
/// \todo It should rather be
|
alpar@2184
|
230 |
/// called \c DistMatrix
|
deba@1699
|
231 |
typedef typename _Traits::DistMap DistMap;
|
deba@1699
|
232 |
/// \brief The operation traits.
|
deba@1699
|
233 |
typedef typename _Traits::OperationTraits OperationTraits;
|
deba@1699
|
234 |
private:
|
deba@1699
|
235 |
/// Pointer to the underlying graph.
|
deba@1699
|
236 |
const Graph *graph;
|
deba@1699
|
237 |
/// Pointer to the length map
|
deba@1699
|
238 |
const LengthMap *length;
|
deba@1699
|
239 |
///Pointer to the map of predecessors edges.
|
deba@1699
|
240 |
PredMap *_pred;
|
deba@1699
|
241 |
///Indicates if \ref _pred is locally allocated (\c true) or not.
|
deba@1699
|
242 |
bool local_pred;
|
deba@1699
|
243 |
///Pointer to the map of distances.
|
deba@1699
|
244 |
DistMap *_dist;
|
deba@1699
|
245 |
///Indicates if \ref _dist is locally allocated (\c true) or not.
|
deba@1699
|
246 |
bool local_dist;
|
deba@1699
|
247 |
|
deba@1699
|
248 |
/// Creates the maps if necessary.
|
deba@1699
|
249 |
void create_maps() {
|
deba@1699
|
250 |
if(!_pred) {
|
deba@1699
|
251 |
local_pred = true;
|
deba@1699
|
252 |
_pred = Traits::createPredMap(*graph);
|
deba@1699
|
253 |
}
|
deba@1699
|
254 |
if(!_dist) {
|
deba@1699
|
255 |
local_dist = true;
|
deba@1699
|
256 |
_dist = Traits::createDistMap(*graph);
|
deba@1699
|
257 |
}
|
deba@1699
|
258 |
}
|
deba@1699
|
259 |
|
deba@1699
|
260 |
public :
|
deba@1699
|
261 |
|
deba@1699
|
262 |
/// \name Named template parameters
|
deba@1699
|
263 |
|
deba@1699
|
264 |
///@{
|
deba@1699
|
265 |
|
deba@1699
|
266 |
template <class T>
|
deba@1699
|
267 |
struct DefPredMapTraits : public Traits {
|
deba@1699
|
268 |
typedef T PredMap;
|
deba@1699
|
269 |
static PredMap *createPredMap(const Graph& graph) {
|
deba@1699
|
270 |
throw UninitializedParameter();
|
deba@1699
|
271 |
}
|
deba@1699
|
272 |
};
|
deba@1699
|
273 |
|
deba@1699
|
274 |
/// \brief \ref named-templ-param "Named parameter" for setting PredMap
|
deba@1699
|
275 |
/// type
|
deba@1699
|
276 |
/// \ref named-templ-param "Named parameter" for setting PredMap type
|
deba@1699
|
277 |
///
|
deba@1699
|
278 |
template <class T>
|
deba@1710
|
279 |
struct DefPredMap
|
deba@1710
|
280 |
: public FloydWarshall< Graph, LengthMap, DefPredMapTraits<T> > {
|
deba@1710
|
281 |
typedef FloydWarshall< Graph, LengthMap, DefPredMapTraits<T> > Create;
|
deba@1710
|
282 |
};
|
deba@1699
|
283 |
|
deba@1699
|
284 |
template <class T>
|
deba@1699
|
285 |
struct DefDistMapTraits : public Traits {
|
deba@1699
|
286 |
typedef T DistMap;
|
deba@1699
|
287 |
static DistMap *createDistMap(const Graph& graph) {
|
deba@1699
|
288 |
throw UninitializedParameter();
|
deba@1699
|
289 |
}
|
deba@1699
|
290 |
};
|
deba@1699
|
291 |
/// \brief \ref named-templ-param "Named parameter" for setting DistMap
|
deba@1699
|
292 |
/// type
|
deba@1699
|
293 |
///
|
deba@1699
|
294 |
/// \ref named-templ-param "Named parameter" for setting DistMap type
|
deba@1699
|
295 |
///
|
deba@1699
|
296 |
template <class T>
|
deba@1710
|
297 |
struct DefDistMap
|
deba@1710
|
298 |
: public FloydWarshall< Graph, LengthMap, DefDistMapTraits<T> > {
|
deba@1710
|
299 |
typedef FloydWarshall< Graph, LengthMap, DefDistMapTraits<T> > Create;
|
deba@1710
|
300 |
};
|
deba@1699
|
301 |
|
deba@1699
|
302 |
template <class T>
|
deba@1699
|
303 |
struct DefOperationTraitsTraits : public Traits {
|
deba@1699
|
304 |
typedef T OperationTraits;
|
deba@1699
|
305 |
};
|
deba@1699
|
306 |
|
deba@1699
|
307 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
deba@1699
|
308 |
/// OperationTraits type
|
deba@1699
|
309 |
///
|
deba@1699
|
310 |
/// \ref named-templ-param "Named parameter" for setting PredMap type
|
deba@1699
|
311 |
template <class T>
|
deba@1710
|
312 |
struct DefOperationTraits
|
deba@1699
|
313 |
: public FloydWarshall< Graph, LengthMap, DefOperationTraitsTraits<T> > {
|
deba@1710
|
314 |
typedef FloydWarshall< Graph, LengthMap, DefOperationTraitsTraits<T> >
|
deba@1710
|
315 |
Create;
|
deba@1699
|
316 |
};
|
deba@1699
|
317 |
|
deba@1699
|
318 |
///@}
|
deba@1699
|
319 |
|
deba@1710
|
320 |
protected:
|
deba@1710
|
321 |
|
deba@1710
|
322 |
FloydWarshall() {}
|
deba@1710
|
323 |
|
deba@1699
|
324 |
public:
|
deba@1710
|
325 |
|
deba@1710
|
326 |
typedef FloydWarshall Create;
|
deba@1699
|
327 |
|
deba@1699
|
328 |
/// \brief Constructor.
|
deba@1699
|
329 |
///
|
deba@1699
|
330 |
/// \param _graph the graph the algorithm will run on.
|
deba@1699
|
331 |
/// \param _length the length map used by the algorithm.
|
deba@1699
|
332 |
FloydWarshall(const Graph& _graph, const LengthMap& _length) :
|
deba@1699
|
333 |
graph(&_graph), length(&_length),
|
deba@1699
|
334 |
_pred(0), local_pred(false),
|
deba@1699
|
335 |
_dist(0), local_dist(false) {}
|
deba@1699
|
336 |
|
deba@1699
|
337 |
///Destructor.
|
deba@1699
|
338 |
~FloydWarshall() {
|
deba@1699
|
339 |
if(local_pred) delete _pred;
|
deba@1699
|
340 |
if(local_dist) delete _dist;
|
deba@1699
|
341 |
}
|
deba@1699
|
342 |
|
deba@1699
|
343 |
/// \brief Sets the length map.
|
deba@1699
|
344 |
///
|
deba@1699
|
345 |
/// Sets the length map.
|
deba@1699
|
346 |
/// \return \c (*this)
|
deba@1699
|
347 |
FloydWarshall &lengthMap(const LengthMap &m) {
|
deba@1699
|
348 |
length = &m;
|
deba@1699
|
349 |
return *this;
|
deba@1699
|
350 |
}
|
deba@1699
|
351 |
|
deba@1699
|
352 |
/// \brief Sets the map storing the predecessor edges.
|
deba@1699
|
353 |
///
|
deba@1699
|
354 |
/// Sets the map storing the predecessor edges.
|
deba@1699
|
355 |
/// If you don't use this function before calling \ref run(),
|
deba@1699
|
356 |
/// it will allocate one. The destuctor deallocates this
|
deba@1699
|
357 |
/// automatically allocated map, of course.
|
deba@1699
|
358 |
/// \return \c (*this)
|
deba@1699
|
359 |
FloydWarshall &predMap(PredMap &m) {
|
deba@1699
|
360 |
if(local_pred) {
|
deba@1699
|
361 |
delete _pred;
|
deba@1699
|
362 |
local_pred=false;
|
deba@1699
|
363 |
}
|
deba@1699
|
364 |
_pred = &m;
|
deba@1699
|
365 |
return *this;
|
deba@1699
|
366 |
}
|
deba@1699
|
367 |
|
deba@1699
|
368 |
/// \brief Sets the map storing the distances calculated by the algorithm.
|
deba@1699
|
369 |
///
|
deba@1699
|
370 |
/// Sets the map storing the distances calculated by the algorithm.
|
deba@1699
|
371 |
/// If you don't use this function before calling \ref run(),
|
deba@1699
|
372 |
/// it will allocate one. The destuctor deallocates this
|
deba@1699
|
373 |
/// automatically allocated map, of course.
|
deba@1699
|
374 |
/// \return \c (*this)
|
deba@1699
|
375 |
FloydWarshall &distMap(DistMap &m) {
|
deba@1699
|
376 |
if(local_dist) {
|
deba@1699
|
377 |
delete _dist;
|
deba@1699
|
378 |
local_dist=false;
|
deba@1699
|
379 |
}
|
deba@1699
|
380 |
_dist = &m;
|
deba@1699
|
381 |
return *this;
|
deba@1699
|
382 |
}
|
deba@1699
|
383 |
|
deba@1699
|
384 |
///\name Execution control
|
deba@1699
|
385 |
/// The simplest way to execute the algorithm is to use
|
deba@1699
|
386 |
/// one of the member functions called \c run(...).
|
deba@1699
|
387 |
/// \n
|
deba@1699
|
388 |
/// If you need more control on the execution,
|
deba@1699
|
389 |
/// Finally \ref start() will perform the actual path
|
deba@1699
|
390 |
/// computation.
|
deba@1699
|
391 |
|
deba@1699
|
392 |
///@{
|
deba@1699
|
393 |
|
deba@1699
|
394 |
/// \brief Initializes the internal data structures.
|
deba@1699
|
395 |
///
|
deba@1699
|
396 |
/// Initializes the internal data structures.
|
deba@1699
|
397 |
void init() {
|
deba@1699
|
398 |
create_maps();
|
deba@1699
|
399 |
for (NodeIt it(*graph); it != INVALID; ++it) {
|
deba@1699
|
400 |
for (NodeIt jt(*graph); jt != INVALID; ++jt) {
|
deba@1699
|
401 |
_pred->set(it, jt, INVALID);
|
deba@1741
|
402 |
_dist->set(it, jt, OperationTraits::infinity());
|
deba@1699
|
403 |
}
|
deba@1741
|
404 |
_dist->set(it, it, OperationTraits::zero());
|
deba@1699
|
405 |
}
|
deba@1699
|
406 |
for (EdgeIt it(*graph); it != INVALID; ++it) {
|
deba@1699
|
407 |
Node source = graph->source(it);
|
deba@1699
|
408 |
Node target = graph->target(it);
|
deba@1699
|
409 |
if (OperationTraits::less((*length)[it], (*_dist)(source, target))) {
|
deba@1699
|
410 |
_dist->set(source, target, (*length)[it]);
|
deba@1699
|
411 |
_pred->set(source, target, it);
|
deba@1699
|
412 |
}
|
deba@1699
|
413 |
}
|
deba@1699
|
414 |
}
|
deba@1699
|
415 |
|
deba@1699
|
416 |
/// \brief Executes the algorithm.
|
deba@1699
|
417 |
///
|
deba@1699
|
418 |
/// This method runs the %FloydWarshall algorithm in order to compute
|
deba@1699
|
419 |
/// the shortest path to each node pairs. The algorithm
|
deba@1699
|
420 |
/// computes
|
deba@1699
|
421 |
/// - The shortest path tree for each node.
|
deba@1699
|
422 |
/// - The distance between each node pairs.
|
deba@1699
|
423 |
void start() {
|
deba@1699
|
424 |
for (NodeIt kt(*graph); kt != INVALID; ++kt) {
|
deba@1699
|
425 |
for (NodeIt it(*graph); it != INVALID; ++it) {
|
deba@1699
|
426 |
for (NodeIt jt(*graph); jt != INVALID; ++jt) {
|
deba@1699
|
427 |
Value relaxed = OperationTraits::plus((*_dist)(it, kt),
|
deba@1699
|
428 |
(*_dist)(kt, jt));
|
deba@1699
|
429 |
if (OperationTraits::less(relaxed, (*_dist)(it, jt))) {
|
deba@1699
|
430 |
_dist->set(it, jt, relaxed);
|
deba@1699
|
431 |
_pred->set(it, jt, (*_pred)(kt, jt));
|
deba@1699
|
432 |
}
|
deba@1699
|
433 |
}
|
deba@1699
|
434 |
}
|
deba@1699
|
435 |
}
|
deba@1699
|
436 |
}
|
deba@1741
|
437 |
|
deba@1754
|
438 |
/// \brief Executes the algorithm and checks the negative cycles.
|
deba@1741
|
439 |
///
|
deba@1741
|
440 |
/// This method runs the %FloydWarshall algorithm in order to compute
|
deba@1754
|
441 |
/// the shortest path to each node pairs. If there is a negative cycle
|
deba@1741
|
442 |
/// in the graph it gives back false.
|
deba@1741
|
443 |
/// The algorithm computes
|
deba@1741
|
444 |
/// - The shortest path tree for each node.
|
deba@1741
|
445 |
/// - The distance between each node pairs.
|
deba@1741
|
446 |
bool checkedStart() {
|
deba@1741
|
447 |
start();
|
deba@1741
|
448 |
for (NodeIt it(*graph); it != INVALID; ++it) {
|
deba@1741
|
449 |
if (OperationTraits::less((*dist)(it, it), OperationTraits::zero())) {
|
deba@1741
|
450 |
return false;
|
deba@1741
|
451 |
}
|
deba@1741
|
452 |
}
|
deba@1741
|
453 |
return true;
|
deba@1741
|
454 |
}
|
deba@1699
|
455 |
|
deba@1699
|
456 |
/// \brief Runs %FloydWarshall algorithm.
|
deba@1699
|
457 |
///
|
deba@1699
|
458 |
/// This method runs the %FloydWarshall algorithm from a each node
|
deba@1699
|
459 |
/// in order to compute the shortest path to each node pairs.
|
deba@1699
|
460 |
/// The algorithm computes
|
deba@1699
|
461 |
/// - The shortest path tree for each node.
|
deba@1699
|
462 |
/// - The distance between each node pairs.
|
deba@1699
|
463 |
///
|
deba@1699
|
464 |
/// \note d.run(s) is just a shortcut of the following code.
|
alpar@1946
|
465 |
///\code
|
deba@1699
|
466 |
/// d.init();
|
deba@1699
|
467 |
/// d.start();
|
alpar@1946
|
468 |
///\endcode
|
deba@1699
|
469 |
void run() {
|
deba@1699
|
470 |
init();
|
deba@1699
|
471 |
start();
|
deba@1699
|
472 |
}
|
deba@1699
|
473 |
|
deba@1699
|
474 |
///@}
|
deba@1699
|
475 |
|
deba@1699
|
476 |
/// \name Query Functions
|
deba@1699
|
477 |
/// The result of the %FloydWarshall algorithm can be obtained using these
|
deba@1699
|
478 |
/// functions.\n
|
deba@1699
|
479 |
/// Before the use of these functions,
|
deba@1699
|
480 |
/// either run() or start() must be called.
|
deba@1699
|
481 |
|
deba@1699
|
482 |
///@{
|
deba@1699
|
483 |
|
deba@2335
|
484 |
typedef PredMatrixMapPath<Graph, PredMap> Path;
|
deba@2335
|
485 |
|
deba@2335
|
486 |
///Gives back the shortest path.
|
deba@2335
|
487 |
|
deba@2335
|
488 |
///Gives back the shortest path.
|
deba@2335
|
489 |
///\pre The \c t should be reachable from the \c t.
|
deba@2335
|
490 |
Path path(Node s, Node t)
|
deba@2335
|
491 |
{
|
deba@2335
|
492 |
return Path(*graph, *_pred, s, t);
|
deba@1699
|
493 |
}
|
deba@1699
|
494 |
|
deba@1699
|
495 |
/// \brief The distance between two nodes.
|
deba@1699
|
496 |
///
|
deba@1699
|
497 |
/// Returns the distance between two nodes.
|
deba@1699
|
498 |
/// \pre \ref run() must be called before using this function.
|
deba@1699
|
499 |
/// \warning If node \c v in unreachable from the root the return value
|
deba@1699
|
500 |
/// of this funcion is undefined.
|
deba@1699
|
501 |
Value dist(Node source, Node target) const {
|
deba@1699
|
502 |
return (*_dist)(source, target);
|
deba@1699
|
503 |
}
|
deba@1699
|
504 |
|
deba@1699
|
505 |
/// \brief Returns the 'previous edge' of the shortest path tree.
|
deba@1699
|
506 |
///
|
deba@1699
|
507 |
/// For the node \c node it returns the 'previous edge' of the shortest
|
deba@1699
|
508 |
/// path tree to direction of the node \c root
|
deba@1699
|
509 |
/// i.e. it returns the last edge of a shortest path from the node \c root
|
deba@1699
|
510 |
/// to \c node. It is \ref INVALID if \c node is unreachable from the root
|
deba@1699
|
511 |
/// or if \c node=root. The shortest path tree used here is equal to the
|
deba@1699
|
512 |
/// shortest path tree used in \ref predNode().
|
deba@1699
|
513 |
/// \pre \ref run() must be called before using this function.
|
deba@1763
|
514 |
Edge predEdge(Node root, Node node) const {
|
deba@1699
|
515 |
return (*_pred)(root, node);
|
deba@1699
|
516 |
}
|
deba@1699
|
517 |
|
deba@1699
|
518 |
/// \brief Returns the 'previous node' of the shortest path tree.
|
deba@1699
|
519 |
///
|
deba@1699
|
520 |
/// For a node \c node it returns the 'previous node' of the shortest path
|
deba@1699
|
521 |
/// tree to direction of the node \c root, i.e. it returns the last but
|
deba@1699
|
522 |
/// one node from a shortest path from the \c root to \c node. It is
|
deba@1699
|
523 |
/// INVALID if \c node is unreachable from the root or if \c node=root.
|
deba@1699
|
524 |
/// The shortest path tree used here is equal to the
|
deba@1763
|
525 |
/// shortest path tree used in \ref predEdge().
|
deba@1699
|
526 |
/// \pre \ref run() must be called before using this function.
|
deba@1699
|
527 |
Node predNode(Node root, Node node) const {
|
deba@1699
|
528 |
return (*_pred)(root, node) == INVALID ?
|
deba@1699
|
529 |
INVALID : graph->source((*_pred)(root, node));
|
deba@1699
|
530 |
}
|
deba@1699
|
531 |
|
deba@1699
|
532 |
/// \brief Returns a reference to the matrix node map of distances.
|
deba@1699
|
533 |
///
|
deba@1699
|
534 |
/// Returns a reference to the matrix node map of distances.
|
deba@1699
|
535 |
///
|
deba@1699
|
536 |
/// \pre \ref run() must be called before using this function.
|
deba@1699
|
537 |
const DistMap &distMap() const { return *_dist;}
|
deba@1699
|
538 |
|
deba@1699
|
539 |
/// \brief Returns a reference to the shortest path tree map.
|
deba@1699
|
540 |
///
|
deba@1699
|
541 |
/// Returns a reference to the matrix node map of the edges of the
|
deba@1699
|
542 |
/// shortest path tree.
|
deba@1699
|
543 |
/// \pre \ref run() must be called before using this function.
|
deba@1699
|
544 |
const PredMap &predMap() const { return *_pred;}
|
deba@1699
|
545 |
|
deba@1699
|
546 |
/// \brief Checks if a node is reachable from the root.
|
deba@1699
|
547 |
///
|
deba@1699
|
548 |
/// Returns \c true if \c v is reachable from the root.
|
deba@1699
|
549 |
/// \pre \ref run() must be called before using this function.
|
deba@1699
|
550 |
///
|
deba@1699
|
551 |
bool connected(Node source, Node target) {
|
deba@1699
|
552 |
return (*_dist)(source, target) != OperationTraits::infinity();
|
deba@1699
|
553 |
}
|
deba@1699
|
554 |
|
deba@1699
|
555 |
///@}
|
deba@1699
|
556 |
};
|
deba@1699
|
557 |
|
deba@1699
|
558 |
} //END OF NAMESPACE LEMON
|
deba@1699
|
559 |
|
deba@1699
|
560 |
#endif
|
deba@1699
|
561 |
|