Given three integers N, D and K. The task is to check whether it is possible to make a tree with exactly N vertices, D diameter (the number of edges in the longest path between any two vertices), and degree of each vertex must be at most K. If it is possible then print all the possible edges otherwise print No.
Input: N = 6, D = 3, K = 4
Input: N = 6, D = 2, K = 4
Approach: Let’s construct the tree with the following algorithm: If (d > n – 1), print “No” and terminate the program. Otherwise, let’s keep the array deg of the length n which will represent degrees of vertices.
The first step is to construct the diameter of the tree. Let the first (d + 1) vertices form it.
Let’s add d edges to the answer and increase degrees of vertices corresponding to these edges, and if some vertex has degree greater than k, print “No” and terminate the program.
The second (and the last) step is to attach the remaining (n – d – 1) vertices to the tree. Let’s call the vertex free if its degree is less than k. Also, let’s keep all free vertices forming the diameter in some data structure which allows us to take the vertex with the minimum maximal distance to any other vertex and remove such vertices. It can be done by, for example, set of pairs (distv, v), where distv is the maximum distance from the vertex v to any other vertex. Now let’s add all the vertices starting from the vertex (d + 1) (0-indexed) to the vertex n?1, let the current vertex be u. One can get the vertex with the minimum maximal distance to any other vertex, let it be v. Now increase the degree of vertices u and v, add the edge between them, and if v still be free, return it to the data structure, otherwise remove it. The same with the vertex u (it is obvious that its maximal distance to any other vertex will be equal to (distv + 1).
If at any step our data structure will be empty or the minimum maximal distance will equal d, the answer is “No”. Otherwise, we can print the answer.
Below is the implementation of the above approach:
1 2 2 3 3 4 5 2 6 2
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Possible edges of a tree for given diameter, height and vertices
- Find the vertex diagonally opposite to the vertex M from an N-sided polygon
- Maximum difference of count of black and white vertices in a path containing vertex V
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Find the remaining vertices of a square from two given vertices
- Check if a path exists in a tree with K vertices present or are at most at a distance D
- Minimum Operations to make value of all vertices of the tree Zero
- Diameter of a Binary Tree
- Diameter of an N-ary tree
- Diameter of a tree using DFS
- Diameter of a Binary Tree in O(n) [A new method]
- Diameter of n-ary tree using BFS
- DP on Trees | Set-3 ( Diameter of N-ary Tree )
- Diameter of a Binary Indexed Tree with N nodes
- Finding the lexicographically smallest diameter in a binary tree
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Print the nodes with a prime degree in given Prufer sequence of a Tree
- Color tree with minimum colors such that colors of edges incident to a vertex are different
- Queries to count connected components after removal of a vertex from a Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.