forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS(Graph).cpp
61 lines (51 loc) · 1.34 KB
/
DFS(Graph).cpp
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
61
#include<iostream>
#include<list>
using namespace std;
class Graph {
int v; // Number of vertices
list<int> *adj; // adjacency list
void Traverse(int v, bool visited[]);
public:
Graph(int v); // Constructor
void addEdge(int v, int w); // Add an edge to graph
void DFS(int v);
};
Graph::Graph(int v) {
this->v = v;
adj = new list<int>[v];
}
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
}
void Graph::DFS(int v) {
// Mark all the vertices as not visited
bool *visited = new bool[v];
for (int s = 0; s < v; s++)
visited[s] = false;
// Call the recursive helper function to print DFS traversal
Traverse(v, visited);
}
void Graph::Traverse(int v, bool visited[]) {
// Mark the current node as visited and print it
visited[v] = true;
cout << v << " ";
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i]) {
Traverse(*i, visited);
}
}
int main() {
// Create a sample graph
Graph g(7);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(1, 4);
g.addEdge(1, 5);
g.addEdge(2, 6);
g.addEdge(5, 3);
g.DFS(0);
return 0;
}