Home/Blog/Data Science/Counting the number of labeled trees
Home/Blog/Data Science/Counting the number of labeled trees

Counting the number of labeled trees

8 min read
Feb 06, 2024

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

In this blog, we’ll talk about labeled trees and the counting the total number of labeled trees in a graph with n vertices.

A labeled tree is one in which all the vertices are assigned a label or a name. For simplicity, we will label these nn vertices from the set 1,2,3,,n{1, 2, 3, \cdots, n}.

A labeled tree, or a labeled graph, is different from its unlabeled counterparts because all the nodes in labeled graphs represent some concrete element—people on social media, or classes or objects in a particular software—and the edges between them correspond to their interaction.

Let’s have a look at some of the labeled trees.

For n=2n = 2, there is only one possible tree, which is shown below.

Tree with two vertices
Tree with two vertices

For n=3n = 3, there are three possible trees, which are shown below.

Labeled trees with three vertices
Labeled trees with three vertices

In the above image, all three images look the same, but if we look at their labels, we will notice a difference—the label of the vertex in the center is different in all three of them, so they are counted as three different trees. If the trees were unlabeled, we would have considered them the same tree.

Just to observe the difference, have a look at an unlabeled tree and some graphs that are not trees, and some unlabeled trees.

An unlabeled tree
An unlabeled tree
1 of 3

One-to-one functions#

In this blog, our goal is to derive a formula for the number of trees with nn labeled vertices. But before discussing labeled trees, let’s talk about one-to-one functions.

f(x) = 5 - x
f(x) = 5 - x

The above diagram represents the function f(x)=5f(x) = 5xx. This function takes input from the domain (shown on the left-hand side) and maps it to the elements of the co-domain (shown on the right-hand side). The mapping between the inputs and outputs of the function is shown by connecting them through the arrows. If none of the elements on the right-hand side have incoming arrows from multiple inputs, then the corresponding function is said to be one-to-one.

Every domain element is paired with a distinct element from the co-domain for any one-to-one function. This indicates that the number of elements between the two sets is the same for such functions. In some cases, some elements of the co-domain might not have a corresponding element from the domain. Here, the cardinalityCardinality of a set is the number of elements present in that set of the domain will be less than the cardinality of the co-domain. An example is shown below.

f(x) = x + 1
f(x) = x + 1

The diagram above represents the one-to-one function f(x)=x+1f(x) = x + 1 from the domain {1,2,3}\{1, 2, 3\} to the co-domain {1,2,3,4}\{1, 2, 3, 4\}. Here, the cardinality of the domain is less than the cardinality of the co-domain.

Are two sets equal?#

Suppose we have a function, f:ABf:A \rightarrow B. This notation tells us that the function ff maps elements from domain AA to the co-domain BB. If this function is one-to-one, then AB|A| \le |B|.

Suppose there is another one-to-one function, g:BAg: B \rightarrow A. This implies that BA|B| \le |A|.

If for any pair of sets AA and BB, we can find two functions ff and gg as described above, we can conclude that the two sets have the same cardinality, i.e., A=B|A| = |B|.

The counting process#

Let’s first look at the outline of the steps before getting into the details.

We are interested in counting the labeled trees. We will define set AA that contains all trees with nn vertices. We will define another set, BB, whose elements can be counted easily.

Next, we will relate these two sets by using two one-to-one functions ff and gg, as described below.

f:ABf: A \rightarrow B

g:BAg: B \rightarrow A

The sets and the functions#

Continuing the discussion, we define set AA as the set of labeled trees of size nn and set BB as the set of arrays of size nn2,2, containing elements from the set {1,2,3,...,n}.\{1, 2, 3, ... , n\}. The function ff will map elements of set AA to elements of the set BB. The function gg will map elements from set BB to the elements of set AA.

Set A#

This set consists of labeled trees. Each tree will contain nn vertices, and each vertex will be assigned a label from the set {1,2,3,,n}\{1, 2, 3, \cdots, n\}. No two vertices will be assigned the same label. Let’s recall that we want to find out the cardinality of this set.

Set B#

Each item of this set is an array of size nn22. The elements of this array are from the set {1,2,3,,n}\{1, 2, 3, \cdots, n\}. For n=3n = 3, we have the following arrays of size 11.

Possible arrays for n = 3
Possible arrays for n = 3

We want to relate this set with set A using one-to-one functions ff and gg.

Let’s discover these functions one at a time.

The function fff#

The function ff is a function from set AA to set BB. In simpler words, we want to store the information of a labeled tree with nn vertices in an array of size nn22. We will demonstrate how to do that in the next section.

Recall that this function will take a tree as an input and return an array at the output. Using this function, we want to relate the number of labeled trees and the number of arrays of corresponding size. An example of the labeled trees with three vertices is shown below.

Visual representation of the function f
Visual representation of the function f

Let’s look at the steps of this function in the pseudocode below. An example will follow afterward.

Algorithm for fff#

Input: A tree with nn vertices

Output: An array EE of size n2n-2

  1. Construct an array E of size n2n-2

  2. FOR (i=1,2,,n2)(i = {1, 2, \cdots, n-2})

  3. x=\hspace{3em} x = the smallest leaf vertex

  4. y=\hspace{3em} y = neighbor of xx

  5. E[i]=y\hspace{3em} E[i] = y

  6. \hspace{3em}DELETE vertex xx from the tree

Example for fff#

Below is an example with 66 vertices.

canvasAnimation-image
1 of 6

This array is also called the Prüfer code. The Prüfer code for the graph discussed in the above example is [4,1,1,4].[4, 1, 1, 4].

The function ggg#

The function gg is a function from set BB to set AA. This function takes an element from the set of arrays and maps it to an element from the set of labeled trees. In fact, the function we will discuss here is the inverse of the function ff.

Again, we will look at the steps of this function before applying the steps to a concrete example.

Algorithm for ggg#

Input: An array Prüfer_Code of size kk

Output: A labeled tree of size k+2k+2

  1. Create a list missing_Numbers

  2. Create a graph G with the vertex set V={1,2,,k}V = \{1, 2, \cdots, k\}

  3. FOR (i=1,2,,k)(i = {1, 2, \cdots, k})

  4. \hspace{3em}IF (ii is missing from the array Prüfer_Code)

  5. \hspace{3em}\hspace{3em}INSERT ii in the list missing_Numbers

  6. FOR (i=1,2,,k2)(i = {1, 2, \cdots, k-2})

  7. x=\hspace{3em} x = minimum number in list missing_Numbers

  8. \hspace{3em}ADD EDGE between the vertices xx and Prüfer_Code[ii]

  9. \hspace{3em}DELETE xx from the list missing_Numbers

  10. \hspace{3em}IF (element Prüfer_Code[ii] is not present in the Array between the indexes i+1i+1 and kk)

  11. \hspace{3em}\hspace{3em}INSERT Prüfer_Code[ii] in the list missing_Numbers

  12. ADD EDGE between the two remaining elements of the list missing_Numbers

Example of function ggg#

Let’s visualize this algorithm with the help of an example. This function takes an array at the input and produces a tree at the output.

canvasAnimation-image
1 of 6

The final calculation#

The functions ffand gg are both one-to-one. Moreover, these functions are the inverse of each other. The function ff takes a tree and maps it to an array, whereas the function gg takes the array and maps it back to the tree. This can be seen from the provided example. The same arrays and trees are used in these examples. This implies that the cardinality of both sets are equal.

Set BB is an array of size n2.n-2. For each entry, there are nn possible options, so the total number of such arrays is nn2.n^{n-2}.

This shows that the number of labeled trees with nn vertices is nn2.n^{n-2}.

Applications#

A graph with nn vertices requires O(n2)O(n^2) storage size if we want to save it in an adjacency matrix. Alternately, for a tree with nn vertices, we would require 2×(n1)2\times(n-1) space if we want to save the information of the edges. Recall that each edge is connected to 22 vertices, and a tree has n1n-1 edges.

With this method, we can store a tree using an array of size n2.n-2. This is the best possible compression we can get if we want to store trees.

If you are interested in learning more about counting, graph theory, or sets, feel free to explore the following Educative courses.


Written By:
Khawaja Muhammad Fahd
 
Join 2.5 million developers at
Explore the catalog

Free Resources