TutorChase logo
Login
AQA A-Level Computer Science

12.6.3 Tracing Dijkstra’s algorithm step-by-step

Learn how to trace Dijkstra’s algorithm step-by-step by following clear, structured examples. Develop confidence through methodical practice using distance tables and labelled graphs.

What does it mean to trace an algorithm?

Tracing an algorithm involves following its internal logic and behaviour, step by step, while carefully monitoring how data changes at each stage. In the case of Dijkstra’s algorithm, tracing means:

  • Following how tentative distances to each node are updated.

  • Observing which nodes are visited and in what order.

  • Recording which nodes are selected and why.

  • Keeping track of the previous node for each current shortest path.

This practice is essential for developing a deep understanding of how the algorithm actually operates on a graph. Rather than memorising steps, students should aim to grasp how the algorithm behaves dynamically depending on the structure of the graph and the edge weights.

Essential components for tracing

Before attempting to trace Dijkstra’s algorithm, you must be familiar with the following elements, which are integral to each trace:

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

When two or more unvisited nodes have the same tentative distance, the algorithm allows for any of them to be chosen next. This situation typically arises when the graph contains multiple paths of equal total weight leading to different nodes. Dijkstra’s algorithm does not specify a unique tie-breaking rule, so the choice between equal-distance nodes may depend on how the algorithm is implemented (e.g. alphabetical order, order of insertion into a priority queue, etc.). Importantly, choosing any of these equally distanced nodes does not affect the correctness of the final distances or the algorithm’s guarantee to find the shortest paths, because all visited nodes are updated based on minimum distances at each step. However, if there are multiple shortest paths to a node with the same total weight, the specific previous node recorded might vary depending on which path is taken first. This means the reconstructed path may differ, but it will always be correct in terms of overall distance.

Yes, Dijkstra’s algorithm can handle graphs that include unreachable nodes, and this should be reflected clearly when tracing. If a node cannot be reached from the starting node — meaning there is no path connecting it either directly or indirectly — its tentative distance will remain at infinity throughout the trace. It will also never be visited, because the algorithm always chooses the unvisited node with the lowest tentative distance, and infinity is never the lowest value. This makes it easy to identify unreachable nodes during a trace, and they should be clearly marked as such in your distance table. If you're asked to trace the algorithm and provide final distances, be sure to include unreachable nodes and note that their distances are still infinity and that no path exists to reach them. These cases often appear in exam questions to test whether students understand how Dijkstra’s algorithm deals with disconnected graphs.

Recording the previous node for each visited node is a key part of tracing Dijkstra’s algorithm because it allows the shortest path to be reconstructed once the algorithm has completed. Without this, you would only know the minimum distance to each node, but not how to actually travel from the start node to that destination. During the algorithm, whenever a node’s tentative distance is updated because a shorter path is found, the current node is set as the previous node for that neighbour. This tracking forms a chain of references from each node back to the source. When the trace is finished, you can reconstruct the full shortest path to any node by following the chain of previous nodes in reverse. For example, to find the shortest path from A to E, you might go from E to D to B to A if those were the previous nodes, and then reverse the path to get the correct order.

In a directed graph, you stop tracing Dijkstra’s algorithm using the same rule as in an undirected graph: once all nodes have been visited, or if you are tracing to a specific target node, once the target has been marked as visited. The direction of the edges matters only in that some connections may be one-way — this affects which neighbours are considered when visiting a node. For instance, if there is an edge from A to B but not from B to A, then when visiting B, A will not be treated as a neighbour. During tracing, you must only consider outgoing edges from the current node when checking for neighbours. Directed graphs are particularly useful in modelling real-world problems like road networks with one-way streets or dependency chains in scheduling. If a node is unreachable due to edge direction, it will remain at infinity and unvisited, just as in undirected graphs with disconnected components.

The fundamental logic of tracing Dijkstra’s algorithm remains the same regardless of whether a priority queue or a basic list is used to select the next node. However, the efficiency and clarity of the tracing process can change. In a priority queue implementation, nodes are automatically ordered based on their current tentative distances, so retrieving the node with the smallest distance is very efficient — this makes tracing smoother and reduces errors in node selection. In contrast, when using a basic list, you must manually scan the entire list of unvisited nodes at each step to find the one with the smallest tentative distance. This manual process can be more error-prone during a trace, especially in graphs with many nodes. For exam tracing, students typically use a list-based approach and pick the lowest-distance node by inspection. While this works for small graphs, understanding the role of priority queues helps explain why the algorithm performs better on large-scale data.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email