# HG changeset patch # User athos # Date 1084290298 0 # Node ID 84b04b70ad89c83c9c3c1b0a83b8576b29f17dfd # Parent 327f7cf13843854e06213cbb88756cd8ea9ec541 Moved things into the include (hugo) directory. diff -r 327f7cf13843 -r 84b04b70ad89 src/work/athos/mincostflows.h --- a/src/work/athos/mincostflows.h Tue May 11 15:42:11 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,254 +0,0 @@ -// -*- 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 <iostream> -#include <hugo/dijkstra.h> -#include <hugo/graph_wrapper.h> -#include <hugo/maps.h> -#include <vector> -#include <for_each_macros.h> - -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 <typename Graph, typename LengthMap, typename CapacityMap> - 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<int> EdgeIntMap; - - // typedef ConstMap<Edge,int> ConstMap; - - typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType; - typedef typename ResGraphType::Edge ResGraphEdge; - - class ModLengthMap { - //typedef typename ResGraphType::template NodeMap<Length> NodeMap; - typedef typename Graph::template NodeMap<Length> 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<Length> potential; - - //Container to store found paths - //std::vector< std::vector<Edge> > paths; - //typedef DirPath<Graph> 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]<<endl; - potential.set(n,0); - } - - - - //We need a residual graph - ResGraphType res_graph(G, capacity, flow); - - //Initialize the copy of the Dijkstra potential to zero - - //typename ResGraphType::template NodeMap<Length> potential(res_graph); - - - ModLengthMap mod_length(res_graph, length, potential); - - Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length); - - int i; - for (i=0; i<k; ++i){ - dijkstra.run(s); - if (!dijkstra.reached(t)){ - //There are no k paths from s to t - break; - }; - - { - //We have to copy the potential - typename ResGraphType::NodeIt n; - for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) { - potential[n] += dijkstra.distMap()[n]; - } - } - - - //Augmenting on the sortest path - Node n=t; - ResGraphEdge e; - while (n!=s){ - e = dijkstra.pred(n); - n = dijkstra.predNode(n); - res_graph.augment(e,1); - //Let's update the total length - if (res_graph.forward(e)) - total_length += length[e]; - else - total_length -= length[e]; - } - - - } - - - return i; - } - - - - - ///This function gives back the total length of the found paths. - ///Assumes that \c run() has been run and nothing changed since then. - Length totalLength(){ - return total_length; - } - - ///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){ - //C^{\Pi}_{i,j} - mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)]; - fl_e = flow[e]; - // std::cout << fl_e << std::endl; - if (0<fl_e && fl_e<capacity[e]){ - if (mod_pot != 0) - return false; - } - else{ - if (mod_pot > 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<typename DirPath> - void getPath(DirPath& p, int j){ - p.clear(); - typename DirPath::Builder B(p); - for(typename std::vector<Edge>::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 327f7cf13843 -r 84b04b70ad89 src/work/athos/minlengthpaths.h --- a/src/work/athos/minlengthpaths.h Tue May 11 15:42:11 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,164 +0,0 @@ -// -*- c++ -*- -#ifndef HUGO_MINLENGTHPATHS_H -#define HUGO_MINLENGTHPATHS_H - -///\ingroup galgs -///\file -///\brief An algorithm for finding k paths of minimal total length. - -#include <iostream> -//#include <hugo/dijkstra.h> -//#include <hugo/graph_wrapper.h> -#include <hugo/maps.h> -#include <vector> -#include <mincostflows.h> -#include <for_each_macros.h> - -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 <typename Graph, typename LengthMap> - 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<int> EdgeIntMap; - - typedef ConstMap<Edge,int> 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<Graph, LengthMap, ConstMap> mincost_flow; - - //Container to store found paths - std::vector< std::vector<Edge> > 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<i; ++j){ - Node n=s; - OutEdgeIt e; - - while (n!=t){ - - - G.first(e,n); - - while (!reversed[e]){ - G.next(e); - } - n = G.head(e); - paths[j].push_back(e); - //total_length += length[e]; - reversed[e] = 1-reversed[e]; - } - - } - return i; - } - - - ///This function gives back the total length of the found paths. - ///Assumes that \c run() has been run and nothing changed since then. - Length totalLength(){ - return mincost_flow.totalLength(); - } - - ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must - ///be called before using this function. - const EdgeIntMap &getFlow() const { return mincost_flow.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 mincost_flow.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 checks optimality, doesn't bother with feasibility - /// - ///\todo Is this OK here? - bool checkComplementarySlackness(){ - return mincost_flow.checkComplementarySlackness(); - } - - ///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 not less than the result of previous \c run, then the result here will be an empty path (\c j can be 0 as well). - template<typename DirPath> - void getPath(DirPath& p, size_t j){ - - p.clear(); - if (j>paths.size()-1){ - return; - } - typename DirPath::Builder B(p); - for(typename std::vector<Edge>::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 327f7cf13843 -r 84b04b70ad89 src/work/athos/minlengthpaths_test.cc --- a/src/work/athos/minlengthpaths_test.cc Tue May 11 15:42:11 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,96 +0,0 @@ -#include <iostream> -#include <list_graph.h> -#include <minlengthpaths.h> -#include <path.h> - -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<int> 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<int> > - 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<ListGraph> DPath; - DPath P(graph); - - surb_test.getPath(P,0); - check(P.length() == 4, "First path should contain 4 edges."); - - surb_test.getPath(P,1); - check(P.length() == 3, "Second path: 3 edges."); - - 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."); - - cout << (passed ? "All tests passed." : "Some of the tests failed!!!") - << endl; - - return passed ? 0 : 1; - -}