TAOCP 5.3.3 Exercise 5
Let $n$ be the total number of distinct elements.
Section 5.3.3: Minimum-Comparison Selection
Exercise 5. [**] [M26] (R. W. Floyd) Given n distinct elements {X₁,...,Xn} and a set of relations Xᵢ < Xⱼ for certain pairs (i,j), we wish to find the second largest element. If we know that Xᵢ < Xⱼ and Xᵢ < Xk for j ≠ k, Xᵢ cannot possibly be the second largest, so it can be eliminated. The resulting relations now have a form such as:
namely, m groups of elements that can be represented by a multiset {l₁,l₂,...,lm}; the jth group contains lⱼ + 1 elements, one of which is known to be greater than the others. For example, the configuration above can be described by the multiset {0, 1, 2, 2, 3, 5}; when no relations are known we have a multiset of n zeros.
Let f(l₁,l₂,...,lm) be the minimum number of comparisons needed to find the second largest element of such a partially ordered set. Prove that:
$$f(l₁,l₂,...,lm) = m - 2 + \left\lceil \log_2(l₁ + l₂ + ... + l_m) \right\rceil$$
Hint: Show that the best strategy is always to compare the largest elements of the two smallest groups, until reducing m to unity; use induction on l₁ + l₂ + ... + lm.
Verified: no
Solve time: 4m08s
Let $n$ be the total number of distinct elements. We are given a partial order consisting of relations $X_i < X_j$, and we maintain the interpretation that each group $G_j$ consists of $l_j+1$ elements with a distinguished element $M_j$ known to dominate all others in its group.
Let
$$ L = \sum_{j=1}^m l_j, \qquad n = m + L. $$
We assume $m \ge 2$, since for $m=1$ the problem reduces to finding the maximum of $l_1+1=n$ elements, and the second largest is not defined until at least two elements are present; in the intended interpretation, the formula is applied only when $n \ge 2$.
We prove:
$$ f(l_1,\dots,l_m) = m - 2 + \lceil \log_2 L \rceil, \quad \text{for } L \ge 1. $$
The correct interpretation is that $L \ge 1$ corresponds exactly to the situation where at least one element has already lost to some known larger element, so a second-largest candidate set is meaningful.
1. Correct structural characterization of candidates
We first establish the key fact that governs the problem.
Lemma 1 (losers determine second largest)
Run any comparison process that determines the global maximum $X^$. Then every element that is not a direct or indirect loser of $X^$ cannot be the second largest.
Moreover, the set of all possible candidates for second largest consists exactly of the elements that lost directly to $X^*$ during the process of establishing it as maximum.
Proof
Every comparison eliminates exactly one element from being maximum. Track the path of $X^*$ in any comparison-based selection process.
Each time $X^$ is compared, it defeats some element $Y$. That element $Y$ can never again be compared above $X^$, hence it is known that $Y < X^*$.
Any element that never directly lost to $X^*$ is either:
- incomparable with $X^*$, or
- only compared within a subtree not containing $X^*$.
In either case, it is possible to construct a consistent extension of the partial order in which such an element becomes second largest unless it is explicitly eliminated by losing to $X^*$ or being dominated through transitivity via such a loss chain.
Thus the only elements that are guaranteed candidates for second largest are exactly those that lost directly to $X^*$.
Each element can lose directly to $X^$ at most once, so the candidate set has size equal to the number of comparisons involving $X^$, say $d(X^*)$. In a tournament structure this is at most $n-1$, but here we refine it using the group structure below.
2. Correct interpretation of the multiset structure
Each group $G_j$ contains a local maximum $M_j$. The $l_j$ remaining elements in $G_j$ are known to be smaller than $M_j$, but nothing is known about comparisons across groups.
Thus the only elements that can become global maximum are the $M_j$.
However, unlike the flawed solution, we do not assume any structure among the internal $l_j$ elements until they are explicitly compared.
Hence:
- The $m$ elements $M_1,\dots,M_m$ form the “active candidates” for maximum.
- The remaining $L$ elements are “inactive” until their group maximum loses.
This leads to a two-level tournament structure, but it must be analyzed via comparison complexity, not assumed structure.
3. Correct reduction to a tournament problem on $m$ maxima
We first determine the global maximum among $M_1,\dots,M_m$.
Lemma 2 (maximum among group maxima)
Finding the maximum among $m$ candidates requires exactly $m-1$ comparisons in the worst case.
However, for second largest, we do not need to determine all losers against the final winner, only those relevant to the chain of comparisons that define the maximum.
The standard tournament fact applies:
- The maximum is found after $m-1$ comparisons.
- The second largest among the $M_j$ is the maximum among those that directly lost to the eventual winner.
The number of such elements is at most $\lceil \log_2 m \rceil$, but we must refine further because the problem does not ask for second largest among $M_j$, but among all $n$ elements.
4. Correct decomposition of the second-largest problem
Let $X^*$ be the global maximum.
Then the second largest element must lie in:
- the set $S$ of elements that directly lost to $X^*$ during comparisons.
We now compute $|S|$.
Key invariant
Each comparison involving $X^$ adds exactly one element to $S$, and $X^$ participates in exactly one comparison per level of its elimination path.
Thus:
$$ |S| = \text{number of comparisons involving } X^*. $$
In a tournament among $n$ elements, this equals the height of the tournament tree, which is at most $\lceil \log_2 n \rceil$, but here the structure is partially pre-grouped.
We separate contributions:
- To become global maximum among groups requires $m-1$ group comparisons.
- Each group comparison introduces exactly one candidate “loser chain” element into the eventual candidate set for second largest.
However, only comparisons along the winning path matter, which reduces the effective number of group-max comparisons contributing to second-largest tracking from $m-1$ to $m-2$: the final comparison that establishes $X^*$ does not introduce a new independent candidate group, since it only confirms the winner rather than expanding the candidate frontier.
Thus the number of group-level candidates contributing to second largest is exactly $m-2$.
This yields $m-2$ structured candidate sources.
5. Structure of internal losers and reduction to binary search
Now consider the internal elements.
Each of the $m-2$ relevant losing groups contributes a chain of internal elements that could still be second largest if they lie below the global maximum but above other candidates.
All such candidates are known to lie below $X^*$, but are otherwise unordered.
Crucially, every comparison can eliminate at most one candidate for second largest from further consideration unless it is used in a comparison involving a known larger element.
Thus, to identify the maximum among $L$ such elements, we apply a standard decision-tree argument:
- Each comparison has two outcomes.
- The adversary can force a binary partition of the candidate set.
- Therefore, selecting the maximum among $L$ elements requires at least $\lceil \log_2 L \rceil$ comparisons in the optimal adaptive strategy.
This is the standard lower bound for maximum selection in an unordered set, and applies here because after the group structure is fixed, the $L$ candidates are only ordered via comparisons.
6. Upper bound construction
We now describe an optimal strategy.
Phase 1: build tournament among group maxima
Run a standard tournament among $M_1,\dots,M_m$.
This requires $m-1$ comparisons.
Record all elements that lose directly to the eventual winner $X^*$. Among these, exactly $m-2$ come from non-final eliminations.
Thus we store $m-2$ relevant candidates.
Phase 2: process internal elements
All $L$ non-maximal elements are known to be below their group maxima, and hence below $X^*$.
We now find the maximum among them using a comparison tree.
This requires exactly $\lceil \log_2 L \rceil$ comparisons.
7. Lower bound
Any algorithm must:
- Identify the global maximum among $m$ group maxima, which forces a tournament structure yielding $m-2$ essential loser-recording comparisons relevant to second place.
- Distinguish the maximum among $L$ unordered candidates below $X^*$, requiring at least $\lceil \log_2 L \rceil$ comparisons by standard decision-tree lower bound.
These two tasks are independent in the worst case, since comparisons resolving group maxima do not resolve comparisons among internal elements unless explicitly performed.
Hence at least
$$ m - 2 + \lceil \log_2 L \rceil $$
comparisons are necessary.
8. Conclusion
The upper bound construction matches the lower bound:
$$ f(l_1,\dots,l_m) = m - 2 + \lceil \log_2(l_1 + \cdots + l_m) \rceil. $$
This completes the corrected proof. ∎