If a graph is a complete graph with n vertices, then total number of spanning trees is n(n-2) where n is the number of nodes in the graph. In complete graph, the task is equal to counting different labeled trees with n nodes for which have Cayley’s formula.
What if graph is not complete?
Follow the given procedure :-
STEP 1: Create Adjacency Matrix for the given graph.
STEP 2: Replace all the diagonal elements with the degree of nodes. For eg. element at (1,1) position of adjacency matrix will be replaced by the degree of node 1, element at (2,2) position of adjacency matrix will be replaced by the degree of node 2, and so on.
STEP 3: Replace all non-diagonal 1’s with -1.
STEP 4: Calculate co-factor for any element.
STEP 5: The cofactor that you get is the total number of spanning tree for that graph.
Consider the following graph:
Adjacency Matrix for the above graph will be as follows:
After applying STEP 2 and STEP 3, adjacency matrix will look like
The co-factor for (1, 1) is 8. Hence total no. of spanning tree that can be formed is 8.
NOTE- Co-factor for all the elements will be same. Hence we can compute co-factor for any element of the matrix.
This method is also known as Kirchhoff’s Theorem. It can be applied to complete graphs also.
Please refer below link for proof of above procedure.
This article is contributed by Kapil Khandelwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Total number of Spanning trees in a Cycle Graph
- Number of spanning trees of a weighted complete Graph
- Problem Solving for Minimum Spanning Trees (Kruskal’s and Prim’s)
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Program to find total number of edges in a Complete Graph
- Applications of Minimum Spanning Tree Problem
- Boruvka's algorithm for Minimum Spanning Tree
- Kruskal's Minimum Spanning Tree using STL in C++
- Minimum Product Spanning Tree
- Reverse Delete Algorithm for Minimum Spanning Tree
- Types of Spanning Tree Protocol (STP)
- Minimum spanning tree cost of given Graphs
- Find the weight of the minimum spanning tree
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Find the minimum spanning tree with alternating colored edges
- Minimum Spanning Tree using Priority Queue and Array List
- Minimum Bottleneck Spanning Tree(MBST)
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Count total ways to reach destination from source in an undirected Graph