Skip to content

Instantly share code, notes, and snippets.

@arkanath
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkanath/fe768077941cef9f7685 to your computer and use it in GitHub Desktop.
Save arkanath/fe768077941cef9f7685 to your computer and use it in GitHub Desktop.
Breadth First Traversal, with Queue
vector<int> adjList[N];
int visited[N];
void bfs(int u)
{
visited[u] = 1;//mapping the visited nodes, by default 0
queue<int> nq;//queue used
nq.push(u);//push initial node
while(!nq.empty())
{
int tt = nq.front();
visited[tt] = 1;
nq.pop();
for(int i=0;i<adjList[tt].size();i++)
{
if(!visited[adjList[tt][i]])
{
nq.push(adjList[tt][i]);//push all non-visited neighbours
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment