src/work/jacint/max_flow.h
changeset 1150 c20bcf71efe3
parent 921 818510fa3d99
equal deleted inserted replaced
19:aeeb36781bab 20:e4e0789c7f13
   324         Node w=queue.front();
   324         Node w=queue.front();
   325 	queue.pop();
   325 	queue.pop();
   326 
   326 
   327 	OutEdgeIt e;
   327 	OutEdgeIt e;
   328 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   328 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   329 	  Node v=g->head(e);
   329 	  Node v=g->target(e);
   330 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   330 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   331 	    queue.push(v);
   331 	    queue.push(v);
   332 	    M.set(v, true);
   332 	    M.set(v, true);
   333 	  }
   333 	  }
   334 	}
   334 	}
   335 
   335 
   336 	InEdgeIt f;
   336 	InEdgeIt f;
   337 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   337 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   338 	  Node v=g->tail(f);
   338 	  Node v=g->source(f);
   339 	  if (!M[v] && (*flow)[f] > 0 ) {
   339 	  if (!M[v] && (*flow)[f] > 0 ) {
   340 	    queue.push(v);
   340 	    queue.push(v);
   341 	    M.set(v, true);
   341 	    M.set(v, true);
   342 	  }
   342 	  }
   343 	}
   343 	}
   368         Node w=queue.front();
   368         Node w=queue.front();
   369 	queue.pop();
   369 	queue.pop();
   370 
   370 
   371 	InEdgeIt e;
   371 	InEdgeIt e;
   372 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   372 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   373 	  Node v=g->tail(e);
   373 	  Node v=g->source(e);
   374 	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   374 	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   375 	    queue.push(v);
   375 	    queue.push(v);
   376 	    M.set(v, false);
   376 	    M.set(v, false);
   377 	  }
   377 	  }
   378 	}
   378 	}
   379 
   379 
   380 	OutEdgeIt f;
   380 	OutEdgeIt f;
   381 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   381 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   382 	  Node v=g->head(f);
   382 	  Node v=g->target(f);
   383 	  if (M[v] && (*flow)[f] > 0 ) {
   383 	  if (M[v] && (*flow)[f] > 0 ) {
   384 	    queue.push(v);
   384 	    queue.push(v);
   385 	    M.set(v, false);
   385 	    M.set(v, false);
   386 	  }
   386 	  }
   387 	}
   387 	}
   431 
   431 
   432       OutEdgeIt e;
   432       OutEdgeIt e;
   433       for(g->first(e,w); g->valid(e); g->next(e)) {
   433       for(g->first(e,w); g->valid(e); g->next(e)) {
   434 
   434 
   435 	if ( (*flow)[e] >= (*capacity)[e] ) continue;
   435 	if ( (*flow)[e] >= (*capacity)[e] ) continue;
   436 	Node v=g->head(e);
   436 	Node v=g->target(e);
   437 
   437 
   438 	if( lev > level[v] ) { //Push is allowed now
   438 	if( lev > level[v] ) { //Push is allowed now
   439 
   439 
   440 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   440 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   441 	    int lev_v=level[v];
   441 	    int lev_v=level[v];
   464       if ( exc > 0 ) {
   464       if ( exc > 0 ) {
   465 	InEdgeIt e;
   465 	InEdgeIt e;
   466 	for(g->first(e,w); g->valid(e); g->next(e)) {
   466 	for(g->first(e,w); g->valid(e); g->next(e)) {
   467 
   467 
   468 	  if( (*flow)[e] <= 0 ) continue;
   468 	  if( (*flow)[e] <= 0 ) continue;
   469 	  Node v=g->tail(e);
   469 	  Node v=g->source(e);
   470 
   470 
   471 	  if( lev > level[v] ) { //Push is allowed now
   471 	  if( lev > level[v] ) { //Push is allowed now
   472 
   472 
   473 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   473 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   474 	      int lev_v=level[v];
   474 	      int lev_v=level[v];
   519 	    bfs_queue.pop();
   519 	    bfs_queue.pop();
   520 	    int l=level[v]+1;
   520 	    int l=level[v]+1;
   521 
   521 
   522 	    InEdgeIt e;
   522 	    InEdgeIt e;
   523 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   523 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   524 	      Node w=g->tail(e);
   524 	      Node w=g->source(e);
   525 	      if ( level[w] == n && w != s ) {
   525 	      if ( level[w] == n && w != s ) {
   526 		bfs_queue.push(w);
   526 		bfs_queue.push(w);
   527 		Node first=level_list[l];
   527 		Node first=level_list[l];
   528 		if ( g->valid(first) ) left.set(first,w);
   528 		if ( g->valid(first) ) left.set(first,w);
   529 		right.set(w,first);
   529 		right.set(w,first);
   537 	  OutEdgeIt e;
   537 	  OutEdgeIt e;
   538 	  for(g->first(e,s); g->valid(e); g->next(e))
   538 	  for(g->first(e,s); g->valid(e); g->next(e))
   539 	    {
   539 	    {
   540 	      Num c=(*capacity)[e];
   540 	      Num c=(*capacity)[e];
   541 	      if ( c <= 0 ) continue;
   541 	      if ( c <= 0 ) continue;
   542 	      Node w=g->head(e);
   542 	      Node w=g->target(e);
   543 	      if ( level[w] < n ) {
   543 	      if ( level[w] < n ) {
   544 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   544 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   545 		flow->set(e, c);
   545 		flow->set(e, c);
   546 		excess.set(w, excess[w]+c);
   546 		excess.set(w, excess[w]+c);
   547 	      }
   547 	      }
   564 	    int l=level[v]+1;
   564 	    int l=level[v]+1;
   565 
   565 
   566 	    InEdgeIt e;
   566 	    InEdgeIt e;
   567 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   567 	    for(g->first(e,v); g->valid(e); g->next(e)) {
   568 	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
   568 	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
   569 	      Node w=g->tail(e);
   569 	      Node w=g->source(e);
   570 	      if ( level[w] == n && w != s ) {
   570 	      if ( level[w] == n && w != s ) {
   571 		bfs_queue.push(w);
   571 		bfs_queue.push(w);
   572 		Node first=level_list[l];
   572 		Node first=level_list[l];
   573 		if ( g->valid(first) ) left.set(first,w);
   573 		if ( g->valid(first) ) left.set(first,w);
   574 		right.set(w,first);
   574 		right.set(w,first);
   578 	    }
   578 	    }
   579 
   579 
   580 	    OutEdgeIt f;
   580 	    OutEdgeIt f;
   581 	    for(g->first(f,v); g->valid(f); g->next(f)) {
   581 	    for(g->first(f,v); g->valid(f); g->next(f)) {
   582 	      if ( 0 >= (*flow)[f] ) continue;
   582 	      if ( 0 >= (*flow)[f] ) continue;
   583 	      Node w=g->head(f);
   583 	      Node w=g->target(f);
   584 	      if ( level[w] == n && w != s ) {
   584 	      if ( level[w] == n && w != s ) {
   585 		bfs_queue.push(w);
   585 		bfs_queue.push(w);
   586 		Node first=level_list[l];
   586 		Node first=level_list[l];
   587 		if ( g->valid(first) ) left.set(first,w);
   587 		if ( g->valid(first) ) left.set(first,w);
   588 		right.set(w,first);
   588 		right.set(w,first);
   597 	  OutEdgeIt e;
   597 	  OutEdgeIt e;
   598 	  for(g->first(e,s); g->valid(e); g->next(e))
   598 	  for(g->first(e,s); g->valid(e); g->next(e))
   599 	    {
   599 	    {
   600 	      Num rem=(*capacity)[e]-(*flow)[e];
   600 	      Num rem=(*capacity)[e]-(*flow)[e];
   601 	      if ( rem <= 0 ) continue;
   601 	      if ( rem <= 0 ) continue;
   602 	      Node w=g->head(e);
   602 	      Node w=g->target(e);
   603 	      if ( level[w] < n ) {
   603 	      if ( level[w] < n ) {
   604 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   604 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   605 		flow->set(e, (*capacity)[e]);
   605 		flow->set(e, (*capacity)[e]);
   606 		excess.set(w, excess[w]+rem);
   606 		excess.set(w, excess[w]+rem);
   607 	      }
   607 	      }
   609 
   609 
   610 	  InEdgeIt f;
   610 	  InEdgeIt f;
   611 	  for(g->first(f,s); g->valid(f); g->next(f))
   611 	  for(g->first(f,s); g->valid(f); g->next(f))
   612 	    {
   612 	    {
   613 	      if ( (*flow)[f] <= 0 ) continue;
   613 	      if ( (*flow)[f] <= 0 ) continue;
   614 	      Node w=g->tail(f);
   614 	      Node w=g->source(f);
   615 	      if ( level[w] < n ) {
   615 	      if ( level[w] < n ) {
   616 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   616 		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   617 		excess.set(w, excess[w]+(*flow)[f]);
   617 		excess.set(w, excess[w]+(*flow)[f]);
   618 		flow->set(f, 0);
   618 		flow->set(f, 0);
   619 	      }
   619 	      }
   708 	return dist[n]; 
   708 	return dist[n]; 
   709       }
   709       }
   710       //       int get(const typename MapGraphWrapper::Node& n) const {
   710       //       int get(const typename MapGraphWrapper::Node& n) const {
   711       // 	return dist[n]; }
   711       // 	return dist[n]; }
   712       //       bool get(const typename MapGraphWrapper::Edge& e) const {
   712       //       bool get(const typename MapGraphWrapper::Edge& e) const {
   713       // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
   713       // 	return (dist.get(g->source(e))<dist.get(g->target(e))); }
   714       bool operator[](const typename MapGraphWrapper::Edge& e) const {
   714       bool operator[](const typename MapGraphWrapper::Edge& e) const {
   715 	return (dist[g->tail(e)]<dist[g->head(e)]);
   715 	return (dist[g->source(e)]<dist[g->target(e)]);
   716       }
   716       }
   717     };
   717     };
   718 
   718 
   719   };
   719   };
   720 
   720 
   858       int l=level[v]+1;
   858       int l=level[v]+1;
   859 
   859 
   860       InEdgeIt e;
   860       InEdgeIt e;
   861       for(g->first(e,v); g->valid(e); g->next(e)) {
   861       for(g->first(e,v); g->valid(e); g->next(e)) {
   862 	if ( (*capacity)[e] <= (*flow)[e] ) continue;
   862 	if ( (*capacity)[e] <= (*flow)[e] ) continue;
   863 	Node u=g->tail(e);
   863 	Node u=g->source(e);
   864 	if ( level[u] >= n ) {
   864 	if ( level[u] >= n ) {
   865 	  bfs_queue.push(u);
   865 	  bfs_queue.push(u);
   866 	  level.set(u, l);
   866 	  level.set(u, l);
   867 	  if ( excess[u] > 0 ) active[l].push(u);
   867 	  if ( excess[u] > 0 ) active[l].push(u);
   868 	}
   868 	}
   869       }
   869       }
   870 
   870 
   871       OutEdgeIt f;
   871       OutEdgeIt f;
   872       for(g->first(f,v); g->valid(f); g->next(f)) {
   872       for(g->first(f,v); g->valid(f); g->next(f)) {
   873 	if ( 0 >= (*flow)[f] ) continue;
   873 	if ( 0 >= (*flow)[f] ) continue;
   874 	Node u=g->head(f);
   874 	Node u=g->target(f);
   875 	if ( level[u] >= n ) {
   875 	if ( level[u] >= n ) {
   876 	  bfs_queue.push(u);
   876 	  bfs_queue.push(u);
   877 	  level.set(u, l);
   877 	  level.set(u, l);
   878 	  if ( excess[u] > 0 ) active[l].push(u);
   878 	  if ( excess[u] > 0 ) active[l].push(u);
   879 	}
   879 	}
   923 
   923 
   924     //searching for augmenting path
   924     //searching for augmenting path
   925     while ( !bfs.finished() ) {
   925     while ( !bfs.finished() ) {
   926       ResGWOutEdgeIt e=bfs;
   926       ResGWOutEdgeIt e=bfs;
   927       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   927       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   928 	Node v=res_graph.tail(e);
   928 	Node v=res_graph.source(e);
   929 	Node w=res_graph.head(e);
   929 	Node w=res_graph.target(e);
   930 	pred.set(w, e);
   930 	pred.set(w, e);
   931 	if (res_graph.valid(pred[v])) {
   931 	if (res_graph.valid(pred[v])) {
   932 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   932 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   933 	} else {
   933 	} else {
   934 	  free.set(w, res_graph.resCap(e));
   934 	  free.set(w, res_graph.resCap(e));
   935 	}
   935 	}
   936 	if (res_graph.head(e)==t) { _augment=true; break; }
   936 	if (res_graph.target(e)==t) { _augment=true; break; }
   937       }
   937       }
   938 
   938 
   939       ++bfs;
   939       ++bfs;
   940     } //end of searching augmenting path
   940     } //end of searching augmenting path
   941 
   941 
   943       Node n=t;
   943       Node n=t;
   944       Num augment_value=free[t];
   944       Num augment_value=free[t];
   945       while (res_graph.valid(pred[n])) {
   945       while (res_graph.valid(pred[n])) {
   946 	ResGWEdge e=pred[n];
   946 	ResGWEdge e=pred[n];
   947 	res_graph.augment(e, augment_value);
   947 	res_graph.augment(e, augment_value);
   948 	n=res_graph.tail(e);
   948 	n=res_graph.source(e);
   949       }
   949       }
   950     }
   950     }
   951 
   951 
   952     status=AFTER_AUGMENTING;
   952     status=AFTER_AUGMENTING;
   953     return _augment;
   953     return _augment;
   981 
   981 
   982     //searching for augmenting path
   982     //searching for augmenting path
   983     while ( !bfs.finished() ) {
   983     while ( !bfs.finished() ) {
   984       ResGWOutEdgeIt e=bfs;
   984       ResGWOutEdgeIt e=bfs;
   985       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   985       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   986 	Node v=res_graph.tail(e);
   986 	Node v=res_graph.source(e);
   987 	Node w=res_graph.head(e);
   987 	Node w=res_graph.target(e);
   988 	pred.set(w, e);
   988 	pred.set(w, e);
   989 	if (res_graph.valid(pred[v])) {
   989 	if (res_graph.valid(pred[v])) {
   990 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   990 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   991 	} else {
   991 	} else {
   992 	  free.set(w, res_graph.resCap(e));
   992 	  free.set(w, res_graph.resCap(e));
   993 	}
   993 	}
   994 	if (res_graph.head(e)==t) { _augment=true; break; }
   994 	if (res_graph.target(e)==t) { _augment=true; break; }
   995       }
   995       }
   996 
   996 
   997       ++bfs;
   997       ++bfs;
   998     } //end of searching augmenting path
   998     } //end of searching augmenting path
   999 
   999 
  1001       Node n=t;
  1001       Node n=t;
  1002       Num augment_value=free[t];
  1002       Num augment_value=free[t];
  1003       while (res_graph.valid(pred[n])) {
  1003       while (res_graph.valid(pred[n])) {
  1004 	ResGWEdge e=pred[n];
  1004 	ResGWEdge e=pred[n];
  1005 	res_graph.augment(e, augment_value);
  1005 	res_graph.augment(e, augment_value);
  1006 	n=res_graph.tail(e);
  1006 	n=res_graph.source(e);
  1007       }
  1007       }
  1008     }
  1008     }
  1009 
  1009 
  1010     status=AFTER_FAST_AUGMENTING;
  1010     status=AFTER_FAST_AUGMENTING;
  1011     return _augment;
  1011     return _augment;
  1048 
  1048 
  1049     while ( !bfs.finished() ) {
  1049     while ( !bfs.finished() ) {
  1050       ResGWOutEdgeIt e=bfs;
  1050       ResGWOutEdgeIt e=bfs;
  1051       if (res_graph.valid(e)) {
  1051       if (res_graph.valid(e)) {
  1052 	if (bfs.isBNodeNewlyReached()) {
  1052 	if (bfs.isBNodeNewlyReached()) {
  1053 	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  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.tail(e)],
  1054 	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)],
  1055 					res_graph_to_F[res_graph.head(e)]);
  1055 					res_graph_to_F[res_graph.target(e)]);
  1056 	  original_edge.update();
  1056 	  original_edge.update();
  1057 	  original_edge.set(f, e);
  1057 	  original_edge.set(f, e);
  1058 	  residual_capacity.update();
  1058 	  residual_capacity.update();
  1059 	  residual_capacity.set(f, res_graph.resCap(e));
  1059 	  residual_capacity.set(f, res_graph.resCap(e));
  1060 	} else {
  1060 	} else {
  1061 	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
  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.tail(e)],
  1062 	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)],
  1063 					  res_graph_to_F[res_graph.head(e)]);
  1063 					  res_graph_to_F[res_graph.target(e)]);
  1064 	    original_edge.update();
  1064 	    original_edge.update();
  1065 	    original_edge.set(f, e);
  1065 	    original_edge.set(f, e);
  1066 	    residual_capacity.update();
  1066 	    residual_capacity.update();
  1067 	    residual_capacity.set(f, res_graph.resCap(e));
  1067 	    residual_capacity.set(f, res_graph.resCap(e));
  1068 	  }
  1068 	  }
  1112 	typename MG::Node n=tF;
  1112 	typename MG::Node n=tF;
  1113 	Num augment_value=free[tF];
  1113 	Num augment_value=free[tF];
  1114 	while (F.valid(pred[n])) {
  1114 	while (F.valid(pred[n])) {
  1115 	  typename MG::Edge e=pred[n];
  1115 	  typename MG::Edge e=pred[n];
  1116 	  res_graph.augment(original_edge[e], augment_value);
  1116 	  res_graph.augment(original_edge[e], augment_value);
  1117 	  n=F.tail(e);
  1117 	  n=F.source(e);
  1118 	  if (residual_capacity[e]==augment_value)
  1118 	  if (residual_capacity[e]==augment_value)
  1119 	    F.erase(e);
  1119 	    F.erase(e);
  1120 	  else
  1120 	  else
  1121 	    residual_capacity.set(e, residual_capacity[e]-augment_value);
  1121 	    residual_capacity.set(e, residual_capacity[e]-augment_value);
  1122 	}
  1122 	}
  1145     bfs.pushAndSetReached(s);
  1145     bfs.pushAndSetReached(s);
  1146     DistanceMap<ResGW> dist(res_graph);
  1146     DistanceMap<ResGW> dist(res_graph);
  1147     while ( !bfs.finished() ) {
  1147     while ( !bfs.finished() ) {
  1148       ResGWOutEdgeIt e=bfs;
  1148       ResGWOutEdgeIt e=bfs;
  1149       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1149       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1150 	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1150 	dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
  1151       }
  1151       }
  1152       ++bfs;
  1152       ++bfs;
  1153     } //computing distances from s in the residual graph
  1153     } //computing distances from s in the residual graph
  1154 
  1154 
  1155       //Subgraph containing the edges on some shortest paths
  1155       //Subgraph containing the edges on some shortest paths
  1245 	// 	  Num j2=a2[b2];
  1245 	// 	  Num j2=a2[b2];
  1246 	Num augment_value=free1[n];
  1246 	Num augment_value=free1[n];
  1247 	while (erasing_res_graph.valid(pred[n])) {
  1247 	while (erasing_res_graph.valid(pred[n])) {
  1248 	  typename ErasingResGW::OutEdgeIt e=pred[n];
  1248 	  typename ErasingResGW::OutEdgeIt e=pred[n];
  1249 	  res_graph.augment(e, augment_value);
  1249 	  res_graph.augment(e, augment_value);
  1250 	  n=erasing_res_graph.tail(e);
  1250 	  n=erasing_res_graph.source(e);
  1251 	  if (res_graph.resCap(e)==0)
  1251 	  if (res_graph.resCap(e)==0)
  1252 	    erasing_res_graph.erase(e);
  1252 	    erasing_res_graph.erase(e);
  1253 	}
  1253 	}
  1254       }
  1254       }
  1255 
  1255