GATE CS 2025 Set 2 | Question 59

Algorithms › Graph Algorithms · NAT · 2 marks · Answer: 6

  1. Question 59Algorithmsnat2 marks
    Consider the algorithm someAlgo(G): (1) run BFS from any vertex , let be a farthest vertex; (2) run BFS from , let be a farthest vertex; (3) output the distance between and . For the tree shown in the figure, the output of someAlgo() is ___________. (Answer in integer)
    GATE CS 2025 Set 2 Question 59 figure

    Answer: 6

    Solution

    Answer: 6 The double-BFS procedure is the standard algorithm for the diameter of a tree: the farthest vertex from any start is always an endpoint of a longest path, and BFS from reaches the other endpoint , so the returned distance equals the tree's diameter. Tracing the longest path in the given tree gives a diameter of edges, so the output is .

More GATE PYQ on Graph Algorithms

All GATE CS Algorithms PYQ →

Attempt GATE CS 2025 Set 2 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →