Complete the code to initialize the queue for BFS traversal.
queue = [[1]]In BFS, the queue starts with the starting node to begin traversal.
Complete the code to mark a node as visited during BFS.
visited.add([1])We mark the current node as visited to avoid revisiting it.
Fix the error in the BFS neighbor check condition.
if neighbor not in [1]:
We only add neighbors to the queue if they have not been visited yet.
Fill both blanks to create a dictionary comprehension that maps nodes to their distances from the start node using BFS.
distances = {node: [1] for node in graph if node [2] visited}We assign a default distance of 1 to nodes not yet visited.
The condition checks nodes not in visited.
Fill all three blanks to complete the BFS function that returns the shortest path length from start to goal.
def bfs_shortest_path(graph, start, goal): queue = [(start, 0)] visited = set([start]) while queue: current, depth = queue.pop(0) if current == goal: return [1] for neighbor in graph[current]: if neighbor not in visited: visited.add(neighbor) queue.append((neighbor, [2])) return [3]
Return the current depth when goal is found.
Append neighbors with depth increased by 1.
Return -1 if goal is not reachable.