Skip to content

Instantly share code, notes, and snippets.

@ziyoshams
Created July 30, 2018 15:41
Show Gist options
  • Save ziyoshams/510c3040d0653abb89d1019e5c0cca56 to your computer and use it in GitHub Desktop.
Save ziyoshams/510c3040d0653abb89d1019e5c0cca56 to your computer and use it in GitHub Desktop.
doesPathExist(firstNode, secondNode){
// we will approach this BFS way
let path = [];
let visited = this.createVisitedObject();
let q = [];
visited[firstNode] = true;
q.push(firstNode);
while(q.length){
let node = q.pop();
path.push(node);
let elements = this.AdjList.get(node);
if(elements.includes(secondNode)){
console.log(path.join('->'))
return true;
}else{
for(let elem of elements){
if(!visited[elem]){
visited[elem] = true;
q.unshift(elem);
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment