2 #ifndef HUGO_MINCOSTFLOWS_H
3 #define HUGO_MINCOSTFLOWS_H
7 ///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost
10 #include <hugo/dijkstra.h>
11 #include <hugo/graph_wrapper.h>
12 #include <hugo/maps.h>
14 #include <for_each_macros.h>
21 ///\brief Implementation of an algorithm for finding a flow of value \c k
22 ///(for small values of \c k) having minimal total cost between 2 nodes
25 /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
26 /// an algorithm for finding a flow of value \c k
27 ///(for small values of \c k) having minimal total cost
28 /// from a given source node to a given target node in an
29 /// edge-weighted directed graph having nonnegative integer capacities.
30 /// The range of the length (weight) function is nonnegative reals but
31 /// the range of capacity function is the set of nonnegative integers.
32 /// It is not a polinomial time algorithm for counting the minimum cost
33 /// maximal flow, since it counts the minimum cost flow for every value 0..M
34 /// where \c M is the value of the maximal flow.
36 ///\author Attila Bernath
37 template <typename Graph, typename LengthMap, typename CapacityMap>
40 typedef typename LengthMap::ValueType Length;
42 //Warning: this should be integer type
43 typedef typename CapacityMap::ValueType Capacity;
45 typedef typename Graph::Node Node;
46 typedef typename Graph::NodeIt NodeIt;
47 typedef typename Graph::Edge Edge;
48 typedef typename Graph::OutEdgeIt OutEdgeIt;
49 typedef typename Graph::template EdgeMap<int> EdgeIntMap;
51 // typedef ConstMap<Edge,int> ConstMap;
53 typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
54 typedef typename ResGraphType::Edge ResGraphEdge;
57 //typedef typename ResGraphType::template NodeMap<Length> NodeMap;
58 typedef typename Graph::template NodeMap<Length> NodeMap;
59 const ResGraphType& G;
60 // const EdgeIntMap& rev;
64 typedef typename LengthMap::KeyType KeyType;
65 typedef typename LengthMap::ValueType ValueType;
67 ValueType operator[](typename ResGraphType::Edge e) const {
69 return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
71 return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
74 ModLengthMap(const ResGraphType& _G,
75 const LengthMap &o, const NodeMap &p) :
76 G(_G), /*rev(_rev),*/ ol(o), pot(p){};
84 const LengthMap& length;
85 const CapacityMap& capacity;
92 //To store the potentila (dual variables)
93 typename Graph::template NodeMap<Length> potential;
102 MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G),
103 length(_length), capacity(_cap), flow(_G), potential(_G){ }
106 ///Runs the algorithm.
108 ///Runs the algorithm.
109 ///Returns k if there are at least k edge-disjoint paths from s to t.
110 ///Otherwise it returns the number of found edge-disjoint paths from s to t.
111 ///\todo May be it does make sense to be able to start with a nonzero
112 /// feasible primal-dual solution pair as well.
113 int run(Node s, Node t, int k) {
115 //Resetting variables from previous runs
118 FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
122 //Initialize the potential to zero
123 FOR_EACH_LOC(typename Graph::NodeIt, n, G){
129 //We need a residual graph
130 ResGraphType res_graph(G, capacity, flow);
133 ModLengthMap mod_length(res_graph, length, potential);
135 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
140 if (!dijkstra.reached(t)){
141 //There are no k paths from s to t
145 //We have to change the potential
146 FOR_EACH_LOC(typename ResGraphType::NodeIt, n, res_graph){
147 potential[n] += dijkstra.distMap()[n];
151 //Augmenting on the sortest path
155 e = dijkstra.pred(n);
156 n = dijkstra.predNode(n);
157 res_graph.augment(e,1);
158 //Let's update the total length
159 if (res_graph.forward(e))
160 total_length += length[e];
162 total_length -= length[e];
175 ///This function gives back the total length of the found paths.
176 ///Assumes that \c run() has been run and nothing changed since then.
177 Length totalLength(){
181 ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
182 ///be called before using this function.
183 const EdgeIntMap &getFlow() const { return flow;}
185 ///Returns a const reference to the NodeMap \c potential (the dual solution).
186 /// \pre \ref run() must be called before using this function.
187 const EdgeIntMap &getPotential() const { return potential;}
189 ///This function checks, whether the given solution is optimal
190 ///Running after a \c run() should return with true
191 ///In this "state of the art" this only check optimality, doesn't bother with feasibility
193 ///\todo Is this OK here?
194 bool checkComplementarySlackness(){
197 FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
199 mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
201 // std::cout << fl_e << std::endl;
202 if (0<fl_e && fl_e<capacity[e]){
207 if (mod_pot > 0 && fl_e != 0)
209 if (mod_pot < 0 && fl_e != capacity[e])
217 }; //class MinCostFlows
223 #endif //HUGO_MINCOSTFLOWS_H