-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_DFS.c
149 lines (127 loc) · 3.23 KB
/
BFS_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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define V (9)
#define INF (0x0FFFFFFF)
struct node
{
int vertex;
struct node *next;
};
typedef struct
{
struct node *head;
struct node *tail;
int count;
}queue_t;
queue_t q = {NULL, NULL, 0};
void add_to_queue(struct node *node)
{
if (q.head == NULL)
{
q.head = node;
}
else
{
q.tail->next = node;
}
q.tail = node;
node->next = NULL;
q.count++;
}
void add_to_head(struct node *node)
{
if (q.tail == NULL)
{
q.tail = node;
}
node->next = q.head;
q.head = node;
q.count++;
}
struct node* pop_head_node()
{
if (q.head == NULL)
{
return NULL;
}
struct node *node = q.head;
q.head = q.head->next;
q.count--;
return node;
}
void BFS(int graph[V][V], int start_vertex)
{
// setup tracking array.
bool visited[V];
for (int i = 0; i < V; i++)
{
visited[i] = false;
}
struct node *start_node = (struct node*)malloc(sizeof(struct node));
start_node->vertex = start_vertex;
add_to_queue(start_node);
visited[start_vertex] = true;
struct node *cur_node;
while ((cur_node = pop_head_node()) != NULL)
{
printf("BFS - Visiting vertex %u\n",cur_node->vertex);
for (int i =0; i < V; i++)
{
if (!visited[i] && graph[cur_node->vertex][i])
{
// Haven't been to this vertex yet & vertices are connected.
struct node *node = (struct node*)malloc(sizeof(struct node));
node->vertex = i;
add_to_queue(node);
visited[i] = true;
}
}
free(cur_node);
}
}
void DFS(int graph[V][V], int start_vertex)
{
// setup tracking array.
bool visited[V];
for (int i = 0; i < V; i++)
{
visited[i] = false;
}
struct node *start_node = (struct node*)malloc(sizeof(struct node));
start_node->vertex = start_vertex;
add_to_head(start_node);
visited[start_vertex] = true;
struct node *cur_node;
while ((cur_node = pop_head_node()) != NULL)
{
printf("DFS - Visiting vertex %u\n",cur_node->vertex);
for (int i =0; i < V; i++)
{
if (!visited[i] && graph[cur_node->vertex][i])
{
// Haven't been to this vertex yet & vertices are connected.
struct node *node = (struct node*)malloc(sizeof(struct node));
node->vertex = i;
add_to_head(node);
visited[i] = true;
}
}
free(cur_node);
}
}
int main()
{
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
BFS(graph, 0);
DFS(graph, 0);
return 0;
}