alpar@906: /* -*- C++ -*- alpar@921: * src/lemon/suurballe.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@906: * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@906: * (Egervary Combinatorial Optimization Research Group, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@921: #ifndef LEMON_SUURBALLE_H alpar@921: #define LEMON_SUURBALLE_H alpar@899: alpar@899: ///\ingroup flowalgs alpar@899: ///\file alpar@899: ///\brief An algorithm for finding k paths of minimal total length. alpar@899: alpar@899: alpar@921: #include <lemon/maps.h> alpar@899: #include <vector> alpar@921: #include <lemon/min_cost_flow.h> alpar@899: alpar@921: namespace lemon { alpar@899: alpar@899: /// \addtogroup flowalgs alpar@899: /// @{ alpar@899: alpar@899: ///\brief Implementation of an algorithm for finding k edge-disjoint paths between 2 nodes alpar@899: /// of minimal total length alpar@899: /// alpar@921: /// The class \ref lemon::Suurballe implements alpar@899: /// an algorithm for finding k edge-disjoint paths alpar@899: /// from a given source node to a given target node in an alpar@899: /// edge-weighted directed graph having minimal total weight (length). alpar@899: /// alpar@899: ///\warning Length values should be nonnegative. alpar@899: /// alpar@899: ///\param Graph The directed graph type the algorithm runs on. alpar@899: ///\param LengthMap The type of the length map (values should be nonnegative). alpar@899: /// alpar@968: ///\note It it questionable whether it is correct to call this method after alpar@1020: ///%Suurballe for it is just a special case of Edmonds' and Karp's algorithm alpar@968: ///for finding minimum cost flows. In fact, this implementation just alpar@899: ///wraps the MinCostFlow algorithms. The paper of both %Suurballe and alpar@899: ///Edmonds-Karp published in 1972, therefore it is possibly right to alpar@899: ///state that they are alpar@899: ///independent results. Most frequently this special case is referred as alpar@899: ///%Suurballe method in the literature, especially in communication alpar@899: ///network context. alpar@899: ///\author Attila Bernath alpar@899: template <typename Graph, typename LengthMap> alpar@899: class Suurballe{ alpar@899: alpar@899: alpar@987: typedef typename LengthMap::Value Length; alpar@899: alpar@899: typedef typename Graph::Node Node; alpar@899: typedef typename Graph::NodeIt NodeIt; alpar@899: typedef typename Graph::Edge Edge; alpar@899: typedef typename Graph::OutEdgeIt OutEdgeIt; alpar@899: typedef typename Graph::template EdgeMap<int> EdgeIntMap; alpar@899: alpar@899: typedef ConstMap<Edge,int> ConstMap; alpar@899: alpar@899: const Graph& G; alpar@899: marci@941: Node s; marci@941: Node t; marci@941: alpar@899: //Auxiliary variables alpar@899: //This is the capacity map for the mincostflow problem alpar@899: ConstMap const1map; alpar@899: //This MinCostFlow instance will actually solve the problem marci@941: MinCostFlow<Graph, LengthMap, ConstMap> min_cost_flow; alpar@899: alpar@899: //Container to store found paths alpar@899: std::vector< std::vector<Edge> > paths; alpar@899: alpar@899: public : alpar@899: alpar@899: marci@941: /*! \brief The constructor of the class. alpar@899: marci@941: \param _G The directed graph the algorithm runs on. marci@941: \param _length The length (weight or cost) of the edges. marci@941: \param _s Source node. marci@941: \param _t Target node. marci@941: */ marci@941: Suurballe(Graph& _G, LengthMap& _length, Node _s, Node _t) : marci@941: G(_G), s(_s), t(_t), const1map(1), marci@941: min_cost_flow(_G, _length, const1map, _s, _t) { } alpar@899: alpar@899: ///Runs the algorithm. alpar@899: alpar@899: ///Runs the algorithm. alpar@899: ///Returns k if there are at least k edge-disjoint paths from s to t. marci@941: ///Otherwise it returns the number of edge-disjoint paths found marci@941: ///from s to t. alpar@899: /// alpar@899: ///\param k How many paths are we looking for? alpar@899: /// marci@941: int run(int k) { marci@941: int i = min_cost_flow.run(k); alpar@899: alpar@899: //Let's find the paths alpar@899: //We put the paths into stl vectors (as an inner representation). alpar@899: //In the meantime we lose the information stored in 'reversed'. alpar@899: //We suppose the lengths to be positive now. alpar@899: marci@941: //We don't want to change the flow of min_cost_flow, so we make a copy alpar@899: //The name here suggests that the flow has only 0/1 values. alpar@899: EdgeIntMap reversed(G); alpar@899: alpar@899: for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) marci@941: reversed[e] = min_cost_flow.getFlow()[e]; alpar@899: alpar@899: paths.clear(); alpar@899: //total_length=0; alpar@899: paths.resize(k); alpar@899: for (int j=0; j<i; ++j){ alpar@899: Node n=s; alpar@899: alpar@899: while (n!=t){ alpar@899: klao@946: OutEdgeIt e(G, n); alpar@899: alpar@899: while (!reversed[e]){ alpar@899: ++e; alpar@899: } alpar@986: n = G.target(e); alpar@899: paths[j].push_back(e); alpar@899: //total_length += length[e]; alpar@899: reversed[e] = 1-reversed[e]; alpar@899: } alpar@899: alpar@899: } alpar@899: return i; alpar@899: } alpar@899: alpar@899: marci@941: ///Returns the total length of the paths. alpar@899: alpar@899: ///This function gives back the total length of the found paths. alpar@899: Length totalLength(){ marci@941: return min_cost_flow.totalLength(); alpar@899: } alpar@899: alpar@899: ///Returns the found flow. alpar@899: alpar@899: ///This function returns a const reference to the EdgeMap \c flow. marci@941: const EdgeIntMap &getFlow() const { return min_cost_flow.flow;} alpar@899: alpar@899: /// Returns the optimal dual solution alpar@899: alpar@899: ///This function returns a const reference to the NodeMap alpar@899: ///\c potential (the dual solution). marci@941: const EdgeIntMap &getPotential() const { return min_cost_flow.potential;} alpar@899: alpar@899: ///Checks whether the complementary slackness holds. alpar@899: alpar@899: ///This function checks, whether the given solution is optimal. alpar@899: ///Currently this function only checks optimality, alpar@899: ///doesn't bother with feasibility alpar@899: ///It is meant for testing purposes. alpar@899: bool checkComplementarySlackness(){ marci@941: return min_cost_flow.checkComplementarySlackness(); alpar@899: } alpar@899: alpar@899: ///Read the found paths. alpar@899: alpar@899: ///This function gives back the \c j-th path in argument p. alpar@899: ///Assumes that \c run() has been run and nothing changed since then. alpar@899: /// \warning It is assumed that \c p is constructed to alpar@899: ///be a path of graph \c G. alpar@899: ///If \c j is not less than the result of previous \c run, alpar@899: ///then the result here will be an empty path (\c j can be 0 as well). alpar@899: /// alpar@921: ///\param Path The type of the path structure to put the result to (must meet lemon path concept). alpar@899: ///\param p The path to put the result to alpar@899: ///\param j Which path you want to get from the found paths (in a real application you would get the found paths iteratively) alpar@899: template<typename Path> alpar@899: void getPath(Path& p, size_t j){ alpar@899: alpar@899: p.clear(); alpar@899: if (j>paths.size()-1){ alpar@899: return; alpar@899: } alpar@899: typename Path::Builder B(p); alpar@899: for(typename std::vector<Edge>::iterator i=paths[j].begin(); alpar@899: i!=paths[j].end(); ++i ){ alpar@899: B.pushBack(*i); alpar@899: } alpar@899: alpar@899: B.commit(); alpar@899: } alpar@899: alpar@899: }; //class Suurballe alpar@899: alpar@899: ///@} alpar@899: alpar@921: } //namespace lemon alpar@899: alpar@921: #endif //LEMON_SUURBALLE_H