0% found this document useful (0 votes)
49 views

Dijkdtra Algorithm

This document describes Dijkstra's algorithm for finding the shortest paths between nodes in a graph. It provides an example graph with 7 nodes labeled A through G and weighted edges. The algorithm works by first setting the distance and previous node for each node to infinity and unknown respectively, except the starting node which is set to 0. It then repeatedly selects the closest unknown node, marks it as known, and updates the distances and previous nodes for any unknown neighbors if a shorter path is found through the known node.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Dijkdtra Algorithm

This document describes Dijkstra's algorithm for finding the shortest paths between nodes in a graph. It provides an example graph with 7 nodes labeled A through G and weighted edges. The algorithm works by first setting the distance and previous node for each node to infinity and unknown respectively, except the starting node which is set to 0. It then repeatedly selects the closest unknown node, marks it as known, and updates the distances and previous nodes for any unknown neighbors if a shorter path is found through the known node.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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