-
Notifications
You must be signed in to change notification settings - Fork 368
/
BreadthFirstSearch.c
101 lines (80 loc) · 2.47 KB
/
BreadthFirstSearch.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
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
struct Node {
int data;
struct Node* next;
};
struct Graph {
int V;
struct Node** adjList;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->adjList = (struct Node**)malloc(V * sizeof(struct Node*));
for (int i = 0; i < V; ++i)
graph->adjList[i] = NULL;
return graph;
}
void addEdge(struct Graph* graph, int src, int dest) {
struct Node* newNode = createNode(dest);
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjList[dest];
graph->adjList[dest] = newNode;
}
// BFS traversal from a given source node 's'
void BFS(struct Graph* graph, int startVertex) {
int visited[MAX_VERTICES] = {0};
// Create a queue for BFS traversal
struct Node* queue = createNode(startVertex);
// Mark the current node as visited and add into queue
visited[startVertex] = 1;
printf("BFS traversal starting from vertex %d: ", startVertex);
while (queue != NULL) {
int currentVertex = queue->data;
// Remove a vertex from queue and print it
printf("%d ", currentVertex);
struct Node* temp = queue;
queue = queue->next;
free(temp);
// Get all adjacent vertices of the dequeued vertex s.
// If an adjacent has not been visited, then mark it visited and enqueue it
struct Node* adj = graph->adjList[currentVertex];
while (adj != NULL) {
int adjVertex = adj->data;
if (visited[adjVertex] == 0) {
struct Node* newNode = createNode(adjVertex);
newNode->next = queue;
queue = newNode;
visited[adjVertex] = 1;
}
adj = adj->next;
}
}
}
int main() {
int N, M, i;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &N, &M);
struct Graph* graph = createGraph(N);
for (i = 1; i <= M; i++) {
int u, v;
printf("Enter edge No. %d: ", i);
scanf("%d %d", &u, &v);
addEdge(graph, u, v);
}
int s;
printf("Enter source vertex: ");
scanf("%d", &s);
BFS(graph, s);
return 0;
}