Skip to content

Commit

Permalink
[Update] Remove unsued files | correct linear search
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Dec 25, 2024
1 parent de7ea21 commit 2b7de3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/algorithms/searching/linear_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @return false otherwise
*/
template <typename T> bool linear_search(std::vector<T> 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
64 changes: 0 additions & 64 deletions src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1320,70 +1320,6 @@ std::unordered_map<T, double> weighted_graph<T>::bellman_ford(T start) {
return dist;
}

template <typename T>
int weighted_graph<T>::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<T, std::vector<std::tuple<T, double, double, T> > > residual;
std::unordered_map<T, int> level;
std::queue<T> 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<T> 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 <typename T> void weighted_graph<T>::visualize() {
std::string s;
Expand Down
47 changes: 23 additions & 24 deletions tests/graph/weighted_graph.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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<char> 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<char, std::vector<std::tuple<char, double, double> > > 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<char> 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<char, std::vector<std::tuple<char, double, double> > > 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

Expand Down

0 comments on commit 2b7de3d

Please sign in to comment.