1.1 --- a/src/hugo/min_length_paths.h Wed Sep 22 08:54:53 2004 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,191 +0,0 @@
1.4 -// -*- c++ -*-
1.5 -#ifndef HUGO_MINLENGTHPATHS_H
1.6 -#define HUGO_MINLENGTHPATHS_H
1.7 -
1.8 -///\ingroup flowalgs
1.9 -///\file
1.10 -///\brief An algorithm for finding k paths of minimal total length.
1.11 -
1.12 -
1.13 -#include <hugo/maps.h>
1.14 -#include <vector>
1.15 -#include <hugo/min_cost_flows.h>
1.16 -
1.17 -namespace hugo {
1.18 -
1.19 -/// \addtogroup flowalgs
1.20 -/// @{
1.21 -
1.22 - ///\brief Implementation of an algorithm for finding k edge-disjoint paths between 2 nodes
1.23 - /// of minimal total length
1.24 - ///
1.25 - /// The class \ref hugo::MinLengthPaths implements
1.26 - /// an algorithm for finding k edge-disjoint paths
1.27 - /// from a given source node to a given target node in an
1.28 - /// edge-weighted directed graph having minimal total weight (length).
1.29 - ///
1.30 - ///\warning Length values should be nonnegative.
1.31 - ///
1.32 - ///\param Graph The directed graph type the algorithm runs on.
1.33 - ///\param LengthMap The type of the length map (values should be nonnegative).
1.34 - ///
1.35 - ///\author Attila Bernath
1.36 - template <typename Graph, typename LengthMap>
1.37 - class MinLengthPaths{
1.38 -
1.39 -
1.40 - typedef typename LengthMap::ValueType Length;
1.41 -
1.42 - typedef typename Graph::Node Node;
1.43 - typedef typename Graph::NodeIt NodeIt;
1.44 - typedef typename Graph::Edge Edge;
1.45 - typedef typename Graph::OutEdgeIt OutEdgeIt;
1.46 - typedef typename Graph::template EdgeMap<int> EdgeIntMap;
1.47 -
1.48 - typedef ConstMap<Edge,int> ConstMap;
1.49 -
1.50 - //Input
1.51 - const Graph& G;
1.52 -
1.53 - //Auxiliary variables
1.54 - //This is the capacity map for the mincostflow problem
1.55 - ConstMap const1map;
1.56 - //This MinCostFlows instance will actually solve the problem
1.57 - MinCostFlows<Graph, LengthMap, ConstMap> mincost_flow;
1.58 -
1.59 - //Container to store found paths
1.60 - std::vector< std::vector<Edge> > paths;
1.61 -
1.62 - public :
1.63 -
1.64 -
1.65 - /// The constructor of the class.
1.66 -
1.67 - ///\param _G The directed graph the algorithm runs on.
1.68 - ///\param _length The length (weight or cost) of the edges.
1.69 - MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
1.70 - const1map(1), mincost_flow(_G, _length, const1map){}
1.71 -
1.72 - ///Runs the algorithm.
1.73 -
1.74 - ///Runs the algorithm.
1.75 - ///Returns k if there are at least k edge-disjoint paths from s to t.
1.76 - ///Otherwise it returns the number of found edge-disjoint paths from s to t.
1.77 - ///
1.78 - ///\param s The source node.
1.79 - ///\param t The target node.
1.80 - ///\param k How many paths are we looking for?
1.81 - ///
1.82 - int run(Node s, Node t, int k) {
1.83 -
1.84 - int i = mincost_flow.run(s,t,k);
1.85 -
1.86 -
1.87 - //Let's find the paths
1.88 - //We put the paths into stl vectors (as an inner representation).
1.89 - //In the meantime we lose the information stored in 'reversed'.
1.90 - //We suppose the lengths to be positive now.
1.91 -
1.92 - //We don't want to change the flow of mincost_flow, so we make a copy
1.93 - //The name here suggests that the flow has only 0/1 values.
1.94 - EdgeIntMap reversed(G);
1.95 -
1.96 - for(typename Graph::EdgeIt e(G); e!=INVALID; ++e)
1.97 - reversed[e] = mincost_flow.getFlow()[e];
1.98 -
1.99 - paths.clear();
1.100 - //total_length=0;
1.101 - paths.resize(k);
1.102 - for (int j=0; j<i; ++j){
1.103 - Node n=s;
1.104 - OutEdgeIt e;
1.105 -
1.106 - while (n!=t){
1.107 -
1.108 -
1.109 - G.first(e,n);
1.110 -
1.111 - while (!reversed[e]){
1.112 - ++e;
1.113 - }
1.114 - n = G.head(e);
1.115 - paths[j].push_back(e);
1.116 - //total_length += length[e];
1.117 - reversed[e] = 1-reversed[e];
1.118 - }
1.119 -
1.120 - }
1.121 - return i;
1.122 - }
1.123 -
1.124 -
1.125 - ///Returns the total length of the paths
1.126 -
1.127 - ///This function gives back the total length of the found paths.
1.128 - ///\pre \ref run() must
1.129 - ///be called before using this function.
1.130 - Length totalLength(){
1.131 - return mincost_flow.totalLength();
1.132 - }
1.133 -
1.134 - ///Returns the found flow.
1.135 -
1.136 - ///This function returns a const reference to the EdgeMap \c flow.
1.137 - ///\pre \ref run() must
1.138 - ///be called before using this function.
1.139 - const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
1.140 -
1.141 - /// Returns the optimal dual solution
1.142 -
1.143 - ///This function returns a const reference to the NodeMap
1.144 - ///\c potential (the dual solution).
1.145 - /// \pre \ref run() must be called before using this function.
1.146 - const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
1.147 -
1.148 - ///Checks whether the complementary slackness holds.
1.149 -
1.150 - ///This function checks, whether the given solution is optimal.
1.151 - ///It should return true after calling \ref run()
1.152 - ///Currently this function only checks optimality,
1.153 - ///doesn't bother with feasibility
1.154 - ///It is meant for testing purposes.
1.155 - ///
1.156 - bool checkComplementarySlackness(){
1.157 - return mincost_flow.checkComplementarySlackness();
1.158 - }
1.159 -
1.160 - ///Read the found paths.
1.161 -
1.162 - ///This function gives back the \c j-th path in argument p.
1.163 - ///Assumes that \c run() has been run and nothing changed since then.
1.164 - /// \warning It is assumed that \c p is constructed to
1.165 - ///be a path of graph \c G.
1.166 - ///If \c j is not less than the result of previous \c run,
1.167 - ///then the result here will be an empty path (\c j can be 0 as well).
1.168 - ///
1.169 - ///\param Path The type of the path structure to put the result to (must meet hugo path concept).
1.170 - ///\param p The path to put the result to
1.171 - ///\param j Which path you want to get from the found paths (in a real application you would get the found paths iteratively)
1.172 - template<typename Path>
1.173 - void getPath(Path& p, size_t j){
1.174 -
1.175 - p.clear();
1.176 - if (j>paths.size()-1){
1.177 - return;
1.178 - }
1.179 - typename Path::Builder B(p);
1.180 - for(typename std::vector<Edge>::iterator i=paths[j].begin();
1.181 - i!=paths[j].end(); ++i ){
1.182 - B.pushBack(*i);
1.183 - }
1.184 -
1.185 - B.commit();
1.186 - }
1.187 -
1.188 - }; //class MinLengthPaths
1.189 -
1.190 - ///@}
1.191 -
1.192 -} //namespace hugo
1.193 -
1.194 -#endif //HUGO_MINLENGTHPATHS_H