forked from ankit-kaushal/Lets-go-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ford fulkerson.cpp
65 lines (58 loc) · 1.43 KB
/
ford fulkerson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#define N 8
#define INF 9999999
int Flow[N][N];
int visited[N];
int min(int a, int b);
int dfs(int s, int t, int minimum);
int graph[N][N] = { {0, 2, 0, 0, 0, 0, 0, 4},
{0, 0, 0, 2, 0, 0, 0, 3},
{0, 0, 0, 4, 3, 0, 0, 0},
{5, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 4, 0, 0},
{0, 0, 0, 2, 0, 0, 0, 3},
{3, 0, 5, 0, 2, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0} };
int main() {
int sent;
memset(Flow, 0, sizeof(Flow));
memset(visited, 0, sizeof(visited));
int s = 6;
int t = 7;
int max_flow = 0;
while (sent = dfs(s, t, INF)) {
max_flow += sent;
memset(visited, 0, sizeof(visited));
}
printf("The max flow from source to sink is %d", max_flow);
printf("\n");
}
int min(int a, int b)
{
if(a<b)
{
return a;
}
else
{
return b;
}
}
int dfs(int s, int t, int minimum) {
int i, sent;
visited[s] = 1;
if (s == t)
return minimum;
for (i = 0; i < N; i++) {
int flow_capacity = graph[s][i] - Flow[s][i];
if (!visited[i] && flow_capacity > 0) {
if (sent = dfs (i, t, min(minimum, flow_capacity))) {
Flow[s][i] += sent;
Flow[i][s] -= sent;
return sent;
}
}
}
return 0;
}