-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS_DFS.c
212 lines (182 loc) · 5.27 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* ---------------PROBLEM STATEMENT ---------------
Implement graph traversal (DFS & BFS)
*/
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Graph
{
int numVertices;
struct Node **adjacencyList;
};
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 numVertices)
{
struct Graph *graph = (struct Graph *)malloc(sizeof(struct Graph));
graph->numVertices = numVertices;
graph->adjacencyList = (struct Node **)malloc(numVertices * sizeof(struct Node *));
// Initialize each adjacency list as empty
for (int i = 0; i < numVertices; ++i)
{
graph->adjacencyList[i] = NULL;
}
return graph;
}
// add edge to src node to dest node.
void addEdge(struct Graph *graph, int src, int dest)
{
// Add an edge from src to dest
struct Node *newNode = createNode(dest);
newNode->next = graph->adjacencyList[src];
graph->adjacencyList[src] = newNode;
// Since the graph is undirected, add an edge from dest to src as well
newNode = createNode(src);
newNode->next = graph->adjacencyList[dest];
graph->adjacencyList[dest] = newNode;
}
void DFS(struct Graph *graph, int vertex, int *visited)
{
visited[vertex] = 1;
printf("%d ", vertex);
struct Node *temp = graph->adjacencyList[vertex];
while (temp != NULL)
{
int adjVertex = temp->data;
if (!visited[adjVertex])
{
DFS(graph, adjVertex, visited);
}
temp = temp->next;
}
}
void BFS(struct Graph *graph, int startVertex)
{
int *visited = (int *)malloc(graph->numVertices * sizeof(int));
for (int i = 0; i < graph->numVertices; ++i)
{
visited[i] = 0;
}
int queue[graph->numVertices];
int front = -1, rear = -1;
visited[startVertex] = 1;
printf("%d ", startVertex);
queue[++rear] = startVertex;
while (front != rear)
{
int currentVertex = queue[++front];
struct Node *temp = graph->adjacencyList[currentVertex];
while (temp != NULL)
{
int adjVertex = temp->data;
if (!visited[adjVertex])
{
visited[adjVertex] = 1;
printf("%d ", adjVertex);
queue[++rear] = adjVertex;
}
temp = temp->next;
}
}
free(visited);
}
void mainMenu()
{
int choice;
char c,ne;
int n;
int s,d;
while (1)
{
printf("\n************* MAIN MENU ****************\n");
printf("\nEnter number of vertices you want to insert: ");
scanf("%d", &n);
struct Graph *graph = createGraph(n);
int visitedDFS[n];
// initializing the visited graph with 0
for(int i = 0; i < n;i++){
visitedDFS[i] = 0;
}
Renter:
printf("Inserting Edge from source to destination\nNOTE: Give source and destination vertices as integers\n");
while(1){
printf("Enter src node : ");
scanf("%d", &s);
printf("Enter dest node : ");
scanf("%d", &d);
// adding new edge from src to dest
addEdge(graph, s, d);
printf("Do you want to add more edges(y/n): ");
scanf(" %c", &ne);
if(ne == 'n' || ne != 'y'){
break;
}
}
while(1){
printf("\n****** Choose any of the following Algorithms ******\n");
printf("1. Depth-first search (DFS)\n2. Breadth-first search (BFS)\n3. Add more edges\n4. Go to main menu\n5. Exit\n");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Depth-First Search (DFS): ");
DFS(graph, 0, visitedDFS);
printf("\n");
break;
case 2:
printf("Breadth-First Search (BFS): ");
BFS(graph, 0);
printf("\n");
break;
case 3:
goto Renter;
break;
case 4:
mainMenu();
break;
case 5:
printf("\nAre you sure you want to exit program? (y/n) : ");
scanf(" %c", &c);
if (c == 'y')
exit(0);
else if (c != 'n')
printf("\nInvalid Input!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
}
}
int main()
{
mainMenu();
// Create a graph with 5 vertices
// struct Graph* graph = createGraph(7);
// // Add edges to the graph
// addEdge(graph, 0, 1);
// addEdge(graph, 0, 2);
// addEdge(graph, 1, 3);
// addEdge(graph, 1, 4);
// addEdge(graph, 2, 4);
// addEdge(graph, 3, 9);
// addEdge(graph, 3, 6);
// printf("Depth-First Search (DFS): ");
// int visitedDFS[7] = {0};
// DFS(graph, 0, visitedDFS);
// printf("\n");
// printf("Breadth-First Search (BFS): ");
// BFS(graph, 0);
// printf("\n");
return 0;
}