Hire-Assistant (n)
1 best = 0 // candidate 0 is a dummy, least-qualified
2 for i = 1 to n
3 interview candidate i
4 if candidate i is better than candidate best
5 best = i
6 hire candidate i
What is the running time of this algorithm?
O(cin + chm)
Worst case: O(chn)
An average running time would be more useful in this case
Randomized-Hire-Assistant (n)
1 randomly permute the list of candidates
2 best = 0 // candidate 0 is a dummy, least-qualified
3 for i = 1 to n
4 interview candidate i
5 if candidate i is better than candidate best
6 best = i
7 hire candidate i
The conditional probability of an event A given that another event B occurs is defined to be:
Pr{A | B} = Pr{A ∩ B} Pr{B}
Two events are independent if
Pr{A ∩ B} = Pr{A} Pr{B}
If Pr{B} ≠ 0, this is equivalent to:
Pr{A | B} = Pr{A}
Pr{A ∩ B} | = | Pr{B} Pr{A | B} |
= | Pr{A} Pr{B | A} |
Solving for Pr{A | B}, we get Bayes's Theorem:
Pr{A | B} = Pr{A} Pr{B | A} Pr{B}
There are two envelopes in front of you. One contains x dollars and the other contains 2x dollars. You choose one, open it, and get $100. You are then offered a switch if you want it. You can take the other envelope instead. Here are two arguments:
Which if any of these arguments do you believe? What is the bug in the other argument?
You are a contestant in a game show in which a prize is hidden behind one of three curtains. You will win the prize if you select the correct curtain. After you have picked one curtain out before the curtain is lifted, the emcee lifts one of the other curtains, revealing an empty stage, and asks if you would like to switch from your current selection to the remaining curtain. How will your chances change if you switch?
(The celebrated Monty Hall problem.)
A discrete random variable X is a function from finite or countably infinite sample space S to the real numbers. Thus, it associates a real number with each possible outcome of an experiment.
For a random variable X and a real number x, we define the event X = x to be {s ∈ S : X(s) = x}
Thus: Pr{X = x} = Σ s∈S:X(s)=x Pr{s}
The function f(x) = Pr{X = x} is the probability density function of the random variable X. Thus:
Σ x Pr{X = x} = 1
The expected value (also called expectation or mean) of a discrete random variable X is
E[X] = Σx x⋅Pr{X = x}
E[X] is well-defined as long as the summation is finite or converges. Expectation is usually denoted by μX.
Suppose that two fair coins are flipped and you earn $3 for each head but lose $2 for each tail. Your average earnings is the expectation of the random variable X representing your earnings:
E[X] | = | 6⋅Pr{2 H's} + 1⋅Pr{1 H, 1 T} − 4.Pr{2 T's} |
= | 6(1/4) + 1(1/2) − 4(1/4) = 1 |
If X is any random variable, then any function g(x) defines a new random variable g(X). The expectation of g(X) is:
E[g(X)] = Σx g(x)⋅Pr{X = x}
If we let g(x) = ax, we have for any constant s
E[aX] = aE[X]
For any two random variables X and Y and any constant a
E[aX + Y] = aE[X] + E[Y]
Permute-By-Sorting (A) 1 n = A.length 2 let P[1..n] be a new array 3 for i = 1 to n 4 P[i] = Random(1, n3) 5 sort A, using P as sort keys
Permute-By-Cyclic (A) 1 n = A.length 2 let B[1..n] be a new array 3 offset = Random(1, n) 4 for i = 1 to n 5 dest = i + offset 6 if dest > n 7 dest = dest - n 8 B[dest] = A[i] 9 return B
Does this work? Why or why not?
Randomize-In-Place (A) 1 n = A.length 2 for i = 1 to n 3 swap A[i] with A[Random(i, n)]
Permute-Without-Identity (A) 1 n = A,length 2 for i = 1 to n−1 3 swap A[i] with A[Random(i+1, n)]