All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
List of all members | Classes | Public Types | Public Member Functions | Public Attributes | Private Member Functions
CostScaling< GR, V, C, TR > Class Template Reference

Detailed Description

template<typename GR, typename V, typename C, typename TR>
class lemon::CostScaling< GR, V, C, TR >

CostScaling implements a cost scaling algorithm that performs push/augment and relabel operations for finding a minimum cost flow [1], [17], [18], [2]. It is a highly efficient primal-dual solution method, which can be viewed as the generalization of the preflow push-relabel algorithm for the maximum flow problem. It is a polynomial algorithm, its running time complexity is $O(n^2m\log(nK))$, where K denotes the maximum arc cost.

In general, NetworkSimplex and CostScaling are the fastest implementations available in LEMON for solving this problem. (For more information, see the module page.)

Most of the parameters of the problem (except for the digraph) can be given using separate functions, and the algorithm can be executed using the run() function. If some parameters are not specified, then default values will be used.

Template Parameters
GRThe digraph type the algorithm runs on.
VThe number type used for flow amounts, capacity bounds and supply values in the algorithm. By default, it is int.
CThe number type used for costs and potentials in the algorithm. By default, it is the same as V.
TRThe traits class that defines various types used by the algorithm. By default, it is CostScalingDefaultTraits<GR, V, C>. In most cases, this parameter should not be set directly, consider to use the named template parameters instead.
Warning
Both V and C must be signed number types.
All input data (capacities, supply values, and costs) must be integer.
This algorithm does not support negative costs for arcs having infinite upper bound.
Note
CostScaling provides three different internal methods, from which the most efficient one is used by default. For more information, see Method.

#include <lemon/cost_scaling.h>

Classes

struct  SetLargeCost
 Named parameter for setting LargeCost type. More...
 

Public Types

enum  ProblemType { INFEASIBLE, OPTIMAL, UNBOUNDED }
 Problem type constants for the run() function. More...
 
enum  Method { PUSH, AUGMENT, PARTIAL_AUGMENT }
 Constants for selecting the internal method. More...
 
typedef TR::Digraph Digraph
 The type of the digraph.
 
typedef TR::Value Value
 The type of the flow amounts, capacity bounds and supply values.
 
typedef TR::Cost Cost
 The type of the arc costs.
 
typedef TR::LargeCost LargeCost
 The large cost type. More...
 
typedef TR Traits
 The traits class of the algorithm.
 

Public Member Functions

 CostScaling (const GR &graph)
 Constructor. More...
 
Parameters

The parameters of the algorithm can be specified using these functions.

template<typename LowerMap >
CostScalinglowerMap (const LowerMap &map)
 Set the lower bounds on the arcs. More...
 
template<typename UpperMap >
CostScalingupperMap (const UpperMap &map)
 Set the upper bounds (capacities) on the arcs. More...
 
template<typename CostMap >
CostScalingcostMap (const CostMap &map)
 Set the costs of the arcs. More...
 
template<typename SupplyMap >
CostScalingsupplyMap (const SupplyMap &map)
 Set the supply values of the nodes. More...
 
CostScalingstSupply (const Node &s, const Node &t, Value k)
 Set single source and target nodes and a supply value. More...
 
Execution control

The algorithm can be executed using run().

ProblemType run (Method method=PARTIAL_AUGMENT, int factor=16)
 Run the algorithm. More...
 
CostScalingresetParams ()
 Reset all the parameters that have been given before. More...
 
CostScalingreset ()
 Reset the internal data structures and all the parameters that have been given before. More...
 
Query Functions

The results of the algorithm can be obtained using these functions.
The run() function must be called before using them.

template<typename Number >
Number totalCost () const
 Return the total cost of the found flow. More...
 
Value flow (const Arc &a) const
 Return the flow on the given arc. More...
 
template<typename FlowMap >
void flowMap (FlowMap &map) const
 Copy the flow values (the primal solution) into the given map. More...
 
Cost potential (const Node &n) const
 Return the potential (dual value) of the given node. More...
 
template<typename PotentialMap >
void potentialMap (PotentialMap &map) const
 Copy the potential values (the dual solution) into the given map. More...
 

Public Attributes

const Value INF
 Constant for infinite upper bounds (capacities). More...
 

Private Member Functions

void startAugment (int max_length)
 Execute the algorithm performing augment and relabel operations.
 
void startPush ()
 Execute the algorithm performing push and relabel operations.
 

Member Typedef Documentation

typedef TR::LargeCost LargeCost

The large cost type used for internal computations. By default, it is long long if the Cost type is integer, otherwise it is double.

Member Enumeration Documentation

Enum type containing the problem type constants that can be returned by the run() function of the algorithm.

Enumerator
INFEASIBLE 

The problem has no feasible solution (flow).

OPTIMAL 

The problem has optimal solution (i.e. it is feasible and bounded), and the algorithm has found optimal flow and node potentials (primal and dual solutions).

UNBOUNDED 

The digraph contains an arc of negative cost and infinite upper bound. It means that the objective function is unbounded on that arc, however, note that it could actually be bounded over the feasible flows, but this algroithm cannot handle these cases.

enum Method

Enum type containing constants for selecting the internal method for the run() function.

CostScaling provides three internal methods that differ mainly in their base operations, which are used in conjunction with the relabel operation. By default, the so called Partial Augment-Relabel method is used, which turned out to be the most efficient and the most robust on various test inputs. However, the other methods can be selected using the run() function with the proper parameter.

Enumerator
PUSH 

Local push operations are used, i.e. flow is moved only on one admissible arc at once.

AUGMENT 

Augment operations are used, i.e. flow is moved on admissible paths from a node with excess to a node with deficit.

PARTIAL_AUGMENT 

Partial augment operations are used, i.e. flow is moved on admissible paths started from a node with excess, but the lengths of these paths are limited. This method can be viewed as a combined version of the previous two operations.

Constructor & Destructor Documentation

CostScaling ( const GR &  graph)
inline

The constructor of the class.

Parameters
graphThe digraph the algorithm runs on.

Member Function Documentation

CostScaling& lowerMap ( const LowerMap &  map)
inline

This function sets the lower bounds on the arcs. If it is not used before calling run(), the lower bounds will be set to zero on all arcs.

Parameters
mapAn arc map storing the lower bounds. Its Value type must be convertible to the Value type of the algorithm.
Returns
(*this)
CostScaling& upperMap ( const UpperMap &  map)
inline

This function sets the upper bounds (capacities) on the arcs. If it is not used before calling run(), the upper bounds will be set to INF on all arcs (i.e. the flow value will be unbounded from above).

Parameters
mapAn arc map storing the upper bounds. Its Value type must be convertible to the Value type of the algorithm.
Returns
(*this)
CostScaling& costMap ( const CostMap &  map)
inline

This function sets the costs of the arcs. If it is not used before calling run(), the costs will be set to 1 on all arcs.

Parameters
mapAn arc map storing the costs. Its Value type must be convertible to the Cost type of the algorithm.
Returns
(*this)
CostScaling& supplyMap ( const SupplyMap &  map)
inline

This function sets the supply values of the nodes. If neither this function nor stSupply() is used before calling run(), the supply of each node will be set to zero.

Parameters
mapA node map storing the supply values. Its Value type must be convertible to the Value type of the algorithm.
Returns
(*this)
CostScaling& stSupply ( const Node &  s,
const Node &  t,
Value  k 
)
inline

This function sets a single source node and a single target node and the required flow value. If neither this function nor supplyMap() is used before calling run(), the supply of each node will be set to zero.

Using this function has the same effect as using supplyMap() with a map in which k is assigned to s, -k is assigned to t and all other nodes have zero supply value.

Parameters
sThe source node.
tThe target node.
kThe required amount of flow from node s to node t (i.e. the supply of s and the demand of t).
Returns
(*this)
ProblemType run ( Method  method = PARTIAL_AUGMENT,
int  factor = 16 
)
inline

This function runs the algorithm. The paramters can be specified using functions lowerMap(), upperMap(), costMap(), supplyMap(), stSupply(). For example,

* CostScaling<ListDigraph> cs(graph);
* cs.lowerMap(lower).upperMap(upper).costMap(cost)
* .supplyMap(sup).run();
*

This function can be called more than once. All the given parameters are kept for the next call, unless resetParams() or reset() is used, thus only the modified parameters have to be set again. If the underlying digraph was also modified after the construction of the class (or the last reset() call), then the reset() function must be called.

Parameters
methodThe internal method that will be used in the algorithm. For more information, see Method.
factorThe cost scaling factor. It must be at least two.
Returns
INFEASIBLE if no feasible flow exists,
OPTIMAL if the problem has optimal solution (i.e. it is feasible and bounded), and the algorithm has found optimal flow and node potentials (primal and dual solutions),
UNBOUNDED if the digraph contains an arc of negative cost and infinite upper bound. It means that the objective function is unbounded on that arc, however, note that it could actually be bounded over the feasible flows, but this algroithm cannot handle these cases.
See Also
ProblemType, Method
resetParams(), reset()
CostScaling& resetParams ( )
inline

This function resets all the paramaters that have been given before using functions lowerMap(), upperMap(), costMap(), supplyMap(), stSupply().

It is useful for multiple run() calls. Basically, all the given parameters are kept for the next run() call, unless resetParams() or reset() is used. If the underlying digraph was also modified after the construction of the class or the last reset() call, then the reset() function must be used, otherwise resetParams() is sufficient.

For example,

* CostScaling<ListDigraph> cs(graph);
*
* // First run
* cs.lowerMap(lower).upperMap(upper).costMap(cost)
* .supplyMap(sup).run();
*
* // Run again with modified cost map (resetParams() is not called,
* // so only the cost map have to be set again)
* cost[e] += 100;
* cs.costMap(cost).run();
*
* // Run again from scratch using resetParams()
* // (the lower bounds will be set to zero on all arcs)
* cs.resetParams();
* cs.upperMap(capacity).costMap(cost)
* .supplyMap(sup).run();
*
Returns
(*this)
See Also
reset(), run()
CostScaling& reset ( )
inline

This function resets the internal data structures and all the paramaters that have been given before using functions lowerMap(), upperMap(), costMap(), supplyMap(), stSupply().

It is useful for multiple run() calls. By default, all the given parameters are kept for the next run() call, unless resetParams() or reset() is used. If the underlying digraph was also modified after the construction of the class or the last reset() call, then the reset() function must be used, otherwise resetParams() is sufficient.

See resetParams() for examples.

Returns
(*this)
See Also
resetParams(), run()
Number totalCost ( ) const
inline

This function returns the total cost of the found flow. Its complexity is O(m).

Note
The return type of the function can be specified as a template parameter. For example,
* cs.totalCost<double>();
*
It is useful if the total cost cannot be stored in the Cost type of the algorithm, which is the default return type of the function.
Precondition
run() must be called before using this function.
Value flow ( const Arc &  a) const
inline

This function returns the flow on the given arc.

Precondition
run() must be called before using this function.
void flowMap ( FlowMap &  map) const
inline

This function copies the flow value on each arc into the given map. The Value type of the algorithm must be convertible to the Value type of the map.

Precondition
run() must be called before using this function.
Cost potential ( const Node &  n) const
inline

This function returns the potential (dual value) of the given node.

Precondition
run() must be called before using this function.
void potentialMap ( PotentialMap &  map) const
inline

This function copies the potential (dual value) of each node into the given map. The Cost type of the algorithm must be convertible to the Value type of the map.

Precondition
run() must be called before using this function.

Member Data Documentation

const Value INF

Constant for infinite upper bounds (capacities). It is std::numeric_limits<Value>::infinity() if available, std::numeric_limits<Value>::max() otherwise.