Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence

This is a java program to check graph construction is possible or not using given degree sequence. If the sum of degree is even graph construction is possible, not otherwise.

Here is the source code of the Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

package com.sanfoundry.combinatorial;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class CheckGraphConstuction
{
    public static Integer sum(List<Integer> list)
    {
        Integer sum = 0;
        for (Integer integer : list)
        {
            sum += integer;
        }
        return sum;
    }
 
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of vertices: ");
        Integer n = sc.nextInt();
        System.out
                .println("Enter the Degree Sequence: <Degree sequence is always in non-increasing order>");
        List<Integer> sequence = new ArrayList<Integer>();
        while (n > 0)
        {
            sequence.add(sc.nextInt());
            n--;
        }
        System.out.println(sequence.toString());
        if (sum(sequence) % 2 == 0)
        {
            System.out
                    .println("Graph can be constructed using the given sequence G=("
                            + sequence.size()
                            + ", "
                            + (sum(sequence) / 2)
                            + ").");
        }
        sc.close();
    }
}

Output:

$ javac CheckGraphConstuction.java
$ java CheckGraphConstuction
 
Enter the number of vertices: 
7
Enter the Degree Sequence: <Degree sequence is always in non-increasing order>
5 3 3 2 2 1 0
[5, 3, 3, 2, 2, 1, 0]
Graph can be constructed using the given sequence G=(7, 8).
 
Enter the number of vertices: 
3
Enter the Degree Sequence: <Degree sequence is always in non-increasing order>
3 3 1
[3, 3, 1]
no soultion exists.

Related posts:

The Difference Between Collection.stream().forEach() and Collection.forEach()
Injecting Prototype Beans into a Singleton Instance in Spring
How to Change the Default Port in Spring Boot
Từ khóa throw và throws trong Java
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Performance Difference Between save() and saveAll() in Spring Data
The Spring @Controller and @RestController Annotations
Guide To CompletableFuture
A Guide to Spring Cloud Netflix – Hystrix
Java Program to Implement Fermat Factorization Algorithm
Collect a Java Stream to an Immutable Collection
Java Program to Implement Hash Tables with Double Hashing
Java Program to Implement Uniform-Cost Search
Upload and Display Excel Files with Spring MVC
Convert String to int or Integer in Java
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Implement String Matching Using Vectors
Getting Started with Forms in Spring MVC
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Spring Boot - Flyway Database
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Implement the MD5 Algorithm
REST Pagination in Spring
Read an Outlook MSG file
Find the Registered Spring Security Filters
Practical Java Examples of the Big O Notation
New Features in Java 10
Java Program to Implement Ternary Tree
A Guide to Java HashMap
Java Program to Implement Circular Singly Linked List
Apache Commons Collections BidiMap
Java Program to Generate Random Numbers Using Middle Square Method