


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



∑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,
∑^ n
i=
E[Xi]
= n ·
n
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
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,
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.
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.
∑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
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.