The Wayback Machine - https://web.archive.org/web/20240531010656/https://www.geeksforgeeks.org/total-number-of-spanning-trees-in-a-cycle-graph/
Open In App

Total number of Spanning trees in a Cycle Graph

Last Updated : 23 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given the number of vertices in a Cycle graph. The task is to find the Total number of Spanning trees possible. 

Note: A cycle/circular graph is a graph that contains only one cycle. A spanning tree is the shortest/minimum path in a graph that covers all the vertices of a graph.

Examples:  

Input: Vertices = 3
Output: Total Spanning tree = 3

Input: Vertices = 4
Output: Total Spanning tree = 4

Example 1: 

For Cycle Graph with vertices = 3 
 

Spanning Tree possible is 3 
 

Example 2: 
For Cycle Graph with vertices = 4 

Spanning Tree possible is 4 

So, the number of spanning trees will always be equal to the number of vertices in a cycle graph.

Implementation:

C++




// C++ program to find number of
// spanning trees
#include <bits/stdc++.h>
using namespace std;
 
// function that calculates the
// total Spanning tree
int Spanning(int vertices)
{
    int result = 0;
 
    result = vertices;
    return result;
}
 
// Driver code
int main()
{
    int vertices = 4;
 
    cout << "Spanning tree = " << Spanning(vertices);
    return 0;
}


Java




// Java program to find number of
// spanning trees
 
import java.io.*;
 
class GFG {
 
// function that calculates the
// total Spanning tree
static int Spanning(int vertices)
{
    int result = 0;
 
    result = vertices;
    return result;
}
 
// Driver code
    public static void main (String[] args) {
    int vertices = 4;
 
    System.out.println("Spanning tree = " + Spanning(vertices));
    }
}
// This code is contributed 
// by chandan_jnu..


Python3




# Python program to find number of
# spanning trees
 
# function that calculates the
# total Spanning tree
def Spanning( vertices):
        result = 0
 
    result = vertices
    return result
 
# Driver code
vertices = 4
print("Spanning tree = ",
       Spanning(vertices))
 
# This code is contributed
# by Sanjit_Prasad


C#




// C# program to find number
// of spanning trees
using System;
 
// function that calculates
// the total Spanning tree
class GFG
{
public int Spanning(int vertices)
{
    int result = 0;
 
    result = vertices;
    return result;
}
 
// Driver code
public static void Main()
{
    GFG g = new GFG();
    int vertices = 4;
 
    Console.WriteLine("Spanning tree = {0}",  
                      g.Spanning(vertices));
}
}
 
// This code is contributed
// by Soumik


PHP




<?php
// PHP program to find number of
// spanning trees
 
// function that calculates the
// total Spanning tree
function Spanning($vertices)
{
    $result = 0;
 
    $result = $vertices;
    return $result;
}
 
// Driver code
$vertices = 4;
 
echo "Spanning tree = " .
     Spanning($vertices);
      
// This code is contributed
// by Ankita Saini
?>


Javascript




<script>
 
// Javascript program to find number of
// spanning trees
 
// Function that calculates the
// total Spanning tree
function Spanning(vertices)
{
    result = 0;
    result = vertices;
    return result;
}
 
// Driver code
var vertices = 4;
document.write("Spanning tree = " +
               Spanning(vertices));
 
// This code is contributed by noob2000
 
</script>


Output

Spanning tree = 4

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

Total number of Spanning Trees in a Graph
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 f
13 min read
Number of spanning trees of a weighted complete Graph
Prerequisites: Graph Theory Basics, Spanning tree. Complete Weighted Graph: A graph in which an edge connects each pair of graph vertices and each edge has a weight associated with it is known as a complete weighted graph. The number of spanning trees for a complete weighted graph with n vertices is n(n-2). Proof: Spanning tree is the subgraph of g
4 min read
Spanning Tree vs Minimum Spanning Tree
Spanning Tree (ST):A spanning tree of a graph is a subgraph that includes all the vertices of the original graph and is also a tree (a connected acyclic graph). The primary goal of a spanning tree is to connect all vertices with the minimum number of edges. Uses of Spanning Tree: STs are used in network design and routing algorithms to ensure conne
2 min read
Total number of possible Binary Search Trees and Binary Trees with n keys
Total number of possible Binary Search Trees with n different keys (countBST(n)) = Catalan number Cn = (2n)! / ((n + 1)! * n!) For n = 0, 1, 2, 3, â€Ķ values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, .... So are numbers of Binary Search Trees. Total number of possible Binary Trees with n different keys (countBT(n)) = countBST(n
12 min read
Problem Solving for Minimum Spanning Trees (Kruskal’s and Prim’s)
Minimum spanning Tree (MST) is an important topic for GATE. Therefore, we will discuss how to solve different types of questions based on MST. Before understanding this article, you should understand basics of MST and their algorithms (Kruskal’s algorithm and Prim’s algorithm). Type 1. Conceptual questions based on MST - There are some important pr
5 min read
Total number of possible Binary Search Trees using Catalan Number
Given an integer N, the task is to count the number of possible Binary Search Trees with N keys. Examples: Input: N = 2 Output: 2 For N = 2, there are 2 unique BSTs 1 2 \ / 2 1 Input: N = 9 Output: 4862 Approach: The number of binary search trees that will be formed with N keys can be calculated by simply evaluating the corresponding number in Cata
13 min read
Detect cycle in the graph using degrees of nodes of graph
Given a graph, the task is to detect a cycle in the graph using degrees of the nodes in the graph and print all the nodes that are involved in any of the cycles. If there is no cycle in the graph then print -1. Examples: Input: Output: 0 1 2 Approach: Recursively remove all vertices of degree 1. This can be done efficiently by storing a map of vert
11 min read
Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum
Given an array arr[] consisting of values of N vertices of an initially unconnected Graph and an integer M, the task is to connect some vertices of the graph with exactly M edges, forming only one connected component, such that no cycle can be formed and Bitwise AND of the connected vertices is maximum possible. Examples: Input: arr[] = {1, 2, 3, 4
9 min read
Algorithms | Graph Minimum Spanning Tree | Question 1
An undirected graph G(V, E) contains n ( n &gt; 2 ) nodes named v1 , v2 ,â€Ķ.vn. Two nodes vi , vj are connected if and only if 0 &lt; |i – j| &lt;= 2. Each edge (vi, vj ) is assigned a weight i + j. A sample graph with n = 4 is shown below. What will be the cost of the minimum spanning tree (MST) of such a graph with n nodes? (GATE CS 2011) (A) 1/12
1 min read
Algorithms | Graph Minimum Spanning Tree | Question 2
The length of the path from v5 to v6 in the MST of previous question with n = 10 is (A) 11 (B) 25 (C) 31 (D) 41 Answer: (C) Explanation: See question 2 of https://www.geeksforgeeks.org/data-structures-and-algorithms-set-27/ for explanation.Quiz of this Question
1 min read