Djikstra's algorithm can also tell you the shortest paths from one vertex of a graph to all the other vertices, so I'll describe that variant here.
1.
Initial vertex: A
unvisited set of vertices = {B,C}
initial distances (haven't checked any of the other vertices, so don't actually know what these distances are yet except that A is length 0 from itself):
A to A = 0
A to B = infinity
A to C = infinity
mark A as visited
check adjacent vertices:
B and C are adjacent
A to B = 5
A to C = 8
for right now, we know 2 new things:
shortest known path is going from A directly to C (A -> C)
shortest known path from A to B is A -> B
2.
Now move on to closest vertex, which is B (5 < 8)
mark B as visited
unvisited set = {C}
C is only unvisited neighbor, so look at B to C
B to C has length 3, so compare to current shortest path, which is A -> C and has length 8.
length through B from A to C is 5+3 is not < 8, so we keep the earlier shortest path A -> C. If d(B,C) was (for instance) 2 instead, we'd update the shortest path to be A -> B -> C.
Done with vertex B.
3.
Now move on to vertex C.
mark C as visited.
C has no unvisited neighbors (since A and B were already visited), so don't have to do anything more here.
No more vertices to check. Done
Results:
shortest path from A to C is A -> C, length 8
shortest path from A to B is A -> B, length 5
TutorKelvin D.
Amazing.01/03/24