deba@2440
|
1 |
/* -*- C++ -*-
|
deba@2440
|
2 |
*
|
deba@2440
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@2440
|
4 |
*
|
deba@2440
|
5 |
* Copyright (C) 2003-2007
|
deba@2440
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@2440
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@2440
|
8 |
*
|
deba@2440
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@2440
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@2440
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@2440
|
12 |
*
|
deba@2440
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@2440
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@2440
|
15 |
* purpose.
|
deba@2440
|
16 |
*
|
deba@2440
|
17 |
*/
|
deba@2440
|
18 |
|
deba@2440
|
19 |
#ifndef LEMON_MIN_COST_MAX_FLOW_H
|
deba@2440
|
20 |
#define LEMON_MIN_COST_MAX_FLOW_H
|
deba@2440
|
21 |
|
deba@2440
|
22 |
/// \ingroup min_cost_flow
|
deba@2440
|
23 |
///
|
deba@2440
|
24 |
/// \file
|
deba@2440
|
25 |
/// \brief An efficient algorithm for finding a minimum cost maximum
|
deba@2440
|
26 |
/// flow.
|
deba@2440
|
27 |
|
deba@2440
|
28 |
#include <lemon/preflow.h>
|
deba@2440
|
29 |
#include <lemon/network_simplex.h>
|
deba@2440
|
30 |
#include <lemon/maps.h>
|
deba@2440
|
31 |
|
deba@2440
|
32 |
namespace lemon {
|
deba@2440
|
33 |
|
deba@2440
|
34 |
/// \addtogroup min_cost_flow
|
deba@2440
|
35 |
/// @{
|
deba@2440
|
36 |
|
deba@2440
|
37 |
/// \brief An efficient algorithm for finding a minimum cost maximum
|
deba@2440
|
38 |
/// flow.
|
deba@2440
|
39 |
///
|
deba@2440
|
40 |
/// \ref lemon::MinCostFlow "MinCostMaxFlow" implements an efficient
|
deba@2440
|
41 |
/// algorithm for finding a maximum flow having minimal total cost
|
deba@2440
|
42 |
/// from a given source node to a given target node in a directed
|
deba@2440
|
43 |
/// graph.
|
deba@2440
|
44 |
///
|
deba@2440
|
45 |
/// \note \ref lemon::MinCostMaxFlow "MinCostMaxFlow" uses
|
deba@2440
|
46 |
/// \ref lemon::Preflow "Preflow" algorithm for finding the maximum
|
deba@2440
|
47 |
/// flow value and \ref lemon::NetworkSimplex "NetworkSimplex"
|
deba@2440
|
48 |
/// algorithm for finding a minimum cost flow of that value.
|
deba@2440
|
49 |
///
|
deba@2440
|
50 |
/// \param Graph The directed graph type the algorithm runs on.
|
deba@2440
|
51 |
/// \param CapacityMap The type of the capacity (upper bound) map.
|
deba@2440
|
52 |
/// \param CostMap The type of the cost (length) map.
|
deba@2440
|
53 |
///
|
deba@2440
|
54 |
/// \warning
|
deba@2440
|
55 |
/// - Edge capacities and costs should be nonnegative integers.
|
deba@2440
|
56 |
/// However \c CostMap::Value should be signed type.
|
deba@2440
|
57 |
///
|
deba@2440
|
58 |
/// \author Peter Kovacs
|
deba@2440
|
59 |
|
kpeter@2533
|
60 |
template < typename Graph,
|
kpeter@2533
|
61 |
typename CapacityMap = typename Graph::template EdgeMap<int>,
|
kpeter@2533
|
62 |
typename CostMap = typename Graph::template EdgeMap<int> >
|
deba@2440
|
63 |
class MinCostMaxFlow
|
deba@2440
|
64 |
{
|
deba@2440
|
65 |
typedef typename Graph::Node Node;
|
deba@2440
|
66 |
typedef typename Graph::Edge Edge;
|
deba@2440
|
67 |
|
deba@2440
|
68 |
typedef typename CapacityMap::Value Capacity;
|
deba@2440
|
69 |
typedef typename Graph::template NodeMap<Capacity> SupplyMap;
|
deba@2440
|
70 |
typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
|
deba@2440
|
71 |
CostMap, SupplyMap >
|
deba@2440
|
72 |
MinCostFlowImpl;
|
deba@2440
|
73 |
|
deba@2440
|
74 |
public:
|
deba@2440
|
75 |
|
deba@2440
|
76 |
/// \brief The type of the flow map.
|
deba@2440
|
77 |
typedef typename Graph::template EdgeMap<Capacity> FlowMap;
|
kpeter@2507
|
78 |
typedef typename CostMap::Value Cost;
|
deba@2440
|
79 |
|
deba@2440
|
80 |
private:
|
deba@2440
|
81 |
|
deba@2440
|
82 |
/// \brief The directed graph the algorithm runs on.
|
deba@2440
|
83 |
const Graph &graph;
|
deba@2440
|
84 |
/// \brief The modified capacity map.
|
deba@2440
|
85 |
const CapacityMap &capacity;
|
deba@2440
|
86 |
/// \brief The cost map.
|
deba@2440
|
87 |
const CostMap &cost;
|
deba@2440
|
88 |
/// \brief The source node.
|
deba@2440
|
89 |
Node source;
|
deba@2440
|
90 |
/// \brief The target node.
|
deba@2440
|
91 |
Node target;
|
deba@2440
|
92 |
/// \brief The edge map of the found flow.
|
deba@2440
|
93 |
FlowMap flow;
|
deba@2440
|
94 |
|
deba@2515
|
95 |
typedef Preflow<Graph, CapacityMap> PreflowImpl;
|
deba@2440
|
96 |
/// \brief \ref lemon::Preflow "Preflow" class for finding the
|
deba@2440
|
97 |
/// maximum flow value.
|
deba@2440
|
98 |
PreflowImpl preflow;
|
deba@2440
|
99 |
|
deba@2440
|
100 |
public:
|
deba@2440
|
101 |
|
deba@2440
|
102 |
/// \brief The constructor of the class.
|
deba@2440
|
103 |
///
|
deba@2440
|
104 |
/// The constructor of the class.
|
deba@2440
|
105 |
///
|
deba@2440
|
106 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
107 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
108 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
109 |
/// \param _s The source node.
|
deba@2440
|
110 |
/// \param _t The target node.
|
deba@2440
|
111 |
MinCostMaxFlow( const Graph &_graph,
|
deba@2440
|
112 |
const CapacityMap &_capacity,
|
deba@2440
|
113 |
const CostMap &_cost,
|
deba@2440
|
114 |
Node _s, Node _t ) :
|
deba@2440
|
115 |
graph(_graph), capacity(_capacity), cost(_cost),
|
deba@2440
|
116 |
source(_s), target(_t), flow(_graph),
|
deba@2515
|
117 |
preflow(_graph, _capacity, _s, _t)
|
deba@2440
|
118 |
{}
|
deba@2440
|
119 |
|
deba@2440
|
120 |
/// \brief Returns a const reference to the flow map.
|
deba@2440
|
121 |
///
|
deba@2440
|
122 |
/// Returns a const reference to the flow map.
|
deba@2440
|
123 |
///
|
deba@2440
|
124 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
125 |
const FlowMap& flowMap() const {
|
deba@2440
|
126 |
return flow;
|
deba@2440
|
127 |
}
|
deba@2440
|
128 |
|
deba@2440
|
129 |
/// \brief Returns the total cost of the found flow.
|
deba@2440
|
130 |
///
|
deba@2440
|
131 |
/// Returns the total cost of the found flow. The complexity of the
|
deba@2440
|
132 |
/// function is \f$ O(e) \f$.
|
deba@2440
|
133 |
///
|
deba@2440
|
134 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
135 |
Cost totalCost() const {
|
deba@2440
|
136 |
Cost c = 0;
|
kpeter@2507
|
137 |
for (typename Graph::EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
138 |
c += flow[e] * cost[e];
|
deba@2440
|
139 |
return c;
|
deba@2440
|
140 |
}
|
deba@2440
|
141 |
|
deba@2440
|
142 |
/// \brief Runs the algorithm.
|
deba@2440
|
143 |
void run() {
|
deba@2515
|
144 |
preflow.flowMap(flow);
|
deba@2515
|
145 |
preflow.runMinCut();
|
deba@2440
|
146 |
MinCostFlowImpl mcf_impl( graph, capacity, cost,
|
deba@2440
|
147 |
source, target, preflow.flowValue() );
|
deba@2440
|
148 |
mcf_impl.run();
|
deba@2440
|
149 |
flow = mcf_impl.flowMap();
|
deba@2440
|
150 |
}
|
deba@2440
|
151 |
|
deba@2440
|
152 |
}; //class MinCostMaxFlow
|
deba@2440
|
153 |
|
deba@2440
|
154 |
///@}
|
deba@2440
|
155 |
|
deba@2440
|
156 |
} //namespace lemon
|
deba@2440
|
157 |
|
deba@2440
|
158 |
#endif //LEMON_MIN_COST_MAX_FLOW_H
|