represent record of searched verices as a hash for O(1) lookup
This commit is contained in:
@@ -18,16 +18,16 @@ def search(name)
|
||||
search_queue = []
|
||||
search_queue += @graph[name]
|
||||
# This array is how you keep track of which people you've searched before.
|
||||
searched = []
|
||||
searched = {}
|
||||
|
||||
until search_queue.empty?
|
||||
person = search_queue.shift
|
||||
# Only search this person if you haven't already searched them.
|
||||
next if searched.member?(person)
|
||||
next if searched[person]
|
||||
return person if yield person
|
||||
search_queue += @graph[person]
|
||||
# Marks this person as searched
|
||||
searched.push(person)
|
||||
searched[person] = true
|
||||
end
|
||||
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user