Question: Diameter of a Tree Problem Description You are given a tree T with n vertices numbered 1 , 2 , . . . n .
Diameter of a Tree
Problem Description
You are given a tree T with n vertices numbered n
Find the diameter of the tree. The diameter of a tree is the maximum distance among all pairs of
vertices in the tree. The distance between two vertices is the number of edges on the shortest path
between them.
Hint Let u be any vertex in the tree and v be the farthest vertex from u If there are several
candidates for v choose any. Let w be the farthest vertex from v if there are several such vertices,
choose any The diameter is equal to the distance between v and w Note that this fact can be used
to design a linear time algorithm to compute the diameter.
Remark : For this question, your program will be tested against multiple test instances within a
single test file. Each test instance contains the description of a tree T
Remark : For this problem, we recommend implementing breadthfirst search for finding v and w
If you implement depthfirst search using recursion, your program may crash if the recursion is too
deep. This is because the maximum call stack size is MB on Gradescope, and the default maximum
depth of recursive calls in Python is
Input and Output
In this assignment, you are provided with sample submissions that contain code for input and output.
You only need to complete a function that, given the edges of the tree T represented by an array
of pairs of integers returns the diameter of T Your function should ideally be able to compute the
diameter in linear time. For completeness, the inputoutput format is given below.
Each input file contains several test instances. The first line contains a single integer C representing
the number of test instances your program is tested against. The description of the P instances follows.
Each test instance is formatted as follows. The first line contains an integer n representing the number
of vertices in the tree T Each of the subsequent n lines contains a pair of spaceseparated integers
u and v indicating that there is an edge between vertex u and vertex v in T
For each test instance, your program should output a single integer: the diameter of the tree T
Constraints
Each input file contains multiple test instances.
C
Each test instance contains a tree of at least vertices.
In a single input file, the sum of the sizes of all trees is at most
The time limit for all test cases in a single file is second for CC and seconds for Python.
Test Cases
Your program will be evaluated on visible test files and hidden test files. Passing all the instances
within a file will earn you points. Note that if your program outputs an incorrect answer for even
one of the instances within a file, you will receive points for that file.
Sample Input :
Sample Output :
Sample Explanation
Sample Input contains C test instances, which are illustrated in Fig.
Submission Guideline
Write your program in either C C or Python in a single file. Submit the file on Gradescope.
The time limit on Gradescope is second for CC and seconds for Python. You can make at
most submission attempts. You may refer to sample.cpp or sample.py for sample code that takes
input and writes output.
a The first instance, where the diameter is the
distance between vertex and vertex
b The second instance, where the diameter is
the distance between and
c The third instance, where the diameter is the
distance between and
d The fourth instance, where the diameter is the
distance between and
Figure : Illustrations for Sample Input
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
