You are on page 1of 2

L25: Dijkstra’s Algorithm CSE332, Spring 2020

Dijkstra’s Algorithm: Example #2


0 
2 B
A 1
1 
5
2  E
1
 1 D
C 5 3

2 6 G Vertex Known? Distance Previous
 10
A 
F
B 
C 
D 
Order Added to Known Set: E 
F 
G 

1
L25: Dijkstra’s Algorithm CSE332, Spring 2020

Runtime, First Approach


dijkstra(Graph g, Vertex start) {
foreach vertex v in g:
v.distance = 
v.known = false
start.distance = 0

while there are vertices in g


that are not known:
select vertex v with lowest cost
v.known = true
foreach unknown edge (v, u) in g:
d1 = v.distance + g.weight(v, u)
d2 = u.distance
if (d1 < d2)
u.distance = d1
u.previous = v
}
Total: 2

You might also like