Analysis of Algorithms Randomized Algorithms The Hiring Problem

The Hiring Problem
UMass Lowell Computer Science 91.404
Analysis of Algorithms
Spring, 2002
Chapter 5 Lecture
Job candidates are numbered 1…
1…n
HIRE-ASSISTANT(n)
HIRE-ASSISTANT(n)
best ! 0
> candidate 0 is a least-qualified dummy candidate
for i ! 1 to n
do interview candidate i
if candidate i is better than candidate best
then best ! i
hire candidate i
Randomized Algorithms
Analysis Goal:
Goal:
- Analyze interview & hiring cost instead of running time
- Interviewing Cost = ci << Hiring Cost = ch
- Assume m people are hired
- Total Cost: O (nc + mc )
Sections 5.1 – 5.3
source: 91.404 textbook Cormen et al.
i
Analyzing the Hiring Problem

Worst-Case Analysis:
Indicator Random Variables

Indicator random variable I{A} associated with event A:
Hire every candidate interviewed
 How can this occur?
if A occurs
&1
#
I { A} = %
"
0
if
A
does
not
occur
$
!




If candidates come in increasing quality order
Hire n times: total hiring cost = O(nc
O(nch)
Probabilistic Analysis:
Appropriate if information about random distribution of inputs
is known
 Use a random variable to represent cost (or run-time)
 Find expected (average) cost (or run-time) over all inputs
 We’
We’ll use this technique to analyze hiring cost…
cost…


First need to introduce indicator random variables to simplify analysis
h

Example:
Example: Find expected number of heads when flipping a
fair coin




Sample space S = {H,
{H, T }
&1 if Y = H #
Random variable Y takes on values H, T X H = I {Y = H } = %
"
$0 if Y = T !
 Prob(
Prob( H ) = Prob(
Prob(T ) = 1/2
XH counts number of heads in a single flip
Expected number of heads in a single coin flip = expected value of XH
E[ X H ] = E[ I {Y = H }] 1 ! Pr{Y = H } + 0 ! Pr{Y = T }
&1#
&1# 1
= 1' $ ! + 0 ' $ ! =
% 2"
% 2" 2
1
Indicator Random Variables
(continued)
 Lemma 5.1:
5.1: Given sample space S and an event A
in S. Let XA = I{A}. Then E[XA] = Pr{A}.
 Proof:
Proof: By definitions of indicator random variable
and expectation:
E[ X ] = E[ I { A}] =
A
= 1 ! Pr{ A} + 0 ! Pr{ A}
= Pr{A}
 Useful for analyzing sequence of events…
events…


 Assume candidates arrive in random order
 X = random variable modeling number of times we hire
 Without indicator
n
random variables: E[ X ] = ! x Pr( X = x)
x =1
 With indicator
random variables:
X = X1 + X 2 + L + X n
difficult
calculation
in this case
if candidate i is hired #
&1
X i = I {candidate i is hired} = %
"
0
if
candidate i is not hired !
$
E[ X i ] = Pr{candidate i is hired}
Need to find this
by
Lemma 5.1
Analyzing the Hiring Problem
using Indicator Random Variables
using Indicator Random Variables
(continued)
(continued)
Candidate i is hired when candidate i is better than each of candidates 1…
1… i-1
Randomness assumption: any of first i candidates is equally likely to be bestqualified so far
Candidate i has probability of 1/i
1/i of being better than each of candidates 1…
1… i-1

using Indicator Random Variables
Analyzing the Hiring Problem
HIRE-ASSISTANT(n)
HIRE-ASSISTANT(n)
best ! 0
> candidate 0 is a least-qualified dummy candidate
for i ! 1 to n
do interview candidate i
if candidate i is better than candidate best
then best ! i
Need to calculate probability
hire candidate i
that these lines are executed

Analyzing the Hiring Problem
HIRE-ASSISTANT(n)
HIRE-ASSISTANT(n)
best ! 0
>candidate 0 is a least-qualified dummy candidate
for i !1 to n
do interview candidate i
Assuming that
if candidate i is better than candidate best
candidates are presented
then best !i
in random order, total
hire candidate i
hiring cost is in O(c h ln n)
n
i =1
Probability candidate i will be hired is therefore: 1/i
1/i
E[ X i ] = Pr{candidate i is hired} = 1 / i
n
n
i =1
i =1
E[ X ] = E[! X i ] = ! E[ X i ] = !1 / i = ln n + O(1)
X = X1 + X 2 + L + X n
by
Linearity of
Expectation
E[ X i ] = Pr{candidate i is hired} = 1 / i
2
And now for something
different…
H
 Randomized Algorithm:
Randomized Hiring Algorithm
 Put randomness into algorithm itself
 Make choices randomly (e.g. using coin flips)
 Use
pseudorandom-number generator
 Algorithm itself behaves randomly
 Different from using probabilistic assumptions
about inputs to analyze average-case behavior
of a deterministic (non-random) algorithm
 Randomized algorithms are often easy to
design & implement & can be very useful in
practice
Nothing is ever really free…
RANDOMIZED-HIRE-ASSISTANT(n)
RANDOMIZED-HIRE-ASSISTANT(n)
randomly permute the list of candidates
best ! 0
> candidate 0 is a least-qualified dummy candidate
How do we for i ! 1 to n
do this well?
do interview candidate i
How much
if candidate i is better than candidate best
time does it
take?
then best ! i
hire candidate i
Goal:
Goal: permute array A
A = rank (1), rank (2),K , rank (n)
Approach:
Approach: assign each element A[ i ] a random priority P[ i ]
RANDOMIZED-HIRE-ASSISTANT(n)
RANDOMIZED-HIRE-ASSISTANT(n)
randomly permute the list of candidates
best ! 0
> candidate 0 is a least-qualified dummy candidate
for i ! 1 to n
do interview candidate i
if candidate i is better than candidate best
deterministic
then best ! i
hire candidate i
random
Represent an input using candidate ranks:
rank (1), rank (2),K , rank (n)
Randomly permuting candidate list creates a random list of candidate ranks.
The expected cost of RANDOMIZED-HIRE-ASSISTANT is in O(c h ln n).
Note: We no longer need to assume that candidates are presented in random order!
Randomized Permutation
Algorithm
Approach:
Approach: assign each element A[ i ] a random priority P[ i ]
PERMUTE-BY-SORTING(A)
PERMUTE-BY-SORTING(A)
n ! length[A]
length[A]
for i ! 1 to n
do P[ i ] ! RANDOM(1,n
RANDOM(1,n3)
sort A, using P as sort keys
return A
This step dominates the
running time. It takes Ω
(n lg n) time if pairs of
values are compared while
sorting
Choose a random
number between 1
and n3
(makes it more likely
that all priorities are
unique)
Now we need to show that PERMUTE-BY-SORTING
produces a uniform random permutation of the input,
assuming all priorities are distinct…
distinct…
3
Randomized Permutation
Algorithm Analysis
Claim:
Claim: PERMUTE-BY-SORTING produces a uniform random
permutation of the input, assuming all priorities are distinct.
Proof Sketch:
Sketch: Consider perm. in which each A[ i ] has i th smallest priority
To show this permutation occurs with probability 1/n! …
Let X i be event that element A[ i ] receives i th smallest priority.
Probability that, for all i, event X i occurs is :
= Pr{ X 1 ! X 2 ! X 3 ! L ! X n "1 ! X n }
= Pr{ X 1} " Pr{ X 2 | X 1} " Pr{ X 3 | X 2 ! X 1}L Pr{ X n | X n #1 ! K ! X 1}
1
1 # & 1 #& 1 # & 1 #
Pr{ X 1 ! X 2 ! X 3 ! L ! X n "1 ! X n } = &$ #!&$
! L $ !$ ! = $ !
% n "% n ' 1 "
To complete the proof, now apply the argument
above to any fixed permutation of {1,2,…
{1,2,… ,n}:
Randomized Permutation
Algorithm Improvement
RANDOMIZE-IN-PLACE(A)
RANDOMIZE-IN-PLACE(A)
n ! length[A]
length[A]
for i ! 1 to n
do swap A[ i ]
!
A[RANDOM(i
A[RANDOM(i,n)]
Claim:
Claim: RANDOMIZE-IN-PLACE produces a uniform random
permutation of the input.
Proof uses a loop invariant for the for loop. See p. 103 of text for detais.
detais.
% 2 "% 1 " % n! "
! = ! (1), ! (2),K , ! (n)
Randomized Permutation
Algorithm Improvement
Invariant:
Just prior to the ith iteration of the for loop, for each possible
(i - 1)-permutation, the subarray A[1…i-1] contains this (i - 1)-permutation
with probability
(n - i + 1)!/n!
Initialization:
Initialization: i = 1.
1.
For each possible 0 -permutation, A[1..0] contains this 0 -permutation
with probability (n - i + 1)!/n! = n!/n! = 1. Follows from the fact that
A[0..1] is empty and the 0 -permutation has no elements.
Termination:
Termination: i = n + 1.
1.
The subarray A[1..n
[1..n ] contains a given n -permutation with probability
(n - n)!/n! = 0!/n! = 1/n!.
Randomized Permutation
Algorithm Improvement
Maintenance (the hard part):
Induction Assumption: Just prior to the ith iteration of the for loop, each
possible (i - 1)-permutation appears in the subarray A[1…i-1] with
probability (n - i + 1)!/n!
Prove: after the ith iteration of the for loop, each possible i-permutation
appears in A[1..i] with probability (n - i)!/n!.
Proof: Start from the induction assumption. After the ith iteration, A[1..i] will
contain a permutation h x1,…, xi-1 i followed by the value xi placed by the
algorithm in A[i]. Let E1 denote the event "the first i-1 iterations have
created a particular (i-1)-permutation h x1,…, xi-1 i in A[1…i-1] ". By the
induction assumption (loop invariant), Pr{E 1} = (n - i + 1)!/n!. Let E2 denote
the event "the ith iteration puts xi in position A[i]". But hx1 ,…, xii occurs in
A[1..i] exactly when both E1 and E2 occur: we need to compute
Pr{E1 Å E2}.
4
Randomized Permutation
Algorithm Improvement
Maintenance (the hard part) 2:
Proof: Pr{E1 Å E2} = ???.
Pr{E1 Å E2 }. = Pr{E2 | E1} Pr{E1 }, by definition of conditional probability.
Pr{E2 | E1 } = 1/(n - i + 1) since is chosen randomly form the (n - i + 1)
positions in A[i..n].
We have:
Pr{E1 Å E2 }. = Pr{E2 | E1} Pr{E1 } = 1/(n - i + 1) * (n - i + 1)!/n! = (n - i)!/n!
5