Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Graph {
  4.     public static void main(String args[]) {
  5.         int n; //n-ul va fi preluat de la 'linia de comanda'
  6.  
  7.         //verificare nr. argumente
  8.         if (args.length > 0)
  9.             n = Integer.parseInt(args[0]);
  10.  
  11.         //validation n is odd.
  12.         if (n % 2 != 0) {
  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, random, sum = 0, degreeVertex = 0, degreeMax = 0, degreeMin = 1000000, degreeTotal = 0;
  17.  
  18.             //G complete
  19.             int[][] graphComplete ( int nrVertex){
  20.                 for (i = 0; i < adjancecyM.length; ++i)
  21.                     for (j = 0; j < adjancecyM.length; ++j)
  22.                         if (i != j) adjancecyM[i][j] = adjancecyM[j][i] = 1;
  23.                 System.out.println("Edges: " + (n * (n - 1) / 2));
  24.             }
  25.             /**
  26.              //G cycle
  27.  
  28.              for (i = 0; i < adjancecyM.length - 1; ++i) {
  29.              adjancecyM[i][i + 1] = 1;
  30.              }
  31.              adjancecyM[adjancecyM.length - 1][0] = 1;
  32.  
  33.              System.out.println("Edges: " + n);
  34.  
  35.              //G random
  36.  
  37.              for (i = 0; i < adjancecyM.length; ++i)
  38.              for (j = 0; j < adjancecyM.length; ++j) {
  39.              if (((int) (Math.random() * 2) == 1) && i != j) {
  40.              adjancecyM[i][j] = 1;
  41.              sum++;
  42.              } else
  43.              adjancecyM[i][j] = 0;
  44.              }
  45.              System.out.println();
  46.              if (sum > 1 && sum % 2 == 1) System.out.println("Edges: " + ((sum + 1) / 2));
  47.              else if (sum > 1 && sum % 2 == 0) System.out.println("Edges: " + sum);
  48.              else
  49.              System.out.println("Graf doesn't create");
  50.              }
  51.              */
  52.             void printMatrix(){
  53.                 System.out.println("Adjancecy Matrix");
  54.                 System.out.println("----------------");
  55.                 for (i = 0; i < adjancecyM.length; ++i) {
  56.                     for (j = 0; j < adjancecyM.length; ++j)
  57.                         System.out.print(adjancecyM[i][j] + " ");
  58.                     System.out.println();
  59.                 }
  60.             }
  61.             //minimum and max degrees of a vertex
  62.             for (i = 0; i < adjancecyM.length; i++) {
  63.                 degreeVertex = 0;
  64.                 for (j = 0; j < adjancecyM.length; j++)
  65.                     if (adjancecyM[i][j] == 1) {
  66.                         degreeVertex++;
  67.                         degreeTotal++;
  68.                     }
  69.  
  70.                 if (degreeVertex > degreeMax) degreeMax = i;
  71.                 if (degreeVertex < degreeMin) degreeMin = i;
  72.                 System.out.println("For vertex: " + i + " degree is " + degreeVertex);
  73.             }
  74.             System.out.println("Max vertex: " + degreeMax + " and Min vertex: " + degreeMin);
  75.             if (degreeTotal == 2 * sum) System.out.println("Equal!");
  76.             else
  77.                 System.out.println("Degrees != 2*Edges");
  78.         } else
  79.             System.out.print("u have insert an even number, let's try again!");
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement