# HG changeset patch # User athos # Date 1084290131 0 # Node ID 327f7cf13843854e06213cbb88756cd8ea9ec541 # Parent 81a0c2f2f7c61ea4812222f0592b5ad02e26eb6f Finished MinLengthPaths: a specialization of MinCostFlows. diff -r 81a0c2f2f7c6 -r 327f7cf13843 src/work/athos/makefile --- a/src/work/athos/makefile Tue May 11 14:58:09 2004 +0000 +++ b/src/work/athos/makefile Tue May 11 15:42:11 2004 +0000 @@ -1,4 +1,4 @@ -BINARIES = suurballe minlength_demo mincostflows_test +BINARIES = minlengthpaths_test minlength_demo INCLUDEDIRS= -I../.. -I.. -I../{athos,klao,marci,jacint,alpar,johanna,akos} include ../makefile diff -r 81a0c2f2f7c6 -r 327f7cf13843 src/work/athos/mincostflows.h --- a/src/work/athos/mincostflows.h Tue May 11 14:58:09 2004 +0000 +++ b/src/work/athos/mincostflows.h Tue May 11 15:42:11 2004 +0000 @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include @@ -77,12 +77,14 @@ };//ModLengthMap + protected: //Input const Graph& G; const LengthMap& length; const CapacityMap& capacity; + //auxiliary variables //To store the flow @@ -98,6 +100,7 @@ Length total_length; + public : @@ -110,6 +113,8 @@ ///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 @@ -185,10 +190,20 @@ return total_length; } - //This function checks, whether the given solution is optimal - //Running after a \c run() should return with true - //In this "state of the art" this only check optimality, doesn't bother with feasibility - bool checkSolution(){ + ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must + ///be called before using this function. + const EdgeIntMap &getFlow() const { return flow;} + + ///Returns a const reference to the NodeMap \c potential (the dual solution). + /// \pre \ref run() must be called before using this function. + const EdgeIntMap &getPotential() const { return potential;} + + ///This function checks, whether the given solution is optimal + ///Running after a \c run() should return with true + ///In this "state of the art" this only check optimality, doesn't bother with feasibility + /// + ///\todo Is this OK here? + bool checkComplementarySlackness(){ Length mod_pot; Length fl_e; FOR_EACH_LOC(typename Graph::EdgeIt, e, G){ diff -r 81a0c2f2f7c6 -r 327f7cf13843 src/work/athos/minlength_demo.cc --- a/src/work/athos/minlength_demo.cc Tue May 11 14:58:09 2004 +0000 +++ b/src/work/athos/minlength_demo.cc Tue May 11 15:42:11 2004 +0000 @@ -2,8 +2,9 @@ #include #include -#include -#include +#include +#include +#include "minlengthpaths.h" //#include using namespace hugo; @@ -19,9 +20,9 @@ Graph G; Node s, t; Graph::EdgeMap cap(G); - readDimacsMaxFlow(std::cin, G, s, t, cap); + readDimacs(std::cin, G, cap, s, t); - std::cout << "preflow demo (ATHOS)..." << std::endl; + std::cout << "Minlengthpaths demo (ATHOS)..." << std::endl; //Graph::EdgeMap flow(G); //0 flow // double pre_time=currTime(); @@ -31,8 +32,14 @@ k = atoi(argv[1]); MinLengthPaths > surb_test(G,cap); - std::cout << surb_test.run(s,t,k) << std::endl; - std::cout << surb_test.totalLength() << std::endl; + Timer ts; + ts.reset(); + std::cout << "Number of found paths: " << surb_test.run(s,t,k) << std::endl; + std::cout << "elapsed time: " << ts << std::endl; + + std::cout << "Total length of found paths: " << surb_test.totalLength() << std::endl; + //std::cout << (surb_test.checkComplementarySlackness() ? "OK (compl. slackn.)." : "Problem (compl. slackn.)!!!") << std::endl; + //preflow_push max_flow_test(G, s, t, cap); //int flow_value=max_flow_test.run(); diff -r 81a0c2f2f7c6 -r 327f7cf13843 src/work/athos/minlengthpaths.h --- a/src/work/athos/minlengthpaths.h Tue May 11 14:58:09 2004 +0000 +++ b/src/work/athos/minlengthpaths.h Tue May 11 15:42:11 2004 +0000 @@ -7,11 +7,12 @@ ///\brief An algorithm for finding k paths of minimal total length. #include -#include -#include -#include -#include - +//#include +//#include +#include +#include +#include +#include namespace hugo { @@ -26,9 +27,13 @@ /// 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 { + class MinLengthPaths{ + typedef typename LengthMap::ValueType Length; @@ -40,114 +45,50 @@ typedef ConstMap ConstMap; - typedef ResGraphWrapper ResGraphType; + //Input + const Graph& G; - 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!!"< mincost_flow; - - - - const Graph& G; - const LengthMap& length; - - //auxiliary variables - - //The value is 1 iff the edge is reversed. - //If the algorithm has finished, the edges of the seeked paths are - //exactly those that are reversed - EdgeIntMap reversed; - //Container to store found paths std::vector< std::vector > paths; - //typedef DirPath DPath; - //DPath paths; - - - Length total_length; public : - MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), - length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ } + 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. + ///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); + int i = mincost_flow.run(s,t,k); + - //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){ + 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 ){ diff -r 81a0c2f2f7c6 -r 327f7cf13843 src/work/athos/minlengthpaths_test.cc --- a/src/work/athos/minlengthpaths_test.cc Tue May 11 14:58:09 2004 +0000 +++ b/src/work/athos/minlengthpaths_test.cc Tue May 11 15:42:11 2004 +0000 @@ -69,6 +69,8 @@ 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); @@ -80,6 +82,8 @@ k=1; check( surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19"); + + check( surb_test.checkComplementarySlackness(), "Complementary slackness conditions are not met."); surb_test.getPath(P,0); check(P.length() == 4, "First path should contain 4 edges."); diff -r 81a0c2f2f7c6 -r 327f7cf13843 src/work/athos/old/minlengthpaths.h --- a/src/work/athos/old/minlengthpaths.h Tue May 11 14:58:09 2004 +0000 +++ b/src/work/athos/old/minlengthpaths.h Tue May 11 15:42:11 2004 +0000 @@ -7,10 +7,10 @@ ///\brief An algorithm for finding k paths of minimal total length. #include -#include -#include -#include -#include +#include +#include +#include +#include namespace hugo { diff -r 81a0c2f2f7c6 -r 327f7cf13843 src/work/klao/path.h --- a/src/work/klao/path.h Tue May 11 14:58:09 2004 +0000 +++ b/src/work/klao/path.h Tue May 11 15:42:11 2004 +0000 @@ -11,8 +11,8 @@ #include #include -#include -#include +#include +#include #include namespace hugo {