Advertisement
mokahato

Untitled

Apr 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1.  
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6.  
  7. public class RouteMatrixThread extends Thread {
  8.  
  9.     private static int[][][] ma;
  10.     private static int[][][] mb;
  11.     private static int[][][] result;
  12.  
  13.     private int lower;
  14.     private int upper;
  15.    
  16.     public static int[][][] getResult() {
  17.         return result;
  18.     }
  19.    
  20.     public void print() {
  21.     System.out.println("Result Matrix:");
  22.     for (int[][] row : result) {
  23.         for (int[] list : row)
  24.         if (list == null)
  25.             System.out.print("[] ");
  26.         else {
  27.             System.out.print("[");
  28.             for (int i=0; i<list.length; i++) {
  29.             System.out.print(list[i]);
  30.             if (i < list.length-1)
  31.                 System.out.print(",");
  32.             }
  33.             System.out.print("] ");
  34.         }
  35.         System.out.println();
  36.     }
  37.     }
  38.  
  39.     /* The method returns null, if any of the following conditions apply:
  40.        - either of the input arrays is null;
  41.        - the last integer of the first argument array is not equal
  42.          to the first integer of the second argument array;
  43.        - the concatenated path would form a loop (i.e. it has duplicates).
  44.  
  45.        Use your Project 4 implementation.
  46.      */
  47.     private int[] concatenate (int[] a, int[] b) {
  48.         int [] a2 = Arrays.copyOf(a, a.length);
  49.         int [] b2 = Arrays.copyOf(b, b.length);
  50.         Arrays.sort(a2);
  51.         Arrays.sort(b2);
  52.         int countA = 0;
  53.         int countB = 0;
  54.         int [] c = new int[a.length+b.length-1];
  55.         for (int i = 0; i < a2.length-1; i++)
  56.             if (a2[i] == a2[i+1])
  57.                 countA = countA + 1;
  58.         for (int i = 0; i < b2.length-1; i++)
  59.             if (b2[i] == b2[i+1])
  60.                 countB = countB + 1;
  61.         if (a != null && b != null) {
  62.             if (a[a.length-1]==b[0]) {
  63.                 if (countA == 0 && countB == 0) {
  64.                     System.arraycopy(a, 0, c, 0, a.length-1);
  65.                     System.arraycopy(b, 0, c, a.length-1, b.length);
  66.                 }
  67.             }
  68.         }
  69.         return c;
  70.     }    
  71.     public RouteMatrixThread(int lower, int upper) {
  72.         this.lower = lower;
  73.         this.upper = upper;
  74.     }
  75.    
  76.     private int[] shortest(int[][] a) {
  77.         int[] lengths = new int[a.length];
  78.         for (int x = 0; x < a.length; x++) {
  79.             if (a[x]!= null)
  80.                 lengths[x] = a[x].length;
  81.         }
  82.         int shortest = lengths[0];
  83.         int shortestVariable = 0;
  84.         if (lengths.length!= 0) {
  85.             for (int z = 0; z < lengths.length; z++)
  86.             {
  87.                 if(z!=lengths.length-1 && lengths[z] > lengths[z+1])
  88.                 {
  89.                     shortest = lengths[z];
  90.                     shortestVariable = z;
  91.                 }
  92.             }
  93.             return a[shortestVariable];
  94.         }
  95.         else
  96.             return null;
  97.     }
  98.    
  99.     public static void main(String[] args) {
  100.     if (args.length < 2) {
  101.         System.out.print("You need to provide a file name and the number of threads!");
  102.         System.out.println(" Exiting...");
  103.         System.exit(0);
  104.     }
  105.    
  106.     ma = null;
  107.     mb = null;
  108.     int size = 0;
  109.  
  110.     // Read the two matrices from the input file
  111.     // =========================================
  112.     File file = new File(args[0]);
  113.     Scanner in = null;
  114.     try {
  115.         in = new Scanner(file);
  116.  
  117.         size = in.nextInt();
  118.         ma = new int[size][size][];
  119.         mb = new int[size][size][];
  120.         int row = 0;
  121.         int col = 0;
  122.         int round = 1;
  123.         int[][][] m = ma;
  124.         System.out.println("Reading in Matrix A...");
  125.         while( in.hasNextLine() && round <= 2) {
  126.         String line = in.nextLine();
  127.         if (line.length() <= 1)
  128.             continue;
  129.         String[] path = line.split(" ");
  130.         for (String s : path) {
  131.             if (s.length() == 2) { // s is "[]"
  132.             m[row][col] = null; // unnecessary, only making it explicit
  133.             }
  134.             if (s.length() > 2) {
  135.             s = s.substring(1, s.length()-1); // drop opening and closing square brackets []
  136.             String[] node = s.split(",");
  137.             m[row][col] = new int[node.length];
  138.             for (int k=0; k<node.length; k++) {
  139.                 m[row][col][k] = Integer.parseInt(node[k]);
  140.             }
  141.             }
  142.             col++;
  143.             if (col == size)
  144.             col = 0;
  145.         }
  146.         row++;
  147.         if (row == size) {
  148.             if (round == 1) { // time to start reading in the second matrix
  149.             row = 0;
  150.             m = mb;
  151.             System.out.print("\n\nReading in Matrix B...");
  152.             }
  153.             round++;
  154.         }
  155.         }
  156.     }
  157.     catch( FileNotFoundException e ) {
  158.         System.out.println("File " + file.getName() + " not found!");
  159.     }
  160.     finally { if( in != null ) in.close(); }
  161.  
  162.     System.out.println();
  163.  
  164.     int n = Integer.parseInt(args[1]); // the number of threads the user specified
  165.    
  166.     // Create Threads to Compute the Result Matrix
  167.     RouteMatrixThread[] threads = new RouteMatrixThread[n];
  168.     int count_lower = 0;
  169.     int count_upper = size;
  170.     for (int i=0; i<threads.length; i++ ) {
  171.         threads[i] = new RouteMatrixThread(count_lower, count_upper);
  172.         threads[i].start();
  173.         count_lower += size;
  174.         count_upper += size;
  175.     }try {
  176.         for (int i=0; i<threads.length;i++)
  177.             threads[i].join();
  178.     }
  179.     catch(InterruptedException e) {
  180.         System.out.print("Thread didn't finish");
  181.     }
  182.     // ===========================================
  183.    
  184.    
  185.    
  186.    
  187.     /*int[][][] mc = new int[size][size][3];
  188.     int[][] conc= new int[ma.length][ma.length];
  189.  
  190.     for (int i = 0; i < ma.length; i++) {
  191.         for (int k = 0; k< mb[i].length; k++)
  192.         {
  193.             for (int j = 0;j< ma[k].length; j++) {
  194.                 conc[k][j] = concatenate(ma[i][k], mb[k][j]);
  195.                
  196.                 if(i = ma.length-1) {
  197.                     mc[i][j] = shortest(conc);
  198.                 }
  199.             }
  200.         }*/
  201.     int[][][] a = {{{5},{5},{5}},
  202.                    {{5},{5},{5}}};
  203.     }
  204.    
  205.    
  206.  
  207.    
  208.  
  209.     @Override
  210.     public void run() {
  211.         int[][] temp = new int[upper][upper];
  212.        
  213.        
  214.         System.out.println(lower+ " "+ upper);
  215.         for (int i=lower; i<upper; i++) {
  216.             for (int j = 0; j < ma[i].length; j++)
  217.                 for (int k=0; k < mb.length; k++) {
  218.                     temp[i] = concatenate(ma[i][k], mb[k][j]);
  219.                 }
  220.     }
  221.         for (int i=lower; i<upper; i++)
  222.             for (int j = 0; j < result[i].length; j++)
  223.                 for (int k=0; k < mb.length; k++) {
  224.                     result[i][j] = shortest(temp);
  225.                 }
  226.                    
  227.        
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement