Skip to content
Snippets Groups Projects
Commit 17d387c8 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add Edmonds-Karp

parent 75269967
No related branches found
No related tags found
No related merge requests found
/**
* Edmonds-Karp
*
* Complexity (time): O(V*E^2)
* Complexity (space): O(V^2)
*/
int N;
int par[MAX];
int graph[MAX][MAX], rg[MAX][MAX];
bool cont[MAX];
// Finds if there's a path between s and t using non-full
// residual edges
bool path(int s, int t) {
queue<int> Q;
Q.push(s);
cont[s] = true;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
if (u == t)
return true;
for (int i = 0; i < N; ++i)
if (!cont[i] && rg[u][i]) {
cont[i] = true;
par[i] = u;
Q.push(i);
}
}
return false;
}
// Returns maximum flow between s (source) and t (sink)
int edmonds_karp(int s, int t) {
int ans = 0;
par[s] = -1;
mset(cont, 0);
memcpy(rg, graph, sizeof(graph));
// Repeat while there's a valid path between s and t
while (path(s, t)) {
int flow = inf;
// Get the minimum capacity among all edges of the chosen path
for (int i = t; par[i] != -1; i = par[i])
flow = min(f, rg[par[i]][i]);
// Update residual graph
for (int i = t; par[i] != -1; i = par[i]) {
rg[par[i]][i] -= flow;
rg[i][par[i]] += flow;
}
ans += flow;
mset(cont, 0);
}
return ans;
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Ford-Fulkerson * Ford-Fulkerson
* *
* Complexity (time): O(Ef) * Complexity (time): O(Ef)
* Complexity (space): O(V + E) * Complexity (space): O(V^2)
*/ */
int N; int N;
......
...@@ -32,6 +32,7 @@ def gen_code_latex(arr, depth): ...@@ -32,6 +32,7 @@ def gen_code_latex(arr, depth):
for i in arr: for i in arr:
print('\\' + depth + '{' + i + '}') print('\\' + depth + '{' + i + '}')
def gen_latex(tree): def gen_latex(tree):
stack = [ (tree, 0) ] stack = [ (tree, 0) ]
depth = ['chapter', 'section', 'subsection'] depth = ['chapter', 'section', 'subsection']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment