| 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 |
/// and it must have an \c |
|
| 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 |
#ifdef DOXYGEN |
| 110 | 110 |
template <typename GR, typename LEN, typename TR> |
| 111 | 111 |
#else |
| 112 | 112 |
template < typename GR, |
| 113 | 113 |
typename LEN = typename GR::template ArcMap<int>, |
| 114 | 114 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
| 115 | 115 |
#endif |
| 116 | 116 |
class HartmannOrlin |
| 117 | 117 |
{
|
| 118 | 118 |
public: |
| 119 | 119 |
|
| 120 | 120 |
/// The type of the digraph |
| 121 | 121 |
typedef typename TR::Digraph Digraph; |
| 122 | 122 |
/// The type of the length map |
| 123 | 123 |
typedef typename TR::LengthMap LengthMap; |
| 124 | 124 |
/// The type of the arc lengths |
| 125 | 125 |
typedef typename TR::Value Value; |
| 126 | 126 |
|
| 127 | 127 |
/// \brief The large value type |
| 128 | 128 |
/// |
| 129 | 129 |
/// The large value type used for internal computations. |
| 130 | 130 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 131 | 131 |
/// it is \c long \c long if the \c Value type is integer, |
| 132 | 132 |
/// otherwise it is \c double. |
| 133 | 133 |
typedef typename TR::LargeValue LargeValue; |
| 134 | 134 |
|
| 135 | 135 |
/// The tolerance type |
| 136 | 136 |
typedef typename TR::Tolerance Tolerance; |
| 137 | 137 |
|
| 138 | 138 |
/// \brief The path type of the found cycles |
| 139 | 139 |
/// |
| 140 | 140 |
/// The path type of the found cycles. |
| 141 | 141 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 142 | 142 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 143 | 143 |
typedef typename TR::Path Path; |
| 144 | 144 |
|
| 145 | 145 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
| 146 | 146 |
typedef TR Traits; |
| 147 | 147 |
|
| 148 | 148 |
private: |
| 149 | 149 |
|
| 150 | 150 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 151 | 151 |
|
| 152 | 152 |
// Data sturcture for path data |
| 153 | 153 |
struct PathData |
| 154 | 154 |
{
|
| 155 | 155 |
LargeValue dist; |
| 156 | 156 |
Arc pred; |
| 157 | 157 |
PathData(LargeValue d, Arc p = INVALID) : |
| 158 | 158 |
dist(d), pred(p) {}
|
| 159 | 159 |
}; |
| 160 | 160 |
|
| 161 | 161 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 162 | 162 |
PathDataNodeMap; |
| 163 | 163 |
|
| 164 | 164 |
private: |
| 165 | 165 |
|
| 166 | 166 |
// The digraph the algorithm runs on |
| 167 | 167 |
const Digraph &_gr; |
| 168 | 168 |
// The length of the arcs |
| 169 | 169 |
const LengthMap &_length; |
| 170 | 170 |
|
| 171 | 171 |
// Data for storing the strongly connected components |
| 172 | 172 |
int _comp_num; |
| 173 | 173 |
typename Digraph::template NodeMap<int> _comp; |
| 174 | 174 |
std::vector<std::vector<Node> > _comp_nodes; |
| 175 | 175 |
std::vector<Node>* _nodes; |
| 176 | 176 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 177 | 177 |
|
| 178 | 178 |
// Data for the found cycles |
| 179 | 179 |
bool _curr_found, _best_found; |
| 180 | 180 |
LargeValue _curr_length, _best_length; |
| 181 | 181 |
int _curr_size, _best_size; |
| 182 | 182 |
Node _curr_node, _best_node; |
| 183 | 183 |
int _curr_level, _best_level; |
| 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 HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
|
| 217 | 217 |
typedef HartmannOrlin<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 HartmannOrlin<GR, LEN, SetPathTraits<T> > {
|
| 235 | 235 |
typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create; |
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
/// @} |
| 239 | 239 |
|
| 240 | 240 |
public: |
| 241 | 241 |
|
| 242 | 242 |
/// \brief Constructor. |
| 243 | 243 |
/// |
| 244 | 244 |
/// The constructor of the class. |
| 245 | 245 |
/// |
| 246 | 246 |
/// \param digraph The digraph the algorithm runs on. |
| 247 | 247 |
/// \param length The lengths (costs) of the arcs. |
| 248 | 248 |
HartmannOrlin( const Digraph &digraph, |
| 249 | 249 |
const LengthMap &length ) : |
| 250 | 250 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
| 251 | 251 |
_best_found(false), _best_length(0), _best_size(1), |
| 252 | 252 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
| 253 | 253 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 254 | 254 |
std::numeric_limits<LargeValue>::infinity() : |
| 255 | 255 |
std::numeric_limits<LargeValue>::max()) |
| 256 | 256 |
{}
|
| 257 | 257 |
|
| 258 | 258 |
/// Destructor. |
| 259 | 259 |
~HartmannOrlin() {
|
| 260 | 260 |
if (_local_path) delete _cycle_path; |
| 261 | 261 |
} |
| 262 | 262 |
|
| 263 | 263 |
/// \brief Set the path structure for storing the found cycle. |
| 264 | 264 |
/// |
| 265 | 265 |
/// This function sets an external path structure for storing the |
| 266 | 266 |
/// found cycle. |
| 267 | 267 |
/// |
| 268 | 268 |
/// If you don't call this function before calling \ref run() or |
| 269 | 269 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 270 | 270 |
/// structure. The destuctor deallocates this automatically |
| 271 | 271 |
/// allocated object, of course. |
| 272 | 272 |
/// |
| 273 | 273 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
| 274 | 274 |
/// "addFront()" function of the given path structure. |
| 275 | 275 |
/// |
| 276 | 276 |
/// \return <tt>(*this)</tt> |
| 277 | 277 |
HartmannOrlin& cycle(Path &path) {
|
| 278 | 278 |
if (_local_path) {
|
| 279 | 279 |
delete _cycle_path; |
| 280 | 280 |
_local_path = false; |
| 281 | 281 |
} |
| 282 | 282 |
_cycle_path = &path; |
| 283 | 283 |
return *this; |
| 284 | 284 |
} |
| 285 | 285 |
|
| 286 | 286 |
/// \brief Set the tolerance used by the algorithm. |
| 287 | 287 |
/// |
| 288 | 288 |
/// This function sets the tolerance object used by the algorithm. |
| 289 | 289 |
/// |
| 290 | 290 |
/// \return <tt>(*this)</tt> |
| 291 | 291 |
HartmannOrlin& tolerance(const Tolerance& tolerance) {
|
| 292 | 292 |
_tolerance = tolerance; |
| 293 | 293 |
return *this; |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 | 296 |
/// \brief Return a const reference to the tolerance. |
| 297 | 297 |
/// |
| 298 | 298 |
/// This function returns a const reference to the tolerance object |
| 299 | 299 |
/// used by the algorithm. |
| 300 | 300 |
const Tolerance& tolerance() const {
|
| 301 | 301 |
return _tolerance; |
| 302 | 302 |
} |
| 303 | 303 |
|
| 304 | 304 |
/// \name Execution control |
| 305 | 305 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 306 | 306 |
/// function.\n |
| 307 | 307 |
/// If you only need the minimum mean length, you may call |
| 308 | 308 |
/// \ref findMinMean(). |
| 309 | 309 |
|
| 310 | 310 |
/// @{
|
| 311 | 311 |
|
| 312 | 312 |
/// \brief Run the algorithm. |
| 313 | 313 |
/// |
| 314 | 314 |
/// This function runs the algorithm. |
| 315 | 315 |
/// It can be called more than once (e.g. if the underlying digraph |
| 316 | 316 |
/// and/or the arc lengths have been modified). |
| 317 | 317 |
/// |
| 318 | 318 |
/// \return \c true if a directed cycle exists in the digraph. |
| 319 | 319 |
/// |
| 320 | 320 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 321 | 321 |
/// \code |
| 322 | 322 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 323 | 323 |
/// \endcode |
| 324 | 324 |
bool run() {
|
| 325 | 325 |
return findMinMean() && findCycle(); |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
/// \brief Find the minimum cycle mean. |
| 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 |
/// and it must have an \c |
|
| 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 |
#ifdef DOXYGEN |
| 108 | 108 |
template <typename GR, typename LEN, typename TR> |
| 109 | 109 |
#else |
| 110 | 110 |
template < typename GR, |
| 111 | 111 |
typename LEN = typename GR::template ArcMap<int>, |
| 112 | 112 |
typename TR = KarpDefaultTraits<GR, LEN> > |
| 113 | 113 |
#endif |
| 114 | 114 |
class Karp |
| 115 | 115 |
{
|
| 116 | 116 |
public: |
| 117 | 117 |
|
| 118 | 118 |
/// The type of the digraph |
| 119 | 119 |
typedef typename TR::Digraph Digraph; |
| 120 | 120 |
/// The type of the length map |
| 121 | 121 |
typedef typename TR::LengthMap LengthMap; |
| 122 | 122 |
/// The type of the arc lengths |
| 123 | 123 |
typedef typename TR::Value Value; |
| 124 | 124 |
|
| 125 | 125 |
/// \brief The large value type |
| 126 | 126 |
/// |
| 127 | 127 |
/// The large value type used for internal computations. |
| 128 | 128 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 129 | 129 |
/// it is \c long \c long if the \c Value type is integer, |
| 130 | 130 |
/// otherwise it is \c double. |
| 131 | 131 |
typedef typename TR::LargeValue LargeValue; |
| 132 | 132 |
|
| 133 | 133 |
/// The tolerance type |
| 134 | 134 |
typedef typename TR::Tolerance Tolerance; |
| 135 | 135 |
|
| 136 | 136 |
/// \brief The path type of the found cycles |
| 137 | 137 |
/// |
| 138 | 138 |
/// The path type of the found cycles. |
| 139 | 139 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 140 | 140 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 141 | 141 |
typedef typename TR::Path Path; |
| 142 | 142 |
|
| 143 | 143 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
| 144 | 144 |
typedef TR Traits; |
| 145 | 145 |
|
| 146 | 146 |
private: |
| 147 | 147 |
|
| 148 | 148 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 149 | 149 |
|
| 150 | 150 |
// Data sturcture for path data |
| 151 | 151 |
struct PathData |
| 152 | 152 |
{
|
| 153 | 153 |
LargeValue dist; |
| 154 | 154 |
Arc pred; |
| 155 | 155 |
PathData(LargeValue d, Arc p = INVALID) : |
| 156 | 156 |
dist(d), pred(p) {}
|
| 157 | 157 |
}; |
| 158 | 158 |
|
| 159 | 159 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 160 | 160 |
PathDataNodeMap; |
| 161 | 161 |
|
| 162 | 162 |
private: |
| 163 | 163 |
|
| 164 | 164 |
// The digraph the algorithm runs on |
| 165 | 165 |
const Digraph &_gr; |
| 166 | 166 |
// The length of the arcs |
| 167 | 167 |
const LengthMap &_length; |
| 168 | 168 |
|
| 169 | 169 |
// Data for storing the strongly connected components |
| 170 | 170 |
int _comp_num; |
| 171 | 171 |
typename Digraph::template NodeMap<int> _comp; |
| 172 | 172 |
std::vector<std::vector<Node> > _comp_nodes; |
| 173 | 173 |
std::vector<Node>* _nodes; |
| 174 | 174 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 175 | 175 |
|
| 176 | 176 |
// Data for the found cycle |
| 177 | 177 |
LargeValue _cycle_length; |
| 178 | 178 |
int _cycle_size; |
| 179 | 179 |
Node _cycle_node; |
| 180 | 180 |
|
| 181 | 181 |
Path *_cycle_path; |
| 182 | 182 |
bool _local_path; |
| 183 | 183 |
|
| 184 | 184 |
// Node map for storing path data |
| 185 | 185 |
PathDataNodeMap _data; |
| 186 | 186 |
// The processed nodes in the last round |
| 187 | 187 |
std::vector<Node> _process; |
| 188 | 188 |
|
| 189 | 189 |
Tolerance _tolerance; |
| 190 | 190 |
|
| 191 | 191 |
// Infinite constant |
| 192 | 192 |
const LargeValue INF; |
| 193 | 193 |
|
| 194 | 194 |
public: |
| 195 | 195 |
|
| 196 | 196 |
/// \name Named Template Parameters |
| 197 | 197 |
/// @{
|
| 198 | 198 |
|
| 199 | 199 |
template <typename T> |
| 200 | 200 |
struct SetLargeValueTraits : public Traits {
|
| 201 | 201 |
typedef T LargeValue; |
| 202 | 202 |
typedef lemon::Tolerance<T> Tolerance; |
| 203 | 203 |
}; |
| 204 | 204 |
|
| 205 | 205 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 206 | 206 |
/// \c LargeValue type. |
| 207 | 207 |
/// |
| 208 | 208 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 209 | 209 |
/// type. It is used for internal computations in the algorithm. |
| 210 | 210 |
template <typename T> |
| 211 | 211 |
struct SetLargeValue |
| 212 | 212 |
: public Karp<GR, LEN, SetLargeValueTraits<T> > {
|
| 213 | 213 |
typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create; |
| 214 | 214 |
}; |
| 215 | 215 |
|
| 216 | 216 |
template <typename T> |
| 217 | 217 |
struct SetPathTraits : public Traits {
|
| 218 | 218 |
typedef T Path; |
| 219 | 219 |
}; |
| 220 | 220 |
|
| 221 | 221 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 222 | 222 |
/// \c %Path type. |
| 223 | 223 |
/// |
| 224 | 224 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 225 | 225 |
/// type of the found cycles. |
| 226 | 226 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 227 | 227 |
/// and it must have an \c addFront() function. |
| 228 | 228 |
template <typename T> |
| 229 | 229 |
struct SetPath |
| 230 | 230 |
: public Karp<GR, LEN, SetPathTraits<T> > {
|
| 231 | 231 |
typedef Karp<GR, LEN, SetPathTraits<T> > Create; |
| 232 | 232 |
}; |
| 233 | 233 |
|
| 234 | 234 |
/// @} |
| 235 | 235 |
|
| 236 | 236 |
public: |
| 237 | 237 |
|
| 238 | 238 |
/// \brief Constructor. |
| 239 | 239 |
/// |
| 240 | 240 |
/// The constructor of the class. |
| 241 | 241 |
/// |
| 242 | 242 |
/// \param digraph The digraph the algorithm runs on. |
| 243 | 243 |
/// \param length The lengths (costs) of the arcs. |
| 244 | 244 |
Karp( const Digraph &digraph, |
| 245 | 245 |
const LengthMap &length ) : |
| 246 | 246 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
| 247 | 247 |
_cycle_length(0), _cycle_size(1), _cycle_node(INVALID), |
| 248 | 248 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
| 249 | 249 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 250 | 250 |
std::numeric_limits<LargeValue>::infinity() : |
| 251 | 251 |
std::numeric_limits<LargeValue>::max()) |
| 252 | 252 |
{}
|
| 253 | 253 |
|
| 254 | 254 |
/// Destructor. |
| 255 | 255 |
~Karp() {
|
| 256 | 256 |
if (_local_path) delete _cycle_path; |
| 257 | 257 |
} |
| 258 | 258 |
|
| 259 | 259 |
/// \brief Set the path structure for storing the found cycle. |
| 260 | 260 |
/// |
| 261 | 261 |
/// This function sets an external path structure for storing the |
| 262 | 262 |
/// found cycle. |
| 263 | 263 |
/// |
| 264 | 264 |
/// If you don't call this function before calling \ref run() or |
| 265 | 265 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 266 | 266 |
/// structure. The destuctor deallocates this automatically |
| 267 | 267 |
/// allocated object, of course. |
| 268 | 268 |
/// |
| 269 | 269 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
| 270 | 270 |
/// "addFront()" function of the given path structure. |
| 271 | 271 |
/// |
| 272 | 272 |
/// \return <tt>(*this)</tt> |
| 273 | 273 |
Karp& cycle(Path &path) {
|
| 274 | 274 |
if (_local_path) {
|
| 275 | 275 |
delete _cycle_path; |
| 276 | 276 |
_local_path = false; |
| 277 | 277 |
} |
| 278 | 278 |
_cycle_path = &path; |
| 279 | 279 |
return *this; |
| 280 | 280 |
} |
| 281 | 281 |
|
| 282 | 282 |
/// \brief Set the tolerance used by the algorithm. |
| 283 | 283 |
/// |
| 284 | 284 |
/// This function sets the tolerance object used by the algorithm. |
| 285 | 285 |
/// |
| 286 | 286 |
/// \return <tt>(*this)</tt> |
| 287 | 287 |
Karp& tolerance(const Tolerance& tolerance) {
|
| 288 | 288 |
_tolerance = tolerance; |
| 289 | 289 |
return *this; |
| 290 | 290 |
} |
| 291 | 291 |
|
| 292 | 292 |
/// \brief Return a const reference to the tolerance. |
| 293 | 293 |
/// |
| 294 | 294 |
/// This function returns a const reference to the tolerance object |
| 295 | 295 |
/// used by the algorithm. |
| 296 | 296 |
const Tolerance& tolerance() const {
|
| 297 | 297 |
return _tolerance; |
| 298 | 298 |
} |
| 299 | 299 |
|
| 300 | 300 |
/// \name Execution control |
| 301 | 301 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 302 | 302 |
/// function.\n |
| 303 | 303 |
/// If you only need the minimum mean length, you may call |
| 304 | 304 |
/// \ref findMinMean(). |
| 305 | 305 |
|
| 306 | 306 |
/// @{
|
| 307 | 307 |
|
| 308 | 308 |
/// \brief Run the algorithm. |
| 309 | 309 |
/// |
| 310 | 310 |
/// This function runs the algorithm. |
| 311 | 311 |
/// It can be called more than once (e.g. if the underlying digraph |
| 312 | 312 |
/// and/or the arc lengths have been modified). |
| 313 | 313 |
/// |
| 314 | 314 |
/// \return \c true if a directed cycle exists in the digraph. |
| 315 | 315 |
/// |
| 316 | 316 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 317 | 317 |
/// \code |
| 318 | 318 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 319 | 319 |
/// \endcode |
| 320 | 320 |
bool run() {
|
| 321 | 321 |
return findMinMean() && findCycle(); |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
/// \brief Find the minimum cycle mean. |
| 325 | 325 |
/// |
| 326 | 326 |
/// This function finds the minimum mean length of the directed |
| 327 | 327 |
/// cycles in the digraph. |
| 328 | 328 |
/// |
0 comments (0 inline)