0
2
0
| ... | ... |
@@ -7,177 +7,128 @@ |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BELLMAN_FORD_H |
| 20 | 20 |
#define LEMON_BELLMAN_FORD_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup shortest_path |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Bellman-Ford algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 |
#include <lemon/tolerance.h> |
|
| 32 | 31 |
#include <lemon/path.h> |
| 33 | 32 |
|
| 34 | 33 |
#include <limits> |
| 35 | 34 |
|
| 36 | 35 |
namespace lemon {
|
| 37 | 36 |
|
| 38 |
/// \brief Default |
|
| 37 |
/// \brief Default OperationTraits for the BellmanFord algorithm class. |
|
| 39 | 38 |
/// |
| 40 | 39 |
/// This operation traits class defines all computational operations |
| 41 | 40 |
/// and constants that are used in the Bellman-Ford algorithm. |
| 42 | 41 |
/// The default implementation is based on the \c numeric_limits class. |
| 43 | 42 |
/// If the numeric type does not have infinity value, then the maximum |
| 44 | 43 |
/// value is used as extremal infinity value. |
| 45 |
/// |
|
| 46 |
/// \see BellmanFordToleranceOperationTraits |
|
| 47 | 44 |
template < |
| 48 | 45 |
typename V, |
| 49 | 46 |
bool has_inf = std::numeric_limits<V>::has_infinity> |
| 50 | 47 |
struct BellmanFordDefaultOperationTraits {
|
| 51 |
/// \ |
|
| 48 |
/// \e |
|
| 52 | 49 |
typedef V Value; |
| 53 | 50 |
/// \brief Gives back the zero value of the type. |
| 54 | 51 |
static Value zero() {
|
| 55 | 52 |
return static_cast<Value>(0); |
| 56 | 53 |
} |
| 57 | 54 |
/// \brief Gives back the positive infinity value of the type. |
| 58 | 55 |
static Value infinity() {
|
| 59 | 56 |
return std::numeric_limits<Value>::infinity(); |
| 60 | 57 |
} |
| 61 | 58 |
/// \brief Gives back the sum of the given two elements. |
| 62 | 59 |
static Value plus(const Value& left, const Value& right) {
|
| 63 | 60 |
return left + right; |
| 64 | 61 |
} |
| 65 | 62 |
/// \brief Gives back \c true only if the first value is less than |
| 66 | 63 |
/// the second. |
| 67 | 64 |
static bool less(const Value& left, const Value& right) {
|
| 68 | 65 |
return left < right; |
| 69 | 66 |
} |
| 70 | 67 |
}; |
| 71 | 68 |
|
| 72 | 69 |
template <typename V> |
| 73 | 70 |
struct BellmanFordDefaultOperationTraits<V, false> {
|
| 74 | 71 |
typedef V Value; |
| 75 | 72 |
static Value zero() {
|
| 76 | 73 |
return static_cast<Value>(0); |
| 77 | 74 |
} |
| 78 | 75 |
static Value infinity() {
|
| 79 | 76 |
return std::numeric_limits<Value>::max(); |
| 80 | 77 |
} |
| 81 | 78 |
static Value plus(const Value& left, const Value& right) {
|
| 82 | 79 |
if (left == infinity() || right == infinity()) return infinity(); |
| 83 | 80 |
return left + right; |
| 84 | 81 |
} |
| 85 | 82 |
static bool less(const Value& left, const Value& right) {
|
| 86 | 83 |
return left < right; |
| 87 | 84 |
} |
| 88 | 85 |
}; |
| 89 | 86 |
|
| 90 |
/// \brief Operation traits for the BellmanFord algorithm class |
|
| 91 |
/// using tolerance. |
|
| 92 |
/// |
|
| 93 |
/// This operation traits class defines all computational operations |
|
| 94 |
/// and constants that are used in the Bellman-Ford algorithm. |
|
| 95 |
/// The only difference between this implementation and |
|
| 96 |
/// \ref BellmanFordDefaultOperationTraits is that this class uses |
|
| 97 |
/// the \ref Tolerance "tolerance technique" in its \ref less() |
|
| 98 |
/// function. |
|
| 99 |
/// |
|
| 100 |
/// \tparam V The value type. |
|
| 101 |
/// \tparam eps The epsilon value for the \ref less() function. |
|
| 102 |
/// By default, it is the epsilon value used by \ref Tolerance |
|
| 103 |
/// "Tolerance<V>". |
|
| 104 |
/// |
|
| 105 |
/// \see BellmanFordDefaultOperationTraits |
|
| 106 |
#ifdef DOXYGEN |
|
| 107 |
template <typename V, V eps> |
|
| 108 |
#else |
|
| 109 |
template < |
|
| 110 |
typename V, |
|
| 111 |
V eps = Tolerance<V>::def_epsilon> |
|
| 112 |
#endif |
|
| 113 |
struct BellmanFordToleranceOperationTraits {
|
|
| 114 |
/// \brief Value type for the algorithm. |
|
| 115 |
typedef V Value; |
|
| 116 |
/// \brief Gives back the zero value of the type. |
|
| 117 |
static Value zero() {
|
|
| 118 |
return static_cast<Value>(0); |
|
| 119 |
} |
|
| 120 |
/// \brief Gives back the positive infinity value of the type. |
|
| 121 |
static Value infinity() {
|
|
| 122 |
return std::numeric_limits<Value>::infinity(); |
|
| 123 |
} |
|
| 124 |
/// \brief Gives back the sum of the given two elements. |
|
| 125 |
static Value plus(const Value& left, const Value& right) {
|
|
| 126 |
return left + right; |
|
| 127 |
} |
|
| 128 |
/// \brief Gives back \c true only if the first value is less than |
|
| 129 |
/// the second. |
|
| 130 |
static bool less(const Value& left, const Value& right) {
|
|
| 131 |
return left + eps < right; |
|
| 132 |
} |
|
| 133 |
}; |
|
| 134 |
|
|
| 135 | 87 |
/// \brief Default traits class of BellmanFord class. |
| 136 | 88 |
/// |
| 137 | 89 |
/// Default traits class of BellmanFord class. |
| 138 | 90 |
/// \param GR The type of the digraph. |
| 139 | 91 |
/// \param LEN The type of the length map. |
| 140 | 92 |
template<typename GR, typename LEN> |
| 141 | 93 |
struct BellmanFordDefaultTraits {
|
| 142 | 94 |
/// The type of the digraph the algorithm runs on. |
| 143 | 95 |
typedef GR Digraph; |
| 144 | 96 |
|
| 145 | 97 |
/// \brief The type of the map that stores the arc lengths. |
| 146 | 98 |
/// |
| 147 | 99 |
/// The type of the map that stores the arc lengths. |
| 148 | 100 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 149 | 101 |
typedef LEN LengthMap; |
| 150 | 102 |
|
| 151 | 103 |
/// The type of the arc lengths. |
| 152 | 104 |
typedef typename LEN::Value Value; |
| 153 | 105 |
|
| 154 | 106 |
/// \brief Operation traits for Bellman-Ford algorithm. |
| 155 | 107 |
/// |
| 156 | 108 |
/// It defines the used operations and the infinity value for the |
| 157 | 109 |
/// given \c Value type. |
| 158 |
/// \see BellmanFordDefaultOperationTraits, |
|
| 159 |
/// BellmanFordToleranceOperationTraits |
|
| 110 |
/// \see BellmanFordDefaultOperationTraits |
|
| 160 | 111 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
| 161 | 112 |
|
| 162 | 113 |
/// \brief The type of the map that stores the last arcs of the |
| 163 | 114 |
/// shortest paths. |
| 164 | 115 |
/// |
| 165 | 116 |
/// The type of the map that stores the last |
| 166 | 117 |
/// arcs of the shortest paths. |
| 167 | 118 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 168 | 119 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
| 169 | 120 |
|
| 170 | 121 |
/// \brief Instantiates a \c PredMap. |
| 171 | 122 |
/// |
| 172 | 123 |
/// This function instantiates a \ref PredMap. |
| 173 | 124 |
/// \param g is the digraph to which we would like to define the |
| 174 | 125 |
/// \ref PredMap. |
| 175 | 126 |
static PredMap *createPredMap(const GR& g) {
|
| 176 | 127 |
return new PredMap(g); |
| 177 | 128 |
} |
| 178 | 129 |
|
| 179 | 130 |
/// \brief The type of the map that stores the distances of the nodes. |
| 180 | 131 |
/// |
| 181 | 132 |
/// The type of the map that stores the distances of the nodes. |
| 182 | 133 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 183 | 134 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
| ... | ... |
@@ -865,50 +816,49 @@ |
| 865 | 816 |
|
| 866 | 817 |
/// \brief Default traits class of bellmanFord() function. |
| 867 | 818 |
/// |
| 868 | 819 |
/// Default traits class of bellmanFord() function. |
| 869 | 820 |
/// \tparam GR The type of the digraph. |
| 870 | 821 |
/// \tparam LEN The type of the length map. |
| 871 | 822 |
template <typename GR, typename LEN> |
| 872 | 823 |
struct BellmanFordWizardDefaultTraits {
|
| 873 | 824 |
/// The type of the digraph the algorithm runs on. |
| 874 | 825 |
typedef GR Digraph; |
| 875 | 826 |
|
| 876 | 827 |
/// \brief The type of the map that stores the arc lengths. |
| 877 | 828 |
/// |
| 878 | 829 |
/// The type of the map that stores the arc lengths. |
| 879 | 830 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 880 | 831 |
typedef LEN LengthMap; |
| 881 | 832 |
|
| 882 | 833 |
/// The type of the arc lengths. |
| 883 | 834 |
typedef typename LEN::Value Value; |
| 884 | 835 |
|
| 885 | 836 |
/// \brief Operation traits for Bellman-Ford algorithm. |
| 886 | 837 |
/// |
| 887 | 838 |
/// It defines the used operations and the infinity value for the |
| 888 | 839 |
/// given \c Value type. |
| 889 |
/// \see BellmanFordDefaultOperationTraits, |
|
| 890 |
/// BellmanFordToleranceOperationTraits |
|
| 840 |
/// \see BellmanFordDefaultOperationTraits |
|
| 891 | 841 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
| 892 | 842 |
|
| 893 | 843 |
/// \brief The type of the map that stores the last |
| 894 | 844 |
/// arcs of the shortest paths. |
| 895 | 845 |
/// |
| 896 | 846 |
/// The type of the map that stores the last arcs of the shortest paths. |
| 897 | 847 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 898 | 848 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
| 899 | 849 |
|
| 900 | 850 |
/// \brief Instantiates a \c PredMap. |
| 901 | 851 |
/// |
| 902 | 852 |
/// This function instantiates a \ref PredMap. |
| 903 | 853 |
/// \param g is the digraph to which we would like to define the |
| 904 | 854 |
/// \ref PredMap. |
| 905 | 855 |
static PredMap *createPredMap(const GR &g) {
|
| 906 | 856 |
return new PredMap(g); |
| 907 | 857 |
} |
| 908 | 858 |
|
| 909 | 859 |
/// \brief The type of the map that stores the distances of the nodes. |
| 910 | 860 |
/// |
| 911 | 861 |
/// The type of the map that stores the distances of the nodes. |
| 912 | 862 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 913 | 863 |
typedef typename GR::template NodeMap<Value> DistMap; |
| 914 | 864 |
| ... | ... |
@@ -83,49 +83,48 @@ |
| 83 | 83 |
bf_test.addSource(s); |
| 84 | 84 |
bf_test.addSource(s, 1); |
| 85 | 85 |
b = bf_test.processNextRound(); |
| 86 | 86 |
b = bf_test.processNextWeakRound(); |
| 87 | 87 |
|
| 88 | 88 |
bf_test.start(); |
| 89 | 89 |
bf_test.checkedStart(); |
| 90 | 90 |
bf_test.limitedStart(k); |
| 91 | 91 |
|
| 92 | 92 |
l = const_bf_test.dist(t); |
| 93 | 93 |
e = const_bf_test.predArc(t); |
| 94 | 94 |
s = const_bf_test.predNode(t); |
| 95 | 95 |
b = const_bf_test.reached(t); |
| 96 | 96 |
d = const_bf_test.distMap(); |
| 97 | 97 |
p = const_bf_test.predMap(); |
| 98 | 98 |
pp = const_bf_test.path(t); |
| 99 | 99 |
pp = const_bf_test.negativeCycle(); |
| 100 | 100 |
|
| 101 | 101 |
for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
|
| 102 | 102 |
} |
| 103 | 103 |
{
|
| 104 | 104 |
BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 105 | 105 |
::SetDistMap<concepts::ReadWriteMap<Node,Value> > |
| 106 | 106 |
::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> > |
| 107 |
::SetOperationTraits<BellmanFordToleranceOperationTraits<Value, 0> > |
|
| 108 | 107 |
::Create bf_test(gr,length); |
| 109 | 108 |
|
| 110 | 109 |
LengthMap length_map; |
| 111 | 110 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
| 112 | 111 |
concepts::ReadWriteMap<Node,Value> dist_map; |
| 113 | 112 |
|
| 114 | 113 |
bf_test |
| 115 | 114 |
.lengthMap(length_map) |
| 116 | 115 |
.predMap(pred_map) |
| 117 | 116 |
.distMap(dist_map); |
| 118 | 117 |
|
| 119 | 118 |
bf_test.run(s); |
| 120 | 119 |
bf_test.run(s,k); |
| 121 | 120 |
|
| 122 | 121 |
bf_test.init(); |
| 123 | 122 |
bf_test.addSource(s); |
| 124 | 123 |
bf_test.addSource(s, 1); |
| 125 | 124 |
b = bf_test.processNextRound(); |
| 126 | 125 |
b = bf_test.processNextWeakRound(); |
| 127 | 126 |
|
| 128 | 127 |
bf_test.start(); |
| 129 | 128 |
bf_test.checkedStart(); |
| 130 | 129 |
bf_test.limitedStart(k); |
| 131 | 130 |
|
0 comments (0 inline)