Skip to content

Instantly share code, notes, and snippets.

@anuvrat
Created March 5, 2017 06:42
Show Gist options
  • Save anuvrat/326e03c4cb9b75155bcf95e4c54e8603 to your computer and use it in GitHub Desktop.
Save anuvrat/326e03c4cb9b75155bcf95e4c54e8603 to your computer and use it in GitHub Desktop.
def _bfs(self):
word_queue = [(self.begin_word, 1)]
word_enqueued = {self.begin_word: True}
while word_queue:
word, level = word_queue.pop(0)
self.word_levels[word] = level
if word == self.end_word: return level
for next_word in self._get_next_words(word):
if next_word not in word_enqueued:
word_queue.append((next_word, level + 1))
word_enqueued[next_word] = True
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment