Randomized Algorithms
Discover how utilizing random numbers within an algorithm can significantly improve average-case time complexity, with a deep dive into Randomized Quick Sort.
Learning Goals
- Define Randomized Algorithms and explain their benefits.
- Compare Las Vegas and Monte Carlo algorithms.
- Trace the Randomized Quick Sort algorithm and analyze its time complexity.
What are Randomized Algorithms?
(Answers 2024 Q9a i & 2024 Q1j)
A Randomized Algorithm is an algorithm that employs a degree of randomness as part of its logic. It makes random choices during execution (such as picking a random pivot, or rolling a digital die) to decide what step to take next.
Unlike Deterministic algorithms (which always follow the exact same path for a given input), a randomized algorithm might take a completely different path every time you run it on the exact same input! We use them because they are often simpler to write and yield much faster average-case time complexities.
Two Classes of Randomized Algorithms:
| Class | Accuracy | Time Complexity | Example |
|---|---|---|---|
| Las Vegas | 100% Guaranteed Correct answer every time. | Running time varies randomly. Could be fast, could be slow. | Randomized Quick Sort |
| Monte Carlo | Might give the Wrong Answer with a small probability. | Running time is strictly fixed/deterministic. | Miller-Rabin Primality Test |
Trace: Randomized Quick Sort
- 1Step 1
In standard Deterministic Quick Sort, we usually pick the first or last element as the Pivot. If the array is already sorted (e.g.,
[1, 2, 3, 4, 5]), picking the last element means we split the array into size and every single time, triggering the worst-case time complexity. - 2Step 2
Instead of deterministically picking the last element, we generate a random number between and , and swap that random element with the last element. Then we proceed with standard partitioning. This guarantees that no specific input (like a sorted array) can force the worst-case scenario.
- 3Step 3
1Algorithm Randomized_QuickSort(A, p, r): 2 if p < r: 3 // Step 1: Find a random pivot and partition 4 q = Randomized_Partition(A, p, r) 5 6 // Step 2: Recurse on left side 7 Randomized_QuickSort(A, p, q - 1) 8 9 // Step 3: Recurse on right side 10 Randomized_QuickSort(A, q + 1, r) 11 12Algorithm Randomized_Partition(A, p, r): 13 i = Random(p, r) // Pick a random index 14 Swap(A[i], A[r]) // Swap it to the end 15 return Partition(A, p, r) // Standard partition - 4Step 4
Expected (Average) Case: Because the pivot is chosen uniformly at random, it will theoretically split the array roughly down the middle (like 25%-75% or 50%-50%) most of the time. This yields a recursion tree of height , giving an expected time complexity of .
Worst Case: If you are incredibly unlucky, the random number generator might pick the absolute largest element as the pivot every single time. The worst-case is technically still , but the probability of this happening on a large array is astronomically close to zero.
Knowledge Check
Randomized algorithms make use of: