0
5
0
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-2010 |
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 |
/// \warning Both |
|
89 |
/// \warning Both \c V and \c C must be signed number types. |
|
90 |
/// \warning All input data (capacities, supply values, and costs) must |
|
90 | 91 |
/// be integer. |
91 | 92 |
/// \warning This algorithm does not support negative costs for such |
92 | 93 |
/// arcs that have infinite upper bound. |
93 | 94 |
#ifdef DOXYGEN |
94 | 95 |
template <typename GR, typename V, typename C, typename TR> |
95 | 96 |
#else |
96 | 97 |
template < typename GR, typename V = int, typename C = V, |
97 | 98 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
98 | 99 |
#endif |
99 | 100 |
class CapacityScaling |
100 | 101 |
{ |
101 | 102 |
public: |
102 | 103 |
|
103 | 104 |
/// The type of the digraph |
104 | 105 |
typedef typename TR::Digraph Digraph; |
105 | 106 |
/// The type of the flow amounts, capacity bounds and supply values |
106 | 107 |
typedef typename TR::Value Value; |
107 | 108 |
/// The type of the arc costs |
108 | 109 |
typedef typename TR::Cost Cost; |
109 | 110 |
|
110 | 111 |
/// The type of the heap used for internal Dijkstra computations |
111 | 112 |
typedef typename TR::Heap Heap; |
112 | 113 |
|
113 | 114 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
114 | 115 |
typedef TR Traits; |
115 | 116 |
|
116 | 117 |
public: |
117 | 118 |
|
118 | 119 |
/// \brief Problem type constants for the \c run() function. |
119 | 120 |
/// |
120 | 121 |
/// Enum type containing the problem type constants that can be |
121 | 122 |
/// returned by the \ref run() function of the algorithm. |
122 | 123 |
enum ProblemType { |
123 | 124 |
/// The problem has no feasible solution (flow). |
124 | 125 |
INFEASIBLE, |
125 | 126 |
/// The problem has optimal solution (i.e. it is feasible and |
126 | 127 |
/// bounded), and the algorithm has found optimal flow and node |
127 | 128 |
/// potentials (primal and dual solutions). |
128 | 129 |
OPTIMAL, |
129 | 130 |
/// The digraph contains an arc of negative cost and infinite |
130 | 131 |
/// upper bound. It means that the objective function is unbounded |
131 | 132 |
/// on that arc, however, note that it could actually be bounded |
132 | 133 |
/// over the feasible flows, but this algroithm cannot handle |
133 | 134 |
/// these cases. |
134 | 135 |
UNBOUNDED |
135 | 136 |
}; |
136 | 137 |
|
137 | 138 |
private: |
138 | 139 |
|
139 | 140 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
140 | 141 |
|
141 | 142 |
typedef std::vector<int> IntVector; |
142 | 143 |
typedef std::vector<Value> ValueVector; |
143 | 144 |
typedef std::vector<Cost> CostVector; |
144 | 145 |
typedef std::vector<char> BoolVector; |
145 | 146 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
146 | 147 |
|
147 | 148 |
private: |
148 | 149 |
|
149 | 150 |
// Data related to the underlying digraph |
150 | 151 |
const GR &_graph; |
151 | 152 |
int _node_num; |
152 | 153 |
int _arc_num; |
153 | 154 |
int _res_arc_num; |
154 | 155 |
int _root; |
155 | 156 |
|
156 | 157 |
// Parameters of the problem |
157 | 158 |
bool _have_lower; |
158 | 159 |
Value _sum_supply; |
159 | 160 |
|
160 | 161 |
// Data structures for storing the digraph |
161 | 162 |
IntNodeMap _node_id; |
162 | 163 |
IntArcMap _arc_idf; |
163 | 164 |
IntArcMap _arc_idb; |
164 | 165 |
IntVector _first_out; |
165 | 166 |
BoolVector _forward; |
166 | 167 |
IntVector _source; |
167 | 168 |
IntVector _target; |
168 | 169 |
IntVector _reverse; |
169 | 170 |
|
170 | 171 |
// Node and arc data |
171 | 172 |
ValueVector _lower; |
172 | 173 |
ValueVector _upper; |
173 | 174 |
CostVector _cost; |
174 | 175 |
ValueVector _supply; |
175 | 176 |
|
176 | 177 |
ValueVector _res_cap; |
177 | 178 |
CostVector _pi; |
178 | 179 |
ValueVector _excess; |
179 | 180 |
IntVector _excess_nodes; |
180 | 181 |
IntVector _deficit_nodes; |
181 | 182 |
|
182 | 183 |
Value _delta; |
183 | 184 |
int _factor; |
184 | 185 |
IntVector _pred; |
185 | 186 |
... | ... |
@@ -20,193 +20,194 @@ |
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 |
/// \warning Both |
|
116 |
/// \warning Both \c V and \c C must be signed number types. |
|
117 |
/// \warning All input data (capacities, supply values, and costs) must |
|
117 | 118 |
/// be integer. |
118 | 119 |
/// \warning This algorithm does not support negative costs for such |
119 | 120 |
/// arcs that have infinite upper bound. |
120 | 121 |
/// |
121 | 122 |
/// \note %CostScaling provides three different internal methods, |
122 | 123 |
/// from which the most efficient one is used by default. |
123 | 124 |
/// For more information, see \ref Method. |
124 | 125 |
#ifdef DOXYGEN |
125 | 126 |
template <typename GR, typename V, typename C, typename TR> |
126 | 127 |
#else |
127 | 128 |
template < typename GR, typename V = int, typename C = V, |
128 | 129 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
129 | 130 |
#endif |
130 | 131 |
class CostScaling |
131 | 132 |
{ |
132 | 133 |
public: |
133 | 134 |
|
134 | 135 |
/// The type of the digraph |
135 | 136 |
typedef typename TR::Digraph Digraph; |
136 | 137 |
/// The type of the flow amounts, capacity bounds and supply values |
137 | 138 |
typedef typename TR::Value Value; |
138 | 139 |
/// The type of the arc costs |
139 | 140 |
typedef typename TR::Cost Cost; |
140 | 141 |
|
141 | 142 |
/// \brief The large cost type |
142 | 143 |
/// |
143 | 144 |
/// The large cost type used for internal computations. |
144 | 145 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
145 | 146 |
/// otherwise it is \c double. |
146 | 147 |
typedef typename TR::LargeCost LargeCost; |
147 | 148 |
|
148 | 149 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
149 | 150 |
typedef TR Traits; |
150 | 151 |
|
151 | 152 |
public: |
152 | 153 |
|
153 | 154 |
/// \brief Problem type constants for the \c run() function. |
154 | 155 |
/// |
155 | 156 |
/// Enum type containing the problem type constants that can be |
156 | 157 |
/// returned by the \ref run() function of the algorithm. |
157 | 158 |
enum ProblemType { |
158 | 159 |
/// The problem has no feasible solution (flow). |
159 | 160 |
INFEASIBLE, |
160 | 161 |
/// The problem has optimal solution (i.e. it is feasible and |
161 | 162 |
/// bounded), and the algorithm has found optimal flow and node |
162 | 163 |
/// potentials (primal and dual solutions). |
163 | 164 |
OPTIMAL, |
164 | 165 |
/// The digraph contains an arc of negative cost and infinite |
165 | 166 |
/// upper bound. It means that the objective function is unbounded |
166 | 167 |
/// on that arc, however, note that it could actually be bounded |
167 | 168 |
/// over the feasible flows, but this algroithm cannot handle |
168 | 169 |
/// these cases. |
169 | 170 |
UNBOUNDED |
170 | 171 |
}; |
171 | 172 |
|
172 | 173 |
/// \brief Constants for selecting the internal method. |
173 | 174 |
/// |
174 | 175 |
/// Enum type containing constants for selecting the internal method |
175 | 176 |
/// for the \ref run() function. |
176 | 177 |
/// |
177 | 178 |
/// \ref CostScaling provides three internal methods that differ mainly |
178 | 179 |
/// in their base operations, which are used in conjunction with the |
179 | 180 |
/// relabel operation. |
180 | 181 |
/// By default, the so called \ref PARTIAL_AUGMENT |
181 | 182 |
/// "Partial Augment-Relabel" method is used, which proved to be |
182 | 183 |
/// the most efficient and the most robust on various test inputs. |
183 | 184 |
/// However, the other methods can be selected using the \ref run() |
184 | 185 |
/// function with the proper parameter. |
185 | 186 |
enum Method { |
186 | 187 |
/// Local push operations are used, i.e. flow is moved only on one |
187 | 188 |
/// admissible arc at once. |
188 | 189 |
PUSH, |
189 | 190 |
/// Augment operations are used, i.e. flow is moved on admissible |
190 | 191 |
/// paths from a node with excess to a node with deficit. |
191 | 192 |
AUGMENT, |
192 | 193 |
/// Partial augment operations are used, i.e. flow is moved on |
193 | 194 |
/// admissible paths started from a node with excess, but the |
194 | 195 |
/// lengths of these paths are limited. This method can be viewed |
195 | 196 |
/// as a combined version of the previous two operations. |
196 | 197 |
PARTIAL_AUGMENT |
197 | 198 |
}; |
198 | 199 |
|
199 | 200 |
private: |
200 | 201 |
|
201 | 202 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
202 | 203 |
|
203 | 204 |
typedef std::vector<int> IntVector; |
204 | 205 |
typedef std::vector<Value> ValueVector; |
205 | 206 |
typedef std::vector<Cost> CostVector; |
206 | 207 |
typedef std::vector<LargeCost> LargeCostVector; |
207 | 208 |
typedef std::vector<char> BoolVector; |
208 | 209 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
209 | 210 |
|
210 | 211 |
private: |
211 | 212 |
|
212 | 213 |
template <typename KT, typename VT> |
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-2010 |
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_CYCLE_CANCELING_H |
20 | 20 |
#define LEMON_CYCLE_CANCELING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Cycle-canceling algorithms for finding a minimum cost flow. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <limits> |
28 | 28 |
|
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
#include <lemon/static_graph.h> |
34 | 34 |
#include <lemon/adaptors.h> |
35 | 35 |
#include <lemon/circulation.h> |
36 | 36 |
#include <lemon/bellman_ford.h> |
37 | 37 |
#include <lemon/howard_mmc.h> |
38 | 38 |
|
39 | 39 |
namespace lemon { |
40 | 40 |
|
41 | 41 |
/// \addtogroup min_cost_flow_algs |
42 | 42 |
/// @{ |
43 | 43 |
|
44 | 44 |
/// \brief Implementation of cycle-canceling algorithms for |
45 | 45 |
/// finding a \ref min_cost_flow "minimum cost flow". |
46 | 46 |
/// |
47 | 47 |
/// \ref CycleCanceling implements three different cycle-canceling |
48 | 48 |
/// algorithms for finding a \ref min_cost_flow "minimum cost flow" |
49 | 49 |
/// \ref amo93networkflows, \ref klein67primal, |
50 | 50 |
/// \ref goldberg89cyclecanceling. |
51 | 51 |
/// The most efficent one (both theoretically and practically) |
52 | 52 |
/// is the \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" algorithm, |
53 | 53 |
/// thus it is the default method. |
54 | 54 |
/// It is strongly polynomial, but in practice, it is typically much |
55 | 55 |
/// slower than the scaling algorithms and NetworkSimplex. |
56 | 56 |
/// |
57 | 57 |
/// Most of the parameters of the problem (except for the digraph) |
58 | 58 |
/// can be given using separate functions, and the algorithm can be |
59 | 59 |
/// executed using the \ref run() function. If some parameters are not |
60 | 60 |
/// specified, then default values will be used. |
61 | 61 |
/// |
62 | 62 |
/// \tparam GR The digraph type the algorithm runs on. |
63 | 63 |
/// \tparam V The number type used for flow amounts, capacity bounds |
64 | 64 |
/// and supply values in the algorithm. By default, it is \c int. |
65 | 65 |
/// \tparam C The number type used for costs and potentials in the |
66 | 66 |
/// algorithm. By default, it is the same as \c V. |
67 | 67 |
/// |
68 |
/// \warning Both |
|
68 |
/// \warning Both \c V and \c C must be signed number types. |
|
69 |
/// \warning All input data (capacities, supply values, and costs) must |
|
69 | 70 |
/// be integer. |
70 | 71 |
/// \warning This algorithm does not support negative costs for such |
71 | 72 |
/// arcs that have infinite upper bound. |
72 | 73 |
/// |
73 | 74 |
/// \note For more information about the three available methods, |
74 | 75 |
/// see \ref Method. |
75 | 76 |
#ifdef DOXYGEN |
76 | 77 |
template <typename GR, typename V, typename C> |
77 | 78 |
#else |
78 | 79 |
template <typename GR, typename V = int, typename C = V> |
79 | 80 |
#endif |
80 | 81 |
class CycleCanceling |
81 | 82 |
{ |
82 | 83 |
public: |
83 | 84 |
|
84 | 85 |
/// The type of the digraph |
85 | 86 |
typedef GR Digraph; |
86 | 87 |
/// The type of the flow amounts, capacity bounds and supply values |
87 | 88 |
typedef V Value; |
88 | 89 |
/// The type of the arc costs |
89 | 90 |
typedef C Cost; |
90 | 91 |
|
91 | 92 |
public: |
92 | 93 |
|
93 | 94 |
/// \brief Problem type constants for the \c run() function. |
94 | 95 |
/// |
95 | 96 |
/// Enum type containing the problem type constants that can be |
96 | 97 |
/// returned by the \ref run() function of the algorithm. |
97 | 98 |
enum ProblemType { |
98 | 99 |
/// The problem has no feasible solution (flow). |
99 | 100 |
INFEASIBLE, |
100 | 101 |
/// The problem has optimal solution (i.e. it is feasible and |
101 | 102 |
/// bounded), and the algorithm has found optimal flow and node |
102 | 103 |
/// potentials (primal and dual solutions). |
103 | 104 |
OPTIMAL, |
104 | 105 |
/// The digraph contains an arc of negative cost and infinite |
105 | 106 |
/// upper bound. It means that the objective function is unbounded |
106 | 107 |
/// on that arc, however, note that it could actually be bounded |
107 | 108 |
/// over the feasible flows, but this algroithm cannot handle |
108 | 109 |
/// these cases. |
109 | 110 |
UNBOUNDED |
110 | 111 |
}; |
111 | 112 |
|
112 | 113 |
/// \brief Constants for selecting the used method. |
113 | 114 |
/// |
114 | 115 |
/// Enum type containing constants for selecting the used method |
115 | 116 |
/// for the \ref run() function. |
116 | 117 |
/// |
117 | 118 |
/// \ref CycleCanceling provides three different cycle-canceling |
118 | 119 |
/// methods. By default, \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" |
119 | 120 |
/// is used, which proved to be the most efficient and the most robust |
120 | 121 |
/// on various test inputs. |
121 | 122 |
/// However, the other methods can be selected using the \ref run() |
122 | 123 |
/// function with the proper parameter. |
123 | 124 |
enum Method { |
124 | 125 |
/// A simple cycle-canceling method, which uses the |
125 | 126 |
/// \ref BellmanFord "Bellman-Ford" algorithm with limited iteration |
126 | 127 |
/// number for detecting negative cycles in the residual network. |
127 | 128 |
SIMPLE_CYCLE_CANCELING, |
128 | 129 |
/// The "Minimum Mean Cycle-Canceling" algorithm, which is a |
129 | 130 |
/// well-known strongly polynomial method |
130 | 131 |
/// \ref goldberg89cyclecanceling. It improves along a |
131 | 132 |
/// \ref min_mean_cycle "minimum mean cycle" in each iteration. |
132 | 133 |
/// Its running time complexity is O(n<sup>2</sup>m<sup>3</sup>log(n)). |
133 | 134 |
MINIMUM_MEAN_CYCLE_CANCELING, |
134 | 135 |
/// The "Cancel And Tighten" algorithm, which can be viewed as an |
135 | 136 |
/// improved version of the previous method |
136 | 137 |
/// \ref goldberg89cyclecanceling. |
137 | 138 |
/// It is faster both in theory and in practice, its running time |
138 | 139 |
/// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)). |
139 | 140 |
CANCEL_AND_TIGHTEN |
140 | 141 |
}; |
141 | 142 |
|
142 | 143 |
private: |
143 | 144 |
|
144 | 145 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
145 | 146 |
|
146 | 147 |
typedef std::vector<int> IntVector; |
147 | 148 |
typedef std::vector<double> DoubleVector; |
148 | 149 |
typedef std::vector<Value> ValueVector; |
149 | 150 |
typedef std::vector<Cost> CostVector; |
150 | 151 |
typedef std::vector<char> BoolVector; |
151 | 152 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
152 | 153 |
|
153 | 154 |
private: |
154 | 155 |
|
155 | 156 |
template <typename KT, typename VT> |
156 | 157 |
class StaticVectorMap { |
157 | 158 |
public: |
158 | 159 |
typedef KT Key; |
159 | 160 |
typedef VT Value; |
160 | 161 |
|
161 | 162 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
162 | 163 |
|
163 | 164 |
const Value& operator[](const Key& key) const { |
164 | 165 |
return _v[StaticDigraph::id(key)]; |
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_KRUSKAL_H |
20 | 20 |
#define LEMON_KRUSKAL_H |
21 | 21 |
|
22 | 22 |
#include <algorithm> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <lemon/unionfind.h> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/bits/traits.h> |
29 | 29 |
|
30 | 30 |
///\ingroup spantree |
31 | 31 |
///\file |
32 | 32 |
///\brief Kruskal's algorithm to compute a minimum cost spanning tree |
33 |
/// |
|
34 |
///Kruskal's algorithm to compute a minimum cost spanning tree. |
|
35 |
/// |
|
36 | 33 |
|
37 | 34 |
namespace lemon { |
38 | 35 |
|
39 | 36 |
namespace _kruskal_bits { |
40 | 37 |
|
41 | 38 |
// Kruskal for directed graphs. |
42 | 39 |
|
43 | 40 |
template <typename Digraph, typename In, typename Out> |
44 | 41 |
typename disable_if<lemon::UndirectedTagIndicator<Digraph>, |
45 | 42 |
typename In::value_type::second_type >::type |
46 | 43 |
kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) { |
47 | 44 |
typedef typename In::value_type::second_type Value; |
48 | 45 |
typedef typename Digraph::template NodeMap<int> IndexMap; |
49 | 46 |
typedef typename Digraph::Node Node; |
50 | 47 |
|
51 | 48 |
IndexMap index(digraph); |
52 | 49 |
UnionFind<IndexMap> uf(index); |
53 | 50 |
for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
54 | 51 |
uf.insert(it); |
55 | 52 |
} |
56 | 53 |
|
57 | 54 |
Value tree_value = 0; |
58 | 55 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
59 | 56 |
if (uf.join(digraph.target(it->first),digraph.source(it->first))) { |
60 | 57 |
out.set(it->first, true); |
61 | 58 |
tree_value += it->second; |
62 | 59 |
} |
63 | 60 |
else { |
64 | 61 |
out.set(it->first, false); |
65 | 62 |
} |
66 | 63 |
} |
67 | 64 |
return tree_value; |
68 | 65 |
} |
69 | 66 |
|
70 | 67 |
// Kruskal for undirected graphs. |
71 | 68 |
|
72 | 69 |
template <typename Graph, typename In, typename Out> |
73 | 70 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>, |
74 | 71 |
typename In::value_type::second_type >::type |
75 | 72 |
kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) { |
76 | 73 |
typedef typename In::value_type::second_type Value; |
77 | 74 |
typedef typename Graph::template NodeMap<int> IndexMap; |
78 | 75 |
typedef typename Graph::Node Node; |
79 | 76 |
|
80 | 77 |
IndexMap index(graph); |
81 | 78 |
UnionFind<IndexMap> uf(index); |
82 | 79 |
for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { |
83 | 80 |
uf.insert(it); |
84 | 81 |
} |
85 | 82 |
|
86 | 83 |
Value tree_value = 0; |
87 | 84 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
88 | 85 |
if (uf.join(graph.u(it->first),graph.v(it->first))) { |
89 | 86 |
out.set(it->first, true); |
90 | 87 |
tree_value += it->second; |
91 | 88 |
} |
92 | 89 |
else { |
93 | 90 |
out.set(it->first, false); |
94 | 91 |
} |
95 | 92 |
} |
96 | 93 |
return tree_value; |
97 | 94 |
} |
98 | 95 |
|
99 | 96 |
|
100 | 97 |
template <typename Sequence> |
101 | 98 |
struct PairComp { |
102 | 99 |
typedef typename Sequence::value_type Value; |
103 | 100 |
bool operator()(const Value& left, const Value& right) { |
104 | 101 |
return left.second < right.second; |
105 | 102 |
} |
106 | 103 |
}; |
107 | 104 |
|
108 | 105 |
template <typename In, typename Enable = void> |
109 | 106 |
struct SequenceInputIndicator { |
110 | 107 |
static const bool value = false; |
111 | 108 |
}; |
112 | 109 |
|
113 | 110 |
template <typename In> |
114 | 111 |
struct SequenceInputIndicator<In, |
115 | 112 |
typename exists<typename In::value_type::first_type>::type> { |
116 | 113 |
static const bool value = true; |
117 | 114 |
}; |
118 | 115 |
|
119 | 116 |
template <typename In, typename Enable = void> |
120 | 117 |
struct MapInputIndicator { |
121 | 118 |
static const bool value = false; |
122 | 119 |
}; |
123 | 120 |
|
124 | 121 |
template <typename In> |
125 | 122 |
struct MapInputIndicator<In, |
126 | 123 |
typename exists<typename In::Value>::type> { |
127 | 124 |
static const bool value = true; |
128 | 125 |
}; |
129 | 126 |
|
130 | 127 |
template <typename In, typename Enable = void> |
131 | 128 |
struct SequenceOutputIndicator { |
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-2010 |
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_NETWORK_SIMPLEX_H |
20 | 20 |
#define LEMON_NETWORK_SIMPLEX_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Network Simplex algorithm for finding a minimum cost flow. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <algorithm> |
30 | 30 |
|
31 | 31 |
#include <lemon/core.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup min_cost_flow_algs |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
/// \brief Implementation of the primal Network Simplex algorithm |
40 | 40 |
/// for finding a \ref min_cost_flow "minimum cost flow". |
41 | 41 |
/// |
42 | 42 |
/// \ref NetworkSimplex implements the primal Network Simplex algorithm |
43 | 43 |
/// for finding a \ref min_cost_flow "minimum cost flow" |
44 | 44 |
/// \ref amo93networkflows, \ref dantzig63linearprog, |
45 | 45 |
/// \ref kellyoneill91netsimplex. |
46 | 46 |
/// This algorithm is a highly efficient specialized version of the |
47 | 47 |
/// linear programming simplex method directly for the minimum cost |
48 | 48 |
/// flow problem. |
49 | 49 |
/// |
50 | 50 |
/// In general, %NetworkSimplex is the fastest implementation available |
51 | 51 |
/// in LEMON for this problem. |
52 | 52 |
/// Moreover, it supports both directions of the supply/demand inequality |
53 | 53 |
/// constraints. For more information, see \ref SupplyType. |
54 | 54 |
/// |
55 | 55 |
/// Most of the parameters of the problem (except for the digraph) |
56 | 56 |
/// can be given using separate functions, and the algorithm can be |
57 | 57 |
/// executed using the \ref run() function. If some parameters are not |
58 | 58 |
/// specified, then default values will be used. |
59 | 59 |
/// |
60 | 60 |
/// \tparam GR The digraph type the algorithm runs on. |
61 | 61 |
/// \tparam V The number type used for flow amounts, capacity bounds |
62 | 62 |
/// and supply values in the algorithm. By default, it is \c int. |
63 | 63 |
/// \tparam C The number type used for costs and potentials in the |
64 | 64 |
/// algorithm. By default, it is the same as \c V. |
65 | 65 |
/// |
66 |
/// \warning Both |
|
66 |
/// \warning Both \c V and \c C must be signed number types. |
|
67 |
/// \warning All input data (capacities, supply values, and costs) must |
|
67 | 68 |
/// be integer. |
68 | 69 |
/// |
69 | 70 |
/// \note %NetworkSimplex provides five different pivot rule |
70 | 71 |
/// implementations, from which the most efficient one is used |
71 | 72 |
/// by default. For more information, see \ref PivotRule. |
72 | 73 |
template <typename GR, typename V = int, typename C = V> |
73 | 74 |
class NetworkSimplex |
74 | 75 |
{ |
75 | 76 |
public: |
76 | 77 |
|
77 | 78 |
/// The type of the flow amounts, capacity bounds and supply values |
78 | 79 |
typedef V Value; |
79 | 80 |
/// The type of the arc costs |
80 | 81 |
typedef C Cost; |
81 | 82 |
|
82 | 83 |
public: |
83 | 84 |
|
84 | 85 |
/// \brief Problem type constants for the \c run() function. |
85 | 86 |
/// |
86 | 87 |
/// Enum type containing the problem type constants that can be |
87 | 88 |
/// returned by the \ref run() function of the algorithm. |
88 | 89 |
enum ProblemType { |
89 | 90 |
/// The problem has no feasible solution (flow). |
90 | 91 |
INFEASIBLE, |
91 | 92 |
/// The problem has optimal solution (i.e. it is feasible and |
92 | 93 |
/// bounded), and the algorithm has found optimal flow and node |
93 | 94 |
/// potentials (primal and dual solutions). |
94 | 95 |
OPTIMAL, |
95 | 96 |
/// The objective function of the problem is unbounded, i.e. |
96 | 97 |
/// there is a directed cycle having negative total cost and |
97 | 98 |
/// infinite upper bound. |
98 | 99 |
UNBOUNDED |
99 | 100 |
}; |
100 | 101 |
|
101 | 102 |
/// \brief Constants for selecting the type of the supply constraints. |
102 | 103 |
/// |
103 | 104 |
/// Enum type containing constants for selecting the supply type, |
104 | 105 |
/// i.e. the direction of the inequalities in the supply/demand |
105 | 106 |
/// constraints of the \ref min_cost_flow "minimum cost flow problem". |
106 | 107 |
/// |
107 | 108 |
/// The default supply type is \c GEQ, the \c LEQ type can be |
108 | 109 |
/// selected using \ref supplyType(). |
109 | 110 |
/// The equality form is a special case of both supply types. |
110 | 111 |
enum SupplyType { |
111 | 112 |
/// This option means that there are <em>"greater or equal"</em> |
112 | 113 |
/// supply/demand constraints in the definition of the problem. |
113 | 114 |
GEQ, |
114 | 115 |
/// This option means that there are <em>"less or equal"</em> |
115 | 116 |
/// supply/demand constraints in the definition of the problem. |
116 | 117 |
LEQ |
117 | 118 |
}; |
118 | 119 |
|
119 | 120 |
/// \brief Constants for selecting the pivot rule. |
120 | 121 |
/// |
121 | 122 |
/// Enum type containing constants for selecting the pivot rule for |
122 | 123 |
/// the \ref run() function. |
123 | 124 |
/// |
124 | 125 |
/// \ref NetworkSimplex provides five different pivot rule |
125 | 126 |
/// implementations that significantly affect the running time |
126 | 127 |
/// of the algorithm. |
127 | 128 |
/// By default, \ref BLOCK_SEARCH "Block Search" is used, which |
128 | 129 |
/// proved to be the most efficient and the most robust on various |
129 | 130 |
/// test inputs. |
130 | 131 |
/// However, another pivot rule can be selected using the \ref run() |
131 | 132 |
/// function with the proper parameter. |
132 | 133 |
enum PivotRule { |
133 | 134 |
|
134 | 135 |
/// The \e First \e Eligible pivot rule. |
135 | 136 |
/// The next eligible arc is selected in a wraparound fashion |
136 | 137 |
/// in every iteration. |
137 | 138 |
FIRST_ELIGIBLE, |
138 | 139 |
|
139 | 140 |
/// The \e Best \e Eligible pivot rule. |
140 | 141 |
/// The best eligible arc is selected in every iteration. |
141 | 142 |
BEST_ELIGIBLE, |
142 | 143 |
|
143 | 144 |
/// The \e Block \e Search pivot rule. |
144 | 145 |
/// A specified number of arcs are examined in every iteration |
145 | 146 |
/// in a wraparound fashion and the best eligible arc is selected |
146 | 147 |
/// from this block. |
147 | 148 |
BLOCK_SEARCH, |
148 | 149 |
|
149 | 150 |
/// The \e Candidate \e List pivot rule. |
150 | 151 |
/// In a major iteration a candidate list is built from eligible arcs |
151 | 152 |
/// in a wraparound fashion and in the following minor iterations |
152 | 153 |
/// the best eligible arc is selected from this list. |
153 | 154 |
CANDIDATE_LIST, |
154 | 155 |
|
155 | 156 |
/// The \e Altering \e Candidate \e List pivot rule. |
156 | 157 |
/// It is a modified version of the Candidate List method. |
157 | 158 |
/// It keeps only the several best eligible arcs from the former |
158 | 159 |
/// candidate list and extends this list in every iteration. |
159 | 160 |
ALTERING_LIST |
160 | 161 |
}; |
161 | 162 |
|
162 | 163 |
private: |
0 comments (0 inline)