The question
Let's recap the important information
- 25 horses
- all horses have always the same speed and they don't get tired
- horses don't have the same speed
- you can get the order of 5 arbitrary horses with one experiment
- Question: What is the minimum number of experiments you have to run to get the 3 fastest horses?
Find an upper bound
Find only fastest horse
Obviously, you need to race at least every horse. So you need at least $\frac{25}{5} = 5$ experiments. Lets say the horses have are named by letters and $a > b$ means $a$ is faster than $b$. So you get:
$a < b < c < d < e$
$f < g < h < i < j$
$k < l < m < n < o$
$p < q < r < s < t$
$u < v < w < x < y$
But after $5$ experiments with distinct horses you don't know which of the 5 winners ($e, j, o, t, y$) is the fastest. So you need at least $1$ more experiment to get the fastest one:
$e < j < o < t < y$
Great, we can find the fastest one with 6 experiments! (It's $y$).
Find fastest and second fastest horse
But what about the two fastest ones? Well, eventually $x$ is faster than $t$. So you have to make another test. But you can be sure that $e$, $j$ and $o$ are not second fastest, so you can skip them.
The only candidates for the second-fastest horse are $x$ and $t$. So keep in mind that we have $3$ horses left that we can compare in this race!
We can find the fastest and the second fastest horse in $7$ experiments.
Find first, second and third-fastest horse.
Who could be third-fastest?
$x, t$ and the next one in the queue who was second fastest. So:
- if $x$ is second fastest, then $t$ and $w$ could be third-fastest.
- if $t$ is second fastest, then $x$ and $s$ could be third-fastest.
So we only have $s$ and $w$ as candidates for the second-fastest place!
As a result:
We need 7 races to find the 3 fastest horses.
The pattern
In the first step you get queues with the fastest horse on top. In all following steps you only compare the top of those queues.
Imagine you have queues of cards. Those cards have an order. In the first step, you make 5 queues of ordered cards. In every following step you compare the top of that queues.
A more general solution
Let's say you have $n$ horses and in each experiment you can compare $m$ horses and you want to get the top $t$ horses.
If $m \geq n$, you're ready. You only need one experiment.
Else, you make $\lceil \frac{n}{m} \rceil$ experiments to get the queues.
If $\lceil \frac{n}{m} \rceil < m$, you make $t$ comparisons of the top of the queues.
Else you make queues of the top of the queues ... ok, I really don't want to go through this now.
There might be better solutions, but I thing this is a good start.
Sorting algortihms
I would like to mention that sorting alorithms have $m = 2$. In that case, the optimal number is well-known:
- You can have $n!$ permutaitons of $n$ elements.
- With every step, you can half the number of candidates
- $\log_2(n!) <\log_2(n^n) = n \cdot \log_2(n)$