-
-
Save williscool/1790863 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
$graph = {a: [:b, :c], b: [:d, :e], c: [:f, :g]} | |
def dfs_nr(node) | |
queue = [node] | |
seen = {} | |
while queue.size > 0 | |
node = queue.pop | |
puts node | |
seen[node] = true | |
if $graph[node] | |
$graph[node].each {|child| queue.push child unless seen[child] } | |
end | |
end | |
end | |
dfs_nr :a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment