1 #include "sources/connexity.h"
2 #include <cgds/Stack.h>
4 #include "sources/utils/boolean.h"
6 int* getConnectedComponents_core(int** NIix
, int* lengthNIix
, int n
)
8 int* cc
= (int*)calloc(n
,sizeof(int));
9 Stack
* toBeExplored
= stack_new(int);
10 int curInd
= 0, nextInd
;
11 bool* alreadyExpanded
= (bool*)calloc(n
,sizeof(bool));
13 // while the entire graph hasn't been visited
18 stack_push(toBeExplored
, curInd
);
20 // while new elements are discovered in current component,
21 // mark them as expanded and stack their neighbors
22 while (!stack_empty(toBeExplored
))
24 stack_top(toBeExplored
, nextInd
);
25 stack_pop(toBeExplored
);
28 for (int j
=0; j
<lengthNIix
[nextInd
]; j
++)
30 if (!alreadyExpanded
[NIix
[nextInd
][j
]])
31 stack_push(toBeExplored
, NIix
[nextInd
][j
]);
33 alreadyExpanded
[nextInd
] = S_TRUE
;
36 // curInd is set to the next unexplored index (if possible)
37 for (int i
=0; i
<n
; i
++)
49 free(alreadyExpanded
);
50 stack_destroy(toBeExplored
);