[72] | 1 | // -*- C++ -*- |
---|
| 2 | /* |
---|
[83] | 3 | preflow_push_hl.h |
---|
[72] | 4 | by jacint. |
---|
| 5 | Runs the highest label variant of the preflow push algorithm with |
---|
| 6 | running time O(n^2\sqrt(m)). |
---|
| 7 | |
---|
| 8 | Member functions: |
---|
| 9 | |
---|
| 10 | void run() : runs the algorithm |
---|
| 11 | |
---|
| 12 | The following functions should be used after run() was already run. |
---|
| 13 | |
---|
| 14 | T maxflow() : returns the value of a maximum flow |
---|
| 15 | |
---|
[83] | 16 | T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e) |
---|
[72] | 17 | |
---|
[83] | 18 | Graph::EdgeMap<T> allflow() : returns the fixed maximum flow x |
---|
[72] | 19 | |
---|
[83] | 20 | Graph::NodeMap<bool> mincut() : returns a |
---|
[72] | 21 | characteristic vector of a minimum cut. (An empty level |
---|
| 22 | in the algorithm gives a minimum cut.) |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifndef PREFLOW_PUSH_HL_H |
---|
| 26 | #define PREFLOW_PUSH_HL_H |
---|
| 27 | |
---|
[85] | 28 | //#include <algorithm> |
---|
[72] | 29 | #include <vector> |
---|
| 30 | #include <stack> |
---|
| 31 | |
---|
[78] | 32 | #include <reverse_bfs.h> |
---|
[72] | 33 | |
---|
| 34 | namespace marci { |
---|
| 35 | |
---|
[78] | 36 | template <typename Graph, typename T> |
---|
[72] | 37 | class preflow_push_hl { |
---|
| 38 | |
---|
| 39 | typedef typename Graph::NodeIt NodeIt; |
---|
| 40 | typedef typename Graph::EdgeIt EdgeIt; |
---|
| 41 | typedef typename Graph::EachNodeIt EachNodeIt; |
---|
| 42 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 43 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
| 44 | |
---|
| 45 | Graph& G; |
---|
| 46 | NodeIt s; |
---|
| 47 | NodeIt t; |
---|
[78] | 48 | typename Graph::EdgeMap<T> flow; |
---|
| 49 | typename Graph::EdgeMap<T> capacity; |
---|
[72] | 50 | T value; |
---|
[78] | 51 | typename Graph::NodeMap<bool> mincutvector; |
---|
[72] | 52 | |
---|
| 53 | public: |
---|
| 54 | |
---|
| 55 | preflow_push_hl(Graph& _G, NodeIt _s, NodeIt _t, |
---|
[78] | 56 | typename Graph::EdgeMap<T>& _capacity) : |
---|
[83] | 57 | G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity), |
---|
| 58 | mincutvector(_G, true) { } |
---|
[72] | 59 | |
---|
| 60 | |
---|
| 61 | /* |
---|
| 62 | The run() function runs the highest label preflow-push, |
---|
| 63 | running time: O(n^2\sqrt(m)) |
---|
| 64 | */ |
---|
| 65 | void run() { |
---|
| 66 | |
---|
[83] | 67 | typename Graph::NodeMap<int> level(G); |
---|
[84] | 68 | typename Graph::NodeMap<T> excess(G); |
---|
[85] | 69 | |
---|
[84] | 70 | int n=G.nodeNum(); |
---|
[83] | 71 | int b=n-2; |
---|
| 72 | /* |
---|
| 73 | b is a bound on the highest level of an active node. |
---|
| 74 | In the beginning it is at most n-2. |
---|
| 75 | */ |
---|
[72] | 76 | |
---|
[85] | 77 | std::vector<int> numb(n); //The number of nodes on level i < n. |
---|
[83] | 78 | std::vector<std::stack<NodeIt> > stack(2*n-1); |
---|
| 79 | //Stack of the active nodes in level i. |
---|
[72] | 80 | |
---|
| 81 | |
---|
| 82 | /*Reverse_bfs from t, to find the starting level.*/ |
---|
[78] | 83 | reverse_bfs<Graph> bfs(G, t); |
---|
[72] | 84 | bfs.run(); |
---|
[83] | 85 | for(EachNodeIt v=G.template first<EachNodeIt>(); v.valid(); ++v) |
---|
| 86 | { |
---|
[85] | 87 | int dist=bfs.dist(v); |
---|
| 88 | level.set(v, dist); |
---|
| 89 | ++numb[dist]; |
---|
[83] | 90 | } |
---|
[72] | 91 | |
---|
| 92 | level.set(s,n); |
---|
| 93 | |
---|
| 94 | |
---|
[83] | 95 | /* Starting flow. It is everywhere 0 at the moment. */ |
---|
| 96 | for(OutEdgeIt e=G.template first<OutEdgeIt>(s); e.valid(); ++e) |
---|
[72] | 97 | { |
---|
[83] | 98 | if ( capacity.get(e) > 0 ) { |
---|
| 99 | NodeIt w=G.head(e); |
---|
[85] | 100 | if ( w!=s ) { |
---|
| 101 | if ( excess.get(w) == 0 && w!=t ) stack[level.get(w)].push(w); |
---|
| 102 | flow.set(e, capacity.get(e)); |
---|
| 103 | excess.set(w, excess.get(w)+capacity.get(e)); |
---|
| 104 | } |
---|
[83] | 105 | } |
---|
[72] | 106 | } |
---|
| 107 | |
---|
| 108 | /* |
---|
| 109 | End of preprocessing |
---|
| 110 | */ |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | /* |
---|
[84] | 115 | Push/relabel on the highest level active nodes. |
---|
[72] | 116 | */ |
---|
| 117 | |
---|
[85] | 118 | /*While there exists an active node.*/ |
---|
[72] | 119 | while (b) { |
---|
| 120 | |
---|
[85] | 121 | /*We decrease the bound if there is no active node of level b.*/ |
---|
[72] | 122 | if (stack[b].empty()) { |
---|
| 123 | --b; |
---|
| 124 | } else { |
---|
| 125 | |
---|
[84] | 126 | NodeIt w=stack[b].top(); //w is a highest label active node. |
---|
| 127 | stack[b].pop(); |
---|
[72] | 128 | |
---|
[84] | 129 | int newlevel=2*n-2; //In newlevel we bound the next level of w. |
---|
[72] | 130 | |
---|
| 131 | for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) { |
---|
[84] | 132 | |
---|
| 133 | if ( flow.get(e) < capacity.get(e) ) { |
---|
| 134 | /*e is an edge of the residual graph */ |
---|
[72] | 135 | |
---|
[84] | 136 | NodeIt v=G.head(e); /*e is the edge wv.*/ |
---|
[72] | 137 | |
---|
[84] | 138 | if( level.get(w) == level.get(v)+1 ) { |
---|
[72] | 139 | /*Push is allowed now*/ |
---|
| 140 | |
---|
[85] | 141 | if ( excess.get(v)==0 && v != s && v !=t ) stack[level.get(v)].push(v); |
---|
| 142 | /*v becomes active.*/ |
---|
| 143 | |
---|
| 144 | if ( capacity.get(e)-flow.get(e) > excess.get(w) ) { |
---|
[72] | 145 | /*A nonsaturating push.*/ |
---|
| 146 | |
---|
| 147 | flow.set(e, flow.get(e)+excess.get(w)); |
---|
| 148 | excess.set(v, excess.get(v)+excess.get(w)); |
---|
| 149 | excess.set(w,0); |
---|
| 150 | break; |
---|
[85] | 151 | |
---|
[72] | 152 | } else { |
---|
| 153 | /*A saturating push.*/ |
---|
| 154 | |
---|
| 155 | excess.set(v, excess.get(v)+capacity.get(e)-flow.get(e)); |
---|
| 156 | excess.set(w, excess.get(w)-capacity.get(e)+flow.get(e)); |
---|
| 157 | flow.set(e, capacity.get(e)); |
---|
[85] | 158 | if ( excess.get(w)==0 ) break; |
---|
| 159 | /*If w is not active any more, then we go on to the next node.*/ |
---|
[72] | 160 | |
---|
[85] | 161 | } |
---|
| 162 | } else { |
---|
| 163 | newlevel = newlevel < level.get(v) ? newlevel : level.get(v); |
---|
| 164 | } |
---|
[72] | 165 | |
---|
[85] | 166 | } //if the out edge wv is in the res graph |
---|
[72] | 167 | |
---|
[85] | 168 | } //for out edges wv |
---|
[72] | 169 | |
---|
| 170 | |
---|
[85] | 171 | if ( excess.get(w) > 0 ) { |
---|
| 172 | |
---|
| 173 | for( InEdgeIt e=G.template first<InEdgeIt>(w); e.valid(); ++e) { |
---|
| 174 | NodeIt v=G.tail(e); /*e is the edge vw.*/ |
---|
[72] | 175 | |
---|
[85] | 176 | if( flow.get(e) > 0 ) { |
---|
| 177 | /*e is an edge of the residual graph */ |
---|
[72] | 178 | |
---|
[85] | 179 | if( level.get(w)==level.get(v)+1 ) { |
---|
| 180 | /*Push is allowed now*/ |
---|
[72] | 181 | |
---|
[85] | 182 | if ( excess.get(v)==0 && v != s && v !=t) stack[level.get(v)].push(v); |
---|
[72] | 183 | /*v becomes active.*/ |
---|
| 184 | |
---|
[85] | 185 | if ( flow.get(e) > excess.get(w) ) { |
---|
| 186 | /*A nonsaturating push.*/ |
---|
[72] | 187 | |
---|
[85] | 188 | flow.set(e, flow.get(e)-excess.get(w)); |
---|
| 189 | excess.set(v, excess.get(v)+excess.get(w)); |
---|
| 190 | excess.set(w,0); |
---|
| 191 | break; |
---|
| 192 | } else { |
---|
| 193 | /*A saturating push.*/ |
---|
| 194 | |
---|
| 195 | excess.set(v, excess.get(v)+flow.get(e)); |
---|
| 196 | excess.set(w, excess.get(w)-flow.get(e)); |
---|
| 197 | flow.set(e,0); |
---|
| 198 | if ( excess.get(w)==0 ) break; |
---|
| 199 | } |
---|
| 200 | } else { |
---|
| 201 | newlevel = newlevel < level.get(v) ? newlevel : level.get(v); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | } //if in edge vw is in the res graph |
---|
[72] | 205 | |
---|
[85] | 206 | } //for in edges vw |
---|
[72] | 207 | |
---|
[85] | 208 | } // if w still has excess after the out edge for cycle |
---|
[72] | 209 | |
---|
| 210 | |
---|
[85] | 211 | /* |
---|
| 212 | Relabel |
---|
| 213 | */ |
---|
| 214 | |
---|
| 215 | if ( excess.get(w) > 0 ) { |
---|
| 216 | |
---|
| 217 | int oldlevel=level.get(w); |
---|
[72] | 218 | level.set(w,++newlevel); |
---|
[85] | 219 | |
---|
| 220 | if ( oldlevel < n ) { |
---|
| 221 | --numb[oldlevel]; |
---|
| 222 | |
---|
| 223 | if ( !numb[oldlevel] ) { //If the level of w gets empty. |
---|
| 224 | |
---|
| 225 | for (EachNodeIt v=G.template first<EachNodeIt>(); v.valid() ; ++v) { |
---|
| 226 | if (level.get(v) > oldlevel && level.get(v) < n ) level.set(v,n); |
---|
| 227 | } |
---|
| 228 | for (int i=oldlevel+1 ; i!=n ; ++i) numb[i]=0; |
---|
| 229 | if ( newlevel < n ) newlevel=n; |
---|
| 230 | } else { |
---|
| 231 | if ( newlevel < n ) ++numb[newlevel]; |
---|
| 232 | } |
---|
| 233 | } else { |
---|
| 234 | if ( newlevel < n ) ++numb[newlevel]; |
---|
| 235 | } |
---|
| 236 | |
---|
[72] | 237 | stack[newlevel].push(w); |
---|
| 238 | b=newlevel; |
---|
[85] | 239 | |
---|
[72] | 240 | } |
---|
| 241 | |
---|
[85] | 242 | } // if stack[b] is nonempty |
---|
[72] | 243 | |
---|
[85] | 244 | } // while(b) |
---|
| 245 | |
---|
[72] | 246 | |
---|
| 247 | value = excess.get(t); |
---|
| 248 | /*Max flow value.*/ |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | } //void run() |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | /* |
---|
| 258 | Returns the maximum value of a flow. |
---|
| 259 | */ |
---|
| 260 | |
---|
| 261 | T maxflow() { |
---|
| 262 | return value; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | /* |
---|
| 268 | For the maximum flow x found by the algorithm, it returns the flow value on Edge e, i.e. x(e). |
---|
| 269 | */ |
---|
| 270 | |
---|
| 271 | T flowonEdge(EdgeIt e) { |
---|
| 272 | return flow.get(e); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | /* |
---|
| 278 | Returns the maximum flow x found by the algorithm. |
---|
| 279 | */ |
---|
| 280 | |
---|
[78] | 281 | typename Graph::EdgeMap<T> allflow() { |
---|
[72] | 282 | return flow; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | /* |
---|
| 288 | Returns a minimum cut by using a reverse bfs from t in the residual graph. |
---|
| 289 | */ |
---|
| 290 | |
---|
[78] | 291 | typename Graph::NodeMap<bool> mincut() { |
---|
[72] | 292 | |
---|
| 293 | std::queue<NodeIt> queue; |
---|
| 294 | |
---|
| 295 | mincutvector.set(t,false); |
---|
| 296 | queue.push(t); |
---|
| 297 | |
---|
| 298 | while (!queue.empty()) { |
---|
| 299 | NodeIt w=queue.front(); |
---|
| 300 | queue.pop(); |
---|
| 301 | |
---|
| 302 | for(InEdgeIt e=G.template first<InEdgeIt>(w) ; e.valid(); ++e) { |
---|
| 303 | NodeIt v=G.tail(e); |
---|
| 304 | if (mincutvector.get(v) && flow.get(e) < capacity.get(e) ) { |
---|
| 305 | queue.push(v); |
---|
| 306 | mincutvector.set(v, false); |
---|
| 307 | } |
---|
| 308 | } // for |
---|
| 309 | |
---|
| 310 | for(OutEdgeIt e=G.template first<OutEdgeIt>(w) ; e.valid(); ++e) { |
---|
| 311 | NodeIt v=G.head(e); |
---|
| 312 | if (mincutvector.get(v) && flow.get(e) > 0 ) { |
---|
| 313 | queue.push(v); |
---|
| 314 | mincutvector.set(v, false); |
---|
| 315 | } |
---|
| 316 | } // for |
---|
| 317 | |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | return mincutvector; |
---|
| 321 | |
---|
| 322 | } |
---|
| 323 | }; |
---|
| 324 | }//namespace marci |
---|
| 325 | #endif |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | |
---|