Skip to content

Instantly share code, notes, and snippets.

@Wolwer1nE
Created October 31, 2019 08:28
Show Gist options
  • Save Wolwer1nE/70ecad84ac9ae61e118b443aeadcff7c to your computer and use it in GitHub Desktop.
Save Wolwer1nE/70ecad84ac9ae61e118b443aeadcff7c to your computer and use it in GitHub Desktop.
def breadth_first_search?(start, goal)
visited = Hash.new(false)
queue = Queue.new << start
visited[start] = true
until queue.empty?
node = queue.pop
return true if node == goal
@vertices[node].each { |child| bfs_add_node(queue, visited, child) }
end
false
end
def bfs_add_node(queue, visited, node)
return if visited[node]
queue.push(node)
visited[node] = true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment