Pregunta

How can i write a pseudocode to calculate elements of matrix ?

The following algorithm finds the sum S of two n-by-n matrices A[0..n-1, 0..n-1] and B[0..n-1, 0..n-1]. To add two matrices, we add their corresponding elements. The resulting matrix S[0..n-1, 0..n-1] is an n-by-n matrix with elements computed by the formula:

S[i, j] = A[i, j] + B[i, j]

Write the pseudocode for calculating the elements of matrix S.

ALGORITHM addMatrices(A[0..n-1,0..n-1], B[0..n-1, 0..n-1])
// Input: Two n-by-n matrices A and B
// Output: Matrix S = A + B

(i) What is the algorithm’s basic operation?

(ii) How many times is the basic operation executed?

(iii)What is the class O(…) the algorithm belongs to?

¿Fue útil?

Solución

  1. Algorithms basic operation is to access each element using two loops and add each element of 'A' matrix to each element of 'B' matrix designated by 'i' and 'j', and construct another matrix 'S' using it.

    'S' denotes the sum matrix of 'A' and 'B' matrices.

  2. The basic operation is executed n2 times, where 'n' is the order of the matrices.
    For each i ∈ {0 , ... , n1-1} and each j ∈ {0 , ... , n2-1} where 'n1' and 'n2' are matrices order.

  3. From 2nd part we denote the Big-O Notation complexity to be n2 or n1·n2 depending upon the order of the matrices.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top