[Lemon-commits] Alpar Juttner: Merge

Lemon HG hg at lemon.cs.elte.hu
Mon Aug 31 08:38:24 CEST 2009


details:   http://lemon.cs.elte.hu/hg/lemon/rev/6f7c1052d260
changeset: 753:6f7c1052d260
user:      Alpar Juttner <alpar [at] cs.elte.hu>
date:      Mon Aug 31 08:32:25 2009 +0200
description:
	Merge

diffstat:

 lemon/Makefile.am         |     1 +
 lemon/bellman_ford.h      |  1100 +++++++++++++++++++++++++++++++++++++++++++++++
 test/CMakeLists.txt       |     1 +
 test/Makefile.am          |     2 +
 test/bellman_ford_test.cc |   283 ++++++++++++
 5 files changed, 1387 insertions(+), 0 deletions(-)

diffs (truncated from 1434 to 300 lines):

diff --git a/lemon/Makefile.am b/lemon/Makefile.am
--- a/lemon/Makefile.am
+++ b/lemon/Makefile.am
@@ -57,6 +57,7 @@
 	lemon/adaptors.h \
 	lemon/arg_parser.h \
 	lemon/assert.h \
+	lemon/bellman_ford.h \
 	lemon/bfs.h \
 	lemon/bin_heap.h \
 	lemon/bucket_heap.h \
diff --git a/lemon/bellman_ford.h b/lemon/bellman_ford.h
new file mode 100644
--- /dev/null
+++ b/lemon/bellman_ford.h
@@ -0,0 +1,1100 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2008
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#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>
+
+namespace lemon {
+
+  /// \brief Default OperationTraits for the BellmanFord algorithm class.
+  ///  
+  /// 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 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);
+    }
+    /// \brief Gives back the positive infinity value of the type.
+    static Value infinity() {
+      return std::numeric_limits<Value>::infinity();
+    }
+    /// \brief Gives back the sum of the given two elements.
+    static Value plus(const Value& left, const Value& right) {
+      return left + right;
+    }
+    /// \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 V>
+  struct BellmanFordDefaultOperationTraits<V, false> {
+    typedef V Value;
+    static Value zero() {
+      return static_cast<Value>(0);
+    }
+    static Value infinity() {
+      return std::numeric_limits<Value>::max();
+    }
+    static Value plus(const Value& left, const Value& right) {
+      if (left == infinity() || right == infinity()) return infinity();
+      return left + right;
+    }
+    static bool less(const Value& left, const Value& right) {
+      return left < right;
+    }
+  };
+  
+  /// \brief Default traits class of BellmanFord class.
+  ///
+  /// Default traits class of BellmanFord class.
+  /// \param GR The type of the digraph.
+  /// \param LEN The type of the length map.
+  template<typename GR, typename LEN>
+  struct BellmanFordDefaultTraits {
+    /// 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 conform to the \ref concepts::ReadMap "ReadMap" concept.
+    typedef LEN LengthMap;
+
+    /// The type of the arc lengths.
+    typedef typename LEN::Value Value;
+
+    /// \brief Operation traits for Bellman-Ford algorithm.
+    ///
+    /// It defines the used operations and the infinity value for the
+    /// given \c Value type.
+    /// \see BellmanFordDefaultOperationTraits
+    typedef BellmanFordDefaultOperationTraits<Value> OperationTraits;
+ 
+    /// \brief The type of the map that stores the last arcs of the 
+    /// shortest paths.
+    /// 
+    /// The type of the map that stores the last
+    /// arcs of the shortest paths.
+    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
+    typedef typename GR::template NodeMap<typename GR::Arc> PredMap;
+
+    /// \brief Instantiates a \c PredMap.
+    /// 
+    /// This function instantiates a \ref PredMap. 
+    /// \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 distances of the nodes.
+    ///
+    /// 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 \c DistMap.
+    ///
+    /// This function instantiates a \ref DistMap. 
+    /// \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);
+    }
+
+  };
+  
+  /// \brief %BellmanFord algorithm class.
+  ///
+  /// \ingroup shortest_path
+  /// 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. The type of the length values is determined by the
+  /// \ref concepts::ReadMap::Value "Value" type of the length map.
+  ///
+  /// 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.
+  ///
+  /// \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 GR, typename LEN, typename TR>
+#else
+  template <typename GR=ListDigraph,
+            typename LEN=typename GR::template ArcMap<int>,
+            typename TR=BellmanFordDefaultTraits<GR,LEN> >
+#endif
+  class BellmanFord {
+  public:
+
+    ///The type of the underlying 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;
+
+    // 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 _pred is locally allocated (true) or not.
+    bool _local_pred;
+    // Pointer to the map of distances.
+    DistMap *_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.
+    void create_maps() {
+      if(!_pred) {
+	_local_pred = true;
+	_pred = Traits::createPredMap(*_gr);
+      }
+      if(!_dist) {
+	_local_dist = true;
+	_dist = Traits::createDistMap(*_gr);
+      }
+      _mask = new MaskMap(*_gr, false);
+    }
+    
+  public :
+ 
+    typedef BellmanFord Create;
+
+    /// \name Named Template Parameters
+
+    ///@{
+
+    template <class T>
+    struct SetPredMapTraits : public Traits {
+      typedef T PredMap;
+      static PredMap *createPredMap(const Digraph&) {
+        LEMON_ASSERT(false, "PredMap is not initialized");
+        return 0; // ignore warnings
+      }
+    };
+
+    /// \brief \ref named-templ-param "Named parameter" for setting
+    /// \c PredMap type.
+    ///
+    /// \ref named-templ-param "Named parameter" for setting
+    /// \c PredMap type.
+    /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
+    template <class T>
+    struct SetPredMap 
+      : public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > {
+      typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create;
+    };
+    
+    template <class T>
+    struct SetDistMapTraits : public Traits {
+      typedef T DistMap;
+      static DistMap *createDistMap(const Digraph&) {
+        LEMON_ASSERT(false, "DistMap is not initialized");
+        return 0; // ignore warnings
+      }
+    };
+
+    /// \brief \ref named-templ-param "Named parameter" for setting
+    /// \c DistMap type.
+    ///
+    /// \ref named-templ-param "Named parameter" for setting
+    /// \c DistMap type.



More information about the Lemon-commits mailing list