Lecture 14: Dijkstra's Shortest Path Algorithm Flashcards
Why we can’t simply use our current BFS algorithm to locate the shortest path?
BFS only works for the shortest path in an unweighted graph
Like MST, the shortest path problem doesn’t change when edges have negative weights?
This is sometimes true when the edges have negative weights
N.B: need more info
Single Source Shortest Path Algorithm assumptions
- edge weights are positive: our algo ONLY works if edge weights are positive. For negative edge weights, there are other algorithms.
- Connected: making things easier, we easily code around this otherwise
Dijkstra’s Shortest Path Algorithm and runtime analysis
A modification of prims - instead of picking the smallest edge weight next, Dijkstra picks the edge that minimizes the total cost
- Initialize the cost of each vertex to infinity
- initialize the cost of the source vertex to 0
- WHILE undiscovered vertices are left in the graph
a. Select undiscovered vertex U with the lowest cost and mark as discovered
b. FOR EACH vertex V adjacent to u
V’s cost = min(V’s old cost, U’s cost + cost of (U, V))
C. Mark U as processed - END-WHILE
Analysis
- Since Dijkstra relies on our current version of Prim’s algorithm as its backbone, we can guess O(V^2)
- The bottleneck is finding the next minimum const vertex to explore for every vertex O(V^2)
- We will see some tricks to make this faster later on
Dijkstra’s comparison to prims
Finds the shortest path <-> finds MST
Undirected & Directed <-> Undirected
No negative edge weights <-> handles negative edge weights