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>
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>
40 typedef typename LengthMap::ValueType Length;
42 typedef typename LengthMap::ValueType Length;
44 typedef typename Graph::Node Node;
45 typedef typename Graph::NodeIt NodeIt;
46 typedef typename Graph::Edge Edge;
47 typedef typename Graph::OutEdgeIt OutEdgeIt;
48 typedef typename Graph::template EdgeMap<int> EdgeIntMap;
50 // typedef ConstMap<Edge,int> ConstMap;
52 typedef ResGraphWrapper<const Graph,int,EdgeIntMap,EdgeIntMap> ResGraphType;
55 typedef typename ResGraphType::template NodeMap<Length> NodeMap;
56 const ResGraphType& G;
57 // const EdgeIntMap& rev;
61 typedef typename LengthMap::KeyType KeyType;
62 typedef typename LengthMap::ValueType ValueType;
64 ValueType operator[](typename ResGraphType::Edge e) const {
66 return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
68 return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
71 ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
72 const LengthMap &o, const NodeMap &p) :
73 G(_G), /*rev(_rev),*/ ol(o), pot(p){};
80 const LengthMap& length;
81 const EdgeIntMap& capacity;
85 //The value is 1 iff the edge is reversed.
86 //If the algorithm has finished, the edges of the seeked paths are
87 //exactly those that are reversed
90 //Container to store found paths
91 std::vector< std::vector<Edge> > paths;
92 //typedef DirPath<Graph> DPath;
101 MinLengthPaths(Graph& _G, LengthMap& _length, EdgeIntMap& _cap) : G(_G),
102 length(_length), capacity(_cap), flow(_G)/*, dijkstra_dist(_G)*/{ }
105 ///Runs the algorithm.
107 ///Runs the algorithm.
108 ///Returns k if there are at least k edge-disjoint paths from s to t.
109 ///Otherwise it returns the number of found edge-disjoint paths from s to t.
110 int run(Node s, Node t, int k) {
113 //We need a residual graph
114 ResGraphType res_graph(G, capacity, flow);
116 //Initialize the copy of the Dijkstra potential to zero
117 typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
118 ModLengthMap mod_length(res_graph, length, dijkstra_dist);
120 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
125 if (!dijkstra.reached(t)){
126 //There are no k paths from s to t
131 //We have to copy the potential
132 typename ResGraphType::NodeIt n;
133 for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
134 dijkstra_dist[n] += dijkstra.distMap()[n];
139 //Augmenting on the sortest path
143 e = dijkstra.pred(n);
144 n = dijkstra.predNode(n);
152 ///\TODO To be implemented later
154 //Let's find the paths
155 //We put the paths into stl vectors (as an inner representation).
156 //In the meantime we lose the information stored in 'reversed'.
157 //We suppose the lengths to be positive now.
159 //Meanwhile we put the total length of the found paths
160 //in the member variable total_length
164 for (int j=0; j<i; ++j){
173 while (!reversed[e]){
177 paths[j].push_back(e);
178 total_length += length[e];
179 reversed[e] = 1-reversed[e];
188 ///This function gives back the total length of the found paths.
189 ///Assumes that \c run() has been run and nothing changed since then.
190 Length totalLength(){
194 ///This function gives back the \c j-th path in argument p.
195 ///Assumes that \c run() has been run and nothing changed since then.
196 /// \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.
197 template<typename DirPath>
198 void getPath(DirPath& p, int j){
200 typename DirPath::Builder B(p);
201 for(typename std::vector<Edge>::iterator i=paths[j].begin();
202 i!=paths[j].end(); ++i ){
209 }; //class MinLengthPaths
215 #endif //HUGO_MINCOSTFLOW_H