Dijkstra's Algorithm¶
Problem Statement¶
The goal of Dijkstra's algorithm is to find all the shortest paths connecting a source vertex
Approach¶
We will divide the graph into two components,
Observations¶
- If we know the distance to every point in some region
, we can tell with certainty the shortest distance to one and only one point in with absolute certainty. - This point is the one with the minimum value of shortest distance to any point in
, i.e. the closest point to . - Any other point may have a shorter path to it from
, which flows through this optimal path. - At the start, the optimal path is only known for the source.
Pseudo-Code¶
set distance of each vertex from source to +inf.
create a min heap H with all vertices in, measured by their distance.
while H is not empty,
frontier vertex f = delete_min(H). - O(V. log V)
for all outgoing edges from (fv) to S', - O(E. log V)
relax d[v] till d[f] + l[fv].
update the heap to reflect this change. - O(log V)
Runtime¶
We run heapify every time a point is moved into the frontier. As this happens no more than
We check each edge once for potential relaxation and heap reset. So the runtime of this part is
Proof Of Correctness¶
This proof is performed via induction.
At any iteration, suppose
Base Case -
Induction Hypothesis - Suppose we have
Suppose an alternate shorter path