forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.cpp
36 lines (27 loc) · 754 Bytes
/
DFS.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
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>> &adjList,vector<bool> &visited,int source,int N){
cout<<"Visited: "<<source<<endl;
visited[source]=true;
int totalNeighbour = adjList[source].size();
for(int i=0;i<totalNeighbour;i++){
int neighbourNode = adjList[source][i];
if(visited[neighbourNode]==false){
dfs(adjList,visited,neighbourNode,N);
}
}
}
int main(){
int N=6;
vector<bool> visited(N,false);
vector<vector<int>> adjList(N);
adjList[0].push_back(1);
adjList[0].push_back(2);
adjList[0].push_back(4);
adjList[1].push_back(3);
adjList[2].push_back(3);
adjList[3].push_back(4);
adjList[3].push_back(5);
dfs(adjList,visited,0,N);
return 0;
}