-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathShortest_path_BFS.cpp
59 lines (43 loc) · 1.07 KB
/
Shortest_path_BFS.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
//Shortest path in undirected graph using BFS.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> adj[10005];
ll dis[10005],vis[10005];
void bfs(ll src){
queue<ll> q;
vis[src]=1;
dis[src]=0;
q.push(src);
while(!q.empty()){
int node = q.front();
q.pop();
for(auto it : adj[node]){ //traversing in the adjacency list.
if(vis[it]==0){
dis[it]=dis[node]+1;
q.push(it);
vis[it]=1;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--){
ll n,m; //no. of nodes & vertices.
cin>>n>>m;
for(ll i=0;i<=n;i++) adj[i].clear() , vis[i]=0;
while(m--){
ll a,b;
cin>>a>>b; adj[a].push_back(b); adj[b].push_back(a);
}
bfs(1); //calling the above BFS function.
for(ll i = 1; i <= n; i++) //printing path from source to ith node.
cout<<dis[i]<<" ";
cout<<endl;
}
}