A randomized algorithm can be defined as one that receives, in

CPSC 335
Randomized Algorithms
Dr. Marina Gavrilova
Computer Science
University of Calgary
Canada
Randomized Algorithm
 A randomized algorithm can be defined as one
that receives, in addition to its input, a stream of
random bits that it can use in the course of its
action for the purpose of making random choices.
 A randomized algorithm may give different
results when applied to the same input in different
runs.
Advantages of Randomized Algorithm
 There are two main advantages of randomized
algorithms:
- First, often the execution time or space
requirement is smaller than that of the best
deterministic algorithm.
- Second, randomized algorithms are extremely
simple to comprehend and implement.
Types of Randomized Algorithm
 Las Vegas algorithms: constitute those
randomized algorithms that always give a correct
solution, or do not have any correct solutionat all.
 Monte Carlo Algorithms: These algorithms always
give an answer, but may occasionally produce an
answer that is incorrect.
Computational Complexity of A
Randomized Algorithm

If A is a randomized algorithm, then its
running time on a fixed instance I of size n may
vary from one execution to another.

Therefore, a more natural measure of
performance is the expected running time of A on a
fixed instance I.

This is the mean time taken by algorithm A to
solve the instance I over and over. Thus, it is
natural to talk about the worst case expected time
and the average case expected time.
(n log n)
Randomized Quicksort

The running time of quicksort is (n log n)
on the average, provided that all permutations of
the input elements are equally likely.
(n ) applications.
This is not the case in many
2
If the input is already sorted, then its running time
is (n 2 )
(n log n)
Randomized Quicksort
One approach to guarantee a running time of
(n log n) on the average is to introduce a
preprocessing step with purpose to permute the
elements to be sorted randomly.
This step can be performed in (n) time.


Another approach is to introduce an element
of randomness into the algorithm.
This can be done by selecting the pivot on
which to split the elements randomly.
The result of choosing the pivot randomly is to
relax the assumption that all permutations of the
input elements are equally likely.
(n log n)
Randomized Quicksort
(n log n)
Randomized Selection

Randomized Las Vegas algorithm
Expected running time - (n)
(n log n)
Randomized Selection
Theorem: The expected number of
elements comparisons performed by
Algorithm RANDOMIZEDSELECT on
input of size n is less than 4n. Its
expected running time is (n) .

Testing String Equality
 Let two parties A and B communicate over a
reliable communication channel.
 They want to determine whether x = y, x and y
are two long strings of A and B respectively.
 Cost of using the channel – extremely expensive.
 Another alternative – A derives much shorter
string (serve as a ‘fingerprint’) from x and send it
to B.
x y
B follows the same procedure.
 If they are equal, B conclude that x = y
Testing String Equality
 One method of fingerprinting is to choose a
prime number p and then use the fingerprint
function - Ip (x) = I (x) (mod p).
 The number of bits transmitted is thus smaller.
If I p ( x)  I p ( y) , then obviously, x  y .
The converse is also true.
If Ip(x) = Ip(y), then it is not necessarily the
case that x = y.
Testing String Equality
 The wellness of this method is that, for fixed p,
there are certain pair of strings x and y on which
the method will always fail.
 To get around this problem of the existence of
these pairs x and y – chose p at random every time
the equality of two strings is to be checked, rather
than agree on p in advance.
Testing String Equality
Pattern Matching
 Given a string of text X= x1x2…xn and a pattern
Y= y1y2…ym, where m  n , determine whether or
not the pattern appears in the text.
 The most straightforward method for solving
this problem is to move the pattern across the
entire text, and in every position compare the
pattern with the portion of the text of length m.
This Brute-force method leads to an O(mn)
running time in the worst case.
More complicated deterministic algorithms have
running time O(m+n).
Monte Carlo Algorithm for Pattern Matching
 Running time is O(m+n).
 Follows the same Brute-force algorithm, but
instead of comparing the pattern with each block
X(j)=xjxj+1…xj+m-1, this algorithm compare the
fingerprint Ip(Y) of the pattern with the
fingerprints Ip(X(j)) of the blocks of text.
 The key observation is that, when one block of
text is shifted to the next, the fingerprint of the
new block X(j+1) can easily be computed from the
fingerprint of X(j).
Pattern Matching
References
 M. E. Dyer and A. M. Frieze, A randomized
algorithm for fixed-dimensional linear programming,
Joural of Mathematical Programming, vol.44, no. 13,May, 1989.
 V. V. Patel, G. Deodhare and T. Viswanath,
Some applications of randomized algorithms for
control system design, Automatica,vol. 38, no. 12,
Dcember 2002.
Review questions




Define Las Vegas and Monte Carlo algorithms
Explain how randomized pattern matching works
Describe testing string equality algorithm
Describe randomized quicksort method
19
Thank You