diff --git a/src/algorithms/searching/linear_search.h b/src/algorithms/searching/linear_search.h index b3480324..e8c01328 100644 --- a/src/algorithms/searching/linear_search.h +++ b/src/algorithms/searching/linear_search.h @@ -14,7 +14,7 @@ * @return false otherwise */ template bool linear_search(std::vector arr, T key) { - return std::find_if(arr.begin(), arr.end(), [key](const auto &x) { return x == key; }) + return std::find_if(arr.begin(), arr.end(), [key](const auto &x) { return x == key; }) != arr.end(); } #endif diff --git a/src/classes/graph/graph.h b/src/classes/graph/graph.h index 7d8c7bbc..73566858 100644 --- a/src/classes/graph/graph.h +++ b/src/classes/graph/graph.h @@ -1320,70 +1320,6 @@ std::unordered_map weighted_graph::bellman_ford(T start) { return dist; } -template -int weighted_graph::max_flow(T start, T end) { - assert(this->_elements.find(start) != this->_elements.end() && this->_elements.find(end) != this->_elements.end()); - int flow_inf = 1e9; - - std::unordered_map > > residual; - std::unordered_map level; - std::queue q; - - auto bfs = [&]() -> bool { - while(!q.empty()) { - T current = q.front(); - q.pop(); - - for(auto &[neigh, flow, cap]: residual[current]) { - if(cap == flow) { continue; } - if(level[neigh] != -1) { continue; } - - level[neigh] = level[current] + 1; - q.push(neigh); - } - } - - return level[end] != -1; - }; - - auto dfs = [&](T _start, int pushed) -> int { - if(pushed == 0) { return 0; } - - std::stack s; - while(!s.empty()) { - auto current = s.top(); - q.pop(); - - for(auto &[neigh, flow, cap]: residual[current]) { - if(level[neigh] != level[current] + 1) { continue; } - flow += - } - } - - if(_start == end) { return pushed; } - - }; - - - int flow = 0; - while(true) { - for(auto els: this->_elements) { - level[els] = -1; - } - level[start] = 0; - q.push(start); - if(!bfs()) { - break; - } - - while(int pushed = dfs(start, flow_inf)) { - flow += pushed; - } - } - - return flow; -} - #ifdef GRAPH_VISUALIZATION_H template void weighted_graph::visualize() { std::string s; diff --git a/tests/graph/weighted_graph.cc b/tests/graph/weighted_graph.cc index 9301f875..2ab0a0cc 100644 --- a/tests/graph/weighted_graph.cc +++ b/tests/graph/weighted_graph.cc @@ -1,4 +1,3 @@ -#define CATCH_CONFIG_MAIN #include "../../src/visualization/graph_visual/graph_visualization.h" #include "../../src/classes/graph/graph.h" #include "../../third_party/catch.hpp" @@ -348,29 +347,29 @@ TEST_CASE("Testing scc function for weighted graph class") { REQUIRE(g3.scc() == 4); } -TEST_CASE("Testing maximum flow with dinic's algorithm") { - weighted_graph g("directed"); - g.add_edge('s', 'A', 7); - g.add_edge('s', 'D', 4); - g.add_edge('A', 'B', 5); - g.add_edge('A', 'C', 3); - g.add_edge('D', 'A', 3); - g.add_edge('D', 'C', 2); - g.add_edge('B', 't', 8); - g.add_edge('C', 't', 5); - g.add_edge('C', 'B', 3); - - std::unordered_map > > f = g.max_flow('s', 't'); - - for(const auto x: f) { - std::cout << x.first << ": "; - for(auto &y: x.second) { - auto [xx, flow, capacity] = y; - std::cout << xx << " " << flow << "/" << capacity << '\n'; - } - std::cout << '\n'; - } -} +// TEST_CASE("Testing maximum flow with dinic's algorithm") { +// weighted_graph g("directed"); +// g.add_edge('s', 'A', 7); +// g.add_edge('s', 'D', 4); +// g.add_edge('A', 'B', 5); +// g.add_edge('A', 'C', 3); +// g.add_edge('D', 'A', 3); +// g.add_edge('D', 'C', 2); +// g.add_edge('B', 't', 8); +// g.add_edge('C', 't', 5); +// g.add_edge('C', 'B', 3); +// +// std::unordered_map > > f = g.max_flow('s', 't'); +// +// for(const auto x: f) { +// std::cout << x.first << ": "; +// for(auto &y: x.second) { +// auto [xx, flow, capacity] = y; +// std::cout << xx << " " << flow << "/" << capacity << '\n'; +// } +// std::cout << '\n'; +// } +// } #define GRAPH_VISUALIZATION_H