tutorial letter 201/1/2017 - unisa study notes

12
COS2611/201/1/2017 Tutorial Letter 201/1/2017 Programming: Data Structures COS2611 Semesters 1 School of Computing This tutorial letter contains solutions to assignment 01 BARCODE

Upload: others

Post on 26-Feb-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial Letter 201/1/2017 - Unisa Study Notes

COS2611/201/1/2017

Tutorial Letter 201/1/2017

Programming: Data Structures

COS2611

Semesters 1

School of Computing

This tutorial letter contains solutions to

assignment 01

BARCODE

Page 2: Tutorial Letter 201/1/2017 - Unisa Study Notes

2

CONTENTS

Page

1 Tutorial matter received so far ................................................................................................... 3

Question 1 ................................................................................................................................................ 4

Question 2 ................................................................................................................................................ 4

Question 3 ................................................................................................................................................ 4

Question 4 ................................................................................................................................................ 4

Question 5 ................................................................................................................................................ 5

Question 6 ................................................................................................................................................ 5

Question 7 ................................................................................................................................................ 5

Question 8 ................................................................................................................................................ 6

Question 9 ................................................................................................................................................ 6

Question 10 .............................................................................................................................................. 6

Question 11 .............................................................................................................................................. 7

Question 12 .............................................................................................................................................. 7

Question 13 .............................................................................................................................................. 7

Question 14 .............................................................................................................................................. 8

Question 15 .............................................................................................................................................. 8

Question 16 .............................................................................................................................................. 9

Question 17 .............................................................................................................................................. 9

Question 18 .............................................................................................................................................. 9

Question 19 .............................................................................................................................................. 9

Question 20 ............................................................................................................................................ 10

Question 21 ............................................................................................................................................ 10

Question 22 ............................................................................................................................................ 10

Question 23 ............................................................................................................................................ 11

Question 24 ............................................................................................................................................ 11

Question 25 ............................................................................................................................................ 11

Question 26 ............................................................................................................................................ 11

Question 27 ............................................................................................................................................ 12

Question 28 ............................................................................................................................................ 12

Question 29 ............................................................................................................................................ 12

Question 30 ............................................................................................................................................ 12

Page 3: Tutorial Letter 201/1/2017 - Unisa Study Notes

COS2611/201/1/2017

3

1 Tutorial matter received so far

COS2611/101/3/2017 General information and assignments

COS2611/201/1/2017 This letter: Solutions to Assignment 01

COS2611/202/1/2017 Solutions to Assignment 02

Dear Student

The purpose of this tutorial letter is to supply the solutions for Assignment 1.

Please feel free to contact your e-tutor or any of the lecturers with any problems, suggestions and/or queries. For this purpose, we appeal to you to make use of the myUnisa discussion forum where solutions to problems/questions and answers to queries posted on the forum are available to the benefit of all students. We also encourage you to actively participate in this forum by providing support (not complete solutions!) to your fellow students.

Page 4: Tutorial Letter 201/1/2017 - Unisa Study Notes

4

Question 1

for(int i = 10000; i > 0; i--)

sum++;

1. O(1) 3. O(log n) 2. O(n) 4. O(n log n)

Sum is executed a constant number of times.

Answer: 1

Question 2

int sum = 0;

int i = 20000;

while (i > 0)

{

sum += i;

i--;

}

Sum is executed a constant number of times.

Answer: 1

Question 3

for(int i = 0; i < 5n; i++)

sum++;

1. O(1) 3. O(5n) 2. O(5n Log n) 4. O(n)

Sum is executed n times. We ignore the constant.

Answer: 4

Question 4

for(int i = 0; i < n*n; i++)

sum++;

1. O(1) 3. O(n) 2. O(log n) 4. O(n log n)

1. O(1) 3. O(2n) 2. O(n2) 4. O(n)

Page 5: Tutorial Letter 201/1/2017 - Unisa Study Notes

COS2611/201/1/2017

5

Sum is executed n2 times.

Answer: 2

Question 5

int sum = 0;

int i = 2;

while (i > 1000)

{

sum += i;

i++;

}

Answer: 1

Question 6

for(int i = 0; i < 2000; i++)

for(int j = 2; j < n; j++)

sum++;

1. O(n) 3. O(n3) 2. O(n4) 4. O(n log n)

Outer for loop: 1 (constant)

Inner for loop: n times

Multiplication rule: 1*n = n (Nested for loops)

Answer: 1

Question 7

for(int i = 1; i < n; i*=2)

for(int j = 0; j < 1000; j++)

sum++;

1. O(1) 3. O(n log n) 2. O(n2) 4. O(log n)

Outer for loop: log n

Inner for loop: 1

Multiplication rule: log n *1 = log n

1. O(1) 3. O(n) 2. O(log n) 4. O(n log n)

Page 6: Tutorial Letter 201/1/2017 - Unisa Study Notes

6

Answer: 4

Question 8

for (int i = 1; i < n; i*=2)

for (int j = 0; j <n; j++)

sum = i + j;

Outer for loop: log n

Inner for loop: n

Multiplication rule: log n * n = n log n

Answer: 4

Question 9

for(int i = 0; i < n; i++)

sum++;

for(int j = 0; j < n; j++)

sum++;

1. O(n2) 3. O(n) 2. O(log n) 4. O(n log n)

First for loop: n

Second for loop: n

Addition rule: n + n = n (ignore constant)

Answer: 3

Question 10

for(int i = 1; i < n; i*=2)

sum++;

for(int j = 0; j < n*n; j++)

sum++;

1. O(n2) 3. O(n) 2. O(log n) 4. O(n log n)

First for loop: log n

Second for loop: n2

Addition rule: log n + n2 = n2

Answer: 1

1. O(n2) 3. O(log n) 2. O(n3) 4. O(n log n)

Page 7: Tutorial Letter 201/1/2017 - Unisa Study Notes

COS2611/201/1/2017

7

Question 11

for(int i = n; i < 1; i /=2)

sum++;

for(int i = 1; i < n; i = i*2)

sum++;

1. O(1) 3. O(log n) 2. O(n2) 4. O(n log n)

First for loop: log n

Second for loop: log n

Addition rule: log n + log n = log n

Answer: 3

Question 12

int i;

for(i = 0; i < n*n*n; i++)

for(int j = 0; j <i; j++)

sum++;

1. O(n) 3. O(n3) 2. O(n4) 4. O(n6)

Outer for loop: n3

Inner for loop: n3

Multiplication rule: n3+ n3 = n6

Answer: 4

Question 13

i = 1;

while (i<=n){

for (j=1; j<10; j++)

doIt();

i++;}

where doIt() has runtime of log n.

1. O(n4) 3. O(log n) 2. O(n2) 4. O(n log n)

Outer while loop: n

Inner for loop: 1

doIt(): log n

Page 8: Tutorial Letter 201/1/2017 - Unisa Study Notes

8

Multiplication rule: log n * n *1 = n log n

Answer: 4

Question 14

for(int i = 1; i < n; i++);

int j =n;

while (j >0)

{

Sum++;

j =j/2;

}

1. O(n) 3. O(n3) 2. O(n2) 4. O(n log n)

First for loop: n

while loop: log n

Addition rule: log n + n = n

Answer: 1

Question 15

i = 1;

while (i<=n){

for (int j=1; j<10; j++)

i++;

}

doIt();

where doIt() has runtime of log n.

while loop: n

Inner for loop: 1

Multiplication rule: n*1=n

doIt(): log n

Addition rule: n + log n = n

Answer: 1

1. O(n) 3. O(log n) 2. O(n2) 4. O(n log n)

Page 9: Tutorial Letter 201/1/2017 - Unisa Study Notes

COS2611/201/1/2017

9

Question 16

for(int i = 1; i <=n/2; i++)

for(int j = 1; j <= n; j++)

sum++;

1. O(n) 3. O(log n) 2. O(n2) 4. O(n log n)

Answer: 2

Question 17

for(int i = n; i < n; i= i/2)

sum++;

1. O(n) 3. O(log n) 2. O(n2) 4. O(n log n)

Answer: 3

Question 18

int i =1;

int j =1;

while(i>n)

{

while(j<n)

{

j++;

}

i++;

sum++;

}

1. O(n) 3. O(log n) 2. O(n2) 4. O(n log n)

Answer: 2

Question 19

int i =1;

int j =n;

while(i>n)

{

i++;

sum++;

Page 10: Tutorial Letter 201/1/2017 - Unisa Study Notes

10

}

while(j>0)

{

j/=2;

}

1. O(n) 3. O(log n) 2. O(n2) 4. O(n log n)

Answer: 1

Question 20

An algorithm takes 20 seconds for an input size of 10. How long will it take for an input size of 100 if the running time is O(n log n)?

20sec → 10*log 10

x sec → 100 log 100

(10 log 10) * x sec = (100 log 100)* 20 sec //cross multiplication.

x sec = 100 log 100 ×20𝑠𝑒𝑐

10 log 10

x sec= 400 sec

Answer: 2

Question 21

An algorithm takes 5 seconds for an input size of 10. How long will it take for an input size of 20 if the running time is O(n3)?

Answer: 4

Question 22

An algorithm takes 2 seconds for an input size of 4. How long will it take for an input size of 8 if the running time is O(2n)?

Answer: 2

1. 8 sec 3. 40 sec 2. 400 sec 4. 200 sec

1. 5 sec 3. 15 sec 2. 35 sec 4. 40 sec

1. 8 seconds 3. 16 seconds 2. 32 seconds 4. 64 seconds

Page 11: Tutorial Letter 201/1/2017 - Unisa Study Notes

COS2611/201/1/2017

11

Question 23

An algorithm takes 4 seconds for an input size of 100. How large a problem can be solved in 25 seconds if the running time is O(n2)?

4 sec → 1002

25 sec → x2

25 sec * 1002 = 4 sec * x2

250000 sec = x2

250 sec = x

Answer: 3

Question 24

An algorithm takes 5 seconds for an input size of 500. How large a problem can be solved in 50 seconds if the running time is linear O(n)?

Answer: 4

Question 25

Which of the following functions has the slowest growth rate (for n > 1)?

Answer: 3

Question 26

Which of the following functions has the fasted growth rate (for n > 1)?

Answer: 3

1. input size of 150 3. input size of 250 2. input size of 200 4. input size of 350

1. input size of 150 3. input size of 500 2. input size of 250 4. input size of 5000

1. n2 3. log n 2. n3/100 4. n3

1. n2 3. n3

2. n log n 4. log n + 220

Page 12: Tutorial Letter 201/1/2017 - Unisa Study Notes

12

Question 27

Which of the following functions is ordered by growth rate from largest to smallest?

Answer: 2

Question 28

Which of the following functions is ordered by growth rate from smallest to largest?

Answer: 1

Consider the table below when answering questions 29 and 30. The table shows the running time against input size (n) of 2 algorithms

Algorithm n = 2 n = 6 n = 12

A 10 sec 10 sec 10 sec

B 4sec 64 sec 4096 sec

Question 29

What is the running complexity of algorithm A?

Answer: 1

Question 30

What is the running complexity of algorithm B?

22 = 4

26 = 64

212= 4096

Answer: 3 ©

UNISA 2017

1. n log n; 2n ; n3; 24 3. 24; n log n; n3; 2n 2. 2n; n3; n log n; 24 4. 2n ; 24; n3; n log n;

1. 10 log n; n log n; 2n2 3. n log n; 2n2; 10 log n 2. 2n2; n log n; 10 log n 4. n log n; 10 log n; 2n2

1. O(1) 3. O(2n) 2. O(n2) 4. O(n log n)

1. O(n2 ) 3. O(2n) 2. O(n2) 4. O( n log n)