#include <stdio.h>
#define A 0
#define B 1
#define C 2
#define MAX_NODES 1000
#define O 1000000000
int n;
int e=0;
int capacity[MAX_NODES][MAX_NODES];
int flow[MAX_NODES][MAX_NODES];
int color[MAX_NODES];
int pred[MAX_NODES];
int min(int x, int y)
{
return x < y ? x : y;
}
int head, tail;
int q[MAX_NODES + 2];
void enqueue(int x)
{
q[tail] = x;
tail++;
color[x] = B;
}
int dequeue()
{
int x = q[head];
head++;
color[x] = C;
return x;
}
int bfs(int start, int target)
{
int u, v;
for (u = 0; u < n; u++)
{
color[u] = A;
}
head = tail = 0;
enqueue(start);
pred[start] = -1;
while (head != tail)
{
u = dequeue();
for (v = 0; v < n; v++)
{
if (color[v] == A && capacity[u][v] - flow[u][v] > 0)
{
enqueue(v);
pred[v] = u;
}
}
}
return color[target] == C;
}
int max_flow(int source, int sink)
{
int i, j, u;
int max_flow = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
flow[i][j] = 0;
}
}
while (bfs(source, sink))
{
int increment = O;
for (u = n - 1; pred[u] >= 0; u = pred[u])
{
increment = min(increment, capacity[pred[u]][u] - flow[pred[u]][u]);
}
for (u = n - 1; pred[u] >= 0; u = pred[u])
{
flow[pred[u]][u] += increment;
flow[u][pred[u]] -= increment;
}
max_flow += increment;
}
return max_flow;
}
int main()
{
printf("\t\t ----```` CrazyCoder -- Tech Apurba ````----\n");
printf("\n\n");
printf("Enter the number of nodes you want :- \n");
scanf("%d",&n);
printf("Enter the adjacency matrix :- \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d",&capacity[i][j]);
if(capacity[i][j]!=0)
e++;
}
}
int s,t;
printf("Enter the starting and ending nodes\n");
scanf("%d%d",&s,&t);
printf("Max Flow: %d\n", max_flow(s, t)); }
OUTPUT :-
For
Other Programs Visit The WebSite:- https://www.techapurba.com/
Follow
Me On Social Media :-
Instagram
link :- https://www.instagram.com/apurba_khanra/
INSTA photography page :- https://www.instagram.com/photo_mania_hub/
Twitter link : - https://twitter.com/ApurbaKhanra199
Facebook page link (Crazy Coders) :-https://www.facebook.com/codercrazy/?modal=admin_todo_tour
Insta photography link :- https://www.instagram.com/photo_mania_hub/
Facebook link :- https://www.facebook.com/profile.php?id=100009747586288
FB photography page :-
https://www.facebook.com/photomaniahub/?modal=admin_todo_tour
For tech related videos visit my other website :- https://apurbatechinfo.blogspot.com/
0 Comments