Graph Algorithms
![Question 51, GATE 2010: A complete undirected graph has vertices 0,1,2,3,4 and symmetric weight matrix W, with rows/columns in that order: [[0,1,8,1,4],[1,0,12,4,9],[8,12,0,7,3],[1,4,7,0,2],[4,9,3,2,0]]. What is the minimum weight of a path from vertex 1 to vertex 2 containing at most three edges?
Options: (A) 7; (B) 8; (C) 9; (D) 10.](../../assets/questions/CS-2010-Q61-1.webp)
![Question 51, GATE 2010: A complete undirected graph has vertices 0,1,2,3,4 and symmetric weight matrix W, with rows/columns in that order: [[0,1,8,1,4],[1,0,12,4,9],[8,12,0,7,3],[1,4,7,0,2],[4,9,3,2,0]]. What is the minimum weight of a path from vertex 1 to vertex 2 containing at most three edges?
Options: (A) 7; (B) 8; (C) 9; (D) 10.](../../assets/questions/CS-2010-Q61-2.webp)
Answer & solution
Answer: B (no official key was published)
Step through it (14 steps)
The direct 1→2 path costs 12. With one intermediate vertex, costs via 0,3,4 are 9,11,12. With two distinct intermediate vertices, the six paths have costs: 1→0→3→2=9; 1→0→4→2=8; 1→3→0→2=13; 1→3→4→2=9; 1→4→0→2=21; 1→4→3→2=18. Thus minimum 8 is attained by 1→0→4→2, option B. All edge weights are positive, so adding a repeated-vertex cycle cannot improve a path within the edge limit.
An unconstrained shortest-path calculation is insufficient because it may return a path with four edges. Indeed 1→0→3→4→2 has weight 1+1+2+3=7 but violates the three-edge limit. Enumerating zero, one and two intermediate vertices is exhaustive for simple paths with at most three edges; the weight-eight candidate obeys the constraint.