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.
public class CheckGraphConstuction
public static Integer sum(List<Integer> list)
Integer sum = 0;
for (Integer integer : list)
sum += integer;
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices: ");
Integer n = sc.nextInt();
.println("Enter the Degree Sequence: <Degree sequence is always in non-increasing order>");
List<Integer> sequence = new ArrayList<Integer>();
while (n > 0)
if (sum(sequence) % 2 == 0)
.println("Graph can be constructed using the given sequence G=("
+ ", "
+ (sum(sequence) / 2)
$ 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.
Sanfoundry Global Education & Learning Series – 1000 Java Programs.