Friday, October 17, 2025
HomeData Modelling & AICayley’s Formula

Cayley’s Formula

Cayley’s Formula: This formula tells how many trees can be constructed with N vertices. It states that there are NN – 2  labeled trees that of N nodes. The nodes are labeled from 1, 2, …, N, and two trees are different if either their structure or labeling is different.

For Example: When N is 4, the number of labeled trees is 44 – 2 = 16.

Below is the image depicting the number of labeled trees:

In the above image, there are 4 nodes given from which 16 labeled trees are created.

Cayley’s formula derived using Prüfer Codes:

Prüfer Code:

  • A Prüfer Code is a sequence of (N – 2) numbers that describes a labeled tree.
  • The code is constructed by following a process that removes (N – 2) leaves from the tree.
  • At each step, the leaf with the smallest label is removed, and the label of its only neighbor is added to the code.

Below are the steps to calculate the Prüfer Code of the following graph:

  • Given a graph with five nodes:

  • Remove node 1 and add node 4 to the code:

  • Then remove node 3 and add node 4 to the code:

  • Finally, remove node 4 and add node 2 to the code:

Thus, the Prüfer code of the graph is given by {4, 4, 2}.

  • Prüfer code can be constructed for any tree.
  • The original tree can be reconstructed from a Prüfer code.
  • Hence, the number of labeled trees of n nodes equals NN – 2 , the number of Prüfer codes of size N.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS