Professional Documents
Culture Documents
CS 332: Algorithms: S-S Shortest Path: Dijkstra's Algorithm Disjoint-Set Union Amortized Analysis
CS 332: Algorithms: S-S Shortest Path: Dijkstra's Algorithm Disjoint-Set Union Amortized Analysis
Review:
Single-Source Shortest Path
Problem: given a weighted directed graph G,
Review: Relaxation
Key technique: relaxation
Maintain upper bound d[v] on (s,v):
Relax(u,v,w) {
if (d[v] > d[u]+w) then d[v]=d[u]+w;
}
5
Relax
5
Relax
7
Review: Bellman-Ford
Running time: O(VE)
Not so good for large dense graphs
But a very practical algorithm in many ways
Note that order in which edges are processed affects
Dijkstras Algorithm
If no negative edge weights, we can beat BF
Similar to breadth-first search
Grow a tree gradually, advancing from vertices
Dijkstras Algorithm
Dijkstra(G)
B
2
10
for each v V
4
3
A
D
d[v] = ;
d[s] = 0; S = ; Q = V;
5
1
C
while (Q )
u = ExtractMin(Q); Ex: run the algorithm
S = S U {u};
for each v u->Adj[]
if (d[v] > d[u]+w(u,v))
Relaxation
Note: this
d[v] = d[u]+w(u,v);
Step
is really a
call to Q->DecreaseKey()
The End