Complete the code to declare the graph as an adjacency list.
graph := map[int][]int[1]The graph is declared as a map from int to slice of ints. The correct syntax uses []int{...} to define the slice.
Complete the code to mark a node as visited in DFS.
visited[node] = [1]We mark a node as visited by setting its visited value to true.
Fix the error in the DFS recursive call to visit neighbors.
for _, neighbor := range graph[node] { if !visited[neighbor] { [1](neighbor) } }
The DFS function is named 'dfs' and should be called recursively with the neighbor node.
Fill both blanks to append the node to result after DFS and to reverse the result slice.
result = append(result, [1]) for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { result[i], result[[2]] = result[j], result[i] }
We append the current node to the result slice. To reverse the slice, we swap elements at positions i and j.
Fill all three blanks to complete the topological sort function using DFS.
func topologicalSort(graph map[int][]int) []int {
visited := make(map[int]bool)
var result []int
var dfs func(int)
dfs = func(node int) {
visited[node] = [1]
for _, neighbor := range graph[node] {
if !visited[neighbor] {
dfs([2])
}
}
result = append(result, [3])
}
for node := range graph {
if !visited[node] {
dfs(node)
}
}
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
return result
}Mark the node as visited (true), call dfs on neighbor, and append the current node to result.
