Skip to content

Instantly share code, notes, and snippets.

@jaredjenkins
Created February 4, 2014 20:07
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/8811344 to your computer and use it in GitHub Desktop.
Save jaredjenkins/8811344 to your computer and use it in GitHub Desktop.
Depth First Search
class Node
attr_accessor :data, :adjacents, :visited?
def initialize(data)
self.data = data
self.visited? = false
self.adjacents = []
end
end
def dfs(node)
visit(node) #do something with the node
node.visited? = true
node.adjacents.each do |adj|
if(!adj.visited?)
dfs(adj)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment