Skip to content

Instantly share code, notes, and snippets.

@rajatk16
Created December 15, 2018 23:45
Show Gist options
  • Select an option

  • Save rajatk16/cef4425ccb7850761834392c0ac54b4b to your computer and use it in GitHub Desktop.

Select an option

Save rajatk16/cef4425ccb7850761834392c0ac54b4b to your computer and use it in GitHub Desktop.
breadthFirst(startingNode, neighborVisit) {
const firstNode = this.searchNode(startingNode)
const visitedNode = nodes.reduce((sum, node) => {
sum[node.id] = false
return sum
}, {})
const queue = queueCreator()
queue.add(firstNode)
while (!queue.empty()) {
const temp = queue.remove()
if (!visitedNode[temp.id]) {
neighborVisit(temp)
visitedNode[temp.id] = true
}
temp.neighbors.forEach(node => {
if(!visitedNode[node.id]) {
queue.add(node)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment