athos@610: // -*- c++ -*- athos@610: #ifndef HUGO_MINCOSTFLOWS_H athos@610: #define HUGO_MINCOSTFLOWS_H athos@610: alpar@758: ///\ingroup flowalgs athos@610: ///\file athos@610: ///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost athos@610: athos@611: athos@610: #include athos@610: #include athos@610: #include athos@610: #include athos@610: athos@610: namespace hugo { athos@610: alpar@758: /// \addtogroup flowalgs athos@610: /// @{ athos@610: athos@610: ///\brief Implementation of an algorithm for finding a flow of value \c k athos@610: ///(for small values of \c k) having minimal total cost between 2 nodes athos@610: /// athos@610: /// athos@610: /// The class \ref hugo::MinCostFlows "MinCostFlows" implements athos@610: /// an algorithm for finding a flow of value \c k marci@893: /// having minimal total cost athos@610: /// from a given source node to a given target node in an marci@893: /// edge-weighted directed graph. To this end, marci@893: /// the edge-capacities and edge-weitghs have to be nonnegative. marci@893: /// The edge-capacities should be integers, but the edge-weights can be marci@893: /// integers, reals or of other comparable numeric type. marci@893: /// This algorithm is intended to use only for small values of \c k, marci@893: /// since it is only polynomial in k, marci@893: /// not in the length of k (which is log k). marci@893: /// In order to find the minimum cost flow of value \c k it athos@860: /// finds the minimum cost flow of value \c i for every marci@893: /// \c i between 0 and \c k. athos@860: /// athos@860: ///\param Graph The directed graph type the algorithm runs on. athos@860: ///\param LengthMap The type of the length map. athos@860: ///\param CapacityMap The capacity map type. athos@610: /// athos@610: ///\author Attila Bernath athos@610: template athos@610: class MinCostFlows { athos@610: athos@610: typedef typename LengthMap::ValueType Length; athos@610: athos@610: //Warning: this should be integer type athos@610: typedef typename CapacityMap::ValueType Capacity; athos@610: athos@610: typedef typename Graph::Node Node; athos@610: typedef typename Graph::NodeIt NodeIt; athos@610: typedef typename Graph::Edge Edge; athos@610: typedef typename Graph::OutEdgeIt OutEdgeIt; athos@610: typedef typename Graph::template EdgeMap EdgeIntMap; athos@610: athos@610: athos@610: typedef ResGraphWrapper ResGraphType; athos@610: typedef typename ResGraphType::Edge ResGraphEdge; athos@610: athos@610: class ModLengthMap { athos@610: typedef typename Graph::template NodeMap NodeMap; athos@610: const ResGraphType& G; athos@610: const LengthMap &ol; athos@610: const NodeMap &pot; athos@610: public : athos@610: typedef typename LengthMap::KeyType KeyType; athos@610: typedef typename LengthMap::ValueType ValueType; athos@610: athos@610: ValueType operator[](typename ResGraphType::Edge e) const { athos@610: if (G.forward(e)) athos@610: return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); athos@610: else athos@610: return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); athos@610: } athos@610: athos@610: ModLengthMap(const ResGraphType& _G, athos@610: const LengthMap &o, const NodeMap &p) : athos@610: G(_G), /*rev(_rev),*/ ol(o), pot(p){}; athos@610: };//ModLengthMap athos@610: athos@610: athos@610: protected: athos@610: athos@610: //Input athos@610: const Graph& G; athos@610: const LengthMap& length; athos@610: const CapacityMap& capacity; athos@610: athos@610: athos@610: //auxiliary variables athos@610: athos@610: //To store the flow athos@610: EdgeIntMap flow; alpar@785: //To store the potential (dual variables) athos@661: typedef typename Graph::template NodeMap PotentialMap; athos@661: PotentialMap potential; athos@610: athos@610: athos@610: Length total_length; athos@610: athos@610: athos@610: public : athos@610: athos@860: /// The constructor of the class. athos@860: athos@860: ///\param _G The directed graph the algorithm runs on. athos@860: ///\param _length The length (weight or cost) of the edges. athos@860: ///\param _cap The capacity of the edges. athos@610: MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), athos@610: length(_length), capacity(_cap), flow(_G), potential(_G){ } athos@610: athos@610: athos@610: ///Runs the algorithm. athos@860: athos@610: ///Runs the algorithm. athos@860: ///Returns k if there is a flow of value at least k edge-disjoint athos@860: ///from s to t. athos@860: ///Otherwise it returns the maximum value of a flow from s to t. athos@860: /// athos@860: ///\param s The source node. athos@860: ///\param t The target node. athos@860: ///\param k The value of the flow we are looking for. athos@860: /// athos@610: ///\todo May be it does make sense to be able to start with a nonzero athos@610: /// feasible primal-dual solution pair as well. athos@610: int run(Node s, Node t, int k) { athos@610: athos@610: //Resetting variables from previous runs athos@610: total_length = 0; athos@610: marci@788: for (typename Graph::EdgeIt e(G); e!=INVALID; ++e) flow.set(e, 0); athos@634: athos@634: //Initialize the potential to zero marci@788: for (typename Graph::NodeIt n(G); n!=INVALID; ++n) potential.set(n, 0); athos@610: athos@610: athos@610: //We need a residual graph athos@610: ResGraphType res_graph(G, capacity, flow); athos@610: athos@610: athos@610: ModLengthMap mod_length(res_graph, length, potential); athos@610: athos@610: Dijkstra dijkstra(res_graph, mod_length); athos@610: athos@610: int i; athos@610: for (i=0; i 0 && fl_e != 0) athos@610: return false; athos@610: if (mod_pot < 0 && fl_e != capacity[e]) athos@610: return false; athos@610: } athos@610: } athos@610: return true; athos@610: } athos@610: athos@610: athos@610: }; //class MinCostFlows athos@610: athos@610: ///@} athos@610: athos@610: } //namespace hugo athos@610: athos@633: #endif //HUGO_MINCOSTFLOWS_H