算法时间复杂度计算
For any defined problem, there can be N number of solution. This is true in general. If I have a problem and I discuss about the problem with all of my friends, they will all suggest me different solutions. And I am the one who has to decide which solution is the best based on the circumstances.
对于任何定义的问题,可以有N个解决方案。 总的来说,这是对的。 如果我有问题,并且与所有朋友讨论该问题,他们都会为我提供不同的解决方案。 我是根据情况决定哪种解决方案最好的人。
Similarly for any problem which must be solved using a program, there can be infinite number of solutions. Let's take a simple example to understand this. Below we have two different algorithms to find square of a number(for some time, forget that square of any number n
is n*n
):
类似地,对于必须使用程序解决的任何问题,可以有无数个解决方案。 让我们举一个简单的例子来理解这一点。 下面我们有两种不同的算法来找到一个数字的平方(一段时间后,忘记任何数字n
平方是n*n
):
One solution to this problem can be, running a loop for n
times, starting with the number n
and adding n
to it, every time.
解决此问题的一种方法是,运行一次循环n
次,每次从数字n
开始加n
。
/*
we have to calculate the square of n
*/
for i=1 to n
do n = n + n
// when the loop ends n will hold its square
return n
Or, we can simply use a mathematical operator *
to find the square.
或者,我们可以简单地使用数学运算符*
来找到平方。
/*
we have to calculate the square of n
*/
return n*n
In the above two simple algorithms, you saw how a single problem can have many solutions. While the first solution required a loop which will execute for n
number of times, the second solution used a mathematical operator *
to return the result in one line. So which one is the better approach, of course the second one.
在以上两种简单算法中,您看到了单个问题如何具有许多解决方案。 第一种解决方案要求循环执行n
次,而第二种解决方案使用数学运算符*
将结果返回一行。 因此,哪种方法更好,当然是第二种。
什么是时间复杂度? (What is Time Complexity?)
Time complexity of an algorithm signifies the total time required by the program to run till its completion.
算法的时间复杂度表示程序运行直至完成所需的总时间。
The time complexity of algorithms is most commonly expressed using the big O notation. It's an asymptotic notation to represent the time complexity. We will study about it in detail in the next tutorial.
算法的时间复杂度通常使用大O表示法表示 。 这是一种渐进符号,表示时间复杂度。 我们将在下一个教程中详细研究它。
Time Complexity is most commonly estimated by counting the number of elementary steps performed by any algorithm to finish execution. Like in the example above, for the first code the loop will run n
number of times, so the time complexity will be n
atleast and as the value of n
will increase the time taken will also increase. While for the second code, time complexity is constant, because it will never be dependent on the value of n
, it will always give the result in 1 step.
时间复杂度通常是通过计算任何算法完成执行的基本步骤的数量来估算的。 像上面的示例一样,对于第一个代码,循环将运行n
次,因此时间复杂度将至少为n
并且随着n
值的增加,花费的时间也会增加。 对于第二个代码,时间复杂度是恒定的,因为它永远不会依赖于n
的值,它将始终以1步给出结果。
And since the algorithm's performance may vary with different types of input data, hence for an algorithm we usually use the worst-case Time complexity of an algorithm because that is the maximum time taken for any input size.
而且由于算法的性能可能随输入数据的不同类型而变化,因此对于一种算法,我们通常使用最坏情况下的时间复杂度 ,因为这是任何大小的输入所花费的最大时间。
计算时间复杂度 (Calculating Time Complexity)
Now lets tap onto the next big topic related to Time complexity, which is How to Calculate Time Complexity. It becomes very confusing some times, but we will try to explain it in the simplest way.
现在,让我们点击与时间复杂度有关的下一个大话题,即如何计算时间复杂度。 有时候它会变得很混乱,但是我们将尝试以最简单的方式进行解释。
Now the most common metric for calculating time complexity is Big O notation. This removes all constant factors so that the running time can be estimated in relation to N, as N approaches infinity. In general you can think of it like this :
现在,用于计算时间复杂度的最常用指标是Big O表示法。 这消除了所有恒定因素,因此当N接近无穷大时,可以相对于N估算运行时间。 通常,您可以这样考虑:
statement;
Above we have a single statement. Its Time Complexity will be Constant. The running time of the statement will not change in relation to N.
上面我们有一个声明。 它的时间复杂度将是恒定的 。 语句的运行时间不会相对于N改变。
for(i=0; i < N; i++)
{
statement;
}
The time complexity for the above algorithm will be Linear. The running time of the loop is directly proportional to N. When N doubles, so does the running time.
上述算法的时间复杂度将为线性 。 循环的运行时间与N成正比。当N加倍时,运行时间也成正比。
for(i=0; i < N; i++)
{
for(j=0; j < N;j++)
{
statement;
}
}
This time, the time complexity for the above code will be Quadratic. The running time of the two loops is proportional to the square of N. When N doubles, the running time increases by N * N.
这次,以上代码的时间复杂度将是Quadratic 。 两个循环的运行时间与N的平方成正比。当N翻倍时,运行时间增加N *N。
while(low <= high)
{
mid = (low + high) / 2;
if (target < list[mid])
high = mid - 1;
else if (target > list[mid])
low = mid + 1;
else break;
}
This is an algorithm to break a set of numbers into halves, to search a particular field(we will study this in detail later). Now, this algorithm will have a Logarithmic Time Complexity. The running time of the algorithm is proportional to the number of times N can be divided by 2(N is high-low here). This is because the algorithm divides the working area in half with each iteration.
这是一种将一组数字分成两半,以搜索特定字段的算法(我们将在后面详细研究)。 现在,该算法将具有对数时间复杂度。 该算法的运行时间与N可以除以2的次数成正比(此处N为高-低)。 这是因为该算法在每次迭代中将工作区域分为两半。
void quicksort(int list[], int left, int right)
{
int pivot = partition(list, left, right);
quicksort(list, left, pivot - 1);
quicksort(list, pivot + 1, right);
}
Taking the previous algorithm forward, above we have a small logic of Quick Sort(we will study this in detail later). Now in Quick Sort, we divide the list into halves every time, but we repeat the iteration N times(where N is the size of list). Hence time complexity will be N*log( N ). The running time consists of N loops (iterative or recursive) that are logarithmic, thus the algorithm is a combination of linear and logarithmic.
继续前面的算法,上面我们有一个快速排序的小逻辑(我们将在后面详细研究)。 现在在“快速排序”中,我们每次都将列表分成两半,但是我们将迭代重复N次(其中N是列表的大小)。 因此,时间复杂度将为N * log(N) 。 运行时间由N个对数的循环(迭代或递归)组成,因此该算法是线性和对数的组合。
NOTE: In general, doing something with every item in one dimension is linear, doing something with every item in two dimensions is quadratic, and dividing the working area in half is logarithmic.
注意:通常,对一维中的每个项目执行某项操作是线性的,对二维中每个项目进行某项操作是二次项,并且将工作区域分为两半是对数的。
时间复杂度的注释类型 (Types of Notations for Time Complexity)
现在,我们将讨论和理解用于时间复杂度的各种符号。Big Oh denotes "fewer than or the same as" <expression> iterations.
Big Oh表示“ 小于或等于 ” <expression>迭代。
Big Omega denotes "more than or the same as" <expression> iterations.
Big Omega表示“ 大于或等于 ” <expression>迭代。
Big Theta denotes "the same as" <expression> iterations.
大Theta表示“ 与 <expression>迭代相同。
Little Oh denotes "fewer than" <expression> iterations.
小哦表示少于 “ <expression>个迭代。
Little Omega denotes "more than" <expression> iterations.
小欧米茄表示的不只是 <expression>迭代。
通过示例了解时间复杂度的表示法 (Understanding Notations of Time Complexity with Example)
O(expression) is the set of functions that grow slower than or at the same rate as expression. It indicates the maximum required by an algorithm for all input values. It represents the worst case of an algorithm's time complexity.
O(expression)是一组比expression慢或以相同速率增长的函数。 它指示算法对所有输入值所需的最大值。 它代表了算法时间复杂度的最坏情况。
Omega(expression) is the set of functions that grow faster than or at the same rate as expression. It indicates the minimum time required by an algorithm for all input values. It represents the best case of an algorithm's time complexity.
Omega(expression)是一组比expression更快或以相同速度增长的函数。 它指示算法对所有输入值所需的最短时间。 它代表了算法时间复杂度的最佳情况。
Theta(expression) consist of all the functions that lie in both O(expression) and Omega(expression). It indicates the average bound of an algorithm. It represents the average case of an algorithm's time complexity.
Theta(表达式)由位于O(表达式)和Omega(表达式)中的所有函数组成。 它表示算法的平均界限。 它代表算法时间复杂度的平均情况。
Suppose you've calculated that an algorithm takes f(n) operations, where,
假设您已经计算出某个算法需要执行f(n)个运算,其中,
f(n) = 3*n^2 + 2*n + 4. // n^2 means square of n
Since this polynomial grows at the same rate as n2, then you could say that the function f lies in the set Theta(n2). (It also lies in the sets O(n2) and Omega(n2) for the same reason.)
由于此多项式以与n 2相同的速率增长,因此可以说函数f位于集合Theta(n 2 )中 。 (出于相同的原因,它也位于集合O(n 2 )和Omega(n 2 )中。)
The simplest explanation is, because Theta denotes the same as the expression. Hence, as f(n) grows by a factor of n2, the time complexity can be best represented as Theta(n2).
最简单的解释是,因为Theta与表达式相同 。 因此,随着f(n)增长n 2倍,时间复杂度可以最好地表示为Theta(n 2 ) 。
翻译自: https://www.studytonight.com/data-structures/time-complexity-of-algorithms
算法时间复杂度计算