04 Randomized Algorithms
04 Randomized Algorithms
Algorithm Fundamentals
Randomized
Algorithms
Lectures prepared by: Dr. Manal Alharbi, and Dr. Areej Alsini
Deterministic algorithms
2.Consistent output: Given the same input, a deterministic algorithm will always
produce the same output. This property is crucial for ensuring the reliability and
predictability of an algorithm.
▪ For many algorithms (such as quicksort), the average run time is much
better than the worst-case run time. Average run times are computed
assuming a specific (typically uniform) distribution on the input space.
However, this assumption may not hold in practice.
• Since the output of any randomizer might differ unpredictably from run to
run, the output of a randomized algorithm could also vary from run to run for
the same input.
• The execution time of a randomized algorithm could also vary from run to run for the
same input.
Randomized Algorithms-Challenge
Input: Given an array with n elements (n even). A[1 … n]. Half of the
array contains 0s, the other half contains 1s.
Output: An index that contains a 1.
Types of randomized algorithms
• Typically, for a fixed input, a Monte Carlo algorithm does not display
much variation in execution time between runs, whereas in the case of a
Las Vegas algorithm, this variation is significant.
Monte Carlo algorithm
Randomized Algorithms
• Randomized algorithms offer simplicity and better performance relative to
their deterministic counterparts. For a number of fundamental problems in
computing (such as sorting, selection, convex hull, etc.).
• We can argue that any deterministic algorithm for solving the above problem
𝑛
needs 2 + 2 time as follows: Consider an adversary who has perfect knowledge
about the algorithm to be used and who is picking the input. Specifically, the
adversary knows the order in which the sequence elements are accessed by the
algorithm. In this case the adversary can make sure that the first + 1 elements
accessed by the algorithm are distinct, forcing the algorithm to access at least
𝑛
one more element ➔ 2 + 2 .
▪ Example: n=8, A= {1, 2, 7, 8, 3, 3, 3, 3}
Identification of the Repeated Element: Las
Vegas algorithm
• We can develop a Las Vegas algorithm that takes only Õ(log 𝑛) time with a
high probability.
Basic step
• repeat
1. Flip an 𝑛-sided coin to get 𝑖;
2. Flip an 𝑛-sided coin to get 𝑗;
3. if 𝑖 ≠ 𝑗 and 𝑘𝑖 = 𝑘𝑗 then output 𝑘𝑖 and quit;
• forever
1 2 3 4 5 6 7 8
𝑊ℎ𝑖𝑙𝑒 (𝑡𝑟𝑢𝑒) 𝑑𝑜: // i and j are random numbers in the range [1,n]
𝑖 = 𝑅𝑎𝑛𝑑𝑜𝑚 () 𝑚𝑜𝑑(𝑛 + 1); //to make sure the range of the value is between 1 to n
𝑗 = 𝑅𝑎𝑛𝑑𝑜𝑚 () 𝑚𝑜𝑑(𝑛 + 1); //to make sure the range of the value is between 1 to n
• In this algorithm, the sampling performed is with repetitions; that is, the first
and second elements are randomly picked from out of the n elements (each
element being equally likely to be picked).
1
• Thus, there is a probability (equal to ) that the same array element is picked
𝑛
each time. If we just check for the equality of the two elements picked, our
answer might be incorrect (in case the algorithm picked the same array index
each time).
• Therefore, it is essential to make sure that the two array indices picked are
different and the two array cells contain the same value.
Identification of the Repeated Element: Las
Vegas algorithm
• Definition: The run time of a Las Vegas algorithm is Õ(𝑓(𝑛)) if− the run time is no more
than 𝑐𝛼𝑓(𝑛) for all 𝑛 ≥ 𝑛0 , with a probability 𝑜𝑓 ≥ (1 − 𝑛 𝛼 ), where 𝑐 and 𝑛0 are
some constants.
• Proof: Call the sequence of three statements within the repeat loop as a basic step.
• Any iteration of the while loop will be successful in identifying the repeated number if i is any
𝑛 𝑛
one the array indices corresponding to the repeated element and j is any one of the same
2 2
indices other than i.
𝒏
• When we have 𝒏 numbers where are repeated, what is the probability of choosing
𝟐
𝒊, 𝒋 from that repeated part?
𝒏 𝒏
−𝟏
• The probability of choosing 𝒊 = 𝟐
, the probability of choosing 𝒋 = 𝟐
𝒏 𝒏
• In other words, the probability that the algorithm quits in any given iteration of the
𝑛 𝑛 𝑛 𝑛
−1 −1
while loop is 2 2
= 2 2
𝑛 𝑛 𝑛2
Identification of the Repeated Element: Las
Vegas algorithm
• Proof: Call the sequence of three statements within the repeat loop as a basic step.
𝑛 𝑛−1
• If n = 2 * 106, for example, any deterministic algorithm will have to spend at least one million-time
steps, as opposed to the 100 iterations of Algorithm.
• We want this probability to be very small, specifically no more than n−α.
4 𝑘 4 𝛼𝑙𝑜𝑔𝑛
➔ = 𝑛−𝛼 ➔ 𝑘 𝑙𝑜𝑔 ≤ − 𝛼𝑙𝑜𝑔𝑛 ➔ 𝑘 ≥ 5
5 5 log(4)
𝛼𝑙𝑜𝑔𝑛
•We have shown that the probability that the above algorithm takes more than 5 basic steps is ≤ n−α.
log(4)
•Note that each basic step takes a constant time. Therefore, the run time of the algorithm is ≤ cαlogn with a
probability of ≥(1- n−α), for some constant c. Thus, it follows that the run time of the algorithm is Õ(logn).
Searching repeated elements- analysis
Finding an Element as Large as the Median:
Monte Carlo algorithm
• Consider the following problem.
• Input: X = k1 ,k2,...,kn;
• Output: An element of X that is at least as large as the median of X.
• For this problem also, several deterministic algorithms can be developed:
1. Sort X and pick an element at the middle or to its right. This will take Θ(nlogn)
time;
2. Find and output the maximum of X. This takes Θ(n) time;
3. Find and output the maximum of k1, k2,...,kn/2. This also takes Θ(n) time.
• One could argue that any deterministic algorithm will need Ω(n) time to solve
this problem as follows:
• If an adversary chooses the input and if this adversary has perfect knowledge
𝑛
about the algorithm, (s)he can force the algorithm to look at ≥ elements of X.
2
𝑛
• If the algorithm is ready to give an answer looking at less than elements, the
2
adversary can choose the other elements of X in such a manner to make the output
of the algorithm incorrect.
Finding an Element as Large as the Median:
Monte Carlo algorithm
• We can devise a Monte Carlo algorithm that takes only Õ(logn) time and
whose output is correct with a high probability as follows.
• Pick a random sample S of αlogn elements from X;
• The above algorithm will give an incorrect answer only if all the elements in S
are less than the median.
• Probability that a randomly picked element of X is less than the median is
1
≤2
1 𝑘
• The probability that all k elements are incorrect is 2
•https://youtu.be/bsZXgXdSomc?si=MMhhEyGWdxA4NOmM