represent record of searched verices as a hash for O(1) lookup

This commit is contained in:
TimoSci
2019-12-08 12:56:36 +01:00
parent b65c3de283
commit 70ddf46cb3

View File

@@ -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