Advertisement
Guest User

Task3

a guest
Jan 27th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.54 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /**
  8.  *
  9.  * @author 18201147
  10.  */
  11. import java.util.Scanner;
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. import java.util.LinkedList;
  15. import java.util.Iterator;
  16.  
  17. public class Task3 {
  18.     public static int[][] undirectedMatrix(String filename) {
  19.         File file;
  20.         Scanner sc;
  21.        
  22.         try {
  23.             file = new File(filename);
  24.             sc = new Scanner(file);
  25.         } catch(FileNotFoundException fnfe) {
  26.             System.out.println(filename + " doesn't exist!");
  27.             return null;
  28.         }
  29.        
  30.         if(!(sc.hasNext() && sc.hasNextInt())) {
  31.             System.out.println("Invalid input format");
  32.             return null;
  33.         }
  34.         int size = sc.nextInt();
  35.         int[][] matrix = new int[size][size];
  36.        
  37.         while(sc.hasNextInt()) {
  38.             int start = sc.nextInt();
  39.             int end = sc.nextInt();
  40.             matrix[start][end] = 1;
  41.             matrix[end][start] = 1;
  42.         }
  43.         sc.close();
  44.         return matrix;
  45.     }
  46.     public static int[][] directedMatrix(String filename) {
  47.         File file;
  48.         Scanner sc;
  49.        
  50.         try {
  51.             file = new File(filename);
  52.             sc = new Scanner(file);
  53.         } catch(FileNotFoundException fnfe) {
  54.             System.out.println(filename + " doesn't exist!");
  55.             return null;
  56.         }
  57.        
  58.         if(!(sc.hasNext() && sc.hasNextInt())) {
  59.             System.out.println("Invalid input format");
  60.             return null;
  61.         }
  62.         int size = sc.nextInt();
  63.         int[][] matrix = new int[size][size];
  64.        
  65.         while(sc.hasNextInt()) {
  66.             int start = sc.nextInt();
  67.             int end = sc.nextInt();
  68.             matrix[start][end] = 1;
  69.         }
  70.         sc.close();
  71.         return matrix;
  72.     }
  73.    
  74.     public static LinkedList<Integer>[] undirectedList(String filename) {
  75.         File file;
  76.         Scanner sc;
  77.        
  78.         try {
  79.             file = new File(filename);
  80.             sc = new Scanner(file);
  81.         } catch(FileNotFoundException fnfe) {
  82.             System.out.println(filename + " doesn't exist!");
  83.             return null;
  84.         }
  85.        
  86.         if(!(sc.hasNext() && sc.hasNextInt())) {
  87.             System.out.println("Invalid input format");
  88.             return null;
  89.         }
  90.         int size = sc.nextInt();
  91.         LinkedList<Integer>[] collection = new LinkedList[size];
  92.         for(int i = 0; i < size; i++) collection[i] = new LinkedList<>();
  93.        
  94.         while(sc.hasNextInt()) {
  95.             int start = sc.nextInt();
  96.             int end = sc.nextInt();
  97.             collection[start].add(end);
  98.             collection[end].add(start);
  99.         }
  100.         sc.close();
  101.         return collection;
  102.     }
  103.     public static LinkedList<Integer>[] directedList(String filename){
  104.         File file;
  105.         Scanner sc;
  106.        
  107.         try {
  108.             file = new File(filename);
  109.             sc = new Scanner(file);
  110.         } catch(FileNotFoundException fnfe) {
  111.             System.out.println(filename + " doesn't exist!");
  112.             return null;
  113.         }
  114.        
  115.         if(!(sc.hasNext() && sc.hasNextInt())) {
  116.             System.out.println("Invalid input format");
  117.             return null;
  118.         }
  119.         int size = sc.nextInt();
  120.         LinkedList<Integer>[] collection = new LinkedList[size];
  121.         for(int i = 0; i < size; i++) collection[i] = new LinkedList<>();
  122.        
  123.         while(sc.hasNextInt()) {
  124.             int start = sc.nextInt();
  125.             int end = sc.nextInt();
  126.             collection[start].add(end);
  127.         }
  128.         sc.close();
  129.         return collection;
  130.     }
  131.    
  132.     public static void printMatrix(int[][] matrix) {
  133.         if(matrix == null) return;
  134.         System.out.print("  ");
  135.         for(int i = 0; i < matrix.length; i++)
  136.             System.out.print(i + " ");
  137.         System.out.println();
  138.  
  139.         for(int i = 0; i < matrix.length; i++) {
  140.             System.out.print(i + " ");
  141.             for(int j = 0; j < matrix[i].length; j++) {
  142.                 System.out.print(matrix[i][j] + " ");
  143.             }
  144.             System.out.println();
  145.         }
  146.     }
  147.     public static void printAdjList(LinkedList<Integer>[] collection) {
  148.         if(collection == null) return;
  149.         for(int i = 0; i < collection.length; i++) {
  150.             System.out.print(i + " -> ");
  151.             Iterator<Integer> itr = collection[i].iterator();
  152.             while(itr.hasNext()) {
  153.                 System.out.print(itr.next());
  154.                 if(itr.hasNext()) System.out.print(" --> ");
  155.             }
  156.             System.out.println();
  157.         }
  158.     }
  159.    
  160.     public static void printOutDegOnly(String filename) {
  161.         File file;
  162.         Scanner sc;
  163.        
  164.         try {
  165.             file = new File(filename);
  166.             sc = new Scanner(file);
  167.         } catch(FileNotFoundException fnfe) {
  168.             System.out.println(filename + " doesn't exist!");
  169.             return;
  170.         }
  171.        
  172.         if(!(sc.hasNext() && sc.hasNextInt())) {
  173.             System.out.println("Invalid input format");
  174.             return;
  175.         }
  176.         int size = sc.nextInt();
  177.         int[] outdegs = new int[size];
  178.        
  179.         while(sc.hasNextInt()) {
  180.             int start = sc.nextInt();
  181.             int end = sc.nextInt();
  182.             outdegs[start]++;
  183.             outdegs[end]++;
  184.         }
  185.         sc.close();
  186.        
  187.         for(int i = 0; i < size; i++) {
  188.             System.out.println(i + " --> " + outdegs[i]);
  189.         }
  190.     }
  191.     public static void printOutInDeg(String filename) {
  192.         File file;
  193.         Scanner sc;
  194.        
  195.         try {
  196.             file = new File(filename);
  197.             sc = new Scanner(file);
  198.         } catch(FileNotFoundException fnfe) {
  199.             System.out.println(filename + " doesn't exist!");
  200.             return;
  201.         }
  202.        
  203.         if(!(sc.hasNext() && sc.hasNextInt())) {
  204.             System.out.println("Invalid input format");
  205.             return;
  206.         }
  207.         int size = sc.nextInt();
  208.         int[] outdegs = new int[size];
  209.         int[] indegs = new int[size];
  210.        
  211.         while(sc.hasNextInt()) {
  212.             int start = sc.nextInt();
  213.             int end = sc.nextInt();
  214.             outdegs[start]++;
  215.             indegs[end]++;
  216.         }
  217.         sc.close();
  218.        
  219.         for(int i = 0; i < size; i++) {
  220.             System.out.printf("%d --> %d/%d\n", i, outdegs[i], indegs[i]);
  221.         }
  222.     }
  223.    
  224.     public static void main(String[] args) {
  225.         final String path = System.getProperty("user.dir") + "\\src\\input.txt";
  226.  
  227.         System.out.println("Undirected Matrix");
  228.         int[][] mat1 = undirectedMatrix(path);
  229.         printMatrix(mat1);
  230.        
  231.         System.out.println("\nUndirected List");
  232.         LinkedList<Integer>[] adjlist1 = undirectedList(path);
  233.         printAdjList(adjlist1);
  234.        
  235.         System.out.println("\nUndirected Out degrees only");
  236.         printOutDegOnly(path);
  237.        
  238.         System.out.println("\nDirected Matrix");
  239.         int[][] mat2 = directedMatrix(path);
  240.         printMatrix(mat2);
  241.  
  242.         System.out.println("\nDirected List");
  243.         LinkedList<Integer>[] adjlist2 = directedList(path);
  244.         printAdjList(adjlist2);
  245.        
  246.         System.out.println("\nDirected Out In degrees");
  247.         printOutInDeg(path);
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement