Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author 18201147
- */
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.LinkedList;
- import java.util.Iterator;
- public class Task3 {
- public static int[][] undirectedMatrix(String filename) {
- File file;
- Scanner sc;
- try {
- file = new File(filename);
- sc = new Scanner(file);
- } catch(FileNotFoundException fnfe) {
- System.out.println(filename + " doesn't exist!");
- return null;
- }
- if(!(sc.hasNext() && sc.hasNextInt())) {
- System.out.println("Invalid input format");
- return null;
- }
- int size = sc.nextInt();
- int[][] matrix = new int[size][size];
- while(sc.hasNextInt()) {
- int start = sc.nextInt();
- int end = sc.nextInt();
- matrix[start][end] = 1;
- matrix[end][start] = 1;
- }
- sc.close();
- return matrix;
- }
- public static int[][] directedMatrix(String filename) {
- File file;
- Scanner sc;
- try {
- file = new File(filename);
- sc = new Scanner(file);
- } catch(FileNotFoundException fnfe) {
- System.out.println(filename + " doesn't exist!");
- return null;
- }
- if(!(sc.hasNext() && sc.hasNextInt())) {
- System.out.println("Invalid input format");
- return null;
- }
- int size = sc.nextInt();
- int[][] matrix = new int[size][size];
- while(sc.hasNextInt()) {
- int start = sc.nextInt();
- int end = sc.nextInt();
- matrix[start][end] = 1;
- }
- sc.close();
- return matrix;
- }
- public static LinkedList<Integer>[] undirectedList(String filename) {
- File file;
- Scanner sc;
- try {
- file = new File(filename);
- sc = new Scanner(file);
- } catch(FileNotFoundException fnfe) {
- System.out.println(filename + " doesn't exist!");
- return null;
- }
- if(!(sc.hasNext() && sc.hasNextInt())) {
- System.out.println("Invalid input format");
- return null;
- }
- int size = sc.nextInt();
- LinkedList<Integer>[] collection = new LinkedList[size];
- for(int i = 0; i < size; i++) collection[i] = new LinkedList<>();
- while(sc.hasNextInt()) {
- int start = sc.nextInt();
- int end = sc.nextInt();
- collection[start].add(end);
- collection[end].add(start);
- }
- sc.close();
- return collection;
- }
- public static LinkedList<Integer>[] directedList(String filename){
- File file;
- Scanner sc;
- try {
- file = new File(filename);
- sc = new Scanner(file);
- } catch(FileNotFoundException fnfe) {
- System.out.println(filename + " doesn't exist!");
- return null;
- }
- if(!(sc.hasNext() && sc.hasNextInt())) {
- System.out.println("Invalid input format");
- return null;
- }
- int size = sc.nextInt();
- LinkedList<Integer>[] collection = new LinkedList[size];
- for(int i = 0; i < size; i++) collection[i] = new LinkedList<>();
- while(sc.hasNextInt()) {
- int start = sc.nextInt();
- int end = sc.nextInt();
- collection[start].add(end);
- }
- sc.close();
- return collection;
- }
- public static void printMatrix(int[][] matrix) {
- if(matrix == null) return;
- System.out.print(" ");
- for(int i = 0; i < matrix.length; i++)
- System.out.print(i + " ");
- System.out.println();
- for(int i = 0; i < matrix.length; i++) {
- System.out.print(i + " ");
- for(int j = 0; j < matrix[i].length; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static void printAdjList(LinkedList<Integer>[] collection) {
- if(collection == null) return;
- for(int i = 0; i < collection.length; i++) {
- System.out.print(i + " -> ");
- Iterator<Integer> itr = collection[i].iterator();
- while(itr.hasNext()) {
- System.out.print(itr.next());
- if(itr.hasNext()) System.out.print(" --> ");
- }
- System.out.println();
- }
- }
- public static void printOutDegOnly(String filename) {
- File file;
- Scanner sc;
- try {
- file = new File(filename);
- sc = new Scanner(file);
- } catch(FileNotFoundException fnfe) {
- System.out.println(filename + " doesn't exist!");
- return;
- }
- if(!(sc.hasNext() && sc.hasNextInt())) {
- System.out.println("Invalid input format");
- return;
- }
- int size = sc.nextInt();
- int[] outdegs = new int[size];
- while(sc.hasNextInt()) {
- int start = sc.nextInt();
- int end = sc.nextInt();
- outdegs[start]++;
- outdegs[end]++;
- }
- sc.close();
- for(int i = 0; i < size; i++) {
- System.out.println(i + " --> " + outdegs[i]);
- }
- }
- public static void printOutInDeg(String filename) {
- File file;
- Scanner sc;
- try {
- file = new File(filename);
- sc = new Scanner(file);
- } catch(FileNotFoundException fnfe) {
- System.out.println(filename + " doesn't exist!");
- return;
- }
- if(!(sc.hasNext() && sc.hasNextInt())) {
- System.out.println("Invalid input format");
- return;
- }
- int size = sc.nextInt();
- int[] outdegs = new int[size];
- int[] indegs = new int[size];
- while(sc.hasNextInt()) {
- int start = sc.nextInt();
- int end = sc.nextInt();
- outdegs[start]++;
- indegs[end]++;
- }
- sc.close();
- for(int i = 0; i < size; i++) {
- System.out.printf("%d --> %d/%d\n", i, outdegs[i], indegs[i]);
- }
- }
- public static void main(String[] args) {
- final String path = System.getProperty("user.dir") + "\\src\\input.txt";
- System.out.println("Undirected Matrix");
- int[][] mat1 = undirectedMatrix(path);
- printMatrix(mat1);
- System.out.println("\nUndirected List");
- LinkedList<Integer>[] adjlist1 = undirectedList(path);
- printAdjList(adjlist1);
- System.out.println("\nUndirected Out degrees only");
- printOutDegOnly(path);
- System.out.println("\nDirected Matrix");
- int[][] mat2 = directedMatrix(path);
- printMatrix(mat2);
- System.out.println("\nDirected List");
- LinkedList<Integer>[] adjlist2 = directedList(path);
- printAdjList(adjlist2);
- System.out.println("\nDirected Out In degrees");
- printOutInDeg(path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement