Académique Documents
Professionnel Documents
Culture Documents
2 u.d = time
3 u.color = GRAY
4 for each v G.Adj [u]
5 if v.color == WHITE
6 v. = u
7 DFS-VISIT (G,v)
8 u.color = BLACK
// blacken u; it is finished
9 time = time + 1
10 u.f = time
8.
9.
10.
if priority[u] > w
// New priority.
priorityQueue.decreaseKey (u, w)
predecessor[u] = v
11.
endif
12.
13.
14.
endfor
A=A U {v}
endwhile
Priority queue contains all the vertices priority pairs of the graph.
Initially all the priorities are set to infinity and a source is chosen at
random and its priority is set to zero. Here extractmin de-queues the
minimum key from the priority key .its complexity is O(log(n)).
Dijkstra's Algorithm for SPT using priority key :
It is similar to prim s algorithm with ony slight change in the pseudo
code . Here is the pseudo code :
Algorithm: Dijkstra-SPT (G, s)
Input: Graph G=(V,E) with edge weights and designated source
vertex s.
1.
2.
Here also the operations of priority key are same as that for prims
algorithm. Here, we see the only difference is in line 9 and 10 were we
compare the priority of vertex with edge weight plus the priority of the
neighbor and update accordingly. The resulting tree is shortest path
tree (SPT). This is Djikshtra s algorithm.
Complexity Analysis of prims, kruskals and Djikshtras algorithm:
E is the number of edges and V is the number of vertex in G(V,E).
Prims algorithm with priority Queue :
Kruskal s algorithm :
Reference :
Introduction to algorithm by Cormen