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

ESO207 Theoretical Assignment 1 Semester II, 2016-17

The document describes 4 theoretical assignment questions. Question 1 involves analyzing the time complexity of a prime number checking algorithm and two subroutines. Question 2 involves ordering asymptotic growth functions. Question 3 describes an algorithm to choose a sequence X to maximize a difference sum given another sequence Y. Question 4 asks to design a tournament to identify the top 3 players with minimum matches.

Uploaded by

Rohit Gputa
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)
51 views

ESO207 Theoretical Assignment 1 Semester II, 2016-17

The document describes 4 theoretical assignment questions. Question 1 involves analyzing the time complexity of a prime number checking algorithm and two subroutines. Question 2 involves ordering asymptotic growth functions. Question 3 describes an algorithm to choose a sequence X to maximize a difference sum given another sequence Y. Question 4 asks to design a tournament to identify the top 3 players with minimum matches.

Uploaded by

Rohit Gputa
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/ 2

ESO207 Theoretical Assignment 1 Semester II, 2016-17

Due Date: 10th February, 2017

1. (a) (5 points) Consider the following algorithm.


1: procedure isprime(n)
2:
3: if n==0 OR n==1 then
4: return F alse
5:
6: ARRAY A[n] = [1, ..., 1] . Initialized to all ones. Index starts at 0
7: i=2
8:

9: while i 2 n do
10: j=2
11: while i j n do
12: A[i j 1] 0
13: j j+1
14: ii+1
15: if A[n 1] == 1 then
16: return T rue
17: else
18: return F alse
Find the time complexity of the above algorithm in the O() notation. Show your work.
(b) (7 points) Consider the following two subroutines:
- combine(A, B, C) takes 3 arrays A, B, C as input and returns an array as output.
- newSort(X) takes an array X as input and returns an array as output.
1: procedure combine(A, B, C) 1: procedure newSort(X)
2: 2:
3: i 0, j 0, k 0, t 0 3: if size(X) > 1 then
4: Initialize an array Y 4: newSort(X[0, . . . , n/3])
5: while i + j + k < n do 5: newSort(X[n/3 + 1, . . . , 2n/3])
6: if (A[i] B[j]) & (A[i] C[k]) then 6: newSort(X[2n/3 + 1, . . . , n 1])
7: Y [t] A[i] 7: combine(X[0, . . . , n/3], X[n/3 +
8: ii+1 1, . . . , 2n/3], X[2n/3 + 1, . . . , n 1])
9: if i = size(A) then
10: A[i]
11: else
12: if (B[j] < A[i]) & (B[j] C[k]) then
13: Y [t] B[j]
14: j j+1
15: if j = size(B) then
16: B[j]
17: else
18: Y [t] C[k]
19: k k+1
20: if k = size(C) then
21: C[k]
22: tt+1
23: return Y
Write a recursion for the time complexity of the algorithm newSort. Solve the recursion to find the
time complexity of newSort in the O() notation.
2. (12 points) Compare and order the asymptotic growth order of the following functions in ascending order.
Mention explicitly if any two functions are equivalent or if they cannot be compared. For each function
f (n) in the list below, what is the best function class O(g(n)) such that f (n) O(g(n))?
(i) 4 2n/2
(ii) en
(iii) 500n2 3n + 2
(iv) n!
(v) n2 + n + 2000
(vi) n log n
(vii) n ln n
(viii) n log(log(log n))

(ix) n
(x) 2n

n
(xi) sin n
(xii) nn
3. (13 points) X = (x1 , x2 , . . . , xn ) and Y = (y1 , y2 , . . . , yn ) are two sequences of n positive integers each such
that 1 xi yi for all 1 i n. Let
X n
C= |xi xi1 |.
i=2

Design an O(n) algorithm that given the sequence Y as input, chooses a sequence X such the value of C is
maximum. The algorithm needs to output only the value of the maximum such C. Prove the correctness
of your algorithm. See the following examples:

Example 1: Y = (10,5,7,3,9). Then your algorithm should output 29. ( As X = (10,1,7,1,9) is a valid
sequence that maximizes the sum C ).
Example 2: n=4, Y = (10,5,5,9). Then your algorithm should output 17. ( As X= (10,1,1,9) is a valid
sequence that maximizes the sum C )
4. (13 points) Give an algorithm to design a tournament consisting of n players so as to correctly identify the
top three players in the tournament, such that the number of matches is minimized in the tournament.
What is the minimum number of matches that such a tournament must have (as a function of n)?
Assume the following:
- for any two players, say A and B; if A is a better player than B, then A will defeat B in any match
played between them irrespective of any factors.
- for any three players, say A, B and C; if A defeats (is better than) B and B defeats (is better than)
C then A can be assumed to defeat (be better than) C.
- no match ever ends in a draw, i.e. between any two players we can always say one of them is better
than the other.
- before the tournament no information is known about the players.

You might also like