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

2.2 Dijkstra's Algorithm

The document describes Dijkstra's algorithm to find the shortest path between two nodes in a graph. It initializes a table with infinity distances for all nodes except the starting node X. It then iteratively updates the distances of adjacent unvisited nodes until all nodes are visited, building up the shortest path tree.

Uploaded by

Thuý Thanh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

2.2 Dijkstra's Algorithm

The document describes Dijkstra's algorithm to find the shortest path between two nodes in a graph. It initializes a table with infinity distances for all nodes except the starting node X. It then iteratively updates the distances of adjacent unvisited nodes until all nodes are visited, building up the shortest path tree.

Uploaded by

Thuý Thanh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

There are many possible paths that will allow us to reach node Y from X.

To find the shortest path


between two nodes, we will use the Dijkstra’s Algorithm.

When we start Dijkstra’s Algorithm, we


Vertex Shortest distance from Previous vertex
don’t even know if other vertices are
X
reachable, so the shortest distance from
X 0
X to every nodes is Infinity, ∞. A ∞
However, we do know the shortest B ∞
distance of one node X. Since we are C ∞
already at X, our start vertex, the D ∞
shortest distance is Zero, 0. E ∞
F ∞
After that update distance values of its G ∞
adjacent vertices. H ∞
I ∞
J ∞
Once we’re initialized our table, we’ll K ∞
need to start visiting vertices from our L ∞
start vertex, X. M ∞
N ∞
A way to keep trach of which nodes we O ∞
have or haven’t visited is with two P ∞
arrays. Q ∞
R ∞
Visited = [] S ∞
Unvisited = [X, A, B, C, D, E, F, G, H, I, J, K, T ∞
L, M, N, O, P, Q, R, S, T, U, V, Y, Z] U ∞
V ∞
Y ∞
Z ∞
Adjacent vertices of X are A and B. The distance values of A and B are updated as 2 and 8.

Vertex X is marked.

Pick up vertex with minimum distance value. The vertex A is picked and marked. The visited
array become [X, A]. Update the distance values of adjacent vertices of A
Adjacent vertices of A are B,M,L. The
distance of B,M,L are updated as 7,9,13.

Pick up vertex with minimum distance value. The vertex B is picked and marked. The visited array
become [X, A, B]. Update the distance values of adjacent vertices of B.

Adjacent vertices of B are C, N. The


distance of C, N are updated as
11,10.
Pick up vertex with minimum distance value. The vertex M is picked and marked. The visited array
become [X, A, B, M]. Update the distance values of adjacent vertices of M.

Adjacent vertices of M is V.
The distance of V is updated
as 15.
Pick up vertex with minimum distance value. The vertex N is picked and marked. The visited array
become [X, A, B, M, N]. Update the distance values of adjacent vertices of N.

Adjacent vertices of N are T and Z V.


The distance of T and Z are updated
as 18 and 12.

Pick up vertex with minimum distance value. The vertex C is picked and marked. The visited array
become [X, A, B, M, N, C]. Update the distance values of adjacent vertices of E and O.

Adjacent vertices of C are E and


O. The distance of E and O are
updated as 15 and 20.
Pick up vertex with minimum distance value. The vertex Z is picked and marked. The visited array
become [X, A, B, M, N, C, Z]. Update the distance values of adjacent vertices of U.

Adjacent vertices of Z
is U. The distance of
U is updated as 14.

We repeat the above steps until visit array includes all vertices of the given graph. And, finally we will
get the Shortest Path Tree like this.
From the graph above, it can be seen that the shortest path from X to Y in the Dijkstras Algorithm has
distance values is 26.

From here we can see two paths from X to Y and have the same distance of 26 are:

X ➡ A➡B➡N➡T➡H➡G➡Y (red)

=2+5+3+8+6+1+1 =26

X ➡ A➡B➡N➡Z➡U➡T➡H➡G➡Y (green)
=2+5+3+2+2+4+6+1+1=26

Vertex Shortest distance from Previous vertex


This is the table that we initialized, it X
records the shortest distance from the X X 0
to the nodes and vertex it passed A 2 X
before. B 7 A
C 11 B
D 21 E
E 15 C
F 33 D
G 25 H
H 24 T
I 26 J
J 21 U
K 17 V
L 13 A
M 9 A
N 10 B
O 20 C
P 25 O
Q 26 S
R 30 G
S 22 T
T 18 N and U(has same
value)
U 14 Z
V 15 M
Y 26 G
Z 12 N

You might also like