Lecture 2: Randomized algo for Approximate median and
Download
Report
Transcript Lecture 2: Randomized algo for Approximate median and
Randomized Algorithms
CS648
Lecture 2
β’ Randomized Algorithm for Approximate Median
β’ Elementary Probability theory
1
RANDOMIZED MONTE CARLO ALGORITHM
FOR
APPROXIMATE MEDIAN
This lecture was delivered at slow pace and its flavor was that of a
tutorial.
Reason: To show that designing and analyzing a randomized
algorithm demands right insight and just elementary probability.
2
A simple probability exercise
There is a coin which gives HEADS with probability ¼ and TAILS with
probability ¾. The coin is tossed π times. What is the probability that we get
at least π/2 HEADS ?
[Stirlingβs approximation for Factorial: π! β
2ππ
(π π)π ]
3
Probability of getting
βat least π/2 HEADS in π tossesβ
Probability of getting at least π/2 heads:
=
π
π=π/2 ππ«[π
=
π
π=π/2
β€
=
β€
β€
=
β€
π
π
HEADS appear in π tosses]
(1 4)π (3 4)πβπ
π
π
(1 4)π (3 4)πβπ
π=π/2
π/2
π
(3 4)π ππ=π/2 (1 3)π
π/2
Using Stirlingβs approximation
π
π
1 4π/2
(3 4)π (1 3)π/2 3 2
β€
2
π/2
π/2
4π/2 (3 4)π (1 3)π/2
Since (3 4)5/2 β€ 1 2 , so β¦
π/2
3
( 4)
Inverse exponential in π.
(1 2)π/5
4
Approximate median
Definition: Given an array A[] storing n numbers and Ο΅ > 0, compute an
element whose rank is in the range [(1- Ο΅)n/2, (1+ Ο΅)n/2].
Best Deterministic Algorithm:
β’ βMedian of Mediansβ algorithm for finding exact median
β’ Running time: O(n)
β’ No faster algorithm possible for approximate median
Can you give a short proof ?
5
½ - Approximate median
A Randomized Algorithm
Rand-Approx-Median(A)
1. Let k ο c log n;
2. S ο β
;
3. For i=1 to k
4.
x ο an element selected randomly uniformly from A;
5.
S ο S U {x};
6. Sort S.
7. Report the median of S.
Running time: O(log n loglog n)
6
Analyzing the error probability of
Rand-approx-median
n/4
Left Quarter
Elements of A arranged in
Increasing order of values
3n/4
Right Quarter
When does the algorithm err ?
To answer this question, try to characterize what
will be a bad sample S ?
7
Analyzing the error probability of
Rand-approx-median
n/4
Elements of A arranged in
Increasing order of values
Left Quarter
3n/4
Median of S
Right Quarter
Observation: Algorithm makes an error only if k/2 or more elements
sampled from the Right Quarter (or Left Quarter).
8
Analyzing the error probability of
Rand-approx-median
n/4
Elements of A arranged in
Increasing order of values
3n/4
Right Quarter
Left Quarter
Pr[ An element selected randomly from A is from Right quarter] = ??
¼
Pr[ Out of k elements sampled from A, at least k/2 are from Right quarter] = ??
β€ (1 2)π/5
log π for π = 10 log π
= (1 2)2Exactly
the same as the coin
= πβ2 tossing exercise we did !
9
Main result we discussed
Theorem: The Rand-approx-median algorithm fails to report ½ approximate median from array A[1.. π] with probability at
most 2 πβ2 .
Homework: Design a randomized Monte Carlo algorithm for
computing Ο΅-approximate median of array A[1.. π] with running
time O(log n loglog n) and error probability πβπ for any given
constants Ο΅ and π.
[Do this homework sincerely without any friendβs help.]
10
ELEMENTARY PROBABILITY THEORY
(IT IS SO SIMPLE THAT YOU UNDERESTIMATE ITS ELEGANCE AND POWER)
11
Elementary probability theory
(Relevant for CS648)
β’ We shall mainly deal with discrete probability theory in this course.
β’ We shall take the set theoretic approach to explain probability theory.
Consider any random experiment :
o Tossing a coin 5 times.
o Throwing a dice 2 times.
o Selecting a number randomly uniformly from [1..n].
How to capture the following facts in the theory of probability ?
1. Outcome will always be from a specified set.
2. Likelihood of each possible outcome is non-negative.
3. We may be interested in a collection of outcomes.
12
Probability Space
Definition: Probability space associated with a random experiment is an
ordered pair (Ξ©,P), where
β’ Ξ© is the set of all possible outcomes of the random experiment
β’ P : Ξ© ο R such that
β P(Ο) β₯ 0 for each ΟΟ΅ Ξ©
β ΟΟ΅ Ξ© P(Ο) = 1
Ξ©
Elements of Ξ© are called elementary events or sample points.
13
Event in a Probability Space
Definition: An event A in a probability space (Ξ©,P) is a subset of Ξ©. The
probability of event A is defined as
P(Ο)
ΟΟ΅ A
A
Ξ©
For sake of compact notation, we extend P for events as described above.
14
Exercises
A randomized algorithm can also be viewed as a random experiment.
1. What is the sample space associated with Randomized Quick sort ?
2. What is the sample space associated with Rand-approx-median
algorithm ?
15
An Important Advice
In the following slides, we shall state well known equations
(highlighted in yellow boxes) from probability theory.
β’ You should internalize them fully.
β’ We shall use them crucially in this course.
β’ Make sincere attempts to solve exercises that follow.
16
Union of two Events
Given two events A and B defined over a probability space (π,P), what is
P(AUB) ?
A
B
Ξ©
P(AUB) = P(A) + P(B)
β P(Aβ©B)
Try to prove it by showing the following:
Each Ο Ο΅ AUB contributes exactly P(Ο) in the right hand side.
17
Union of three Events
Given three events Aβ, Aβ, Aβ, defined over a probability space (π,P), what is
P(Aβ U Aβ U Aβ) ?
A
B
C
Ξ©
P(Aβ U AβUAβ) = P(Aβ) + P(Aβ) + P( Aβ)
β P(Aββ©Aβ) β P(Aββ©Aβ) β P(Aββ©Aβ)
+ P(Aββ©Aββ©Aβ)
Try to prove this equation as well by showing the following:
Each Ο Ο΅ Aβ U AβUAβ contributes exactly P(Ο) in the right hand side.
18
Exercises
β’ For events π1 ,β¦, ππ defined over a probability space (π,P), prove that
P( ππ=1 ππ ) =
π P(π π )
β π<π P(ππ
ππ )
+
π<π<π P(π π
ββ¦
(β1)π+1 P(π1
ππ
π2 β¦
ππ )
ππ )
β’ There are π letters π envelopes. For each letter, there is a unique envelope
in which it should be placed. A careless postman places the letters
randomly into envelopes (one letter in each envelope). What is the
probability that no letter is placed correctly (into the envelope meant for
it) ?
19
Conditional Probability
Happening of some event influences the likelihood of happening of other events. This
notion is formally captured by conditional probability as follows.
Probability of event A conditioned on event B, compactly represented as P[A|B],
means the following.
Given that event B has happened, what is the probability that event A has also
happened ?
You might have seen and used the following equation for conditional probability.
P[A|B] =
π[πβ©π]
π[π]
Can you give suitable reason to justify the validity of the above equation ?
In particular, give justification for π[π β© π] in numerator and π[π] in denominator in
this equation.
20
Exercises
β’ A man possesses five coins, two of which are double-headed, one is
double-tailed, and two are normal. He shuts his eyes, picks a coin at
random, and tosses it. What is the probability that the lower face of the
coin is a head ? He opens his eyes and sees that the coin is showing heads;
what it the probability that the lower face is a head ? He shuts his eyes
again, and tosses the coin again. What is the probability that the lower
face is a head ? He opens his eyes and sees that the coin is showing heads;
what is the probability that the lower face is a head ? He discards this coin,
picks another at random, and tosses it. What is the probability that it
shows heads ?
21
Partition of sample space and
an βimportant Equationβ
A set of events π1 ,β¦, ππ defined over a probability space (π,P) is said to
induce a partition of π if
π
β’
π=1 π π = π
β’ ππ
ππ =β
for allπ β π
B
Ξ©
Given an event B, how can we express P(B) in terms of a given partition ?
P(B) =
π P(π π β©B
)
22
Exercises
β’ There are π sticks each of different heights. There are π vacant slots
arranged along a line and numbered from 1 to π as we move from left to
right. The sticks are placed into the slots according to a uniformly random
permutation. A stick placed at πth slot is said to be a dominating stick if its
height is largest among all sticks placed in slots 1 to π β 1. Find the
probability that πth slot contains a dominating stick.
23
Independent Events
Two events A and B defined over a probability space (π,P) are said to be
independent if happening of one of them has no influence on the probability
of the another event. Mathematically, it means that
P(A|B)= P(A) and P(B|A)=P(B)
The following equation also compactly captures independence of two events.
P(A β© B) = P(A) · P(B)
Question: Can two independent events ever be disjoint ?
24
Exercises
1.
Two fair dice are rolled. Show that the event that their sum is 7 is
independent of the score shown by the first die.
2.
Let (π,P) be a probability space where π = {1,2,β¦,p} for a given prime
number p, and each elementary event has probability 1/p. Show that if
two events A and B defined over (π,P) are independent, then at least
one of A and B is either β
or π.
25