[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[921] | 19 | #ifndef LEMON_SUURBALLE_H |
---|
| 20 | #define LEMON_SUURBALLE_H |
---|
[899] | 21 | |
---|
| 22 | ///\ingroup flowalgs |
---|
| 23 | ///\file |
---|
| 24 | ///\brief An algorithm for finding k paths of minimal total length. |
---|
| 25 | |
---|
| 26 | |
---|
[921] | 27 | #include <lemon/maps.h> |
---|
[899] | 28 | #include <vector> |
---|
[2276] | 29 | #include <lemon/ssp_min_cost_flow.h> |
---|
[899] | 30 | |
---|
[921] | 31 | namespace lemon { |
---|
[899] | 32 | |
---|
| 33 | /// \addtogroup flowalgs |
---|
| 34 | /// @{ |
---|
| 35 | |
---|
[2276] | 36 | ///\brief Implementation of an algorithm for finding k edge-disjoint |
---|
| 37 | /// paths between 2 nodes of minimal total length |
---|
[899] | 38 | /// |
---|
[921] | 39 | /// The class \ref lemon::Suurballe implements |
---|
[899] | 40 | /// an algorithm for finding k edge-disjoint paths |
---|
| 41 | /// from a given source node to a given target node in an |
---|
| 42 | /// edge-weighted directed graph having minimal total weight (length). |
---|
| 43 | /// |
---|
[1527] | 44 | ///\warning Length values should be nonnegative! |
---|
[899] | 45 | /// |
---|
| 46 | ///\param Graph The directed graph type the algorithm runs on. |
---|
| 47 | ///\param LengthMap The type of the length map (values should be nonnegative). |
---|
| 48 | /// |
---|
[968] | 49 | ///\note It it questionable whether it is correct to call this method after |
---|
[1020] | 50 | ///%Suurballe for it is just a special case of Edmonds' and Karp's algorithm |
---|
[968] | 51 | ///for finding minimum cost flows. In fact, this implementation just |
---|
[2276] | 52 | ///wraps the SspMinCostFlow algorithms. The paper of both %Suurballe and |
---|
[899] | 53 | ///Edmonds-Karp published in 1972, therefore it is possibly right to |
---|
| 54 | ///state that they are |
---|
| 55 | ///independent results. Most frequently this special case is referred as |
---|
| 56 | ///%Suurballe method in the literature, especially in communication |
---|
| 57 | ///network context. |
---|
| 58 | ///\author Attila Bernath |
---|
| 59 | template <typename Graph, typename LengthMap> |
---|
| 60 | class Suurballe{ |
---|
| 61 | |
---|
| 62 | |
---|
[987] | 63 | typedef typename LengthMap::Value Length; |
---|
[899] | 64 | |
---|
| 65 | typedef typename Graph::Node Node; |
---|
| 66 | typedef typename Graph::NodeIt NodeIt; |
---|
| 67 | typedef typename Graph::Edge Edge; |
---|
| 68 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 69 | typedef typename Graph::template EdgeMap<int> EdgeIntMap; |
---|
| 70 | |
---|
| 71 | typedef ConstMap<Edge,int> ConstMap; |
---|
| 72 | |
---|
| 73 | const Graph& G; |
---|
| 74 | |
---|
[941] | 75 | Node s; |
---|
| 76 | Node t; |
---|
| 77 | |
---|
[899] | 78 | //Auxiliary variables |
---|
| 79 | //This is the capacity map for the mincostflow problem |
---|
| 80 | ConstMap const1map; |
---|
| 81 | //This MinCostFlow instance will actually solve the problem |
---|
[2276] | 82 | SspMinCostFlow<Graph, LengthMap, ConstMap> min_cost_flow; |
---|
[899] | 83 | |
---|
| 84 | //Container to store found paths |
---|
| 85 | std::vector< std::vector<Edge> > paths; |
---|
| 86 | |
---|
| 87 | public : |
---|
| 88 | |
---|
| 89 | |
---|
[2276] | 90 | /// \brief The constructor of the class. |
---|
| 91 | /// |
---|
| 92 | /// \param _G The directed graph the algorithm runs on. |
---|
| 93 | /// \param _length The length (weight or cost) of the edges. |
---|
| 94 | /// \param _s Source node. |
---|
| 95 | /// \param _t Target node. |
---|
[941] | 96 | Suurballe(Graph& _G, LengthMap& _length, Node _s, Node _t) : |
---|
| 97 | G(_G), s(_s), t(_t), const1map(1), |
---|
| 98 | min_cost_flow(_G, _length, const1map, _s, _t) { } |
---|
[899] | 99 | |
---|
[2276] | 100 | /// \brief Runs the algorithm. |
---|
[899] | 101 | /// |
---|
[2276] | 102 | /// Runs the algorithm. |
---|
| 103 | /// Returns k if there are at least k edge-disjoint paths from s to t. |
---|
| 104 | /// Otherwise it returns the number of edge-disjoint paths found |
---|
| 105 | /// from s to t. |
---|
| 106 | /// |
---|
| 107 | /// \param k How many paths are we looking for? |
---|
[899] | 108 | /// |
---|
[941] | 109 | int run(int k) { |
---|
| 110 | int i = min_cost_flow.run(k); |
---|
[899] | 111 | |
---|
| 112 | //Let's find the paths |
---|
| 113 | //We put the paths into stl vectors (as an inner representation). |
---|
| 114 | //In the meantime we lose the information stored in 'reversed'. |
---|
| 115 | //We suppose the lengths to be positive now. |
---|
| 116 | |
---|
[941] | 117 | //We don't want to change the flow of min_cost_flow, so we make a copy |
---|
[899] | 118 | //The name here suggests that the flow has only 0/1 values. |
---|
| 119 | EdgeIntMap reversed(G); |
---|
| 120 | |
---|
| 121 | for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) |
---|
[941] | 122 | reversed[e] = min_cost_flow.getFlow()[e]; |
---|
[899] | 123 | |
---|
| 124 | paths.clear(); |
---|
| 125 | paths.resize(k); |
---|
| 126 | for (int j=0; j<i; ++j){ |
---|
| 127 | Node n=s; |
---|
| 128 | |
---|
| 129 | while (n!=t){ |
---|
| 130 | |
---|
[946] | 131 | OutEdgeIt e(G, n); |
---|
[899] | 132 | |
---|
| 133 | while (!reversed[e]){ |
---|
| 134 | ++e; |
---|
| 135 | } |
---|
[986] | 136 | n = G.target(e); |
---|
[899] | 137 | paths[j].push_back(e); |
---|
| 138 | reversed[e] = 1-reversed[e]; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | } |
---|
| 142 | return i; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
[2276] | 146 | /// \brief Returns the total length of the paths. |
---|
| 147 | /// |
---|
| 148 | /// This function gives back the total length of the found paths. |
---|
[899] | 149 | Length totalLength(){ |
---|
[941] | 150 | return min_cost_flow.totalLength(); |
---|
[899] | 151 | } |
---|
| 152 | |
---|
[2276] | 153 | /// \brief Returns the found flow. |
---|
| 154 | /// |
---|
| 155 | /// This function returns a const reference to the EdgeMap \c flow. |
---|
[941] | 156 | const EdgeIntMap &getFlow() const { return min_cost_flow.flow;} |
---|
[899] | 157 | |
---|
[2276] | 158 | /// \brief Returns the optimal dual solution |
---|
| 159 | /// |
---|
| 160 | /// This function returns a const reference to the NodeMap \c |
---|
| 161 | /// potential (the dual solution). |
---|
[941] | 162 | const EdgeIntMap &getPotential() const { return min_cost_flow.potential;} |
---|
[899] | 163 | |
---|
[2276] | 164 | /// \brief Checks whether the complementary slackness holds. |
---|
| 165 | /// |
---|
| 166 | /// This function checks, whether the given solution is optimal. |
---|
| 167 | /// Currently this function only checks optimality, doesn't bother |
---|
| 168 | /// with feasibility. It is meant for testing purposes. |
---|
[899] | 169 | bool checkComplementarySlackness(){ |
---|
[941] | 170 | return min_cost_flow.checkComplementarySlackness(); |
---|
[899] | 171 | } |
---|
| 172 | |
---|
[2276] | 173 | /// \brief Read the found paths. |
---|
[899] | 174 | /// |
---|
[2276] | 175 | /// This function gives back the \c j-th path in argument p. |
---|
| 176 | /// Assumes that \c run() has been run and nothing has changed |
---|
| 177 | /// since then. |
---|
| 178 | /// |
---|
| 179 | /// \warning It is assumed that \c p is constructed to be a path |
---|
| 180 | /// of graph \c G. If \c j is not less than the result of |
---|
| 181 | /// previous \c run, then the result here will be an empty path |
---|
| 182 | /// (\c j can be 0 as well). |
---|
| 183 | /// |
---|
| 184 | /// \param Path The type of the path structure to put the result |
---|
| 185 | /// to (must meet lemon path concept). |
---|
| 186 | /// \param p The path to put the result to. |
---|
| 187 | /// \param j Which path you want to get from the found paths (in a |
---|
| 188 | /// real application you would get the found paths iteratively). |
---|
[899] | 189 | template<typename Path> |
---|
| 190 | void getPath(Path& p, size_t j){ |
---|
| 191 | |
---|
| 192 | p.clear(); |
---|
| 193 | if (j>paths.size()-1){ |
---|
| 194 | return; |
---|
| 195 | } |
---|
| 196 | typename Path::Builder B(p); |
---|
| 197 | for(typename std::vector<Edge>::iterator i=paths[j].begin(); |
---|
| 198 | i!=paths[j].end(); ++i ){ |
---|
| 199 | B.pushBack(*i); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | B.commit(); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | }; //class Suurballe |
---|
| 206 | |
---|
| 207 | ///@} |
---|
| 208 | |
---|
[921] | 209 | } //namespace lemon |
---|
[899] | 210 | |
---|
[921] | 211 | #endif //LEMON_SUURBALLE_H |
---|