Question

** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....

** Use Java programming language

Program the following algorithms in JAVA:

A. Classical matrix multiplication

B. Divide-and-conquer matrix multiplication


In order to obtain more accurate results, the algorithms should be tested with the same matrices of different sizes many times. The total time spent is then divided by the number of times the algorithm is performed to obtain the time taken to solve the given instance.

For example, you randomly generate 1000 sets of input data for size_of_n=16. For each set of input data, you run it 20 times and you get the average time for this particular set of input data by averaging them. After getting 1000 average time results for all 1000 sets of input data, you can find the overall average time for size_of_n =16. Let the matrix size be n x n. Carry out a complete test of your algorithms with n = 2, 4, 8, 16, 32, 64, 128, 256, … (up to the largest size of n that your computer can handle)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Classical matrix multiplication:

public class Multiply {

    public static void main(String[] args) 
   {
        int r1 = 2, c1 = 3;                      // dimensions of first matrix 
        int r2 = 3, c2 = 2;                      // dimensions of second matrix
        int[][] firstMatrix = { {3, -2, 5}, {3, 0, 4} };          // elements of first matrix
        int[][] secondMatrix = { {2, 3}, {-9, 0}, {0, 4} };       //elements of second matrix

                                        
        int[][] product = new int[r1][c2];                  // Mutliplying Two matrices
        for(int i = 0; i < r1; i++)
        {
            for (int j = 0; j < c2; j++) 
            {
                for (int k = 0; k < c1; k++) 
                {
                    product[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
                }
             }
         }

        // Displaying the result


        System.out.println("Product of two matrices is: ");
        for(int[] row : product) 
        {
            for (int column : row) 
            {
                System.out.print(column + "    ");
            }
            System.out.println();
        }


    }

}

screenshot:

public class Multiply { 1 2 3 public static void main(String[] args) { int rl = 2 cl = 3; // dimensions of first matrix int r

Divide-and-conquer matrix multiplication:

  Algorithm:

  SQUARE-MATRIX-MULTIPLY-RECURSIVE(A,B) 1 n = A.rows 2 let C be a new n x n matrix 3 if n == 1 4 Cu = au·bu 5 else partition A,

Code:

import java.io.*;

public class MatrixMultiplication {

        static int[][] multiply(int[][] matrix1, int[][] matrix2) {
                int size = matrix1.length;
                int[][] product = new int[size][size];
                if (size == 1) {
                        product[0][0] = matrix1[0][0] * matrix2[0][0];
                } else if (size % 2 == 0) {
                        int [][] a = new int[size/2][size/2];
                        int [][] b = new int[size/2][size/2];
                        int [][] c = new int[size/2][size/2];
                        int [][] d = new int[size/2][size/2];
                        int [][] e = new int[size/2][size/2];
                        int [][] f = new int[size/2][size/2];
                        int [][] g = new int[size/2][size/2];
                        int [][] h = new int[size/2][size/2];
                        for (int i = 0; i < size; i++) {
                                for (int j = 0; j < size; j++) {
                                        if (i < size / 2 & j < size / 2) {
                                                a[i][j] = matrix1[i][j];
                                                e[i][j] = matrix2[i][j];
                                        } else if (i < size / 2 && j >= size / 2) {
                                                b[i][j - size/2] = matrix1[i][j];
                                                f[i][j - size/2] = matrix2[i][j];
                                        } else if (i >= size / 2 && j < size / 2) {
                                                c[i - size/2][j] = matrix1[i][j];
                                                g[i - size/2][j] = matrix2[i][j];
                                        } else {
                                                d[i - size/2][j - size/2] = matrix1[i][j];
                                                h[i - size/2][j - size/2] = matrix2[i][j];
                                        }
                                }
                        }
                        int[][] p1 = multiply(a, minus(f, h));
                        int[][] p2 = multiply(add(a, b), h);
                        int[][] p3 = multiply(add(c, d), e);
                        int[][] p4 = multiply(d, minus(g, e));
                        int[][] p5 = multiply(add(a, d), add(e, h));
                        int[][] p6 = multiply(minus(b, d), add(g, h));
                        int[][] p7 = multiply(minus(a, c), add(e, f));
                        int[][] c1 = add(minus(add(p5, p4), p2), p6);
                        int[][] c2 = add(p1, p2);
                        int[][] c3 = add(p3, p4);
                        int[][] c4 = minus(add(p1, p5), add(p3, p7));
                        for (int i = 0; i < size/2; i++) {
                                for (int j = 0; j < size/2; j++) {
                                        product[i][j] = c1[i][j];
                                        product[i][j + size/2] = c2[i][j];
                                        product[i + size/2][j] = c3[i][j];
                                        product[i + size/2][j + size/2] = c4[i][j];
                                }
                        }
                } else {
                        int[][] copy1 = new int[size + 1][size + 1];
                        int[][] copy2 = new int[size + 1][size + 2];
                        for (int i = 0; i < size; i++) {
                                for (int j = 0; j < size; j++) {
                                        copy1[i][j] = matrix1[i][j];
                                        copy2[i][j] = matrix2[i][j];
                                }
                        }
                        int[][] copy3 = multiply(copy1, copy2);
                        for (int i = 0; i < size; i++) {
                                for (int j = 0; j < size; j++) {
                                        product[i][j] = copy3[i][j];
                                }
                        }
                }
                return product;
                
        }

        static int[][] add(int[][] matrix1, int[][] matrix2) {
                int size = matrix1.length;
                int[][] sum = new int[size][size]; 
                for (int i = 0; i < size; i++) {
                        for (int j = 0; j < size; j++) {
                                sum[i][j] = matrix1[i][j] + matrix2[i][j];
                        }
                }
                return sum;
        }
        
        static int[][] minus(int[][] matrix1, int[][] matrix2) {
                int size = matrix1.length;
                int[][] difference = new int[size][size]; 
                for (int i = 0; i < size; i++) {
                        for (int j = 0; j < size; j++) {
                                difference[i][j] = matrix1[i][j] - matrix2[i][j];
                        }
                }
                return difference;
        }
        
        static void print(int[][] matrix) {
                int size = matrix.length;
                for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                        System.out.printf("%4d", matrix[i][j]);
                }
                System.out.println();
        }
        }
        
    public static void main(String[] args) throws FileNotFoundException {
        int[][] first = {
                        {1, 1, 1},
                        {1, 1, 1},
                        {1, 1, 1}
        };
        int[][] second = {
                        {1, 2, 3, 4, 5},
                        {1, 1, 1, 1, 1},
                        {1, 1, 1, 1, 1},
                        {5, 4, 3, 2, 1},
                        {1, 1, 1, 1, 1}
        };
        int[][] test1 = multiply(first, first);
        print(multiply(second, second));
    }
}
Add a comment
Know the answer?
Add Answer to:
** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I already solved part A and I just need help with part B 1. Matrix Multiplication...

    I already solved part A and I just need help with part B 1. Matrix Multiplication The product of two n xn matrices X and Y is a third n x n matrix 2 = XY, with entries 2 - 21; = xixYk x k=1 There are n’ entries to compute, each one at a cost of O(n). The formula implies an algorithm with O(nº) running time. For a long time this was widely believed to be the best running...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Big Number Arithmetic C Program The goal is to perform several multiplication functions/algorithims using unlimited length...

    Big Number Arithmetic C Program The goal is to perform several multiplication functions/algorithims using unlimited length digits (BigInts) and compare their execution times. The program requires a header file which includes all functions needed by the .c file. Required functions: ClassicalMult (bigInt A, bigInt B, int base) - this function multiplies two big integers using the classical multiplication algorithim. KaratsubaMult (bigInt A, bigInt B, int base) - this function multiplies two big integers using the Karatsuba multiplication algorithim. ToomMult (bigInt...

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

  • Create sets of integers with the following characteristics with array sizes of 100,000, 500,000 and 1000,000...

    Create sets of integers with the following characteristics with array sizes of 100,000, 500,000 and 1000,000 1. Set where no integer is repeated 2. Set where the range is low and the integers repeat Compare the performances of the following sorting algorithms. You can either write the algorithms on your own or modify algorithms obtained from elsewhere. Create a table with the rows being the different algorithms and the columns being the inputs. Run each input 10 times and report...

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Suppose the following is a divide-and-conquer algorithm for some problem. "Make the input of size n...

    Suppose the following is a divide-and-conquer algorithm for some problem. "Make the input of size n into 3 subproblems of sizes n/2 , n/4 , n/8 , respectively with O(n) time; Recursively call on these subproblems; and then combine the results in O(n) time. The recursive call returns when the problems become of size 1 and the time in this case is constant." (a) Let T(n) denote the worst-case running time of this approach on the problem of size n....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT