HN
Today

Floyd's Sampling Algorithm

This piece delves into Floyd's Sampling Algorithm, a 'magical' method for generating a uniform random k-subset that often defies easy intuition. The author unpacks its mechanics through two distinct perspectives, revealing its elegance and connection to other classic algorithms. It's a prime example of the kind of clever, foundational computer science that Hacker News revels in.

11
Score
0
Comments
#13
Highest Rank
3h
on Front Page
First Seen
Apr 12, 4:00 PM
Last Seen
Apr 12, 6:00 PM
Rank Over Time
131826

The Lowdown

Floyd's Sampling Algorithm is presented as a particularly elegant, yet initially perplexing, method for selecting k distinct integers uniformly at random from a set of n integers. The author, Justin Jaffray, shares the algorithm's pseudocode and describes how he initially encountered its 'coolness' via a coworker's comment.

  • The algorithm's core function is to generate a uniform random k-subset from the set {1, 2, ..., n}.
  • Its structure, particularly the conditional branch, makes immediate combinatorial or visual intuition challenging.
  • First Perspective: This view establishes the algorithm's uniform distribution by showing that each iteration transforms a k-subset of [1..n] and an element of [1..n+1] into a uniform (k+1)-subset of [1..n+1]. This is demonstrated by proving a (k+1)-to-1 mapping for all possible outputs.
  • Second Perspective: This perspective, attributed to Paul, links Floyd's algorithm to the Fisher-Yates shuffle's 'upward' variation. It posits that Floyd's algorithm directly performs the final k swaps of a Fisher-Yates shuffle, effectively plucking the last k elements to form the sample.
  • The two branches within Floyd's algorithm directly correspond to whether an element i (from the 'tail' of the array) is swapped with another element in the tail or outside the tail during this simulated shuffle.

Ultimately, the article provides valuable insights into how this seemingly counter-intuitive algorithm achieves its uniform random sampling, offering clearer conceptual models for understanding its operation.