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