-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.c
92 lines (77 loc) · 1.69 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
#include <stdio.h>
#include <stdlib.h>
struct node {
int visited;
int label;
struct node *next;
};
struct node vertices[10];
void graphify(int from, int to) {
struct node *vertex = (struct node *)malloc(sizeof(struct node));
struct node *temp = vertices[from].next;
if(vertices[from].label == -1) { // this is a new vertex
vertices[from].label = from;
vertices[from].visited = 0;
}
vertex->visited = 0; // this node is not yet visited
vertex->label = to;
// enqueue the 'to' node
while(temp != NULL) {
if(temp->next == NULL)
break;
else temp = temp->next;
}
if(temp == NULL)
vertices[from].next = vertex;
else temp->next = vertex;
vertex->next = NULL;
}
void printAdjencyList() {
int i = 0;
struct node *temp;
for(i = 0; i < 10; i++) {
printf("%d", vertices[i].label);
if(vertices[i].next != NULL) {
temp = vertices[i].next;
while(temp != NULL) {
printf("->%d", temp->label);
temp = temp->next;
}
}
printf("\n");
}
}
void depthFirst(int vertex) {
struct node *temp;
if(vertices[vertex].visited == 0) {
// this vertex is not yet visited, print it and go to the next vertex
printf("%d ", vertices[vertex].label);
vertices[vertex].visited = 1;
if(vertices[vertex].next != NULL) {
temp = vertices[vertex].next;
while(temp != NULL) {
depthFirst(temp->label);
temp = temp->next;
}
}
}
}
int main() {
int i = 0;
for(i = 0; i < 10; i++) {
vertices[i].visited = 0;
vertices[i].label = -1;
vertices[i].next = NULL;
}
graphify(0, 1);
graphify(0, 2);
graphify(2, 4);
graphify(1, 3);
graphify(1, 4);
graphify(3, 4);
graphify(3, 5);
graphify(4, 5);
depthFirst(0);
printf("\n");
printAdjencyList();
}