渐近符号

When it comes to analysing the complexity of any algorithm in terms of time and space, we can never provide an exact number to define the time required and the space required by the algorithm, instead we express it using some standard notations, also known as Asymptotic Notations.

在分析任何算法在时间和空间方面的复杂性时,我们永远无法提供确切的数字来定义算法所需的时间和所需的空间,而是使用一些标准符号(也称为渐近线)来表示它记号

When we analyse any algorithm, we generally get a formula to represent the amount of time required for execution or the time required by the computer to run the lines of code of the algorithm, number of memory accesses, number of comparisons, temporary variables occupying memory space etc. This formula often contains unimportant details that don't really tell us anything about the running time.

分析任何算法时,通常都会得到一个公式,表示执行所需的时间或计算机运行算法代码行,计算机访问次数,比较次数,占用内存的临时变量所需的时间。空间等。此公式通常包含不重要的细节,这些细节并不能真正告诉我们运行时间。

Let us take an example, if some algorithm has a time complexity of T(n) = (n2 + 3n + 4), which is a quadratic equation. For large values of n, the 3n + 4 part will become insignificant compared to the n2 part.

让我们举一个例子,如果某个算法的时间复杂度为T(n)=(n 2 + 3n + 4),这是一个二次方程。 对于较大的n值,与n 2部分相比, 3n + 4部分将变得无关紧要。

Asymptotic Notations example

For n = 1000, n2 will be 1000000 while 3n + 4 will be 3004.

对于n = 1000, n 2将为10000003n + 4将为3004

Also, When we compare the execution times of two algorithms the constant coefficients of higher order terms are also neglected.

同样,当我们比较两种算法的执行时间时,高阶项的常数系数也被忽略。

An algorithm that takes a time of 200n2 will be faster than some other algorithm that takes n3 time, for any value of n larger than 200. Since we're only interested in the asymptotic behavior of the growth of the function, the constant factor can be ignored too.

对于任何大于200n值,耗时200n 2算法将比其他耗时n 3算法更快。 由于我们只对函数增长的渐近行为感兴趣,因此常数因子也可以忽略。

什么是渐近行为 (What is Asymptotic Behaviour)

The word Asymptotic means approaching a value or curve arbitrarily closely (i.e., as some sort of limit is taken).

渐近 ”一词的意思是任意接近一个值或曲线(即,采取某种限制)。

Remember studying about Limits in High School, this is the same.

记住要学习有关高中极限的知识 ,这是相同的。

The only difference being, here we do not have to find the value of any expression where n is approaching any finite number or infinity, but in case of Asymptotic notations, we use the same model to ignore the constant factors and insignificant parts of an expression, to device a better way of representing complexities of algorithms, in a single coefficient, so that comparison between algorithms can be done easily.

唯一的区别是,在这里n逼近任何有限数或无穷大时,我们不必查找任何表达式的值,但是在渐近表示法的情况下,我们使用同一模型来忽略表达式的常量因子和无关紧要的部分,以便在单个系数中提供一种更好的表示算法复杂性的方法,从而可以轻松完成算法之间的比较。

Let's take an example to understand this:

让我们以一个例子来理解这一点:

If we have two algorithms with the following expressions representing the time required by them for execution, then:

如果我们有两个算法,它们的以下表达式表示它们执行所需的时间,则:

Expression 1: (20n2 + 3n - 4)

表达式1 :(20n 2 + 3n-4)

Expression 2: (n3 + 100n - 2)

表达式2 :(n 3 + 100n-2)

Now, as per asymptotic notations, we should just worry about how the function will grow as the value of n(input) will grow, and that will entirely depend on n2 for the Expression 1, and on n3 for Expression 2. Hence, we can clearly say that the algorithm for which running time is represented by the Expression 2, will grow faster than the other one, simply by analysing the highest power coeeficient and ignoring the other constants(20 in 20n2) and insignificant parts of the expression(3n - 4 and 100n - 2).

现在,按照渐近符号,我们只需要担心函数随着n (输入)的值的增长而增长,这完全取决于表达式1的n 2和表达式2的n 3 。 ,我们可以清楚地说,仅通过分析最高功率系数并忽略其他常数​​(20n 2中的 20)和该表达式的无关紧要的部分,用表达式2表示运行时间的算法将比另一个算法增长更快。表达式( 3n - 4100n - 2 )。

The main idea behind casting aside the less important part is to make things manageable.

抛弃不太重要的部分背后的主要思想是使事情易于管理

All we need to do is, first analyse the algorithm to find out an expression to define it's time requirements and then analyse how that expression will grow as the input(n) will grow.

我们需要做的是,首先分析算法以找到一个表达式来定义其时间要求,然后分析该表达式将如何随着input(n)的增长而增长。

渐近符号的类型 (Types of Asymptotic Notations)

We use three types of asymptotic notations to represent the growth of any algorithm, as input increases:

随着输入的增加,我们使用三种渐近符号来表示任何算法的增长:

  1. Big Theta (Θ)

    大西塔(Θ)

  2. Big Oh(O)

    大哦(O)

  3. Big Omega (Ω)

    大欧米茄(Ω)

紧密界限:Theta (Tight Bounds: Theta)

When we say tight bounds, we mean that the time compexity represented by the Big-Θ notation is like the average value or range within which the actual time of execution of the algorithm will be.

当我们说严格界限时,我们的意思是,Big-Θ表示法表示的时间兼容性类似于算法实际执行时间的平均值或范围。

For example, if for some algorithm the time complexity is represented by the expression 3n2 + 5n, and we use the Big-Θ notation to represent this, then the time complexity would be Θ(n2), ignoring the constant coefficient and removing the insignificant part, which is 5n.

例如,如果对于某些算法,时间复杂度由表达式3n 2 + 5n表示,并且我们使用Big-Θ表示法表示,则时间常数将为Θ(n 2 ),而忽略常数系数并去除无关紧要的部分,即5n。

Here, in the example above, complexity of Θ(n2) means, that the avaerage time for any input n will remain in between, k1 * n2 and k2 * n2, where k1, k2 are two constants, therby tightly binding the expression rpresenting the growth of the algorithm.

此处,在上面的示例中,Θ(n 2 )的复杂度意味着,任何输入n的可用时间将保持在k1 * n 2k2 * n 2 ,其中k1,k2是两个常数,从而紧密绑定表示算法增长的表达式。

Big thets representation

上限:Big-O (Upper Bounds: Big-O)

This notation is known as the upper bound of the algorithm, or a Worst Case of an algorithm.

此表示法称为算法的上限,或算法的最坏情况。

It tells us that a certain function will never exceed a specified time for any value of input n.

它告诉我们,对于任何输入n值,某个函数都不会超过指定时间。

The question is why we need this representation when we already have the big-Θ notation, which represents the tightly bound running time for any algorithm. Let's take a small example to understand this.

问题是为什么当我们已经有了big-表示法时,为什么需要这种表示法,它表示任何算法的运行时间都非常紧密。 让我们举一个小例子来理解这一点。

Consider Linear Search algorithm, in which we traverse an array elements, one by one to search a given number.

考虑线性搜索算法,其中我们逐个遍历数组元素以搜索给定的数字。

In Worst case, starting from the front of the array, we find the element or number we are searching for at the end, which will lead to a time complexity of n, where n represents the number of total elements.

最坏的情况下 ,从数组的开头开始,我们会在末尾找到要搜索的元素或数字,这将导致时间复杂度n ,其中n表示元素总数。

But it can happen, that the element that we are searching for is the first element of the array, in which case the time complexity will be 1.

但是可能发生的是,我们要搜索的元素是数组的第一个元素,在这种情况下,时间复杂度将为1

Now in this case, saying that the big-Θ or tight bound time complexity for Linear search is Θ(n), will mean that the time required will always be related to n, as this is the right way to represent the average time complexity, but when we use the big-O notation, we mean to say that the time complexity is O(n), which means that the time complexity will never exceed n, defining the upper bound, hence saying that it can be less than or equal to n, which is the correct representation.

现在,在这种情况下,说线性搜索的大Θ或紧约束时间复杂度为Θ(n),将意味着所需时间将始终与n相关,因为这是表示平均时间复杂度的正确方法,但是当我们使用big-O表示法时,是指时间复杂度为O(n),这意味着时间复杂度永远不会超过n ,定义了上限,因此可以说小于或等于等于n ,这是正确的表示形式。

This is the reason, most of the time you will see Big-O notation being used to represent the time complexity of any algorithm, because it makes more sense.

这就是原因,在大多数情况下,您会看到Big-O表示法用于表示任何算法的时间复杂度,因为这样做更有意义。

下界:欧米茄 (Lower Bounds: Omega)

Big Omega notation is used to define the lower bound of any algorithm or we can say the best case of any algorithm.

大欧米茄符号用于定义任何算法的下限 ,或者我们可以说是任何算法的最佳情况

This always indicates the minimum time required for any algorithm for all input values, therefore the best case of any algorithm.

这始终表示任何算法对于所有输入值所需的最短时间,因此是任何算法的最佳情况。

In simple words, when we represent a time complexity for any algorithm in the form of big-Ω, we mean that the algorithm will take atleast this much time to cmplete it's execution. It can definitely take more time than this too.

简而言之,当我们以big-Ω形式表示任何算法的时间复杂度时,我们的意思是该算法将花费最少的时间来完成其执行。 肯定会花更多的时间。

翻译自: https://www.studytonight.com/data-structures/aysmptotic-notations

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值