Let's consider the following algorithm to multiply squares matrix:
A
is a matrix of NxN
.
r_i, r_j
defines interval of rows. For example r_i = 2, r_j=3
means the second and the third rows. c_i, c_j
means the same as r_i, r_j
but for columns. We assume that N = 2^s
for some s
.
mul(A, B, r_i, r_j, c_i, c_j){
if(r_i != r_j){
r_m = floor((r_i+r_j)/2)
mul(A, B, r_i, r_m, c_i, c_j)
mul(A, B, r_m+1, r_j, c_i, c_j)
} else if(c_i != c_j){
c_m = floor((c_i+c_j)/2)
mul(A, B, r_i,r_j, c_i, c_m)
mul(A, B, r_i, r_j, c_m+1, c_j)
}else{
for i = 1 to N:
C[r_i][c_i] += A[r_i][i] * B[i][c_i]
}
}
And the most important:
Complexity of that algorithm takes: T(n) = 4T(n/2) + n = O(n^2)
And it is not correct. The correct answer is O(n^3). Why my computation is incorrect.