** 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)
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](http://img.homeworklib.com/questions/db0ca080-be6e-11eb-b937-2b2c4377f3c1.png?x-oss-process=image/resize,w_560)
Divide-and-conquer matrix multiplication:
Algorithm:

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));
}
}** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....
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...
Java Question: Write a multithread Java program that performs matrix multiplication. In the program, you need to calculate each element in the result matrix in a separate thread. The code should be generic with regard to matrix size. The sizes of three matrices should be stored in variable and used. The two operand matrices and the result matrix should be printed. Thanks
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...
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...
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...
The goal in this assignment is to perform various matrix multiplications. Input matrices A and B are read from a file. Output matrices A*A, A*B, B*A and B*B are written to file. -Ask the user to enter the input and output file names. -Read the input data matrices A and B from the input file. (A sample input file is shown in Appendix A) -Read the number of rows n. -Read matrix A, which has n rows and...
Please create a C program with the following C code declares variable mtx of matrix type, and dynamically allocates memory for mtx. It will then get keyboard input for the N-by-N matrix. Subsequently, the program de-allocates (free) the memory of the matrix. A. Your task is to write the codes to dynamically allocate and de-allocate the memory for the matrix. B. Subsequently, you have to modify the above code so that now the program will first ask the user to...
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. 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...
The goal in this assignment is to perform various matrix multiplications. Input matrices A and B are read from a file. Output matrices A*A, A*B, B*A and B*B are written to file. Code Requirements: -Include the problem statement in the top of the code (copy and paste this document and enclose in comments) -Include comments in the code -Ask the user to enter the input and output file names. -Read the input data matrices A and B from the input...