Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         if (args.length != 2) {
  8.             System.out.println("Invalid number of arguments. The program has ended");
  9.             System.exit(97);
  10.         } else {
  11.             Graph currentGraph = new Graph(Integer.parseInt(args[0]), args[1]);
  12.             currentGraph.printDetails();
  13.         }
  14.     }
  15. }
  16.  
  17. class Graph {
  18.     private int numberOfNodes;
  19.     private boolean[][] adjacency;
  20.     private double creationTime;
  21.     private String smallDelta = "\u03B4";
  22.     private String bigDelta = "\u0394";
  23.  
  24.     Graph(int numberOfNodes, String creationOption) {
  25.  
  26.         creationTime = System.nanoTime();
  27.         if (numberOfNodes < 1) {
  28.             System.out.println("Number of nodes is too low. The program has ended.");
  29.             System.exit(99);
  30.         }
  31.         adjacency = new boolean[numberOfNodes][numberOfNodes];
  32.         this.numberOfNodes = numberOfNodes;
  33.  
  34.  
  35.         if (creationOption.equals("random")) {
  36.             this.randomizeMatrix();
  37.         } else if (creationOption.equals("cycle")) {
  38.             this.createCycle();
  39.         } else if (creationOption.equals("complete")) {
  40.             this.createComplete();
  41.         } else {
  42.             System.out.println("Creation option should be either <random>,<cycle> or <complete>. The program has ended");
  43.             System.exit(98);
  44.         }
  45.  
  46.     }
  47.  
  48.     private void randomizeMatrix() {
  49.  
  50.  
  51.         for (int i = 0; i < this.numberOfNodes - 1; i++) {
  52.             for (int j = i + 1; j < this.numberOfNodes; j++) {
  53.                 double aux = Math.random();
  54.                 if (aux < 0.5) {
  55.                     this.adjacency[i][j] = false;
  56.                 } else {
  57.                     this.adjacency[i][j] = true;
  58.                 }
  59.                 this.adjacency[j][i] = this.adjacency[i][j];
  60.             }
  61.         }
  62.         for (int i = 0; i < this.numberOfNodes; i++) {
  63.             this.adjacency[i][i] = false;
  64.         }
  65.     }
  66.  
  67.     private void createComplete() {
  68.         for (int i = 0; i < numberOfNodes; i++) {
  69.             for (int j = 0; j < numberOfNodes; j++) {
  70.                 if (i != j) {
  71.                     this.adjacency[i][j] = true;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     private void createCycle(){
  78.     int[] cyclePermutation=createPermutation();
  79.         for (int i = 0; i < numberOfNodes-1 ; i++) {
  80.             adjacency[cyclePermutation[i]][cyclePermutation[i+1]]=true;
  81.             adjacency[cyclePermutation[i+1]][cyclePermutation[i]]=true;
  82.         }
  83.         adjacency[cyclePermutation[numberOfNodes-1]][cyclePermutation[0]]=true;
  84.         adjacency[cyclePermutation[0]][cyclePermutation[numberOfNodes-1]]=true;
  85.     }
  86.  
  87.     private int[] createPermutation(){
  88.         List<Integer> permutation=new ArrayList<>();
  89.         int[] permutationArray=new int[numberOfNodes];
  90.         for (int i = 0; i < numberOfNodes; i++) {
  91.             permutation.add(i);
  92.         }
  93.         java.util.Collections.shuffle(permutation);
  94.         for (int i = 0; i < numberOfNodes; i++) {
  95.             permutationArray[i]=permutation.get(i);
  96.         }
  97.         return permutationArray;
  98.     }
  99.  
  100.     private void printMatrix() {
  101.  
  102.         String fullCircle = "\u26AB";
  103.         String strokeCircle = "\u26AA";
  104.  
  105.         for (int i = 0; i < this.numberOfNodes; i++) {
  106.             for (int j = 0; j < this.numberOfNodes; j++) {
  107.                 if (this.adjacency[i][j]) {
  108.                     System.out.print(fullCircle + " ");
  109.                 } else {
  110.                     System.out.print(strokeCircle + " ");
  111.                 }
  112.             }
  113.             System.out.print("\n");
  114.         }
  115.     }
  116.  
  117.     private void smallNumberPrint() {
  118.         this.printMatrix();
  119.         System.out.println("Number of vertices is " + this.numberOfVertices());
  120.         System.out.println(this.smallDelta + " = " + this.getMinDegrees());
  121.         System.out.println(this.bigDelta + " = " + this.getMaxDegrees());
  122.         System.out.println("The graph is regular : " + this.isRegular());
  123.         System.out.println("The graph was created with errors : " + this.hasErrors());
  124.  
  125.     }
  126.  
  127.     private void bigNumberPrint() {
  128.         System.out.println("Number of vertices is " + this.numberOfVertices());
  129.         System.out.println(this.smallDelta + " = " + this.getMinDegrees());
  130.         System.out.println(this.bigDelta + " = " + this.getMaxDegrees());
  131.         System.out.println("The graph is regular : " + this.isRegular());
  132.         System.out.println("The graph was created with errors : " + this.hasErrors());
  133.  
  134.         double stopTime = System.nanoTime();
  135.         double totalTime = (stopTime - creationTime) / 1_000_000_000;
  136.         System.out.println(" The program finished in " + totalTime + " seconds");
  137.  
  138.  
  139.     }
  140.  
  141.     void printDetails() {
  142.         if (numberOfNodes > 100) {
  143.             bigNumberPrint();
  144.         } else {
  145.             smallNumberPrint();
  146.         }
  147.     }
  148.  
  149.     private int numberOfVertices() {
  150.  
  151.         int countVertices = 0;
  152.  
  153.         for (int i = 0; i < this.numberOfNodes; i++) {
  154.             for (int j = 0; j < this.numberOfNodes; j++) {
  155.                 if (this.adjacency[i][j]) {
  156.                     countVertices++;
  157.                 }
  158.             }
  159.         }
  160.         countVertices /= 2;
  161.         return countVertices;
  162.     }
  163.  
  164.     private int getMaxDegrees() {
  165.         int currentDegree;
  166.         int maxDegree = 0;
  167.         for (int i = 0; i < numberOfNodes; i++) {
  168.             currentDegree = 0;
  169.             for (int j = 0; j < numberOfNodes; j++) {
  170.                 if (this.adjacency[i][j]) {
  171.                     currentDegree++;
  172.                 }
  173.             }
  174.             if (currentDegree > maxDegree) {
  175.                 maxDegree = currentDegree;
  176.             }
  177.         }
  178.         return maxDegree;
  179.     }
  180.  
  181.     private int getMinDegrees() {
  182.         int currentDegree;
  183.         int minDegree = numberOfNodes;
  184.         for (int i = 0; i < numberOfNodes; i++) {
  185.             currentDegree = 0;
  186.             for (int j = 0; j < numberOfNodes; j++) {
  187.                 if (this.adjacency[i][j]) {
  188.                     currentDegree++;
  189.                 }
  190.             }
  191.             if (currentDegree < minDegree) {
  192.                 minDegree = currentDegree;
  193.             }
  194.         }
  195.         return minDegree;
  196.     }
  197.  
  198.     private boolean isRegular() {
  199.         int maxDegree = this.getMaxDegrees();
  200.         int currentDegree;
  201.         for (int i = 0; i < numberOfNodes; i++) {
  202.             currentDegree = 0;
  203.             for (int j = 0; j < numberOfNodes; j++) {
  204.                 if (adjacency[i][j]) {
  205.                     currentDegree++;
  206.                 }
  207.             }
  208.             if (currentDegree != maxDegree)
  209.                 return false;
  210.         }
  211.         return true;
  212.     }
  213.  
  214.     private boolean hasErrors() {
  215.  
  216.         int degreeSum = 0;
  217.  
  218.         for (int i = 0; i < numberOfNodes; i++) {
  219.             for (int j = 0; j < numberOfNodes; j++) {
  220.                 if (adjacency[i][j]) {
  221.                     degreeSum++;
  222.                 }
  223.             }
  224.         }
  225.         if (degreeSum == 2 * numberOfVertices())
  226.             return false;
  227.         return true;
  228.     }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement