Skip to content

Instantly share code, notes, and snippets.

@ianbishop
Last active December 11, 2015 09:39
Show Gist options
  • Select an option

  • Save ianbishop/4581542 to your computer and use it in GitHub Desktop.

Select an option

Save ianbishop/4581542 to your computer and use it in GitHub Desktop.
# BFS
def bfs(source, target)
q = []
visited = {}
q << source
while !q.empty?
current = q.shift
visited[current] = true
return if (current == target) # done
current.neighbors.each do |neighbor|
q << neighbor if !visited[neighbor]
end
end
# no solution
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment