Lecture 2: Randomised Algorithms

Categories of Algorithms

Heuristic Algorithms

Randomised Algorithms

There are 2 categories of randomised algorithms:

  1. Using random numbers to find a solution to a problem
  2. Using random numbers to improve a solution to a problem

Category 1:

Las Vegas Algorithm

Problem: given an array of nn elements, find the first element which has a value equal to xx

begin 
    repeat 
        Randomly select one element a out of n elements
    until a==x
end

Monte Carlo Algorithm

For the same problem we can also use the Monte Carlo algorithm

begin 
    i := 0
    repeat 
        Randomly select one element a out of n elements 
        i := i + 1 
    until (a==x) || (i==k)
end

Monte Carlo vs Las Vegas

The Las Vegas algorithm is a randomised algorithm that always gives correct results. The running time varies from run to run.

The Monte Carlo algorithm is also a randomised algorithm whose running time is deterministic but results may be incorrect with a small probability

Differences:

Advantages of Randomised Algorithms

For a deterministic linear search algorithm, such as iterative search:

For the Las Vegas algorithm:

For the Monte Carlo algorithm:

Solving the Nuts and Bolts Problem using Randomised Algorithms

A disorganised carpenter has a collection of nn nuts of distinct sizes and nn bolts. They want to find the corresponding pairs of bolts and nuts.
Each nut matches exactly one bolt (and vice versa). They can only compare nuts to bolts (not nuts to nuts or bolts to bolts)

The brute force approach to solving this problem would be to compare each bolt to each nut with a time complexity of O(n2)O(n^2)

We can use a quicksort approach to speedup the process.

Aside

The sorting problem: given and array AA of nn numbers, sort the numbers in increasing order.

Quicksort Algorithm:

less,equal, greater := three empty arrays
if length(array) > 1 {
  pivot := select an element of array
  for each x in array {
    if x < pivot add x to less
    if x = pivot add x to equal 
    if x > pivot add x to greater
  }
}
quicksort(less)
quicksort(greater)
array := concatenate(less,equal,greater)

Randomised Quicksort Algorithm

The time complexity of the deterministic quicksort algorithm is O(nlogn)O(n\log n) on average for a random permutation array

The worst case complexity is O(n2)O(n^2), this occurs when an already sorted array is entered

For the randomised quicksort algorithm: selecting a pivot point randomly

Solving the problem using this apporach is not trivial and a proof can be found here

Applications of Randomised Algorithms

Advantages and Disadvantages of Randomised Algorithms

Advantages

Disadvantages