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
11 #include <graph_wrapper.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;
56 typedef typename ResGraphType::template NodeMap<Length> NodeMap;
57 const ResGraphType& G;
58 // const EdgeIntMap& rev;
62 typedef typename LengthMap::KeyType KeyType;
63 typedef typename LengthMap::ValueType ValueType;
65 ValueType operator[](typename ResGraphType::Edge e) const {
67 return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
69 return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
72 ModLengthMap(const ResGraphType& _G,
73 const LengthMap &o, const NodeMap &p) :
74 G(_G), /*rev(_rev),*/ ol(o), pot(p){};
81 const LengthMap& length;
82 const CapacityMap& capacity;
86 //The value is 1 iff the edge is reversed.
87 //If the algorithm has finished, the edges of the seeked paths are
88 //exactly those that are reversed
91 //Container to store found paths
92 std::vector< std::vector<Edge> > paths;
93 //typedef DirPath<Graph> DPath;
102 MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G),
103 length(_length), capacity(_cap), flow(_G)/*, dijkstra_dist(_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 int run(Node s, Node t, int k) {
113 //Resetting variables from previous runs
115 FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
120 //We need a residual graph
121 ResGraphType res_graph(G, capacity, flow);
123 //Initialize the copy of the Dijkstra potential to zero
124 typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
125 ModLengthMap mod_length(res_graph, length, dijkstra_dist);
127 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
132 if (!dijkstra.reached(t)){
133 //There are no k paths from s to t
138 //We have to copy the potential
139 typename ResGraphType::NodeIt n;
140 for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
141 dijkstra_dist[n] += dijkstra.distMap()[n];
146 //Augmenting on the sortest path
150 e = dijkstra.pred(n);
151 n = dijkstra.predNode(n);
152 res_graph.augment(e,1);
153 //Let's update the total length
154 if (res_graph.forward(e))
155 total_length += length[e];
157 total_length -= length[e];
169 ///This function gives back the total length of the found paths.
170 ///Assumes that \c run() has been run and nothing changed since then.
171 Length totalLength(){
176 ///\todo To be implemented later
178 ///This function gives back the \c j-th path in argument p.
179 ///Assumes that \c run() has been run and nothing changed since then.
180 /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path.
181 template<typename DirPath>
182 void getPath(DirPath& p, int j){
184 typename DirPath::Builder B(p);
185 for(typename std::vector<Edge>::iterator i=paths[j].begin();
186 i!=paths[j].end(); ++i ){
195 }; //class MinCostFlows
201 #endif //HUGO_MINCOSTFLOW_H