Cost to interview low C i Cost to firehire expensive C h n number of candidates m hired O c i n c h m Independent of order of candidates depends on order of candidates ID: 760264
Download Presentation The PPT/PDF document "CSC317 1 Hiring problem-review" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CSC317
1
Hiring
problem-review
Cost to interview (low Ci)Cost to fire/hire … (expensive Ch)n number of candidatesm hired
O (cin + chm)
Independent of order of candidates
depends on order of candidates
Slide2CSC317
2
Randomized hiring problem(n)
1. randomly permute the list of candidates
2. Hire-Assistant(n)
Hire
-Assistant
(n)
1. best = 0 //least qualified candidate
2.
for
i = 1
to
n
3.
interview
candidate
i
4.
if
candidate
i
better
than
best
5
.
best = i
6.
hire
candidate
i
Slide3CSC317
3
Randomized hiring problem(n)
Instead of worst case
We want to know the cost on average of hiring new applicantsX random variable equal to number times new person hired
i
f candidate
i
is hired
vice versa
For notation, we can drop the I symbol:
i
f candidate
i
is hired
v
ice versa
Slide4CSC317
4
Randomized hiring problem(n)
X
random variable equal to number times new person hired
Probability that candidate
i
is hired
Slide5CSC317
5
Randomized hiring problem(n)
X
random variable equal to number times new person hired
Probability that candidate
i
is hired
Slide6CSC317
6
Randomized hiring problem(n)
X
random variable equal to number times new person hired
Harmonic series
?how come?
Slide7CSC317
7
Randomized hiring problem(n)
Cost of hiring a candidate
C
h
On average:
C
h
ln
(n)
Compare to worst case
C
h
n
.
Slide8CSC317
8
Quicksort
Input:
n numbersOutput: sorted numbers, e.g., in increasing order
O(n log(n))
on average
Sorts in place, unlike
mergesort
Elegant
Slide9CSC317
9
Quicksort
Quicksort(A, p, r)
1.
If p<r2. q = Partition(A,p,r)3. Quicksort(A,p,q-1)4. Quicksort(A,q+1,r)
p
r
Divide-and-conquer; but what is special about it?
Slide10CSC317
10
Partition
1. Pick pivot element in array
A (for now, we’ll choose last element)2. Rearrange A so that elements before pivot are smaller, and elements after pivot are largerExample:A = [8 6 1 3 7 5 2 4] PivotAfter partition:A = [1 3 2 4 5 7 6 8] < pivot > pivotPivot in its right positionhttp://www.cs.miami.edu/~burt/learning/Csc517.101/workbook/partition.html
Slide11CSC317
11
Partition (more algorithmically)
Partition(A, p, r)
p
r
1.
x
=A[ r ]2. i=p-13. for j = p to r-14. if A[ j ] <= x5. i = i + 1;6. swap A[ i ] with A[ j ]7. swap A[ i+1 ] with A[ r ]8. return i+1
O(?)
Slide12CSC317
12
Partition (more algorithmically)
Partition(A, p, r)
p
r
1.
x
=A[ r ]2. i=p-13. for j = p to r-14. if A[ j ] <= x5. i = i + 1;6. swap A[ i ] with A[ j ]7. swap A[ i+1 ] with A[ r ]8. return i+1
O(n)Why? For each j at least one swap.
pivot
Slide13CSC317
13
Partition
Loop
invariants?
p
r
i
j
i
keeps track of pivot location
j keeps track of next element to check
Before the for loop at given j
1. All elements in A[p..i] ≤ pivot2. All elements in A[i+1..j-1] > pivot3. A[r] = pivot
Slide14CSC317
14
Quicksort
Quicksort(A, p, r)
1.
If p<r2. q = Partition(A,p,r)3. Quicksort(A,p,q-1)4. Quicksort(A,q+1,r)
p
r
When will partition do a bad job?
When the partition is for zero elements versus
n-1
p
r
Slide15CSC317
15
Quicksort
Quicksort(A, p, r)
1.
If p<r2. q = Partition(A,p,r)3. Quicksort(A,p,q-1)4. Quicksort(A,q+1,r)
p
r
When will partition do a good job?
When
the partition is roughly equal in terms of numbers smaller than and greater than pivot
Mergesort
anyone?
Slide16CSC317
16
Quicksort
Quicksort(A, p, r)
1.
If p<r2. q = Partition(A,p,r)3. Quicksort(A,p,q-1)4. Quicksort(A,q+1,r)
p
r
How do we choose a pivot point that we are good on average?
We choose pivot randomly!
Slide17CSC317
17
Quicksort
Quicksort(A, p, r)
1.
If p<r2. q = Partition(A,p,r)3. Quicksort(A,p,q-1)4. Quicksort(A,q+1,r)
p
r
What about a
9 to 1 split?
Slide18CSC317
18
Why might a random choice be good enough?
Even a 9 to 1, or 3 to 1 split, is
O(n log n)
Slide19CSC317
19
Quicksort on average run time
We’ll prove that
average run time with random pivots for any input array is O(n log n)Randomness is in choosing pivotAverage as good as best case!
Most work of Quicksort in
comparisons
Each
call to partition is constant
plus number
of
comparisons in for
loop
Let
X = total number of comparisons in
all
calls
to Partition