Skip to content

Instantly share code, notes, and snippets.

@BrianLitwin
Last active May 27, 2018 01:16
Show Gist options
  • Save BrianLitwin/d1f85f29ba67098003a85182d8e69e52 to your computer and use it in GitHub Desktop.
Save BrianLitwin/d1f85f29ba67098003a85182d8e69e52 to your computer and use it in GitHub Desktop.
class Node: Hashable {
var adjacentNodes = [Node]()
}
func breadthFirstSearch(from startingNode: Node{
let queue = [startingNode]
var visited = Set<Node>()
while let next = queue.first {
visited.insert(next)
queue.remove(at: 0)
next.adjacentNodes.forEach({
if visited.contains($0) == false {
queue.append($0)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment