Misc Randomized Algs
Tags | AlgorithmsCS161 |
---|
Colinear points
This one has both a non-random and a random solution
Nonrandom solution
- Get all pairwise points
- Sort these pairwise points lexographically (in order of , and in order of as a tiebreaker. Comparisons are done ad-hoc). This is .
- Find the longest run. This is , but it is overshadowed by the sorting so the final complexity is
Random solution
If we knew that the length of the longest run is , then this is what we do:
- pick a random pair of points, compute .
- For all other points, see if they lie on this line formed by the pair. This takes time
- If the ending count is , then return the set. Otherwise, pick another random pair of points.
The worst case runtime is , but what about expected runtime? Well, how likely are we to pick the right set of points? There are points in the longest line, so the chance of us picking a pair of points is
We can simplify into
Therefore, the probability of getting a pair within the longest run is within . Now, if we treat it as a goemtric random variable, it means it will take around tries to get this pair. As such, our final expected compelxity is , or .