Average-case Analysis: The world behaves randomly, randomly generated input solved by traditional algorithm and analysed.
Randomized Algorithms: The algorithm makes random decisions as the algorithm processes the worst-case input.
Algorithm
Answer it yields
Efficient deterministic algorithms
Always the correct answer
a special case
Efficient randomized algorithms
With high probability be the correct answer
Randomized algorithms
always correct and run efficiently in expectation
Exp: The Hiring Problem
Problem description
We want to hire an office assistant from headhunter, and we interview a different candidate per day for N days.
Interviewing Cost: Ci
Hiring Cost: Ch
Assume Ci<<Ch.
Naïve Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14
intHiring( EventType C[ ], int N ){ /* candidate 0 is a least-qualified dummy candidate */ int Best = 0; int BestQ = the quality of candidate 0; for ( i=1; i<=N; i++ ) { Qi = interview( i ); /* Ci */ if ( Qi > BestQ ) { BestQ = Qi; Best = i; hire( i ); /* Ch */ } } return Best; }
Worst case
The candidates come in increasing quality order, all will be hired.
Candidate i will be hired if i is the best one of Candidate 1,2,...,i. As any of first i candidates is equally likely to be best-qualified so far,
P[Xi=1]=i1,E[Xi]=i1
Then X, the number of hires,
X=i=1∑NXi
E[X]=E[i=1∑NXi]=i=1∑NE[Xi]=i=1∑Ni1=lnN+O(1)
The total cost will be
O(ChE[X]+NCi)=O(ChlnN+NCi)
Randomized Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
intRandomizedHiring( EventType C[ ], int N ){ /* candidate 0 is a least-qualified dummy candidate */ int Best = 0; int BestQ = the quality of candidate 0; /* The only modification: adding a random shuffle */ randomly permute the list of candidates;
for ( i=1; i<=N; i++ ) { Qi = interview( i ); /* Ci */ if ( Qi > BestQ ) { BestQ = Qi; Best = i; hire( i ); /* Ch */ } }
The random shuffle guarantees the presentation order of the candidates is random.
But the shuffle takes time!
Randomized Permutation
Assign each element A[ i ] a random priorityP[ i ]
Sort by P[i]
1 2 3 4 5 6 7
voidPermuteBySorting( ElemType A[ ], int N ) { for ( i=1; i<=N; i++ ) A[i].P = 1 + rand()%(N3); /* makes it more likely that all priorities are unique */ Sort A, using P as the sort keys; }
Online Hiring Algorithm
interview the first k candidates and mark the best value among then BESTK .
interview the remaining candidates and hire the first one that better than BESTK.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intOnlineHiring( EventType C[ ], int N, int k){ int Best = N; int BestQ = - INT_MAX ; for ( i=1; i<=k; i++ ) { Qi = interview( i ); if ( Qi > BestQ ) BestQ = Qi; } for ( i=k+1; i<=N; i++ ) { Qi = interview( i ); if ( Qi > BestQ ) { Best = i; break; } } return Best;
Given k
Let Si be subproblem such that the ith candidate is the best. Then the algorithm works if
A: the best one is at position i
P[A]=N1
B: no one at positions k+1 ~ i–1 are hired
P[B]=i−1k
Since the best of Candidate 1,2,...,i is in the fist k candidates.