Randomized algorithms are computational methods that make random choices during execution to achieve improved performance, simplicity, or probability guarantees. Unlike deterministic algorithms, which follow a strict sequence of operations, randomized algorithms use random numbers or coin flips to guide their decisions. This incorporation of randomness allows them to handle complex problems more efficiently, particularly in cases where deterministic solutions are too slow or difficult to design.
Key Concepts
At the heart of a randomized algorithm is the use of a random source, often a pseudo-random number generator. The algorithm's behavior can vary from one run to another even on the same input. However, it is designed in such a way that, on average or with high probability, it produces the correct or near-optimal solution.
Randomized algorithms fall into two primary categories:
Las Vegas Algorithms: These always produce the correct result, but their runtime may vary depending on the random choices made. The randomness only affects efficiency, not correctness. An example is Randomized QuickSort, where the pivot is chosen randomly to avoid worst-case scenarios.
Monte Carlo Algorithms: These may produce an incorrect result with a small probability, but they generally run faster or are simpler than deterministic counterparts. An example is the Miller-Rabin primality test, which determines whether a number is prime with high probability.