Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views4 pages

Tutorial I

Tut

Uploaded by

wandilegusha925
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)
3 views4 pages

Tutorial I

Tut

Uploaded by

wandilegusha925
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/ 4

UNIVERSITY OF KWAZULU-NATAL

SCHOOL OF MATHEMATICS, STATISTICS AND COMPUTER


SCIENCE

DISCIPLINE OF COMPUTER SCIENCE

Tutorial I

COURSE CODE: COMP201 Data Structures

Examiner: E. Jembere
Duration: open

Instructions

1. Attempt all questions.

1. Explain the meaning of the following expressions

a. 𝑓(𝑛) 𝑖𝑠 𝑂(1)

b. 𝑓(𝑛) 𝑖𝑠 𝑂(𝑛4 )

c. 𝑓(𝑛) 𝑖𝑠 𝑛𝑂(1)

2. Assuming that 𝑓1 (𝑛) 𝑖𝑠 𝑂(𝑔1 (𝑛)) and 𝑓2 (𝑛) 𝑖𝑠 𝑂(𝑔2 (𝑛)), prove the following
statements:

a. 𝑓1 (𝑛) + 𝑓2 (𝑛) 𝑖𝑠 𝑂((max[𝑔1 (𝑛), 𝑔2 (𝑛)])

b. 𝑓1 (𝑛) ∗ 𝑓2 (𝑛) 𝑖𝑠 𝑂(𝑔1 (𝑛) ∗ 𝑔2 (𝑛))

1|Page
3. Consider the following functions and using primitive counting, express the runtime of
these function in Big-Oh notation, along with a justification for your answer.

a.

b.
1 i n t function ( i n t a r r [ ] , i n t x ) {
3 int i=0;
4 int r=arr.length−1;
5 while ( i <= r ) {
6 i n t m = (i + r ) / 2 ;
7 i f ( a r r [m] = = x )
8 return m;
9 i f ( a r r [m] < x )
10 i=m+1;
11 else
12 r=m−1;
13 }
14 return −1;
15 }

4. Determine the complexity of the following algorithms for adding, multiplying and
transposing n x n matrices

a.

b.

c.

2|Page
5. Find the computational complexity of the following loops.

a. 𝑐𝑜𝑢𝑛𝑡 = 0;

𝑓𝑜𝑟(int 𝑖 = 1; 𝑖 <= 𝑛; 𝑖 + + )

𝑓𝑜𝑟(𝑖𝑛𝑡 𝑗 = 1; 𝑗 <= 𝑖; 𝑗 + + )

𝑐𝑜𝑢𝑛𝑡 + +;

b. 𝑐𝑛𝑡4 = 0;

𝑓𝑜𝑟(𝑖𝑛𝑡 𝑖 = 1; 𝑖 <= 𝑛; 𝑖 ∗= 2 )

𝑓𝑜𝑟(𝑖𝑛𝑡 𝑗 = 1; 𝑗 <= 𝑖; 𝑗 + + )

𝑐𝑛𝑡4 + +;

6. Determine which of the algorithms shown in the figure below for computing xn is
more efficient. Assume x, is a real number and n is an integer.

public double power(double x, int n) { public double power(double x, int n)


double result =x; {
if(n==0) double result =x;
result = 1; if(n==0) {
else
return 1;
𝑓𝑜𝑟(𝑖𝑛𝑡for(int
𝑖 = 1; 𝑖i ≤
= 𝑛;
1;𝑖 i< n; i++) {
+ +){
}
result = result*x;
else
}
return x * power(x, n-1);
return result;
}
}
(a) (b)

7. Design an algorithm to compute 𝑥 𝑛 (where 𝑥 is a double and n is an integer) that runs


in logarithmic time complexity.

Hint: Assuming the concept 𝑥 2 is defined, carefully write a method based on the
following definition of 𝑥 𝑛 :

1 𝑖𝑓 𝑛 = 0
𝑛 𝑛
𝑥𝑛 = {𝑥 2 ∗ 𝑥2 𝑖𝑓 𝑛%2 = 0
𝑛 𝑛
𝑥2 ∗ 𝑥2 ∗𝑥 𝑖𝑓 𝑛%2 = 1

3|Page
8. The computational complexity of some given algorithm is determined by the function,
𝑛
𝑇(𝑛) = 𝑇 ( 3 ) + 𝑛𝑐. Given that the algorithm terminates when n = 1, and T(1) = O(1),
Determine the complexity of the algorithm.

9. Use the animation of the Towers of Hanoi on


https://www.mathsisfun.com/games/towerofhanoi.html, to check whether you can
follow the logic of the Towers of Hanoi algorithm. If you can do so, you should be
able to solve the problem within the minimum possible moves. You are advised to use
3 towers and 4 discs.

10. Consider the pseudo-code in the figure below for illustrating the solution to the Towers
of Hanoi problem. The objective is to demonstrate how to move n disks from tower A
to tower B with the assistance of tower C. Initially, all the disks are on tower A. Disk n
is the biggest while the other disks n-1 up to 1 are in decreasing size. At no time can a
bigger disk be placed on a smaller one and only one disk can be moved at a time.

public void towersOfHanoi (int n, char A, char B, char C)


{
if (n>0) {
towersOfHanoi(n – 1, A, C, B)
display the phrase: “Move disk” + n + “from” + A +”to” + B
towersOfHanoi(n – 1, C, B, A)
}
}

Figure 1

Deduce the complexity of this algorithm.

11. Write an O(n) program that prompts the user to enter a sequence of integers ending
with 0 and finds the longest subsequence with the same number. The figure below
shows some sample output of the program

----------------------------------------------------------------------------------------------------------------

WISHING YOU ALL THE BEST

4|Page

You might also like