简体   繁体   English

递归 function 的时间复杂度

[英]Time-Complexity of recursive function

I'm having some trouble determining the time complexity of this recursive function in terms of Big-O notation.我在确定这个递归 function 在 Big-O 表示法方面的时间复杂度时遇到了一些麻烦。

double expRecursive(double x, int n) {
    if (n <= 4) {
        return expIterativ(x, n);
    }

    return expRecursive(x, n/2) *
           expRecursive(x, (n + 1)/2);
}

(The time complexity of the method expIterative is O(n)) (expIterative方法的时间复杂度是O(n))

Is the recurrence relation of expRecursive() T(n) = 2T(n/2) + n? expRecursive()的递归关系是T(n) = 2T(n/2) + n吗?

If this is the case I suppose T(n) = O(nlogn) after applying the master theorem?如果是这种情况,我想在应用主定理之后 T(n) = O(nlogn)?

My biggest problem is coming up with the correct recurrence relation...我最大的问题是提出正确的递归关系......

To get the recurrence relation, it helps to first annotate the code:要获得递归关系,首先注释代码会有所帮助:

double expRecursive(double x, int n) {
    if (n <= 4) {
        return expIterativ(x, n);       // O(1) since n is bounded
    }

    return expRecursive(x, n/2)         // T(n/2)
           *                            // O(1)
           expRecursive(x, (n + 1)/2);  // T(n/2)
}

So the recurrence relation is所以递归关系是

T(n) = O(1),              for 0 <= n <= 4
T(n) = 2 * T(n/2) + O(1), for n > 4

which has the solution T(n) ∈ Ө(n) .有解T(n) ∈ Ө(n)

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

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