Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Last active July 28, 2020 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KerryJones/d4d38d7cd5602d419644 to your computer and use it in GitHub Desktop.
Save KerryJones/d4d38d7cd5602d419644 to your computer and use it in GitHub Desktop.
Python Breadth-First-Search
class Node:
visited = None
data = None
adjacent = None
def __init__(self, value):
self.data = value
self.adjacent = []
self.visited = False
def bfs(node, data):
queue = [node]
while len(queue) > 0:
adjacent_node = queue.pop(0)
if adjacent_node.visited:
continue
if adjacent_node.data == data:
return adjacent_node
adjacent_node.visited = True
queue += adjacent_node.adjacent
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment