Lably
Skip to content

Randomized algorithm

Algorithm that employs a degree of randomness as part of its logic or procedure

  • 14 min read
  • 6 chapters

Related on Lably

Through Algorithm, Randomness and Quicksort

There is a distinction between algorithms that use the random input so that they always terminate with the correct answer, but where the expected running time is finite (Las Vegas algorithms, for example Quicksort), and algorithms which have a chance of producing an incorrect result (Monte Carlo algorithms, for example the Monte Carlo algorithm for the MFAS problem) or fail to produce a result either by signaling a failure or failing to terminate. In some cases, probabilistic algorithms are the only practical means of solving a problem.

In common practice, randomized algorithms are approximated using a pseudorandom number generator in place of a true source of random bits; such an implementation may deviate from the expected theoretical behavior and mathematical guarantees which may depend on the existence of an ideal true random number generator.

Motivation

As a motivating example, consider the problem of finding an 'a' in an array of n elements.

Input: An array of n≥2 elements, in which half are 'a's and the other half are 'b's.

Output: Find an 'a' in the array.

We give two versions of the algorithm, one Las Vegas algorithm and one Monte Carlo algorithm.

Las Vegas algorithm:

<span id="mwOw"></span><span class="n" id="mwPA">findingA_LV</span><span class="p" id="mwPQ">(</span><span class="k" id="mwPg">array</span><span class="w" id="mwPw"> </span><span class="n" id="mwQA">A</span><span class="o" id="mwQQ">,</span><span class="w" id="mwQg"> </span><span class="n" id="mwQw">n</span><span class="p" id="mwRA">)</span> <span class="k" id="mwRQ">begin</span> <span class="w" id="mwRg"> </span><span class="k" id="mwRw">repeat</span> <span class="w" id="mwSA"> </span><span class="n" id="mwSQ">Randomly</span><span class="w" id="mwSg"> </span><span class="n" id="mwSw">select</span><span class="w" id="mwTA"> </span><span class="n" id="mwTQ">one</span><span class="w" id="mwTg"> </span><span class="n" id="mwTw">element</span><span class="w" id="mwUA"> </span><span class="n" id="mwUQ">out</span><span class="w" id="mwUg"> </span><span class="k" id="mwUw">of</span><span class="w" id="mwVA"> </span><span class="n" id="mwVQ">n</span><span class="w" id="mwVg"> </span><span class="n" id="mwVw">elements</span><span class="o" id="mwWA">.</span> <span class="w" id="mwWQ"> </span><span class="k" id="mwWg">until</span><span class="w" id="mwWw"> </span><span class="s" id="mwXA">'a'</span><span class="w" id="mwXQ"> </span><span class="k" id="mwXg">is</span><span class="w" id="mwXw"> </span><span class="n" id="mwYA">found</span> <span class="k" id="mwYQ">end</span>

This algorithm succeeds with probability 1. The number of iterations varies and can be arbitrarily large, but the expected number of iterations is

limn→∞∑i=1ni2i=2

Since it is constant, the expected run time over many calls is Θ(1). (See Big Theta notation)

Monte Carlo algorithm:

<span id="mwbA"></span><span class="n" id="mwbQ">findingA_MC</span><span class="p" id="mwbg">(</span><span class="k" id="mwbw">array</span><span class="w" id="mwcA"> </span><span class="n" id="mwcQ">A</span><span class="o" id="mwcg">,</span><span class="w" id="mwcw"> </span><span class="n" id="mwdA">n</span><span class="o" id="mwdQ">,</span><span class="w" id="mwdg"> </span><span class="n" id="mwdw">k</span><span class="p" id="mweA">)</span> <span class="k" id="mweQ">begin</span> <span class="w" id="mweg"> </span><span class="n" id="mwew">i</span><span class="w" id="mwfA"> </span><span class="o" id="mwfQ">:=</span><span class="w" id="mwfg"> </span><span class="mi" id="mwfw">0</span> <span class="w" id="mwgA"> </span><span class="k" id="mwgQ">repeat</span> <span class="w" id="mwgg"> </span><span class="n" id="mwgw">Randomly</span><span class="w" id="mwhA"> </span><span class="n" id="mwhQ">select</span><span class="w" id="mwhg"> </span><span class="n" id="mwhw">one</span><span class="w" id="mwiA"> </span><span class="n" id="mwiQ">element</span><span class="w" id="mwig"> </span><span class="n" id="mwiw">out</span><span class="w" id="mwjA"> </span><span class="k" id="mwjQ">of</span><span class="w" id="mwjg"> </span><span class="n" id="mwjw">n</span><span class="w" id="mwkA"> </span><span class="n" id="mwkQ">elements</span><span class="o" id="mwkg">.</span> <span class="w" id="mwkw"> </span><span class="n" id="mwlA">i</span><span class="w" id="mwlQ"> </span><span class="o" id="mwlg">:=</span><span class="w" id="mwlw"> </span><span class="n" id="mwmA">i</span><span class="w" id="mwmQ"> </span><span class="o" id="mwmg">+</span><span class="w" id="mwmw"> </span><span class="mi" id="mwnA">1</span> <span class="w" id="mwnQ"> </span><span class="k" id="mwng">until</span><span class="w" id="mwnw"> </span><span class="n" id="mwoA">i</span><span class="w" id="mwoQ"> </span><span class="o" id="mwog">=</span><span class="w" id="mwow"> </span><span class="n" id="mwpA">k</span><span class="w" id="mwpQ"> </span><span class="k" id="mwpg">or</span><span class="w" id="mwpw"> </span><span class="s" id="mwqA">'a'</span><span class="w" id="mwqQ"> </span><span class="k" id="mwqg">is</span><span class="w" id="mwqw"> </span><span class="n" id="mwrA">found</span> <span class="k" id="mwrQ">end</span>

If an 'a' is found, the algorithm succeeds, else the algorithm fails. After k iterations, the probability of finding an 'a' is:

Pr[finda]=1−(1/2)k

This algorithm does not guarantee success, but the run time is bounded. The number of iterations is always less than or equal to k. Taking k to be constant the run time (expected and absolute) is Θ(1).

Randomized algorithms are particularly useful when faced with a malicious "adversary" or attacker who deliberately tries to feed a bad input to the algorithm (see worst-case complexity and competitive analysis (online algorithm)) such as in the Prisoner's dilemma. It is for this reason that randomness is ubiquitous in cryptography. In cryptographic applications, pseudo-random numbers cannot be used, since the adversary can predict them, making the algorithm effectively deterministic. Therefore, either a source of truly random numbers or a cryptographically secure pseudo-random number generator is required. Another area in which randomness is inherent is quantum computing.

In the example above, the Las Vegas algorithm always outputs the correct answer, but its running time is a random variable. The Monte Carlo algorithm (related to the Monte Carlo method for simulation) is guaranteed to complete in an amount of time that can be bounded by a function the input size and its parameter k, but allows a small probability of error. Observe that any Las Vegas algorithm can be converted into a Monte Carlo algorithm (via Markov's inequality), by having it output an arbitrary, possibly incorrect answer if it fails to complete within a specified time. Conversely, if an efficient verification procedure exists to check whether an answer is correct, then a Monte Carlo algorithm can be converted into a Las Vegas algorithm by running the Monte Carlo algorithm repeatedly till a correct answer is obtained.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication