# HG changeset patch # User athos # Date 1083672013 0 # Node ID 4da6fb1046642d8c550efe398df7dfe5bab8ce15 # Parent a0ed1fa1b800c7ec5b80da4b7a3aea1b8cf7f516 Started. diff -r a0ed1fa1b800 -r 4da6fb104664 src/work/athos/mincostflows.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/athos/mincostflows.h Tue May 04 12:00:13 2004 +0000 @@ -0,0 +1,209 @@ +// -*- c++ -*- +#ifndef HUGO_MINCOSTFLOWS_H +#define HUGO_MINCOSTFLOWS_H + +///\ingroup galgs +///\file +///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost + +#include +#include +#include +#include +#include + + +namespace hugo { + +/// \addtogroup galgs +/// @{ + + ///\brief Implementation of an algorithm for finding a flow of value \c k + ///(for small values of \c k) having minimal total cost between 2 nodes + /// + /// + /// The class \ref hugo::MinCostFlows "MinCostFlows" implements + /// an algorithm for finding a flow of value \c k + ///(for small values of \c k) having minimal total cost + /// from a given source node to a given target node in an + /// edge-weighted directed graph having nonnegative integer capacities. + /// The range of the length (weight) function is nonnegative reals but + /// the range of capacity function is the set of nonnegative integers. + /// It is not a polinomial time algorithm for counting the minimum cost + /// maximal flow, since it counts the minimum cost flow for every value 0..M + /// where \c M is the value of the maximal flow. + /// + ///\author Attila Bernath + template + class MinCostFlows { + + typedef typename LengthMap::ValueType Length; + + typedef typename Graph::Node Node; + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::Edge Edge; + typedef typename Graph::OutEdgeIt OutEdgeIt; + typedef typename Graph::template EdgeMap EdgeIntMap; + + typedef ConstMap ConstMap; + + typedef ResGraphWrapper ResGraphType; + + class ModLengthMap { + typedef typename ResGraphType::template NodeMap NodeMap; + const ResGraphType& G; + const EdgeIntMap& rev; + const LengthMap &ol; + const NodeMap &pot; + public : + typedef typename LengthMap::KeyType KeyType; + typedef typename LengthMap::ValueType ValueType; + + ValueType operator[](typename ResGraphType::Edge e) const { + //if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){ + // std::cout<<"Negative length!!"< > paths; + //typedef DirPath DPath; + //DPath paths; + + + Length total_length; + + public : + + + MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), + length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ } + + + ///Runs the algorithm. + + ///Runs the algorithm. + ///Returns k if there are at least k edge-disjoint paths from s to t. + ///Otherwise it returns the number of found edge-disjoint paths from s to t. + int run(Node s, Node t, int k) { + ConstMap const1map(1); + + + //We need a residual graph, in which some of the edges are reversed + ResGraphType res_graph(G, const1map, reversed); + + //Initialize the copy of the Dijkstra potential to zero + typename ResGraphType::template NodeMap dijkstra_dist(res_graph); + ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist); + + Dijkstra dijkstra(res_graph, mod_length); + + int i; + for (i=0; i + void getPath(DirPath& p, int j){ + p.clear(); + typename DirPath::Builder B(p); + for(typename std::vector::iterator i=paths[j].begin(); + i!=paths[j].end(); ++i ){ + B.pushBack(*i); + } + + B.commit(); + } + + }; //class MinLengthPaths + + ///@} + +} //namespace hugo + +#endif //HUGO_MINLENGTHPATHS_H