简体   繁体   English

查找该算法的时间复杂度

[英]Finding Time Complexity Of This Algorithm

I'm trying to find the time complexity of this algorithm, I tried calculating it using T(n) , assumed that T(n) = 2T(n-1) + Const and got O(n) as a result.我试图找到这个算法的时间复杂度,我尝试使用T(n)计算它,假设T(n) = 2T(n-1) + Const并得到O(n)结果。

bool sum(int arr[], int x, int n, int index){
    if(x == 0 && index == 3)
        return true;
    if(n == 0)
        return false;
    if (index == 3)
        return false;

    if(sum(arr+1, x-arr[0], n-1, index + 1))
        return true;
    return sum(arr+1, x, n-1, index);
}

The top call starts with index = 0. Basically I'm trying to see if there is a triple that sums to a given value x.顶部调用以 index = 0 开始。基本上我想看看是否有一个三元组总和为给定值 x。

Am I missing something?我错过了什么吗?

First of all, T(n) = 2T(n-1) + Const would make T(n) be in O(2^n), not O(n).首先, T(n) = 2T(n-1) + Const会使 T(n) 为 O(2^n),而不是 O(n)。 This would be the runtime if you didn't have the index == 3 stopping condition.如果您没有index == 3停止条件,这将是运行时。 But this stopping condition decreases the runtime significantly.但是这种停止条件会显着降低运行时间。

One way to find the time complexity is to count the number of leaves in the recursion tree (ie number of times a stop condition was reached).找到时间复杂度的一种方法是计算递归树中叶子的数量(即达到停止条件的次数)。 Each leaf with index == 3 corresponds to a choice of 3 out of n elements, so there are C(n, 3) such nodes.每个index == 3叶子对应于 n 个元素中的 3 个选择,因此有 C(n, 3) 个这样的节点。 Leaves with n == 0 and index < 3 corresponds to a choice of 0, 1, or 2 elements, ie C(n, 0) + C(n, 1) + C(n, 2). n == 0index < 3叶子对应于 0、1 或 2 个元素的选择,即 C(n, 0) + C(n, 1) + C(n, 2)。 The total number of leaves is thus O(n^3).因此叶子的总数是 O(n^3)。

Since the number of inner nodes (calls which do not reach a stop condition and thus make recursive calls) is about equal to the number of leaves, and each call does O(1) work not including the recursive calls, the total runtime is O(n^3).由于内部节点(未达到停止条件并因此进行递归调用的调用)的数量大约等于叶子的数量,并且每次调用都执行 O(1) 工作,不包括递归调用,因此总运行时间为 O (n^3)。

Another way to get the same result is to consider T(n, index) :获得相同结果的另一种方法是考虑T(n, index)

T(n, 3) = C = O(1)
T(n, 2) = T(n-1, 3) + T(n-1, 2) + C = O(n)
T(n, 1) = T(n-1, 2) + T(n-1, 1) + C = O(n^2)
T(n, 0) = T(n-1, 1) + T(n-1, 0) + C = O(n^3)

Given that the top-level call is assumed to be made with index == 0 (from comments), the algorithm is O(n 3 ).鉴于假设顶级调用是使用index == 0 (来自评论)进行的,算法是 O(n 3 )。 Ignore the details of the implementation, and consider more abstractly what it is doing:忽略实现的细节,更抽象地考虑它在做什么:

  • It performs a linear scan over array arr , where它对数组arr执行线性扫描,其中
    • for each element e , it performs a linear scan over the tail of arr starting after e , where对于每个元素e ,它在e之后开始对arr的尾部执行线性扫描,其中
      • for each element f , it performs a linear scan over the tail of arr starting after f , where对于每个元素f ,它在f之后开始对arr的尾部执行线性扫描,其中
        • for each element g , it checks whether e + f + g == x对于每个元素g ,它检查是否e + f + g == x

The boundary case is the one in which no triplet of elements sums to x , and in that case the procedure does not end until all the scans are complete.边界情况是没有三元组元素和x ,在这种情况下,直到所有扫描完成,过程才会结束。 As should be clear from that description, the recursion is equivalent to a triply-nested loop.从该描述中可以清楚地看出,递归等效于三重嵌套循环。

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

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