Created
July 30, 2018 15:41
-
-
Save ziyoshams/510c3040d0653abb89d1019e5c0cca56 to your computer and use it in GitHub Desktop.
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
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