-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.c
133 lines (98 loc) · 2.95 KB
/
DFS.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
#include "include/DFS.h"
Stack* createStack(int maxSize) {
assert((0 < maxSize) && "Couldn't create stack with negative size.");
Stack* stack = (Stack*)calloc(1, sizeof(Stack));
stack->currentSize = 0;
stack->maxSize = maxSize;
stack->elems = (int*)calloc(maxSize, sizeof(int));
return stack;
}
int pushStack(Stack* stack, int elemToAdd) {
assert((NULL != stack) && "Couldn't push to null-stack.");
if (stack->currentSize == stack->maxSize) {
fprintf(stderr, "%s\n", "Failed to push to stack: has reached maximum size.");
return -1;
}
stack->elems[stack->currentSize++] = elemToAdd;
return 1;
}
int isEmptyStack(Stack* stack) {
assert((NULL != stack) && "Couldn't check for emptiness null-stack.");
return (stack->currentSize > 0);
}
int popStack(Stack* stack) {
assert((NULL != stack) && "Couldn't pop from null-stack.");
if (isEmptyStack(stack))
stack->currentSize--;
return stack->elems[stack->currentSize];
fprintf(stderr, "%s\n", "Couldn't pop from empty stack.");
return -1;
}
int destructStack(Stack* stack) {
if (NULL == stack) {
fprintf(stderr, "%s\n", "Attempt at destroing null-stack.");
return -1;
}
free(stack->elems);
stack->currentSize = 0;
stack->maxSize = 0;
free(stack);
return 1;
}
void DFS(Graph* graph, int currentNode) {
assert((NULL != graph) && "Couldn't DFS null-graph");
printf("Processing %d node\n", currentNode);
graph->nodes[currentNode].color = GREY;
for (int i = 0; i < graph->size; ++i) {
if (i == currentNode) continue;
if (graph->adjacencyMatrix->matrix[currentNode][i] == 1) {
switch (graph->nodes[i].color) {
case WHITE:
DFS(graph, i);
break;
case GREY:
printf("%s Backedge is: %d --> %d\n", "Cycle detected!", currentNode, i);
break;
case BLACK:
printf("Node %d has been already processed. Crossedge is %d --> %d\n", i, i, currentNode);
break;
default:
printf("%s\n", "Unknown node color!");
break;
}
}
}
graph->nodes[currentNode].color = BLACK;
}
void stackDFS(Graph* graph, int startingNode) {
assert((NULL != graph) && "Couldn't DFS null-graph");
printf("Starting DFS from %d node\n", startingNode);
Stack* stack = createStack(32);
pushStack(stack, startingNode);
while (isEmptyStack(stack)) {
int currentNode = popStack(stack);
printf("Processing %d node\n", currentNode);
graph->nodes[currentNode].color = GREY;
for (int i = 0; i < graph->size; ++i) {
if (i == currentNode) continue;
if (graph->adjacencyMatrix->matrix[currentNode][i] == 1) {
switch (graph->nodes[i].color) {
case WHITE:
pushStack(stack, i);
break;
case GREY:
printf("%s Backedge is: %d --> %d\n", "Cycle detected!", currentNode, i);
break;
case BLACK:
printf("%s %d %s\n", "Node", i ,"has been already processed.");
break;
default:
printf("%s\n", "Unknown node color!");
break;
}
}
graph->nodes[currentNode].color = BLACK;
}
}
destructStack(stack);
}