Randomized
Algorithms
Dr. Ramachandra Reddy
A short list of categories
• Algorithm types we will consider include:
• Simple recursive algorithms
• Backtracking algorithms
• Divide and conquer algorithms
• Dynamic programming algorithms
• Greedy algorithms
• Branch and bound algorithms
• Brute force algorithms
• Randomized algorithms
■ Also known as Monte Carlo algorithms or stochastic methods
Deterministic Algorithms
input output
Algorithm
• Goal to prove that the algorithm solves the
problem correctly always an quickly typically the
number of steps should be polynomial in the size
of the input.
Randomized Algorithms
input ouput
Algorithm
RANDOM NUMBERS
• In addition to input algorithm takes a source of
random numbers and makes random choices
during execution.
• Random algorithms make decisions on rolls of the
dice.
• Ex : Quick sort, Quick Select and Hash tables.
Why use randomness?
• Avoid worst-case behavior: randomness can
(probabilistically) guarantee average case behavior
• Efficient approximate solutions to inflexible
problems.
Randomized Algorithms
• A randomized algorithm is just one that depends on random
numbers for its operation.
• These are randomized algorithms:
• Using random numbers to help find a solution to a
problem.
• Using random numbers to improve a solution to a
problem.
• These are related topics:
• Getting or generating “random” numbers.
• Generating random data for testing (or other) purposes.
Types of Random Algorithms
• Las Vegas:
•Guaranteed to produce correct answer, but running
time is probabilistic.
• Monte Carlo:
Running time bounded by input size, but answer
may be wrong.
Las Vegas
• Always gives the true answer.
• Running time is random.
• Running time is bounded.
• Quick sort is a Las Vegas algorithm.
• A Las Vegas algorithm always produces the correct answer its
running time is a random variable whose expectation is bounded (say
by a polynomial).
Monte Carlo
• It may produce incorrect answer!
• We are able to bound its probability.
• By running it many times on independent random
variables, we can make the failure probability
arbitrarily small at the expense of running time.
• A Monte Carlo algorithm runs for a fixed number
of steps and produces an answer that is correct
with probability ≥1/2.
Advantages of randomized
algorithms
• Simplicity.
• Performance.
• For many problems, a randomized algorithm is the simplest, the
fastest, or both.
Quick Sort
Worst Case Partitioning of Quick Sort
Best Case Partitioning of Quick Sort
Avg Case of Quick Sort
Randomized Quick Sort
Randomized Quick Sort- Analysis
• Worst case runtime: O(m^2)
• Expected runtime: O(m log m).
• Expected runtime is a good measure of the performance of
randomized algorithms, often more informative than worst case
runtimes
Applications: Simple Algorithms and Card Games
• A randomized algorithm is an algorithm whose behavior
depends, in part, on the outcomes of random choices or the
values of random bits.
• The main advantage of using randomization in algorithm
design is that the results are often simple and efficient.
• In addition, there are some problems that need
randomization for them to work effectively.
• For instance, consider the problem common in computer
games involving playing cards—that of randomly shuffling a
deck of cards so that all possible orderings are equally likely.
Shuffling Cards
• Shuffling card is an essential part of every card game.
• There are many techniques for shuffling cards but overhand
and riffle are the most popular ones.
• A deck of cards is essentially a fixed sized array of length 52.
Overhand shuffle puts set of cards from the end of the array
to the beginning of an array.
• This process gets repeated to get a good shuffle.
Shuffling Cards
• Riffle shuffle
• This involves cutting the deck into 2 halves so we get two sets of
cards and riffle them such that at the end both halves gets
interleaved.
Shuffling Cards
• Fisher-Yates shuffle is one such algorithm for achieving a perfect
shuffle using a random number generator.
• The algorithm is named after Ronald Fisher and Frank Yates who
first described this algorithm in their book in 1938.
• Later Donal Knuth and Richard Durstenfeld introduced an
improved version of the algorithm in 1964.
Shuffling Cards
• Algorithm:
• 1. First, fill the array with the values in order.
• 2. Go through the array and exchange each element with the
randomly chosen element in the range from itself to the end.
Random Shuffling
• deck = ["Ace of hearts","2 of hearts","3 of hearts","4 of hearts","5 of hearts","6 of hearts","7 of
hearts","8 of hearts","9 of hearts","10 of hearts","Jack of hearts","Queen of hearts","King of
hearts","Ace of diamonds","2 of diamonds","3 of diamonds","4 of diamonds","5 of diamonds","6
of diamonds","7 of diamonds","8 of diamonds","9 of diamonds","10 of diamonds","Jack of
diamonds","Queen of diamonds","King of diamonds","Ace of clubs","2 of clubs","3 of clubs","4 of
clubs","5 of clubs","6 of clubs","7 of clubs","8 of clubs","9 of clubs","10 of clubs","Jack of
clubs","Queen of clubs","King of clubs","Ace of spades","2 of spades","3 of spades","4 of
spades","5 of spades","6 of spades","7 of spades","8 of spades","9 of spades","10 of
spades","Jack of spades","Queen of spades","King of spades"]