\documentclass[11pt]{amsart} \pdfsuppressptexinfo=-1 \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage[margin=1.1in]{geometry} \usepackage{amssymb,mathtools} \usepackage{booktabs} \usepackage{array} \usepackage{enumitem} \usepackage{microtype} \usepackage[hidelinks]{hyperref} \newtheorem{theorem}{Theorem}[section] \newtheorem{lemma}[theorem]{Lemma} \newtheorem{proposition}[theorem]{Proposition} \newtheorem{corollary}[theorem]{Corollary} \newtheorem{conjecture}[theorem]{Conjecture} \newtheorem{theoremA}{Theorem} \renewcommand{\thetheoremA}{\Alph{theoremA}} \newtheorem{corollaryA}[theoremA]{Corollary} \theoremstyle{definition} \newtheorem{definition}[theorem]{Definition} \theoremstyle{remark} \newtheorem{remark}[theorem]{Remark} \newcommand{\cl}[1]{\lceil #1\rceil} \newcommand{\fl}[1]{\lfloor #1\rfloor} \newcommand{\Uh}{\hat U} \newcommand{\Vh}{\hat V} \newcommand{\live}{\operatorname{live}} \newcommand{\Ones}{\mathrm{Ones}} \newcommand{\Good}{\mathrm{Good}} \newcommand{\LTP}{\mathrm{LTP}} \newcommand{\defect}{\operatorname{def}} \newcommand{\LB}{\mathrm{LB}} \newcommand{\todo}[1]{\textbf{[#1]}} \makeatletter\let\tabinput\@@input\makeatother \input{ids.tex} \hypersetup{pdftitle={Exact sizes of networks that select the three or four largest of n elements}, pdfauthor={Tejasvi Singh Tomar}} \title{Exact sizes of networks that select the three or four largest of $n$ elements} \author{Tejasvi Singh Tomar} \renewcommand{\urladdrname}{{\itshape ORCID}} \urladdr{\href{https://orcid.org/0000-0003-4668-3639}{https://orcid.org/0000-0003-4668-3639}} \date{\pubdate} \subjclass[2020]{Primary 68P10; Secondary 68Q06} \keywords{Selection networks, comparator networks, lower bounds, exhaustive search, formal verification} \begin{document} \begin{abstract} We study $(t,n)$-selection networks, which move the $t$ largest of $n$ inputs to $t$ given lines in any order, and the least number $\Uh_t(n)$ of comparators in such a network. Alekseev showed that $\Uh_t(n)\ge(n-t)\cl{\lg(t+1)}$. For $t=2^k\ge 4$ we raise this bound by $k-1$ for % ALEKGAIN every $n>t$, and by $2k-5$ when $t\ge 32$; as far as we know, these are the first improvements at powers of two. The proofs follow the weights of the lines, and a pruning step of Yao extends the gain to every $t\ge 3$. For $t=3$, Yao's upper bound $2n-5+\fl{\lg(n-3)}$ is exact for $4\le n\le 18$. For $t=4$ the new bound is $3n-11$ for all $n\ge 5$, and the value $3n-10$ that Knuth expected is exact for $6\le n\le 12$. Their lower bounds rest on exhaustive searches at a few sizes and on the fact that one more input costs at least two more comparators. Most of the results are also proved in Lean~4, some through compiled checks. \end{abstract} \maketitle \section{Introduction}\label{sec:intro} Selection networks, comparator networks that move the $t$ largest of $n$ inputs to $t$ given lines, appear in Knuth's treatment of sorting networks \cite[Sec.~5.3.4]{Knu98} and, more recently, as encodings of cardinality constraints in SAT and constraint solving \cite{CZ10,ANORC11,KP15}. Their least size is known only in a few cases. A comparator network on the lines $0,\dots,n-1$ is a sequence of comparators $[i{:}j]$ with $it$ with $C(t)=k-1+\delta_k-(k+1)t-(t-2^k)\lg t$. The second bound is weaker for large $n$, but it adds $t-2^k+k-1+\delta_k$ to Alekseev's bound for every $n>t$. This is at least $1$ for $t\ge 3$, so for $n>t$ Alekseev's bound is exact only when $t\le 2$. Both bounds are attained at $n=t+1$ for $2\le t\le 7$, where $\Uh_t(t+1)=\Uh_1(t+1)=t$. For $t=4$, Theorem~\ref{thm:alek} and the bound of Knuth's exercise show that $\Uh_4(n)$ % ALEKGAIN is $3n-11$ or $3n-10$ for every $n\ge 6$ (Corollary~\ref{cor:u4}), and Theorem~\ref{thm:u4} below decides between them for $n\le 12$. For $t=3$ and $t=4$ we determine $\Uh_t(n)$ for small $n$. \begin{theorem}\label{thm:u3} $\Uh_3(n)=2n-5+\fl{\lg(n-3)}$ for $4\le n\le 18$. In particular $\Uh_3(n)=2n-2$ for $11\le n\le 18$. \end{theorem} \begin{theorem}\label{thm:u3low} $\Uh_3(n)\ge 2n-2$ for every $n\ge 11$. \end{theorem} The bound $2n-2$ of Theorem~\ref{thm:u3low} minus the lower bound in \eqref{eq:yao} is $4-\cl{\lg\cl{n/3}}$: $2$ for $n=11,12$, $1$ for $13\le n\le 24$, $0$ for $25\le n\le 48$, and negative from $n=49$ on. Beyond Theorem~\ref{thm:u3}, it is new only for $19\le n\le 24$, where it leaves $\Uh_3(n)\in\{2n-2,2n-1\}$ (Table~\ref{tab:u3}). \begin{theorem}\label{thm:u4} $\Uh_4(n)=3n-10$ for $6\le n\le 12$. \end{theorem} For $n=8$ this settles the conjecture of Qu, Tasbihi and Kschischang, and for $7\le n\le 12$ it confirms Knuth's expectation. The case $n=6$ was known, since $\Uh_4(6)=\Uh_2(6)=8$, and $\Uh_4(7)=\Uh_3(7)=11$ follows from Theorem~\ref{thm:u3}. For $n=11$ and $n=12$ the step from the bound $3n-11$ of Theorem~\ref{thm:alek} to $3n-10$ rests % ALEKGAIN on the searches of Section~\ref{sec:search} alone. \begin{lemma}[step lemma]\label{lem:step} If $t\ge 2$ and $n\ge t+1$, then $\Uh_t(n)\ge\Uh_t(n-1)+2$. \end{lemma} The lemma is the analogue for $\Uh_t$ of Knuth's inequality $\Vh_3(n)\ge\Vh_3(n-1)+2$ for $n\ge 4$, where $\Vh_3(n)$ is the least size of a network that moves the third largest of $n$ inputs to a given line \cite[answer to exercise 5.3.4--20(a)]{KnuErr}. We prove the lemma in Section~\ref{sec:step} by a different argument. Applied repeatedly from $n=t$, it yields Alekseev's bound $2(n-t)$ for $t=2,3$. \begin{table}[ht] \centering\small \begin{tabular}{@{}rrrrr@{}} \toprule $n$ & Alekseev $2(n-3)$ & Yao's lower bound & $\Uh_3(n)$ & Yao's upper bound \\ \midrule \tabinput tab_u3.tex \bottomrule \end{tabular} \caption{The values of $\Uh_3(n)$ for $n\le 18$ (Theorem~\ref{thm:u3}) and the lower bound of Theorem~\ref{thm:u3low} for $19\le n\le 24$, with the bounds in \eqref{eq:yao}.} \label{tab:u3} \end{table} For $t=3$ the upper bounds come from Yao's construction \cite[Thm.~2.7]{Yao75}. For $t=4$, the bound $\Uh_4(n)\le 3n-10$ is Knuth's exercise \cite[answer to exercise 5.3.4--20(b)]{KnuErr}. Our upper bounds for $8\le n\le 12$ are the optimal networks that our searches output, checked on all inputs (Section~\ref{sec:upper}); those for $n=6,7$ come from $\Uh_4(n)=\Uh_{n-4}(n)$. The lower bounds for $t=3$ need only two exhaustive searches, at $n=7$ and $n=11$. These are the sizes $n=2^j+3$ with $52^{j-1}$ and $c\notin Q_j$, we have $c>3\cdot 2^{j-1}$. If $b\le 2^j$, then $a\ge c-b>3\cdot 2^{j-1}-2^j=2^{j-1}$. Otherwise $b$ has level $j+1$, so $c$ has level $j+2$, as the gain is $1$; then $c>2^{j+1}$ and $a\ge c-b>2^{j+1}-3\cdot 2^{j-1}=2^{j-1}$. In both cases $2^{j-1}2^{j-1}$, so $\cl{\lg v}\ge j$. So if $v$ lies in two sets, $Q_j$ and $Q_{j+1}$, then $\cl{\lg v}\ge j+1\ge 2$, and if it lies in one, then $\cl{\lg v}\ge 1$, as $v\ge 2$. \end{proof} \begin{proof}[Proof of Theorem~\ref{thm:alek}] Let $t=2^k$ with $k\ge 2$, so that the cap $t+1$ has level $k+1$. We use the sets $Q_1,\dots,Q_{k-1}$, which lie in % ALEKGAIN $\{2,\dots,3t/4\}$; by Lemma~\ref{lem:weights}(c), a line that has a weight in one of them at the end is an output line. At each moment and for $1\le j\le k-1$, let $\chi_j=1$ if some line had a weight in $Q_j$ at an earlier moment and no line has one now, and $\chi_j=0$ otherwise. Let $\Phi$ be the potential plus $\chi_1+\dots+\chi_{k-1}$. At the start every weight is $1$, so $\Phi=0$. Each comparator raises $\Phi$ by at most $1$. Indeed, $\chi_j$ can rise only at a comparator that empties $Q_j$. If no $\chi_j$ rises, $\Phi$ rises by at most the gain, which is at most $1$. If $\chi_j$ rises, the comparator lifts the last weight in $Q_j$ to a weight outside $Q_j$, and its helper is not in $Q_j$; so its gain is $0$ by Lemma~\ref{lem:classes}(a), and no other $\chi_i$ rises, by Lemma~\ref{lem:classes}(b). Since $n\ge t+1$, some line has weight $t+1$ at the end, by Lemma~\ref{lem:weights}(c). As an entry of the multiset, this weight started at $1$, of level $0$, and each lift raised its level by at most $1$, since $c\le 2b$. So for each $j\le k-1$ it had level $j$ at some moment, and then it lay in $Q_j$. Hence, at the end, for each $j\le k-1$ either $\chi_j=1$ or some output line has a weight in $Q_j$. By Lemma~\ref{lem:classes}(c), the weight of an output line lies in at most as many of the sets as its level, so $\varepsilon$, the sum of the levels of the output lines, is at least the number of sets $Q_j$ with $j\le k-1$ that still contain a weight. Therefore $\varepsilon+\chi_1+\dots+\chi_{k-1}\ge k-1$, and at the end \[ \Phi=(n-t)(k+1)+\varepsilon+\chi_1+\dots+\chi_{k-1}\ge(n-t)(k+1)+k-1. \] As $\Phi$ starts at $0$ and each comparator raises it by at most $1$, the network has at least this many comparators. \end{proof} In the notation of \eqref{eq:waste}, the proof shows that $\sigma\ge\chi_1+\dots+\chi_{k-1}$ at the end, and hence $\sigma+\varepsilon\ge k-1$. The proof uses only the weights and Lemma~\ref{lem:weights}, so it holds in the following \emph{weight model}. A % ALEKGAIN state is a multiset of $n$ weights, at the start all equal to $1$; a move replaces two weights $a\le b$ by $a$ and some $c$ with $b\le c\le\min(a+b,t+1)$; and a final state has $n-t$ weights equal to $t+1$ and $t$ weights at most $t$. Gain, waste, $\sigma$, $\varepsilon$ and $\Phi$ are defined as before. By Lemma~\ref{lem:weights}, every $(t,n)$-selector gives a sequence of as many moves from the start to a final state, with the same $\sigma$ and $\varepsilon$, and the proof of Theorem~\ref{thm:alek} shows that $\sigma+\varepsilon\ge k-1$ for every such sequence. To go further, fix such a sequence, for $t=2^k$ with $k\ge 2$, and let $F$ be the multiset of its $t$ output weights % ALEKGAIN at the end. By the proof of Theorem~\ref{thm:alek}, every set $Q_j$ with $j\le k-1$ has been entered by the end, and at the end these sets hold only output weights. Let $D$ be the total amount by which the moves fall short of raising $\Phi$ by $1$, and let $q(F)$ be the number of sets $Q_j$, $j\le k-1$, that contain a weight of $F$. Then $\Phi$ ends at $(n-t)(k+1)+\varepsilon+k-1-q(F)$, the number of moves is this value plus $D$, and \eqref{eq:waste} gives \begin{equation}\label{eq:split} \sigma+\varepsilon=k-1+f(F)+D,\qquad\text{where}\quad f(F)=\sum_{v\in F}\cl{\lg v}-q(F)\ge 0 \end{equation} by Lemma~\ref{lem:classes}(c). Call a move \emph{idle} if it has gain $0$ and empties no set $Q_j$ and enters none; such a move adds $1$ to $D$. For $1\le j\le k-1$ let $S_j=\{3\cdot 2^{j-1}+1,\dots,2^{j+1}\}$, the upper part of level $j+1$. These sets are disjoint; $S_j$ lies in $Q_{j+1}$ if $j\le k-2$, and $S_{k-1}$ meets none of the sets $Q_1,\dots,Q_{k-1}$. So a move that lifts a weight of $S_j$ to a weight of $S_j$ is idle. \begin{lemma}\label{lem:band} Let $1\le j\le k-1$, suppose that no output weight lies in $Q_j\cup S_j$, and let $A$ be the largest output weight with $A\le 2^{j-1}$. Then $A$ exists, and at least $\max\bigl(0,\cl{(2^{j-1}+1)/A}-2\bigr)$ moves lift a weight of $S_j$ to a weight of $S_j$. \end{lemma} \begin{proof} The set $Q_j$ has been entered and holds no output weight, so it is empty at the end. Let $\tau$ be the last move that empties it; after $\tau$ no weight enters $Q_j$. The move $\tau$ lifts the last weight $b$ of $Q_j$ to some $c\notin Q_j$, and its helper $a$ is not in $Q_j$. Then $c>3\cdot 2^{j-1}$, and $a\le 2^{j-1}$, as $a\le b\le 3\cdot 2^{j-1}$ and $a\notin Q_j$; so $c\le a+b\le 2^{j+1}$, and $c\in S_j$. After $\tau$, a weight $w\le 2^{j-1}$ stays at most $2^{j-1}$, since a lift takes it to at most $2w\le 3\cdot 2^{j-1}$ and the weights from $2^{j-1}+1$ to $3\cdot 2^{j-1}$ form $Q_j$. So it ends as an output weight, and it is at most $A$ at every moment after $\tau$. The helper $a$ is such a weight, so $A$ exists and $a\le A$. It follows that after $\tau$ no weight enters $S_j$, as it would have to come from $Q_j$ or from a weight at most $2^{j-1}$; and as $S_j$ holds no output weight and $2^{j+1}\le t$, it is empty at the end. While $S_j$ is not empty after $\tau$, let $m$ be its least weight; $m$ never decreases. A weight $m$ can be lifted only with a helper at most $m$, which is a weight at most $A$ or another weight $m$. So $m$ changes only at a move that lifts the last weight $m$, with a helper at most $A$, to at most $m+A$. If $m\le 2^{j+1}-A$, the lifted weight stays in $S_j$, so the move lifts a weight of $S_j$ to a weight of $S_j$, and $m$ rises by at most $A$. Just after $\tau$, $m\le c\le 3\cdot 2^{j-1}+A$, and $m$ exceeds $2^{j+1}-A$ before $S_j$ becomes empty, since the last weight to leave $S_j$ is lifted with a helper at most $A$. Hence $m$ rises at least $\cl{(2^{j-1}+1-2A)/A}=\cl{(2^{j-1}+1)/A}-2$ times while $m\le 2^{j+1}-A$, each time at a move that lifts a weight of $S_j$ to a weight of $S_j$. \end{proof} For a multiset $F$ of weights in $\{1,\dots,t\}$, let $J(F)$ be the set of indices $j\le k-1$ such that no weight of $F$ lies in $Q_j\cup S_j$, and for $j\in J(F)$ let $A_j(F)$ be the largest weight of $F$ that is at most $2^{j-1}$. Let \begin{equation}\label{eq:LB} \LB(k)=\min_F\Bigl(f(F)+\sum_{j\in J(F)}\max\bigl(0,\cl{(2^{j-1}+1)/A_j(F)}-2\bigr)\Bigr), \end{equation} the minimum over the multisets $F$ of $t$ weights in $\{1,\dots,t\}$ for which $A_j(F)$ exists for every $j\in J(F)$. \begin{proof}[Proof of Theorem~\ref{thm:bands}] Let $F$ be the output weights of a sequence of moves of the weight model to a final state. By Lemma~\ref{lem:band}, $A_j(F)$ exists for every $j\in J(F)$, and at least $\max(0,\cl{(2^{j-1}+1)/A_j(F)}-2)$ idle moves lift a weight of $S_j$ to a weight of $S_j$. These moves are distinct for distinct $j$, as the sets $S_j$ are disjoint, and each adds $1$ to $D$. By \eqref{eq:split}, $\sigma+\varepsilon\ge k-1+\LB(k)$, and Lemma~\ref{lem:weights} turns this into the bound on $\Uh_t(n)$. \end{proof} \begin{proof}[Proof of Theorem~\ref{thm:pow2plus}] Let $k\ge 4$. By the proof of Theorem~\ref{thm:bands} it suffices to show that the expression minimized in \eqref{eq:LB} is at least $k-4$ for every $F$; we use only its term $j=k-1$. For a weight $v$ let $d(v)$ be $\cl{\lg v}$ minus the number of sets $Q_j$, $j\le k-1$, that contain $v$; then $f(F)\ge\sum_{v\in F}d(v)$, and each term is nonnegative by Lemma~\ref{lem:classes}(c). We have $d(1)=d(2)=0$. If $2^{i-1}2^e/3$, and $k-e-2+\cl{2^e/3}-2$ is $k-4$ for $e=2,3$ and at least $k-2$ for $e\ge 4$. If $A>3\cdot 2^{i-2}$, then $d(A)=k-e-1$ and $(h+1)/A>2^{e-2}$, so that the expression is at least $k-e-1+2^{e-2}-1\ge k-3$. \end{proof} \begin{remark}\label{rem:LB} In \eqref{eq:LB}, $f(F)$, $J(F)$ and the comparisons with $2^{j-1}$ depend on a weight only through the half of its level in which it lies, and each term of the sum decreases as $A_j(F)$ grows; so only the largest weight of $F$ in each half-level matters, and $\LB(k)$ is a minimum over finitely many sets. Two programs in the supplement~\cite{Supp}, written separately, compute it; these values are computed, not formalized: \begin{center}\small\setlength{\tabcolsep}{3.5pt} \begin{tabular}{@{}l*{19}{c}@{}} $k$ & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20\\ $\LB(k)$ & 0 & 0 & 0 & 1 & 2 & 3 & 4 & 6 & 8 & 10 & 12 & 15 & 18 & 21 & 24 & 28 & 32 & 36 & 40 \end{tabular} \end{center} So $\LB(k)=\max(0,k-4)$ for $k\le 8$, and for $9\le k\le 20$ Theorem~\ref{thm:bands} is stronger than Theorem~\ref{thm:pow2plus}, by $\LB(k)-(k-4)$, which grows from $1$ to $24$ in the table; for $t=512$ it gives $\Uh_t(n)\ge 10n-5106$. For $k\ge 2$, Corollary~\ref{cor:general} holds as well with $\LB(k)$ in place of $\delta_k$, by the same proof. \end{remark} We write $m\times(a,b\to c)$ for $m$ moves that each lift a weight $b$ to $c$ with a helper $a$. \begin{proposition}\label{prop:wsharp} Let $t=2^k$. In the weight model, the least value of $\sigma+\varepsilon$ over all sequences of moves to a final state is $k-1$ for $k=2,3,4$, for every $n\ge 5$, $9$ and $18$ respectively, $5$ for $k=5$ and every $n\ge 34$, and $7$ for $k=6$ and every $n\ge 67$. \end{proposition} \begin{proof} Theorems~\ref{thm:alek} and~\ref{thm:pow2plus} give the lower bounds. The following sequences reach them; in each, one checks move by move that the helpers are present and every move is allowed, and that the last state is a final state. For $k=2$: $(n-3)\times(1,1\to 2)$, $(n-4)\times(2,2\to 4)$ and $(n-4)\times(1,4\to 5)$, with no waste and the output weights $1,1,1,2$. For $k=3$: $(n-7)\times(1,1\to 2)$, $(n-7)\times(1,2\to 3)$, $(n-8)\times(3,3\to 6)$ and $(n-8)\times(3,6\to 9)$, with no waste and the output weights seven times $1$ and once $3$. For $k=4$: $(n-15)\times(1,1\to 2)$, $2\times(2,2\to 4)$, $(n-17)\times(1,2\to 3)$, $(4,4\to 8)$, $(3,4\to 6)$, $(n-18)\times(3,3\to 6)$, $(n-17)\times(3,6\to 9)$, $(n-17)\times(8,9\to 17)$, and $(3,8\to 11)$, $(3,11\to 14)$, $(3,14\to 17)$, with waste $1$ (the move $11\to 14$) and the output weights fifteen times $1$ and once $3$. For $k=5$: $(n-30)\times(1,1\to 2)$, $3\times(2,2\to 4)$, $2\times(4,4\to 8)$ and $(8,8\to 16)$; then, $n-34$ times, the moves $(1,2\to 3)$, $(2,3\to 5)$, $(4,5\to 9)$, $(8,9\to 17)$, $(16,17\to 33)$ on one weight $2$; and $(2,4\to 6)$, $(6,8\to 11)$, $(6,11\to 17)$, $(16,17\to 33)$, $(6,16\to 22)$, $(6,22\to 28)$, $(6,28\to 33)$, with waste $1$ (the move $22\to 28$) and the output weights thirty times $1$, once $2$ and once $6$, so that $\varepsilon=4$. For $k=6$: $(n-62)\times(1,1\to 2)$, $4\times(2,2\to 4)$, $(1,2\to 3)$, $3\times(4,4\to 8)$, $(3,4\to 6)$, $(3,6\to 9)$, $2\times(8,8\to 16)$, $(8,9\to 17)$ and $(16,16\to 32)$; then, $n-67$ times, the moves $(1,2\to 3)$, $(3,3\to 6)$, $(3,6\to 9)$, $(8,9\to 17)$, $(16,17\to 33)$, $(32,33\to 65)$ on one weight $2$; and $(3,8\to 11)$, $(16,17\to 33)$, $(11,16\to 22)$, $(11,22\to 33)$, $2\times(32,33\to 65)$, $(11,32\to 43)$, $(11,43\to 54)$, $(11,54\to 65)$, with waste $1$ (the move $43\to 54$) and the output weights sixty-two times $1$, once $3$ and once $11$. \end{proof} Hence, at the sizes $n$ of Proposition~\ref{prop:wsharp}, no argument that uses only these properties of the weights can improve Theorem~\ref{thm:alek} for $t=4$, $8$ and $16$, or Theorem~\ref{thm:pow2plus} for $t=32$ and $t=64$. For $k\ge 7$ we know only the lower bound $k-1+\LB(k)$ of Theorem~\ref{thm:bands}. The pruning step behind Corollary~\ref{cor:general} is Yao's. His selectors collect the $t$ smallest values, and his % ALEKGAIN proof of $\Uh_3(n)\ge 2n-6+\cl{\lg\cl{n/3}}$ follows the smallest value and prunes a $(3,n)$-selector to a $(2,n-1)$-selector \cite[proof of Thm.~2.6]{Yao75}; he notes that the argument extends by induction to every $t$ that is not a power of two \cite[Sec.~2.3.3]{Yao75}. In our orientation it follows the largest value. \begin{lemma}\label{lem:prune} If $t\ge 2$ and $n\ge t+1$, then $\Uh_t(n)\ge\Uh_{t-1}(n-1)+\cl{\lg\cl{n/t}}$. \end{lemma} \begin{proof} Let $N$ be an optimal $(t,n)$-selector. Give line $i$ a value larger than all other inputs; this maximum stays the maximum, each comparator that meets it moves it to its upper line, and it ends on an output line. Call the comparators that meet it the \emph{upper path} of $i$, and its length $L^+(i)$. The upper paths of the $n$ lines end on the $t$ output lines, so at least $\cl{n/t}$ of them end on the same output line. Two upper paths that meet at a comparator coincide from there on, so these paths form a binary tree, with the comparators on them as inner nodes and the input lines as leaves, and $L^+(i)\ge\cl{\lg\cl{n/t}}$ for one of these lines $i$. Now argue as in the proof of Lemma~\ref{lem:step}, with the maximum in place of the minimum: deleting the upper path of $i$ and renaming the lines of the later comparators leaves a generalized network with $|N|-L^+(i)$ comparators that never uses line $i$, followed by a permutation of the lines that takes line $i$ to an output line, and on the other $n-1$ lines this network moves the $t-1$ largest of its inputs to $t-1$ fixed lines. By Lemma~\ref{lem:gen}, $\Uh_{t-1}(n-1)\le|N|-L^+(i)\le\Uh_t(n)-\cl{\lg\cl{n/t}}$. \end{proof} \begin{proof}[Proof of Corollary~\ref{cor:general}] Let $m=n-t+2^k$, so that $m\ge 2^k+1$. Lemma~\ref{lem:prune} is an inequality between least sizes: as in the % ALEKGAIN proof of Lemma~\ref{lem:step}, the pruned network, which has generalized comparators and new output lines, is compared with $\Uh_{t-1}(n-1)$ through Lemma~\ref{lem:gen}. So the lemma can be applied repeatedly, and at $(t-i,n-i)$ for $i=0,\dots,t-2^k-1$ it gives \[ \Uh_t(n)\ge\Uh_{2^k}(m)+\sum_{i=0}^{t-2^k-1}\cl{\lg\cl{\tfrac{n-i}{t-i}}}. \] If $2\le k\le 4$, Theorem~\ref{thm:alek} gives $\Uh_{2^k}(m)\ge(k+1)(m-2^k)+k-1$, if $k\ge 5$, Theorem~\ref{thm:pow2plus} gives $\Uh_{2^k}(m)\ge(k+1)(m-2^k)+2k-5$, and if $k=1$, Alekseev's bound gives $\Uh_2(m)\ge 2(m-2)$; in each case this is $(k+1)(n-t)+k-1+\delta_k$. This proves the first inequality. For the second, each term of the sum is at least $1$, since $n-i>t-i$, and $k+1=\cl{\lg(t+1)}$, since $2^k\le t<2^{k+1}$. By Lemma~\ref{lem:gen}, both bounds hold as well for generalized networks that move the $t$ largest values to any $t$ fixed lines. \end{proof} \begin{corollary}\label{cor:u4} $\Uh_4(n)\ge 3n-11$ for every $n\ge 5$, with equality at $n=5$. Hence $\Uh_4(n)$ is $3n-11$ or $3n-10$ for every % ALEKGAIN $n\ge 6$. \end{corollary} \begin{proof} The bound is Theorem~\ref{thm:alek} for $t=4$, and $\Uh_4(5)=\Uh_1(5)=4$. For $n\ge 6$ the bound of Knuth's exercise \cite[answer to exercise 5.3.4--20(b)]{KnuErr} gives $\Uh_4(n)\le 3n-10$. \end{proof} By \eqref{eq:waste}, Knuth's expected equality $\Uh_4(n)=3n-10$ says that $\sigma+\varepsilon\ge 2$ for every % ALEKGAIN $(4,n)$-selector with $n\ge 6$, while the proof of Theorem~\ref{thm:alek} gives $\sigma+\varepsilon\ge 1$, and by Proposition~\ref{prop:wsharp} the weight model allows $\sigma+\varepsilon=1$. Theorem~\ref{thm:u4} proves $\sigma+\varepsilon\ge 2$ for $6\le n\le 12$; the step from $1$ to $2$ uses structure of the networks that the weights do not record. \begin{remark}\label{rem:sharp} The proof of Theorem~\ref{thm:alek} works for every $t\ge 2$ with the sets $Q_j$ for $1\le j\le\fl{\lg(2t/3)}$, % ALEKGAIN which are the sets $Q_j$ inside $\{2,\dots,t\}$. Lemma~\ref{lem:classes} holds for every $t$, and a weight that ends at $t+1$ passes through every level up to $\cl{\lg(t+1)}$, which exceeds $\fl{\lg(2t/3)}$. A single potential therefore gives $\Uh_t(n)\ge(n-t)\cl{\lg(t+1)}+\fl{\lg(2t/3)}$ for $t\ge 2$ and $n\ge t+1$. It equals the second bound of Corollary~\ref{cor:general} for $t=2,3,4,8,16$, and is weaker for every other $t$. \end{remark} \section{Exhaustive search}\label{sec:search} By Lemmas~\ref{lem:gen} and~\ref{lem:lb}, $\Uh_t(n)>D$ follows from a breadth-first search over the sets reachable from $V_{n,t}$: layer $k$ holds the states reachable with $k$ generalized comparators, up to a permutation of the lines, and a state in layer $k$ is discarded when $k+\max(\live(S)-t,\cl{\lg|S|})>D$. If no layer up to $D$ contains a singleton, then $\Uh_t(n)>D$. Two states may be merged only if one is a permutation of the other, and each program uses a merge test that never merges any other states. Two programs, written independently, compute $\Uh_t(n)$ this way. We call them B and C, after their folders in the supplement~\cite{Supp}, which also holds their logs. Program~B merges states that are permutations of each other after sorting the lines by an invariant. Program~C was written from the definition alone, without access to Program~B; it uses its own canonical forms, the bound of Lemma~\ref{lem:lb}, and a checkpointed, multithreaded variant for the largest cases. Table~\ref{tab:runs} lists the values each program gives. For $\Uh_4(12)$, Program~B's search ran under a new \emph{parallel driver}, which runs it on several threads with the same states, merge test and moves as Program~B. We compared the driver's output at every depth with that of the sequential program for $t=3$ with $10\le n\le 12$ and for $t=4$ with $8\le n\le 11$; the two agree. \begin{table}[ht] \centering\small \begin{tabular}{@{}llp{0.46\textwidth}@{}} \toprule value & programs & largest run \\ \midrule $\Uh_2(n)=2n-4$, $3\le n\le 10$ & C, and B for $n=8,9$ & --- \\ $\Uh_3(n)$, $4\le n\le 10$ & C, and B for $6\le n\le 10$ & --- \\ $\Uh_3(11)=20$ & B, C & B: $7.2$\,s, $314{,}518$ states; C: $25.4$\,s \\ $\Uh_3(12)=22$ & B, C & B: $74.9$\,s, $2{,}349{,}706$ states; C: $243.4$\,s, largest layer $460{,}255$ \\ $\Uh_3(13)=24$ & B & $2{,}131.5$\,s, $18{,}962{,}336$ states \\ $\Uh_4(n)$, $n=8,9,10$ & B, C & at $n=10$, B: $34.1$\,s, $1{,}553{,}147$ states; C: $199.2$\,s, largest layer $397{,}180$ \\ $\Uh_4(11)=23$ & B, C & B: $3{,}421.5$\,s, $48{,}814{,}192$ states; C: $149.1$\,s on $90$ threads, $45{,}358{,}610$ states \\ $\Uh_4(12)=26$ & B, C & B: $868.9$\,s on $190$ threads, $1{,}908{,}917{,}869$ states; C: $7{,}723.4$\,s on $90$ threads, $1{,}806{,}537{,}098$ states, largest layer $486{,}241{,}921$ \\ \bottomrule \end{tabular} \caption{The searches, with the figures of the runs logged in the supplement. Times are for one core unless stated. The two programs count states differently.} \label{tab:runs} \end{table} For the values used in the proofs of Theorems~\ref{thm:u3} and~\ref{thm:u3low}, and for $\Uh_4(n)$ with $6\le n\le 10$, a separate generator writes the states of the search, with their depths, as a certificate. A certificate is \emph{closed} if it contains $V_{n,t}$ at depth $0$, no state is a singleton, and every move from a state of depth $dD$. The certificate for $\Uh_3(7)>10$ has $191$ states, and the one for $\Uh_3(11)>19$ has $210{,}535$. A separate program, which shares no code with the generator, checks that the certificates are closed. \section{Proofs of the theorems}\label{sec:proofs} \begin{proof}[Proof of Theorem~\ref{thm:u3}] Write $Y(n)=2n-5+\fl{\lg(n-3)}$. Then $Y(n)-Y(n-1)=3$ if $n-3$ is a power of two, and $2$ otherwise. We have $\Uh_3(4)=\Uh_1(4)=3=Y(4)$ and $\Uh_3(5)=\Uh_2(5)=6=Y(5)$. The searches show that $\Uh_3(7)\ge 11=Y(7)$ and $\Uh_3(11)\ge 20=Y(11)$. By Lemma~\ref{lem:step}, $\Uh_3(n)\ge\Uh_3(n-1)+2$, which covers every other $n\le 18$: the next size with $n-3$ a power of two is $19$. So $\Uh_3(n)\ge Y(n)$ for $4\le n\le 18$, and Yao's networks show that equality holds. The searches of Table~\ref{tab:runs} agree for $n\le 13$. \end{proof} \begin{proof}[Proof of Theorem~\ref{thm:u3low}] $\Uh_3(n)\ge\Uh_3(11)+2(n-11)=2n-2$ by Lemma~\ref{lem:step}. \end{proof} \begin{proof}[Proof of Theorem~\ref{thm:u4}] For $n=6,7$ see Section~\ref{sec:intro}. For $8\le n\le 12$ Programs~B and~C find no $(4,n)$-selector with fewer than $3n-10$ comparators (for $n=12$, Program~B under its parallel driver), and the networks of Section~\ref{sec:upper} have $3n-10$ comparators. \end{proof} Thus $\Uh_4(n)=8,11,14,17,20,23,26$ for $n=6,\dots,12$: Knuth's bound $3n-10$, one more than the bound of Theorem~\ref{thm:alek} and two more than Alekseev's bound $3(n-4)$. % ALEKGAIN \section{The sizes $2^j+3$}\label{sec:blocks} \begin{proposition}\label{prop:blocks} Let $M\ge 4$. Then $\Uh_3(n)=2n-5+\fl{\lg(n-3)}$ for all $4\le n\le M$ if and only if $\Uh_3(m)\ge\Uh_3(m-1)+3$ for every $m=2^j+3$ with $5\le m\le M$. \end{proposition} \begin{proof} The base is $\Uh_3(4)=\Uh_1(4)=3=Y(4)$. For $n\ge 5$, $Y(n)-Y(n-1)$ is $3$ at the sizes $m=2^j+3$ and $2$ otherwise, and by Lemma~\ref{lem:step}, $\Uh_3(n)-\Uh_3(n-1)\ge 2$. If the step is at least $3$ at every such $m\le M$, induction from $n=4$ yields $\Uh_3(n)\ge Y(n)$ for $4\le n\le M$; conversely, if $\Uh_3=Y$ on $4\le n\le M$, the step at each such $m$ is $3$. The upper bound $\Uh_3(n)\le Y(n)$ for all $n\ge 4$ is Yao's construction \cite[Thm.~2.7]{Yao75}; we checked the networks explicitly only for $n\le 18$ (Section~\ref{sec:upper}). \end{proof} Yao's question is therefore whether $\Uh_3$ grows by three at every size $2^j+3$. This holds for $m=5,7,11$. At $m=19$ we know only $\Uh_3(19)\in\{36,37\}$, and the number of states that Program~B keeps grows by a factor of about $8$ from $n=12$ to $n=13$ (Table~\ref{tab:runs}), which puts the search out of reach at $n=19$. Suppose $\Uh_3(m)=\Uh_3(m-1)+2$ for some $m=2^j+3$, and let $N$ be an optimal $(3,m)$-selector. By the proof of Lemma~\ref{lem:step}, every minimum path of $N$ has length at most $2$, some has length $2$, and deleting it leaves an optimal $(3,m-1)$-selector $N'$. By Lemma~\ref{lem:gen} we may treat $N$ and $N'$ as generalized networks. After renaming lines, $N$ then arises from $N'$ by \emph{inserting} a line $z$ with two comparators. We number the comparators of $N'$ by \emph{times} $1,2,\dots$; then $z$ meets a line $x$ just before the comparator at time $p_1$ of $N'$ and a line $y$ just before time $p_2\ge p_1$, and at both new comparators $z$ keeps the smaller value. Let $O'$ be the set of the three lines on which $N'$ puts the three largest values. The following criterion decides whether an insertion gives a selector. A \emph{slot} of $N'$ is a pair $s=(l,\tau)$ of a line $l$ and a time $\tau$, meaning line $l$ just before the comparator at time $\tau$ (or at the end). For a pair $\{p,q\}$ of lines, run $N'$ on the input with ones on $p$ and $q$. Let $\Ones(s)$ be the set of pairs for which line $l$ holds a $1$ at time $\tau$. A pair outside $\Ones(s)$ is \emph{good} at $s$ if an extra $1$ put on line $l$ at time $\tau$ ends, together with the other two, on the lines $O'$, and \emph{bad} otherwise. Let $\Good(s)$ be the set of pairs good at $s$. \begin{proposition}[slot criterion]\label{prop:slot} The insertion of $z$ at the slots $(x,p_1)$ and $(y,p_2)$ gives a $(3,m)$-selector if and only if no pair is bad at $(x,p_1)$ and $\Ones(x,p_1)\subseteq\Good(y,p_2)$. \end{proposition} \begin{proof} By Lemmas~\ref{lem:layer} and~\ref{lem:gen} only inputs of weight $3$ matter. If $z$ holds a $0$, both new comparators leave the other value in place and the network runs as $N'$. Otherwise the input is $z$ together with a pair $\{p,q\}$, and $N'$ runs on $\{p,q\}$ undisturbed until $z$ meets a line holding a $0$. If line $x$ holds a $0$ at time $p_1$, the $1$ of $z$ moves to $x$ there and the run succeeds if and only if the pair is good at $(x,p_1)$. If line $x$ holds a $1$, $z$ keeps its $1$ until time $p_2$. If line $y$ then holds a $0$, the run succeeds if and only if the pair is good at $(y,p_2)$. If line $y$ holds a $1$, the $1$ of $z$ stays on $z$ to the end, and $z$ is not one of the three output lines: the minimum of any input, entered on $z$, ends on $z$, and it is not among the three largest. These three cases are exhaustive. \end{proof} Hence $\Uh_3(2^j+3)\ge\Uh_3(2^j+2)+3$ if and only if, in every optimal $(3,2^j+2)$-selector, for every slot $(x,p_1)$ without bad pairs and every slot $(y,p_2)$ with $p_2\ge p_1$, some pair of $\Ones(x,p_1)$ is not good at $(y,p_2)$. We checked this exhaustively at $m=7$ and on samples at $m=11$ (Table~\ref{tab:insert}). The margin is one pair: in each of the $7{,}245$ networks at $m=7$ and of $400$ sampled networks at $m=11$, the least number of pairs of $\Ones(x,p_1)$ that are not good at $(y,p_2)$, over all such pairs of slots, is exactly $1$. Between the sizes $2^j+3$, by contrast, many insertions succeed. \begin{table}[ht] \centering\small \begin{tabular}{@{}lrrr@{}} \toprule insertions into & networks & insertions & selectors \\ \midrule all optimal $(3,5)$-selectors & $3{,}450$ & $2{,}415{,}000$ & $22{,}935$ \\ all optimal $(3,6)$-selectors & $7{,}245$ & $11{,}736{,}900$ & $0$ \\ random optimal $(3,7)$-selectors & $2{,}000$ & $7{,}644{,}000$ & $71{,}552$ \\ random optimal $(3,10)$-selectors & $2{,}000$ & $34{,}200{,}000$ & $0$ \\ \bottomrule \end{tabular} \caption{Insertions of a line with a minimum path of length $2$ into optimal $(3,m-1)$-selectors, counted by brute force; the criterion of Proposition~\ref{prop:slot} gives the same counts for the first two rows. Standard networks are counted up to the order of commuting comparators.} \label{tab:insert} \end{table} \begin{conjecture}\label{conj:slot} For every $j\ge 1$, in every optimal $(3,2^j+2)$-selector, for every slot $(x,p_1)$ without bad pairs and every slot $(y,p_2)$ with $p_2\ge p_1$, some pair of $\Ones(x,p_1)$ is not good at $(y,p_2)$. Equivalently, Yao's upper bound in \eqref{eq:yao} is exact for all $n\ge 4$. \end{conjecture} The same reduction works for $t=4$, where it has to use the size of the networks (Remark~\ref{rem:nostructural}). \begin{proposition}\label{prop:sizeaware} $\Uh_4(n)=3n-10$ for all $n\ge 6$ if and only if, for every $n\ge 8$, every $(4,n)$-selector in which every minimum path has length at most $2$ has at least $3n-10$ comparators. Likewise, Yao's upper bound is exact for all $n\ge 4$ if and only if, for every $m=2^j+3\ge 19$, every $(3,m)$-selector in which every minimum path has length at most $2$ has at least $2m-5+j$ comparators. \end{proposition} \begin{proof} One direction is clear. For the other, recall $\Uh_4(6)=8$ and $\Uh_4(7)=11$, and let $n\ge 8$ be the least size with $\Uh_4(n)<3n-10$. Here the upper bounds are needed: by the bound of Knuth's exercise \cite[answer to exercise 5.3.4--20(b)]{KnuErr}, $\Uh_4(n-1)\le 3(n-1)-10$, so by minimality $\Uh_4(n-1)=3n-13$, equal to the formula. Lemma~\ref{lem:step} then implies $\Uh_4(n)\ge 3n-11$, hence $\Uh_4(n)=3n-11=\Uh_4(n-1)+2$. A minimum path of length $3$ or more in an optimal $(4,n)$-selector would leave, by the proof of Lemma~\ref{lem:step}, a $(4,n-1)$-selector with fewer than $\Uh_4(n-1)$ comparators. So every minimum path of this network has length at most $2$, and it has $3n-11$ comparators. For $t=3$, let $m$ be the least size with $\Uh_3(m){\raggedright\arraybackslash}p{0.5\textwidth}@{}} \toprule result & status in Lean \\ \midrule Lemma~\ref{lem:step} & kernel only \\ Theorem~\ref{thm:u3}, $4\le n\le 10$ & kernel only \\ Theorem~\ref{thm:u3}, $11\le n\le 18$ & compiled checks \\ Theorem~\ref{thm:u3low} & compiled checks \\ Theorem~\ref{thm:u4}, $n=6,7$ & kernel only \\ Theorem~\ref{thm:u4}, $n=8,9,10$ & compiled checks \\ Theorem~\ref{thm:u4}, $n=11,12$ & the bound $3n-11$ kernel only; the lower bound $3n-10$ not formalized \\ Theorem~\ref{thm:alek}, with its cases $t=4,8$ & kernel only \\ Theorem~\ref{thm:pow2plus}, with its case $t=64$ & kernel only \\ Theorem~\ref{thm:bands}; Remark~\ref{rem:LB} & not formalized \\ Proposition~\ref{prop:wsharp} & not formalized \\ Corollary~\ref{cor:general}, second bound & kernel only \\ Corollary~\ref{cor:general}, first bound; Lemma~\ref{lem:prune} & not formalized \\ Corollary~\ref{cor:u4} & the bound $3n-11$ kernel only; $\Uh_4(5)=4$ and the upper bound for $n\ge 13$ not formalized \\ Remark~\ref{rem:sharp} & kernel only \\ Remark~\ref{rem:nostructural}; Sections~\ref{sec:blocks} and~\ref{sec:ltp} & not formalized \\ \bottomrule \end{tabular} \caption{The status of each result in the Lean development. The upper bounds of Theorems~\ref{thm:u3} and~\ref{thm:u4} are proved by the kernel alone for every $n$ in their ranges; the compiled checks are those of the lower bounds. The single Lean statements of Theorem~\ref{thm:u3} and of $\Uh_4(n)=3n-10$ for $6\le n\le 10$ use the compiled checks; the rows marked kernel only rest on the statements for single values of $n$ and on Lemma~\ref{lem:step}.} \label{tab:lean} \end{table} \section*{Open questions} Yao's question on $\Uh_3(n)$ is open from $n=19$ on, where $\Uh_3(19)$ is $36$ or $37$. Knuth's expected equality % ALEKGAIN $\Uh_4(n)=3n-10$ is open for every $n\ge 13$, where $\Uh_4(n)$ is $3n-11$ or $3n-10$. At powers of two $t\ge 8$ we do not know how far Theorems~\ref{thm:alek}--\ref{thm:bands} are from $\Uh_t(n)$. In the weight model the least value of $\sigma+\varepsilon$ is known for $k\le 6$; for larger $k$ we know only the lower bound $k-1+\LB(k)$ of Theorem~\ref{thm:bands}, and not how fast this least value grows. \section*{Data availability} The supplement~\cite{Supp}, \href{https://doi.org/\suppdoi}{doi:\suppdoi}, contains both search programs, with their logs, and the parallel driver used for $\Uh_4(12)$; Yao's networks and the optimal $(4,n)$-selectors, with independent checkers; the two programs for the restricted searches of Section~\ref{sec:blocks}, and the data of Table~\ref{tab:insert}; the potentials and dual solutions of Section~\ref{sec:ltp}, with two checkers each; the checks of Section~\ref{sec:weights}, among them a replay of its sequences, two computations of $\LB(k)$ and the searches in the weight model; and a separate check that the certificates of % ALEKGAIN Section~\ref{sec:search} are closed. Its README explains how to re-run each check. The Lean development of Section~\ref{sec:lean}, with these certificates, is archived at~\cite{LeanRec}, \href{https://doi.org/\leandoi}{doi:\leandoi}. \bibliographystyle{amsplain} \bibliography{refs_sel} \end{document}