171. Use Path Operations To Minimize Tree Diameter

Asked in

Use Path Operations To Minimize Tree Diameter
You are given an undirected tree with vertexCount vertices numbered from 1 to vertexCount.

The diameter of a tree is the maximum number of edges on the unique simple path between any two vertices.

You may perform a special path operation to reduce the tree diameter.

In one operation, choose two vertices start and end. Let the vertices on the simple path from start to end be:
p0, p1, p2, ..., pk
where p0 = start and pk = end.

Remove every edge on this path:
(p0, p1), (p1, p2), ..., (pk-1, pk)

Then connect every other path vertex directly to start:
(p0, p1), (p0, p2), ..., (p0, pk)

After the operation, the graph is still a tree.

Your task is to return the minimum number of such operations required so that the tree reaches the smallest possible diameter.

Class

class TreeDiameterPathOperation

Method

int minimumOperationsToMinimizeDiameter(int vertexCount, List<String> connections)
  • vertexCount is the number of vertices in the tree.
  • connections contains exactly vertexCount - 1 strings.
  • Each string in connections is formatted as "u,v", representing an undirected edge between vertices u and v.
  • The method returns the minimum number of path operations needed to achieve the minimum possible diameter.

Definitions

Tree Diameter

The diameter of a tree is the longest distance between any pair of vertices. The distance between two vertices is the number of edges on the unique simple path connecting them.

Constraints

  • 2 ≤ vertexCount ≤ 200,000
  • connections.size() = vertexCount - 1
  • Each connection string is formatted as "u,v".
  • 1 ≤ u, v ≤ vertexCount
  • u != v
  • The given connections always form a valid tree.

Examples

Example 1

minimumOperationsToMinimizeDiameter(vertexCount = 4, connections = ["1,2", "2,3", "3,4"])
Returns 1.

The tree is a path of length 3. One operation can flatten this path around one endpoint, reducing the diameter to the minimum possible value.

Example 2

minimumOperationsToMinimizeDiameter(vertexCount = 2, connections = ["1,2"])
Returns 0.

The tree already has diameter 1, which is the smallest possible diameter for a tree with two vertices.

Example 3

minimumOperationsToMinimizeDiameter(vertexCount = 5, connections = ["1,2", "1,3", "1,4", "1,5"])
Returns 0.

The tree is already a star centered at vertex 1. Its diameter is already minimal.

Example 4

minimumOperationsToMinimizeDiameter(vertexCount = 7, connections = ["1,2", "2,3", "3,4", "3,5", "2,6", "6,7"])
Returns 2.

The tree has several leaf branches. At least two path operations are required to reshape the tree so that the final diameter becomes as small as possible.

Example 5

minimumOperationsToMinimizeDiameter(vertexCount = 11, connections = ["1,2", "1,3", "2,4", "3,5", "3,8", "5,6", "5,7", "7,9", "7,10", "5,11"])
Returns 4.

Multiple leaf branches are far from any single best center. Four operations are needed to make the diameter minimal.




Please use Laptop/Desktop or any other large screen to add/edit code.