Time Complexity Practice Question
Time Complexity Practice Question
Overview. This worksheet has worked examples for the running time analysis of small pieces of code, by
counting the number of performed operations.
To get the full benefit, go through the examples in the same order they are presented and avoid looking at the
solutions and explanations until you have tried to find the running time on your own.
You will find at the end of the worksheet a summary of basic summations and logarithms rules, which are
helpful when analyzing running times for simple pieces of code.
Question. For each of the following pieces of code, find the number of times op() is called as a function of the
input size n. Express your answer in terms of the Big-Theta notation.
Single Loops
Ex.1
for (i = 10; i < n + 5; i += 2)
Solution. Θ(n).
op(); op() is called exactly (n + 5 − 10)/2 times.
Ex.4
for (i = 0; i < n * n * n; i *= 2)
Solution. Θ(log n).
op(); log2 n 3 = 3 log2 n. We can drop the coefficient and
ignore the base.
Ex.6
for (i = 10; i < n; i++)
Solution. Θ(n 2 ).
for (j = 0; j < n; j += 2) n 1
op() is called exactly (n − 10) × = n 2 − 5n
op(); 2 2
times.
Ex.8
for (i = 0; i < n; i++) { Solution. Θ(n 2 ).
for (j = 0; j < n; j++) At each iteration of the outer loop, the first inner
op(); loop runs and then the second inner loop runs.
for (j = 1; j < n; j *= 2); Therefore, op() is called n × (n + log n)
op(); = n 2 + n log n times, which is in the order of n 2.
}
Explanation. To understand why Eq. 1 is true, note that if we add the numbers twice, we get:
0 + 1 + … + (n − 1) + n
+ n + (n − 1) + … + 1 + 0
= n + n + … + n + n n +1 terms
We can also think of this visually as follows: How many blue circles for n = 4?
n rows and n + 1 columns
area = n × (n + 1)
1
blue circles = × n × (n + 1)
2
COS226 Analysis of Algorithms: Practice Examples Spring ‘20
n 2 −10
(n 2 − 10) × ((n 2 − 10) + 1)
∑
i , which equals to = (using Eq. 1)
i=0
2
Simplifying the result and dropping the coefficients and lower order terms leads to n 4.
n
1
12 + 22 + 32 + … + n 2 = i2 ∼ n3
∑
Eq. 2
3
i=1
Explanation.
To understand why this summation is in the order of n 3, consider the following two observations:
1. Upper bound: 12 + 22 + … + n 2
≤ n2 + n2 + … + n2
≤ n × n2
Therefore, the order of growth is not more than n 3 .
1
Therefore, the order of growth is not less than 8
n 3.
Since we drop constant coefficients, the upper and lower bounds imply that the order of growth is n 3.
See B.6 in the math cheat sheet for the exact closed form of this summation.
COS226 Analysis of Algorithms: Practice Examples Spring ‘20
∑
op(); j = 2, etc. Therefore, op() is called j times.
j=1
However, i takes all the values from 1 → n. Therefore, the total number of times op() is called is:
n i n
i(i + 1)
∑∑ ∑
j= (using Eq. 1)
i=1 j=1 i=1
2
n n n
1 2 1 1
i 2+ 1
∑ ∑ ∑
= i + i = i
2 2 2 2
i=1 i=1 i=1
n
1
i 2, which is in the order of n 3 (using Eq. 2).
∑
Dropping lower order terms, we are left with
2
i=1
where m = lg n and r = 2 in this exercise. The following is a visual explanation for this special case:
20
20 21
0
2 21 22
21 = 22 − 1 22 = 23 − 1 23 = 24 − 1
3 7 15
COS226 Analysis of Algorithms: Practice Examples Spring ‘20
Therefore, op() is called lg 1 + lg 2 + … + lg n times. Using the identity log(a b) = log a + log b, we
can rewrite the summation as: lg 1 + lg 2 + … + lg n = lg(1 × 2 × . . . × n) = lg(n!).
Using Stirling’s Approximation:
lg(n!) ∼ n lg n Eq. 5
This is n multiplied by a Harmonic Number. Harmonic numbers can be approximated by turning the
summation into an integral:
n n
1 1
∑i ∫1 i
∼ di = ln n Eq. 6
i=1
Recursive Algorithms
Ex.21
void f(int n) {
Solution. Θ(n).
if (n == 0) return; Consider the following visualization for the
op(); recursive calls:
f(n/2);
f(n/2);
}
f(n) 20 op() calls
f( n2 ) f( n2 ) 21 op() calls
op() is performed once in each recursive call.
The total number of calls to op() is
f( n4 ) f( n4 ) f( n4 ) f( n4 ) 22 op() calls
0 1 2 lg n
2 +2 +2 +…+2 , which is in the order of n …
(see Eq.3).
f( nn ) f( nn ) 2lg n op() calls
COS226 Analysis of Algorithms: Practice Examples Spring ‘20
Ex.23
void f(int n) {
Solution. Θ(n lg n).
if (n == 0) return; In each call to f(n), op() is performed a number of
for (int i = 0; i < n; i++) times that depends on the value of the argument n .
op(); The visualization below shows the value of n in each
f(n/2); of the recursive calls and the number of times op() is
f(n/2); performed at each level of the recursion tree.
} The recursion tree has lg n + 1 levels, and op() is
performed a total of n times at each level. Therefore,
the order of growth is n lg n.
f( n2 ) f( n2 )
n
2
+ n
2
=n op() calls
lg n + 1
f( n4 ) f( n4 ) f( n4 ) f( n4 ) 4× n
4
=n op() calls
…
n
f( nn ) f( nn ) n× n
=n op() calls
A. Logarithms Notation:
b log n (no base) → used with
1. a = c → b = loga c Definition
orders of growth to indicate
2. logb b = 1, logb 1 = 0 Special cases
that the base is not important.
x
3. logb( ) = logb x − logb y Logarithms with different bases
y
differ by a constant factor as
4. logb(x × y) = logb x + logb y shown in the listed identities.
5. logb x y = y × logb x Follows directly from the previous rule. lg n → base 2.
logc x ln n → natural logarithm
6. logb x = Changing bases
logc b (base is e)
7. x logb y = y logb x
8. b logb x = x Follows directly from the previous rule.
9. lg(n!) = ∼ n lg n Stirling’s Approximation
B. Summations
n
∑
1. i =1+2+…+n Definition
i=1
n
∑
2. c =c +c +…+c =c ×n If c does not depend on i.
i=1
n n
∑ ∑
3. c × fi = c × fi
i=1 i=1
n n n
∑ ∑ ∑
4. fi + gi = fi + gi
i=1 i=1 i=1
n
n(n + 1)
∑
5. i =1+2+3+…+n =
i=1
2
n
n(n + 1)(2n + 1)
i 2 = 12 + 22 + 32 + … + n 2 =
∑
6.
i=1
6
n
r n+1 − 1
r i = r0 + r1 + r 2 + … + r n =
∑
7. , r ≠1 Geometric Sum.
i=0
r −1
n n
1 1 1 1 Special cases of a geometric sum
2i = 2n+1 − 1 , ( 2 )i = 1+ 2 + 4 +…+ 2 n = ∼ 2
∑ ∑
8.
(r = 2 and r = 0.5).
i=0 i=0
n n
1 1 1 1 1
∑i ∫
9. =1+ + +…+ ∼ di = ln n Harmonic Number Hn.
i=1
2 3 n 1 i
Prepared by Ibrahim Albluwi (isma@[Link]). Many thanks to Kevin Wayne for providing very valuable feedback.
Loop time complexity practice 9/6/2024
Closed form for a summation is the answer formula for it. E.g. 1+2+3+4+…+N has closed form N(N+1)/2 .
A. Normal
0. For each of the questions below say which letters can show up in the final time
complexity. You should answer this BEFORE you solve them. E.g. i,N,k,M,t,S etc
1.
for(i=1; i<=3N; i=i+3)
for(k=1; k<=M; k=k*6)
printf("C");
2.
for(i=0; i<=(N+1); i=i+S)
for(t=1; t<=M; t=t+3)
printf("B");
3.
for(i=0; i<=N; i=i+7)
1
for(k=1; k<=i; k=k+2)
printf("B");
4.
for(i=N; i>=1; i=i/2)
for(k=0; k<=M; k=k+S)
printf("B");
5.
for(i=N; i>=0; i=i-5)
for(k=N; k>=1; k=k/3)
printf("B");
7. Good.
// int mistery(int N, int v); has Θ(N2)
for(i=N; i>=0; i--){
int res = mistery(i, i);
for(k=N; k>=1; k=k-1)
printf("B");
8. Good
for (i = 101; i<=(100+N); i++)
for (k=1; k<=i; k = k+1)
printf("B ");
9. Good
for (i=1; i<=N; i=i*2)
for (k=1; k<=2*i; k=k+1)
for (t=0; t<7; t=t+1)
printf("A");
10.
for(t=1; t<=N; t=t*3)
for(i=10; i<=N; i=i+2)
for(k=N; k>=1; k=k-5)
printf("G");
2
11.
// *** good example
for(i=1; i<=N; i=i+1)
for(k=1; k<=i; k=k+k)
for(t=1; t<=S; t++)
printf("G");
12.
for (i=1; i<=N; i=i+1) {
for (k=1; k<=i; k++) {
printf("B");
for (t=1; t<=k; t++)
printf("C");
} // end for-k
…
} // end for-i
13. good.
// Assume void do_smth(int N);has Θ(N)
for (i=1; i<=N; i=i+1) {
for (k=1; k<=i; k++) {
printf("B");
for (t=1; t<=k; t++)
do_smth(k);
} // end for-k
…
} // end for-i
14.
// example from Dr. Weems
for (i=1; i<=N-1; i++) {
…
for (k=i+1; k<=N; k++) {
temp=ab[ell][k];
ab[ell][k]=ab[i][k];
ab[i][k]=temp;
for (t=i+1; t<=N; t++)
ab[t][k] -= ab[t][i]*ab[i][k];
}// end for-k
…
} // end for-i
3
15.
for (i=1; i<=N; i=i+i) {
for (k=1; k<=N; k++) {
printf("B");
for (t=1; t<=N; t++)
printf("C");
} // end for-k
…
} // end for-i
True/False: if we remove k=0 from the for(k…) loop, it will not change the TC of the entire code.
True/False: if we remove k=0 from the for(i…) loop, it will not change the TC of the entire code.
2)
for(i=0, k=0; i<=N; i=i+k)
for(k=0; k<M; k=k+1)
for(t=0; t<=k; t=t+1)
printf("G");
3)
for(i=0; i<=N; i=i+k)
for(k=0; k<S; k=k+1)
printf("B");
4)
i = 0;
while (i<=N){
for(t=0, k=1; k<N; t=t+1, k=2*k)
printf("G");
i=i+t;
}
A. Variations
for(k=7; k<=M; k=k+6)
printf("A");
Master Theorem
The Master Theorem applies to recurrences of the following form:
where a ≥ 1 and b > 1 are constants and f (n) is an asymptotically positive function.
There are 3 cases:
1. If f (n) = O(nlogb a− ) for some constant > 0, then T (n) = Θ(nlogb a ).
2. If f (n) = Θ(nlogb a logk n) with1 k ≥ 0, then T (n) = Θ(nlogb a logk+1 n).
3. If f (n) = Ω(nlogb a+ ) with > 0, and f (n) satisfies the regularity condition, then T (n) = Θ(f (n)).
Regularity condition: af (n/b) ≤ cf (n) for some constant c < 1 and all sufficiently large n.
Practice Problems
For each of the following recurrences, give an expression for the runtime T (n) if the recurrence can be
solved with the Master Theorem. Otherwise, indicate that the Master Theorem does not apply.
1. T (n) = 3T (n/2) + n2
2. T (n) = 4T (n/2) + n2
3. T (n) = T (n/2) + 2n
4. T (n) = 2n T (n/2) + nn
1
7. T (n) = 2T (n/2) + n/ log n
√
11. T (n) = 2T (n/2) + log n
√
13. T (n) = 3T (n/3) + n
2
Solutions
1. T (n) = 3T (n/2) + n2 =⇒ T (n) = Θ(n2 ) (Case 3)
7. T (n) = 2T (n/2) + n/ log n =⇒ Does not apply (non-polynomial difference between f (n) and nlogb a )
19. T (n) = 64T (n/8) − n2 log n =⇒ Does not apply (f (n) is not positive)
22. T (n) = T (n/2) + n(2 − cos n) =⇒ Does not apply. We are in Case 3, but the regularity condition is
violated. (Consider n = 2πk, where k is odd and arbitrarily large. For any such choice of n, you can
show that c ≥ 3/2, thereby violating the regularity condition.)
3
Practice Question