Embed presentation
Download to read offline

![Example 02:
Solution
Visited
Vertex
A B C D E F
A 0 ∞ ∞ ∞ ∞ ∞
B 2 5 ∞ ∞ 11
C 5 7 15 11
D 7 15 11
F 15 11
E 15
if(d(u) + c(u,v) < d(v))
then d(v) = d(u) + c(u,v)
Shortest path from A to E: A, B, E
Distance = 2 + 13 = 15
Dijkstra’s Algorithm Pseudocode
function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
A
F D
B
C
E
11
17
1
12
5
2
8 13
5](https://image.slidesharecdn.com/dijkstraalgorithm-210706144230/85/Dijkstra-algorithm-2-320.jpg)
![distance[S] <- 0
while Q IS NOT EMPTY
U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]](https://image.slidesharecdn.com/dijkstraalgorithm-210706144230/85/Dijkstra-algorithm-3-320.jpg)

Dijkstra's algorithm is used to find the shortest path between nodes in a weighted graph. It works by assigning initial distances to all nodes from the starting node and updating them as shorter paths are found, extracting the node with the lowest distance, and updating distances for neighboring unvisited nodes. The algorithm returns the shortest distance and path between the starting node and all other nodes in the graph.

![Example 02:
Solution
Visited
Vertex
A B C D E F
A 0 ∞ ∞ ∞ ∞ ∞
B 2 5 ∞ ∞ 11
C 5 7 15 11
D 7 15 11
F 15 11
E 15
if(d(u) + c(u,v) < d(v))
then d(v) = d(u) + c(u,v)
Shortest path from A to E: A, B, E
Distance = 2 + 13 = 15
Dijkstra’s Algorithm Pseudocode
function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
A
F D
B
C
E
11
17
1
12
5
2
8 13
5](https://image.slidesharecdn.com/dijkstraalgorithm-210706144230/85/Dijkstra-algorithm-2-320.jpg)
![distance[S] <- 0
while Q IS NOT EMPTY
U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]](https://image.slidesharecdn.com/dijkstraalgorithm-210706144230/85/Dijkstra-algorithm-3-320.jpg)