# HG changeset patch # User athos # Date 1084292118 0 # Node ID 4ce8c695e748847126567bb62c3282959d97995b # Parent 0566ac97809bc6a7d228dfe0d7c5fdfff70cee69 Sorry, the other half of the move comes here. diff -r 0566ac97809b -r 4ce8c695e748 src/hugo/mincostflows.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hugo/mincostflows.h Tue May 11 16:15:18 2004 +0000 @@ -0,0 +1,254 @@ +// -*- 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 +#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; + + //Warning: this should be integer type + typedef typename CapacityMap::ValueType Capacity; + + 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; + typedef typename ResGraphType::Edge ResGraphEdge; + + class ModLengthMap { + //typedef typename ResGraphType::template NodeMap NodeMap; + typedef typename Graph::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 (G.forward(e)) + return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); + else + return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); + } + + ModLengthMap(const ResGraphType& _G, + const LengthMap &o, const NodeMap &p) : + G(_G), /*rev(_rev),*/ ol(o), pot(p){}; + };//ModLengthMap + + + protected: + + //Input + const Graph& G; + const LengthMap& length; + const CapacityMap& capacity; + + + //auxiliary variables + + //To store the flow + EdgeIntMap flow; + //To store the potentila (dual variables) + typename Graph::template NodeMap potential; + + //Container to store found paths + //std::vector< std::vector > paths; + //typedef DirPath DPath; + //DPath paths; + + + Length total_length; + + + public : + + + MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), + length(_length), capacity(_cap), flow(_G), potential(_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. + ///\todo May be it does make sense to be able to start with a nonzero + /// feasible primal-dual solution pair as well. + int run(Node s, Node t, int k) { + + //Resetting variables from previous runs + total_length = 0; + + FOR_EACH_LOC(typename Graph::EdgeIt, e, G){ + flow.set(e,0); + } + + FOR_EACH_LOC(typename Graph::NodeIt, n, G){ + //cout << potential[n]< potential(res_graph); + + + ModLengthMap mod_length(res_graph, length, potential); + + Dijkstra dijkstra(res_graph, mod_length); + + int i; + for (i=0; i 0 && fl_e != 0) + return false; + if (mod_pot < 0 && fl_e != capacity[e]) + return false; + } + } + return true; + } + + /* + ///\todo To be implemented later + + ///This function gives back the \c j-th path in argument p. + ///Assumes that \c run() has been run and nothing changed since then. + /// \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. + template + 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 MinCostFlows + + ///@} + +} //namespace hugo + +#endif //HUGO_MINCOSTFLOW_H diff -r 0566ac97809b -r 4ce8c695e748 src/hugo/minlengthpaths.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hugo/minlengthpaths.h Tue May 11 16:15:18 2004 +0000 @@ -0,0 +1,164 @@ +// -*- c++ -*- +#ifndef HUGO_MINLENGTHPATHS_H +#define HUGO_MINLENGTHPATHS_H + +///\ingroup galgs +///\file +///\brief An algorithm for finding k paths of minimal total length. + +#include +//#include +//#include +#include +#include +#include +#include + +namespace hugo { + +/// \addtogroup galgs +/// @{ + + ///\brief Implementation of an algorithm for finding k paths between 2 nodes + /// of minimal total length + /// + /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements + /// an algorithm for finding k edge-disjoint paths + /// from a given source node to a given target node in an + /// edge-weighted directed graph having minimal total weigth (length). + /// + ///\warning It is assumed that the lengths are positive, since the + /// general flow-decomposition is not implemented yet. + /// + ///\author Attila Bernath + template + class MinLengthPaths{ + + + 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; + + //Input + const Graph& G; + + //Auxiliary variables + //This is the capacity map for the mincostflow problem + ConstMap const1map; + //This MinCostFlows instance will actually solve the problem + MinCostFlows mincost_flow; + + //Container to store found paths + std::vector< std::vector > paths; + + public : + + + MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), + const1map(1), mincost_flow(_G, _length, const1map){} + + ///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) { + + int i = mincost_flow.run(s,t,k); + + + + //Let's find the paths + //We put the paths into stl vectors (as an inner representation). + //In the meantime we lose the information stored in 'reversed'. + //We suppose the lengths to be positive now. + + //We don't want to change the flow of mincost_flow, so we make a copy + //The name here suggests that the flow has only 0/1 values. + EdgeIntMap reversed(G); + + FOR_EACH_LOC(typename Graph::EdgeIt, e, G){ + reversed[e] = mincost_flow.getFlow()[e]; + } + + paths.clear(); + //total_length=0; + paths.resize(k); + for (int j=0; j + void getPath(DirPath& p, size_t j){ + + p.clear(); + if (j>paths.size()-1){ + return; + } + 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 diff -r 0566ac97809b -r 4ce8c695e748 src/test/minlengthpaths_test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/minlengthpaths_test.cc Tue May 11 16:15:18 2004 +0000 @@ -0,0 +1,99 @@ +#include +#include +#include +#include + +using namespace std; +using namespace hugo; + + + +bool passed = true; + +void check(bool rc, char *msg="") { + passed = passed && rc; + if(!rc) { + std::cerr << "Test failed! ("<< msg << ")" << std::endl; \ + + + } +} + + + +int main() +{ + + typedef ListGraph::Node Node; + typedef ListGraph::Edge Edge; + + ListGraph graph; + + //Ahuja könyv példája + + Node s=graph.addNode(); + Node v1=graph.addNode(); + Node v2=graph.addNode(); + Node v3=graph.addNode(); + Node v4=graph.addNode(); + Node v5=graph.addNode(); + Node t=graph.addNode(); + + Edge s_v1=graph.addEdge(s, v1); + Edge v1_v2=graph.addEdge(v1, v2); + Edge s_v3=graph.addEdge(s, v3); + Edge v2_v4=graph.addEdge(v2, v4); + Edge v2_v5=graph.addEdge(v2, v5); + Edge v3_v5=graph.addEdge(v3, v5); + Edge v4_t=graph.addEdge(v4, t); + Edge v5_t=graph.addEdge(v5, t); + + + ListGraph::EdgeMap length(graph); + + length.set(s_v1, 6); + length.set(v1_v2, 4); + length.set(s_v3, 10); + length.set(v2_v4, 5); + length.set(v2_v5, 1); + length.set(v3_v5, 5); + length.set(v4_t, 8); + length.set(v5_t, 8); + + std::cout << "Minlengthpaths algorithm test..." << std::endl; + + + int k=3; + MinLengthPaths< ListGraph, ListGraph::EdgeMap > + surb_test(graph, length); + + check( surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 46,"Two paths, total length should be 46"); + + check( surb_test.checkComplementarySlackness(), "Complementary slackness conditions are not met."); + + typedef DirPath DPath; + DPath P(graph); + + /* + surb_test.getPath(P,0); + check(P.length() == 4, "First path should contain 4 edges."); + cout<