gpt4 book ai didi

algorithm - 复杂度是 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + log(2)) = O(log(n)) 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:06:09 26 4
gpt4 key购买 nike

作为家庭作业,我被要求在 O(log(n)) 中编写一个算法,我可以计算出我编写的算法的复杂度为 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + log(2)).

我认为它是 O(n),因为它大致是 log(n)*O(log(n)) = O(n)。但我不确定。我知道这里不欢迎作业问题,但我真的不知道还有什么方法可以找出它的复杂性。谷歌搜索对我没有帮助。

指定:n in N and n = pow(2, c), c in N

最佳答案

不,这不是 O(log n)。它是 O((log n)2).

O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + log(2))
= O(log(n * n/2 * n/4 * n/8 * ... * 2)) using log multiplication rule
= O(log(n * n * n * ... * n / 2 / 4 / 8 / ... / n))
= O(log(n<sup>log n</sup> / 2 / 4 / 8 / ... / n))
= O(log(n<sup>log n</sup> / (1 * 2 * 4 * 8 * ... * n/4 * n/2 * n))
= O(log(n<sup>log n</sup> / ((1 * n) * (2 * n/2) * (4 * n/4) * ...) group first and last terms
= O(log(n<sup>log n</sup> / (n * n * n * ...)) since we grouped terms,
= O(log(n<sup>log n</sup> / n<sup>(log n)/2</sup>) we halved the number of terms
= O(log(n<sup>log n</sup>) - log(n<sup>(log n)/2</sup>)) log division rule
= O(log n.log n) - ((log n)/2).log n) log power rule * 2
= O(log n.log n) - (log n.log n)/2)
= O(log n.log n)/2)
= O((log n)<sup>2</sup>/2)
= O((log n)<sup>2</sup>) big-O doesn't care about constants

关于algorithm - 复杂度是 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + log(2)) = O(log(n)) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44231116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com