0% found this document useful (0 votes)
17 views2 pages

Randomized Algorithms

A randomized algorithm uses a randomizer to make some decisions, so its output and runtime can vary between runs even for the same input. There are two types: Las Vegas algorithms always produce the same output but runtime varies, while Monte Carlo algorithms may produce different outputs but have less runtime variation. The document describes randomized algorithms for identifying a repeated element in an array and for primality testing based on Fermat's theorem.

Uploaded by

forallofus435
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views2 pages

Randomized Algorithms

A randomized algorithm uses a randomizer to make some decisions, so its output and runtime can vary between runs even for the same input. There are two types: Las Vegas algorithms always produce the same output but runtime varies, while Monte Carlo algorithms may produce different outputs but have less runtime variation. The document describes randomized algorithms for identifying a repeated element in an array and for primality testing based on Fermat's theorem.

Uploaded by

forallofus435
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Randomized Algorithms

A randomized algorithm is one that makes use of a randomizer (such as a random


number generator). Some of the decisions made in the algorithm depend on the output of
the randomizer. Since the output of any randomizer might differ in an unpredictable way
from run to run, the output of a randomized algorithm could also differ 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.
Random algorithms can be categorized into two classes:
1. Las Vegas algorithms – always produce the same output for the same input. The
execution time of a Las Vegas algorithm depends on the output of the randomizer. The
Las Vegas algorithm might terminate fast or it might run for a longer period of time. The
execution time of a Las Vegas algorithm is characterized as a random variable.
2. Monte Carlo algorithms - algorithms whose outputs might differ from run to run for
the same input. 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.
Identifying the Repeated Element
Consider an array a[ ] of n numbers that has n/2 distinct elements and n/2 copies of
another element. The problem is to identify the repeated element.
Any deterministic algorithm for solving this problem will need at least (n/2) +2 time
steps in the worst case. In contrast there is a simple and elegant randomized Las Vegas
algorithm that takes only Õ(logn) time. It randomly picks two array elements and checks
whether they come from two different cells and have the same value. If they do, the
repeated element has been found. If not, this basic step of sampling is repeated as many
times as it takes to identify the repeated element. The algorithm is given below.

Primality Testing ꓶꓩꓡꓙ/


Given an integer n, the problem of deciding whether n is a prime is known as primality
testing. If a number n is composite (i-e., nonprime), it must have a divisor ≤ ꓡ √n ꓙ. This
observation leads to the following simple algorithm for primality testing:
Consider each number l in the interval [2, ꓡ √n ꓙ] and check whether l divides n. If none
of these numbers divides n, then n is prime; otherwise, it is composite. Assuming that it
takes O(1) time to determine whether one integer divides another, the naive primality
testing algorithm has a run time of O( √n).
A Monte Carlo randomized algorithm can be devised for primality testing that runs in
time O((logn)^2). The output of this algorithm is correct with high probability. If the input
is prime, the algorithm never gives an incorrect answer. However, if the input number is
composite (i.e., nonprime), then there is a small probability that the answer may be
incorrect. Algorithms of this kind are said to have one-sided error. Fermat’s theorem
from number theory serves as the backbone of the algorithm.
Fermat’s theorem states that If n is prime, for any integer a < n, (a ^ n) mod n ≡ 1
Fermat’s theorem suggests the following algorithm for primality testing: Randomly
choose an a that is less than a, and check if (a ^ n) mod n ≡ 1 (Fermat’s Equation). If
Fermat’s equation is not satisfied, n is composite. If the equation is satisfied, we try some
more random a’s. If on each a tried, Fermat’s equation is satisfied, we output “n is prime”;
otherwise we output “n is composite”. The algorithm is given below.

You might also like