Docsity

Assignment 2 Solutions - Design and Analysis-Algorithms | CS 3510, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS 3510: Design and Analysis of Algorithms
Problem Set 2 Solutions
Problem 1
1. Suppose that each of the ncustomers gives a hat to a hat-checker at a restaurant, and the
hat-checker returns the hats to the customers in a random order, that is, gives each customer
a hat chosen uniformly at random from the remaining hats. What is the expected number of
customers who get their own hats back?
Solution: Let Xbe the random variable for the number customers who get back their
own hats, and let Xibe the Bernoulli random variable such that Xi= 1 if and only if the
i-th customer gets back his/her own hat. Then X=Pn
i=1 Xi, and thus by the linearity
of expectation, E[X] = Pn
i=1 E[Xi]. For each i, the number of permutations on ndistinct
elements that fixes the i-th element is (n−1)!, while the total number of permutations is n!.
Therefore, for each i,
E[Xi] = Pr[Xi= 1]
=(n−1)!
n!=1
n.
Hence,
E[X] =
n
X
i=1
E[Xi]
=n·1
n= 1.
2. Let a1, . . . , anbe a uniformly random permutation of {1, . . . , n}. What is the expected number
of inversions in a1, . . . , an? Recall that a pair of indices (i, j) is an inversion if i < j and
ai> aj.
Solution: Let Xbe the random variable for the total number inversions, and let Xij be
the Bernoulli random variable such that Xi= 1 if and only if there is an inversion at (i, j).
Then X=Pi<j Xij, and thus by the linearity of expectation, E[X] = Pi<j E[Xij]. It can be
shown that for each i < j, Pr[Xij = 1] = 1/2, as the probability that a random permutation
inverts aiand ajis 1/2. (Veryfiy this.) Therefore,
E[X] = X
i<j
E[Xij]
=X
i<j
Pr[Xij = 1]
=n
2·1
2=n(n−1)
4.
1
pf3
pf4

Partial preview of the text

Download Assignment 2 Solutions - Design and Analysis-Algorithms | CS 3510 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 3510: Design and Analysis of Algorithms

Problem Set 2 Solutions

Problem 1

  1. Suppose that each of the n customers gives a hat to a hat-checker at a restaurant, and the hat-checker returns the hats to the customers in a random order, that is, gives each customer a hat chosen uniformly at random from the remaining hats. What is the expected number of customers who get their own hats back? Solution: Let X be the random variable for the number customers who get back their own hats, and let Xi be the Bernoulli random variable such that Xi = 1 if and only if the i-th customer gets back his/her own hat. Then X =

∑n i=1 Xi, and thus by the linearity of expectation, E[X] =

∑n i=1 E[Xi].^ For each^ i, the number of permutations on^ n^ distinct elements that fixes the i-th element is (n − 1)!, while the total number of permutations is n!. Therefore, for each i,

E[Xi] = Pr[Xi = 1]

= (n − 1)! n!

n

Hence,

E[X] =

∑^ n

i=

E[Xi]

= n ·

n

  1. Let a 1 ,... , an be a uniformly random permutation of { 1 ,... , n}. What is the expected number of inversions in a 1 ,... , an? Recall that a pair of indices (i, j) is an inversion if i < j and ai > aj. Solution: Let X be the random variable for the total number inversions, and let Xij be the Bernoulli random variable such that Xi = 1 if and only if there is an inversion at (i, j). Then X =

i<j Xij^ , and thus by the linearity of expectation,^ E[X] =^

i<j E[Xij^ ]. It can be shown that for each i < j, Pr[Xij = 1] = 1/2, as the probability that a random permutation inverts ai and aj is 1/2. (Veryfiy this.) Therefore,

E[X] =

i<j

E[Xij ]

i<j

Pr[Xij = 1]

n 2

n(n − 1) 4

Problem 2

Design a deterministic algorithm that given a set S of n numbers and a target number t, determines in O(n log n) time whether there exist two elements in S whose sum equals t.

Solution:

Algorithm: On input S and t,

  1. Sort S by an O(n log n)-time sorting algorithm.
  2. For each x ∈ S, decide whether t − x ∈ S by binary search in the sorted sequence.

The correctness of the algorithm is clear. Step 1 takes O(n log n) time. In Step 2, binary search takes O(log n) time for each x ∈ S, and thus O(n log n) time for all elements in S. Therefore the algorithm takes O(n log n) time. 

Problem 3

Let n and m be positive integers with m  n (e.g. m = n^2 ). Let U = { 1 ,... , m}. Suppose there is a function h : U → { 1 ,... , n} that for each x ∈ U , assigns a value h(x) that is chosen uniformly at random from { 1 ,... , n}. Such a function h is usually called a hash function. Let S = {a 1 ,... , an} ⊂ U be a subset of n distinct numbers in U. Create a hash table T = T [1], T [2],... , T [n] which is a one-dimensional array, where each entry T [i] consists of a linked list. For each i, place ai on the linked list at T [h(ai)], the h(ai)-th entry of the table T.

  1. For each i ∈ { 1 ,... , n}, what is the expected length of the list at T [i], the i-th entry of the table T? Solution: Consider a fixed i ∈ { 1 ,... , n}. For each j ∈ { 1 ,... , n}, let Xj be the Bernoulli random variable such that Xj = 1 if and only if h(aj ) = i. Then |T [i]| =

∑n j=1 Xj^ , and by the linearity of expectation, the expected length of the list at T [i] is

E[|T (i)|] =

∑^ n

j=

E[Xj ]

∑^ n

j=

Pr[Xj = 1]

∑^ n

j=

Pr[h(aj ) = i]

= n ·

n

  1. Assume that the hash function h as described above could be computed in Θ(1) time. Based on Part (1), design a randomized algorithm that given a number x ∈ S and a target number t, uses the hash function h and hash table T to find in expected Θ(1) time whether there is a y ∈ S such that x + y = t.

Proof: We employ the decision tree model to prove the claim lower bound. For every comparison- based algorithm for the given problem, consider its decision tree for input size n and block size k. Since it is promised that in the input sequence, the elements in each block are all smaller than the elements in the succeeding block and larger than those in the preceding block, the total number of possible distinct permutations (and thus outputs) is (k!)n/k, and hence the decision tree must have at least (k!)n/k^ leaves. Therefore, the number of comparisons in the worst case, which is the height of the decision tree, is at least log 2 ((k!)n/k) = nk · log 2 k! = nk · Ω(k log k) = Ω(n log k). This bound is tight as it is attained by the algorithm that sorts each block separately.