Question Details

Consider the following algorithm someAlgo that takes an undirected graph G as input.
someAlgo (G)


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.


Run BFS on G again with u as the starting vertex. Let z be the vertex at maximum distance from uu as given by the BFS.


Output the distance between u and z in G.
The output of someAlgo(T) for the tree shown in the given figure is ______.

Show Answer
Correct Answer
6

Solution :

The correct answer is 6.

Understanding the Algorithm
The given algorithm, someAlgo(G), is a well-known, efficient two-pass Breadth-First Search (BFS) algorithm used to find the diameter of a tree. The diameter of a tree is defined as the length of the longest path (in terms of the number of edges) between any two vertices in the tree.

Let's break down the steps of the algorithm:

1. First BFS: Start at an arbitrary vertex
v
and find the farthest vertex
u
from
v
using BFS.

2. Second BFS: Start a new BFS from
u
and find the farthest vertex
z
from
u
using BFS.

3. Output: The shortest path distance between
u
and
z
which corresponds to the diameter of the tree.

Why the Algorithm Works on a Tree
For any tree
T
this algorithm is guaranteed to find the correct diameter because of the following property:
- The node
u
found during the first BFS starting from any arbitrary node
v
is guaranteed to be one of the endpoints of a longest path (diameter) of the tree.
- The subsequent BFS starting from the endpoint
u
will traverse to the other endpoint
z
of the diameter path.

Since the correct answer is 6, the diameter of the tree shown in the figure is 6, meaning the longest path between any two nodes in the tree consists of exactly 6 edges.

Transparent Access on Mindyard

Built for Learners & Educators Alike

Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.

FREE FOR STUDENTS

Practice & Learn Free for Students

Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.

  • Unlimited mock tests & question bank practice
  • Full access to curated study library & formula notes
  • 100% free for students • No credit card required
2 MONTHS FREE TRIAL

Educators & Coaching Institutes

Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.

  • AI question uploader & rapid test generator
  • Anti-cheating tab proctoring & performance reports
  • Full Pro access free for 60 days • No upfront commitment
Discover more resources

You may also like

Mock Tests

View All
  • GATE
  • beginner
  • 3 hours
  • civil engineering

Prepare for GATE 2026 Civil Engineering with the latest question papers, previous year papers, detailed solutions, syllabus, exam pattern, preparation tips, mock tests, and subject-wise study material. Download GATE 2026 CE question papers with solutions in PDF format and boost your preparation for top engineering institutes and PSU recruitment.

  • GATE
  • beginner
  • 3 hours
  • electronics and communication engineering

Prepare effectively for GATE 2026 Electronics & Communication Engineering (EC) with this comprehensive Question Paper with Solutions. This resource includes detailed explanations, concept-wise solutions, previous year paper analysis, and exam-oriented practice to help aspirants strengthen core subjects like Electronic Devices, Analog Circuits, Digital Circuits, Signals & Systems, Network Theory, Control Systems, Electromagnetics, Communications, and more. Perfect for revision, self-assessment, and boosting your GATE 2026 EC preparation strategy.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...
Ask your own doubt or choose a quick prompt: