diff --git a/test/edmonds_karp_test.cpp b/test/edmonds_karp_test.cpp index 3b323037462d4198dc285b26d93988eb52b9e721..96db85d12f400c4f58704b1cc4b7d6be5c3c08ed 100644 --- a/test/edmonds_karp_test.cpp +++ b/test/edmonds_karp_test.cpp @@ -4,8 +4,96 @@ #include <gp-bnb/graph.hpp> #include <gp-bnb/metis_reader.hpp> -// To do: tests for Edmonds-Karp Max Flow algorithm +// Tests for Edmonds-Karp Max Flow algorithm TEST_CASE("EdmondsKarpMaxFlow") { - + // Same graph instance as in IBFS test + std::vector<std::vector<node_id>> adjacency_list { + { 2, 7 }, // 1 + { 1, 3, 4 }, // 2 + { 2, 5 }, // 3 + { 2, 5 }, // 4 + { 3, 4, 6}, // 5 + { 5, 8 }, // 6 + { 1, 8 }, // 7 + { 6, 7 } // 8 + }; + + graph g(adjacency_list); + + std::vector<node_id> sources { 1 }; + std::vector<node_id> sinks { 8 }; + edmonds_karp ek = edmonds_karp(g, sources, sinks); + + ek.run(); + int max_flow = ek.get_max_flow(); + + REQUIRE(max_flow == 2); + + //Testing multiple sources and sinks + std::vector<std::vector<node_id>> adjacency_list2 { + { 5, 2, 3 }, + { 1, 3, 4 }, + { 5, 4, 2, 1 }, + { 2, 3, 6, 7 }, + { 1, 3, 6 }, + { 5, 4, 7 }, + { 6, 4 } + }; + + graph g2(adjacency_list2); + + // 1 Case for g2 + std::vector<node_id> sources2 { 1 }; + std::vector<node_id> sinks2 { 6, 7 }; + edmonds_karp ek2 = edmonds_karp(g2, sources2, sinks2); + + ek2.run(); + int max_flow2 = ek2.get_max_flow(); + + REQUIRE(max_flow2 == 3); + + // 2 Case for g2 + std::vector<node_id> sources3 { 2, 3 }; + std::vector<node_id> sinks3 { 5, 6 }; + edmonds_karp ek3 = edmonds_karp(g2, sources3, sinks3); + + ek3.run(); + int max_flow3 = ek3.get_max_flow(); + + REQUIRE(max_flow3 == 4); + + // 3 Case for g2 (balanced 2-partition) + std::vector<node_id> sources4 { 1, 4, 6 }; + std::vector<node_id> sinks4 { 2, 3, 5, 7 }; + edmonds_karp ek4 = edmonds_karp(g2, sources4, sinks4); + + ek4.run(); + int max_flow4 = ek4.get_max_flow(); + + REQUIRE(max_flow4 == 8); + + // 4 Case for g2 (balanced 2-partition) + std::vector<node_id> sources5 { 1, 2, 3, 5 }; + std::vector<node_id> sinks5 { 4, 6, 7 }; + edmonds_karp ek5 = edmonds_karp(g2, sources5, sinks5); + + ek5.run(); + int max_flow5 = ek5.get_max_flow(); + + REQUIRE(max_flow5 == 3); + + // Testing on a graph with ~1000 nodes + std::string graph_file = "../test/inputs/delaunay_n10.graph"; + graph g3 = metis_reader().read(graph_file); + + std::vector<node_id> sources6 = {1, 3, 8, 9, 100}; + std::vector<node_id> sinks6 = {50, 60, 88}; + edmonds_karp ek6 = edmonds_karp(g3, sources6, sinks6); + + ek6.run(); + int max_flow6 = ek6.get_max_flow(); + + REQUIRE(max_flow6 == 18); + } \ No newline at end of file