Data Structures & AlgorithmsHardGATE CS · TCS NQT
A tree on vertices 0 to 9 has the edges 1-0, 2-0, 3-1, 4-2, 5-2, 6-4, 7-0, 8-2, 9-8. What is its diameter, meaning the largest number of edges on any path between two vertices?
- A. 4
- B. 6
- C. 5
- D. 3
Correct answer: C. 5
The diameter is found by two BFS passes: from any vertex to a farthest vertex, then from that vertex. It is an edge count; the eccentricity of one fixed vertex, or the vertex count of the path, is a different number.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A tree on vertices 0 to 7 has the edges 1-0, 2-0, 3-2, 4-2, 5-1, 6-1, 7-4. What is its diameter, meaning the largest number of edges on…
- A tree on vertices 0 to 10 has the edges 1-0, 2-1, 3-0, 4-0, 5-2, 6-4, 7-3, 8-5, 9-5, 10-8. What is its diameter, meaning the largest…
- A tree on vertices 0 to 7 has the edges 1-0, 2-1, 3-0, 4-3, 5-0, 6-4, 7-0. What is its diameter, meaning the largest number of edges on…
- A tree on vertices 0 to 11 has the edges 1-0, 2-0, 3-0, 4-2, 5-2, 6-3, 7-0, 8-3, 9-3, 10-0, 11-6. What is its diameter, meaning the…
- A tree on vertices 0 to 6 has the edges 1-0, 2-0, 3-1, 4-0, 5-4, 6-1. What is its diameter, meaning the largest number of edges on any…
- A rooted tree on vertices 0 to 5 has parent[i] = [0, 1, 0, 1, 4] for i = 1 to 5 (vertex 0 is the root). What is the sum, over all vertices…