20,481 views
60 60 votes

Two matrices $M_1$ and $M_2$ are to be stored in arrays $A$ and $B$ respectively. Each array can be stored either in row-major or column-major order in contiguous memory locations. The time complexity of an algorithm to compute $M_1 \times M_2$ will be

  1. best if $A$ is in row-major, and $B$ is in column-major order

  2. best if both are in row-major order

  3. best if both are in column-major order

  4. independent of the storage scheme

7 Answers

Best answer
78 78 votes

D is correct

Here time complexity is asked, for each access of array element it will be constant,

So the time complexity will not depend upon storage. If at all program execution time is asked option a is true.

edited by
33 33 votes

Running time of an algorithm is always independent of the storage scheme. While computing the running time of an algorithm we assume that to access any element time taken is same. So Answer is D.

But if the question asked best time complexity in which of the following implementation (not algorithm) then Option a is correct.

edited by
7 7 votes

ans a)

Matrix multiplication takes the rows of M2 and columns of M2 in order. So, if the array A is stored in row-major and array B is stored in column-major, when the first element is accessed, the neighbouring elements (which will be immediately needed) will also be brought to cache. So, this storage scheme is best for matrix multiplication in terms of execution time.

 

5 5 votes
  • The choice between row-major and column-major affects memory access patterns and cache locality, which can influence the actual runtime performance of the algorithm. For example, accessing elements sequentially in memory (as opposed to non-sequential) is generally faster because of better cache utilization.

  • However, the time complexity (which is about the number of operations and not about the details of memory access or cache) remains the same and is independent of storage scheme.

3 3 votes
The question asks about time complexity, not time taken by the program and for time complexity, it doesn't matter how we store array elements or which data structure we use, we always need to access same number of elements of M1 and M2 to multiply the matrices. It is always constant or O(1) time to do element access, thus the constants may differ for different schemes, but not the time complexity.
2 2 votes

The correct answer is D) independent of the storage scheme.


Why Asymptotic Complexity is Independent of Storage

From a purely theoretical, asymptotic complexity perspective, the way a matrix is stored in memory does not change the total number of fundamental operations (multiplications and additions) required to compute the result.

The standard algorithm for multiplying two matrices always performs:

  • multiplications

  • additions

Regardless of whether the matrices are stored in row-major or column-major order, this exact number of arithmetic operations must be performed.

Practical Performance vs. Theoretical Complexity

  • Performance: In the real world, matching the storage layout to the access pattern (A row-major, B column-major) drastically improves performance by reducing cache misses. This is a crucial concept in systems programming and performance optimization.

 

  • Theoretical Complexity: GATE questions on this topic are typically asking about the fundamental asymptotic complexity, which only counts the number of operations as a function of . In this context, memory access time is considered constant for each operation, so caching effects are ignored.

Therefore, since the number of operations is always the same, the asymptotic time complexity is independent of the storage scheme.

Answer:
Position:
Show:

Related questions

16 16 votes
7 answers 7 answers
14.9k
14.9k views
Kathleen asked Sep 18, 2014
14,899 views
The problem $\text{3-SAT}$ and $\text{2-SAT}$ are both in $\text{P}$both $\text{NP}$ complete$\text{NP}$-complete and in $\text{P}$ respectivelyundecidable and $\text{NP}...
126 126 votes
8 answers 8 answers
51.5k
51.5k views
Kathleen asked Sep 18, 2014
51,544 views
The tightest lower bound on the number of comparisons, in the worst case, for comparison-based sorting is of the order of$n$$n^2$$n \log n$$n \log^2n$
40 40 votes
7 answers 7 answers
28.2k
28.2k views
Kathleen asked Sep 18, 2014
28,199 views
The time complexity of the following C function is (assume $n 0$)int recursive (int n) { if(n == 1) return (1); else return (recursive (n-1) + recursive (n-1)); }$O(n)$$...
85 85 votes
13 answers 13 answers
32.8k
32.8k views
Kathleen asked Sep 18, 2014
32,831 views
Let $A[1,\ldots,n]$ be an array storing a bit ($1$ or $0$) at each location, and $f(m)$ is a function whose time complexity is $\Theta(m)$. Consider the following program...