From 70ddf46cb3254718fc371255eaba0773e52aa63c Mon Sep 17 00:00:00 2001 From: TimoSci Date: Sun, 8 Dec 2019 12:56:36 +0100 Subject: [PATCH] represent record of searched verices as a hash for O(1) lookup --- 06_breadth-first_search/ruby/01_breadth-first_search.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/06_breadth-first_search/ruby/01_breadth-first_search.rb b/06_breadth-first_search/ruby/01_breadth-first_search.rb index a47e0d0..2aa15f3 100644 --- a/06_breadth-first_search/ruby/01_breadth-first_search.rb +++ b/06_breadth-first_search/ruby/01_breadth-first_search.rb @@ -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