A curated collection of elegant algorithms.
Here you’ll find cool algorithms that offer fascinating solutions to computational problems, complete with the math behind them, plus pseudocode and code implementations.
Suppose we want to compute an integral, which doesn't have a closed-form answer. Then we can use a numerical method such as Monte Carlo simulation, which uses random sampling to approximate the value. A major advantage of this method is that the convergence rate does not worsen exponentially with increased dimensions.
Here, we are looking at the most basic form of Monte Carlo integration with uniform sampling, using the two-dimensional space ℝ2
. This approach can be easily generalized to higher dimensions.
The key idea is to bound the desired integral (an area in ℝ2
) within a region whose area is easy to compute.
- Treat the bounding region as the sample space
- Sample
N
points uniformly within the sample space - Count the fraction
r
of points that fall inside the desired integral's region - Compute the desired area/integral using the formula:
Approximation = r * (area of bounding region)
By the law of large numbers, the computed value converges to the true value as N
increases. The error decreases proportionally to 1 / sqrt(N)
, which can be derived easily.
- BEGIN
- REPEAT
- Generate N random points in the bounding region
- Count the fraction
r
of points inside the target region - Compute the integral approximation:
r * (area of bounding region)
- UNTIL error is within error bars
- OUTPUT the result
- END
Approximate π
using this method.
Hint: Consider the function:
f(x, y) = 1, if x2 + y2 ≤ r2 0, otherwise
The desired area (of the quarter-circle) is proportional to π r2
. Use a square of side 2r
as the bounding region.