Data Structures and Algorithms
In an undirected graph, the edge weights (distances) are S−A: 4, S−B: 1, A−B: 2, A−C: 1, B−C: 5, C−T: 3 and A−T: 6. Which shows the shortest path from S to T found by Dijkstra's algorithm and its length?
- AS→A→C→T, length 8
- BS→B→A→C→T, length 7
- CS→B→C→T, length 9
Show answer and explanation
Answer: B. S→B→A→C→T, length 7
Dijkstra's algorithm repeatedly finalizes the nearest vertex whose distance is not yet final and updates the distances of its neighbors. From S (0), B is finalized at 1, and going through B gives A a distance of 1 + 2 = 3, shorter than the direct 4. Finalizing A (3) gives C 3 + 1 = 4 and T 3 + 6 = 9, and finalizing C (4) updates T to 4 + 3 = 7. The shortest path is therefore S→B→A→C→T with length 7. S→A→C→T (8) is longer because it takes S−A directly, and S→B→C→T (9) is longer because B−C has the large weight 5.
- A. Going from S to A through B (1 + 2 = 3) is shorter than going directly (4).
- B. Correct. S→B (1), B→A (2), A→C (1) and C→T (3) total 7.
- C. B−C has a large weight of 5, so going through A is shorter.