1 // -*- C++ -*- |
1 // -*- C++ -*- |
2 /* |
2 /* |
3 preflow_hl3.h |
3 preflow_hl3.h |
4 by jacint. |
4 by jacint. |
5 Runs the highest label variant of the preflow push algorithm with |
5 |
6 running time O(n^2\sqrt(m)), with the felszippantos 'empty level' |
6 Heuristics: |
7 and with the two-phase heuristic: if there is no active node of |
7 |
8 level at most n, then we go into phase 1, do a bfs |
8 suck gap : if there is a gap, then we put all |
9 from s, and flow the excess back to s. |
9 nodes reachable from the relabelled node to level n |
10 |
10 2 phase |
11 In phase 1 we shift everything downwards by n. |
11 highest label |
12 |
12 |
13 'A' is a parameter for the empty_level heuristic |
13 The constructor runs the algorithm. |
14 |
14 |
15 Member functions: |
15 Members: |
16 |
16 |
17 void run() : runs the algorithm |
17 T maxFlow() : returns the value of a maximum flow |
18 |
18 |
19 The following functions should be used after run() was already run. |
19 T flowOnEdge(EdgeIt e) : for a fixed maximum flow x it returns x(e) |
20 |
20 |
21 T maxflow() : returns the value of a maximum flow |
21 FlowMap Flow() : returns the fixed maximum flow x |
22 |
22 |
23 T flowonedge(EdgeIt e) : for a fixed maximum flow x it returns x(e) |
23 void minCut(CutMap& M) : sets M to the characteristic vector of a |
24 |
|
25 FlowMap allflow() : returns the fixed maximum flow x |
|
26 |
|
27 void mincut(CutMap& M) : sets M to the characteristic vector of a |
|
28 minimum cut. M should be a map of bools initialized to false. |
24 minimum cut. M should be a map of bools initialized to false. |
29 |
25 |
30 void min_mincut(CutMap& M) : sets M to the characteristic vector of the |
26 void minMinCut(CutMap& M) : sets M to the characteristic vector of the |
31 minimum min cut. M should be a map of bools initialized to false. |
27 minimum min cut. M should be a map of bools initialized to false. |
32 |
28 |
33 void max_mincut(CutMap& M) : sets M to the characteristic vector of the |
29 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the |
34 maximum min cut. M should be a map of bools initialized to false. |
30 maximum min cut. M should be a map of bools initialized to false. |
35 |
31 |
36 */ |
32 */ |
37 |
33 |
38 #ifndef PREFLOW_HL3_H |
34 #ifndef PREFLOW_HL3_H |
39 #define PREFLOW_HL3_H |
35 #define PREFLOW_HL3_H |
40 |
|
41 #define A 1 |
|
42 |
36 |
43 #include <vector> |
37 #include <vector> |
44 #include <stack> |
38 #include <stack> |
45 #include <queue> |
39 #include <queue> |
46 |
40 |
|
41 #include <time_measure.h> //for test |
|
42 |
47 namespace hugo { |
43 namespace hugo { |
48 |
44 |
49 template <typename Graph, typename T, |
45 template <typename Graph, typename T, |
50 typename FlowMap=typename Graph::EdgeMap<T>, typename CapMap=typename Graph::EdgeMap<T>, |
46 typename FlowMap=typename Graph::EdgeMap<T>, |
51 typename IntMap=typename Graph::NodeMap<int>, typename TMap=typename Graph::NodeMap<T> > |
47 typename CapMap=typename Graph::EdgeMap<T> > |
52 class preflow_hl3 { |
48 class preflow_hl3 { |
53 |
49 |
54 typedef typename Graph::NodeIt NodeIt; |
50 typedef typename Graph::NodeIt NodeIt; |
55 typedef typename Graph::EdgeIt EdgeIt; |
51 typedef typename Graph::EdgeIt EdgeIt; |
56 typedef typename Graph::EachNodeIt EachNodeIt; |
52 typedef typename Graph::EachNodeIt EachNodeIt; |
64 CapMap& capacity; |
60 CapMap& capacity; |
65 T value; |
61 T value; |
66 |
62 |
67 public: |
63 public: |
68 |
64 |
|
65 double time; //for test |
|
66 |
69 preflow_hl3(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) : |
67 preflow_hl3(Graph& _G, NodeIt _s, NodeIt _t, CapMap& _capacity) : |
70 G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { } |
68 G(_G), s(_s), t(_t), flow(_G, 0), capacity(_capacity) { |
71 |
69 |
72 |
|
73 void run() { |
|
74 |
|
75 bool phase=0; |
70 bool phase=0; |
76 int n=G.nodeNum(); |
71 int n=G.nodeNum(); |
77 int b=n-2; |
72 int b=n-2; |
78 /* |
73 /* |
79 b is a bound on the highest level of the stack. |
74 b is a bound on the highest level of the stack. |
80 In the beginning it is at most n-2. |
75 In the beginning it is at most n-2. |
81 */ |
76 */ |
82 |
77 |
83 IntMap level(G,n); |
78 typename Graph::NodeMap<int> level(G,n); |
84 TMap excess(G); |
79 typename Graph::NodeMap<T> excess(G); |
85 |
80 |
86 std::vector<int> numb(n); |
81 std::vector<int> numb(n); |
87 /* |
82 /* |
88 The number of nodes on level i < n. It is |
83 The number of nodes on level i < n. It is |
89 initialized to n+1, because of the reverse_bfs-part. |
84 initialized to n+1, because of the reverse_bfs-part. |
185 } |
181 } |
186 |
182 |
187 if ( stack[b].empty() ) --b; |
183 if ( stack[b].empty() ) --b; |
188 else { |
184 else { |
189 |
185 |
190 NodeIt w=stack[b].top(); //w is a highest label active node. |
186 NodeIt w=stack[b].top(); |
191 stack[b].pop(); |
187 stack[b].pop(); |
192 int lev=level.get(w); |
188 int lev=level.get(w); |
193 int exc=excess.get(w); |
189 T exc=excess.get(w); |
194 int newlevel=n; //In newlevel we bound the next level of w. |
190 int newlevel=n; //bound on the next level of w. |
195 |
191 |
196 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) { |
192 for(OutEdgeIt e=G.template first<OutEdgeIt>(w); e.valid(); ++e) { |
197 |
193 |
198 if ( flow.get(e) == capacity.get(e) ) continue; |
194 if ( flow.get(e) == capacity.get(e) ) continue; |
199 NodeIt v=G.head(e); |
195 NodeIt v=G.head(e); |
204 |
200 |
205 if ( excess.get(v)==0 && v !=t && v!=s ) |
201 if ( excess.get(v)==0 && v !=t && v!=s ) |
206 stack[level.get(v)].push(v); |
202 stack[level.get(v)].push(v); |
207 /*v becomes active.*/ |
203 /*v becomes active.*/ |
208 |
204 |
209 int cap=capacity.get(e); |
205 T cap=capacity.get(e); |
210 int flo=flow.get(e); |
206 T flo=flow.get(e); |
211 int remcap=cap-flo; |
207 T remcap=cap-flo; |
212 |
208 |
213 if ( remcap >= exc ) { |
209 if ( remcap >= exc ) { |
214 /*A nonsaturating push.*/ |
210 /*A nonsaturating push.*/ |
215 |
211 |
216 flow.set(e, flo+exc); |
212 flow.set(e, flo+exc); |