9.3 * Dijkstra`s Algorithm

9.3 – Dijkstra's Algorithm
- Used to solve the single-source shortest paths problem for
weighted connected graphs
→ Start at a vertex designated as the source
→ Find shortest paths to all other vertices
- Some uses for this problem:
Transportation planning
Packet routing of communication
iii. Searching social networks
iv. Speech Recognition
Dijkstra's Algorithm
- Applies to directed and undirected graphs with non negative
weights only
- Finds shortest paths to all vertices in order of distance from the
source vertex.
→ The weights of the edges in the graph are used as measure of
- A sub-tree of the graph is made based on the vertices already
visited by the algorithm.
- The set of vertices that are adjacent to the current sub-tree are
called “fringe vertices”
→ candidates the algorithm will choose the next element from
Dijkstra's Algorithm
- To determine the next closest vertex
→ a) For every fringe vertex, compute distance to nearest
vertex 'v' of the sub-tree
→ b) Then, add the length of the shortest distance from
the source vertex to 'v' to the value obtained in (a)
→ c) Fringe vertex with the lowest computed sum
becomes the next
Required Data
- Each vertex 'v' should hold two values
→ d – the shortest distance from the source node to vertex 'v'
→ a link to the parent node of the current vertex 'v'
- Once a fringe node 'u' has been identified as the next node in the
→ Move node 'u' from the fringe vertex list to the set of tree
→ For all remaining fringe nodes adjacent to node 'u' calculate if
the distance from the source node to 'u' + the distance from
'u' to the fringe node is less than the current distance
associated with the fringe node. If it is, replace the current
distance with the new distance
The Algorithm
ALGORITHM Dijkstra(G, s)
//Dijkstra’s algorithm for single-source shortest paths
//Input: A weighted connected graph G =
V, E with non-negative weights
// and its vertex s
//Output: The length dv of a shortest path from s to v
// and its penultimate vertex pv for every vertex v in V
Initialize(Q) //initialize priority queue to empty
for every vertex v in V
dv ←∞; pv ←null
Insert(Q, v, dv) //initialize vertex priority in the priority queue
ds ←0; Decrease(Q, s, ds) //update priority of s with ds
for i ←0 to |V| − 1 do
u∗ ←DeleteMin(Q) //delete the minimum priority element
VT ←VT ∪ {u∗}
for every vertex u in V − VT that is adjacent to u∗ do
If du∗ + w(u∗, u) < du
du ←du∗ + w(u∗, u); pu←u∗
Decrease(Q, u, du)
 For graphs represented by their weight matrix and the
priority queue implemented as an unordered array, it is in
 Θ(|V |2)
 For graphs represented by their adjacency lists and the
priority queue implemented as a min-heap, it is in
 O(|E| log |V |)

similar documents