forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class graph.cpp
72 lines (66 loc) · 1.44 KB
/
class graph.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
class Graph{
public:
int v;
list<int> *l;
Graph(int V){
v = V;
l = new list<int>[v+1];
}
void addedge(int u,int v){
l[u].push_back(v);
l[v].push_back(u);
}
void Print(){
for(int i = 1;i<v+1;i++){
cout<<i<<"->";
for(auto x:l[i]){
cout<<x<<" ";
}
cout<<endl;
}
}
void bfs(int k,int ans)
{
queue<int> q;
int *dist; bool *vis;;
dist = new int[v+1];
vis = new bool[v+1];
q.push(k);
vis[k]=1;
int cnt= 0 ;
while(!q.empty())
{
int node=q.front();
q.pop();
for(auto it:l[node])
{
if(!vis[it])
{
dist[it]=dist[node]+1;
vis[it]=1;
q.push(it);
//if(dist[it]==ans){cnt+=1;}
}
}
}
cout<<dist[ans]<<"\n";
}
int dfshelper(int g,bool visited[]){
visited[g] = true;
cout<< g <<" ";
for(auto x:l[g]){
if(!visited[x]){
dfshelper(x,visited);
}
}
}
void dfs(int src){
bool *vis;
vis = new bool[v];
dfshelper(i, vis);
cout<<endl;
}
}
//cout<<"No. of connected compo = "<<cnt<<endl;
}
};