Last active
August 29, 2015 14:06
-
-
Save arkanath/fe768077941cef9f7685 to your computer and use it in GitHub Desktop.
Breadth First Traversal, with 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
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