The Wayback Machine - https://web.archive.org/web/20240930235709/https://www.geeksforgeeks.org/randomized-algorithms-set-0-mathematical-background/
Open In App

Randomized Algorithms | Set 0 (Mathematical Background)

Last Updated : 07 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

P(A|B) = \frac{P(A\cap B)}{P(B)}

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) 

conditional_probab

Below is Bayes’s formula for conditional probability. 

P(A|B) = \frac{P(B|A)P(A)}{P(B)}

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) 

P(A|B) = \frac{P(A\cap B)}{P(B)} ---1

P(B|A) = \frac{P(B\cap A)}{P(A)} ---2

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. 

More on Randomized Algorithms: 

All Randomized Algorithm Topics 


 


Previous Article
Next Article

Similar Reads

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 Randomize
6 min read
Randomized Algorithms | Set 2 (Classification and Applications)
We strongly recommend to refer below post as a prerequisite of this. Randomized Algorithms | Set 1 (Introduction and Analysis) Classification 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 guarant
13 min read
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 w
4 min read
Importance of Randomized Algorithms
Introduction: Randomization is an important concept, and hence randomization algorithms are used in a variety of fields, such as number theory, computational geometry, graph theory, and distributed computing.The inputs for a randomized algorithm are similar to those of deterministic algorithms, along with a sequence of random bits that can be used
4 min read
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 soluti
2 min read
Disjoint Set Union (Randomized Algorithm)
A Disjoint set union is an algorithm that is used to manage a collection of disjoint sets. A disjoint set is a set in which the elements are not in any other set. Also, known as union-find or merge-find. The disjoint set union algorithm allows you to perform the following operations efficiently: Find: Determine which set a given element belongs to.
15+ min read
Count of array elements that can be found using Randomized Binary Search on every array element
Given an array arr[] of size N, the task is to find the minimum count of array elements found by applying the Randomized Binary Search for each array elements. Examples: Input: arr[] = { 5, 4, 9 } Output: 2 Explanation: Applying Randomized Binary Search for arr[0] in the array. Initially, search space is [0, 2] Suppose pivot = 1 and arr[pivot] <
7 min read
Load Balancing on Servers (Randomized Algorithm)
Consider a high traffic website that receives millions of requests (of different types) per five minutes, the site has k (for example n = 1000) servers to process the requests. How should the load be balanced among servers? The solutions that we generally think of are a) Round Robin b) Assign new request to a server that has minimum load. Both of t
3 min read
Treap (A Randomized Binary Search Tree)
Like Red-Black and AVL Trees, Treap is a Balanced Binary Search Tree, but not guaranteed to have height as O(Log n). The idea is to use Randomization and Binary Heap property to maintain balance with high probability. The expected time complexity of search, insert and delete is O(Log n). Every node of Treap maintains two values. 1) Key Follows stan
2 min read
Randomized Binary Search Algorithm
We are given a sorted array A[] of n elements. We need to find if x is present in A or not.In binary search we always used middle element, here we will randomly pick one element in given range.In Binary Search we had middle = (start + end)/2 In Randomized binary search we do following Generate a random number t Since range of number in which we wan
13 min read
Randomized Algorithm in Python
Randomized algorithms are algorithms that use randomness as part of their logic to achieve efficiency and simplicity in solving computational problems. Unlike deterministic algorithms, which produce the same output for a given input, randomized algorithms may produce different outputs for the same input due to the involvement of randomness. These a
7 min read
Mathematical Algorithms - Divisibility and Large Numbers
Divisibility is about whether one number can be divided by another number without leaving any remainder. This is an important concept in math. In this article, we'll look at the different ways to test if a number is divisible by another number. We'll explain the techniques used to quickly figure out if a number can be divided evenly, without any le
6 min read
Mathematical Algorithms - Sequence & Series
Mathematical algorithms are step-by-step procedures used to solve math problems. This article looks at sequences and series, which are important parts of these algorithms. Sequences are ordered sets of numbers, while series are the sums of these numbers. Understanding sequences and series is vital for solving complex math problems, modeling real-wo
4 min read
Mathematical Algorithms - Number Digits
"Mathematical Algorithms | Number Digits" is a guide that explores problems and solutions involving digits. It covers common digit-related issues, such as digit properties, manipulation, and counting under constraints. The article discusses algorithmic approaches like modular arithmetic, string handling, recursion, and math libraries. It also highl
9 min read
Mathematical Algorithms
Mathematical algorithms in Data Structures and Algorithms (DSA) are fundamental tools used to solve various computational problems efficiently. These algorithms leverage mathematical concepts and principles to optimize solutions for tasks such as sorting, searching, and graph traversal . By applying mathematical techniques, these algorithms aim to
6 min read
Mathematical Algorithms - Prime Factorization and Divisors
In the Mathematical algorithm there are two fundamental mathematical algorithms that is prime factorization and finding divisors. Prime factorization is the process of breaking down a number into its prime factors, while finding divisors involves identifying all the numbers that can evenly divide the original number. These concepts are very importa
3 min read
Mathematical Algorithms - Prime numbers and Primality Tests
What is Prime Number?A prime number is a natural number greater than 1 that is divisible by only 1 and the number itself. In other words, the prime number can be defined as a positive integer greater than that has exactly two factors, 1 and the number itself. First 10 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. Note: 1 is neither a prime
3 min read
Parity of the given mathematical expression using given N numbers
Given N positive integers A1, A2, ..., AN, the task is to determine the parity of the expression S. For the given N numbers, the expression S is given as: [Tex]S = 1 + \sum_{i=1}^{N}A_i + \sum_{i=1}^{N}(A_i * A_{i+1}) + \sum_{i=1}^{N}(A_i * A_{i+1} * A_{i+2}) + ...+ (A_1 * A_2 * A_3 * ... * A_N) [/Tex] Examples: Input: N = 3, A1 = 2, A2 = 3, A3 = 1
5 min read
Arrange given numbers in a mathematical expression using operators [+, -, *, /] and parentheses to get value 24
Given an array arr[] consisting of 4 integers each between [1-9], the task is to check whether it is possible to get the number 24 by placing the operators +, -, /, and * between the numbers or grouping them using parenthesis. If it is possible then print "Possible", Otherwise print "Not Possible". Examples: Input: arr[] = {3, 6, 8, 2}Output: Possi
11 min read
Check if two arrays are permutations of each other using Mathematical Operation
Given two unsorted arrays of same size where arr[i] >= 0 for all i, the task is to check if two arrays are permutations of each other or not. Examples: Input: arr1[] = {2, 1, 3, 5, 4, 3, 2} arr2[] = {3, 2, 2, 4, 5, 3, 1}Output: YesInput: arr1[] = {2, 1, 3, 5} arr2[] = {3, 2, 2, 4}Output: NoRecommended: Please try your approach on {IDE} first, be
10 min read
Tau - A Mathematical Constant
What is Tau?The constant is numerically equal to 2*pi (2 times pi), and with value approximately 6.28. The ratio equates to 2*C/D. Where C is circumference and D is diameter of circle.Applications of Tau There are many expressions that actually require "2*pi" calculation, having tau being equal to that simplifies them to great extent, for e.g Circu
4 min read
Mathematical understanding of RNN and its variants
Introduction: Experts expect Artificial Intelligence (AI) to work towards creating a better life to live. They say as more computation power will be available in the coming time i.e more graphical processing units, AI will make more advancement and productive to humans. Today, one can see a lot of such AI-powered applications like the fight against
8 min read
Data Structures and Algorithms | Set 36
Que - 1. The function shiftNode() which takes as input two linked lists- destination and source. It deletes front node from source and places it onto the front of destination. Choose the set of statements which replace X, Y, Z in given function. void shiftNode(struct node** destRoot, struct node** srcRoot) { // the front of source node struct node*
4 min read
Data Structures and Algorithms | Set 37
Que - 1. For 8 keys and 6 slots in a hashing table with uniform hashing and chaining, what is the expected number of items that hash to a particular location. (A) 2.33 (B) 0.75 (C) 1.33 (D) 2 Solution: Probability that key1 ends up in slot 1 = 1/6 Probability that key2 ends up in slot 1 = 1/6 Probability that key3 ends up in slot x = 1/6 Probabilit
4 min read
Algorithms | Recurrences | Set 1
Question 1: Which of the following is the value of T3(n) where T3(n) is defined as T3(n) = 5*T3(n-1) - 4*T3(n-2) C1*5n + C2*4n C1 + C2*4n C1*2n + C2*4n C1*5n + C2*(-4)n Answer: 2 Explanation: The recursion function (equation) seems to have a strange form. Let's change the variable T2(n) to get an equation of a familiar form; so, we let A(n) = T3(n)
5 min read
Algorithms Sample Questions | Recurrences | Set 2
Question 1: What is the complexity of T(n)? [Tex]T(n) = T(n-1) + \frac{1}{n(n-1)}[/Tex] Θ( 1⁄n ) Θ( 1⁄n2 ) Θ( 1 ) Θ( ln( n ) ) Answer: 3 Explanation: Using the substitution technique to solve the given recursive function, the closed form (non-recursive form) of T(n) can be inductively guessed as following: [Tex]T
10 min read
Algorithms Sample Questions | Set 3 | Time Order Analysis
Question 1: What is the asymptotic boundary of T(n)? [Tex] T(n) = \sum_{i=2}^{n} log_{i}n = log_{2}n + log_{3}n + \ldots + log_{n}n [/Tex] θ( n*log(n) ) θ( n2 ) θ( n ) θ( n*log2(n) ) θ( n2*log2(n) ) Answer: 3 Explanation: To find appropriate upper and lower boundaries, an approach which first comes to mind is to expand
10 min read
When to use each Sorting Algorithms | Set 2
Sorting is the process of arranging a set of data in a specific order, which may be numerical (ascending, descending) or lexicographical (alphabetical) order. Why Sorting Is Required? Sorting is very essential when there is a need to highly optimize the searching algorithm. For example, let's assume two cases for searching for a certain element.Cas
5 min read
Program for Page Replacement Algorithms | Set 2 (FIFO)
Prerequisite : Page Replacement Algorithms In operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newl
10 min read
Lower bound for comparison based sorting algorithms
The problem of sorting can be viewed as following. Input: A sequence of n numbers <a1, a2, . . . , an>. Output: A permutation (reordering) <a'1, a'2, . . . , a'n> of the input sequence such that a'1 <= a'2 ..... <= a'n. A sorting algorithm is comparison based if it uses comparison operators to find the order between two numbers. C
7 min read
Article Tags :