2 #ifndef LEMON_AUGMENTING_FLOW_H
3 #define LEMON_AUGMENTING_FLOW_H
8 #include <lemon/graph_wrapper.h>
10 #include <lemon/invalid.h>
11 #include <lemon/maps.h>
12 #include <lemon/tight_edge_filter_map.h>
15 /// \brief Maximum flow algorithms.
22 /// Class for augmenting path flow algorithms.
24 /// This class provides various algorithms for finding a flow of
25 /// maximum value in a directed graph. The \e source node, the \e
26 /// target node, the \e capacity of the edges and the \e starting \e
27 /// flow value of the edges should be passed to the algorithm through the
29 // /// 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.
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
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
45 template <typename Graph, typename Num,
46 typename CapMap=typename Graph::template EdgeMap<Num>,
47 typename FlowMap=typename Graph::template EdgeMap<Num> >
48 class AugmentingFlow {
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;
59 const CapMap* capacity;
61 // int n; //the number of nodes of G
62 typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
63 //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
64 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
65 typedef typename ResGW::Edge ResGWEdge;
66 //typedef typename ResGW::template NodeMap<bool> ReachedMap;
67 typedef typename Graph::template NodeMap<int> ReachedMap;
69 //level works as a bool map in augmenting path algorithms and is
70 //used by bfs for storing reached information. In preflow, it
71 //shows the levels of nodes.
75 ///Indicates the property of the starting flow.
77 ///Indicates the property of the starting flow. The meanings are as follows:
78 ///- \c ZERO_FLOW: constant zero flow
79 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
80 ///the sum of the out-flows in every node except the \e source and
82 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
83 ///least the sum of the out-flows in every node except the \e source.
84 ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
85 ///set to the constant zero flow in the beginning of the algorithm in this case.
96 AFTER_FAST_AUGMENTING,
97 AFTER_PRE_FLOW_PHASE_1,
98 AFTER_PRE_FLOW_PHASE_2
101 /// Don not needle this flag only if necessary.
103 int number_of_augmentations;
106 template<typename IntMap>
107 class TrickyReachedMap {
110 int* number_of_augmentations;
112 TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
113 map(&_map), number_of_augmentations(&_number_of_augmentations) { }
114 void set(const Node& n, bool b) {
116 map->set(n, *number_of_augmentations);
118 map->set(n, *number_of_augmentations-1);
120 bool operator[](const Node& n) const {
121 return (*map)[n]==*number_of_augmentations;
125 AugmentingFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
127 g(&_G), s(_s), t(_t), capacity(&_capacity),
128 flow(&_flow), //n(_G.nodeNum()),
129 level(_G), //excess(_G,0),
130 status(AFTER_NOTHING), number_of_augmentations(0) { }
132 /// Starting from a flow, this method searches for an augmenting path
133 /// according to the Edmonds-Karp algorithm
134 /// and augments the flow on if any.
135 /// The return value shows if the augmentation was succesful.
136 bool augmentOnShortestPath();
137 bool augmentOnShortestPath2();
139 /// Starting from a flow, this method searches for an augmenting blocking
140 /// flow according to Dinits' algorithm and augments the flow on if any.
141 /// The blocking flow is computed in a physically constructed
142 /// residual graph of type \c Mutablegraph.
143 /// The return value show sif the augmentation was succesful.
144 template<typename MutableGraph> bool augmentOnBlockingFlow();
146 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
147 /// residual graph is not constructed physically.
148 /// The return value shows if the augmentation was succesful.
149 bool augmentOnBlockingFlow2();
151 template<typename _CutMap>
152 void actMinCut(_CutMap& M) const {
155 case AFTER_PRE_FLOW_PHASE_1:
156 // std::cout << "AFTER_PRE_FLOW_PHASE_1" << std::endl;
157 // for(g->first(v); g->valid(v); g->next(v)) {
158 // if (level[v] < n) {
165 case AFTER_PRE_FLOW_PHASE_2:
166 // std::cout << "AFTER_PRE_FLOW_PHASE_2" << std::endl;
169 // std::cout << "AFTER_NOTHING" << std::endl;
172 case AFTER_AUGMENTING:
173 // std::cout << "AFTER_AUGMENTING" << std::endl;
174 for(g->first(v); v!=INVALID; ++v) {
182 case AFTER_FAST_AUGMENTING:
183 // std::cout << "AFTER_FAST_AUGMENTING" << std::endl;
184 for(g->first(v); v!=INVALID; ++v) {
185 if (level[v]==number_of_augmentations) {
195 template<typename _CutMap>
196 void minMinCut(_CutMap& M) const {
197 std::queue<Node> queue;
202 while (!queue.empty()) {
203 Node w=queue.front();
207 for(g->first(e,w) ; e!=INVALID; ++e) {
209 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
216 for(g->first(f,w) ; f!=INVALID; ++f) {
218 if (!M[v] && (*flow)[f] > 0 ) {
226 template<typename _CutMap>
227 void minMinCut2(_CutMap& M) const {
228 ResGW res_graph(*g, *capacity, *flow);
229 BfsIterator<ResGW, _CutMap> bfs(res_graph, M);
230 bfs.pushAndSetReached(s);
231 while (!bfs.finished()) ++bfs;
234 Num flowValue() const {
236 for (InEdgeIt e(*g, t); e!=INVALID; ++e) a+=(*flow)[e];
237 for (OutEdgeIt e(*g, t); e!=INVALID; ++e) a-=(*flow)[e];
239 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
246 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
247 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
249 ResGW res_graph(*g, *capacity, *flow);
250 typename ResGW::ResCap res_cap(res_graph);
254 //ReachedMap level(res_graph);
255 for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
256 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
257 bfs.pushAndSetReached(s);
259 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
260 pred.set(s, INVALID);
262 typename ResGW::template NodeMap<Num> free(res_graph);
264 //searching for augmenting path
265 while ( !bfs.finished() ) {
267 if (e!=INVALID && bfs.isBNodeNewlyReached()) {
268 Node v=res_graph.tail(e);
269 Node w=res_graph.head(e);
271 if (pred[v]!=INVALID) {
272 free.set(w, std::min(free[v], res_cap[e]));
274 free.set(w, res_cap[e]);
276 if (res_graph.head(e)==t) { _augment=true; break; }
280 } //end of searching augmenting path
284 Num augment_value=free[t];
285 while (pred[n]!=INVALID) {
287 res_graph.augment(e, augment_value);
292 status=AFTER_AUGMENTING;
296 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
297 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
299 ResGW res_graph(*g, *capacity, *flow);
300 typename ResGW::ResCap res_cap(res_graph);
304 if (status!=AFTER_FAST_AUGMENTING) {
305 for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
306 number_of_augmentations=1;
308 ++number_of_augmentations;
310 TrickyReachedMap<ReachedMap>
311 tricky_reached_map(level, number_of_augmentations);
312 //ReachedMap level(res_graph);
313 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
314 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
315 bfs(res_graph, tricky_reached_map);
316 bfs.pushAndSetReached(s);
318 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
319 pred.set(s, INVALID);
321 typename ResGW::template NodeMap<Num> free(res_graph);
323 //searching for augmenting path
324 while ( !bfs.finished() ) {
326 if (e!=INVALID && bfs.isBNodeNewlyReached()) {
327 Node v=res_graph.tail(e);
328 Node w=res_graph.head(e);
330 if (pred[v]!=INVALID) {
331 free.set(w, std::min(free[v], res_cap[e]));
333 free.set(w, res_cap[e]);
335 if (res_graph.head(e)==t) { _augment=true; break; }
339 } //end of searching augmenting path
343 Num augment_value=free[t];
344 while (pred[n]!=INVALID) {
346 res_graph.augment(e, augment_value);
351 status=AFTER_FAST_AUGMENTING;
356 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
357 template<typename MutableGraph>
358 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
360 typedef MutableGraph MG;
363 ResGW res_graph(*g, *capacity, *flow);
364 typename ResGW::ResCap res_cap(res_graph);
366 //bfs for distances on the residual graph
367 //ReachedMap level(res_graph);
368 for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
369 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
370 bfs.pushAndSetReached(s);
371 typename ResGW::template NodeMap<int>
372 dist(res_graph); //filled up with 0's
374 //F will contain the physical copy of the residual graph
375 //with the set of edges which are on shortest paths
377 typename ResGW::template NodeMap<typename MG::Node>
378 res_graph_to_F(res_graph);
380 typename ResGW::NodeIt n;
381 for(res_graph.first(n); n!=INVALID; ++n)
382 res_graph_to_F.set(n, F.addNode());
385 typename MG::Node sF=res_graph_to_F[s];
386 typename MG::Node tF=res_graph_to_F[t];
387 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
388 typename MG::template EdgeMap<Num> residual_capacity(F);
390 while ( !bfs.finished() ) {
393 if (bfs.isBNodeNewlyReached()) {
394 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
395 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
396 res_graph_to_F[res_graph.head(e)]);
397 //original_edge.update();
398 original_edge.set(f, e);
399 //residual_capacity.update();
400 residual_capacity.set(f, res_cap[e]);
402 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
403 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
404 res_graph_to_F[res_graph.head(e)]);
405 //original_edge.update();
406 original_edge.set(f, e);
407 //residual_capacity.update();
408 residual_capacity.set(f, res_cap[e]);
413 } //computing distances from s in the residual graph
419 //computing blocking flow with dfs
420 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
421 typename MG::template NodeMap<typename MG::Edge> pred(F);
422 pred.set(sF, INVALID);
423 //invalid iterators for sources
425 typename MG::template NodeMap<Num> free(F);
427 dfs.pushAndSetReached(sF);
428 while (!dfs.finished()) {
430 if (typename MG::Edge(dfs)!=INVALID) {
431 if (dfs.isBNodeNewlyReached()) {
432 typename MG::Node v=F.tail(dfs);
433 typename MG::Node w=F.head(dfs);
435 if (pred[v]!=INVALID) {
436 free.set(w, std::min(free[v], residual_capacity[dfs]));
438 free.set(w, residual_capacity[dfs]);
447 F.erase(typename MG::Edge(dfs));
453 typename MG::Node n=tF;
454 Num augment_value=free[tF];
455 while (pred[n]!=INVALID) {
456 typename MG::Edge e=pred[n];
457 res_graph.augment(original_edge[e], augment_value);
459 if (residual_capacity[e]==augment_value)
462 residual_capacity.set(e, residual_capacity[e]-augment_value);
468 status=AFTER_AUGMENTING;
472 /// Blocking flow augmentation without constructing the layered
473 /// graph physically in which the blocking flow is computed.
474 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
475 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
479 ResGW res_graph(*g, *capacity, *flow);
480 typename ResGW::ResCap res_cap(res_graph);
482 //Potential map, for distances from s
483 typename ResGW::template NodeMap<int> potential(res_graph, 0);
484 typedef ConstMap<typename ResGW::Edge, int> Const1Map;
485 Const1Map const_1_map(1);
486 TightEdgeFilterMap<ResGW, typename ResGW::template NodeMap<int>,
487 Const1Map> tight_edge_filter(res_graph, potential, const_1_map);
489 for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
490 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
491 bfs.pushAndSetReached(s);
493 //computing distances from s in the residual graph
494 while ( !bfs.finished() ) {
496 if (e!=INVALID && bfs.isBNodeNewlyReached())
497 potential.set(res_graph.head(e), potential[res_graph.tail(e)]+1);
501 //Subgraph containing the edges on some shortest paths
503 ConstMap<typename ResGW::Node, bool> true_map(true);
504 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
505 TightEdgeFilterMap<ResGW, typename ResGW::template NodeMap<int>,
506 Const1Map> > FilterResGW;
507 FilterResGW filter_res_graph(res_graph, true_map, tight_edge_filter);
509 //Subgraph, which is able to delete edges which are already
511 typename FilterResGW::template NodeMap<typename FilterResGW::Edge>
512 first_out_edges(filter_res_graph);
513 for (typename FilterResGW::NodeIt v(filter_res_graph); v!=INVALID; ++v)
515 (v, typename FilterResGW::OutEdgeIt(filter_res_graph, v));
517 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
518 template NodeMap<typename FilterResGW::Edge> > ErasingResGW;
519 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
526 //computing blocking flow with dfs
527 DfsIterator< ErasingResGW,
528 typename ErasingResGW::template NodeMap<bool> >
529 dfs(erasing_res_graph);
530 typename ErasingResGW::
531 template NodeMap<typename ErasingResGW::Edge> pred(erasing_res_graph);
532 pred.set(s, INVALID);
533 //invalid iterators for sources
535 typename ErasingResGW::template NodeMap<Num>
536 free1(erasing_res_graph);
538 dfs.pushAndSetReached
540 (typename ErasingResGW::Node
541 (typename FilterResGW::Node
542 (typename ResGW::Node(s)
547 while (!dfs.finished()) {
549 if (typename ErasingResGW::Edge(dfs)!=INVALID) {
550 if (dfs.isBNodeNewlyReached()) {
552 typename ErasingResGW::Node v=erasing_res_graph.tail(dfs);
553 typename ErasingResGW::Node w=erasing_res_graph.head(dfs);
555 pred.set(w, typename ErasingResGW::Edge(dfs));
556 if (pred[v]!=INVALID) {
558 (w, std::min(free1[v], res_cap
559 [typename ErasingResGW::Edge(dfs)]));
563 [typename ErasingResGW::Edge(dfs)]);
572 erasing_res_graph.erase(dfs);
578 typename ErasingResGW::Node
579 n=typename FilterResGW::Node(typename ResGW::Node(t));
580 Num augment_value=free1[n];
581 while (pred[n]!=INVALID) {
582 typename ErasingResGW::Edge e=pred[n];
583 res_graph.augment(e, augment_value);
584 n=erasing_res_graph.tail(e);
586 erasing_res_graph.erase(e);
590 } //while (__augment)
592 status=AFTER_AUGMENTING;
599 #endif //LEMON_AUGMENTING_FLOW_H