Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Graph {
  4.     public static void main(String args[]) {
  5.         Scanner in = new Scanner(System.in);
  6.         System.out.print("Please insert a odd number: ");
  7.         int n = in.nextInt();
  8.         //validation n is odd.
  9.         while (n % 2 == 0) {
  10.             System.out.print("u have insert an even number, let's try again: ");
  11.             n = in.nextInt();
  12.         }
  13.         System.out.println("That's correct -> n= " + n + ';');
  14.         //create a NxN matrix;
  15.         int[][] adjancecyM = new int[n][n];
  16.         int i, j, cnt, random, sum = 0,degreeVertex=0,degreeMax=0,degreeMin=1000000,degreeTotal=0;
  17.         System.out.println("Which matrix would like to see ?");
  18.         System.out.println("--------------------------------");
  19.         System.out.print("1.G complete\n2.G cycle\n3.G random\nPlease enter a number: ");
  20.         cnt = in.nextInt();
  21.         switch (cnt) {
  22.             //G is the complete graph
  23.             case 1:
  24.                 for (i = 0; i < adjancecyM.length; ++i)
  25.                     for (j = 0; j < adjancecyM.length; ++j)
  26.                         if (i != j) adjancecyM[i][j] = adjancecyM[j][i] = 1;
  27.                 System.out.println("Edges: " + (n * (n - 1) / 2));
  28.                 break;
  29.             //G cycle
  30.             case 2:
  31.                 for (i = 0; i < adjancecyM.length - 1; ++i) {
  32.                     adjancecyM[i][i + 1] = 1;
  33.                 }
  34.                 adjancecyM[adjancecyM.length - 1][0] = 1;
  35.  
  36.                 System.out.println("Edges: " + n);
  37.                 break;
  38.             //G random
  39.             case 3:
  40.                 for (i = 0; i < adjancecyM.length; ++i)
  41.                     for (j = 0; j < adjancecyM.length; ++j) {
  42.                         if (((int) (Math.random() * 2) == 1) && i != j) {
  43.                             adjancecyM[i][j] = 1;
  44.                             sum++;
  45.                         } else
  46.                             adjancecyM[i][j] = 0;
  47.                     }
  48.                 System.out.println();
  49.                 if (sum > 1 && sum % 2 == 1) System.out.println("Edges: " + ((sum + 1) / 2));
  50.                 else if (sum > 1 && sum % 2 == 0) System.out.println("Edges: " + sum);
  51.                 else
  52.                     System.out.println("Graf doesn't create");
  53.                 break;
  54.  
  55.             default:
  56.                 System.out.println("Pick another number!");
  57.         }
  58.         //Afisare
  59.         System.out.println("Adjancecy Matrix");
  60.         System.out.println("----------------");
  61.         for (i = 0; i < adjancecyM.length; ++i) {
  62.             for (j = 0; j < adjancecyM.length; ++j)
  63.                 System.out.print(adjancecyM[i][j] + " ");
  64.             System.out.println();
  65.         }
  66.         //minimum and max degrees of a vertex
  67.         for (i = 0; i < adjancecyM.length; i++)
  68.         {
  69.             degreeVertex=0;
  70.             for(j=0 ; j < adjancecyM.length ; j++)
  71.                 if(adjancecyM[i][j] == 1){
  72.                     degreeVertex++;
  73.                     degreeTotal++;
  74.                 }
  75.  
  76.                 if(degreeVertex > degreeMax) degreeMax=i;
  77.                 if(degreeVertex < degreeMin) degreeMin=i;
  78.                 System.out.println("For vertex: " + i + " degree is " + degreeVertex);
  79.         }
  80.         System.out.println("Max vertex: " + degreeMax + " and Min vertex: " +degreeMin);
  81.         if(degreeTotal == 2*sum) System.out.println("Equal!");
  82.             else
  83.             System.out.println("Degrees != 2*Edges");
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement