Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 12

Trees and Spanning Trees

Recommended resources

Welcome to the next lesson in our journey through Discrete Mathematics. In our last session, we contrasted Eulerian paths, which are easy to verify using vertex degrees, with the computationally hard problem of finding Hamiltonian paths. We explored how sufficient conditions like Dirac's and Ore's theorems can give us guarantees about a graph's structure. Today, we narrow our focus to a special and ubiquitous class of graphs: trees.

This lesson centers on analyzing the properties of trees and, most importantly, on a fundamental question for GATE: "How many ways can we form a minimal network connecting all points in a graph?" This leads us to the concept of spanning trees. We will learn two powerful techniques to count them: the general-purpose Kirchhoff's Matrix Tree Theorem and the elegant shortcut, Cayley's formula, for complete graphs. Mastering these methods will equip you to solve a common type of numerical answer question in the exam.

What is a Tree?

In graph theory, a tree is a connected graph that contains no cycles. This simple definition gives rise to a set of powerful and equivalent properties. For any graph with vertices, the following statements are equivalent:

  • is a tree.
  • is connected and has exactly edges.
  • is acyclic and has exactly edges.
  • There is a unique simple path between any two vertices in .
  • is minimally connected: if you remove any single edge, the graph becomes disconnected.
  • is maximally acyclic: if you add any single edge between two non-adjacent vertices, a cycle is created.

Trees are the backbone of many algorithms and data structures, from the file systems on your computer to decision trees in machine learning. Their property of providing a unique path between any two nodes with the minimum number of edges makes them exceptionally efficient.


Spanning Trees: The Skeleton of a Graph

Most graphs you encounter in problems are not trees themselves because they contain cycles. However, within any connected graph, we can find a "skeletal" version that is a tree. This is called a spanning tree.

A spanning tree of a connected graph is a subgraph of that:

  1. Is a tree.
  2. Includes all the vertices of .

Think of it as finding the most economical way to connect all cities in a transport network. You need enough roads to ensure everyone is connected, but no extra roads that create redundant loops. For any connected graph, there can be multiple, often many, spanning trees.

The following video provides a great visual introduction to this idea.

Graph Theory 13: Spanning Trees and Kirchhoff's Theorem

This segment from a video by Math at Andrews University clearly defines and illustrates the concept of a spanning tree by showing a simple graph and several of its different spanning trees.

Please watch the first part of the video, from the beginning to this point. Observe how removing different edges from the cycles of the original graph results in different valid spanning trees.


Counting Spanning Trees: Kirchhoff's Matrix Tree Theorem

While listing out all spanning trees is feasible for a tiny graph, it quickly becomes impossible for larger, more complex graphs. For GATE, you need a systematic, algorithmic approach. This is provided by Kirchhoff's Matrix Tree Theorem.

The theorem uses a special matrix representation of the graph called the Laplacian matrix. The number of spanning trees is then found by calculating a determinant. Let's break down the procedure.

Steps to apply Kirchhoff's Theorem:

  1. Construct the Adjacency Matrix ():
    This is an matrix where if there is an edge between vertex and vertex , and otherwise. For simple graphs, the diagonal entries are 0.

  2. Construct the Degree Matrix ():
    This is an diagonal matrix where the entry is the degree of vertex , and all off-diagonal entries are .

  3. Form the Laplacian Matrix ():
    The Laplacian is defined as .

    • The diagonal entries will be the degree of vertex .
    • The off-diagonal entries will be if an edge exists between and , and otherwise.
    • A key property is that all rows and columns of the Laplacian matrix sum to zero.
  4. Calculate a Cofactor:
    Choose any row and any column and delete them to create an submatrix. This submatrix is called a cofactor. A common choice is to remove the first row and first column.

  5. Find the Determinant:
    The number of spanning trees of the graph is the determinant of this cofactor matrix. The theorem guarantees that the result will be the same no matter which row and column you choose to delete.

The next segment of the video you just watched walks through this exact procedure with the graph from the introduction.

Graph Theory 13: Spanning Trees and Kirchhoff's Theorem

This part of the video demonstrates Kirchhoff's algorithm step-by-step. It shows how to build the adjacency, degree, and Laplacian matrices, and then computes the determinant of a cofactor to find the number of spanning trees.

Please watch from this segment. Follow the construction of each matrix carefully. Note how deleting the first row and column gives a 3x3 matrix whose determinant is 8, matching the number of trees they found by enumeration.

For your B.Tech in IT, you are already familiar with matrices and determinants. The main new concept here is the construction of the Laplacian matrix and the surprising fact that its cofactors' determinants count something as combinatorial as spanning trees. This is a powerful and very testable procedure.


A Special Case: Complete Graphs and Cayley's Formula

What if we need to find the number of spanning trees for a complete graph , where every vertex is connected to every other vertex? We could use Kirchhoff's theorem, but it would involve calculating the determinant of a large matrix.

Fortunately, there is a famous and beautiful shortcut known as Cayley's Formula.

Cayley's Formula: The number of spanning trees in a complete graph is .

This formula also answers a related question: "How many distinct labeled trees are there with vertices?" Since any labeled tree on vertices is a spanning tree of the complete graph , the answer is the same.

Example:

  • For , the number of spanning trees is .
  • For , the number of spanning trees is .

The final part of the video explains how this formula is connected to Kirchhoff's Theorem.

Graph Theory 13: Spanning Trees and Kirchhoff's Theorem

This last segment connects the general method of Kirchhoff to the specific case of a complete graph, showing that Cayley's formula is the result of applying the Matrix Tree Theorem to K_n.

Watch from the application section. The key insight is that any labeled tree on n vertices can be seen as a spanning tree of the complete graph Kn.

Problem-Solving Strategy for GATE:
When a question asks for the number of spanning trees:

  1. Check the graph type: Is it a general graph or a complete graph ? (A question about "all labeled trees on n vertices" implies ).
  2. If it's , use Cayley's Formula: . This is a fast, direct calculation.
  3. If it's any other graph, use Kirchhoff's Matrix Tree Theorem: build , find a cofactor, and compute its determinant.

Conclusion

In this lesson, we focused on trees and their role as the fundamental building blocks of connected graphs. You learned the essential properties of trees and the definition of a spanning tree. Most importantly, you acquired two algorithmic methods for counting spanning trees—a common task in GATE.

Key Takeaways:

  • A tree is a connected acyclic graph with vertices and edges.
  • A spanning tree is a subgraph that connects all vertices of the original graph without forming any cycles.
  • Kirchhoff's Matrix Tree Theorem provides a universal algorithm for counting spanning trees in any graph by calculating the determinant of a cofactor of the Laplacian matrix ().
  • Cayley's Formula () is a powerful shortcut for the special case of complete graphs () or, equivalently, for finding the total number of labeled trees on vertices.

In our next lesson, we will continue exploring subgraph structures by investigating Graph Matching. We'll focus on bipartite graphs and tackle problems related to finding the maximum number of independent edges, a concept with wide-ranging applications in assignment and scheduling problems.