-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
45 lines (35 loc) · 1.12 KB
/
DFS.cpp
File metadata and controls
45 lines (35 loc) · 1.12 KB
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
#include <iostream>
#include <vector>
using namespace std;
// Function to perform DFS traversal
void dfs(int node, vector<int>& visited, vector<vector<int>>& graph) {
// Mark the current node as visited
visited[node] = 1 cout << node " ";
// Traverse all adjacent nodes of the current node
for (int i = 0; i < graph[node].size(); i {
int adjacentNode = graph[node][i];
if (!visited[adjacentNode]) {
dfs(adjacentNode, visited, graph);
}
}
}
int main() {
int numNodes, numEdges;
cout << "Enter the number of nodes: ";
cin >> numNodes;
cout <<Enter the number of edges: ";
cin >> numEdges;
vector<vector<int>> graph(numNodes);
cout << "Enter the edges (node1 node2):" << endl;
for (int i = 0; i < numEdges; i++) {
int node1, node2;
cin >> node1 >> node2;
Add edges to the graph
graph[node1].push_back(node2);
graph[node2].push_back(node1);
}
vector<int> visited(numNodes, 0); // Initialize visited array with 0
cout << " traversal starting from node 0: ";
dfs(0, visited, graph);
return 0;
}