0% found this document useful (0 votes)
129 views3 pages

CS3230: Design and Analysis of Algorithms Semester 2, 2020-21, School of Computing, NUS

This document contains solutions to questions from a written assignment on the design and analysis of algorithms. Question 1 involves analyzing the probability of outcomes for tossing a biased coin and selecting random elements. Question 2 analyzes the time complexity of operations on a multi-stack data structure, showing both worst-case and amortized analysis are O(n) or O(log n) respectively.

Uploaded by

Sourabh Raj
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)
129 views3 pages

CS3230: Design and Analysis of Algorithms Semester 2, 2020-21, School of Computing, NUS

This document contains solutions to questions from a written assignment on the design and analysis of algorithms. Question 1 involves analyzing the probability of outcomes for tossing a biased coin and selecting random elements. Question 2 analyzes the time complexity of operations on a multi-stack data structure, showing both worst-case and amortized analysis are O(n) or O(log n) respectively.

Uploaded by

Sourabh Raj
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/ 3

CS3230: Design and Analysis of Algorithms

Semester 2, 2020-21, School of Computing, NUS


Solutions of Written Assignment 3

Q1 (a) Let X denote the random variable for the number of heads you get when you toss the biased coin
k times.
Then,
k
k X
Pr(X ≥ )= Pr(X = i)
2 k
i= 2
k    i  k−i
X k 1 3
=
i 4 4
i= k
2

k    i  k−i
X k 1 3
= k
2 4 4
i= k
2

k  i  k−i
 X    
k 1 3 k k
= k [Using ≤ k , ∀k]
2 k
4 4 x 2
i= 2
k  i  k−i
 X
k 1 3
= k
2 k
4 4
i= 2
k
 k X
k
2k
 
2 3 k
≤ 3−i [Using k ≤ ]
2 4 k 2 2
i= 2
k
2k 3 3 2
= [Using GP formula]
2 4k 2
k
3 2k 3 2
=
4 22k
k
32 3
≤ k [Using ≤ 1]
2 4
k
2log2 3 2
= [Using 2log2 x = x, ∀x]
2k
1.6k
≤ 2 2 −k [Using log2 3 ≤ 1.6]
k
= 2− 5
1
= k
25

Therefore, Pr(X ≥ k2 ) ≤ 1
k .
25

1
(b) Number of elements that are near median = all the elements that are ∈ [ n4 , 3n
4 ]
3n n
So # elements that are near median = 4 − 4 +1
Let Y denote the random variable having the value of the element that we picked. Then,

3n
− n4 + 1
 
n 3n 4
Pr Y ∈ [ , ] =
4 4 n
n
+ 1
= 2
n
1 1
= +
2 n
n
(c) Let A denote the event that the median of the 5 log n elements is < 4 and let B denote the event that
the median is > 3n
4
Then,

Pr(Failure) = Pr(A ∪ B)
≤ Pr(A) + Pr(B) [Using Union Bound Inequality]
≤ 2 Pr(A) Both the events are symmetric and identical
Pr(A) = Pr(# elements < n/4 are at-least n/2)

n
Here we treat each element as biased coin toss where you pick the BAD event, i.e. < 4 with probability
≤ 14 . So using (a) we know this probability with k = 5 log n is ≤ 1k
25

n
Pr(A) = Pr(X ≥ )
2
1
≤ k
25
1
= log n [Substitute k = 5 log n]
2
1
=
n
Pr(Failure) ≤ 2 Pr(A)
2

n
2
Pr(Success) ≥ 1 −
n

Q2 Throughout this, assuming each standard pop and push cost 1 unit of time.
a) MPUSH:
The
Pk worst case happen when stack 0 to stack k are full, where k is the maximum number such that
i
i=0 3 = (3k+1 − 1)/2 ≤ n. Then the time will be 3k+1 = Θ(n) (we need to pop and push each
element in each stack once, with one additional push for the new element at the end)

MPOP:

2
For any stack Si with i > 0, it can only be either empty, having 1/3 of its capacity, having 2/3 of its
capacity, or full.

Worst case happen when stack 0 to stack k are empty, also, using the observation,
Pk since stack k+1 is not
empty, then 3k+1 ×(1/3) = 3k ≤ n so k <= log3 n). Then the time will be i=0 3i ×2+1 = 3k+1 = Θ(n)
(similar to MPUSH, need to pop and and push each element in each stack once, with one additional
pop at the end)

b) Using accounting methods, for each MPUSH/MPOP, we will charge 20 log3 n = O(log n).
Now, consider an arbitrary MPUSH, let j equal to i after the loop (see the pseudocode in the question)
to find the first stack that is not full. Then the number of element in S0 , S1 , ..., Sj−1 , ... must be:
1, 31 , ..., 3j−1 , ... (correspondingly). Denote this configuration as Cj .
Pj−1
The cost to execute this MPUSH is 1 + 2 y=0 3y = 3j . After it is executed, the number of element in
S0 , S1 , ..., Sj−1 , ... must be: 1, 30 , ..., 3j−2 , ... (correspondingly).
Pj−1
To reach the configuration Cj again, we need at least y=1 (3y − 3y−1 ) = 3j−1 − 1 MPUSH operations.
Therefore, in MPUSH, i can only be equal a particular j at most once every 3j−1 MPUSH.
Pk m
So, total cost of MPUSH during any m operations can be at most j=0 3j 3j−1 = 3km = O(m log n)
(since j can be at most k = log3 n).
Similarly, consider an arbitrary MPOP, let j equal to i after the loop to find the first stack that is not
empty. Then the number of element in S0 , S1 , ..., Sj−1 , ... must be:
0, 0, ..., 0, ... (correspondingly). Denote this configuration as Dj .
Pj−1
The cost to execute this MPOP is 1 + 2 y=0 3y = 3j . After it is executed, the number of element in
S0 , S1 , ..., Sj−1 , ... must be: 0, 2(30 ), ..., 2(3j−2 ), ... (correspondingly).
Pj−1 y j−1
To reach the configuration Dj again, we need at least y=1 2(3 ) = 3 − 1 MPOP operations.
j−1
Therefore, in MPOP, i can only be equal a particularP j at most once every 3 MPOP. So, total cost
k m
of MPOP during any m operations can be at most j=0 3j 3j−1 = 3km = O(m log n) (since stack j
can not be empty, j can be at most k = log3 n = O(log n)).
Thus, during any m operation, the balance will be at least 20m log3 n − 3m log3 n − 3m log3 n ≥ 0.

You might also like