Skip to content

Instantly share code, notes, and snippets.

@arkanath
Created September 24, 2014 05:33
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/5b8f0645558ee76c024f to your computer and use it in GitHub Desktop.
Save arkanath/5b8f0645558ee76c024f to your computer and use it in GitHub Desktop.
Breadth First Traversal, without using queue.
#define nodecount 10000
bool discovered[nodecount];
vector<vector<int> > L;//Layers
void BFS(int s, vector<vector<int, int> > & adjList)//BFS for arrays, without queue, non-recursive, by @arkanathpathak
{
fill(discovered, discovered+nodecount, false);
discovered[s] = true;
for(int v=0;v<nodecount;v++)
{
if(v!=s)discovered[v]=false;
}
int i=0;
L.push_back(*(new vector<int>));
L[0].push_back(s);
while(L[i].size()!=0)
{
L.push_back(*(new vector<int>));
for(int j=0;j < (int)L[i].size(); j++)
{
int u = L[i][j];
for(int k=0;k<(int)adjList[u].size();k++)
{
int v = adjList[u][k];
if(discovered[v]==false)
{
discovered[v] = true;
L[i+1].push_back(v);
}
}
}
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment