Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class RouteMatrixThread extends Thread {
- private static int[][][] ma;
- private static int[][][] mb;
- private static int[][][] result;
- private int lower;
- private int upper;
- public static int[][][] getResult() {
- return result;
- }
- public void print() {
- System.out.println("Result Matrix:");
- for (int[][] row : result) {
- for (int[] list : row)
- if (list == null)
- System.out.print("[] ");
- else {
- System.out.print("[");
- for (int i=0; i<list.length; i++) {
- System.out.print(list[i]);
- if (i < list.length-1)
- System.out.print(",");
- }
- System.out.print("] ");
- }
- System.out.println();
- }
- }
- /* The method returns null, if any of the following conditions apply:
- - either of the input arrays is null;
- - the last integer of the first argument array is not equal
- to the first integer of the second argument array;
- - the concatenated path would form a loop (i.e. it has duplicates).
- Use your Project 4 implementation.
- */
- private int[] concatenate (int[] a, int[] b) {
- int [] a2 = Arrays.copyOf(a, a.length);
- int [] b2 = Arrays.copyOf(b, b.length);
- Arrays.sort(a2);
- Arrays.sort(b2);
- int countA = 0;
- int countB = 0;
- int [] c = new int[a.length+b.length-1];
- for (int i = 0; i < a2.length-1; i++)
- if (a2[i] == a2[i+1])
- countA = countA + 1;
- for (int i = 0; i < b2.length-1; i++)
- if (b2[i] == b2[i+1])
- countB = countB + 1;
- if (a != null && b != null) {
- if (a[a.length-1]==b[0]) {
- if (countA == 0 && countB == 0) {
- System.arraycopy(a, 0, c, 0, a.length-1);
- System.arraycopy(b, 0, c, a.length-1, b.length);
- }
- }
- }
- return c;
- }
- public RouteMatrixThread(int lower, int upper) {
- this.lower = lower;
- this.upper = upper;
- }
- private int[] shortest(int[][] a) {
- int[] lengths = new int[a.length];
- for (int x = 0; x < a.length; x++) {
- if (a[x]!= null)
- lengths[x] = a[x].length;
- }
- int shortest = lengths[0];
- int shortestVariable = 0;
- if (lengths.length!= 0) {
- for (int z = 0; z < lengths.length; z++)
- {
- if(z!=lengths.length-1 && lengths[z] > lengths[z+1])
- {
- shortest = lengths[z];
- shortestVariable = z;
- }
- }
- return a[shortestVariable];
- }
- else
- return null;
- }
- public static void main(String[] args) {
- if (args.length < 2) {
- System.out.print("You need to provide a file name and the number of threads!");
- System.out.println(" Exiting...");
- System.exit(0);
- }
- ma = null;
- mb = null;
- int size = 0;
- // Read the two matrices from the input file
- // =========================================
- File file = new File(args[0]);
- Scanner in = null;
- try {
- in = new Scanner(file);
- size = in.nextInt();
- ma = new int[size][size][];
- mb = new int[size][size][];
- int row = 0;
- int col = 0;
- int round = 1;
- int[][][] m = ma;
- System.out.println("Reading in Matrix A...");
- while( in.hasNextLine() && round <= 2) {
- String line = in.nextLine();
- if (line.length() <= 1)
- continue;
- String[] path = line.split(" ");
- for (String s : path) {
- if (s.length() == 2) { // s is "[]"
- m[row][col] = null; // unnecessary, only making it explicit
- }
- if (s.length() > 2) {
- s = s.substring(1, s.length()-1); // drop opening and closing square brackets []
- String[] node = s.split(",");
- m[row][col] = new int[node.length];
- for (int k=0; k<node.length; k++) {
- m[row][col][k] = Integer.parseInt(node[k]);
- }
- }
- col++;
- if (col == size)
- col = 0;
- }
- row++;
- if (row == size) {
- if (round == 1) { // time to start reading in the second matrix
- row = 0;
- m = mb;
- System.out.print("\n\nReading in Matrix B...");
- }
- round++;
- }
- }
- }
- catch( FileNotFoundException e ) {
- System.out.println("File " + file.getName() + " not found!");
- }
- finally { if( in != null ) in.close(); }
- System.out.println();
- int n = Integer.parseInt(args[1]); // the number of threads the user specified
- // Create Threads to Compute the Result Matrix
- RouteMatrixThread[] threads = new RouteMatrixThread[n];
- int count_lower = 0;
- int count_upper = size;
- for (int i=0; i<threads.length; i++ ) {
- threads[i] = new RouteMatrixThread(count_lower, count_upper);
- threads[i].start();
- count_lower += size;
- count_upper += size;
- }try {
- for (int i=0; i<threads.length;i++)
- threads[i].join();
- }
- catch(InterruptedException e) {
- System.out.print("Thread didn't finish");
- }
- // ===========================================
- /*int[][][] mc = new int[size][size][3];
- int[][] conc= new int[ma.length][ma.length];
- for (int i = 0; i < ma.length; i++) {
- for (int k = 0; k< mb[i].length; k++)
- {
- for (int j = 0;j< ma[k].length; j++) {
- conc[k][j] = concatenate(ma[i][k], mb[k][j]);
- if(i = ma.length-1) {
- mc[i][j] = shortest(conc);
- }
- }
- }*/
- int[][][] a = {{{5},{5},{5}},
- {{5},{5},{5}}};
- }
- @Override
- public void run() {
- int[][] temp = new int[upper][upper];
- System.out.println(lower+ " "+ upper);
- for (int i=lower; i<upper; i++) {
- for (int j = 0; j < ma[i].length; j++)
- for (int k=0; k < mb.length; k++) {
- temp[i] = concatenate(ma[i][k], mb[k][j]);
- }
- }
- for (int i=lower; i<upper; i++)
- for (int j = 0; j < result[i].length; j++)
- for (int k=0; k < mb.length; k++) {
- result[i][j] = shortest(temp);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement