Created
February 4, 2014 20:07
-
-
Save jaredjenkins/8811344 to your computer and use it in GitHub Desktop.
Depth First Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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