-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraverse.c
241 lines (204 loc) · 5.53 KB
/
traverse.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*Implementation of BFS and DFS for each graph representations.***/
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
typedef enum { false, true } bool;
int adj[MAX][MAX]; // Adjacency matrix
bool visited[MAX]; // Visited nodes
int n; // Number of nodes in the graph
void create_graph();
void display();
void dfs(int v);
void dfs_rec(int v);
void bfs(int v);
void adj_nodes(int v);
void components();
int main() {
int choice, v;
create_graph();
while (1) {
printf("\n1. Adjacency matrix\n");
printf("2. Depth First Search using stack\n");
printf("3. Depth First Search through recursion\n");
printf("4. Breadth First Search\n");
printf("5. Adjacent vertices\n");
printf("6. Components\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Adjacency Matrix\n");
display();
break;
case 2:
printf("Enter starting node for Depth First Search: ");
scanf("%d", &v);
for (int i = 1; i <= n; i++)
visited[i] = false;
dfs(v);
break;
case 3:
printf("Enter starting node for Depth First Search: ");
scanf("%d", &v);
for (int i = 1; i <= n; i++)
visited[i] = false;
dfs_rec(v);
break;
case 4:
printf("Enter starting node for Breadth First Search: ");
scanf("%d", &v);
for (int i = 1; i <= n; i++)
visited[i] = false;
bfs(v);
break;
case 5:
printf("Enter node to find adjacent vertices: ");
scanf("%d", &v);
printf("Adjacent Vertices are: ");
adj_nodes(v);
break;
case 6:
components();
break;
case 7:
exit(0);
default:
printf("Wrong choice\n");
break;
}
}
}
void create_graph() {
int origin, destin;
printf("Enter number of nodes: ");
scanf("%d", &n);
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
adj[i][j] = 0; // Initialize adjacency matrix
}
}
printf("Enter edges (0 0 to quit):\n");
while (1) {
printf("Enter edge (origin destination): ");
scanf("%d %d", &origin, &destin);
if (origin == 0 && destin == 0) break;
if (origin > n || destin > n || origin <= 0 || destin <= 0) {
printf("Invalid edge!\n");
} else {
adj[origin][destin] = 1;
// Uncomment the next line if you want to create an undirected graph
// adj[destin][origin] = 1;
}
}
}
void display() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%4d", adj[i][j]);
}
printf("\n");
}
}
void dfs_rec(int v) {
visited[v] = true;
printf("%d ", v);
for (int i = 1; i <= n; i++) {
if (adj[v][i] == 1 && !visited[i]) {
dfs_rec(i);
}
}
}
void dfs(int v) {
int stack[MAX], top = -1;
top++;
stack[top] = v;
while (top >= 0) {
int pop_v = stack[top--]; // Pop from stack
if (!visited[pop_v]) {
printf("%d ", pop_v);
visited[pop_v] = true;
}
for (int i = n; i >= 1; i--) {
if (adj[pop_v][i] == 1 && !visited[i]) {
stack[++top] = i; // Push unvisited neighbors
}
}
}
}
void bfs(int v) {
int queue[MAX], front = -1, rear = -1;
queue[++rear] = v;
visited[v] = true;
printf("%d ", v);
while (front < rear) {
v = queue[++front]; // Dequeue
for (int i = 1; i <= n; i++) {
if (adj[v][i] == 1 && !visited[i]) {
printf("%d ", i);
visited[i] = true;
queue[++rear] = i; // Enqueue
}
}
}
}
void adj_nodes(int v) {
printf("Adjacent Vertices: ");
for (int i = 1; i <= n; i++) {
if (adj[v][i] == 1) {
printf("%d ", i);
}
}
printf("\n");
}
void components() {
printf("Components:\n");
for (int i = 1; i <= n; i++)
visited[i] = false;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
dfs_rec(i);
printf("\n");
}
}
}
//SAMPLE OUTPUT
/*
Enter number of nodes: 5
Enter edge 1 (0 0 to quit): 1 2
Enter edge 2 (0 0 to quit): 1 3
Enter edge 3 (0 0 to quit): 2 4
Enter edge 4 (0 0 to quit): 3 5
Enter edge 5 (0 0 to quit): 0 0
1. Adjacency matrix
2. Depth First Search using stack
3. Depth First Search through recursion
4. Breadth First Search
5. Adjacent vertices
6. Components
7. Exit
Enter your choice: 1
Adjacency Matrix
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 1 1 0 0
2 0 1 0 0 1 0
3 0 1 0 0 0 1
4 0 0 0 0 0 0
5 0 0 0 0 0 0
Enter your choice: 2
Enter starting node for Depth First Search: 1
1 3 5 2 4
Enter your choice: 3
Enter starting node for Depth First Search: 1
1 2 4 3 5
Enter your choice: 4
Enter starting node for Breadth First Search: 1
1 2 3 4 5
Enter your choice: 5
Enter node to find adjacent vertices: 2
Adjacent Vertices are: 1 4
Enter your choice: 6
1 2 3 4 5
Enter your choice: 7
*/