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:

Spring Boot - Runners
Java Program for Topological Sorting in Graphs
Java Program to Solve the 0-1 Knapsack Problem
Spring Data Reactive Repositories with MongoDB
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Primitive Type Streams in Java 8
Validations for Enum Types
Spring Boot - Actuator
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Print only Odd Numbered Levels of a Tree
HttpClient 4 – Send Custom Cookie
Checking for Empty or Blank Strings in Java
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Updating your Password
Java Program to Implement Selection Sort
Java Program to Check whether Graph is a Bipartite using DFS
How to Read HTTP Headers in Spring REST Controllers
Find the Registered Spring Security Filters
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java – Reader to String
New Features in Java 15
Vòng lặp for, while, do-while trong Java
Java Program to Convert a Decimal Number to Binary Number using Stacks
DynamoDB in a Spring Boot Application Using Spring Data
Guide to Java 8’s Collectors
Injecting Prototype Beans into a Singleton Instance in Spring
Java Program to Implement the MD5 Algorithm
TreeSet và sử dụng Comparable, Comparator trong java
Limiting Query Results with JPA and Spring Data JPA
Spring Boot - Quick Start
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm