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:
String Processing with Apache Commons Lang 3
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Sorting Query Results with Spring Data
Java Program to Generate Random Hexadecimal Byte
Spring Security 5 for Reactive Applications
Java 14 Record Keyword
Java Scanner hasNext() vs. hasNextLine()
Java Program to Implement Ternary Heap
Spring Boot - Database Handling
Java Program to Implement PriorityBlockingQueue API
Hướng dẫn sử dụng Lớp FilePermission trong java
Giới thiệu SOAP UI và thực hiện test Web Service
Introduction to Spliterator in Java
Java Program to Implement Trie
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Deep Learning Essentials - Yusuke Sugomori
Life Cycle of a Thread in Java
Một số nguyên tắc, định luật trong lập trình
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Implement LinkedTransferQueue API
LinkedHashSet trong java
Spring Security – Reset Your Password
Mapping a Dynamic JSON Object with Jackson
Join and Split Arrays and Collections in Java
Redirect to Different Pages after Login with Spring Security
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Spring Cloud – Adding Angular
Spring Boot - Google Cloud Platform
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Write/Read cookies using HTTP and Read a file from the internet
The Basics of Java Security