| 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-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_PREFLOW_H |
| 20 | 20 |
#define LEMON_PREFLOW_H |
| 21 | 21 |
|
| 22 |
#include <lemon/error.h> |
|
| 23 | 22 |
#include <lemon/tolerance.h> |
| 24 | 23 |
#include <lemon/elevator.h> |
| 25 | 24 |
|
| 26 | 25 |
/// \file |
| 27 | 26 |
/// \ingroup max_flow |
| 28 | 27 |
/// \brief Implementation of the preflow algorithm. |
| 29 | 28 |
|
| 30 | 29 |
namespace lemon {
|
| 31 | 30 |
|
| 32 | 31 |
/// \brief Default traits class of Preflow class. |
| 33 | 32 |
/// |
| 34 | 33 |
/// Default traits class of Preflow class. |
| 35 | 34 |
/// \param _Graph Digraph type. |
| 36 | 35 |
/// \param _CapacityMap Type of capacity map. |
| 37 | 36 |
template <typename _Graph, typename _CapacityMap> |
| 38 | 37 |
struct PreflowDefaultTraits {
|
| 39 | 38 |
|
| 40 | 39 |
/// \brief The digraph type the algorithm runs on. |
| 41 | 40 |
typedef _Graph Digraph; |
| 42 | 41 |
|
| 43 | 42 |
/// \brief The type of the map that stores the arc capacities. |
| 44 | 43 |
/// |
| 45 | 44 |
/// The type of the map that stores the arc capacities. |
| 46 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 47 | 46 |
typedef _CapacityMap CapacityMap; |
| 48 | 47 |
|
| 49 | 48 |
/// \brief The type of the length of the arcs. |
| 50 | 49 |
typedef typename CapacityMap::Value Value; |
| 51 | 50 |
|
| 52 | 51 |
/// \brief The map type that stores the flow values. |
| 53 | 52 |
/// |
| 54 | 53 |
/// The map type that stores the flow values. |
| 55 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 56 | 55 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 57 | 56 |
|
| 58 | 57 |
/// \brief Instantiates a FlowMap. |
| 59 | 58 |
/// |
| 60 | 59 |
/// This function instantiates a \ref FlowMap. |
| 61 | 60 |
/// \param digraph The digraph, to which we would like to define |
| 62 | 61 |
/// the flow map. |
| 63 | 62 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 64 | 63 |
return new FlowMap(digraph); |
| 65 | 64 |
} |
| 66 | 65 |
|
| 67 | 66 |
/// \brief The eleavator type used by Preflow algorithm. |
| 68 | 67 |
/// |
| 69 | 68 |
/// The elevator type used by Preflow algorithm. |
| 70 | 69 |
/// |
| 71 | 70 |
/// \sa Elevator |
| 72 | 71 |
/// \sa LinkedElevator |
| 73 | 72 |
typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator; |
| 74 | 73 |
|
| 75 | 74 |
/// \brief Instantiates an Elevator. |
| 76 | 75 |
/// |
| 77 | 76 |
/// This function instantiates a \ref Elevator. |
| 78 | 77 |
/// \param digraph The digraph, to which we would like to define |
| 79 | 78 |
/// the elevator. |
| 80 | 79 |
/// \param max_level The maximum level of the elevator. |
| 81 | 80 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 82 | 81 |
return new Elevator(digraph, max_level); |
| 83 | 82 |
} |
| 84 | 83 |
|
| 85 | 84 |
/// \brief The tolerance used by the algorithm |
| 86 | 85 |
/// |
| 87 | 86 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 88 | 87 |
typedef lemon::Tolerance<Value> Tolerance; |
| 89 | 88 |
|
| 90 | 89 |
}; |
| 91 | 90 |
|
| 92 | 91 |
|
| 93 | 92 |
/// \ingroup max_flow |
| 94 | 93 |
/// |
| 95 | 94 |
/// \brief %Preflow algorithms class. |
| 96 | 95 |
/// |
| 97 | 96 |
/// This class provides an implementation of the Goldberg's \e |
| 98 | 97 |
/// preflow \e algorithm producing a flow of maximum value in a |
| 99 | 98 |
/// digraph. The preflow algorithms are the fastest known max |
| 100 | 99 |
/// flow algorithms. The current implementation use a mixture of the |
| 101 | 100 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
| 102 | 101 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
|
| 103 | 102 |
/// |
| 104 | 103 |
/// The algorithm consists from two phases. After the first phase |
| 105 | 104 |
/// the maximal flow value and the minimum cut can be obtained. The |
| 106 | 105 |
/// second phase constructs the feasible maximum flow on each arc. |
| 107 | 106 |
/// |
| 108 | 107 |
/// \param _Graph The digraph type the algorithm runs on. |
| 109 | 108 |
/// \param _CapacityMap The flow map type. |
| 110 | 109 |
/// \param _Traits Traits class to set various data types used by |
| 111 | 110 |
/// the algorithm. The default traits class is \ref |
| 112 | 111 |
/// PreflowDefaultTraits. See \ref PreflowDefaultTraits for the |
| 113 | 112 |
/// documentation of a %Preflow traits class. |
| 114 | 113 |
/// |
| 115 | 114 |
///\author Jacint Szabo and Balazs Dezso |
| 116 | 115 |
#ifdef DOXYGEN |
| 117 | 116 |
template <typename _Graph, typename _CapacityMap, typename _Traits> |
| 118 | 117 |
#else |
| 119 | 118 |
template <typename _Graph, |
| 120 | 119 |
typename _CapacityMap = typename _Graph::template ArcMap<int>, |
| 121 | 120 |
typename _Traits = PreflowDefaultTraits<_Graph, _CapacityMap> > |
| 122 | 121 |
#endif |
| 123 | 122 |
class Preflow {
|
| 124 | 123 |
public: |
| 125 | 124 |
|
| 126 | 125 |
typedef _Traits Traits; |
| 127 | 126 |
typedef typename Traits::Digraph Digraph; |
| 128 | 127 |
typedef typename Traits::CapacityMap CapacityMap; |
| 129 | 128 |
typedef typename Traits::Value Value; |
| 130 | 129 |
|
| 131 | 130 |
typedef typename Traits::FlowMap FlowMap; |
| 132 | 131 |
typedef typename Traits::Elevator Elevator; |
| 133 | 132 |
typedef typename Traits::Tolerance Tolerance; |
| 134 | 133 |
|
| 135 |
/// \brief \ref Exception for uninitialized parameters. |
|
| 136 |
/// |
|
| 137 |
/// This error represents problems in the initialization |
|
| 138 |
/// of the parameters of the algorithms. |
|
| 139 |
class UninitializedParameter : public lemon::Exception {
|
|
| 140 |
public: |
|
| 141 |
virtual const char* what() const throw() {
|
|
| 142 |
return "lemon::Preflow::UninitializedParameter"; |
|
| 143 |
} |
|
| 144 |
}; |
|
| 145 |
|
|
| 146 | 134 |
private: |
| 147 | 135 |
|
| 148 | 136 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 149 | 137 |
|
| 150 | 138 |
const Digraph& _graph; |
| 151 | 139 |
const CapacityMap* _capacity; |
| 152 | 140 |
|
| 153 | 141 |
int _node_num; |
| 154 | 142 |
|
| 155 | 143 |
Node _source, _target; |
| 156 | 144 |
|
| 157 | 145 |
FlowMap* _flow; |
| 158 | 146 |
bool _local_flow; |
| 159 | 147 |
|
| 160 | 148 |
Elevator* _level; |
| 161 | 149 |
bool _local_level; |
| 162 | 150 |
|
| 163 | 151 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 164 | 152 |
ExcessMap* _excess; |
| 165 | 153 |
|
| 166 | 154 |
Tolerance _tolerance; |
| 167 | 155 |
|
| 168 | 156 |
bool _phase; |
| 169 | 157 |
|
| 170 | 158 |
|
| 171 | 159 |
void createStructures() {
|
| 172 | 160 |
_node_num = countNodes(_graph); |
| 173 | 161 |
|
| 174 | 162 |
if (!_flow) {
|
| 175 | 163 |
_flow = Traits::createFlowMap(_graph); |
| 176 | 164 |
_local_flow = true; |
| 177 | 165 |
} |
| 178 | 166 |
if (!_level) {
|
| 179 | 167 |
_level = Traits::createElevator(_graph, _node_num); |
| 180 | 168 |
_local_level = true; |
| 181 | 169 |
} |
| 182 | 170 |
if (!_excess) {
|
| 183 | 171 |
_excess = new ExcessMap(_graph); |
| 184 | 172 |
} |
| 185 | 173 |
} |
| 186 | 174 |
|
| 187 | 175 |
void destroyStructures() {
|
| 188 | 176 |
if (_local_flow) {
|
| 189 | 177 |
delete _flow; |
| 190 | 178 |
} |
| 191 | 179 |
if (_local_level) {
|
| 192 | 180 |
delete _level; |
| 193 | 181 |
} |
| 194 | 182 |
if (_excess) {
|
| 195 | 183 |
delete _excess; |
| 196 | 184 |
} |
| 197 | 185 |
} |
| 198 | 186 |
|
| 199 | 187 |
public: |
| 200 | 188 |
|
| 201 | 189 |
typedef Preflow Create; |
| 202 | 190 |
|
| 203 | 191 |
///\name Named template parameters |
| 204 | 192 |
|
| 205 | 193 |
///@{
|
| 206 | 194 |
|
| 207 | 195 |
template <typename _FlowMap> |
| 208 | 196 |
struct DefFlowMapTraits : public Traits {
|
| 209 | 197 |
typedef _FlowMap FlowMap; |
| 210 | 198 |
static FlowMap *createFlowMap(const Digraph&) {
|
| 211 |
|
|
| 199 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
|
| 200 |
return 0; // ignore warnings |
|
| 212 | 201 |
} |
| 213 | 202 |
}; |
| 214 | 203 |
|
| 215 | 204 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 216 | 205 |
/// FlowMap type |
| 217 | 206 |
/// |
| 218 | 207 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
| 219 | 208 |
/// type |
| 220 | 209 |
template <typename _FlowMap> |
| 221 | 210 |
struct DefFlowMap |
| 222 | 211 |
: public Preflow<Digraph, CapacityMap, DefFlowMapTraits<_FlowMap> > {
|
| 223 | 212 |
typedef Preflow<Digraph, CapacityMap, |
| 224 | 213 |
DefFlowMapTraits<_FlowMap> > Create; |
| 225 | 214 |
}; |
| 226 | 215 |
|
| 227 | 216 |
template <typename _Elevator> |
| 228 | 217 |
struct DefElevatorTraits : public Traits {
|
| 229 | 218 |
typedef _Elevator Elevator; |
| 230 | 219 |
static Elevator *createElevator(const Digraph&, int) {
|
| 231 |
|
|
| 220 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
|
| 221 |
return 0; // ignore warnings |
|
| 232 | 222 |
} |
| 233 | 223 |
}; |
| 234 | 224 |
|
| 235 | 225 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 236 | 226 |
/// Elevator type |
| 237 | 227 |
/// |
| 238 | 228 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
| 239 | 229 |
/// type |
| 240 | 230 |
template <typename _Elevator> |
| 241 | 231 |
struct DefElevator |
| 242 | 232 |
: public Preflow<Digraph, CapacityMap, DefElevatorTraits<_Elevator> > {
|
| 243 | 233 |
typedef Preflow<Digraph, CapacityMap, |
| 244 | 234 |
DefElevatorTraits<_Elevator> > Create; |
| 245 | 235 |
}; |
| 246 | 236 |
|
| 247 | 237 |
template <typename _Elevator> |
| 248 | 238 |
struct DefStandardElevatorTraits : public Traits {
|
| 249 | 239 |
typedef _Elevator Elevator; |
| 250 | 240 |
static Elevator *createElevator(const Digraph& digraph, int max_level) {
|
| 251 | 241 |
return new Elevator(digraph, max_level); |
| 252 | 242 |
} |
| 253 | 243 |
}; |
| 254 | 244 |
|
| 255 | 245 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 256 | 246 |
/// Elevator type |
| 257 | 247 |
/// |
| 258 | 248 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
| 259 | 249 |
/// type. The Elevator should be standard constructor interface, ie. |
| 260 | 250 |
/// the digraph and the maximum level should be passed to it. |
| 261 | 251 |
template <typename _Elevator> |
| 262 | 252 |
struct DefStandardElevator |
| 263 | 253 |
: public Preflow<Digraph, CapacityMap, |
| 264 | 254 |
DefStandardElevatorTraits<_Elevator> > {
|
| 265 | 255 |
typedef Preflow<Digraph, CapacityMap, |
| 266 | 256 |
DefStandardElevatorTraits<_Elevator> > Create; |
| 267 | 257 |
}; |
| 268 | 258 |
|
| 269 | 259 |
/// @} |
| 270 | 260 |
|
| 271 | 261 |
protected: |
| 272 | 262 |
|
| 273 | 263 |
Preflow() {}
|
| 274 | 264 |
|
| 275 | 265 |
public: |
| 276 | 266 |
|
| 277 | 267 |
|
| 278 | 268 |
/// \brief The constructor of the class. |
| 279 | 269 |
/// |
| 280 | 270 |
/// The constructor of the class. |
| 281 | 271 |
/// \param digraph The digraph the algorithm runs on. |
| 282 | 272 |
/// \param capacity The capacity of the arcs. |
| 283 | 273 |
/// \param source The source node. |
| 284 | 274 |
/// \param target The target node. |
| 285 | 275 |
Preflow(const Digraph& digraph, const CapacityMap& capacity, |
| 286 | 276 |
Node source, Node target) |
| 287 | 277 |
: _graph(digraph), _capacity(&capacity), |
| 288 | 278 |
_node_num(0), _source(source), _target(target), |
| 289 | 279 |
_flow(0), _local_flow(false), |
| 290 | 280 |
_level(0), _local_level(false), |
| 291 | 281 |
_excess(0), _tolerance(), _phase() {}
|
| 292 | 282 |
|
| 293 | 283 |
/// \brief Destrcutor. |
| 294 | 284 |
/// |
| 295 | 285 |
/// Destructor. |
| 296 | 286 |
~Preflow() {
|
| 297 | 287 |
destroyStructures(); |
| 298 | 288 |
} |
| 299 | 289 |
|
| 300 | 290 |
/// \brief Sets the capacity map. |
| 301 | 291 |
/// |
| 302 | 292 |
/// Sets the capacity map. |
| 303 | 293 |
/// \return \c (*this) |
| 304 | 294 |
Preflow& capacityMap(const CapacityMap& map) {
|
| 305 | 295 |
_capacity = ↦ |
| 306 | 296 |
return *this; |
| 307 | 297 |
} |
| 308 | 298 |
|
| 309 | 299 |
/// \brief Sets the flow map. |
| 310 | 300 |
/// |
| 311 | 301 |
/// Sets the flow map. |
| 312 | 302 |
/// \return \c (*this) |
| 313 | 303 |
Preflow& flowMap(FlowMap& map) {
|
| 314 | 304 |
if (_local_flow) {
|
| 315 | 305 |
delete _flow; |
| 316 | 306 |
_local_flow = false; |
| 317 | 307 |
} |
| 318 | 308 |
_flow = ↦ |
| 319 | 309 |
return *this; |
| 320 | 310 |
} |
| 321 | 311 |
|
| 322 | 312 |
/// \brief Returns the flow map. |
| 323 | 313 |
/// |
| 324 | 314 |
/// \return The flow map. |
| 325 | 315 |
const FlowMap& flowMap() {
|
| 326 | 316 |
return *_flow; |
| 327 | 317 |
} |
0 comments (0 inline)