Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMPSCI 220

Assignment 1

2022

1. In each of the following situations, indicate whether f = O(g) or f = Ω(g) or f = Θ(g) by writing “true” in the respective cells.

(1 marks)

 

f (n)

g (n)

f = O (g)

f =  (g)

f = Θ (g)

 

(a)

n _ 100

n _ 200

 

 

 

(b)

n 

2

 

 

 

(c)

100n + log n

n + (log n)2

 

 

 

(d)

2n

2n+1

 

 

 

(e)

n3 + 2n log n

2n2

 

 

 

(f)

2n + n2 log n

2n + 22

 

 

 

2. An algorithm with running time Θ(,n(log n)2 ) spends exactly 1 millisecond to process an input of 1,024 data items. Assuming that the time T (n) of processing n items is directly proportional to,n(lg n)2 , that is T (n) = c,n(lg n)2  (“lg” denotes base-2 log”), derive a formula for T (n), and estimate how long this algorithm will process 216  items.

(1 marks)

3.  Suppose you are given two algorithms A, B that solve the same problem. The running time of A is TA (n) = cn2  and the running time of B is TB (n) = dn lg n where c, d are constants (and “lg” denotes “base-2 log”). Suppose that for small values of n, A runs faster than B . Moreover, when n = 210 , these two algorithms have the same running time.  What is the value of n for which the running time of A is exactly 512 times the running time of B?

(1 marks)

4.  Suppose you are given 3 algorithms A1 , A2  and A3  solving the same problem. You know that the running times of the three algorithms are respectively:

● T1 (n) = (2100 log n + n)(n2 + 100n log n),

● T2 (n) = 1.01n (n _ 3)1.5 ,

● T3 (n) = 1012n log10 2n + (10 log n)10 .

Which algorithm is the fastest for very large inputs?  Which algorithm is the slowest for very large inputs?

(1 marks)

5. Work out the number of elementary operations for the following algorithm. Analyse the asymp- totic behaviour of the running time growth function with respect to the input n (a positive integer).

function soMEALco(n)

a - 0

b - 1

for i = 1; i < n; i - i + 1 do

c - 0

for j = 1;j < a + b;j - j + 1 do

c - c + 1

end for

a - b

b - c

end for

return b

end function

(1 marks)

6. Derive an explicit (closed-form) formula for time T (n) of processing an array of size n if T (n) is specified implicitly by the recurrence:

T (n) = (T (0) + T (1) + . . . + T (n _ 1)) + cn;

T (0) = 0.

(1 marks)

7. The following algorithm takes as input two length-n positive integers a, b. Analyse the asymp- totic behaviour of this algorithm, treating all assignment operation, comparisons, control flow,

and arithmetic operations as elementary. Note that   mod  refers to the modulo operation, i.e.,

a mod b outputs the remainder of a ÷ b.

Express the best-case and the worst-case running time of the algorithm using Θ-notation, with respect to the input length n.

function ALco(a, b)

Require: Integers a, b

if a < b then

c - b

b - a

a - c

end if

while b  0 do

c - a mod b

a - b

b - c

end while

return a

end function

(2 marks)

8. Programming question:

● Question: Write a program that takes input a sequence of names with two attributes, and prints out the list of names whose first attributes are 0 and second attributes are sorted (in ascending order).  This question has two purposes:  (1) for you to obtain familiarity with the automated marker system which will be used more in later assignments; and (2) for you to get familiar with sorting algorithms.

● Algorithm:  To practice your algorithm abilities, please refrain from calling the system sort function. In this way, you should implement the sorting algorithm yourself. We will monitor your code and those who call the system sort function may not receive their mark.

● Input: The input consists of a number of lines. Each line contains a name, a 0/1-valued attribute, and another attribute whose value is a positive integer.  The names and the attributes are separated by space ‘  ’ . For example:

William  1  21

Mary  0  25

John  1  19

Lingling  0  21

Rachel  0  14

Adam  1  17

Vivian  0  21

Raj  1  19

● Output:  The output also consists of a number lines.  Each line contains a name.   (1) Only names whose first attribute is 0 is listed;  (2) The names are listed so that their corresponding second attributes are in ascending order; and (3) Whenever there are two names whose second attributes are the same, arrange the names in the same order as they appear in the input list. For example, the following output should correspond to the input above:

Rachel

Lingling

Vivian

Mary

● Language:  You can use Java, C, C++, PyPy, Python, C#.  See the system Help/FAQ for the detailed version numbers of each language.

● Automarker:   You  can  access the  automarker  system  https://www.automarker.cs. auckland.ac.nz.

● Submission: You should upload your submission (one single le with extension .java, .c, .cpp, .py, .py3, .cs) to the automarker system from Submit Assignment”.

● Number of attempts: There is a limit of 10 submission attempts for this assignment in order to get full mark. The last submission submitted before the assignment deadline will be the one marked.

● Useful information: You can assume that input will come from standard input (stdin) in a stream that represents one string per line. Output should be sent to standard output. In the automarker, the input will come from a file that is piped to standard in and the output redirected to a file, but your program shouldn’t know that.

Your answer to each question should be a single file (containing all nonstandard classes you use). You may assume that the automarker has access to all standard libraries.         If your submission was put in the queue before the assignment due time then it will be accepted. All submissions after the assignment due time will not be considered.

Start early! Lots of students will be submitting their work closer to the deadline so it may take 30 min before your program will be executed and you will see the results.

Your output should exactly match the one in the system for the submission to be correct. So be careful with the printing. No extra symbols! It may look the same on the first glance but may have a different end of line character or extra space.

Note. To help you get used to automarker, there is a sample question called “helloworld” for you to practice. The helloworld question has unlimited number of attempts.                 The input to the helloworld program consists of a single line

Welcome

The output to the helloworld program consists of a single line

Hello world