Data Structures - Time Complexity Exercises (with Parsing)

Preface
  This article is used to practice time complexity solving . There are 20+ questions in total, and the difficulty is moderate. The source of the questions is the supplementary textbooks for postgraduate entrance examinations and online collection. If there are any mistakes, please point out and modify them as soon as possible.




1 Find the time complexity

1. Find the time complexity of the following program

void function(int n)
{
    
    
	if (n==1)
	return;
	for (int i=1; i<=n; i++)
	{
    
    
		for (int j=1; j<=n; j++)
		{
    
    
			cout << "*";
			break;
		}
	cout << endl;
	}
}

Analysis Don’t ignore
the inner break .



Answer
O ( n ) O(n)O ( n )



2. Find the time complexity of the following program

void function(int n)
{
    
    
	int count = 0;
	for (int i=n/2; i<=n; i++)
		for (int j=1; j<=n; j = 2 * j)
			for (int k=1; k<=n; k = k * 2)
				count++;
}

analyze

void function(int n)
{
    
    
	int count = 0;

	// 执行 n/2 次
	for (int i=n/2; i<=n; i++)

		// 执行 n/2 次
		for (int j=1; j+n/2<=n; j = j++)

			// 执行 logn 次
			for (int k=1; k<=n; k = k * 2)
				count++;
}

So the result is the time complexity multiplication of the three layers.



Answer
O ( n 2 logn ) O(n^2logn)O ( n2 logn)



3. Find the time complexity of the following program

void function(int n)
{
    
    
	int count = 0;
	for (int i=0; i<n; i++)
		for (int j=i; j< i*i; j++)
			if (j%i == 0)
			{
    
    
				for (int k=0; k<j; k++)
					printf("*");
			}
}

analyze

void function(int n)
{
    
    
	int count = 0;

	// 执行 n 次
	for (int i=0; i<n; i++)

		// 执行 O(n*n) 次
		for (int j=i; j< i*i; j++)
			if (j%i == 0)
			{
    
    
				// 执行 j 次 = O(n*n) 次
				for (int k=0; k<j; k++)
					printf("*");
			}
}

The time complexity can be roughly obtained by multiplying the execution times of each layer.



Answer
O ( n 5 ) O(n^5)O ( n5 )



4. Find the time complexity of the following program

count = 0
for (int i = N; i > 0; i /= 2)
for (int j = 0; j < i; j++)
	count++;

Analysis
This is an easy mistake. Some would argue that the outermost loop executes log N logNl o g N times, the innermost layerNNN times, so the total time complexity isO ( N log N ) O(NlogN)O ( Nlog N ) times . _ This isthe classic mistake~


positive solution:

Consider how many times count++ will run:
when i=N, it will run N times.
When i=N/2, it will run N/2 times.
When i=N/4, it will run N/4 times.
and so on...

Therefore, count++a total of times will be run N+N/2+N/4+...+1=2*N. So the time complexity is O ( N ) O(N)O ( N ) .



Answers
O ( N ) O(N)O ( N )



5. Where n is a positive integer, the statement frequency of the last line is in the worst case:

for(int i = n - 1; i >= 1; --i)
	for(int j = 1; j <=	i; ++j)
		if(A[j] > A[j + 1]) 
			swap(A[j], A[j + 1]);

Analysis
Bubble sorting, in the worst case, is equivalent to two for loops running full, so it is O ( n 2 ) O(n^2)O ( n2 )



Answers
O ( n 2 ) O(n^2)O ( n2 )



6. (2019 Huazhong University of Science and Technology) What is time complexity?
Analysis slightly


7. Find the time complexity of the following program

int a = 0;
for (i = 0; i < N; i++) {
    
    
	for (j = N; j > i; j--) {
    
    
		a = a + i + j;
	}
}

Parsing
The outer layer is executed a total of N-1times, and the inner layer is executed once each N - itime. So the formula can be listed:
∑ i = 0 N − 1 ( N − i ) = ∑ i = 0 N − 1 N − ∑ i = 0 N − 1 i = N 2 − N ( 0 + N − 1 ) 2 = 1 2 N 2 + 1 2 N ≈ O ( N 2 ) \sum\limits^{N-1}_{i=0}(Ni)=\sum\limits^{N-1}_{i=0 }N-\sum\limits^{N-1}_{i=0}i=N^2-\frac{N(0+N-1)}{2}=\frac{1}{2}N ^2+\frac{1}{2}N \approx O(N^2)i=0N1(Ni)=i=0N1Ni=0N1i=N22N(0+N1)=21N2+21NO ( N2 )



Answers
O ( N 2 ) O(N^2)O ( N2 )



8. Find the time complexity of the following programs

void fun(int n){
    
    
	int i = 0;
	while(i * i * i <= n)
		i++;
}

Parsing
i++ is a basic operation, and it will not change the time complexity of the cycle as a whole, so it is ignored; the t*t*t≤nresult is O( n 3 \sqrt[3]{n}3n )



Answer
O( n 3 \sqrt[3]{n}3n )



9. Find the execution times of "m++"

int m = 0, i, j;
for(i = 1; i <= n; i++)
	for(j = 1; j <= 2 * i; j++)
		m++;

Parseable
formula:
∑ i = 1 n ∑ j = 1 2 i 1 = ∑ i = 1 n 2 i = 2 ∑ i = 1 ni = 2 n ( n + 1 ) 2 = n ( n + 1 ) \sum\limits^n_{i=1}\sum\limits^{2i}_{j=1}1=\sum\limits^{n}_{i=1}2i=2\sum\limits^{ n}_{i=1}i=2\frac{n(n+1)}{2}=n(n+1)i=1nj=12 i1=i=1n2i _=2i=1ni=22n(n+1)=n(n+1 )



Answer
n(n+1)



10. Find the time complexity of the following programs

int fact(int n){
    
    
	if(n <= 1) return 1;
	return n * fact(n - 1);
}

Analyzing
the time complexity of the recursive algorithm: 递归次数 * 每次递归中的操作次数
Here, the recursive parameters are reduced each time the call is made , so a total of recursive calls 1are executed . Answer O ( n ) O(n)n




O ( n )



11. Find the time complexity of the following programs

int func(int n){
    
    
	int i = 0, sum = 0;
	while(sum < n) sum += ++i;
	return i;
}

Analysis
List some formulas to find the law:
by sum += ++i, get sum = sum + ++i
Then tset the number of loop executions, then:

t=1时, sum = 0 + ++i = 0 + 1;
t=2时, sum = 0 + 1 + ++i = 0 + 1 + 2;
t=3时, sum = 0 + 1 + 2 + ++i = 0 + 1 + 2 + 3;

So it can be deduced that the sum value calculation formula is:
t=k时, sum = 0 + 1 + 2 + ··· + ++(k-1)= 0 + 1 + 2 + ··· + k= k(1+k)/2
because sum < nit is the condition to terminate the loop, so we make k(1+k)/2 < n, that is,
k ( 1 + k ) 2 < n \frac{k(1+k)}{2} < n2k(1+k)<The calculation process of n
will not be described here, and the constant term is ignored in the result,njust keep that term.
The solution is k =n 1 2 n^{\frac{1}{2}}n21
So the time complexity is O( n 1 2 n^{\frac{1}{2}}n21)



The answer
O( n 1 2 n^{\frac{1}{2}}n21)



12. Find the time complexity of the following program

for(int i = 1; i <= n; i++)
	for(int j = 1; j <= i; j++)
		for(int k = 1; k <= j; k++)
			x++;

Analysis In the calculation process , the sum formula of arithmetic series and the sum of squares formula
are used : O ( ∑ i = 1 n ∑ j = 1 i ∑ k = 1 j 1 ) = ∑ i = 1 n ∑ j = 1 ij = ∑ i = 1 ni ( 1 + i ) 2 = 1 2 ∑ i = 1 n ( i + i 2 ) = 1 2 ( ( 1 + n ) n 2 + n ( n + 1 ) ( 2 n + 1 ) 6 ) = 6 n 2 − 2 n 3 + n + 1 12 ≈ O ( 1 6 n 3 ) ≈ O ( n 3 ) O(\sum\limits^{n}_{i=1}\sum\limits^{i} _{j=1}\sum\limits_{k=1}^{j}1)=\sum\limits^{n}_{i=1}\sum\limits^{i}_{j=1} j=\sum\limits^{n}_{i=1}\frac{i(1+i)}{2}=\frac{1}{2}\sum\limits^{n}_{i= 1}(i+i^2) =\frac{1}{2}(\frac{(1+n)n}{2}+\frac{n(n+1)(2n+1)}{6 }) =\frac{6n^2-2n^3+n+1}{12} \approx O(\frac{1}{6}n^3) \approx O(n^3)
O(i=1nj=1ik=1j1)=i=1nj=1ij=i=1n2i(1+i)=21i=1n(i+i2)=21(2(1+n)n+6n ( n + 1 ) ( 2 n + 1 ))=126 n22n3+n+1O(61n3)O ( n3 )



Answers
O ( n 3 ) O(n^3)O ( n3)


2 Find the recursive time complexity

1. The time required for an algorithm is represented by the following recursive equation. Try to find the time complexity level of the algorithm (in the following formula, n is the scale of the problem, and n is an integer power of 2)

T(n) = { 1 , n = 1 2 T ( n / 2 ) + n , n > 1 \begin{cases}1, n=1 \\2T(n/2)+n, n>1 \end{cases} { 1,n=12T ( n / 2 )+n,n>1

Analysis
For the time complexity of recursive programs, there are many methods to solve them, such as master theorem, recursive tree, recursive method, etc.
Here, the recursion method is used to solve the problem:
First, according to the meaning of the question, the formula ① is obtained:
T ( n ) = 2 T ( n 2 ) + n ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ① T(n) =2T(\frac{n}{2})+n ··············①T(n)=2T ( _2n)+n⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅①Then expandthe recursive formula
withthe extended recursion technique, andobtain the formula ②:= 2 T ( n 2 2 ) + n 2 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ② =2T(\frac{\ frac{n}{2}}{2})+\frac{n}{2}·············②n=n/2
=2T ( _22n)+2n⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅
Substitute ② into ① to get ③:
T ( n ) = 2 ( 2 T ( n 2 2 ) + n 2 ) + n ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ③ T(n)=2(2T( \frac{\frac{n}{2}}{2})+\frac{n}{2})+n······③T(n)=2 ( 2T ) _22n)+2n)+n⋅⋅⋅⋅⋅⋅⋅③Then
repeat the calculation steps of ②③ formula:
= T ( n ) = 2 T ( n / 2 ) + n =T(n)=2T(n/2)+n=T(n)=2T ( n / 2 )+n
= 2 2 T ( n 2 2 ) + 2 n =2^2T(\frac{n}{2^2})+2^n =22T (_22n)+2n
. . . ... ...
whenk = log 2 nk = log_2nk=log2n , the recursion ends, and this is the final recursion formula:
T ( n ) = 2 log 2 n T ( 1 ) + nlog 2 n T(n)=2^{log_{2}n}T(1 )+nlog_2nT(n)=2log2nT(1)+n l o g2n
= n ( l o g 2 n + 1 ) =n(log_2n+1) =n(log2n+1 )
= O ( nlog 2 n ) =O(nlog_2n)=O ( n l o g2n )



Answer
O ( nlog 2 n ) O(nlog_2n)O ( n l o g2n)

Replenish

The extended recursion technique is a method for solving recurrence relations. Its function is to replace the items on the right side of the equation in the recursive relation according to the recursive expression, which is called expansion, and the expanded items are expanded again, and then a sum expression will be obtained in sequence. In this way, the time complexity of the recursive algorithm can be easily calculated.



2. The time required by an algorithm is expressed by the following recursive equation, try to find the time complexity level of the algorithm

T(n) = { 1 , 0 ≤ n ≤ 1 2 T ( n − 1 ) + 1 , n > 1 \begin{cases}1, 0≤n≤1 \\2T(n-1)+1, n>1 \end{cases} { 1,0n12 T ( n1)+1,n>1

Analysis
Recursion method:
from T(n)=2T(n-1)+1, then deduce
T ( 1 ) = 2 T ( 0 ) + 1 = 2 0 + 2 0 − 1 T(1)=2T(0)+1=2^0+2^0-1T(1)=2T ( 0 ) _+1=20+201
T ( 2 ) = 2 T ( 1 ) + 1 = 2 1 + 2 1 − 1 T(2)=2T(1)+1=2^1+2^1-1 T(2)=2T ( 1 ) _+1=21+211
T ( 3 ) = 2 T ( 2 ) + 1 = 2 2 + 2 2 − 1 T(3)=2T(2)+1=2^2+2^2-1 T(3)=2T ( 2 ) _+1=22+221
. . . ... ...
T ( n ) = 2 T ( n − 1 ) + 1 = 2 n − 1 + 2 n − 1 − 1 = 2 n − 1 T(n)=2T(n-1)+1=2^{n-1}+2^{n-1}-1=2^n-1 T(n)=2 T ( n1)+1=2n1+2n11=2n1
Keep the highest order item, and finally get:T ( n ) = 2 n − 1 = O ( 2 n ) T(n)=2^n-1=O(2^n)T(n)=2n1=O(2n )



Answers
O ( 2 n ) O(2^n)O(2n )



3. Analyze the time complexity of Fibonacci sequence under recursive and non-recursive algorithms

F(n) = { 0 , n = 0 1 , n = 1 F ( n − 1 ) + F ( n − 2 ) , n > 1 \begin{cases}0, n=0 \\1, n=1 \\F(n-1)+F(n-2), n>1 \end{cases} 0,n=01,n=1F(n1)+F(n2),n>1

recursive algorithm

int Fib1(long long num){
    
     
  if ((num == 1) || (num == 0)){
    
     
    return num; 
  } 
  return Fib1(num-1)+Fib1(num-2); 
} 

Analysis
, figure it out and come back to fill in the hole (´ο`)

Answer


non-recursive algorithm:

int Fib(int n) {
    
    
  if (n == 0) return 0;
  if (n == 1) return 1;
  int a = 0, b = 1;
  
  for (int i = 2; i <= n; i++) {
    
    
    int temp = a + b;
    a = b;
    b = temp;
  }
  return b;
}

Parsing
The non-recursive algorithm requires only one loop. Use two variables to store the values ​​of the first two items, and use a loop to update them.



Answer
O ( n ) O(n)O ( n )



4. What is the time complexity of executing the following function mergesort
()? Assuming that the function call is written as mergesort ( 1,n ), the time complexity of the function merge () is O ( n ) O(n)O ( n )

void mergesort(int i, int j){
    
    
	int m;
	if(i != j){
    
    
		m = (i + j) / 2;
		mergesort(i, m);
		mergesort(i + 1, j);
		merge(i, j, m);  //这一句函数复杂度为O(n)
	}
}

analyze
insert image description here

Answer
O ( nlog 2 n ) O(nlog_2n)O ( n l o g2n) or O ( n l o g n ) O(nlogn) The two ways of writing O ( n log n )
are equivalent and both are correct





5. The time required by an algorithm is represented by the following recursive equation, try to find the time complexity level of the algorithm

T(n) = { 3 T ( n − 1 ) , n > 0 1 , o t h e r w i s e \begin{cases}3T(n-1), n>0 \\1, otherwise \end{cases} { 3 T ( n1),n>01,otherwise

Parsing
Expand according to recursion:
T ( n ) = 3 T ( n − 1 ) T(n)=3T(n-1)T(n)=3 T ( n1 )
= 3 ( 3 T ( n − 2 ) ) = 3(3T(n-2))=3 ( 3 T ( n2))
= 3 2 T ( n − 2 ) = 3^2T(n-2) =32 T ( n2 )
= 3 3 T ( n − 3 ) = 3^3T(n-3)=33 T ( n3)
… …
= 3 n T ( n − n ) = 3^nT(nn)=3nT(nn )
= 3 n T ( 0 ) = 3 n = 3^nT(0) = 3^n=3nT(0)=3n



answers
O ( 3 n ) O(3^n)O(3n )



6. The time required by an algorithm is represented by the following recursive equation, try to find the time complexity level of the algorithm

T(n) = { 2 T ( n − 1 ) – 1 , n > 0 , 1 , o t h e r w i s e \begin{cases}2T(n-1) – 1, n>0, \\1, otherwise \end{cases} { 2 T ( n1)–1,n>0,1,otherwise

Analyze
  and expand the recursive formula, calculate the value of the first few items, you will find that the result of each formula is equal to 1:
T ( n ) = 2 T ( n − 1 ) – 1 = 1 T(n) = 2T(n-1 ) – 1=1T(n)=2 T ( n1)–1=1
= 2 2 ( T ( n − 2 ) ) – 2 – 1 = 1 = 2^2(T(n-2)) – 2 – 1=1 =22(T(n2))–2–1=1
= 2 3 ( T ( n − 3 ) ) – 2 2 – 2 1 – 2 0 = 1 = 2^3(T(n-3)) – 2^2 – 2^1 – 2^0=1 =23(T(n3))222120=1
… … ...
so the time complexity isO ( 1 ) O(1)O(1)
  Note that although the recurrence relation in this question seems to be exponential at first glance, by expanding the recurrence relation, a completely different result is obtained. Therefore, when dealing with such problems, it is best to expand the recursion and then determine the time complexity.



Answers
O ( 1 ) O(1)O(1)





Comes with a wallpaper ヽ( ^ー^)人(^ー^ )ノ
insert image description here

Guess you like

Origin blog.csdn.net/m0_56494923/article/details/129540683