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:
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Converting Between an Array and a Set in Java
Spring Boot - OAuth2 with JWT
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Guide to Java OutputStream
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Remove HTML tags from a file to extract only the TEXT
Java Program to Optimize Wire Length in Electrical Circuit
Guide to Character Encoding
Java Program to Implement Disjoint Set Data Structure
Spring Boot - Batch Service
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Create a Random Linear Extension for a DAG
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Java Program to Implement Sieve Of Atkin
Toán tử trong java
Java InputStream to String
Allow user:password in URL
Guide to Apache Commons CircularFifoQueue
Spring Autowiring of Generic Types
The Basics of Java Security
Java Program to Represent Graph Using Linked List
Tránh lỗi NullPointerException trong Java như thế nào?
Functional Interfaces in Java 8
Java Program to Solve TSP Using Minimum Spanning Trees
Spring Security OAuth Login with WebFlux
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Netflix Archaius with Various Database Configurations
Converting String to Stream of chars
Java Program to Implement Iterative Deepening
Java Convenience Factory Methods for Collections