Skip to content

Instantly share code, notes, and snippets.

@jiachen247
Last active May 30, 2022 03:18
Show Gist options
  • Save jiachen247/5f137fa3ed18e1300be45b68095420ac to your computer and use it in GitHub Desktop.
Save jiachen247/5f137fa3ed18e1300be45b68095420ac to your computer and use it in GitHub Desktop.
graph = {
'a' : ['b','c'],
'b' : ['d'],
'c' : ['e'],
'd' : ['f'],
'e' : [],
'f' : []
}
visited = set() # List to keep track of visited nodes.
queue = [] #Initialize a queue
def bfs(node):
visited.add(node)
queue.append(node)
while queue:
s = queue.pop(0)
print (s, end = " ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.add(node)
queue.append(neighbour)
# Driver Code
bfs('a')
def containsX(node, target):
pass
print(containsX('a', 'e'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment