Complexity | Note | |
---|---|---|
Worst Time: | O(E log V + V log V) | For every vertex V and edge E: heap operations take log V |
Worst Space: | O(V + E) | Assuming an Adjacency List is used |
Exactly like Breadth-First Search, but applied to finding the shortest distances between vertices. This is designed for weighted graphs.
The following algorithm can be used for searching a single vertex and a shortest path, by modifying it to stop as soon as the target is finalised. And then, the shortest path from the source to the target can be recovered easily by building a list of pointers from the start to end.
- Initialise a heap called
Discovered
and insert the starting vertex in it with distance0
- Initialise a list called
Finalised
to store the distances from the source to every finalised vertex - While
Discovered
is not empty:- Pop the root of the heap (this is the vertex
V
with the smallest distance from the source) - For each outgoing edge
(V, U, weight)
ofV
- If
U
is not inDiscovered
- Insert
U
inDiscovered
with the distancedistance of U from V
+distance of V from source
- Insert
- Else, if it is in
Discovered
and ifU
is not finalised,- Update the distance of
U
with the smaller distance: eitherdistance of U from V
+distance of V from source
or leave it be
- Update the distance of
- If
- Move
V
fromDiscovered
toFinalised
- Pop the root of the heap (this is the vertex
- Suppose S → T represents the shortest path from S to T
- Let
U
be the last vertex on the shortest path S → T- Shortest path S from S → U must be a part of S → T
- The shortest path distance of S → U is smaller than S → T
- Since S → U is smaller than S → T, U is finalised before T
- Therefore, the shortest path from S → T can be obtained by greedily extending previously known shortest paths