0
0
Data Structures Theoryknowledge~10 mins

Graph representations (adjacency matrix vs list) in Data Structures Theory - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an adjacency matrix for a graph with 3 nodes.

Data Structures Theory
adj_matrix = [[0 for _ in range(3)] for _ in range([1])]
Drag options to blanks, or click blank then click option'
A3
B2
C4
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong size for the matrix, like 2 or 4 instead of 3.
2fill in blank
medium

Complete the code to add an edge from node 1 to node 2 in an adjacency list.

Data Structures Theory
adj_list = {0: [], 1: [], 2: []}
adj_list[[1]].append(2)
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Appending to the wrong node's list.
Using a node number that does not exist in the dictionary.
3fill in blank
hard

Fix the error in the adjacency matrix code to mark an edge between node 0 and node 2.

Data Structures Theory
adj_matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
adj_matrix[0][[1]] = 1
adj_matrix[2][0] = 1
Drag options to blanks, or click blank then click option'
A2
B3
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column index.
Confusing row and column indices.
4fill in blank
hard

Fill both blanks to create an adjacency list entry for node 2 with edges to nodes 0 and 1.

Data Structures Theory
adj_list = {2: [[1], [2]]}
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Including the node itself (2) in its adjacency list.
Using nodes not connected to node 2.
5fill in blank
hard

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.

Data Structures Theory
adj_list = {i: [0 if j != i else 1 for j in range(5)] for i in range([1]) if i [2] 3}
Drag options to blanks, or click blank then click option'
A{
B5
C<
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of curly braces for dictionary comprehension.
Incorrect range or condition syntax.