-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortest_Path_BFS.cpp
More file actions
66 lines (58 loc) · 1.26 KB
/
Shortest_Path_BFS.cpp
File metadata and controls
66 lines (58 loc) · 1.26 KB
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
// Implementation of Shortest path using BFS
//
// Running Time Complexity : O(V+E)
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define int long long
#define pii pair<int, int>
const int N = 100005;
int V, E;
bool vis[N];
int dist[N];
vector<int> adj[N];
void bfsShortestPath(int src)
{
int i, x, y;
queue<int> Q;
Q.push(src);
dist[src] = 0;
while (!Q.empty())
{
x = Q.front();
Q.pop();
vis[x] = true;
for(i = 0 ; i < adj[x].size() ; i++)
{
y = adj[x][i];
if(!vis[y])
{
Q.push(y);
dist[y] = min(dist[y], dist[x] + 1);
}
}
}
cout << "Distance from the source node " << src << endl;
for(i = 0 ; i < V ; i++)
cout << i << "\t" << dist[i] << endl;
}
int32_t main()
{
int x, y, i, src;
memset(dist, INF, sizeof(dist));
memset(vis, false, sizeof(vis));
cout << "Enter the no of Nodes";
cin >> V;
cout << "Enter the no of Edges";
cin >> E;
for(i = 0 ; i < E ; i++)
{
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
cout << "Enter the source node:";
cin >> src;
bfsShortestPath(src);
return 0;
}