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