-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS algorithm.cpp
More file actions
46 lines (36 loc) · 773 Bytes
/
BFS algorithm.cpp
File metadata and controls
46 lines (36 loc) · 773 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
//BFS algorithm-level order traversal matlab first level gets exceuted then second then third similar to queue-FIFO so use fifo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define w(t) ll t; cin>>t; while(t--)
#define vi vector<int>
#define pb push_back
void bfs(vector<vector<int> >&adj,int src){
queue<int>q;
q.push(src);
vector<bool>vis(adj.size(),false);
vis[src]=true;
while(!q.empty()){
int u=q.front();
cout<<u<<" ";
q.pop();
for(int i=0;i<adj[u].size();i++){
if(vis[adj[u][i]]==false){
q.push(adj[u][i]);
vis[adj[u][i]]=true;
}
}
}
}
int main() {
int n,m;
cin>>n>>m;
vector<vector<int> >adj(n+1);
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
bfs(adj,1);
}