Professional Documents
Culture Documents
Dijkastra Algorithm 34
Dijkastra Algorithm 34
College of E&ME,
National University of Sciences and Technology,
Islamabad, Pakistan.
The closest node to s, say x, must be adjacent to s and the next closest
node, say y, must be either adjacent to s or x. The third closest node to
s must be either adjacent to s or x or y, and so on.
INITIALIZE-SINGLE-SOURCE (G, s)
1. for each vertex v V
2. do d[v]
3. [v] NIL
4. d[s] = 0
After initialization, [v] =NIL, d[v] = 0 for v = s and d[v] = for all
v V s
RELAX (u, v, w)
1. if d[v] > d[u] + w(u, v)
2. then d[v] d[u] + w(u, v)
3. [v] u
DIJKSTRA (G, w, s)
1 INITIALIZE-SINGLE-SOURCE (G, s) (distance to source vertex is zero)
(set all other distances to infinity)
2 S (S, the set of visited vertices is initially empty)
3 Q V [G] (Q, the queue initially contains all vertices)
4 while Q 6= (while the queue is not empty)
5 do u EXT RACT M IN (Q) (select the element of Q with the min. distance)
6 S S {u} (add u to list of visited vertices)
7 for each vertex v Adj[u]
8 do RELAX(u, v, w) (if new shortest path found)
(set new value of shortest path)