-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS
More file actions
35 lines (27 loc) · 617 Bytes
/
Copy pathDFS
File metadata and controls
35 lines (27 loc) · 617 Bytes
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
#include <stdio.h>
#define V 5 // Number of vertices
int graph[V][V] = {
{0, 1, 1, 0, 0},
{1, 0, 0, 1, 0},
{1, 0, 0, 1, 1},
{0, 1, 1, 0, 1},
{0, 0, 1, 1, 0}
};
int visited[V]; // To track visited nodes
void DFS(int vertex) {
printf("%d ", vertex);
visited[vertex] = 1;
for (int i = 0; i < V; i++) {
if (graph[vertex][i] == 1 && !visited[i]) {
DFS(i);
}
}
}
int main() {
// Initialize visited array
for (int i = 0; i < V; i++)
visited[i] = 0;
printf("DFS Traversal: ");
DFS(0); // Start DFS from vertex 0
return 0;
}