0 ratings0% found this document useful (0 votes) 33 views6 pagesModule 4 - Randomized - Searching
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
aq algorithm is a different design approach taken by the standard algorithms
el few random bits are added to a part of their logic. They are different from determin-
‘istic algorithms; that follow a definite procedure to get the same output every time an input
passed whereas randomized algorithms produce a different output every time they're
INPUT OUTPUT
ALGORITHM
Figure 11.5: Deterministic ApproachCOMPUTATIONAL APPROACHES TO PROBLEM SOLVING
a epecin inc
ee acest
a ton i agate et
parts.
Cons of Randomized Approach
« In the worst case, a randomized algorithm may be very slow. :
«+ There is a finite probability of getting incorrect answer. However, the probability of
getting a wrong answer can be made arbitrarily small by the repeated employment of
randomness.
* Getting true random numbers is almost impossible.
‘Types of Randomized Algorithms
1. Las Vegas: It is a randomized algorithm that always returns a correct result. But the
‘running time may vary between executions.
Example: Randomized QUICK SORT Algorithm
2. Monte Carlo:
Itis a randomized algorithm that terminates in polynomial time, but might produce
erroneous result.
Example: Randomized MINCUT Algorithm
Example 1: A company selling jeans gives a coupon with each jeans. There are n
different coupons. Collecting n different coupons would give you a free jeans. How
many jeans do you expect to buy before you get a free jeans?
* The coupon collection process is in phase j when you have already collected j
different coupons and are buying to get a new type.
* Anew type of coupon ends phase j and you enter phase j + 1.
+ LetXj be the random variable equal to the number of jeans you buy in phase j.
+ Then,alculate
mt
Lx
=
is the number of jeans bought to har i
we n different coupons,
‘The expected number of jeans bought in phase j,randomized Approach
. succes i
‘The success probability, p in the j-th phase is
E[Xjlis:
‘qhe expected number of jeans bought before all n types of coupons are collected is
E[X] = nH, = (nlogn)
Example
Suppose a company is giving out coupons with every pair of
{ifferent coupons. Once you collect all 5 different coupons,
We want to calculate the expected number of jeans you need to
coupons.
Step-by-Step Breakdown
jeans sold, and there are n=5
‘you get a free pair of jeans.
‘buy to collect all 5 different
1, First Coupon:
+ You buy your first pair of jeans and get your first coupon. This is always new
since you don't have any coupons yet.
+ Expected number of jeans bought so far: 1.
2. Second Coupon:
+ "The probability of getting a new coupon is 4;
coupons are still new to you.
+ The expected number of jeans t0 get a new coupon is 5/4.
{Expected number of jeans bought s0 far: 145/4=2.25.
3. Third Coupon:
«The probability of getting a new coupons 3/5 because 3 out oftthe 5 possible
coupons are still new to you.
«The expected number of jeans to get a MeN CONE is 513.
+ Expected number of jeans bought so far: 2.25+5/3=2.25+1.67=3.92
4, Fourth Coupon:
» ‘The probability of geting anew coupon 1S 4I5 because 2 out of the 5 possible
coupons are still new to you:
+ The expected number of jeans to Bet ¢
«+ Expected number of jeans ‘bought so far:
5, Fifth Coupon: ‘i
The probability of getting
coupons is still new to you.
'5 because 4 out of the 5 possible
ew coupon is 5/2.
- 3,9245/2=3.9242. 5=6.42.
anew coupon is 1/5 because 1 out ofthe 5 possible11, COMPUTATIONAL APPROACHES TO PROBLEM SOLVING
205 __Chopl 11. COMPUTATION
ted number of jeans to get a new cou
zy Tas eros 6A24+5=11.42,
or of jeans bought so far: 6.
pees Peete jeans ae need to buy to collect all 5 different coupons
0, the expected
BRpromatly 1142
General Formula Application
‘To verify using the formula
spon is S/I=5.
E(n) ="-Hy
Forn=5, the harmonic number HS is:
HS=141/2413+1/4+1/5=2.283
Thea
E(5)=5*2.283=11.42
Example 2: n people go to a party and drop off their hats to a hat-check person.
‘When the party is over, a different hat-check person is on duty and returns the n hats
randomly back to each person. What is the expected number of people who get back
their hats?
Let X be the random variable representing the number of people who get their own hat
back.
+ Forn=3, find E[X] by first computing the probability mass function Px, and then
applying the definition of expectation.
* Find a general formula for F(X], for any positive integer n,
To find the expected number of people who get their own hat back for n=3,we'll first
Compute the probability mass function (PMF) P(X=k) for k=0,1,2,3 and then apply the
definition of expectation.
Step-by-Step Calculation
1. Enumerate all Possible Permutations:
For n=3, there are 3!=6 possible permutations of the hats. We list them: (1,2,3),
13,2), (2,1,3),(2,3,1),3,1,2) and (3,2,1)
2. Count Fixed Points in Each Permutation:
+ (1,2,3): All 3 people All 3 people get their own hat back G3 fixed points),
+ G1, 3,2): Only person 1 gets their hat back (1 fixed point),
* @, 1,3); Only person 3 gets their hat back (1 fixed point),
* 23,1): No one gets their own hat back (0 fixed points).
* G,1,2): No one gets their own hat back (0 fixed points),
* G,2, 1): Only person 2 gets their hat back (1 fixed point).NsowING =|
nt coupons i
2,3),
118 Randomized Approach
207
3 ome Probability Mass Funetion POX=t)
+ PEK=0): Ni si 5
): Number of permutations with 0 fixed points / Total permutations =
216 = 1/3
+ PCX=1): Number of
a f Permutations with 1 fixed point / Total permutations = 3/6
£ POR2): Number of permutations with 2 fixed points °
+ P(K=3): Number of,
4, Definition of eee tutions wih 3 fixed pois = 16
E(x] = Yerx )
ca)
5. Compute Expectation for n=3:
E[X]-0*P(X=0)+1*P(X=1)+2*P(X=2)+3*P(X=3}
=I S =3)
E[X]= 0° 1/3+ 11/2424013"1/6
E[X]=0+ 1/2+0+1/2= 1
‘The expected number of people who get their own hat back for n=3 is E[X]=1.
‘The expected number of people who get their own hat back can be found using indicator
random variables and the linearity of expectation. For i = 1,...,7, let X; be the indicator
variable that equals 1 if person i gets their hat back, and 0 otherwise.
1 if person i gets their hat back
0 otherwise
Let X be the total number of people who get their own hat back:
X= x]
By the linearity of expectation:
EX]=E [e+] LEX
mi | mt
Each person has a probability of + of getting their own hat back:
x
E(x] = P(X =
Therefore
EX]=
‘Tins, the expected number of people who get their own hat back is 1.Es TO PROBLEM SOLVING,
CH
208 _Chapter 11 COMPUTATIONAL APPROAS
si roach
rhe Renda er
11.7. Motivations
algorithms offer several significam :
oe simpler: For instance, finding the kth
using deterministic methods,
icked to parti-
ized algorithms are often SiMP ©
Seas .d list can be complex ae
hh where a random element is I
rithm.
Randomize!
1, Simplicity: Rando’
‘smallest element in an unordere
However, using a randomized approacl
tion the problem, results in a much simpler alB0
‘i better asymptotic running times. For
iency: ced algorithms can have mpto i
pemeraca, sore algorithms run in exponential time, while randomized
algorithms run in polynomial time.
zation is useful when dealing with incomplete infor.
ice and Bob, each with an n-bit number.
the same with minimal communica.
ging n bits, but a randomized protocol
h probability using only O(logn) bits,
3. Lack of Information: Randomi: f
‘mation. For example, consider two parties, Ali
re
They need to determine if their numbers
tion. A deterministic protocol requires exchan}
based on fingerprinting can achieve this with hig!
4, Symmetry Breaking: Randomization helps in designing contention resolution mech-
anisms in distributed protocols. For example, the Ethernet protocol uses random
back-off duration to manage simultaneous transmission attempts effectively, without
prior communication.
. Counting via Sampling: Randomization can help estimate the size of large spaces
or sets through sampling. For instance, computing the integral of a multivariate
function over a region can be approximated by sampling points from a bounding box
and determining the fraction that lies below the function.
6. Searchit : ii i
se aaa pow Randomized algorithms are effective in finding witnesses
cee een ties ae the density of witnesses is high. In Polynomial Identity
a iple, random points are likel identi
Boi aia sno sdncahges ly to identify non-zero evaluations if the