theta符号_大Theta和渐近符号的解释

theta符号

Big Omega tells us the lower bound of the runtime of a function, and Big O tells us the upper bound.

Big Omega告诉我们函数运行时的下限,Big O告诉我们上限。

Often times, they are different and we can’t put a guarantee on the runtime - it will vary between the two bounds and the inputs. But what happens when they’re the same? Then we can give a theta (Θ) bound - our function will run in that time, no matter what input we give it.

通常,它们是不同的,我们不能保证运行时-它在两个范围和输入之间会有所不同。 但是,当它们相同时会发生什么呢? 然后,我们可以给定theta (θ)界限-无论我们给它什么输入,我们的函数都会在那个时候运行。

In general, we always want to give a theta bound if possible because it is the most accurate and tightest bound. If we can’t give a theta bound, the next best thing is the tightest O bound possible.

通常,如果可能的话,我们总是希望给出theta界限,因为它是最准确,最严格的界限。 如果我们不能给定theta界限,那么下一个最好的事情就是尽可能严格的O界限。

Take, for example, a function that searches an array for the value 0:

以一个在数组中搜索值0的函数为例:

def containsZero(arr): #assume normal array of length n with no edge cases
  for num x in arr:
    if x == 0:
       return true
  return false
  1. What’s the best case? Well, if the array we give it has 0 as the first value, it will take constant time: Ω (1)

    最好的情况是什么? 好吧,如果我们给它的数组的第一个值为0,则它​​将花费恒定的时间:Ω(1)
  2. What’s the worst case? If the array doesn’t contain 0, we will have iterated through the whole array: O(n)

    最坏的情况是什么? 如果数组不包含0,我们将遍历整个数组:O(n)

We’ve given it an omega and O bound, so what about theta? We can’t give it one! Depending on the array we give it, the runtime will be somewhere in between constant and linear.

我们给它加了O和O,那么theta呢? 我们不能给它一个! 根据我们提供的数组,运行时将介于常量和线性之间。

Let’s change our code a bit.

让我们稍微更改一下代码。

def printNums(arr): #assume normal array of length n with no edge cases
  for num x in arr:
    print(x)

Can you think of a best case and worst case?? I can’t! No matter what array we give it, we have to iterate through every value in the array. So the function will take AT LEAST n time (Ω(n)), but we also know it won’t take any longer than n time (O(n)). What does this mean? Our function will take exactly n time: Θ(n).

您能想到最好的情况和最坏的情况吗? 我不行 不管我们给它什么数组,我们都必须遍历数组中的每个值。 因此,该函数将花费至少n个时间(Ω(n)),但我们也知道它所花费的时间不会超过n个时间(O(n))。 这是什么意思? 我们的函数恰好需要 n个时间:Θ(n)。

If the bounds are confusing, think about it like this. We have 2 numbers, x and y. We are given that x <= y and that y <= x. If x is less than or equal to y, and y is less than or equal to x, then x has to equal y!

如果界限令人困惑,请这样考虑。 我们有2个数字,x和y。 给出x <= y和y <= x。 如果x小于或等于y,并且y小于或等于x,则x必须等于y!

If you’re familiar with linked lists, test yourself and think about the runtimes for each of these functions!

如果您熟悉链表,请测试一下自己并考虑一下每个函数的运行时!

  1. get

    得到
  2. remove

    去掉
  3. add

Things get even more interesting when you consider a doubly linked list!

当您考虑一个双向链表时,事情变得更加有趣!

渐近符号 (Asymptotic Notation)

How do we measure the performance value of algorithms?

我们如何衡量算法的性能价值?

Consider how time is one of our most valuable resources. In computing, we can measure performance with the amount of time a process takes to complete. If the data processed by two algorithms is the same, we can decide on the best implementation to solve a problem.

考虑一下时间是我们最宝贵的资源之一。 在计算中,我们可以用过程完成所需的时间来衡量性能。 如果两种算法处理的数据相同,我们可以决定最佳解决方案。

We do this by defining the mathematical limits of an algorithm. These are the big-O, big-omega, and big-theta, or the asymptotic notations of an algorithm. On a graph the big-O would be the longest an algorithm could take for any given data set, or the “upper bound”. Big-omega is like the opposite of big-O, the “lower bound”. That’s where the algorithm reaches its top-speed for any data set. Big theta is either the exact performance value of the algorithm, or a useful range between narrow upper and lower bounds.

我们通过定义算法的数学极限来做到这一点。 这些是big-O,big-omega和big-theta或算法的渐近符号。 在图中,big-O对于任何给定的数据集或“上限”来说,算法所能花费的时间最长。 大欧米茄就像大欧米茄的反面,“下界”。 这就是算法对任何数据集达到最高速度的地方。 大theta要么是算法的准确性能值,要么是狭窄的上限和下限之间的有用范围。

Some examples:

一些例子:

  • “The delivery will be there within your lifetime.” (big-O, upper-bound)

    “交付将在您的一生中在那里。” (大O,上限)
  • “I can pay you at least one dollar.” (big-omega, lower bound)

    “我至少可以付给你一美元。” (大欧米茄,下界)
  • “The high today will be 25ºC and the low will be 19ºC.” (big-theta, narrow)

    “今天的最高温度为25ºC,最低温度为19ºC。” (大θ,窄)
  • “It’s a kilometer walk to the beach.” (big-theta, exact)

    “到海滩只有一公里的步行路程。” (精确到大θ)

更多信息: (More Information:)

https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation https://stackoverflow.com/questions/10376740/what-exactly-does-big-%D3%A8-notation-represent https://www.geeksforgeeks.org/analysis-of-algorithms-set-3asymptotic-notations/

https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-big-theta-notation https://stackoverflow.com/questions/10376740/what-exactly-does-big -%D3%A8符号表示 https://www.geeksforgeeks.org/analysis-of-algorithms-set-3asymptotic-notations/

翻译自: https://www.freecodecamp.org/news/big-theta-and-asymptotic-notation-explained/

theta符号

你可能感兴趣的:(算法,python,java,机器学习,人工智能)