Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph

This Java program is to find the number of spanning trees in a Complete Bipartite graph. This can be calculated using the matrix tree theorem or Cayley’s formula.

Here is the source code of the Java program to ind the number of spanning trees in a Complete Bipartite graph. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.Scanner;
 
public class NumOfSpanningBipartite
{
    private int firstSetSize;
    private int secondSetSize;
 
    public int numberOfSpanningTree(int firstSetSize, int secondSetSize)
    {
        this.firstSetSize = firstSetSize;
        this.secondSetSize = secondSetSize;
        return (this.firstSetSize^(this.secondSetSize - 1)) *(this.secondSetSize ^ (this.firstSetSize -1)); 
    }
 
    public static void main(String...arg)
    {
        int m, n;
        Scanner scanner = new Scanner(System.in);
        System.out.println("enter the size of the bipartite graph (m and n)");
        m = scanner.nextInt();
        n = scanner.nextInt();
 
        NumOfSpanningBipartite bipartite = new  NumOfSpanningBipartite();
        System.out.println(" the number of spanning trees are  " + bipartite.numberOfSpanningTree(m, n));
        scanner.close();
    }
}
$javac NumOfSpanningBipartite.java
$java NumOfSpanningBipartite
enter the size of the bipartite graph (m and n)
2 2
 the number of spanning trees are  9

Related posts:

Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Spring Boot - File Handling
A Guide To UDP In Java
Testing an OAuth Secured API with Spring MVC
Java Program to Implement Sorted Array
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Hướng dẫn Java Design Pattern – DAO
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
An Introduction to ThreadLocal in Java
Tips for dealing with HTTP-related problems
JUnit 5 @Test Annotation
Returning Custom Status Codes from Spring Controllers
Hướng dẫn sử dụng String Format trong Java
Từ khóa throw và throws trong Java
Hướng dẫn Java Design Pattern – Dependency Injection
Quick Guide to @RestClientTest in Spring Boot
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Spring REST API with Protocol Buffers
Java Optional as Return Type
Java Program to Compute Cross Product of Two Vectors
Convert String to int or Integer in Java
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Spring Boot - Quick Start
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Spring Data JPA @Query
Java Program to Implement Solovay Strassen Primality Test Algorithm
Jackson vs Gson
Java Program to Perform the Sorting Using Counting Sort
Java Program to Implement Sorted Doubly Linked List
Lập trình đa luồng trong Java (Java Multi-threading)
Java Program to Check Multiplicability of Two Matrices
HttpClient 4 – Follow Redirects for POST