forked from SanjayDevTech/Code-with-love
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpranav230_DFS.cpp
More file actions
47 lines (37 loc) · 1.01 KB
/
Copy pathpranav230_DFS.cpp
File metadata and controls
47 lines (37 loc) · 1.01 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
#include<bits/stdc++.h>
using namespace std;
void fndepthfirstserach(int currentvertex,vector<int> &v,vector<vector<int>> g,int n)
{
int i;
//marking the starting vertex as visited
v[currentvertex]=1;
for(i=0;i<n;i++)
//if the vertex that is connected to current vertex is not visited then applying DFS for that vertex
if(g[currentvertex][i]&& !v[i])
//here the starting vertex is the non visited vertex
fndepthfirstserach(i,v,g,n);
}
int main(void)
{
int i,j,k;
int numvert,vert;
cout<<"Enter the no. of vertices: ";
cin>>numvert;
vector<int> visited(numvert,0);
vector<vector<int>> graph(numvert,vector<int>(numvert));
cout<<"Enter the adjacency matrix here:\n";
for(i=0;i<numvert;i++)
for(j=0;j<numvert;j++)
cin>>graph[i][j];
cout<<"Enter the source vertex: ";
cin>>vert;
fndepthfirstserach(vert-1,visited,graph,numvert);
for(k=0;k<numvert;k++)
{
if(visited[k])
cout<<"\n vertex "<<k+1<<" reachable\n";
else
cout<<"\n vertex "<<k+1<<" not reachacble\n";
}
return 0;
}