sp-graphs
https://practice.geeksforgeeks.org/tracks/sp-graph/?batchId=152
BFS of graph Learned on 20th July 2020
vector <int> bfs(vector<int> g[], int N) {
queue<int> q;
bool *visited= new bool[N+1]{false};
vector<int> ans;
q.push(0);
while(!q.empty()){
for(auto node: g[q.front()]){
if(! visited[node]){
q.push(node);
visited[node]= true;
}
}
ans.push_back(q.front());
q.pop();
}
return ans;
}
Last updated
Was this helpful?