-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
60 lines (50 loc) · 1.45 KB
/
DFS.cpp
File metadata and controls
60 lines (50 loc) · 1.45 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Graph class
class Graph {
private:
int vertices; // Number of vertices
vector<vector<int>> adjList; // Adjacency list
public:
// Constructor
Graph(int v) : vertices(v), adjList(v) {}
// Function to add an edge
void addEdge(int u, int v) {
adjList[u].push_back(v); // Add v to u's list
adjList[v].push_back(u); // Add u to v's list (for undirected graph)
}
// BFS function
void dfsHelper(int node, vector<bool> &visited) {
// Mark the current node as visited
visited[node] = true;
cout << node << " ";
// Recur for all adjacent nodes
for (int neighbor : adjList[node]) {
if (!visited[neighbor]) {
dfsHelper(neighbor, visited);
}
}
}
// DFS Function
void dfs(int start) {
vector<bool> visited(vertices, false); // Track visited nodes
cout << "DFS Traversal starting from node " << start << ": ";
dfsHelper(start, visited);
cout << endl;
}
};
// Main function
int main() {
Graph g(6); // Create a graph with 6 vertices (0 to 5)
// Add edges
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 5);
// Perform BFS starting from node 0
g.dfs(0);
return 0;
}