# Roots of a tree which give minimum height

Given an undirected graph, which has tree characteristics. It is possible to choose any node as root, the task is to find those nodes only which minimize the height of tree.

Example:

In below diagram all node are made as root one by one, we can see that when 3 and 4 are root, height of tree is minimum(2) so {3, 4} is our answer.

We can solve this problem by first thinking about 1-D solution, that is if a longest graph is given, then the node which will minimize the height will be mid node if total node count is odd or mid two node if total node count is even. This solution can be reached by following approach – Start two pointers from both end of the path and move one step each time, until pointers meet or one step away, at the end pointers will be at those nodes which will minimize the height because we have divided the nodes evenly so height will be minimum.

Same approach can be applied to a general tree also. Start pointers from all leaf nodes and move one step inside each time, keep combining pointers which overlap while moving, at the end only one pointer will remain on some vertex or two pointers will remain at one distance away. Those node represent the root of the vertex which will minimize the height of the tree.

So we can have only one root or at max two root for minimum height depending on tree structure as explained above. For implementation we will not use actual pointers instead we’ll follow BFS like approach, In starting all leaf node are pushed into the queue, then they are removed from tree, next new leaf node are pushed in queue, this procedure keeps on going until we have only 1 or 2 node in our tree, which represent the result.

As we are accessing each node once, total time complexity of solution is O(n).

## C++

`// C++ program to find root which gives minimum height to tree ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// This class represents a undirected graph using adjacency list ` `// representation ` `class` `Graph ` `{ ` `public` `: ` ` ` `int` `V; ` `// No. of vertices ` ` ` ` ` `// Pointer to an array containing adjacency lists ` ` ` `list<` `int` `> *adj; ` ` ` ` ` `// Vector which stores degree of all vertices ` ` ` `vector<` `int` `> degree; ` ` ` ` ` `Graph(` `int` `V); ` `// Constructor ` ` ` `void` `addEdge(` `int` `v, ` `int` `w); ` `// To add an edge ` ` ` ` ` `// function to get roots which give minimum height ` ` ` `vector<` `int` `> rootForMinimumHeight(); ` `}; ` ` ` `// Constructor of graph, initializes adjacency list and ` `// degree vector ` `Graph::Graph(` `int` `V) ` `{ ` ` ` `this` `->V = V; ` ` ` `adj = ` `new` `list<` `int` `>[V]; ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `degree.push_back(0); ` `} ` ` ` `// addEdge method adds vertex to adjacency list and increases ` `// degree by 1 ` `void` `Graph::addEdge(` `int` `v, ` `int` `w) ` `{ ` ` ` `adj[v].push_back(w); ` `// Add w to v’s list ` ` ` `adj[w].push_back(v); ` `// Add v to w’s list ` ` ` `degree[v]++; ` `// increment degree of v by 1 ` ` ` `degree[w]++; ` `// increment degree of w by 1 ` `} ` ` ` `// Method to return roots which gives minimum height to tree ` `vector<` `int` `> Graph::rootForMinimumHeight() ` `{ ` ` ` `queue<` `int` `> q; ` ` ` ` ` `// first enqueue all leaf nodes in queue ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `if` `(degree[i] == 1) ` ` ` `q.push(i); ` ` ` ` ` `// loop untill total vertex remains less than 2 ` ` ` `while` `(V > 2) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < q.size(); i++) ` ` ` `{ ` ` ` `int` `t = q.front(); ` ` ` `q.pop(); ` ` ` `V--; ` ` ` ` ` `// for each neighbour, decrease its degree and ` ` ` `// if it become leaf, insert into queue ` ` ` `for` `(` `auto` `j = adj[t].begin(); j != adj[t].end(); j++) ` ` ` `{ ` ` ` `degree[*j]--; ` ` ` `if` `(degree[*j] == 1) ` ` ` `q.push(*j); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// copying the result from queue to result vector ` ` ` `vector<` `int` `> res; ` ` ` `while` `(!q.empty()) ` ` ` `{ ` ` ` `res.push_back(q.front()); ` ` ` `q.pop(); ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `Graph g(6); ` ` ` `g.addEdge(0, 3); ` ` ` `g.addEdge(1, 3); ` ` ` `g.addEdge(2, 3); ` ` ` `g.addEdge(4, 3); ` ` ` `g.addEdge(5, 4); ` ` ` ` ` `vector<` `int` `> res = g.rootForMinimumHeight(); ` ` ` `for` `(` `int` `i = 0; i < res.size(); i++) ` ` ` `cout << res[i] << ` `" "` `; ` ` ` `cout << endl; ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find root which gives minimum ` `# height to tree ` ` ` `# This class represents a undirected graph using ` `# adjacency list representation ` `class` `Graph: ` ` ` ` ` `# Constructor of graph, initialize adjacency list ` ` ` `# and degree vector ` ` ` `def` `__init__(` `self` `, V, addEdge, rootForMinimumHeight): ` ` ` `self` `.V ` `=` `V ` ` ` `self` `.adj ` `=` `dict` `( (i, []) ` `for` `i ` `in` `range` `(V)) ` ` ` `self` `.degree ` `=` `list` `() ` ` ` `for` `i ` `in` `range` `(V): ` ` ` `self` `.degree.append(` `0` `) ` ` ` ` ` `# The below lines allows us define methods outside ` ` ` `# of class definition ` ` ` `# Check http://bit.ly/2e5HfrW for better explanation ` ` ` `Graph.addEdge ` `=` `addEdge ` ` ` `Graph.rootForMinimumHeight ` `=` `rootForMinimumHeight ` ` ` ` ` `# addEdge method adds vertex to adjacency list and ` `# increases degree by 1 ` `def` `addEdge(` `self` `, v, w): ` ` ` `self` `.adj[v].append(w) ` `# Adds w to v's list ` ` ` `self` `.adj[w].append(v) ` `# Adds v to w's list ` ` ` `self` `.degree[v] ` `+` `=` `1` `# increment degree of v by 1 ` ` ` `self` `.degree[w] ` `+` `=` `1` `# increment degree of w by 1 ` ` ` ` ` `# Method to return roots which gives minimum height to tree ` `def` `rootForMinimumHeight(` `self` `): ` ` ` ` ` `from` `Queue ` `import` `Queue ` ` ` `q ` `=` `Queue() ` ` ` ` ` `# First enqueue all leaf nodes in queue ` ` ` `for` `i ` `in` `range` `(` `self` `.V): ` ` ` `if` `self` `.degree[i] ` `=` `=` `1` `: ` ` ` `q.put(i) ` ` ` ` ` `# loop until total vertex remains less than 2 ` ` ` `while` `(` `self` `.V > ` `2` `): ` ` ` `for` `i ` `in` `range` `(q.qsize()): ` ` ` `t ` `=` `q.get() ` ` ` `self` `.V ` `-` `=` `1` ` ` ` ` `# for each neighbour, decrease its degree and ` ` ` `# if it become leaf, insert into queue ` ` ` `for` `j ` `in` `self` `.adj[t]: ` ` ` `self` `.degree[j] ` `-` `=` `1` ` ` `if` `self` `.degree[j] ` `=` `=` `1` `: ` ` ` `q.put(j) ` ` ` ` ` ` ` `# Copying the result from queue to result vector ` ` ` `res ` `=` `list` `() ` ` ` `while` `(q.qsize() > ` `0` `): ` ` ` `res.append(q.get()) ` ` ` ` ` `return` `res ` ` ` ` ` `# Driver code to test above methods ` `g ` `=` `Graph(` `6` `, addEdge, rootForMinimumHeight ) ` `g.addEdge(` `0` `, ` `3` `) ` `g.addEdge(` `1` `, ` `3` `) ` `g.addEdge(` `2` `, ` `3` `) ` `g.addEdge(` `4` `, ` `3` `) ` `g.addEdge(` `5` `, ` `4` `) ` `res ` `=` `g.rootForMinimumHeight() ` `for` `i ` `in` `res: ` ` ` `print` `i, ` ` ` `# This code is contributed by Nikhil Kumar Singh(nickzuck_007) ` |

*chevron_right*

*filter_none*

Output:

3 4

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Applications of Minimum Spanning Tree Problem
- Boruvka's algorithm for Minimum Spanning Tree
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Find minimum s-t cut in a flow network
- Find the minimum cost to reach destination using a train
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Karger’s algorithm for Minimum Cut | Set 2 (Analysis and Applications)
- Steiner Tree Problem
- Minimum steps to reach a destination
- Minimum time required to rot all oranges
- Check whether given degrees of vertices represent a Graph or Tree
- Kruskal's Minimum Spanning Tree using STL in C++
- Check if two nodes are on same path in a tree
- Finding minimum vertex cover size of a graph using binary search
- Minimum steps to reach end of array under constraints
- Minimum edges to reverse to make path from a source to a destination
- Minimum number of operation required to convert number x into y
- Minimum number of swaps required to sort an array
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed