-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.cpp
54 lines (47 loc) · 924 Bytes
/
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
// Implementation of 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;
bool vis[N];
vector<int> adj[N];
void bfs(int src)
{
int i, x, y;
queue<int> Q;
Q.push(src);
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);
}
cout << x << " ";
}
}
int32_t main()
{
int V, E, x, y, i;
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);
}
bfs(0);
return 0;
}