Complete the code to create an adjacency matrix for a graph with 3 nodes.
adj_matrix = [[0 for _ in range(3)] for _ in range([1])]
The adjacency matrix for a graph with 3 nodes must be a 3x3 matrix, so the range should be 3.
Complete the code to add an edge from node 1 to node 2 in an adjacency list.
adj_list = {0: [], 1: [], 2: []}
adj_list[[1]].append(2)To add an edge from node 1 to node 2, we append 2 to the list of node 1.
Fix the error in the adjacency matrix code to mark an edge between node 0 and node 2.
adj_matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] adj_matrix[0][[1]] = 1 adj_matrix[2][0] = 1
To mark an edge from node 0 to node 2, set adj_matrix[0][2] = 1. The blank is the column index, which should be 2.
But since the code already sets adj_matrix[2][0] = 1, the blank must be 2 to complete the symmetric edge.
Fill both blanks to create an adjacency list entry for node 2 with edges to nodes 0 and 1.
adj_list = {2: [[1], [2]]}Node 2 has edges to nodes 0 and 1, so the list should contain 0 and 1.
Fill both blanks to create a dictionary comprehension that builds an adjacency list for nodes with edges only if the node index is less than 3.
adj_list = {i: [0 if j != i else 1 for j in range(5)] for i in range([1]) if i [2] 3}The dictionary comprehension starts with '{'. The range for i is 5. The condition 'i < 3' filters nodes less than 3.