\documentclass[11pt]{article} \usepackage[T1]{fontenc} \usepackage{lmodern,amsmath,amssymb,amsthm,booktabs,microtype} \usepackage[hmargin=0.95in,vmargin=0.79in]{geometry} \usepackage[hidelinks]{hyperref} \hypersetup{pdftitle={The exact diameter of binary multithreaded programs},pdfauthor={Peiwei Wu and Qian Peng},pdfsubject={Reachability and shortest executions in binary multithreaded programs}} \setlength{\emergencystretch}{1em} \linespread{0.975} \newtheorem{theorem}{Theorem} \newtheorem{lemma}[theorem]{Lemma} \newtheorem{proposition}[theorem]{Proposition} \newtheorem{corollary}[theorem]{Corollary} \title{The exact diameter of binary multithreaded programs} \author{Peiwei Wu \qquad Qian Peng\thanks{Corresponding author.}\\[3pt] \small The University of Melbourne} \date{2 December 2026} \begin{document} \begingroup \makeatletter \renewcommand{\@maketitle}{\begin{center} {\LARGE\@title\par}\vspace{0.65em} {\large\begin{tabular}[t]{c}\@author\end{tabular}\par} \vspace{0.45em}\@date \end{center}} \makeatother \maketitle \endgroup \begin{abstract} Consider \(n\) labelled threads, each with one private bit, communicating through a single shared bit. Each thread has its own transition rules. We prove Malkis's conjecture that the maximum finite distance between states is exactly \(3n\) for every number \(n\ge5\) of threads. The lower bound was known. For the upper bound we show that every reachable endpoint admits an execution in which at most three threads act more than once. Three are sometimes necessary, even when there are only three threads. Once the other threads act only once, two counters and a visit condition suffice to put their moves back into a short execution. A finite certificate, verified in Lean, establishes the controller exchanges and the length bounds needed for all \(n\). \end{abstract} \section{The distance problem} A binary multithreaded program has one shared bit \(g\) and a private bit \(L_i\) for each of its \(n\) threads. A step of thread \(i\) changes the pair \((g,L_i)\) according to its own directed relation \(P_i\) on \(\{0,1\}^2\). All other private bits keep their values. A thread that needs to see a one in shared memory may have to wait for another thread to supply it. Supplying the one can also change a private bit, so the same thread may be unable to supply it a second time. For a program \(P=(P_1,\ldots,P_n)\) and states \(s,t\) with \(t\) reachable from \(s\), let \(\delta_P(s,t)\) be the minimum number of steps from \(s\) to \(t\). Write \(D(n)\) for the maximum of this distance over all such programs and all such ordered pairs. Malkis and Borgwardt proved a polynomial upper bound~\cite{icdcs}. Malkis records \(D(n)\le n^{2^{13}}\) for \(n\ge2\) in OEIS A290642~\cite{oeis}. His later analysis gives \(O(n^{2^{12}})\) in the binary case and the linear bound \(3n\) when even one thread has a strongly connected transition graph on its four local pairs~\cite{jpdc}. The first six values are \(3,7,13,14,15,18\). Malkis constructed programs attaining \(3n\) and conjectured that no program exceeds this length once \(n\ge5\)~\cite{oeis}. \begin{theorem}\label{thm:main} For every \(n\ge5\), \(D(n)=3n\). \end{theorem} \paragraph{An execution that takes exactly \(3n\) steps.} The known lower bound illustrates what the shared bit can cost. Give one clock thread the chain \[ (0,0)\longrightarrow(0,1)\longrightarrow(1,0)\longrightarrow(1,1) \] and give each of the \(n-1\) workers the sole move \((1,0)\to(0,1)\). Here and throughout, the shared bit is written first. Starting from all zeros, advance the clock twice and execute one fresh worker. This sets the worker's bit to one and returns the shared bit and the clock bit to zero. Repeat for all workers, then advance the clock three times. The all-one state has been reached in \(3(n-1)+3\) steps. To see that every execution between these endpoints has the same length, let \(k\) count the workers whose private bit is one and let \(c\) be the clock bit. Each clock move raises \(2g+c\) by one. A worker move raises \(3k\) by three and lowers \(2g+c\) by two. The total \(3k+2g+c\) therefore rises by one at every step, from zero to \(3n\). \paragraph{The upper bound.} We first choose three threads to perform all repeated moves (Section~\ref{sec:normal}). Counting the upward and downward flips of the other threads reduces the path-length question to finitely many cases (Sections~\ref{sec:counters} and~\ref{sec:finite}). Restoring the original labels gives the bound \(3n\) (Section~\ref{sec:return}). \section{Choosing the three controllers}\label{sec:normal} Complementing the shared bit and any chosen private bits simply relabels states and rules, so we take the initial state to be all zero. We can also discard self-loops. In a \emph{normal execution} three designated threads, called controllers, may move arbitrarily often. Each remaining thread, called an outside thread, moves at most once. The controller labels can depend on the endpoint. \begin{lemma}\label{lem:normal} For \(n\ge3\), every reachable endpoint admits a normal execution. \end{lemma} An outside move can be recorded by its label and its actual edge. If the move changes the shared bit, count it in \(u\) for \(0\to1\) or in \(d\) for \(1\to0\). A neutral \(0\to0\) move can be placed at the start. A neutral \(1\to1\) move requires a visit to shared value one. Thus the path search for the three controllers needs the two counts and a flag specifying whether such a visit is required. The individual edges are retained for reconstruction. At each required outside flip we may assign any as-yet-unused label whose recorded edge has that direction. The label has private input zero, because it has never acted. Its move restores exactly its prescribed final private bit. \begin{proof}[Proof of Lemma~\ref{lem:normal}] Induct on the length of an execution. In the empty execution choose any three labels as controllers. Suppose the current endpoint has a normal execution and append one legal move. A controller or an unused outside thread can move immediately. The remaining case is a second move by an outside thread \(X\). Denote the first and second edges of \(X\) by \(e,f\). Let \(x\) be its private bit after \(f\) and \(L=(L_1,L_2,L_3)\) the three controller bits just before \(f\). Keep the multiset of moves of the other outside threads. With the first edge \(e\) included, these requirements can reach the shared input of \(f\) and the controller bits \(L\). The finite implication verified in Section~\ref{sec:finite} now supplies one of the following two ways to reach the shared output of \(f\). \begin{enumerate} \item Keep the three controllers with endpoint \(L\). Thread \(X\) stays idle if \(x=0\), or makes one of the edges \(e,f\) from private zero to \(x\). \item Replace controller \(i\) by \(X\), allowing \(X\) to use the two edges \(e,f\) and requiring it to end at \(x\). The other two controllers keep their rules and endpoints. The displaced thread stays idle if \(L_i=0\), or makes one of its own edges from private zero to \(L_i\). \end{enumerate} In either case the retained outside moves can be assigned back to their labels in the order of the new execution. The shared bit and every private bit have the required final values. This closes the induction. \end{proof} \begin{proposition}\label{prop:three} For each \(n\ge3\), some binary program on \(n\) threads has two states such that the second is reachable from the first and every execution between them uses at least three threads more than once. \end{proposition} \begin{proof} Encode a local pair \((g,\ell)\) by \(g+2\ell\). Give three active threads \(A,B,C\) the following edges. Any further threads have no edges. \[ \begin{array}{c|l} A & 1\to3,\ 2\to3,\ 3\to1\\ B & 1\to2,\ 3\to0\\ C & 0\to3,\ 2\to1 \end{array} \] Start at zero and aim for shared bit one with all private bits zero. The schedule \(C,A,B,A,A,B,C\) reaches this target. To see why all three threads are needed, encode a state by \(g+2L_A+4L_B+8L_C\). Applying the displayed edges with one thread disabled gives the following reachable sets from zero: \[ \begin{array}{c|ccc} \text{disabled thread}& A&B&C\\ \hline \text{reachable states}&\{0,5,9,12\}&\{0,9,11\}&\{0\}. \end{array} \] The target has code \(1\), so each active thread must move. Its first move puts its own bit at one, and only it can return that bit to zero. Hence each must move at least twice. Adding idle threads leaves these executions and reachable sets unchanged. \end{proof} \section{From unbounded counts to a finite table}\label{sec:counters} After suppressing neutral outside moves a trace has four state bits, namely the shared bit and the three controller bits. Its length counts controller moves and outside flips. Write \(m=\min(u,d)\) and \(h=|u-d|\). Fold \(m\) to \(\min(m,3)\). Keep \(h\) if \(h\le5\), and otherwise fold it to \(5+((h-5)\bmod 2)\). Reconstruct a representative pair with the original direction of the excess. The folded excess is at most six, so the larger representative counter is at most \(3+6=9\). The finite table associates a family of three-controller programs to each representative pair, four-bit endpoint and visit flag. Its initial entry contains every program. Its entries are closed under a legal controller move, an outside flip and dropping a visit requirement. A flip in either direction visits one. Checking a representative and the result of adding one flip requires only \(m\le3\) and \(h\le7\): beyond these cutoffs the minority count has saturated and the excess depends only on parity. These implications carry any actual trace forward into the table. To recover a short execution from a table entry we must also undo the folding. \begin{lemma}\label{lem:lift} Set \(B(u,d)=\max\{3(3+u+d),\,13+u+d\}\). Suppose a folded pair has a trace meeting its endpoint and visit requirement within that pair's budget \(B\). The original pair then has a trace to the same endpoint meeting that requirement within \(B(u,d)\). \end{lemma} \begin{proof} One lost unit of the minority count is restored by inserting an up flip followed by a down flip at the start. This costs two steps and restores the initial shared value. It suffices first to restore any excess, which was discarded in pairs. Consider an excess of up flips. The representative already has \(u-d\ge5\). Since the final shared bit is zero or one, the controllers together supply at least \(u-d-1\ge4\) more down flips than up flips. Some controller contributes at least two. Give its edges weight \(+1\) for a down flip, \(-1\) for an up flip and zero otherwise. Remove simple cycles from its walk on its two private states. The remaining simple path has at most one edge, so some removed cycle has positive weight. A one-edge cycle of positive weight is a down flip that preserves the private bit. It can be followed by an outside up flip and the two steps repeated. A two-edge cycle of positive weight has either two down flips or one down flip and a neutral edge. In the first case interleave the down flips with outside up flips. In the second, start with the down edge: perform the neutral edge before the outside up flip if it needs shared zero, and after that flip if it needs shared one. Repeat this cycle twice. In each case the insertion starts at an occurrence of the source of a down edge in the original trace, returns the shared and controller bits to their original values, and adds two up flips in at most six steps. For an excess of down flips, the final shared bit \(g\) and the initial value zero give net upward controller movement \(d-u+g\ge d-u\ge5\). The same cycle insertion, with up and down exchanged, adds two down flips. In the excess regime \(3+u+d\ge8\), so increasing the excess by two increases the budget by six. Inserting a balanced pair increases the budget by at least two. All insertions preserve a required visit to one. \end{proof} For example, \((u,d)=(11,4)\) has minority count four and excess seven. Its representative is \((8,3)\). A pair of additional up flips first gives \((10,3)\); a balanced pair then restores \((11,4)\). Only the representative needs a stored path. \section{What the finite certificate checks}\label{sec:finite} Each controller relation is a subset of the twelve directed non-loop edges on four local states. A triple of relations is therefore a 36-bit mask. For a fixed endpoint and outside requirements, a path legal for a mask remains legal if more edges are enabled. An upward-closed set of masks is determined by its inclusion-minimal members. The certificate represents repeated parts of these families by a shared decision diagram and checks their minimal masks against a list of \(15{,}200\) programs. There are three parts to the verification. First, the initial state and the transition inclusions described in Section~\ref{sec:counters} ensure that every reachable requirement occurs in the table. Second, to justify Lemma~\ref{lem:normal}, a table entry before a second use of \(X\) must imply at least one of the two conclusions in that proof. The replacement test substitutes the edge indicators for \(e,f\) in controller position \(i\), retaining the indicators of the other two programs. The displaced thread's single move is tested against its \emph{original} rules. A successful conclusion for a minimal antecedent mask remains successful for every larger mask. The size of the exchange check can be counted directly. With private input zero there are six choices for the first non-loop edge \(e\). With \(e\) fixed, the private input of \(f\) is fixed and there are six choices for \(f\). There are four minority counts. Stored representatives have signed excess between \(-6\) and \(6\), but a single flip can cross that boundary. The exchange check tests all fifteen differences from \(-7\) to \(7\) before folding. For example, excesses five and seven have the same representative, yet a flip toward zero takes them to four and six. The shared input of \(f\) is fixed, leaving \(2^3=8\) possible controller private endpoints. At zero outside flips a required visit to one adds eight cases, one per endpoint. This gives \[ 6\cdot6\,(4\cdot15\cdot8+8)=17{,}568 \] exchange implications in the independent minimal-mask check. Third, membership at a representative must yield an actual trace within budget. This is checked by induction on \(u+d\). One may append a final outside flip to a shorter trace or insert a balanced pair. Where these extensions do not cover a minimal mask, the certificate supplies a stored path, allowing a permutation of the three controller labels. The verifier checks its individual moves, endpoint, counters, visit flag and length. A separate inclusion test checks that the extensions and stored paths cover every required mask. When there are no outside flips and a visit to one is required, the allowance is thirteen steps. Table~\ref{tab:witness} summarizes the Lean certificate. A separate C++ implementation builds the antichains directly and checks distances by breadth-first search. It verifies the exchange implications on \(6{,}458{,}424\) minimal antecedents and checks \(586{,}437\) shortest-path requirements. \begin{table}[htbp] \centering \setlength{\belowcaptionskip}{4pt} \caption{Finite witnesses for the three-controller certificate.} \label{tab:witness} \begin{tabular}{@{}lr@{}} \toprule Witness data & Count\\ \midrule Internal gates of the decision diagram & 10,147\\ Distinct listed three-controller programs & 15,200\\ Canonical short paths & 12,909\\ Boundary requirements checked for path coverage & 840\\ Path certificates after relabelling, counted per target & 70,637\\ \bottomrule \end{tabular} \end{table} \section{The length of the reconstructed execution}\label{sec:return} \begin{corollary}\label{cor:bound} For \(n\ge3\), \(D(n)\le\max\{3n,n+10\}\). \end{corollary} \begin{proof} Choose a normal execution by Lemma~\ref{lem:normal}. Let \(r\) count the outside moves that preserve the shared bit. Since each outside thread acts at most once, \(3+u+d+r\le n\). The finite path check and Lemma~\ref{lem:lift} give a trace, including the \(u+d\) outside flips, of length at most \(B(u,d)\). Assign these flips to their recorded labels and insert each neutral move at the start or at a visit to one, as its shared input requires. This reaches the prescribed endpoint in at most \(B(u,d)+r\) steps. The two terms satisfy \[ 3(3+u+d)+r\le 3n,\qquad 13+u+d+r\le n+10.\qedhere \] \end{proof} For three and four threads the bound is respectively 13 and 14, matching the known values~\cite{oeis}. At five threads the two terms coincide. For \(n\ge5\) the clock construction attains \(3n\), proving Theorem~\ref{thm:main}. Together these results give \(D(n)=\max\{3n,n+10\}\) for every \(n\ge3\). The constant \(B(0,0)=13\) agrees with \(D(3)\); the finite path check also enforces this budget when the trace must visit shared value one. \paragraph{Beyond binary programs.} If private states may take three values, each thread can be forced to follow \(0\to1\to2\), so reaching the all-two state can require every thread to move twice. Keeping the private bit binary isolates a different question. With three shared states, how many threads can be forced to act more than once on the way to a reachable endpoint? \paragraph{Proof materials.} The \href{https://github.com/zxcipo753/binary-multithreaded-program-diameter}{repository} and its Zenodo archive (DOI \href{https://doi.org/10.5281/zenodo.24748081}{10.5281/zenodo.24748081}) provide the sources, build instructions and C++ verifier. In Lean~4.33.1, the kernel checks \texttt{MalkisDiameter.\allowbreak exact\_diameter} using only \texttt{propext}, \texttt{Classical.choice} and \texttt{Quot.sound}. Its finite certificates are checked in the kernel, without \texttt{native\_decide}. With compiled dependencies, the main theorem module took about six seconds to recheck on Apple silicon. \texttt{ThreeNecessary.lean} checks the five-thread example. \texttt{UniformBound.lean} also derives the small-case bounds from \(D(5)=15\). Adjoining a thread that must flip its private bit once while preserving the shared bit increases the prescribed distance by exactly one. Thus for \(n<5\), padding to five threads gives \(D(n)+(5-n)\le15\). \paragraph{Acknowledgments.} We thank \texttt{moridinamael} for suggesting the shared-bit flux argument and \texttt{kent234535} for proposing the three-controller bound and testing it on four-thread programs. \paragraph{Author contributions and use of AI.} Peiwei Wu and Qian Peng directed the investigation of Malkis's diameter conjecture. They used OpenAI Codex to develop the controller-exchange and length arguments, construct and test the finite certificates, formalize the proofs in Lean, and draft and revise the manuscript. \small \begin{thebibliography}{9}\raggedright \bibitem{oeis} A. Malkis, \emph{Maximum diameter for an \(n\)-threaded binary program}, OEIS A290642, 2017. \url{https://oeis.org/A290642}. \bibitem{icdcs} A. Malkis and S. Borgwardt, Reachability in binary multithreaded programs is polynomial, \emph{37th IEEE International Conference on Distributed Computing Systems}, 2017, 2083--2088. \href{https://doi.org/10.1109/ICDCS.2017.290}{doi:10.1109/ICDCS.2017.290}. \bibitem{jpdc} A. Malkis, Reachability in parallel programs is polynomial in the number of threads, \emph{J. Parallel Distrib. Comput.} \textbf{162} (2022), 1--16. \href{https://doi.org/10.1016/j.jpdc.2021.11.008}{doi:10.1016/j.jpdc.2021.11.008}. \end{thebibliography} \end{document}