Question

In: Computer Science

The following algorithm multiplies two square matrices using a straightforward implementation of the definition of matrix...

The following algorithm multiplies two square matrices using a straightforward implementation of the definition of matrix multiplication.

(If you are unfamiliar with matrices, think of them as 2-dimensional arrays, and don’t worry, you don’t need to know how to do matrix multiplication to solve this problem)

1 2 3 4 5 6 7 8 9

10

ALGORITHM MatrixMultiplication(A[1..n, 1..n], B[1..n, 1..n])
// Multiply two square matrices of order n using the definition-based algorithm //Input: twon×nmatricesAandB
// Output: matrix C = AB
fori←1tondo

for j ← 1 to n do C[i, j] ← 0

for k ← 1 to n do

C[i, j] ← C[i, j] + (A[i, k] * B[k, j]) return C

The basic operations are the addition and multiplication of matrix elements at line 9. Determine the number of basic operations performed for two input matrices of size n × n. Write your answer in simplest form, if possible.

please try to explain how you did thi, thank you!

Solutions

Expert Solution

fori←1 to n do

for j ← 1 to n do

C[i, j] ← 0

for k ← 1 to n do

C[i, j] ← C[i, j] + (A[i, k] * B[k, j]) // basic operation

return C

If the matrix is of order n x n, the number of basic operations take place is n3. For example, if the matrix is of the order 2 x 2, the number of basic operations performed will be 23 =8 . If it is a 3 x 3 matrix then it will be 33 =27 and so on.

Example

Consider the 2 x 2 matrix A = 1 2 and B = 5 6

  3 4 1   2

The result will also be a 2 x 2 matrix ( 4 elements)

Row 1

Initially c[i,j=0

so in the first summation it will be 0 + 1 *5 =5 count 1

second summation will be 5+2*1 =7 count 2

First element in the resultant matrix will be 7

Row 1

Again c[i,j=0

For the second element the first summation will be 0+1*6=6 count 3

second summation will be 6+2*2 =10 count 4

So second element in first row will be 10

Row 2

Initially c[i,j=0

so in the first summation it will be 0 + 3 *5 =15 count 5

second summation will be 15+1*4 =19 count 6

So first element in the second row will be 19

Row 2

For the second element in row 2,  the first summation will be 0+3*6=18 count 7

second summation will be 18+4*2 =26 count 8

So second element in second row will be 26

All together the count of basic operations is 8 which is 23


Related Solutions

2. Using matrices, create an algorithm that takes a matrix of dimension N x N and...
2. Using matrices, create an algorithm that takes a matrix of dimension N x N and feed it in a spiral shape with the sequential number from 1 to N^2. Then do an algorithm in PSEint
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A...
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A for any square matrix A. Prove that X must be the identity matrix. Prove that for any invertible matrix A, the inverse matrix must be unique. Hint: Assume that there are two inverses and then show that they much in fact be the same matrix. Prove Theorem which shows that Gauss-Jordan Elimination produces the inverse matrix for any invertible matrix A. Your proof cannot...
Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n...
Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n x n each, find their multiplication matrix. Naive Method Following is a simple way to multiply two matrices.                void multiply(int A[][N], int B[][N], int C[][N]) {     for (int i = 0;   i < N; i++) {         for (int j = 0; j < N; j++) {             C[i][j] = 0;             for (int k = 0; k < N; k++) {                 C[i][j] += A[i][k]*B[k][j];             }...
Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices....
Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices. Provide a recursive implementation of the function void print_parenth(Matrix K[], int i, int j); that takes as input the matrix K of k values that are needed to construct the optimal parenthesization for Ai · · · Aj . Assume access to a print function that takes as input a string and prints its value. You may also assume a “+” operation for string...
Given the following two matrices: Matrix A that contains the marks of the college of engineering...
Given the following two matrices: Matrix A that contains the marks of the college of engineering students (250 Student) in 12 courses Matrix B that contains the number of credit hours of each course. Write a Matlab code to Print the letter grades (A to D) of student if you are given his ID and the course number from 1 to 12 Print his GPA in this semester. Gpa=sum(mark in subject i * subiect credit i) /total number of credit,...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the time it takes to run the program with different data sizes. Languages: Python Task: Create matrix multiplication Input: Size of square matrix.   Size should be    250, 500, 1000, 1500, 2000 Internals: Explicitly or implicitly allocate sufficient memory to hold three NxN floating point Matrices, using a random number generator -- populate two of the Matrices, Multiply the two matrices, putting the result into the...
For the following matrices, first find a basis for the column space of the matrix. Then...
For the following matrices, first find a basis for the column space of the matrix. Then use the Gram-Schmidt process to find an orthogonal basis for the column space. Finally, scale the vectors of the orthogonal basis to find an orthonormal basis for the column space. (a) [1 1 1, 1 0 2, 3 1 0, 0 0 4 ] b) [?1 6 6, 3 ?8 3, 1 ?2 6, 1 ?4 ?3 ]
What is the difference between multiplying a matrix times a vector and multiplying two matrices?
What is the difference between multiplying a matrix times a vector and multiplying two matrices?
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation...
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation of the function void print_path(Matrix D[], int n, int i, int j); that takes as input the array of matrices D[1], D[2], . . . , D[n − 1] that have been computed by the algorithm, and prints the optimal path from vertex i to vertex j. Hint: for convenience you may use the notation Dr ij to denote the value D[r][i, j], i.e....
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT