GateAI
Graph Traversals

GATE CSE 2025 Set 2 · Q59

NAT2 marksMedium

Time

Consider the following algorithm someAlgo that takes an undirected graph GG as input. someAlgo(G) 1. Let vv be any vertex in GG. Run BFS on GG starting at vv. Let uu be a vertex in GG at maximum distance from vv as given by the BFS. 2. Run BFS on GG again with uu as the starting vertex. Let zz be the vertex at maximum distance from uu as given by the BFS. 3. Output the distance between uu and zz in GG. The output of someAlgo(T) for the tree shown in the given figure is ____________ . (Answer in integer)

GATE CSE 2025 Set 2 Q59 diagram

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 6

Graph Traversals

The algorithm someAlgo is the standard two-BFS/DFS algorithm used to find the diameter of a tree (i.e., the length of the longest path between any two vertices in the tree).

Let us find the longest simple path in the given tree:

  1. Starting from the bottom-leftmost leaf, we traverse up through the left branch to the central branch and then along the rightmost branch to the rightmost leaf.

  2. The sequence of vertices along the longest path contains 77 vertices and hence 66 edges:

    Bottom-left leaf→degree-2 node→junction node→center node→junction node→node→rightmost leaf\text{Bottom-left leaf} \to \text{degree-2 node} \to \text{junction node} \to \text{center node} \to \text{junction node} \to \text{node} \to \text{rightmost leaf}
  3. Counting the number of edges along this longest path:

    Distance=6\text{Distance} = 6

Therefore, the diameter of the tree is 6.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Graph Traversals previous year question is Q59 of the GATE CSE 2025 Set 2 paper. Find more GATE CSE PYQs on Graph Traversals in Algorithms, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Graph Traversals PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Graph Traversals in GATE CSE

Graph Traversals has 16 GATE CSE PYQs across 12 papers from 2014 to 2026. This is the only 2-mark NAT question among them, and GATE CSE 2025 Set 2 has 2.

How GATE tests Graph Traversals

More from GATE CSE 2025 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 2 · Q59 · NAT

Consider the following algorithm someAlgo that takes an undirected graph G as input. someAlgo(G) 1. Let v be any vertex in G. Run BFS on G starting at v. Let u be a vertex in G at maximum distance from v as given by the BFS. 2. Run BFS on G again with u as the starting vertex. Let z be the vertex at maximum distance from u as given by the BFS. 3. Output the distance between u and z in G. The output of someAlgo(T) for the tree shown in the given figure is … (Answer in integer)

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.