[714] | 1 | // -*- C++ -*- |
---|
| 2 | #ifndef HUGO_MAX_FLOW_NO_STACK_H |
---|
| 3 | #define HUGO_MAX_FLOW_NO_STACK_H |
---|
| 4 | |
---|
| 5 | #include <vector> |
---|
| 6 | #include <queue> |
---|
| 7 | //#include <stack> |
---|
| 8 | |
---|
| 9 | #include <hugo/graph_wrapper.h> |
---|
| 10 | #include <bfs_dfs.h> |
---|
| 11 | #include <hugo/invalid.h> |
---|
| 12 | #include <hugo/maps.h> |
---|
| 13 | #include <hugo/for_each_macros.h> |
---|
| 14 | |
---|
| 15 | /// \file |
---|
| 16 | /// \brief The same as max_flow.h, but without using stl stack for the active nodes. Only for test. |
---|
| 17 | /// \ingroup galgs |
---|
| 18 | |
---|
| 19 | namespace hugo { |
---|
| 20 | |
---|
| 21 | /// \addtogroup galgs |
---|
| 22 | /// @{ |
---|
| 23 | ///Maximum flow algorithms class. |
---|
| 24 | |
---|
| 25 | ///This class provides various algorithms for finding a flow of |
---|
| 26 | ///maximum value in a directed graph. The \e source node, the \e |
---|
| 27 | ///target node, the \e capacity of the edges and the \e starting \e |
---|
| 28 | ///flow value of the edges should be passed to the algorithm through the |
---|
| 29 | ///constructor. It is possible to change these quantities using the |
---|
| 30 | ///functions \ref resetSource, \ref resetTarget, \ref resetCap and |
---|
| 31 | ///\ref resetFlow. Before any subsequent runs of any algorithm of |
---|
| 32 | ///the class \ref resetFlow should be called. |
---|
| 33 | |
---|
| 34 | ///After running an algorithm of the class, the actual flow value |
---|
| 35 | ///can be obtained by calling \ref flowValue(). The minimum |
---|
| 36 | ///value cut can be written into a \c node map of \c bools by |
---|
| 37 | ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes |
---|
| 38 | ///the inclusionwise minimum and maximum of the minimum value |
---|
| 39 | ///cuts, resp.) |
---|
| 40 | ///\param Graph The directed graph type the algorithm runs on. |
---|
| 41 | ///\param Num The number type of the capacities and the flow values. |
---|
| 42 | ///\param CapMap The capacity map type. |
---|
| 43 | ///\param FlowMap The flow map type. |
---|
| 44 | ///\author Marton Makai, Jacint Szabo |
---|
| 45 | template <typename Graph, typename Num, |
---|
| 46 | typename CapMap=typename Graph::template EdgeMap<Num>, |
---|
| 47 | typename FlowMap=typename Graph::template EdgeMap<Num> > |
---|
| 48 | class MaxFlowNoStack { |
---|
| 49 | protected: |
---|
| 50 | typedef typename Graph::Node Node; |
---|
| 51 | typedef typename Graph::NodeIt NodeIt; |
---|
| 52 | typedef typename Graph::EdgeIt EdgeIt; |
---|
| 53 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 54 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
| 55 | |
---|
| 56 | // typedef typename std::vector<std::stack<Node> > VecStack; |
---|
| 57 | typedef typename std::vector<Node> VecFirst; |
---|
| 58 | typedef typename Graph::template NodeMap<Node> NNMap; |
---|
| 59 | typedef typename std::vector<Node> VecNode; |
---|
| 60 | |
---|
| 61 | const Graph* g; |
---|
| 62 | Node s; |
---|
| 63 | Node t; |
---|
| 64 | const CapMap* capacity; |
---|
| 65 | FlowMap* flow; |
---|
| 66 | int n; //the number of nodes of G |
---|
| 67 | typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW; |
---|
| 68 | //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW; |
---|
| 69 | typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt; |
---|
| 70 | typedef typename ResGW::Edge ResGWEdge; |
---|
| 71 | //typedef typename ResGW::template NodeMap<bool> ReachedMap; |
---|
| 72 | typedef typename Graph::template NodeMap<int> ReachedMap; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | //level works as a bool map in augmenting path algorithms and is |
---|
| 76 | //used by bfs for storing reached information. In preflow, it |
---|
| 77 | //shows the levels of nodes. |
---|
| 78 | ReachedMap level; |
---|
| 79 | |
---|
| 80 | //excess is needed only in preflow |
---|
| 81 | typename Graph::template NodeMap<Num> excess; |
---|
| 82 | |
---|
| 83 | //fixme |
---|
| 84 | // protected: |
---|
| 85 | // MaxFlow() { } |
---|
| 86 | // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, |
---|
| 87 | // FlowMap& _flow) |
---|
| 88 | // { |
---|
| 89 | // g=&_G; |
---|
| 90 | // s=_s; |
---|
| 91 | // t=_t; |
---|
| 92 | // capacity=&_capacity; |
---|
| 93 | // flow=&_flow; |
---|
| 94 | // n=_G.nodeNum; |
---|
| 95 | // level.set (_G); //kellene vmi ilyesmi fv |
---|
| 96 | // excess(_G,0); //itt is |
---|
| 97 | // } |
---|
| 98 | |
---|
| 99 | // constants used for heuristics |
---|
| 100 | static const int H0=20; |
---|
| 101 | static const int H1=1; |
---|
| 102 | |
---|
| 103 | public: |
---|
| 104 | |
---|
| 105 | ///Indicates the property of the starting flow. |
---|
| 106 | |
---|
| 107 | ///Indicates the property of the starting flow. The meanings are as follows: |
---|
| 108 | ///- \c ZERO_FLOW: constant zero flow |
---|
| 109 | ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to |
---|
| 110 | ///the sum of the out-flows in every node except the \e source and |
---|
| 111 | ///the \e target. |
---|
| 112 | ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at |
---|
| 113 | ///least the sum of the out-flows in every node except the \e source. |
---|
| 114 | ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be |
---|
| 115 | ///set to the constant zero flow in the beginning of the algorithm in this case. |
---|
| 116 | enum FlowEnum{ |
---|
| 117 | ZERO_FLOW, |
---|
| 118 | GEN_FLOW, |
---|
| 119 | PRE_FLOW, |
---|
| 120 | NO_FLOW |
---|
| 121 | }; |
---|
| 122 | |
---|
| 123 | enum StatusEnum { |
---|
| 124 | AFTER_NOTHING, |
---|
| 125 | AFTER_AUGMENTING, |
---|
| 126 | AFTER_FAST_AUGMENTING, |
---|
| 127 | AFTER_PRE_FLOW_PHASE_1, |
---|
| 128 | AFTER_PRE_FLOW_PHASE_2 |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | /// Don not needle this flag only if necessary. |
---|
| 132 | StatusEnum status; |
---|
| 133 | int number_of_augmentations; |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | template<typename IntMap> |
---|
| 137 | class TrickyReachedMap { |
---|
| 138 | protected: |
---|
| 139 | IntMap* map; |
---|
| 140 | int* number_of_augmentations; |
---|
| 141 | public: |
---|
| 142 | TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : |
---|
| 143 | map(&_map), number_of_augmentations(&_number_of_augmentations) { } |
---|
| 144 | void set(const Node& n, bool b) { |
---|
| 145 | if (b) |
---|
| 146 | map->set(n, *number_of_augmentations); |
---|
| 147 | else |
---|
| 148 | map->set(n, *number_of_augmentations-1); |
---|
| 149 | } |
---|
| 150 | bool operator[](const Node& n) const { |
---|
| 151 | return (*map)[n]==*number_of_augmentations; |
---|
| 152 | } |
---|
| 153 | }; |
---|
| 154 | |
---|
| 155 | ///Constructor |
---|
| 156 | |
---|
| 157 | ///\todo Document, please. |
---|
| 158 | /// |
---|
| 159 | MaxFlowNoStack(const Graph& _G, Node _s, Node _t, const CapMap& _capacity, |
---|
| 160 | FlowMap& _flow) : |
---|
| 161 | g(&_G), s(_s), t(_t), capacity(&_capacity), |
---|
| 162 | flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), |
---|
| 163 | status(AFTER_NOTHING), number_of_augmentations(0) { } |
---|
| 164 | |
---|
| 165 | ///Runs a maximum flow algorithm. |
---|
| 166 | |
---|
| 167 | ///Runs a preflow algorithm, which is the fastest maximum flow |
---|
| 168 | ///algorithm up-to-date. The default for \c fe is ZERO_FLOW. |
---|
| 169 | ///\pre The starting flow must be |
---|
| 170 | /// - a constant zero flow if \c fe is \c ZERO_FLOW, |
---|
| 171 | /// - an arbitary flow if \c fe is \c GEN_FLOW, |
---|
| 172 | /// - an arbitary preflow if \c fe is \c PRE_FLOW, |
---|
| 173 | /// - any map if \c fe is NO_FLOW. |
---|
| 174 | void run(FlowEnum fe=ZERO_FLOW) { |
---|
| 175 | preflow(fe); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | ///Runs a preflow algorithm. |
---|
| 180 | |
---|
| 181 | ///Runs a preflow algorithm. The preflow algorithms provide the |
---|
| 182 | ///fastest way to compute a maximum flow in a directed graph. |
---|
| 183 | ///\pre The starting flow must be |
---|
| 184 | /// - a constant zero flow if \c fe is \c ZERO_FLOW, |
---|
| 185 | /// - an arbitary flow if \c fe is \c GEN_FLOW, |
---|
| 186 | /// - an arbitary preflow if \c fe is \c PRE_FLOW, |
---|
| 187 | /// - any map if \c fe is NO_FLOW. |
---|
| 188 | /// |
---|
| 189 | ///\todo NO_FLOW should be the default flow. |
---|
| 190 | void preflow(FlowEnum fe) { |
---|
| 191 | preflowPhase1(fe); |
---|
| 192 | preflowPhase2(); |
---|
| 193 | } |
---|
| 194 | // Heuristics: |
---|
| 195 | // 2 phase |
---|
| 196 | // gap |
---|
| 197 | // list 'level_list' on the nodes on level i implemented by hand |
---|
| 198 | // stack 'active' on the active nodes on level i |
---|
| 199 | // runs heuristic 'highest label' for H1*n relabels |
---|
| 200 | // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label' |
---|
| 201 | // Parameters H0 and H1 are initialized to 20 and 1. |
---|
| 202 | |
---|
| 203 | ///Runs the first phase of the preflow algorithm. |
---|
| 204 | |
---|
| 205 | ///The preflow algorithm consists of two phases, this method runs the |
---|
| 206 | ///first phase. After the first phase the maximum flow value and a |
---|
| 207 | ///minimum value cut can already be computed, though a maximum flow |
---|
| 208 | ///is net yet obtained. So after calling this method \ref flowValue |
---|
| 209 | ///and \ref actMinCut gives proper results. |
---|
| 210 | ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not |
---|
| 211 | ///give minimum value cuts unless calling \ref preflowPhase2. |
---|
| 212 | ///\pre The starting flow must be |
---|
| 213 | /// - a constant zero flow if \c fe is \c ZERO_FLOW, |
---|
| 214 | /// - an arbitary flow if \c fe is \c GEN_FLOW, |
---|
| 215 | /// - an arbitary preflow if \c fe is \c PRE_FLOW, |
---|
| 216 | /// - any map if \c fe is NO_FLOW. |
---|
| 217 | void preflowPhase1(FlowEnum fe); |
---|
| 218 | |
---|
| 219 | ///Runs the second phase of the preflow algorithm. |
---|
| 220 | |
---|
| 221 | ///The preflow algorithm consists of two phases, this method runs |
---|
| 222 | ///the second phase. After calling \ref preflowPhase1 and then |
---|
| 223 | ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut, |
---|
| 224 | ///\ref minMinCut and \ref maxMinCut give proper results. |
---|
| 225 | ///\pre \ref preflowPhase1 must be called before. |
---|
| 226 | void preflowPhase2(); |
---|
| 227 | |
---|
| 228 | /// Starting from a flow, this method searches for an augmenting path |
---|
| 229 | /// according to the Edmonds-Karp algorithm |
---|
| 230 | /// and augments the flow on if any. |
---|
| 231 | /// The return value shows if the augmentation was succesful. |
---|
| 232 | bool augmentOnShortestPath(); |
---|
| 233 | bool augmentOnShortestPath2(); |
---|
| 234 | |
---|
| 235 | /// Starting from a flow, this method searches for an augmenting blocking |
---|
| 236 | /// flow according to Dinits' algorithm and augments the flow on if any. |
---|
| 237 | /// The blocking flow is computed in a physically constructed |
---|
| 238 | /// residual graph of type \c Mutablegraph. |
---|
| 239 | /// The return value show sif the augmentation was succesful. |
---|
| 240 | template<typename MutableGraph> bool augmentOnBlockingFlow(); |
---|
| 241 | |
---|
| 242 | /// The same as \c augmentOnBlockingFlow<MutableGraph> but the |
---|
| 243 | /// residual graph is not constructed physically. |
---|
| 244 | /// The return value shows if the augmentation was succesful. |
---|
| 245 | bool augmentOnBlockingFlow2(); |
---|
| 246 | |
---|
| 247 | /// Returns the maximum value of a flow. |
---|
| 248 | |
---|
| 249 | /// Returns the maximum value of a flow, by counting the |
---|
| 250 | /// over-flow of the target node \ref t. |
---|
| 251 | /// It can be called already after running \ref preflowPhase1. |
---|
| 252 | Num flowValue() const { |
---|
| 253 | Num a=0; |
---|
| 254 | FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e]; |
---|
| 255 | FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e]; |
---|
| 256 | return a; |
---|
| 257 | //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | ///Returns a minimum value cut after calling \ref preflowPhase1. |
---|
| 261 | |
---|
| 262 | ///After the first phase of the preflow algorithm the maximum flow |
---|
| 263 | ///value and a minimum value cut can already be computed. This |
---|
| 264 | ///method can be called after running \ref preflowPhase1 for |
---|
| 265 | ///obtaining a minimum value cut. |
---|
| 266 | /// \warning Gives proper result only right after calling \ref |
---|
| 267 | /// preflowPhase1. |
---|
| 268 | /// \todo We have to make some status variable which shows the |
---|
| 269 | /// actual state |
---|
| 270 | /// of the class. This enables us to determine which methods are valid |
---|
| 271 | /// for MinCut computation |
---|
| 272 | template<typename _CutMap> |
---|
| 273 | void actMinCut(_CutMap& M) const { |
---|
| 274 | NodeIt v; |
---|
| 275 | switch (status) { |
---|
| 276 | case AFTER_PRE_FLOW_PHASE_1: |
---|
| 277 | for(g->first(v); g->valid(v); g->next(v)) { |
---|
| 278 | if (level[v] < n) { |
---|
| 279 | M.set(v, false); |
---|
| 280 | } else { |
---|
| 281 | M.set(v, true); |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | break; |
---|
| 285 | case AFTER_PRE_FLOW_PHASE_2: |
---|
| 286 | case AFTER_NOTHING: |
---|
| 287 | minMinCut(M); |
---|
| 288 | break; |
---|
| 289 | case AFTER_AUGMENTING: |
---|
| 290 | for(g->first(v); g->valid(v); g->next(v)) { |
---|
| 291 | if (level[v]) { |
---|
| 292 | M.set(v, true); |
---|
| 293 | } else { |
---|
| 294 | M.set(v, false); |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | break; |
---|
| 298 | case AFTER_FAST_AUGMENTING: |
---|
| 299 | for(g->first(v); g->valid(v); g->next(v)) { |
---|
| 300 | if (level[v]==number_of_augmentations) { |
---|
| 301 | M.set(v, true); |
---|
| 302 | } else { |
---|
| 303 | M.set(v, false); |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | break; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | ///Returns the inclusionwise minimum of the minimum value cuts. |
---|
| 311 | |
---|
| 312 | ///Sets \c M to the characteristic vector of the minimum value cut |
---|
| 313 | ///which is inclusionwise minimum. It is computed by processing |
---|
| 314 | ///a bfs from the source node \c s in the residual graph. |
---|
| 315 | ///\pre M should be a node map of bools initialized to false. |
---|
| 316 | ///\pre \c flow must be a maximum flow. |
---|
| 317 | template<typename _CutMap> |
---|
| 318 | void minMinCut(_CutMap& M) const { |
---|
| 319 | std::queue<Node> queue; |
---|
| 320 | |
---|
| 321 | M.set(s,true); |
---|
| 322 | queue.push(s); |
---|
| 323 | |
---|
| 324 | while (!queue.empty()) { |
---|
| 325 | Node w=queue.front(); |
---|
| 326 | queue.pop(); |
---|
| 327 | |
---|
| 328 | OutEdgeIt e; |
---|
| 329 | for(g->first(e,w) ; g->valid(e); g->next(e)) { |
---|
| 330 | Node v=g->head(e); |
---|
| 331 | if (!M[v] && (*flow)[e] < (*capacity)[e] ) { |
---|
| 332 | queue.push(v); |
---|
| 333 | M.set(v, true); |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | InEdgeIt f; |
---|
| 338 | for(g->first(f,w) ; g->valid(f); g->next(f)) { |
---|
| 339 | Node v=g->tail(f); |
---|
| 340 | if (!M[v] && (*flow)[f] > 0 ) { |
---|
| 341 | queue.push(v); |
---|
| 342 | M.set(v, true); |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | ///Returns the inclusionwise maximum of the minimum value cuts. |
---|
| 349 | |
---|
| 350 | ///Sets \c M to the characteristic vector of the minimum value cut |
---|
| 351 | ///which is inclusionwise maximum. It is computed by processing a |
---|
| 352 | ///backward bfs from the target node \c t in the residual graph. |
---|
| 353 | ///\pre M should be a node map of bools initialized to false. |
---|
| 354 | ///\pre \c flow must be a maximum flow. |
---|
| 355 | template<typename _CutMap> |
---|
| 356 | void maxMinCut(_CutMap& M) const { |
---|
| 357 | |
---|
| 358 | NodeIt v; |
---|
| 359 | for(g->first(v) ; g->valid(v); g->next(v)) { |
---|
| 360 | M.set(v, true); |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | std::queue<Node> queue; |
---|
| 364 | |
---|
| 365 | M.set(t,false); |
---|
| 366 | queue.push(t); |
---|
| 367 | |
---|
| 368 | while (!queue.empty()) { |
---|
| 369 | Node w=queue.front(); |
---|
| 370 | queue.pop(); |
---|
| 371 | |
---|
| 372 | InEdgeIt e; |
---|
| 373 | for(g->first(e,w) ; g->valid(e); g->next(e)) { |
---|
| 374 | Node v=g->tail(e); |
---|
| 375 | if (M[v] && (*flow)[e] < (*capacity)[e] ) { |
---|
| 376 | queue.push(v); |
---|
| 377 | M.set(v, false); |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | OutEdgeIt f; |
---|
| 382 | for(g->first(f,w) ; g->valid(f); g->next(f)) { |
---|
| 383 | Node v=g->head(f); |
---|
| 384 | if (M[v] && (*flow)[f] > 0 ) { |
---|
| 385 | queue.push(v); |
---|
| 386 | M.set(v, false); |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | ///Returns a minimum value cut. |
---|
| 393 | |
---|
| 394 | ///Sets \c M to the characteristic vector of a minimum value cut. |
---|
| 395 | ///\pre M should be a node map of bools initialized to false. |
---|
| 396 | ///\pre \c flow must be a maximum flow. |
---|
| 397 | template<typename CutMap> |
---|
| 398 | void minCut(CutMap& M) const { minMinCut(M); } |
---|
| 399 | |
---|
| 400 | ///Resets the source node to \c _s. |
---|
| 401 | |
---|
| 402 | ///Resets the source node to \c _s. |
---|
| 403 | /// |
---|
| 404 | void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; } |
---|
| 405 | |
---|
| 406 | ///Resets the target node to \c _t. |
---|
| 407 | |
---|
| 408 | ///Resets the target node to \c _t. |
---|
| 409 | /// |
---|
| 410 | void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; } |
---|
| 411 | |
---|
| 412 | /// Resets the edge map of the capacities to _cap. |
---|
| 413 | |
---|
| 414 | /// Resets the edge map of the capacities to _cap. |
---|
| 415 | /// |
---|
| 416 | void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; } |
---|
| 417 | |
---|
| 418 | /// Resets the edge map of the flows to _flow. |
---|
| 419 | |
---|
| 420 | /// Resets the edge map of the flows to _flow. |
---|
| 421 | /// |
---|
| 422 | void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; } |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | private: |
---|
| 426 | |
---|
| 427 | int push(Node w, NNMap& next, VecFirst& first) { |
---|
| 428 | |
---|
| 429 | int lev=level[w]; |
---|
| 430 | Num exc=excess[w]; |
---|
| 431 | int newlevel=n; //bound on the next level of w |
---|
| 432 | |
---|
| 433 | OutEdgeIt e; |
---|
| 434 | for(g->first(e,w); g->valid(e); g->next(e)) { |
---|
| 435 | |
---|
| 436 | if ( (*flow)[e] >= (*capacity)[e] ) continue; |
---|
| 437 | Node v=g->head(e); |
---|
| 438 | |
---|
| 439 | if( lev > level[v] ) { //Push is allowed now |
---|
| 440 | |
---|
| 441 | if ( excess[v]<=0 && v!=t && v!=s ) { |
---|
| 442 | next.set(v,first[level[v]]); |
---|
| 443 | first[level[v]]=v; |
---|
| 444 | // int lev_v=level[v]; |
---|
| 445 | //active[lev_v].push(v); |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | Num cap=(*capacity)[e]; |
---|
| 449 | Num flo=(*flow)[e]; |
---|
| 450 | Num remcap=cap-flo; |
---|
| 451 | |
---|
| 452 | if ( remcap >= exc ) { //A nonsaturating push. |
---|
| 453 | |
---|
| 454 | flow->set(e, flo+exc); |
---|
| 455 | excess.set(v, excess[v]+exc); |
---|
| 456 | exc=0; |
---|
| 457 | break; |
---|
| 458 | |
---|
| 459 | } else { //A saturating push. |
---|
| 460 | flow->set(e, cap); |
---|
| 461 | excess.set(v, excess[v]+remcap); |
---|
| 462 | exc-=remcap; |
---|
| 463 | } |
---|
| 464 | } else if ( newlevel > level[v] ) newlevel = level[v]; |
---|
| 465 | } //for out edges wv |
---|
| 466 | |
---|
| 467 | if ( exc > 0 ) { |
---|
| 468 | InEdgeIt e; |
---|
| 469 | for(g->first(e,w); g->valid(e); g->next(e)) { |
---|
| 470 | |
---|
| 471 | if( (*flow)[e] <= 0 ) continue; |
---|
| 472 | Node v=g->tail(e); |
---|
| 473 | |
---|
| 474 | if( lev > level[v] ) { //Push is allowed now |
---|
| 475 | |
---|
| 476 | if ( excess[v]<=0 && v!=t && v!=s ) { |
---|
| 477 | next.set(v,first[level[v]]); |
---|
| 478 | first[level[v]]=v; |
---|
| 479 | //int lev_v=level[v]; |
---|
| 480 | //active[lev_v].push(v); |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | Num flo=(*flow)[e]; |
---|
| 484 | |
---|
| 485 | if ( flo >= exc ) { //A nonsaturating push. |
---|
| 486 | |
---|
| 487 | flow->set(e, flo-exc); |
---|
| 488 | excess.set(v, excess[v]+exc); |
---|
| 489 | exc=0; |
---|
| 490 | break; |
---|
| 491 | } else { //A saturating push. |
---|
| 492 | |
---|
| 493 | excess.set(v, excess[v]+flo); |
---|
| 494 | exc-=flo; |
---|
| 495 | flow->set(e,0); |
---|
| 496 | } |
---|
| 497 | } else if ( newlevel > level[v] ) newlevel = level[v]; |
---|
| 498 | } //for in edges vw |
---|
| 499 | |
---|
| 500 | } // if w still has excess after the out edge for cycle |
---|
| 501 | |
---|
| 502 | excess.set(w, exc); |
---|
| 503 | |
---|
| 504 | return newlevel; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | |
---|
| 508 | void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first, |
---|
| 509 | VecNode& level_list, NNMap& left, NNMap& right) |
---|
| 510 | { |
---|
| 511 | std::queue<Node> bfs_queue; |
---|
| 512 | |
---|
| 513 | switch (fe) { |
---|
| 514 | case NO_FLOW: //flow is already set to const zero in this case |
---|
| 515 | case ZERO_FLOW: |
---|
| 516 | { |
---|
| 517 | //Reverse_bfs from t, to find the starting level. |
---|
| 518 | level.set(t,0); |
---|
| 519 | bfs_queue.push(t); |
---|
| 520 | |
---|
| 521 | while (!bfs_queue.empty()) { |
---|
| 522 | |
---|
| 523 | Node v=bfs_queue.front(); |
---|
| 524 | bfs_queue.pop(); |
---|
| 525 | int l=level[v]+1; |
---|
| 526 | |
---|
| 527 | InEdgeIt e; |
---|
| 528 | for(g->first(e,v); g->valid(e); g->next(e)) { |
---|
| 529 | Node w=g->tail(e); |
---|
| 530 | if ( level[w] == n && w != s ) { |
---|
| 531 | bfs_queue.push(w); |
---|
| 532 | Node z=level_list[l]; |
---|
| 533 | if ( g->valid(z) ) left.set(z,w); |
---|
| 534 | right.set(w,z); |
---|
| 535 | level_list[l]=w; |
---|
| 536 | level.set(w, l); |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | //the starting flow |
---|
| 542 | OutEdgeIt e; |
---|
| 543 | for(g->first(e,s); g->valid(e); g->next(e)) |
---|
| 544 | { |
---|
| 545 | Num c=(*capacity)[e]; |
---|
| 546 | if ( c <= 0 ) continue; |
---|
| 547 | Node w=g->head(e); |
---|
| 548 | if ( level[w] < n ) { |
---|
| 549 | if ( excess[w] <= 0 && w!=t ) |
---|
| 550 | { |
---|
| 551 | next.set(w,first[level[w]]); |
---|
| 552 | first[level[w]]=w; |
---|
| 553 | //active[level[w]].push(w); |
---|
| 554 | } |
---|
| 555 | flow->set(e, c); |
---|
| 556 | excess.set(w, excess[w]+c); |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | break; |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | case GEN_FLOW: |
---|
| 563 | case PRE_FLOW: |
---|
| 564 | { |
---|
| 565 | //Reverse_bfs from t in the residual graph, |
---|
| 566 | //to find the starting level. |
---|
| 567 | level.set(t,0); |
---|
| 568 | bfs_queue.push(t); |
---|
| 569 | |
---|
| 570 | while (!bfs_queue.empty()) { |
---|
| 571 | |
---|
| 572 | Node v=bfs_queue.front(); |
---|
| 573 | bfs_queue.pop(); |
---|
| 574 | int l=level[v]+1; |
---|
| 575 | |
---|
| 576 | InEdgeIt e; |
---|
| 577 | for(g->first(e,v); g->valid(e); g->next(e)) { |
---|
| 578 | if ( (*capacity)[e] <= (*flow)[e] ) continue; |
---|
| 579 | Node w=g->tail(e); |
---|
| 580 | if ( level[w] == n && w != s ) { |
---|
| 581 | bfs_queue.push(w); |
---|
| 582 | Node z=level_list[l]; |
---|
| 583 | if ( g->valid(z) ) left.set(z,w); |
---|
| 584 | right.set(w,z); |
---|
| 585 | level_list[l]=w; |
---|
| 586 | level.set(w, l); |
---|
| 587 | } |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | OutEdgeIt f; |
---|
| 591 | for(g->first(f,v); g->valid(f); g->next(f)) { |
---|
| 592 | if ( 0 >= (*flow)[f] ) continue; |
---|
| 593 | Node w=g->head(f); |
---|
| 594 | if ( level[w] == n && w != s ) { |
---|
| 595 | bfs_queue.push(w); |
---|
| 596 | Node z=level_list[l]; |
---|
| 597 | if ( g->valid(z) ) left.set(z,w); |
---|
| 598 | right.set(w,z); |
---|
| 599 | level_list[l]=w; |
---|
| 600 | level.set(w, l); |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | |
---|
| 606 | //the starting flow |
---|
| 607 | OutEdgeIt e; |
---|
| 608 | for(g->first(e,s); g->valid(e); g->next(e)) |
---|
| 609 | { |
---|
| 610 | Num rem=(*capacity)[e]-(*flow)[e]; |
---|
| 611 | if ( rem <= 0 ) continue; |
---|
| 612 | Node w=g->head(e); |
---|
| 613 | if ( level[w] < n ) { |
---|
| 614 | if ( excess[w] <= 0 && w!=t ) |
---|
| 615 | { |
---|
| 616 | next.set(w,first[level[w]]); |
---|
| 617 | first[level[w]]=w; |
---|
| 618 | //active[level[w]].push(w); |
---|
| 619 | } |
---|
| 620 | flow->set(e, (*capacity)[e]); |
---|
| 621 | excess.set(w, excess[w]+rem); |
---|
| 622 | } |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | InEdgeIt f; |
---|
| 626 | for(g->first(f,s); g->valid(f); g->next(f)) |
---|
| 627 | { |
---|
| 628 | if ( (*flow)[f] <= 0 ) continue; |
---|
| 629 | Node w=g->tail(f); |
---|
| 630 | if ( level[w] < n ) { |
---|
| 631 | if ( excess[w] <= 0 && w!=t ) |
---|
| 632 | { |
---|
| 633 | next.set(w,first[level[w]]); |
---|
| 634 | first[level[w]]=w; |
---|
| 635 | //active[level[w]].push(w); |
---|
| 636 | } |
---|
| 637 | excess.set(w, excess[w]+(*flow)[f]); |
---|
| 638 | flow->set(f, 0); |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | break; |
---|
| 642 | } //case PRE_FLOW |
---|
| 643 | } |
---|
| 644 | } //preflowPreproc |
---|
| 645 | |
---|
| 646 | |
---|
| 647 | |
---|
| 648 | void relabel(Node w, int newlevel, NNMap& next, VecFirst& first, |
---|
| 649 | VecNode& level_list, NNMap& left, |
---|
| 650 | NNMap& right, int& b, int& k, bool what_heur ) |
---|
| 651 | { |
---|
| 652 | |
---|
| 653 | Num lev=level[w]; |
---|
| 654 | |
---|
| 655 | Node right_n=right[w]; |
---|
| 656 | Node left_n=left[w]; |
---|
| 657 | |
---|
| 658 | //unlacing starts |
---|
| 659 | if ( g->valid(right_n) ) { |
---|
| 660 | if ( g->valid(left_n) ) { |
---|
| 661 | right.set(left_n, right_n); |
---|
| 662 | left.set(right_n, left_n); |
---|
| 663 | } else { |
---|
| 664 | level_list[lev]=right_n; |
---|
| 665 | left.set(right_n, INVALID); |
---|
| 666 | } |
---|
| 667 | } else { |
---|
| 668 | if ( g->valid(left_n) ) { |
---|
| 669 | right.set(left_n, INVALID); |
---|
| 670 | } else { |
---|
| 671 | level_list[lev]=INVALID; |
---|
| 672 | } |
---|
| 673 | } |
---|
| 674 | //unlacing ends |
---|
| 675 | |
---|
| 676 | if ( !g->valid(level_list[lev]) ) { |
---|
| 677 | |
---|
| 678 | //gapping starts |
---|
| 679 | for (int i=lev; i!=k ; ) { |
---|
| 680 | Node v=level_list[++i]; |
---|
| 681 | while ( g->valid(v) ) { |
---|
| 682 | level.set(v,n); |
---|
| 683 | v=right[v]; |
---|
| 684 | } |
---|
| 685 | level_list[i]=INVALID; |
---|
| 686 | if ( !what_heur ) first[i]=INVALID; |
---|
| 687 | /*{ |
---|
| 688 | while ( !active[i].empty() ) { |
---|
| 689 | active[i].pop(); //FIXME: ezt szebben kene |
---|
| 690 | } |
---|
| 691 | }*/ |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | level.set(w,n); |
---|
| 695 | b=lev-1; |
---|
| 696 | k=b; |
---|
| 697 | //gapping ends |
---|
| 698 | |
---|
| 699 | } else { |
---|
| 700 | |
---|
| 701 | if ( newlevel == n ) level.set(w,n); |
---|
| 702 | else { |
---|
| 703 | level.set(w,++newlevel); |
---|
| 704 | next.set(w,first[newlevel]); |
---|
| 705 | first[newlevel]=w; |
---|
| 706 | // active[newlevel].push(w); |
---|
| 707 | if ( what_heur ) b=newlevel; |
---|
| 708 | if ( k < newlevel ) ++k; //now k=newlevel |
---|
| 709 | Node z=level_list[newlevel]; |
---|
| 710 | if ( g->valid(z) ) left.set(z,w); |
---|
| 711 | right.set(w,z); |
---|
| 712 | left.set(w,INVALID); |
---|
| 713 | level_list[newlevel]=w; |
---|
| 714 | } |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | } //relabel |
---|
| 718 | |
---|
| 719 | |
---|
| 720 | template<typename MapGraphWrapper> |
---|
| 721 | class DistanceMap { |
---|
| 722 | protected: |
---|
| 723 | const MapGraphWrapper* g; |
---|
| 724 | typename MapGraphWrapper::template NodeMap<int> dist; |
---|
| 725 | public: |
---|
| 726 | DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { } |
---|
| 727 | void set(const typename MapGraphWrapper::Node& n, int a) { |
---|
| 728 | dist.set(n, a); |
---|
| 729 | } |
---|
| 730 | int operator[](const typename MapGraphWrapper::Node& n) const { |
---|
| 731 | return dist[n]; |
---|
| 732 | } |
---|
| 733 | // int get(const typename MapGraphWrapper::Node& n) const { |
---|
| 734 | // return dist[n]; } |
---|
| 735 | // bool get(const typename MapGraphWrapper::Edge& e) const { |
---|
| 736 | // return (dist.get(g->tail(e))<dist.get(g->head(e))); } |
---|
| 737 | bool operator[](const typename MapGraphWrapper::Edge& e) const { |
---|
| 738 | return (dist[g->tail(e)]<dist[g->head(e)]); |
---|
| 739 | } |
---|
| 740 | }; |
---|
| 741 | |
---|
| 742 | }; |
---|
| 743 | |
---|
| 744 | |
---|
| 745 | template <typename Graph, typename Num, typename CapMap, typename FlowMap> |
---|
| 746 | void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe) |
---|
| 747 | { |
---|
| 748 | |
---|
| 749 | int heur0=(int)(H0*n); //time while running 'bound decrease' |
---|
| 750 | int heur1=(int)(H1*n); //time while running 'highest label' |
---|
| 751 | int heur=heur1; //starting time interval (#of relabels) |
---|
| 752 | int numrelabel=0; |
---|
| 753 | |
---|
| 754 | bool what_heur=1; |
---|
| 755 | //It is 0 in case 'bound decrease' and 1 in case 'highest label' |
---|
| 756 | |
---|
| 757 | bool end=false; |
---|
| 758 | //Needed for 'bound decrease', true means no active nodes are above bound |
---|
| 759 | //b. |
---|
| 760 | |
---|
| 761 | int k=n-2; //bound on the highest level under n containing a node |
---|
| 762 | int b=k; //bound on the highest level under n of an active node |
---|
| 763 | |
---|
| 764 | VecFirst first(n, INVALID); |
---|
| 765 | NNMap next(*g, INVALID); //maybe INVALID is not needed |
---|
| 766 | // VecStack active(n); |
---|
| 767 | |
---|
| 768 | NNMap left(*g, INVALID); |
---|
| 769 | NNMap right(*g, INVALID); |
---|
| 770 | VecNode level_list(n,INVALID); |
---|
| 771 | //List of the nodes in level i<n, set to n. |
---|
| 772 | |
---|
| 773 | NodeIt v; |
---|
| 774 | for(g->first(v); g->valid(v); g->next(v)) level.set(v,n); |
---|
| 775 | //setting each node to level n |
---|
| 776 | |
---|
| 777 | if ( fe == NO_FLOW ) { |
---|
| 778 | EdgeIt e; |
---|
| 779 | for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0); |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | switch (fe) { //computing the excess |
---|
| 783 | case PRE_FLOW: |
---|
| 784 | { |
---|
| 785 | NodeIt v; |
---|
| 786 | for(g->first(v); g->valid(v); g->next(v)) { |
---|
| 787 | Num exc=0; |
---|
| 788 | |
---|
| 789 | InEdgeIt e; |
---|
| 790 | for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e]; |
---|
| 791 | OutEdgeIt f; |
---|
| 792 | for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f]; |
---|
| 793 | |
---|
| 794 | excess.set(v,exc); |
---|
| 795 | |
---|
| 796 | //putting the active nodes into the stack |
---|
| 797 | int lev=level[v]; |
---|
| 798 | if ( exc > 0 && lev < n && v != t ) |
---|
| 799 | { |
---|
| 800 | next.set(v,first[lev]); |
---|
| 801 | first[lev]=v; |
---|
| 802 | } |
---|
| 803 | // active[lev].push(v); |
---|
| 804 | } |
---|
| 805 | break; |
---|
| 806 | } |
---|
| 807 | case GEN_FLOW: |
---|
| 808 | { |
---|
| 809 | NodeIt v; |
---|
| 810 | for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0); |
---|
| 811 | |
---|
| 812 | Num exc=0; |
---|
| 813 | InEdgeIt e; |
---|
| 814 | for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e]; |
---|
| 815 | OutEdgeIt f; |
---|
| 816 | for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f]; |
---|
| 817 | excess.set(t,exc); |
---|
| 818 | break; |
---|
| 819 | } |
---|
| 820 | case ZERO_FLOW: |
---|
| 821 | case NO_FLOW: |
---|
| 822 | { |
---|
| 823 | NodeIt v; |
---|
| 824 | for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0); |
---|
| 825 | break; |
---|
| 826 | } |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | preflowPreproc(fe, next, first,/*active*/ level_list, left, right); |
---|
| 830 | //End of preprocessing |
---|
| 831 | |
---|
| 832 | |
---|
| 833 | //Push/relabel on the highest level active nodes. |
---|
| 834 | while ( true ) { |
---|
| 835 | if ( b == 0 ) { |
---|
| 836 | if ( !what_heur && !end && k > 0 ) { |
---|
| 837 | b=k; |
---|
| 838 | end=true; |
---|
| 839 | } else break; |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | if ( !g->valid(first[b])/*active[b].empty()*/ ) --b; |
---|
| 843 | else { |
---|
| 844 | end=false; |
---|
| 845 | Node w=first[b]; |
---|
| 846 | first[b]=next[w]; |
---|
| 847 | /* Node w=active[b].top(); |
---|
| 848 | active[b].pop();*/ |
---|
| 849 | int newlevel=push(w,/*active*/next, first); |
---|
| 850 | if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list, |
---|
| 851 | left, right, b, k, what_heur); |
---|
| 852 | |
---|
| 853 | ++numrelabel; |
---|
| 854 | if ( numrelabel >= heur ) { |
---|
| 855 | numrelabel=0; |
---|
| 856 | if ( what_heur ) { |
---|
| 857 | what_heur=0; |
---|
| 858 | heur=heur0; |
---|
| 859 | end=false; |
---|
| 860 | } else { |
---|
| 861 | what_heur=1; |
---|
| 862 | heur=heur1; |
---|
| 863 | b=k; |
---|
| 864 | } |
---|
| 865 | } |
---|
| 866 | } |
---|
| 867 | } |
---|
| 868 | |
---|
| 869 | status=AFTER_PRE_FLOW_PHASE_1; |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | |
---|
| 873 | |
---|
| 874 | template <typename Graph, typename Num, typename CapMap, typename FlowMap> |
---|
| 875 | void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase2() |
---|
| 876 | { |
---|
| 877 | |
---|
| 878 | int k=n-2; //bound on the highest level under n containing a node |
---|
| 879 | int b=k; //bound on the highest level under n of an active node |
---|
| 880 | |
---|
| 881 | |
---|
| 882 | VecFirst first(n, INVALID); |
---|
| 883 | NNMap next(*g, INVALID); //maybe INVALID is not needed |
---|
| 884 | // VecStack active(n); |
---|
| 885 | level.set(s,0); |
---|
| 886 | std::queue<Node> bfs_queue; |
---|
| 887 | bfs_queue.push(s); |
---|
| 888 | |
---|
| 889 | while (!bfs_queue.empty()) { |
---|
| 890 | |
---|
| 891 | Node v=bfs_queue.front(); |
---|
| 892 | bfs_queue.pop(); |
---|
| 893 | int l=level[v]+1; |
---|
| 894 | |
---|
| 895 | InEdgeIt e; |
---|
| 896 | for(g->first(e,v); g->valid(e); g->next(e)) { |
---|
| 897 | if ( (*capacity)[e] <= (*flow)[e] ) continue; |
---|
| 898 | Node u=g->tail(e); |
---|
| 899 | if ( level[u] >= n ) { |
---|
| 900 | bfs_queue.push(u); |
---|
| 901 | level.set(u, l); |
---|
| 902 | if ( excess[u] > 0 ) { |
---|
| 903 | next.set(u,first[l]); |
---|
| 904 | first[l]=u; |
---|
| 905 | //active[l].push(u); |
---|
| 906 | } |
---|
| 907 | } |
---|
| 908 | } |
---|
| 909 | |
---|
| 910 | OutEdgeIt f; |
---|
| 911 | for(g->first(f,v); g->valid(f); g->next(f)) { |
---|
| 912 | if ( 0 >= (*flow)[f] ) continue; |
---|
| 913 | Node u=g->head(f); |
---|
| 914 | if ( level[u] >= n ) { |
---|
| 915 | bfs_queue.push(u); |
---|
| 916 | level.set(u, l); |
---|
| 917 | if ( excess[u] > 0 ) { |
---|
| 918 | next.set(u,first[l]); |
---|
| 919 | first[l]=u; |
---|
| 920 | //active[l].push(u); |
---|
| 921 | } |
---|
| 922 | } |
---|
| 923 | } |
---|
| 924 | } |
---|
| 925 | b=n-2; |
---|
| 926 | |
---|
| 927 | while ( true ) { |
---|
| 928 | |
---|
| 929 | if ( b == 0 ) break; |
---|
| 930 | |
---|
| 931 | if ( !g->valid(first[b])/*active[b].empty()*/ ) --b; |
---|
| 932 | else { |
---|
| 933 | |
---|
| 934 | Node w=first[b]; |
---|
| 935 | first[b]=next[w]; |
---|
| 936 | /* Node w=active[b].top(); |
---|
| 937 | active[b].pop();*/ |
---|
| 938 | int newlevel=push(w,next, first/*active*/); |
---|
| 939 | |
---|
| 940 | //relabel |
---|
| 941 | if ( excess[w] > 0 ) { |
---|
| 942 | level.set(w,++newlevel); |
---|
| 943 | next.set(w,first[newlevel]); |
---|
| 944 | first[newlevel]=w; |
---|
| 945 | //active[newlevel].push(w); |
---|
| 946 | b=newlevel; |
---|
| 947 | } |
---|
| 948 | } // if stack[b] is nonempty |
---|
| 949 | } // while(true) |
---|
| 950 | |
---|
| 951 | status=AFTER_PRE_FLOW_PHASE_2; |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | |
---|
| 955 | |
---|
| 956 | template <typename Graph, typename Num, typename CapMap, typename FlowMap> |
---|
| 957 | bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath() |
---|
| 958 | { |
---|
| 959 | ResGW res_graph(*g, *capacity, *flow); |
---|
| 960 | bool _augment=false; |
---|
| 961 | |
---|
| 962 | //ReachedMap level(res_graph); |
---|
| 963 | FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); |
---|
| 964 | BfsIterator<ResGW, ReachedMap> bfs(res_graph, level); |
---|
| 965 | bfs.pushAndSetReached(s); |
---|
| 966 | |
---|
| 967 | typename ResGW::template NodeMap<ResGWEdge> pred(res_graph); |
---|
| 968 | pred.set(s, INVALID); |
---|
| 969 | |
---|
| 970 | typename ResGW::template NodeMap<Num> free(res_graph); |
---|
| 971 | |
---|
| 972 | //searching for augmenting path |
---|
| 973 | while ( !bfs.finished() ) { |
---|
| 974 | ResGWOutEdgeIt e=bfs; |
---|
| 975 | if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { |
---|
| 976 | Node v=res_graph.tail(e); |
---|
| 977 | Node w=res_graph.head(e); |
---|
| 978 | pred.set(w, e); |
---|
| 979 | if (res_graph.valid(pred[v])) { |
---|
| 980 | free.set(w, std::min(free[v], res_graph.resCap(e))); |
---|
| 981 | } else { |
---|
| 982 | free.set(w, res_graph.resCap(e)); |
---|
| 983 | } |
---|
| 984 | if (res_graph.head(e)==t) { _augment=true; break; } |
---|
| 985 | } |
---|
| 986 | |
---|
| 987 | ++bfs; |
---|
| 988 | } //end of searching augmenting path |
---|
| 989 | |
---|
| 990 | if (_augment) { |
---|
| 991 | Node n=t; |
---|
| 992 | Num augment_value=free[t]; |
---|
| 993 | while (res_graph.valid(pred[n])) { |
---|
| 994 | ResGWEdge e=pred[n]; |
---|
| 995 | res_graph.augment(e, augment_value); |
---|
| 996 | n=res_graph.tail(e); |
---|
| 997 | } |
---|
| 998 | } |
---|
| 999 | |
---|
| 1000 | status=AFTER_AUGMENTING; |
---|
| 1001 | return _augment; |
---|
| 1002 | } |
---|
| 1003 | |
---|
| 1004 | |
---|
| 1005 | template <typename Graph, typename Num, typename CapMap, typename FlowMap> |
---|
| 1006 | bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2() |
---|
| 1007 | { |
---|
| 1008 | ResGW res_graph(*g, *capacity, *flow); |
---|
| 1009 | bool _augment=false; |
---|
| 1010 | |
---|
| 1011 | if (status!=AFTER_FAST_AUGMENTING) { |
---|
| 1012 | FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); |
---|
| 1013 | number_of_augmentations=1; |
---|
| 1014 | } else { |
---|
| 1015 | ++number_of_augmentations; |
---|
| 1016 | } |
---|
| 1017 | TrickyReachedMap<ReachedMap> |
---|
| 1018 | tricky_reached_map(level, number_of_augmentations); |
---|
| 1019 | //ReachedMap level(res_graph); |
---|
| 1020 | // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); |
---|
| 1021 | BfsIterator<ResGW, TrickyReachedMap<ReachedMap> > |
---|
| 1022 | bfs(res_graph, tricky_reached_map); |
---|
| 1023 | bfs.pushAndSetReached(s); |
---|
| 1024 | |
---|
| 1025 | typename ResGW::template NodeMap<ResGWEdge> pred(res_graph); |
---|
| 1026 | pred.set(s, INVALID); |
---|
| 1027 | |
---|
| 1028 | typename ResGW::template NodeMap<Num> free(res_graph); |
---|
| 1029 | |
---|
| 1030 | //searching for augmenting path |
---|
| 1031 | while ( !bfs.finished() ) { |
---|
| 1032 | ResGWOutEdgeIt e=bfs; |
---|
| 1033 | if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { |
---|
| 1034 | Node v=res_graph.tail(e); |
---|
| 1035 | Node w=res_graph.head(e); |
---|
| 1036 | pred.set(w, e); |
---|
| 1037 | if (res_graph.valid(pred[v])) { |
---|
| 1038 | free.set(w, std::min(free[v], res_graph.resCap(e))); |
---|
| 1039 | } else { |
---|
| 1040 | free.set(w, res_graph.resCap(e)); |
---|
| 1041 | } |
---|
| 1042 | if (res_graph.head(e)==t) { _augment=true; break; } |
---|
| 1043 | } |
---|
| 1044 | |
---|
| 1045 | ++bfs; |
---|
| 1046 | } //end of searching augmenting path |
---|
| 1047 | |
---|
| 1048 | if (_augment) { |
---|
| 1049 | Node n=t; |
---|
| 1050 | Num augment_value=free[t]; |
---|
| 1051 | while (res_graph.valid(pred[n])) { |
---|
| 1052 | ResGWEdge e=pred[n]; |
---|
| 1053 | res_graph.augment(e, augment_value); |
---|
| 1054 | n=res_graph.tail(e); |
---|
| 1055 | } |
---|
| 1056 | } |
---|
| 1057 | |
---|
| 1058 | status=AFTER_FAST_AUGMENTING; |
---|
| 1059 | return _augment; |
---|
| 1060 | } |
---|
| 1061 | |
---|
| 1062 | |
---|
| 1063 | template <typename Graph, typename Num, typename CapMap, typename FlowMap> |
---|
| 1064 | template<typename MutableGraph> |
---|
| 1065 | bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow() |
---|
| 1066 | { |
---|
| 1067 | typedef MutableGraph MG; |
---|
| 1068 | bool _augment=false; |
---|
| 1069 | |
---|
| 1070 | ResGW res_graph(*g, *capacity, *flow); |
---|
| 1071 | |
---|
| 1072 | //bfs for distances on the residual graph |
---|
| 1073 | //ReachedMap level(res_graph); |
---|
| 1074 | FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); |
---|
| 1075 | BfsIterator<ResGW, ReachedMap> bfs(res_graph, level); |
---|
| 1076 | bfs.pushAndSetReached(s); |
---|
| 1077 | typename ResGW::template NodeMap<int> |
---|
| 1078 | dist(res_graph); //filled up with 0's |
---|
| 1079 | |
---|
| 1080 | //F will contain the physical copy of the residual graph |
---|
| 1081 | //with the set of edges which are on shortest paths |
---|
| 1082 | MG F; |
---|
| 1083 | typename ResGW::template NodeMap<typename MG::Node> |
---|
| 1084 | res_graph_to_F(res_graph); |
---|
| 1085 | { |
---|
| 1086 | typename ResGW::NodeIt n; |
---|
| 1087 | for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) { |
---|
| 1088 | res_graph_to_F.set(n, F.addNode()); |
---|
| 1089 | } |
---|
| 1090 | } |
---|
| 1091 | |
---|
| 1092 | typename MG::Node sF=res_graph_to_F[s]; |
---|
| 1093 | typename MG::Node tF=res_graph_to_F[t]; |
---|
| 1094 | typename MG::template EdgeMap<ResGWEdge> original_edge(F); |
---|
| 1095 | typename MG::template EdgeMap<Num> residual_capacity(F); |
---|
| 1096 | |
---|
| 1097 | while ( !bfs.finished() ) { |
---|
| 1098 | ResGWOutEdgeIt e=bfs; |
---|
| 1099 | if (res_graph.valid(e)) { |
---|
| 1100 | if (bfs.isBNodeNewlyReached()) { |
---|
| 1101 | dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1); |
---|
| 1102 | typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], |
---|
| 1103 | res_graph_to_F[res_graph.head(e)]); |
---|
| 1104 | original_edge.update(); |
---|
| 1105 | original_edge.set(f, e); |
---|
| 1106 | residual_capacity.update(); |
---|
| 1107 | residual_capacity.set(f, res_graph.resCap(e)); |
---|
| 1108 | } else { |
---|
| 1109 | if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) { |
---|
| 1110 | typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], |
---|
| 1111 | res_graph_to_F[res_graph.head(e)]); |
---|
| 1112 | original_edge.update(); |
---|
| 1113 | original_edge.set(f, e); |
---|
| 1114 | residual_capacity.update(); |
---|
| 1115 | residual_capacity.set(f, res_graph.resCap(e)); |
---|
| 1116 | } |
---|
| 1117 | } |
---|
| 1118 | } |
---|
| 1119 | ++bfs; |
---|
| 1120 | } //computing distances from s in the residual graph |
---|
| 1121 | |
---|
| 1122 | bool __augment=true; |
---|
| 1123 | |
---|
| 1124 | while (__augment) { |
---|
| 1125 | __augment=false; |
---|
| 1126 | //computing blocking flow with dfs |
---|
| 1127 | DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F); |
---|
| 1128 | typename MG::template NodeMap<typename MG::Edge> pred(F); |
---|
| 1129 | pred.set(sF, INVALID); |
---|
| 1130 | //invalid iterators for sources |
---|
| 1131 | |
---|
| 1132 | typename MG::template NodeMap<Num> free(F); |
---|
| 1133 | |
---|
| 1134 | dfs.pushAndSetReached(sF); |
---|
| 1135 | while (!dfs.finished()) { |
---|
| 1136 | ++dfs; |
---|
| 1137 | if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) { |
---|
| 1138 | if (dfs.isBNodeNewlyReached()) { |
---|
| 1139 | typename MG::Node v=F.aNode(dfs); |
---|
| 1140 | typename MG::Node w=F.bNode(dfs); |
---|
| 1141 | pred.set(w, dfs); |
---|
| 1142 | if (F.valid(pred[v])) { |
---|
| 1143 | free.set(w, std::min(free[v], residual_capacity[dfs])); |
---|
| 1144 | } else { |
---|
| 1145 | free.set(w, residual_capacity[dfs]); |
---|
| 1146 | } |
---|
| 1147 | if (w==tF) { |
---|
| 1148 | __augment=true; |
---|
| 1149 | _augment=true; |
---|
| 1150 | break; |
---|
| 1151 | } |
---|
| 1152 | |
---|
| 1153 | } else { |
---|
| 1154 | F.erase(/*typename MG::OutEdgeIt*/(dfs)); |
---|
| 1155 | } |
---|
| 1156 | } |
---|
| 1157 | } |
---|
| 1158 | |
---|
| 1159 | if (__augment) { |
---|
| 1160 | typename MG::Node n=tF; |
---|
| 1161 | Num augment_value=free[tF]; |
---|
| 1162 | while (F.valid(pred[n])) { |
---|
| 1163 | typename MG::Edge e=pred[n]; |
---|
| 1164 | res_graph.augment(original_edge[e], augment_value); |
---|
| 1165 | n=F.tail(e); |
---|
| 1166 | if (residual_capacity[e]==augment_value) |
---|
| 1167 | F.erase(e); |
---|
| 1168 | else |
---|
| 1169 | residual_capacity.set(e, residual_capacity[e]-augment_value); |
---|
| 1170 | } |
---|
| 1171 | } |
---|
| 1172 | |
---|
| 1173 | } |
---|
| 1174 | |
---|
| 1175 | status=AFTER_AUGMENTING; |
---|
| 1176 | return _augment; |
---|
| 1177 | } |
---|
| 1178 | |
---|
| 1179 | |
---|
| 1180 | |
---|
| 1181 | |
---|
| 1182 | template <typename Graph, typename Num, typename CapMap, typename FlowMap> |
---|
| 1183 | bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2() |
---|
| 1184 | { |
---|
| 1185 | bool _augment=false; |
---|
| 1186 | |
---|
| 1187 | ResGW res_graph(*g, *capacity, *flow); |
---|
| 1188 | |
---|
| 1189 | //ReachedMap level(res_graph); |
---|
| 1190 | FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); |
---|
| 1191 | BfsIterator<ResGW, ReachedMap> bfs(res_graph, level); |
---|
| 1192 | |
---|
| 1193 | bfs.pushAndSetReached(s); |
---|
| 1194 | DistanceMap<ResGW> dist(res_graph); |
---|
| 1195 | while ( !bfs.finished() ) { |
---|
| 1196 | ResGWOutEdgeIt e=bfs; |
---|
| 1197 | if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { |
---|
| 1198 | dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1); |
---|
| 1199 | } |
---|
| 1200 | ++bfs; |
---|
| 1201 | } //computing distances from s in the residual graph |
---|
| 1202 | |
---|
| 1203 | //Subgraph containing the edges on some shortest paths |
---|
| 1204 | ConstMap<typename ResGW::Node, bool> true_map(true); |
---|
| 1205 | typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>, |
---|
| 1206 | DistanceMap<ResGW> > FilterResGW; |
---|
| 1207 | FilterResGW filter_res_graph(res_graph, true_map, dist); |
---|
| 1208 | |
---|
| 1209 | //Subgraph, which is able to delete edges which are already |
---|
| 1210 | //met by the dfs |
---|
| 1211 | typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt> |
---|
| 1212 | first_out_edges(filter_res_graph); |
---|
| 1213 | typename FilterResGW::NodeIt v; |
---|
| 1214 | for(filter_res_graph.first(v); filter_res_graph.valid(v); |
---|
| 1215 | filter_res_graph.next(v)) |
---|
| 1216 | { |
---|
| 1217 | typename FilterResGW::OutEdgeIt e; |
---|
| 1218 | filter_res_graph.first(e, v); |
---|
| 1219 | first_out_edges.set(v, e); |
---|
| 1220 | } |
---|
| 1221 | typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW:: |
---|
| 1222 | template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW; |
---|
| 1223 | ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges); |
---|
| 1224 | |
---|
| 1225 | bool __augment=true; |
---|
| 1226 | |
---|
| 1227 | while (__augment) { |
---|
| 1228 | |
---|
| 1229 | __augment=false; |
---|
| 1230 | //computing blocking flow with dfs |
---|
| 1231 | DfsIterator< ErasingResGW, |
---|
| 1232 | typename ErasingResGW::template NodeMap<bool> > |
---|
| 1233 | dfs(erasing_res_graph); |
---|
| 1234 | typename ErasingResGW:: |
---|
| 1235 | template NodeMap<typename ErasingResGW::OutEdgeIt> |
---|
| 1236 | pred(erasing_res_graph); |
---|
| 1237 | pred.set(s, INVALID); |
---|
| 1238 | //invalid iterators for sources |
---|
| 1239 | |
---|
| 1240 | typename ErasingResGW::template NodeMap<Num> |
---|
| 1241 | free1(erasing_res_graph); |
---|
| 1242 | |
---|
| 1243 | dfs.pushAndSetReached |
---|
| 1244 | ///\bug hugo 0.2 |
---|
| 1245 | (typename ErasingResGW::Node |
---|
| 1246 | (typename FilterResGW::Node |
---|
| 1247 | (typename ResGW::Node(s) |
---|
| 1248 | ) |
---|
| 1249 | ) |
---|
| 1250 | ); |
---|
| 1251 | while (!dfs.finished()) { |
---|
| 1252 | ++dfs; |
---|
| 1253 | if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs))) |
---|
| 1254 | { |
---|
| 1255 | if (dfs.isBNodeNewlyReached()) { |
---|
| 1256 | |
---|
| 1257 | typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs); |
---|
| 1258 | typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs); |
---|
| 1259 | |
---|
| 1260 | pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs)); |
---|
| 1261 | if (erasing_res_graph.valid(pred[v])) { |
---|
| 1262 | free1.set |
---|
| 1263 | (w, std::min(free1[v], res_graph.resCap |
---|
| 1264 | (typename ErasingResGW::OutEdgeIt(dfs)))); |
---|
| 1265 | } else { |
---|
| 1266 | free1.set |
---|
| 1267 | (w, res_graph.resCap |
---|
| 1268 | (typename ErasingResGW::OutEdgeIt(dfs))); |
---|
| 1269 | } |
---|
| 1270 | |
---|
| 1271 | if (w==t) { |
---|
| 1272 | __augment=true; |
---|
| 1273 | _augment=true; |
---|
| 1274 | break; |
---|
| 1275 | } |
---|
| 1276 | } else { |
---|
| 1277 | erasing_res_graph.erase(dfs); |
---|
| 1278 | } |
---|
| 1279 | } |
---|
| 1280 | } |
---|
| 1281 | |
---|
| 1282 | if (__augment) { |
---|
| 1283 | typename ErasingResGW::Node |
---|
| 1284 | n=typename FilterResGW::Node(typename ResGW::Node(t)); |
---|
| 1285 | // typename ResGW::NodeMap<Num> a(res_graph); |
---|
| 1286 | // typename ResGW::Node b; |
---|
| 1287 | // Num j=a[b]; |
---|
| 1288 | // typename FilterResGW::NodeMap<Num> a1(filter_res_graph); |
---|
| 1289 | // typename FilterResGW::Node b1; |
---|
| 1290 | // Num j1=a1[b1]; |
---|
| 1291 | // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph); |
---|
| 1292 | // typename ErasingResGW::Node b2; |
---|
| 1293 | // Num j2=a2[b2]; |
---|
| 1294 | Num augment_value=free1[n]; |
---|
| 1295 | while (erasing_res_graph.valid(pred[n])) { |
---|
| 1296 | typename ErasingResGW::OutEdgeIt e=pred[n]; |
---|
| 1297 | res_graph.augment(e, augment_value); |
---|
| 1298 | n=erasing_res_graph.tail(e); |
---|
| 1299 | if (res_graph.resCap(e)==0) |
---|
| 1300 | erasing_res_graph.erase(e); |
---|
| 1301 | } |
---|
| 1302 | } |
---|
| 1303 | |
---|
| 1304 | } //while (__augment) |
---|
| 1305 | |
---|
| 1306 | status=AFTER_AUGMENTING; |
---|
| 1307 | return _augment; |
---|
| 1308 | } |
---|
| 1309 | |
---|
| 1310 | |
---|
| 1311 | } //namespace hugo |
---|
| 1312 | |
---|
| 1313 | #endif //HUGO_MAX_FLOW_H |
---|
| 1314 | |
---|
| 1315 | |
---|
| 1316 | |
---|
| 1317 | |
---|