[Lemon-commits] Peter Kovacs: Improvements and unifications for ...
Lemon HG
hg at lemon.cs.elte.hu
Mon Aug 31 08:38:23 CEST 2009
details: http://lemon.cs.elte.hu/hg/lemon/rev/9496ed797f20
changeset: 750:9496ed797f20
user: Peter Kovacs <kpeter [at] inf.elte.hu>
date: Sun Aug 02 13:24:46 2009 +0200
description:
Improvements and unifications for BellmanFord (#51)
- Rework the function type interface to fit to dijkstra().
- Rename named template parameters (Def* -> Set*).
- Rename some private member variables (to start with an
underscore).
- Simplify template parameter names.
- Many unifications and improvements in the doc.
diffstat:
lemon/bellman_ford.h | 1118 ++++++++++++++++++++++++++++-------------------------
1 files changed, 593 insertions(+), 525 deletions(-)
diffs (truncated from 1549 to 300 lines):
diff --git a/lemon/bellman_ford.h b/lemon/bellman_ford.h
--- a/lemon/bellman_ford.h
+++ b/lemon/bellman_ford.h
@@ -16,18 +16,18 @@
*
*/
-#ifndef LEMON_BELMANN_FORD_H
-#define LEMON_BELMANN_FORD_H
+#ifndef LEMON_BELLMAN_FORD_H
+#define LEMON_BELLMAN_FORD_H
/// \ingroup shortest_path
/// \file
/// \brief Bellman-Ford algorithm.
-///
#include <lemon/bits/path_dump.h>
#include <lemon/core.h>
#include <lemon/error.h>
#include <lemon/maps.h>
+#include <lemon/path.h>
#include <limits>
@@ -35,15 +35,17 @@
/// \brief Default OperationTraits for the BellmanFord algorithm class.
///
- /// It defines all computational operations and constants which are
- /// used in the Bellman-Ford algorithm. The default implementation
- /// is based on the numeric_limits class. If the numeric type does not
- /// have infinity value then the maximum value is used as extremal
- /// infinity value.
+ /// This operation traits class defines all computational operations
+ /// and constants that are used in the Bellman-Ford algorithm.
+ /// The default implementation is based on the \c numeric_limits class.
+ /// If the numeric type does not have infinity value, then the maximum
+ /// value is used as extremal infinity value.
template <
- typename Value,
- bool has_infinity = std::numeric_limits<Value>::has_infinity>
+ typename V,
+ bool has_inf = std::numeric_limits<V>::has_infinity>
struct BellmanFordDefaultOperationTraits {
+ /// \e
+ typedef V Value;
/// \brief Gives back the zero value of the type.
static Value zero() {
return static_cast<Value>(0);
@@ -56,14 +58,16 @@
static Value plus(const Value& left, const Value& right) {
return left + right;
}
- /// \brief Gives back true only if the first value less than the second.
+ /// \brief Gives back \c true only if the first value is less than
+ /// the second.
static bool less(const Value& left, const Value& right) {
return left < right;
}
};
- template <typename Value>
- struct BellmanFordDefaultOperationTraits<Value, false> {
+ template <typename V>
+ struct BellmanFordDefaultOperationTraits<V, false> {
+ typedef V Value;
static Value zero() {
return static_cast<Value>(0);
}
@@ -82,26 +86,26 @@
/// \brief Default traits class of BellmanFord class.
///
/// Default traits class of BellmanFord class.
- /// \param _Digraph Digraph type.
- /// \param _LegthMap Type of length map.
- template<class _Digraph, class _LengthMap>
+ /// \param GR The type of the digraph.
+ /// \param LEN The type of the length map.
+ template<typename GR, typename LEN>
struct BellmanFordDefaultTraits {
- /// The digraph type the algorithm runs on.
- typedef _Digraph Digraph;
+ /// The type of the digraph the algorithm runs on.
+ typedef GR Digraph;
/// \brief The type of the map that stores the arc lengths.
///
/// The type of the map that stores the arc lengths.
- /// It must meet the \ref concepts::ReadMap "ReadMap" concept.
- typedef _LengthMap LengthMap;
+ /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
+ typedef LEN LengthMap;
- // The type of the length of the arcs.
- typedef typename _LengthMap::Value Value;
+ /// The type of the arc lengths.
+ typedef typename LEN::Value Value;
/// \brief Operation traits for Bellman-Ford algorithm.
///
- /// It defines the infinity type on the given Value type
- /// and the used operation.
+ /// It defines the used operations and the infinity value for the
+ /// given \c Value type.
/// \see BellmanFordDefaultOperationTraits
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
@@ -110,33 +114,31 @@
///
/// The type of the map that stores the last
/// arcs of the shortest paths.
- /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef typename Digraph::template NodeMap<typename _Digraph::Arc> PredMap;
+ /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
+ typedef typename GR::template NodeMap<typename GR::Arc> PredMap;
- /// \brief Instantiates a PredMap.
+ /// \brief Instantiates a \c PredMap.
///
/// This function instantiates a \ref PredMap.
- /// \param digraph is the digraph, to which we would like to define the PredMap.
- static PredMap *createPredMap(const _Digraph& digraph) {
- return new PredMap(digraph);
+ /// \param g is the digraph to which we would like to define the
+ /// \ref PredMap.
+ static PredMap *createPredMap(const GR& g) {
+ return new PredMap(g);
}
- /// \brief The type of the map that stores the dists of the nodes.
+ /// \brief The type of the map that stores the distances of the nodes.
///
- /// The type of the map that stores the dists of the nodes.
- /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
- ///
- typedef typename Digraph::template NodeMap<typename _LengthMap::Value>
- DistMap;
+ /// The type of the map that stores the distances of the nodes.
+ /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
+ typedef typename GR::template NodeMap<typename LEN::Value> DistMap;
- /// \brief Instantiates a DistMap.
+ /// \brief Instantiates a \c DistMap.
///
/// This function instantiates a \ref DistMap.
- /// \param digraph is the digraph, to which we would like to define the
- /// \ref DistMap
- static DistMap *createDistMap(const _Digraph& digraph) {
- return new DistMap(digraph);
+ /// \param g is the digraph to which we would like to define the
+ /// \ref DistMap.
+ static DistMap *createDistMap(const GR& g) {
+ return new DistMap(g);
}
};
@@ -144,106 +146,109 @@
/// \brief %BellmanFord algorithm class.
///
/// \ingroup shortest_path
- /// This class provides an efficient implementation of \c Bellman-Ford
- /// algorithm. The arc lengths are passed to the algorithm using a
+ /// This class provides an efficient implementation of the Bellman-Ford
+ /// algorithm. The maximum time complexity of the algorithm is
+ /// <tt>O(ne)</tt>.
+ ///
+ /// The Bellman-Ford algorithm solves the single-source shortest path
+ /// problem when the arcs can have negative lengths, but the digraph
+ /// should not contain directed cycles with negative total length.
+ /// If all arc costs are non-negative, consider to use the Dijkstra
+ /// algorithm instead, since it is more efficient.
+ ///
+ /// The arc lengths are passed to the algorithm using a
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any
- /// kind of length.
+ /// kind of length. The type of the length values is determined by the
+ /// \ref concepts::ReadMap::Value "Value" type of the length map.
///
- /// The Bellman-Ford algorithm solves the shortest path from one node
- /// problem when the arcs can have negative length but the digraph should
- /// not contain cycles with negative sum of length. If we can assume
- /// that all arc is non-negative in the digraph then the dijkstra algorithm
- /// should be used rather.
+ /// There is also a \ref bellmanFord() "function-type interface" for the
+ /// Bellman-Ford algorithm, which is convenient in the simplier cases and
+ /// it can be used easier.
///
- /// The maximal time complexity of the algorithm is \f$ O(ne) \f$.
- ///
- /// The type of the length is determined by the
- /// \ref concepts::ReadMap::Value "Value" of the length map.
- ///
- /// \param _Digraph The digraph type the algorithm runs on. The default value
- /// is \ref ListDigraph. The value of _Digraph is not used directly by
- /// BellmanFord, it is only passed to \ref BellmanFordDefaultTraits.
- /// \param _LengthMap This read-only ArcMap determines the lengths of the
- /// arcs. The default map type is \ref concepts::Digraph::ArcMap
- /// "Digraph::ArcMap<int>". The value of _LengthMap is not used directly
- /// by BellmanFord, it is only passed to \ref BellmanFordDefaultTraits.
- /// \param _Traits Traits class to set various data types used by the
- /// algorithm. The default traits class is \ref BellmanFordDefaultTraits
- /// "BellmanFordDefaultTraits<_Digraph,_LengthMap>". See \ref
- /// BellmanFordDefaultTraits for the documentation of a BellmanFord traits
- /// class.
+ /// \tparam GR The type of the digraph the algorithm runs on.
+ /// The default type is \ref ListDigraph.
+ /// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies
+ /// the lengths of the arcs. The default map type is
+ /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
#ifdef DOXYGEN
- template <typename _Digraph, typename _LengthMap, typename _Traits>
+ template <typename GR, typename LEN, typename TR>
#else
- template <typename _Digraph,
- typename _LengthMap=typename _Digraph::template ArcMap<int>,
- typename _Traits=BellmanFordDefaultTraits<_Digraph,_LengthMap> >
+ template <typename GR=ListDigraph,
+ typename LEN=typename GR::template ArcMap<int>,
+ typename TR=BellmanFordDefaultTraits<GR,LEN> >
#endif
class BellmanFord {
public:
- typedef _Traits Traits;
///The type of the underlying digraph.
- typedef typename _Traits::Digraph Digraph;
+ typedef typename TR::Digraph Digraph;
+
+ /// \brief The type of the arc lengths.
+ typedef typename TR::LengthMap::Value Value;
+ /// \brief The type of the map that stores the arc lengths.
+ typedef typename TR::LengthMap LengthMap;
+ /// \brief The type of the map that stores the last
+ /// arcs of the shortest paths.
+ typedef typename TR::PredMap PredMap;
+ /// \brief The type of the map that stores the distances of the nodes.
+ typedef typename TR::DistMap DistMap;
+ /// The type of the paths.
+ typedef PredMapPath<Digraph, PredMap> Path;
+ ///\brief The \ref BellmanFordDefaultOperationTraits
+ /// "operation traits class" of the algorithm.
+ typedef typename TR::OperationTraits OperationTraits;
+
+ ///The \ref BellmanFordDefaultTraits "traits class" of the algorithm.
+ typedef TR Traits;
+
+ private:
typedef typename Digraph::Node Node;
typedef typename Digraph::NodeIt NodeIt;
typedef typename Digraph::Arc Arc;
typedef typename Digraph::OutArcIt OutArcIt;
-
- /// \brief The type of the length of the arcs.
- typedef typename _Traits::LengthMap::Value Value;
- /// \brief The type of the map that stores the arc lengths.
- typedef typename _Traits::LengthMap LengthMap;
- /// \brief The type of the map that stores the last
- /// arcs of the shortest paths.
- typedef typename _Traits::PredMap PredMap;
- /// \brief The type of the map that stores the dists of the nodes.
- typedef typename _Traits::DistMap DistMap;
- /// \brief The operation traits.
- typedef typename _Traits::OperationTraits OperationTraits;
- private:
- /// Pointer to the underlying digraph.
- const Digraph *digraph;
- /// Pointer to the length map
- const LengthMap *length;
- ///Pointer to the map of predecessors arcs.
+
+ // Pointer to the underlying digraph.
+ const Digraph *_gr;
+ // Pointer to the length map
+ const LengthMap *_length;
+ // Pointer to the map of predecessors arcs.
PredMap *_pred;
- ///Indicates if \ref _pred is locally allocated (\c true) or not.
- bool local_pred;
- ///Pointer to the map of distances.
+ // Indicates if _pred is locally allocated (true) or not.
+ bool _local_pred;
+ // Pointer to the map of distances.
DistMap *_dist;
- ///Indicates if \ref _dist is locally allocated (\c true) or not.
- bool local_dist;
+ // Indicates if _dist is locally allocated (true) or not.
+ bool _local_dist;
typedef typename Digraph::template NodeMap<bool> MaskMap;
MaskMap *_mask;
std::vector<Node> _process;
- /// Creates the maps if necessary.
+ // Creates the maps if necessary.
void create_maps() {
if(!_pred) {
More information about the Lemon-commits
mailing list