Bird
Raised Fist0
Data Structures Theoryknowledge~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which graph representation uses a 2D grid to show connections between nodes?
easy
A. Incidence matrix
B. Adjacency matrix
C. Edge list
D. Adjacency list

Solution

  1. Step 1: Understand adjacency matrix structure

    An adjacency matrix is a 2D grid where rows and columns represent nodes, and cells show if an edge exists.
  2. Step 2: Compare with other representations

    Adjacency lists store neighbors in lists, not grids. Edge lists and incidence matrices differ in format.
  3. Final Answer:

    Adjacency matrix -> Option B
  4. Quick Check:

    2D grid = adjacency matrix [OK]
Hint: Matrix means grid; list means neighbors [OK]
Common Mistakes:
  • Confusing adjacency list with matrix
  • Thinking edge list is a grid
  • Mixing incidence matrix with adjacency matrix
2. Which of the following is the correct way to represent an adjacency list in Python?
easy
A. graph = [[1, 2], 0, [0, 1]]
B. graph = [[0,1,0],[1,0,1],[0,1,0]]
C. graph = [(0,1), (1,2), (2,0)]
D. graph = {0: [1, 2], 1: [0], 2: [0, 1]}

Solution

  1. Step 1: Identify adjacency list format

    An adjacency list maps each node to a list of its neighbors, often using a dictionary in Python.
  2. Step 2: Check each option

    graph = {0: [1, 2], 1: [0], 2: [0, 1]} uses a dictionary with keys as nodes and values as neighbor lists, which is correct. graph = [[0,1,0],[1,0,1],[0,1,0]] is a matrix, C is an edge list, D incorrectly uses an integer 0 for node 1 instead of a list.
  3. Final Answer:

    graph = {0: [1, 2], 1: [0], 2: [0, 1]} -> Option D
  4. Quick Check:

    Dict with neighbors = adjacency list [OK]
Hint: Adjacency list uses dict with node keys [OK]
Common Mistakes:
  • Choosing matrix format as list
  • Confusing edge list with adjacency list
  • Using integer instead of list for neighbors
3. Given the adjacency matrix below, which nodes are connected to node 1?
graph = [[0, 1, 0], [1, 0, 1], [0, 1, 0]]
medium
A. Nodes 0 and 2
B. Nodes 1 and 2
C. Nodes 0 and 1
D. Nodes 2 only

Solution

  1. Step 1: Locate row for node 1

    Row 1 in the matrix is [1, 0, 1], representing edges from node 1 to nodes 0, 1, and 2.
  2. Step 2: Identify connected nodes

    Values 1 indicate connection. Here, positions 0 and 2 have 1, so node 1 connects to nodes 0 and 2.
  3. Final Answer:

    Nodes 0 and 2 -> Option A
  4. Quick Check:

    Row 1 has 1s at 0 and 2 [OK]
Hint: Check row for node, 1 means connected [OK]
Common Mistakes:
  • Confusing row and column indices
  • Including node itself as connected
  • Misreading zeros as edges
4. What is wrong with this adjacency list representation?
graph = {0: [1, 2], 1: [0, 3], 2: [0], 3: 1}
medium
A. Node 3's neighbors should be in a list
B. Node 1 has an invalid neighbor
C. Node 0 should not have neighbors
D. The graph should be an adjacency matrix

Solution

  1. Step 1: Check format of neighbors for each node

    Nodes 0, 1, and 2 have neighbor lists. Node 3 has a single integer instead of a list.
  2. Step 2: Identify correct adjacency list format

    Neighbors must always be in a list, even if only one neighbor exists, to keep consistent structure.
  3. Final Answer:

    Node 3's neighbors should be in a list -> Option A
  4. Quick Check:

    Neighbors must be lists [OK]
Hint: Neighbors always in lists, never single values [OK]
Common Mistakes:
  • Ignoring single neighbor format
  • Thinking adjacency list must be matrix
  • Assuming neighbors can be integers
5. For a graph with 1000 nodes and only 10,000 edges, which representation is more memory efficient and why?
hard
A. Adjacency matrix, because it allows faster edge checks
B. Adjacency matrix, because it uses fixed size memory
C. Adjacency list, because it stores only existing edges
D. Adjacency list, because it stores all possible edges

Solution

  1. Step 1: Calculate memory use for adjacency matrix

    An adjacency matrix for 1000 nodes uses 1000x1000 = 1,000,000 cells, regardless of edges.
  2. Step 2: Calculate memory use for adjacency list

    An adjacency list stores only the 10,000 edges, so memory use is proportional to edges, much less than matrix.
  3. Step 3: Compare efficiency

    Since edges are sparse compared to possible connections, adjacency list is more memory efficient.
  4. Final Answer:

    Adjacency list, because it stores only existing edges -> Option C
  5. Quick Check:

    Sparse graph = adjacency list efficient [OK]
Hint: Sparse graph? Use adjacency list for less memory [OK]
Common Mistakes:
  • Choosing matrix for sparse graphs
  • Confusing speed with memory use
  • Thinking adjacency list stores all edges