CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using...

22
CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! • Using employment agency to send one candidate at a time • Each day, we interview one candidate • We must decide immediately if to hire candidate and if so, fire previous • Always want the best hire… • Cost to interview (low) • Cost to fire/hire… (expensive) In other words …

description

CSC317 3 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 Always want the best hire… fire if better candidate comes along Cost to interview (low) C i Cost to fire/hire… (expensive) C h C h > C i n total number of candidates m total number of hires O (?) different cost of hiring Not run time but cost of hiring, but flavor of max problem

Transcript of CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using...

Page 1: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

1CSC317

Randomized algorithms

Hiring problem

We always want the best hire for a job!• Using employment agency to send one candidate at a time• Each day, we interview one candidate• We must decide immediately if to hire candidate and if so, fire previous

• Always want the best hire…• Cost to interview (low)• Cost to fire/hire… (expensive)

In other words …

Page 2: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

2CSC317

Hire-Assistant(n)1. best = 0 //least qualified candidate2. for i = 1 to n3. interview candidate i4. if candidate i better than best5. best = i6. hire candidate i

• Always want the best hire… fire if better candidate comes along

• Cost to interview (low) Ci

• Cost to fire/hire… (expensive) Ch

Ch > Ci

n total number of candidatesm total number of hires O (?)

Page 3: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

3CSC317

Hire-Assistant(n)1. best = 0 //least qualified candidate2. for i = 1 to n3. interview candidate i4. if candidate i better than best5. best = i6. hire candidate i

• Always want the best hire… fire if better candidate comes along

• Cost to interview (low) Ci

• Cost to fire/hire… (expensive) Ch

Ch > Ci

n total number of candidatesm total number of hires O (?) different cost of hiring

Not run time but cost of hiring, but flavor of max problem

Page 4: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

4CSC317

• Always want the best hire… fire if better candidate comes along

• Cost to interview (low) Ci

• Cost to fire/hire… (expensive) Ch

Ch > Ci

n total number of candidatesm total number of hires

Different type of cost – not run time, but cost of hiring.But when is it most expensive?

When candidates interview in reverse order, worst first…

O (cin + chm)

Page 5: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

5CSC317

Hiring problem

1. best = 0 //least qualified candidate2. for i = worst_candidate to best_candidate…

O (cin + chn) = O(chn)

When is this most expensive?When candidates interview in reverse order, worst first…

Page 6: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

6CSC317

• Always want the best hire… fire if better candidate comes along

• Cost to interview (low) Ci

• Cost to fire/hire… (expensive) Ch

Ch > Ci

n total number of candidatesm total number of hires

When is it least expensive?

Page 7: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

7CSC317

Hiring problem

1. best = 0 //least qualified candidate2. for i = best_candidate to worst_candidate…

O (cin + ch)

When is this leat expensive?When candidates interview in order, best first…But we don’t know who is best a priori

Page 8: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

8CSC317

Hiring problem

• Cost to interview (low Ci)• Cost to fire/hire … (expensive Ch)• n number of candidates• m hired

O (cin + chm)

Independent of order of candidates

depends on order of candidates

What about if we randomly invite candidates?

Page 9: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

9CSC317

Hiring problem and randomized algorithms

O (cin + chm) depends on order of candidates

• Can’t assume in advance that candidates are in random order – bias might exist.

• We can add randomization to our algorithm – by asking the agency to send names in advance and randomize

Page 10: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

10CSC317

Hiring problem - randomized

We always want the best hire for a job!• Employment agency sends list of n candidatesin advance• Each day, we choose randomly a candidatefrom the list to interview• We do not rely on the agency to send usrandomly, but rather take control in algorithm

Page 11: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

11CSC317

Randomized hiring problem(n)

1. randomly permute the list of candidates2. Hire-Assistant(n)

What do we mean by randomly permute (we need a random number generator)?

Random(a,b)

Function that returns an integer between a and b,inclusive, where each integer has equal probability

Most programs: pseudorandom-number generator

Page 12: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

12CSC317

Including random number generator

Random(a,b)

Function that returns an integer between a and b,inclusive, where each integer has equal probability

So, Random(0,1)?

0 with P = 0.51 with P = 0.5

Page 13: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

13CSC317

Including random number generator

Random(a,b)

Function that returns an integer between a and b,inclusive, where each integer has equal probability

So, Random(3,7)?

3 with P = 0.24 with P = 0.25 with P = 0.26 with P = 0.27 with P = 0.2

Like rolling a (7-3+1) dice

Page 14: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

14CSC317

How do we random permutations?

Randomize-in-place(A,n)1. For i = 1 to n2. swap A[i] with A[Random(i,n)]

A = 1 2 3 4 5 6

A = 3 2 1 4 5 6

A = 3 6 1 4 5 2

A = 3 6 1 4 5 2

i Random choice from A[i,…,n]

Page 15: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

15CSC317

Probability review• Sample space S: space of all possible outcomes (e.g. that can happen in a

coin toss)

• Probability of each outcome: p(i) ≥ 0

• Example: coin toss S = {H, T}

Page 16: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

16CSC317

Probability review• Event: a subset of all possible outcomes; subset of sample space S

• Probability of event:

• Example: coin toss that give heads E = {H}• Example: rolling two dice

Page 17: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

17CSC317

Probability review

Example: rolling two dice

• List all possible outcomes

S={(1,1},(1,2),(1,3), …(2,1),(2,2),(2,3), …(6,6)}

• List the event or subset of outcomes for which the sum of the dice is 7

E = {(1,6),(6,1),(2,5),(5,2),(3,4),(4,3)}

• Probability of event that sum of dice 7: 6/36 = 1/6

Page 18: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

18CSC317

Probability review

• Random variable: attaches value to an outcome/s

Example: value of one diceExample: sum of two dice

P(X = x): Probability that random variable X takes on value x

random variable

value of random variable

P(X = x): Probability that sum of two dice has value x, e.g. P(X = 7) = 1/6

Page 19: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

19CSC317

Probability review

• Expectation or Expected Value of Random Variables: Average

sum over each possible values, weighted by probability of that value

2 3 4 5 6 7 8 9 10 11 12135

Page 20: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

20CSC317

Easier way?

• Linearity of Expectation

random variables, then:

Does not require independence of random variables.

Example: X1 and X2 represent the values of a dice

Page 21: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

21CSC317

Probability review

• Indicator Random Variable (indicating if occurs…)

if A occursvice versa

Lemma: For event A

Example: Expected number Heads when flipping fair coin

Page 22: CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,

22CSC317

Probability review

• Indicator Random Variable & Linearity of Expectation

if A occursvice versa

Example: Expected number Heads when flipping 3 fair coins