Shortest path
In a weighted graph or network, travelling becomes more complex as optimising resources, such as time or cost, must be considered. This is where finding the shortest path plays a critical role in determining the most efficient route between vertices.
Finding Shortest Path
Finding the shortest path in a network can sometimes be done by inspection, particularly when the network is simple. However, as the complexity of the network increases, a more systematic approach is required. One such method is Dijkstra's algorithm, which efficiently identifies the shortest path between vertices.
Dijkstra’s Algorithm
Dijkstra’s algorithm is a method of finding the shortest path. The steps involved are:
- Assign a starting value of \(0\) to the starting vertex.
- Compare the distance from the starting vertex to all unvisited neighbouring vertices, and next to each of these vertices calculate the value required to reach it from the value of the visited vertex.
- Mark the lowest calculation out of all the unvisited neighbouring vertices as visited. This can be done by drawing a box around the visited vertex.
- From the newly visited vertex, calculate the value required to reach the unvisited neighbouring vertices.
- If the calculation is less than the previous value, then update the value to that vertex
- If the calculation is more than the previous value, then leave the previous value on the vertex.
- Repeat steps 3 and 4 until the end vertex is visited.
- Backtrack from the end vertex to the start to determine the shortest path.
Worked Example
A delivery company needs to find the shortest path from their main warehouse \(A\) to delivery hub \(G\). The network which describes the distances, in km, between the hubs is shown below. Using Dijkstra’s algorithm, find the shortest path from the warehouse \(A\) to delivery hub \(G\). Provide the total distance and the path taken.
To find the shortest path we begin backtracking. This means taking the value from the marked value and subtracting as we move through the network.
From \(G-E\):
\[7-4\neq5\]
From \(G-F\):
\[7-5\neq3\]
From \(G-D\):
\[7-1=6\]
Therefore the first part of the backtracking is from \(G\) to \(D\). This process is repeated until the path is found.
\(A-B-F-D-G\) is the shortest path with a distance of \(7km\).
