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 = []
|
||||||
search_queue += @graph[name]
|
search_queue += @graph[name]
|
||||||
# This array is how you keep track of which people you've searched before.
|
# This array is how you keep track of which people you've searched before.
|
||||||
searched = []
|
searched = {}
|
||||||
|
|
||||||
until search_queue.empty?
|
until search_queue.empty?
|
||||||
person = search_queue.shift
|
person = search_queue.shift
|
||||||
# Only search this person if you haven't already searched them.
|
# 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
|
return person if yield person
|
||||||
search_queue += @graph[person]
|
search_queue += @graph[person]
|
||||||
# Marks this person as searched
|
# Marks this person as searched
|
||||||
searched.push(person)
|
searched[person] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
false
|
false
|
||||||
|
|||||||
Reference in New Issue
Block a user