11#include <gtest/gtest.h>
91 std::vector<std::vector<Node*>>
nodes(
rows, std::vector<Node*>(
cols));
95 for (
int i = 0; i <
rows; ++i)
96 for (
int j = 0; j <
cols; ++j)
100 for (
int i = 0; i <
rows; ++i)
101 for (
int j = 0; j <
cols - 1; ++j)
105 for (
int i = 0; i <
rows - 1; ++i)
106 for (
int j = 0; j <
cols; ++j)
112 [[maybe_unused]]
void reset_flow(
TestNet& net)
115 it.get_curr()->flow = 0;
127 build_linear_network(net);
131 EXPECT_DOUBLE_EQ(flow, 5.0);
138 build_diamond_network(net);
143 EXPECT_DOUBLE_EQ(flow, 20.0);
150 build_complex_network(net);
155 EXPECT_DOUBLE_EQ(flow, 23.0);
169 EXPECT_DOUBLE_EQ(flow, 0.0);
180 EXPECT_DOUBLE_EQ(flow, 42.0);
188TEST(CapacityScalingTest, LinearNetwork)
191 build_linear_network(net);
195 EXPECT_DOUBLE_EQ(flow, 5.0);
199TEST(CapacityScalingTest, DiamondNetwork)
202 build_diamond_network(net);
206 EXPECT_DOUBLE_EQ(flow, 20.0);
210TEST(CapacityScalingTest, ComplexNetwork)
213 build_complex_network(net);
217 EXPECT_DOUBLE_EQ(flow, 23.0);
221TEST(CapacityScalingTest, LargeCapacities)
232 EXPECT_DOUBLE_EQ(flow, 1000000.0);
243 build_linear_network(net);
247 EXPECT_DOUBLE_EQ(flow, 5.0);
254 build_diamond_network(net);
258 EXPECT_DOUBLE_EQ(flow, 20.0);
265 build_complex_network(net);
269 EXPECT_DOUBLE_EQ(flow, 23.0);
278TEST(FlowDecompositionTest, LinearFlow)
281 build_linear_network(net);
286 EXPECT_EQ(decomp.num_paths(), 1);
287 EXPECT_DOUBLE_EQ(decomp.total_flow(), 5.0);
290TEST(FlowDecompositionTest, DiamondFlow)
293 build_diamond_network(net);
299 EXPECT_EQ(decomp.num_paths(), 2);
300 EXPECT_DOUBLE_EQ(decomp.total_flow(), 20.0);
303TEST(FlowDecompositionTest, ComplexFlow)
306 build_complex_network(net);
312 EXPECT_DOUBLE_EQ(decomp.total_flow(), 23.0);
314 EXPECT_GT(decomp.num_paths(), 1);
317TEST(FlowDecompositionTest, ZeroFlow)
331 EXPECT_EQ(decomp.num_paths(), 0);
332 EXPECT_DOUBLE_EQ(decomp.total_flow(), 0.0);
335TEST(FlowDecompositionTest, CycleDetection)
367 EXPECT_EQ(decomp.num_paths(), 1);
368 EXPECT_EQ(decomp.num_cycles(), 1);
370 EXPECT_DOUBLE_EQ(decomp.total_flow(), 5.0);
373 const auto& cycle = decomp.cycles.get_first();
374 EXPECT_DOUBLE_EQ(cycle.flow, 5.0);
377 const auto& path = decomp.paths.get_first();
378 EXPECT_DOUBLE_EQ(path.flow, 5.0);
386TEST(FlowStatisticsTest, BeforeFlow)
389 build_diamond_network(net);
393 EXPECT_DOUBLE_EQ(stats.total_capacity, 40.0);
394 EXPECT_EQ(stats.num_empty_arcs, 4);
395 EXPECT_EQ(stats.num_saturated_arcs, 0);
396 EXPECT_EQ(stats.num_partial_arcs, 0);
399TEST(FlowStatisticsTest, AfterFlow)
402 build_diamond_network(net);
407 EXPECT_DOUBLE_EQ(stats.total_flow, 20.0);
408 EXPECT_DOUBLE_EQ(stats.utilization, 0.5);
427 std::set<Node*> reachable;
441 Arc* arc = it.get_curr();
444 if (reachable.count(v) > 0)
449 double residual = forward ? (arc->cap - arc->flow) : arc->flow;
460 double cut_capacity = 0.0;
462 for (
auto u : reachable)
466 Arc* arc = it.get_curr();
469 if (reachable.count(v) == 0)
470 cut_capacity += arc->cap;
482 return std::abs(max_flow -
min_cut) < 1e-6;
488 build_linear_network(net);
497 EXPECT_DOUBLE_EQ(
min_cut, 5.0);
500TEST(MinCutTest, DiamondNetwork)
503 build_diamond_network(net);
510 EXPECT_DOUBLE_EQ(
min_cut, 20.0);
513TEST(MinCutTest, ComplexNetwork)
516 build_complex_network(net);
523 EXPECT_DOUBLE_EQ(
min_cut, 23.0);
536TEST(MinCutTest, SingleBottleneck)
553 EXPECT_DOUBLE_EQ(flow, 10.0);
557 EXPECT_DOUBLE_EQ(
min_cut, 10.0);
573 EXPECT_DOUBLE_EQ(flow, 60.0);
577 EXPECT_DOUBLE_EQ(
min_cut, 60.0);
580TEST(MinCutTest, AllAlgorithmsGiveSameMinCut)
583 TestNet net1, net2, net3, net4;
584 build_complex_network(net1);
585 build_complex_network(net2);
586 build_complex_network(net3);
587 build_complex_network(net4);
606 EXPECT_DOUBLE_EQ(cut1, cut2);
607 EXPECT_DOUBLE_EQ(cut2, cut3);
608 EXPECT_DOUBLE_EQ(cut3, cut4);
611TEST(MinCutTest, VerifyCutPartition)
615 build_diamond_network(net);
624 std::set<Node*> S_side;
637 Arc* arc = it.get_curr();
640 if (S_side.count(v) > 0)
644 double residual = forward ? (arc->cap - arc->flow) : arc->flow;
655 EXPECT_TRUE(S_side.count(source) > 0);
656 EXPECT_FALSE(S_side.count(sink) > 0);
659 for (
auto u : S_side)
663 Arc* arc = it.get_curr();
666 if (S_side.count(v) == 0)
669 EXPECT_NEAR(arc->flow, arc->cap, 1e-6);
675 Arc* arc = it.get_curr();
678 if (S_side.count(v) == 0)
681 EXPECT_NEAR(arc->flow, 0.0, 1e-6);
692TEST(AlgorithmComparisonTest, AllAlgorithmsAgree)
695 TestNet net1, net2, net3, net4;
696 build_complex_network(net1);
697 build_complex_network(net2);
698 build_complex_network(net3);
699 build_complex_network(net4);
706 EXPECT_DOUBLE_EQ(flow_ff, flow_ek);
707 EXPECT_DOUBLE_EQ(flow_ek, flow_dinic);
708 EXPECT_DOUBLE_EQ(flow_dinic, flow_cs);
711TEST(AlgorithmComparisonTest, GridNetwork)
724 EXPECT_DOUBLE_EQ(flow_ek, flow_dinic);
725 EXPECT_DOUBLE_EQ(flow_dinic, flow_cs);
733TEST(EdgeCasesTest, ParallelArcs)
745 EXPECT_DOUBLE_EQ(flow, 60.0);
748TEST(EdgeCasesTest, SourceBottleneck)
759 EXPECT_DOUBLE_EQ(flow, 5.0);
762TEST(EdgeCasesTest, SinkBottleneck)
773 EXPECT_DOUBLE_EQ(flow, 5.0);
784 build_diamond_network(net);
787 auto flow = algo(net);
788 EXPECT_DOUBLE_EQ(flow, 20.0);
791TEST(FunctorTest, CapacityScalingFunctor)
794 build_diamond_network(net);
797 auto flow = algo(net);
798 EXPECT_DOUBLE_EQ(flow, 20.0);
804 build_diamond_network(net);
807 auto flow = algo(net);
808 EXPECT_DOUBLE_EQ(flow, 20.0);
811TEST(FunctorTest, DecomposeFunctor)
814 build_diamond_network(net);
818 auto decomp = decomposer(net);
820 EXPECT_DOUBLE_EQ(decomp.total_flow(), 20.0);
828TEST(PerformanceTest, Benchmark)
830 const int grid_size = 10;
831 std::cout <<
"\n=== Max Flow Performance Benchmark (Grid "
832 << grid_size <<
"x" << grid_size <<
") ===\n";
834 auto benchmark = [&](
const std::string& name,
auto algo) {
838 auto start = std::chrono::high_resolution_clock::now();
839 auto flow = algo(net);
840 auto end = std::chrono::high_resolution_clock::now();
841 double ms = std::chrono::duration<double, std::milli>(end - start).count();
843 std::cout << name <<
": " << ms <<
" ms, flow=" << flow <<
"\n";
847 auto flow_ek = benchmark(
"Edmonds-Karp ", [](
TestNet& n) {
851 auto flow_dinic = benchmark(
"Dinic ", [](
TestNet& n) {
855 auto flow_cs = benchmark(
"Capacity Scaling", [](
TestNet& n) {
860 EXPECT_DOUBLE_EQ(flow_ek, flow_dinic);
861 EXPECT_DOUBLE_EQ(flow_dinic, flow_cs);
869TEST(RegressionTest, HLPPDisconnectedNodeRelabel)
894 EXPECT_DOUBLE_EQ(flow, 1.0);
897 double flow_in_a = 0, flow_out_a = 0;
900 auto arc = it.get_curr();
902 flow_in_a += arc->flow;
904 flow_out_a += arc->flow;
906 EXPECT_DOUBLE_EQ(flow_in_a, flow_out_a);
943TEST(AntiParallelTest, AllAlgorithmsAgreeOnAntiParallel)
946 TestNet net1, net2, net3, net4, net5;
962 EXPECT_DOUBLE_EQ(flow_ff, flow_ek);
963 EXPECT_DOUBLE_EQ(flow_ek, flow_dinic);
964 EXPECT_DOUBLE_EQ(flow_dinic, flow_cs);
965 EXPECT_DOUBLE_EQ(flow_cs, flow_hlpp);
979TEST(AntiParallelTest, PureAntiParallel)
997 EXPECT_DOUBLE_EQ(flow, 20.0);
1006TEST(CapacityScalingTest, FractionalCapacities)
1010 auto build_net = [](
TestNet& net) {
1018 build_net(net_dinic);
1023 EXPECT_DOUBLE_EQ(flow_dinic, 0.5);
1024 EXPECT_DOUBLE_EQ(flow_cs, 0.5);
1027TEST(CapacityScalingTest, MixedIntegerFractional)
1032 auto build_net = [](
TestNet& net) {
1045 build_net(net_dinic);
1051 EXPECT_DOUBLE_EQ(flow_dinic, 10.7);
1052 EXPECT_DOUBLE_EQ(flow_cs, 10.7);
1066 auto p = it.get_curr();
1070 double in_flow = 0, out_flow = 0;
1073 auto arc = ait.get_curr();
1075 in_flow += arc->flow;
1077 out_flow += arc->flow;
1080 EXPECT_NEAR(in_flow, out_flow, 1e-6)
1081 <<
"Conservation violated at node with in=" << in_flow
1082 <<
" out=" << out_flow;
1103 for (
int i = 0; i <
N; ++i)
1114 for (
int i = 0; i <
N - 1; ++i)
1116 net.
insert_arc(chain_a[i], chain_a[i+1], 50);
1117 net.
insert_arc(chain_b[i], chain_b[i+1], 50);
1121 for (
int i = 0; i <
N; ++i)
1132 auto build_ladder = [&](
TestNet& n) {
1133 auto s2 = n.insert_node(0);
1134 auto t2 = n.insert_node(1);
1137 for (
int i = 0; i <
N; ++i)
1139 ca[i] = n.insert_node(10 + i);
1140 cb[i] = n.insert_node(30 + i);
1142 n.insert_arc(s2, ca[0], 100);
1143 n.insert_arc(s2, cb[0], 100);
1144 for (
int i = 0; i <
N - 1; ++i)
1146 n.insert_arc(ca[i], ca[i+1], 50);
1147 n.insert_arc(cb[i], cb[i+1], 50);
1149 for (
int i = 0; i <
N; ++i)
1151 n.insert_arc(ca[i], cb[i], 10);
1152 n.insert_arc(cb[i], ca[i], 10);
1154 n.insert_arc(ca[
N-1], t2, 100);
1155 n.insert_arc(cb[
N-1], t2, 100);
1166 EXPECT_DOUBLE_EQ(flow_dinic, flow_hlpp);
1167 EXPECT_DOUBLE_EQ(flow_dinic, flow_ek);
1172 verify_flow_conservation<TestNet>(net);
1173 verify_flow_conservation<TestNet>(net2);
1176TEST(HLPPStressTest, HighExcessBottleneck)
1198 EXPECT_DOUBLE_EQ(flow, 2.0);
1200 verify_flow_conservation<TestNet>(net);
1208TEST(FlowDecompositionTest, ArcFlowConsistency)
1213 build_complex_network(net);
1220 auto arc = it.get_curr();
1221 original_flow[arc] = arc->flow;
1229 reconstructed_flow[it.get_curr()] = 0.0;
1231 for (
auto path_it = decomp.paths.get_it(); path_it.has_curr(); path_it.next_ne())
1233 const auto& path = path_it.get_curr();
1234 for (
auto arc_it = path.arcs.get_it(); arc_it.has_curr(); arc_it.next_ne())
1235 reconstructed_flow[arc_it.get_curr()] += path.flow;
1238 for (
auto cycle_it = decomp.cycles.get_it(); cycle_it.has_curr(); cycle_it.next_ne())
1240 const auto& cycle = cycle_it.get_curr();
1241 for (
auto arc_it = cycle.arcs.get_it(); arc_it.has_curr(); arc_it.next_ne())
1242 reconstructed_flow[arc_it.get_curr()] += cycle.flow;
1248 auto arc = it.get_curr();
1249 EXPECT_NEAR(reconstructed_flow[arc], original_flow[arc], 1e-6)
1250 <<
"Arc flow mismatch: reconstructed=" << reconstructed_flow[arc]
1251 <<
" original=" << original_flow[arc];
1255TEST(FlowDecompositionTest, Phase2CycleClosingNode)
1286 EXPECT_EQ(decomp.num_paths(), 1);
1287 EXPECT_DOUBLE_EQ(decomp.total_flow(), 5.0);
1290 EXPECT_GE(decomp.num_cycles(), 1);
1292 if (decomp.num_cycles() >= 1)
1294 const auto& cycle = decomp.cycles.get_first();
1295 EXPECT_DOUBLE_EQ(cycle.flow, 3.0);
1299 size_t n_nodes = 0, n_arcs = 0;
1300 for (
auto it = cycle.nodes.get_it(); it.has_curr(); it.next_ne())
1302 for (
auto it = cycle.arcs.get_it(); it.has_curr(); it.next_ne())
1305 EXPECT_EQ(n_nodes, n_arcs + 1)
1306 <<
"Cycle should have nodes.size() == arcs.size() + 1 (closing node)";
1309 auto first_node = cycle.nodes.get_first();
1310 auto last_node = cycle.nodes.get_last();
1312 <<
"Cycle first and last node should be the same";
1316TEST(FlowDecompositionTest, Phase1CycleClosingNode)
1338 EXPECT_EQ(decomp.num_paths(), 1);
1339 EXPECT_GE(decomp.num_cycles(), 1);
1341 if (decomp.num_cycles() >= 1)
1343 const auto& cycle = decomp.cycles.get_first();
1345 size_t n_nodes = 0, n_arcs = 0;
1346 for (
auto it = cycle.nodes.get_it(); it.has_curr(); it.next_ne())
1348 for (
auto it = cycle.arcs.get_it(); it.has_curr(); it.next_ne())
1351 EXPECT_EQ(n_nodes, n_arcs + 1)
1352 <<
"Phase-1 cycle should also have nodes.size() == arcs.size() + 1";
1354 auto first_node = cycle.nodes.get_first();
1355 auto last_node = cycle.nodes.get_last();
1357 <<
"Phase-1 cycle first and last node should be the same";
1366TEST(DisconnectedTest, ZeroCapacityBottleneck)
1379 EXPECT_DOUBLE_EQ(flow, 0.0);
1382TEST(DisconnectedTest, InvalidNetworkThrows)
1410 const int chain_len = 50;
1416 for (
int i = 0; i < chain_len; ++i)
1425 EXPECT_DOUBLE_EQ(flow, 100.0);
1433 const int width = 100;
1438 for (
int i = 0; i < width; ++i)
1449 for (
int i = 0; i < width; ++i)
1459 EXPECT_DOUBLE_EQ(flow_dinic,
static_cast<double>(width));
1460 EXPECT_DOUBLE_EQ(flow_hlpp,
static_cast<double>(width));
1476 template <
class NetT>
1477 void build_random_network(NetT & net,
int n,
int m,
int max_cap,
1481 <<
"build_random_network requires n >= 3";
1483 using NodeT =
typename NetT::Node;
1485 std::uniform_int_distribution<int> node_dist(0, n - 1);
1486 std::uniform_int_distribution<int> cap_dist(1, max_cap);
1488 std::vector<NodeT *>
nodes(n);
1489 for (
int i = 0; i < n; ++i)
1490 nodes[i] = net.insert_node(i);
1496 for (
int i = 0; i <
m - 2; ++i)
1498 int u = node_dist(
rng);
1499 int v = node_dist(
rng);
1500 if (u == v) v = (u + 1 < n) ? (u + 1) : 0;
1503 if (u == n - 1) u = n - 2;
1505 if (u == v)
continue;
1511 template <
class NetT>
1512 void build_n_random_networks(std::vector<NetT> & nets,
1514 int max_cap,
unsigned seed)
1517 for (
int i = 0; i <
count; ++i)
1518 build_random_network(nets[i], n,
m, max_cap,
seed);
1522TEST(RandomStressTest, SmallRandomGraphs)
1527 std::vector<TestNet> nets;
1528 build_n_random_networks(nets, 5, 8, 15, 100,
seed);
1530 if (not (nets[0].is_single_source()
and nets[0].is_single_sink()))
1538 EXPECT_DOUBLE_EQ(f_ek, f_dinic)
1539 <<
"EK vs Dinic disagree at seed=" <<
seed;
1540 EXPECT_DOUBLE_EQ(f_ek, f_cs)
1541 <<
"EK vs CapScale disagree at seed=" <<
seed;
1542 EXPECT_DOUBLE_EQ(f_ek, f_hlpp)
1543 <<
"EK vs HLPP disagree at seed=" <<
seed;
1545 EXPECT_TRUE(nets[0].check_network()) <<
"EK invalid at seed=" <<
seed;
1546 EXPECT_TRUE(nets[1].check_network()) <<
"Dinic invalid at seed=" <<
seed;
1547 EXPECT_TRUE(nets[2].check_network()) <<
"CapScale invalid at seed=" <<
seed;
1548 EXPECT_TRUE(nets[3].check_network()) <<
"HLPP invalid at seed=" <<
seed;
1552TEST(RandomStressTest, MediumRandomGraphs)
1557 std::vector<TestNet> nets;
1558 build_n_random_networks(nets, 4, 50, 150, 1000,
seed);
1560 if (not (nets[0].is_single_source()
and nets[0].is_single_sink()))
1567 EXPECT_DOUBLE_EQ(f_ek, f_dinic)
1568 <<
"EK vs Dinic disagree at seed=" <<
seed;
1569 EXPECT_DOUBLE_EQ(f_ek, f_hlpp)
1570 <<
"EK vs HLPP disagree at seed=" <<
seed;
1572 verify_flow_conservation<TestNet>(nets[0]);
1573 verify_flow_conservation<TestNet>(nets[1]);
1574 verify_flow_conservation<TestNet>(nets[2]);
1583TEST(IntegerFlowTest, AllAlgorithmsAgree)
1586 IntNet net1, net2, net3, net4;
1588 auto build_int_diamond = [](
IntNet & net) {
1590 auto a = net.insert_node(1);
1591 auto b = net.insert_node(2);
1592 auto t = net.insert_node(3);
1593 net.insert_arc(s, a, 10);
1594 net.insert_arc(s, b, 10);
1595 net.insert_arc(a, t, 10);
1596 net.insert_arc(b, t, 10);
1599 build_int_diamond(net1);
1600 build_int_diamond(net2);
1601 build_int_diamond(net3);
1602 build_int_diamond(net4);
1609 EXPECT_EQ(f_ek, 20);
1610 EXPECT_EQ(f_dinic, 20);
1611 EXPECT_EQ(f_cs, 20);
1612 EXPECT_EQ(f_hlpp, 20);
1619TEST(IntegerFlowTest, ComplexIntegerNetwork)
1623 auto build = [](
IntNet & net) {
1624 auto s = net.insert_node(0);
1625 auto a = net.insert_node(1);
1626 auto b = net.insert_node(2);
1627 auto c = net.insert_node(3);
1628 auto d = net.insert_node(4);
1629 auto t = net.insert_node(5);
1631 net.insert_arc(s, a, 16);
1632 net.insert_arc(s, c, 13);
1633 net.insert_arc(a, b, 12);
1634 net.insert_arc(a, c, 10);
1635 net.insert_arc(b, t, 20);
1636 net.insert_arc(c, d, 14);
1637 net.insert_arc(d, b, 7);
1638 net.insert_arc(d, t, 4);
1649 EXPECT_EQ(f_dinic, 23);
1650 EXPECT_EQ(f_cs, 23);
1651 EXPECT_EQ(f_hlpp, 23);
1657TEST(IntegerFlowTest, RandomIntegerGraphs)
1662 std::vector<IntNet> nets;
1663 build_n_random_networks(nets, 4, 12, 25, 50,
seed);
1665 if (not (nets[0].is_single_source()
and nets[0].is_single_sink()))
1673 EXPECT_EQ(f_ek, f_dinic)
1674 <<
"EK vs Dinic disagree at seed=" <<
seed;
1675 EXPECT_EQ(f_ek, f_cs)
1676 <<
"EK vs CapScale disagree at seed=" <<
seed;
1677 EXPECT_EQ(f_ek, f_hlpp)
1678 <<
"EK vs HLPP disagree at seed=" <<
seed;
1680 EXPECT_TRUE(nets[0].check_network()) <<
"seed=" <<
seed;
1681 EXPECT_TRUE(nets[1].check_network()) <<
"seed=" <<
seed;
1682 EXPECT_TRUE(nets[2].check_network()) <<
"seed=" <<
seed;
1683 EXPECT_TRUE(nets[3].check_network()) <<
"seed=" <<
seed;
1687TEST(IntegerFlowTest, MinCutVerification)
1692 auto build = [](
IntNet & net) {
1693 auto s = net.insert_node(0);
1694 auto a = net.insert_node(1);
1695 auto b = net.insert_node(2);
1696 auto c = net.insert_node(3);
1697 auto t = net.insert_node(4);
1699 net.insert_arc(s, a, 3);
1700 net.insert_arc(s, b, 2);
1701 net.insert_arc(a, b, 1);
1702 net.insert_arc(a, c, 3);
1703 net.insert_arc(b, c, 1);
1704 net.insert_arc(c, t, 4);
1722 auto arc = it.get_curr();
1726 if (arc->flow == arc->cap)
1727 cut_cap += arc->cap;
1730 EXPECT_GE(cut_cap, flow);
1738TEST(FordFulkersonTest, DecimalCapacitiesKeepTheBalance)
1741 const int spec[][3] = {{1, 3, 20}, {3, 5, 20}, {5, 2, 30}, {4, 3, 20},
1742 {0, 2, 29}, {4, 0, 2}, {0, 1, 1}, {5, 0, 10},
1743 {4, 0, 27}, {0, 1, 19}, {4, 5, 10}};
1744 auto build = [&spec] (
TestNet & net)
1746 std::vector<Node *>
nodes;
1747 for (
int i = 0; i < 6; ++i)
1748 nodes.push_back(net.insert_node(i));
1749 for (
const auto & a : spec)
1750 net.insert_arc(
nodes[
size_t(a[0])],
nodes[size_t(a[1])], a[2] / 10.0);
1762 const double dfs_value = aumenting_path_maximum_flow<TestNet, Find_Aumenting_Path_DFS>(dfs);
1766 EXPECT_TRUE(dfs.check_network());
1768 EXPECT_NEAR(ff_value, 5.9, 1e-12);
1769 EXPECT_NEAR(ek_value, 5.9, 1e-12);
1770 EXPECT_NEAR(dfs_value, 5.9, 1e-12);
1778TEST(FordFulkersonTest, ResiduesOfLargerArcsKeepTheBalance)
1781 const int spec[][3] = {{0, 18, 0}, {7, 10, 3}, {20, 4, 7}, {7, 16, 0}, {19, 16, 1},
1782 {16, 17, 2}, {4, 3, 7}, {10, 4, 3}, {18, 0, 0}, {9, 6, 2},
1783 {11, 20, 6}, {5, 14, 20}, {6, 20, 1}, {12, 13, 0}, {2, 14, 10},
1784 {3, 14, 8}, {15, 19, 1}, {3, 15, 2}, {4, 20, 0}, {1, 20, 0},
1785 {5, 7, 3}, {6, 12, 5}, {12, 19, 0}, {1, 6, 16}, {17, 3, 1},
1786 {16, 5, 11}, {16, 13, 1}, {2, 14, 1}, {6, 8, 10}, {17, 8, 5},
1787 {16, 6, 9}, {3, 17, 5}, {12, 11, 7}, {19, 20, 1}, {1, 5, 9},
1788 {11, 19, 1}, {1, 16, 14}, {16, 12, 7}, {15, 9, 2}, {8, 2, 11},
1790 auto build = [&spec] (
TestNet & net)
1792 std::vector<Node *>
nodes;
1793 for (
int i = 0; i < 21; ++i)
1794 nodes.push_back(net.insert_node(i));
1795 for (
const auto & a : spec)
1796 net.insert_arc(
nodes[
size_t(a[0])],
nodes[size_t(a[1])], a[2] / 10.0);
1807 ASSERT_EQ(ff.
get_sink()->get_info(), 14);
1810 const double dfs_value = aumenting_path_maximum_flow<TestNet, Find_Aumenting_Path_DFS>(dfs);
1814 EXPECT_TRUE(dfs.check_network());
1815 EXPECT_NEAR(ff_value, ek_value, 1e-12);
1816 EXPECT_NEAR(dfs_value, ek_value, 1e-12);
1824TEST(FordFulkersonTest, DecimalCapacitiesEndOnAMidSizeNetwork)
1831 std::mt19937 gen(1);
1832 std::vector<Node *>
nodes;
1833 for (
int i = 0; i < n; ++i)
1835 auto arc = [&] (
int a,
int b)
1837 const double cap = double(1 + gen() % 40) * 0.1;
1840 for (
int i = 1; i < n - 1; ++i)
1842 arc(
int(gen() %
unsigned(i)), i);
1843 arc(i, i + 1 +
int(gen() %
unsigned(n - 1 - i)));
1845 for (
int k = 0;
k < 10 * n; ++
k)
1847 const int a = int(gen() %
unsigned(n - 1));
1848 const int b = 1 + int(gen() %
unsigned(n - 1));
1856 EXPECT_NEAR(
value, 33.6, 1e-9);
1863 bool residual_path_to_sink(
TestNet & net)
1866 std::vector<Node *> queue = {net.
get_source()};
1867 for (
size_t k = 0;
k < queue.size(); ++
k)
1870 Arc * a = it.get_curr();
1876 if (v !=
nullptr and seen.insert(v).second)
1879 return seen.count(net.
get_sink()) > 0;
1884 void build_tenths(
TestNet & net,
int n,
const std::vector<std::array<int, 3>> &
arcs)
1886 std::vector<Node *>
nodes;
1887 for (
int i = 0; i < n; ++i)
1889 for (
const auto & a :
arcs)
1890 net.insert_arc(
nodes[size_t(a[0])],
nodes[size_t(a[1])], a[2] / 10.0);
1899 template <
template <
class>
class Maxflow>
1900 void expect_a_minimum_cut(
int n,
const std::vector<std::array<int, 3>> &
arcs,
1901 double value,
const char * name)
1905 build_tenths(net, n,
arcs);
1908 EXPECT_NEAR((min_cut<TestNet, Maxflow>(net, vs, vt, cuts, cutt)),
value, 1e-12);
1911 double capacity = 0;
1914 Arc * a = it.get_curr();
1916 if (from
and not to)
1919 EXPECT_EQ(a->flow, a->cap);
1921 else if (to
and not from)
1922 EXPECT_EQ(a->flow, 0);
1924 EXPECT_NEAR(capacity,
value, 1e-12);
1925 EXPECT_FALSE(residual_path_to_sink(net));
1928 std::vector<double> flows;
1930 flows.push_back(it.get_curr()->flow);
1934 EXPECT_EQ(it.get_curr()->flow, flows[i]);
1937 template <
template <
class>
class Maxflow>
1938 void expect_minimum_cuts_of_tenths(
const char * name)
1945 expect_a_minimum_cut<Maxflow>(4, {{0, 1, 31}, {0, 2, 1}, {1, 2, 8}, {2, 3, 29}}, 0.9, name);
1950 expect_a_minimum_cut<Maxflow>(6, {{0, 1, 13}, {1, 2, 25}, {0, 3, 15}, {2, 4, 25},
1951 {4, 5, 30}, {3, 4, 19}}, 2.8, name);
1954 template <
class Net>
1955 struct DFS_Augmenting_Paths
1959 return aumenting_path_maximum_flow<Net, Find_Aumenting_Path_DFS>(net);
1968TEST(MinCutTest, AFloatingFlowKeepsTheArcsOfTheCutFull)
1970 expect_minimum_cuts_of_tenths<Ford_Fulkerson_Maximum_Flow>(
"ford_fulkerson");
1971 expect_minimum_cuts_of_tenths<Edmonds_Karp_Maximum_Flow>(
"edmonds_karp");
1972 expect_minimum_cuts_of_tenths<DFS_Augmenting_Paths>(
"depth-first paths");
1973 expect_minimum_cuts_of_tenths<Heap_Preflow_Maximum_Flow>(
"heap_preflow");
1974 expect_minimum_cuts_of_tenths<Fifo_Preflow_Maximum_Flow>(
"fifo_preflow");
1975 expect_minimum_cuts_of_tenths<Random_Preflow_Maximum_Flow>(
"random_preflow");
1976 expect_minimum_cuts_of_tenths<HLPP_Maximum_Flow>(
"hlpp");
1977 expect_minimum_cuts_of_tenths<Dinic_Maximum_Flow>(
"dinic");
1978 expect_minimum_cuts_of_tenths<Capacity_Scaling_Maximum_Flow>(
"capacity_scaling");
1986TEST(ReturnDustTest, KeepsTheResiduesWithinTheToleranceBesideDust)
1989 std::vector<Node *> n;
1990 for (
int i = 0; i < 8; ++i)
1993 auto arc = [&] (
int u,
int v,
double cap,
double flow)
1999 Arc * kept[] = {arc(0, 1, 3.1, 0.8), arc(0, 2, 0.1, 0.1), arc(1, 2, 0.8, 0.8),
2000 arc(2, 3, 2.9, 0.9), arc(0, 5, 0.7, 0.7), arc(5, 6, 0.1, 0.1),
2001 arc(5, 7, 0.6, 0.6), arc(6, 3, 3.2, 0.1), arc(7, 3, 0.7, 0.6)};
2002 Arc * dust = arc(0, 4, 1, 5.5511151231257827e-17);
2006 std::vector<double> flows;
2007 for (
Arc * a : kept)
2008 flows.push_back(a->flow);
2012 for (
size_t i = 0; i < flows.size(); ++i)
2013 EXPECT_EQ(kept[i]->flow, flows[i]) <<
"arc " << i;
2014 EXPECT_EQ(dust->flow, 0);
2026TEST(ReturnDustTest, ReturnsTheDustOfAnEndFromItsSmallestFlowsFirst)
2028 const double d = 0x1p-40;
2029 for (
bool at_source : {
true,
false})
2038 Arc * big, * small, * back;
2059 const char * end = at_source ?
"source" :
"sink";
2060 EXPECT_EQ(big->flow, 4 * d) << end;
2061 EXPECT_EQ(small->flow, 0) << end;
2062 EXPECT_EQ(back->flow, 4 * d) << end;
2069 struct Named_Maximum_Flow
2076 const Named_Maximum_Flow path_maximum_flows[] = {
2083 const Named_Maximum_Flow preflow_maximum_flows[] = {
2093TEST(InfiniteCapacityTest, ArcsOfInfiniteCapacityAloneGiveAnInfiniteMaximumFlow)
2095 const double inf = std::numeric_limits<double>::infinity();
2096 auto check = [inf] (
const Named_Maximum_Flow & f)
2104 EXPECT_EQ(f.run(net), inf) << f.name;
2107 for (
const auto & f : path_maximum_flows)
2109 for (
const auto & f : preflow_maximum_flows)
2120TEST(InfiniteCapacityTest, AFinitePathThroughAnInfiniteArcCarriesItsBottleneck)
2122 const double inf = std::numeric_limits<double>::infinity();
2123 auto check = [inf] (
const Named_Maximum_Flow & f)
2134 EXPECT_EQ(f.run(net), 4.0) << f.name;
2141 auto check_bound = [inf] (
const Named_Maximum_Flow & f)
2152 EXPECT_EQ(f.run(net), 6.0) << f.name;
2155 for (
const auto & f : path_maximum_flows)
2160 for (
const auto & f : preflow_maximum_flows)
2173TEST(InfiniteCapacityTest, APathOfInfiniteArcsMakesTheMaximumFlowInfinite)
2175 auto check = [] (
const Named_Maximum_Flow & f,
double cap)
2184 EXPECT_EQ(f.run(net), cap) << f.name <<
" with a capacity of " << cap;
2186 for (
const double cap : {std::numeric_limits<double>::infinity(),
2187 std::numeric_limits<double>::max()})
2189 for (
const auto & f : path_maximum_flows)
2191 for (
const auto & f : preflow_maximum_flows)
2199TEST(InfiniteCapacityTest, AnInfinitePathBesideAnInfiniteArcToAFinitePart)
2201 const double inf = std::numeric_limits<double>::infinity();
2202 auto check = [inf] (
const Named_Maximum_Flow & f)
2213 EXPECT_EQ(f.run(net), inf) << f.name;
2216 for (
const auto & f : path_maximum_flows)
2218 for (
const auto & f : preflow_maximum_flows)
2227TEST(InfiniteCapacityTest, DinicAndHlppKeepTheCookiesOfTheNodes)
2229 const double inf = std::numeric_limits<double>::infinity();
2230 const Named_Maximum_Flow keepers[] = {
2233 for (
const auto & f : keepers)
2234 for (
const double cap : {2.5, inf})
2242 int tags[3] = {0, 1, 2};
2246 EXPECT_EQ(f.run(net), cap) << f.name <<
" with a -> t of " << cap;
2247 EXPECT_EQ(
NODE_COOKIE(s),
static_cast<void *
>(&tags[0]))
2248 << f.name <<
" with a -> t of " << cap;
2249 EXPECT_EQ(
NODE_COOKIE(a),
static_cast<void *
>(&tags[1]))
2250 << f.name <<
" with a -> t of " << cap;
2251 EXPECT_EQ(
NODE_COOKIE(t),
static_cast<void *
>(&tags[2]))
2252 << f.name <<
" with a -> t of " << cap;
2259 ::testing::InitGoogleTest(&argc, argv);
2260 return RUN_ALL_TESTS();
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
WeightedDigraph::Node Node
size_t size_t int32_t value
Iterator over the arcs of a node.
bool has_curr() const noexcept
Return true if the iterator is on an arc.
static Array create(const size_t n)
Create an array with n logical elements.
bool has_curr() const noexcept
Return true the iterator has an current arc.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
T & front()
Return a modifiable reference to the oldest item in the queue.
T & insert(const T &data)
bool is_empty() const noexcept
Return true if this is empty.
Doubly-linked list (defined in tpl_dynList.H).
Generic key-value map implemented on top of a binary search tree.
Dynamic set backed by balanced binary search trees with automatic memory management.
bool contains(const Key &key) const
Checks if a key exists in the set.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Filtered iterator for incoming arcs of a node.
Number Theoretic Transform over Z / MOD Z.
Node * insert_node(Node *p) override
Filtered iterator on the nodes of a graph.
Filtered iterator for outcoming arcs of a node.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
#define NODE_COOKIE(p)
Return the node cookie
Net build_grid_network(int rows, int cols, FlowType base_cap=10.0)
Build a grid network for stress testing.
void return_dust(Net &net, typename Net::Node *source, typename Net::Node *sink)
Returns the dust of a floating flow that a preflow algorithm left in net, from source to sink: what e...
Main namespace for Aleph-w library functions.
FlowStatistics< typename Net::Flow_Type > compute_flow_statistics(const Net &net)
Compute statistics about the current flow in a network.
Net::Flow_Type edmonds_karp_maximum_flow(Net &net)
Maximize flow using the Edmonds-Karp algorithm.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Net::Flow_Type random_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a random active-node queue.
Net::Flow_Type dinic_maximum_flow(Net &net)
Compute maximum flow using Dinic's algorithm.
Net::Flow_Type heap_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a height-ordered heap.
FlowDecomposition< Net > decompose_flow(const Net &net)
Decompose network flow into paths and cycles.
Net::Flow_Type capacity_scaling_maximum_flow(Net &net)
Compute maximum flow using capacity scaling.
Net::Flow_Type hlpp_maximum_flow(Net &net)
Compute maximum flow using Highest-Label Preflow-Push.
and
Check uniqueness with explicit hash + equality functors.
Net::Flow_Type fifo_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a FIFO queue of active nodes.
Net::Flow_Type ford_fulkerson_maximum_flow(Net &net)
Maximize flow using the Ford-Fulkerson algorithm.
Net::Flow_Type min_cut(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Compute max flow and the corresponding minimum cut.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Filtered iterator on all the arcs of a graph.
Functor wrapper for capacity scaling.
Functor wrapper for flow decomposition.
Functor wrapper for Dinic's algorithm.
Functor wrapper for edmonds_karp_maximum_flow().
Functor wrapper for ford_fulkerson_maximum_flow().
Functor wrapper for HLPP.
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
bool check_network() const
Validate flow-conservation and capacity constraints.
constexpr bool is_single_source() const noexcept
Return true if the network has a single source.
bool is_source(Node *node) const noexcept
Return true if node is a source.
Node * get_source() const
Return an arbitrary source node.
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, const typename Arc::Arc_Type &arc_info=Arc_Type())
Insert a capacitated arc with an initial flow.
Node * get_sink() const
Return an arbitrary sink node.
bool is_sink(Node *node) const noexcept
Return true if node is a sink.
typename Arc::Flow_Type Flow_Type
Capacity/flow numeric type.
constexpr bool is_single_sink() const noexcept
Return true if the network has a single sink.
bool check_node(Node *node) const noexcept
Return true if node satisfies flow conservation constraints.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Advanced maximum flow algorithms.
void verify_flow_conservation(const Net &net)
Node * build_antiparallel_network(TestNet &net)
bool verify_max_flow_min_cut(Net &net, double max_flow)
double compute_min_cut_capacity(Net &net)
Network flow graph structures.
Treap< int >::Node * last_node