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.

  1.  
  2. package com.sanfoundry.combinatorial;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class CheckGraphConstuction
  9. {
  10.     public static Integer sum(List<Integer> list)
  11.     {
  12.         Integer sum = 0;
  13.         for (Integer integer : list)
  14.         {
  15.             sum += integer;
  16.         }
  17.         return sum;
  18.     }
  19.  
  20.     public static void main(String[] args)
  21.     {
  22.         Scanner sc = new Scanner(System.in);
  23.         System.out.println("Enter the number of vertices: ");
  24.         Integer n = sc.nextInt();
  25.         System.out
  26.                 .println("Enter the Degree Sequence: <Degree sequence is always in non-increasing order>");
  27.         List<Integer> sequence = new ArrayList<Integer>();
  28.         while (n > 0)
  29.         {
  30.             sequence.add(sc.nextInt());
  31.             n--;
  32.         }
  33.         System.out.println(sequence.toString());
  34.         if (sum(sequence) % 2 == 0)
  35.         {
  36.             System.out
  37.                     .println("Graph can be constructed using the given sequence G=("
  38.                             + sequence.size()
  39.                             + ", "
  40.                             + (sum(sequence) / 2)
  41.                             + ").");
  42.         }
  43.         sc.close();
  44.     }
  45. }

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.

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.