














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!















-^ Review some probability•^ Extended example: - Review probability– Review/learn basic technique
{^
}^
{^
}
{^
} 2
2
1
2
1
Event Pr
Event
Event Pr
Event|
Event Pr
∩
=
Σx^ (x^ ⋅^
Pr{X=x})
linearity of expectation
⋅X] = a
if X,Y independent
Goal:
Hire the most qualified person for a job.
Algorithm we’ll use:
1.^
Decide how many people (n) to interview.
2.^
Hire the first candidate.
3.^
For each interviewee, in turn^ a.^
Interview.b. Hire, if better than current employee.
Costs
(n people interviewed
⋅^ ci^
each) + (m people hired
⋅^ ch
each)
= O(c
⋅^ n + ci
⋅^ m)h
By definition, n
≥^ m. Assume c
`^ i c.h
Easy so far, butwhat is m?
Number each interviewee 1…n.Consider each permutation of the set {1,…,n}.
E[Hired?
] = 1i
⋅Pr{#i hired} + 0
⋅Pr{#i not hired}
= Pr{#i hired}= 1/i
Each has equal chance to bethe best of first i.
E[TotalHired] = E[
Σi=1..n
Hired?
]i
i=1..n
E[Hired?
]i
Define: TotalHired =
Σi=1…n
Hired?
i
Hired?
= Pr{interviewee #i hired}i = 1 if #i hired, 0 if #i not hired = Σi=1..n
1/i = ln n + O(1)
The key – use a loop invariant:
Before i
th^ iteration, for any (i-1)-permutation of elements from 1…n, A[1…i-1] contains the permutation with probability ((n-i+1)!)/(n!).
A[1..0] contains the 0-permutation (no data) with probability (n!)/(n!)=1.Holds trivially. Let n = # elements in AFor i=1 to n, swap A[i] with A[random(i…n)]
Proving loop invariant: Before i
th^ iteration, for any (i-1)- permutation of elements from 1…n,A[1…i-1] contains the permutationwith probability ((n-i+1)!)/(n!).
For an arbitrary i-permutation, the permutation is obtained
: its first i-1 elements, an (i-1)-permutation, were obtained in the 1 first i-1 iterations,
and
: the i 2
th^ iteration chooses that i-permutation’s i
th^ element.
Let n = # elements in AFor i=1 to n, swap A[i] with A[random(i…n)]
Proving loop invariant: Before i
th^ iteration, for any (i-1)- permutation of elements from 1…n,A[1…i-1] contains the permutationwith probability ((n-i+1)!)/(n!).
Pr{Event
Event
} = Pr{Event 2
Pr{Event
| Event 2
= (n-(i+1)+1)!/n!
Algebra
= (n-i)!/n!
Algebra, cancelling term
= (n-i+1)!/n!
⋅^ 1/(n-i+1)
Invariant
any n-permutation of elements 1…n occurs in A[1..n] with probability 1/(n!).I.e., uniform random distribution. Let n = # elements in AFor i=1 to n, swap A[i] with A[random(i…n)]
Proving loop invariant: Before i
th^ iteration, for any (i-1)- permutation of elements from 1…n,A[1…i-1] contains the permutationwith probability ((n-i+1)!)/(n!).
BestScore := -
∞
For i=1 to k,
BestScore = max(BestScore, Score(i)) For i=k+1 to n,
If Score(i) > BestScore,Then Hire #i & Quit Hire #n
Strategy to find best k:1.^
Fix k, & compute the probabilityof getting best candidate.
2.^
Find k to maximize probability.
Define: S = succeed in finding best candidate
S= succeed in finding best candidate & best is #ii^
Since we never hire from the first k,
Pr{S
} = … = Pr{S 1
} = 0k
Pr{S} =
Σi=k+1…n
Pr{S
}i
Since S
’s disjoint,i Pr{S} =
Σi=1...n
Pr{S
}i
S= candidate #i is best & candidates k+1…i-1 not choseni^ = candidate #i is best & best among 1…i-1 is among 1…k (not k+1…i-1)
= 1/n
⋅^ k/(i-1) = k/(n
⋅(i-1))
Independent
Pr{S
} =^ i ?