[英]Calculate time complexity of algorithm
So here is an algorithm that is supposed to return the polynomial value of P(x) of a given polynomial with any given x. 因此,这里有一种算法应该返回具有任意给定x的给定多项式的P(x)的多项式值。
A[] is the coefficient array and P[] the power of x array. A []是系数数组,P []是x数组的幂。
(eg x^2 +2*x + 1 would have: A[] = {1,2,1} , P[]= {2,1,0}) (例如x ^ 2 + 2 * x + 1将具有:A [] = {1,2,1},P [] = {2,1,0})
Also, recPower() = O(logn) 另外,recPower()= O(logn)
int polynomial(int x, int A[], int P[], int l, int r)
{
if (r - l == 1)
return ( A[l] * recPower(x, P[l]) ) + ( A[r] * recPower (x, P[r]) );
int m = (l + r) / 2;
return polynomial(x, A, P, l, m) + polynomial(x, A, P, m, r);
}
How do I go about calculating this time complexity? 如何计算时间复杂度? I am perplexed due to the if statement. 由于if语句,我感到困惑。 I have no idea what the recurrence relation will be. 我不知道递归关系是什么。
Following observation might help: As soon as we have r = l + 1
, we spend O(logn) time and we are done. 进行观察可能会有所帮助:一旦我们使r = l + 1
,我们就花费O(logn)时间并完成。
My answer requires good understanding of Recursion Tree . 我的答案需要对递归树有很好的理解。 So proceed wisely. 因此,明智地进行。
So our aim is to find : after how many iterations will we be able to tell that we have r = l + 1? 因此,我们的目标是发现: 经过多少次迭代,我们就能知道我们有r = l + 1?
Lets find out: 让我们找出:
Focusing on return polynomial(x, A, P, l, m) + polynomial(x, A, P, m, r);
专注于return polynomial(x, A, P, l, m) + polynomial(x, A, P, m, r);
Let us first consider left function polynomial(x, A, P, l, m)
. 让我们首先考虑左函数polynomial(x, A, P, l, m)
。 Key thing to note is that l
, remains constant , in all subsequent left function called recursively. 要注意的关键是l
在所有后续递归调用的左函数中保持不变。
By left function I mean polynomial(x, A, P, l, m)
and by right function I mean 左函数是polynomial(x, A, P, l, m)
,右函数是
polynomial(x, A, P, m, r)
. polynomial(x, A, P, m, r)
For left function polynomial(x, A, P, l, m)
, We have: 对于左函数polynomial(x, A, P, l, m)
,我们有:
First iteration 第一次迭代
l = l and r = (l + r)/2
Second iteration 第二次迭代
l = l and r = (l + (l + r)/2)/2
which means that 意思就是
r = (2l + l + r)/2
Third iteration 第三次迭代
l = l and r = (l + (l + (l + r)/2)/2)/2
which means that 意思就是
r = (4l + 2l + l + r)/4
Fourth iteration 第四次迭代
l = l and r = (l + (l + (l + (l + r)/2)/2)/2)/2
which means that 意思就是
r = (8l + 4l + 2l + l + r)/8
This means in nth iteration we have: 这意味着在第n次迭代中,我们有:
r = (l(1 + 2 + 4 + 8 +......2^n-1) + r)/2^n
and terminating condition is r = l + 1
终止条件为r = l + 1
Solving (l(1 + 2 + 4 + 8 +......2^n-1) + r)/2^n = l + 1
, we get 求解(l(1 + 2 + 4 + 8 +......2^n-1) + r)/2^n = l + 1
,我们得到
2^n = r - l
This means that n = log(r - l)
. 这意味着n = log(r - l)
。 One might say that in all subsequent calls of left function we ignored the other call, that is right function call. 可能会说在所有后续的左函数调用中,我们忽略了另一个调用,即右函数调用。 The reason is this: 原因是这样的:
Since in the right function call we l = m
, where m is already a reduced , as we take the mean, and r = r
, which is even more averaged this asymptotically wont have any effect on time complexity. 因为在正确的函数调用中,我们取平均值,所以l = m
,其中m已经被减小,而r = r
,它甚至更平均,这渐近不会对时间复杂度产生任何影响。
So our recursion tree will have maximum depth = log(r - l) . 因此,我们的递归树将具有最大深度= log(r-l) 。 Its true that not all levels will be fully populated, but for the sake of simplicity, we assume this in asymptotic analysis . 的确,并非所有级别都将完全填充,但为简单起见,我们在渐近分析中假设这一点。 So after reaching a depth of log(r - l)
, we call function recPower
, which takes O(logn) time. 因此,在达到log(r - l)
的深度之后,我们调用函数recPower
,这需要O(logn)时间。 Total nodes ( assuming all levels above are full ) at depth log(r - l)
is 2^(log(r - l) - 1)
. 深度log(r - l)
)处的总节点( 假设以上所有级别均已满 )为2^(log(r - l) - 1)
。 For a single node , we take O(logn) time. 对于单个节点,我们花费O(logn)时间。
Therefore we have total time = O( logn*(2^(log(r - l) - 1)) ) . 因此,总时间= O(logn *(2 ^(log(r-l)-1))) 。
This might help: 这可能会有所帮助:
T(#terms) = 2T(#terms/2) + a
T(2) = 2logn + b
Where a and b are constants, and #terms refer to number of terms in polynomial. 其中a和b为常数,#项指多项式中的项数。 This recurrence relation can be solved using Master's Theorem or using the Recursion tree method. 可以使用Master定理或使用递归树方法来解决此递归关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.