-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadth_first_search.c
139 lines (118 loc) · 2.61 KB
/
breadth_first_search.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
#include <stdio.h>
#include <stdlib.h>
#define EDGE_EXIST 1
#define EXPLORING 2
#define VISITED 1
#define NOT_VISITED 0
// Structure for each node.
typedef struct node
{
int data;
struct node *link;
} queue;
// Function to check QUEUE is empty.
int is_empty(queue **front)
{
if (NULL == (*front))
return 1;
return 0;
}
// Function for enqueue operation.
void enqueue(int item, queue **front, queue **rear)
{
queue *new_node = (queue *)malloc(sizeof(queue));
if (NULL == new_node)
{
printf("\n\tOVERFLOW!\n");
exit(-1);
}
new_node->data = item;
new_node->link = NULL;
if (is_empty(front))
*front = *rear = new_node;
else
{
(*rear)->link = new_node;
(*rear) = new_node;
}
}
// Function for dequeue operation.
int dequeue(queue **front, queue **rear)
{
if (is_empty(front))
{
printf("\n\tUNDERFLOW!");
return -1;
}
int item = (*front)->data;
queue *del = *front;
if (*front == *rear)
*front = *rear = NULL;
else
*front = (*front)->link;
del->link = NULL;
free(del);
return item;
}
// BFS Algorithm.
void bfs(int **graph, int vertices, int vertex, int *visited)
{
queue *front = NULL, *rear = NULL;
// Starting node.
enqueue(vertex, &front, &rear);
visited[vertex] = EXPLORING;
while (!is_empty(&front))
{
// Dequeue node from the queue & change to visited.
vertex = dequeue(&front, &rear);
visited[vertex] = VISITED;
printf("\n\t VISITING NODE : %d ", vertex + 1);
for (int adj_vertex = 0; adj_vertex < vertices; adj_vertex++)
// Exploring all the adjacent nodes.
if (EDGE_EXIST == graph[vertex][adj_vertex] && NOT_VISITED == visited[adj_vertex])
{
// Enqueue the unvisited nodes & change to exploring.
enqueue(adj_vertex, &front, &rear);
visited[adj_vertex] = EXPLORING;
}
}
}
int main()
{
int vertices;
printf("\n TOTAL NUMBER OF NODES : ");
scanf("%d", &vertices);
// Allocating memeory.
int *visited = (int *)calloc(vertices, sizeof(int));
int **graph = (int **)malloc(vertices * sizeof(int *));
for (int i = 0; i < vertices; i++)
graph[i] = (int *)malloc(vertices * sizeof(int));
// Adjacency Matrix of given graph.
printf("\n Adjacency Matrix \n");
for (int i = 0; i < vertices; i++)
{
printf(" Node %d : ", i + 1);
for (int j = 0; j < vertices; j++)
scanf("%d", &graph[i][j]);
}
// BFS calling for components of graph.
printf("\n Breadth first search :\n");
for (int vertex = 0; vertex < vertices; vertex++)
if (NOT_VISITED == visited[vertex])
bfs(graph, vertices, vertex, visited);
printf("\n");
return 0;
}
/*
0 1 1 1 0
1 0 1 0 0
1 1 0 0 1
1 0 0 0 0
0 0 1 0 0
0 1 0 1 0 0
1 0 1 0 1 0
0 1 0 0 0 0
1 0 0 0 0 0
0 1 0 0 0 1
0 0 0 0 1 0
*/