Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Graph {
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- System.out.print("Please insert a odd number: ");
- int n = in.nextInt();
- //validation n is odd.
- while (n % 2 == 0) {
- System.out.print("u have insert an even number, let's try again: ");
- n = in.nextInt();
- }
- System.out.println("That's correct -> n= " + n + ';');
- //create a NxN matrix;
- int[][] adjancecyM = new int[n][n];
- int i, j, cnt, random, sum = 0,degreeVertex=0,degreeMax=0,degreeMin=1000000,degreeTotal=0;
- System.out.println("Which matrix would like to see ?");
- System.out.println("--------------------------------");
- System.out.print("1.G complete\n2.G cycle\n3.G random\nPlease enter a number: ");
- cnt = in.nextInt();
- switch (cnt) {
- //G is the complete graph
- case 1:
- for (i = 0; i < adjancecyM.length; ++i)
- for (j = 0; j < adjancecyM.length; ++j)
- if (i != j) adjancecyM[i][j] = adjancecyM[j][i] = 1;
- System.out.println("Edges: " + (n * (n - 1) / 2));
- break;
- //G cycle
- case 2:
- for (i = 0; i < adjancecyM.length - 1; ++i) {
- adjancecyM[i][i + 1] = 1;
- }
- adjancecyM[adjancecyM.length - 1][0] = 1;
- System.out.println("Edges: " + n);
- break;
- //G random
- case 3:
- for (i = 0; i < adjancecyM.length; ++i)
- for (j = 0; j < adjancecyM.length; ++j) {
- if (((int) (Math.random() * 2) == 1) && i != j) {
- adjancecyM[i][j] = 1;
- sum++;
- } else
- adjancecyM[i][j] = 0;
- }
- System.out.println();
- if (sum > 1 && sum % 2 == 1) System.out.println("Edges: " + ((sum + 1) / 2));
- else if (sum > 1 && sum % 2 == 0) System.out.println("Edges: " + sum);
- else
- System.out.println("Graf doesn't create");
- break;
- default:
- System.out.println("Pick another number!");
- }
- //Afisare
- System.out.println("Adjancecy Matrix");
- System.out.println("----------------");
- for (i = 0; i < adjancecyM.length; ++i) {
- for (j = 0; j < adjancecyM.length; ++j)
- System.out.print(adjancecyM[i][j] + " ");
- System.out.println();
- }
- //minimum and max degrees of a vertex
- for (i = 0; i < adjancecyM.length; i++)
- {
- degreeVertex=0;
- for(j=0 ; j < adjancecyM.length ; j++)
- if(adjancecyM[i][j] == 1){
- degreeVertex++;
- degreeTotal++;
- }
- if(degreeVertex > degreeMax) degreeMax=i;
- if(degreeVertex < degreeMin) degreeMin=i;
- System.out.println("For vertex: " + i + " degree is " + degreeVertex);
- }
- System.out.println("Max vertex: " + degreeMax + " and Min vertex: " +degreeMin);
- if(degreeTotal == 2*sum) System.out.println("Equal!");
- else
- System.out.println("Degrees != 2*Edges");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement