Skip to content

Instantly share code, notes, and snippets.

@ziyoshams
Last active July 30, 2018 01:49
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 ziyoshams/c37d1fcf075de7c0345dc049e2006b13 to your computer and use it in GitHub Desktop.
Save ziyoshams/c37d1fcf075de7c0345dc049e2006b13 to your computer and use it in GitHub Desktop.
// HELPER FUNCTION
createVisitedObject(){
let arr = {};
for(let key of this.AdjList.keys()){
arr[key] = false;
}
return arr;
}
// Breadth First Search
bfs(startingNode){
let visited = this.createVisitedObject();
let q = [];
visited[startingNode] = true;
q.push(startingNode);
while(q.length){
let current = q.pop()
console.log(current);
let arr = this.AdjList.get(current);
for(let elem of arr){
if(!visited[elem]){
visited[elem] = true;
q.unshift(elem)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment