Need help from an expert?
The world’s top online tutoring provider trusted by students, parents, and schools globally.
Dijkstra's algorithm is a method for finding the shortest path between two nodes in a graph.
Dijkstra's algorithm works by starting at the source node and assigning a tentative distance to all other nodes in the graph. The tentative distance is initially set to infinity for all nodes except the source node, which is set to 0. The algorithm then selects the node with the smallest tentative distance and examines all of its neighbours. For each neighbour, the algorithm calculates the distance from the source node through the current node and updates the tentative distance if it is smaller than the previous value. This process is repeated until the destination node is reached or all nodes have been visited.
To illustrate the algorithm, consider the following graph:

Suppose we want to find the shortest path from node A to node F. We start by assigning tentative distances to all nodes:
| Node | Tentative Distance |
|------|--------------------|
| A | 0 |
| B | ∞ |
| C | ∞ |
| D | ∞ |
| E | ∞ |
| F | ∞ |
We then select node A and examine its neighbours. The distance from A to B is 2, so we update the tentative distance for node B:
| Node | Tentative Distance |
|------|--------------------|
| A | 0 |
| B | 2 |
| C | ∞ |
| D | ∞ |
| E | ∞ |
| F | ∞ |
We repeat this process for node B, selecting node E as the next node with the smallest tentative distance. The distance from B to E is 3, so we update the tentative distance for node E:
| Node | Tentative Distance |
|------|--------------------|
| A | 0 |
| B | 2 |
| C | ∞ |
| D | ∞ |
| E | 5 |
| F | ∞ |
We continue in this way, selecting nodes C and D next, until we reach node F:
| Node | Tentative Distance |
|------|--------------------|
| A | 0 |
| B | 2 |
| C | 5 |
| D |
Study and Practice for Free
Trusted by 100,000+ Students Worldwide
Achieve Top Grades in your Exams with our Free Resources.
Practice Questions, Study Notes, and Past Exam Papers for all Subjects!
The world’s top online tutoring provider trusted by students, parents, and schools globally.