-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path(Recursion)Dfs.cpp
More file actions
50 lines (38 loc) · 972 Bytes
/
(Recursion)Dfs.cpp
File metadata and controls
50 lines (38 loc) · 972 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define MAX_SIZE 100
int adj[MAX_SIZE][MAX_SIZE]; // Adjacency matrix representation
bool visited[MAX_SIZE];
void DFS(int start) {
if (visited[start]) {
return;
}
visited[start] = true;
printf("%d ", start); // Print the current node
for (int i = 0; i < MAX_SIZE; i++) {
if (adj[start][i] == 1 && !visited[i]) { // Check for connection and unvisited node
DFS(i);
}
}
}
int main() {
int nodes, edges, n1, n2;
printf("Total Nodes: ");
scanf("%d", &nodes);
printf("Total Edges: ");
scanf("%d", &edges);
// Initialize adjacency matrix with zeros (no connections)
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
adj[i][j] = 0;
}
}
for (int i = 0; i < edges; i++) {
scanf("%d %d", &n1, &n2);
adj[n1][n2] = 1; // Mark connection in both directions for undirected graph
adj[n2][n1] = 1;
}
DFS(1);
return 0;
}