1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/hugo/suurballe.h Wed Sep 22 09:55:41 2004 +0000
1.3 @@ -0,0 +1,200 @@
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_flow.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::Suurballe 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 + ///\note It it questionable if it is correct to call this method after
1.36 + ///%Suurballe for it is just a special case of Edmond's and Karp's algorithm
1.37 + ///for finding minimum cost flows. In fact, this implementation is just
1.38 + ///wraps the MinCostFlow algorithms. The paper of both %Suurballe and
1.39 + ///Edmonds-Karp published in 1972, therefore it is possibly right to
1.40 + ///state that they are
1.41 + ///independent results. Most frequently this special case is referred as
1.42 + ///%Suurballe method in the literature, especially in communication
1.43 + ///network context.
1.44 + ///\author Attila Bernath
1.45 + template <typename Graph, typename LengthMap>
1.46 + class Suurballe{
1.47 +
1.48 +
1.49 + typedef typename LengthMap::ValueType Length;
1.50 +
1.51 + typedef typename Graph::Node Node;
1.52 + typedef typename Graph::NodeIt NodeIt;
1.53 + typedef typename Graph::Edge Edge;
1.54 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.55 + typedef typename Graph::template EdgeMap<int> EdgeIntMap;
1.56 +
1.57 + typedef ConstMap<Edge,int> ConstMap;
1.58 +
1.59 + //Input
1.60 + const Graph& G;
1.61 +
1.62 + //Auxiliary variables
1.63 + //This is the capacity map for the mincostflow problem
1.64 + ConstMap const1map;
1.65 + //This MinCostFlow instance will actually solve the problem
1.66 + MinCostFlow<Graph, LengthMap, ConstMap> mincost_flow;
1.67 +
1.68 + //Container to store found paths
1.69 + std::vector< std::vector<Edge> > paths;
1.70 +
1.71 + public :
1.72 +
1.73 +
1.74 + /// The constructor of the class.
1.75 +
1.76 + ///\param _G The directed graph the algorithm runs on.
1.77 + ///\param _length The length (weight or cost) of the edges.
1.78 + Suurballe(Graph& _G, LengthMap& _length) : G(_G),
1.79 + const1map(1), mincost_flow(_G, _length, const1map){}
1.80 +
1.81 + ///Runs the algorithm.
1.82 +
1.83 + ///Runs the algorithm.
1.84 + ///Returns k if there are at least k edge-disjoint paths from s to t.
1.85 + ///Otherwise it returns the number of found edge-disjoint paths from s to t.
1.86 + ///
1.87 + ///\param s The source node.
1.88 + ///\param t The target node.
1.89 + ///\param k How many paths are we looking for?
1.90 + ///
1.91 + int run(Node s, Node t, int k) {
1.92 +
1.93 + int i = mincost_flow.run(s,t,k);
1.94 +
1.95 +
1.96 + //Let's find the paths
1.97 + //We put the paths into stl vectors (as an inner representation).
1.98 + //In the meantime we lose the information stored in 'reversed'.
1.99 + //We suppose the lengths to be positive now.
1.100 +
1.101 + //We don't want to change the flow of mincost_flow, so we make a copy
1.102 + //The name here suggests that the flow has only 0/1 values.
1.103 + EdgeIntMap reversed(G);
1.104 +
1.105 + for(typename Graph::EdgeIt e(G); e!=INVALID; ++e)
1.106 + reversed[e] = mincost_flow.getFlow()[e];
1.107 +
1.108 + paths.clear();
1.109 + //total_length=0;
1.110 + paths.resize(k);
1.111 + for (int j=0; j<i; ++j){
1.112 + Node n=s;
1.113 + OutEdgeIt e;
1.114 +
1.115 + while (n!=t){
1.116 +
1.117 +
1.118 + G.first(e,n);
1.119 +
1.120 + while (!reversed[e]){
1.121 + ++e;
1.122 + }
1.123 + n = G.head(e);
1.124 + paths[j].push_back(e);
1.125 + //total_length += length[e];
1.126 + reversed[e] = 1-reversed[e];
1.127 + }
1.128 +
1.129 + }
1.130 + return i;
1.131 + }
1.132 +
1.133 +
1.134 + ///Returns the total length of the paths
1.135 +
1.136 + ///This function gives back the total length of the found paths.
1.137 + ///\pre \ref run() must
1.138 + ///be called before using this function.
1.139 + Length totalLength(){
1.140 + return mincost_flow.totalLength();
1.141 + }
1.142 +
1.143 + ///Returns the found flow.
1.144 +
1.145 + ///This function returns a const reference to the EdgeMap \c flow.
1.146 + ///\pre \ref run() must
1.147 + ///be called before using this function.
1.148 + const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
1.149 +
1.150 + /// Returns the optimal dual solution
1.151 +
1.152 + ///This function returns a const reference to the NodeMap
1.153 + ///\c potential (the dual solution).
1.154 + /// \pre \ref run() must be called before using this function.
1.155 + const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
1.156 +
1.157 + ///Checks whether the complementary slackness holds.
1.158 +
1.159 + ///This function checks, whether the given solution is optimal.
1.160 + ///It should return true after calling \ref run()
1.161 + ///Currently this function only checks optimality,
1.162 + ///doesn't bother with feasibility
1.163 + ///It is meant for testing purposes.
1.164 + ///
1.165 + bool checkComplementarySlackness(){
1.166 + return mincost_flow.checkComplementarySlackness();
1.167 + }
1.168 +
1.169 + ///Read the found paths.
1.170 +
1.171 + ///This function gives back the \c j-th path in argument p.
1.172 + ///Assumes that \c run() has been run and nothing changed since then.
1.173 + /// \warning It is assumed that \c p is constructed to
1.174 + ///be a path of graph \c G.
1.175 + ///If \c j is not less than the result of previous \c run,
1.176 + ///then the result here will be an empty path (\c j can be 0 as well).
1.177 + ///
1.178 + ///\param Path The type of the path structure to put the result to (must meet hugo path concept).
1.179 + ///\param p The path to put the result to
1.180 + ///\param j Which path you want to get from the found paths (in a real application you would get the found paths iteratively)
1.181 + template<typename Path>
1.182 + void getPath(Path& p, size_t j){
1.183 +
1.184 + p.clear();
1.185 + if (j>paths.size()-1){
1.186 + return;
1.187 + }
1.188 + typename Path::Builder B(p);
1.189 + for(typename std::vector<Edge>::iterator i=paths[j].begin();
1.190 + i!=paths[j].end(); ++i ){
1.191 + B.pushBack(*i);
1.192 + }
1.193 +
1.194 + B.commit();
1.195 + }
1.196 +
1.197 + }; //class Suurballe
1.198 +
1.199 + ///@}
1.200 +
1.201 +} //namespace hugo
1.202 +
1.203 +#endif //HUGO_MINLENGTHPATHS_H