0
12
0
... | ... |
@@ -126,96 +126,101 @@ |
126 | 126 |
static PredMap *createPredMap(const GR& g) { |
127 | 127 |
return new PredMap(g); |
128 | 128 |
} |
129 | 129 |
|
130 | 130 |
/// \brief The type of the map that stores the distances of the nodes. |
131 | 131 |
/// |
132 | 132 |
/// The type of the map that stores the distances of the nodes. |
133 | 133 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
134 | 134 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
135 | 135 |
|
136 | 136 |
/// \brief Instantiates a \c DistMap. |
137 | 137 |
/// |
138 | 138 |
/// This function instantiates a \ref DistMap. |
139 | 139 |
/// \param g is the digraph to which we would like to define the |
140 | 140 |
/// \ref DistMap. |
141 | 141 |
static DistMap *createDistMap(const GR& g) { |
142 | 142 |
return new DistMap(g); |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
/// \brief %BellmanFord algorithm class. |
148 | 148 |
/// |
149 | 149 |
/// \ingroup shortest_path |
150 | 150 |
/// This class provides an efficient implementation of the Bellman-Ford |
151 | 151 |
/// algorithm. The maximum time complexity of the algorithm is |
152 | 152 |
/// <tt>O(ne)</tt>. |
153 | 153 |
/// |
154 | 154 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
155 | 155 |
/// problem when the arcs can have negative lengths, but the digraph |
156 | 156 |
/// should not contain directed cycles with negative total length. |
157 | 157 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
158 | 158 |
/// algorithm instead, since it is more efficient. |
159 | 159 |
/// |
160 | 160 |
/// The arc lengths are passed to the algorithm using a |
161 | 161 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
162 | 162 |
/// kind of length. The type of the length values is determined by the |
163 | 163 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
164 | 164 |
/// |
165 | 165 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
166 | 166 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
167 | 167 |
/// it can be used easier. |
168 | 168 |
/// |
169 | 169 |
/// \tparam GR The type of the digraph the algorithm runs on. |
170 | 170 |
/// The default type is \ref ListDigraph. |
171 | 171 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
172 | 172 |
/// the lengths of the arcs. The default map type is |
173 | 173 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
174 |
/// \tparam TR The traits class that defines various types used by the |
|
175 |
/// algorithm. By default, it is \ref BellmanFordDefaultTraits |
|
176 |
/// "BellmanFordDefaultTraits<GR, LEN>". |
|
177 |
/// In most cases, this parameter should not be set directly, |
|
178 |
/// consider to use the named template parameters instead. |
|
174 | 179 |
#ifdef DOXYGEN |
175 | 180 |
template <typename GR, typename LEN, typename TR> |
176 | 181 |
#else |
177 | 182 |
template <typename GR=ListDigraph, |
178 | 183 |
typename LEN=typename GR::template ArcMap<int>, |
179 | 184 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
180 | 185 |
#endif |
181 | 186 |
class BellmanFord { |
182 | 187 |
public: |
183 | 188 |
|
184 | 189 |
///The type of the underlying digraph. |
185 | 190 |
typedef typename TR::Digraph Digraph; |
186 | 191 |
|
187 | 192 |
/// \brief The type of the arc lengths. |
188 | 193 |
typedef typename TR::LengthMap::Value Value; |
189 | 194 |
/// \brief The type of the map that stores the arc lengths. |
190 | 195 |
typedef typename TR::LengthMap LengthMap; |
191 | 196 |
/// \brief The type of the map that stores the last |
192 | 197 |
/// arcs of the shortest paths. |
193 | 198 |
typedef typename TR::PredMap PredMap; |
194 | 199 |
/// \brief The type of the map that stores the distances of the nodes. |
195 | 200 |
typedef typename TR::DistMap DistMap; |
196 | 201 |
/// The type of the paths. |
197 | 202 |
typedef PredMapPath<Digraph, PredMap> Path; |
198 | 203 |
///\brief The \ref BellmanFordDefaultOperationTraits |
199 | 204 |
/// "operation traits class" of the algorithm. |
200 | 205 |
typedef typename TR::OperationTraits OperationTraits; |
201 | 206 |
|
202 | 207 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
203 | 208 |
typedef TR Traits; |
204 | 209 |
|
205 | 210 |
private: |
206 | 211 |
|
207 | 212 |
typedef typename Digraph::Node Node; |
208 | 213 |
typedef typename Digraph::NodeIt NodeIt; |
209 | 214 |
typedef typename Digraph::Arc Arc; |
210 | 215 |
typedef typename Digraph::OutArcIt OutArcIt; |
211 | 216 |
|
212 | 217 |
// Pointer to the underlying digraph. |
213 | 218 |
const Digraph *_gr; |
214 | 219 |
// Pointer to the length map |
215 | 220 |
const LengthMap *_length; |
216 | 221 |
// Pointer to the map of predecessors arcs. |
217 | 222 |
PredMap *_pred; |
218 | 223 |
// Indicates if _pred is locally allocated (true) or not. |
219 | 224 |
bool _local_pred; |
220 | 225 |
// Pointer to the map of distances. |
221 | 226 |
DistMap *_dist; |
... | ... |
@@ -888,96 +893,99 @@ |
888 | 893 |
typedef typename Base::Digraph::Node Node; |
889 | 894 |
|
890 | 895 |
// Pointer to the underlying digraph. |
891 | 896 |
void *_graph; |
892 | 897 |
// Pointer to the length map |
893 | 898 |
void *_length; |
894 | 899 |
// Pointer to the map of predecessors arcs. |
895 | 900 |
void *_pred; |
896 | 901 |
// Pointer to the map of distances. |
897 | 902 |
void *_dist; |
898 | 903 |
//Pointer to the shortest path to the target node. |
899 | 904 |
void *_path; |
900 | 905 |
//Pointer to the distance of the target node. |
901 | 906 |
void *_di; |
902 | 907 |
|
903 | 908 |
public: |
904 | 909 |
/// Constructor. |
905 | 910 |
|
906 | 911 |
/// This constructor does not require parameters, it initiates |
907 | 912 |
/// all of the attributes to default values \c 0. |
908 | 913 |
BellmanFordWizardBase() : |
909 | 914 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
910 | 915 |
|
911 | 916 |
/// Constructor. |
912 | 917 |
|
913 | 918 |
/// This constructor requires two parameters, |
914 | 919 |
/// others are initiated to \c 0. |
915 | 920 |
/// \param gr The digraph the algorithm runs on. |
916 | 921 |
/// \param len The length map. |
917 | 922 |
BellmanFordWizardBase(const GR& gr, |
918 | 923 |
const LEN& len) : |
919 | 924 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
920 | 925 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
921 | 926 |
_pred(0), _dist(0), _path(0), _di(0) {} |
922 | 927 |
|
923 | 928 |
}; |
924 | 929 |
|
925 | 930 |
/// \brief Auxiliary class for the function-type interface of the |
926 | 931 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
927 | 932 |
/// |
928 | 933 |
/// This auxiliary class is created to implement the |
929 | 934 |
/// \ref bellmanFord() "function-type interface" of the |
930 | 935 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
931 | 936 |
/// It does not have own \ref run() method, it uses the |
932 | 937 |
/// functions and features of the plain \ref BellmanFord. |
933 | 938 |
/// |
934 | 939 |
/// This class should only be used through the \ref bellmanFord() |
935 | 940 |
/// function, which makes it easier to use the algorithm. |
941 |
/// |
|
942 |
/// \tparam TR The traits class that defines various types used by the |
|
943 |
/// algorithm. |
|
936 | 944 |
template<class TR> |
937 | 945 |
class BellmanFordWizard : public TR { |
938 | 946 |
typedef TR Base; |
939 | 947 |
|
940 | 948 |
typedef typename TR::Digraph Digraph; |
941 | 949 |
|
942 | 950 |
typedef typename Digraph::Node Node; |
943 | 951 |
typedef typename Digraph::NodeIt NodeIt; |
944 | 952 |
typedef typename Digraph::Arc Arc; |
945 | 953 |
typedef typename Digraph::OutArcIt ArcIt; |
946 | 954 |
|
947 | 955 |
typedef typename TR::LengthMap LengthMap; |
948 | 956 |
typedef typename LengthMap::Value Value; |
949 | 957 |
typedef typename TR::PredMap PredMap; |
950 | 958 |
typedef typename TR::DistMap DistMap; |
951 | 959 |
typedef typename TR::Path Path; |
952 | 960 |
|
953 | 961 |
public: |
954 | 962 |
/// Constructor. |
955 | 963 |
BellmanFordWizard() : TR() {} |
956 | 964 |
|
957 | 965 |
/// \brief Constructor that requires parameters. |
958 | 966 |
/// |
959 | 967 |
/// Constructor that requires parameters. |
960 | 968 |
/// These parameters will be the default values for the traits class. |
961 | 969 |
/// \param gr The digraph the algorithm runs on. |
962 | 970 |
/// \param len The length map. |
963 | 971 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
964 | 972 |
: TR(gr, len) {} |
965 | 973 |
|
966 | 974 |
/// \brief Copy constructor |
967 | 975 |
BellmanFordWizard(const TR &b) : TR(b) {} |
968 | 976 |
|
969 | 977 |
~BellmanFordWizard() {} |
970 | 978 |
|
971 | 979 |
/// \brief Runs the Bellman-Ford algorithm from the given source node. |
972 | 980 |
/// |
973 | 981 |
/// This method runs the Bellman-Ford algorithm from the given source |
974 | 982 |
/// node in order to compute the shortest path to each node. |
975 | 983 |
void run(Node s) { |
976 | 984 |
BellmanFord<Digraph,LengthMap,TR> |
977 | 985 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
978 | 986 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
979 | 987 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
980 | 988 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
981 | 989 |
bf.run(s); |
982 | 990 |
} |
983 | 991 |
... | ... |
@@ -76,96 +76,101 @@ |
76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
77 | 77 |
#endif |
78 | 78 |
{ |
79 | 79 |
return new ProcessedMap(); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 | 87 |
///Instantiates a \c ReachedMap. |
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref ReachedMap. |
90 | 90 |
///\param g is the digraph, to which |
91 | 91 |
///we would like to define the \ref ReachedMap. |
92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
93 | 93 |
{ |
94 | 94 |
return new ReachedMap(g); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
///The type of the map that stores the distances of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the distances of the nodes. |
100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 | 102 |
///Instantiates a \c DistMap. |
103 | 103 |
|
104 | 104 |
///This function instantiates a \ref DistMap. |
105 | 105 |
///\param g is the digraph, to which we would like to define the |
106 | 106 |
///\ref DistMap. |
107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(g); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%BFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 117 |
/// |
118 | 118 |
///There is also a \ref bfs() "function-type interface" for the BFS |
119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 120 |
///used easier. |
121 | 121 |
/// |
122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 123 |
///The default type is \ref ListDigraph. |
124 |
///\tparam TR The traits class that defines various types used by the |
|
125 |
///algorithm. By default, it is \ref BfsDefaultTraits |
|
126 |
///"BfsDefaultTraits<GR>". |
|
127 |
///In most cases, this parameter should not be set directly, |
|
128 |
///consider to use the named template parameters instead. |
|
124 | 129 |
#ifdef DOXYGEN |
125 | 130 |
template <typename GR, |
126 | 131 |
typename TR> |
127 | 132 |
#else |
128 | 133 |
template <typename GR=ListDigraph, |
129 | 134 |
typename TR=BfsDefaultTraits<GR> > |
130 | 135 |
#endif |
131 | 136 |
class Bfs { |
132 | 137 |
public: |
133 | 138 |
|
134 | 139 |
///The type of the digraph the algorithm runs on. |
135 | 140 |
typedef typename TR::Digraph Digraph; |
136 | 141 |
|
137 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
138 | 143 |
///shortest paths. |
139 | 144 |
typedef typename TR::PredMap PredMap; |
140 | 145 |
///The type of the map that stores the distances of the nodes. |
141 | 146 |
typedef typename TR::DistMap DistMap; |
142 | 147 |
///The type of the map that indicates which nodes are reached. |
143 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
144 | 149 |
///The type of the map that indicates which nodes are processed. |
145 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
146 | 151 |
///The type of the paths. |
147 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
148 | 153 |
|
149 | 154 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
150 | 155 |
typedef TR Traits; |
151 | 156 |
|
152 | 157 |
private: |
153 | 158 |
|
154 | 159 |
typedef typename Digraph::Node Node; |
155 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
156 | 161 |
typedef typename Digraph::Arc Arc; |
157 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
158 | 163 |
|
159 | 164 |
//Pointer to the underlying digraph. |
160 | 165 |
const Digraph *G; |
161 | 166 |
//Pointer to the map of predecessor arcs. |
162 | 167 |
PredMap *_pred; |
163 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
164 | 169 |
bool local_pred; |
165 | 170 |
//Pointer to the map of distances. |
166 | 171 |
DistMap *_dist; |
167 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
168 | 173 |
bool local_dist; |
169 | 174 |
//Pointer to the map of reached status of the nodes. |
170 | 175 |
ReachedMap *_reached; |
171 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
... | ... |
@@ -912,96 +917,99 @@ |
912 | 917 |
typedef BfsWizardDefaultTraits<GR> Base; |
913 | 918 |
protected: |
914 | 919 |
//The type of the nodes in the digraph. |
915 | 920 |
typedef typename Base::Digraph::Node Node; |
916 | 921 |
|
917 | 922 |
//Pointer to the digraph the algorithm runs on. |
918 | 923 |
void *_g; |
919 | 924 |
//Pointer to the map of reached nodes. |
920 | 925 |
void *_reached; |
921 | 926 |
//Pointer to the map of processed nodes. |
922 | 927 |
void *_processed; |
923 | 928 |
//Pointer to the map of predecessors arcs. |
924 | 929 |
void *_pred; |
925 | 930 |
//Pointer to the map of distances. |
926 | 931 |
void *_dist; |
927 | 932 |
//Pointer to the shortest path to the target node. |
928 | 933 |
void *_path; |
929 | 934 |
//Pointer to the distance of the target node. |
930 | 935 |
int *_di; |
931 | 936 |
|
932 | 937 |
public: |
933 | 938 |
/// Constructor. |
934 | 939 |
|
935 | 940 |
/// This constructor does not require parameters, it initiates |
936 | 941 |
/// all of the attributes to \c 0. |
937 | 942 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
938 | 943 |
_dist(0), _path(0), _di(0) {} |
939 | 944 |
|
940 | 945 |
/// Constructor. |
941 | 946 |
|
942 | 947 |
/// This constructor requires one parameter, |
943 | 948 |
/// others are initiated to \c 0. |
944 | 949 |
/// \param g The digraph the algorithm runs on. |
945 | 950 |
BfsWizardBase(const GR &g) : |
946 | 951 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
947 | 952 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
948 | 953 |
|
949 | 954 |
}; |
950 | 955 |
|
951 | 956 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
952 | 957 |
|
953 | 958 |
/// This auxiliary class is created to implement the |
954 | 959 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
955 | 960 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
956 | 961 |
/// functions and features of the plain \ref Bfs. |
957 | 962 |
/// |
958 | 963 |
/// This class should only be used through the \ref bfs() function, |
959 | 964 |
/// which makes it easier to use the algorithm. |
965 |
/// |
|
966 |
/// \tparam TR The traits class that defines various types used by the |
|
967 |
/// algorithm. |
|
960 | 968 |
template<class TR> |
961 | 969 |
class BfsWizard : public TR |
962 | 970 |
{ |
963 | 971 |
typedef TR Base; |
964 | 972 |
|
965 | 973 |
typedef typename TR::Digraph Digraph; |
966 | 974 |
|
967 | 975 |
typedef typename Digraph::Node Node; |
968 | 976 |
typedef typename Digraph::NodeIt NodeIt; |
969 | 977 |
typedef typename Digraph::Arc Arc; |
970 | 978 |
typedef typename Digraph::OutArcIt OutArcIt; |
971 | 979 |
|
972 | 980 |
typedef typename TR::PredMap PredMap; |
973 | 981 |
typedef typename TR::DistMap DistMap; |
974 | 982 |
typedef typename TR::ReachedMap ReachedMap; |
975 | 983 |
typedef typename TR::ProcessedMap ProcessedMap; |
976 | 984 |
typedef typename TR::Path Path; |
977 | 985 |
|
978 | 986 |
public: |
979 | 987 |
|
980 | 988 |
/// Constructor. |
981 | 989 |
BfsWizard() : TR() {} |
982 | 990 |
|
983 | 991 |
/// Constructor that requires parameters. |
984 | 992 |
|
985 | 993 |
/// Constructor that requires parameters. |
986 | 994 |
/// These parameters will be the default values for the traits class. |
987 | 995 |
/// \param g The digraph the algorithm runs on. |
988 | 996 |
BfsWizard(const Digraph &g) : |
989 | 997 |
TR(g) {} |
990 | 998 |
|
991 | 999 |
///Copy constructor |
992 | 1000 |
BfsWizard(const TR &b) : TR(b) {} |
993 | 1001 |
|
994 | 1002 |
~BfsWizard() {} |
995 | 1003 |
|
996 | 1004 |
///Runs BFS algorithm from the given source node. |
997 | 1005 |
|
998 | 1006 |
///This method runs BFS algorithm from node \c s |
999 | 1007 |
///in order to compute the shortest path to each node. |
1000 | 1008 |
void run(Node s) |
1001 | 1009 |
{ |
1002 | 1010 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1003 | 1011 |
if (Base::_pred) |
1004 | 1012 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1005 | 1013 |
if (Base::_dist) |
1006 | 1014 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1007 | 1015 |
if (Base::_reached) |
... | ... |
@@ -1250,101 +1258,101 @@ |
1250 | 1258 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1251 | 1259 |
template<class GR> |
1252 | 1260 |
struct BfsVisitDefaultTraits { |
1253 | 1261 |
|
1254 | 1262 |
/// \brief The type of the digraph the algorithm runs on. |
1255 | 1263 |
typedef GR Digraph; |
1256 | 1264 |
|
1257 | 1265 |
/// \brief The type of the map that indicates which nodes are reached. |
1258 | 1266 |
/// |
1259 | 1267 |
/// The type of the map that indicates which nodes are reached. |
1260 | 1268 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1261 | 1269 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1262 | 1270 |
|
1263 | 1271 |
/// \brief Instantiates a ReachedMap. |
1264 | 1272 |
/// |
1265 | 1273 |
/// This function instantiates a ReachedMap. |
1266 | 1274 |
/// \param digraph is the digraph, to which |
1267 | 1275 |
/// we would like to define the ReachedMap. |
1268 | 1276 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1269 | 1277 |
return new ReachedMap(digraph); |
1270 | 1278 |
} |
1271 | 1279 |
|
1272 | 1280 |
}; |
1273 | 1281 |
|
1274 | 1282 |
/// \ingroup search |
1275 | 1283 |
/// |
1276 | 1284 |
/// \brief BFS algorithm class with visitor interface. |
1277 | 1285 |
/// |
1278 | 1286 |
/// This class provides an efficient implementation of the BFS algorithm |
1279 | 1287 |
/// with visitor interface. |
1280 | 1288 |
/// |
1281 | 1289 |
/// The BfsVisit class provides an alternative interface to the Bfs |
1282 | 1290 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1283 | 1291 |
/// the member functions of the \c Visitor class on every BFS event. |
1284 | 1292 |
/// |
1285 | 1293 |
/// This interface of the BFS algorithm should be used in special cases |
1286 | 1294 |
/// when extra actions have to be performed in connection with certain |
1287 | 1295 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1288 | 1296 |
/// instead. |
1289 | 1297 |
/// |
1290 | 1298 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1291 | 1299 |
/// The default type is \ref ListDigraph. |
1292 | 1300 |
/// The value of GR is not used directly by \ref BfsVisit, |
1293 | 1301 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
1294 | 1302 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1295 | 1303 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
1296 | 1304 |
/// does not observe the BFS events. If you want to observe the BFS |
1297 | 1305 |
/// events, you should implement your own visitor class. |
1298 |
/// \tparam TR Traits class to set various data types used by the |
|
1299 |
/// algorithm. The default traits class is |
|
1300 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
|
1301 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
|
1302 |
/// |
|
1306 |
/// \tparam TR The traits class that defines various types used by the |
|
1307 |
/// algorithm. By default, it is \ref BfsVisitDefaultTraits |
|
1308 |
/// "BfsVisitDefaultTraits<GR>". |
|
1309 |
/// In most cases, this parameter should not be set directly, |
|
1310 |
/// consider to use the named template parameters instead. |
|
1303 | 1311 |
#ifdef DOXYGEN |
1304 | 1312 |
template <typename GR, typename VS, typename TR> |
1305 | 1313 |
#else |
1306 | 1314 |
template <typename GR = ListDigraph, |
1307 | 1315 |
typename VS = BfsVisitor<GR>, |
1308 | 1316 |
typename TR = BfsVisitDefaultTraits<GR> > |
1309 | 1317 |
#endif |
1310 | 1318 |
class BfsVisit { |
1311 | 1319 |
public: |
1312 | 1320 |
|
1313 | 1321 |
///The traits class. |
1314 | 1322 |
typedef TR Traits; |
1315 | 1323 |
|
1316 | 1324 |
///The type of the digraph the algorithm runs on. |
1317 | 1325 |
typedef typename Traits::Digraph Digraph; |
1318 | 1326 |
|
1319 | 1327 |
///The visitor type used by the algorithm. |
1320 | 1328 |
typedef VS Visitor; |
1321 | 1329 |
|
1322 | 1330 |
///The type of the map that indicates which nodes are reached. |
1323 | 1331 |
typedef typename Traits::ReachedMap ReachedMap; |
1324 | 1332 |
|
1325 | 1333 |
private: |
1326 | 1334 |
|
1327 | 1335 |
typedef typename Digraph::Node Node; |
1328 | 1336 |
typedef typename Digraph::NodeIt NodeIt; |
1329 | 1337 |
typedef typename Digraph::Arc Arc; |
1330 | 1338 |
typedef typename Digraph::OutArcIt OutArcIt; |
1331 | 1339 |
|
1332 | 1340 |
//Pointer to the underlying digraph. |
1333 | 1341 |
const Digraph *_digraph; |
1334 | 1342 |
//Pointer to the visitor object. |
1335 | 1343 |
Visitor *_visitor; |
1336 | 1344 |
//Pointer to the map of reached status of the nodes. |
1337 | 1345 |
ReachedMap *_reached; |
1338 | 1346 |
//Indicates if _reached is locally allocated (true) or not. |
1339 | 1347 |
bool local_reached; |
1340 | 1348 |
|
1341 | 1349 |
std::vector<typename Digraph::Node> _list; |
1342 | 1350 |
int _list_front, _list_back; |
1343 | 1351 |
|
1344 | 1352 |
//Creates the maps if necessary. |
1345 | 1353 |
void create_maps() { |
1346 | 1354 |
if(!_reached) { |
1347 | 1355 |
local_reached = true; |
1348 | 1356 |
_reached = Traits::createReachedMap(*_digraph); |
1349 | 1357 |
} |
1350 | 1358 |
} |
... | ... |
@@ -32,99 +32,104 @@ |
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \brief Default traits class of CapacityScaling algorithm. |
35 | 35 |
/// |
36 | 36 |
/// Default traits class of CapacityScaling algorithm. |
37 | 37 |
/// \tparam GR Digraph type. |
38 | 38 |
/// \tparam V The number type used for flow amounts, capacity bounds |
39 | 39 |
/// and supply values. By default it is \c int. |
40 | 40 |
/// \tparam C The number type used for costs and potentials. |
41 | 41 |
/// By default it is the same as \c V. |
42 | 42 |
template <typename GR, typename V = int, typename C = V> |
43 | 43 |
struct CapacityScalingDefaultTraits |
44 | 44 |
{ |
45 | 45 |
/// The type of the digraph |
46 | 46 |
typedef GR Digraph; |
47 | 47 |
/// The type of the flow amounts, capacity bounds and supply values |
48 | 48 |
typedef V Value; |
49 | 49 |
/// The type of the arc costs |
50 | 50 |
typedef C Cost; |
51 | 51 |
|
52 | 52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
53 | 53 |
/// |
54 | 54 |
/// The type of the heap used for internal Dijkstra computations. |
55 | 55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
56 | 56 |
/// its priority type must be \c Cost and its cross reference type |
57 | 57 |
/// must be \ref RangeMap "RangeMap<int>". |
58 | 58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
/// \addtogroup min_cost_flow_algs |
62 | 62 |
/// @{ |
63 | 63 |
|
64 | 64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
65 | 65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
66 | 66 |
/// |
67 | 67 |
/// \ref CapacityScaling implements the capacity scaling version |
68 | 68 |
/// of the successive shortest path algorithm for finding a |
69 | 69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
70 | 70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
71 | 71 |
/// solution method. |
72 | 72 |
/// |
73 | 73 |
/// Most of the parameters of the problem (except for the digraph) |
74 | 74 |
/// can be given using separate functions, and the algorithm can be |
75 | 75 |
/// executed using the \ref run() function. If some parameters are not |
76 | 76 |
/// specified, then default values will be used. |
77 | 77 |
/// |
78 | 78 |
/// \tparam GR The digraph type the algorithm runs on. |
79 | 79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
80 |
/// and supply values in the algorithm. By default it is \c int. |
|
80 |
/// and supply values in the algorithm. By default, it is \c int. |
|
81 | 81 |
/// \tparam C The number type used for costs and potentials in the |
82 |
/// algorithm. By default it is the same as \c V. |
|
82 |
/// algorithm. By default, it is the same as \c V. |
|
83 |
/// \tparam TR The traits class that defines various types used by the |
|
84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
|
85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
|
86 |
/// In most cases, this parameter should not be set directly, |
|
87 |
/// consider to use the named template parameters instead. |
|
83 | 88 |
/// |
84 | 89 |
/// \warning Both number types must be signed and all input data must |
85 | 90 |
/// be integer. |
86 | 91 |
/// \warning This algorithm does not support negative costs for such |
87 | 92 |
/// arcs that have infinite upper bound. |
88 | 93 |
#ifdef DOXYGEN |
89 | 94 |
template <typename GR, typename V, typename C, typename TR> |
90 | 95 |
#else |
91 | 96 |
template < typename GR, typename V = int, typename C = V, |
92 | 97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
93 | 98 |
#endif |
94 | 99 |
class CapacityScaling |
95 | 100 |
{ |
96 | 101 |
public: |
97 | 102 |
|
98 | 103 |
/// The type of the digraph |
99 | 104 |
typedef typename TR::Digraph Digraph; |
100 | 105 |
/// The type of the flow amounts, capacity bounds and supply values |
101 | 106 |
typedef typename TR::Value Value; |
102 | 107 |
/// The type of the arc costs |
103 | 108 |
typedef typename TR::Cost Cost; |
104 | 109 |
|
105 | 110 |
/// The type of the heap used for internal Dijkstra computations |
106 | 111 |
typedef typename TR::Heap Heap; |
107 | 112 |
|
108 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
109 | 114 |
typedef TR Traits; |
110 | 115 |
|
111 | 116 |
public: |
112 | 117 |
|
113 | 118 |
/// \brief Problem type constants for the \c run() function. |
114 | 119 |
/// |
115 | 120 |
/// Enum type containing the problem type constants that can be |
116 | 121 |
/// returned by the \ref run() function of the algorithm. |
117 | 122 |
enum ProblemType { |
118 | 123 |
/// The problem has no feasible solution (flow). |
119 | 124 |
INFEASIBLE, |
120 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
121 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
122 | 127 |
/// potentials (primal and dual solutions). |
123 | 128 |
OPTIMAL, |
124 | 129 |
/// The digraph contains an arc of negative cost and infinite |
125 | 130 |
/// upper bound. It means that the objective function is unbounded |
126 | 131 |
/// on that arc, however, note that it could actually be bounded |
127 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
128 | 133 |
/// these cases. |
129 | 134 |
UNBOUNDED |
130 | 135 |
}; |
... | ... |
@@ -128,96 +128,101 @@ |
128 | 128 |
|
129 | 129 |
The exact formulation of this problem is the following. |
130 | 130 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$ |
131 | 131 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and |
132 | 132 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
133 | 133 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$ |
134 | 134 |
denotes the signed supply values of the nodes. |
135 | 135 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
136 | 136 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
137 | 137 |
\f$-sup(u)\f$ demand. |
138 | 138 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$ |
139 | 139 |
solution of the following problem. |
140 | 140 |
|
141 | 141 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) |
142 | 142 |
\geq sup(u) \quad \forall u\in V, \f] |
143 | 143 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
144 | 144 |
|
145 | 145 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
146 | 146 |
zero or negative in order to have a feasible solution (since the sum |
147 | 147 |
of the expressions on the left-hand side of the inequalities is zero). |
148 | 148 |
It means that the total demand must be greater or equal to the total |
149 | 149 |
supply and all the supplies have to be carried out from the supply nodes, |
150 | 150 |
but there could be demands that are not satisfied. |
151 | 151 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
152 | 152 |
constraints have to be satisfied with equality, i.e. all demands |
153 | 153 |
have to be satisfied and all supplies have to be used. |
154 | 154 |
|
155 | 155 |
If you need the opposite inequalities in the supply/demand constraints |
156 | 156 |
(i.e. the total demand is less than the total supply and all the demands |
157 | 157 |
have to be satisfied while there could be supplies that are not used), |
158 | 158 |
then you could easily transform the problem to the above form by reversing |
159 | 159 |
the direction of the arcs and taking the negative of the supply values |
160 | 160 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
161 | 161 |
|
162 | 162 |
This algorithm either calculates a feasible circulation, or provides |
163 | 163 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
164 | 164 |
cannot exist. |
165 | 165 |
|
166 | 166 |
Note that this algorithm also provides a feasible solution for the |
167 | 167 |
\ref min_cost_flow "minimum cost flow problem". |
168 | 168 |
|
169 | 169 |
\tparam GR The type of the digraph the algorithm runs on. |
170 | 170 |
\tparam LM The type of the lower bound map. The default |
171 | 171 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
172 | 172 |
\tparam UM The type of the upper bound (capacity) map. |
173 | 173 |
The default map type is \c LM. |
174 | 174 |
\tparam SM The type of the supply map. The default map type is |
175 | 175 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
176 |
\tparam TR The traits class that defines various types used by the |
|
177 |
algorithm. By default, it is \ref CirculationDefaultTraits |
|
178 |
"CirculationDefaultTraits<GR, LM, UM, SM>". |
|
179 |
In most cases, this parameter should not be set directly, |
|
180 |
consider to use the named template parameters instead. |
|
176 | 181 |
*/ |
177 | 182 |
#ifdef DOXYGEN |
178 | 183 |
template< typename GR, |
179 | 184 |
typename LM, |
180 | 185 |
typename UM, |
181 | 186 |
typename SM, |
182 | 187 |
typename TR > |
183 | 188 |
#else |
184 | 189 |
template< typename GR, |
185 | 190 |
typename LM = typename GR::template ArcMap<int>, |
186 | 191 |
typename UM = LM, |
187 | 192 |
typename SM = typename GR::template NodeMap<typename UM::Value>, |
188 | 193 |
typename TR = CirculationDefaultTraits<GR, LM, UM, SM> > |
189 | 194 |
#endif |
190 | 195 |
class Circulation { |
191 | 196 |
public: |
192 | 197 |
|
193 | 198 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
194 | 199 |
typedef TR Traits; |
195 | 200 |
///The type of the digraph the algorithm runs on. |
196 | 201 |
typedef typename Traits::Digraph Digraph; |
197 | 202 |
///The type of the flow and supply values. |
198 | 203 |
typedef typename Traits::Value Value; |
199 | 204 |
|
200 | 205 |
///The type of the lower bound map. |
201 | 206 |
typedef typename Traits::LowerMap LowerMap; |
202 | 207 |
///The type of the upper bound (capacity) map. |
203 | 208 |
typedef typename Traits::UpperMap UpperMap; |
204 | 209 |
///The type of the supply map. |
205 | 210 |
typedef typename Traits::SupplyMap SupplyMap; |
206 | 211 |
///The type of the flow map. |
207 | 212 |
typedef typename Traits::FlowMap FlowMap; |
208 | 213 |
|
209 | 214 |
///The type of the elevator. |
210 | 215 |
typedef typename Traits::Elevator Elevator; |
211 | 216 |
///The type of the tolerance. |
212 | 217 |
typedef typename Traits::Tolerance Tolerance; |
213 | 218 |
|
214 | 219 |
private: |
215 | 220 |
|
216 | 221 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
217 | 222 |
|
218 | 223 |
const Digraph &_g; |
219 | 224 |
int _node_num; |
220 | 225 |
|
221 | 226 |
const LowerMap *_lo; |
222 | 227 |
const UpperMap *_up; |
223 | 228 |
const SupplyMap *_supply; |
... | ... |
@@ -59,130 +59,134 @@ |
59 | 59 |
/// The type of the arc costs |
60 | 60 |
typedef C Cost; |
61 | 61 |
|
62 | 62 |
/// \brief The large cost type used for internal computations |
63 | 63 |
/// |
64 | 64 |
/// The large cost type used for internal computations. |
65 | 65 |
/// It is \c long \c long if the \c Cost type is integer, |
66 | 66 |
/// otherwise it is \c double. |
67 | 67 |
/// \c Cost must be convertible to \c LargeCost. |
68 | 68 |
typedef double LargeCost; |
69 | 69 |
}; |
70 | 70 |
|
71 | 71 |
// Default traits class for integer cost types |
72 | 72 |
template <typename GR, typename V, typename C> |
73 | 73 |
struct CostScalingDefaultTraits<GR, V, C, true> |
74 | 74 |
{ |
75 | 75 |
typedef GR Digraph; |
76 | 76 |
typedef V Value; |
77 | 77 |
typedef C Cost; |
78 | 78 |
#ifdef LEMON_HAVE_LONG_LONG |
79 | 79 |
typedef long long LargeCost; |
80 | 80 |
#else |
81 | 81 |
typedef long LargeCost; |
82 | 82 |
#endif |
83 | 83 |
}; |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// \addtogroup min_cost_flow_algs |
87 | 87 |
/// @{ |
88 | 88 |
|
89 | 89 |
/// \brief Implementation of the Cost Scaling algorithm for |
90 | 90 |
/// finding a \ref min_cost_flow "minimum cost flow". |
91 | 91 |
/// |
92 | 92 |
/// \ref CostScaling implements a cost scaling algorithm that performs |
93 | 93 |
/// push/augment and relabel operations for finding a \ref min_cost_flow |
94 | 94 |
/// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
95 | 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
96 | 96 |
/// It is a highly efficient primal-dual solution method, which |
97 | 97 |
/// can be viewed as the generalization of the \ref Preflow |
98 | 98 |
/// "preflow push-relabel" algorithm for the maximum flow problem. |
99 | 99 |
/// |
100 | 100 |
/// Most of the parameters of the problem (except for the digraph) |
101 | 101 |
/// can be given using separate functions, and the algorithm can be |
102 | 102 |
/// executed using the \ref run() function. If some parameters are not |
103 | 103 |
/// specified, then default values will be used. |
104 | 104 |
/// |
105 | 105 |
/// \tparam GR The digraph type the algorithm runs on. |
106 | 106 |
/// \tparam V The number type used for flow amounts, capacity bounds |
107 |
/// and supply values in the algorithm. By default it is \c int. |
|
107 |
/// and supply values in the algorithm. By default, it is \c int. |
|
108 | 108 |
/// \tparam C The number type used for costs and potentials in the |
109 |
/// algorithm. By default it is the same as \c V. |
|
109 |
/// algorithm. By default, it is the same as \c V. |
|
110 |
/// \tparam TR The traits class that defines various types used by the |
|
111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
|
112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
|
113 |
/// In most cases, this parameter should not be set directly, |
|
114 |
/// consider to use the named template parameters instead. |
|
110 | 115 |
/// |
111 | 116 |
/// \warning Both number types must be signed and all input data must |
112 | 117 |
/// be integer. |
113 | 118 |
/// \warning This algorithm does not support negative costs for such |
114 | 119 |
/// arcs that have infinite upper bound. |
115 | 120 |
/// |
116 | 121 |
/// \note %CostScaling provides three different internal methods, |
117 | 122 |
/// from which the most efficient one is used by default. |
118 | 123 |
/// For more information, see \ref Method. |
119 | 124 |
#ifdef DOXYGEN |
120 | 125 |
template <typename GR, typename V, typename C, typename TR> |
121 | 126 |
#else |
122 | 127 |
template < typename GR, typename V = int, typename C = V, |
123 | 128 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
124 | 129 |
#endif |
125 | 130 |
class CostScaling |
126 | 131 |
{ |
127 | 132 |
public: |
128 | 133 |
|
129 | 134 |
/// The type of the digraph |
130 | 135 |
typedef typename TR::Digraph Digraph; |
131 | 136 |
/// The type of the flow amounts, capacity bounds and supply values |
132 | 137 |
typedef typename TR::Value Value; |
133 | 138 |
/// The type of the arc costs |
134 | 139 |
typedef typename TR::Cost Cost; |
135 | 140 |
|
136 | 141 |
/// \brief The large cost type |
137 | 142 |
/// |
138 | 143 |
/// The large cost type used for internal computations. |
139 |
/// Using the \ref CostScalingDefaultTraits "default traits class", |
|
140 |
/// it is \c long \c long if the \c Cost type is integer, |
|
144 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
|
141 | 145 |
/// otherwise it is \c double. |
142 | 146 |
typedef typename TR::LargeCost LargeCost; |
143 | 147 |
|
144 | 148 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
145 | 149 |
typedef TR Traits; |
146 | 150 |
|
147 | 151 |
public: |
148 | 152 |
|
149 | 153 |
/// \brief Problem type constants for the \c run() function. |
150 | 154 |
/// |
151 | 155 |
/// Enum type containing the problem type constants that can be |
152 | 156 |
/// returned by the \ref run() function of the algorithm. |
153 | 157 |
enum ProblemType { |
154 | 158 |
/// The problem has no feasible solution (flow). |
155 | 159 |
INFEASIBLE, |
156 | 160 |
/// The problem has optimal solution (i.e. it is feasible and |
157 | 161 |
/// bounded), and the algorithm has found optimal flow and node |
158 | 162 |
/// potentials (primal and dual solutions). |
159 | 163 |
OPTIMAL, |
160 | 164 |
/// The digraph contains an arc of negative cost and infinite |
161 | 165 |
/// upper bound. It means that the objective function is unbounded |
162 | 166 |
/// on that arc, however, note that it could actually be bounded |
163 | 167 |
/// over the feasible flows, but this algroithm cannot handle |
164 | 168 |
/// these cases. |
165 | 169 |
UNBOUNDED |
166 | 170 |
}; |
167 | 171 |
|
168 | 172 |
/// \brief Constants for selecting the internal method. |
169 | 173 |
/// |
170 | 174 |
/// Enum type containing constants for selecting the internal method |
171 | 175 |
/// for the \ref run() function. |
172 | 176 |
/// |
173 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
174 | 178 |
/// in their base operations, which are used in conjunction with the |
175 | 179 |
/// relabel operation. |
176 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
177 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
178 | 182 |
/// the most efficient and the most robust on various test inputs. |
179 | 183 |
/// However, the other methods can be selected using the \ref run() |
180 | 184 |
/// function with the proper parameter. |
181 | 185 |
enum Method { |
182 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
183 | 187 |
/// admissible arc at once. |
184 | 188 |
PUSH, |
185 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
186 | 190 |
/// paths from a node with excess to a node with deficit. |
187 | 191 |
AUGMENT, |
188 | 192 |
/// Partial augment operations are used, i.e. flow is moved on |
... | ... |
@@ -76,96 +76,101 @@ |
76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
77 | 77 |
#endif |
78 | 78 |
{ |
79 | 79 |
return new ProcessedMap(); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 | 87 |
///Instantiates a \c ReachedMap. |
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref ReachedMap. |
90 | 90 |
///\param g is the digraph, to which |
91 | 91 |
///we would like to define the \ref ReachedMap. |
92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
93 | 93 |
{ |
94 | 94 |
return new ReachedMap(g); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
///The type of the map that stores the distances of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the distances of the nodes. |
100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 | 102 |
///Instantiates a \c DistMap. |
103 | 103 |
|
104 | 104 |
///This function instantiates a \ref DistMap. |
105 | 105 |
///\param g is the digraph, to which we would like to define the |
106 | 106 |
///\ref DistMap. |
107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(g); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%DFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %DFS algorithm. |
117 | 117 |
/// |
118 | 118 |
///There is also a \ref dfs() "function-type interface" for the DFS |
119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 120 |
///used easier. |
121 | 121 |
/// |
122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 123 |
///The default type is \ref ListDigraph. |
124 |
///\tparam TR The traits class that defines various types used by the |
|
125 |
///algorithm. By default, it is \ref DfsDefaultTraits |
|
126 |
///"DfsDefaultTraits<GR>". |
|
127 |
///In most cases, this parameter should not be set directly, |
|
128 |
///consider to use the named template parameters instead. |
|
124 | 129 |
#ifdef DOXYGEN |
125 | 130 |
template <typename GR, |
126 | 131 |
typename TR> |
127 | 132 |
#else |
128 | 133 |
template <typename GR=ListDigraph, |
129 | 134 |
typename TR=DfsDefaultTraits<GR> > |
130 | 135 |
#endif |
131 | 136 |
class Dfs { |
132 | 137 |
public: |
133 | 138 |
|
134 | 139 |
///The type of the digraph the algorithm runs on. |
135 | 140 |
typedef typename TR::Digraph Digraph; |
136 | 141 |
|
137 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
138 | 143 |
///DFS paths. |
139 | 144 |
typedef typename TR::PredMap PredMap; |
140 | 145 |
///The type of the map that stores the distances of the nodes. |
141 | 146 |
typedef typename TR::DistMap DistMap; |
142 | 147 |
///The type of the map that indicates which nodes are reached. |
143 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
144 | 149 |
///The type of the map that indicates which nodes are processed. |
145 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
146 | 151 |
///The type of the paths. |
147 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
148 | 153 |
|
149 | 154 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
150 | 155 |
typedef TR Traits; |
151 | 156 |
|
152 | 157 |
private: |
153 | 158 |
|
154 | 159 |
typedef typename Digraph::Node Node; |
155 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
156 | 161 |
typedef typename Digraph::Arc Arc; |
157 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
158 | 163 |
|
159 | 164 |
//Pointer to the underlying digraph. |
160 | 165 |
const Digraph *G; |
161 | 166 |
//Pointer to the map of predecessor arcs. |
162 | 167 |
PredMap *_pred; |
163 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
164 | 169 |
bool local_pred; |
165 | 170 |
//Pointer to the map of distances. |
166 | 171 |
DistMap *_dist; |
167 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
168 | 173 |
bool local_dist; |
169 | 174 |
//Pointer to the map of reached status of the nodes. |
170 | 175 |
ReachedMap *_reached; |
171 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
... | ... |
@@ -842,96 +847,99 @@ |
842 | 847 |
typedef DfsWizardDefaultTraits<GR> Base; |
843 | 848 |
protected: |
844 | 849 |
//The type of the nodes in the digraph. |
845 | 850 |
typedef typename Base::Digraph::Node Node; |
846 | 851 |
|
847 | 852 |
//Pointer to the digraph the algorithm runs on. |
848 | 853 |
void *_g; |
849 | 854 |
//Pointer to the map of reached nodes. |
850 | 855 |
void *_reached; |
851 | 856 |
//Pointer to the map of processed nodes. |
852 | 857 |
void *_processed; |
853 | 858 |
//Pointer to the map of predecessors arcs. |
854 | 859 |
void *_pred; |
855 | 860 |
//Pointer to the map of distances. |
856 | 861 |
void *_dist; |
857 | 862 |
//Pointer to the DFS path to the target node. |
858 | 863 |
void *_path; |
859 | 864 |
//Pointer to the distance of the target node. |
860 | 865 |
int *_di; |
861 | 866 |
|
862 | 867 |
public: |
863 | 868 |
/// Constructor. |
864 | 869 |
|
865 | 870 |
/// This constructor does not require parameters, it initiates |
866 | 871 |
/// all of the attributes to \c 0. |
867 | 872 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
868 | 873 |
_dist(0), _path(0), _di(0) {} |
869 | 874 |
|
870 | 875 |
/// Constructor. |
871 | 876 |
|
872 | 877 |
/// This constructor requires one parameter, |
873 | 878 |
/// others are initiated to \c 0. |
874 | 879 |
/// \param g The digraph the algorithm runs on. |
875 | 880 |
DfsWizardBase(const GR &g) : |
876 | 881 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
877 | 882 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
878 | 883 |
|
879 | 884 |
}; |
880 | 885 |
|
881 | 886 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
882 | 887 |
|
883 | 888 |
/// This auxiliary class is created to implement the |
884 | 889 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
885 | 890 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
886 | 891 |
/// functions and features of the plain \ref Dfs. |
887 | 892 |
/// |
888 | 893 |
/// This class should only be used through the \ref dfs() function, |
889 | 894 |
/// which makes it easier to use the algorithm. |
895 |
/// |
|
896 |
/// \tparam TR The traits class that defines various types used by the |
|
897 |
/// algorithm. |
|
890 | 898 |
template<class TR> |
891 | 899 |
class DfsWizard : public TR |
892 | 900 |
{ |
893 | 901 |
typedef TR Base; |
894 | 902 |
|
895 | 903 |
typedef typename TR::Digraph Digraph; |
896 | 904 |
|
897 | 905 |
typedef typename Digraph::Node Node; |
898 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
899 | 907 |
typedef typename Digraph::Arc Arc; |
900 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
901 | 909 |
|
902 | 910 |
typedef typename TR::PredMap PredMap; |
903 | 911 |
typedef typename TR::DistMap DistMap; |
904 | 912 |
typedef typename TR::ReachedMap ReachedMap; |
905 | 913 |
typedef typename TR::ProcessedMap ProcessedMap; |
906 | 914 |
typedef typename TR::Path Path; |
907 | 915 |
|
908 | 916 |
public: |
909 | 917 |
|
910 | 918 |
/// Constructor. |
911 | 919 |
DfsWizard() : TR() {} |
912 | 920 |
|
913 | 921 |
/// Constructor that requires parameters. |
914 | 922 |
|
915 | 923 |
/// Constructor that requires parameters. |
916 | 924 |
/// These parameters will be the default values for the traits class. |
917 | 925 |
/// \param g The digraph the algorithm runs on. |
918 | 926 |
DfsWizard(const Digraph &g) : |
919 | 927 |
TR(g) {} |
920 | 928 |
|
921 | 929 |
///Copy constructor |
922 | 930 |
DfsWizard(const TR &b) : TR(b) {} |
923 | 931 |
|
924 | 932 |
~DfsWizard() {} |
925 | 933 |
|
926 | 934 |
///Runs DFS algorithm from the given source node. |
927 | 935 |
|
928 | 936 |
///This method runs DFS algorithm from node \c s |
929 | 937 |
///in order to compute the DFS path to each node. |
930 | 938 |
void run(Node s) |
931 | 939 |
{ |
932 | 940 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
933 | 941 |
if (Base::_pred) |
934 | 942 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
935 | 943 |
if (Base::_dist) |
936 | 944 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
937 | 945 |
if (Base::_reached) |
... | ... |
@@ -1192,101 +1200,101 @@ |
1192 | 1200 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1193 | 1201 |
template<class GR> |
1194 | 1202 |
struct DfsVisitDefaultTraits { |
1195 | 1203 |
|
1196 | 1204 |
/// \brief The type of the digraph the algorithm runs on. |
1197 | 1205 |
typedef GR Digraph; |
1198 | 1206 |
|
1199 | 1207 |
/// \brief The type of the map that indicates which nodes are reached. |
1200 | 1208 |
/// |
1201 | 1209 |
/// The type of the map that indicates which nodes are reached. |
1202 | 1210 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1203 | 1211 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1204 | 1212 |
|
1205 | 1213 |
/// \brief Instantiates a ReachedMap. |
1206 | 1214 |
/// |
1207 | 1215 |
/// This function instantiates a ReachedMap. |
1208 | 1216 |
/// \param digraph is the digraph, to which |
1209 | 1217 |
/// we would like to define the ReachedMap. |
1210 | 1218 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1211 | 1219 |
return new ReachedMap(digraph); |
1212 | 1220 |
} |
1213 | 1221 |
|
1214 | 1222 |
}; |
1215 | 1223 |
|
1216 | 1224 |
/// \ingroup search |
1217 | 1225 |
/// |
1218 | 1226 |
/// \brief DFS algorithm class with visitor interface. |
1219 | 1227 |
/// |
1220 | 1228 |
/// This class provides an efficient implementation of the DFS algorithm |
1221 | 1229 |
/// with visitor interface. |
1222 | 1230 |
/// |
1223 | 1231 |
/// The DfsVisit class provides an alternative interface to the Dfs |
1224 | 1232 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1225 | 1233 |
/// the member functions of the \c Visitor class on every DFS event. |
1226 | 1234 |
/// |
1227 | 1235 |
/// This interface of the DFS algorithm should be used in special cases |
1228 | 1236 |
/// when extra actions have to be performed in connection with certain |
1229 | 1237 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1230 | 1238 |
/// instead. |
1231 | 1239 |
/// |
1232 | 1240 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1233 | 1241 |
/// The default type is \ref ListDigraph. |
1234 | 1242 |
/// The value of GR is not used directly by \ref DfsVisit, |
1235 | 1243 |
/// it is only passed to \ref DfsVisitDefaultTraits. |
1236 | 1244 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1237 | 1245 |
/// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which |
1238 | 1246 |
/// does not observe the DFS events. If you want to observe the DFS |
1239 | 1247 |
/// events, you should implement your own visitor class. |
1240 |
/// \tparam TR Traits class to set various data types used by the |
|
1241 |
/// algorithm. The default traits class is |
|
1242 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>". |
|
1243 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
|
1244 |
/// |
|
1248 |
/// \tparam TR The traits class that defines various types used by the |
|
1249 |
/// algorithm. By default, it is \ref DfsVisitDefaultTraits |
|
1250 |
/// "DfsVisitDefaultTraits<GR>". |
|
1251 |
/// In most cases, this parameter should not be set directly, |
|
1252 |
/// consider to use the named template parameters instead. |
|
1245 | 1253 |
#ifdef DOXYGEN |
1246 | 1254 |
template <typename GR, typename VS, typename TR> |
1247 | 1255 |
#else |
1248 | 1256 |
template <typename GR = ListDigraph, |
1249 | 1257 |
typename VS = DfsVisitor<GR>, |
1250 | 1258 |
typename TR = DfsVisitDefaultTraits<GR> > |
1251 | 1259 |
#endif |
1252 | 1260 |
class DfsVisit { |
1253 | 1261 |
public: |
1254 | 1262 |
|
1255 | 1263 |
///The traits class. |
1256 | 1264 |
typedef TR Traits; |
1257 | 1265 |
|
1258 | 1266 |
///The type of the digraph the algorithm runs on. |
1259 | 1267 |
typedef typename Traits::Digraph Digraph; |
1260 | 1268 |
|
1261 | 1269 |
///The visitor type used by the algorithm. |
1262 | 1270 |
typedef VS Visitor; |
1263 | 1271 |
|
1264 | 1272 |
///The type of the map that indicates which nodes are reached. |
1265 | 1273 |
typedef typename Traits::ReachedMap ReachedMap; |
1266 | 1274 |
|
1267 | 1275 |
private: |
1268 | 1276 |
|
1269 | 1277 |
typedef typename Digraph::Node Node; |
1270 | 1278 |
typedef typename Digraph::NodeIt NodeIt; |
1271 | 1279 |
typedef typename Digraph::Arc Arc; |
1272 | 1280 |
typedef typename Digraph::OutArcIt OutArcIt; |
1273 | 1281 |
|
1274 | 1282 |
//Pointer to the underlying digraph. |
1275 | 1283 |
const Digraph *_digraph; |
1276 | 1284 |
//Pointer to the visitor object. |
1277 | 1285 |
Visitor *_visitor; |
1278 | 1286 |
//Pointer to the map of reached status of the nodes. |
1279 | 1287 |
ReachedMap *_reached; |
1280 | 1288 |
//Indicates if _reached is locally allocated (true) or not. |
1281 | 1289 |
bool local_reached; |
1282 | 1290 |
|
1283 | 1291 |
std::vector<typename Digraph::Arc> _stack; |
1284 | 1292 |
int _stack_head; |
1285 | 1293 |
|
1286 | 1294 |
//Creates the maps if necessary. |
1287 | 1295 |
void create_maps() { |
1288 | 1296 |
if(!_reached) { |
1289 | 1297 |
local_reached = true; |
1290 | 1298 |
_reached = Traits::createReachedMap(*_digraph); |
1291 | 1299 |
} |
1292 | 1300 |
} |
... | ... |
@@ -147,96 +147,101 @@ |
147 | 147 |
{ |
148 | 148 |
return new ProcessedMap(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
///The type of the map that stores the distances of the nodes. |
152 | 152 |
|
153 | 153 |
///The type of the map that stores the distances of the nodes. |
154 | 154 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
155 | 155 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
156 | 156 |
///Instantiates a \c DistMap. |
157 | 157 |
|
158 | 158 |
///This function instantiates a \ref DistMap. |
159 | 159 |
///\param g is the digraph, to which we would like to define |
160 | 160 |
///the \ref DistMap. |
161 | 161 |
static DistMap *createDistMap(const Digraph &g) |
162 | 162 |
{ |
163 | 163 |
return new DistMap(g); |
164 | 164 |
} |
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
///%Dijkstra algorithm class. |
168 | 168 |
|
169 | 169 |
/// \ingroup shortest_path |
170 | 170 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
171 | 171 |
/// |
172 | 172 |
///The %Dijkstra algorithm solves the single-source shortest path problem |
173 | 173 |
///when all arc lengths are non-negative. If there are negative lengths, |
174 | 174 |
///the BellmanFord algorithm should be used instead. |
175 | 175 |
/// |
176 | 176 |
///The arc lengths are passed to the algorithm using a |
177 | 177 |
///\ref concepts::ReadMap "ReadMap", |
178 | 178 |
///so it is easy to change it to any kind of length. |
179 | 179 |
///The type of the length is determined by the |
180 | 180 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
181 | 181 |
///It is also possible to change the underlying priority heap. |
182 | 182 |
/// |
183 | 183 |
///There is also a \ref dijkstra() "function-type interface" for the |
184 | 184 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
185 | 185 |
///it can be used easier. |
186 | 186 |
/// |
187 | 187 |
///\tparam GR The type of the digraph the algorithm runs on. |
188 | 188 |
///The default type is \ref ListDigraph. |
189 | 189 |
///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
190 | 190 |
///the lengths of the arcs. |
191 | 191 |
///It is read once for each arc, so the map may involve in |
192 | 192 |
///relatively time consuming process to compute the arc lengths if |
193 | 193 |
///it is necessary. The default map type is \ref |
194 | 194 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
195 |
///\tparam TR The traits class that defines various types used by the |
|
196 |
///algorithm. By default, it is \ref DijkstraDefaultTraits |
|
197 |
///"DijkstraDefaultTraits<GR, LEN>". |
|
198 |
///In most cases, this parameter should not be set directly, |
|
199 |
///consider to use the named template parameters instead. |
|
195 | 200 |
#ifdef DOXYGEN |
196 | 201 |
template <typename GR, typename LEN, typename TR> |
197 | 202 |
#else |
198 | 203 |
template <typename GR=ListDigraph, |
199 | 204 |
typename LEN=typename GR::template ArcMap<int>, |
200 | 205 |
typename TR=DijkstraDefaultTraits<GR,LEN> > |
201 | 206 |
#endif |
202 | 207 |
class Dijkstra { |
203 | 208 |
public: |
204 | 209 |
|
205 | 210 |
///The type of the digraph the algorithm runs on. |
206 | 211 |
typedef typename TR::Digraph Digraph; |
207 | 212 |
|
208 | 213 |
///The type of the arc lengths. |
209 | 214 |
typedef typename TR::Value Value; |
210 | 215 |
///The type of the map that stores the arc lengths. |
211 | 216 |
typedef typename TR::LengthMap LengthMap; |
212 | 217 |
///\brief The type of the map that stores the predecessor arcs of the |
213 | 218 |
///shortest paths. |
214 | 219 |
typedef typename TR::PredMap PredMap; |
215 | 220 |
///The type of the map that stores the distances of the nodes. |
216 | 221 |
typedef typename TR::DistMap DistMap; |
217 | 222 |
///The type of the map that indicates which nodes are processed. |
218 | 223 |
typedef typename TR::ProcessedMap ProcessedMap; |
219 | 224 |
///The type of the paths. |
220 | 225 |
typedef PredMapPath<Digraph, PredMap> Path; |
221 | 226 |
///The cross reference type used for the current heap. |
222 | 227 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
223 | 228 |
///The heap type used by the algorithm. |
224 | 229 |
typedef typename TR::Heap Heap; |
225 | 230 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
226 | 231 |
///of the algorithm. |
227 | 232 |
typedef typename TR::OperationTraits OperationTraits; |
228 | 233 |
|
229 | 234 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
230 | 235 |
typedef TR Traits; |
231 | 236 |
|
232 | 237 |
private: |
233 | 238 |
|
234 | 239 |
typedef typename Digraph::Node Node; |
235 | 240 |
typedef typename Digraph::NodeIt NodeIt; |
236 | 241 |
typedef typename Digraph::Arc Arc; |
237 | 242 |
typedef typename Digraph::OutArcIt OutArcIt; |
238 | 243 |
|
239 | 244 |
//Pointer to the underlying digraph. |
240 | 245 |
const Digraph *G; |
241 | 246 |
//Pointer to the length map. |
242 | 247 |
const LengthMap *_length; |
... | ... |
@@ -1047,96 +1052,99 @@ |
1047 | 1052 |
//The type of the nodes in the digraph. |
1048 | 1053 |
typedef typename Base::Digraph::Node Node; |
1049 | 1054 |
|
1050 | 1055 |
//Pointer to the digraph the algorithm runs on. |
1051 | 1056 |
void *_g; |
1052 | 1057 |
//Pointer to the length map. |
1053 | 1058 |
void *_length; |
1054 | 1059 |
//Pointer to the map of processed nodes. |
1055 | 1060 |
void *_processed; |
1056 | 1061 |
//Pointer to the map of predecessors arcs. |
1057 | 1062 |
void *_pred; |
1058 | 1063 |
//Pointer to the map of distances. |
1059 | 1064 |
void *_dist; |
1060 | 1065 |
//Pointer to the shortest path to the target node. |
1061 | 1066 |
void *_path; |
1062 | 1067 |
//Pointer to the distance of the target node. |
1063 | 1068 |
void *_di; |
1064 | 1069 |
|
1065 | 1070 |
public: |
1066 | 1071 |
/// Constructor. |
1067 | 1072 |
|
1068 | 1073 |
/// This constructor does not require parameters, therefore it initiates |
1069 | 1074 |
/// all of the attributes to \c 0. |
1070 | 1075 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1071 | 1076 |
_dist(0), _path(0), _di(0) {} |
1072 | 1077 |
|
1073 | 1078 |
/// Constructor. |
1074 | 1079 |
|
1075 | 1080 |
/// This constructor requires two parameters, |
1076 | 1081 |
/// others are initiated to \c 0. |
1077 | 1082 |
/// \param g The digraph the algorithm runs on. |
1078 | 1083 |
/// \param l The length map. |
1079 | 1084 |
DijkstraWizardBase(const GR &g,const LEN &l) : |
1080 | 1085 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1081 | 1086 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&l))), |
1082 | 1087 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1083 | 1088 |
|
1084 | 1089 |
}; |
1085 | 1090 |
|
1086 | 1091 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1087 | 1092 |
|
1088 | 1093 |
/// This auxiliary class is created to implement the |
1089 | 1094 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1090 | 1095 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
1091 | 1096 |
/// functions and features of the plain \ref Dijkstra. |
1092 | 1097 |
/// |
1093 | 1098 |
/// This class should only be used through the \ref dijkstra() function, |
1094 | 1099 |
/// which makes it easier to use the algorithm. |
1100 |
/// |
|
1101 |
/// \tparam TR The traits class that defines various types used by the |
|
1102 |
/// algorithm. |
|
1095 | 1103 |
template<class TR> |
1096 | 1104 |
class DijkstraWizard : public TR |
1097 | 1105 |
{ |
1098 | 1106 |
typedef TR Base; |
1099 | 1107 |
|
1100 | 1108 |
typedef typename TR::Digraph Digraph; |
1101 | 1109 |
|
1102 | 1110 |
typedef typename Digraph::Node Node; |
1103 | 1111 |
typedef typename Digraph::NodeIt NodeIt; |
1104 | 1112 |
typedef typename Digraph::Arc Arc; |
1105 | 1113 |
typedef typename Digraph::OutArcIt OutArcIt; |
1106 | 1114 |
|
1107 | 1115 |
typedef typename TR::LengthMap LengthMap; |
1108 | 1116 |
typedef typename LengthMap::Value Value; |
1109 | 1117 |
typedef typename TR::PredMap PredMap; |
1110 | 1118 |
typedef typename TR::DistMap DistMap; |
1111 | 1119 |
typedef typename TR::ProcessedMap ProcessedMap; |
1112 | 1120 |
typedef typename TR::Path Path; |
1113 | 1121 |
typedef typename TR::Heap Heap; |
1114 | 1122 |
|
1115 | 1123 |
public: |
1116 | 1124 |
|
1117 | 1125 |
/// Constructor. |
1118 | 1126 |
DijkstraWizard() : TR() {} |
1119 | 1127 |
|
1120 | 1128 |
/// Constructor that requires parameters. |
1121 | 1129 |
|
1122 | 1130 |
/// Constructor that requires parameters. |
1123 | 1131 |
/// These parameters will be the default values for the traits class. |
1124 | 1132 |
/// \param g The digraph the algorithm runs on. |
1125 | 1133 |
/// \param l The length map. |
1126 | 1134 |
DijkstraWizard(const Digraph &g, const LengthMap &l) : |
1127 | 1135 |
TR(g,l) {} |
1128 | 1136 |
|
1129 | 1137 |
///Copy constructor |
1130 | 1138 |
DijkstraWizard(const TR &b) : TR(b) {} |
1131 | 1139 |
|
1132 | 1140 |
~DijkstraWizard() {} |
1133 | 1141 |
|
1134 | 1142 |
///Runs Dijkstra algorithm from the given source node. |
1135 | 1143 |
|
1136 | 1144 |
///This method runs %Dijkstra algorithm from the given source node |
1137 | 1145 |
///in order to compute the shortest path to each node. |
1138 | 1146 |
void run(Node s) |
1139 | 1147 |
{ |
1140 | 1148 |
Dijkstra<Digraph,LengthMap,TR> |
1141 | 1149 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
1142 | 1150 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
... | ... |
@@ -61,119 +61,123 @@ |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
63 | 63 |
typedef double LargeValue; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addFront() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer value types |
77 | 77 |
template <typename GR, typename LEN> |
78 | 78 |
struct HartmannOrlinDefaultTraits<GR, LEN, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef LEN LengthMap; |
82 | 82 |
typedef typename LengthMap::Value Value; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeValue; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeValue; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of the Hartmann-Orlin algorithm for finding |
97 | 97 |
/// a minimum mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements the Hartmann-Orlin algorithm for finding |
100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
103 | 103 |
/// it applies an efficient early termination scheme. |
104 | 104 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
105 | 105 |
/// |
106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
107 | 107 |
/// \tparam LEN The type of the length map. The default |
108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
109 |
/// \tparam TR The traits class that defines various types used by the |
|
110 |
/// algorithm. By default, it is \ref HartmannOrlinDefaultTraits |
|
111 |
/// "HartmannOrlinDefaultTraits<GR, LEN>". |
|
112 |
/// In most cases, this parameter should not be set directly, |
|
113 |
/// consider to use the named template parameters instead. |
|
109 | 114 |
#ifdef DOXYGEN |
110 | 115 |
template <typename GR, typename LEN, typename TR> |
111 | 116 |
#else |
112 | 117 |
template < typename GR, |
113 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
114 | 119 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
115 | 120 |
#endif |
116 | 121 |
class HartmannOrlin |
117 | 122 |
{ |
118 | 123 |
public: |
119 | 124 |
|
120 | 125 |
/// The type of the digraph |
121 | 126 |
typedef typename TR::Digraph Digraph; |
122 | 127 |
/// The type of the length map |
123 | 128 |
typedef typename TR::LengthMap LengthMap; |
124 | 129 |
/// The type of the arc lengths |
125 | 130 |
typedef typename TR::Value Value; |
126 | 131 |
|
127 | 132 |
/// \brief The large value type |
128 | 133 |
/// |
129 | 134 |
/// The large value type used for internal computations. |
130 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
|
131 |
/// it is \c long \c long if the \c Value type is integer, |
|
135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
132 | 136 |
/// otherwise it is \c double. |
133 | 137 |
typedef typename TR::LargeValue LargeValue; |
134 | 138 |
|
135 | 139 |
/// The tolerance type |
136 | 140 |
typedef typename TR::Tolerance Tolerance; |
137 | 141 |
|
138 | 142 |
/// \brief The path type of the found cycles |
139 | 143 |
/// |
140 | 144 |
/// The path type of the found cycles. |
141 | 145 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
142 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
143 | 147 |
typedef typename TR::Path Path; |
144 | 148 |
|
145 | 149 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
146 | 150 |
typedef TR Traits; |
147 | 151 |
|
148 | 152 |
private: |
149 | 153 |
|
150 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
151 | 155 |
|
152 | 156 |
// Data sturcture for path data |
153 | 157 |
struct PathData |
154 | 158 |
{ |
155 | 159 |
LargeValue dist; |
156 | 160 |
Arc pred; |
157 | 161 |
PathData(LargeValue d, Arc p = INVALID) : |
158 | 162 |
dist(d), pred(p) {} |
159 | 163 |
}; |
160 | 164 |
|
161 | 165 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
162 | 166 |
PathDataNodeMap; |
163 | 167 |
|
164 | 168 |
private: |
165 | 169 |
|
166 | 170 |
// The digraph the algorithm runs on |
167 | 171 |
const Digraph &_gr; |
168 | 172 |
// The length of the arcs |
169 | 173 |
const LengthMap &_length; |
170 | 174 |
|
171 | 175 |
// Data for storing the strongly connected components |
172 | 176 |
int _comp_num; |
173 | 177 |
typename Digraph::template NodeMap<int> _comp; |
174 | 178 |
std::vector<std::vector<Node> > _comp_nodes; |
175 | 179 |
std::vector<Node>* _nodes; |
176 | 180 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
177 | 181 |
|
178 | 182 |
// Data for the found cycles |
179 | 183 |
bool _curr_found, _best_found; |
... | ... |
@@ -61,119 +61,123 @@ |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
63 | 63 |
typedef double LargeValue; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addBack() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer value types |
77 | 77 |
template <typename GR, typename LEN> |
78 | 78 |
struct HowardDefaultTraits<GR, LEN, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef LEN LengthMap; |
82 | 82 |
typedef typename LengthMap::Value Value; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeValue; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeValue; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of Howard's algorithm for finding a minimum |
97 | 97 |
/// mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements Howard's policy iteration algorithm for finding |
100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// This class provides the most efficient algorithm for the |
103 | 103 |
/// minimum mean cycle problem, though the best known theoretical |
104 | 104 |
/// bound on its running time is exponential. |
105 | 105 |
/// |
106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
107 | 107 |
/// \tparam LEN The type of the length map. The default |
108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
109 |
/// \tparam TR The traits class that defines various types used by the |
|
110 |
/// algorithm. By default, it is \ref HowardDefaultTraits |
|
111 |
/// "HowardDefaultTraits<GR, LEN>". |
|
112 |
/// In most cases, this parameter should not be set directly, |
|
113 |
/// consider to use the named template parameters instead. |
|
109 | 114 |
#ifdef DOXYGEN |
110 | 115 |
template <typename GR, typename LEN, typename TR> |
111 | 116 |
#else |
112 | 117 |
template < typename GR, |
113 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
114 | 119 |
typename TR = HowardDefaultTraits<GR, LEN> > |
115 | 120 |
#endif |
116 | 121 |
class Howard |
117 | 122 |
{ |
118 | 123 |
public: |
119 | 124 |
|
120 | 125 |
/// The type of the digraph |
121 | 126 |
typedef typename TR::Digraph Digraph; |
122 | 127 |
/// The type of the length map |
123 | 128 |
typedef typename TR::LengthMap LengthMap; |
124 | 129 |
/// The type of the arc lengths |
125 | 130 |
typedef typename TR::Value Value; |
126 | 131 |
|
127 | 132 |
/// \brief The large value type |
128 | 133 |
/// |
129 | 134 |
/// The large value type used for internal computations. |
130 |
/// Using the \ref HowardDefaultTraits "default traits class", |
|
131 |
/// it is \c long \c long if the \c Value type is integer, |
|
135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
132 | 136 |
/// otherwise it is \c double. |
133 | 137 |
typedef typename TR::LargeValue LargeValue; |
134 | 138 |
|
135 | 139 |
/// The tolerance type |
136 | 140 |
typedef typename TR::Tolerance Tolerance; |
137 | 141 |
|
138 | 142 |
/// \brief The path type of the found cycles |
139 | 143 |
/// |
140 | 144 |
/// The path type of the found cycles. |
141 | 145 |
/// Using the \ref HowardDefaultTraits "default traits class", |
142 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
143 | 147 |
typedef typename TR::Path Path; |
144 | 148 |
|
145 | 149 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
146 | 150 |
typedef TR Traits; |
147 | 151 |
|
148 | 152 |
private: |
149 | 153 |
|
150 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
151 | 155 |
|
152 | 156 |
// The digraph the algorithm runs on |
153 | 157 |
const Digraph &_gr; |
154 | 158 |
// The length of the arcs |
155 | 159 |
const LengthMap &_length; |
156 | 160 |
|
157 | 161 |
// Data for the found cycles |
158 | 162 |
bool _curr_found, _best_found; |
159 | 163 |
LargeValue _curr_length, _best_length; |
160 | 164 |
int _curr_size, _best_size; |
161 | 165 |
Node _curr_node, _best_node; |
162 | 166 |
|
163 | 167 |
Path *_cycle_path; |
164 | 168 |
bool _local_path; |
165 | 169 |
|
166 | 170 |
// Internal data used by the algorithm |
167 | 171 |
typename Digraph::template NodeMap<Arc> _policy; |
168 | 172 |
typename Digraph::template NodeMap<bool> _reached; |
169 | 173 |
typename Digraph::template NodeMap<int> _level; |
170 | 174 |
typename Digraph::template NodeMap<LargeValue> _dist; |
171 | 175 |
|
172 | 176 |
// Data for storing the strongly connected components |
173 | 177 |
int _comp_num; |
174 | 178 |
typename Digraph::template NodeMap<int> _comp; |
175 | 179 |
std::vector<std::vector<Node> > _comp_nodes; |
176 | 180 |
std::vector<Node>* _nodes; |
177 | 181 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
178 | 182 |
|
179 | 183 |
// Queue used for BFS search |
... | ... |
@@ -59,119 +59,123 @@ |
59 | 59 |
/// The large value type used for internal computations. |
60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
63 | 63 |
typedef double LargeValue; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addFront() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer value types |
77 | 77 |
template <typename GR, typename LEN> |
78 | 78 |
struct KarpDefaultTraits<GR, LEN, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef LEN LengthMap; |
82 | 82 |
typedef typename LengthMap::Value Value; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeValue; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeValue; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of Karp's algorithm for finding a minimum |
97 | 97 |
/// mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements Karp's algorithm for finding a directed |
100 | 100 |
/// cycle of minimum mean length (cost) in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
103 | 103 |
/// |
104 | 104 |
/// \tparam GR The type of the digraph the algorithm runs on. |
105 | 105 |
/// \tparam LEN The type of the length map. The default |
106 | 106 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
107 |
/// \tparam TR The traits class that defines various types used by the |
|
108 |
/// algorithm. By default, it is \ref KarpDefaultTraits |
|
109 |
/// "KarpDefaultTraits<GR, LEN>". |
|
110 |
/// In most cases, this parameter should not be set directly, |
|
111 |
/// consider to use the named template parameters instead. |
|
107 | 112 |
#ifdef DOXYGEN |
108 | 113 |
template <typename GR, typename LEN, typename TR> |
109 | 114 |
#else |
110 | 115 |
template < typename GR, |
111 | 116 |
typename LEN = typename GR::template ArcMap<int>, |
112 | 117 |
typename TR = KarpDefaultTraits<GR, LEN> > |
113 | 118 |
#endif |
114 | 119 |
class Karp |
115 | 120 |
{ |
116 | 121 |
public: |
117 | 122 |
|
118 | 123 |
/// The type of the digraph |
119 | 124 |
typedef typename TR::Digraph Digraph; |
120 | 125 |
/// The type of the length map |
121 | 126 |
typedef typename TR::LengthMap LengthMap; |
122 | 127 |
/// The type of the arc lengths |
123 | 128 |
typedef typename TR::Value Value; |
124 | 129 |
|
125 | 130 |
/// \brief The large value type |
126 | 131 |
/// |
127 | 132 |
/// The large value type used for internal computations. |
128 |
/// Using the \ref KarpDefaultTraits "default traits class", |
|
129 |
/// it is \c long \c long if the \c Value type is integer, |
|
133 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
130 | 134 |
/// otherwise it is \c double. |
131 | 135 |
typedef typename TR::LargeValue LargeValue; |
132 | 136 |
|
133 | 137 |
/// The tolerance type |
134 | 138 |
typedef typename TR::Tolerance Tolerance; |
135 | 139 |
|
136 | 140 |
/// \brief The path type of the found cycles |
137 | 141 |
/// |
138 | 142 |
/// The path type of the found cycles. |
139 | 143 |
/// Using the \ref KarpDefaultTraits "default traits class", |
140 | 144 |
/// it is \ref lemon::Path "Path<Digraph>". |
141 | 145 |
typedef typename TR::Path Path; |
142 | 146 |
|
143 | 147 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
144 | 148 |
typedef TR Traits; |
145 | 149 |
|
146 | 150 |
private: |
147 | 151 |
|
148 | 152 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
149 | 153 |
|
150 | 154 |
// Data sturcture for path data |
151 | 155 |
struct PathData |
152 | 156 |
{ |
153 | 157 |
LargeValue dist; |
154 | 158 |
Arc pred; |
155 | 159 |
PathData(LargeValue d, Arc p = INVALID) : |
156 | 160 |
dist(d), pred(p) {} |
157 | 161 |
}; |
158 | 162 |
|
159 | 163 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
160 | 164 |
PathDataNodeMap; |
161 | 165 |
|
162 | 166 |
private: |
163 | 167 |
|
164 | 168 |
// The digraph the algorithm runs on |
165 | 169 |
const Digraph &_gr; |
166 | 170 |
// The length of the arcs |
167 | 171 |
const LengthMap &_length; |
168 | 172 |
|
169 | 173 |
// Data for storing the strongly connected components |
170 | 174 |
int _comp_num; |
171 | 175 |
typename Digraph::template NodeMap<int> _comp; |
172 | 176 |
std::vector<std::vector<Node> > _comp_nodes; |
173 | 177 |
std::vector<Node>* _nodes; |
174 | 178 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
175 | 179 |
|
176 | 180 |
// Data for the found cycle |
177 | 181 |
LargeValue _cycle_length; |
... | ... |
@@ -67,107 +67,108 @@ |
67 | 67 |
/// \brief Instantiates a \c ArborescenceMap. |
68 | 68 |
/// |
69 | 69 |
/// This function instantiates a \c ArborescenceMap. |
70 | 70 |
/// \param digraph The digraph to which we would like to calculate |
71 | 71 |
/// the \c ArborescenceMap. |
72 | 72 |
static ArborescenceMap *createArborescenceMap(const Digraph &digraph){ |
73 | 73 |
return new ArborescenceMap(digraph); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
/// \brief The type of the \c PredMap |
77 | 77 |
/// |
78 | 78 |
/// The type of the \c PredMap. It must confrom to the |
79 | 79 |
/// \ref concepts::WriteMap "WriteMap" concept, and its value type |
80 | 80 |
/// must be the \c Arc type of the digraph. |
81 | 81 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
82 | 82 |
|
83 | 83 |
/// \brief Instantiates a \c PredMap. |
84 | 84 |
/// |
85 | 85 |
/// This function instantiates a \c PredMap. |
86 | 86 |
/// \param digraph The digraph to which we would like to define the |
87 | 87 |
/// \c PredMap. |
88 | 88 |
static PredMap *createPredMap(const Digraph &digraph){ |
89 | 89 |
return new PredMap(digraph); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
/// \ingroup spantree |
95 | 95 |
/// |
96 | 96 |
/// \brief Minimum Cost Arborescence algorithm class. |
97 | 97 |
/// |
98 | 98 |
/// This class provides an efficient implementation of the |
99 | 99 |
/// Minimum Cost Arborescence algorithm. The arborescence is a tree |
100 | 100 |
/// which is directed from a given source node of the digraph. One or |
101 | 101 |
/// more sources should be given to the algorithm and it will calculate |
102 | 102 |
/// the minimum cost subgraph that is the union of arborescences with the |
103 | 103 |
/// given sources and spans all the nodes which are reachable from the |
104 | 104 |
/// sources. The time complexity of the algorithm is O(n<sup>2</sup>+e). |
105 | 105 |
/// |
106 | 106 |
/// The algorithm also provides an optimal dual solution, therefore |
107 | 107 |
/// the optimality of the solution can be checked. |
108 | 108 |
/// |
109 | 109 |
/// \param GR The digraph type the algorithm runs on. |
110 | 110 |
/// \param CM A read-only arc map storing the costs of the |
111 | 111 |
/// arcs. It is read once for each arc, so the map may involve in |
112 | 112 |
/// relatively time consuming process to compute the arc costs if |
113 | 113 |
/// it is necessary. The default map type is \ref |
114 | 114 |
/// concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
115 |
/// \param TR Traits class to set various data types used |
|
116 |
/// by the algorithm. The default traits class is |
|
117 |
/// \ |
|
115 |
/// \tparam TR The traits class that defines various types used by the |
|
116 |
/// algorithm. By default, it is \ref MinCostArborescenceDefaultTraits |
|
118 | 117 |
/// "MinCostArborescenceDefaultTraits<GR, CM>". |
118 |
/// In most cases, this parameter should not be set directly, |
|
119 |
/// consider to use the named template parameters instead. |
|
119 | 120 |
#ifndef DOXYGEN |
120 | 121 |
template <typename GR, |
121 | 122 |
typename CM = typename GR::template ArcMap<int>, |
122 | 123 |
typename TR = |
123 | 124 |
MinCostArborescenceDefaultTraits<GR, CM> > |
124 | 125 |
#else |
125 |
template <typename GR, typename CM, |
|
126 |
template <typename GR, typename CM, typename TR> |
|
126 | 127 |
#endif |
127 | 128 |
class MinCostArborescence { |
128 | 129 |
public: |
129 | 130 |
|
130 | 131 |
/// \brief The \ref MinCostArborescenceDefaultTraits "traits class" |
131 | 132 |
/// of the algorithm. |
132 | 133 |
typedef TR Traits; |
133 | 134 |
/// The type of the underlying digraph. |
134 | 135 |
typedef typename Traits::Digraph Digraph; |
135 | 136 |
/// The type of the map that stores the arc costs. |
136 | 137 |
typedef typename Traits::CostMap CostMap; |
137 | 138 |
///The type of the costs of the arcs. |
138 | 139 |
typedef typename Traits::Value Value; |
139 | 140 |
///The type of the predecessor map. |
140 | 141 |
typedef typename Traits::PredMap PredMap; |
141 | 142 |
///The type of the map that stores which arcs are in the arborescence. |
142 | 143 |
typedef typename Traits::ArborescenceMap ArborescenceMap; |
143 | 144 |
|
144 | 145 |
typedef MinCostArborescence Create; |
145 | 146 |
|
146 | 147 |
private: |
147 | 148 |
|
148 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
149 | 150 |
|
150 | 151 |
struct CostArc { |
151 | 152 |
|
152 | 153 |
Arc arc; |
153 | 154 |
Value value; |
154 | 155 |
|
155 | 156 |
CostArc() {} |
156 | 157 |
CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {} |
157 | 158 |
|
158 | 159 |
}; |
159 | 160 |
|
160 | 161 |
const Digraph *_digraph; |
161 | 162 |
const CostMap *_cost; |
162 | 163 |
|
163 | 164 |
PredMap *_pred; |
164 | 165 |
bool local_pred; |
165 | 166 |
|
166 | 167 |
ArborescenceMap *_arborescence; |
167 | 168 |
bool local_arborescence; |
168 | 169 |
|
169 | 170 |
typedef typename Digraph::template ArcMap<int> ArcOrder; |
170 | 171 |
ArcOrder *_arc_order; |
171 | 172 |
|
172 | 173 |
typedef typename Digraph::template NodeMap<int> NodeOrder; |
173 | 174 |
NodeOrder *_node_order; |
... | ... |
@@ -74,96 +74,101 @@ |
74 | 74 |
/// \sa Elevator, LinkedElevator |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
77 | 77 |
#else |
78 | 78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
79 | 79 |
#endif |
80 | 80 |
|
81 | 81 |
/// \brief Instantiates an Elevator. |
82 | 82 |
/// |
83 | 83 |
/// This function instantiates an \ref Elevator. |
84 | 84 |
/// \param digraph The digraph for which we would like to define |
85 | 85 |
/// the elevator. |
86 | 86 |
/// \param max_level The maximum level of the elevator. |
87 | 87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
88 | 88 |
return new Elevator(digraph, max_level); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
/// \brief The tolerance used by the algorithm |
92 | 92 |
/// |
93 | 93 |
/// The tolerance used by the algorithm to handle inexact computation. |
94 | 94 |
typedef lemon::Tolerance<Value> Tolerance; |
95 | 95 |
|
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
|
99 | 99 |
/// \ingroup max_flow |
100 | 100 |
/// |
101 | 101 |
/// \brief %Preflow algorithm class. |
102 | 102 |
/// |
103 | 103 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
104 | 104 |
/// \e push-relabel algorithm producing a \ref max_flow |
105 | 105 |
/// "flow of maximum value" in a digraph \ref clrs01algorithms, |
106 | 106 |
/// \ref amo93networkflows, \ref goldberg88newapproach. |
107 | 107 |
/// The preflow algorithms are the fastest known maximum |
108 | 108 |
/// flow algorithms. The current implementation uses a mixture of the |
109 | 109 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
110 | 110 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
111 | 111 |
/// |
112 | 112 |
/// The algorithm consists of two phases. After the first phase |
113 | 113 |
/// the maximum flow value and the minimum cut is obtained. The |
114 | 114 |
/// second phase constructs a feasible maximum flow on each arc. |
115 | 115 |
/// |
116 | 116 |
/// \warning This implementation cannot handle infinite or very large |
117 | 117 |
/// capacities (e.g. the maximum value of \c CAP::Value). |
118 | 118 |
/// |
119 | 119 |
/// \tparam GR The type of the digraph the algorithm runs on. |
120 | 120 |
/// \tparam CAP The type of the capacity map. The default map |
121 | 121 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
122 |
/// \tparam TR The traits class that defines various types used by the |
|
123 |
/// algorithm. By default, it is \ref PreflowDefaultTraits |
|
124 |
/// "PreflowDefaultTraits<GR, CAP>". |
|
125 |
/// In most cases, this parameter should not be set directly, |
|
126 |
/// consider to use the named template parameters instead. |
|
122 | 127 |
#ifdef DOXYGEN |
123 | 128 |
template <typename GR, typename CAP, typename TR> |
124 | 129 |
#else |
125 | 130 |
template <typename GR, |
126 | 131 |
typename CAP = typename GR::template ArcMap<int>, |
127 | 132 |
typename TR = PreflowDefaultTraits<GR, CAP> > |
128 | 133 |
#endif |
129 | 134 |
class Preflow { |
130 | 135 |
public: |
131 | 136 |
|
132 | 137 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
133 | 138 |
typedef TR Traits; |
134 | 139 |
///The type of the digraph the algorithm runs on. |
135 | 140 |
typedef typename Traits::Digraph Digraph; |
136 | 141 |
///The type of the capacity map. |
137 | 142 |
typedef typename Traits::CapacityMap CapacityMap; |
138 | 143 |
///The type of the flow values. |
139 | 144 |
typedef typename Traits::Value Value; |
140 | 145 |
|
141 | 146 |
///The type of the flow map. |
142 | 147 |
typedef typename Traits::FlowMap FlowMap; |
143 | 148 |
///The type of the elevator. |
144 | 149 |
typedef typename Traits::Elevator Elevator; |
145 | 150 |
///The type of the tolerance. |
146 | 151 |
typedef typename Traits::Tolerance Tolerance; |
147 | 152 |
|
148 | 153 |
private: |
149 | 154 |
|
150 | 155 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
151 | 156 |
|
152 | 157 |
const Digraph& _graph; |
153 | 158 |
const CapacityMap* _capacity; |
154 | 159 |
|
155 | 160 |
int _node_num; |
156 | 161 |
|
157 | 162 |
Node _source, _target; |
158 | 163 |
|
159 | 164 |
FlowMap* _flow; |
160 | 165 |
bool _local_flow; |
161 | 166 |
|
162 | 167 |
Elevator* _level; |
163 | 168 |
bool _local_level; |
164 | 169 |
|
165 | 170 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
166 | 171 |
ExcessMap* _excess; |
167 | 172 |
|
168 | 173 |
Tolerance _tolerance; |
169 | 174 |
|
0 comments (0 inline)