Started.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/work/athos/mincostflows.h Tue May 04 12:00:13 2004 +0000
1.3 @@ -0,0 +1,209 @@
1.4 +// -*- c++ -*-
1.5 +#ifndef HUGO_MINCOSTFLOWS_H
1.6 +#define HUGO_MINCOSTFLOWS_H
1.7 +
1.8 +///\ingroup galgs
1.9 +///\file
1.10 +///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost
1.11 +
1.12 +#include <iostream>
1.13 +#include <dijkstra.h>
1.14 +#include <graph_wrapper.h>
1.15 +#include <maps.h>
1.16 +#include <vector.h>
1.17 +
1.18 +
1.19 +namespace hugo {
1.20 +
1.21 +/// \addtogroup galgs
1.22 +/// @{
1.23 +
1.24 + ///\brief Implementation of an algorithm for finding a flow of value \c k
1.25 + ///(for small values of \c k) having minimal total cost between 2 nodes
1.26 + ///
1.27 + ///
1.28 + /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
1.29 + /// an algorithm for finding a flow of value \c k
1.30 + ///(for small values of \c k) having minimal total cost
1.31 + /// from a given source node to a given target node in an
1.32 + /// edge-weighted directed graph having nonnegative integer capacities.
1.33 + /// The range of the length (weight) function is nonnegative reals but
1.34 + /// the range of capacity function is the set of nonnegative integers.
1.35 + /// It is not a polinomial time algorithm for counting the minimum cost
1.36 + /// maximal flow, since it counts the minimum cost flow for every value 0..M
1.37 + /// where \c M is the value of the maximal flow.
1.38 + ///
1.39 + ///\author Attila Bernath
1.40 + template <typename Graph, typename LengthMap>
1.41 + class MinCostFlows {
1.42 +
1.43 + typedef typename LengthMap::ValueType Length;
1.44 +
1.45 + typedef typename Graph::Node Node;
1.46 + typedef typename Graph::NodeIt NodeIt;
1.47 + typedef typename Graph::Edge Edge;
1.48 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.49 + typedef typename Graph::template EdgeMap<int> EdgeIntMap;
1.50 +
1.51 + typedef ConstMap<Edge,int> ConstMap;
1.52 +
1.53 + typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
1.54 +
1.55 + class ModLengthMap {
1.56 + typedef typename ResGraphType::template NodeMap<Length> NodeMap;
1.57 + const ResGraphType& G;
1.58 + const EdgeIntMap& rev;
1.59 + const LengthMap &ol;
1.60 + const NodeMap &pot;
1.61 + public :
1.62 + typedef typename LengthMap::KeyType KeyType;
1.63 + typedef typename LengthMap::ValueType ValueType;
1.64 +
1.65 + ValueType operator[](typename ResGraphType::Edge e) const {
1.66 + //if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
1.67 + // std::cout<<"Negative length!!"<<std::endl;
1.68 + //}
1.69 + return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
1.70 + }
1.71 +
1.72 + ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
1.73 + const LengthMap &o, const NodeMap &p) :
1.74 + G(_G), rev(_rev), ol(o), pot(p){};
1.75 + };//ModLengthMap
1.76 +
1.77 +
1.78 +
1.79 +
1.80 + const Graph& G;
1.81 + const LengthMap& length;
1.82 +
1.83 + //auxiliary variables
1.84 +
1.85 + //The value is 1 iff the edge is reversed.
1.86 + //If the algorithm has finished, the edges of the seeked paths are
1.87 + //exactly those that are reversed
1.88 + EdgeIntMap reversed;
1.89 +
1.90 + //Container to store found paths
1.91 + std::vector< std::vector<Edge> > paths;
1.92 + //typedef DirPath<Graph> DPath;
1.93 + //DPath paths;
1.94 +
1.95 +
1.96 + Length total_length;
1.97 +
1.98 + public :
1.99 +
1.100 +
1.101 + MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
1.102 + length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
1.103 +
1.104 +
1.105 + ///Runs the algorithm.
1.106 +
1.107 + ///Runs the algorithm.
1.108 + ///Returns k if there are at least k edge-disjoint paths from s to t.
1.109 + ///Otherwise it returns the number of found edge-disjoint paths from s to t.
1.110 + int run(Node s, Node t, int k) {
1.111 + ConstMap const1map(1);
1.112 +
1.113 +
1.114 + //We need a residual graph, in which some of the edges are reversed
1.115 + ResGraphType res_graph(G, const1map, reversed);
1.116 +
1.117 + //Initialize the copy of the Dijkstra potential to zero
1.118 + typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
1.119 + ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
1.120 +
1.121 + Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
1.122 +
1.123 + int i;
1.124 + for (i=0; i<k; ++i){
1.125 + dijkstra.run(s);
1.126 + if (!dijkstra.reached(t)){
1.127 + //There are no k paths from s to t
1.128 + break;
1.129 + };
1.130 +
1.131 + {
1.132 + //We have to copy the potential
1.133 + typename ResGraphType::NodeIt n;
1.134 + for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
1.135 + dijkstra_dist[n] += dijkstra.distMap()[n];
1.136 + }
1.137 + }
1.138 +
1.139 +
1.140 + //Reversing the sortest path
1.141 + Node n=t;
1.142 + Edge e;
1.143 + while (n!=s){
1.144 + e = dijkstra.pred(n);
1.145 + n = dijkstra.predNode(n);
1.146 + reversed[e] = 1-reversed[e];
1.147 + }
1.148 +
1.149 +
1.150 + }
1.151 +
1.152 + //Let's find the paths
1.153 + //We put the paths into stl vectors (as an inner representation).
1.154 + //In the meantime we lose the information stored in 'reversed'.
1.155 + //We suppose the lengths to be positive now.
1.156 +
1.157 + //Meanwhile we put the total length of the found paths
1.158 + //in the member variable total_length
1.159 + paths.clear();
1.160 + total_length=0;
1.161 + paths.resize(k);
1.162 + for (int j=0; j<i; ++j){
1.163 + Node n=s;
1.164 + OutEdgeIt e;
1.165 +
1.166 + while (n!=t){
1.167 +
1.168 +
1.169 + G.first(e,n);
1.170 +
1.171 + while (!reversed[e]){
1.172 + G.next(e);
1.173 + }
1.174 + n = G.head(e);
1.175 + paths[j].push_back(e);
1.176 + total_length += length[e];
1.177 + reversed[e] = 1-reversed[e];
1.178 + }
1.179 +
1.180 + }
1.181 +
1.182 + return i;
1.183 + }
1.184 +
1.185 + ///This function gives back the total length of the found paths.
1.186 + ///Assumes that \c run() has been run and nothing changed since then.
1.187 + Length totalLength(){
1.188 + return total_length;
1.189 + }
1.190 +
1.191 + ///This function gives back the \c j-th path in argument p.
1.192 + ///Assumes that \c run() has been run and nothing changed since then.
1.193 + /// \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.
1.194 + template<typename DirPath>
1.195 + void getPath(DirPath& p, int j){
1.196 + p.clear();
1.197 + typename DirPath::Builder B(p);
1.198 + for(typename std::vector<Edge>::iterator i=paths[j].begin();
1.199 + i!=paths[j].end(); ++i ){
1.200 + B.pushBack(*i);
1.201 + }
1.202 +
1.203 + B.commit();
1.204 + }
1.205 +
1.206 + }; //class MinLengthPaths
1.207 +
1.208 + ///@}
1.209 +
1.210 +} //namespace hugo
1.211 +
1.212 +#endif //HUGO_MINLENGTHPATHS_H