Predict the output
What does this print?
edges = [(0, 1), (0, 2), (2, 3)]
graph: dict[int, list[int]] = {v: [] for v in range(4)}
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
print(graph[0], graph[3], sum(map(len, graph.values())))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [1, 2] [2] 6
Right: each undirected edge appears twice, so 3 edges give 6 entries.
An undirected adjacency list holds 2E entries, which is why its memory is O(V + E).
Why not the others?
- B [1, 2] [] 3. Undirected edges are added in both directions.
- C [1, 2] [2] 3. Count both directions: 2 × 3 = 6.