forked from vedant781999/Array_operation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS-Graph.cpp
More file actions
50 lines (43 loc) · 813 Bytes
/
BFS-Graph.cpp
File metadata and controls
50 lines (43 loc) · 813 Bytes
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
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+2;
bool vis[N];
vector<int> adj[N];
int main()
{
for(int i=0;i<N;i++)
{
vis[i]=0;
}
int n,m;
cin>>n>>m;
int x,y;
// graph input (jitni edges he utne time loop chalayo aur kaam khatam)
for(int i=0;i<m;i++)
{
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
// BFS is implemented using queue
queue<int> q;
q.push(1);
vis[1]=true;
while(!q.empty())
{
int node = q.front();
q.pop();
cout<<node<<endl;
vector<int> :: iterator it;
for(it = adj[node].begin();it!=adj[node].end();it++)
{
if(!vis[*it])
{
vis[*it]=1;
q.push(*it);
}
}
}
cout<<endl;
return 0;
}