Last active
May 27, 2018 01:16
-
-
Save BrianLitwin/d1f85f29ba67098003a85182d8e69e52 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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