Dijkstra's AlgorithmIt is a greedy algorithm that solves the single-source shortest path problem for a directed graph G = (V, E) with nonnegative edge weights, i.e., w (u, v) ≥ 0 for each edge (u, v) ∈ E. Dijkstra's Algorithm maintains a set S of vertices whose final shortest - path weights from the source s have already been determined. That's for all vertices v ∈ S; we have d [v] = δ (s, v). The algorithm repeatedly selects the vertex u ∈ V - S with the minimum shortest - path estimate, insert u into S and relaxes all edges leaving u. Because it always chooses the "lightest" or "closest" vertex in V - S to insert into set S, it is called as the greedy strategy. Dijkstra's Algorithm (G, w, s) 1. INITIALIZE - SINGLE - SOURCE (G, s) 2. S←∅ 3. Q←V [G] 4. while Q ≠ ∅ 5. do u ← EXTRACT - MIN (Q) 6. S ← S ∪ {u} 7. for each vertex v ∈ Adj [u] 8. do RELAX (u, v, w) Analysis: The running time of Dijkstra's algorithm on a graph with edges E and vertices V can be expressed as a function of |E| and |V| using the Big - O notation. The simplest implementation of the Dijkstra's algorithm stores vertices of set Q in an ordinary linked list or array, and operation Extract - Min (Q) is simply a linear search through all vertices in Q. In this case, the running time is O (|V2 |+|E|=O(V2 ). Example: ![]() Solution: Step1: Q =[s, t, x, y, z] We scanned vertices one by one and find out its adjacent. Calculate the distance of each adjacent to the source vertices. We make a stack, which contains those vertices which are selected after computation of shortest distance. Firstly we take's' in stack M (which is a source) Step 2: Now find the adjacent of s that are t and y. ![]() Case - (i) s → t Case - (ii) s→ y By comparing case (i) and case (ii) ![]() Step 3: Now find the adjacent of y that is t, x, z. Case - (i) y →t Case - (ii) y → x Case - (iii) y → z By comparing case (i), case (ii) and case (iii) ![]() Step - 4 Now we will find adj [z] that are s, x Case - (i) z → x Case - (ii) z → s ![]() Step 5: Now we will find Adj [t] Adj [t] → [x, y] [Here t is u and x and y are v] Case - (i) t → x Case - (ii) t → y ![]() Thus we get all shortest path vertex as Weight from s to y is 5 These are the shortest distance from the source's' in the given graph. ![]() Disadvantage of Dijkstra's Algorithm:
Next TopicBellman-Ford Algorithm
|