Question bank › A-level › A-level Computer Science › Fundamentals of data structures

Graphs (adjacency matrix and list)

10 questions41 marksabout 50 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Standard5 marks

The diagram shows a weighted, undirected graph.

(a) The graph is to be stored as an adjacency matrix. Complete rows C, D and E of the matrix, using 0 where there is no edge. [2]

ABCDE
A04200
B405100
C
D
E

(b) Explain why an adjacency list might be more suitable than an adjacency matrix for storing a graph with many vertices but few edges. [2]

(c) Explain why this graph is not a tree. [1]

4251034ABCDE
2Core3 marks

A graph is an abstract data type that can be used to represent complex relationships.

(a) State what is meant by a weighted graph. [1]

(b) Explain the difference between a directed graph and an undirected graph. [1]

(c) A website stores which pages contain hyperlinks to which other pages. State, with a reason, whether a directed or an undirected graph should be used to model this. [1]

3Core3 marks

The diagram shows a directed, unweighted graph.

(a) Complete the adjacency list for this graph. The entry for vertex P has been done for you. [2]

VertexAdjacent vertices
PQ, R
Q
R
S
T

(b) The same graph is to be stored as an adjacency matrix. Explain why the matrix will not be symmetrical about its leading diagonal. [1]

PQRST
Stuck on Graphs (adjacency matrix and list)?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Standard4 marks

The adjacency matrix below represents an unweighted graph. A 1 in row X, column Y means there is an edge from vertex X to vertex Y.

ABCDE
A01100
B00010
C01011
D00000
E10010

(a) State, with a reason based on the matrix, whether the graph is directed or undirected. [1]

(b) State how many edges the graph has. [1]

(c) Identify the vertex from which no other vertex can be reached. [1]

(d) Identify the vertex that has the most edges leading into it. [1]

5Standard3 marks

An airline flies between 12 airports. Almost every pair of airports has a direct flight between them. The booking system frequently needs to check whether a direct flight exists between two given airports.

Explain why an adjacency matrix would be a more suitable way to store this graph than an adjacency list. [3]

6Standard4 marks

A satellite navigation system models a road network as a weighted, undirected graph. There are 50 000 junctions (vertices) and 70 000 two-way roads (edges).

(a) The graph is stored as an adjacency matrix in which every cell holds a 2-byte value. Calculate the memory needed for the matrix. Give your answer in gigabytes, where 1 GB = $10^9$ bytes. [2]

(b) Instead, the graph is stored as an adjacency list. Each entry in the list holds a 4-byte vertex number and a 2-byte weight. Calculate the memory needed for all of the entries in the adjacency list, in bytes. Ignore any memory used for the list headers. [2]

7Hard5 marks

A student has written the following adjacency list for a weighted, undirected graph. Each pair shows (adjacent vertex, weight).

F: (G,7), (H,3)
G: (F,7), (J,2), (K,6)
H: (F,3), (J,4)
J: (G,2), (H,5), (K,1)
K: (G,6), (J,1)

(a) The list contains one error. Identify the error and explain how you know it is an error. [2]

The error is corrected so that the edge has weight 4.

(b) State the number of edges in the graph. [1]

(c) The graph is to be stored as an adjacency matrix with rows and columns in the order F, G, H, J, K, using 0 to show no edge. Write down row J of the matrix. [1]

(d) Calculate the total weight of all the edges in the graph. [1]

8Hard4 marks

The subroutine ShowList uses an adjacency matrix M for a weighted graph with four vertices. A value of 0 means there is no edge. The array Names holds the vertex names. Arrays are indexed from 0.

SUBROUTINE ShowList(M, Names)
  FOR i ← 0 TO 3
    Line ← Names[i] + ':'
    FOR j ← 0 TO 3
      IF M[i][j] > 0 THEN
        Line ← Line + ' ' + Names[j] + '(' + IntToString(M[i][j]) + ')'
      ENDIF
    ENDFOR
    OUTPUT Line
  ENDFOR
ENDSUBROUTINE

The subroutine is called with

M ← [[0, 5, 0, 2],
     [5, 0, 8, 0],
     [0, 8, 0, 3],
     [2, 0, 3, 0]]
Names ← ['W', 'X', 'Y', 'Z']

(a) Write down the four lines that are output. [3]

(b) The subroutine is changed so that it works for a graph with $n$ vertices. State, in terms of $n$, how many times the condition M[i][j] > 0 is evaluated. [1]

9Stretch4 marks

A weighted graph with n vertices is stored in a two-dimensional array M, indexed from 0, as an adjacency matrix.

Write an algorithm, in pseudo-code or a programming language, for a function IsUndirected(M, n) that returns True if the matrix could represent an undirected graph and False otherwise. [4]

10Stretch6 marks

A social media platform has 2 000 000 accounts. An account can follow another account; following is not necessarily returned. On average, each account follows 200 other accounts. The relationships are to be stored as a graph.

(a) Explain why the graph should be directed. [1]

(b) Calculate the number of cells in an adjacency matrix for this graph, and the percentage of those cells that would show an edge. [2]

(c) Using your answers to part (b), evaluate whether an adjacency matrix or an adjacency list should be used to store this graph. [3]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session