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
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
For
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.
In this blog, our goal is to derive a formula for the number of trees with
The above diagram represents the function
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
The diagram above represents the one-to-one function
Suppose we have a function,
Suppose there is another one-to-one function,
If for any pair of sets
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
Next, we will relate these two sets by using two one-to-one functions
Continuing the discussion, we define set
This set consists of labeled trees. Each tree will contain
Each item of this set is an array of size
We want to relate this set with set A using one-to-one functions
Let’s discover these functions one at a time.
The function
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.
Let’s look at the steps of this function in the pseudocode below. An example will follow afterward.
Input: A tree with
Output: An array
Construct an array E of size
FOR
Below is an example with
This array is also called the Prüfer code. The Prüfer code for the graph discussed in the above example is
The function
Again, we will look at the steps of this function before applying the steps to a concrete example.
Input: An array Prüfer_Code of size
Output: A labeled tree of size
Create a list missing_Numbers
Create a graph G with the vertex set
FOR
FOR
ADD EDGE between the two remaining elements of the list missing_Numbers
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.
The functions
Set
This shows that the number of labeled trees with
A graph with
With this method, we can store a tree using an array of size
If you are interested in learning more about counting, graph theory, or sets, feel free to explore the following Educative courses.
Free Resources