Created
September 24, 2014 05:33
-
-
Save arkanath/5b8f0645558ee76c024f to your computer and use it in GitHub Desktop.
Breadth First Traversal, without using queue.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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