Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Homework 1

Problem 1.
(a) (10 points) Describe an algorithm that sorts an input array A[1 · · · n] by calling a
subroutine SQRTSORT(k), which sorts the subarray A[k + 1 · · · k +√n] in place,
given an arbitrary integer k between 0 and n −√n as input. (To simplify the problem,
assume that √n is an integer.) Your algorithm is only allowed to inspect or modify
the input array by calling SQRTSORT; in particular, your algorithm must not directly
compare, move or copy array elements. How many times does your algorithm call
SQRTSORT in the worst case? Give pseudocode. Remember that you cannot use
anything other than calling the SQRTSORT routine and maybe some loops. No
formal proof of correctness is mandatory. But write a few sentences justifying your
approach.

Ans:
We will use a nested loop to sort the array. At each complete iteration of the outer loop, we
will collect n/2 greatest terms from the sub-array at the end of the array.

At each iteration of the inner loop, we will pass n terms to the SQRTSORT(n) algorithm
with n/2 terms overlapping with the last iteration. {Reason for taking n/2 terms in
explained in b) part }.
By doing so, we will accumulate n/2 greatest terms at the end of the array at each
iteration of the outer loop.

SQRTSORT(k)
*/ defined /*

helper(A, n)
if(n== 0)return
t1 = 0
t2 = n
while( t2 >= sqrt(n)/2 )
for(t1 = 0 ; t1 <= n ; )
SQRTSORT(t1)
t1 = t1 + sqrt(n)/2
end of for loop
t2 = t2 - sqrt(n)/2
end of while loop
end of helper function

Homework 1 1
How many times does my algorithm call SQRTSORT in the worst case?

Number of times SQRTSORT called on 1st iteration = 2 n-1


Number of times SQRTSORT called on 2nd iteration = 2 n − 2
Number of times SQRTSORT called on kth iteration = 2 n − k − 1

2 n (2 n −1)
Total Number of calls = (2 n − 1) + (2 n − 2) + ... + 2 + 1 = 2
= (2n − n) = O(n)
In the worst-case Total number of calls to the SQRTSORT function is O(n)

(b) (10 points) Prove that your algorithm from part (a) is optimal up to constant
factors. In other words, if f(n) is the number of times your algorithm calls
SQRTSORT, prove that no algorithm can sort using o(f(n)) calls to SQRTSORT. Note
that here we are assuming that these algorithms cannot do anything other than
calling SQRTSORT repeatedly.
(Hint: Think of a typical worst case example for any sorting algorithm)

Ans:
To show that no algorithm can sort using o(f(n)) calls to SQRTSORT, we need to prove that
the number of calls to SQRTSORT(k) is minimum in our algorithm.

Let us assume that we are taking x terms common in each iteration.


We need to prove that the number of time our algorithm call SQRTSORT in minimum when
n
x= 2

Let Number of Iteration of Outer while loop = m


Number of iteration of inner loop = k

nk − xk = n
k= n
n −x

and m∗x=n
m = nx
Thus total Iteration = k*m
n n
= x ∗ n −x

Number of time our algorithm call SQRTSORT = total Iteration = n


x
∗ n
n −x

To find the minimum:


d

Homework 1 2
d n n
(
dx x
∗ n −x
) =0
n −2x)
−n2 ( x2 ( n −x)2
) =0
−n2 ( n − 2x) = 0
n
x= 2

2
(3x3 −6 n x2 +4nx−n n )
d
dx
(−n2 ( x2 ( n −2x) ))
n −x)2 x=
n = ( −2n x3 (−x+ n )4
)x= n >0
2 2

This implies that the number of time our algorithm call SQRTSORT is minimum when x
= n/2
Hence no algorithm can sort using o(f(n)) calls to SQRTSORT.

(c) (10 points) Now suppose SQRTSORT is implemented recursively, by calling your
sorting algorithm from part (a). For example, at the second level of recursion, the
algorithm is sorting arrays roughly of size n1/4. What is the worst-case running time
of the resulting sorting algorithm? (To simplify the analysis, assume that the array
size n has the form 2^2^k), so that repeated square roots are always integers.)

Ans:
According to the question our helper function is calling SQRTSORT() function (2n − n)
{calculated in part (a) } which in return is calling back the helper function with the new size
as n

So the recurrence relation of the Helper function will be

T (n) = (2n − n)T ( n) + c ∗ (2n − n)

(2n − n) ter is the number of times we call SQRTSORT(K)


T aking (2n − n) ~ n

T (n) = n ∗ T ( n) + c ∗ n

We will solve this recurrence relation with the help of the substitution method.

T (n) = n ∗ T ( n) + c ∗ n
1
T (n) = n ∗ ( n ∗ T (n 4 ) + c ∗ n) + c ∗ n
1 1 1 1
T (n) = n ∗ (n 2 ∗ (n 22 ∗ T (n 23 ) + c ∗ n 22 ) + c ∗ n) + c ∗ n
1 1 1 1 1 1
T (n) = n ∗ (n 2 ∗ (n 22 ∗ (n 23 ∗ (n 24 ∗ ... ∗ (c)...) + c ∗ n 23 ) + c ∗ n 22 ) + c ∗ n) +

Homework 1 3
1 1 1 1 1 1 1 1 1 1 1 1
T (n) = n1+ 2 + 23 + 23 +...+ 2k + c ∗ n1+ 2 + 23 + 23 +...+ 2k−1 + c ∗ n1+ 2 + 23 + 23 +...+ 2k−2 +

Problem 2. (10 points)


Let n = 2^l − 1 for some positive integer l. Suppose someone claims to hold an
unsorted array A[1 · · · n] of distinct l-bit strings; thus, exactly one l-bit string does
not appear in A. Suppose further that the only way we can access A is by calling the
function FETCHBIT(i, j), which returns the jth bit of the string A[i] in O(1) time.
Describe an algorithm to find the missing string in A using only O(n) calls to
FETCHBIT. Again, give either pseudocode or write a generic description.
Demonstrating the algorithm on a specific example will not fetch any marks.

Ans:
Idea - If all the combinations of the string were present, then the total number of the 0’s
and 1’s at all the j th bit of the array would be equal.

Explanation
In our algorithm, we first count the number of 0’s and 1’s in the j th bit of the string and
store them in the respective array

j = log2 (n + 1)

After counting There is two possible cases

If the number of the 0’s at the j th bit is less than the 1’s at the j th bit then the
missing string has 0 at the j th bit

else the missing string has 1 at the j th bit

Then recursively call the same function on the array of smaller size and after returning
print the missing bit

Base case
if n = 0 then the array is empty directly return from the function

if n = 1 then print the opposite bit (1 for 0 , 0 for 1) and return

Pseudo Code -

find_string(A, n)
if(n == 0)

Homework 1 4
then return
if(n == 1)
then if(FETCHBIT(0, 0) == 0)
print "1"
return
else
print "0"
return

j = lg(n+1)/lg(2)
zero_bit= {}
one_bit = {}

for i = 0 to n
if(FETCHBIT(i, j) == 1)
then one_bit.insert(A[i])
else
zero_bit.insert(A[i])

if(zero_bit.size() < one_bit.size()) then


find_string(zero_bit, zero_bit.size())

else
find_string(one_bit, one_bit.size())

Proof that there are only O(n) calls to FETCHBIT:

Number of calls to FETCHBIT = n/2 + n/4 + ... + n/2k

where n/2k = 1
k = log2 n

Number of calls to FETCHBIT = n + n/2 + n/4 + ... + n/2k


< n + n/2 + n/4 + ...
n
< = 2n
(1 − 1/2)
Number of calls to FETCHBIT = O(n)

Problem 3. (10 points)


Use recursion tree to solve the following recurrence.
T(n) = T(n/15) + T(n/10) + 2T(n/6) + √n

Ans:

Homework 1 5
Work done at level 0 = n
Work done at level 1 = n
15
+ n
10
+ n
6
+ n
6
1 1 2
= n[ 15 + 10 + 3]

=a n
1 1 2
where a = 15 + 10 + 3

n 1 1 2 n 1 1 2
Work done at level 2 = 15 [ 15 + 10 + 3] + 10 [ 15 + 10 + 3]

n 1 1 2 n 1 1 2
+ 6[ 15 + 10 + 3] + 6[ 15 + 10 + 3]

1 1 2 2
= n[ 15 + 10 + 3]

= a2 n
1 1 2
where a = 15 + 10 + 3

1 1 2 k
Work done at level k = n[ 15 + 10 + 3]

= ak n

Size of the tree :


To find the height of the tree, we will consider the (n/6) branch as it will be the
longest.
n
size of subproblem at level k = 6k
If k is the last level then
n
6k
=1
n = 6k

Homework 1 6
k = log6 n
Total number of levels in the recursion tree k = log6 n

Total Work Done = ∑log6n


i=0 a
k
n
log 6 n
a(log6 n+1) − 1
= n ∑ ak = n( )
i=0
a−1

c na(log6 n) = c ∗ n ∗ n(log6 a)
using base change formula
1 1 2
c∗ n ∗ n(log6 a) = c ∗ n0.5log6 ( 15 + 10 + 3 )
= c ∗ n0.5+log6 1.3909
= c ∗ n0.5+0.184 = c ∗ n0.684

ans:

T(n) = O(n0.684 )

Homework 1 7

You might also like