0% found this document useful (0 votes)
2 views35 pages

Randomized Algorithms (1)

Randomized algorithms utilize randomness to enhance efficiency and simplify design, often yielding faster solutions than deterministic algorithms. Random variables, which can be discrete or continuous, assign numerical values to outcomes of random experiments and are fundamental in statistics. The document also covers the definitions, types, and formulas related to random variables, including their mean and variance, as well as examples and applications in probability distributions.

Uploaded by

Raunak Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views35 pages

Randomized Algorithms (1)

Randomized algorithms utilize randomness to enhance efficiency and simplify design, often yielding faster solutions than deterministic algorithms. Random variables, which can be discrete or continuous, assign numerical values to outcomes of random experiments and are fundamental in statistics. The document also covers the definitions, types, and formulas related to random variables, including their mean and variance, as well as examples and applications in probability distributions.

Uploaded by

Raunak Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Randomized Algorithms

Randomized algorithms in data structures and algorithms (DSA) are algorithms that use
randomness in their computations to achieve a desired outcome. These algorithms introduce
randomness to improve efficiency or simplify the algorithm design. By incorporating random
choices into their processes, randomized algorithms can often provide faster solutions or better
approximations compared to deterministic algorithms. They are particularly useful in situations
where exact solutions are difficult to find or when a probabilistic approach is acceptable.

For example, in Randomized Quick Sort, we use a random number to pick the next pivot (or
we randomly shuffle the array). Typically, this randomness is used to reduce time complexity
or space complexity in other standard algorithms.

Random Variables

A random variable in statistics is a function that assigns a real value to an outcome in the
sample space of a random experiment. For example: if you roll a die, you can assign a number
to each possible outcome.

Random variables can have specific values or any value in a range.

There are two basic types of random variables,

 Discrete Random Variables

 Continuous Random Variables

In this article, we will learn about random variables in Statistics, their types, examples, and
others in detail.

What is Random Variable Meaning

A Random variable in probability is a mathematical concept that assigns numerical values to


outcomes of a sample space. They can describe the outcomes of objective randomness (like
tossing a coin) or subjective randomness(results of a cricket game).

There are two types of Random Variables- Discrete and Continuous.

A random variable is considered a discrete random variable when it takes specific, or distinct
values within an interval. Conversely, if it takes a continuous range of values, then it is
classified as a continuous random variable.

Random variables are generally represented by capital letters like X and Y. This is explained
by the example below:

Example
If two unbiased coins are tossed then find the random variable associated with that event.

Solution:

Suppose Two (unbiased) coins are tossed

X = number of heads. [X is a random variable or function]

Here, the sample space S = {HH, HT, TH, TT}

Random Variable Definition

We define a random variable as a function that maps from the sample space of an experiment
to the real numbers. Mathematically, Random Variable is expressed as,

X: S →R

where,

 X is Random Variable (It is usually denoted using capital letter)

 S is Sample Space

 R is Set of Real Numbers

Suppose a random variable X takes m different values i.e. sample space

X = {x1, x2, x3………xm} with probabilities

P(X = xi) = pi

where 1 ≤ i ≤ m

The probabilities must satisfy the following conditions :

 0 ≤ pi ≤ 1; where 1 ≤ i ≤ m

 p1 + p2 + p3 + ……. + pm = 1 Or we can say 0 ≤ pi ≤ 1 and ∑pi = 1

Hence possible values for random variable X are 0, 1, 2.

X = {0, 1, 2} where m = 3

 P(X = 0) = (Probability that number of heads is 0) = P(TT) = 1/2×1/2 = 1/4

 P(X = 1) = (Probability that number of heads is 1) = P(HT | TH) = 1/2×1/2 + 1/2×1/2 = 1/2

 P(X = 2) = (Probability that number of heads is 2) = P(HH) = 1/2×1/2 = 1/4

Here, you can observe that, (0 ≤ p1, p2, p3 ≤ 1/2)

p1 + p2 + p3 = 1/4 + 2/4 + 1/4 = 1

For example,
Suppose a dice is thrown (X = outcome of the dice). Here, the sample space S = {1, 2, 3, 4, 5,
6}. The output of the function will be:

 P(X=1) = 1/6

 P(X=2) = 1/6

 P(X=3) = 1/6

 P(X=4) = 1/6

 P(X=5) = 1/6

 P(X=6) = 1/6

Variate

A variate is a generalization of the concept of a random variable that is defined without


reference to a particular type of probabilistic experiment.

It has the same properties as random variables and is denoted by capital letters (commonly X).

The possible values a random variable X can take are its range, denoted R_X. Individual
values within this range are called quantiles, and the probability of X taking a specific value x
is written as P(X=x).

Types of Random Variable

Random variables are of two types that are,

 Discrete Random Variable

 Continuous Random Variable

Discrete Random Variable


A Discrete Random Variable takes on a finite number of values. The probability function
associated with it is said to be PMF.

PMF(Probability Mass Function)

If X is a discrete random variable and the PMF of X is P(xi), then

 0 ≤ pi ≤ 1

 ∑pi = 1 where the sum is taken over all possible values of x

Discrete Random Variables Example

Example: Let S = {0, 1, 2}

xi 0 1 2

Pi(X = xi) P1 0.3 0.5

Find the value of P (X = 0)

Solution:

We know that the sum of all probabilities is equal to 1. And P (X = 0) be P1

P1 + 0.3 + 0.5 = 1

P1 = 0.2

Then, P (X = 0) is 0.2

Continuous Random Variable

Continuous Random Variable takes on an infinite number of values. The probability function
associated with it is said to be PDF (Probability Density Function).

PDF (Probability Density Function)

If X is a continuous random variable. P (x < X < x + dx) = f(x)dx then,

 0 ≤ f(x) ≤ 1; for all x

 ∫ f(x) dx = 1 over all values of x

Then P (X) is said to be a PDF of the distribution.

Continuous Random Variables Example

Find the value of P (1 < X < 2)


Such that,

 f(x) = kx3; 0 ≤ x ≤ 3 = 0

Otherwise f(x) is a density function.

Solution:

If a function f is said to be a density function, then the sum of all probabilities is equal to 1.

Since it is a continuous random variable Integral value is 1 overall sample space s.

∫ f(x) dx = 1

∫ kx3 dx = 1

K[x4]/4 = 1

Given interval, 0 ≤ x ≤ 3 = 0

K[34 – 04]/4 = 1

K(81/4) = 1

K = 4/81

Thus,

P (1 < X < 2) = k×[X4]/4

P = 4/81×[16-1]/4

P = 15/81

Random Variable Formulas

There are two main random variable formulas,

 Mean of Random Variable

 Variance of Random Variable

Let’s learn about the same in detail,

Mean of Random Variable

For any random variable X where P is its respective probability we define its mean as,

Mean(μ) = ∑ X.P

where,

 X is the random variable that consist of all possible values.

 P is the probability of respective variables


Variance of Random Variable

The variance of a random variable tells us how the random variable is spread about the mean
value of the random variable. Variance of Random Variable is calculated using the formula,

Var(x) = σ2 = E(X2) – {E(X)}2

where,

 E(X2) = ∑X2P

 E(X) = ∑XP

Random Variable Functions

For any random variable X if it assume the values x1, x2,…xn where the probability
corresponding to each random variable is P(x1), P(x2),…P(xn), then the expected value of the
variable is,

Expectation of X, E(x) = ∑ x.P(x)

Now for any new random variable Y in which the random variable X is its input, i.e. Y = f(X),
then the cumulative distribution function of Y is,

Fy(Y) = P(g(X) ≤ y)

Probability Distribution and Random Variable

For a random variable its probability distribution is calculated using three methods,

 Theoretical listing of outcomes and probabilities of the outcomes.

 Experimental listing of outcomes followed with their observed relative frequencies.

 Subjective listing of outcomes followed with their subjective probabilities.

Probability of a random variable X that takes values x is defined using a probability function of
X that is denoted by f (x) = f (X = x).

There are various probability distributions that are,

 Binomial Distribution

 Poisson Distribution

 Bernoulli’s Distribution

 Exponential Distribution

 Normal Distribution

Also Check,
 Probability Distribution Function

 Expected Value

 Variance and Standard Deviation

Random Variable Example with Solutions

Here are some of the solved examples on Random variable. Learn random variables by
practicing these solved examples.

Example 1

Find the mean value for the continuous random variable, f(x) = x2, 1 ≤ x ≤ 3

Solution:

Given,

f(x) = x2

1≤x≤3

E(x) = ∫31 x.f(x)dx

E(x) = ∫31 x.x2.dx

E(x) = ∫31 x3.dx

E(x) = [x4/4]31

E(x) = 1/4{34– 14} = 1/4{81 – 1}

E(x) = 1/4{80} = 20

Example 2

Find the mean value for the continuous random variable, f(x) = ex, 1 ≤ x ≤ 3

Solution:

Given,

f(x) = ex

1≤x≤3

E(x) = ∫31 x.f(x)dx

E(x) = ∫31 x.ex.dx

E(x) = [x.ex – ex]31

E(x) = [ex(x – 1)]31


E(x) = e3(2) – e(0)

E(x) = 2e3

Practice Problems on Random Variable

Practice random variables by solving these practice questions on random variable.

P1. Find the mean value for the continuous random variable, f(x) = 3x3, 0 ≤ x ≤ 9

P2. Find the mean value for the continuous random variable, f(x) = x + sin x, 0 ≤ x ≤ π/4

P3. Find the variance value for the continuous random variable, f(x) = 2ex +x, -2 ≤ x ≤ 2

P4. Find the variance value for the continuous random variable, f(x) = 5 + x.tanx, -π/4 ≤ x
≤ π/4

Frequently Asked Questions on Random Variable

What is a Random Variable?

A random variable in statistics are the variables that represent all the possible outcome of a
Random Variable.

What are Two Types of Random Variable?

There are two types of Random Variables and that are,

 Continuous Random Variable

 Discrete Random Variable

What is Mean of a Random Variable?

Mean of Random Variable is calculated using the formula,

 Mean of a Discrete Random Variable: E[X] = ∑x.P(X = x)

 Mean of a Continuous Random Variable: E[X] = ∫ x.f(x).dx

What is Variance of a Random Variable?

Variance of Random Variable is calculated using the formula,

 Variance of a Discrete Random Variable: V[X] = ∑(x – μ)2.P(X = x)

 Variance of a Continuous Random Variable: V[X] = ∫ (x – μ)2.f(x).dx

What is Random Variables Expected Value?

Expected value of a Random Variable is the weighted average of all possible values of the
variable. Weight of the random variable is the probability of random variable at specific
values.
What are Continuous Random Variables?

Continuous Random Variables are type of random variable in probability theory and statistics
that are used to represent the continuous probability of the distribution of a function.

Binomial Random Variables

In this post, we’ll discuss Binomial Random Variables.


Prerequisite : Random Variables
A specific type of discrete random variable that counts how often a particular event occurs in a
fixed number of tries or trials.
For a variable to be a binomial random variable, ALL of the following conditions must be
met:

1. There are a fixed number of trials (a fixed sample size).

2. On each trial, the event of interest either occurs or does not.

3. The probability of occurrence (or not) is the same on each trial.

4. Trials are independent of one another.

Mathematical Notations

n = number of trials

p = probability of success in each trial

k = number of success in n trials

Now we try to find out the probability of k success in n trials.


Here the probability of success in each trial is p independent of other trials.
So we first choose k trials in which there will be a success and in rest n-k trials there will be a
failure. Number of ways to do so is

Since all n events are independent, hence the probability of k success in n trials is equivalent to
multiplication of probability for each trial.
Here its k success and n-k failures, So probability for each way to achieve k success and n-k
failure is
Hence final probability is

(number of ways to achieve k success

and n-k failures)

(probability for each way to achieve k

success and n-k failure)

Then Binomial Random Variable Probability is given by:

Let X be a binomial random variable with the number of trials n and probability of success in
each trial be p.
Expected number of success is given by

E[X] = np

Variance of number of success is given by

Var[X] = np(1-p)

Example 1 : Consider a random experiment in which a biased coin (probability of head = 1/3)
is thrown for 10 times. Find the probability that the number of heads appearing will be 5.
Solution :

Let X be binomial random variable

with n = 10 and p = 1/3

P(X=5) = ?
Here is the implementation for the same

 C++

 Java

 Python3

 C#

 PHP

 Javascript

// C++ program to compute Binomial Probability

#include <iostream>

#include <cmath>

using namespace std;

// function to calculate nCr i.e., number of

// ways to choose r out of n objects

int nCr(int n, int r)

// Since nCr is same as nC(n-r)

// To decrease number of iterations

if (r > n / 2)

r = n - r;

int answer = 1;

for (int i = 1; i <= r; i++) {


answer *= (n - r + i);

answer /= i;

return answer;

// function to calculate binomial r.v. probability

float binomialProbability(int n, int k, float p)

return nCr(n, k) * pow(p, k) *

pow(1 - p, n - k);

// Driver code

int main()

int n = 10;

int k = 5;

float p = 1.0 / 3;

float probability = binomialProbability(n, k, p);

cout << "Probability of " << k;

cout << " heads when a coin is tossed " << n;

cout << " times where probability of each head is " << p << endl;
cout << " is = " << probability << endl;

Output:

Probability of 5 heads when a coin is tossed 10 times where probability of each head is
0.333333

is = 0.136565

Randomized Algorithms | Set 0 (Mathematical Background)


Conditional Probability Conditional probability P(A | B) indicates the probability of even ‘A’
happening given that the even B happened.

We can easily understand above formula using below diagram. Since B has already happened,
the sample space reduces to B. So the probability of A happening becomes P(A ? B) divided by
P(B)
Below is Bayes’s formula for conditional probability.

The formula provides relationship between P(A|B) and P(B|A). It is mainly derived from
conditional probability formula discussed in the previous post.

Consider the below formulas for conditional probabilities P(A|B) and P(B|A)

Since P(B ? A) = P(A ? B), we can replace P(A ? B) in first formula with P(B|A)P(A)
After replacing, we get the given formula. Refer this for examples of Bayes’s formula.

Random Variables:
A random variable is actually a function that maps outcome of a random event (like coin toss) to
a real value.

Example :

Coin tossing game :

A player pays 50 bucks if result of coin

toss is "Head"

The person gets 50 bucks if the result is

Tail.

A random variable profit for person can

be defined as below :

Profit = +50 if Head

-50 if Tail

Generally gambling games are not fair for players,

the organizer takes a share of profit for all

arrangements. So expected profit is negative for

a player in gambling and positive for the organizer.

That is how organizers make money.

Expected Value of Random Variable :


Expected value of a random variable R can be defined as following

E[R] = r1*p1 + r2*p2 + ... rk*pk

ri ==> Value of R with probability pi

Expected value is basically sum of product of following two terms (for all possible events)
a) Probability of an event.
b) Value of R at that even
Example 1:

In above example of coin toss,

Expected value of profit = 50 * (1/2) +

(-50) * (1/2)

=0

Example 2:

Expected value of six faced dice throw is

= 1*(1/6) + 2*(1/6) + .... + 6*(1/6)

= 3.5

Linearity of Expectation:
Let R1 and R2 be two discrete random variables on some probability space, then

E[R1 + R2] = E[R1] + E[R2]

For example, expected value of sum for 3 dice throws is = 3 * 7/2 = 7

Refer this for more detailed explanation and examples.

Expected Number of Trials until Success


If probability of success is p in every trial, then expected number of trials until success is 1/p. For
example, consider 6 faced fair dice is thrown until a ‘5’ is seen as result of dice throw. The
expected number of throws before seeing a 5 is 6. Note that 1/6 is probability of getting a 5 in
every trial. So number of trials is 1/(1/6) = 6.
As another example, consider a QuickSort version that keeps on looking for pivots until one of
the middle n/2 elements is picked. The expected time number of trials for finding middle pivot
would be 2 as probability of picking one of the middle n/2 elements is 1/2. This example is
discussed in more detail in Set 1.
Refer this for more detailed explanation and examples.

Randomized Algorithms | Set 1 (Introduction and Analysis)



What is a Randomized Algorithm?

An algorithm that uses random numbers to decide what to do next anywhere in its logic is called
a Randomized Algorithm. For example, in Randomized Quick Sort, we use a random number to
pick the next pivot (or we randomly shuffle the array). And in Karger’s algorithm, we randomly
pick an edge.

How to analyse Randomized Algorithms?

Some randomized algorithms have deterministic time complexity. For


example, this implementation of Karger’s algorithm has time complexity is O(E). Such
algorithms are called Monte Carlo Algorithms and are easier to analyse for worst case.
On the other hand, time complexity of other randomized algorithms (other than Las Vegas) is
dependent on value of random variable. Such Randomized algorithms are called Las Vegas
Algorithms. These algorithms are typically analysed for expected worst case. To compute
expected time taken in worst case, all possible values of the used random variable needs to be
considered in worst case and time taken by every possible value needs to be evaluated. Average
of all evaluated times is the expected worst case time complexity. Below facts are generally
helpful in analysis os such algorithms.
Linearity of Expectation
Expected Number of Trials until Success.
For example consider below a randomized version of QuickSort.
A Central Pivot is a pivot that divides the array in such a way that one side has at-least 1/4
elements.

// Sorts an array arr[low..high]

randQuickSort(arr[], low, high)

1. If low >= high, then EXIT.

2. While pivot 'x' is not a Central Pivot.

(i) Choose uniformly at random a number from [low..high].

Let the randomly picked number number be x.

(ii) Count elements in arr[low..high] that are smaller


than arr[x]. Let this count be sc.

(iii) Count elements in arr[low..high] that are greater

than arr[x]. Let this count be gc.

(iv) Let n = (high-low+1). If sc >= n/4 and

gc >= n/4, then x is a central pivot.

3. Partition arr[low..high] around the pivot x.

4. // Recur for smaller elements

randQuickSort(arr, low, sc-1)

5. // Recur for greater elements

randQuickSort(arr, high-gc+1, high)

The important thing in our analysis is, time taken by step 2 is O(n).
How many times while loop runs before finding a central pivot?
The probability that the randomly chosen element is central pivot is 1/n.
Therefore, expected number of times the while loop runs is n (See this for details)
Thus, the expected time complexity of step 2 is O(n).
What is overall Time Complexity in Worst Case?
In worst case, each partition divides array such that one side has n/4 elements and other side has
3n/4 elements. The worst case height of recursion tree is Log 3/4 n which is O(Log n).

T(n) < T(n/4) + T(3n/4) + O(n)

T(n) < 2T(3n/4) + O(n)

Solution of above recurrence is O(n Log n)

Note that the above randomized algorithm is not the best way to implement randomized Quick
Sort. The idea here is to simplify the analysis as it is simple to analyse.
Typically, randomized Quick Sort is implemented by randomly picking a pivot (no loop). Or by
shuffling array elements. Expected worst case time complexity of this algorithm is also O(n Log
n), but analysis is complex, the MIT prof himself mentions same in his lecture here.
Example :
C

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int find_solution(int n) {

// seed the random number generator with the current time

srand(time(0));

// randomly select a number between 1 and n and return it as the solution

return rand() % n + 1;

int main() {

int n = 10; // the range of possible solutions is 1 to n

printf("Solution: %d\n", find_solution(n));

return 0;

C++

#include <iostream>

#include <stdlib.h>

#include <time.h>

int find_solution(int n) {

// seed the random number generator with the current time

srand(time(0));
// randomly select a number between 1 and n and return it as the solution

return rand() % n + 1;

int main() {

int n = 10; // the range of possible solutions is 1 to n

std::cout << "Solution: " << find_solution(n) << std::endl;

return 0;

Java

import java.util.Random;

public class Solution {

// method to find a random solution within a given range

public static int findSolution(int n) {

// create a Random object to generate random numbers

Random rand = new Random();

// use the nextInt method to generate a random number between 0 and n-1

int solution = rand.nextInt(n) + 1;

return solution;

public static void main(String[] args) {

int n = 10; // the range of possible solutions is 1 to n

System.out.println("Solution: " + findSolution(n));


}

Python3

import random

import time

def find_solution(n):

# seed the random number generator with the current time

random.seed(time.time())

# randomly select a number between 1 and n and return it as the solution

return random.randint(1, n)

def main():

n = 10 # the range of possible solutions is 1 to n

print("Solution:", find_solution(n))

if __name__ == '__main__':

main()

Javascript

function findSolution(n) {

// seed the random number generator with the current time

const random = Math.random() * (new Date().getTime());


// randomly select a number between 1 and n and return it as the solution

return Math.floor((random % n) + 1);

function main() {

const n = 10; // the range of possible solutions is 1 to n

console.log(`Solution: ${findSolution(n)}`);

main();

C#

// C# equivalent

using System;

public class Solution

// method to find a random solution within a given range

public static int findSolution(int n)

// create a Random object to generate random numbers

Random rand = new Random();

// use the Next method to generate a random number between 0 and n-1

int solution = rand.Next(n) + 1;

return solution;

}
public static void Main(string[] args)

int n = 10; // the range of possible solutions is 1 to n

Console.WriteLine("Solution: " + findSolution(n));

Output

Solution: 10

Randomized Algorithms | Set 2 (Classification and Applications)

Randomized algorithms are classified in two categories.

Las Vegas:

A Las Vegas algorithm were introduced by Laszlo Babai in 1979.

A Las Vegas algorithm is an algorithm which uses randomness, but gives guarantees that the
solution obtained for given problem is correct. It takes the risk with resources used. A quick-
sort algorithm is a simple example of Las-Vegas algorithm. To sort the given array of n
numbers quickly we use the quick sort algorithm. For that we find out central element which is
also called as pivot element and each element is compared with this pivot element. Sorting is
done in less time or it requires more time is dependent on how we select the pivot element. To
pick the pivot element randomly we can use Las-Vegas algorithm.

Definition:

A randomized algorithm that always produce correct result with only variation from one aun to
another being its running time is known as Las-Vegas algorithm.

OR

A randomized algorithm which always produces a correct result or it informs about the failure
is known as Las-Vegas algorithm.

OR
A Las-Vegas algorithm take the risk with the resources used for computation but it does not
take risk with the result i.e. it gives correct and expected output for the given problem.

Let us consider the above example of quick sort algorithm. In this algorithm we choose the
pivot element randomly. But the result of this problem is always a sorted array. A Las-Vegas
algorithm is having one restriction i.e. the solution for the given problem can be found out in
finite time. In this algorithm the numbers of possible solutions arc limited. The actual solution
is complex in nature or complicated to calculate but it is easy to verify the correctness of
candidate solution.

These algorithms always produce correct or optimum result. Time complexity of these
algorithms is based on a random value and time complexity is evaluated as expected value. For
example, Randomized Quick Sort always sorts an input array and expected worst case time
complexity of Quick Sort is O(nLogn).

Relation with the Monte-Carlo Algorithms:

 The Las-Vegas algorithm can be differentiated with the Monte-carlo algorithms in which
the resources used to find out the solution are bounded but it does not give guarantee that
the solution obtained is accurate.

 In some applications by making early termination a Las-Vegas algorithm can be converted


into Monte-Carlo algorithm.

Complexity Analysis:

The complexity class of given problem which is solved by using a Las-Vegas algorithms is
expect that the given problem is solved with zero error probability and in polynomial time.

This zero error probability polynomial time is also called as ZPP which is obtained as follows,

ZPP = RP ? CO-RP

Where, RP = Randomized polynomial time.

Randomized polynomial time algorithm always provide correct output when the correct output
is no, but with a certain probability bounded away from one when the answer is yes. These
kinds of decision problem can be included in class RP i.e. randomized where polynomial time.

That is how we can solve given problem in expected polynomial time by using Las-Vegas
algorithm. Generally there is no upper bound for Las-vegas algorithm related to worst case run
time.

Monte Carlo:
The computational algorithms which rely on repeated random sampling to compute their
results such algorithm are called as Monte-Carlo algorithms.

OR

The random algorithm is Monte-carlo algorithms if it can give the wrong answer sometimes.

Whenever the existing deterministic algorithm is fail or it is impossible to compute the solution
for given problem then Monte-Carlo algorithms or methods are used. Monte-carlo methods are
best repeated computation of the random numbers, and that’s why these algorithms are used
for solving physical simulation system and mathematical system.

This Monte-carlo algorithms are specially useful for disordered materials, fluids, cellular
structures. In case of mathematics these method are used to calculate the definite integrals,
these integrals are provided with the complicated boundary conditions for multidimensional
integrals. This method is successive one with consideration of risk analysis when compared to
other methods.

There is no single Monte carlo methods other than the term describes a large and widely used
class approaches and these approach use the following pattern.

1. Possible inputs of domain is defined.

2. By using a certain specified probability distribution generate the inputs randomly from the
domain.

3. By using these inputs perform a deterministic computation.


4.The final result can be computed by aggregating the results of the individual computation.

Produce correct or optimum result with some probability. These algorithms have deterministic
running time and it is generally easier to find out worst case time complexity. For example this
implementation of Karger’s Algorithm produces minimum cut with probability greater than or
equal to 1/n2 (n is number of vertices) and has worst case time complexity as O(E). Another
example is Fermat Method for Primality Testing. Example to Understand Classification:

Consider a binary array where exactly half elements are 0

and half are 1. The task is to find index of any 1.

A Las Vegas algorithm for this task is to keep picking a random element until we find a 1. A
Monte Carlo algorithm for the same is to keep picking a random element until we either find 1
or we have tried maximum allowed times say k. The Las Vegas algorithm always finds an
index of 1, but time complexity is determined as expect value. The expected number of trials
before success is 2, therefore expected time complexity is O(1). The Monte Carlo Algorithm
finds a 1 with probability [1 – (1/2) k]. Time complexity of Monte Carlo is O(k) which is
deterministic

Optimization of Monte-Carlo Algorithms:

 In general the Monte-carlo optimization techniques are dependent on the random walks.
The program for Monte carlo algorithms move in multidimensional space around the
generated marker or handle. It wanted to move to the lower function but sometimes moves
against the gradient.

 In numerical optimization the numerical simulation is used which effective and efficient
and popular application for the random numbers. The travelling salesman problem is an
optimization problem which is one of the best examples of optimizations.

 There are various optimization techniques available for Monte-carlo algorithms such as
Evolution strategy, Genetic algorithms, parallel tempering etc.

Applications and Scope:

The Monte-carlo methods has wider range of applications. It uses in various areas like physical
science, Design and visuals, Finance and business, Telecommunication etc. In general Monte
carlo methods are used in mathematics. By generating random numbers we can solve the
various problem. The problems which are complex in nature or difficult to solve are solved by
using Monte-carlo algorithms. Monte carlo integration is the most common application of
Monte-carlo algorithm.

The deterministic algorithm provides a correct solution but it takes long time or its runtime is
large. This run-time can be improved by using the Monte carlo integration algorithms. There
are various methods used for integration by using Monte-carlo methods such as,

i) Direct sampling methods which includes the stratified sampling, recursive


stratified sampling, importance sampling.

ii) Random walk Monte-carlo algorithm which is used to find out the integration for
given problem.
iii) Gibbs sampling.

 Consider a tool that basically does sorting. Let the tool be used by many users and there are
few users who always use tool for already sorted array. If the tool uses simple (not
randomized) QuickSort, then those few users are always going to face worst case situation.
On the other hand if the tool uses Randomized QuickSort, then there is no user that always
gets worst case. Everybody gets expected O(n Log n) time.

 Randomized algorithms have huge applications in Cryptography.


 Load Balancing.

 Number-Theoretic Applications: Primality Testing

 Data Structures: Hashing, Sorting, Searching, Order Statistics and Computational


Geometry.

 Algebraic identities: Polynomial and matrix identity verification. Interactive proof systems.

 Mathematical programming: Faster algorithms for linear programming, Rounding linear


program solutions to integer program solutions

 Graph algorithms: Minimum spanning trees, shortest paths, minimum cuts.

 Counting and enumeration: Matrix permanent Counting combinatorial structures.

 Parallel and distributed computing: Deadlock avoidance distributed consensus.

 Probabilistic existence proofs: Show that a combinatorial object arises with non-zero
probability among objects drawn from a suitable probability space.

 Derandomization: First devise a randomized algorithm then argue that it can be


derandomized to yield a deterministic algorithm.

Randomized algorithms are algorithms that use randomness as a key component in their
operation. They can be used to solve a wide variety of problems, including optimization,
search, and decision-making. Some examples of applications of randomized algorithms
include:

1. Monte Carlo methods: These are a class of randomized algorithms that use random
sampling to solve problems that may be deterministic in principle, but are too complex to
solve exactly. Examples include estimating pi, simulating physical systems, and solving
optimization problems.

2. Randomized search algorithms: These are algorithms that use randomness to search for
solutions to problems. Examples include genetic algorithms and simulated annealing.

3. Randomized data structures: These are data structures that use randomness to improve their
performance. Examples include skip lists and hash tables.

4. Randomized load balancing: These are algorithms used to distribute load across a network
of computers, using randomness to avoid overloading any one computer.

5. Randomized encryption: These are algorithms used to encrypt and decrypt data, using
randomness to make it difficult for an attacker to decrypt the data without the correct key.

Example 1:
 C++

 Java

 Python

 C#

 Javascript

#include <iostream>

#include <algorithm>

#include <random>

// Generates a random permutation of the given array

void random_permutation(int* array, int size) {

// Create a random number generator

std::mt19937 rng(std::random_device{}());

// Shuffle the array using the random number generator

std::shuffle(array, array + size, rng);

int main() {

int array[] = {1, 2, 3, 4, 5};

int size = 5;

// Generate a random permutation of the array

random_permutation(array, size);

// Print the shuffled array


for (int i = 0; i < size; i++) {

std::cout << array[i] << " ";

std::cout << std::endl;

return 0;

Output

51423

Example 2 :

 C++

 Java

 Python3

 C#

 Javascript

#include <algorithm>

#include <iostream>

#include <random>

#include <vector>

// Find the median of a list of numbers

int find_median(std::vector<int> numbers)

int n = numbers.size();

if (n == 0) {
return -1;

if (n == 1) {

return numbers[0];

// Shuffle the list to ensure a random ordering

std::random_device rd;

std::mt19937 g(rd());

std::shuffle(numbers.begin(), numbers.end(), g);

// Find the median by selecting the middle element

return numbers[n / 2];

int main()

std::vector<int> numbers1 = { 1, 2, 3, 4, 5 };

std::vector<int> numbers2 = { 1, 2, 3, 4, 5, 6 };

std::vector<int> numbers3 = {};

std::vector<int> numbers4 = { 7 };

// Example usage

std::cout << find_median(numbers1)

<< std::endl; // Output: 3

std::cout

<< find_median(numbers2)
<< std::endl; // Output: 3 or 4 (randomly chosen)

std::cout << find_median(numbers3)

<< std::endl; // Output: -1

std::cout << find_median(numbers4)

<< std::endl; // Output: 7

return 0;

Learn Data Structures & Algorithms with GeeksforGeeks

Output

None

Randomized Algorithms | Set 3 (1/2 Approximate Median)


We strongly recommend to refer below articles as a prerequisite of this.

Randomized Algorithms | Set 1 (Introduction and Analysis)


Randomized Algorithms | Set 2 (Classification and Applications)

In this post, a Monte Carlo algorithm is discussed.

Problem Statement : Given an unsorted array A[] of n numbers and ε > 0, compute an element
whose rank (position in sorted A[]) is in the range [(1 – ε)n/2, (1 + ε)n/2].
For ½ Approximate Median Algorithm &epsilom; is 1/2 => rank should be in the range [n/4,
3n/4]

We can find k’th smallest element in O(n) expected time and O(n) worst case time.
What if we want in less than O(n) time with low probable error allowed?
Following steps represent an algorithm that is O((Log n) x (Log Log n)) time and produces
incorrect result with probability less than or equal to 2/n2.

1. Randomly choose k elements from the array where k=c log n (c is some constant)

2. Insert then into a set.

3. Sort elements of the set.

4. Return median of the set i.e. (k/2)th element from the set

o C

o Java

/* C++ program to find Approximate Median using

1/2 Approximate Algorithm */

#include<bits/stdc++.h>

using namespace std;

// This function returns the Approximate Median

int randApproxMedian(int arr[],int n)

// Declaration for the random number generator

random_device rand_dev;

mt19937 generator(rand_dev());

// Random number generated will be in the range [0,n-1]

uniform_int_distribution<int> distribution(0, n-1);

if (n==0)

return 0;
int k = 10*log2(n); // Taking c as 10

// A set stores unique elements in sorted order

set<int> s;

for (int i=0; i<k; i++)

// Generating a random index

int index = distribution(generator);

//Inserting into the set

s.insert(arr[index]);

set<int> ::iterator itr = s.begin();

// Report the median of the set at k/2 position

// Move the itr to k/2th position

advance(itr, (s.size()/2) - 1);

// Return the median

return *itr;

// Driver method to test above method

int main()

int arr[] = {1, 3, 2, 4, 5, 6, 8, 7};


int n = sizeof(arr)/sizeof(int);

printf("Approximate Median is %d\n",randApproxMedian(arr,n));

return 0

Time Complexity:
We use a set provided by the STL in C++. In STL Set, insertion for each element takes O(log
k). So for k insertions, time taken is O (k log k).
Now replacing k with c log n
=>O(c log n (log (clog n))) =>O (log n (log log n))

How is probability of error less than 2/n 2?


Algorithm makes an error if the set S has at least k/2 elements are from the Left Quarter or
Right Quarter.

It is quite easy to visualize this statement since the median which we report will be (k/2)th
element and if we take k/2 elements from the left quarter(or right quarter) the median will be
from the left quarter (or the right quarter).

An array can be divided into 4 quarters each of size n/4. So P(selecting left quarter) is 1/4. So
what is the probability that at least k/2 elements are from the Left Quarter or Right Quarter?
This probability problem is same as below :

Given a coin which gives HEADS with probability 1/4 and TAILS with 3/4. The coin is tossed
k times. What is the probability that we get at least k/2 HEADS is less than or equal to?
Explanation:

If we put k = c log n for c = 10, we get

P <= (1/2)2log n

P <= (1/2)log n2

P <= n-2

Probability of selecting at least k/2 elements from the left quarter) <= 1/n 2
Probability of selecting at least k/2 elements from the left or right quarter) <= 2/n 2

Therefore algorithm produces incorrect result with probability less than or equal to 2/n 2.

You might also like