Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Link to the question : https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1
- class Solution
- {
- public:
- //Function to return Breadth First Traversal of given graph.
- vector<int>bfsOfGraph(int V, vector<int> adj[])
- {
- vector<bool> visit(V,false);
- vector<int>ans;
- queue<int>q;
- q.push(0);
- while(q.size()!=0){
- int curr = q.front();
- q.pop();
- ans.push_back(curr);
- for(int i = 0; i<adj[curr].size(); i++){
- int ncurr = adj[curr][i];
- if(visit[ncurr]==false){
- q.push(ncurr);
- visit[ncurr] = true;
- }
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement