-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.cpp
81 lines (64 loc) · 1.28 KB
/
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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<vector>
#include<stdlib.h>
using namespace std;
vector <int> adj[10];
int n,e;
void initialize(){
cout<<"ENTER EDGES"<<endl;
int x,y;
for (int i=0 ; i<e ; i++) {
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i=1 ; i<=n ; i++) {
cout<<"adj["<<i<<"] = ";
for (int j=0 ; j<adj[i].size(); j++) {
cout<<adj[i][j]<<" ";
}
cout<<endl;
}
}
int fun(bool *a,int i){
int j=0;
if (a[i] == 1) {
return 1;
}
if (a[i] != 1) {
a[i] = 1;
}
while ( j<adj[i].size() ) {
if (a[adj[i][j] == 1]) {
j++;
}
else{
fun(a,adj[i][j]);
j++;
}
}
return 1;
}
int main(){
cout<<"this is undirected graph"<<endl;
cout<<"enter number of nodes"<<endl;
cin>>n;
cout<<"enter number of edges"<<endl;
cin>>e;
initialize();
bool a[n+1];
for (int i=1 ; i<=n ; i++) {
a[i] = 0;
}
int i;
cout<<"enter root"<<endl;
cin>>i;
fun(a,i);
cout<<"connected nodes with root are"<<endl;
for (int i=1 ; i<=n ; i++) {
if (a[i] == 1) {
cout<<i<<" ";
}
}
cout<<endl;
}