-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
54 lines (54 loc) · 1.05 KB
/
dfs.cpp
File metadata and controls
54 lines (54 loc) · 1.05 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
#include<bits/stdc++.h>
using namespace std;
class graph
{
int v;
list<int>* adj;
bool *visited;
vector<int> ve;
public:
graph(int v)
{
this->v=v;
adj=new list<int>[v];
visited=new bool[v];
for(int i=0;i<v;i++)
visited[i]=false;
}
void addEdge(int v,int w)
{
adj[v].push_back(w);
// adj[w].push_back(v);
}
void DFS(int s)
{list<int>:: iterator i;
visited[s]=true;
ve.push_back(s);
for(i=adj[s].begin();i!=adj[s].end();i++)
{
if(!visited[*i])
{cout<<"adj to "<<s<<" :"<<*i<<endl;
DFS(*i);
}
}
cout<<endl;
}
void print()
{
for(int i=0;i<ve.size();i++)
cout<<ve[i]<<" ";
}
};
int main()
{vector<int> v;
graph g(6);
g.addEdge(0,1);
g.addEdge(4,2);
g.addEdge(1,2);
g.addEdge(2,5);
g.addEdge(4,3);
g.addEdge(3,0);
g.addEdge(5,1);
g.DFS(4);
g.print();
}