0% found this document useful (0 votes)
87 views

Data Structures & Algorithms: Tutorial 1 Solution Computational Complexity

This document is a tutorial on computational complexity and data structures and algorithms. It contains examples of analyzing the time complexity of various algorithms using Big O notation. Some key examples include sorting efficiencies from smallest to largest, determining if statements about Big O are true or false, and calculating the time complexity of different algorithms and code snippets in terms of Big O notation.

Uploaded by

meelan grg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Data Structures & Algorithms: Tutorial 1 Solution Computational Complexity

This document is a tutorial on computational complexity and data structures and algorithms. It contains examples of analyzing the time complexity of various algorithms using Big O notation. Some key examples include sorting efficiencies from smallest to largest, determining if statements about Big O are true or false, and calculating the time complexity of different algorithms and code snippets in terms of Big O notation.

Uploaded by

meelan grg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.

HCM
Khoa Khoa học & Kỹ thuật Máy tính

DATA STRUCTURES & ALGORITHMS


Tutorial 1 Solution

COMPUTATIONAL COMPLEXITY

Question 1.

Reorder the following efficiencies from the smallest to the largest:


a. n + n2 + n5
b. 20,000
c. 4n
d. n4
e. n!
f. nlog2(n)

Solution:
Efficiency: a measure of amount of time for an algorithm to execute (Time Efficiency) or a
measure of amount of memory needed for an algorithm to execute (Space Efficiency).

20,000 <nlog2(n) < n4 < n + n2 + n5 < 4n < n!

Question 2.

Decide whether these statements are True or False.


a. If O(f(n)) = O(g(n)) then f(n) = g(n)
b. If f(n) = O(g(n)) and g(n) = O(f(n)) then f(n) = g(n)

Solution:
a. False.
Ex: f(n) = 2n, g(n) = 3n
b.False
Ex: f(n) = n, g(n) = n + 1.
Explanation:
First, we review the definition of Big-O:
f(n) = O(g(n))  there are constants C R+ and kN such that 0 ≤ f(n) ≤ Cg(n) where n > k

Here, we have : f(n) ≤ 1× g(n) where n > 1. Hence, f(n) = O(g(n)).


And, we also have: g(n) ≤ 2 × f(n) where n > 1. Hence, g(n) = O(f(n)).

Tài liệu môn Cấu trúc dữ liệu 503001 – HK01/2010-2011


TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính

But f(n) ≠ g(n).

Question 3.

Determine the big-O notation for the following:


a. n2 + n5
b. 100n + 20n2/3 + 15n5/7
c. 9log2(n) + 6n
d. n100 + 2n
e. n! + 2n
f. nlog2(n) + 5n

Solution:
a. O(n5)
b. O(n)
c. O(n)
d. O(2n)
e. O(n!)
f. O(nlog2(n))
Question 4.

Calculate the run-time efficiency of the following program segment:


1 i = n
2 loop (i >= n/2)
1 j = n – i
2 loop (j < i)
1 print(i, j)
2 j = j + 1
3 end loop
4 i = i - 1
3 end loop

Solution:
Assume that n/2 = [n/2] in the given program segment.
 If n is even, the run-time efficiency is
n + (n – 2) + (n – 4) + … + 2 = n(n + 2)/4 = O(n2)
 If n is odd, the run-time efficiency is
n + (n – 2) + (n – 4) + … + 1 = (n + 1)2/4 = O(n2)
Or generally, the run-time efficiency of the given program segment is O(n2).

Question 5.

Tài liệu môn Cấu trúc dữ liệu 503001 – HK01/2010-2011


TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính

Estimating the time complexity of the following program segment:


1 i = 0
2 loop (i < N)
1 j = i
2 loop (j < N)
1k = 0
2loop (k < M)
1 x = 0
2 loop (x < N)
1 print(i, j, k)
2 x = x + 3
3 end loop
4 k = k + 1
3 end loop
4 k = 0
5 loop (k < 2*M)
1 print(k)
2 k = k + 1
6 end loop
7 j = j + 1
3 end loop
4 i = i + 1
3 end loop

Solution:
The iteration of variable i is executed N times. For each loop of variable i, the iteration of
variable j is executed N, N-1, N-2, …, 1 times. For each loop of variable j, the first iteration of
variable k is executed M times. For each loop of variable k, the iteration of variable x is
executed [(N+2)/3] times; the second iteration of variable k is executed 2M times. Therefore,
the run-time efficiency is
N*(M*[(N+2)/3] + 2*M) + (N-1)*(M*[(N+2)/3] + 2*M) + … + 1*(M*[(N+2)/3] + 2*M)
= N*(N+1)*(M*[(N+2)/3] + 2*M) / 2
= O(N3*M)

Question 6.

If the algorithm doIt has an efficiency factor of 7n, calculate the run time efficiency of the
following program segment:
1i = 1
2 loop (i <= n)
1j = 1
2 loop (j < n)
1 k = 1

Tài liệu môn Cấu trúc dữ liệu 503001 – HK01/2010-2011


TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính

2 loop (k <= n)
1 doIt(…)
2 k = k + 1
3 end loop
4 j = j + 1
3 end loop
4 i = i + 1
3 end loop

Solution:
There are 3 nested loops, the iteration of variable i is executed n times, j is executed n-1 times,
k is executed n times. Therefore, the run-time efficiency is n(n-1)n(7n) = O(n4).

Question 7.

Write a recurrence equation for the running time T(n) of f(n), and solve that recurrence.
Algorithm f (val n <integer>)
Pre n must be greater than 0
Return integer value of f corresponding to n
1 if (n = 1)
1 return 1
2 else
1 return f(n – 1) + f(n – 1)
End f

Solution:
T(1) = 1
T(n) = 1 + 2*T(n-1) = 1 + 2*(1 + 2*T(n-2)) = …
= 1 + 2 + 22 + … + 2n-1 = 2n - 1 = O(2n)

Question 8.

Write a recurrence equation for the running time T(n) of g(n), and solve that recurrence.
Algorithm g (val n <integer>)
Pre n must be greater than 0
Return integer value of g corresponding to n
1 if (n = 1)
1 return 1
2 else
1 return 2*g(n – 1)
End g

Tài liệu môn Cấu trúc dữ liệu 503001 – HK01/2010-2011


TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM
Khoa Khoa học & Kỹ thuật Máy tính

Solution:
T(1) = 1
T(n) = 1 + T(n-1) = 1 + 1 + T(n-2) = 1 + 1 + … + 1 + 1 + T(1) = n = O(n)

Question 9.

Given that the efficiency of an algorithm is 5nlog2(n), if a step in this algorithm takes 1
nanosecond (10−9), how long does it take the algorithm to process an input of size 7000?

Solution:
5 7000 log2(7000) 10−9 = 4.4706 10-4s

-- End --

Tài liệu môn Cấu trúc dữ liệu 503001 – HK01/2010-2011

You might also like