Professional Documents
Culture Documents
Lecture 3 CS648
Lecture 3 CS648
Lecture 3 CS648
CS648
Lecture 3
• Two fundamental problems
• Balls into bins
• Randomized Quick Sort
• Random Variable and Expected value
1
BALLS INTO BINS
CALCULATING PROBABILITY OF SOME INTERESTING EVENTS
2
Balls into Bins
1 2 3 4 5 … m-1 m
1 2 3 … i … n
Ball-bin Experiment: There are balls and bins. Each ball selects its bin
randomly uniformly and independent of other balls and falls into it.
Applications:
• Hashing
• Load balancing in distributed environment
3
Balls into Bins
1 2 3 4 5 … m-1 m
1 2 3 … i … n
Question : What is the probability that there is at least one empty bin ?
4
Balls into Bins
1 2 3 4 5 … m-1 m
1 2 3 … i … n
5
Balls into Bins
1 2 3 4 5 … j … m-1 m
1 2 3 … i … n
: th ball falls into th bin.
• Events and are ??
• Events and are ?? disjoint
• Events and are ?? Independent
Independent
6
Balls into Bins
1 2 3 4 5 … j … m-1 m
1 2 3 … i … n
: th ball enters th bin.
• Pr[] = ??
1
• Pr[] = ?? 𝑛
1
• Pr[th bin 1 is empty]
− = ??
𝑛
Pr[ ∩ …∩] …]
=
= Pr[ ] ⨯
7
Balls into Bins
1 2 3 4 5 … j … m-1 m
1 2 3 … i … n
• Pr[th bin is empty] =
8
Balls into Bins
Question: What is the probability that there is at least one empty bin ?
Attempt 1: Explore the sample space associated with the “balls into bins”.
9
Balls into Bins
Theorem: For events ,…, defined over a probability space (,P), then
P() =
…
)
--------------------------------------------------------------------
=
…
𝑝(𝑛, 𝑚)
…
10
Balls into Bins
Homework Exercise:
What is the probability that there are exactly empty bins ?
Hint: You will need to use with suitable values of .
11
RANDOMIZED QUICK SORT
WHAT IS PROBABILITY OF TWO SPECIFIC ELEMENTS GETTING COMPARED ?
12
Randomized Quick Sort
Input: [0..n-1]
RandomizedQuickSort(,, ) //For the first call, =0, =n-1
{ If ( < )
an element selected randomly uniformly from [..];
Partition(,,,x);
RandomizedQuickSort(,, );
RandomizedQuickSort(,, )
}
Assumption : All elements are distinct (if not, break the ties arbitrarily)
Notation : th smallest element of array .
Question: What is the probability that is compared with ?
Attempt 2: ??
View the execution of RandomizedQuickSort from perspective of and
14
Randomized-Quick-Sort
from perspective of and
16
Randomized-Quick-Sort
from perspective of and
𝑒𝑖 𝑒𝑗
𝑺𝑖 𝑗
Observation:
and get compared during an instance of Randomized Quick Sort iff
the first pivot element from is either or .
Let us define two events.
: first pivot element selected from during Randomized Quick Sort is .
: first pivot element selected from during Randomized Quick Sort is .
Pr[and get compared] = ??
Pr[U]
17
Randomized-Quick-Sort
from perspective of and
𝑒𝑖 𝑒𝑗
𝑺𝑖 𝑗
Pr[and get compared] = Pr[U]
= Pr[] + Pr[] - Pr[ ∩]
= Pr[] + Pr[]
What=relation
+ exists between and ?
=
What is Pr[] ? Pr[] = .
and are disjoint events.
18
Randomized-Quick-Sort
from perspective of and
Inferences:
• Probability depends upon the rank separation
• Probability is independent of the size of the array.
• and are compared surely for each .
• Probability of comparison of and is .
19
ALTERNATE SOLUTION
USING ANALOGY TO ANOTHER RANDOM EXPERIMENT
20
A Random Experiment:
A Story of two friends
There were two soldiers A and B serving in the army of a nation named Krakozhia.
They were very fast friends as well. During the war, they fought bravely but they got
captured by the enemy. A total of n soldiers got captured in this manner. Being war
prisoners, their future is quite uncertain. They are blindfolded and placed along a
straight line. All the soldiers will be dispatched to different locations in Syberia. A and
B are very anxious. They want to meet each other before departing forever. Showing
some mercy to the prisoners, the enemy uses the following protocol to break the
groups .
A person, say p, is selected randomly and uniformly from the current group. He goes
and meets every other person in the group and after that the group is broken into two
smaller groups: The persons lying to the left of p forms one group and the persons
lying to the right of p forms another group. Thereafter, p is sent to some location in
Syberia and the two groups are separated from each others and processed in a similar
manner recursively. In this manner a group is broken into smaller and smaller
subgroups. The order within each group is always maintained.
If A and B are located at positions and respectively initially, what is the probability
that they will be able to meet each other ?
21
Viewing the entire experiment
from perspective of A and B
𝑖 𝑗
1 2 3 4 … n-1 n
A B
22
Viewing the entire experiment
from perspective of A and B
𝑖 𝑗
1 2 3 4 … n-1 n
A B
23
Viewing the entire experiment
from perspective of A and B
𝑖 𝑗
A B
24
Viewing the entire experiment
from perspective of A and B
𝑖 𝑗
A B
25
Viewing the entire experiment
from perspective of A and B
𝑖 𝑗
A B
26
Viewing the entire experiment
from perspective of A and B
𝑖 𝑗
A B
27
PROBABILITY THEORY
28
Random variable
Randomized-
Quick-Sort
on array of size
n
Examples:
o The number of HEADS when a coin is tossed 5 times.
o The sum of numbers seen when a dice is thrown 3 times.
o The number of comparisons during Randomized Quick Sort on an array of
size n.
30
Many Random Variables
for the same Probability space
Random Experiment: Throwing a dice two times
• X : the largest number seen
• Y : sum of the two numbers seen
ω
X() = 6 Y() = 9
31
Expected Value of a random variable
(average value)
Definition: Expected value of a random variable X defined over a probability
space (Ω,P) is
E[X] =
X= c
X= a X= b
E[X] =
32
Examples
Random experiment 1: A fair coin is tossed n times
Random Variable X: The number of HEADS
E[X] =
=
=
33
Can we solve these problems ?