Given an integer L which is the sum of degrees of all the vertices of some tree. The task is to find the count of all such distinct trees (labeled trees). Two trees are distinct if they have at least a single different edge.
Input: L = 2
Input: L = 6
Simple Solution: A simple solution is to find the number of nodes of the tree which has sum of degrees of all vertices as L. Number of nodes in such a tree is n = (L / 2 + 1) as described in this article.
Now the solution is to form all the labeled trees which can be formed using n nodes. This approach is quite complex and for larger values of n it is not possible to find out the number of trees using this process.
Efficient Solution: An efficient solution is to find the number of nodes using Cayley’s formula which states that there are n(n – 2) trees with n labeled vertices. So the time complexity of the code now reduces to O(n) which can be further reduced to O(logn) using modular exponentiation.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Finding in and out degrees of all vertices in a graph
- Construct a graph from given degrees of all vertices
- Maximize the sum of products of the degrees between any two vertices of the tree
- Check whether given degrees of vertices represent a Graph or Tree
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Minimum number of edges between two vertices of a graph using DFS
- Number of Simple Graph with N Vertices and M Edges
- Minimum number of edges between two vertices of a Graph
- Number of cycles in a Polygon with lines from Centroid to Vertices
- Find maximum number of edge disjoint paths between two vertices
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
- Number of cycles formed by joining vertices of n sided polygon at the center
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Total number of possible Binary Search Trees using Catalan Number
- Number of pairs such that path between pairs has the two vertices A and B
- Sum of degrees of all nodes of a undirected graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.