-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.cpp
More file actions
209 lines (202 loc) · 3.34 KB
/
Project.cpp
File metadata and controls
209 lines (202 loc) · 3.34 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <string>
#include <climits>
#include <queue>
#include <cmath>
#include <cstring>
#define Max_V 100
using namespace std;
class Queue
{
private:
pair<int,int> *arr;
int front;
int rear;
int size;
public:
Queue(int size)
{
this->size=size;
arr=new pair<int,int>[size];
front=0;
rear=0;
}
~Queue()
{
delete[] arr;
}
bool empty()
{
return front==rear;
}
void push(int a,int b)
{
if(rear<size)
{
arr[rear++]=make_pair(a,b);
}
else
{
cout<<"Queue is Full"<<endl;
}
}
void pop()
{
if(!empty())
{
front++;
}
else
{
cout<<"Queue is Empty"<<endl;
}
}
int frontFirst()
{
if(!empty())
{
return arr[front].first;
}
else
{
cout<<"Queue is Empty"<<endl;
return -1;
}
}
int frontSecond()
{
if(!empty())
{
return arr[front].second;
}
else
{
cout<<"Queue is Empty"<<endl;
return -1;
}
}
};
class Graph
{
private:
int V;
int capacity[Max_V][Max_V];
int flow[Max_V][Max_V];
int adj[Max_V][Max_V];
int adjCount[Max_V];
public:
Graph(int V)
{
this->V=V;
memset(capacity,0,sizeof(capacity));
memset(flow,0,sizeof(flow));
memset(adj,0,sizeof(adj));
memset(adjCount,0,sizeof(adjCount));
}
void addEdge(int u,int v,int capacity)
{
this->capacity[u][v]=capacity;
//this->flow[u][v]=flow;
//this->flow[v][u]=-flow;
adj[u][adjCount[u]++]=v;
adj[v][adjCount[v]++]=u;
}
int Bfs(int s,int t,int parent[])
{
memset(parent,-1,sizeof(int)*Max_V);
parent[s]=-2;
queue <pair<int,int>> q;
q.push({s,INT_MAX});
while(!q.empty())
{
int u=q.front().first;
int flow_in_path=q.front().second;
q.pop();
for(int i=0;i<adjCount[u];i++)
{
int v=adj[u][i];
int residual_capacity=capacity[u][v]-flow[u][v];
if(parent[v]==-1 && residual_capacity>0)
{
parent[v]=u;
int new_flow=min(flow_in_path,residual_capacity);
if(v==t)
{
return new_flow;
}
q.push({v,new_flow});
}
}
}
return 0;
}
void Dfs(int u ,bool visited[])
{
visited[u]=true;
for(int i=0;i<adjCount[u];i++)
{
int v=adj[u][i];
if(!visited[v] && capacity[u][v]-flow[u][v]>0)
{
Dfs(v,visited);
}
}
}
int maxFlow(int s,int t)
{
int total_flow=0;
int parent[Max_V];
int flow_in_path;
while((flow_in_path=Bfs(s,t,parent)))
{
total_flow+=flow_in_path;
int v=t;
while(v!=s)
{
int u=parent[v];
flow[u][v]+=flow_in_path;
flow[v][u]-=flow_in_path;
v=u;
}
}
return total_flow;
}
void minCut(int s)
{
bool visited[Max_V];
memset(visited,false,sizeof(visited));
Dfs(s,visited);
for(int u=0;u<V;u++)
{
if(visited[u])
{
for(int i=0;i<adjCount[u];i++)
{
int v=adj[u][i];
if(!visited[v] && capacity[u][v] >0)
{
cout<<u<<" - "<<v<<" is the min cut"<<endl;
}
}
}
}
}
};
int main()
{
//3 test cases need to be implemented 2 have been implemented one opposite direction remains;
Graph g(10);
g.addEdge(0, 1, 3);
g.addEdge(0, 3, 4);
g.addEdge(0, 2, 2);
g.addEdge(1, 4, 5);
g.addEdge(2, 5, 4);
g.addEdge(3, 7, 2);
g.addEdge(5, 4, 4);
g.addEdge(5, 8, 1);
g.addEdge(7, 8, 3);
g.addEdge(4, 9, 3);
g.addEdge(8, 9, 5);
cout << "Max Flow: " << g.maxFlow(0, 9) << endl;
g.minCut(0);
}