How to calculate running time (i.e. calculate time complexity)

general rule

for loop

The running time of a for loop is at most the running time of the statements (including tests) in the for loop multiplied by the number of iterations.

This is very simple, so I won't talk about it. For n cycles, its time complexity is O(N).

nested for loops

Analyze these loops from the inside out. The total running time of a statement inside a set of nested loops is the product of the running time of the statement times the size of all the for loops shuffled.

For the following loop, the number of loops in the inner layer is 0 to n-1 times, that is, n times, and the same is true for the outer layer. Multiply to get the resultO(N^2)

for(i=0;i<n;i++)
    for(j=0;j<n;j++)
        k++;

So what if the inner loop is like this?

for(i=0;i<n;i++)
    for(j=2;j<n;j++)
        k++;

In the same way, the number of inner loops is n-2 times, and the outer loop is n times. Multiply to get the result O(N^2-2N). But we generally ignore constant terms and low-order terms. So the result is O(N^2).

Some people may think, wouldn't the accuracy change if the low-order terms are ignored? But in fact, the calculation accuracy requirement of time complexity is already very low. What we often say about time complexity is actually the worst case time complexity of the algorithm. That is, the worst-case running time of the algorithm T_{worst}(N)will not be lower than this.

In other words, what we are looking for is actually the "lower bound", for which we do not require high precision.

Back to the topic, if we change it again, what is the inner loop now?

for(i=0;i<n;i++)
    for(j=i;j<n;j++)
        k++;

Look directly at the running time of the inner loop in the worst case. First look at "j=i". When the outer layer i=0, the inner loop j=0, and the inner loop executes n times. As i increases, the number of executions of j changes from n to (n-1), (n-2) until (n-1). Summing them we get

\sum^{n}_{j=1}(n-j)

But this thing does not need to be calculated, and there is no need to sum the number sequence to solve it, and even write the above step is redundant.

Remember that we are looking for a lower bound (worst case time complexity)? Then let's see how many times the statement in the innermost loop can be executed at most -- or n. So we directly get the size of the inner loop as n. The outer layer is also n. The result isO(N^2)

sequential statement

There is nothing to say about this, assuming your program is like this now.

#include <iostream>
using namespace std;

int main()
{
	int num = 0;
	int n;
	cin >> n;
	for (int i = 0;i < n;i++)
		num++;
	
	for (int i = 0;i < n;i++)
		for (int j = 0;j < n;j++)
			num++;
}

We can sum the running time of each statement. In fact, we still look at the highest item, and the result isO(N^2)

if/else statement

Like the loop, we still look at its worst-case time complexity. Just find the one with the longest running time in the if/else. The result is stillO(N^2)

#include <iostream>
using namespace std;

int main()
{
	int num = 0;
	int n;
	cin >> n;
	if (n == 0)
	{
		for (int i = 0;i < n;i++)
			num++;
	}
	else if (n == 1)
	{
		for (int i = 0;i < n;i++)
			for (int j = 0;j < n;j++)
				num++;
	}

}

Guess you like

Origin blog.csdn.net/white_night_SZTU/article/details/129570094