MATH CAMP - ALGORITHMS - HOMEWORK 3 SOLUTIONS MICHELLE BODNAR 1. Describe an implementation of Random(a,b) that only makes calls to Random(0,1). What is the expected running time of your procedure as a function of a and b? Without loss of generality we may assume that a = 0. Otherwise we can generate a random number between 0 and b − a, then add a to it. Algorithm 1 RANDOM(a,b) 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: n = ⌊lg(b)⌋ + 1 Initialize an array A of length n while true do for i = 1 to n do A[i] = RAN DOM (0, 1) end for if A holds the binary representation of a number less than or equal to b then return number represented by A end if end while Each iteration of the while loop takes O(n) time to run. The probability that the while loop stops on a given iteration is (b+1)/2n . Thus the expected running time is the expected number of iterations of the while-loop times n. This is given by: b + 1 i−1 b + 1 b+1 2n 2 2n ) ( ) = n ( ) ( ) = n = O(lg(b)). 2n 2n 2n b+1 b+1 i≥1 In the original setup of the problem, the runtime of the algorithm becomes O(lg(b − a)). n ∑ i (1 − 2. You are given a function Biased-Random which outputs 1 with probability p > 0 and 0 with probability 1 − p. You aren’t told what p is. Write an algorithm that uses Biased-Random to return 0 with probability 1/2 and 1 with probability 1/2. What is its expected running time? Algorithm 2 Unbiased-Random while true do a = Biased-Random b = Biased-Random if a > b then Return 1 else if a < b then Return 0 end if end while The probability of returning 1 on any particular iteration of the loop is p(1 − p). The probability of returning 0 on any iteration of the loop is also p(1 − p). Therefore the output is equally likely to be either 0 or 1. Each iteration of the loop takes constant time, so the expected runtime is ∑ i(1 − 2p(1 − p)) i−1 (2p(1 − p)) = i≥1 1 1 2p(1 − p) 2 MICHELLE BODNAR where we use the series identity ∑i≥1 iai = a (1−a)2 for ∣a∣ < 1. 3. I claim that the following algorithm produces at random any permutation besides the identity permutation: Algorithm 3 Permute(A) for i = 1 to n − 1 do Swap A[i] with A[Random(i + 1, n)] end for Does it do what I claim? Prove or give a counter-example. The code does not do what he intends. Suppose A = [1, 2, 3]. If the algorithm worked as proposed, then with nonzero probability the algorithm should output [3, 2, 1]. On the first iteration we swap A[1] with either A[2] or A[3]. Since we want [3, 2, 1] and will never again alter A[1], we must necessarily swap with A[3]. Now the current array is [3, 2, 1]. On the second (and final) iteration, we have no choice but to swap A[2] with A[3], so the resulting array is [3, 1, 2]. Thus, the procedure cannot possibly be producing random non-identity permutations. 4. In this problem, your goal is to identify who among a group of people has a certain disease. You collect a blood sample from each of the people in the group, and label them 1 through n. Suppose that you know in advance that exactly one person is infected with the disease, and you must identify who that person is by performing blood tests. In a single blood test, you can specify any subset of the samples, combine a drop of blood from each of these samples, and then get a result in constant time. If any sample in the subset is infected, the test will come up positive. Otherwise it will come up negative. Your goal is to write an algorithm to find the infected person with as few blood tests as possible. How many samples must you test? What are the best-case and worst-case numbers for your algorithm? We first assume that we have a function TEST(S) which returns true if any samples whose labels are in the set S are infected. The following algorithm returns the index of the infected sample in the range i through j, assuming such a sample exists. Algorithm 4 Find-Infected([i,j]) if j − i == 1 then Return i else if TEST([i..⌊(j − i)/2⌋]) then Return Find-Infected([1, ⌊(j − i)/2⌋]) else Return Find-Infected([⌊(j − i)/2⌋ + 1, j]) end if To find the infected sample from among n, we call Find-Infected([1, n]). The best and worst case runtimes for this algorithm are both O(log(n)). We could improve the best case runtime (and not affect the asymptotics of the worst case) by checking a single sample each time we reduce the size of the problem. This will only worsen the constant hidden by the big-oh. However, if each test is extremely costly then this constant becomes relevant, and it would be detrimental to perform these additional tests. 5. There are n friends getting together for a math-camper reunion, but due to demanding class schedules they cannot all be present at the same time. Each friend 1 ≤ i ≤ n has an arrival time ai and a departure time di > ai . They want to schedule a group photo for a time when as many of them are there as possible. Assume that the time required to actually take the photo is negligible. (a) Come up with the fastest algorithm you can to determine the optimal time to take the photo. What is its runtime? MATH CAMP - ALGORITHMS - HOMEWORK 3 SOLUTIONS 3 First sort the departure times and arrival times with Merge Sort, in O(n log n) time. Since we don’t know that arrival times will be integers, nor do we know the range of times, or anything about their distribution, we can’t use any of the awesomely fast sorts we saw in the second lecture. Merge Sort will have to do! Now we are in the setup of part (b), so see the solution below. There is a linear time solution to part (b), so the overall runtime of the algorithm is O(n log n). (b) Suppose now that your input is in a different format. You are given both a sorted array A of all arrival times, and a sorted array D of all departure times. Note: Since the orders in which people arrive and depart might be different, A[i] and D[i] aren’t necessarily referring to the same friend. Is it still possible to solve the problem? If so, write an efficient algorithm to solve it and analyze its runtime. If not, explain why. It is still possible. It will suffice to assume that we take the picture exactly at the departure time of someone (as they are leaving, so they are still in the picture). To find out how many people are present, we simply scan the arrival array from left to right to find out how many people have arrived prior to the departure time under consideration. Since we can maintain this as we go, the algorithm runs in linear time. The following algorithm shows explicitly how to solve this problem: Algorithm 5 Picture(A,D) maxN um = 0 bestT ime = 1 num = 0 j=1 for i = 1 to n do while A[j] ≤ D[i] do j =j+1 num = num + 1 end while if num > maxN um then maxN um = num bestT ime = i end if num = num − 1 end for Return D[bestT ime]
© Copyright 2026 Paperzz