Dijkstra's algorithm is a greedy algorithm that finds the shortest path between two vertices of a connected, weighted graph. The algorithm uses two data structures a graph representing the input and a min priority queue which is used to find the vertex with the minimum tentative distance.
The algorithm performs the following steps:
- Initialize a graph distance table by assigning every vertex an initial distance of infinity each entry in the table has the form | distance | vertex | prev |, prev is set to
null to start.
- Set the distance of the source vertex (the one we start at) to zero.
- Create a min-priority queue containing all vertices, keyed by their current tentative distances.
- While the priority queue is not empty:
- Remove the vertex with the smallest tentative distance (using
extractMin).
- For each outgoing edge from that vertex:
- Compute the distance to the neighbouring vertex through the current vertex.
- If this distance is smaller than the previously recorded distance (the one in the distance table)
- Update the neighbour’s distance.
- Update the neighbour’s position in the priority queue, and set it's previous vertex to be the current vertex