1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/suurballe.h Wed Sep 29 15:30:04 2004 +0000
1.3 @@ -0,0 +1,215 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/suurballe.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_SUURBALLE_H
1.21 +#define LEMON_SUURBALLE_H
1.22 +
1.23 +///\ingroup flowalgs
1.24 +///\file
1.25 +///\brief An algorithm for finding k paths of minimal total length.
1.26 +
1.27 +
1.28 +#include <lemon/maps.h>
1.29 +#include <vector>
1.30 +#include <lemon/min_cost_flow.h>
1.31 +
1.32 +namespace lemon {
1.33 +
1.34 +/// \addtogroup flowalgs
1.35 +/// @{
1.36 +
1.37 + ///\brief Implementation of an algorithm for finding k edge-disjoint paths between 2 nodes
1.38 + /// of minimal total length
1.39 + ///
1.40 + /// The class \ref lemon::Suurballe implements
1.41 + /// an algorithm for finding k edge-disjoint paths
1.42 + /// from a given source node to a given target node in an
1.43 + /// edge-weighted directed graph having minimal total weight (length).
1.44 + ///
1.45 + ///\warning Length values should be nonnegative.
1.46 + ///
1.47 + ///\param Graph The directed graph type the algorithm runs on.
1.48 + ///\param LengthMap The type of the length map (values should be nonnegative).
1.49 + ///
1.50 + ///\note It it questionable if it is correct to call this method after
1.51 + ///%Suurballe for it is just a special case of Edmond's and Karp's algorithm
1.52 + ///for finding minimum cost flows. In fact, this implementation is just
1.53 + ///wraps the MinCostFlow algorithms. The paper of both %Suurballe and
1.54 + ///Edmonds-Karp published in 1972, therefore it is possibly right to
1.55 + ///state that they are
1.56 + ///independent results. Most frequently this special case is referred as
1.57 + ///%Suurballe method in the literature, especially in communication
1.58 + ///network context.
1.59 + ///\author Attila Bernath
1.60 + template <typename Graph, typename LengthMap>
1.61 + class Suurballe{
1.62 +
1.63 +
1.64 + typedef typename LengthMap::ValueType Length;
1.65 +
1.66 + typedef typename Graph::Node Node;
1.67 + typedef typename Graph::NodeIt NodeIt;
1.68 + typedef typename Graph::Edge Edge;
1.69 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.70 + typedef typename Graph::template EdgeMap<int> EdgeIntMap;
1.71 +
1.72 + typedef ConstMap<Edge,int> ConstMap;
1.73 +
1.74 + //Input
1.75 + const Graph& G;
1.76 +
1.77 + //Auxiliary variables
1.78 + //This is the capacity map for the mincostflow problem
1.79 + ConstMap const1map;
1.80 + //This MinCostFlow instance will actually solve the problem
1.81 + MinCostFlow<Graph, LengthMap, ConstMap> mincost_flow;
1.82 +
1.83 + //Container to store found paths
1.84 + std::vector< std::vector<Edge> > paths;
1.85 +
1.86 + public :
1.87 +
1.88 +
1.89 + /// The constructor of the class.
1.90 +
1.91 + ///\param _G The directed graph the algorithm runs on.
1.92 + ///\param _length The length (weight or cost) of the edges.
1.93 + Suurballe(Graph& _G, LengthMap& _length) : G(_G),
1.94 + const1map(1), mincost_flow(_G, _length, const1map){}
1.95 +
1.96 + ///Runs the algorithm.
1.97 +
1.98 + ///Runs the algorithm.
1.99 + ///Returns k if there are at least k edge-disjoint paths from s to t.
1.100 + ///Otherwise it returns the number of found edge-disjoint paths from s to t.
1.101 + ///
1.102 + ///\param s The source node.
1.103 + ///\param t The target node.
1.104 + ///\param k How many paths are we looking for?
1.105 + ///
1.106 + int run(Node s, Node t, int k) {
1.107 +
1.108 + int i = mincost_flow.run(s,t,k);
1.109 +
1.110 +
1.111 + //Let's find the paths
1.112 + //We put the paths into stl vectors (as an inner representation).
1.113 + //In the meantime we lose the information stored in 'reversed'.
1.114 + //We suppose the lengths to be positive now.
1.115 +
1.116 + //We don't want to change the flow of mincost_flow, so we make a copy
1.117 + //The name here suggests that the flow has only 0/1 values.
1.118 + EdgeIntMap reversed(G);
1.119 +
1.120 + for(typename Graph::EdgeIt e(G); e!=INVALID; ++e)
1.121 + reversed[e] = mincost_flow.getFlow()[e];
1.122 +
1.123 + paths.clear();
1.124 + //total_length=0;
1.125 + paths.resize(k);
1.126 + for (int j=0; j<i; ++j){
1.127 + Node n=s;
1.128 + OutEdgeIt e;
1.129 +
1.130 + while (n!=t){
1.131 +
1.132 +
1.133 + G.first(e,n);
1.134 +
1.135 + while (!reversed[e]){
1.136 + ++e;
1.137 + }
1.138 + n = G.head(e);
1.139 + paths[j].push_back(e);
1.140 + //total_length += length[e];
1.141 + reversed[e] = 1-reversed[e];
1.142 + }
1.143 +
1.144 + }
1.145 + return i;
1.146 + }
1.147 +
1.148 +
1.149 + ///Returns the total length of the paths
1.150 +
1.151 + ///This function gives back the total length of the found paths.
1.152 + ///\pre \ref run() must
1.153 + ///be called before using this function.
1.154 + Length totalLength(){
1.155 + return mincost_flow.totalLength();
1.156 + }
1.157 +
1.158 + ///Returns the found flow.
1.159 +
1.160 + ///This function returns a const reference to the EdgeMap \c flow.
1.161 + ///\pre \ref run() must
1.162 + ///be called before using this function.
1.163 + const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
1.164 +
1.165 + /// Returns the optimal dual solution
1.166 +
1.167 + ///This function returns a const reference to the NodeMap
1.168 + ///\c potential (the dual solution).
1.169 + /// \pre \ref run() must be called before using this function.
1.170 + const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
1.171 +
1.172 + ///Checks whether the complementary slackness holds.
1.173 +
1.174 + ///This function checks, whether the given solution is optimal.
1.175 + ///It should return true after calling \ref run()
1.176 + ///Currently this function only checks optimality,
1.177 + ///doesn't bother with feasibility
1.178 + ///It is meant for testing purposes.
1.179 + ///
1.180 + bool checkComplementarySlackness(){
1.181 + return mincost_flow.checkComplementarySlackness();
1.182 + }
1.183 +
1.184 + ///Read the found paths.
1.185 +
1.186 + ///This function gives back the \c j-th path in argument p.
1.187 + ///Assumes that \c run() has been run and nothing changed since then.
1.188 + /// \warning It is assumed that \c p is constructed to
1.189 + ///be a path of graph \c G.
1.190 + ///If \c j is not less than the result of previous \c run,
1.191 + ///then the result here will be an empty path (\c j can be 0 as well).
1.192 + ///
1.193 + ///\param Path The type of the path structure to put the result to (must meet lemon path concept).
1.194 + ///\param p The path to put the result to
1.195 + ///\param j Which path you want to get from the found paths (in a real application you would get the found paths iteratively)
1.196 + template<typename Path>
1.197 + void getPath(Path& p, size_t j){
1.198 +
1.199 + p.clear();
1.200 + if (j>paths.size()-1){
1.201 + return;
1.202 + }
1.203 + typename Path::Builder B(p);
1.204 + for(typename std::vector<Edge>::iterator i=paths[j].begin();
1.205 + i!=paths[j].end(); ++i ){
1.206 + B.pushBack(*i);
1.207 + }
1.208 +
1.209 + B.commit();
1.210 + }
1.211 +
1.212 + }; //class Suurballe
1.213 +
1.214 + ///@}
1.215 +
1.216 +} //namespace lemon
1.217 +
1.218 +#endif //LEMON_SUURBALLE_H