Skip to content

Commit

Permalink
Create BFSGraph.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Jul 4, 2024
1 parent d7a6058 commit ef75685
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Graphs/BFSGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
// Function to return Breadth First Traversal of given graph.
vector<int> bfsOfGraph(int V, vector<int> adj[]) {
// Code here
queue<int>q;
vector<int>visited(V,0);
vector<int>ans;
q.push(0);
visited[0]=1;
ans.push_back(0);
while(!q.empty()){
int node=q.front();
q.pop();
// ans.push_back(node);
for(auto it: adj[node]){
if(!visited[it]){
visited[it]=1;
q.push(it);
ans.push_back(it);
}
}
}
return ans;
}
};

0 comments on commit ef75685

Please sign in to comment.