【发布时间】:2016-10-13 00:07:03
【问题描述】:
对于 MakeMatrix,显示 T(n) 为 O(n) 且 S(n) 为 O(n^2),它创建一个方阵并仅将对角线元素设置为零。 (忽略 malloc 的时间)
MakeMatrix(size):
A = malloc(size * size * sizeof(int))
for i from 0 to size -1
A[i,i] =0
return A
我想我明白为什么 T(n) 是线性 O(n) 因为只有 1 个 for 循环,但为什么空间复杂度会是 O(n^2)?
【问题讨论】:
-
因为您仍然必须将整个矩阵存储在内存中,所以 O(n^2)。
-
请使用正确的格式...“MakeMatrix(size) A = malloc(sizesizesizeof(int)) for i from 0 to size -1 A[i,i] =0 return A”相当不可读。
标签: algorithm time big-o complexity-theory space