Skip to content

Instantly share code, notes, and snippets.

@williscool
Forked from rahulkmr/gist:1789807
Created February 10, 2012 16:55
Show Gist options
  • Save williscool/1790863 to your computer and use it in GitHub Desktop.
Save williscool/1790863 to your computer and use it in GitHub Desktop.
$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