Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent.

Similar presentations


Presentation on theme: "CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent."— Presentation transcript:

1 CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (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

2 CSC317 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

3 CSC317 3 Randomized hiring problem(n) Instead of worst case We want to know the cost on average of hiring new applicants X random variable equal to number times new person hired if candidate i is hired vice versa For notation, we can drop the I symbol: if candidate i is hired vice versa

4 CSC317 4 Randomized hiring problem(n) X random variable equal to number times new person hired Probability that candidate i is hired

5 CSC317 5 Randomized hiring problem(n) X random variable equal to number times new person hired Probability that candidate i is hired

6 CSC317 6 Randomized hiring problem(n) X random variable equal to number times new person hired Harmonic series ?how come?

7 CSC317 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.

8 CSC317 8 Quicksort Input: n numbers Output: sorted numbers, e.g., in increasing order O(n log(n)) on average Sorts in place, unlike mergesort Elegant

9 CSC317 9 Quicksort Quicksort(A, p, r) 1. If p<r 2. 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?

10 CSC317 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 larger Example: A = [8 6 1 3 7 5 2 4] Pivot After partition: A = [1 3 2 4 5 7 6 8] pivot Pivot in its right position http://www.cs.miami.edu/~burt/learning/Csc517.101/workbook/partition.html

11 CSC317 11 Partition (more algorithmically) Partition(A, p, r) p r 1. x=A[ r ] 2. i=p-1 3. for j = p to r-1 4. if A[ j ] <= x 5. i = i + 1; 6. swap A[ i ] with A[ j ] 7. swap A[ i+1 ] with A[ r ] 8. return i+1 O(?)

12 CSC317 12 Partition (more algorithmically) Partition(A, p, r) p r 1. x=A[ r ] 2. i=p-1 3. for j = p to r-1 4. if A[ j ] <= x 5. 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

13 CSC317 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] ≤ pivot 2. All elements in A[i+1..j-1] > pivot 3. A[r] = pivot

14 CSC317 14 Quicksort Quicksort(A, p, r) 1. If p<r 2. 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

15 CSC317 15 Quicksort Quicksort(A, p, r) 1. If p<r 2. 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?

16 CSC317 16 Quicksort Quicksort(A, p, r) 1. If p<r 2. 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!

17 CSC317 17 Quicksort Quicksort(A, p, r) 1. If p<r 2. 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?

18 CSC317 18 Why might a random choice be good enough? Even a 9 to 1, or 3 to 1 split, is O(n log n)

19 CSC317 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 pivot Average 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


Download ppt "CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent."

Similar presentations


Ads by Google