Advanced Algorithms: Lab Problems Set 1

You might also like

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

Advanced Algorithms : Lab problems Set 1

1. Birthday paradox
The well known birthday paradox involves computing the minimum number of people required
in a room such that the probability of at least two people sharing a birthday is 50%.
(a) Write a program that takes the number of days and the probability as inputs and computes
the minimum number of people required such that the probability of two people sharing a
birthday is above a certain value(p). The following are the specifications.
Inputs :
The number of days N
The desired probability p (0 < p 1)
Output : The minimum number of people (m) such that the probability that at least
two share a birthday (among the N days) > p.
(b) Write a program to carry out a simulation and check the above.
2. Hat check problem
Write a program to simulate the hat check problem, taking the number of people (hats) as the
input. Verify by doing a reasonably large number of independent trials whether the number of
people who get their own hats on average is close to the theoretical expected value.
3. Coupon Collectors problem
Suppose there are b types of coupons and we wish to keep collecting coupons until we have
at least one of each type. What is the number of coupons we need to collect, on average for
achieving the above?
This problem can be viewed in a Balls and bins setting. i.e. there are b distinct bins. A ball is
thrown into a randomly selected bin. On average, how many balls need to be thrown so that
each bin contains at least one ball?
Write a program that takes the number of bins b as input and simulates the above. Perform a
simulation with multiple trials (say a few hundred or 1000 trials) and report the following.
(a) The average number of balls m that need to be thrown to achieve the above.
(b) The number of balls that need to be thrown to transition from one phase to next. Eg.
to transition from phase 0 (no bin has at least one ball) to phase 1 (exactly one bin is
non empty) exactly one trial is required. Similarly record in each trial the number of balls
required to be thrown to transition from phase 1 to 2, phase 2 to 3, . . . phase n 1 to n.
4. A randomized algorithm for the general selection problem
Generate a random array with reasonably large number of elements (N 10000). Implement the
randomized selection algorithm for finding the median discussed in class. Report the following :

1
(a) The size of the array being examined in each phase. The process is said to be in phase j
 j  j+1
3 3
if the size of the array being currently examined is between 4
n and 4
n where n
is the size of the array.
(b) The number of algorithm steps spent in each phase. For example X4 is the number of
 4
algorithm steps where the size of the sub array being processed is between 43 n and
 5
3
4
n. Recall that each step involves selecting a random pivot and finding the number
of elements less than it. If the pivot is not a central element, discard it and randomly
select another pivot, increment the number of steps in this phase by 1.

5. Hashing simulation
Simulate a hashing scenario using the balls and bins setting where the number of balls m is much
greater then the number of bins n. Conduct multiple simulation experiments and compute the
following across the experiments : Let nmax be the maximum number of items hashed to a single
slot amongst all the slots.

(a) The average (averaged across multiple experiments) value of nmax .


(b) The standard deviation of nmax

You might also like