An undirected graph template UndirGraph<Graph> can be used.
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 Graph::Node Node;
43 typedef typename Graph::NodeIt NodeIt;
44 typedef typename Graph::Edge Edge;
45 typedef typename Graph::OutEdgeIt OutEdgeIt;
46 typedef typename Graph::template EdgeMap<int> EdgeIntMap;
48 typedef ConstMap<Edge,int> ConstMap;
50 typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
53 typedef typename ResGraphType::template NodeMap<Length> NodeMap;
54 const ResGraphType& G;
55 const EdgeIntMap& rev;
59 typedef typename LengthMap::KeyType KeyType;
60 typedef typename LengthMap::ValueType ValueType;
62 ValueType operator[](typename ResGraphType::Edge e) const {
63 //if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
64 // std::cout<<"Negative length!!"<<std::endl;
66 return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
69 ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
70 const LengthMap &o, const NodeMap &p) :
71 G(_G), rev(_rev), ol(o), pot(p){};
78 const LengthMap& length;
82 //The value is 1 iff the edge is reversed.
83 //If the algorithm has finished, the edges of the seeked paths are
84 //exactly those that are reversed
87 //Container to store found paths
88 std::vector< std::vector<Edge> > paths;
89 //typedef DirPath<Graph> DPath;
98 MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
99 length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
102 ///Runs the algorithm.
104 ///Runs the algorithm.
105 ///Returns k if there are at least k edge-disjoint paths from s to t.
106 ///Otherwise it returns the number of found edge-disjoint paths from s to t.
107 int run(Node s, Node t, int k) {
108 ConstMap const1map(1);
111 //We need a residual graph, in which some of the edges are reversed
112 ResGraphType res_graph(G, const1map, reversed);
114 //Initialize the copy of the Dijkstra potential to zero
115 typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
116 ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
118 Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
123 if (!dijkstra.reached(t)){
124 //There are no k paths from s to t
129 //We have to copy the potential
130 typename ResGraphType::NodeIt n;
131 for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
132 dijkstra_dist[n] += dijkstra.distMap()[n];
137 //Reversing the sortest path
141 e = dijkstra.pred(n);
142 n = dijkstra.predNode(n);
143 reversed[e] = 1-reversed[e];
149 //Let's find the paths
150 //We put the paths into stl vectors (as an inner representation).
151 //In the meantime we lose the information stored in 'reversed'.
152 //We suppose the lengths to be positive now.
154 //Meanwhile we put the total length of the found paths
155 //in the member variable total_length
159 for (int j=0; j<i; ++j){
168 while (!reversed[e]){
172 paths[j].push_back(e);
173 total_length += length[e];
174 reversed[e] = 1-reversed[e];
182 ///This function gives back the total length of the found paths.
183 ///Assumes that \c run() has been run and nothing changed since then.
184 Length totalLength(){
188 ///This function gives back the \c j-th path in argument p.
189 ///Assumes that \c run() has been run and nothing changed since then.
190 /// \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.
191 template<typename DirPath>
192 void getPath(DirPath& p, int j){
194 typename DirPath::Builder B(p);
195 for(typename std::vector<Edge>::iterator i=paths[j].begin();
196 i!=paths[j].end(); ++i ){
203 }; //class MinLengthPaths
209 #endif //HUGO_MINLENGTHPATHS_H