-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
252 lines (221 loc) · 4.75 KB
/
main.cpp
File metadata and controls
252 lines (221 loc) · 4.75 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <list>
#include <fstream>
using namespace std;
#define MAXIMUM_NODES 100
int no_of_vertices, no_of_edges;
int capacity_matrix[MAXIMUM_NODES][MAXIMUM_NODES];
int flow_matrix[MAXIMUM_NODES][MAXIMUM_NODES];
int vertices[MAXIMUM_NODES][3];
bool visited_vertices[MAXIMUM_NODES];
int topoSorted[MAXIMUM_NODES],topoCount = 0;
void initialise();
int incoming_flow(int);
int outgoing_flow(int);
void print_adj_list();
void print_adj_matrix();
void start_wave();
void increase_flow();
void decrease_flow();
void dfs(int );
void initialise()
{
for(int i=0;i<no_of_vertices;i++)
{
for(int j=0;j<no_of_vertices;j++)
{
capacity_matrix[i][j] = 0;
flow_matrix[i][j] = 0;
}
}
vertices[0][0] = 0; //blocked initially
vertices[0][1] = 0;
vertices[0][2] = 0;
for(int i=1;i<no_of_vertices;i++)
{
vertices[i][0] = 1; //unblocked initially
vertices[i][1] = 0;
vertices[i][2] = 0;
}
for(int i=0;i<no_of_vertices;i++)
visited_vertices[i] = false;
}
void print_adj_matrix()
{
cout<<"\nFlow matrix is:\n";
for(int i=0;i<no_of_vertices;i++)
{
for(int j=0;j<no_of_vertices;j++)
{
cout<<flow_matrix[i][j]<<" ";
}
cout<<endl;
}
}
void dfs(int u)
{
visited_vertices[u]=true;
for(int i=0;i<no_of_vertices;i++)
{
if((!visited_vertices[i]) && capacity_matrix[u][i] > 0)
{
dfs(i);
}
}
topoSorted[topoCount++]=u;
}
void print_adj_list()
{
cout<<"\nFlow diagram is:\n";
for(int i=0;i<no_of_vertices;i++)
{
for(int j=0;j<no_of_vertices;j++)
{
if(flow_matrix[i][j]!=0)
cout<<i<<" "<<j<<" "<<flow_matrix[i][j]<<endl;
}
}
}
void start_wave()
{
for(int j=0;j<no_of_vertices;j++)
{
flow_matrix[0][j] = capacity_matrix[0][j];
}
}
void increase_flow()
{
bool balanced;
int add_flow;
int fin,fout;
int i;
for(int r=0;r<no_of_vertices;r++)
{
i = topoSorted[no_of_vertices-r-1];
//scan topologically all vertices except s and t.
if (i == 0 || i == (no_of_vertices-1))
continue;
balanced = true;
fin = incoming_flow(i);
fout = outgoing_flow(i);
if(vertices[i][0] == 1 && fin != fout )
{
balanced =false;
//attempt to balance the node
for(int j=0;j<no_of_vertices > 0;j++)
{
//check if j is a neighbor
if(capacity_matrix[i][j] > 0)
{
//if the vertex j is unblocked
if( vertices[j][0] == 1 )
{
add_flow = min(capacity_matrix[i][j]-flow_matrix[i][j],fin - fout);
flow_matrix[i][j] = flow_matrix[i][j] + add_flow;
fout = fout + add_flow;
}
if(fin == fout)
{
balanced = true;
break;
}
}
}
}
//if unable to balance the vertex
if(balanced == false)
{
vertices[i][0] = 0;
}
}
//If there is blocked, unbalanced vertex other than s, call decrease flow_matrix
for(int i=1;i<no_of_vertices;i++)
{
if(vertices[i][0] == 0 && incoming_flow(i) != outgoing_flow(i) )
decrease_flow();
}
}
int incoming_flow(int vertex_id)
{
int fin = 0;
for(int i=0;i<no_of_vertices;i++)
{
fin = fin + flow_matrix[i][vertex_id];
}
return fin;
}
int outgoing_flow(int vertex_id)
{
int fout = 0;
for(int i=0;i<no_of_vertices;i++)
{
fout = fout + flow_matrix[vertex_id][i];
}
return fout;
}
void decrease_flow()
{
int subtract_flow;
int fin,fout;
int j;
for(int r=no_of_vertices-1;r>=0;r--)
{
j = topoSorted[no_of_vertices - r - 1];
//scan topologically all vertices except s and t.
if (j == 0 || j == (no_of_vertices-1))
continue;
fin = incoming_flow(j);
fout = outgoing_flow(j);
if(vertices[j][0] == 0 && fin != fout )
{
for(int i=0;i<no_of_vertices;i++)
{
//check if i and j are neighbor
if(capacity_matrix[i][j] > 0)
{
subtract_flow = min(flow_matrix[i][j],fin - fout);
flow_matrix[i][j] = flow_matrix[i][j] - subtract_flow;
fin = fin - subtract_flow;
}
}
}
}
//if there an unblocked, unbalanced vertex other than t call increase flow_matrix
for(int i=0;i<no_of_vertices-1;i++)
{
if(vertices[i][0] == 1 && incoming_flow(i) != outgoing_flow(i) )
increase_flow();
}
}
int main(int argc, char **argv)
{
int blocking_flow =0;
int no_of_lines;
//ifstream fin;
//fin.open("../testcases/tc0");
cin>>no_of_vertices>>no_of_edges;
int v,w,capacity;
initialise();
for(int i=0;i<no_of_edges;i++)
{
cin>>v>>w>>capacity;
capacity_matrix[v][w] = capacity;
}
//do the topological sorting
for(int i=0;i<no_of_vertices;i++)
if(!visited_vertices[i])
dfs(i);
//print the topo order
cout<<"Topoligical Order:\n";
for(int i=0;i<no_of_vertices;i++)
cout<<topoSorted[no_of_vertices -i - 1]<<" ";
start_wave();
increase_flow();
print_adj_list();
for(int j=0;j<no_of_vertices;j++)
{
blocking_flow = blocking_flow + flow_matrix[0][j];
}
cout<<"\nBlocking Flow is "<<blocking_flow<<endl;
//fin.close();
}