forked from srabin1/CPGC-Paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dinics_tri.c
311 lines (189 loc) · 6.66 KB
/
dinics_tri.c
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*This code is the implementation of Dinic's algorithm for tripartite graph*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include<time.h>
int* leftPartitionSize;
int* middlePartitionSize;
int* rightPartitionSize;
int* edges;
double run_time, total_run_time;
int maximumFlow;
char* filename[100];
int total_nodes;
struct timespec readingmtx, begin, end;
struct Edge {
int v;
int flow;
int C;
int rev;
};
struct Graph {
int V;
int* level;
struct Edge** adj;
int* edgeCount;
};
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->adj = (struct Edge**)malloc(V * sizeof(struct Edge*));
for (int i = 0; i < V; i++) {
graph->adj[i] = (struct Edge*)malloc(V * sizeof(struct Edge));
}
graph->level = (int*)malloc(V * sizeof(int));
graph->edgeCount = (int*)calloc(V, sizeof(int));
return graph;
}
struct Graph* graph;
void addEdge(struct Graph* graph, int u, int v, int C) {
graph->adj[u][graph->edgeCount[u]] = (struct Edge){ v, 0, C, graph->edgeCount[v] };
graph->adj[v][graph->edgeCount[v]] = (struct Edge){ u, 0, 0, graph->edgeCount[u] };
graph->edgeCount[u]++;
graph->edgeCount[v]++;
}
bool BFS(struct Graph* graph, int s, int t) {
for (int i = 0; i < graph->V; i++)
graph->level[i] = -1;
graph->level[s] = 0;
int* q = (int*)malloc(graph->V * sizeof(int));
int front = 0, rear = 0;
q[rear++] = s;
while (front < rear) {
int u = q[front++];
for (int i = 0; i < graph->edgeCount[u]; i++) {
struct Edge e = graph->adj[u][i];
if (graph->level[e.v] < 0 && e.flow < e.C) {
graph->level[e.v] = graph->level[u] + 1;
q[rear++] = e.v;
}
}
}
free(q);
return graph->level[t] < 0 ? false : true;
}
int sendFlow(struct Graph* graph, int u, int flow, int t, int start[]) {
if (u == t) {
return flow;
}
for (; start[u] < graph->edgeCount[u]; start[u]++) {
struct Edge* e = &graph->adj[u][start[u]];
if (graph->level[e->v] == graph->level[u] + 1 && e->flow < e->C) {
int curr_flow = flow < e->C - e->flow ? flow : e->C - e->flow;
int temp_flow = sendFlow(graph, e->v, curr_flow, t, start);
if (temp_flow > 0) {
e->flow += temp_flow;
graph->adj[e->v][e->rev].flow -= temp_flow;
return temp_flow;
}
}
}
return 0;
}
int DinicMaxflow(struct Graph* graph, int s, int t) {
if (s == t) {
return -1;
}
int total = 0;
while (BFS(graph, s, t) == true) {
int* start = (int*)malloc(graph->V * sizeof(int));
for (int i = 0; i < graph->V; i++)
start[i] = 0;
int flow;
while ((flow = sendFlow(graph, s, INT_MAX, t, start)) > 0) {
total += flow;
}
free(start);
}
return total;
}
void readMatrixMarketFile() {
FILE* file = fopen(filename, "r");
// Check if the file could be opened
if (file == NULL) {
printf("Failed to open the file.\n");
exit(1);
}
// Read the header information
char line[256];
fgets(line, sizeof(line), file);
if (strncmp(line, "%%MatrixMarket matrix coordinate", 31) != 0) {
printf("Invalid Matrix Market file.\n");
exit(1);
}
fgets(line, sizeof(line), file);
while (line[0] == '%') {
fgets(line, sizeof(line), file);
}
// Parse the matrix size and number of non-zero values
sscanf(line, "%d %d %d %d\n", &leftPartitionSize, &middlePartitionSize, &rightPartitionSize, &edges);
total_nodes = (int)leftPartitionSize + (int)rightPartitionSize + (int)middlePartitionSize + 2;
//printf("total_nodes: %d\n", total_nodes);
graph = createGraph(total_nodes);
int i;
for (i = 0; i < edges; i++) {
int row, col;
fscanf(file, "%d %d \n", &row, &col);
//addEdge(graph, row, col + (int)leftPartitionSize + (int)middlePartitionSize, 1);
if (col > (int) rightPartitionSize)
addEdge(graph, row, col, 1);
else
addEdge(graph, row, col + (int)leftPartitionSize + (int)middlePartitionSize, 1);
if (i < (int)leftPartitionSize) {
//printf("%d %d \n", 0, i+1 );
addEdge(graph, 0, i + 1, 1);
}
//printf("%d %d \n", row, col + (int)leftPartitionSize);
if (i < (int)rightPartitionSize) {
//printf("%d %d \n", i + 1 + (int)leftPartitionSize, total_nodes -1);
addEdge(graph, i + 1 + (int)leftPartitionSize + (int)middlePartitionSize, total_nodes - 1, 1);
}
//addEdge(graph, row, col, 1);
// //adjMatrix[row][col] = 1;
// //matrix.row_indices[i] = row; // Convert to 0-based indexing
// //matrix.col_indices[i] = col; // Convert to 0-based indexing
// //matrix.values[i] = 1;
}
fclose(file);
//return matrix;
}
int main(int argc, char* argv[]) {
int nodes;
int density;
int exp;
float delta;
if (argc > 1) {
nodes = atoi(argv[1]);
density = atoi(argv[2]);
exp = atoi(argv[3]);
delta = atof(argv[4]);
char *algo = argv[5];
sprintf(filename, "datasets/%s_tripartite_graph_%d_%d_%d_%d.mtx", algo, nodes, density, exp, (int)(delta*100));
//sprintf(filename, "data3_1/tripartite_graph_%d_%d_%d.mtx", nodes, density, exp);
}
clock_gettime(CLOCK_REALTIME, &readingmtx);
readMatrixMarketFile();
clock_gettime(CLOCK_REALTIME, &begin);
maximumFlow = DinicMaxflow(graph, 0, total_nodes - 1);
//printf("Maximum flow: %d\n", DinicMaxflow(graph, 0, total_nodes - 1));
clock_gettime(CLOCK_REALTIME, &end);
long seconds = end.tv_sec - begin.tv_sec;
long nanoseconds = end.tv_nsec - begin.tv_nsec;
run_time = (seconds + nanoseconds * 1e-9) * 1000;
long seconds2 = end.tv_sec - readingmtx.tv_sec;
long nanoseconds2 = end.tv_nsec - readingmtx.tv_nsec;
total_run_time = (seconds2 + nanoseconds2 * 1e-9) * 1000;
printf("%d, %d, %d, %d, %f, %d, %f, %f\n", nodes, total_nodes, density, exp, delta, maximumFlow, run_time, total_run_time);
//printf("Execution Time: %f ms\nTotal execution time: %f ms\n", run_time, total_run_time);
// Free dynamically allocated memory
for (int i = 0; i < graph->V; i++) {
free(graph->adj[i]);
}
free(graph->adj);
free(graph->level);
free(graph->edgeCount);
free(graph);
return 0;
}