-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
51 lines (39 loc) · 966 Bytes
/
Solution.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
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int graph[MAX][MAX], visited[MAX], n;
void DFS(int v) {
printf("%d ", v);
visited[v] = 1;
for (int i = 1; i <= n; i++) {
if (graph[v][i] && !visited[i]) {
DFS(i);
}
}
}
int main() {
int edges, u, v;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("Enter the number of edges: ");
scanf("%d", &edges);
// Initialize graph and visited arrays
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
graph[i][j] = 0;
}
visited[i] = 0;
}
printf("Enter the edges (u v):\n");
for (int i = 0; i < edges; i++) {
scanf("%d %d", &u, &v);
graph[u][v] = 1;
graph[v][u] = 1; // For undirected graph
}
int start;
printf("Enter the starting node: ");
scanf("%d", &start);
printf("DFS Traversal: ");
DFS(start);
return 0;
}