0
6
0
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 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_CAPACITY_SCALING_H |
| 20 | 20 |
#define LEMON_CAPACITY_SCALING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/bin_heap.h> |
| 31 | 31 |
|
| 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 | 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 | 82 |
/// algorithm. By default, it is the same as \c V. |
| 83 | 83 |
/// \tparam TR The traits class that defines various types used by the |
| 84 | 84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
| 85 | 85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
| 86 | 86 |
/// In most cases, this parameter should not be set directly, |
| 87 | 87 |
/// consider to use the named template parameters instead. |
| 88 | 88 |
/// |
| 89 | 89 |
/// \warning Both number types must be signed and all input data must |
| 90 | 90 |
/// be integer. |
| 91 | 91 |
/// \warning This algorithm does not support negative costs for such |
| 92 | 92 |
/// arcs that have infinite upper bound. |
| 93 | 93 |
#ifdef DOXYGEN |
| 94 | 94 |
template <typename GR, typename V, typename C, typename TR> |
| 95 | 95 |
#else |
| 96 | 96 |
template < typename GR, typename V = int, typename C = V, |
| 97 | 97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
| 98 | 98 |
#endif |
| 99 | 99 |
class CapacityScaling |
| 100 | 100 |
{
|
| 101 | 101 |
public: |
| 102 | 102 |
|
| 103 | 103 |
/// The type of the digraph |
| 104 | 104 |
typedef typename TR::Digraph Digraph; |
| 105 | 105 |
/// The type of the flow amounts, capacity bounds and supply values |
| 106 | 106 |
typedef typename TR::Value Value; |
| 107 | 107 |
/// The type of the arc costs |
| 108 | 108 |
typedef typename TR::Cost Cost; |
| 109 | 109 |
|
| 110 | 110 |
/// The type of the heap used for internal Dijkstra computations |
| 111 | 111 |
typedef typename TR::Heap Heap; |
| 112 | 112 |
|
| 113 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
| 114 | 114 |
typedef TR Traits; |
| 115 | 115 |
|
| 116 | 116 |
public: |
| 117 | 117 |
|
| 118 | 118 |
/// \brief Problem type constants for the \c run() function. |
| 119 | 119 |
/// |
| 120 | 120 |
/// Enum type containing the problem type constants that can be |
| 121 | 121 |
/// returned by the \ref run() function of the algorithm. |
| 122 | 122 |
enum ProblemType {
|
| 123 | 123 |
/// The problem has no feasible solution (flow). |
| 124 | 124 |
INFEASIBLE, |
| 125 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
| 126 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
| 127 | 127 |
/// potentials (primal and dual solutions). |
| 128 | 128 |
OPTIMAL, |
| 129 | 129 |
/// The digraph contains an arc of negative cost and infinite |
| 130 | 130 |
/// upper bound. It means that the objective function is unbounded |
| 131 | 131 |
/// on that arc, however, note that it could actually be bounded |
| 132 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
| 133 | 133 |
/// these cases. |
| 134 | 134 |
UNBOUNDED |
| 135 | 135 |
}; |
| 136 | 136 |
|
| 137 | 137 |
private: |
| 138 | 138 |
|
| 139 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 140 | 140 |
|
| 141 | 141 |
typedef std::vector<int> IntVector; |
| 142 | 142 |
typedef std::vector<Value> ValueVector; |
| 143 | 143 |
typedef std::vector<Cost> CostVector; |
| 144 | 144 |
typedef std::vector<char> BoolVector; |
| 145 | 145 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 146 | 146 |
|
| 147 | 147 |
private: |
| 148 | 148 |
|
| 149 | 149 |
// Data related to the underlying digraph |
| 150 | 150 |
const GR &_graph; |
| 151 | 151 |
int _node_num; |
| 152 | 152 |
int _arc_num; |
| 153 | 153 |
int _res_arc_num; |
| 154 | 154 |
int _root; |
| 155 | 155 |
|
| 156 | 156 |
// Parameters of the problem |
| 157 | 157 |
bool _have_lower; |
| 158 | 158 |
Value _sum_supply; |
| 159 | 159 |
|
| 160 | 160 |
// Data structures for storing the digraph |
| 161 | 161 |
IntNodeMap _node_id; |
| 162 | 162 |
IntArcMap _arc_idf; |
| 163 | 163 |
IntArcMap _arc_idb; |
| 164 | 164 |
IntVector _first_out; |
| 165 | 165 |
BoolVector _forward; |
| 166 | 166 |
IntVector _source; |
| 167 | 167 |
IntVector _target; |
| 168 | 168 |
IntVector _reverse; |
| 169 | 169 |
|
| 170 | 170 |
// Node and arc data |
| 171 | 171 |
ValueVector _lower; |
| 172 | 172 |
ValueVector _upper; |
| 173 | 173 |
CostVector _cost; |
| 174 | 174 |
ValueVector _supply; |
| 175 | 175 |
|
| 176 | 176 |
ValueVector _res_cap; |
| 177 | 177 |
CostVector _pi; |
| 178 | 178 |
ValueVector _excess; |
| 179 | 179 |
IntVector _excess_nodes; |
| 180 | 180 |
IntVector _deficit_nodes; |
| 181 | 181 |
|
| 182 | 182 |
Value _delta; |
| 183 | 183 |
int _factor; |
| 184 | 184 |
IntVector _pred; |
| 185 | 185 |
|
| 186 | 186 |
public: |
| 187 | 187 |
|
| 188 | 188 |
/// \brief Constant for infinite upper bounds (capacities). |
| 189 | 189 |
/// |
| 190 | 190 |
/// Constant for infinite upper bounds (capacities). |
| 191 | 191 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 192 | 192 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 193 | 193 |
const Value INF; |
| 194 | 194 |
|
| 195 | 195 |
private: |
| 196 | 196 |
|
| 197 | 197 |
// Special implementation of the Dijkstra algorithm for finding |
| 198 | 198 |
// shortest paths in the residual network of the digraph with |
| 199 | 199 |
// respect to the reduced arc costs and modifying the node |
| 200 | 200 |
// potentials according to the found distance labels. |
| 201 | 201 |
class ResidualDijkstra |
| 202 | 202 |
{
|
| 203 | 203 |
private: |
| 204 | 204 |
|
| 205 | 205 |
int _node_num; |
| 206 | 206 |
bool _geq; |
| 207 | 207 |
const IntVector &_first_out; |
| 208 | 208 |
const IntVector &_target; |
| 209 | 209 |
const CostVector &_cost; |
| 210 | 210 |
const ValueVector &_res_cap; |
| 211 | 211 |
const ValueVector &_excess; |
| 212 | 212 |
CostVector &_pi; |
| 213 | 213 |
IntVector &_pred; |
| 214 | 214 |
|
| 215 | 215 |
IntVector _proc_nodes; |
| 216 | 216 |
CostVector _dist; |
| 217 | 217 |
|
| 218 | 218 |
public: |
| 219 | 219 |
|
| 220 | 220 |
ResidualDijkstra(CapacityScaling& cs) : |
| 221 | 221 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
| 222 | 222 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
| 223 | 223 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
| 224 | 224 |
_pred(cs._pred), _dist(cs._node_num) |
| 225 | 225 |
{}
|
| 226 | 226 |
|
| 227 | 227 |
int run(int s, Value delta = 1) {
|
| 228 | 228 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
| 229 | 229 |
Heap heap(heap_cross_ref); |
| 230 | 230 |
heap.push(s, 0); |
| 231 | 231 |
_pred[s] = -1; |
| 232 | 232 |
_proc_nodes.clear(); |
| 233 | 233 |
|
| 234 | 234 |
// Process nodes |
| 235 | 235 |
while (!heap.empty() && _excess[heap.top()] > -delta) {
|
| 236 | 236 |
int u = heap.top(), v; |
| 237 | 237 |
Cost d = heap.prio() + _pi[u], dn; |
| 238 | 238 |
_dist[u] = heap.prio(); |
| 239 | 239 |
_proc_nodes.push_back(u); |
| 240 | 240 |
heap.pop(); |
| 241 | 241 |
|
| 242 | 242 |
// Traverse outgoing residual arcs |
| 243 | 243 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
| 244 | 244 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 245 | 245 |
if (_res_cap[a] < delta) continue; |
| 246 | 246 |
v = _target[a]; |
| 247 | 247 |
switch (heap.state(v)) {
|
| 248 | 248 |
case Heap::PRE_HEAP: |
| 249 | 249 |
heap.push(v, d + _cost[a] - _pi[v]); |
| 250 | 250 |
_pred[v] = a; |
| 251 | 251 |
break; |
| 252 | 252 |
case Heap::IN_HEAP: |
| 253 | 253 |
dn = d + _cost[a] - _pi[v]; |
| 254 | 254 |
if (dn < heap[v]) {
|
| 255 | 255 |
heap.decrease(v, dn); |
| 256 | 256 |
_pred[v] = a; |
| 257 | 257 |
} |
| 258 | 258 |
break; |
| 259 | 259 |
case Heap::POST_HEAP: |
| 260 | 260 |
break; |
| 261 | 261 |
} |
| 262 | 262 |
} |
| 263 | 263 |
} |
| 264 | 264 |
if (heap.empty()) return -1; |
| 265 | 265 |
|
| 266 | 266 |
// Update potentials of processed nodes |
| 267 | 267 |
int t = heap.top(); |
| 268 | 268 |
Cost dt = heap.prio(); |
| 269 | 269 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) {
|
| 270 | 270 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
return t; |
| 274 | 274 |
} |
| 275 | 275 |
|
| 276 | 276 |
}; //class ResidualDijkstra |
| 277 | 277 |
|
| 278 | 278 |
public: |
| 279 | 279 |
|
| 280 | 280 |
/// \name Named Template Parameters |
| 281 | 281 |
/// @{
|
| 282 | 282 |
|
| 283 | 283 |
template <typename T> |
| 284 | 284 |
struct SetHeapTraits : public Traits {
|
| 285 | 285 |
typedef T Heap; |
| 286 | 286 |
}; |
| 287 | 287 |
|
| 288 | 288 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 289 | 289 |
/// \c Heap type. |
| 290 | 290 |
/// |
| 291 | 291 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
| 292 | 292 |
/// type, which is used for internal Dijkstra computations. |
| 293 | 293 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
| 294 | 294 |
/// its priority type must be \c Cost and its cross reference type |
| 295 | 295 |
/// must be \ref RangeMap "RangeMap<int>". |
| 296 | 296 |
template <typename T> |
| 297 | 297 |
struct SetHeap |
| 298 | 298 |
: public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
|
| 299 | 299 |
typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create; |
| 300 | 300 |
}; |
| 301 | 301 |
|
| 302 | 302 |
/// @} |
| 303 | 303 |
|
| 304 |
protected: |
|
| 305 |
|
|
| 306 |
CapacityScaling() {}
|
|
| 307 |
|
|
| 304 | 308 |
public: |
| 305 | 309 |
|
| 306 | 310 |
/// \brief Constructor. |
| 307 | 311 |
/// |
| 308 | 312 |
/// The constructor of the class. |
| 309 | 313 |
/// |
| 310 | 314 |
/// \param graph The digraph the algorithm runs on. |
| 311 | 315 |
CapacityScaling(const GR& graph) : |
| 312 | 316 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 313 | 317 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 314 | 318 |
std::numeric_limits<Value>::infinity() : |
| 315 | 319 |
std::numeric_limits<Value>::max()) |
| 316 | 320 |
{
|
| 317 | 321 |
// Check the number types |
| 318 | 322 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 319 | 323 |
"The flow type of CapacityScaling must be signed"); |
| 320 | 324 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 321 | 325 |
"The cost type of CapacityScaling must be signed"); |
| 322 | 326 |
|
| 323 | 327 |
// Reset data structures |
| 324 | 328 |
reset(); |
| 325 | 329 |
} |
| 326 | 330 |
|
| 327 | 331 |
/// \name Parameters |
| 328 | 332 |
/// The parameters of the algorithm can be specified using these |
| 329 | 333 |
/// functions. |
| 330 | 334 |
|
| 331 | 335 |
/// @{
|
| 332 | 336 |
|
| 333 | 337 |
/// \brief Set the lower bounds on the arcs. |
| 334 | 338 |
/// |
| 335 | 339 |
/// This function sets the lower bounds on the arcs. |
| 336 | 340 |
/// If it is not used before calling \ref run(), the lower bounds |
| 337 | 341 |
/// will be set to zero on all arcs. |
| 338 | 342 |
/// |
| 339 | 343 |
/// \param map An arc map storing the lower bounds. |
| 340 | 344 |
/// Its \c Value type must be convertible to the \c Value type |
| 341 | 345 |
/// of the algorithm. |
| 342 | 346 |
/// |
| 343 | 347 |
/// \return <tt>(*this)</tt> |
| 344 | 348 |
template <typename LowerMap> |
| 345 | 349 |
CapacityScaling& lowerMap(const LowerMap& map) {
|
| 346 | 350 |
_have_lower = true; |
| 347 | 351 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 348 | 352 |
_lower[_arc_idf[a]] = map[a]; |
| 349 | 353 |
_lower[_arc_idb[a]] = map[a]; |
| 350 | 354 |
} |
| 351 | 355 |
return *this; |
| 352 | 356 |
} |
| 353 | 357 |
|
| 354 | 358 |
/// \brief Set the upper bounds (capacities) on the arcs. |
| 355 | 359 |
/// |
| 356 | 360 |
/// This function sets the upper bounds (capacities) on the arcs. |
| 357 | 361 |
/// If it is not used before calling \ref run(), the upper bounds |
| 358 | 362 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
| 359 | 363 |
/// unbounded from above). |
| 360 | 364 |
/// |
| 361 | 365 |
/// \param map An arc map storing the upper bounds. |
| 362 | 366 |
/// Its \c Value type must be convertible to the \c Value type |
| 363 | 367 |
/// of the algorithm. |
| 364 | 368 |
/// |
| 365 | 369 |
/// \return <tt>(*this)</tt> |
| 366 | 370 |
template<typename UpperMap> |
| 367 | 371 |
CapacityScaling& upperMap(const UpperMap& map) {
|
| 368 | 372 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 369 | 373 |
_upper[_arc_idf[a]] = map[a]; |
| 370 | 374 |
} |
| 371 | 375 |
return *this; |
| 372 | 376 |
} |
| 373 | 377 |
|
| 374 | 378 |
/// \brief Set the costs of the arcs. |
| 375 | 379 |
/// |
| 376 | 380 |
/// This function sets the costs of the arcs. |
| 377 | 381 |
/// If it is not used before calling \ref run(), the costs |
| 378 | 382 |
/// will be set to \c 1 on all arcs. |
| 379 | 383 |
/// |
| 380 | 384 |
/// \param map An arc map storing the costs. |
| 381 | 385 |
/// Its \c Value type must be convertible to the \c Cost type |
| 382 | 386 |
/// of the algorithm. |
| 383 | 387 |
/// |
| 384 | 388 |
/// \return <tt>(*this)</tt> |
| 385 | 389 |
template<typename CostMap> |
| 386 | 390 |
CapacityScaling& costMap(const CostMap& map) {
|
| 387 | 391 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 388 | 392 |
_cost[_arc_idf[a]] = map[a]; |
| 389 | 393 |
_cost[_arc_idb[a]] = -map[a]; |
| 390 | 394 |
} |
| 391 | 395 |
return *this; |
| 392 | 396 |
} |
| 393 | 397 |
|
| 394 | 398 |
/// \brief Set the supply values of the nodes. |
| 395 | 399 |
/// |
| 396 | 400 |
/// This function sets the supply values of the nodes. |
| 397 | 401 |
/// If neither this function nor \ref stSupply() is used before |
| 398 | 402 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 399 | 403 |
/// |
| 400 | 404 |
/// \param map A node map storing the supply values. |
| 401 | 405 |
/// Its \c Value type must be convertible to the \c Value type |
| 402 | 406 |
/// of the algorithm. |
| 403 | 407 |
/// |
| 404 | 408 |
/// \return <tt>(*this)</tt> |
| 405 | 409 |
template<typename SupplyMap> |
| 406 | 410 |
CapacityScaling& supplyMap(const SupplyMap& map) {
|
| 407 | 411 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 408 | 412 |
_supply[_node_id[n]] = map[n]; |
| 409 | 413 |
} |
| 410 | 414 |
return *this; |
| 411 | 415 |
} |
| 412 | 416 |
|
| 413 | 417 |
/// \brief Set single source and target nodes and a supply value. |
| 414 | 418 |
/// |
| 415 | 419 |
/// This function sets a single source node and a single target node |
| 416 | 420 |
/// and the required flow value. |
| 417 | 421 |
/// If neither this function nor \ref supplyMap() is used before |
| 418 | 422 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 419 | 423 |
/// |
| 420 | 424 |
/// Using this function has the same effect as using \ref supplyMap() |
| 421 | 425 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 422 | 426 |
/// assigned to \c t and all other nodes have zero supply value. |
| 423 | 427 |
/// |
| 424 | 428 |
/// \param s The source node. |
| 425 | 429 |
/// \param t The target node. |
| 426 | 430 |
/// \param k The required amount of flow from node \c s to node \c t |
| 427 | 431 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 428 | 432 |
/// |
| 429 | 433 |
/// \return <tt>(*this)</tt> |
| 430 | 434 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 431 | 435 |
for (int i = 0; i != _node_num; ++i) {
|
| 432 | 436 |
_supply[i] = 0; |
| 433 | 437 |
} |
| 434 | 438 |
_supply[_node_id[s]] = k; |
| 435 | 439 |
_supply[_node_id[t]] = -k; |
| 436 | 440 |
return *this; |
| 437 | 441 |
} |
| 438 | 442 |
|
| 439 | 443 |
/// @} |
| 440 | 444 |
|
| 441 | 445 |
/// \name Execution control |
| 442 | 446 |
/// The algorithm can be executed using \ref run(). |
| 443 | 447 |
|
| 444 | 448 |
/// @{
|
| 445 | 449 |
|
| 446 | 450 |
/// \brief Run the algorithm. |
| 447 | 451 |
/// |
| 448 | 452 |
/// This function runs the algorithm. |
| 449 | 453 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 450 | 454 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 451 | 455 |
/// For example, |
| 452 | 456 |
/// \code |
| 453 | 457 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 454 | 458 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 455 | 459 |
/// .supplyMap(sup).run(); |
| 456 | 460 |
/// \endcode |
| 457 | 461 |
/// |
| 458 | 462 |
/// This function can be called more than once. All the given parameters |
| 459 | 463 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 460 | 464 |
/// is used, thus only the modified parameters have to be set again. |
| 461 | 465 |
/// If the underlying digraph was also modified after the construction |
| 462 | 466 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 463 | 467 |
/// function must be called. |
| 464 | 468 |
/// |
| 465 | 469 |
/// \param factor The capacity scaling factor. It must be larger than |
| 466 | 470 |
/// one to use scaling. If it is less or equal to one, then scaling |
| 467 | 471 |
/// will be disabled. |
| 468 | 472 |
/// |
| 469 | 473 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 470 | 474 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 471 | 475 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 472 | 476 |
/// optimal flow and node potentials (primal and dual solutions), |
| 473 | 477 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 474 | 478 |
/// and infinite upper bound. It means that the objective function |
| 475 | 479 |
/// is unbounded on that arc, however, note that it could actually be |
| 476 | 480 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 477 | 481 |
/// these cases. |
| 478 | 482 |
/// |
| 479 | 483 |
/// \see ProblemType |
| 480 | 484 |
/// \see resetParams(), reset() |
| 481 | 485 |
ProblemType run(int factor = 4) {
|
| 482 | 486 |
_factor = factor; |
| 483 | 487 |
ProblemType pt = init(); |
| 484 | 488 |
if (pt != OPTIMAL) return pt; |
| 485 | 489 |
return start(); |
| 486 | 490 |
} |
| 487 | 491 |
|
| 488 | 492 |
/// \brief Reset all the parameters that have been given before. |
| 489 | 493 |
/// |
| 490 | 494 |
/// This function resets all the paramaters that have been given |
| 491 | 495 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 492 | 496 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 493 | 497 |
/// |
| 494 | 498 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
| 495 | 499 |
/// parameters are kept for the next \ref run() call, unless |
| 496 | 500 |
/// \ref resetParams() or \ref reset() is used. |
| 497 | 501 |
/// If the underlying digraph was also modified after the construction |
| 498 | 502 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 499 | 503 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 500 | 504 |
/// |
| 501 | 505 |
/// For example, |
| 502 | 506 |
/// \code |
| 503 | 507 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 504 | 508 |
/// |
| 505 | 509 |
/// // First run |
| 506 | 510 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 507 | 511 |
/// .supplyMap(sup).run(); |
| 508 | 512 |
/// |
| 509 | 513 |
/// // Run again with modified cost map (resetParams() is not called, |
| 510 | 514 |
/// // so only the cost map have to be set again) |
| 511 | 515 |
/// cost[e] += 100; |
| 512 | 516 |
/// cs.costMap(cost).run(); |
| 513 | 517 |
/// |
| 514 | 518 |
/// // Run again from scratch using resetParams() |
| 515 | 519 |
/// // (the lower bounds will be set to zero on all arcs) |
| 516 | 520 |
/// cs.resetParams(); |
| 517 | 521 |
/// cs.upperMap(capacity).costMap(cost) |
| 518 | 522 |
/// .supplyMap(sup).run(); |
| 519 | 523 |
/// \endcode |
| 520 | 524 |
/// |
| 521 | 525 |
/// \return <tt>(*this)</tt> |
| 522 | 526 |
/// |
| 523 | 527 |
/// \see reset(), run() |
| 524 | 528 |
CapacityScaling& resetParams() {
|
| 525 | 529 |
for (int i = 0; i != _node_num; ++i) {
|
| 526 | 530 |
_supply[i] = 0; |
| 527 | 531 |
} |
| 528 | 532 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 529 | 533 |
_lower[j] = 0; |
| 530 | 534 |
_upper[j] = INF; |
| 531 | 535 |
_cost[j] = _forward[j] ? 1 : -1; |
| 532 | 536 |
} |
| 533 | 537 |
_have_lower = false; |
| 534 | 538 |
return *this; |
| 535 | 539 |
} |
| 536 | 540 |
|
| 537 | 541 |
/// \brief Reset the internal data structures and all the parameters |
| 538 | 542 |
/// that have been given before. |
| 539 | 543 |
/// |
| 540 | 544 |
/// This function resets the internal data structures and all the |
| 541 | 545 |
/// paramaters that have been given before using functions \ref lowerMap(), |
| 542 | 546 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 543 | 547 |
/// |
| 544 | 548 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
| 545 | 549 |
/// parameters are kept for the next \ref run() call, unless |
| 546 | 550 |
/// \ref resetParams() or \ref reset() is used. |
| 547 | 551 |
/// If the underlying digraph was also modified after the construction |
| 548 | 552 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 549 | 553 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 550 | 554 |
/// |
| 551 | 555 |
/// See \ref resetParams() for examples. |
| 552 | 556 |
/// |
| 553 | 557 |
/// \return <tt>(*this)</tt> |
| 554 | 558 |
/// |
| 555 | 559 |
/// \see resetParams(), run() |
| 556 | 560 |
CapacityScaling& reset() {
|
| 557 | 561 |
// Resize vectors |
| 558 | 562 |
_node_num = countNodes(_graph); |
| 559 | 563 |
_arc_num = countArcs(_graph); |
| 560 | 564 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 561 | 565 |
_root = _node_num; |
| 562 | 566 |
++_node_num; |
| 563 | 567 |
|
| 564 | 568 |
_first_out.resize(_node_num + 1); |
| 565 | 569 |
_forward.resize(_res_arc_num); |
| 566 | 570 |
_source.resize(_res_arc_num); |
| 567 | 571 |
_target.resize(_res_arc_num); |
| 568 | 572 |
_reverse.resize(_res_arc_num); |
| 569 | 573 |
|
| 570 | 574 |
_lower.resize(_res_arc_num); |
| 571 | 575 |
_upper.resize(_res_arc_num); |
| 572 | 576 |
_cost.resize(_res_arc_num); |
| 573 | 577 |
_supply.resize(_node_num); |
| 574 | 578 |
|
| 575 | 579 |
_res_cap.resize(_res_arc_num); |
| 576 | 580 |
_pi.resize(_node_num); |
| 577 | 581 |
_excess.resize(_node_num); |
| 578 | 582 |
_pred.resize(_node_num); |
| 579 | 583 |
|
| 580 | 584 |
// Copy the graph |
| 581 | 585 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1; |
| 582 | 586 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 583 | 587 |
_node_id[n] = i; |
| 584 | 588 |
} |
| 585 | 589 |
i = 0; |
| 586 | 590 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 587 | 591 |
_first_out[i] = j; |
| 588 | 592 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 589 | 593 |
_arc_idf[a] = j; |
| 590 | 594 |
_forward[j] = true; |
| 591 | 595 |
_source[j] = i; |
| 592 | 596 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 593 | 597 |
} |
| 594 | 598 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 595 | 599 |
_arc_idb[a] = j; |
| 596 | 600 |
_forward[j] = false; |
| 597 | 601 |
_source[j] = i; |
| 598 | 602 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 599 | 603 |
} |
| 600 | 604 |
_forward[j] = false; |
| 601 | 605 |
_source[j] = i; |
| 602 | 606 |
_target[j] = _root; |
| 603 | 607 |
_reverse[j] = k; |
| 604 | 608 |
_forward[k] = true; |
| 605 | 609 |
_source[k] = _root; |
| 606 | 610 |
_target[k] = i; |
| 607 | 611 |
_reverse[k] = j; |
| 608 | 612 |
++j; ++k; |
| 609 | 613 |
} |
| 610 | 614 |
_first_out[i] = j; |
| 611 | 615 |
_first_out[_node_num] = k; |
| 612 | 616 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 613 | 617 |
int fi = _arc_idf[a]; |
| 614 | 618 |
int bi = _arc_idb[a]; |
| 615 | 619 |
_reverse[fi] = bi; |
| 616 | 620 |
_reverse[bi] = fi; |
| 617 | 621 |
} |
| 618 | 622 |
|
| 619 | 623 |
// Reset parameters |
| 620 | 624 |
resetParams(); |
| 621 | 625 |
return *this; |
| 622 | 626 |
} |
| 623 | 627 |
|
| 624 | 628 |
/// @} |
| 625 | 629 |
|
| 626 | 630 |
/// \name Query Functions |
| 627 | 631 |
/// The results of the algorithm can be obtained using these |
| 628 | 632 |
/// functions.\n |
| 629 | 633 |
/// The \ref run() function must be called before using them. |
| 630 | 634 |
|
| 631 | 635 |
/// @{
|
| 632 | 636 |
|
| 633 | 637 |
/// \brief Return the total cost of the found flow. |
| 634 | 638 |
/// |
| 635 | 639 |
/// This function returns the total cost of the found flow. |
| 636 | 640 |
/// Its complexity is O(e). |
| 637 | 641 |
/// |
| 638 | 642 |
/// \note The return type of the function can be specified as a |
| 639 | 643 |
/// template parameter. For example, |
| 640 | 644 |
/// \code |
| 641 | 645 |
/// cs.totalCost<double>(); |
| 642 | 646 |
/// \endcode |
| 643 | 647 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 644 | 648 |
/// type of the algorithm, which is the default return type of the |
| 645 | 649 |
/// function. |
| 646 | 650 |
/// |
| 647 | 651 |
/// \pre \ref run() must be called before using this function. |
| 648 | 652 |
template <typename Number> |
| 649 | 653 |
Number totalCost() const {
|
| 650 | 654 |
Number c = 0; |
| 651 | 655 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 652 | 656 |
int i = _arc_idb[a]; |
| 653 | 657 |
c += static_cast<Number>(_res_cap[i]) * |
| 654 | 658 |
(-static_cast<Number>(_cost[i])); |
| 655 | 659 |
} |
| 656 | 660 |
return c; |
| 657 | 661 |
} |
| 658 | 662 |
|
| 659 | 663 |
#ifndef DOXYGEN |
| 660 | 664 |
Cost totalCost() const {
|
| 661 | 665 |
return totalCost<Cost>(); |
| 662 | 666 |
} |
| 663 | 667 |
#endif |
| 664 | 668 |
|
| 665 | 669 |
/// \brief Return the flow on the given arc. |
| 666 | 670 |
/// |
| 667 | 671 |
/// This function returns the flow on the given arc. |
| 668 | 672 |
/// |
| 669 | 673 |
/// \pre \ref run() must be called before using this function. |
| 670 | 674 |
Value flow(const Arc& a) const {
|
| 671 | 675 |
return _res_cap[_arc_idb[a]]; |
| 672 | 676 |
} |
| 673 | 677 |
|
| 674 | 678 |
/// \brief Return the flow map (the primal solution). |
| 675 | 679 |
/// |
| 676 | 680 |
/// This function copies the flow value on each arc into the given |
| 677 | 681 |
/// map. The \c Value type of the algorithm must be convertible to |
| 678 | 682 |
/// the \c Value type of the map. |
| 679 | 683 |
/// |
| 680 | 684 |
/// \pre \ref run() must be called before using this function. |
| 681 | 685 |
template <typename FlowMap> |
| 682 | 686 |
void flowMap(FlowMap &map) const {
|
| 683 | 687 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 684 | 688 |
map.set(a, _res_cap[_arc_idb[a]]); |
| 685 | 689 |
} |
| 686 | 690 |
} |
| 687 | 691 |
|
| 688 | 692 |
/// \brief Return the potential (dual value) of the given node. |
| 689 | 693 |
/// |
| 690 | 694 |
/// This function returns the potential (dual value) of the |
| 691 | 695 |
/// given node. |
| 692 | 696 |
/// |
| 693 | 697 |
/// \pre \ref run() must be called before using this function. |
| 694 | 698 |
Cost potential(const Node& n) const {
|
| 695 | 699 |
return _pi[_node_id[n]]; |
| 696 | 700 |
} |
| 697 | 701 |
|
| 698 | 702 |
/// \brief Return the potential map (the dual solution). |
| 699 | 703 |
/// |
| 700 | 704 |
/// This function copies the potential (dual value) of each node |
| 701 | 705 |
/// into the given map. |
| 702 | 706 |
/// The \c Cost type of the algorithm must be convertible to the |
| 703 | 707 |
/// \c Value type of the map. |
| 704 | 708 |
/// |
| 705 | 709 |
/// \pre \ref run() must be called before using this function. |
| 706 | 710 |
template <typename PotentialMap> |
| 707 | 711 |
void potentialMap(PotentialMap &map) const {
|
| 708 | 712 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 709 | 713 |
map.set(n, _pi[_node_id[n]]); |
| 710 | 714 |
} |
| 711 | 715 |
} |
| 712 | 716 |
|
| 713 | 717 |
/// @} |
| 714 | 718 |
|
| 715 | 719 |
private: |
| 716 | 720 |
|
| 717 | 721 |
// Initialize the algorithm |
| 718 | 722 |
ProblemType init() {
|
| 719 | 723 |
if (_node_num <= 1) return INFEASIBLE; |
| 720 | 724 |
|
| 721 | 725 |
// Check the sum of supply values |
| 722 | 726 |
_sum_supply = 0; |
| 723 | 727 |
for (int i = 0; i != _root; ++i) {
|
| 724 | 728 |
_sum_supply += _supply[i]; |
| 725 | 729 |
} |
| 726 | 730 |
if (_sum_supply > 0) return INFEASIBLE; |
| 727 | 731 |
|
| 728 | 732 |
// Initialize vectors |
| 729 | 733 |
for (int i = 0; i != _root; ++i) {
|
| 730 | 734 |
_pi[i] = 0; |
| 731 | 735 |
_excess[i] = _supply[i]; |
| 732 | 736 |
} |
| 733 | 737 |
|
| 734 | 738 |
// Remove non-zero lower bounds |
| 735 | 739 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 736 | 740 |
int last_out; |
| 737 | 741 |
if (_have_lower) {
|
| 738 | 742 |
for (int i = 0; i != _root; ++i) {
|
| 739 | 743 |
last_out = _first_out[i+1]; |
| 740 | 744 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 741 | 745 |
if (_forward[j]) {
|
| 742 | 746 |
Value c = _lower[j]; |
| 743 | 747 |
if (c >= 0) {
|
| 744 | 748 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
| 745 | 749 |
} else {
|
| 746 | 750 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
| 747 | 751 |
} |
| 748 | 752 |
_excess[i] -= c; |
| 749 | 753 |
_excess[_target[j]] += c; |
| 750 | 754 |
} else {
|
| 751 | 755 |
_res_cap[j] = 0; |
| 752 | 756 |
} |
| 753 | 757 |
} |
| 754 | 758 |
} |
| 755 | 759 |
} else {
|
| 756 | 760 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 757 | 761 |
_res_cap[j] = _forward[j] ? _upper[j] : 0; |
| 758 | 762 |
} |
| 759 | 763 |
} |
| 760 | 764 |
|
| 761 | 765 |
// Handle negative costs |
| 762 | 766 |
for (int i = 0; i != _root; ++i) {
|
| 763 | 767 |
last_out = _first_out[i+1] - 1; |
| 764 | 768 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 765 | 769 |
Value rc = _res_cap[j]; |
| 766 | 770 |
if (_cost[j] < 0 && rc > 0) {
|
| 767 | 771 |
if (rc >= MAX) return UNBOUNDED; |
| 768 | 772 |
_excess[i] -= rc; |
| 769 | 773 |
_excess[_target[j]] += rc; |
| 770 | 774 |
_res_cap[j] = 0; |
| 771 | 775 |
_res_cap[_reverse[j]] += rc; |
| 772 | 776 |
} |
| 773 | 777 |
} |
| 774 | 778 |
} |
| 775 | 779 |
|
| 776 | 780 |
// Handle GEQ supply type |
| 777 | 781 |
if (_sum_supply < 0) {
|
| 778 | 782 |
_pi[_root] = 0; |
| 779 | 783 |
_excess[_root] = -_sum_supply; |
| 780 | 784 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 781 | 785 |
int ra = _reverse[a]; |
| 782 | 786 |
_res_cap[a] = -_sum_supply + 1; |
| 783 | 787 |
_res_cap[ra] = 0; |
| 784 | 788 |
_cost[a] = 0; |
| 785 | 789 |
_cost[ra] = 0; |
| 786 | 790 |
} |
| 787 | 791 |
} else {
|
| 788 | 792 |
_pi[_root] = 0; |
| 789 | 793 |
_excess[_root] = 0; |
| 790 | 794 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 791 | 795 |
int ra = _reverse[a]; |
| 792 | 796 |
_res_cap[a] = 1; |
| 793 | 797 |
_res_cap[ra] = 0; |
| 794 | 798 |
_cost[a] = 0; |
| 795 | 799 |
_cost[ra] = 0; |
| 796 | 800 |
} |
| 797 | 801 |
} |
| 798 | 802 |
|
| 799 | 803 |
// Initialize delta value |
| 800 | 804 |
if (_factor > 1) {
|
| 801 | 805 |
// With scaling |
| 802 | 806 |
Value max_sup = 0, max_dem = 0, max_cap = 0; |
| 803 | 807 |
for (int i = 0; i != _root; ++i) {
|
| 804 | 808 |
Value ex = _excess[i]; |
| 805 | 809 |
if ( ex > max_sup) max_sup = ex; |
| 806 | 810 |
if (-ex > max_dem) max_dem = -ex; |
| 807 | 811 |
int last_out = _first_out[i+1] - 1; |
| 808 | 812 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 809 | 813 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
| 810 | 814 |
} |
| 811 | 815 |
} |
| 812 | 816 |
max_sup = std::min(std::min(max_sup, max_dem), max_cap); |
| 813 | 817 |
for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ; |
| 814 | 818 |
} else {
|
| 815 | 819 |
// Without scaling |
| 816 | 820 |
_delta = 1; |
| 817 | 821 |
} |
| 818 | 822 |
|
| 819 | 823 |
return OPTIMAL; |
| 820 | 824 |
} |
| 821 | 825 |
|
| 822 | 826 |
ProblemType start() {
|
| 823 | 827 |
// Execute the algorithm |
| 824 | 828 |
ProblemType pt; |
| 825 | 829 |
if (_delta > 1) |
| 826 | 830 |
pt = startWithScaling(); |
| 827 | 831 |
else |
| 828 | 832 |
pt = startWithoutScaling(); |
| 829 | 833 |
|
| 830 | 834 |
// Handle non-zero lower bounds |
| 831 | 835 |
if (_have_lower) {
|
| 832 | 836 |
int limit = _first_out[_root]; |
| 833 | 837 |
for (int j = 0; j != limit; ++j) {
|
| 834 | 838 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 835 | 839 |
} |
| 836 | 840 |
} |
| 837 | 841 |
|
| 838 | 842 |
// Shift potentials if necessary |
| 839 | 843 |
Cost pr = _pi[_root]; |
| 840 | 844 |
if (_sum_supply < 0 || pr > 0) {
|
| 841 | 845 |
for (int i = 0; i != _node_num; ++i) {
|
| 842 | 846 |
_pi[i] -= pr; |
| 843 | 847 |
} |
| 844 | 848 |
} |
| 845 | 849 |
|
| 846 | 850 |
return pt; |
| 847 | 851 |
} |
| 848 | 852 |
|
| 849 | 853 |
// Execute the capacity scaling algorithm |
| 850 | 854 |
ProblemType startWithScaling() {
|
| 851 | 855 |
// Perform capacity scaling phases |
| 852 | 856 |
int s, t; |
| 853 | 857 |
ResidualDijkstra _dijkstra(*this); |
| 854 | 858 |
while (true) {
|
| 855 | 859 |
// Saturate all arcs not satisfying the optimality condition |
| 856 | 860 |
int last_out; |
| 857 | 861 |
for (int u = 0; u != _node_num; ++u) {
|
| 858 | 862 |
last_out = _sum_supply < 0 ? |
| 859 | 863 |
_first_out[u+1] : _first_out[u+1] - 1; |
| 860 | 864 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 861 | 865 |
int v = _target[a]; |
| 862 | 866 |
Cost c = _cost[a] + _pi[u] - _pi[v]; |
| 863 | 867 |
Value rc = _res_cap[a]; |
| 864 | 868 |
if (c < 0 && rc >= _delta) {
|
| 865 | 869 |
_excess[u] -= rc; |
| 866 | 870 |
_excess[v] += rc; |
| 867 | 871 |
_res_cap[a] = 0; |
| 868 | 872 |
_res_cap[_reverse[a]] += rc; |
| 869 | 873 |
} |
| 870 | 874 |
} |
| 871 | 875 |
} |
| 872 | 876 |
|
| 873 | 877 |
// Find excess nodes and deficit nodes |
| 874 | 878 |
_excess_nodes.clear(); |
| 875 | 879 |
_deficit_nodes.clear(); |
| 876 | 880 |
for (int u = 0; u != _node_num; ++u) {
|
| 877 | 881 |
Value ex = _excess[u]; |
| 878 | 882 |
if (ex >= _delta) _excess_nodes.push_back(u); |
| 879 | 883 |
if (ex <= -_delta) _deficit_nodes.push_back(u); |
| 880 | 884 |
} |
| 881 | 885 |
int next_node = 0, next_def_node = 0; |
| 882 | 886 |
|
| 883 | 887 |
// Find augmenting shortest paths |
| 884 | 888 |
while (next_node < int(_excess_nodes.size())) {
|
| 885 | 889 |
// Check deficit nodes |
| 886 | 890 |
if (_delta > 1) {
|
| 887 | 891 |
bool delta_deficit = false; |
| 888 | 892 |
for ( ; next_def_node < int(_deficit_nodes.size()); |
| 889 | 893 |
++next_def_node ) {
|
| 890 | 894 |
if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
|
| 891 | 895 |
delta_deficit = true; |
| 892 | 896 |
break; |
| 893 | 897 |
} |
| 894 | 898 |
} |
| 895 | 899 |
if (!delta_deficit) break; |
| 896 | 900 |
} |
| 897 | 901 |
|
| 898 | 902 |
// Run Dijkstra in the residual network |
| 899 | 903 |
s = _excess_nodes[next_node]; |
| 900 | 904 |
if ((t = _dijkstra.run(s, _delta)) == -1) {
|
| 901 | 905 |
if (_delta > 1) {
|
| 902 | 906 |
++next_node; |
| 903 | 907 |
continue; |
| 904 | 908 |
} |
| 905 | 909 |
return INFEASIBLE; |
| 906 | 910 |
} |
| 907 | 911 |
|
| 908 | 912 |
// Augment along a shortest path from s to t |
| 909 | 913 |
Value d = std::min(_excess[s], -_excess[t]); |
| 910 | 914 |
int u = t; |
| 911 | 915 |
int a; |
| 912 | 916 |
if (d > _delta) {
|
| 913 | 917 |
while ((a = _pred[u]) != -1) {
|
| 914 | 918 |
if (_res_cap[a] < d) d = _res_cap[a]; |
| 915 | 919 |
u = _source[a]; |
| 916 | 920 |
} |
| 917 | 921 |
} |
| 918 | 922 |
u = t; |
| 919 | 923 |
while ((a = _pred[u]) != -1) {
|
| 920 | 924 |
_res_cap[a] -= d; |
| 921 | 925 |
_res_cap[_reverse[a]] += d; |
| 922 | 926 |
u = _source[a]; |
| 923 | 927 |
} |
| 924 | 928 |
_excess[s] -= d; |
| 925 | 929 |
_excess[t] += d; |
| 926 | 930 |
|
| 927 | 931 |
if (_excess[s] < _delta) ++next_node; |
| 928 | 932 |
} |
| 929 | 933 |
|
| 930 | 934 |
if (_delta == 1) break; |
| 931 | 935 |
_delta = _delta <= _factor ? 1 : _delta / _factor; |
| 932 | 936 |
} |
| 933 | 937 |
|
| 934 | 938 |
return OPTIMAL; |
| 935 | 939 |
} |
| 936 | 940 |
|
| 937 | 941 |
// Execute the successive shortest path algorithm |
| 938 | 942 |
ProblemType startWithoutScaling() {
|
| 939 | 943 |
// Find excess nodes |
| 940 | 944 |
_excess_nodes.clear(); |
| 941 | 945 |
for (int i = 0; i != _node_num; ++i) {
|
| 942 | 946 |
if (_excess[i] > 0) _excess_nodes.push_back(i); |
| 943 | 947 |
} |
| 944 | 948 |
if (_excess_nodes.size() == 0) return OPTIMAL; |
| 945 | 949 |
int next_node = 0; |
| 946 | 950 |
|
| 947 | 951 |
// Find shortest paths |
| 948 | 952 |
int s, t; |
| 949 | 953 |
ResidualDijkstra _dijkstra(*this); |
| 950 | 954 |
while ( _excess[_excess_nodes[next_node]] > 0 || |
| 951 | 955 |
++next_node < int(_excess_nodes.size()) ) |
| 952 | 956 |
{
|
| 953 | 957 |
// Run Dijkstra in the residual network |
| 954 | 958 |
s = _excess_nodes[next_node]; |
| 955 | 959 |
if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE; |
| 956 | 960 |
|
| 957 | 961 |
// Augment along a shortest path from s to t |
| 958 | 962 |
Value d = std::min(_excess[s], -_excess[t]); |
| 959 | 963 |
int u = t; |
| 960 | 964 |
int a; |
| 961 | 965 |
if (d > 1) {
|
| 962 | 966 |
while ((a = _pred[u]) != -1) {
|
| 963 | 967 |
if (_res_cap[a] < d) d = _res_cap[a]; |
| 964 | 968 |
u = _source[a]; |
| 965 | 969 |
} |
| 966 | 970 |
} |
| 967 | 971 |
u = t; |
| 968 | 972 |
while ((a = _pred[u]) != -1) {
|
| 969 | 973 |
_res_cap[a] -= d; |
| 970 | 974 |
_res_cap[_reverse[a]] += d; |
| 971 | 975 |
u = _source[a]; |
| 972 | 976 |
} |
| 973 | 977 |
_excess[s] -= d; |
| 974 | 978 |
_excess[t] += d; |
| 975 | 979 |
} |
| 976 | 980 |
|
| 977 | 981 |
return OPTIMAL; |
| 978 | 982 |
} |
| 979 | 983 |
|
| 980 | 984 |
}; //class CapacityScaling |
| 981 | 985 |
|
| 982 | 986 |
///@} |
| 983 | 987 |
|
| 984 | 988 |
} //namespace lemon |
| 985 | 989 |
|
| 986 | 990 |
#endif //LEMON_CAPACITY_SCALING_H |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 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_COST_SCALING_H |
| 20 | 20 |
#define LEMON_COST_SCALING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <deque> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/core.h> |
| 31 | 31 |
#include <lemon/maps.h> |
| 32 | 32 |
#include <lemon/math.h> |
| 33 | 33 |
#include <lemon/static_graph.h> |
| 34 | 34 |
#include <lemon/circulation.h> |
| 35 | 35 |
#include <lemon/bellman_ford.h> |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief Default traits class of CostScaling algorithm. |
| 40 | 40 |
/// |
| 41 | 41 |
/// Default traits class of CostScaling algorithm. |
| 42 | 42 |
/// \tparam GR Digraph type. |
| 43 | 43 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 44 | 44 |
/// and supply values. By default it is \c int. |
| 45 | 45 |
/// \tparam C The number type used for costs and potentials. |
| 46 | 46 |
/// By default it is the same as \c V. |
| 47 | 47 |
#ifdef DOXYGEN |
| 48 | 48 |
template <typename GR, typename V = int, typename C = V> |
| 49 | 49 |
#else |
| 50 | 50 |
template < typename GR, typename V = int, typename C = V, |
| 51 | 51 |
bool integer = std::numeric_limits<C>::is_integer > |
| 52 | 52 |
#endif |
| 53 | 53 |
struct CostScalingDefaultTraits |
| 54 | 54 |
{
|
| 55 | 55 |
/// The type of the digraph |
| 56 | 56 |
typedef GR Digraph; |
| 57 | 57 |
/// The type of the flow amounts, capacity bounds and supply values |
| 58 | 58 |
typedef V Value; |
| 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 | 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 | 109 |
/// algorithm. By default, it is the same as \c V. |
| 110 | 110 |
/// \tparam TR The traits class that defines various types used by the |
| 111 | 111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
| 112 | 112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
| 113 | 113 |
/// In most cases, this parameter should not be set directly, |
| 114 | 114 |
/// consider to use the named template parameters instead. |
| 115 | 115 |
/// |
| 116 | 116 |
/// \warning Both number types must be signed and all input data must |
| 117 | 117 |
/// be integer. |
| 118 | 118 |
/// \warning This algorithm does not support negative costs for such |
| 119 | 119 |
/// arcs that have infinite upper bound. |
| 120 | 120 |
/// |
| 121 | 121 |
/// \note %CostScaling provides three different internal methods, |
| 122 | 122 |
/// from which the most efficient one is used by default. |
| 123 | 123 |
/// For more information, see \ref Method. |
| 124 | 124 |
#ifdef DOXYGEN |
| 125 | 125 |
template <typename GR, typename V, typename C, typename TR> |
| 126 | 126 |
#else |
| 127 | 127 |
template < typename GR, typename V = int, typename C = V, |
| 128 | 128 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
| 129 | 129 |
#endif |
| 130 | 130 |
class CostScaling |
| 131 | 131 |
{
|
| 132 | 132 |
public: |
| 133 | 133 |
|
| 134 | 134 |
/// The type of the digraph |
| 135 | 135 |
typedef typename TR::Digraph Digraph; |
| 136 | 136 |
/// The type of the flow amounts, capacity bounds and supply values |
| 137 | 137 |
typedef typename TR::Value Value; |
| 138 | 138 |
/// The type of the arc costs |
| 139 | 139 |
typedef typename TR::Cost Cost; |
| 140 | 140 |
|
| 141 | 141 |
/// \brief The large cost type |
| 142 | 142 |
/// |
| 143 | 143 |
/// The large cost type used for internal computations. |
| 144 | 144 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
| 145 | 145 |
/// otherwise it is \c double. |
| 146 | 146 |
typedef typename TR::LargeCost LargeCost; |
| 147 | 147 |
|
| 148 | 148 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
| 149 | 149 |
typedef TR Traits; |
| 150 | 150 |
|
| 151 | 151 |
public: |
| 152 | 152 |
|
| 153 | 153 |
/// \brief Problem type constants for the \c run() function. |
| 154 | 154 |
/// |
| 155 | 155 |
/// Enum type containing the problem type constants that can be |
| 156 | 156 |
/// returned by the \ref run() function of the algorithm. |
| 157 | 157 |
enum ProblemType {
|
| 158 | 158 |
/// The problem has no feasible solution (flow). |
| 159 | 159 |
INFEASIBLE, |
| 160 | 160 |
/// The problem has optimal solution (i.e. it is feasible and |
| 161 | 161 |
/// bounded), and the algorithm has found optimal flow and node |
| 162 | 162 |
/// potentials (primal and dual solutions). |
| 163 | 163 |
OPTIMAL, |
| 164 | 164 |
/// The digraph contains an arc of negative cost and infinite |
| 165 | 165 |
/// upper bound. It means that the objective function is unbounded |
| 166 | 166 |
/// on that arc, however, note that it could actually be bounded |
| 167 | 167 |
/// over the feasible flows, but this algroithm cannot handle |
| 168 | 168 |
/// these cases. |
| 169 | 169 |
UNBOUNDED |
| 170 | 170 |
}; |
| 171 | 171 |
|
| 172 | 172 |
/// \brief Constants for selecting the internal method. |
| 173 | 173 |
/// |
| 174 | 174 |
/// Enum type containing constants for selecting the internal method |
| 175 | 175 |
/// for the \ref run() function. |
| 176 | 176 |
/// |
| 177 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
| 178 | 178 |
/// in their base operations, which are used in conjunction with the |
| 179 | 179 |
/// relabel operation. |
| 180 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
| 181 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
| 182 | 182 |
/// the most efficient and the most robust on various test inputs. |
| 183 | 183 |
/// However, the other methods can be selected using the \ref run() |
| 184 | 184 |
/// function with the proper parameter. |
| 185 | 185 |
enum Method {
|
| 186 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
| 187 | 187 |
/// admissible arc at once. |
| 188 | 188 |
PUSH, |
| 189 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
| 190 | 190 |
/// paths from a node with excess to a node with deficit. |
| 191 | 191 |
AUGMENT, |
| 192 | 192 |
/// Partial augment operations are used, i.e. flow is moved on |
| 193 | 193 |
/// admissible paths started from a node with excess, but the |
| 194 | 194 |
/// lengths of these paths are limited. This method can be viewed |
| 195 | 195 |
/// as a combined version of the previous two operations. |
| 196 | 196 |
PARTIAL_AUGMENT |
| 197 | 197 |
}; |
| 198 | 198 |
|
| 199 | 199 |
private: |
| 200 | 200 |
|
| 201 | 201 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 202 | 202 |
|
| 203 | 203 |
typedef std::vector<int> IntVector; |
| 204 | 204 |
typedef std::vector<Value> ValueVector; |
| 205 | 205 |
typedef std::vector<Cost> CostVector; |
| 206 | 206 |
typedef std::vector<LargeCost> LargeCostVector; |
| 207 | 207 |
typedef std::vector<char> BoolVector; |
| 208 | 208 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 209 | 209 |
|
| 210 | 210 |
private: |
| 211 | 211 |
|
| 212 | 212 |
template <typename KT, typename VT> |
| 213 | 213 |
class StaticVectorMap {
|
| 214 | 214 |
public: |
| 215 | 215 |
typedef KT Key; |
| 216 | 216 |
typedef VT Value; |
| 217 | 217 |
|
| 218 | 218 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 219 | 219 |
|
| 220 | 220 |
const Value& operator[](const Key& key) const {
|
| 221 | 221 |
return _v[StaticDigraph::id(key)]; |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 | 224 |
Value& operator[](const Key& key) {
|
| 225 | 225 |
return _v[StaticDigraph::id(key)]; |
| 226 | 226 |
} |
| 227 | 227 |
|
| 228 | 228 |
void set(const Key& key, const Value& val) {
|
| 229 | 229 |
_v[StaticDigraph::id(key)] = val; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
private: |
| 233 | 233 |
std::vector<Value>& _v; |
| 234 | 234 |
}; |
| 235 | 235 |
|
| 236 | 236 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
| 237 | 237 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
| 238 | 238 |
|
| 239 | 239 |
private: |
| 240 | 240 |
|
| 241 | 241 |
// Data related to the underlying digraph |
| 242 | 242 |
const GR &_graph; |
| 243 | 243 |
int _node_num; |
| 244 | 244 |
int _arc_num; |
| 245 | 245 |
int _res_node_num; |
| 246 | 246 |
int _res_arc_num; |
| 247 | 247 |
int _root; |
| 248 | 248 |
|
| 249 | 249 |
// Parameters of the problem |
| 250 | 250 |
bool _have_lower; |
| 251 | 251 |
Value _sum_supply; |
| 252 | 252 |
int _sup_node_num; |
| 253 | 253 |
|
| 254 | 254 |
// Data structures for storing the digraph |
| 255 | 255 |
IntNodeMap _node_id; |
| 256 | 256 |
IntArcMap _arc_idf; |
| 257 | 257 |
IntArcMap _arc_idb; |
| 258 | 258 |
IntVector _first_out; |
| 259 | 259 |
BoolVector _forward; |
| 260 | 260 |
IntVector _source; |
| 261 | 261 |
IntVector _target; |
| 262 | 262 |
IntVector _reverse; |
| 263 | 263 |
|
| 264 | 264 |
// Node and arc data |
| 265 | 265 |
ValueVector _lower; |
| 266 | 266 |
ValueVector _upper; |
| 267 | 267 |
CostVector _scost; |
| 268 | 268 |
ValueVector _supply; |
| 269 | 269 |
|
| 270 | 270 |
ValueVector _res_cap; |
| 271 | 271 |
LargeCostVector _cost; |
| 272 | 272 |
LargeCostVector _pi; |
| 273 | 273 |
ValueVector _excess; |
| 274 | 274 |
IntVector _next_out; |
| 275 | 275 |
std::deque<int> _active_nodes; |
| 276 | 276 |
|
| 277 | 277 |
// Data for scaling |
| 278 | 278 |
LargeCost _epsilon; |
| 279 | 279 |
int _alpha; |
| 280 | 280 |
|
| 281 | 281 |
IntVector _buckets; |
| 282 | 282 |
IntVector _bucket_next; |
| 283 | 283 |
IntVector _bucket_prev; |
| 284 | 284 |
IntVector _rank; |
| 285 | 285 |
int _max_rank; |
| 286 | 286 |
|
| 287 | 287 |
// Data for a StaticDigraph structure |
| 288 | 288 |
typedef std::pair<int, int> IntPair; |
| 289 | 289 |
StaticDigraph _sgr; |
| 290 | 290 |
std::vector<IntPair> _arc_vec; |
| 291 | 291 |
std::vector<LargeCost> _cost_vec; |
| 292 | 292 |
LargeCostArcMap _cost_map; |
| 293 | 293 |
LargeCostNodeMap _pi_map; |
| 294 | 294 |
|
| 295 | 295 |
public: |
| 296 | 296 |
|
| 297 | 297 |
/// \brief Constant for infinite upper bounds (capacities). |
| 298 | 298 |
/// |
| 299 | 299 |
/// Constant for infinite upper bounds (capacities). |
| 300 | 300 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 301 | 301 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 302 | 302 |
const Value INF; |
| 303 | 303 |
|
| 304 | 304 |
public: |
| 305 | 305 |
|
| 306 | 306 |
/// \name Named Template Parameters |
| 307 | 307 |
/// @{
|
| 308 | 308 |
|
| 309 | 309 |
template <typename T> |
| 310 | 310 |
struct SetLargeCostTraits : public Traits {
|
| 311 | 311 |
typedef T LargeCost; |
| 312 | 312 |
}; |
| 313 | 313 |
|
| 314 | 314 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 315 | 315 |
/// \c LargeCost type. |
| 316 | 316 |
/// |
| 317 | 317 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
| 318 | 318 |
/// type, which is used for internal computations in the algorithm. |
| 319 | 319 |
/// \c Cost must be convertible to \c LargeCost. |
| 320 | 320 |
template <typename T> |
| 321 | 321 |
struct SetLargeCost |
| 322 | 322 |
: public CostScaling<GR, V, C, SetLargeCostTraits<T> > {
|
| 323 | 323 |
typedef CostScaling<GR, V, C, SetLargeCostTraits<T> > Create; |
| 324 | 324 |
}; |
| 325 | 325 |
|
| 326 | 326 |
/// @} |
| 327 | 327 |
|
| 328 |
protected: |
|
| 329 |
|
|
| 330 |
CostScaling() {}
|
|
| 331 |
|
|
| 328 | 332 |
public: |
| 329 | 333 |
|
| 330 | 334 |
/// \brief Constructor. |
| 331 | 335 |
/// |
| 332 | 336 |
/// The constructor of the class. |
| 333 | 337 |
/// |
| 334 | 338 |
/// \param graph The digraph the algorithm runs on. |
| 335 | 339 |
CostScaling(const GR& graph) : |
| 336 | 340 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 337 | 341 |
_cost_map(_cost_vec), _pi_map(_pi), |
| 338 | 342 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 339 | 343 |
std::numeric_limits<Value>::infinity() : |
| 340 | 344 |
std::numeric_limits<Value>::max()) |
| 341 | 345 |
{
|
| 342 | 346 |
// Check the number types |
| 343 | 347 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 344 | 348 |
"The flow type of CostScaling must be signed"); |
| 345 | 349 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 346 | 350 |
"The cost type of CostScaling must be signed"); |
| 347 | 351 |
|
| 348 | 352 |
// Reset data structures |
| 349 | 353 |
reset(); |
| 350 | 354 |
} |
| 351 | 355 |
|
| 352 | 356 |
/// \name Parameters |
| 353 | 357 |
/// The parameters of the algorithm can be specified using these |
| 354 | 358 |
/// functions. |
| 355 | 359 |
|
| 356 | 360 |
/// @{
|
| 357 | 361 |
|
| 358 | 362 |
/// \brief Set the lower bounds on the arcs. |
| 359 | 363 |
/// |
| 360 | 364 |
/// This function sets the lower bounds on the arcs. |
| 361 | 365 |
/// If it is not used before calling \ref run(), the lower bounds |
| 362 | 366 |
/// will be set to zero on all arcs. |
| 363 | 367 |
/// |
| 364 | 368 |
/// \param map An arc map storing the lower bounds. |
| 365 | 369 |
/// Its \c Value type must be convertible to the \c Value type |
| 366 | 370 |
/// of the algorithm. |
| 367 | 371 |
/// |
| 368 | 372 |
/// \return <tt>(*this)</tt> |
| 369 | 373 |
template <typename LowerMap> |
| 370 | 374 |
CostScaling& lowerMap(const LowerMap& map) {
|
| 371 | 375 |
_have_lower = true; |
| 372 | 376 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 373 | 377 |
_lower[_arc_idf[a]] = map[a]; |
| 374 | 378 |
_lower[_arc_idb[a]] = map[a]; |
| 375 | 379 |
} |
| 376 | 380 |
return *this; |
| 377 | 381 |
} |
| 378 | 382 |
|
| 379 | 383 |
/// \brief Set the upper bounds (capacities) on the arcs. |
| 380 | 384 |
/// |
| 381 | 385 |
/// This function sets the upper bounds (capacities) on the arcs. |
| 382 | 386 |
/// If it is not used before calling \ref run(), the upper bounds |
| 383 | 387 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
| 384 | 388 |
/// unbounded from above). |
| 385 | 389 |
/// |
| 386 | 390 |
/// \param map An arc map storing the upper bounds. |
| 387 | 391 |
/// Its \c Value type must be convertible to the \c Value type |
| 388 | 392 |
/// of the algorithm. |
| 389 | 393 |
/// |
| 390 | 394 |
/// \return <tt>(*this)</tt> |
| 391 | 395 |
template<typename UpperMap> |
| 392 | 396 |
CostScaling& upperMap(const UpperMap& map) {
|
| 393 | 397 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 394 | 398 |
_upper[_arc_idf[a]] = map[a]; |
| 395 | 399 |
} |
| 396 | 400 |
return *this; |
| 397 | 401 |
} |
| 398 | 402 |
|
| 399 | 403 |
/// \brief Set the costs of the arcs. |
| 400 | 404 |
/// |
| 401 | 405 |
/// This function sets the costs of the arcs. |
| 402 | 406 |
/// If it is not used before calling \ref run(), the costs |
| 403 | 407 |
/// will be set to \c 1 on all arcs. |
| 404 | 408 |
/// |
| 405 | 409 |
/// \param map An arc map storing the costs. |
| 406 | 410 |
/// Its \c Value type must be convertible to the \c Cost type |
| 407 | 411 |
/// of the algorithm. |
| 408 | 412 |
/// |
| 409 | 413 |
/// \return <tt>(*this)</tt> |
| 410 | 414 |
template<typename CostMap> |
| 411 | 415 |
CostScaling& costMap(const CostMap& map) {
|
| 412 | 416 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 413 | 417 |
_scost[_arc_idf[a]] = map[a]; |
| 414 | 418 |
_scost[_arc_idb[a]] = -map[a]; |
| 415 | 419 |
} |
| 416 | 420 |
return *this; |
| 417 | 421 |
} |
| 418 | 422 |
|
| 419 | 423 |
/// \brief Set the supply values of the nodes. |
| 420 | 424 |
/// |
| 421 | 425 |
/// This function sets the supply values of the nodes. |
| 422 | 426 |
/// If neither this function nor \ref stSupply() is used before |
| 423 | 427 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 424 | 428 |
/// |
| 425 | 429 |
/// \param map A node map storing the supply values. |
| 426 | 430 |
/// Its \c Value type must be convertible to the \c Value type |
| 427 | 431 |
/// of the algorithm. |
| 428 | 432 |
/// |
| 429 | 433 |
/// \return <tt>(*this)</tt> |
| 430 | 434 |
template<typename SupplyMap> |
| 431 | 435 |
CostScaling& supplyMap(const SupplyMap& map) {
|
| 432 | 436 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 433 | 437 |
_supply[_node_id[n]] = map[n]; |
| 434 | 438 |
} |
| 435 | 439 |
return *this; |
| 436 | 440 |
} |
| 437 | 441 |
|
| 438 | 442 |
/// \brief Set single source and target nodes and a supply value. |
| 439 | 443 |
/// |
| 440 | 444 |
/// This function sets a single source node and a single target node |
| 441 | 445 |
/// and the required flow value. |
| 442 | 446 |
/// If neither this function nor \ref supplyMap() is used before |
| 443 | 447 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 444 | 448 |
/// |
| 445 | 449 |
/// Using this function has the same effect as using \ref supplyMap() |
| 446 | 450 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 447 | 451 |
/// assigned to \c t and all other nodes have zero supply value. |
| 448 | 452 |
/// |
| 449 | 453 |
/// \param s The source node. |
| 450 | 454 |
/// \param t The target node. |
| 451 | 455 |
/// \param k The required amount of flow from node \c s to node \c t |
| 452 | 456 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 453 | 457 |
/// |
| 454 | 458 |
/// \return <tt>(*this)</tt> |
| 455 | 459 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 456 | 460 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 457 | 461 |
_supply[i] = 0; |
| 458 | 462 |
} |
| 459 | 463 |
_supply[_node_id[s]] = k; |
| 460 | 464 |
_supply[_node_id[t]] = -k; |
| 461 | 465 |
return *this; |
| 462 | 466 |
} |
| 463 | 467 |
|
| 464 | 468 |
/// @} |
| 465 | 469 |
|
| 466 | 470 |
/// \name Execution control |
| 467 | 471 |
/// The algorithm can be executed using \ref run(). |
| 468 | 472 |
|
| 469 | 473 |
/// @{
|
| 470 | 474 |
|
| 471 | 475 |
/// \brief Run the algorithm. |
| 472 | 476 |
/// |
| 473 | 477 |
/// This function runs the algorithm. |
| 474 | 478 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 475 | 479 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 476 | 480 |
/// For example, |
| 477 | 481 |
/// \code |
| 478 | 482 |
/// CostScaling<ListDigraph> cs(graph); |
| 479 | 483 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 480 | 484 |
/// .supplyMap(sup).run(); |
| 481 | 485 |
/// \endcode |
| 482 | 486 |
/// |
| 483 | 487 |
/// This function can be called more than once. All the given parameters |
| 484 | 488 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 485 | 489 |
/// is used, thus only the modified parameters have to be set again. |
| 486 | 490 |
/// If the underlying digraph was also modified after the construction |
| 487 | 491 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 488 | 492 |
/// function must be called. |
| 489 | 493 |
/// |
| 490 | 494 |
/// \param method The internal method that will be used in the |
| 491 | 495 |
/// algorithm. For more information, see \ref Method. |
| 492 | 496 |
/// \param factor The cost scaling factor. It must be larger than one. |
| 493 | 497 |
/// |
| 494 | 498 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 495 | 499 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 496 | 500 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 497 | 501 |
/// optimal flow and node potentials (primal and dual solutions), |
| 498 | 502 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 499 | 503 |
/// and infinite upper bound. It means that the objective function |
| 500 | 504 |
/// is unbounded on that arc, however, note that it could actually be |
| 501 | 505 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 502 | 506 |
/// these cases. |
| 503 | 507 |
/// |
| 504 | 508 |
/// \see ProblemType, Method |
| 505 | 509 |
/// \see resetParams(), reset() |
| 506 | 510 |
ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) {
|
| 507 | 511 |
_alpha = factor; |
| 508 | 512 |
ProblemType pt = init(); |
| 509 | 513 |
if (pt != OPTIMAL) return pt; |
| 510 | 514 |
start(method); |
| 511 | 515 |
return OPTIMAL; |
| 512 | 516 |
} |
| 513 | 517 |
|
| 514 | 518 |
/// \brief Reset all the parameters that have been given before. |
| 515 | 519 |
/// |
| 516 | 520 |
/// This function resets all the paramaters that have been given |
| 517 | 521 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 518 | 522 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 519 | 523 |
/// |
| 520 | 524 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
| 521 | 525 |
/// parameters are kept for the next \ref run() call, unless |
| 522 | 526 |
/// \ref resetParams() or \ref reset() is used. |
| 523 | 527 |
/// If the underlying digraph was also modified after the construction |
| 524 | 528 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 525 | 529 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 526 | 530 |
/// |
| 527 | 531 |
/// For example, |
| 528 | 532 |
/// \code |
| 529 | 533 |
/// CostScaling<ListDigraph> cs(graph); |
| 530 | 534 |
/// |
| 531 | 535 |
/// // First run |
| 532 | 536 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 533 | 537 |
/// .supplyMap(sup).run(); |
| 534 | 538 |
/// |
| 535 | 539 |
/// // Run again with modified cost map (resetParams() is not called, |
| 536 | 540 |
/// // so only the cost map have to be set again) |
| 537 | 541 |
/// cost[e] += 100; |
| 538 | 542 |
/// cs.costMap(cost).run(); |
| 539 | 543 |
/// |
| 540 | 544 |
/// // Run again from scratch using resetParams() |
| 541 | 545 |
/// // (the lower bounds will be set to zero on all arcs) |
| 542 | 546 |
/// cs.resetParams(); |
| 543 | 547 |
/// cs.upperMap(capacity).costMap(cost) |
| 544 | 548 |
/// .supplyMap(sup).run(); |
| 545 | 549 |
/// \endcode |
| 546 | 550 |
/// |
| 547 | 551 |
/// \return <tt>(*this)</tt> |
| 548 | 552 |
/// |
| 549 | 553 |
/// \see reset(), run() |
| 550 | 554 |
CostScaling& resetParams() {
|
| 551 | 555 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 552 | 556 |
_supply[i] = 0; |
| 553 | 557 |
} |
| 554 | 558 |
int limit = _first_out[_root]; |
| 555 | 559 |
for (int j = 0; j != limit; ++j) {
|
| 556 | 560 |
_lower[j] = 0; |
| 557 | 561 |
_upper[j] = INF; |
| 558 | 562 |
_scost[j] = _forward[j] ? 1 : -1; |
| 559 | 563 |
} |
| 560 | 564 |
for (int j = limit; j != _res_arc_num; ++j) {
|
| 561 | 565 |
_lower[j] = 0; |
| 562 | 566 |
_upper[j] = INF; |
| 563 | 567 |
_scost[j] = 0; |
| 564 | 568 |
_scost[_reverse[j]] = 0; |
| 565 | 569 |
} |
| 566 | 570 |
_have_lower = false; |
| 567 | 571 |
return *this; |
| 568 | 572 |
} |
| 569 | 573 |
|
| 570 | 574 |
/// \brief Reset all the parameters that have been given before. |
| 571 | 575 |
/// |
| 572 | 576 |
/// This function resets all the paramaters that have been given |
| 573 | 577 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 574 | 578 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 575 | 579 |
/// |
| 576 | 580 |
/// It is useful for multiple run() calls. If this function is not |
| 577 | 581 |
/// used, all the parameters given before are kept for the next |
| 578 | 582 |
/// \ref run() call. |
| 579 | 583 |
/// However, the underlying digraph must not be modified after this |
| 580 | 584 |
/// class have been constructed, since it copies and extends the graph. |
| 581 | 585 |
/// \return <tt>(*this)</tt> |
| 582 | 586 |
CostScaling& reset() {
|
| 583 | 587 |
// Resize vectors |
| 584 | 588 |
_node_num = countNodes(_graph); |
| 585 | 589 |
_arc_num = countArcs(_graph); |
| 586 | 590 |
_res_node_num = _node_num + 1; |
| 587 | 591 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 588 | 592 |
_root = _node_num; |
| 589 | 593 |
|
| 590 | 594 |
_first_out.resize(_res_node_num + 1); |
| 591 | 595 |
_forward.resize(_res_arc_num); |
| 592 | 596 |
_source.resize(_res_arc_num); |
| 593 | 597 |
_target.resize(_res_arc_num); |
| 594 | 598 |
_reverse.resize(_res_arc_num); |
| 595 | 599 |
|
| 596 | 600 |
_lower.resize(_res_arc_num); |
| 597 | 601 |
_upper.resize(_res_arc_num); |
| 598 | 602 |
_scost.resize(_res_arc_num); |
| 599 | 603 |
_supply.resize(_res_node_num); |
| 600 | 604 |
|
| 601 | 605 |
_res_cap.resize(_res_arc_num); |
| 602 | 606 |
_cost.resize(_res_arc_num); |
| 603 | 607 |
_pi.resize(_res_node_num); |
| 604 | 608 |
_excess.resize(_res_node_num); |
| 605 | 609 |
_next_out.resize(_res_node_num); |
| 606 | 610 |
|
| 607 | 611 |
_arc_vec.reserve(_res_arc_num); |
| 608 | 612 |
_cost_vec.reserve(_res_arc_num); |
| 609 | 613 |
|
| 610 | 614 |
// Copy the graph |
| 611 | 615 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num; |
| 612 | 616 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 613 | 617 |
_node_id[n] = i; |
| 614 | 618 |
} |
| 615 | 619 |
i = 0; |
| 616 | 620 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 617 | 621 |
_first_out[i] = j; |
| 618 | 622 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 619 | 623 |
_arc_idf[a] = j; |
| 620 | 624 |
_forward[j] = true; |
| 621 | 625 |
_source[j] = i; |
| 622 | 626 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 623 | 627 |
} |
| 624 | 628 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 625 | 629 |
_arc_idb[a] = j; |
| 626 | 630 |
_forward[j] = false; |
| 627 | 631 |
_source[j] = i; |
| 628 | 632 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 629 | 633 |
} |
| 630 | 634 |
_forward[j] = false; |
| 631 | 635 |
_source[j] = i; |
| 632 | 636 |
_target[j] = _root; |
| 633 | 637 |
_reverse[j] = k; |
| 634 | 638 |
_forward[k] = true; |
| 635 | 639 |
_source[k] = _root; |
| 636 | 640 |
_target[k] = i; |
| 637 | 641 |
_reverse[k] = j; |
| 638 | 642 |
++j; ++k; |
| 639 | 643 |
} |
| 640 | 644 |
_first_out[i] = j; |
| 641 | 645 |
_first_out[_res_node_num] = k; |
| 642 | 646 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 643 | 647 |
int fi = _arc_idf[a]; |
| 644 | 648 |
int bi = _arc_idb[a]; |
| 645 | 649 |
_reverse[fi] = bi; |
| 646 | 650 |
_reverse[bi] = fi; |
| 647 | 651 |
} |
| 648 | 652 |
|
| 649 | 653 |
// Reset parameters |
| 650 | 654 |
resetParams(); |
| 651 | 655 |
return *this; |
| 652 | 656 |
} |
| 653 | 657 |
|
| 654 | 658 |
/// @} |
| 655 | 659 |
|
| 656 | 660 |
/// \name Query Functions |
| 657 | 661 |
/// The results of the algorithm can be obtained using these |
| 658 | 662 |
/// functions.\n |
| 659 | 663 |
/// The \ref run() function must be called before using them. |
| 660 | 664 |
|
| 661 | 665 |
/// @{
|
| 662 | 666 |
|
| 663 | 667 |
/// \brief Return the total cost of the found flow. |
| 664 | 668 |
/// |
| 665 | 669 |
/// This function returns the total cost of the found flow. |
| 666 | 670 |
/// Its complexity is O(e). |
| 667 | 671 |
/// |
| 668 | 672 |
/// \note The return type of the function can be specified as a |
| 669 | 673 |
/// template parameter. For example, |
| 670 | 674 |
/// \code |
| 671 | 675 |
/// cs.totalCost<double>(); |
| 672 | 676 |
/// \endcode |
| 673 | 677 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 674 | 678 |
/// type of the algorithm, which is the default return type of the |
| 675 | 679 |
/// function. |
| 676 | 680 |
/// |
| 677 | 681 |
/// \pre \ref run() must be called before using this function. |
| 678 | 682 |
template <typename Number> |
| 679 | 683 |
Number totalCost() const {
|
| 680 | 684 |
Number c = 0; |
| 681 | 685 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 682 | 686 |
int i = _arc_idb[a]; |
| 683 | 687 |
c += static_cast<Number>(_res_cap[i]) * |
| 684 | 688 |
(-static_cast<Number>(_scost[i])); |
| 685 | 689 |
} |
| 686 | 690 |
return c; |
| 687 | 691 |
} |
| 688 | 692 |
|
| 689 | 693 |
#ifndef DOXYGEN |
| 690 | 694 |
Cost totalCost() const {
|
| 691 | 695 |
return totalCost<Cost>(); |
| 692 | 696 |
} |
| 693 | 697 |
#endif |
| 694 | 698 |
|
| 695 | 699 |
/// \brief Return the flow on the given arc. |
| 696 | 700 |
/// |
| 697 | 701 |
/// This function returns the flow on the given arc. |
| 698 | 702 |
/// |
| 699 | 703 |
/// \pre \ref run() must be called before using this function. |
| 700 | 704 |
Value flow(const Arc& a) const {
|
| 701 | 705 |
return _res_cap[_arc_idb[a]]; |
| 702 | 706 |
} |
| 703 | 707 |
|
| 704 | 708 |
/// \brief Return the flow map (the primal solution). |
| 705 | 709 |
/// |
| 706 | 710 |
/// This function copies the flow value on each arc into the given |
| 707 | 711 |
/// map. The \c Value type of the algorithm must be convertible to |
| 708 | 712 |
/// the \c Value type of the map. |
| 709 | 713 |
/// |
| 710 | 714 |
/// \pre \ref run() must be called before using this function. |
| 711 | 715 |
template <typename FlowMap> |
| 712 | 716 |
void flowMap(FlowMap &map) const {
|
| 713 | 717 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 714 | 718 |
map.set(a, _res_cap[_arc_idb[a]]); |
| 715 | 719 |
} |
| 716 | 720 |
} |
| 717 | 721 |
|
| 718 | 722 |
/// \brief Return the potential (dual value) of the given node. |
| 719 | 723 |
/// |
| 720 | 724 |
/// This function returns the potential (dual value) of the |
| 721 | 725 |
/// given node. |
| 722 | 726 |
/// |
| 723 | 727 |
/// \pre \ref run() must be called before using this function. |
| 724 | 728 |
Cost potential(const Node& n) const {
|
| 725 | 729 |
return static_cast<Cost>(_pi[_node_id[n]]); |
| 726 | 730 |
} |
| 727 | 731 |
|
| 728 | 732 |
/// \brief Return the potential map (the dual solution). |
| 729 | 733 |
/// |
| 730 | 734 |
/// This function copies the potential (dual value) of each node |
| 731 | 735 |
/// into the given map. |
| 732 | 736 |
/// The \c Cost type of the algorithm must be convertible to the |
| 733 | 737 |
/// \c Value type of the map. |
| 734 | 738 |
/// |
| 735 | 739 |
/// \pre \ref run() must be called before using this function. |
| 736 | 740 |
template <typename PotentialMap> |
| 737 | 741 |
void potentialMap(PotentialMap &map) const {
|
| 738 | 742 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 739 | 743 |
map.set(n, static_cast<Cost>(_pi[_node_id[n]])); |
| 740 | 744 |
} |
| 741 | 745 |
} |
| 742 | 746 |
|
| 743 | 747 |
/// @} |
| 744 | 748 |
|
| 745 | 749 |
private: |
| 746 | 750 |
|
| 747 | 751 |
// Initialize the algorithm |
| 748 | 752 |
ProblemType init() {
|
| 749 | 753 |
if (_res_node_num <= 1) return INFEASIBLE; |
| 750 | 754 |
|
| 751 | 755 |
// Check the sum of supply values |
| 752 | 756 |
_sum_supply = 0; |
| 753 | 757 |
for (int i = 0; i != _root; ++i) {
|
| 754 | 758 |
_sum_supply += _supply[i]; |
| 755 | 759 |
} |
| 756 | 760 |
if (_sum_supply > 0) return INFEASIBLE; |
| 757 | 761 |
|
| 758 | 762 |
|
| 759 | 763 |
// Initialize vectors |
| 760 | 764 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 761 | 765 |
_pi[i] = 0; |
| 762 | 766 |
_excess[i] = _supply[i]; |
| 763 | 767 |
} |
| 764 | 768 |
|
| 765 | 769 |
// Remove infinite upper bounds and check negative arcs |
| 766 | 770 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 767 | 771 |
int last_out; |
| 768 | 772 |
if (_have_lower) {
|
| 769 | 773 |
for (int i = 0; i != _root; ++i) {
|
| 770 | 774 |
last_out = _first_out[i+1]; |
| 771 | 775 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 772 | 776 |
if (_forward[j]) {
|
| 773 | 777 |
Value c = _scost[j] < 0 ? _upper[j] : _lower[j]; |
| 774 | 778 |
if (c >= MAX) return UNBOUNDED; |
| 775 | 779 |
_excess[i] -= c; |
| 776 | 780 |
_excess[_target[j]] += c; |
| 777 | 781 |
} |
| 778 | 782 |
} |
| 779 | 783 |
} |
| 780 | 784 |
} else {
|
| 781 | 785 |
for (int i = 0; i != _root; ++i) {
|
| 782 | 786 |
last_out = _first_out[i+1]; |
| 783 | 787 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 784 | 788 |
if (_forward[j] && _scost[j] < 0) {
|
| 785 | 789 |
Value c = _upper[j]; |
| 786 | 790 |
if (c >= MAX) return UNBOUNDED; |
| 787 | 791 |
_excess[i] -= c; |
| 788 | 792 |
_excess[_target[j]] += c; |
| 789 | 793 |
} |
| 790 | 794 |
} |
| 791 | 795 |
} |
| 792 | 796 |
} |
| 793 | 797 |
Value ex, max_cap = 0; |
| 794 | 798 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 795 | 799 |
ex = _excess[i]; |
| 796 | 800 |
_excess[i] = 0; |
| 797 | 801 |
if (ex < 0) max_cap -= ex; |
| 798 | 802 |
} |
| 799 | 803 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 800 | 804 |
if (_upper[j] >= MAX) _upper[j] = max_cap; |
| 801 | 805 |
} |
| 802 | 806 |
|
| 803 | 807 |
// Initialize the large cost vector and the epsilon parameter |
| 804 | 808 |
_epsilon = 0; |
| 805 | 809 |
LargeCost lc; |
| 806 | 810 |
for (int i = 0; i != _root; ++i) {
|
| 807 | 811 |
last_out = _first_out[i+1]; |
| 808 | 812 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 809 | 813 |
lc = static_cast<LargeCost>(_scost[j]) * _res_node_num * _alpha; |
| 810 | 814 |
_cost[j] = lc; |
| 811 | 815 |
if (lc > _epsilon) _epsilon = lc; |
| 812 | 816 |
} |
| 813 | 817 |
} |
| 814 | 818 |
_epsilon /= _alpha; |
| 815 | 819 |
|
| 816 | 820 |
// Initialize maps for Circulation and remove non-zero lower bounds |
| 817 | 821 |
ConstMap<Arc, Value> low(0); |
| 818 | 822 |
typedef typename Digraph::template ArcMap<Value> ValueArcMap; |
| 819 | 823 |
typedef typename Digraph::template NodeMap<Value> ValueNodeMap; |
| 820 | 824 |
ValueArcMap cap(_graph), flow(_graph); |
| 821 | 825 |
ValueNodeMap sup(_graph); |
| 822 | 826 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 823 | 827 |
sup[n] = _supply[_node_id[n]]; |
| 824 | 828 |
} |
| 825 | 829 |
if (_have_lower) {
|
| 826 | 830 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 827 | 831 |
int j = _arc_idf[a]; |
| 828 | 832 |
Value c = _lower[j]; |
| 829 | 833 |
cap[a] = _upper[j] - c; |
| 830 | 834 |
sup[_graph.source(a)] -= c; |
| 831 | 835 |
sup[_graph.target(a)] += c; |
| 832 | 836 |
} |
| 833 | 837 |
} else {
|
| 834 | 838 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 835 | 839 |
cap[a] = _upper[_arc_idf[a]]; |
| 836 | 840 |
} |
| 837 | 841 |
} |
| 838 | 842 |
|
| 839 | 843 |
_sup_node_num = 0; |
| 840 | 844 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 841 | 845 |
if (sup[n] > 0) ++_sup_node_num; |
| 842 | 846 |
} |
| 843 | 847 |
|
| 844 | 848 |
// Find a feasible flow using Circulation |
| 845 | 849 |
Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap> |
| 846 | 850 |
circ(_graph, low, cap, sup); |
| 847 | 851 |
if (!circ.flowMap(flow).run()) return INFEASIBLE; |
| 848 | 852 |
|
| 849 | 853 |
// Set residual capacities and handle GEQ supply type |
| 850 | 854 |
if (_sum_supply < 0) {
|
| 851 | 855 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 852 | 856 |
Value fa = flow[a]; |
| 853 | 857 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 854 | 858 |
_res_cap[_arc_idb[a]] = fa; |
| 855 | 859 |
sup[_graph.source(a)] -= fa; |
| 856 | 860 |
sup[_graph.target(a)] += fa; |
| 857 | 861 |
} |
| 858 | 862 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 859 | 863 |
_excess[_node_id[n]] = sup[n]; |
| 860 | 864 |
} |
| 861 | 865 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 862 | 866 |
int u = _target[a]; |
| 863 | 867 |
int ra = _reverse[a]; |
| 864 | 868 |
_res_cap[a] = -_sum_supply + 1; |
| 865 | 869 |
_res_cap[ra] = -_excess[u]; |
| 866 | 870 |
_cost[a] = 0; |
| 867 | 871 |
_cost[ra] = 0; |
| 868 | 872 |
_excess[u] = 0; |
| 869 | 873 |
} |
| 870 | 874 |
} else {
|
| 871 | 875 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 872 | 876 |
Value fa = flow[a]; |
| 873 | 877 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 874 | 878 |
_res_cap[_arc_idb[a]] = fa; |
| 875 | 879 |
} |
| 876 | 880 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 877 | 881 |
int ra = _reverse[a]; |
| 878 | 882 |
_res_cap[a] = 0; |
| 879 | 883 |
_res_cap[ra] = 0; |
| 880 | 884 |
_cost[a] = 0; |
| 881 | 885 |
_cost[ra] = 0; |
| 882 | 886 |
} |
| 883 | 887 |
} |
| 884 | 888 |
|
| 885 | 889 |
return OPTIMAL; |
| 886 | 890 |
} |
| 887 | 891 |
|
| 888 | 892 |
// Execute the algorithm and transform the results |
| 889 | 893 |
void start(Method method) {
|
| 890 | 894 |
// Maximum path length for partial augment |
| 891 | 895 |
const int MAX_PATH_LENGTH = 4; |
| 892 | 896 |
|
| 893 | 897 |
// Initialize data structures for buckets |
| 894 | 898 |
_max_rank = _alpha * _res_node_num; |
| 895 | 899 |
_buckets.resize(_max_rank); |
| 896 | 900 |
_bucket_next.resize(_res_node_num + 1); |
| 897 | 901 |
_bucket_prev.resize(_res_node_num + 1); |
| 898 | 902 |
_rank.resize(_res_node_num + 1); |
| 899 | 903 |
|
| 900 | 904 |
// Execute the algorithm |
| 901 | 905 |
switch (method) {
|
| 902 | 906 |
case PUSH: |
| 903 | 907 |
startPush(); |
| 904 | 908 |
break; |
| 905 | 909 |
case AUGMENT: |
| 906 | 910 |
startAugment(); |
| 907 | 911 |
break; |
| 908 | 912 |
case PARTIAL_AUGMENT: |
| 909 | 913 |
startAugment(MAX_PATH_LENGTH); |
| 910 | 914 |
break; |
| 911 | 915 |
} |
| 912 | 916 |
|
| 913 | 917 |
// Compute node potentials for the original costs |
| 914 | 918 |
_arc_vec.clear(); |
| 915 | 919 |
_cost_vec.clear(); |
| 916 | 920 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 917 | 921 |
if (_res_cap[j] > 0) {
|
| 918 | 922 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 919 | 923 |
_cost_vec.push_back(_scost[j]); |
| 920 | 924 |
} |
| 921 | 925 |
} |
| 922 | 926 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 923 | 927 |
|
| 924 | 928 |
typename BellmanFord<StaticDigraph, LargeCostArcMap> |
| 925 | 929 |
::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map); |
| 926 | 930 |
bf.distMap(_pi_map); |
| 927 | 931 |
bf.init(0); |
| 928 | 932 |
bf.start(); |
| 929 | 933 |
|
| 930 | 934 |
// Handle non-zero lower bounds |
| 931 | 935 |
if (_have_lower) {
|
| 932 | 936 |
int limit = _first_out[_root]; |
| 933 | 937 |
for (int j = 0; j != limit; ++j) {
|
| 934 | 938 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 935 | 939 |
} |
| 936 | 940 |
} |
| 937 | 941 |
} |
| 938 | 942 |
|
| 939 | 943 |
// Initialize a cost scaling phase |
| 940 | 944 |
void initPhase() {
|
| 941 | 945 |
// Saturate arcs not satisfying the optimality condition |
| 942 | 946 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 943 | 947 |
int last_out = _first_out[u+1]; |
| 944 | 948 |
LargeCost pi_u = _pi[u]; |
| 945 | 949 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 946 | 950 |
int v = _target[a]; |
| 947 | 951 |
if (_res_cap[a] > 0 && _cost[a] + pi_u - _pi[v] < 0) {
|
| 948 | 952 |
Value delta = _res_cap[a]; |
| 949 | 953 |
_excess[u] -= delta; |
| 950 | 954 |
_excess[v] += delta; |
| 951 | 955 |
_res_cap[a] = 0; |
| 952 | 956 |
_res_cap[_reverse[a]] += delta; |
| 953 | 957 |
} |
| 954 | 958 |
} |
| 955 | 959 |
} |
| 956 | 960 |
|
| 957 | 961 |
// Find active nodes (i.e. nodes with positive excess) |
| 958 | 962 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 959 | 963 |
if (_excess[u] > 0) _active_nodes.push_back(u); |
| 960 | 964 |
} |
| 961 | 965 |
|
| 962 | 966 |
// Initialize the next arcs |
| 963 | 967 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 964 | 968 |
_next_out[u] = _first_out[u]; |
| 965 | 969 |
} |
| 966 | 970 |
} |
| 967 | 971 |
|
| 968 | 972 |
// Early termination heuristic |
| 969 | 973 |
bool earlyTermination() {
|
| 970 | 974 |
const double EARLY_TERM_FACTOR = 3.0; |
| 971 | 975 |
|
| 972 | 976 |
// Build a static residual graph |
| 973 | 977 |
_arc_vec.clear(); |
| 974 | 978 |
_cost_vec.clear(); |
| 975 | 979 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 976 | 980 |
if (_res_cap[j] > 0) {
|
| 977 | 981 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 978 | 982 |
_cost_vec.push_back(_cost[j] + 1); |
| 979 | 983 |
} |
| 980 | 984 |
} |
| 981 | 985 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 982 | 986 |
|
| 983 | 987 |
// Run Bellman-Ford algorithm to check if the current flow is optimal |
| 984 | 988 |
BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
| 985 | 989 |
bf.init(0); |
| 986 | 990 |
bool done = false; |
| 987 | 991 |
int K = int(EARLY_TERM_FACTOR * std::sqrt(double(_res_node_num))); |
| 988 | 992 |
for (int i = 0; i < K && !done; ++i) {
|
| 989 | 993 |
done = bf.processNextWeakRound(); |
| 990 | 994 |
} |
| 991 | 995 |
return done; |
| 992 | 996 |
} |
| 993 | 997 |
|
| 994 | 998 |
// Global potential update heuristic |
| 995 | 999 |
void globalUpdate() {
|
| 996 | 1000 |
int bucket_end = _root + 1; |
| 997 | 1001 |
|
| 998 | 1002 |
// Initialize buckets |
| 999 | 1003 |
for (int r = 0; r != _max_rank; ++r) {
|
| 1000 | 1004 |
_buckets[r] = bucket_end; |
| 1001 | 1005 |
} |
| 1002 | 1006 |
Value total_excess = 0; |
| 1003 | 1007 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 1004 | 1008 |
if (_excess[i] < 0) {
|
| 1005 | 1009 |
_rank[i] = 0; |
| 1006 | 1010 |
_bucket_next[i] = _buckets[0]; |
| 1007 | 1011 |
_bucket_prev[_buckets[0]] = i; |
| 1008 | 1012 |
_buckets[0] = i; |
| 1009 | 1013 |
} else {
|
| 1010 | 1014 |
total_excess += _excess[i]; |
| 1011 | 1015 |
_rank[i] = _max_rank; |
| 1012 | 1016 |
} |
| 1013 | 1017 |
} |
| 1014 | 1018 |
if (total_excess == 0) return; |
| 1015 | 1019 |
|
| 1016 | 1020 |
// Search the buckets |
| 1017 | 1021 |
int r = 0; |
| 1018 | 1022 |
for ( ; r != _max_rank; ++r) {
|
| 1019 | 1023 |
while (_buckets[r] != bucket_end) {
|
| 1020 | 1024 |
// Remove the first node from the current bucket |
| 1021 | 1025 |
int u = _buckets[r]; |
| 1022 | 1026 |
_buckets[r] = _bucket_next[u]; |
| 1023 | 1027 |
|
| 1024 | 1028 |
// Search the incomming arcs of u |
| 1025 | 1029 |
LargeCost pi_u = _pi[u]; |
| 1026 | 1030 |
int last_out = _first_out[u+1]; |
| 1027 | 1031 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 1028 | 1032 |
int ra = _reverse[a]; |
| 1029 | 1033 |
if (_res_cap[ra] > 0) {
|
| 1030 | 1034 |
int v = _source[ra]; |
| 1031 | 1035 |
int old_rank_v = _rank[v]; |
| 1032 | 1036 |
if (r < old_rank_v) {
|
| 1033 | 1037 |
// Compute the new rank of v |
| 1034 | 1038 |
LargeCost nrc = (_cost[ra] + _pi[v] - pi_u) / _epsilon; |
| 1035 | 1039 |
int new_rank_v = old_rank_v; |
| 1036 | 1040 |
if (nrc < LargeCost(_max_rank)) |
| 1037 | 1041 |
new_rank_v = r + 1 + int(nrc); |
| 1038 | 1042 |
|
| 1039 | 1043 |
// Change the rank of v |
| 1040 | 1044 |
if (new_rank_v < old_rank_v) {
|
| 1041 | 1045 |
_rank[v] = new_rank_v; |
| 1042 | 1046 |
_next_out[v] = _first_out[v]; |
| 1043 | 1047 |
|
| 1044 | 1048 |
// Remove v from its old bucket |
| 1045 | 1049 |
if (old_rank_v < _max_rank) {
|
| 1046 | 1050 |
if (_buckets[old_rank_v] == v) {
|
| 1047 | 1051 |
_buckets[old_rank_v] = _bucket_next[v]; |
| 1048 | 1052 |
} else {
|
| 1049 | 1053 |
_bucket_next[_bucket_prev[v]] = _bucket_next[v]; |
| 1050 | 1054 |
_bucket_prev[_bucket_next[v]] = _bucket_prev[v]; |
| 1051 | 1055 |
} |
| 1052 | 1056 |
} |
| 1053 | 1057 |
|
| 1054 | 1058 |
// Insert v to its new bucket |
| 1055 | 1059 |
_bucket_next[v] = _buckets[new_rank_v]; |
| 1056 | 1060 |
_bucket_prev[_buckets[new_rank_v]] = v; |
| 1057 | 1061 |
_buckets[new_rank_v] = v; |
| 1058 | 1062 |
} |
| 1059 | 1063 |
} |
| 1060 | 1064 |
} |
| 1061 | 1065 |
} |
| 1062 | 1066 |
|
| 1063 | 1067 |
// Finish search if there are no more active nodes |
| 1064 | 1068 |
if (_excess[u] > 0) {
|
| 1065 | 1069 |
total_excess -= _excess[u]; |
| 1066 | 1070 |
if (total_excess <= 0) break; |
| 1067 | 1071 |
} |
| 1068 | 1072 |
} |
| 1069 | 1073 |
if (total_excess <= 0) break; |
| 1070 | 1074 |
} |
| 1071 | 1075 |
|
| 1072 | 1076 |
// Relabel nodes |
| 1073 | 1077 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 1074 | 1078 |
int k = std::min(_rank[u], r); |
| 1075 | 1079 |
if (k > 0) {
|
| 1076 | 1080 |
_pi[u] -= _epsilon * k; |
| 1077 | 1081 |
_next_out[u] = _first_out[u]; |
| 1078 | 1082 |
} |
| 1079 | 1083 |
} |
| 1080 | 1084 |
} |
| 1081 | 1085 |
|
| 1082 | 1086 |
/// Execute the algorithm performing augment and relabel operations |
| 1083 | 1087 |
void startAugment(int max_length = std::numeric_limits<int>::max()) {
|
| 1084 | 1088 |
// Paramters for heuristics |
| 1085 | 1089 |
const int EARLY_TERM_EPSILON_LIMIT = 1000; |
| 1086 | 1090 |
const double GLOBAL_UPDATE_FACTOR = 3.0; |
| 1087 | 1091 |
|
| 1088 | 1092 |
const int global_update_freq = int(GLOBAL_UPDATE_FACTOR * |
| 1089 | 1093 |
(_res_node_num + _sup_node_num * _sup_node_num)); |
| 1090 | 1094 |
int next_update_limit = global_update_freq; |
| 1091 | 1095 |
|
| 1092 | 1096 |
int relabel_cnt = 0; |
| 1093 | 1097 |
|
| 1094 | 1098 |
// Perform cost scaling phases |
| 1095 | 1099 |
std::vector<int> path; |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 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_HARTMANN_ORLIN_H |
| 20 | 20 |
#define LEMON_HARTMANN_ORLIN_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of HartmannOrlin algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of HartmannOrlin algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::Rea_data "Rea_data" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HartmannOrlinDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 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 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 | 109 |
/// \tparam TR The traits class that defines various types used by the |
| 110 | 110 |
/// algorithm. By default, it is \ref HartmannOrlinDefaultTraits |
| 111 | 111 |
/// "HartmannOrlinDefaultTraits<GR, LEN>". |
| 112 | 112 |
/// In most cases, this parameter should not be set directly, |
| 113 | 113 |
/// consider to use the named template parameters instead. |
| 114 | 114 |
#ifdef DOXYGEN |
| 115 | 115 |
template <typename GR, typename LEN, typename TR> |
| 116 | 116 |
#else |
| 117 | 117 |
template < typename GR, |
| 118 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
| 119 | 119 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
| 120 | 120 |
#endif |
| 121 | 121 |
class HartmannOrlin |
| 122 | 122 |
{
|
| 123 | 123 |
public: |
| 124 | 124 |
|
| 125 | 125 |
/// The type of the digraph |
| 126 | 126 |
typedef typename TR::Digraph Digraph; |
| 127 | 127 |
/// The type of the length map |
| 128 | 128 |
typedef typename TR::LengthMap LengthMap; |
| 129 | 129 |
/// The type of the arc lengths |
| 130 | 130 |
typedef typename TR::Value Value; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief The large value type |
| 133 | 133 |
/// |
| 134 | 134 |
/// The large value type used for internal computations. |
| 135 | 135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
| 136 | 136 |
/// otherwise it is \c double. |
| 137 | 137 |
typedef typename TR::LargeValue LargeValue; |
| 138 | 138 |
|
| 139 | 139 |
/// The tolerance type |
| 140 | 140 |
typedef typename TR::Tolerance Tolerance; |
| 141 | 141 |
|
| 142 | 142 |
/// \brief The path type of the found cycles |
| 143 | 143 |
/// |
| 144 | 144 |
/// The path type of the found cycles. |
| 145 | 145 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 146 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 147 | 147 |
typedef typename TR::Path Path; |
| 148 | 148 |
|
| 149 | 149 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
| 150 | 150 |
typedef TR Traits; |
| 151 | 151 |
|
| 152 | 152 |
private: |
| 153 | 153 |
|
| 154 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 155 | 155 |
|
| 156 | 156 |
// Data sturcture for path data |
| 157 | 157 |
struct PathData |
| 158 | 158 |
{
|
| 159 | 159 |
LargeValue dist; |
| 160 | 160 |
Arc pred; |
| 161 | 161 |
PathData(LargeValue d, Arc p = INVALID) : |
| 162 | 162 |
dist(d), pred(p) {}
|
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 166 | 166 |
PathDataNodeMap; |
| 167 | 167 |
|
| 168 | 168 |
private: |
| 169 | 169 |
|
| 170 | 170 |
// The digraph the algorithm runs on |
| 171 | 171 |
const Digraph &_gr; |
| 172 | 172 |
// The length of the arcs |
| 173 | 173 |
const LengthMap &_length; |
| 174 | 174 |
|
| 175 | 175 |
// Data for storing the strongly connected components |
| 176 | 176 |
int _comp_num; |
| 177 | 177 |
typename Digraph::template NodeMap<int> _comp; |
| 178 | 178 |
std::vector<std::vector<Node> > _comp_nodes; |
| 179 | 179 |
std::vector<Node>* _nodes; |
| 180 | 180 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 181 | 181 |
|
| 182 | 182 |
// Data for the found cycles |
| 183 | 183 |
bool _curr_found, _best_found; |
| 184 | 184 |
LargeValue _curr_length, _best_length; |
| 185 | 185 |
int _curr_size, _best_size; |
| 186 | 186 |
Node _curr_node, _best_node; |
| 187 | 187 |
int _curr_level, _best_level; |
| 188 | 188 |
|
| 189 | 189 |
Path *_cycle_path; |
| 190 | 190 |
bool _local_path; |
| 191 | 191 |
|
| 192 | 192 |
// Node map for storing path data |
| 193 | 193 |
PathDataNodeMap _data; |
| 194 | 194 |
// The processed nodes in the last round |
| 195 | 195 |
std::vector<Node> _process; |
| 196 | 196 |
|
| 197 | 197 |
Tolerance _tolerance; |
| 198 | 198 |
|
| 199 | 199 |
// Infinite constant |
| 200 | 200 |
const LargeValue INF; |
| 201 | 201 |
|
| 202 | 202 |
public: |
| 203 | 203 |
|
| 204 | 204 |
/// \name Named Template Parameters |
| 205 | 205 |
/// @{
|
| 206 | 206 |
|
| 207 | 207 |
template <typename T> |
| 208 | 208 |
struct SetLargeValueTraits : public Traits {
|
| 209 | 209 |
typedef T LargeValue; |
| 210 | 210 |
typedef lemon::Tolerance<T> Tolerance; |
| 211 | 211 |
}; |
| 212 | 212 |
|
| 213 | 213 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 214 | 214 |
/// \c LargeValue type. |
| 215 | 215 |
/// |
| 216 | 216 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 217 | 217 |
/// type. It is used for internal computations in the algorithm. |
| 218 | 218 |
template <typename T> |
| 219 | 219 |
struct SetLargeValue |
| 220 | 220 |
: public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
|
| 221 | 221 |
typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create; |
| 222 | 222 |
}; |
| 223 | 223 |
|
| 224 | 224 |
template <typename T> |
| 225 | 225 |
struct SetPathTraits : public Traits {
|
| 226 | 226 |
typedef T Path; |
| 227 | 227 |
}; |
| 228 | 228 |
|
| 229 | 229 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 230 | 230 |
/// \c %Path type. |
| 231 | 231 |
/// |
| 232 | 232 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 233 | 233 |
/// type of the found cycles. |
| 234 | 234 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 235 | 235 |
/// and it must have an \c addFront() function. |
| 236 | 236 |
template <typename T> |
| 237 | 237 |
struct SetPath |
| 238 | 238 |
: public HartmannOrlin<GR, LEN, SetPathTraits<T> > {
|
| 239 | 239 |
typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create; |
| 240 | 240 |
}; |
| 241 | 241 |
|
| 242 | 242 |
/// @} |
| 243 | 243 |
|
| 244 |
protected: |
|
| 245 |
|
|
| 246 |
HartmannOrlin() {}
|
|
| 247 |
|
|
| 244 | 248 |
public: |
| 245 | 249 |
|
| 246 | 250 |
/// \brief Constructor. |
| 247 | 251 |
/// |
| 248 | 252 |
/// The constructor of the class. |
| 249 | 253 |
/// |
| 250 | 254 |
/// \param digraph The digraph the algorithm runs on. |
| 251 | 255 |
/// \param length The lengths (costs) of the arcs. |
| 252 | 256 |
HartmannOrlin( const Digraph &digraph, |
| 253 | 257 |
const LengthMap &length ) : |
| 254 | 258 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
| 255 | 259 |
_best_found(false), _best_length(0), _best_size(1), |
| 256 | 260 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
| 257 | 261 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 258 | 262 |
std::numeric_limits<LargeValue>::infinity() : |
| 259 | 263 |
std::numeric_limits<LargeValue>::max()) |
| 260 | 264 |
{}
|
| 261 | 265 |
|
| 262 | 266 |
/// Destructor. |
| 263 | 267 |
~HartmannOrlin() {
|
| 264 | 268 |
if (_local_path) delete _cycle_path; |
| 265 | 269 |
} |
| 266 | 270 |
|
| 267 | 271 |
/// \brief Set the path structure for storing the found cycle. |
| 268 | 272 |
/// |
| 269 | 273 |
/// This function sets an external path structure for storing the |
| 270 | 274 |
/// found cycle. |
| 271 | 275 |
/// |
| 272 | 276 |
/// If you don't call this function before calling \ref run() or |
| 273 | 277 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 274 | 278 |
/// structure. The destuctor deallocates this automatically |
| 275 | 279 |
/// allocated object, of course. |
| 276 | 280 |
/// |
| 277 | 281 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
| 278 | 282 |
/// "addFront()" function of the given path structure. |
| 279 | 283 |
/// |
| 280 | 284 |
/// \return <tt>(*this)</tt> |
| 281 | 285 |
HartmannOrlin& cycle(Path &path) {
|
| 282 | 286 |
if (_local_path) {
|
| 283 | 287 |
delete _cycle_path; |
| 284 | 288 |
_local_path = false; |
| 285 | 289 |
} |
| 286 | 290 |
_cycle_path = &path; |
| 287 | 291 |
return *this; |
| 288 | 292 |
} |
| 289 | 293 |
|
| 290 | 294 |
/// \brief Set the tolerance used by the algorithm. |
| 291 | 295 |
/// |
| 292 | 296 |
/// This function sets the tolerance object used by the algorithm. |
| 293 | 297 |
/// |
| 294 | 298 |
/// \return <tt>(*this)</tt> |
| 295 | 299 |
HartmannOrlin& tolerance(const Tolerance& tolerance) {
|
| 296 | 300 |
_tolerance = tolerance; |
| 297 | 301 |
return *this; |
| 298 | 302 |
} |
| 299 | 303 |
|
| 300 | 304 |
/// \brief Return a const reference to the tolerance. |
| 301 | 305 |
/// |
| 302 | 306 |
/// This function returns a const reference to the tolerance object |
| 303 | 307 |
/// used by the algorithm. |
| 304 | 308 |
const Tolerance& tolerance() const {
|
| 305 | 309 |
return _tolerance; |
| 306 | 310 |
} |
| 307 | 311 |
|
| 308 | 312 |
/// \name Execution control |
| 309 | 313 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 310 | 314 |
/// function.\n |
| 311 | 315 |
/// If you only need the minimum mean length, you may call |
| 312 | 316 |
/// \ref findMinMean(). |
| 313 | 317 |
|
| 314 | 318 |
/// @{
|
| 315 | 319 |
|
| 316 | 320 |
/// \brief Run the algorithm. |
| 317 | 321 |
/// |
| 318 | 322 |
/// This function runs the algorithm. |
| 319 | 323 |
/// It can be called more than once (e.g. if the underlying digraph |
| 320 | 324 |
/// and/or the arc lengths have been modified). |
| 321 | 325 |
/// |
| 322 | 326 |
/// \return \c true if a directed cycle exists in the digraph. |
| 323 | 327 |
/// |
| 324 | 328 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 325 | 329 |
/// \code |
| 326 | 330 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 327 | 331 |
/// \endcode |
| 328 | 332 |
bool run() {
|
| 329 | 333 |
return findMinMean() && findCycle(); |
| 330 | 334 |
} |
| 331 | 335 |
|
| 332 | 336 |
/// \brief Find the minimum cycle mean. |
| 333 | 337 |
/// |
| 334 | 338 |
/// This function finds the minimum mean length of the directed |
| 335 | 339 |
/// cycles in the digraph. |
| 336 | 340 |
/// |
| 337 | 341 |
/// \return \c true if a directed cycle exists in the digraph. |
| 338 | 342 |
bool findMinMean() {
|
| 339 | 343 |
// Initialization and find strongly connected components |
| 340 | 344 |
init(); |
| 341 | 345 |
findComponents(); |
| 342 | 346 |
|
| 343 | 347 |
// Find the minimum cycle mean in the components |
| 344 | 348 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 345 | 349 |
if (!initComponent(comp)) continue; |
| 346 | 350 |
processRounds(); |
| 347 | 351 |
|
| 348 | 352 |
// Update the best cycle (global minimum mean cycle) |
| 349 | 353 |
if ( _curr_found && (!_best_found || |
| 350 | 354 |
_curr_length * _best_size < _best_length * _curr_size) ) {
|
| 351 | 355 |
_best_found = true; |
| 352 | 356 |
_best_length = _curr_length; |
| 353 | 357 |
_best_size = _curr_size; |
| 354 | 358 |
_best_node = _curr_node; |
| 355 | 359 |
_best_level = _curr_level; |
| 356 | 360 |
} |
| 357 | 361 |
} |
| 358 | 362 |
return _best_found; |
| 359 | 363 |
} |
| 360 | 364 |
|
| 361 | 365 |
/// \brief Find a minimum mean directed cycle. |
| 362 | 366 |
/// |
| 363 | 367 |
/// This function finds a directed cycle of minimum mean length |
| 364 | 368 |
/// in the digraph using the data computed by findMinMean(). |
| 365 | 369 |
/// |
| 366 | 370 |
/// \return \c true if a directed cycle exists in the digraph. |
| 367 | 371 |
/// |
| 368 | 372 |
/// \pre \ref findMinMean() must be called before using this function. |
| 369 | 373 |
bool findCycle() {
|
| 370 | 374 |
if (!_best_found) return false; |
| 371 | 375 |
IntNodeMap reached(_gr, -1); |
| 372 | 376 |
int r = _best_level + 1; |
| 373 | 377 |
Node u = _best_node; |
| 374 | 378 |
while (reached[u] < 0) {
|
| 375 | 379 |
reached[u] = --r; |
| 376 | 380 |
u = _gr.source(_data[u][r].pred); |
| 377 | 381 |
} |
| 378 | 382 |
r = reached[u]; |
| 379 | 383 |
Arc e = _data[u][r].pred; |
| 380 | 384 |
_cycle_path->addFront(e); |
| 381 | 385 |
_best_length = _length[e]; |
| 382 | 386 |
_best_size = 1; |
| 383 | 387 |
Node v; |
| 384 | 388 |
while ((v = _gr.source(e)) != u) {
|
| 385 | 389 |
e = _data[v][--r].pred; |
| 386 | 390 |
_cycle_path->addFront(e); |
| 387 | 391 |
_best_length += _length[e]; |
| 388 | 392 |
++_best_size; |
| 389 | 393 |
} |
| 390 | 394 |
return true; |
| 391 | 395 |
} |
| 392 | 396 |
|
| 393 | 397 |
/// @} |
| 394 | 398 |
|
| 395 | 399 |
/// \name Query Functions |
| 396 | 400 |
/// The results of the algorithm can be obtained using these |
| 397 | 401 |
/// functions.\n |
| 398 | 402 |
/// The algorithm should be executed before using them. |
| 399 | 403 |
|
| 400 | 404 |
/// @{
|
| 401 | 405 |
|
| 402 | 406 |
/// \brief Return the total length of the found cycle. |
| 403 | 407 |
/// |
| 404 | 408 |
/// This function returns the total length of the found cycle. |
| 405 | 409 |
/// |
| 406 | 410 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 407 | 411 |
/// using this function. |
| 408 | 412 |
Value cycleLength() const {
|
| 409 | 413 |
return static_cast<Value>(_best_length); |
| 410 | 414 |
} |
| 411 | 415 |
|
| 412 | 416 |
/// \brief Return the number of arcs on the found cycle. |
| 413 | 417 |
/// |
| 414 | 418 |
/// This function returns the number of arcs on the found cycle. |
| 415 | 419 |
/// |
| 416 | 420 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 417 | 421 |
/// using this function. |
| 418 | 422 |
int cycleArcNum() const {
|
| 419 | 423 |
return _best_size; |
| 420 | 424 |
} |
| 421 | 425 |
|
| 422 | 426 |
/// \brief Return the mean length of the found cycle. |
| 423 | 427 |
/// |
| 424 | 428 |
/// This function returns the mean length of the found cycle. |
| 425 | 429 |
/// |
| 426 | 430 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
| 427 | 431 |
/// following code. |
| 428 | 432 |
/// \code |
| 429 | 433 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum(); |
| 430 | 434 |
/// \endcode |
| 431 | 435 |
/// |
| 432 | 436 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 433 | 437 |
/// using this function. |
| 434 | 438 |
double cycleMean() const {
|
| 435 | 439 |
return static_cast<double>(_best_length) / _best_size; |
| 436 | 440 |
} |
| 437 | 441 |
|
| 438 | 442 |
/// \brief Return the found cycle. |
| 439 | 443 |
/// |
| 440 | 444 |
/// This function returns a const reference to the path structure |
| 441 | 445 |
/// storing the found cycle. |
| 442 | 446 |
/// |
| 443 | 447 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 444 | 448 |
/// this function. |
| 445 | 449 |
const Path& cycle() const {
|
| 446 | 450 |
return *_cycle_path; |
| 447 | 451 |
} |
| 448 | 452 |
|
| 449 | 453 |
///@} |
| 450 | 454 |
|
| 451 | 455 |
private: |
| 452 | 456 |
|
| 453 | 457 |
// Initialization |
| 454 | 458 |
void init() {
|
| 455 | 459 |
if (!_cycle_path) {
|
| 456 | 460 |
_local_path = true; |
| 457 | 461 |
_cycle_path = new Path; |
| 458 | 462 |
} |
| 459 | 463 |
_cycle_path->clear(); |
| 460 | 464 |
_best_found = false; |
| 461 | 465 |
_best_length = 0; |
| 462 | 466 |
_best_size = 1; |
| 463 | 467 |
_cycle_path->clear(); |
| 464 | 468 |
for (NodeIt u(_gr); u != INVALID; ++u) |
| 465 | 469 |
_data[u].clear(); |
| 466 | 470 |
} |
| 467 | 471 |
|
| 468 | 472 |
// Find strongly connected components and initialize _comp_nodes |
| 469 | 473 |
// and _out_arcs |
| 470 | 474 |
void findComponents() {
|
| 471 | 475 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 472 | 476 |
_comp_nodes.resize(_comp_num); |
| 473 | 477 |
if (_comp_num == 1) {
|
| 474 | 478 |
_comp_nodes[0].clear(); |
| 475 | 479 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 476 | 480 |
_comp_nodes[0].push_back(n); |
| 477 | 481 |
_out_arcs[n].clear(); |
| 478 | 482 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 479 | 483 |
_out_arcs[n].push_back(a); |
| 480 | 484 |
} |
| 481 | 485 |
} |
| 482 | 486 |
} else {
|
| 483 | 487 |
for (int i = 0; i < _comp_num; ++i) |
| 484 | 488 |
_comp_nodes[i].clear(); |
| 485 | 489 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 486 | 490 |
int k = _comp[n]; |
| 487 | 491 |
_comp_nodes[k].push_back(n); |
| 488 | 492 |
_out_arcs[n].clear(); |
| 489 | 493 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 490 | 494 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
| 491 | 495 |
} |
| 492 | 496 |
} |
| 493 | 497 |
} |
| 494 | 498 |
} |
| 495 | 499 |
|
| 496 | 500 |
// Initialize path data for the current component |
| 497 | 501 |
bool initComponent(int comp) {
|
| 498 | 502 |
_nodes = &(_comp_nodes[comp]); |
| 499 | 503 |
int n = _nodes->size(); |
| 500 | 504 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
| 501 | 505 |
return false; |
| 502 | 506 |
} |
| 503 | 507 |
for (int i = 0; i < n; ++i) {
|
| 504 | 508 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
| 505 | 509 |
} |
| 506 | 510 |
return true; |
| 507 | 511 |
} |
| 508 | 512 |
|
| 509 | 513 |
// Process all rounds of computing path data for the current component. |
| 510 | 514 |
// _data[v][k] is the length of a shortest directed walk from the root |
| 511 | 515 |
// node to node v containing exactly k arcs. |
| 512 | 516 |
void processRounds() {
|
| 513 | 517 |
Node start = (*_nodes)[0]; |
| 514 | 518 |
_data[start][0] = PathData(0); |
| 515 | 519 |
_process.clear(); |
| 516 | 520 |
_process.push_back(start); |
| 517 | 521 |
|
| 518 | 522 |
int k, n = _nodes->size(); |
| 519 | 523 |
int next_check = 4; |
| 520 | 524 |
bool terminate = false; |
| 521 | 525 |
for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) {
|
| 522 | 526 |
processNextBuildRound(k); |
| 523 | 527 |
if (k == next_check || k == n) {
|
| 524 | 528 |
terminate = checkTermination(k); |
| 525 | 529 |
next_check = next_check * 3 / 2; |
| 526 | 530 |
} |
| 527 | 531 |
} |
| 528 | 532 |
for ( ; k <= n && !terminate; ++k) {
|
| 529 | 533 |
processNextFullRound(k); |
| 530 | 534 |
if (k == next_check || k == n) {
|
| 531 | 535 |
terminate = checkTermination(k); |
| 532 | 536 |
next_check = next_check * 3 / 2; |
| 533 | 537 |
} |
| 534 | 538 |
} |
| 535 | 539 |
} |
| 536 | 540 |
|
| 537 | 541 |
// Process one round and rebuild _process |
| 538 | 542 |
void processNextBuildRound(int k) {
|
| 539 | 543 |
std::vector<Node> next; |
| 540 | 544 |
Node u, v; |
| 541 | 545 |
Arc e; |
| 542 | 546 |
LargeValue d; |
| 543 | 547 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 544 | 548 |
u = _process[i]; |
| 545 | 549 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
| 546 | 550 |
e = _out_arcs[u][j]; |
| 547 | 551 |
v = _gr.target(e); |
| 548 | 552 |
d = _data[u][k-1].dist + _length[e]; |
| 549 | 553 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
| 550 | 554 |
if (_data[v][k].dist == INF) next.push_back(v); |
| 551 | 555 |
_data[v][k] = PathData(d, e); |
| 552 | 556 |
} |
| 553 | 557 |
} |
| 554 | 558 |
} |
| 555 | 559 |
_process.swap(next); |
| 556 | 560 |
} |
| 557 | 561 |
|
| 558 | 562 |
// Process one round using _nodes instead of _process |
| 559 | 563 |
void processNextFullRound(int k) {
|
| 560 | 564 |
Node u, v; |
| 561 | 565 |
Arc e; |
| 562 | 566 |
LargeValue d; |
| 563 | 567 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 564 | 568 |
u = (*_nodes)[i]; |
| 565 | 569 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
| 566 | 570 |
e = _out_arcs[u][j]; |
| 567 | 571 |
v = _gr.target(e); |
| 568 | 572 |
d = _data[u][k-1].dist + _length[e]; |
| 569 | 573 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
| 570 | 574 |
_data[v][k] = PathData(d, e); |
| 571 | 575 |
} |
| 572 | 576 |
} |
| 573 | 577 |
} |
| 574 | 578 |
} |
| 575 | 579 |
|
| 576 | 580 |
// Check early termination |
| 577 | 581 |
bool checkTermination(int k) {
|
| 578 | 582 |
typedef std::pair<int, int> Pair; |
| 579 | 583 |
typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0)); |
| 580 | 584 |
typename GR::template NodeMap<LargeValue> pi(_gr); |
| 581 | 585 |
int n = _nodes->size(); |
| 582 | 586 |
LargeValue length; |
| 583 | 587 |
int size; |
| 584 | 588 |
Node u; |
| 585 | 589 |
|
| 586 | 590 |
// Search for cycles that are already found |
| 587 | 591 |
_curr_found = false; |
| 588 | 592 |
for (int i = 0; i < n; ++i) {
|
| 589 | 593 |
u = (*_nodes)[i]; |
| 590 | 594 |
if (_data[u][k].dist == INF) continue; |
| 591 | 595 |
for (int j = k; j >= 0; --j) {
|
| 592 | 596 |
if (level[u].first == i && level[u].second > 0) {
|
| 593 | 597 |
// A cycle is found |
| 594 | 598 |
length = _data[u][level[u].second].dist - _data[u][j].dist; |
| 595 | 599 |
size = level[u].second - j; |
| 596 | 600 |
if (!_curr_found || length * _curr_size < _curr_length * size) {
|
| 597 | 601 |
_curr_length = length; |
| 598 | 602 |
_curr_size = size; |
| 599 | 603 |
_curr_node = u; |
| 600 | 604 |
_curr_level = level[u].second; |
| 601 | 605 |
_curr_found = true; |
| 602 | 606 |
} |
| 603 | 607 |
} |
| 604 | 608 |
level[u] = Pair(i, j); |
| 605 | 609 |
if (j != 0) {
|
| 606 | 610 |
u = _gr.source(_data[u][j].pred); |
| 607 | 611 |
} |
| 608 | 612 |
} |
| 609 | 613 |
} |
| 610 | 614 |
|
| 611 | 615 |
// If at least one cycle is found, check the optimality condition |
| 612 | 616 |
LargeValue d; |
| 613 | 617 |
if (_curr_found && k < n) {
|
| 614 | 618 |
// Find node potentials |
| 615 | 619 |
for (int i = 0; i < n; ++i) {
|
| 616 | 620 |
u = (*_nodes)[i]; |
| 617 | 621 |
pi[u] = INF; |
| 618 | 622 |
for (int j = 0; j <= k; ++j) {
|
| 619 | 623 |
if (_data[u][j].dist < INF) {
|
| 620 | 624 |
d = _data[u][j].dist * _curr_size - j * _curr_length; |
| 621 | 625 |
if (_tolerance.less(d, pi[u])) pi[u] = d; |
| 622 | 626 |
} |
| 623 | 627 |
} |
| 624 | 628 |
} |
| 625 | 629 |
|
| 626 | 630 |
// Check the optimality condition for all arcs |
| 627 | 631 |
bool done = true; |
| 628 | 632 |
for (ArcIt a(_gr); a != INVALID; ++a) {
|
| 629 | 633 |
if (_tolerance.less(_length[a] * _curr_size - _curr_length, |
| 630 | 634 |
pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
|
| 631 | 635 |
done = false; |
| 632 | 636 |
break; |
| 633 | 637 |
} |
| 634 | 638 |
} |
| 635 | 639 |
return done; |
| 636 | 640 |
} |
| 637 | 641 |
return (k == n); |
| 638 | 642 |
} |
| 639 | 643 |
|
| 640 | 644 |
}; //class HartmannOrlin |
| 641 | 645 |
|
| 642 | 646 |
///@} |
| 643 | 647 |
|
| 644 | 648 |
} //namespace lemon |
| 645 | 649 |
|
| 646 | 650 |
#endif //LEMON_HARTMANN_ORLIN_H |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 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_HOWARD_H |
| 20 | 20 |
#define LEMON_HOWARD_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Howard's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Howard class. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Howard class. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HowardDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 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 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 | 109 |
/// \tparam TR The traits class that defines various types used by the |
| 110 | 110 |
/// algorithm. By default, it is \ref HowardDefaultTraits |
| 111 | 111 |
/// "HowardDefaultTraits<GR, LEN>". |
| 112 | 112 |
/// In most cases, this parameter should not be set directly, |
| 113 | 113 |
/// consider to use the named template parameters instead. |
| 114 | 114 |
#ifdef DOXYGEN |
| 115 | 115 |
template <typename GR, typename LEN, typename TR> |
| 116 | 116 |
#else |
| 117 | 117 |
template < typename GR, |
| 118 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
| 119 | 119 |
typename TR = HowardDefaultTraits<GR, LEN> > |
| 120 | 120 |
#endif |
| 121 | 121 |
class Howard |
| 122 | 122 |
{
|
| 123 | 123 |
public: |
| 124 | 124 |
|
| 125 | 125 |
/// The type of the digraph |
| 126 | 126 |
typedef typename TR::Digraph Digraph; |
| 127 | 127 |
/// The type of the length map |
| 128 | 128 |
typedef typename TR::LengthMap LengthMap; |
| 129 | 129 |
/// The type of the arc lengths |
| 130 | 130 |
typedef typename TR::Value Value; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief The large value type |
| 133 | 133 |
/// |
| 134 | 134 |
/// The large value type used for internal computations. |
| 135 | 135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
| 136 | 136 |
/// otherwise it is \c double. |
| 137 | 137 |
typedef typename TR::LargeValue LargeValue; |
| 138 | 138 |
|
| 139 | 139 |
/// The tolerance type |
| 140 | 140 |
typedef typename TR::Tolerance Tolerance; |
| 141 | 141 |
|
| 142 | 142 |
/// \brief The path type of the found cycles |
| 143 | 143 |
/// |
| 144 | 144 |
/// The path type of the found cycles. |
| 145 | 145 |
/// Using the \ref HowardDefaultTraits "default traits class", |
| 146 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 147 | 147 |
typedef typename TR::Path Path; |
| 148 | 148 |
|
| 149 | 149 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
| 150 | 150 |
typedef TR Traits; |
| 151 | 151 |
|
| 152 | 152 |
private: |
| 153 | 153 |
|
| 154 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 155 | 155 |
|
| 156 | 156 |
// The digraph the algorithm runs on |
| 157 | 157 |
const Digraph &_gr; |
| 158 | 158 |
// The length of the arcs |
| 159 | 159 |
const LengthMap &_length; |
| 160 | 160 |
|
| 161 | 161 |
// Data for the found cycles |
| 162 | 162 |
bool _curr_found, _best_found; |
| 163 | 163 |
LargeValue _curr_length, _best_length; |
| 164 | 164 |
int _curr_size, _best_size; |
| 165 | 165 |
Node _curr_node, _best_node; |
| 166 | 166 |
|
| 167 | 167 |
Path *_cycle_path; |
| 168 | 168 |
bool _local_path; |
| 169 | 169 |
|
| 170 | 170 |
// Internal data used by the algorithm |
| 171 | 171 |
typename Digraph::template NodeMap<Arc> _policy; |
| 172 | 172 |
typename Digraph::template NodeMap<bool> _reached; |
| 173 | 173 |
typename Digraph::template NodeMap<int> _level; |
| 174 | 174 |
typename Digraph::template NodeMap<LargeValue> _dist; |
| 175 | 175 |
|
| 176 | 176 |
// Data for storing the strongly connected components |
| 177 | 177 |
int _comp_num; |
| 178 | 178 |
typename Digraph::template NodeMap<int> _comp; |
| 179 | 179 |
std::vector<std::vector<Node> > _comp_nodes; |
| 180 | 180 |
std::vector<Node>* _nodes; |
| 181 | 181 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
| 182 | 182 |
|
| 183 | 183 |
// Queue used for BFS search |
| 184 | 184 |
std::vector<Node> _queue; |
| 185 | 185 |
int _qfront, _qback; |
| 186 | 186 |
|
| 187 | 187 |
Tolerance _tolerance; |
| 188 | 188 |
|
| 189 | 189 |
// Infinite constant |
| 190 | 190 |
const LargeValue INF; |
| 191 | 191 |
|
| 192 | 192 |
public: |
| 193 | 193 |
|
| 194 | 194 |
/// \name Named Template Parameters |
| 195 | 195 |
/// @{
|
| 196 | 196 |
|
| 197 | 197 |
template <typename T> |
| 198 | 198 |
struct SetLargeValueTraits : public Traits {
|
| 199 | 199 |
typedef T LargeValue; |
| 200 | 200 |
typedef lemon::Tolerance<T> Tolerance; |
| 201 | 201 |
}; |
| 202 | 202 |
|
| 203 | 203 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 204 | 204 |
/// \c LargeValue type. |
| 205 | 205 |
/// |
| 206 | 206 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 207 | 207 |
/// type. It is used for internal computations in the algorithm. |
| 208 | 208 |
template <typename T> |
| 209 | 209 |
struct SetLargeValue |
| 210 | 210 |
: public Howard<GR, LEN, SetLargeValueTraits<T> > {
|
| 211 | 211 |
typedef Howard<GR, LEN, SetLargeValueTraits<T> > Create; |
| 212 | 212 |
}; |
| 213 | 213 |
|
| 214 | 214 |
template <typename T> |
| 215 | 215 |
struct SetPathTraits : public Traits {
|
| 216 | 216 |
typedef T Path; |
| 217 | 217 |
}; |
| 218 | 218 |
|
| 219 | 219 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 220 | 220 |
/// \c %Path type. |
| 221 | 221 |
/// |
| 222 | 222 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 223 | 223 |
/// type of the found cycles. |
| 224 | 224 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 225 | 225 |
/// and it must have an \c addBack() function. |
| 226 | 226 |
template <typename T> |
| 227 | 227 |
struct SetPath |
| 228 | 228 |
: public Howard<GR, LEN, SetPathTraits<T> > {
|
| 229 | 229 |
typedef Howard<GR, LEN, SetPathTraits<T> > Create; |
| 230 | 230 |
}; |
| 231 | 231 |
|
| 232 | 232 |
/// @} |
| 233 | 233 |
|
| 234 |
protected: |
|
| 235 |
|
|
| 236 |
Howard() {}
|
|
| 237 |
|
|
| 234 | 238 |
public: |
| 235 | 239 |
|
| 236 | 240 |
/// \brief Constructor. |
| 237 | 241 |
/// |
| 238 | 242 |
/// The constructor of the class. |
| 239 | 243 |
/// |
| 240 | 244 |
/// \param digraph The digraph the algorithm runs on. |
| 241 | 245 |
/// \param length The lengths (costs) of the arcs. |
| 242 | 246 |
Howard( const Digraph &digraph, |
| 243 | 247 |
const LengthMap &length ) : |
| 244 | 248 |
_gr(digraph), _length(length), _best_found(false), |
| 245 | 249 |
_best_length(0), _best_size(1), _cycle_path(NULL), _local_path(false), |
| 246 | 250 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph), |
| 247 | 251 |
_comp(digraph), _in_arcs(digraph), |
| 248 | 252 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 249 | 253 |
std::numeric_limits<LargeValue>::infinity() : |
| 250 | 254 |
std::numeric_limits<LargeValue>::max()) |
| 251 | 255 |
{}
|
| 252 | 256 |
|
| 253 | 257 |
/// Destructor. |
| 254 | 258 |
~Howard() {
|
| 255 | 259 |
if (_local_path) delete _cycle_path; |
| 256 | 260 |
} |
| 257 | 261 |
|
| 258 | 262 |
/// \brief Set the path structure for storing the found cycle. |
| 259 | 263 |
/// |
| 260 | 264 |
/// This function sets an external path structure for storing the |
| 261 | 265 |
/// found cycle. |
| 262 | 266 |
/// |
| 263 | 267 |
/// If you don't call this function before calling \ref run() or |
| 264 | 268 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 265 | 269 |
/// structure. The destuctor deallocates this automatically |
| 266 | 270 |
/// allocated object, of course. |
| 267 | 271 |
/// |
| 268 | 272 |
/// \note The algorithm calls only the \ref lemon::Path::addBack() |
| 269 | 273 |
/// "addBack()" function of the given path structure. |
| 270 | 274 |
/// |
| 271 | 275 |
/// \return <tt>(*this)</tt> |
| 272 | 276 |
Howard& cycle(Path &path) {
|
| 273 | 277 |
if (_local_path) {
|
| 274 | 278 |
delete _cycle_path; |
| 275 | 279 |
_local_path = false; |
| 276 | 280 |
} |
| 277 | 281 |
_cycle_path = &path; |
| 278 | 282 |
return *this; |
| 279 | 283 |
} |
| 280 | 284 |
|
| 281 | 285 |
/// \brief Set the tolerance used by the algorithm. |
| 282 | 286 |
/// |
| 283 | 287 |
/// This function sets the tolerance object used by the algorithm. |
| 284 | 288 |
/// |
| 285 | 289 |
/// \return <tt>(*this)</tt> |
| 286 | 290 |
Howard& tolerance(const Tolerance& tolerance) {
|
| 287 | 291 |
_tolerance = tolerance; |
| 288 | 292 |
return *this; |
| 289 | 293 |
} |
| 290 | 294 |
|
| 291 | 295 |
/// \brief Return a const reference to the tolerance. |
| 292 | 296 |
/// |
| 293 | 297 |
/// This function returns a const reference to the tolerance object |
| 294 | 298 |
/// used by the algorithm. |
| 295 | 299 |
const Tolerance& tolerance() const {
|
| 296 | 300 |
return _tolerance; |
| 297 | 301 |
} |
| 298 | 302 |
|
| 299 | 303 |
/// \name Execution control |
| 300 | 304 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 301 | 305 |
/// function.\n |
| 302 | 306 |
/// If you only need the minimum mean length, you may call |
| 303 | 307 |
/// \ref findMinMean(). |
| 304 | 308 |
|
| 305 | 309 |
/// @{
|
| 306 | 310 |
|
| 307 | 311 |
/// \brief Run the algorithm. |
| 308 | 312 |
/// |
| 309 | 313 |
/// This function runs the algorithm. |
| 310 | 314 |
/// It can be called more than once (e.g. if the underlying digraph |
| 311 | 315 |
/// and/or the arc lengths have been modified). |
| 312 | 316 |
/// |
| 313 | 317 |
/// \return \c true if a directed cycle exists in the digraph. |
| 314 | 318 |
/// |
| 315 | 319 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 316 | 320 |
/// \code |
| 317 | 321 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 318 | 322 |
/// \endcode |
| 319 | 323 |
bool run() {
|
| 320 | 324 |
return findMinMean() && findCycle(); |
| 321 | 325 |
} |
| 322 | 326 |
|
| 323 | 327 |
/// \brief Find the minimum cycle mean. |
| 324 | 328 |
/// |
| 325 | 329 |
/// This function finds the minimum mean length of the directed |
| 326 | 330 |
/// cycles in the digraph. |
| 327 | 331 |
/// |
| 328 | 332 |
/// \return \c true if a directed cycle exists in the digraph. |
| 329 | 333 |
bool findMinMean() {
|
| 330 | 334 |
// Initialize and find strongly connected components |
| 331 | 335 |
init(); |
| 332 | 336 |
findComponents(); |
| 333 | 337 |
|
| 334 | 338 |
// Find the minimum cycle mean in the components |
| 335 | 339 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 336 | 340 |
// Find the minimum mean cycle in the current component |
| 337 | 341 |
if (!buildPolicyGraph(comp)) continue; |
| 338 | 342 |
while (true) {
|
| 339 | 343 |
findPolicyCycle(); |
| 340 | 344 |
if (!computeNodeDistances()) break; |
| 341 | 345 |
} |
| 342 | 346 |
// Update the best cycle (global minimum mean cycle) |
| 343 | 347 |
if ( _curr_found && (!_best_found || |
| 344 | 348 |
_curr_length * _best_size < _best_length * _curr_size) ) {
|
| 345 | 349 |
_best_found = true; |
| 346 | 350 |
_best_length = _curr_length; |
| 347 | 351 |
_best_size = _curr_size; |
| 348 | 352 |
_best_node = _curr_node; |
| 349 | 353 |
} |
| 350 | 354 |
} |
| 351 | 355 |
return _best_found; |
| 352 | 356 |
} |
| 353 | 357 |
|
| 354 | 358 |
/// \brief Find a minimum mean directed cycle. |
| 355 | 359 |
/// |
| 356 | 360 |
/// This function finds a directed cycle of minimum mean length |
| 357 | 361 |
/// in the digraph using the data computed by findMinMean(). |
| 358 | 362 |
/// |
| 359 | 363 |
/// \return \c true if a directed cycle exists in the digraph. |
| 360 | 364 |
/// |
| 361 | 365 |
/// \pre \ref findMinMean() must be called before using this function. |
| 362 | 366 |
bool findCycle() {
|
| 363 | 367 |
if (!_best_found) return false; |
| 364 | 368 |
_cycle_path->addBack(_policy[_best_node]); |
| 365 | 369 |
for ( Node v = _best_node; |
| 366 | 370 |
(v = _gr.target(_policy[v])) != _best_node; ) {
|
| 367 | 371 |
_cycle_path->addBack(_policy[v]); |
| 368 | 372 |
} |
| 369 | 373 |
return true; |
| 370 | 374 |
} |
| 371 | 375 |
|
| 372 | 376 |
/// @} |
| 373 | 377 |
|
| 374 | 378 |
/// \name Query Functions |
| 375 | 379 |
/// The results of the algorithm can be obtained using these |
| 376 | 380 |
/// functions.\n |
| 377 | 381 |
/// The algorithm should be executed before using them. |
| 378 | 382 |
|
| 379 | 383 |
/// @{
|
| 380 | 384 |
|
| 381 | 385 |
/// \brief Return the total length of the found cycle. |
| 382 | 386 |
/// |
| 383 | 387 |
/// This function returns the total length of the found cycle. |
| 384 | 388 |
/// |
| 385 | 389 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 386 | 390 |
/// using this function. |
| 387 | 391 |
Value cycleLength() const {
|
| 388 | 392 |
return static_cast<Value>(_best_length); |
| 389 | 393 |
} |
| 390 | 394 |
|
| 391 | 395 |
/// \brief Return the number of arcs on the found cycle. |
| 392 | 396 |
/// |
| 393 | 397 |
/// This function returns the number of arcs on the found cycle. |
| 394 | 398 |
/// |
| 395 | 399 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 396 | 400 |
/// using this function. |
| 397 | 401 |
int cycleArcNum() const {
|
| 398 | 402 |
return _best_size; |
| 399 | 403 |
} |
| 400 | 404 |
|
| 401 | 405 |
/// \brief Return the mean length of the found cycle. |
| 402 | 406 |
/// |
| 403 | 407 |
/// This function returns the mean length of the found cycle. |
| 404 | 408 |
/// |
| 405 | 409 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
| 406 | 410 |
/// following code. |
| 407 | 411 |
/// \code |
| 408 | 412 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum(); |
| 409 | 413 |
/// \endcode |
| 410 | 414 |
/// |
| 411 | 415 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 412 | 416 |
/// using this function. |
| 413 | 417 |
double cycleMean() const {
|
| 414 | 418 |
return static_cast<double>(_best_length) / _best_size; |
| 415 | 419 |
} |
| 416 | 420 |
|
| 417 | 421 |
/// \brief Return the found cycle. |
| 418 | 422 |
/// |
| 419 | 423 |
/// This function returns a const reference to the path structure |
| 420 | 424 |
/// storing the found cycle. |
| 421 | 425 |
/// |
| 422 | 426 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 423 | 427 |
/// this function. |
| 424 | 428 |
const Path& cycle() const {
|
| 425 | 429 |
return *_cycle_path; |
| 426 | 430 |
} |
| 427 | 431 |
|
| 428 | 432 |
///@} |
| 429 | 433 |
|
| 430 | 434 |
private: |
| 431 | 435 |
|
| 432 | 436 |
// Initialize |
| 433 | 437 |
void init() {
|
| 434 | 438 |
if (!_cycle_path) {
|
| 435 | 439 |
_local_path = true; |
| 436 | 440 |
_cycle_path = new Path; |
| 437 | 441 |
} |
| 438 | 442 |
_queue.resize(countNodes(_gr)); |
| 439 | 443 |
_best_found = false; |
| 440 | 444 |
_best_length = 0; |
| 441 | 445 |
_best_size = 1; |
| 442 | 446 |
_cycle_path->clear(); |
| 443 | 447 |
} |
| 444 | 448 |
|
| 445 | 449 |
// Find strongly connected components and initialize _comp_nodes |
| 446 | 450 |
// and _in_arcs |
| 447 | 451 |
void findComponents() {
|
| 448 | 452 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 449 | 453 |
_comp_nodes.resize(_comp_num); |
| 450 | 454 |
if (_comp_num == 1) {
|
| 451 | 455 |
_comp_nodes[0].clear(); |
| 452 | 456 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 453 | 457 |
_comp_nodes[0].push_back(n); |
| 454 | 458 |
_in_arcs[n].clear(); |
| 455 | 459 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
| 456 | 460 |
_in_arcs[n].push_back(a); |
| 457 | 461 |
} |
| 458 | 462 |
} |
| 459 | 463 |
} else {
|
| 460 | 464 |
for (int i = 0; i < _comp_num; ++i) |
| 461 | 465 |
_comp_nodes[i].clear(); |
| 462 | 466 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 463 | 467 |
int k = _comp[n]; |
| 464 | 468 |
_comp_nodes[k].push_back(n); |
| 465 | 469 |
_in_arcs[n].clear(); |
| 466 | 470 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
| 467 | 471 |
if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a); |
| 468 | 472 |
} |
| 469 | 473 |
} |
| 470 | 474 |
} |
| 471 | 475 |
} |
| 472 | 476 |
|
| 473 | 477 |
// Build the policy graph in the given strongly connected component |
| 474 | 478 |
// (the out-degree of every node is 1) |
| 475 | 479 |
bool buildPolicyGraph(int comp) {
|
| 476 | 480 |
_nodes = &(_comp_nodes[comp]); |
| 477 | 481 |
if (_nodes->size() < 1 || |
| 478 | 482 |
(_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
|
| 479 | 483 |
return false; |
| 480 | 484 |
} |
| 481 | 485 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 482 | 486 |
_dist[(*_nodes)[i]] = INF; |
| 483 | 487 |
} |
| 484 | 488 |
Node u, v; |
| 485 | 489 |
Arc e; |
| 486 | 490 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 487 | 491 |
v = (*_nodes)[i]; |
| 488 | 492 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
| 489 | 493 |
e = _in_arcs[v][j]; |
| 490 | 494 |
u = _gr.source(e); |
| 491 | 495 |
if (_length[e] < _dist[u]) {
|
| 492 | 496 |
_dist[u] = _length[e]; |
| 493 | 497 |
_policy[u] = e; |
| 494 | 498 |
} |
| 495 | 499 |
} |
| 496 | 500 |
} |
| 497 | 501 |
return true; |
| 498 | 502 |
} |
| 499 | 503 |
|
| 500 | 504 |
// Find the minimum mean cycle in the policy graph |
| 501 | 505 |
void findPolicyCycle() {
|
| 502 | 506 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 503 | 507 |
_level[(*_nodes)[i]] = -1; |
| 504 | 508 |
} |
| 505 | 509 |
LargeValue clength; |
| 506 | 510 |
int csize; |
| 507 | 511 |
Node u, v; |
| 508 | 512 |
_curr_found = false; |
| 509 | 513 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 510 | 514 |
u = (*_nodes)[i]; |
| 511 | 515 |
if (_level[u] >= 0) continue; |
| 512 | 516 |
for (; _level[u] < 0; u = _gr.target(_policy[u])) {
|
| 513 | 517 |
_level[u] = i; |
| 514 | 518 |
} |
| 515 | 519 |
if (_level[u] == i) {
|
| 516 | 520 |
// A cycle is found |
| 517 | 521 |
clength = _length[_policy[u]]; |
| 518 | 522 |
csize = 1; |
| 519 | 523 |
for (v = u; (v = _gr.target(_policy[v])) != u; ) {
|
| 520 | 524 |
clength += _length[_policy[v]]; |
| 521 | 525 |
++csize; |
| 522 | 526 |
} |
| 523 | 527 |
if ( !_curr_found || |
| 524 | 528 |
(clength * _curr_size < _curr_length * csize) ) {
|
| 525 | 529 |
_curr_found = true; |
| 526 | 530 |
_curr_length = clength; |
| 527 | 531 |
_curr_size = csize; |
| 528 | 532 |
_curr_node = u; |
| 529 | 533 |
} |
| 530 | 534 |
} |
| 531 | 535 |
} |
| 532 | 536 |
} |
| 533 | 537 |
|
| 534 | 538 |
// Contract the policy graph and compute node distances |
| 535 | 539 |
bool computeNodeDistances() {
|
| 536 | 540 |
// Find the component of the main cycle and compute node distances |
| 537 | 541 |
// using reverse BFS |
| 538 | 542 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 539 | 543 |
_reached[(*_nodes)[i]] = false; |
| 540 | 544 |
} |
| 541 | 545 |
_qfront = _qback = 0; |
| 542 | 546 |
_queue[0] = _curr_node; |
| 543 | 547 |
_reached[_curr_node] = true; |
| 544 | 548 |
_dist[_curr_node] = 0; |
| 545 | 549 |
Node u, v; |
| 546 | 550 |
Arc e; |
| 547 | 551 |
while (_qfront <= _qback) {
|
| 548 | 552 |
v = _queue[_qfront++]; |
| 549 | 553 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
| 550 | 554 |
e = _in_arcs[v][j]; |
| 551 | 555 |
u = _gr.source(e); |
| 552 | 556 |
if (_policy[u] == e && !_reached[u]) {
|
| 553 | 557 |
_reached[u] = true; |
| 554 | 558 |
_dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length; |
| 555 | 559 |
_queue[++_qback] = u; |
| 556 | 560 |
} |
| 557 | 561 |
} |
| 558 | 562 |
} |
| 559 | 563 |
|
| 560 | 564 |
// Connect all other nodes to this component and compute node |
| 561 | 565 |
// distances using reverse BFS |
| 562 | 566 |
_qfront = 0; |
| 563 | 567 |
while (_qback < int(_nodes->size())-1) {
|
| 564 | 568 |
v = _queue[_qfront++]; |
| 565 | 569 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
| 566 | 570 |
e = _in_arcs[v][j]; |
| 567 | 571 |
u = _gr.source(e); |
| 568 | 572 |
if (!_reached[u]) {
|
| 569 | 573 |
_reached[u] = true; |
| 570 | 574 |
_policy[u] = e; |
| 571 | 575 |
_dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length; |
| 572 | 576 |
_queue[++_qback] = u; |
| 573 | 577 |
} |
| 574 | 578 |
} |
| 575 | 579 |
} |
| 576 | 580 |
|
| 577 | 581 |
// Improve node distances |
| 578 | 582 |
bool improved = false; |
| 579 | 583 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 580 | 584 |
v = (*_nodes)[i]; |
| 581 | 585 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
| 582 | 586 |
e = _in_arcs[v][j]; |
| 583 | 587 |
u = _gr.source(e); |
| 584 | 588 |
LargeValue delta = _dist[v] + _length[e] * _curr_size - _curr_length; |
| 585 | 589 |
if (_tolerance.less(delta, _dist[u])) {
|
| 586 | 590 |
_dist[u] = delta; |
| 587 | 591 |
_policy[u] = e; |
| 588 | 592 |
improved = true; |
| 589 | 593 |
} |
| 590 | 594 |
} |
| 591 | 595 |
} |
| 592 | 596 |
return improved; |
| 593 | 597 |
} |
| 594 | 598 |
|
| 595 | 599 |
}; //class Howard |
| 596 | 600 |
|
| 597 | 601 |
///@} |
| 598 | 602 |
|
| 599 | 603 |
} //namespace lemon |
| 600 | 604 |
|
| 601 | 605 |
#endif //LEMON_HOWARD_H |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 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_KARP_H |
| 20 | 20 |
#define LEMON_KARP_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Karp's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Karp algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Karp algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct KarpDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 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 | 107 |
/// \tparam TR The traits class that defines various types used by the |
| 108 | 108 |
/// algorithm. By default, it is \ref KarpDefaultTraits |
| 109 | 109 |
/// "KarpDefaultTraits<GR, LEN>". |
| 110 | 110 |
/// In most cases, this parameter should not be set directly, |
| 111 | 111 |
/// consider to use the named template parameters instead. |
| 112 | 112 |
#ifdef DOXYGEN |
| 113 | 113 |
template <typename GR, typename LEN, typename TR> |
| 114 | 114 |
#else |
| 115 | 115 |
template < typename GR, |
| 116 | 116 |
typename LEN = typename GR::template ArcMap<int>, |
| 117 | 117 |
typename TR = KarpDefaultTraits<GR, LEN> > |
| 118 | 118 |
#endif |
| 119 | 119 |
class Karp |
| 120 | 120 |
{
|
| 121 | 121 |
public: |
| 122 | 122 |
|
| 123 | 123 |
/// The type of the digraph |
| 124 | 124 |
typedef typename TR::Digraph Digraph; |
| 125 | 125 |
/// The type of the length map |
| 126 | 126 |
typedef typename TR::LengthMap LengthMap; |
| 127 | 127 |
/// The type of the arc lengths |
| 128 | 128 |
typedef typename TR::Value Value; |
| 129 | 129 |
|
| 130 | 130 |
/// \brief The large value type |
| 131 | 131 |
/// |
| 132 | 132 |
/// The large value type used for internal computations. |
| 133 | 133 |
/// By default, it is \c long \c long if the \c Value type is integer, |
| 134 | 134 |
/// otherwise it is \c double. |
| 135 | 135 |
typedef typename TR::LargeValue LargeValue; |
| 136 | 136 |
|
| 137 | 137 |
/// The tolerance type |
| 138 | 138 |
typedef typename TR::Tolerance Tolerance; |
| 139 | 139 |
|
| 140 | 140 |
/// \brief The path type of the found cycles |
| 141 | 141 |
/// |
| 142 | 142 |
/// The path type of the found cycles. |
| 143 | 143 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 144 | 144 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 145 | 145 |
typedef typename TR::Path Path; |
| 146 | 146 |
|
| 147 | 147 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
| 148 | 148 |
typedef TR Traits; |
| 149 | 149 |
|
| 150 | 150 |
private: |
| 151 | 151 |
|
| 152 | 152 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 153 | 153 |
|
| 154 | 154 |
// Data sturcture for path data |
| 155 | 155 |
struct PathData |
| 156 | 156 |
{
|
| 157 | 157 |
LargeValue dist; |
| 158 | 158 |
Arc pred; |
| 159 | 159 |
PathData(LargeValue d, Arc p = INVALID) : |
| 160 | 160 |
dist(d), pred(p) {}
|
| 161 | 161 |
}; |
| 162 | 162 |
|
| 163 | 163 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 164 | 164 |
PathDataNodeMap; |
| 165 | 165 |
|
| 166 | 166 |
private: |
| 167 | 167 |
|
| 168 | 168 |
// The digraph the algorithm runs on |
| 169 | 169 |
const Digraph &_gr; |
| 170 | 170 |
// The length of the arcs |
| 171 | 171 |
const LengthMap &_length; |
| 172 | 172 |
|
| 173 | 173 |
// Data for storing the strongly connected components |
| 174 | 174 |
int _comp_num; |
| 175 | 175 |
typename Digraph::template NodeMap<int> _comp; |
| 176 | 176 |
std::vector<std::vector<Node> > _comp_nodes; |
| 177 | 177 |
std::vector<Node>* _nodes; |
| 178 | 178 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 179 | 179 |
|
| 180 | 180 |
// Data for the found cycle |
| 181 | 181 |
LargeValue _cycle_length; |
| 182 | 182 |
int _cycle_size; |
| 183 | 183 |
Node _cycle_node; |
| 184 | 184 |
|
| 185 | 185 |
Path *_cycle_path; |
| 186 | 186 |
bool _local_path; |
| 187 | 187 |
|
| 188 | 188 |
// Node map for storing path data |
| 189 | 189 |
PathDataNodeMap _data; |
| 190 | 190 |
// The processed nodes in the last round |
| 191 | 191 |
std::vector<Node> _process; |
| 192 | 192 |
|
| 193 | 193 |
Tolerance _tolerance; |
| 194 | 194 |
|
| 195 | 195 |
// Infinite constant |
| 196 | 196 |
const LargeValue INF; |
| 197 | 197 |
|
| 198 | 198 |
public: |
| 199 | 199 |
|
| 200 | 200 |
/// \name Named Template Parameters |
| 201 | 201 |
/// @{
|
| 202 | 202 |
|
| 203 | 203 |
template <typename T> |
| 204 | 204 |
struct SetLargeValueTraits : public Traits {
|
| 205 | 205 |
typedef T LargeValue; |
| 206 | 206 |
typedef lemon::Tolerance<T> Tolerance; |
| 207 | 207 |
}; |
| 208 | 208 |
|
| 209 | 209 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 210 | 210 |
/// \c LargeValue type. |
| 211 | 211 |
/// |
| 212 | 212 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 213 | 213 |
/// type. It is used for internal computations in the algorithm. |
| 214 | 214 |
template <typename T> |
| 215 | 215 |
struct SetLargeValue |
| 216 | 216 |
: public Karp<GR, LEN, SetLargeValueTraits<T> > {
|
| 217 | 217 |
typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create; |
| 218 | 218 |
}; |
| 219 | 219 |
|
| 220 | 220 |
template <typename T> |
| 221 | 221 |
struct SetPathTraits : public Traits {
|
| 222 | 222 |
typedef T Path; |
| 223 | 223 |
}; |
| 224 | 224 |
|
| 225 | 225 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 226 | 226 |
/// \c %Path type. |
| 227 | 227 |
/// |
| 228 | 228 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 229 | 229 |
/// type of the found cycles. |
| 230 | 230 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 231 | 231 |
/// and it must have an \c addFront() function. |
| 232 | 232 |
template <typename T> |
| 233 | 233 |
struct SetPath |
| 234 | 234 |
: public Karp<GR, LEN, SetPathTraits<T> > {
|
| 235 | 235 |
typedef Karp<GR, LEN, SetPathTraits<T> > Create; |
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
/// @} |
| 239 | 239 |
|
| 240 |
protected: |
|
| 241 |
|
|
| 242 |
Karp() {}
|
|
| 243 |
|
|
| 240 | 244 |
public: |
| 241 | 245 |
|
| 242 | 246 |
/// \brief Constructor. |
| 243 | 247 |
/// |
| 244 | 248 |
/// The constructor of the class. |
| 245 | 249 |
/// |
| 246 | 250 |
/// \param digraph The digraph the algorithm runs on. |
| 247 | 251 |
/// \param length The lengths (costs) of the arcs. |
| 248 | 252 |
Karp( const Digraph &digraph, |
| 249 | 253 |
const LengthMap &length ) : |
| 250 | 254 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
| 251 | 255 |
_cycle_length(0), _cycle_size(1), _cycle_node(INVALID), |
| 252 | 256 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
| 253 | 257 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 254 | 258 |
std::numeric_limits<LargeValue>::infinity() : |
| 255 | 259 |
std::numeric_limits<LargeValue>::max()) |
| 256 | 260 |
{}
|
| 257 | 261 |
|
| 258 | 262 |
/// Destructor. |
| 259 | 263 |
~Karp() {
|
| 260 | 264 |
if (_local_path) delete _cycle_path; |
| 261 | 265 |
} |
| 262 | 266 |
|
| 263 | 267 |
/// \brief Set the path structure for storing the found cycle. |
| 264 | 268 |
/// |
| 265 | 269 |
/// This function sets an external path structure for storing the |
| 266 | 270 |
/// found cycle. |
| 267 | 271 |
/// |
| 268 | 272 |
/// If you don't call this function before calling \ref run() or |
| 269 | 273 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 270 | 274 |
/// structure. The destuctor deallocates this automatically |
| 271 | 275 |
/// allocated object, of course. |
| 272 | 276 |
/// |
| 273 | 277 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
| 274 | 278 |
/// "addFront()" function of the given path structure. |
| 275 | 279 |
/// |
| 276 | 280 |
/// \return <tt>(*this)</tt> |
| 277 | 281 |
Karp& cycle(Path &path) {
|
| 278 | 282 |
if (_local_path) {
|
| 279 | 283 |
delete _cycle_path; |
| 280 | 284 |
_local_path = false; |
| 281 | 285 |
} |
| 282 | 286 |
_cycle_path = &path; |
| 283 | 287 |
return *this; |
| 284 | 288 |
} |
| 285 | 289 |
|
| 286 | 290 |
/// \brief Set the tolerance used by the algorithm. |
| 287 | 291 |
/// |
| 288 | 292 |
/// This function sets the tolerance object used by the algorithm. |
| 289 | 293 |
/// |
| 290 | 294 |
/// \return <tt>(*this)</tt> |
| 291 | 295 |
Karp& tolerance(const Tolerance& tolerance) {
|
| 292 | 296 |
_tolerance = tolerance; |
| 293 | 297 |
return *this; |
| 294 | 298 |
} |
| 295 | 299 |
|
| 296 | 300 |
/// \brief Return a const reference to the tolerance. |
| 297 | 301 |
/// |
| 298 | 302 |
/// This function returns a const reference to the tolerance object |
| 299 | 303 |
/// used by the algorithm. |
| 300 | 304 |
const Tolerance& tolerance() const {
|
| 301 | 305 |
return _tolerance; |
| 302 | 306 |
} |
| 303 | 307 |
|
| 304 | 308 |
/// \name Execution control |
| 305 | 309 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 306 | 310 |
/// function.\n |
| 307 | 311 |
/// If you only need the minimum mean length, you may call |
| 308 | 312 |
/// \ref findMinMean(). |
| 309 | 313 |
|
| 310 | 314 |
/// @{
|
| 311 | 315 |
|
| 312 | 316 |
/// \brief Run the algorithm. |
| 313 | 317 |
/// |
| 314 | 318 |
/// This function runs the algorithm. |
| 315 | 319 |
/// It can be called more than once (e.g. if the underlying digraph |
| 316 | 320 |
/// and/or the arc lengths have been modified). |
| 317 | 321 |
/// |
| 318 | 322 |
/// \return \c true if a directed cycle exists in the digraph. |
| 319 | 323 |
/// |
| 320 | 324 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 321 | 325 |
/// \code |
| 322 | 326 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 323 | 327 |
/// \endcode |
| 324 | 328 |
bool run() {
|
| 325 | 329 |
return findMinMean() && findCycle(); |
| 326 | 330 |
} |
| 327 | 331 |
|
| 328 | 332 |
/// \brief Find the minimum cycle mean. |
| 329 | 333 |
/// |
| 330 | 334 |
/// This function finds the minimum mean length of the directed |
| 331 | 335 |
/// cycles in the digraph. |
| 332 | 336 |
/// |
| 333 | 337 |
/// \return \c true if a directed cycle exists in the digraph. |
| 334 | 338 |
bool findMinMean() {
|
| 335 | 339 |
// Initialization and find strongly connected components |
| 336 | 340 |
init(); |
| 337 | 341 |
findComponents(); |
| 338 | 342 |
|
| 339 | 343 |
// Find the minimum cycle mean in the components |
| 340 | 344 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 341 | 345 |
if (!initComponent(comp)) continue; |
| 342 | 346 |
processRounds(); |
| 343 | 347 |
updateMinMean(); |
| 344 | 348 |
} |
| 345 | 349 |
return (_cycle_node != INVALID); |
| 346 | 350 |
} |
| 347 | 351 |
|
| 348 | 352 |
/// \brief Find a minimum mean directed cycle. |
| 349 | 353 |
/// |
| 350 | 354 |
/// This function finds a directed cycle of minimum mean length |
| 351 | 355 |
/// in the digraph using the data computed by findMinMean(). |
| 352 | 356 |
/// |
| 353 | 357 |
/// \return \c true if a directed cycle exists in the digraph. |
| 354 | 358 |
/// |
| 355 | 359 |
/// \pre \ref findMinMean() must be called before using this function. |
| 356 | 360 |
bool findCycle() {
|
| 357 | 361 |
if (_cycle_node == INVALID) return false; |
| 358 | 362 |
IntNodeMap reached(_gr, -1); |
| 359 | 363 |
int r = _data[_cycle_node].size(); |
| 360 | 364 |
Node u = _cycle_node; |
| 361 | 365 |
while (reached[u] < 0) {
|
| 362 | 366 |
reached[u] = --r; |
| 363 | 367 |
u = _gr.source(_data[u][r].pred); |
| 364 | 368 |
} |
| 365 | 369 |
r = reached[u]; |
| 366 | 370 |
Arc e = _data[u][r].pred; |
| 367 | 371 |
_cycle_path->addFront(e); |
| 368 | 372 |
_cycle_length = _length[e]; |
| 369 | 373 |
_cycle_size = 1; |
| 370 | 374 |
Node v; |
| 371 | 375 |
while ((v = _gr.source(e)) != u) {
|
| 372 | 376 |
e = _data[v][--r].pred; |
| 373 | 377 |
_cycle_path->addFront(e); |
| 374 | 378 |
_cycle_length += _length[e]; |
| 375 | 379 |
++_cycle_size; |
| 376 | 380 |
} |
| 377 | 381 |
return true; |
| 378 | 382 |
} |
| 379 | 383 |
|
| 380 | 384 |
/// @} |
| 381 | 385 |
|
| 382 | 386 |
/// \name Query Functions |
| 383 | 387 |
/// The results of the algorithm can be obtained using these |
| 384 | 388 |
/// functions.\n |
| 385 | 389 |
/// The algorithm should be executed before using them. |
| 386 | 390 |
|
| 387 | 391 |
/// @{
|
| 388 | 392 |
|
| 389 | 393 |
/// \brief Return the total length of the found cycle. |
| 390 | 394 |
/// |
| 391 | 395 |
/// This function returns the total length of the found cycle. |
| 392 | 396 |
/// |
| 393 | 397 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 394 | 398 |
/// using this function. |
| 395 | 399 |
Value cycleLength() const {
|
| 396 | 400 |
return static_cast<Value>(_cycle_length); |
| 397 | 401 |
} |
| 398 | 402 |
|
| 399 | 403 |
/// \brief Return the number of arcs on the found cycle. |
| 400 | 404 |
/// |
| 401 | 405 |
/// This function returns the number of arcs on the found cycle. |
| 402 | 406 |
/// |
| 403 | 407 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 404 | 408 |
/// using this function. |
| 405 | 409 |
int cycleArcNum() const {
|
| 406 | 410 |
return _cycle_size; |
| 407 | 411 |
} |
| 408 | 412 |
|
| 409 | 413 |
/// \brief Return the mean length of the found cycle. |
| 410 | 414 |
/// |
| 411 | 415 |
/// This function returns the mean length of the found cycle. |
| 412 | 416 |
/// |
| 413 | 417 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
| 414 | 418 |
/// following code. |
| 415 | 419 |
/// \code |
| 416 | 420 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum(); |
| 417 | 421 |
/// \endcode |
| 418 | 422 |
/// |
| 419 | 423 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 420 | 424 |
/// using this function. |
| 421 | 425 |
double cycleMean() const {
|
| 422 | 426 |
return static_cast<double>(_cycle_length) / _cycle_size; |
| 423 | 427 |
} |
| 424 | 428 |
|
| 425 | 429 |
/// \brief Return the found cycle. |
| 426 | 430 |
/// |
| 427 | 431 |
/// This function returns a const reference to the path structure |
| 428 | 432 |
/// storing the found cycle. |
| 429 | 433 |
/// |
| 430 | 434 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 431 | 435 |
/// this function. |
| 432 | 436 |
const Path& cycle() const {
|
| 433 | 437 |
return *_cycle_path; |
| 434 | 438 |
} |
| 435 | 439 |
|
| 436 | 440 |
///@} |
| 437 | 441 |
|
| 438 | 442 |
private: |
| 439 | 443 |
|
| 440 | 444 |
// Initialization |
| 441 | 445 |
void init() {
|
| 442 | 446 |
if (!_cycle_path) {
|
| 443 | 447 |
_local_path = true; |
| 444 | 448 |
_cycle_path = new Path; |
| 445 | 449 |
} |
| 446 | 450 |
_cycle_path->clear(); |
| 447 | 451 |
_cycle_length = 0; |
| 448 | 452 |
_cycle_size = 1; |
| 449 | 453 |
_cycle_node = INVALID; |
| 450 | 454 |
for (NodeIt u(_gr); u != INVALID; ++u) |
| 451 | 455 |
_data[u].clear(); |
| 452 | 456 |
} |
| 453 | 457 |
|
| 454 | 458 |
// Find strongly connected components and initialize _comp_nodes |
| 455 | 459 |
// and _out_arcs |
| 456 | 460 |
void findComponents() {
|
| 457 | 461 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 458 | 462 |
_comp_nodes.resize(_comp_num); |
| 459 | 463 |
if (_comp_num == 1) {
|
| 460 | 464 |
_comp_nodes[0].clear(); |
| 461 | 465 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 462 | 466 |
_comp_nodes[0].push_back(n); |
| 463 | 467 |
_out_arcs[n].clear(); |
| 464 | 468 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 465 | 469 |
_out_arcs[n].push_back(a); |
| 466 | 470 |
} |
| 467 | 471 |
} |
| 468 | 472 |
} else {
|
| 469 | 473 |
for (int i = 0; i < _comp_num; ++i) |
| 470 | 474 |
_comp_nodes[i].clear(); |
| 471 | 475 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 472 | 476 |
int k = _comp[n]; |
| 473 | 477 |
_comp_nodes[k].push_back(n); |
| 474 | 478 |
_out_arcs[n].clear(); |
| 475 | 479 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 476 | 480 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
| 477 | 481 |
} |
| 478 | 482 |
} |
| 479 | 483 |
} |
| 480 | 484 |
} |
| 481 | 485 |
|
| 482 | 486 |
// Initialize path data for the current component |
| 483 | 487 |
bool initComponent(int comp) {
|
| 484 | 488 |
_nodes = &(_comp_nodes[comp]); |
| 485 | 489 |
int n = _nodes->size(); |
| 486 | 490 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
| 487 | 491 |
return false; |
| 488 | 492 |
} |
| 489 | 493 |
for (int i = 0; i < n; ++i) {
|
| 490 | 494 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
| 491 | 495 |
} |
| 492 | 496 |
return true; |
| 493 | 497 |
} |
| 494 | 498 |
|
| 495 | 499 |
// Process all rounds of computing path data for the current component. |
| 496 | 500 |
// _data[v][k] is the length of a shortest directed walk from the root |
| 497 | 501 |
// node to node v containing exactly k arcs. |
| 498 | 502 |
void processRounds() {
|
| 499 | 503 |
Node start = (*_nodes)[0]; |
| 500 | 504 |
_data[start][0] = PathData(0); |
| 501 | 505 |
_process.clear(); |
| 502 | 506 |
_process.push_back(start); |
| 503 | 507 |
|
| 504 | 508 |
int k, n = _nodes->size(); |
| 505 | 509 |
for (k = 1; k <= n && int(_process.size()) < n; ++k) {
|
| 506 | 510 |
processNextBuildRound(k); |
| 507 | 511 |
} |
| 508 | 512 |
for ( ; k <= n; ++k) {
|
| 509 | 513 |
processNextFullRound(k); |
| 510 | 514 |
} |
| 511 | 515 |
} |
| 512 | 516 |
|
| 513 | 517 |
// Process one round and rebuild _process |
| 514 | 518 |
void processNextBuildRound(int k) {
|
| 515 | 519 |
std::vector<Node> next; |
| 516 | 520 |
Node u, v; |
| 517 | 521 |
Arc e; |
| 518 | 522 |
LargeValue d; |
| 519 | 523 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 520 | 524 |
u = _process[i]; |
| 521 | 525 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
| 522 | 526 |
e = _out_arcs[u][j]; |
| 523 | 527 |
v = _gr.target(e); |
| 524 | 528 |
d = _data[u][k-1].dist + _length[e]; |
| 525 | 529 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
| 526 | 530 |
if (_data[v][k].dist == INF) next.push_back(v); |
| 527 | 531 |
_data[v][k] = PathData(d, e); |
| 528 | 532 |
} |
| 529 | 533 |
} |
| 530 | 534 |
} |
| 531 | 535 |
_process.swap(next); |
| 532 | 536 |
} |
| 533 | 537 |
|
| 534 | 538 |
// Process one round using _nodes instead of _process |
| 535 | 539 |
void processNextFullRound(int k) {
|
| 536 | 540 |
Node u, v; |
| 537 | 541 |
Arc e; |
| 538 | 542 |
LargeValue d; |
| 539 | 543 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 540 | 544 |
u = (*_nodes)[i]; |
| 541 | 545 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
| 542 | 546 |
e = _out_arcs[u][j]; |
| 543 | 547 |
v = _gr.target(e); |
| 544 | 548 |
d = _data[u][k-1].dist + _length[e]; |
| 545 | 549 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
| 546 | 550 |
_data[v][k] = PathData(d, e); |
| 547 | 551 |
} |
| 548 | 552 |
} |
| 549 | 553 |
} |
| 550 | 554 |
} |
| 551 | 555 |
|
| 552 | 556 |
// Update the minimum cycle mean |
| 553 | 557 |
void updateMinMean() {
|
| 554 | 558 |
int n = _nodes->size(); |
| 555 | 559 |
for (int i = 0; i < n; ++i) {
|
| 556 | 560 |
Node u = (*_nodes)[i]; |
| 557 | 561 |
if (_data[u][n].dist == INF) continue; |
| 558 | 562 |
LargeValue length, max_length = 0; |
| 559 | 563 |
int size, max_size = 1; |
| 560 | 564 |
bool found_curr = false; |
| 561 | 565 |
for (int k = 0; k < n; ++k) {
|
| 562 | 566 |
if (_data[u][k].dist == INF) continue; |
| 563 | 567 |
length = _data[u][n].dist - _data[u][k].dist; |
| 564 | 568 |
size = n - k; |
| 565 | 569 |
if (!found_curr || length * max_size > max_length * size) {
|
| 566 | 570 |
found_curr = true; |
| 567 | 571 |
max_length = length; |
| 568 | 572 |
max_size = size; |
| 569 | 573 |
} |
| 570 | 574 |
} |
| 571 | 575 |
if ( found_curr && (_cycle_node == INVALID || |
| 572 | 576 |
max_length * _cycle_size < _cycle_length * max_size) ) {
|
| 573 | 577 |
_cycle_length = max_length; |
| 574 | 578 |
_cycle_size = max_size; |
| 575 | 579 |
_cycle_node = u; |
| 576 | 580 |
} |
| 577 | 581 |
} |
| 578 | 582 |
} |
| 579 | 583 |
|
| 580 | 584 |
}; //class Karp |
| 581 | 585 |
|
| 582 | 586 |
///@} |
| 583 | 587 |
|
| 584 | 588 |
} //namespace lemon |
| 585 | 589 |
|
| 586 | 590 |
#endif //LEMON_KARP_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 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_SUURBALLE_H |
| 20 | 20 |
#define LEMON_SUURBALLE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup shortest_path |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief An algorithm for finding arc-disjoint paths between two |
| 25 | 25 |
/// nodes having minimum total length. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/list_graph.h> |
| 32 | 32 |
#include <lemon/dijkstra.h> |
| 33 | 33 |
#include <lemon/maps.h> |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \brief Default traits class of Suurballe algorithm. |
| 38 | 38 |
/// |
| 39 | 39 |
/// Default traits class of Suurballe algorithm. |
| 40 | 40 |
/// \tparam GR The digraph type the algorithm runs on. |
| 41 | 41 |
/// \tparam LEN The type of the length map. |
| 42 | 42 |
/// The default value is <tt>GR::ArcMap<int></tt>. |
| 43 | 43 |
#ifdef DOXYGEN |
| 44 | 44 |
template <typename GR, typename LEN> |
| 45 | 45 |
#else |
| 46 | 46 |
template < typename GR, |
| 47 | 47 |
typename LEN = typename GR::template ArcMap<int> > |
| 48 | 48 |
#endif |
| 49 | 49 |
struct SuurballeDefaultTraits |
| 50 | 50 |
{
|
| 51 | 51 |
/// The type of the digraph. |
| 52 | 52 |
typedef GR Digraph; |
| 53 | 53 |
/// The type of the length map. |
| 54 | 54 |
typedef LEN LengthMap; |
| 55 | 55 |
/// The type of the lengths. |
| 56 | 56 |
typedef typename LEN::Value Length; |
| 57 | 57 |
/// The type of the flow map. |
| 58 | 58 |
typedef typename GR::template ArcMap<int> FlowMap; |
| 59 | 59 |
/// The type of the potential map. |
| 60 | 60 |
typedef typename GR::template NodeMap<Length> PotentialMap; |
| 61 | 61 |
|
| 62 | 62 |
/// \brief The path type |
| 63 | 63 |
/// |
| 64 | 64 |
/// The type used for storing the found arc-disjoint paths. |
| 65 | 65 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 66 | 66 |
/// and it must have an \c addBack() function. |
| 67 | 67 |
typedef lemon::Path<Digraph> Path; |
| 68 | 68 |
|
| 69 | 69 |
/// The cross reference type used for the heap. |
| 70 | 70 |
typedef typename GR::template NodeMap<int> HeapCrossRef; |
| 71 | 71 |
|
| 72 | 72 |
/// \brief The heap type used for internal Dijkstra computations. |
| 73 | 73 |
/// |
| 74 | 74 |
/// The type of the heap used for internal Dijkstra computations. |
| 75 | 75 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept |
| 76 | 76 |
/// and its priority type must be \c Length. |
| 77 | 77 |
typedef BinHeap<Length, HeapCrossRef> Heap; |
| 78 | 78 |
}; |
| 79 | 79 |
|
| 80 | 80 |
/// \addtogroup shortest_path |
| 81 | 81 |
/// @{
|
| 82 | 82 |
|
| 83 | 83 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes |
| 84 | 84 |
/// having minimum total length. |
| 85 | 85 |
/// |
| 86 | 86 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for |
| 87 | 87 |
/// finding arc-disjoint paths having minimum total length (cost) |
| 88 | 88 |
/// from a given source node to a given target node in a digraph. |
| 89 | 89 |
/// |
| 90 | 90 |
/// Note that this problem is a special case of the \ref min_cost_flow |
| 91 | 91 |
/// "minimum cost flow problem". This implementation is actually an |
| 92 | 92 |
/// efficient specialized version of the \ref CapacityScaling |
| 93 | 93 |
/// "successive shortest path" algorithm directly for this problem. |
| 94 | 94 |
/// Therefore this class provides query functions for flow values and |
| 95 | 95 |
/// node potentials (the dual solution) just like the minimum cost flow |
| 96 | 96 |
/// algorithms. |
| 97 | 97 |
/// |
| 98 | 98 |
/// \tparam GR The digraph type the algorithm runs on. |
| 99 | 99 |
/// \tparam LEN The type of the length map. |
| 100 | 100 |
/// The default value is <tt>GR::ArcMap<int></tt>. |
| 101 | 101 |
/// |
| 102 | 102 |
/// \warning Length values should be \e non-negative. |
| 103 | 103 |
/// |
| 104 | 104 |
/// \note For finding \e node-disjoint paths, this algorithm can be used |
| 105 | 105 |
/// along with the \ref SplitNodes adaptor. |
| 106 | 106 |
#ifdef DOXYGEN |
| 107 | 107 |
template <typename GR, typename LEN, typename TR> |
| 108 | 108 |
#else |
| 109 | 109 |
template < typename GR, |
| 110 | 110 |
typename LEN = typename GR::template ArcMap<int>, |
| 111 | 111 |
typename TR = SuurballeDefaultTraits<GR, LEN> > |
| 112 | 112 |
#endif |
| 113 | 113 |
class Suurballe |
| 114 | 114 |
{
|
| 115 | 115 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 116 | 116 |
|
| 117 | 117 |
typedef ConstMap<Arc, int> ConstArcMap; |
| 118 | 118 |
typedef typename GR::template NodeMap<Arc> PredMap; |
| 119 | 119 |
|
| 120 | 120 |
public: |
| 121 | 121 |
|
| 122 | 122 |
/// The type of the digraph. |
| 123 | 123 |
typedef typename TR::Digraph Digraph; |
| 124 | 124 |
/// The type of the length map. |
| 125 | 125 |
typedef typename TR::LengthMap LengthMap; |
| 126 | 126 |
/// The type of the lengths. |
| 127 | 127 |
typedef typename TR::Length Length; |
| 128 | 128 |
|
| 129 | 129 |
/// The type of the flow map. |
| 130 | 130 |
typedef typename TR::FlowMap FlowMap; |
| 131 | 131 |
/// The type of the potential map. |
| 132 | 132 |
typedef typename TR::PotentialMap PotentialMap; |
| 133 | 133 |
/// The type of the path structures. |
| 134 | 134 |
typedef typename TR::Path Path; |
| 135 | 135 |
/// The cross reference type used for the heap. |
| 136 | 136 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
| 137 | 137 |
/// The heap type used for internal Dijkstra computations. |
| 138 | 138 |
typedef typename TR::Heap Heap; |
| 139 | 139 |
|
| 140 | 140 |
/// The \ref SuurballeDefaultTraits "traits class" of the algorithm. |
| 141 | 141 |
typedef TR Traits; |
| 142 | 142 |
|
| 143 | 143 |
private: |
| 144 | 144 |
|
| 145 | 145 |
// ResidualDijkstra is a special implementation of the |
| 146 | 146 |
// Dijkstra algorithm for finding shortest paths in the |
| 147 | 147 |
// residual network with respect to the reduced arc lengths |
| 148 | 148 |
// and modifying the node potentials according to the |
| 149 | 149 |
// distance of the nodes. |
| 150 | 150 |
class ResidualDijkstra |
| 151 | 151 |
{
|
| 152 | 152 |
private: |
| 153 | 153 |
|
| 154 | 154 |
const Digraph &_graph; |
| 155 | 155 |
const LengthMap &_length; |
| 156 | 156 |
const FlowMap &_flow; |
| 157 | 157 |
PotentialMap &_pi; |
| 158 | 158 |
PredMap &_pred; |
| 159 | 159 |
Node _s; |
| 160 | 160 |
Node _t; |
| 161 | 161 |
|
| 162 | 162 |
PotentialMap _dist; |
| 163 | 163 |
std::vector<Node> _proc_nodes; |
| 164 | 164 |
|
| 165 | 165 |
public: |
| 166 | 166 |
|
| 167 | 167 |
// Constructor |
| 168 | 168 |
ResidualDijkstra(Suurballe &srb) : |
| 169 | 169 |
_graph(srb._graph), _length(srb._length), |
| 170 | 170 |
_flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred), |
| 171 | 171 |
_s(srb._s), _t(srb._t), _dist(_graph) {}
|
| 172 | 172 |
|
| 173 | 173 |
// Run the algorithm and return true if a path is found |
| 174 | 174 |
// from the source node to the target node. |
| 175 | 175 |
bool run(int cnt) {
|
| 176 | 176 |
return cnt == 0 ? startFirst() : start(); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
private: |
| 180 | 180 |
|
| 181 | 181 |
// Execute the algorithm for the first time (the flow and potential |
| 182 | 182 |
// functions have to be identically zero). |
| 183 | 183 |
bool startFirst() {
|
| 184 | 184 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
| 185 | 185 |
Heap heap(heap_cross_ref); |
| 186 | 186 |
heap.push(_s, 0); |
| 187 | 187 |
_pred[_s] = INVALID; |
| 188 | 188 |
_proc_nodes.clear(); |
| 189 | 189 |
|
| 190 | 190 |
// Process nodes |
| 191 | 191 |
while (!heap.empty() && heap.top() != _t) {
|
| 192 | 192 |
Node u = heap.top(), v; |
| 193 | 193 |
Length d = heap.prio(), dn; |
| 194 | 194 |
_dist[u] = heap.prio(); |
| 195 | 195 |
_proc_nodes.push_back(u); |
| 196 | 196 |
heap.pop(); |
| 197 | 197 |
|
| 198 | 198 |
// Traverse outgoing arcs |
| 199 | 199 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
|
| 200 | 200 |
v = _graph.target(e); |
| 201 | 201 |
switch(heap.state(v)) {
|
| 202 | 202 |
case Heap::PRE_HEAP: |
| 203 | 203 |
heap.push(v, d + _length[e]); |
| 204 | 204 |
_pred[v] = e; |
| 205 | 205 |
break; |
| 206 | 206 |
case Heap::IN_HEAP: |
| 207 | 207 |
dn = d + _length[e]; |
| 208 | 208 |
if (dn < heap[v]) {
|
| 209 | 209 |
heap.decrease(v, dn); |
| 210 | 210 |
_pred[v] = e; |
| 211 | 211 |
} |
| 212 | 212 |
break; |
| 213 | 213 |
case Heap::POST_HEAP: |
| 214 | 214 |
break; |
| 215 | 215 |
} |
| 216 | 216 |
} |
| 217 | 217 |
} |
| 218 | 218 |
if (heap.empty()) return false; |
| 219 | 219 |
|
| 220 | 220 |
// Update potentials of processed nodes |
| 221 | 221 |
Length t_dist = heap.prio(); |
| 222 | 222 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) |
| 223 | 223 |
_pi[_proc_nodes[i]] = _dist[_proc_nodes[i]] - t_dist; |
| 224 | 224 |
return true; |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
// Execute the algorithm. |
| 228 | 228 |
bool start() {
|
| 229 | 229 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
| 230 | 230 |
Heap heap(heap_cross_ref); |
| 231 | 231 |
heap.push(_s, 0); |
| 232 | 232 |
_pred[_s] = INVALID; |
| 233 | 233 |
_proc_nodes.clear(); |
| 234 | 234 |
|
| 235 | 235 |
// Process nodes |
| 236 | 236 |
while (!heap.empty() && heap.top() != _t) {
|
| 237 | 237 |
Node u = heap.top(), v; |
| 238 | 238 |
Length d = heap.prio() + _pi[u], dn; |
| 239 | 239 |
_dist[u] = heap.prio(); |
| 240 | 240 |
_proc_nodes.push_back(u); |
| 241 | 241 |
heap.pop(); |
| 242 | 242 |
|
| 243 | 243 |
// Traverse outgoing arcs |
| 244 | 244 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
|
| 245 | 245 |
if (_flow[e] == 0) {
|
| 246 | 246 |
v = _graph.target(e); |
| 247 | 247 |
switch(heap.state(v)) {
|
| 248 | 248 |
case Heap::PRE_HEAP: |
| 249 | 249 |
heap.push(v, d + _length[e] - _pi[v]); |
| 250 | 250 |
_pred[v] = e; |
| 251 | 251 |
break; |
| 252 | 252 |
case Heap::IN_HEAP: |
| 253 | 253 |
dn = d + _length[e] - _pi[v]; |
| 254 | 254 |
if (dn < heap[v]) {
|
| 255 | 255 |
heap.decrease(v, dn); |
| 256 | 256 |
_pred[v] = e; |
| 257 | 257 |
} |
| 258 | 258 |
break; |
| 259 | 259 |
case Heap::POST_HEAP: |
| 260 | 260 |
break; |
| 261 | 261 |
} |
| 262 | 262 |
} |
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 | 265 |
// Traverse incoming arcs |
| 266 | 266 |
for (InArcIt e(_graph, u); e != INVALID; ++e) {
|
| 267 | 267 |
if (_flow[e] == 1) {
|
| 268 | 268 |
v = _graph.source(e); |
| 269 | 269 |
switch(heap.state(v)) {
|
| 270 | 270 |
case Heap::PRE_HEAP: |
| 271 | 271 |
heap.push(v, d - _length[e] - _pi[v]); |
| 272 | 272 |
_pred[v] = e; |
| 273 | 273 |
break; |
| 274 | 274 |
case Heap::IN_HEAP: |
| 275 | 275 |
dn = d - _length[e] - _pi[v]; |
| 276 | 276 |
if (dn < heap[v]) {
|
| 277 | 277 |
heap.decrease(v, dn); |
| 278 | 278 |
_pred[v] = e; |
| 279 | 279 |
} |
| 280 | 280 |
break; |
| 281 | 281 |
case Heap::POST_HEAP: |
| 282 | 282 |
break; |
| 283 | 283 |
} |
| 284 | 284 |
} |
| 285 | 285 |
} |
| 286 | 286 |
} |
| 287 | 287 |
if (heap.empty()) return false; |
| 288 | 288 |
|
| 289 | 289 |
// Update potentials of processed nodes |
| 290 | 290 |
Length t_dist = heap.prio(); |
| 291 | 291 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) |
| 292 | 292 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist; |
| 293 | 293 |
return true; |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 | 296 |
}; //class ResidualDijkstra |
| 297 | 297 |
|
| 298 | 298 |
public: |
| 299 | 299 |
|
| 300 | 300 |
/// \name Named Template Parameters |
| 301 | 301 |
/// @{
|
| 302 | 302 |
|
| 303 | 303 |
template <typename T> |
| 304 | 304 |
struct SetFlowMapTraits : public Traits {
|
| 305 | 305 |
typedef T FlowMap; |
| 306 | 306 |
}; |
| 307 | 307 |
|
| 308 | 308 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 309 | 309 |
/// \c FlowMap type. |
| 310 | 310 |
/// |
| 311 | 311 |
/// \ref named-templ-param "Named parameter" for setting |
| 312 | 312 |
/// \c FlowMap type. |
| 313 | 313 |
template <typename T> |
| 314 | 314 |
struct SetFlowMap |
| 315 | 315 |
: public Suurballe<GR, LEN, SetFlowMapTraits<T> > {
|
| 316 | 316 |
typedef Suurballe<GR, LEN, SetFlowMapTraits<T> > Create; |
| 317 | 317 |
}; |
| 318 | 318 |
|
| 319 | 319 |
template <typename T> |
| 320 | 320 |
struct SetPotentialMapTraits : public Traits {
|
| 321 | 321 |
typedef T PotentialMap; |
| 322 | 322 |
}; |
| 323 | 323 |
|
| 324 | 324 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 325 | 325 |
/// \c PotentialMap type. |
| 326 | 326 |
/// |
| 327 | 327 |
/// \ref named-templ-param "Named parameter" for setting |
| 328 | 328 |
/// \c PotentialMap type. |
| 329 | 329 |
template <typename T> |
| 330 | 330 |
struct SetPotentialMap |
| 331 | 331 |
: public Suurballe<GR, LEN, SetPotentialMapTraits<T> > {
|
| 332 | 332 |
typedef Suurballe<GR, LEN, SetPotentialMapTraits<T> > Create; |
| 333 | 333 |
}; |
| 334 | 334 |
|
| 335 | 335 |
template <typename T> |
| 336 | 336 |
struct SetPathTraits : public Traits {
|
| 337 | 337 |
typedef T Path; |
| 338 | 338 |
}; |
| 339 | 339 |
|
| 340 | 340 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 341 | 341 |
/// \c %Path type. |
| 342 | 342 |
/// |
| 343 | 343 |
/// \ref named-templ-param "Named parameter" for setting \c %Path type. |
| 344 | 344 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 345 | 345 |
/// and it must have an \c addBack() function. |
| 346 | 346 |
template <typename T> |
| 347 | 347 |
struct SetPath |
| 348 | 348 |
: public Suurballe<GR, LEN, SetPathTraits<T> > {
|
| 349 | 349 |
typedef Suurballe<GR, LEN, SetPathTraits<T> > Create; |
| 350 | 350 |
}; |
| 351 | 351 |
|
| 352 | 352 |
template <typename H, typename CR> |
| 353 | 353 |
struct SetHeapTraits : public Traits {
|
| 354 | 354 |
typedef H Heap; |
| 355 | 355 |
typedef CR HeapCrossRef; |
| 356 | 356 |
}; |
| 357 | 357 |
|
| 358 | 358 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 359 | 359 |
/// \c Heap and \c HeapCrossRef types. |
| 360 | 360 |
/// |
| 361 | 361 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
| 362 | 362 |
/// and \c HeapCrossRef types with automatic allocation. |
| 363 | 363 |
/// They will be used for internal Dijkstra computations. |
| 364 | 364 |
/// The heap type must conform to the \ref lemon::concepts::Heap "Heap" |
| 365 | 365 |
/// concept and its priority type must be \c Length. |
| 366 | 366 |
template <typename H, |
| 367 | 367 |
typename CR = typename Digraph::template NodeMap<int> > |
| 368 | 368 |
struct SetHeap |
| 369 | 369 |
: public Suurballe<GR, LEN, SetHeapTraits<H, CR> > {
|
| 370 | 370 |
typedef Suurballe<GR, LEN, SetHeapTraits<H, CR> > Create; |
| 371 | 371 |
}; |
| 372 | 372 |
|
| 373 | 373 |
/// @} |
| 374 | 374 |
|
| 375 | 375 |
private: |
| 376 | 376 |
|
| 377 | 377 |
// The digraph the algorithm runs on |
| 378 | 378 |
const Digraph &_graph; |
| 379 | 379 |
// The length map |
| 380 | 380 |
const LengthMap &_length; |
| 381 | 381 |
|
| 382 | 382 |
// Arc map of the current flow |
| 383 | 383 |
FlowMap *_flow; |
| 384 | 384 |
bool _local_flow; |
| 385 | 385 |
// Node map of the current potentials |
| 386 | 386 |
PotentialMap *_potential; |
| 387 | 387 |
bool _local_potential; |
| 388 | 388 |
|
| 389 | 389 |
// The source node |
| 390 | 390 |
Node _s; |
| 391 | 391 |
// The target node |
| 392 | 392 |
Node _t; |
| 393 | 393 |
|
| 394 | 394 |
// Container to store the found paths |
| 395 | 395 |
std::vector<Path> _paths; |
| 396 | 396 |
int _path_num; |
| 397 | 397 |
|
| 398 | 398 |
// The pred arc map |
| 399 | 399 |
PredMap _pred; |
| 400 | 400 |
|
| 401 | 401 |
// Data for full init |
| 402 | 402 |
PotentialMap *_init_dist; |
| 403 | 403 |
PredMap *_init_pred; |
| 404 | 404 |
bool _full_init; |
| 405 | 405 |
|
| 406 |
protected: |
|
| 407 |
|
|
| 408 |
Suurballe() {}
|
|
| 409 |
|
|
| 406 | 410 |
public: |
| 407 | 411 |
|
| 408 | 412 |
/// \brief Constructor. |
| 409 | 413 |
/// |
| 410 | 414 |
/// Constructor. |
| 411 | 415 |
/// |
| 412 | 416 |
/// \param graph The digraph the algorithm runs on. |
| 413 | 417 |
/// \param length The length (cost) values of the arcs. |
| 414 | 418 |
Suurballe( const Digraph &graph, |
| 415 | 419 |
const LengthMap &length ) : |
| 416 | 420 |
_graph(graph), _length(length), _flow(0), _local_flow(false), |
| 417 | 421 |
_potential(0), _local_potential(false), _pred(graph), |
| 418 | 422 |
_init_dist(0), _init_pred(0) |
| 419 | 423 |
{}
|
| 420 | 424 |
|
| 421 | 425 |
/// Destructor. |
| 422 | 426 |
~Suurballe() {
|
| 423 | 427 |
if (_local_flow) delete _flow; |
| 424 | 428 |
if (_local_potential) delete _potential; |
| 425 | 429 |
delete _init_dist; |
| 426 | 430 |
delete _init_pred; |
| 427 | 431 |
} |
| 428 | 432 |
|
| 429 | 433 |
/// \brief Set the flow map. |
| 430 | 434 |
/// |
| 431 | 435 |
/// This function sets the flow map. |
| 432 | 436 |
/// If it is not used before calling \ref run() or \ref init(), |
| 433 | 437 |
/// an instance will be allocated automatically. The destructor |
| 434 | 438 |
/// deallocates this automatically allocated map, of course. |
| 435 | 439 |
/// |
| 436 | 440 |
/// The found flow contains only 0 and 1 values, since it is the |
| 437 | 441 |
/// union of the found arc-disjoint paths. |
| 438 | 442 |
/// |
| 439 | 443 |
/// \return <tt>(*this)</tt> |
| 440 | 444 |
Suurballe& flowMap(FlowMap &map) {
|
| 441 | 445 |
if (_local_flow) {
|
| 442 | 446 |
delete _flow; |
| 443 | 447 |
_local_flow = false; |
| 444 | 448 |
} |
| 445 | 449 |
_flow = ↦ |
| 446 | 450 |
return *this; |
| 447 | 451 |
} |
| 448 | 452 |
|
| 449 | 453 |
/// \brief Set the potential map. |
| 450 | 454 |
/// |
| 451 | 455 |
/// This function sets the potential map. |
| 452 | 456 |
/// If it is not used before calling \ref run() or \ref init(), |
| 453 | 457 |
/// an instance will be allocated automatically. The destructor |
| 454 | 458 |
/// deallocates this automatically allocated map, of course. |
| 455 | 459 |
/// |
| 456 | 460 |
/// The node potentials provide the dual solution of the underlying |
| 457 | 461 |
/// \ref min_cost_flow "minimum cost flow problem". |
| 458 | 462 |
/// |
| 459 | 463 |
/// \return <tt>(*this)</tt> |
| 460 | 464 |
Suurballe& potentialMap(PotentialMap &map) {
|
| 461 | 465 |
if (_local_potential) {
|
| 462 | 466 |
delete _potential; |
| 463 | 467 |
_local_potential = false; |
| 464 | 468 |
} |
| 465 | 469 |
_potential = ↦ |
| 466 | 470 |
return *this; |
| 467 | 471 |
} |
| 468 | 472 |
|
| 469 | 473 |
/// \name Execution Control |
| 470 | 474 |
/// The simplest way to execute the algorithm is to call the run() |
| 471 | 475 |
/// function.\n |
| 472 | 476 |
/// If you need to execute the algorithm many times using the same |
| 473 | 477 |
/// source node, then you may call fullInit() once and start() |
| 474 | 478 |
/// for each target node.\n |
| 475 | 479 |
/// If you only need the flow that is the union of the found |
| 476 | 480 |
/// arc-disjoint paths, then you may call findFlow() instead of |
| 477 | 481 |
/// start(). |
| 478 | 482 |
|
| 479 | 483 |
/// @{
|
| 480 | 484 |
|
| 481 | 485 |
/// \brief Run the algorithm. |
| 482 | 486 |
/// |
| 483 | 487 |
/// This function runs the algorithm. |
| 484 | 488 |
/// |
| 485 | 489 |
/// \param s The source node. |
| 486 | 490 |
/// \param t The target node. |
| 487 | 491 |
/// \param k The number of paths to be found. |
| 488 | 492 |
/// |
| 489 | 493 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 490 | 494 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
| 491 | 495 |
/// arc-disjoint paths found. |
| 492 | 496 |
/// |
| 493 | 497 |
/// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is |
| 494 | 498 |
/// just a shortcut of the following code. |
| 495 | 499 |
/// \code |
| 496 | 500 |
/// s.init(s); |
| 497 | 501 |
/// s.start(t, k); |
| 498 | 502 |
/// \endcode |
| 499 | 503 |
int run(const Node& s, const Node& t, int k = 2) {
|
| 500 | 504 |
init(s); |
| 501 | 505 |
start(t, k); |
| 502 | 506 |
return _path_num; |
| 503 | 507 |
} |
| 504 | 508 |
|
| 505 | 509 |
/// \brief Initialize the algorithm. |
| 506 | 510 |
/// |
| 507 | 511 |
/// This function initializes the algorithm with the given source node. |
| 508 | 512 |
/// |
| 509 | 513 |
/// \param s The source node. |
| 510 | 514 |
void init(const Node& s) {
|
| 511 | 515 |
_s = s; |
| 512 | 516 |
|
| 513 | 517 |
// Initialize maps |
| 514 | 518 |
if (!_flow) {
|
| 515 | 519 |
_flow = new FlowMap(_graph); |
| 516 | 520 |
_local_flow = true; |
| 517 | 521 |
} |
| 518 | 522 |
if (!_potential) {
|
| 519 | 523 |
_potential = new PotentialMap(_graph); |
| 520 | 524 |
_local_potential = true; |
| 521 | 525 |
} |
| 522 | 526 |
_full_init = false; |
| 523 | 527 |
} |
| 524 | 528 |
|
| 525 | 529 |
/// \brief Initialize the algorithm and perform Dijkstra. |
| 526 | 530 |
/// |
| 527 | 531 |
/// This function initializes the algorithm and performs a full |
| 528 | 532 |
/// Dijkstra search from the given source node. It makes consecutive |
| 529 | 533 |
/// executions of \ref start() "start(t, k)" faster, since they |
| 530 | 534 |
/// have to perform %Dijkstra only k-1 times. |
| 531 | 535 |
/// |
| 532 | 536 |
/// This initialization is usually worth using instead of \ref init() |
| 533 | 537 |
/// if the algorithm is executed many times using the same source node. |
| 534 | 538 |
/// |
| 535 | 539 |
/// \param s The source node. |
| 536 | 540 |
void fullInit(const Node& s) {
|
| 537 | 541 |
// Initialize maps |
| 538 | 542 |
init(s); |
| 539 | 543 |
if (!_init_dist) {
|
| 540 | 544 |
_init_dist = new PotentialMap(_graph); |
| 541 | 545 |
} |
| 542 | 546 |
if (!_init_pred) {
|
| 543 | 547 |
_init_pred = new PredMap(_graph); |
| 544 | 548 |
} |
| 545 | 549 |
|
| 546 | 550 |
// Run a full Dijkstra |
| 547 | 551 |
typename Dijkstra<Digraph, LengthMap> |
| 548 | 552 |
::template SetStandardHeap<Heap> |
| 549 | 553 |
::template SetDistMap<PotentialMap> |
| 550 | 554 |
::template SetPredMap<PredMap> |
| 551 | 555 |
::Create dijk(_graph, _length); |
| 552 | 556 |
dijk.distMap(*_init_dist).predMap(*_init_pred); |
| 553 | 557 |
dijk.run(s); |
| 554 | 558 |
|
| 555 | 559 |
_full_init = true; |
| 556 | 560 |
} |
| 557 | 561 |
|
| 558 | 562 |
/// \brief Execute the algorithm. |
| 559 | 563 |
/// |
| 560 | 564 |
/// This function executes the algorithm. |
| 561 | 565 |
/// |
| 562 | 566 |
/// \param t The target node. |
| 563 | 567 |
/// \param k The number of paths to be found. |
| 564 | 568 |
/// |
| 565 | 569 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 566 | 570 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
| 567 | 571 |
/// arc-disjoint paths found. |
| 568 | 572 |
/// |
| 569 | 573 |
/// \note Apart from the return value, <tt>s.start(t, k)</tt> is |
| 570 | 574 |
/// just a shortcut of the following code. |
| 571 | 575 |
/// \code |
| 572 | 576 |
/// s.findFlow(t, k); |
| 573 | 577 |
/// s.findPaths(); |
| 574 | 578 |
/// \endcode |
| 575 | 579 |
int start(const Node& t, int k = 2) {
|
| 576 | 580 |
findFlow(t, k); |
| 577 | 581 |
findPaths(); |
| 578 | 582 |
return _path_num; |
| 579 | 583 |
} |
| 580 | 584 |
|
| 581 | 585 |
/// \brief Execute the algorithm to find an optimal flow. |
| 582 | 586 |
/// |
| 583 | 587 |
/// This function executes the successive shortest path algorithm to |
| 584 | 588 |
/// find a minimum cost flow, which is the union of \c k (or less) |
| 585 | 589 |
/// arc-disjoint paths. |
| 586 | 590 |
/// |
| 587 | 591 |
/// \param t The target node. |
| 588 | 592 |
/// \param k The number of paths to be found. |
| 589 | 593 |
/// |
| 590 | 594 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 591 | 595 |
/// the source node to the given node \c t in the digraph. |
| 592 | 596 |
/// Otherwise it returns the number of arc-disjoint paths found. |
| 593 | 597 |
/// |
| 594 | 598 |
/// \pre \ref init() must be called before using this function. |
| 595 | 599 |
int findFlow(const Node& t, int k = 2) {
|
| 596 | 600 |
_t = t; |
| 597 | 601 |
ResidualDijkstra dijkstra(*this); |
| 598 | 602 |
|
| 599 | 603 |
// Initialization |
| 600 | 604 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
| 601 | 605 |
(*_flow)[e] = 0; |
| 602 | 606 |
} |
| 603 | 607 |
if (_full_init) {
|
| 604 | 608 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 605 | 609 |
(*_potential)[n] = (*_init_dist)[n]; |
| 606 | 610 |
} |
| 607 | 611 |
Node u = _t; |
| 608 | 612 |
Arc e; |
| 609 | 613 |
while ((e = (*_init_pred)[u]) != INVALID) {
|
| 610 | 614 |
(*_flow)[e] = 1; |
| 611 | 615 |
u = _graph.source(e); |
| 612 | 616 |
} |
| 613 | 617 |
_path_num = 1; |
| 614 | 618 |
} else {
|
| 615 | 619 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 616 | 620 |
(*_potential)[n] = 0; |
| 617 | 621 |
} |
| 618 | 622 |
_path_num = 0; |
| 619 | 623 |
} |
| 620 | 624 |
|
| 621 | 625 |
// Find shortest paths |
| 622 | 626 |
while (_path_num < k) {
|
| 623 | 627 |
// Run Dijkstra |
| 624 | 628 |
if (!dijkstra.run(_path_num)) break; |
| 625 | 629 |
++_path_num; |
| 626 | 630 |
|
| 627 | 631 |
// Set the flow along the found shortest path |
| 628 | 632 |
Node u = _t; |
| 629 | 633 |
Arc e; |
| 630 | 634 |
while ((e = _pred[u]) != INVALID) {
|
| 631 | 635 |
if (u == _graph.target(e)) {
|
| 632 | 636 |
(*_flow)[e] = 1; |
| 633 | 637 |
u = _graph.source(e); |
| 634 | 638 |
} else {
|
| 635 | 639 |
(*_flow)[e] = 0; |
| 636 | 640 |
u = _graph.target(e); |
| 637 | 641 |
} |
| 638 | 642 |
} |
| 639 | 643 |
} |
| 640 | 644 |
return _path_num; |
| 641 | 645 |
} |
| 642 | 646 |
|
| 643 | 647 |
/// \brief Compute the paths from the flow. |
| 644 | 648 |
/// |
| 645 | 649 |
/// This function computes arc-disjoint paths from the found minimum |
| 646 | 650 |
/// cost flow, which is the union of them. |
| 647 | 651 |
/// |
| 648 | 652 |
/// \pre \ref init() and \ref findFlow() must be called before using |
| 649 | 653 |
/// this function. |
| 650 | 654 |
void findPaths() {
|
| 651 | 655 |
FlowMap res_flow(_graph); |
| 652 | 656 |
for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a]; |
| 653 | 657 |
|
| 654 | 658 |
_paths.clear(); |
| 655 | 659 |
_paths.resize(_path_num); |
| 656 | 660 |
for (int i = 0; i < _path_num; ++i) {
|
| 657 | 661 |
Node n = _s; |
| 658 | 662 |
while (n != _t) {
|
| 659 | 663 |
OutArcIt e(_graph, n); |
| 660 | 664 |
for ( ; res_flow[e] == 0; ++e) ; |
| 661 | 665 |
n = _graph.target(e); |
| 662 | 666 |
_paths[i].addBack(e); |
| 663 | 667 |
res_flow[e] = 0; |
| 664 | 668 |
} |
| 665 | 669 |
} |
| 666 | 670 |
} |
| 667 | 671 |
|
| 668 | 672 |
/// @} |
| 669 | 673 |
|
| 670 | 674 |
/// \name Query Functions |
| 671 | 675 |
/// The results of the algorithm can be obtained using these |
| 672 | 676 |
/// functions. |
| 673 | 677 |
/// \n The algorithm should be executed before using them. |
| 674 | 678 |
|
| 675 | 679 |
/// @{
|
| 676 | 680 |
|
| 677 | 681 |
/// \brief Return the total length of the found paths. |
| 678 | 682 |
/// |
| 679 | 683 |
/// This function returns the total length of the found paths, i.e. |
| 680 | 684 |
/// the total cost of the found flow. |
| 681 | 685 |
/// The complexity of the function is O(e). |
| 682 | 686 |
/// |
| 683 | 687 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 684 | 688 |
/// this function. |
| 685 | 689 |
Length totalLength() const {
|
| 686 | 690 |
Length c = 0; |
| 687 | 691 |
for (ArcIt e(_graph); e != INVALID; ++e) |
| 688 | 692 |
c += (*_flow)[e] * _length[e]; |
| 689 | 693 |
return c; |
| 690 | 694 |
} |
| 691 | 695 |
|
| 692 | 696 |
/// \brief Return the flow value on the given arc. |
| 693 | 697 |
/// |
| 694 | 698 |
/// This function returns the flow value on the given arc. |
| 695 | 699 |
/// It is \c 1 if the arc is involved in one of the found arc-disjoint |
| 696 | 700 |
/// paths, otherwise it is \c 0. |
| 697 | 701 |
/// |
| 698 | 702 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 699 | 703 |
/// this function. |
| 700 | 704 |
int flow(const Arc& arc) const {
|
| 701 | 705 |
return (*_flow)[arc]; |
| 702 | 706 |
} |
| 703 | 707 |
|
| 704 | 708 |
/// \brief Return a const reference to an arc map storing the |
| 705 | 709 |
/// found flow. |
| 706 | 710 |
/// |
| 707 | 711 |
/// This function returns a const reference to an arc map storing |
| 708 | 712 |
/// the flow that is the union of the found arc-disjoint paths. |
| 709 | 713 |
/// |
| 710 | 714 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 711 | 715 |
/// this function. |
| 712 | 716 |
const FlowMap& flowMap() const {
|
| 713 | 717 |
return *_flow; |
| 714 | 718 |
} |
| 715 | 719 |
|
| 716 | 720 |
/// \brief Return the potential of the given node. |
| 717 | 721 |
/// |
| 718 | 722 |
/// This function returns the potential of the given node. |
| 719 | 723 |
/// The node potentials provide the dual solution of the |
| 720 | 724 |
/// underlying \ref min_cost_flow "minimum cost flow problem". |
| 721 | 725 |
/// |
| 722 | 726 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 723 | 727 |
/// this function. |
| 724 | 728 |
Length potential(const Node& node) const {
|
| 725 | 729 |
return (*_potential)[node]; |
| 726 | 730 |
} |
| 727 | 731 |
|
| 728 | 732 |
/// \brief Return a const reference to a node map storing the |
| 729 | 733 |
/// found potentials (the dual solution). |
| 730 | 734 |
/// |
| 731 | 735 |
/// This function returns a const reference to a node map storing |
| 732 | 736 |
/// the found potentials that provide the dual solution of the |
| 733 | 737 |
/// underlying \ref min_cost_flow "minimum cost flow problem". |
| 734 | 738 |
/// |
| 735 | 739 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 736 | 740 |
/// this function. |
| 737 | 741 |
const PotentialMap& potentialMap() const {
|
| 738 | 742 |
return *_potential; |
| 739 | 743 |
} |
| 740 | 744 |
|
| 741 | 745 |
/// \brief Return the number of the found paths. |
| 742 | 746 |
/// |
| 743 | 747 |
/// This function returns the number of the found paths. |
| 744 | 748 |
/// |
| 745 | 749 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 746 | 750 |
/// this function. |
| 747 | 751 |
int pathNum() const {
|
| 748 | 752 |
return _path_num; |
| 749 | 753 |
} |
| 750 | 754 |
|
| 751 | 755 |
/// \brief Return a const reference to the specified path. |
| 752 | 756 |
/// |
| 753 | 757 |
/// This function returns a const reference to the specified path. |
| 754 | 758 |
/// |
| 755 | 759 |
/// \param i The function returns the <tt>i</tt>-th path. |
| 756 | 760 |
/// \c i must be between \c 0 and <tt>%pathNum()-1</tt>. |
| 757 | 761 |
/// |
| 758 | 762 |
/// \pre \ref run() or \ref findPaths() must be called before using |
| 759 | 763 |
/// this function. |
| 760 | 764 |
const Path& path(int i) const {
|
| 761 | 765 |
return _paths[i]; |
| 762 | 766 |
} |
| 763 | 767 |
|
| 764 | 768 |
/// @} |
| 765 | 769 |
|
| 766 | 770 |
}; //class Suurballe |
| 767 | 771 |
|
| 768 | 772 |
///@} |
| 769 | 773 |
|
| 770 | 774 |
} //namespace lemon |
| 771 | 775 |
|
| 772 | 776 |
#endif //LEMON_SUURBALLE_H |
0 comments (0 inline)