简体   繁体   English

如何找到这个程序的最坏情况复杂度是多少?

[英]How to find what is the worst case complexity of this program?

  • V is sorted V已排序
  • V.size() = N
  • The function is initially called as searchNumOccurrence(V, k, 0, N-1)该函数最初被称为searchNumOccurrence(V, k, 0, N-1)

Code for function:功能代码:

int searchNumOccurrence(vector<int> &V, int k, int start, int end) {
    if (start > end) return 0;
    int mid = (start + end) / 2;

    if (V[mid] < k) return searchNumOccurrence(V, k, mid + 1, end);
    if (V[mid] > k) return searchNumOccurrence(V, k, start, mid - 1);

    return searchNumOccurrence(V, k, start, mid - 1) + 
            1 + searchNumOccurrence(V,k, mid + 1, end);
}

Ok, lets check what you do.好的,让我们检查一下你在做什么。 First, you take middle index.首先,您采用中间索引。 Then you check if number is bigger or smaller V[mid] , and if its not, you will increase counter and check left and right, by moving mid position by 1 (searchNumOccurrence(V, k, start, mid - 1)) & searchNumOccurrence(V,k, mid + 1, end) .然后你检查 number 是更大或更小V[mid] ,如果不是,你将增加计数器并左右检查,通过将mid位置移动1 (searchNumOccurrence(V, k, start, mid - 1)) & searchNumOccurrence(V,k, mid + 1, end)

Problem here is this moving of mid by 1. Lets say, you have this array:这里的问题是mid移动了 1。假设,你有这个数组:

2 2 2 2 2 2 2 2 2 2 2 2 2 2

This means, you will have to check each index by iterating it by 1. That is O(n) , so your algorithm is O(n) .这意味着,您必须通过迭代 1 来检查每个索引。即O(n) ,因此您的算法是O(n)

Now, there is a better way.现在,有一个更好的方法。 Since you have sorted array, don't look for each occurrence, search for subset of that number, more precise, starting and ending index of subset of that number in given array.由于您已经对数组进行了排序,因此不要查找每个匹配项,而是在给定数组中搜索该数字的子集,更精确地,该数字子集的起始和结束索引。 In that way, you basically search for 2 indexes, with this rule:这样,您基本上可以使用以下规则搜索 2 个索引:

  • index1 is where V[index1] = k , but if index1 > 0 then its V[index1 - 1] < k index1V[index1] = k ,但如果index1 > 0那么它的V[index1 - 1] < k

  • index2 is where V[index2] = k but if index2 < end - 1 then its V[index2 + 1] > k index2V[index2] = k但如果index2 < end - 1那么它的V[index2 + 1] > k

Searching this two indexes are O(log_2(n)) ( O(lgn) ), and result is numberOfOccurrences = index2 - index1 + 1 .搜索这两个索引是O(log_2(n)) ( O(lgn) ),结果是numberOfOccurrences = index2 - index1 + 1

Edit: First, search for left index.编辑:首先,搜索左索引。 If k doesn't exist in array, return 0 for function and you don't need to go for right index (thanks @craig-young).如果数组中不存在k ,则为函数返回0 ,并且您不需要寻找正确的索引(感谢 @craig-young)。 If left index is found, then you know you will find right (exist if and only if left index exists), but search under subset [index1, end) .如果找到左索引,那么你知道你会找到右(当且仅当左索引存在时存在),但在子集[index1, end)下搜索。

The worst case scenario would be when the recursive function must iterate through all elements.最坏的情况是递归函数必须遍历所有元素。

ie) If |V|=4 , k=1即)如果|V|=4k=1

1 1 1 1 1 1 1 1

1 1 | 1 1 | 1 1 1 1

1 | 1 | 1 1

1 1

The height of the tree above would be O(log_2(|V|)) and the number of elements would be O(|V|) .上面树的高度为O(log_2(|V|)) ,元素数量为O(|V|) Hence wrt |V|, there would be at most O(|V|log_2(|V|)) recursive calls where |V|因此,对于 |V|,最多会有 O(|V|log_2(|V|)) 次递归调用,其中 |V| is the size of V.是 V 的大小。

It's been a while since I took algorithms, so if anybody can let me know if I got it wrong somewhere that would be great.我已经有一段时间没有学习算法了,所以如果有人能告诉我我是否在某个地方弄错了,那就太好了。

What if all the values in the array are the same ?如果数组中的所有值都相同怎么办? How long does the program run then ?It might seem at the first look that the program is O(log N).然后程序运行多长时间?乍一看,程序似乎是 O(log N)。 However, the last case然而,最后一种情况

return searchNumOccurrence(V, k, start, mid - 1) + 1 + searchNumOccurrence(V, k, mid + 1, end);

is the bottleneck step.是瓶颈步骤。 Assuming all the values in the array are the same, we get the following relation :假设数组中的所有值都相同,我们得到以下关系:

T(N) = 2 * T(N/2) + constant
     = 4 * T(N/4) + constant     ( 2 * constant = another constant ) 
     = 8 * T(N/8) + constant 
     ...
     = N * T(N/N) + constant 
     = N + constant 
     = O(N)

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

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