Skip to content

Instantly share code, notes, and snippets.

@jaredjenkins
Created February 4, 2014 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredjenkins/8811495 to your computer and use it in GitHub Desktop.
Save jaredjenkins/8811495 to your computer and use it in GitHub Desktop.
Breadth First Search
class Node
attr_accessor :data, :adjacents, :visited?
def initialize(data)
self.data = data
self.visited? = false
self.adjacents = []
end
end
def bfs(node)
queue = []
queue << node
while queue.length > 0
n = queue.shift
visit(n) #do something with the node
n.visited? = true
n.adjacents.each do |adj|
if(!adj.visited?)
queue << adj
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment