Question bank › A-level › A-level Computer Science › Fundamentals of algorithms

Graph traversal and Dijkstra's shortest path

10 questions50 marksabout 60 min

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

1Hard6 marks

The weighted graph below shows the distances, in km, between six locations.

Dijkstra's algorithm is used to find the shortest route from A to every other node. At each step, the unfinalised node with the smallest working distance is permanently labelled (finalised), and the working distance of each of its unfinalised neighbours is updated if the route through the finalised node is shorter.

(a) State the order in which the nodes are permanently labelled (finalised). [2]

(b) State the length of the shortest path from A to F. [1]

(c) State the shortest path from A to F. [1]

(d) Explain why the working value for B changes from 4 to 3 during the algorithm. [1]

(e) State one real-world application of Dijkstra's shortest path algorithm. [1]

4215810263ABCDEF
2Hard6 marks

The undirected graph below is stored as an adjacency list. When choosing which neighbour to visit next, always choose the neighbour that is earliest alphabetically.

(a) Write the order in which nodes are visited in a breadth-first traversal starting at A. [2]

(b) Write the order in which nodes are visited in a depth-first traversal starting at A. [2]

(c) State the data structure used to hold nodes waiting to be visited in a breadth-first traversal. [1]

(d) State the data structure that a depth-first traversal uses, either explicitly or through recursion. [1]

ABCDEFG
3Core4 marks

Graph traversal algorithms and Dijkstra's algorithm each have typical applications.

(a) State one application of a breadth-first traversal. [1]

(b) State one application of a depth-first traversal. [1]

(c) State one real-world application of Dijkstra's shortest path algorithm. [1]

(d) Explain why both breadth-first and depth-first traversals must record which vertices have already been visited. [1]

Stuck on Graph traversal and Dijkstra's shortest path?

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

An undirected graph is stored as the adjacency list below. Neighbours are always processed in the order they appear in the list.

VertexAdjacent vertices
PQ, S
QP, R, T
RQ, U
SP, T
TQ, S, U, V
UR, T
VT

A breadth-first traversal starts at P. A vertex is added to the rear of a queue when it is first discovered and is visited when it is removed from the front of the queue.

(a) Write the order in which the vertices are visited. [2]

(b) Write the contents of the queue, front first, immediately after R has been removed from the queue and its undiscovered neighbours have been added. [1]

(c) State the minimum number of edges on a path from P to V and explain why a breadth-first traversal is guaranteed to find it. [1]

5Standard4 marks

The recursive subroutine below performs a depth-first traversal. Graph is a dictionary that maps each vertex to a list of its neighbours, and Visited is a list that is initially empty.

SUBROUTINE DFS(V)
  APPEND V TO Visited
  OUTPUT V
  FOR EACH W IN Graph[V]
    IF W NOT IN Visited THEN
      DFS(W)
    ENDIF
  ENDFOR
ENDSUBROUTINE

The graph is:

Graph = { 1: [2, 4],  2: [1, 3, 5],  3: [2, 6],
          4: [1, 5],  5: [2, 4, 6],  6: [3, 5] }

(a) Write the output when DFS(1) is called. [2]

(b) State the maximum number of calls to DFS that are active (on the call stack) at the same time during this traversal. [1]

(c) Explain what would happen if the selection statement IF W NOT IN Visited were removed. [1]

6Hard6 marks

The directed, weighted graph below shows one-way links in a network. The weights are transmission times in ms.

Dijkstra's algorithm is used to find the quickest route from S to every other vertex. At each step the unvisited vertex with the smallest current distance is visited, and the distances of its unvisited successors are updated if the route through it is shorter.

(a) Complete a table showing, for each vertex A, B, C, D and T, the final shortest distance from S and the previous vertex on that shortest route. [3]

(b) State the quickest route from S to T and its total time. [2]

(c) The algorithm is now run with T as the starting vertex. State the result for every other vertex and explain why. [1]

7234628139SABCDT
7Core4 marks

Dijkstra's algorithm has been run on a weighted graph starting from vertex A. The final values stored in two arrays are shown below.

VertexABCDEFGH
Distance032569812
Previous–AABCDEF

(a) State the shortest path from A to H. [1]

(b) State the weight of the edge between F and H. [1]

(c) Describe how a program could use the Previous array and a stack to output the shortest path from A to H in the correct order (starting with A). [2]

8Stretch4 marks

A directed graph has edges S→A with weight 4, S→B with weight 5 and B→A with weight −3.

Dijkstra's algorithm is run from S. Once a vertex has been visited (finalised) its distance is never changed.

(a) State the distance to A that Dijkstra's algorithm will output. [1]

(b) State the true shortest distance from S to A. [1]

(c) Explain why Dijkstra's algorithm does not give the correct answer for this graph. [2]

9Standard6 marks

Dijkstra's algorithm repeatedly selects the unvisited vertex that currently has the smallest known distance from the start vertex. For each unvisited neighbour of that vertex it calculates the distance to the neighbour through the selected vertex; if this is shorter than the neighbour's stored distance, it updates the stored distance and records the selected vertex as the neighbour's previous vertex.

The incomplete pseudo-code below implements this algorithm. Dist and Prev are arrays indexed by vertex, and Weight(X, Y) returns the weight of the edge from X to Y.

FOR EACH vertex N IN Graph
  Dist[N] ← INFINITY
  Prev[N] ← NULL
  ADD N TO Unvisited
ENDFOR
Dist[Start] ← 0
WHILE Unvisited IS NOT EMPTY
  Current ← vertex in Unvisited with  ___(i)___
  REMOVE Current FROM Unvisited
  FOR EACH neighbour N OF Current THAT IS IN Unvisited
    NewDist ← ___(ii)___
    IF ___(iii)___ THEN
      Dist[N] ← NewDist
      ___(iv)___
    ENDIF
  ENDFOR
ENDWHILE

(a) Write the missing code for (i), (ii), (iii) and (iv). [4]

(b) Explain why every value in Dist except the start vertex is initialised to INFINITY. [1]

(c) A data structure is often used to select Current efficiently. State the name of this data structure. [1]

10Hard6 marks

The diagram shows a maze of nine cells, A to I, seen from above. Thick lines are walls; a move can be made between two adjacent cells only if there is no wall between them.

The maze is to be represented as an undirected graph.

(a) State what the vertices and the edges of the graph represent. [1]

(b) A recursive depth-first traversal starts at A, always tries neighbours in alphabetical order and stops as soon as I is visited. Write the order in which the cells are visited. [2]

(c) State the minimum number of moves from A to I. [1]

(d) When I is reached, the route found by the depth-first traversal is the path of cells on the recursion stack. Using your answers to (b) and (c), explain why a breadth-first traversal would be chosen instead of a depth-first traversal to find the shortest route through this maze. [2]

ABCDEFGHI

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