必须保持 n 值的整数是否会增加 space 复杂度?

Can an integer which must hold the value of n contribute to space complexity?

如果算法需要一个可以包含数字 n 的整数(例如计算输入数组的大小),则该整数的顺序必须为 log(n) space(对吗?)。

如果这是唯一的 space 与 n 成比例,算法的 space 复杂度是否为 O(logn)?

这里 n 是有界的,即 n 将是 32 位有符号整数,因为数组大小有一定的限制。所以 log(32) 是有界的,它的 O(1)

形式上,这取决于您使用的计算模型。在经典的 random access machine model, with a fixed word size, simply storing the length n of the input indeed requires O(log n) space (and simple arithmetic on such numbers takes O(log n) time). On the other hand, in the transdichotomous model 中,假设字的大小随输入大小 n 呈对数增长,它只需要 O(1) space(和时间) .其他模型可能会产生其他答案。

在实践中,大多数 analysis of algorithms assumes that simple arithmetic on moderate-size integers (i.e. proportional to the input length) can be done in constant time and space. A practical reason for this, besides the fact that it simplifies analysis, is that in practice the logarithm of the input length cannot grow very large — even a computer capable of counting up from zero to, say, 2256, much less of reading that many bits of input, is probably forever beyond the means of humankind to build 使用已知的物理学。因此,对于任何可以想到的 现实 输入,您可以简单地假设具有 256 位字长的机器可以将输入的长度存储在单个字中(并且具有更小字长的机器单词大小仍然只需要少量恒定的单词数)。