Complete the code to initialize the queue with the start node.
queue := []int[1]
visited := make(map[int]bool)
visited[start] = trueThe BFS queue starts with the start node to begin traversal.
Complete the code to dequeue the first element from the queue.
current := queue[[1]] queue = queue[1:]
In Go slices, the first element is at index 0, so we dequeue from index 0.
Fix the error in checking if a neighbor node has been visited.
if !visited[[1]] { queue = append(queue, neighbor) visited[neighbor] = true }
We check if the neighbor node has been visited before adding it to the queue.
Fill both blanks to correctly loop over neighbors and mark them visited.
for _, [1] := range graph[current] { if !visited[[2]] { queue = append(queue, neighbor) visited[neighbor] = true } }
The loop variable should be 'neighbor' and the visited check should use 'neighbor'.
Fill all three blanks to build the BFS function that returns the order of visited nodes.
func BFS(graph map[int][]int, start int) []int {
var order []int
queue := []int[1]
visited := make(map[int]bool)
visited[start] = true
for len(queue) > 0 {
current := queue[[2]]
queue = queue[1:]
order = append(order, current)
for _, [3] := range graph[current] {
if !visited[neighbor] {
queue = append(queue, neighbor)
visited[neighbor] = true
}
}
}
return order
}The BFS starts with the start node in the queue, dequeues from index 0, and loops over neighbors using 'neighbor'.
