-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoloringAlgorithm.c
More file actions
388 lines (341 loc) · 9.89 KB
/
coloringAlgorithm.c
File metadata and controls
388 lines (341 loc) · 9.89 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "./include/readFiles.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct queue {
int *items;
int front;
int rear;
};
struct queue* createQueue(int N) {
// printf("create\n");
struct queue* q = (struct queue*)malloc(sizeof(struct queue));
q->items = (int*)malloc(N * sizeof(int));
q->front=-1;
q->rear=-1;
return q;
}
int isEmpty(struct queue* q) {
return q->rear == -1 ? 1 : 0;
}
void enqueue(struct queue *q, int N, int v) {
if(q->rear == N - 1) {
printf("Queue is full!");
} else {
if(q->front == -1)
q->front++;
q->rear++;
q->items[q->rear] = v;
}
}
int dequeue(struct queue *q) {
int item;
if(isEmpty(q)) {
printf("Queue is empty!");
return 0;
} else {
item = q->items[q->front];
q->front++;
if(q->front > q->rear) {
// printf("Resseting queue\n");
q->front = q->rear = -1;
}
}
return item;
}
void printQueue(struct queue *q) {
if(isEmpty(q)) {
printf("Queue is empty!");
} else {
printf("\nQueue contains: ");
for(int i = q->front; i < q->rear+1; i++) {
printf("%d ", q->items[i]);
}
}
}
// void BFS(int s, bool visited[SIZE], int **graph) {
// struct queue *q = createQueue();
// visited[s] = true;
// enqueue(q, s);
// while(!isEmpty(q)) {
// printQueue(q);
// int currentVertex = dequeue(q);
// printf("\nVisited: %d", currentVertex);
// int *adjList = graph[currentVertex];
// for(int adjVertex = 0; adjVertex < SIZE; adjVertex++) {
// if(adjList[adjVertex] == 1 && visited[adjVertex] == false) {
// visited[adjVertex] = true;
// enqueue(q, adjVertex);
// }
// }
// }
// }
// int** calcTraspose(int **graph) {
// int **tGraph = malloc(SIZE * sizeof(int*));
// for(int i = 0; i < SIZE; i++) {
// tGraph[i] = malloc(SIZE * sizeof(int));
// }
// for(int i = 0; i < SIZE; i++) {
// for(int j = 0; j < SIZE; j++) {
// tGraph[i][j] = graph[j][i];
// tGraph[j][i] = graph[i][j];
// }
// }
// return tGraph;
// }
// Coloring Algorithm
int coloringAlgorithm(int **mtx_csr, int **mtx_csc, int N) {
struct queue *q = createQueue(N);
int *colors = (int*) malloc(N * sizeof(int));
int *visited = (int*) malloc(N * sizeof(int));
int *scc_ids = (int*)malloc(N * sizeof(int));
for(int i = 0; i < N; i++) {
enqueue(q, N, i);
scc_ids[i] = -1;
visited[i] = false;
}
// printQueue(q);
while(!isEmpty(q)){
// printf("\nCOLORS: ");
for(int i = q->front; i < q->rear+1; i++) {
int v = q->items[i];
colors[v] = v;
// visited[v] = false;
// printf("%d ", colors[v]);
}
// printf("\n");
bool color_changed = true;
while(color_changed) {
color_changed = false;
for(int i = 0; i < q->rear+1; i++) {
int v = q->items[i];
visited[v] = true;
// printf("v: %d\n", v);
// pass color to vertices v points
int dsc_start = mtx_csr[0][v];
int dsc_end = mtx_csr[0][v + 1];
// printf("start: %d end: %d\n", dsc_start, dsc_end);
for(int dsc_id = dsc_start; dsc_id < dsc_end; dsc_id++) {
int dsc = mtx_csr[1][dsc_id];
// printf("dsc: %d\n", dsc);
if(visited[dsc] == false) {
colors[dsc] = colors[v];
color_changed = true;
}
}
int pred_start = mtx_csc[1][v];
int pred_end = mtx_csc[1][v + 1];
for(int u_id = pred_start; u_id < pred_end; u_id++) {
int u = mtx_csc[0][u_id];
if(scc_ids[u] == -1 && colors[v] > colors[u]) {
colors[v] = colors[u];
color_changed = true;
}
}
// printf("COLORS: ");
// for(int i = q->front; i < q->rear+1; i++ ){
// printf("%d ", colors[q->items[i]]);
// }
// printf("\n");
}
}
for(int i = 0; i < q->rear+1; i++) {
int v = q->items[i];
if(colors[v] == v) {
int scc_color = colors[v];
struct queue *bfs_q = createQueue(N);
enqueue(bfs_q, N, v);
scc_ids[v] = scc_color;
while(!isEmpty(bfs_q)) {
// printQueue(bfs_q);
int curr_vertex = dequeue(bfs_q);
int pred_start = mtx_csc[1][curr_vertex];
int pred_end = mtx_csc[1][curr_vertex + 1];
for(int u_id = pred_start; u_id < pred_end; u_id++) {
int u = mtx_csc[0][u_id];
if(scc_ids[u] == -1 && colors[u] == scc_color) {
// printf("%d\n", u);
enqueue(bfs_q , N, u);
scc_ids[u] = scc_color;
}
}
// printQueue(bfs_q);
// printf("\n");
}
}
}
for(int i = 0; i < q->rear+1; i++) {
int v = q->items[i];
if(scc_ids[v] != -1) {
if(i == q->front) {
q->front++;
// printf("front: %d", q->front);
} else if(i == q->rear) {
q->rear--;
} else {
for(int j = i; j < q->rear; j++) {
// printf("v: %d\n", v);
q->items[j] = q->items[j+1];
}
q->rear--;
}
if(q->front > q->rear) {
// printf("Resseting queue\n");
q->front = q->rear = -1;
}
}
}
// printf("\n");;
}
// for(int i = 0; i < N; i++) {
// printf("%d ", scc_ids[i]);
// }
int cnt = 0;
int *scc_cnt = (int*)malloc(N * sizeof(int));
for(int i = 0; i < N; i++) {
scc_cnt[i] = 0;
}
for(int i = 0; i < N; i++) {
if(scc_cnt[scc_ids[i]] == 0) {
scc_cnt[scc_ids[i]]++;
cnt++;
} else {
scc_cnt[scc_ids[i]]++;
}
}
//******* TRIM na kanoume prin to proccess ******************/
// for(int i = 0; i < N; i++) {
// if(scc_cnt[scc_ids[i]] == 1) {
// cnt--;
// }
// }
// printf("%d ", scc_ids[i]);
free(scc_cnt);
free(scc_ids);
free(q->items);
free(q);
return cnt;
}
// #define nz 11
// #define M 7
// #define N 7
// int main()
// {
// MM_typecode matcode;
// int *I = (int*)malloc(nz * sizeof(int));
// int *J = (int*)malloc(nz * sizeof(int));
// I[0] = 5;
// I[1] = 0;
// I[2] = 1;
// I[3] = 3;
// I[4] = 5;
// I[5] = 2;
// I[6] = 1;
// I[7] = 6;
// I[8] = 1;
// I[9] = 3;
// I[10] = 4;
// J[0] = 0;
// J[1] = 1;
// J[2] = 2;
// J[3] = 2;
// J[4] = 2;
// J[5] = 3;
// J[6] = 4;
// J[7] = 4;
// J[8] = 5;
// J[9] = 6;
// J[10] = 6;
// FILE *fptr;
// fptr = fopen("./graph.mtx", "w+");
// if(fptr == NULL) {
// printf("Error!");
// exit(1);
// }
// mm_initialize_typecode(&matcode);
// mm_set_matrix(&matcode);
// mm_set_coordinate(&matcode);
// mm_set_real(&matcode);
// mm_write_banner(fptr, matcode);
// mm_write_mtx_crd_size(fptr, M, N, nz);
// /* NOTE: matrix market files use 1-based indices, i.e. first element
// of a vector has index 1, not 0. */
// for (int i=0; i<nz; i++)
// fprintf(fptr, "%d %d\n", I[i]+1, J[i]+1);
// fclose(fptr);
// return 0;
// }
int main(int argc, char *argv[]) {
MM_typecode matcode;
FILE *f;
int M, N, nz;
int i, *I, *J, *val;
int ret_code;
if (argc < 2)
{
fprintf(stderr, "Usage: %s [martix-market-filename]\n", argv[0]);
exit(1);
}
else
{
if ((f = fopen(argv[1], "r")) == NULL)
exit(1);
}
if (mm_read_banner(f, &matcode) != 0)
{
printf("Could not process Matrix Market banner.\n");
exit(1);
}
/* find out size of sparse matrix */
if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) != 0)
exit(1);
/* reserve memory for matrices */
// int nz = 11;
// int N = 7;
I = (int*)malloc(nz * sizeof(int));
J = (int*)malloc(nz * sizeof(int));
val = (int*)malloc(nz * sizeof(int));
for (int i = 0; i < nz; i++)
{
fscanf(f, "%d %d\n", &I[i], &J[i]); /* don't read values (or weights) */
I[i]--; /* adjust from 1-based to 0-based */
J[i]--; /* adjust from 1-based to 0-based */
}
if (f != stdin)
fclose(f);
// I[0] = 5;
// I[1] = 0;
// I[2] = 1;
// I[3] = 3;
// I[4] = 5;
// I[5] = 2;
// I[6] = 1;
// I[7] = 6;
// I[8] = 1;
// I[9] = 3;
// I[10] = 4;
// J[0] = 0;
// J[1] = 1;
// J[2] = 2;
// J[3] = 2;
// J[4] = 2;
// J[5] = 3;
// J[6] = 4;
// J[7] = 4;
// J[8] = 5;
// J[9] = 6;
// J[10] = 6;
int **mtx_csr = graph_csr((int*)I, (int*)J, nz, N);
int **mtx_csc = graph_csc((int*)I, (int*)J, nz, N);
int sccs = coloringAlgorithm(mtx_csr, mtx_csc, N);
printf("sccs: %d\n", sccs);
free(mtx_csr[1]);
free(mtx_csr[0]);
free(mtx_csc[1]);
free(mtx_csc[0]);
free(mtx_csc);
free(mtx_csr);
free(I);
free(J);
return 0;
}