Last active
July 28, 2020 05:32
-
-
Save KerryJones/d4d38d7cd5602d419644 to your computer and use it in GitHub Desktop.
Python Breadth-First-Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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