Complete the code to declare an adjacency list for a graph with 5 nodes.
graph := make([][]int, [1])The adjacency list is created with a slice of slices, one for each node. Since there are 5 nodes, we use 5.
Complete the code to initialize an adjacency matrix for a graph with 4 nodes.
matrix := make([][]int, [1]) for i := range matrix { matrix[i] = make([]int, [1]) }
An adjacency matrix for a graph with 4 nodes is a 4x4 matrix, so both dimensions are 4.
Fix the error in the code that adds an edge from node 1 to node 3 in an adjacency list.
graph[[1]] = append(graph[1], 3)
The index before append must be the node number where the edge starts, which is 1 here.
Fill both blanks to check if there is an edge from node 2 to node 4 in an adjacency matrix.
if matrix[[1]][[2]] == 1 { fmt.Println("Edge exists") }
To check edge from node 2 to 4, use matrix[2][4].
Fill all three blanks to create a map of node to neighbors using adjacency list style.
neighbors := map[int][]int{
[1]: {2, 3},
[2]: {1},
[3]: {1, 4},
}Keys are node numbers. Node 1 connects to 2 and 3, node 2 connects to 1, node 4 connects to 1 and 4.
