Skip to content

Instantly share code, notes, and snippets.

@ahmadyan
Created December 8, 2016 00:25
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 ahmadyan/ad2ad8371f3403b6495225381fa0e248 to your computer and use it in GitHub Desktop.
Save ahmadyan/ad2ad8371f3403b6495225381fa0e248 to your computer and use it in GitHub Desktop.
The BFS algorithm implemented using std::list
//list is basically a linked-list
int bfs_list(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
list<int> q;
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
auto u = q.front();
sig += u;
q.pop_front();
for(auto v: g->adj_list[u]){
if(!visited[v]){
visited[v]=true;
q.push_back(v);
}
}
}
return sig;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment