Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
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 largest number of edges on any path between two vertices?
- A. 3
- B. 8
- C. 5
- D. 6
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 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…
- A rooted tree on vertices 0 to 11 has parent[i] = [0, 0, 2, 1, 0, 1, 4, 4, 4, 6, 3] for i = 1 to 11 (vertex 0 is the root). What is the…
- A rooted tree on vertices 0 to 7 has parent[i] = [0, 0, 2, 3, 0, 0, 5] for i = 1 to 7 (vertex 0 is the root). What is the sum, over all…
- A rooted tree on vertices 0 to 5 has parent[i] = [0, 0, 2, 3, 0] for i = 1 to 5 (vertex 0 is the root). What is the sum, over all vertices…
- What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [3, 7, 4, 1, 8, 5, 6, 2, 9]?