简体   繁体   English

计算函数的复杂度,在python中为hw

[英]Calculate the complexity of the function, hw in python

I need to calculate the running time complexity of the function in terms of n (for exmaple O(n)), n is len(lst) , lst is a variable of list type. 我需要用n来计算函数的运行时复杂度(对于例子O(n)),n是len(lst),lst是列表类型的变量。

ques1

this is what I thought, is it right? 这就是我的想法,是不是? (I need to find the most tight bound!!!) (我需要找到最紧的!!!) 在此输入图像描述

Ok, it's a pretty complex problem, and quite an enjoyable one for a homework. 好吧,这是一个非常复杂的问题,对于家庭作业来说非常愉快。

First it is obvious that you have to split the problem into two parts, either if n is larger than 100 000 or not. 首先,很明显你必须将问题分成两部分,如果n大于100 000。

If n is larger than 100 000: 如果n大于100 000:

You will run the innermost loop maximum 20 000 times at once (because when j gets larger than 100 000, your k loop will result in 0 iterations). 您将同时运行最内层循环最多20 000次(因为当j大于100 000时,您的k循环将导致0次迭代)。 So for every simulation, for the part of n above 50 000 you will exactly perform 因此,对于每个模拟,对于超过50 000的n部分,您将完全执行

在此输入图像描述

total loops. 总循环。 It gives 它给

在此输入图像描述

. You have to add the time of the calculation of the i < 50 000 (n < 100 000) part, which can be written as 您必须添加i <50 000(n <100 000)部分的计算时间,可以写成

在此输入图像描述 ,

that gives 这给了 在此输入图像描述 .

It means that above n = 100 000, your problem has a clear O(n) complexity with a constant part one or more magnitudes smaller than the linear one: 这意味着在n = 100 000以上,您的问题具有明显的O(n)复杂度,其中一个或多个常数小于线性一个或多个:

在此输入图像描述

The tricky part arrives when you get below n = 100 000. Here you can write 当你低于n = 100 000时,棘手的部分到了。在这里你可以写

在此输入图像描述 ,

which can be transformed into: 可以转化为:

在此输入图像描述

This function shows a linear increase with a squared decrease. 此函数显示线性增加,平方减少。 Guess where its root is… (almost) exactly at 100 000. 猜猜它的根源在哪里......(几乎)正好在10万。

Calculating the sum and leaving the O(1), O(n) and non-significant parts you get: 计算总和并留下O(1),O(n)和非重要部分:

在此输入图像描述 .

You can see that its cubic part is negative, so the time would go below 0 for large ns. 您可以看到它的立方体部分是负数,因此对于大ns,时间将低于0。 But you can also see that at n = 100 000, the squared part is still three times bigger than the cubic one, and the maximum of the function is at n = 200 000. So don't worry, you won't go into negative times, in fact, you won't even reach the maximum of this function. 但你也可以看到,在n = 100 000时,平方部分仍然比立方体大三倍,并且函数的最大值是n = 200 000.所以不要担心,你不会进入事实上,负面时间你甚至不会达到这个功能的最大值。

How can you interpret this? 你怎么解释这个? Well, in worst case, you have O(n2) complexity. 好吧,在最坏的情况下,你有O(n2)的复杂性。 But you have a higher order decrease, and for large ns below 100 000, you can do much (max. 33%) better than the O(n2) would predict. 但是你有一个更高的阶数减少,而对于低于10万的大ns,你可以比O(n2)预测的更好(最多33%)。

So to sum up: 总结一下:

  • If n is much smaller than 100 000: O(n2) 如果n远小于100 000:O(n2)
  • If n is close, but smaller than 100 000: somewhat better than O(n2) 如果n接近但小于100 000:稍好于O(n2)
  • If n is larger than 100 000: O(n) 如果n大于100 000:O(n)

I ran an example to measure what's going on. 我举了一个例子来测量发生了什么。 I used different numbers (because your original problem would run forever), so your 100 000 threshold value is at 100 in my case. 我使用了不同的数字(因为你原来的问题会永远存在),所以你的100 000阈值在我的情况下为100。 You can see that in the beginning, O(n2) works fine, then O(n2-n3) fits well the complexity, finally, above 100, we enter the linear part. 你可以看到,在开始时,O(n2)工作正常,然后O(n2-n3)很好地符合复杂性,最后,在100以上,我们进入线性部分。

在此输入图像描述

And for large ns (still with a threshold of 100): 对于大ns(仍然具有100的阈值):

在此输入图像描述

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

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