Hashing and Randomized Algorithms

Probability

6.1. You are given ${\displaystyle n}$ unbiased coins, and perform the following process to generate all heads. Toss all ${\displaystyle n}$ coins independently at random onto a table. Each round consists of picking up all the tails-up coins and tossing them onto the table again. You repeat until all coins are heads.
(a) What is the expected number of rounds performed by the process?
(b) What is the expected number of coin tosses performed by the process?

6.2. Suppose we flip ${\displaystyle n}$ coins each of known bias, such that ${\displaystyle p_{i}}$ is the probability of the ${\displaystyle i}$th coin being a head. Present an efficient algorithm to determine the exact probability of getting exactly ${\displaystyle k}$ heads given ${\displaystyle p_{1},...,p_{n}\in [0,1]}$.

6.3. An inversion of a permutation is a pair of elements that are out of order.
(a) Show that a permutation of ${\displaystyle n}$ items has at most ${\displaystyle n(n-1)/2}$ inversions. Which permutation(s) have exactly n(n - 1)/2 inversions?
(b) Let P be a permutation and ${\displaystyle P^{r}}$ be the reversal of this permutation. Show that ${\displaystyle P}$ and ${\displaystyle P^{r}}$ have a total of exactly ${\displaystyle n(n-1)/2}$ inversions.
(c) Use the previous result to argue that the expected number of inversions in a random permutation is ${\displaystyle n(n-1)/4}$.

6.4. A derangement is a permutation ${\displaystyle p}$ of ${\displaystyle {1,...,n}}$ such that no item is in its proper position, that is, ${\displaystyle p_{i}\neq i}$ for all ${\displaystyle 1\leq i\leq n}$. What is the probability that a random permutation is a derangement?

Hashing

6.5. An all-Beatles radio station plays nothing but recordings by the Beatles, selecting the next song at random (uniformly with replacement). They get through about ten songs per hour. I listened for 90 minutes before hearing a repeated song. Estimate how many songs the Beatles recorded.

6.6. Given strings ${\displaystyle S}$ and ${\displaystyle T}$ of length ${\displaystyle n}$ and ${\displaystyle m}$ respectively, find the shortest window in ${\displaystyle S}$ that contains all the characters in ${\displaystyle T}$ in expected ${\displaystyle O(n+m)}$ time.

6.7. Design and implement an efficient data structure to maintain a least recently used (LRU) cache of ${\displaystyle n}$ integer elements. A LRU cache will discard the least recently accessed element once the cache has reached its capacity, supporting the following operations:
get(k)– Return the value associated with the key ${\displaystyle k}$ if it currently exists in the cache, otherwise return -1.
put(k,v) – Set the value associated with key ${\displaystyle k}$ to ${\displaystyle v}$, or insert if ${\displaystyle k}$ is not already present. If there are already ${\displaystyle n}$ items in the queue, delete the least recently used item before inserting ${\displaystyle (k,v)}$. Both operations should be done in ${\displaystyle O(1)}$ expected time.

Randomized Algorithms

6.8

6.9

6.10

6.11

6.12

Back to Chapter List