src/work/marci/augmenting_flow.h
changeset 842 a4bb28813570
parent 775 e46a1f0623a0
child 854 baf0b6e40211
equal deleted inserted replaced
1:1e771fa295d2 2:643a0607aa0c
     9 
     9 
    10 #include <hugo/graph_wrapper.h>
    10 #include <hugo/graph_wrapper.h>
    11 #include <bfs_dfs.h>
    11 #include <bfs_dfs.h>
    12 #include <hugo/invalid.h>
    12 #include <hugo/invalid.h>
    13 #include <hugo/maps.h>
    13 #include <hugo/maps.h>
    14 #include <for_each_macros.h>
    14 //#include <for_each_macros.h>
    15 
    15 
    16 /// \file
    16 /// \file
    17 /// \brief Maximum flow algorithms.
    17 /// \brief Maximum flow algorithms.
    18 /// \ingroup galgs
    18 /// \ingroup galgs
    19 
    19 
  1080       while (!bfs.finished()) ++bfs;
  1080       while (!bfs.finished()) ++bfs;
  1081     }
  1081     }
  1082 
  1082 
  1083     Num flowValue() const {
  1083     Num flowValue() const {
  1084       Num a=0;
  1084       Num a=0;
  1085       FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
  1085       for (InEdgeIt e(*g, t); e!=INVALID; ++e) a+=(*flow)[e];
  1086       FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
  1086       for (OutEdgeIt e(*g, t); e!=INVALID; ++e) a-=(*flow)[e];
  1087       return a;
  1087       return a;
  1088       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
  1088       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
  1089     }
  1089     }
  1090 
  1090 
  1091     template<typename MapGraphWrapper>
  1091     template<typename MapGraphWrapper>
  1119   {
  1119   {
  1120     ResGW res_graph(*g, *capacity, *flow);
  1120     ResGW res_graph(*g, *capacity, *flow);
  1121     bool _augment=false;
  1121     bool _augment=false;
  1122 
  1122 
  1123     //ReachedMap level(res_graph);
  1123     //ReachedMap level(res_graph);
  1124     FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1124     for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
  1125     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1125     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1126     bfs.pushAndSetReached(s);
  1126     bfs.pushAndSetReached(s);
  1127 
  1127 
  1128     typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
  1128     typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
  1129     pred.set(s, INVALID);
  1129     pred.set(s, INVALID);
  1130 
  1130 
  1131     typename ResGW::template NodeMap<Num> free(res_graph);
  1131     typename ResGW::template NodeMap<Num> free(res_graph);
  1132 
  1132 
  1133     //searching for augmenting path
  1133     //searching for augmenting path
  1134     while ( !bfs.finished() ) {
  1134     while ( !bfs.finished() ) {
  1135       ResGWOutEdgeIt e=bfs;
  1135       ResGWEdge e=bfs;
  1136       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1136       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1137 	Node v=res_graph.tail(e);
  1137 	Node v=res_graph.tail(e);
  1138 	Node w=res_graph.head(e);
  1138 	Node w=res_graph.head(e);
  1139 	pred.set(w, e);
  1139 	pred.set(w, e);
  1140 	if (pred[v]!=INVALID) {
  1140 	if (pred[v]!=INVALID) {
  1167   {
  1167   {
  1168     ResGW res_graph(*g, *capacity, *flow);
  1168     ResGW res_graph(*g, *capacity, *flow);
  1169     bool _augment=false;
  1169     bool _augment=false;
  1170 
  1170 
  1171     if (status!=AFTER_FAST_AUGMENTING) {
  1171     if (status!=AFTER_FAST_AUGMENTING) {
  1172       FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); 
  1172       for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0); 
  1173       number_of_augmentations=1;
  1173       number_of_augmentations=1;
  1174     } else {
  1174     } else {
  1175       ++number_of_augmentations;
  1175       ++number_of_augmentations;
  1176     }
  1176     }
  1177     TrickyReachedMap<ReachedMap> 
  1177     TrickyReachedMap<ReachedMap> 
  1187 
  1187 
  1188     typename ResGW::template NodeMap<Num> free(res_graph);
  1188     typename ResGW::template NodeMap<Num> free(res_graph);
  1189 
  1189 
  1190     //searching for augmenting path
  1190     //searching for augmenting path
  1191     while ( !bfs.finished() ) {
  1191     while ( !bfs.finished() ) {
  1192       ResGWOutEdgeIt e=bfs;
  1192       ResGWEdge e=bfs;
  1193       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1193       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1194 	Node v=res_graph.tail(e);
  1194 	Node v=res_graph.tail(e);
  1195 	Node w=res_graph.head(e);
  1195 	Node w=res_graph.head(e);
  1196 	pred.set(w, e);
  1196 	pred.set(w, e);
  1197 	if (pred[v]!=INVALID) {
  1197 	if (pred[v]!=INVALID) {
  1229 
  1229 
  1230     ResGW res_graph(*g, *capacity, *flow);
  1230     ResGW res_graph(*g, *capacity, *flow);
  1231 
  1231 
  1232     //bfs for distances on the residual graph
  1232     //bfs for distances on the residual graph
  1233     //ReachedMap level(res_graph);
  1233     //ReachedMap level(res_graph);
  1234     FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1234     for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
  1235     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1235     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1236     bfs.pushAndSetReached(s);
  1236     bfs.pushAndSetReached(s);
  1237     typename ResGW::template NodeMap<int>
  1237     typename ResGW::template NodeMap<int>
  1238       dist(res_graph); //filled up with 0's
  1238       dist(res_graph); //filled up with 0's
  1239 
  1239 
  1253     typename MG::Node tF=res_graph_to_F[t];
  1253     typename MG::Node tF=res_graph_to_F[t];
  1254     typename MG::template EdgeMap<ResGWEdge> original_edge(F);
  1254     typename MG::template EdgeMap<ResGWEdge> original_edge(F);
  1255     typename MG::template EdgeMap<Num> residual_capacity(F);
  1255     typename MG::template EdgeMap<Num> residual_capacity(F);
  1256 
  1256 
  1257     while ( !bfs.finished() ) {
  1257     while ( !bfs.finished() ) {
  1258       ResGWOutEdgeIt e=bfs;
  1258       ResGWEdge e=bfs;
  1259       if (e!=INVALID) {
  1259       if (e!=INVALID) {
  1260 	if (bfs.isBNodeNewlyReached()) {
  1260 	if (bfs.isBNodeNewlyReached()) {
  1261 	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1261 	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1262 	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
  1262 	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
  1263 					res_graph_to_F[res_graph.head(e)]);
  1263 					res_graph_to_F[res_graph.head(e)]);
  1294       dfs.pushAndSetReached(sF);
  1294       dfs.pushAndSetReached(sF);
  1295       while (!dfs.finished()) {
  1295       while (!dfs.finished()) {
  1296 	++dfs;
  1296 	++dfs;
  1297 	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
  1297 	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
  1298 	  if (dfs.isBNodeNewlyReached()) {
  1298 	  if (dfs.isBNodeNewlyReached()) {
  1299 	    typename MG::Node v=F.aNode(dfs);
  1299 	    typename MG::Node v=F.tail(dfs);
  1300 	    typename MG::Node w=F.bNode(dfs);
  1300 	    typename MG::Node w=F.head(dfs);
  1301 	    pred.set(w, dfs);
  1301 	    pred.set(w, dfs);
  1302 	    if (pred[v]!=INVALID) {
  1302 	    if (pred[v]!=INVALID) {
  1303 	      free.set(w, std::min(free[v], residual_capacity[dfs]));
  1303 	      free.set(w, std::min(free[v], residual_capacity[dfs]));
  1304 	    } else {
  1304 	    } else {
  1305 	      free.set(w, residual_capacity[dfs]);
  1305 	      free.set(w, residual_capacity[dfs]);
  1343     bool _augment=false;
  1343     bool _augment=false;
  1344 
  1344 
  1345     ResGW res_graph(*g, *capacity, *flow);
  1345     ResGW res_graph(*g, *capacity, *flow);
  1346 
  1346 
  1347     //ReachedMap level(res_graph);
  1347     //ReachedMap level(res_graph);
  1348     FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1348     for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
  1349     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1349     BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1350 
  1350 
  1351     bfs.pushAndSetReached(s);
  1351     bfs.pushAndSetReached(s);
  1352     DistanceMap<ResGW> dist(res_graph);
  1352     DistanceMap<ResGW> dist(res_graph);
  1353     while ( !bfs.finished() ) {
  1353     while ( !bfs.finished() ) {
  1354       ResGWOutEdgeIt e=bfs;
  1354       ResGWEdge e=bfs;
  1355       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1355       if (e!=INVALID && bfs.isBNodeNewlyReached()) {
  1356 	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1356 	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1357       }
  1357       }
  1358       ++bfs;
  1358       ++bfs;
  1359     } //computing distances from s in the residual graph
  1359     } //computing distances from s in the residual graph
  1360 
  1360 
  1361       //Subgraph containing the edges on some shortest paths
  1361     //Subgraph containing the edges on some shortest paths
  1362     ConstMap<typename ResGW::Node, bool> true_map(true);
  1362     ConstMap<typename ResGW::Node, bool> true_map(true);
  1363     typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
  1363     typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
  1364       DistanceMap<ResGW> > FilterResGW;
  1364       DistanceMap<ResGW> > FilterResGW;
  1365     FilterResGW filter_res_graph(res_graph, true_map, dist);
  1365     FilterResGW filter_res_graph(res_graph, true_map, dist);
  1366 
  1366 
  1367     //Subgraph, which is able to delete edges which are already
  1367     //Subgraph, which is able to delete edges which are already
  1368     //met by the dfs
  1368     //met by the dfs
  1369     typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
  1369     typename FilterResGW::template NodeMap<typename FilterResGW::Edge>
  1370       first_out_edges(filter_res_graph);
  1370       first_out_edges(filter_res_graph);
  1371     typename FilterResGW::NodeIt v;
  1371     typename FilterResGW::NodeIt v;
  1372     for(filter_res_graph.first(v); v!=INVALID; ++v)
  1372     for(filter_res_graph.first(v); v!=INVALID; ++v)
  1373       {
  1373       {
  1374  	typename FilterResGW::OutEdgeIt e;
  1374   	typename FilterResGW::OutEdgeIt e;
  1375  	filter_res_graph.first(e, v);
  1375   	filter_res_graph.first(e, v);
  1376  	first_out_edges.set(v, e);
  1376   	first_out_edges.set(v, e);
  1377       }
  1377       }
  1378     typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
  1378     typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
  1379       template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
  1379       template NodeMap<typename FilterResGW::Edge> > ErasingResGW;
  1380     ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
  1380     ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
  1381 
  1381 
  1382     bool __augment=true;
  1382     bool __augment=true;
  1383 
  1383 
  1384     while (__augment) {
  1384     while (__augment) {
  1387       //computing blocking flow with dfs
  1387       //computing blocking flow with dfs
  1388       DfsIterator< ErasingResGW,
  1388       DfsIterator< ErasingResGW,
  1389 	typename ErasingResGW::template NodeMap<bool> >
  1389 	typename ErasingResGW::template NodeMap<bool> >
  1390 	dfs(erasing_res_graph);
  1390 	dfs(erasing_res_graph);
  1391       typename ErasingResGW::
  1391       typename ErasingResGW::
  1392 	template NodeMap<typename ErasingResGW::OutEdgeIt>
  1392 	template NodeMap<typename ErasingResGW::Edge> pred(erasing_res_graph);
  1393 	pred(erasing_res_graph);
       
  1394       pred.set(s, INVALID);
  1393       pred.set(s, INVALID);
  1395       //invalid iterators for sources
  1394       //invalid iterators for sources
  1396 
  1395 
  1397       typename ErasingResGW::template NodeMap<Num>
  1396       typename ErasingResGW::template NodeMap<Num>
  1398 	free1(erasing_res_graph);
  1397 	free1(erasing_res_graph);
  1399 
  1398 
  1400       dfs.pushAndSetReached
  1399       dfs.pushAndSetReached
  1401 	///\bug hugo 0.2
  1400 	/// \bug hugo 0.2
  1402 	(typename ErasingResGW::Node
  1401 	(typename ErasingResGW::Node
  1403 	 (typename FilterResGW::Node
  1402 	 (typename FilterResGW::Node
  1404 	  (typename ResGW::Node(s)
  1403 	  (typename ResGW::Node(s)
  1405 	   )
  1404 	   )
  1406 	  )
  1405 	  )
  1407 	 );
  1406 	 );
       
  1407 	
  1408       while (!dfs.finished()) {
  1408       while (!dfs.finished()) {
  1409 	++dfs;
  1409 	++dfs;
  1410 	if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
  1410 	if (typename ErasingResGW::Edge(dfs)!=INVALID)
  1411  	  {
  1411  	  {
  1412   	    if (dfs.isBNodeNewlyReached()) {
  1412   	    if (dfs.isBNodeNewlyReached()) {
  1413 
  1413 
  1414  	      typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
  1414  	      typename ErasingResGW::Node v=erasing_res_graph.tail(dfs);
  1415  	      typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
  1415  	      typename ErasingResGW::Node w=erasing_res_graph.head(dfs);
  1416 
  1416 
  1417  	      pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
  1417  	      pred.set(w, typename ErasingResGW::Edge(dfs));
  1418  	      if (pred[v]!=INVALID) {
  1418  	      if (pred[v]!=INVALID) {
  1419  		free1.set
  1419  		free1.set
  1420 		  (w, std::min(free1[v], res_graph.resCap
  1420 		  (w, std::min(free1[v], res_graph.resCap
  1421 			       (typename ErasingResGW::OutEdgeIt(dfs))));
  1421 			       (typename ErasingResGW::Edge(dfs))));
  1422  	      } else {
  1422  	      } else {
  1423  		free1.set
  1423  		free1.set
  1424 		  (w, res_graph.resCap
  1424 		  (w, res_graph.resCap
  1425 		   (typename ErasingResGW::OutEdgeIt(dfs)));
  1425 		   (typename ErasingResGW::Edge(dfs)));
  1426  	      }
  1426  	      }
  1427 
  1427 
  1428  	      if (w==t) {
  1428  	      if (w==t) {
  1429  		__augment=true;
  1429  		__augment=true;
  1430  		_augment=true;
  1430  		_augment=true;
  1447 	// 	  Num j1=a1[b1];
  1447 	// 	  Num j1=a1[b1];
  1448 	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
  1448 	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
  1449 	// 	  typename ErasingResGW::Node b2;
  1449 	// 	  typename ErasingResGW::Node b2;
  1450 	// 	  Num j2=a2[b2];
  1450 	// 	  Num j2=a2[b2];
  1451 	Num augment_value=free1[n];
  1451 	Num augment_value=free1[n];
  1452 	while (erasing_res_graph.valid(pred[n])) {
  1452 	while (pred[n]!=INVALID) {
  1453 	  typename ErasingResGW::OutEdgeIt e=pred[n];
  1453 	  typename ErasingResGW::Edge e=pred[n];
  1454 	  res_graph.augment(e, augment_value);
  1454 	  res_graph.augment(e, augment_value);
  1455 	  n=erasing_res_graph.tail(e);
  1455 	  n=erasing_res_graph.tail(e);
  1456 	  if (res_graph.resCap(e)==0)
  1456 	  if (res_graph.resCap(e)==0)
  1457 	    erasing_res_graph.erase(e);
  1457 	    erasing_res_graph.erase(e);
  1458 	}
  1458 	}