athos@276: // -*- c++ -*- athos@523: #ifndef HUGO_MINCOSTFLOWS_H athos@523: #define HUGO_MINCOSTFLOWS_H athos@276: klao@491: ///\ingroup galgs alpar@294: ///\file athos@523: ///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost alpar@294: athos@276: #include athos@276: #include athos@276: #include athos@306: #include athos@511: #include athos@322: athos@306: athos@276: namespace hugo { athos@276: alpar@430: /// \addtogroup galgs alpar@430: /// @{ athos@322: athos@523: ///\brief Implementation of an algorithm for finding a flow of value \c k athos@523: ///(for small values of \c k) having minimal total cost between 2 nodes athos@523: /// klao@310: /// athos@523: /// The class \ref hugo::MinCostFlows "MinCostFlows" implements athos@523: /// an algorithm for finding a flow of value \c k athos@523: ///(for small values of \c k) having minimal total cost klao@310: /// from a given source node to a given target node in an athos@523: /// edge-weighted directed graph having nonnegative integer capacities. athos@523: /// The range of the length (weight) function is nonnegative reals but athos@523: /// the range of capacity function is the set of nonnegative integers. athos@523: /// It is not a polinomial time algorithm for counting the minimum cost athos@523: /// maximal flow, since it counts the minimum cost flow for every value 0..M athos@523: /// where \c M is the value of the maximal flow. alpar@456: /// alpar@456: ///\author Attila Bernath klao@310: template athos@523: class MinCostFlows { athos@276: klao@310: typedef typename LengthMap::ValueType Length; athos@511: athos@276: typedef typename Graph::Node Node; athos@276: typedef typename Graph::NodeIt NodeIt; athos@276: typedef typename Graph::Edge Edge; athos@276: typedef typename Graph::OutEdgeIt OutEdgeIt; athos@511: typedef typename Graph::template EdgeMap EdgeIntMap; athos@306: athos@306: typedef ConstMap ConstMap; athos@306: marci@330: typedef ResGraphWrapper ResGraphType; athos@276: athos@306: class ModLengthMap { athos@511: typedef typename ResGraphType::template NodeMap NodeMap; athos@306: const ResGraphType& G; klao@310: const EdgeIntMap& rev; klao@310: const LengthMap &ol; klao@310: const NodeMap &pot; athos@306: public : athos@306: typedef typename LengthMap::KeyType KeyType; athos@306: typedef typename LengthMap::ValueType ValueType; athos@511: athos@306: ValueType operator[](typename ResGraphType::Edge e) const { athos@322: //if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){ athos@322: // std::cout<<"Negative length!!"< > paths; athos@511: //typedef DirPath DPath; athos@511: //DPath paths; athos@511: athos@511: athos@511: Length total_length; athos@322: athos@276: public : klao@310: athos@276: athos@306: MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), athos@306: length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ } athos@276: alpar@294: alpar@329: ///Runs the algorithm. alpar@329: alpar@329: ///Runs the algorithm. athos@306: ///Returns k if there are at least k edge-disjoint paths from s to t. alpar@329: ///Otherwise it returns the number of found edge-disjoint paths from s to t. athos@306: int run(Node s, Node t, int k) { athos@306: ConstMap const1map(1); athos@276: athos@511: athos@314: //We need a residual graph, in which some of the edges are reversed marci@330: ResGraphType res_graph(G, const1map, reversed); athos@306: athos@306: //Initialize the copy of the Dijkstra potential to zero athos@511: typename ResGraphType::template NodeMap dijkstra_dist(res_graph); klao@310: ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist); athos@306: athos@306: Dijkstra dijkstra(res_graph, mod_length); athos@322: athos@322: int i; athos@322: for (i=0; i athos@511: void getPath(DirPath& p, int j){ athos@511: p.clear(); athos@511: typename DirPath::Builder B(p); athos@511: for(typename std::vector::iterator i=paths[j].begin(); athos@511: i!=paths[j].end(); ++i ){ athos@520: B.pushBack(*i); athos@511: } athos@511: athos@511: B.commit(); athos@511: } athos@276: klao@310: }; //class MinLengthPaths athos@276: alpar@430: ///@} athos@276: athos@276: } //namespace hugo athos@276: athos@306: #endif //HUGO_MINLENGTHPATHS_H