-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_BFSTraversal.cpp
37 lines (30 loc) · 942 Bytes
/
GFG_BFSTraversal.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
/* https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1#
* Given a directed graph. The task is to do Breadth First Traversal of this graph starting from 0.
*/
// } Driver Code Ends
class Solution {
public:
// Function to return Breadth First Traversal of given graph.
vector<int> bfsOfGraph(int V, vector<int> adj[]) {
// Code here
vector<int> bfs_lists;
queue<int> q;
vector<bool> visited(V,0);
q.push(0);
visited[0]=true;
while(!q.empty())
{
int i = q.front(); q.pop();
bfs_lists.push_back(i);
for(int w: adj[i])
{
if(!visited[w])
{
visited[w] = true;
q.push(w);
}
}
}// while
return bfs_lists;
}
};