Nehany folyamalgoritmus futasi ideje, azzal a kozponti kerdessel, hogy a sok dereferalas
hasznalata/kerulese
optimalizalassal/optimalizalas nelkul
kulonbozo gepeken Celeron 600/karp
milyen futasi idoket eredmenyez.
6 list 'level_list' on the nodes on level i implemented by hand
7 stack 'active' on the active nodes on level i implemented by hand
8 runs heuristic 'highest label' for H1*n relabels
9 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
11 Parameters H0 and H1 are initialized to 20 and 10.
15 Preflow(Graph, Node, Node, CapMap, FlowMap)
21 T flowValue() : returns the value of a maximum flow
23 void minMinCut(CutMap& M) : sets M to the characteristic vector of the
24 minimum min cut. M should be a map of bools initialized to false.
26 void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
27 maximum min cut. M should be a map of bools initialized to false.
29 void minCut(CutMap& M) : sets M to the characteristic vector of
30 a min cut. M should be a map of bools initialized to false.
34 #ifndef HUGO_PREFLOW_H
35 #define HUGO_PREFLOW_H
45 template <typename Graph, typename T,
46 typename FlowMap=typename Graph::EdgeMap<T>,
47 typename CapMap=typename Graph::EdgeMap<T> >
50 typedef typename Graph::Node Node;
51 typedef typename Graph::Edge Edge;
52 typedef typename Graph::NodeIt NodeIt;
53 typedef typename Graph::OutEdgeIt OutEdgeIt;
54 typedef typename Graph::InEdgeIt InEdgeIt;
60 const CapMap& capacity;
64 Preflow(Graph& _G, Node _s, Node _t, CapMap& _capacity, FlowMap& _flow ) :
65 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) {}
70 bool phase=0; //phase 0 is the 1st phase, phase 1 is the 2nd
72 int heur0=(int)(H0*n); //time while running 'bound decrease'
73 int heur1=(int)(H1*n); //time while running 'highest label'
74 int heur=heur1; //starting time interval (#of relabels)
77 what_heur is 0 in case 'bound decrease'
78 and 1 in case 'highest label'
82 Needed for 'bound decrease', 'true'
83 means no active nodes are above bound b.
86 int k=n-2; //bound on the highest level under n containing a node
87 int b=k; //bound on the highest level under n of an active node
89 typename Graph::NodeMap<int> level(G,n);
90 typename Graph::NodeMap<T> excess(G);
92 std::vector<Node> active(n,INVALID);
93 typename Graph::NodeMap<Node> next(G,INVALID);
94 //Stack of the active nodes in level i < n.
95 //We use it in both phases.
97 typename Graph::NodeMap<Node> left(G,INVALID);
98 typename Graph::NodeMap<Node> right(G,INVALID);
99 std::vector<Node> level_list(n,INVALID);
101 List of the nodes in level i<n.
104 /*Reverse_bfs from t, to find the starting level.*/
106 std::queue<Node> bfs_queue;
109 while (!bfs_queue.empty()) {
111 Node v=bfs_queue.front();
116 for(G.first(e,v); G.valid(e); G.next(e)) {
118 if ( level[w] == n && w != s ) {
120 Node first=level_list[l];
121 if ( G.valid(first) ) left.set(first,w);
132 /* Starting flow. It is everywhere 0 at the moment. */
134 for(G.first(e,s); G.valid(e); G.next(e))
137 if ( c == 0 ) continue;
139 if ( level[w] < n ) {
140 if ( excess[w] == 0 && w!=t ) {
141 next.set(w,active[level[w]]);
145 excess.set(w, excess[w]+c);
156 Push/relabel on the highest level active nodes.
163 if ( !what_heur && !end && k > 0 ) {
169 std::queue<Node> bfs_queue;
172 while (!bfs_queue.empty()) {
174 Node v=bfs_queue.front();
179 for(G.first(e,v); G.valid(e); G.next(e)) {
180 if ( capacity[e] == flow[e] ) continue;
182 if ( level[u] >= n ) {
185 if ( excess[u] > 0 ) {
186 next.set(u,active[l]);
193 for(G.first(f,v); G.valid(f); G.next(f)) {
194 if ( 0 == flow[f] ) continue;
196 if ( level[u] >= n ) {
199 if ( excess[u] > 0 ) {
200 next.set(u,active[l]);
212 if ( !G.valid(active[b]) ) --b;
220 int newlevel=n; //bound on the next level of w
223 for(G.first(e,w); G.valid(e); G.next(e)) {
225 if ( flow[e] == capacity[e] ) continue;
229 if( lev > level[v] ) {
230 /*Push is allowed now*/
232 if ( excess[v]==0 && v!=t && v!=s ) {
234 next.set(v,active[lev_v]);
242 if ( remcap >= exc ) {
243 /*A nonsaturating push.*/
245 flow.set(e, flo+exc);
246 excess.set(v, excess[v]+exc);
251 /*A saturating push.*/
254 excess.set(v, excess[v]+remcap);
257 } else if ( newlevel > level[v] ){
266 for(G.first(e,w); G.valid(e); G.next(e)) {
268 if( flow[e] == 0 ) continue;
272 if( lev > level[v] ) {
273 /*Push is allowed now*/
275 if ( excess[v]==0 && v!=t && v!=s ) {
277 next.set(v,active[lev_v]);
284 /*A nonsaturating push.*/
286 flow.set(e, flo-exc);
287 excess.set(v, excess[v]+exc);
291 /*A saturating push.*/
293 excess.set(v, excess[v]+flo);
297 } else if ( newlevel > level[v] ) {
302 } // if w still has excess after the out edge for cycle
312 //now 'lev' is the old level of w
315 level.set(w,++newlevel);
316 next.set(w,active[newlevel]);
321 Node right_n=right[w];
324 if ( G.valid(right_n) ) {
325 if ( G.valid(left_n) ) {
326 right.set(left_n, right_n);
327 left.set(right_n, left_n);
329 level_list[lev]=right_n;
330 left.set(right_n, INVALID);
333 if ( G.valid(left_n) ) {
334 right.set(left_n, INVALID);
336 level_list[lev]=INVALID;
342 if ( !G.valid(level_list[lev]) ) {
344 for (int i=lev; i!=k ; ) {
345 Node v=level_list[++i];
346 while ( G.valid(v) ) {
350 level_list[i]=INVALID;
351 if ( !what_heur ) active[i]=INVALID;
360 if ( newlevel == n ) level.set(w,n);
362 level.set(w,++newlevel);
363 next.set(w,active[newlevel]);
365 if ( what_heur ) b=newlevel;
366 if ( k < newlevel ) ++k;
367 Node first=level_list[newlevel];
368 if ( G.valid(first) ) left.set(first,w);
371 level_list[newlevel]=w;
377 if ( relabel >= heur ) {
395 } // if stack[b] is nonempty
410 Returns the maximum value of a flow.
424 void Flow(FlowMap& _flow ) {
426 for(G.first(v) ; G.valid(v); G.next(v))
427 _flow.set(v,flow[v]);
433 Returns the minimum min cut, by a bfs from s in the residual graph.
436 template<typename _CutMap>
437 void minMinCut(_CutMap& M) {
439 std::queue<Node> queue;
444 while (!queue.empty()) {
445 Node w=queue.front();
449 for(G.first(e,w) ; G.valid(e); G.next(e)) {
451 if (!M[v] && flow[e] < capacity[e] ) {
458 for(G.first(f,w) ; G.valid(f); G.next(f)) {
460 if (!M[v] && flow[f] > 0 ) {
471 Returns the maximum min cut, by a reverse bfs
472 from t in the residual graph.
475 template<typename _CutMap>
476 void maxMinCut(_CutMap& M) {
478 std::queue<Node> queue;
483 while (!queue.empty()) {
484 Node w=queue.front();
489 for(G.first(e,w) ; G.valid(e); G.next(e)) {
491 if (!M[v] && flow[e] < capacity[e] ) {
498 for(G.first(f,w) ; G.valid(f); G.next(f)) {
500 if (!M[v] && flow[f] > 0 ) {
508 for(G.first(v) ; G.valid(v); G.next(v)) {
516 template<typename CutMap>
517 void minCut(CutMap& M) {