-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfssoul.cpp
85 lines (76 loc) · 1.65 KB
/
dfssoul.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
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class dfs
{
public:
stack<int> s;
int ver,edge;
int *visited;
int *store;
dfs(int ver,int edge)
{
this->ver=ver;
this->edge=edge;
this->visited=new int(ver);
this->store=new int(ver);
}
void creategraph(int a,int b,vector <int> *graph);
void display(vector <int> *graph);
void takedfs(int a,vector <int> *g);
void forst(vector <int> *v);
};
void dfs::creategraph(int a,int b,vector <int> *graph)
{
graph[a].push_back(b);
graph[b].push_back(a);
}
void dfs::display(vector <int> *v)
{
for(int i=0;i<ver;i++)
{
cout<<"Vertex "<<i<<" : ";
for(auto j=v[i].begin();j!=v[i].end();j++)
{
cout<<*j<<" ";
}
cout<<endl;
}
}
void dfs::takedfs(int a,vector <int> *g)
{
for(int k=0;k<ver;k++)
visited[k]=0;
int m=0;
s.push(a);
while(!s.empty())
{
int b=s.top();
s.pop();
if(!(visited[b]))
{
cout<<"Visited: "<<b<<" "<<endl;
store[m++]=b;
visited[b]=1;
}
for(int i=0;i<g[b].size();i++)
{
if(!(visited[g[b][i]]))
{
cout<<"Adjancancy of "<<b<<" is "<<g[b][i]<<" and push it into stack"<<endl;
s.push(g[b][i]);
// visited[*i]=1;
}
}
}
cout<<endl;
}
void dfs::forst(vector <int> *v)
{
for(int i=0;i<ver-1;i++)
{
v[store[i]].push_back(store[i+1]);
v[store[i+1]].push_back(store[i]);
}
}