简体   繁体   English

以下递归 function 的时间复杂度是多少

[英]What would be the time complexity of below recursive function

What would the time complexity be for the below recursive function?下面的递归 function 的时间复杂度是多少?

I am using the the below T(n) but not sure if I created the correct equation for this function我正在使用下面的 T(n) 但不确定我是否为这个 function 创建了正确的方程

T(n)=T(n-1)+n -> o(n^2) T(n)=T(n-1)+n -> o(n^2)

public static int test2(int n){
    if(n<=0){
        return 0;
    }
    for(int i =0; i<=n; i++){
        for(int j =0; j<=n; j++){
            System.out.println(" in here " + i + j);
        }
        test2(n-1);
    }
    return 1;
}

I think the function is: T(n)=n(T(n-1))+n^2我认为 function 是:T(n)=n(T(n-1))+n^2

O(n^2) is the Time complexity of a single non-terminating recursive call, but not the overall Time complexity. O(n^2)单个非终止递归调用的时间复杂度,但不是整体时间复杂度。

Each call test(n) , if it doesn't hit the Base case of the recursion, creates n + 1 branches of recursive calls.每次调用test(n) ,如果它没有达到递归的基本情况,就会创建n + 1个递归调用分支。

For instance, if n = 2 .例如,如果n = 2 The tree of recursive calls would be:递归调用树将是:

               t(2)
           /    |   \
          /     |    \
      t(1)     t(1)   t(1)
      /  \     /  \    /  \
  t(0) t(0) t(0) t(0) t(0) t(0) 

So the call test(2) results in 9 recursive calls.所以调用test(2)会导致9次递归调用。 If we call test(3) it would spawn 40 recursive calls.如果我们调用test(3)它将产生40递归调用。

Basically we have:基本上我们有:

(n) * (n + 1 - 1) * (n + 1 - 2) * (n + 1 - 3) * ... until `n` doesn't turn to 1

Which resembles a factorial , if we neglect +1 and it would be roughly n!这类似于阶乘,如果我们忽略+1 ,它大致n! recursive calls.递归调用。 Each of which would have a time complexity O(n^2)每个都有时间复杂度O(n^2)

So the overall time complexity can be expressed as:所以整体的时间复杂度可以表示为:

O(n^2 * n!)

I am going to tackle this problem from a mathematical point of view.我将从数学的角度来解决这个问题。

first of all, your equation for T(n) is not correct it should be:首先,你的 T(n) 方程不正确,它应该是:

T(n) = n * (T (n-1) + n)

The reason being that you have n iterations and for every iteration (this is where the product comes), you do a recursion along with n iterations.原因是您有 n 次迭代,并且对于每次迭代(这就是产品的来源),您在进行 n 次迭代时进行递归。 So basically you will have:所以基本上你会有:

T(n)   =  n    * T(n-1) +  n^2
T(n-1) = (n-1) * T(n-2) + (n-1)^2
T(n-2) = (n-2) * T(n-3) + (n-2)^2
.
.
.
T(2)   =  2    * T(1) + 2^2
T(1)   =  1    * T(0) + 1^2

where T(0) = 1 basically given your definition.其中 T(0) = 1 基本上给出了您的定义。 So now with a bit of reordering and multiplication you will have:因此,现在通过一些重新排序和乘法,您将拥有:

T (n) = n^2 + n*((n-1)^2) + n*(n-1)*((n-2)^2) + ... + n*(n-1)*(n-2)*(n-3)*...*(n-(n-3))*(2^2) + (2 * n!) 

the reason for that last 2 * n.最后一个 2 * n 的原因。 is that T(1) = 2 and through the multiplications it gets a coefficient of n.. (Do to the process just basically start from T(n-1) and by multiplying the equation with n and adding it to the equation of T(n) you get rid of T(n-1). But you now have T(n-2) in the equation of T(n) so repeat the process) So I think (but am not sure) that the time complexity will be n!是 T(1) = 2 并通过乘法得到 n 的系数..(对过程基本上从 T(n-1) 开始并将方程与 n 相乘并将其添加到 T 的方程(n) 你摆脱了 T(n-1)。但是你现在在 T(n) 的等式中有 T(n-2) 所以重复这个过程)所以我认为(但我不确定)时间复杂度将是 n! and not n^2.而不是 n^2。 I hope this is helpful.我希望这是有帮助的。

Replacing the operations by the time they take, you establish the following recurrence:按时间替换操作,您可以建立以下循环:

T(0) = C0
T(n) =
    Σ{i=0..n}
        Σ{j=0..n}
            C1
        +
        T(n-1)
    + C2

This can be rewritten这可以重写

T(n) = (n+1).((n+1).C1 + T(n-1)) + C2 = (n+1).T(n-1) + (n+1)².C1 + C2

The exact solution of this recurrence is a little technical.这个递归的精确解有点技术含量。 By solving the homogeneous equation, we have the solution C.(n+1)!通过求解齐次方程,我们得到解C.(n+1)! and by variation of the coefficient we set并通过我们设置的系数的变化

T(n) = (n+1)!.U(n)

Then然后

(n+1)!.U(n) = (n+1).n!.U(n-1) + C1.(n+1)² + C2

or或者

U(n) = U(n-1) + C1/(n-2)! + 2C1/(n-1)! + C2/(n+1)!

We recognize truncated series for e, which very quickly converges to a constant and我们认识到 e 的截断级数,它很快收敛到一个常数并且

T(n) ~ C(n+1)! = O((n+1)!)

This is a rather complex function. Lets move from bottom up and maybe we can see something.这是一个相当复杂的 function。让我们从下往上移动,也许我们可以看到一些东西。

T(0) = 1
T(1) = 2 * (2 + T(0)) = 2 * (2 + 1) = 2 * 3 = 6
T(2) = 3 * (3 + T(1)) = 3 * (3 + 6) = 2 * 9 = 27
T(3) = 4 * (4 + T(2)) = 4 * (4 + 27) = 4 * 31 = 124

If we expand it differently如果我们以不同的方式扩展它

T(1) = 2 * (2 + T(0))
T(2) = 3 * (3 + 2 * (2 + T(0)))
T(3) = 4 * (4 + T(2)) = 4 * (4 + 3 * (3 + 2 * (2 + T(0))))
...
T(n) = (n+1) * (n+1 + T(n)) = (n+1) * (n+1 + n * (n + T(n -1))) =
(n+1) * (n+1 + n * (n + (n-1) * ((n-1) + T(n-2))))

Now if we open parenthises of T(n)现在如果我们打开 T(n) 的括号

T(n) = (n+1)^2 + (n+1)n*(n + T(n-1)) = (n+1)^2 + (n+1)n^2 + (n+1)n(n-1)*((n-1) + T(n-1)) = ... 
= (n+1)^2 + (n+1)n^2 + ... + (n+1)n(n-1)...2^2 + (n+1)! = Sum[iProduct[j,{j,i,n}],{i,2,n}] + (n+1)!  

(I used wolfram alpha for the sum thing, i hope tis correct) (我使用 wolfram alpha 作为总和,我希望它是正确的)

What can we see from the final sum is that the largest member of the sum is (n+1)!从最后的求和可以看出,求和的最大成员是(n+1)! Everything else will be smaller, so we can disregard those.其他一切都会变小,所以我们可以忽略它们。 Also the +1 is pointless so we can also drop that. +1也毫无意义,所以我们也可以放弃它。 End result is that your recursive function is o(n!) .最终结果是您的递归 function 是o(n!)

If anyone asks why n+1 , it because the loop condition is i<=n not i < n .如果有人问为什么n+1 ,那是因为循环条件是i<=n而不是i < n Also i havent done this type of analysis for quite some years now, i hope i didnt make any major mistakes.另外,我已经好几年没有做过这种类型的分析了,我希望我没有犯任何重大错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM