BCSE204L:Design and Analysis of Algorithms Lab Slot: L29+L30
BCSE204L:Design and Analysis of Algorithms Lab Slot: L29+L30
Submitted by:
NAME: A MANOJ SAI
Reg No: 21BDS0224
B.Tech : 2nd Year
Name of School: Scope
1 | Page
[Document title]
Code:
Ford-Fulkerson Approach:
For Graph 1:
#include <iostream>
2 | Page
[Document title]
#include <string.h>
using namespace std;
#define N 10
#define INF 9999999
int Flow[N][N];
bool visited[N];
int graph[N][N] = {
{ 0, 6, 0, 0, 0, 6, 0, 0, 2, 0}, //s
{ 0, 0, 3, 0, 0, 3, 0, 0, 0, 0}, //a
{ 0, 0, 0, 2, 0, 6, 0, 3, 0, 0 }, //b
{ 0, 0, 0, 0, 4, 0, 0, 0, 0, 0}, //c
{ 0, 0, 0, 0, 0, 0, 0, 4, 0, 7}, //d
{ 0, 0, 0, 0, 0, 0, 3, 0, 2, 0}, //e
{ 0, 0, 0, 0, 0, 0, 0, 2, 1, 0}, //f
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}, //g
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 6}, //h
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //t
};
int min(int a,int b)
3 | Page
[Document title]
{
if(a<b)
return a;
else
return b;
}
int dfs(int s, int t, int minimum) {
visited[s] = true;
if (s == t)
return minimum;
for (int i = 0; i < N; i++) {
int flow_capacity = graph[s][i] - Flow[s][i];
if (!visited[i] && flow_capacity > 0) {
if (int sent = dfs (i, t, min(minimum, flow_capacity))) {
Flow[s][i] += sent;
Flow[i][s] -= sent;
return sent;
}
}
}
4 | Page
[Document title]
return false;
}
int main() {
memset(Flow, 0, sizeof(Flow));
memset(visited, 0, sizeof(visited));
int s = 0;
int t = 9;
int max_flow = 0;
while (int sent = dfs(s, t, INF)) {
max_flow += sent;
memset(visited, 0, sizeof(visited));
}
printf("The max flow from node %d to sink node %d is
%d",s,t,max_flow);
}
Output:
5 | Page
[Document title]
For Graph 2:
#include <iostream>
#include <string.h>
using namespace std;
#define N 9
#define INF 9999999
int Flow[N][N];
bool visited[N];
int graph[N][N] = {
{ 0, 26, 17, 0, 37,0, 0, 0, 0}, //s
{ 0, 0, 0, 20, 10, 0, 0, 0, 0}, //a
{ 0, 0, 0, 0, 20, 15, 0, 3, 0}, //b
{ 0, 0, 0, 0, 0, 10, 22, 18,0}, //c
{ 0, 0, 0, 35, 0, 15, 0, 0, 0}, //d
{ 0, 0, 0, 0, 0, 0, 0, 34, 0}, //e
{ 0, 0, 0, 0, 0, 0, 0, 0, 48}, //f
{ 0, 0, 0, 0, 0, 0, 20, 0, 30}, //g
{ 0, 0, 0, 0, 0, 0, 0, 0, 0}, //t
};
6 | Page
[Document title]
7 | Page
[Document title]
}
return false;
}
int main() {
memset(Flow, 0, sizeof(Flow));
memset(visited, 0, sizeof(visited));
int s = 0;
int t = 8;
int max_flow = 0;
while (int sent = dfs(s, t, INF)) {
max_flow += sent;
memset(visited, 0, sizeof(visited));
}
printf("The max flow from node %d to sink node %d is
%d",s,t,max_flow);
}
Output:
8 | Page
[Document title]
For Graph 3:
#include <iostream>
#include <string.h>
using namespace std;
#define N 9
#define INF 9999999
int Flow[N][N];
bool visited[N];
int graph[N][N] = {
{ 0, 26, 17, 0, 37,0, 0, 0, 0}, //s
{ 0, 0, 0, 20, 5, 0, 0, 0, 0}, //a
{ 0, 0, 0, 0, 20, 15, 0, 3, 0}, //b
{ 0, 0, 0, 0, 0, 10, 22, 18,0}, //c
{ 0, 0, 0, 25, 0, 15, 0, 0, 0}, //d
{ 0, 0, 0, 0, 0, 0, 0, 34, 0}, //e
{ 0, 0, 0, 0, 0, 0, 0, 0, 24}, //f
{ 0, 0, 0, 0, 0, 0, 20, 0, 26}, //g
{ 0, 0, 0, 0, 0, 0, 0, 0, 0}, //t
};
9 | Page
[Document title]
10 | P a g e
[Document title]
}
return false;
}
int main() {
memset(Flow, 0, sizeof(Flow));
memset(visited, 0, sizeof(visited));
int s = 0;
int t = 8;
int max_flow = 0;
while (int sent = dfs(s, t, INF)) {
max_flow += sent;
memset(visited, 0, sizeof(visited));
}
printf("The max flow from node %d to sink node %d is
%d",s,t,max_flow);
}
Output:
11 | P a g e
[Document title]
12 | P a g e
[Document title]
13 | P a g e
[Document title]
queue[rear++] = v;
parent[v] = u;
visited[v] = 1;
}
}
}
14 | P a g e
[Document title]
15 | P a g e
[Document title]
16 | P a g e
[Document title]
int s=0;
int t=9;
int graph[V][V] = {
{ 0, 6, 0, 0, 0, 6, 0, 0, 2, 0}, //s
{ 0, 0, 3, 0, 0, 3, 0, 0, 0, 0}, //a
{ 0, 0, 0, 2, 0, 6, 0, 3, 0, 0 }, //b
{ 0, 0, 0, 0, 4, 0, 0, 0, 0, 0}, //c
{ 0, 0, 0, 0, 0, 0, 0, 4, 0, 7}, //d
{ 0, 0, 0, 0, 0, 0, 3, 0, 2, 0}, //e
{ 0, 0, 0, 0, 0, 0, 0, 2, 1, 0}, //f
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}, //g
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 6}, //h
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},};
printf("The max flow from node %d to sink node %d is %d",
s,t,edmonds_karp(graph,0,9));
}
Output:
17 | P a g e
[Document title]
For Graph 2:
#include<stdio.h>
#include <string.h>
#include <limits.h>
#define V 9
18 | P a g e
[Document title]
19 | P a g e
[Document title]
20 | P a g e
[Document title]
21 | P a g e
[Document title]
22 | P a g e
[Document title]
{ 0, 0, 0, 0, 0, 0, 0, 0, 48}, //f
{ 0, 0, 0, 0, 0, 0, 20, 0, 30}, //g
{ 0, 0, 0, 0, 0, 0, 0, 0, 0}};//t
printf("The max flow from node %d to sink node %d is %d",
s,t,edmonds_karp(graph,0,8));
}
Output:
For Graph 3:
#include<stdio.h>
#include <string.h>
#include <limits.h>
#define V 9
23 | P a g e
[Document title]
24 | P a g e
[Document title]
parent[v] = u;
visited[v] = 1;
}
}
}
25 | P a g e
[Document title]
26 | P a g e
[Document title]
27 | P a g e
[Document title]
int t=8;
int graph[V][V] = {
{ 0, 26, 17, 0, 37,0, 0, 0, 0}, //s
{ 0, 0, 0, 20, 5, 0, 0, 0, 0}, //a
{ 0, 0, 0, 0, 20, 15, 0, 3, 0}, //b
{ 0, 0, 0, 0, 0, 10, 22, 18,0}, //c
{ 0, 0, 0, 25, 0, 15, 0, 0, 0}, //d
{ 0, 0, 0, 0, 0, 0, 0, 34, 0}, //e
{ 0, 0, 0, 0, 0, 0, 0, 0, 24}, //f
{ 0, 0, 0, 0, 0, 0, 20, 0, 26}, //g
{ 0, 0, 0, 0, 0, 0, 0, 0, 0},//t
};
printf("The max flow from node %d to sink node %d is %d",
s,t,edmonds_karp(graph,0,8));
}
Output:
Comparsion:
28 | P a g e
[Document title]
29 | P a g e
[Document title]
30 | P a g e
[Document title]
31 | P a g e
[Document title]
32 | P a g e
[Document title]
for(int v=0;v<N;v++)
{
if(visited[v]==false && rgraph[u][v]>0)
{
q.push(v);
parent[v]=u;
visited[v]=true;
}
}
}
return visited[t]==true;
}
int edmondKarp(int G[N][N],int s,int t)
{
int u,v;
int rgraph[N][N];
for(int u=0;u<N;u++)
{
for(int v=0;v<N;v++)
{
33 | P a g e
[Document title]
rgraph[u][v]=G[u][v];
}
}
int parent[N];
int maxflow=0;
while(bfs(rgraph,s,t,parent))
{
int pathflow=INT_MAX;
for(v=t;v!=s;v=parent[v])
{
u=parent[v];
pathflow=min(pathflow,rgraph[u][v]);
}
for(v=t;v!=s;v=parent[v])
{
u=parent[v];
rgraph[u][v]-=pathflow;
rgraph[v][u]+=pathflow;
}
maxflow+=pathflow;
34 | P a g e
[Document title]
}
return maxflow;
}
bool DFS(int rGraph[N][N],int parent[],bool visited[],int
n,int x,int t)
{
if(x == t)
return true;
visited[x] = true;
for(int i=0;i<n;++i)
{
if(rGraph[x][i]>0 && !visited[i])
{
parent[i]=x;
if(DFS(rGraph, parent, visited, n, i, t))
return true;
}
}
return false;
}
35 | P a g e
[Document title]
36 | P a g e
[Document title]
u=parent[v];
pathflow=min(pathflow,rgraph[u][v]);
}
for(v=t;v!=s;v=parent[v])
{
u=parent[v];
rgraph[u][v]-=pathflow;
rgraph[v][u]+=pathflow;
}
maxflow+=pathflow;
}
return maxflow;
}
int main()
{
int G1[9][9]={{0,26,17,0,37,0,0,0,0},
{0,0,0,20,10,0,0,0,0},
{0,0,0,0,20,15,0,0,},
{0,0,0,0,0,10,22,18,0},
{0,0,0,35,0,15,0,0,0},
37 | P a g e
[Document title]
{0,0,0,0,0,0,0,34,0},
{0,0,0,0,0,0,0,0,48},
{0,0,0,0,0,0,20,0,30},
{0,0,0,0,0,0,0,0,0}};
int G2[9][9]={{0,5,0,5,0,0,0,0,0},
{0,0,3,0,3,0,0,0,0},
{0,0,0,0,0,4,0,0,0},
{0,0,2,0,0,4,0,0,0},
{0,0,0,0,0,0,6,0,0},
{0,0,0,0,0,0,6,0,0},
{0,0,0,0,0,0,0,5,0},
{0,0,0,0,0,0,0,3,3},
{0,0,0,0,0,0,0,0,5}};
int G3[9][9]={{0,9,1,0,10,0,0,0,6},
{0,0,5,3,4,0,0,7,0},
{0,0,0,10,0,0,0,0,0},
{0,4,7,0,5,0,0,0,0},
{0,4,0,10,0,0,0,0,7},
{0,0,5,0,0,0,9,1,0},
{0,1,0,4,0,0,0,5,0},
38 | P a g e
[Document title]
{0,1,6,1,5,0,0,0,5},
{0,0,0,0,0,0,0,0,0}};
cout<<"Ford Fulkerson ";
cout<<fordFulkerson(G1,9,0,8)<<"\t"<<fordFulkerson(G2,9,
0,8)<<"\t"<<fordFulkerson(G3,9,0,8);
cout<<"\nEdmond Karp"<<"\t";
cout<<edmondKarp(G1,0,8)<<"\t"<<edmondKarp(G2,0,8)<<
"\t"<<edmondKarp(G3,0,8);
}
39 | P a g e