Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) {
- if (args.length != 2) {
- System.out.println("Invalid number of arguments. The program has ended");
- System.exit(97);
- } else {
- Graph currentGraph = new Graph(Integer.parseInt(args[0]), args[1]);
- currentGraph.printDetails();
- }
- }
- }
- class Graph {
- private int numberOfNodes;
- private boolean[][] adjacency;
- private double creationTime;
- private String smallDelta = "\u03B4";
- private String bigDelta = "\u0394";
- Graph(int numberOfNodes, String creationOption) {
- creationTime = System.nanoTime();
- if (numberOfNodes < 1) {
- System.out.println("Number of nodes is too low. The program has ended.");
- System.exit(99);
- }
- adjacency = new boolean[numberOfNodes][numberOfNodes];
- this.numberOfNodes = numberOfNodes;
- if (creationOption.equals("random")) {
- this.randomizeMatrix();
- } else if (creationOption.equals("cycle")) {
- this.createCycle();
- } else if (creationOption.equals("complete")) {
- this.createComplete();
- } else {
- System.out.println("Creation option should be either <random>,<cycle> or <complete>. The program has ended");
- System.exit(98);
- }
- }
- private void randomizeMatrix() {
- for (int i = 0; i < this.numberOfNodes - 1; i++) {
- for (int j = i + 1; j < this.numberOfNodes; j++) {
- double aux = Math.random();
- if (aux < 0.5) {
- this.adjacency[i][j] = false;
- } else {
- this.adjacency[i][j] = true;
- }
- this.adjacency[j][i] = this.adjacency[i][j];
- }
- }
- for (int i = 0; i < this.numberOfNodes; i++) {
- this.adjacency[i][i] = false;
- }
- }
- private void createComplete() {
- for (int i = 0; i < numberOfNodes; i++) {
- for (int j = 0; j < numberOfNodes; j++) {
- if (i != j) {
- this.adjacency[i][j] = true;
- }
- }
- }
- }
- private void createCycle(){
- int[] cyclePermutation=createPermutation();
- for (int i = 0; i < numberOfNodes-1 ; i++) {
- adjacency[cyclePermutation[i]][cyclePermutation[i+1]]=true;
- adjacency[cyclePermutation[i+1]][cyclePermutation[i]]=true;
- }
- adjacency[cyclePermutation[numberOfNodes-1]][cyclePermutation[0]]=true;
- adjacency[cyclePermutation[0]][cyclePermutation[numberOfNodes-1]]=true;
- }
- private int[] createPermutation(){
- List<Integer> permutation=new ArrayList<>();
- int[] permutationArray=new int[numberOfNodes];
- for (int i = 0; i < numberOfNodes; i++) {
- permutation.add(i);
- }
- java.util.Collections.shuffle(permutation);
- for (int i = 0; i < numberOfNodes; i++) {
- permutationArray[i]=permutation.get(i);
- }
- return permutationArray;
- }
- private void printMatrix() {
- String fullCircle = "\u26AB";
- String strokeCircle = "\u26AA";
- for (int i = 0; i < this.numberOfNodes; i++) {
- for (int j = 0; j < this.numberOfNodes; j++) {
- if (this.adjacency[i][j]) {
- System.out.print(fullCircle + " ");
- } else {
- System.out.print(strokeCircle + " ");
- }
- }
- System.out.print("\n");
- }
- }
- private void smallNumberPrint() {
- this.printMatrix();
- System.out.println("Number of vertices is " + this.numberOfVertices());
- System.out.println(this.smallDelta + " = " + this.getMinDegrees());
- System.out.println(this.bigDelta + " = " + this.getMaxDegrees());
- System.out.println("The graph is regular : " + this.isRegular());
- System.out.println("The graph was created with errors : " + this.hasErrors());
- }
- private void bigNumberPrint() {
- System.out.println("Number of vertices is " + this.numberOfVertices());
- System.out.println(this.smallDelta + " = " + this.getMinDegrees());
- System.out.println(this.bigDelta + " = " + this.getMaxDegrees());
- System.out.println("The graph is regular : " + this.isRegular());
- System.out.println("The graph was created with errors : " + this.hasErrors());
- double stopTime = System.nanoTime();
- double totalTime = (stopTime - creationTime) / 1_000_000_000;
- System.out.println(" The program finished in " + totalTime + " seconds");
- }
- void printDetails() {
- if (numberOfNodes > 100) {
- bigNumberPrint();
- } else {
- smallNumberPrint();
- }
- }
- private int numberOfVertices() {
- int countVertices = 0;
- for (int i = 0; i < this.numberOfNodes; i++) {
- for (int j = 0; j < this.numberOfNodes; j++) {
- if (this.adjacency[i][j]) {
- countVertices++;
- }
- }
- }
- countVertices /= 2;
- return countVertices;
- }
- private int getMaxDegrees() {
- int currentDegree;
- int maxDegree = 0;
- for (int i = 0; i < numberOfNodes; i++) {
- currentDegree = 0;
- for (int j = 0; j < numberOfNodes; j++) {
- if (this.adjacency[i][j]) {
- currentDegree++;
- }
- }
- if (currentDegree > maxDegree) {
- maxDegree = currentDegree;
- }
- }
- return maxDegree;
- }
- private int getMinDegrees() {
- int currentDegree;
- int minDegree = numberOfNodes;
- for (int i = 0; i < numberOfNodes; i++) {
- currentDegree = 0;
- for (int j = 0; j < numberOfNodes; j++) {
- if (this.adjacency[i][j]) {
- currentDegree++;
- }
- }
- if (currentDegree < minDegree) {
- minDegree = currentDegree;
- }
- }
- return minDegree;
- }
- private boolean isRegular() {
- int maxDegree = this.getMaxDegrees();
- int currentDegree;
- for (int i = 0; i < numberOfNodes; i++) {
- currentDegree = 0;
- for (int j = 0; j < numberOfNodes; j++) {
- if (adjacency[i][j]) {
- currentDegree++;
- }
- }
- if (currentDegree != maxDegree)
- return false;
- }
- return true;
- }
- private boolean hasErrors() {
- int degreeSum = 0;
- for (int i = 0; i < numberOfNodes; i++) {
- for (int j = 0; j < numberOfNodes; j++) {
- if (adjacency[i][j]) {
- degreeSum++;
- }
- }
- }
- if (degreeSum == 2 * numberOfVertices())
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement