80 }; |
84 }; |
81 |
85 |
82 /// \brief Default traits class of BellmanFord class. |
86 /// \brief Default traits class of BellmanFord class. |
83 /// |
87 /// |
84 /// Default traits class of BellmanFord class. |
88 /// Default traits class of BellmanFord class. |
85 /// \param _Digraph Digraph type. |
89 /// \param GR The type of the digraph. |
86 /// \param _LegthMap Type of length map. |
90 /// \param LEN The type of the length map. |
87 template<class _Digraph, class _LengthMap> |
91 template<typename GR, typename LEN> |
88 struct BellmanFordDefaultTraits { |
92 struct BellmanFordDefaultTraits { |
89 /// The digraph type the algorithm runs on. |
93 /// The type of the digraph the algorithm runs on. |
90 typedef _Digraph Digraph; |
94 typedef GR Digraph; |
91 |
95 |
92 /// \brief The type of the map that stores the arc lengths. |
96 /// \brief The type of the map that stores the arc lengths. |
93 /// |
97 /// |
94 /// The type of the map that stores the arc lengths. |
98 /// The type of the map that stores the arc lengths. |
95 /// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
99 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
96 typedef _LengthMap LengthMap; |
100 typedef LEN LengthMap; |
97 |
101 |
98 // The type of the length of the arcs. |
102 /// The type of the arc lengths. |
99 typedef typename _LengthMap::Value Value; |
103 typedef typename LEN::Value Value; |
100 |
104 |
101 /// \brief Operation traits for Bellman-Ford algorithm. |
105 /// \brief Operation traits for Bellman-Ford algorithm. |
102 /// |
106 /// |
103 /// It defines the infinity type on the given Value type |
107 /// It defines the used operations and the infinity value for the |
104 /// and the used operation. |
108 /// given \c Value type. |
105 /// \see BellmanFordDefaultOperationTraits |
109 /// \see BellmanFordDefaultOperationTraits |
106 typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
110 typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
107 |
111 |
108 /// \brief The type of the map that stores the last arcs of the |
112 /// \brief The type of the map that stores the last arcs of the |
109 /// shortest paths. |
113 /// shortest paths. |
110 /// |
114 /// |
111 /// The type of the map that stores the last |
115 /// The type of the map that stores the last |
112 /// arcs of the shortest paths. |
116 /// arcs of the shortest paths. |
113 /// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
117 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
114 /// |
118 typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
115 typedef typename Digraph::template NodeMap<typename _Digraph::Arc> PredMap; |
119 |
116 |
120 /// \brief Instantiates a \c PredMap. |
117 /// \brief Instantiates a PredMap. |
|
118 /// |
121 /// |
119 /// This function instantiates a \ref PredMap. |
122 /// This function instantiates a \ref PredMap. |
120 /// \param digraph is the digraph, to which we would like to define the PredMap. |
123 /// \param g is the digraph to which we would like to define the |
121 static PredMap *createPredMap(const _Digraph& digraph) { |
124 /// \ref PredMap. |
122 return new PredMap(digraph); |
125 static PredMap *createPredMap(const GR& g) { |
123 } |
126 return new PredMap(g); |
124 |
127 } |
125 /// \brief The type of the map that stores the dists of the nodes. |
128 |
126 /// |
129 /// \brief The type of the map that stores the distances of the nodes. |
127 /// The type of the map that stores the dists of the nodes. |
130 /// |
128 /// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
131 /// The type of the map that stores the distances of the nodes. |
129 /// |
132 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
130 typedef typename Digraph::template NodeMap<typename _LengthMap::Value> |
133 typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
131 DistMap; |
134 |
132 |
135 /// \brief Instantiates a \c DistMap. |
133 /// \brief Instantiates a DistMap. |
|
134 /// |
136 /// |
135 /// This function instantiates a \ref DistMap. |
137 /// This function instantiates a \ref DistMap. |
136 /// \param digraph is the digraph, to which we would like to define the |
138 /// \param g is the digraph to which we would like to define the |
137 /// \ref DistMap |
139 /// \ref DistMap. |
138 static DistMap *createDistMap(const _Digraph& digraph) { |
140 static DistMap *createDistMap(const GR& g) { |
139 return new DistMap(digraph); |
141 return new DistMap(g); |
140 } |
142 } |
141 |
143 |
142 }; |
144 }; |
143 |
145 |
144 /// \brief %BellmanFord algorithm class. |
146 /// \brief %BellmanFord algorithm class. |
145 /// |
147 /// |
146 /// \ingroup shortest_path |
148 /// \ingroup shortest_path |
147 /// This class provides an efficient implementation of \c Bellman-Ford |
149 /// This class provides an efficient implementation of the Bellman-Ford |
148 /// algorithm. The arc lengths are passed to the algorithm using a |
150 /// algorithm. The maximum time complexity of the algorithm is |
|
151 /// <tt>O(ne)</tt>. |
|
152 /// |
|
153 /// The Bellman-Ford algorithm solves the single-source shortest path |
|
154 /// problem when the arcs can have negative lengths, but the digraph |
|
155 /// should not contain directed cycles with negative total length. |
|
156 /// If all arc costs are non-negative, consider to use the Dijkstra |
|
157 /// algorithm instead, since it is more efficient. |
|
158 /// |
|
159 /// The arc lengths are passed to the algorithm using a |
149 /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
160 /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
150 /// kind of length. |
161 /// kind of length. The type of the length values is determined by the |
151 /// |
162 /// \ref concepts::ReadMap::Value "Value" type of the length map. |
152 /// The Bellman-Ford algorithm solves the shortest path from one node |
163 /// |
153 /// problem when the arcs can have negative length but the digraph should |
164 /// There is also a \ref bellmanFord() "function-type interface" for the |
154 /// not contain cycles with negative sum of length. If we can assume |
165 /// Bellman-Ford algorithm, which is convenient in the simplier cases and |
155 /// that all arc is non-negative in the digraph then the dijkstra algorithm |
166 /// it can be used easier. |
156 /// should be used rather. |
167 /// |
157 /// |
168 /// \tparam GR The type of the digraph the algorithm runs on. |
158 /// The maximal time complexity of the algorithm is \f$ O(ne) \f$. |
169 /// The default type is \ref ListDigraph. |
159 /// |
170 /// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
160 /// The type of the length is determined by the |
171 /// the lengths of the arcs. The default map type is |
161 /// \ref concepts::ReadMap::Value "Value" of the length map. |
172 /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
162 /// |
|
163 /// \param _Digraph The digraph type the algorithm runs on. The default value |
|
164 /// is \ref ListDigraph. The value of _Digraph is not used directly by |
|
165 /// BellmanFord, it is only passed to \ref BellmanFordDefaultTraits. |
|
166 /// \param _LengthMap This read-only ArcMap determines the lengths of the |
|
167 /// arcs. The default map type is \ref concepts::Digraph::ArcMap |
|
168 /// "Digraph::ArcMap<int>". The value of _LengthMap is not used directly |
|
169 /// by BellmanFord, it is only passed to \ref BellmanFordDefaultTraits. |
|
170 /// \param _Traits Traits class to set various data types used by the |
|
171 /// algorithm. The default traits class is \ref BellmanFordDefaultTraits |
|
172 /// "BellmanFordDefaultTraits<_Digraph,_LengthMap>". See \ref |
|
173 /// BellmanFordDefaultTraits for the documentation of a BellmanFord traits |
|
174 /// class. |
|
175 #ifdef DOXYGEN |
173 #ifdef DOXYGEN |
176 template <typename _Digraph, typename _LengthMap, typename _Traits> |
174 template <typename GR, typename LEN, typename TR> |
177 #else |
175 #else |
178 template <typename _Digraph, |
176 template <typename GR=ListDigraph, |
179 typename _LengthMap=typename _Digraph::template ArcMap<int>, |
177 typename LEN=typename GR::template ArcMap<int>, |
180 typename _Traits=BellmanFordDefaultTraits<_Digraph,_LengthMap> > |
178 typename TR=BellmanFordDefaultTraits<GR,LEN> > |
181 #endif |
179 #endif |
182 class BellmanFord { |
180 class BellmanFord { |
183 public: |
181 public: |
184 |
182 |
185 typedef _Traits Traits; |
|
186 ///The type of the underlying digraph. |
183 ///The type of the underlying digraph. |
187 typedef typename _Traits::Digraph Digraph; |
184 typedef typename TR::Digraph Digraph; |
|
185 |
|
186 /// \brief The type of the arc lengths. |
|
187 typedef typename TR::LengthMap::Value Value; |
|
188 /// \brief The type of the map that stores the arc lengths. |
|
189 typedef typename TR::LengthMap LengthMap; |
|
190 /// \brief The type of the map that stores the last |
|
191 /// arcs of the shortest paths. |
|
192 typedef typename TR::PredMap PredMap; |
|
193 /// \brief The type of the map that stores the distances of the nodes. |
|
194 typedef typename TR::DistMap DistMap; |
|
195 /// The type of the paths. |
|
196 typedef PredMapPath<Digraph, PredMap> Path; |
|
197 ///\brief The \ref BellmanFordDefaultOperationTraits |
|
198 /// "operation traits class" of the algorithm. |
|
199 typedef typename TR::OperationTraits OperationTraits; |
|
200 |
|
201 ///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
|
202 typedef TR Traits; |
|
203 |
|
204 private: |
188 |
205 |
189 typedef typename Digraph::Node Node; |
206 typedef typename Digraph::Node Node; |
190 typedef typename Digraph::NodeIt NodeIt; |
207 typedef typename Digraph::NodeIt NodeIt; |
191 typedef typename Digraph::Arc Arc; |
208 typedef typename Digraph::Arc Arc; |
192 typedef typename Digraph::OutArcIt OutArcIt; |
209 typedef typename Digraph::OutArcIt OutArcIt; |
193 |
210 |
194 /// \brief The type of the length of the arcs. |
211 // Pointer to the underlying digraph. |
195 typedef typename _Traits::LengthMap::Value Value; |
212 const Digraph *_gr; |
196 /// \brief The type of the map that stores the arc lengths. |
213 // Pointer to the length map |
197 typedef typename _Traits::LengthMap LengthMap; |
214 const LengthMap *_length; |
198 /// \brief The type of the map that stores the last |
215 // Pointer to the map of predecessors arcs. |
199 /// arcs of the shortest paths. |
|
200 typedef typename _Traits::PredMap PredMap; |
|
201 /// \brief The type of the map that stores the dists of the nodes. |
|
202 typedef typename _Traits::DistMap DistMap; |
|
203 /// \brief The operation traits. |
|
204 typedef typename _Traits::OperationTraits OperationTraits; |
|
205 private: |
|
206 /// Pointer to the underlying digraph. |
|
207 const Digraph *digraph; |
|
208 /// Pointer to the length map |
|
209 const LengthMap *length; |
|
210 ///Pointer to the map of predecessors arcs. |
|
211 PredMap *_pred; |
216 PredMap *_pred; |
212 ///Indicates if \ref _pred is locally allocated (\c true) or not. |
217 // Indicates if _pred is locally allocated (true) or not. |
213 bool local_pred; |
218 bool _local_pred; |
214 ///Pointer to the map of distances. |
219 // Pointer to the map of distances. |
215 DistMap *_dist; |
220 DistMap *_dist; |
216 ///Indicates if \ref _dist is locally allocated (\c true) or not. |
221 // Indicates if _dist is locally allocated (true) or not. |
217 bool local_dist; |
222 bool _local_dist; |
218 |
223 |
219 typedef typename Digraph::template NodeMap<bool> MaskMap; |
224 typedef typename Digraph::template NodeMap<bool> MaskMap; |
220 MaskMap *_mask; |
225 MaskMap *_mask; |
221 |
226 |
222 std::vector<Node> _process; |
227 std::vector<Node> _process; |
223 |
228 |
224 /// Creates the maps if necessary. |
229 // Creates the maps if necessary. |
225 void create_maps() { |
230 void create_maps() { |
226 if(!_pred) { |
231 if(!_pred) { |
227 local_pred = true; |
232 _local_pred = true; |
228 _pred = Traits::createPredMap(*digraph); |
233 _pred = Traits::createPredMap(*_gr); |
229 } |
234 } |
230 if(!_dist) { |
235 if(!_dist) { |
231 local_dist = true; |
236 _local_dist = true; |
232 _dist = Traits::createDistMap(*digraph); |
237 _dist = Traits::createDistMap(*_gr); |
233 } |
238 } |
234 _mask = new MaskMap(*digraph, false); |
239 _mask = new MaskMap(*_gr, false); |
235 } |
240 } |
236 |
241 |
237 public : |
242 public : |
238 |
243 |
239 typedef BellmanFord Create; |
244 typedef BellmanFord Create; |
240 |
245 |
241 /// \name Named template parameters |
246 /// \name Named Template Parameters |
242 |
247 |
243 ///@{ |
248 ///@{ |
244 |
249 |
245 template <class T> |
250 template <class T> |
246 struct DefPredMapTraits : public Traits { |
251 struct SetPredMapTraits : public Traits { |
247 typedef T PredMap; |
252 typedef T PredMap; |
248 static PredMap *createPredMap(const Digraph&) { |
253 static PredMap *createPredMap(const Digraph&) { |
249 LEMON_ASSERT(false, "PredMap is not initialized"); |
254 LEMON_ASSERT(false, "PredMap is not initialized"); |
250 return 0; // ignore warnings |
255 return 0; // ignore warnings |
251 } |
256 } |
252 }; |
257 }; |
253 |
258 |
254 /// \brief \ref named-templ-param "Named parameter" for setting PredMap |
259 /// \brief \ref named-templ-param "Named parameter" for setting |
255 /// type |
260 /// \c PredMap type. |
256 /// \ref named-templ-param "Named parameter" for setting PredMap type |
261 /// |
257 /// |
262 /// \ref named-templ-param "Named parameter" for setting |
|
263 /// \c PredMap type. |
|
264 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
258 template <class T> |
265 template <class T> |
259 struct SetPredMap |
266 struct SetPredMap |
260 : public BellmanFord< Digraph, LengthMap, DefPredMapTraits<T> > { |
267 : public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > { |
261 typedef BellmanFord< Digraph, LengthMap, DefPredMapTraits<T> > Create; |
268 typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
262 }; |
269 }; |
263 |
270 |
264 template <class T> |
271 template <class T> |
265 struct DefDistMapTraits : public Traits { |
272 struct SetDistMapTraits : public Traits { |
266 typedef T DistMap; |
273 typedef T DistMap; |
267 static DistMap *createDistMap(const Digraph&) { |
274 static DistMap *createDistMap(const Digraph&) { |
268 LEMON_ASSERT(false, "DistMap is not initialized"); |
275 LEMON_ASSERT(false, "DistMap is not initialized"); |
269 return 0; // ignore warnings |
276 return 0; // ignore warnings |
270 } |
277 } |
271 }; |
278 }; |
272 |
279 |
273 /// \brief \ref named-templ-param "Named parameter" for setting DistMap |
280 /// \brief \ref named-templ-param "Named parameter" for setting |
274 /// type |
281 /// \c DistMap type. |
275 /// |
282 /// |
276 /// \ref named-templ-param "Named parameter" for setting DistMap type |
283 /// \ref named-templ-param "Named parameter" for setting |
277 /// |
284 /// \c DistMap type. |
|
285 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
278 template <class T> |
286 template <class T> |
279 struct SetDistMap |
287 struct SetDistMap |
280 : public BellmanFord< Digraph, LengthMap, DefDistMapTraits<T> > { |
288 : public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > { |
281 typedef BellmanFord< Digraph, LengthMap, DefDistMapTraits<T> > Create; |
289 typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
282 }; |
290 }; |
283 |
291 |
284 template <class T> |
292 template <class T> |
285 struct DefOperationTraitsTraits : public Traits { |
293 struct SetOperationTraitsTraits : public Traits { |
286 typedef T OperationTraits; |
294 typedef T OperationTraits; |
287 }; |
295 }; |
288 |
296 |
289 /// \brief \ref named-templ-param "Named parameter" for setting |
297 /// \brief \ref named-templ-param "Named parameter" for setting |
290 /// OperationTraits type |
298 /// \c OperationTraits type. |
291 /// |
299 /// |
292 /// \ref named-templ-param "Named parameter" for setting OperationTraits |
300 /// \ref named-templ-param "Named parameter" for setting |
293 /// type |
301 /// \c OperationTraits type. |
|
302 /// For more information see \ref BellmanFordDefaultOperationTraits. |
294 template <class T> |
303 template <class T> |
295 struct SetOperationTraits |
304 struct SetOperationTraits |
296 : public BellmanFord< Digraph, LengthMap, DefOperationTraitsTraits<T> > { |
305 : public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
297 typedef BellmanFord< Digraph, LengthMap, DefOperationTraitsTraits<T> > |
306 typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
298 Create; |
307 Create; |
299 }; |
308 }; |
300 |
309 |
301 ///@} |
310 ///@} |
302 |
311 |
306 |
315 |
307 public: |
316 public: |
308 |
317 |
309 /// \brief Constructor. |
318 /// \brief Constructor. |
310 /// |
319 /// |
311 /// \param _graph the digraph the algorithm will run on. |
320 /// Constructor. |
312 /// \param _length the length map used by the algorithm. |
321 /// \param g The digraph the algorithm runs on. |
313 BellmanFord(const Digraph& _graph, const LengthMap& _length) : |
322 /// \param length The length map used by the algorithm. |
314 digraph(&_graph), length(&_length), |
323 BellmanFord(const Digraph& g, const LengthMap& length) : |
315 _pred(0), local_pred(false), |
324 _gr(&g), _length(&length), |
316 _dist(0), local_dist(false), _mask(0) {} |
325 _pred(0), _local_pred(false), |
|
326 _dist(0), _local_dist(false), _mask(0) {} |
317 |
327 |
318 ///Destructor. |
328 ///Destructor. |
319 ~BellmanFord() { |
329 ~BellmanFord() { |
320 if(local_pred) delete _pred; |
330 if(_local_pred) delete _pred; |
321 if(local_dist) delete _dist; |
331 if(_local_dist) delete _dist; |
322 if(_mask) delete _mask; |
332 if(_mask) delete _mask; |
323 } |
333 } |
324 |
334 |
325 /// \brief Sets the length map. |
335 /// \brief Sets the length map. |
326 /// |
336 /// |
327 /// Sets the length map. |
337 /// Sets the length map. |
328 /// \return \c (*this) |
338 /// \return <tt>(*this)</tt> |
329 BellmanFord &lengthMap(const LengthMap &m) { |
339 BellmanFord &lengthMap(const LengthMap &map) { |
330 length = &m; |
340 _length = ↦ |
331 return *this; |
341 return *this; |
332 } |
342 } |
333 |
343 |
334 /// \brief Sets the map storing the predecessor arcs. |
344 /// \brief Sets the map that stores the predecessor arcs. |
335 /// |
345 /// |
336 /// Sets the map storing the predecessor arcs. |
346 /// Sets the map that stores the predecessor arcs. |
337 /// If you don't use this function before calling \ref run(), |
347 /// If you don't use this function before calling \ref run() |
338 /// it will allocate one. The destuctor deallocates this |
348 /// or \ref init(), an instance will be allocated automatically. |
339 /// automatically allocated map, of course. |
349 /// The destructor deallocates this automatically allocated map, |
340 /// \return \c (*this) |
350 /// of course. |
341 BellmanFord &predMap(PredMap &m) { |
351 /// \return <tt>(*this)</tt> |
342 if(local_pred) { |
352 BellmanFord &predMap(PredMap &map) { |
|
353 if(_local_pred) { |
343 delete _pred; |
354 delete _pred; |
344 local_pred=false; |
355 _local_pred=false; |
345 } |
356 } |
346 _pred = &m; |
357 _pred = ↦ |
347 return *this; |
358 return *this; |
348 } |
359 } |
349 |
360 |
350 /// \brief Sets the map storing the distances calculated by the algorithm. |
361 /// \brief Sets the map that stores the distances of the nodes. |
351 /// |
362 /// |
352 /// Sets the map storing the distances calculated by the algorithm. |
363 /// Sets the map that stores the distances of the nodes calculated |
353 /// If you don't use this function before calling \ref run(), |
364 /// by the algorithm. |
354 /// it will allocate one. The destuctor deallocates this |
365 /// If you don't use this function before calling \ref run() |
355 /// automatically allocated map, of course. |
366 /// or \ref init(), an instance will be allocated automatically. |
356 /// \return \c (*this) |
367 /// The destructor deallocates this automatically allocated map, |
357 BellmanFord &distMap(DistMap &m) { |
368 /// of course. |
358 if(local_dist) { |
369 /// \return <tt>(*this)</tt> |
|
370 BellmanFord &distMap(DistMap &map) { |
|
371 if(_local_dist) { |
359 delete _dist; |
372 delete _dist; |
360 local_dist=false; |
373 _local_dist=false; |
361 } |
374 } |
362 _dist = &m; |
375 _dist = ↦ |
363 return *this; |
376 return *this; |
364 } |
377 } |
365 |
378 |
366 /// \name Execution control |
379 /// \name Execution Control |
367 /// The simplest way to execute the algorithm is to use |
380 /// The simplest way to execute the Bellman-Ford algorithm is to use |
368 /// one of the member functions called \c run(...). |
381 /// one of the member functions called \ref run().\n |
369 /// \n |
382 /// If you need better control on the execution, you have to call |
370 /// If you need more control on the execution, |
383 /// \ref init() first, then you can add several source nodes |
371 /// first you must call \ref init(), then you can add several source nodes |
384 /// with \ref addSource(). Finally the actual path computation can be |
372 /// with \ref addSource(). |
385 /// performed with \ref start(), \ref checkedStart() or |
373 /// Finally \ref start() will perform the actual path |
386 /// \ref limitedStart(). |
374 /// computation. |
|
375 |
387 |
376 ///@{ |
388 ///@{ |
377 |
389 |
378 /// \brief Initializes the internal data structures. |
390 /// \brief Initializes the internal data structures. |
379 /// |
391 /// |
380 /// Initializes the internal data structures. |
392 /// Initializes the internal data structures. The optional parameter |
|
393 /// is the initial distance of each node. |
381 void init(const Value value = OperationTraits::infinity()) { |
394 void init(const Value value = OperationTraits::infinity()) { |
382 create_maps(); |
395 create_maps(); |
383 for (NodeIt it(*digraph); it != INVALID; ++it) { |
396 for (NodeIt it(*_gr); it != INVALID; ++it) { |
384 _pred->set(it, INVALID); |
397 _pred->set(it, INVALID); |
385 _dist->set(it, value); |
398 _dist->set(it, value); |
386 } |
399 } |
387 _process.clear(); |
400 _process.clear(); |
388 if (OperationTraits::less(value, OperationTraits::infinity())) { |
401 if (OperationTraits::less(value, OperationTraits::infinity())) { |
389 for (NodeIt it(*digraph); it != INVALID; ++it) { |
402 for (NodeIt it(*_gr); it != INVALID; ++it) { |
390 _process.push_back(it); |
403 _process.push_back(it); |
391 _mask->set(it, true); |
404 _mask->set(it, true); |
392 } |
405 } |
393 } |
406 } |
394 } |
407 } |
395 |
408 |
396 /// \brief Adds a new source node. |
409 /// \brief Adds a new source node. |
397 /// |
410 /// |
398 /// Adds a new source node. The optional second parameter is the |
411 /// This function adds a new source node. The optional second parameter |
399 /// initial distance of the node. It just sets the distance of the |
412 /// is the initial distance of the node. |
400 /// node to the given value. |
|
401 void addSource(Node source, Value dst = OperationTraits::zero()) { |
413 void addSource(Node source, Value dst = OperationTraits::zero()) { |
402 _dist->set(source, dst); |
414 _dist->set(source, dst); |
403 if (!(*_mask)[source]) { |
415 if (!(*_mask)[source]) { |
404 _process.push_back(source); |
416 _process.push_back(source); |
405 _mask->set(source, true); |
417 _mask->set(source, true); |
482 return _process.empty(); |
502 return _process.empty(); |
483 } |
503 } |
484 |
504 |
485 /// \brief Executes the algorithm. |
505 /// \brief Executes the algorithm. |
486 /// |
506 /// |
487 /// \pre init() must be called and at least one node should be added |
507 /// Executes the algorithm. |
488 /// with addSource() before using this function. |
508 /// |
489 /// |
509 /// This method runs the Bellman-Ford algorithm from the root node(s) |
490 /// This method runs the %BellmanFord algorithm from the root node(s) |
510 /// in order to compute the shortest path to each node. |
491 /// in order to compute the shortest path to each node. The algorithm |
511 /// |
492 /// computes |
512 /// The algorithm computes |
493 /// - The shortest path tree. |
513 /// - the shortest path tree (forest), |
494 /// - The distance of each node from the root(s). |
514 /// - the distance of each node from the root(s). |
|
515 /// |
|
516 /// \pre init() must be called and at least one root node should be |
|
517 /// added with addSource() before using this function. |
495 void start() { |
518 void start() { |
496 int num = countNodes(*digraph) - 1; |
519 int num = countNodes(*_gr) - 1; |
497 for (int i = 0; i < num; ++i) { |
520 for (int i = 0; i < num; ++i) { |
498 if (processNextWeakRound()) break; |
521 if (processNextWeakRound()) break; |
499 } |
522 } |
500 } |
523 } |
501 |
524 |
502 /// \brief Executes the algorithm and checks the negative cycles. |
525 /// \brief Executes the algorithm and checks the negative cycles. |
503 /// |
526 /// |
504 /// \pre init() must be called and at least one node should be added |
527 /// Executes the algorithm and checks the negative cycles. |
505 /// with addSource() before using this function. |
528 /// |
506 /// |
529 /// This method runs the Bellman-Ford algorithm from the root node(s) |
507 /// This method runs the %BellmanFord algorithm from the root node(s) |
530 /// in order to compute the shortest path to each node and also checks |
508 /// in order to compute the shortest path to each node. The algorithm |
531 /// if the digraph contains cycles with negative total length. |
509 /// computes |
532 /// |
510 /// - The shortest path tree. |
533 /// The algorithm computes |
511 /// - The distance of each node from the root(s). |
534 /// - the shortest path tree (forest), |
|
535 /// - the distance of each node from the root(s). |
512 /// |
536 /// |
513 /// \return \c false if there is a negative cycle in the digraph. |
537 /// \return \c false if there is a negative cycle in the digraph. |
|
538 /// |
|
539 /// \pre init() must be called and at least one root node should be |
|
540 /// added with addSource() before using this function. |
514 bool checkedStart() { |
541 bool checkedStart() { |
515 int num = countNodes(*digraph); |
542 int num = countNodes(*_gr); |
516 for (int i = 0; i < num; ++i) { |
543 for (int i = 0; i < num; ++i) { |
517 if (processNextWeakRound()) return true; |
544 if (processNextWeakRound()) return true; |
518 } |
545 } |
519 return _process.empty(); |
546 return _process.empty(); |
520 } |
547 } |
521 |
548 |
522 /// \brief Executes the algorithm with path length limit. |
549 /// \brief Executes the algorithm with arc number limit. |
523 /// |
550 /// |
524 /// \pre init() must be called and at least one node should be added |
551 /// Executes the algorithm with arc number limit. |
525 /// with addSource() before using this function. |
552 /// |
526 /// |
553 /// This method runs the Bellman-Ford algorithm from the root node(s) |
527 /// This method runs the %BellmanFord algorithm from the root |
554 /// in order to compute the shortest path distance for each node |
528 /// node(s) in order to compute the shortest path lengths with at |
555 /// using only the paths consisting of at most \c num arcs. |
529 /// most \c num arc. |
556 /// |
|
557 /// The algorithm computes |
|
558 /// - the limited distance of each node from the root(s), |
|
559 /// - the predecessor arc for each node. |
530 /// |
560 /// |
531 /// \warning The paths with limited arc number cannot be retrieved |
561 /// \warning The paths with limited arc number cannot be retrieved |
532 /// easily with \ref path() or \ref predArc() functions. If you |
562 /// easily with \ref path() or \ref predArc() functions. If you also |
533 /// need the shortest path and not just the distance you should store |
563 /// need the shortest paths and not only the distances, you should |
534 /// after each iteration the \ref predMap() map and manually build |
564 /// store the \ref predMap() "predecessor map" after each iteration |
535 /// the path. |
565 /// and build the path manually. |
536 /// |
566 /// |
537 /// The algorithm computes |
567 /// \pre init() must be called and at least one root node should be |
538 /// - The predecessor arc from each node. |
568 /// added with addSource() before using this function. |
539 /// - The limited distance of each node from the root(s). |
|
540 void limitedStart(int num) { |
569 void limitedStart(int num) { |
541 for (int i = 0; i < num; ++i) { |
570 for (int i = 0; i < num; ++i) { |
542 if (processNextRound()) break; |
571 if (processNextRound()) break; |
543 } |
572 } |
544 } |
573 } |
545 |
574 |
546 /// \brief Runs %BellmanFord algorithm from node \c s. |
575 /// \brief Runs the algorithm from the given root node. |
547 /// |
576 /// |
548 /// This method runs the %BellmanFord algorithm from a root node \c s |
577 /// This method runs the Bellman-Ford algorithm from the given root |
549 /// in order to compute the shortest path to each node. The algorithm |
578 /// node \c s in order to compute the shortest path to each node. |
550 /// computes |
579 /// |
551 /// - The shortest path tree. |
580 /// The algorithm computes |
552 /// - The distance of each node from the root. |
581 /// - the shortest path tree (forest), |
553 /// |
582 /// - the distance of each node from the root(s). |
554 /// \note d.run(s) is just a shortcut of the following code. |
583 /// |
555 ///\code |
584 /// \note bf.run(s) is just a shortcut of the following code. |
556 /// d.init(); |
585 /// \code |
557 /// d.addSource(s); |
586 /// bf.init(); |
558 /// d.start(); |
587 /// bf.addSource(s); |
559 ///\endcode |
588 /// bf.start(); |
|
589 /// \endcode |
560 void run(Node s) { |
590 void run(Node s) { |
561 init(); |
591 init(); |
562 addSource(s); |
592 addSource(s); |
563 start(); |
593 start(); |
564 } |
594 } |
565 |
595 |
566 /// \brief Runs %BellmanFord algorithm with limited path length |
596 /// \brief Runs the algorithm from the given root node with arc |
567 /// from node \c s. |
597 /// number limit. |
568 /// |
598 /// |
569 /// This method runs the %BellmanFord algorithm from a root node \c s |
599 /// This method runs the Bellman-Ford algorithm from the given root |
570 /// in order to compute the shortest path with at most \c len arcs |
600 /// node \c s in order to compute the shortest path distance for each |
571 /// to each node. The algorithm computes |
601 /// node using only the paths consisting of at most \c num arcs. |
572 /// - The shortest path tree. |
602 /// |
573 /// - The distance of each node from the root. |
603 /// The algorithm computes |
574 /// |
604 /// - the limited distance of each node from the root(s), |
575 /// \note d.run(s, num) is just a shortcut of the following code. |
605 /// - the predecessor arc for each node. |
576 ///\code |
606 /// |
577 /// d.init(); |
607 /// \warning The paths with limited arc number cannot be retrieved |
578 /// d.addSource(s); |
608 /// easily with \ref path() or \ref predArc() functions. If you also |
579 /// d.limitedStart(num); |
609 /// need the shortest paths and not only the distances, you should |
580 ///\endcode |
610 /// store the \ref predMap() "predecessor map" after each iteration |
|
611 /// and build the path manually. |
|
612 /// |
|
613 /// \note bf.run(s, num) is just a shortcut of the following code. |
|
614 /// \code |
|
615 /// bf.init(); |
|
616 /// bf.addSource(s); |
|
617 /// bf.limitedStart(num); |
|
618 /// \endcode |
581 void run(Node s, int num) { |
619 void run(Node s, int num) { |
582 init(); |
620 init(); |
583 addSource(s); |
621 addSource(s); |
584 limitedStart(num); |
622 limitedStart(num); |
585 } |
623 } |
586 |
624 |
587 ///@} |
625 ///@} |
588 |
626 |
589 /// \name Query Functions |
627 /// \brief LEMON iterator for getting the active nodes. |
590 /// The result of the %BellmanFord algorithm can be obtained using these |
628 /// |
591 /// functions.\n |
629 /// This class provides a common style LEMON iterator that traverses |
592 /// Before the use of these functions, |
630 /// the active nodes of the Bellman-Ford algorithm after the last |
593 /// either run() or start() must be called. |
631 /// phase. These nodes should be checked in the next phase to |
594 |
632 /// find augmenting arcs outgoing from them. |
595 ///@{ |
|
596 |
|
597 /// \brief Lemon iterator for get the active nodes. |
|
598 /// |
|
599 /// Lemon iterator for get the active nodes. This class provides a |
|
600 /// common style lemon iterator which gives back a subset of the |
|
601 /// nodes. The iterated nodes are active in the algorithm after |
|
602 /// the last phase so these should be checked in the next phase to |
|
603 /// find augmenting arcs from these. |
|
604 class ActiveIt { |
633 class ActiveIt { |
605 public: |
634 public: |
606 |
635 |
607 /// \brief Constructor. |
636 /// \brief Constructor. |
608 /// |
637 /// |
609 /// Constructor for get the nodeset of the variable. |
638 /// Constructor for getting the active nodes of the given BellmanFord |
|
639 /// instance. |
610 ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
640 ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
611 { |
641 { |
612 _index = _algorithm->_process.size() - 1; |
642 _index = _algorithm->_process.size() - 1; |
613 } |
643 } |
614 |
644 |
615 /// \brief Invalid constructor. |
645 /// \brief Invalid constructor. |
616 /// |
646 /// |
617 /// Invalid constructor. |
647 /// Invalid constructor. |
618 ActiveIt(Invalid) : _algorithm(0), _index(-1) {} |
648 ActiveIt(Invalid) : _algorithm(0), _index(-1) {} |
619 |
649 |
620 /// \brief Conversion to node. |
650 /// \brief Conversion to \c Node. |
621 /// |
651 /// |
622 /// Conversion to node. |
652 /// Conversion to \c Node. |
623 operator Node() const { |
653 operator Node() const { |
624 return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
654 return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
625 } |
655 } |
626 |
656 |
627 /// \brief Increment operator. |
657 /// \brief Increment operator. |
644 |
674 |
645 private: |
675 private: |
646 const BellmanFord* _algorithm; |
676 const BellmanFord* _algorithm; |
647 int _index; |
677 int _index; |
648 }; |
678 }; |
649 |
679 |
650 typedef PredMapPath<Digraph, PredMap> Path; |
680 /// \name Query Functions |
651 |
681 /// The result of the Bellman-Ford algorithm can be obtained using these |
652 /// \brief Gives back the shortest path. |
682 /// functions.\n |
|
683 /// Either \ref run() or \ref init() should be called before using them. |
|
684 |
|
685 ///@{ |
|
686 |
|
687 /// \brief The shortest path to the given node. |
653 /// |
688 /// |
654 /// Gives back the shortest path. |
689 /// Gives back the shortest path to the given node from the root(s). |
655 /// \pre The \c t should be reachable from the source. |
690 /// |
656 Path path(Node t) |
691 /// \warning \c t should be reached from the root(s). |
|
692 /// |
|
693 /// \pre Either \ref run() or \ref init() must be called before |
|
694 /// using this function. |
|
695 Path path(Node t) const |
657 { |
696 { |
658 return Path(*digraph, *_pred, t); |
697 return Path(*_gr, *_pred, t); |
659 } |
698 } |
660 |
|
661 |
|
662 // TODO : implement negative cycle |
|
663 // /// \brief Gives back a negative cycle. |
|
664 // /// |
|
665 // /// This function gives back a negative cycle. |
|
666 // /// If the algorithm have not found yet negative cycle it will give back |
|
667 // /// an empty path. |
|
668 // Path negativeCycle() { |
|
669 // typename Digraph::template NodeMap<int> state(*digraph, 0); |
|
670 // for (ActiveIt it(*this); it != INVALID; ++it) { |
|
671 // if (state[it] == 0) { |
|
672 // for (Node t = it; predArc(t) != INVALID; t = predNode(t)) { |
|
673 // if (state[t] == 0) { |
|
674 // state[t] = 1; |
|
675 // } else if (state[t] == 2) { |
|
676 // break; |
|
677 // } else { |
|
678 // p.clear(); |
|
679 // typename Path::Builder b(p); |
|
680 // b.setStartNode(t); |
|
681 // b.pushFront(predArc(t)); |
|
682 // for(Node s = predNode(t); s != t; s = predNode(s)) { |
|
683 // b.pushFront(predArc(s)); |
|
684 // } |
|
685 // b.commit(); |
|
686 // return true; |
|
687 // } |
|
688 // } |
|
689 // for (Node t = it; predArc(t) != INVALID; t = predNode(t)) { |
|
690 // if (state[t] == 1) { |
|
691 // state[t] = 2; |
|
692 // } else { |
|
693 // break; |
|
694 // } |
|
695 // } |
|
696 // } |
|
697 // } |
|
698 // return false; |
|
699 // } |
|
700 |
699 |
701 /// \brief The distance of a node from the root. |
700 /// \brief The distance of the given node from the root(s). |
702 /// |
701 /// |
703 /// Returns the distance of a node from the root. |
702 /// Returns the distance of the given node from the root(s). |
704 /// \pre \ref run() must be called before using this function. |
703 /// |
705 /// \warning If node \c v in unreachable from the root the return value |
704 /// \warning If node \c v is not reached from the root(s), then |
706 /// of this funcion is undefined. |
705 /// the return value of this function is undefined. |
|
706 /// |
|
707 /// \pre Either \ref run() or \ref init() must be called before |
|
708 /// using this function. |
707 Value dist(Node v) const { return (*_dist)[v]; } |
709 Value dist(Node v) const { return (*_dist)[v]; } |
708 |
710 |
709 /// \brief Returns the 'previous arc' of the shortest path tree. |
711 /// \brief Returns the 'previous arc' of the shortest path tree for |
710 /// |
712 /// the given node. |
711 /// For a node \c v it returns the 'previous arc' of the shortest path |
713 /// |
712 /// tree, i.e. it returns the last arc of a shortest path from the root |
714 /// This function returns the 'previous arc' of the shortest path |
713 /// to \c v. It is \ref INVALID if \c v is unreachable from the root or |
715 /// tree for node \c v, i.e. it returns the last arc of a |
714 /// if \c v=s. The shortest path tree used here is equal to the shortest |
716 /// shortest path from a root to \c v. It is \c INVALID if \c v |
715 /// path tree used in \ref predNode(). |
717 /// is not reached from the root(s) or if \c v is a root. |
716 /// \pre \ref run() must be called before using |
718 /// |
717 /// this function. |
719 /// The shortest path tree used here is equal to the shortest path |
|
720 /// tree used in \ref predNode() and \predMap(). |
|
721 /// |
|
722 /// \pre Either \ref run() or \ref init() must be called before |
|
723 /// using this function. |
718 Arc predArc(Node v) const { return (*_pred)[v]; } |
724 Arc predArc(Node v) const { return (*_pred)[v]; } |
719 |
725 |
720 /// \brief Returns the 'previous node' of the shortest path tree. |
726 /// \brief Returns the 'previous node' of the shortest path tree for |
721 /// |
727 /// the given node. |
722 /// For a node \c v it returns the 'previous node' of the shortest path |
728 /// |
723 /// tree, i.e. it returns the last but one node from a shortest path from |
729 /// This function returns the 'previous node' of the shortest path |
724 /// the root to \c /v. It is INVALID if \c v is unreachable from the root |
730 /// tree for node \c v, i.e. it returns the last but one node of |
725 /// or if \c v=s. The shortest path tree used here is equal to the |
731 /// a shortest path from a root to \c v. It is \c INVALID if \c v |
726 /// shortest path tree used in \ref predArc(). \pre \ref run() must be |
732 /// is not reached from the root(s) or if \c v is a root. |
727 /// called before using this function. |
733 /// |
|
734 /// The shortest path tree used here is equal to the shortest path |
|
735 /// tree used in \ref predArc() and \predMap(). |
|
736 /// |
|
737 /// \pre Either \ref run() or \ref init() must be called before |
|
738 /// using this function. |
728 Node predNode(Node v) const { |
739 Node predNode(Node v) const { |
729 return (*_pred)[v] == INVALID ? INVALID : digraph->source((*_pred)[v]); |
740 return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
730 } |
741 } |
731 |
742 |
732 /// \brief Returns a reference to the NodeMap of distances. |
743 /// \brief Returns a const reference to the node map that stores the |
733 /// |
744 /// distances of the nodes. |
734 /// Returns a reference to the NodeMap of distances. \pre \ref run() must |
745 /// |
735 /// be called before using this function. |
746 /// Returns a const reference to the node map that stores the distances |
|
747 /// of the nodes calculated by the algorithm. |
|
748 /// |
|
749 /// \pre Either \ref run() or \ref init() must be called before |
|
750 /// using this function. |
736 const DistMap &distMap() const { return *_dist;} |
751 const DistMap &distMap() const { return *_dist;} |
737 |
752 |
738 /// \brief Returns a reference to the shortest path tree map. |
753 /// \brief Returns a const reference to the node map that stores the |
739 /// |
754 /// predecessor arcs. |
740 /// Returns a reference to the NodeMap of the arcs of the |
755 /// |
741 /// shortest path tree. |
756 /// Returns a const reference to the node map that stores the predecessor |
742 /// \pre \ref run() must be called before using this function. |
757 /// arcs, which form the shortest path tree (forest). |
|
758 /// |
|
759 /// \pre Either \ref run() or \ref init() must be called before |
|
760 /// using this function. |
743 const PredMap &predMap() const { return *_pred; } |
761 const PredMap &predMap() const { return *_pred; } |
744 |
762 |
745 /// \brief Checks if a node is reachable from the root. |
763 /// \brief Checks if a node is reached from the root(s). |
746 /// |
764 /// |
747 /// Returns \c true if \c v is reachable from the root. |
765 /// Returns \c true if \c v is reached from the root(s). |
748 /// \pre \ref run() must be called before using this function. |
766 /// |
749 /// |
767 /// \pre Either \ref run() or \ref init() must be called before |
750 bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); } |
768 /// using this function. |
|
769 bool reached(Node v) const { |
|
770 return (*_dist)[v] != OperationTraits::infinity(); |
|
771 } |
|
772 |
|
773 // TODO: implement negative cycle |
|
774 // /// \brief Gives back a negative cycle. |
|
775 // /// |
|
776 // /// This function gives back a negative cycle. |
|
777 // /// If the algorithm have not found yet negative cycle it will give back |
|
778 // /// an empty path. |
|
779 // Path negativeCycle() { |
|
780 // typename Digraph::template NodeMap<int> state(*digraph, 0); |
|
781 // for (ActiveIt it(*this); it != INVALID; ++it) { |
|
782 // if (state[it] == 0) { |
|
783 // for (Node t = it; predArc(t) != INVALID; t = predNode(t)) { |
|
784 // if (state[t] == 0) { |
|
785 // state[t] = 1; |
|
786 // } else if (state[t] == 2) { |
|
787 // break; |
|
788 // } else { |
|
789 // p.clear(); |
|
790 // typename Path::Builder b(p); |
|
791 // b.setStartNode(t); |
|
792 // b.pushFront(predArc(t)); |
|
793 // for(Node s = predNode(t); s != t; s = predNode(s)) { |
|
794 // b.pushFront(predArc(s)); |
|
795 // } |
|
796 // b.commit(); |
|
797 // return true; |
|
798 // } |
|
799 // } |
|
800 // for (Node t = it; predArc(t) != INVALID; t = predNode(t)) { |
|
801 // if (state[t] == 1) { |
|
802 // state[t] = 2; |
|
803 // } else { |
|
804 // break; |
|
805 // } |
|
806 // } |
|
807 // } |
|
808 // } |
|
809 // return false; |
|
810 // } |
751 |
811 |
752 ///@} |
812 ///@} |
753 }; |
813 }; |
754 |
814 |
755 /// \brief Default traits class of BellmanFord function. |
815 /// \brief Default traits class of bellmanFord() function. |
756 /// |
816 /// |
757 /// Default traits class of BellmanFord function. |
817 /// Default traits class of bellmanFord() function. |
758 /// \param _Digraph Digraph type. |
818 /// \tparam GR The type of the digraph. |
759 /// \param _LengthMap Type of length map. |
819 /// \tparam LEN The type of the length map. |
760 template <typename _Digraph, typename _LengthMap> |
820 template <typename GR, typename LEN> |
761 struct BellmanFordWizardDefaultTraits { |
821 struct BellmanFordWizardDefaultTraits { |
762 /// \brief The digraph type the algorithm runs on. |
822 /// The type of the digraph the algorithm runs on. |
763 typedef _Digraph Digraph; |
823 typedef GR Digraph; |
764 |
824 |
765 /// \brief The type of the map that stores the arc lengths. |
825 /// \brief The type of the map that stores the arc lengths. |
766 /// |
826 /// |
767 /// The type of the map that stores the arc lengths. |
827 /// The type of the map that stores the arc lengths. |
768 /// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
828 /// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
769 typedef _LengthMap LengthMap; |
829 typedef LEN LengthMap; |
770 |
830 |
771 /// \brief The value type of the length map. |
831 /// The type of the arc lengths. |
772 typedef typename _LengthMap::Value Value; |
832 typedef typename LEN::Value Value; |
773 |
833 |
774 /// \brief Operation traits for Bellman-Ford algorithm. |
834 /// \brief Operation traits for Bellman-Ford algorithm. |
775 /// |
835 /// |
776 /// It defines the infinity type on the given Value type |
836 /// It defines the used operations and the infinity value for the |
777 /// and the used operation. |
837 /// given \c Value type. |
778 /// \see BellmanFordDefaultOperationTraits |
838 /// \see BellmanFordDefaultOperationTraits |
779 typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
839 typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
780 |
840 |
781 /// \brief The type of the map that stores the last |
841 /// \brief The type of the map that stores the last |
782 /// arcs of the shortest paths. |
842 /// arcs of the shortest paths. |
783 /// |
843 /// |
784 /// The type of the map that stores the last |
844 /// The type of the map that stores the last arcs of the shortest paths. |
785 /// arcs of the shortest paths. |
845 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
786 /// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
846 typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
787 typedef NullMap <typename _Digraph::Node,typename _Digraph::Arc> PredMap; |
847 |
788 |
848 /// \brief Instantiates a \c PredMap. |
789 /// \brief Instantiates a PredMap. |
|
790 /// |
849 /// |
791 /// This function instantiates a \ref PredMap. |
850 /// This function instantiates a \ref PredMap. |
792 static PredMap *createPredMap(const _Digraph &) { |
851 /// \param g is the digraph to which we would like to define the |
793 return new PredMap(); |
852 /// \ref PredMap. |
794 } |
853 static PredMap *createPredMap(const GR &g) { |
795 /// \brief The type of the map that stores the dists of the nodes. |
854 return new PredMap(g); |
796 /// |
855 } |
797 /// The type of the map that stores the dists of the nodes. |
856 |
798 /// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
857 /// \brief The type of the map that stores the distances of the nodes. |
799 typedef NullMap<typename Digraph::Node, Value> DistMap; |
858 /// |
800 /// \brief Instantiates a DistMap. |
859 /// The type of the map that stores the distances of the nodes. |
|
860 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
861 typedef typename GR::template NodeMap<Value> DistMap; |
|
862 |
|
863 /// \brief Instantiates a \c DistMap. |
801 /// |
864 /// |
802 /// This function instantiates a \ref DistMap. |
865 /// This function instantiates a \ref DistMap. |
803 static DistMap *createDistMap(const _Digraph &) { |
866 /// \param g is the digraph to which we would like to define the |
804 return new DistMap(); |
867 /// \ref DistMap. |
805 } |
868 static DistMap *createDistMap(const GR &g) { |
|
869 return new DistMap(g); |
|
870 } |
|
871 |
|
872 ///The type of the shortest paths. |
|
873 |
|
874 ///The type of the shortest paths. |
|
875 ///It must meet the \ref concepts::Path "Path" concept. |
|
876 typedef lemon::Path<Digraph> Path; |
806 }; |
877 }; |
807 |
878 |
808 /// \brief Default traits used by \ref BellmanFordWizard |
879 /// \brief Default traits class used by BellmanFordWizard. |
809 /// |
880 /// |
810 /// To make it easier to use BellmanFord algorithm |
881 /// Default traits class used by BellmanFordWizard. |
811 /// we have created a wizard class. |
882 /// \tparam GR The type of the digraph. |
812 /// This \ref BellmanFordWizard class needs default traits, |
883 /// \tparam LEN The type of the length map. |
813 /// as well as the \ref BellmanFord class. |
884 template <typename GR, typename LEN> |
814 /// The \ref BellmanFordWizardBase is a class to be the default traits of the |
|
815 /// \ref BellmanFordWizard class. |
|
816 /// \todo More named parameters are required... |
|
817 template<class _Digraph,class _LengthMap> |
|
818 class BellmanFordWizardBase |
885 class BellmanFordWizardBase |
819 : public BellmanFordWizardDefaultTraits<_Digraph,_LengthMap> { |
886 : public BellmanFordWizardDefaultTraits<GR, LEN> { |
820 |
887 |
821 typedef BellmanFordWizardDefaultTraits<_Digraph,_LengthMap> Base; |
888 typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
822 protected: |
889 protected: |
823 /// Type of the nodes in the digraph. |
890 // Type of the nodes in the digraph. |
824 typedef typename Base::Digraph::Node Node; |
891 typedef typename Base::Digraph::Node Node; |
825 |
892 |
826 /// Pointer to the underlying digraph. |
893 // Pointer to the underlying digraph. |
827 void *_graph; |
894 void *_graph; |
828 /// Pointer to the length map |
895 // Pointer to the length map |
829 void *_length; |
896 void *_length; |
830 ///Pointer to the map of predecessors arcs. |
897 // Pointer to the map of predecessors arcs. |
831 void *_pred; |
898 void *_pred; |
832 ///Pointer to the map of distances. |
899 // Pointer to the map of distances. |
833 void *_dist; |
900 void *_dist; |
834 ///Pointer to the source node. |
901 //Pointer to the shortest path to the target node. |
835 Node _source; |
902 void *_path; |
|
903 //Pointer to the distance of the target node. |
|
904 void *_di; |
836 |
905 |
837 public: |
906 public: |
838 /// Constructor. |
907 /// Constructor. |
839 |
908 |
840 /// This constructor does not require parameters, therefore it initiates |
909 /// This constructor does not require parameters, it initiates |
841 /// all of the attributes to default values (0, INVALID). |
910 /// all of the attributes to default values \c 0. |
842 BellmanFordWizardBase() : _graph(0), _length(0), _pred(0), |
911 BellmanFordWizardBase() : |
843 _dist(0), _source(INVALID) {} |
912 _graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
844 |
913 |
845 /// Constructor. |
914 /// Constructor. |
846 |
915 |
847 /// This constructor requires some parameters, |
916 /// This constructor requires two parameters, |
848 /// listed in the parameters list. |
917 /// others are initiated to \c 0. |
849 /// Others are initiated to 0. |
918 /// \param gr The digraph the algorithm runs on. |
850 /// \param digraph is the initial value of \ref _graph |
919 /// \param len The length map. |
851 /// \param length is the initial value of \ref _length |
920 BellmanFordWizardBase(const GR& gr, |
852 /// \param source is the initial value of \ref _source |
921 const LEN& len) : |
853 BellmanFordWizardBase(const _Digraph& digraph, |
922 _graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
854 const _LengthMap& length, |
923 _length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
855 Node source = INVALID) : |
924 _pred(0), _dist(0), _path(0), _di(0) {} |
856 _graph(reinterpret_cast<void*>(const_cast<_Digraph*>(&digraph))), |
|
857 _length(reinterpret_cast<void*>(const_cast<_LengthMap*>(&length))), |
|
858 _pred(0), _dist(0), _source(source) {} |
|
859 |
925 |
860 }; |
926 }; |
861 |
927 |
862 /// A class to make the usage of BellmanFord algorithm easier |
928 /// \brief Auxiliary class for the function-type interface of the |
863 |
929 /// \ref BellmanFord "Bellman-Ford" algorithm. |
864 /// This class is created to make it easier to use BellmanFord algorithm. |
930 /// |
865 /// It uses the functions and features of the plain \ref BellmanFord, |
931 /// This auxiliary class is created to implement the |
866 /// but it is much simpler to use it. |
932 /// \ref bellmanFord() "function-type interface" of the |
867 /// |
933 /// \ref BellmanFord "Bellman-Ford" algorithm. |
868 /// Simplicity means that the way to change the types defined |
934 /// It does not have own \ref run() method, it uses the |
869 /// in the traits class is based on functions that returns the new class |
935 /// functions and features of the plain \ref BellmanFord. |
870 /// and not on templatable built-in classes. |
936 /// |
871 /// When using the plain \ref BellmanFord |
937 /// This class should only be used through the \ref bellmanFord() |
872 /// the new class with the modified type comes from |
938 /// function, which makes it easier to use the algorithm. |
873 /// the original class by using the :: |
939 template<class TR> |
874 /// operator. In the case of \ref BellmanFordWizard only |
940 class BellmanFordWizard : public TR { |
875 /// a function have to be called and it will |
941 typedef TR Base; |
876 /// return the needed class. |
942 |
877 /// |
943 typedef typename TR::Digraph Digraph; |
878 /// It does not have own \ref run method. When its \ref run method is called |
|
879 /// it initiates a plain \ref BellmanFord class, and calls the \ref |
|
880 /// BellmanFord::run method of it. |
|
881 template<class _Traits> |
|
882 class BellmanFordWizard : public _Traits { |
|
883 typedef _Traits Base; |
|
884 |
|
885 ///The type of the underlying digraph. |
|
886 typedef typename _Traits::Digraph Digraph; |
|
887 |
944 |
888 typedef typename Digraph::Node Node; |
945 typedef typename Digraph::Node Node; |
889 typedef typename Digraph::NodeIt NodeIt; |
946 typedef typename Digraph::NodeIt NodeIt; |
890 typedef typename Digraph::Arc Arc; |
947 typedef typename Digraph::Arc Arc; |
891 typedef typename Digraph::OutArcIt ArcIt; |
948 typedef typename Digraph::OutArcIt ArcIt; |
892 |
949 |
893 ///The type of the map that stores the arc lengths. |
950 typedef typename TR::LengthMap LengthMap; |
894 typedef typename _Traits::LengthMap LengthMap; |
|
895 |
|
896 ///The type of the length of the arcs. |
|
897 typedef typename LengthMap::Value Value; |
951 typedef typename LengthMap::Value Value; |
898 |
952 typedef typename TR::PredMap PredMap; |
899 ///\brief The type of the map that stores the last |
953 typedef typename TR::DistMap DistMap; |
900 ///arcs of the shortest paths. |
954 typedef typename TR::Path Path; |
901 typedef typename _Traits::PredMap PredMap; |
|
902 |
|
903 ///The type of the map that stores the dists of the nodes. |
|
904 typedef typename _Traits::DistMap DistMap; |
|
905 |
955 |
906 public: |
956 public: |
907 /// Constructor. |
957 /// Constructor. |
908 BellmanFordWizard() : _Traits() {} |
958 BellmanFordWizard() : TR() {} |
909 |
959 |
910 /// \brief Constructor that requires parameters. |
960 /// \brief Constructor that requires parameters. |
911 /// |
961 /// |
912 /// Constructor that requires parameters. |
962 /// Constructor that requires parameters. |
913 /// These parameters will be the default values for the traits class. |
963 /// These parameters will be the default values for the traits class. |
914 BellmanFordWizard(const Digraph& digraph, const LengthMap& length, |
964 /// \param gr The digraph the algorithm runs on. |
915 Node src = INVALID) |
965 /// \param len The length map. |
916 : _Traits(digraph, length, src) {} |
966 BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
|
967 : TR(gr, len) {} |
917 |
968 |
918 /// \brief Copy constructor |
969 /// \brief Copy constructor |
919 BellmanFordWizard(const _Traits &b) : _Traits(b) {} |
970 BellmanFordWizard(const TR &b) : TR(b) {} |
920 |
971 |
921 ~BellmanFordWizard() {} |
972 ~BellmanFordWizard() {} |
922 |
973 |
923 /// \brief Runs BellmanFord algorithm from a given node. |
974 /// \brief Runs the Bellman-Ford algorithm from the given source node. |
924 /// |
975 /// |
925 /// Runs BellmanFord algorithm from a given node. |
976 /// This method runs the Bellman-Ford algorithm from the given source |
926 /// The node can be given by the \ref source function. |
977 /// node in order to compute the shortest path to each node. |
927 void run() { |
978 void run(Node s) { |
928 LEMON_ASSERT(Base::_source != INVALID, "Source node is not given"); |
979 BellmanFord<Digraph,LengthMap,TR> |
929 BellmanFord<Digraph,LengthMap,_Traits> |
|
930 bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
980 bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
931 *reinterpret_cast<const LengthMap*>(Base::_length)); |
981 *reinterpret_cast<const LengthMap*>(Base::_length)); |
932 if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
982 if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
933 if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
983 if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
934 bf.run(Base::_source); |
984 bf.run(s); |
935 } |
985 } |
936 |
986 |
937 /// \brief Runs BellmanFord algorithm from the given node. |
987 /// \brief Runs the Bellman-Ford algorithm to find the shortest path |
938 /// |
988 /// between \c s and \c t. |
939 /// Runs BellmanFord algorithm from the given node. |
989 /// |
940 /// \param src is the given source. |
990 /// This method runs the Bellman-Ford algorithm from node \c s |
941 void run(Node src) { |
991 /// in order to compute the shortest path to node \c t. |
942 Base::_source = src; |
992 /// Actually, it computes the shortest path to each node, but using |
943 run(); |
993 /// this function you can retrieve the distance and the shortest path |
|
994 /// for a single target node easier. |
|
995 /// |
|
996 /// \return \c true if \c t is reachable form \c s. |
|
997 bool run(Node s, Node t) { |
|
998 BellmanFord<Digraph,LengthMap,TR> |
|
999 bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
|
1000 *reinterpret_cast<const LengthMap*>(Base::_length)); |
|
1001 if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1002 if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1003 bf.run(s); |
|
1004 if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t); |
|
1005 if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t); |
|
1006 return bf.reached(t); |
944 } |
1007 } |
945 |
1008 |
946 template<class T> |
1009 template<class T> |
947 struct DefPredMapBase : public Base { |
1010 struct SetPredMapBase : public Base { |
948 typedef T PredMap; |
1011 typedef T PredMap; |
949 static PredMap *createPredMap(const Digraph &) { return 0; }; |
1012 static PredMap *createPredMap(const Digraph &) { return 0; }; |
950 DefPredMapBase(const _Traits &b) : _Traits(b) {} |
1013 SetPredMapBase(const TR &b) : TR(b) {} |
951 }; |
1014 }; |
952 |
1015 |
953 ///\brief \ref named-templ-param "Named parameter" |
1016 /// \brief \ref named-templ-param "Named parameter" for setting |
954 ///function for setting PredMap type |
1017 /// the predecessor map. |
955 /// |
1018 /// |
956 /// \ref named-templ-param "Named parameter" |
1019 /// \ref named-templ-param "Named parameter" for setting |
957 ///function for setting PredMap type |
1020 /// the map that stores the predecessor arcs of the nodes. |
958 /// |
|
959 template<class T> |
1021 template<class T> |
960 BellmanFordWizard<DefPredMapBase<T> > predMap(const T &t) |
1022 BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) { |
961 { |
|
962 Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1023 Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
963 return BellmanFordWizard<DefPredMapBase<T> >(*this); |
1024 return BellmanFordWizard<SetPredMapBase<T> >(*this); |
964 } |
1025 } |
965 |
1026 |
966 template<class T> |
1027 template<class T> |
967 struct DefDistMapBase : public Base { |
1028 struct SetDistMapBase : public Base { |
968 typedef T DistMap; |
1029 typedef T DistMap; |
969 static DistMap *createDistMap(const Digraph &) { return 0; }; |
1030 static DistMap *createDistMap(const Digraph &) { return 0; }; |
970 DefDistMapBase(const _Traits &b) : _Traits(b) {} |
1031 SetDistMapBase(const TR &b) : TR(b) {} |
971 }; |
1032 }; |
972 |
1033 |
973 ///\brief \ref named-templ-param "Named parameter" |
1034 /// \brief \ref named-templ-param "Named parameter" for setting |
974 ///function for setting DistMap type |
1035 /// the distance map. |
975 /// |
1036 /// |
976 /// \ref named-templ-param "Named parameter" |
1037 /// \ref named-templ-param "Named parameter" for setting |
977 ///function for setting DistMap type |
1038 /// the map that stores the distances of the nodes calculated |
978 /// |
1039 /// by the algorithm. |
979 template<class T> |
1040 template<class T> |
980 BellmanFordWizard<DefDistMapBase<T> > distMap(const T &t) { |
1041 BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) { |
981 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1042 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
982 return BellmanFordWizard<DefDistMapBase<T> >(*this); |
1043 return BellmanFordWizard<SetDistMapBase<T> >(*this); |
983 } |
1044 } |
984 |
1045 |
985 template<class T> |
1046 template<class T> |
986 struct DefOperationTraitsBase : public Base { |
1047 struct SetPathBase : public Base { |
987 typedef T OperationTraits; |
1048 typedef T Path; |
988 DefOperationTraitsBase(const _Traits &b) : _Traits(b) {} |
1049 SetPathBase(const TR &b) : TR(b) {} |
989 }; |
1050 }; |
990 |
1051 |
991 ///\brief \ref named-templ-param "Named parameter" |
1052 /// \brief \ref named-func-param "Named parameter" for getting |
992 ///function for setting OperationTraits type |
1053 /// the shortest path to the target node. |
993 /// |
1054 /// |
994 /// \ref named-templ-param "Named parameter" |
1055 /// \ref named-func-param "Named parameter" for getting |
995 ///function for setting OperationTraits type |
1056 /// the shortest path to the target node. |
996 /// |
|
997 template<class T> |
1057 template<class T> |
998 BellmanFordWizard<DefOperationTraitsBase<T> > distMap() { |
1058 BellmanFordWizard<SetPathBase<T> > path(const T &t) |
999 return BellmanFordWizard<DefDistMapBase<T> >(*this); |
1059 { |
1000 } |
1060 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1001 |
1061 return BellmanFordWizard<SetPathBase<T> >(*this); |
1002 /// \brief Sets the source node, from which the BellmanFord algorithm runs. |
1062 } |
1003 /// |
1063 |
1004 /// Sets the source node, from which the BellmanFord algorithm runs. |
1064 /// \brief \ref named-func-param "Named parameter" for getting |
1005 /// \param src is the source node. |
1065 /// the distance of the target node. |
1006 BellmanFordWizard<_Traits>& source(Node src) { |
1066 /// |
1007 Base::_source = src; |
1067 /// \ref named-func-param "Named parameter" for getting |
|
1068 /// the distance of the target node. |
|
1069 BellmanFordWizard dist(const Value &d) |
|
1070 { |
|
1071 Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
1008 return *this; |
1072 return *this; |
1009 } |
1073 } |
1010 |
1074 |
1011 }; |
1075 }; |
1012 |
1076 |
1013 /// \brief Function type interface for BellmanFord algorithm. |
1077 /// \brief Function type interface for the \ref BellmanFord "Bellman-Ford" |
|
1078 /// algorithm. |
1014 /// |
1079 /// |
1015 /// \ingroup shortest_path |
1080 /// \ingroup shortest_path |
1016 /// Function type interface for BellmanFord algorithm. |
1081 /// Function type interface for the \ref BellmanFord "Bellman-Ford" |
|
1082 /// algorithm. |
1017 /// |
1083 /// |
1018 /// This function also has several \ref named-templ-func-param |
1084 /// This function also has several \ref named-templ-func-param |
1019 /// "named parameters", they are declared as the members of class |
1085 /// "named parameters", they are declared as the members of class |
1020 /// \ref BellmanFordWizard. |
1086 /// \ref BellmanFordWizard. |
1021 /// The following |
1087 /// The following examples show how to use these parameters. |
1022 /// example shows how to use these parameters. |
1088 /// \code |
1023 ///\code |
1089 /// // Compute shortest path from node s to each node |
1024 /// bellmanford(g,length,source).predMap(preds).run(); |
1090 /// bellmanFord(g,length).predMap(preds).distMap(dists).run(s); |
1025 ///\endcode |
1091 /// |
|
1092 /// // Compute shortest path from s to t |
|
1093 /// bool reached = bellmanFord(g,length).path(p).dist(d).run(s,t); |
|
1094 /// \endcode |
1026 /// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()" |
1095 /// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()" |
1027 /// to the end of the parameter list. |
1096 /// to the end of the parameter list. |
1028 /// \sa BellmanFordWizard |
1097 /// \sa BellmanFordWizard |
1029 /// \sa BellmanFord |
1098 /// \sa BellmanFord |
1030 template<class _Digraph, class _LengthMap> |
1099 template<typename GR, typename LEN> |
1031 BellmanFordWizard<BellmanFordWizardBase<_Digraph,_LengthMap> > |
1100 BellmanFordWizard<BellmanFordWizardBase<GR,LEN> > |
1032 bellmanFord(const _Digraph& digraph, |
1101 bellmanFord(const GR& digraph, |
1033 const _LengthMap& length, |
1102 const LEN& length) |
1034 typename _Digraph::Node source = INVALID) { |
1103 { |
1035 return BellmanFordWizard<BellmanFordWizardBase<_Digraph,_LengthMap> > |
1104 return BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >(digraph, length); |
1036 (digraph, length, source); |
|
1037 } |
1105 } |
1038 |
1106 |
1039 } //END OF NAMESPACE LEMON |
1107 } //END OF NAMESPACE LEMON |
1040 |
1108 |
1041 #endif |
1109 #endif |