Dijkstra's Algorithm
Dijkstra's algorithm solves the single-source shortest-path problem when all edges have non-negative weights. It is a greedy algorithm and similar to Prim's algorithm. Algorithm starts at the source vertex, s, it grows a tree, T, that ultimately spans all vertices reachable from S. Vertices are added to T in order of distance i.e., first S, then the vertex closest to S, then the next closest, and so on. Following implementation assumes that graph G is represented by adjacency lists.
DIJKSTRA (G, w, s)
1. INITIALIZE
SINGLE-SOURCE (G, s)
2. S ← {
} // S will ultimately contains vertices of final
shortest-path weights from s
3. Initialize
priority queue Q i.e., Q ← V[G]
4. while
priority queue Q is not empty do
5.
u ← EXTRACT_MIN(Q) // Pull out new vertex
6.
S ← S È {u}
// Perform relaxation for each vertex v adjacent to u
// Perform relaxation for each vertex v adjacent to u
7.
for each vertex v in Adj[u] do
8.
Relax (u, v, w)
Dijkstra's algorithm runs in O(|E|lg|V|)
time.
Comments
Post a Comment