Docsity

Probabilistic Analysis in Algorithmics: Hiring Problem and Randomization, Assignments of Algorithms and Programming

Partial preview of the text

Download Probabilistic Analysis in Algorithmics: Hiring Problem and Randomization and more Assignments Algorithms and Programming in PDF only on Docsity!

COMP 482 / ELEC 420 Probabilistic Algorithm Analysis

Math Background: Review & Beyond

1.^

Asymptotic concepts & useful math

2.^

Recurrences

3.^

Probabilistic analysis

Motivation

Not all algorithmic analysis is based on deterministic

recurrences.

Some require probabilistic analysis.

-^ Review some probability•^ Extended example: - Review probability– Review/learn basic technique

Conditional Probability

{^

}^

{^

}

{^

} 2

2

1

2

1

Event Pr

Event

Event Pr

Event|

Event Pr

∩

=

Random Variables

Random variables X,Y “independent”

Pr{X=x

∧^ Y=y} = Pr{X=x}

⋅^ Pr{Y=y}

Expected values of random variables:

– E[X] =

Σx^ (x^ ⋅^

Pr{X=x})

– E[X+Y] = E[X]+E[Y]

linearity of expectation

  • E[a

⋅X] = a

⋅E[X]
– E[X
⋅Y] = E[X]
⋅E[Y]

if X,Y independent

Hiring Problem (aka Secretary Problem)

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?

?^

Hiring Problem: Average-Case

What is m likely to be?

Wrong answer: m can be 1…n, so use the mean m=n/2.Instead, average cost over all possible interviewee orders.

Number each interviewee 1…n.Consider each permutation of the set {1,…,n}.

To compute this average, use probabilistic techniques to avoidlisting every possible permutation.

Averaging via Expected Values

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

Expected algorithm cost

= O(c

n + ci

random variableindicator randomvariable log n)h^

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)

Randomization

Simple algorithm, with an interesting proof that it computes a

uniform random permutation.

Randomizing the order of an array A: Let n = # elements in AFor i=1 to n, swap A[i] with A[random(i…n)]

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!).

Randomization

Base case, i=1:

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!).

Randomization

Inductive case, show holds for i+1:

For an arbitrary i-permutation, the permutation is obtained

  • Event

: its first i-1 elements, an (i-1)-permutation, were obtained in the 1 first i-1 iterations,

and

  • Event

: 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

∩^1

Event

} = Pr{Event 2

}^ ⋅^1

Pr{Event

| Event 2

= (n-(i+1)+1)!/n!

Algebra

= (n-i)!/n!

Algebra, cancelling term

= (n-i+1)!/n!

⋅^ 1/(n-i+1)

Randomization

At termination (i=n+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!).

On-Line Hiring Problem^ One possible algorithm:

Interview k candidates, and hire the first one after that with a

better score than previously found.

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.

On-Line Hiring Problem

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

Need to determine Pr{S

} for ii

∈k+1…n.

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 ?