Ramsey goodness of books revisited

Abstract: The Ramsey number $r(G,H)$ is the minimum $N$ such that every graph on $N$ vertices contains $G$ as a subgraph or its complement contains $H$ as a subgraph. For integers $n \geq k \geq 1$, the $k$-book $B_{k,n}$ is the graph on $n$ vertices consisting of a copy of $K_k$, called the spine, as well as $n-k$ additional vertices each adjacent to every vertex of the spine and non-adjacent to each other. A connected graph $H$ on $n$ vertices is called $p$-good if $r(K_p,H)=(p-1)(n-1)+1$. Nikiforov and Rousseau proved that if $n$ is sufficiently large in terms of $p$ and $k$, then $B_{k,n}$ is $p$-good. Their proof uses Szemerédi's regularity lemma and gives a tower-type bound on $n$. We give a short new proof that avoids using the regularity method and shows that every $B_{k,n}$ with $n \geq 2^{k^{10p}}$ is $p$-good.Using Szemerédi's regularity lemma, Nikiforov and Rousseau also proved much more general goodness-type results, proving a tight bound on $r(G,H)$ for several families of sparse graphs $G$ and $H$ as long as $|V(G)| < \delta |V(H)|$ for a small constant $\delta > 0$. Using our techniques, we prove a new result of this type, showing that $r(G,H) = (p-1)(n-1)+1$ when $H =B_{k,n}$ and $G$ is a complete $p$-partite graph whose first $p-1$ parts have constant size and whose last part has size $\delta n$, for some small constant $\delta>0$. Again, our proof does not use the regularity method, and thus yields double-exponential bounds on $\delta$.

\section{Introduction}
For two graphs $G,H$, their \emph{Ramsey number} $r(G,H)$ is the smallest $N$ such that every graph $\Gamma$ on $N$ vertices contains $G$ as a subgraph, or its complement contains $H$ as a subgraph. The existence of $r(G,H)$ is guaranteed by Ramsey's theorem \cite{Ramsey}. The most well-studied Ramsey number is the \emph{diagonal} Ramsey number $r(K_k, K_k)$. One of the oldest (and easiest) results in Ramsey theory is the fact that $r(K_k,K_k) \geq (k-1)^2+1$, which is proved by taking $\Gamma$ to be the complete balanced $(k-1)$-partite graph on $(k-1)^2$ vertices.

This quadratic lower bound is far from best possible. Indeed, it is known \cite{Erdos47,ErSz} that $r(K_k,K_k)$ must grow exponentially in $k$, though the exact exponential rate remains unknown despite decades of intense research. Nonetheless, it is an instance of a much more general inequality which can be tight. Write $\chi(G)$ for the chromatic number of $G$. The inequality in question is then
\begin{equation}\label{eq:goodness}
r(G,H) \ge (p-1)(n-1)+a,
\end{equation}
which holds under the conditions that $\chi(G)=p$ and $a$ is the minimum size of a color class among all proper $p$-colorings of $G$, and $H$ is a connected graph with $n$ vertices. Inequality (\ref{eq:goodness}) was first proved by Burr~\cite{Burr}, by taking $\Gamma$ to be a complete $p$-partite graph with $p-1$ parts of size $n-1$ and one part of size $a-1$.

Burr and Erd\H os \cite{BuEr} initiated the study of when (\ref{eq:goodness}) is tight; following their terminology, one says that a connected $n$-vertex graph $H$ is \emph{$G$-good} if (\ref{eq:goodness}) is tight when $\chi(G)=p$ and $a$ is the minimum size of a color class among all proper $p$-colorings of $G$. In case $G=K_p$, one says that $H$ is \textit{$p$-good}, rather than $K_p$-good.

While the Ramsey goodness bound (\ref{eq:goodness}) is far from tight in the case of cliques, it turns out that many interesting graphs are $p$-good, and that the theory of Ramsey goodness generalizes many important results in graph theory. For example, Tur\'an's theorem, which states that the balanced complete $(p-1)$-partite graph has the most edges among all $K_p$-free graphs on $N$ vertices, is equivalent to the fact that stars are $p$-good.
Extending this fact, Chv\'atal~\cite{Chvatal} proved that all trees are $p$-good for all $p \geq 3$, and this theorem inspired Burr and Erd\H os to define Ramsey goodness. At this point, there is a rich theory of Ramsey goodness, about which we refer the interested reader to the survey \cite[Section 2.5]{CoFoSu15}.

For $n \geq k \geq 1$, the $k$-book $\book kn$ on $n$ vertices consists of a copy of $K_k$, called the \emph{spine}, as well as $n-k$ additional vertices each joined to every vertex of the spine; equivalently, $\book kn$ consists\footnote{We remark that other notation exists for book graphs; notably, some other papers (e.g.\ \cite{Conlon,CFW,NR04}) use $B_{n-k}\up k$ to denote what is $\book kn$ in our notation.} of $n-k$ cliques of order $k+1$ sharing a common $K_k$. Book graphs arise naturally in the study of Ramsey numbers. Indeed, Ramsey \cite{Ramsey} originally proved the finiteness of $r(K_k,K_k)$ by proving the finiteness of $r(\book kn, \book kn)$ for every $n$, and it was observed by Erd\H os, Faudree, Rousseau, and Schelp \cite{ErFaRoSc} that the classical Erd\H os--Szekeres \cite{ErSz} upper bound on Ramsey numbers can also be framed as an upper bound on certain book Ramsey numbers. This connection yields an important approach to improving upper bounds on $r(K_k,K_k)$; for more details, see e.g.\ \cite{Conlon,CFW}.

In \cite{NR04}, Nikiforov and Rousseau used Szemer\'edi's regularity lemma to prove that for every $k,p \geq 1$ and every sufficiently large $n$, the book $\book kn$ is $p$-good.
One consequence of applying the regularity method is that their proof yields tower-type bounds on how large $n$ must be in terms of $k$ and $p$, and they raised the question of what the best possible $n$ is. Our first main result is a new proof of $p$-goodness for books which avoids the use of the regularity lemma, and thus gets a much better dependence for $n$ on $k$ and $p$.
\begin{theorem}\label{thm:main1}
    If $n \geq 2^{k^{10p}}$, then $\book kn$ is $p$-good.
\end{theorem}
Our main technique is a novel variant of the greedy embedding strategy, which allows us to build a large induced copy of a complete multipartite graph inside a $K_p$-free graph whose complement does not contain a very large book.

Extending the techniques from \cite{NR04}, Nikiforov and Rousseau \cite{NR09} were later able to prove a remarkable theorem, which remains the most general result in the field of Ramsey goodness. As the result in its full generality requires some notation, we state only the following special case.

\begin{theorem}[{Nikiforov and Rousseau \cite[Theorem 2.12]{NR09}}] \label{thm:book-book}
    For every $k,p\geq 2$, there exists some $\delta>0$ such that for all sufficiently large $n$,
    \[
        r(\book {p-1}{\delta n}, \book kn) = (p-1)(n-1)+1.
    \]
\end{theorem}
In other words, the Ramsey goodness result $r(K_p, \book kn) = (p-1)(n-1)+1$ remains true even if we replace $K_p$ by the much larger graph $\book {p-1}{\delta n}$ containing it.
This result goes beyond the basic Ramsey goodness framework introduced by Burr and Erd\H os, to show that $r(G,H) = (p-1)(n-1)+1$ in cases even when $G$ is not a fixed graph.

Just as before, the proof of \cite{NR09} uses Szemer\'edi's regularity lemma, and hence the bound on $1/\delta$ in \cref{thm:book-book} is of tower type. In order to demonstrate the flexibility of our proof technique, we prove the following generalization of \cref{thm:book-book}, which again goes beyond the basic Ramsey goodness framework of Burr and Erd\H os.
\begin{theorem}\label{thm:multipartite}
    For every $k,p,t \geq 2$, there exists $\delta>0$ such that the following holds for all $n\ge 1$. Let $1 \leq a_1 \leq \dotsb \leq a_{p-1} \leq t$ and $a_p \leq \delta n$ be positive integers. Let $G$ be the complete $p$-partite graph with parts of sizes $a_1,\dots,a_p$, and let $H = \book kn$. Then $r(G,H) = (p-1)(n-1)+a_1$ if and only if $a_1 = a_2 = 1$.
\end{theorem}
Note that \cref{thm:multipartite} is vacuously true unless $n$ is sufficiently large, as otherwise there does not exist a positive integer $a_p \le \delta n$. Our proof shows that one may take $1/\delta$ to be double-exponential in $k,p$, and $t$. Additionally, once $n$ is double-exponential in $k,p$, and $t$, the statement holds with $1/\delta$ merely single-exponential in $k,p,$ and $t$.

Although \cref{thm:multipartite} has not appeared in the literature, the ``if'' direction (which is the harder one) can be deduced from the general theorem of Nikiforov and Rousseau \cite[Theorem 2.1]{NR09}. Nonetheless, the main novelty is not the statement of \cref{thm:multipartite}, but rather the fact that our proof again avoids the use of the regularity lemma, so that the bounds on $1/\delta$ are not of tower-type. It would be very interesting to see how far one can push these ideas; for example, is it possible to completely eliminate the use of the regularity lemma from the proof of \cite[Theorem 2.1]{NR09}?

\vspace{3mm} \noindent {\bf Organization.} In \cref{sec:good-book}, we warm up by proving \cref{thm:book-book}; in fact, we prove a generalization that sets the groundwork for \cref{thm:multipartite}. In \cref{sec:stability-supersaturation}, we prove a stability-supersaturation version of Tur\'an's theorem, and use that to prove a variant of the Andr\'asfai--Erd\H os--S\'os theorem, \cref{thm:aes-variant}, in \cref{sec:aes-variant}. \cref{thm:aes-variant} is an important ingredient in the proof of \cref{thm:multipartite}, as it essentially allows us to reduce to the case that $\Gamma$ is $(p-1)$-partite. While both such results are relatively standard, the specific statements we need are apparently new. Finally, the proof of \cref{thm:multipartite} is completed in \cref{sec:main-proof}, and we collect some interesting open problems in \cref{sec:concluding}.

For the sake of clarity of presentation,
we omit floor and ceiling signs when they are not crucial.

\section{Ramsey goodness of books}\label{sec:good-book}

Let $K_r(t)$ denote the complete $r$-partite graph with parts of size $t$. The following result is the greedy embedding lemma that we use. Given a graph $\Gamma$, it allows us to find a large book in $\ol \Gamma$ or find a large induced complete multipartite subgraph of $\Gamma$.

\begin{lemma}\label{lem:greedy}
Let $k, r, s, t$ be positive integers with $s \leq t$ and $2k \le t$, and let $G$ be any graph.  Let $\Gamma$ be a $G$-free graph with $N \geq \binom ts^r \frac{t}{2ks}r(G, K_s)$ vertices which contains $K_{r}(t)$ as an induced subgraph, with parts $V_1,\dots,V_r$. If $\overline \Gamma$ does not contain a book $\book kn$ with $n \geq (1-4ks/t)N/r$ vertices, then $\Gamma$ contains an induced copy of $K_{r+1}(s)$ with parts $W_0,\dots,W_r$, where $W_i \subseteq V_i$ for every $1 \leq i \leq r$.
\end{lemma}

\begin{comment}
\begin{lemma}\label{lem:greedy}
Let $s,t,r$ be positive integers with $s \leq t$. Let $\eps \in [s/t,1]$.  Let $G$ be an $H$-free graph with $N \geq \left({t\choose s}/{\eps t \choose s}\right)^r (2k\eps)^{-1}r(H,K_s)$ vertices which contains $K_{r}(t)$ as an induced subgraph, with parts $V_1,\dots,V_r$. If $\overline G$ does not contain a book $\book kn$ with $k \geq t/2$ and $n \geq (1-4k\eps)N/r$ vertices, then $G$ contains an induced copy of $K_{r+1}(s)$ with parts $W_0,\dots,W_r$, where $W_i \subseteq V_i$ for every $1 \leq i \leq r$.
\end{lemma}
\end{comment}
\begin{proof}
Let $\eps = s/t$. Partition the vertex set of $\Gamma$ into $r+1$ parts $U_0,U_1,\ldots,U_r$, where, for each $i \in [r]$, every vertex in $U_i$ has degree at most $\eps t$ to $V_i$, and every vertex in $U_0$ has degree at least $\eps t$ to each $V_j$. Note that by construction, $V_i \subseteq U_i$ for $i\in [r]$.

Suppose there is $i \in [r]$ such that $|U_i| \geq (1-2k\eps)N/r$. Let $X$ denote the set of all vertices $v \in V_i$ with at most $2\eps|U_i \setminus V_i|$ neighbors in $U_i \setminus V_i$. Since each vertex in $U_i$ has density at most $\eps$ to $V_i$, we have $|X| \geq |V_i|/2 = t/2\geq k$. Let $Q$ be any $k$ vertices in $X$. Then all but at most a $2k\eps $ fraction of the vertices in $U_i \setminus V_i$ are empty to $Q$. So $Q$ together with the vertices of $U_i$ that have have no neighbors in $Q$ form a $k$-book in $\overline \Gamma$ with at least
$(1-2k\eps)|U_i \setminus V_i|+|V_i| \geq (1-4k\eps)N/r$ vertices.

So we may assume that there is no $i \in [r]$ with $|U_i| \geq (1-2k\eps)N/r$. In this case, we have $|U_0| \geq N-r(1-2k\eps)N/r=2k\eps N$. By the pigeonhole principle, there is
a subset $T \subset U_0$ of size at least ${\binom ts}^{-r}|U_0| \geq r(G, K_s)$ such that there are subsets $W_i \subseteq V_i$ with $|W_i|=s$ for $i\geq 1$ such that every vertex in $T$ is complete to each $W_i$. As $\Gamma$ and hence the induced subgraph $\Gamma[T]$ is $G$-free and $|T| \geq r(G, K_s)$, we know that $T$ contains an independent set $W_0$ of order $s$. Then $W_0,W_1,\ldots,W_r$ form a complete induced $(r+1)$-partite subgraph of $\Gamma$ with parts of size $s$.
\end{proof}
Our next lemma shows that, once we find a large induced complete multipartite subgraph of $\Gamma$, we can find a large book in $\ol \Gamma$.

\begin{lemma}\label{lem:simple}
If a $K_p$-free graph $\Gamma$ on $n$ vertices contains $K_{p-1}(k)$ as an induced subgraph, then its vertex set can be partitioned into $p-1$ subsets that each span a $k$-book in $\overline \Gamma$.
\end{lemma}
\begin{proof}
Let $V_1,\ldots,V_{p-1}$ be the $p-1$ parts of the induced $K_{p-1}(k)$. As $\Gamma$ is $K_p$-free, each vertex in $\Gamma$ has no neighbors in some $V_i$. Partition the vertex set of $\Gamma$ into $p-1$ parts $U_1,\ldots,U_{p-1}$, where, for each $i \in [p-1]$, each vertex in $U_i$ has no neighbors in $V_i$. Then each $U_i$ spans a $k$-book in $\overline \Gamma$ with spine $V_i$.
\end{proof}

Our next result is the main form in which we use \cref{lem:greedy}, and follows from it by a simple inductive argument.

\begin{lemma}
\label{lem:find-blowup}
Let $k,p,x$ be positive integers, and let $z = x\cdot(20k)^{p}$. Let $\Gamma$ be a $K_p$-free graph on at least $N=(p-1)(n-1)+1$ vertices, and suppose $S \subseteq V(\Gamma)$ satisfies $\ab S \ge z^z \cdot r(K_p, K_z)$. Then either $\ol \Gamma$ contains a copy of $\book kn$, or else $\Gamma$ contains $K_{p-1}(x)$ as an induced subgraph, one part of which is a subset of $S$.
\end{lemma}

\begin{proof}
For $r=1,\ldots,p-2$, let $\eps_r = \left(1-r/(p-1)\right)/(4k)$ so that $(1-4k\eps_r)/r=1/(p-1)$. Let $t_{p-1}=x$ and $t_r=t_{r+1}/\eps_r$ for $r=p-2,\ldots,1$.
Observe that
\[
t_1=t_{p-1}/\prod_{r=1}^{p-2} \eps_r = x(4k)^{p-2}(p-1)^{p-2}/(p-2)!<(20k)^p x = z.
\]
Since $t_1 \geq t_2 \geq \dotsb \geq t_{p-1}$, this implies that $t_r <z$ for all $r$. We now prove by induction on $r$ for $r \in [p-1]$ that $\Gamma$ contains $K_r(t_r)$ as an induced subgraph, with the first part of $K_r(t_r)$ being a subset of $S$.

For the base case $r=1$, we have $\ab S \geq r(K_p, K_z) > r(K_p,K_{t_1})$, so $\Gamma$ contains an independent set of order $t_1$, that is, $\Gamma[S]$ contains $K_r(t_r)$ with $r=1$ as an induced subgraph.

Now suppose $\Gamma$ contains $K_r(t_r)$ as an induced subgraph, with the first part a subset of $S$. We apply Lemma \ref{lem:greedy} with $s=t_{r+1}$, $t=t_r$, and $G = K_p$.
Observe that
\begin{eqnarray*}
\binom{t_{r+1}}{t_r}^r (2kt_{r+1}/t_r)^{-1}r(K_p, K_{t_{r+1}})  & \leq & (e/\eps_r)^{rt_r}(2kt_{r+1}/t_r)^{-1}r(K_p, K_{t_{r+1}}) \\
& < & z^z \cdot r(K_p, K_z) \le |S|.
\end{eqnarray*}
So either $\ol \Gamma$ contains a $k$-book with at least $(1-4k\eps_r)N/r=N/(p-1) \ge n$ vertices, in which case we are done, or $\Gamma$ contains an induced $K_{r+1}(t_{r+1})$ whose first $r$ parts are subsets of the $r$ parts of the $K_r(t_r)$. In particular, the first part of this induced $K_{r+1}(t_r)$ is a subset of $S$. This proves the claimed inductive statement. The desired statement is just then the case $r=p-1$.
\end{proof}
We are now ready to prove \cref{thm:main1}, whose statement we now recall.
\begin{thm:main1}
If $n \geq 2^{k^{10p}}$, then $\book kn$ is $p$-good, that is, $r(K_p,\book kn) = (p-1)(n-1)+1$.
\end{thm:main1}
\begin{proof}
Let $N=(p-1)(n-1)+1$. Our choice of $n$ guarantees that if $z=k(20k)^p$, then $N\ge z^z \cdot r(K_p, K_z)$. Suppose for the sake of contradiction that there is a $K_p$-free graph on $N$ vertices such that $\overline \Gamma$ does not contain a $k$-book with $n$ vertices. By \cref{lem:find-blowup}, applied with $S=V(\Gamma)$ and $x = k$, we see that $\Gamma$ must contain $K_{p-1}(k)$ as an induced subgraph. But then Lemma \ref{lem:simple} implies that $\ol \Gamma$ contains a $k$-book with $n$ vertices as a subgraph, completing the proof.
\end{proof}

\section{A stability-supersaturation theorem}\label{sec:stability-supersaturation}

One of our main tools is a version of the Erd\H os--Simonovits stability version of Tur\'an's theorem. While many variants of the stability theorem are known, we were not able to find the following result in the literature, though its proof is similar to the proofs of several known results. Roughly speaking, this result combines two types of well-known variants of Tur\'an's theorem. The first, namely the Erd\H os--Simonovits stability theorem \cite{MR0232703, MR0233735}, says that if $\Gamma$ is a $K_p$-free graph with slightly fewer edges than the Tur\'an graph, then $\Gamma$ can be turned into the Tur\'an graph by changing a small number of edges. The second, often known as a supersaturation result \cite{MR726456}, says that if $\Gamma$ is an $m$-vertex graph with slightly \emph{more} edges than the $K_p$-free Tur\'an graph, then it actually contains many (that is, $\Omega(m^p)$) copies of $K_p$. Contrapositively, this latter result says that if $\Gamma$ has few copies of $K_p$, then it cannot have substantially more edges than the Tur\'an graph.

The result that we need, a combination of the two mentioned above, is the following. It asserts that if $\Gamma$ has slightly fewer edges than the Tur\'an graph (the stability regime) and has few copies of $K_p$ (the supersaturation regime), then it is close to the Tur\'an graph.

\begin{theorem}\label{thm:stability}
	For every $\varepsilon>0$ and every integer $p \geq 3$, there exist $\eta,\gamma>0$ such that the following holds for all $m \geq 5$. Suppose $\Gamma$ is a graph on $m$ vertices with minimum degree at least $(1- \frac{1}{p-1}- \gamma)m$ and at most $\eta m^p$ copies of $K_p$. Then $V(\Gamma)$ can be partitioned into $V_1 \sqcup \dotsb \sqcup V_{p-1}$, such that the total number of internal edges in $V_1,\dotsc,V_{p-1}$ is at most $\varepsilon \binom m2$.

	Moreover, we may take $\gamma= \min \{1/(2p^2),\varepsilon/2\}$ and $\eta = p^{-10p} \varepsilon$.
\end{theorem}
A natural approach to prove \cref{thm:stability} is to first apply the celebrated graph removal lemma (see the survey \cite{MR3156927}). This allows us to pass to a $K_p$-free subgraph $\Gamma'$ of $\Gamma$ which still has very many edges. At this point, we can apply the standard stability theorem to deduce that $\Gamma'$ is nearly $(p-1)$-partite; since we deleted few edges to go from $\Gamma$ to $\Gamma'$, we must also have that $\Gamma$ is nearly $(p-1)$-partite. This proof technique was used to prove \cite[Corollary 3.4]{MR4170438}, which is a very similar result to \cref{thm:stability}. This proof technique actually proves a stronger theorem than \cref{thm:stability}, weakening the minimum degree condition to an average degree condition.

However, since the known bounds in the graph removal lemma are very weak, this proof technique would yield a tower-type dependence in the parameters $\varepsilon$ and $\eta$ in the statement of \cref{thm:stability}. Moreover, a super-polynomial dependence on the parameters is unavoidable if one only assumes an average degree condition. Indeed, let $\Gamma$ be the disjoint union of a Tur\'an graph on $(1- \gamma)m$ vertices and a graph $\Gamma_0$ on $\gamma m$ vertices which is extremal for the $K_p$ removal lemma, so that $\Gamma$ has at least $(1- \frac{1}{p-1}- \gamma)\binom m2$ edges. Then the distance of $\Gamma$ from being $(p-1)$-partite is roughly the same as the distance of $\Gamma_0$ from being $K_p$-free, and it is known that the clique removal lemma requires super-polynomial bounds in general. Such a construction shows that the clique removal lemma and stability-supersaturation theorems like \cref{thm:stability} are very closely related.

The $\Gamma$ constructed has high average degree but low minimum degree, and this distinction turns out to be crucial. Indeed, in \cite{2105.09194}, Fox and Wigderson proved that the $K_p$ removal lemma has \emph{linear} bounds if the minimum degree of $\Gamma$ is above a certain threshold, namely $(1-\frac{2}{2p-3})m$. This allows us to prove \cref{thm:stability} using the technique outlined above, while obtaining much stronger quantitative control.

The first tool we need to prove \cref{thm:stability} is the high-degree removal lemma with linear bounds mentioned above, from \cite[Theorem 2.1]{2105.09194}. We remark that the explicit $p$-dependence of the constant is not given in \cite[Theorem 2.1]{2105.09194}, but it is easy to verify that the proof yields the following result.
\begin{theorem}\label{thm:removal-above-threshold}
	Let $\Gamma$ be an $m$-vertex graph with with minimum degree at least $(1- \frac{2}{2p-3}+\beta)m$ and with at most $(10p)^{-2p} \beta \lambda m^p$ copies of $K_p$. Then $\Gamma$ can be made $K_p$-free by deleting at most $\lambda m^2$ edges.
\end{theorem}
\noindent We also use the following quantitative form of the stability theorem, due to F\"uredi \cite{MR3383250}.
\begin{theorem}\label{thm:furedi}
	Let $\Gamma$ be an $m$-vertex $K_p$-free graph with at least $(1- \frac{1}{p-1})\frac{m^2}{2}-\ell$ edges. Then $\Gamma$ can be made $(p-1)$-partite by deleting at most $\ell$ edges.
\end{theorem}
\noindent With these preliminaries, we can now prove \cref{thm:stability}.
\begin{proof}[Proof of \cref{thm:stability}]
	Since $\gamma \leq 1/(2p^2)$, we see that
	\[
		1- \frac{1}{p-1} - \gamma \geq 1 - \frac{1}{p-1} -\frac{1}{2p^2} = 1- \frac{2}{2p-3} + \frac{5p-3}{4p^4-10p^3 + 6p^2} \geq 1 - \frac{2}{2p-3} + \frac{1}{p^3}.
	\]
	Therefore, we may apply \cref{thm:removal-above-threshold} with $\beta = 1/p^3$. We also set $\lambda = \varepsilon/10$, and note that the number of $K_p$ in $\Gamma$ is at most
	\[
		\eta  m^p = p^{-10p} \varepsilon m^p \leq (10p)^{-2p} \cdot \frac{1}{p^3} \cdot \frac \varepsilon {10} \cdot m^p = (10p)^{-2p} \beta \lambda m^p.
	\]
This implies that we may delete at most $\frac \varepsilon {10} m^2$ edges from $\Gamma$ to obtain a $K_p$-free graph $\Gamma'$. Since $\Gamma$ has minimum degree at least $(1- \frac{1}{p-1}- \gamma)n$, we see that $\Gamma'$ has at least $(1- \frac{1}{p-1} - \gamma)\frac{m^2}{2}- \frac \varepsilon {10}m^2$ edges. Therefore, by \cref{thm:furedi}, we see that $\Gamma'$ can be made $(p-1)$-partite by deleting at most $(\frac \gamma 2 + \frac\varepsilon {10})m^2$ edges. Let $\Gamma''$ be this $(p-1)$-partite subgraph, and let $V_1 \sqcup \dotsb \sqcup V_{p-1}$ be its $(p-1)$-partition. Since each $V_i$ is an independent set in $\Gamma''$, we see that the total number of edges of $\Gamma$ contained in $V_1,\dots,V_{p-1}$ is at most
	\[
		\frac \varepsilon {10}m^2 + \left(\frac \gamma 2 + \frac \varepsilon{10}\right)	m^2 \leq \left(\frac \varepsilon 5 + \frac \varepsilon 4\right)m^2 \leq \varepsilon \binom m2
	\]
	by our choice of $\gamma \leq \varepsilon/2$ and $m \geq 5$.
\end{proof}

\section{A blowup variant of the Andr\'asfai--Erd\H os--S\'os theorem}\label{sec:aes-variant}
The Andr\'asfai--Erd\H os--S\'os theorem \cite{AES} is a minimum-degree stability version of Tur\'an's theorem. It says that if an $m$-vertex $K_p$-free graph has minimum degree greater than $\frac{3p-7}{3p-4}m$, then it is $(p-1)$-partite; moreover, the constant $\frac{3p-7}{3p-4}$ is best possible. We need a related result, which says that if a graph has high minimum degree and does not contain some blowup of $K_p$, then it is $(p-1)$-partite. We remark that unlike Andr\'asfai, Erd\H os, and S\'os, we do not obtain the exact minimum degree threshold for being $(p-1)$-partite; for more on such refined questions, see e.g.\ \cite{Illingworth}.
\begin{theorem}\label{thm:aes-variant}
For every $p,t \geq 2$, every $1 =a_1 = a_2 \leq a_3 \leq \dotsb \leq a_{p-1} \leq t$, there exist some $\gamma,\delta>0$ such that if $m$ is large enough in terms of $a_{p-1}$ and $p$, $a_p \leq \delta m$, and $\Gamma$ is a $K_p(a_1,a_2,\dots,a_p)$-free graph on $m$ vertices with minimum degree at least $(1-1/(p-1)-\gamma)m$, then $\Gamma$ is $(p-1)$-partite.
\end{theorem}
\noindent We need the following lemma, which is essentially due to Erd\H os \cite{Erdos64}.
\begin{lemma}\label{lem:zarankiewicz}
For every $\eta>0$ and $p,t \geq 2$, and $1\leq a_1 \leq \dotsb \leq a_{p-1} \leq t$, there exists some $\delta>0$ such that the following holds for large enough $m$. If $a_p \leq \delta m$ and $\Gamma$ is a $K_p(a_1,a_2,\dots,a_p)$-free graph on $m$ vertices, then $\Gamma$ has at most $\eta m^p$ copies of $K_p$.
\end{lemma}
\begin{proof}
We proceed by induction on $p$. The base case $p=2$ just says that a $K_{a_1,\delta m}$-free graph has at most $\eta m^2$ edges. We double-count the number of copies of $K_{1,a_1}$ in $\Gamma$. On the one hand, every $a_1$-set has at most $\delta m$ common neighbors, so there are at most $\delta m \binom m{a_1} < \delta m^{a_1+1}$ copies of $K_{1,a_1}$. On the other hand, a vertex of degree $d$ contributes $\binom d{a_1}$ many copies. Therefore,
\[
\delta m^{a_1+1} > \sum_{v \in V(G)} \binom{\deg(v)}{a_1} \geq m \binom{2 e(\Gamma)/m}{a_1} \geq m \left(\frac{2e(\Gamma)}{ea_1 m}\right)^{a_1}
\]
where the second inequality uses Jensen's inequality. Rearranging, we find that $e(\Gamma) < 2a_1\delta^{1/a_1} m^2$. If we let $\delta = (\eta/(2 a_1))^{a_1}$, this gives the desired result.

We now proceed with the inductive step. For every $(p-1)$-set of vertices $S$, let $\ext(S)$ denote the set of vertices $v$ such that $S \cup \{v\}$ is a $K_p$. Note that the sum of $\ab{\ext(S)}$ over all $(p-1)$-sets $S$ is exactly $p$ times the number of $K_p$ in $\Gamma$. By assumption, this sum is therefore more than $p \eta m^p$. Thus, the average value of $\ab{\ext(S)}$ is greater than $p \eta m^p/\binom m{p-1}> \eta m$. Again by convexity
\[
\sum_{S \in \binom{V(\Gamma)}{p-1}} \binom{\ext(S)}{a_1} > \binom{m}{p-1} \binom{\eta m}{a_1}
\]
Therefore, there is some $a_1$-set $A$ such that the common neighborhood of $A$ has at least
\[
\binom{m}{p-1} \binom{\eta m}{a_1} / \binom m{a_1} \geq \eta' m^{p-1}
\]
copies of $K_{p-1}$, for some $\eta'$ depending only on $\eta$, $t$, and $p$. By induction, the common neighborhood of $A$ must have a copy of $K_{p-1}(a_2,\dots,a_p)$, which is a contradiction.
\end{proof}

We can now prove \cref{thm:aes-variant}.
\begin{proof}[Proof of \cref{thm:aes-variant}]
Fix some small $\eps>0$ depending on $p$ and $t$. Let $\gamma,\eta$ be the parameters given in \cref{thm:stability}, depending only on $\eps$ and $p$, and recall that $\gamma \leq \eps$. Finally, let $\delta>0$ be the parameter in \cref{lem:zarankiewicz}. By \cref{lem:zarankiewicz}, we see that since $\Gamma$ is a $K_p(a_1,a_2,\dots,a_p)$-free graph on $m$ vertices, it must have at most $\eta m^p$ copies of $K_p$, and it has minimum degree at least $(1-1/(p-1)- \gamma)m$ by assumption. Therefore, \cref{thm:stability} implies that $\Gamma$ has a partition into parts $V_1,\dots,V_{p-1}$ such that the total number of internal edges is at most $\varepsilon \binom m2$. We fix such a partition with the minimum number of total internal edges. In particular, every vertex must have at least as many neighbors in every other part as it does in its own part.

Since $\Gamma$ has minimum degree at least $(1-1/(p-1)-\gamma)m$, it must have at least $(1-1/(p-1)-\gamma)\frac{m^2}{2}$ edges. Therefore, since there are at most $\eps \frac{m^2}{2}$ internal edges in $V_1,\dots,V_{p-1}$, we must have that
\begin{equation}\label{eq:edges-across-lb}
		\sum_{1 \leq i<j\leq p-1} e(V_i,V_j) \geq \left( 1- \frac{1}{p-1} -\gamma -\varepsilon \right) \frac{m^2} 2 \geq \left( 1- \frac{1}{p-1} - 2 \varepsilon \right) \frac{m^2}{2}
	\end{equation}
since $\gamma \leq \eps$.
We note that
\begin{equation}\label{eq:sum-of-squares}
	\sum_{i=1}^{p-1} \left( \ab{V_i}- \frac{m}{p-1} \right) ^2= \sum_{i=1}^{p-1} \ab{V_i}^2 - \frac{2m}{p-1} \sum_{i=1}^{p-1} \ab{V_i} + \frac{m^2}{p-1} = \sum_{i=1}^{p-1} \ab{V_i}^2 - \frac{m^2}{p-1}.
\end{equation}
Since the left-hand side of (\ref{eq:sum-of-squares}) is non-negative, we see that
\[
	\sum_{1 \leq i<j\leq p-1} \ab{V_i} \ab{V_j} = \frac12\left( m^2 - \sum_{i=1}^{p-1} \ab{V_i}^2 \right)\leq \frac 12\left( m^2 - \frac{m^2}{p-1} \right) = \left( 1- \frac{1}{p-1} \right) \frac{m^2}2.
\]
We can conclude from this that each $V_i$ has cardinality $\frac{m}{p-1} \pm  \sqrt{2\varepsilon}m$. For if not, then the left-hand side of (\ref{eq:sum-of-squares}) would be larger than $2 \varepsilon m^2$, and the above computation would contradict (\ref{eq:edges-across-lb}).

Now, suppose that for some $1\leq a < b\leq p-1$, we have that $e(V_a,V_b) <(1- p^2\varepsilon)\ab{V_a}\ab{V_b}$. Then we would find that
\[
	\sum_{1 \leq i<j\leq p-1} e(V_i,V_j) < \sum_{1 \leq i<j \leq p-1} \ab{V_i}\ab{V_j} -p^2 \varepsilon \ab{V_a} \ab{V_b} \leq \left( 1- \frac{1}{p-1}-2 \varepsilon \right) \frac{m^2}{2},
\]
contradicting (\ref{eq:edges-across-lb}), using the bound $\ab{V_a} \geq \frac{m}{p-1}-\sqrt{2 \varepsilon}m \geq m/p$ for sufficiently small $\eps$. Therefore, we find that for all $i \neq j$,
\begin{equation}\label{eq:pairs-dense}
	e(V_i,V_j) \geq (1- p^2 \varepsilon) \ab{V_i} \ab{V_j}.
\end{equation}
Now suppose that some vertex $v \in V_i$ has more than $2p^2\sqrt \eps \ab{V_i}$ neighbors in its own part $V_i$. By our assumption above, this means that $v$ also has more than $2p^2 \sqrt \eps \ab{V_i}\geq p^2 \sqrt \eps \ab{V_j}$ neighbors in each part $V_j$ for $j \neq i$, where we used the fact that $\ab{V_j} = \frac{m}{p-1} \pm \sqrt{2\eps}m$ and the fact that $\eps$ is sufficiently small to conclude that $\ab{V_i} \geq \frac 12 \ab{V_j}$. Let $U_j = N(v) \cap V_j$ denote the neighbors of $v$ in $V_j$. For every $1 \leq a \neq b \leq p-1$, we have by (\ref{eq:pairs-dense}) that
\[
    e(U_a,U_b) \geq \ab{U_a} \ab{U_b} - p^2 \eps \ab{V_a} \ab {V_b} \geq \left(1 - \frac{p^2 \eps}{p^4 \eps}\right) \ab{U_a} \ab{U_b} =\left(1 - \frac 1{p^2}\right) \ab{U_a} \ab{U_b},
\]
where the second inequality uses our assumption that $\ab{U_a} \geq p^2 \sqrt \eps \ab{V_a}$, and similarly for $U_b$. By the union bound, if we pick a random vertex from $U_a$ for each $1 \leq a \leq p-1$, then they span a copy of $K_{p-1}$ with probability at least $1-\binom p2/p^2\geq \frac12$. Therefore, the neighborhood of $v$ contains at least
\[
\frac 12 \prod_{a=1}^{p-1} \ab{U_a} \geq \frac{(p^2 \sqrt \eps)^{p-1}}{2}\prod_{a=1}^{p-1} \ab{V_a} \geq \frac{(p^2\sqrt \eps)^{p-1}}{2p^{p-1}} m^{p-1} = \eta' m^{p-1}
\]
copies of $K_{p-1}$, for $\eta'$ depending only on $p$ and $\eps$.
By \cref{lem:zarankiewicz}, this implies that if $\delta$ is sufficiently small in terms of $p,t$, and $\eps$, then the neighborhood of $v$ contains a copy of $K_{p-1}(a_2,\dots,a_p)$. Since $a_1=1$, this implies that $\Gamma$ contains a copy of $K_p(a_1,a_2,\dots,a_p)$, which is a contradiction. Thus, we conclude that every vertex $v \in V_i$ has at most $2p^2 \sqrt \eps \ab{V_i}$ neighbors in its own part $V_i$, for every $1 \leq i \leq p-1$.

We now claim that for every $1 \leq i \neq j \leq p-1$, every vertex $v \in V_i$ has at least $(1-1/(2pt))\ab{V_j}$ neighbors in $V_j$, as long as $\eps$ is sufficiently small in terms of $p$ and $t$. Indeed, if not, then $v$ has at least $\ab{V_j}/(2pt)$ non-neighbors in $V_j$, and at least  $(1-2p^2 \sqrt \eps)\ab{V_i}-1$ non-neighbors in $V_i$. In total, the number of non-neighbors of $v$ is at least
\[
\frac{1}{2pt} \ab{V_j} + (1-2p^2 \sqrt \eps)\ab{V_i}-1 > \left(1 + \frac 1{2pt} - 2p^2 \sqrt \eps - 4\sqrt \eps\right)\frac m{p-1} > \left(\frac 1{p-1} + \eps\right)m,
\]
as long as $\eps$ is sufficiently small in terms of $p$ and $t$. This contradicts the assumption that the minimum degree of $\Gamma$ is at least $(1-1/(p-1)-\eps)m$.

Now suppose that there is some edge $vw$ inside some part $V_i$, and assume without loss of generality that $i=1$. The vertices $v$ and $w$ have at least $(1-1/(pt))\ab{V_2}>a_3$ common neighbors in $V_2$, so we may pick some set of $a_3$ common neighbors in $V_2$. Then $v,w,$ and these $a_3$ common neighbors have at least $(1-(a_3+2)/(2pt))\ab{V_3}>(1-2t/(2pt))\ab{V_3}>a_4$ common neighbors in $V_3$, so we may pick $a_4$ such common neighbors in $V_3$. Continuing in this way, we can greedily pick $a_{j+1}$ vertices from $V_j$ which are common neighbors of the previously chosen vertices, for each $j \leq p-2$. Having done this, we have picked at most $pt$ vertices, so they still have at least $(1-pt/(2pt))\ab{V_{p-1}} = \frac 12 \ab{V_{p-1}}> \delta m$ common neighbors in $V_{p-1}$. Thus, we have built a copy of $K_p(a_1,\dots,a_p)$ in $\Gamma$, a contradiction. This shows that there can be no edge inside any $V_i$, and thus that $\Gamma$ is $(p-1)$-partite.
\end{proof}

\section{Proof of Theorem \ref{thm:multipartite}} \label{sec:main-proof}

 In this section, we prove \cref{thm:multipartite}. Recall the statement: for any $k, p, t \ge 2$, there exists $\delta > 0$ such that the following holds. If $n$ is large enough in terms of $k,p$ and $t$, $1\le a_1 \le a_2 \le \cdots \le a_{p-1} \le t \le a_p = \delta n$, $G = K_p (a_1, a_2, \ldots, a_p)$, and $H = \book kn$, then $r(G,H) = (p-1)(n-1) + a_1$ if and only if $a_1 = a_2 = 1$. Here $K_p(a_1,a_2, \ldots, a_p)$ is the complete $p$-partite graph with part sizes $a_1, \ldots, a_p$.

We start with the construction for the ``only if'' direction.

\begin{proof}[Proof of ``only if'' direction of \cref{thm:multipartite}.]
It suffices to show that if $a_2 \ge 2$, then $r(G,H) > (p-1)(n-1)+a_1$.

Let $\Gamma$ be a graph on $N = (p-1)(n-1)+a_1$ vertices which are divided into $p-1$ parts $U_1,\ldots, U_{p-1}$ with $|U_1| = n+a_1-1$ and $|U_2| = \cdots = |U_{p-1}| = n-1$. The edges of $\Gamma$ are defined as follows. First, all pairs of vertices in two different parts are adjacent. Second, $U_1$ induces a $C_4$-free subgraph $A = \Gamma[U_1]$ which is almost $a_1$-regular. This means that either $A$ is $a_1$-regular (if $|U_1|$ or $a_1$ is even), or else all but one vertices of $A$ have degree $a_1$ and one vertex has degree $a_1-1$ (if $|U_1|$ and $a_1$ are both odd). Such a graph $A$ always exists if $n$ is large enough in terms of $a_1$.

It remains to show that $\Gamma$ is $G$-free and $\overline{\Gamma}$ is $H$-free. Suppose $\Gamma$ contained a copy of $G$. Since $G$ is complete $p$-partite and $U_2,\ldots, U_{p-1}$ are independent sets of $\Gamma$, each of these sets can contain only vertices from one part of this copy of $G$. Thus, at least two parts of $G$ must be entirely contained inside $U_1$, which means that $\Gamma[U_1]$ must contain a copy of the complete bipartite graph $K_{a_1, a_2}$. By construction, $A = \Gamma[U_1]$ is $C_4$-free, so this is impossible unless $a_1=1$. When $a_1=1$, $A$ has maximum degree $1$ and thus cannot contain a copy of $K_{a_1, a_2}$ either, when $a_2 \ge 2$. In all cases, $\Gamma$ is $G$-free.

The complement $\overline{\Gamma}$ is a disjoint union of $\overline{A}$ and $p-2$ copies of $K_{n-1}$. The book $H$ is connected and has $n$ vertices, so $K_{n-1}$ cannot contain a copy of $H$. Also, $k\ge 2$, so $H$ contains at least two vertices of degree $n-1$, whereas $\overline{A}$ has either one or zero vertices of degree at least $n-1$. It follows that $\overline{A}$ contains no copies of $H$ either, completing the proof.
\end{proof}

The proof of the ``if'' direction of \cref{thm:multipartite} divides into three parts. First, we show \cref{lem:low-deg-vtxs} below (which uses \cref{lem:find-blowup}) that under the assumptions of the theorem, we can assume that most vertices of $\Gamma$ have degree at least $(1-1/(p-1) - o(1)) N$. We then apply \cref{thm:aes-variant}, which proves that except for the small number of low-degree vertices, $\Gamma$ is $(p-1)$-partite. Finally, we use a careful averaging argument to show that under these assumptions, $\ol \Gamma$ must contain a copy of $H=\book kn$, completing the proof.

We begin by proving that most vertices of $\Gamma$ have high degree.

\begin{lemma}\label{lem:low-deg-vtxs}
Under the assumptions of \cref{thm:multipartite}, if $a_1 = a_2 = 1$, then the following holds for any $\alpha > 0$, assuming that $n$ is sufficiently large in terms of $\alpha$. If $\Gamma$ is a graph on $N = (p-1)(n-1)+1$ vertices such that $\Gamma$ is $G$-free and $\overline{\Gamma}$ is $H$-free, then at most $\alpha N$ vertices of $\Gamma$ have degree at most $d = (1-1/(p-1) - \alpha) N$.
\end{lemma}
\begin{proof}

Let $\eps = \eps(k, \alpha) > 0$ be small in terms of $k$ and $\alpha$, and let $x = x(k, \eps) \ge 1$ be large in terms of $k$ and $\eps$. We also assume that $\delta = \delta(k,p,t) > 0$ is chosen to be small in terms of $\eps$ and $\alpha$.

Let $S \subset V(\Gamma)$ be the set of vertices of degree
less than $d$. We proceed by contradiction and assume that $|S| \ge \alpha N$. By \cref{lem:find-blowup}, there is an induced copy of $K_{p-1}(x)$ in $\Gamma$ whose parts are $V_1,\ldots, V_{p-1}$ with $V_1 \subseteq S$. Thus, all the vertices of $V_1$ have degree less than $d$. We remark that \cref{lem:find-blowup} requires $|S|$ to be double-exponentially large in $p$, so we require $n$ to be at least double-exponentially large in $p$ for this step. This is the only place where a double-exponential dependence is needed.

Partition the vertices of $\Gamma$ into $p$ parts $U_0,\ldots, U_{p-1}$, where for $i\ge 1$,  each vertex in $U_i$ has at most $\eps x$ neighbors in $V_i$, and $U_0$ consists of all vertices more than $\eps x$ neighbors in each $V_i$.

First, if $|U_0|\ge (e/\eps)^{tp} \cdot \delta n$, then we can find a copy of $G$ in $\Gamma$ as follows. If we pick a set $W$ by taking $a_i$ vertices uniformly at random from $V_i$ for $i=1,\ldots,p-1$, then the expected number of vertices of $U_0$ complete to $W$ is at least
\[
\prod_{i=1}^{p-1} \frac{\binom{\eps x}{a_i}}{\binom{x}{a_i}} \cdot |U_0| \ge (\eps/e)^{tp}\cdot |U_0| \ge \delta n.
\]
Thus, there exists a $W$ for which we can find $\delta n$ vertices of $U_0$ which together with $W$ form a copy of $G= K_p (a_1, \ldots, a_p)$, which is impossible.

Next, suppose $|U_i| \ge (1-2k \eps)^{-1} (n-k)$ for some $i\ge 1$. Every vertex in $U_i$ has at most $\eps x$ neighbors in $V_i$, so we may remove half the vertices of $V_i$ (the ones with highest degree to $U_i$) to find a subset $V'_i$ such that every $v\in V'_i$ has at most $2\eps |U_i|$ neighbors in $U_i$. Take $W$ to be any $k$-subset of $V'_i$, and let $U'_i \subseteq U_i$ be the set of vertices with no neighbors in $W$. We have $|U'_i| \ge (1-2 k\eps) |U_i|\ge n-k$, and so $W$ and $U'_i$ form a copy of $\book kn$ in $\ol \Gamma$, which is again impossible. We henceforth assume that $|U_i| < (1-2k \eps)^{-1}(n-k)$ for all $i\geq 1$.

Finally, suppose $|U_1| \ge (1-2k\eps)^{-1}(n-k) - (\alpha /10)^k N.$
We seek to find a copy of $\book kn$ in $\ol \Gamma$ again, this time using the degree condition on $V_1$. As before, we may pass to a subset $V'_1$ of half the vertices of $V_1$ such that each has at least $(1-2\eps) |U_1|$ non-neighbors in $U_1$. Each vertex of $V'_1$ has degree at most $d = (1-1/(p-1)-\alpha)N$, and so has at least $N-1-d = N/(p-1)+\alpha N -1 \geq n + \alpha N-2$ non-neighbors in total. In particular, since $|U_1| < (1-2k \eps)^{-1}(n-k) < (1+3k\eps) n-2$ and $\alpha \ge 6k \eps$, each vertex of $V'_1$ has at least $(n + \alpha N -2) - |U_1| \ge \alpha N/2$ non-neighbors in $\ol{U_1}$.

Pick a random $k$-subset $W$ of $V'_1$ to form the spine of the book. The number of common non-neighbors the vertices of $W$ have inside $U_1$ is at least $(1-2k\eps)|U_1|$. We now count the expected number of common non-neighbors the vertices of $W$ have in $\ol{U_1}$. For the convenience of the following calculation, we insert phantom vertices to $\ol{U_1}$, each complete to $W$, until $|\ol{U_1}| = N$; this has no effect on the common non-neighborhood we care about. If $u \in \ol{U_1}$ has $y$ non-neighbors in $V'_1$, then the probability that $W$ is chosen entirely among these $y$ vertices is $\binom{y}{k}/\binom{x/2}{k}$.
Since vertices in $V'_1$ have at most $(1-\alpha/2)N$ neighbors in $\ol{U_1}$, the average value of $y$ over a random $u\in \ol{U_1}$ is at least $\alpha/2 \cdot |V'_1| = \alpha x/4$. By linearity of expectation and convexity we find that the expected number of common non-neighbors of $W$ in $\ol{U_1}$ is at least
\[
\binom{\alpha x / 4}{k}\binom{x/2}{k}^{-1} |\ol{U_1}| \ge \left(\frac{\alpha x}{4k}\right)^k\left(\frac{2k}{ex}\right)^k |\ol{U_1}| \ge (\alpha / (2e))^k \cdot (N/2) \ge (\alpha / 10)^k N.
\] Thus, there exists some particular $W$ with at least $(1-2\eps)|U_1| + (\alpha/10)^k N \ge n-k$ non-neighbors, forming the desired $\book kn$ in $\ol \Gamma$. This contradicts our assumptions on $\Gamma$.

We conclude that the partition $V(\Gamma) = U_0\sqcup \cdots \sqcup U_{p-1}$ satisfies
\begin{align*}
    |U_0|& < (e/\eps)^{tp} \cdot \delta n \\
    |U_1| & < \frac{n-k}{1-2k\eps} - (\alpha /10)^k N \\
    |U_i| & < \frac{n-k}{1-2k \eps} \textnormal{ if $i \ge 2$.}
\end{align*}
Adding these together, we obtain that the number $N$ of vertices in $\Gamma$ is
\begin{align*}
    \sum_{i=0}^{p-1} |U_i| & < (e/\eps)^{tp} \cdot \delta n + (p-1) \frac{n-k}{1-2k \eps} - (\alpha /10)^k N \\
    & < (1+ 3k\eps) N + (e/\eps)^{tp} \cdot \delta n - (\alpha /10)^k N \\
    & < N,
\end{align*}
if $\eps$ is small enough compared to $k$ and $\alpha$, and $\delta$ is small enough compared to $\eps$, $t$, and $p$. This is a contradiction and we are done.
\end{proof}

We now have all the tools to complete the proof.

\begin{proof}[Proof of ``if'' direction of \cref{thm:multipartite}.]

Recall that $H=\book kn$, and $G = K_p(a_1,\dots,a_p)$, where $1 \leq a_1 \leq \dotsb \leq a_{p-1} \leq t$, $a_p = \delta n$, and $n$ is sufficiently large in terms of $t,k,$ and $p$. We are given a $G$-free graph $\Gamma$ on $N = (p-1)(n-1)+1$ vertices, and we wish to show that $\ol \Gamma$ contains a copy of $H$. We have already proved in \cref{lem:low-deg-vtxs} that at most $\alpha N$ vertices of $\Gamma$ have degree at most $d = (1-1/(p-1)- \alpha)N$, for any fixed $\alpha >0$ and sufficiently large $N$. If we let $T$ be the set of vertices of degree greater than $d$, then the induced subgraph $\Gamma[T]$ has at least $(1-\alpha)N$ vertices and thus minimum degree at least $(1-1/(p-1)-2 \alpha)\ab T$. Applying \cref{thm:aes-variant} to the graph $\Gamma[T]$, we find that as long as $\alpha$ is sufficiently small in terms of $p$ and $t$, we have that $\Gamma[T]$ is $(p-1)$-partite. Let the parts of $\Gamma[T]$ be $T_1,\dots,T_{p-1}$. We now argue roughly as in the proof of \cref{thm:aes-variant}.

Recall that for a vertex $v$ and a vertex set $W$, we denote by $d(v,W)$ the \textit{density} of $v$ to $W$, namely the number of neighbors of $v$ in $W$ divided by $\ab{W}$.
\begin{claim}\label{cl:partition-properties}
Let $T_1,\dots,T_{p-1}$ be as defined above. Let $\xi = 4p^2 \alpha$. Then for every $1 \leq i \neq j \leq p-1$, we have that
\begin{equation}\label{eq:part-sizes}
    \left(\frac{1}{p-1} -\xi\right)N \leq \ab{T_i} \leq \left(\frac{1}{p-1} +\xi\right)N
\end{equation}
and
\begin{equation}\label{eq:pair-densities}
    d(w,T_j) \geq 1-\xi \text{ for every }w \in T_i.
\end{equation}
\end{claim}
\begin{proof}
    Since $T_i$ is an independent set, every vertex in $T_i$ has degree at most $N-\ab{T_i}$. Since every vertex in $T_i$ has degree at least $d$, this implies that $\ab{T_i} \leq N-d = (1/(p-1)+\alpha)N$. Since $T_1,\dots,T_{p-1}$ partition $T$, which has size at least $(1-\alpha)N$, this  implies that $\ab{T_i} = \ab{T} -\sum_{j \neq i} \ab{T_j} \geq (1/(p-1)-p \alpha)N$, which proves (\ref{eq:part-sizes}) since $p \alpha < \xi$.

    For (\ref{eq:pair-densities}), we recall that the induced subgraph $\Gamma[T]$ has minimum degree at least $(1-1/(p-1)-2 \alpha)\ab{T}$. So any $w \in T_i$ has at most $(1/(p-1)+2 \alpha)\ab T$ non-neighbors in $T$. Additionally, since $T_i$ is an independent set, every $w \in T_i$ has $\ab{T_i}-1$ non-neighbors in $T_i$. If $d(w,T_j)<1-\xi$, then the total number of non-neighbors of $w$ is at least
    \[
    \xi \ab{T_j} + \ab{T_i} - 1 \geq (1+\xi)\left( \frac{1}{p-1} - p \alpha\right)N - 1 > \left( \frac{1+\xi}{p-1} - 2p \alpha\right) N > \left(\frac 1{p-1} + 2\alpha\right)\ab{T},
    \]
    using the computations above and our choice of $\xi = 4p^2 \alpha$. This is a contradiction.
\end{proof}
Let $S$ be the complement of $T$, i.e.\ the set of vertices in $\Gamma$ with degree less than $d$, and recall that $\ab{S} \leq \alpha N$.

\begin{claim}
Let $\zeta =pt \xi =  4p^3 t \alpha$. For every $v \in S$, at least one of the following is true. Either $v$ has no edges to some $T_i$, or else $d(v,T_i) < \zeta$ for at least two different choices of $i \in [p-1]$.
\end{claim}
\begin{proof}
    Suppose for contradiction that this is false for some $v \in S$. Thus, $d(v,T_i)\geq \zeta$ for all but at most one choice of $i \in [p-1]$, and additionally $v$ has a neighbor in each $T_i$. By relabeling the parts, we may assume that $d(v,T_i) \geq \zeta$ for all $i \in [p-2]$. Let $w$ be a neighbor of $v$ in $T_{p-1}$. By (\ref{eq:pair-densities}), we see that $v$ and $w$ have at least $(\zeta-\xi)\ab{T_1} > \xi\ab{T_1} > a_3$ common neighbors in $T_1$, for $N$ sufficiently large. Pick any $a_3$ common neighbors in $T_1$. Then $v,w$, and these $a_3$ common neighbors have at least $(\zeta - (a_3+1)\xi)\ab{T_2} > \xi\ab{T_2} > a_4$ common neighbors in $T_2$. Continuing in this way, we can pick out $a_i$ vertices in $T_{i+2}$ which are common neighbors of all previously-chosen vertices. At the end of this process, we can still pick at least $(\zeta-(p-1)t\xi)\ab{T_{p-2}} \geq \delta n$ common neighbors in $T_{p-2}$, and thus we can build a copy of $G$, contradicting our assumption that $\Gamma$ is $G$-free.
\end{proof}
We partition $S$ into $S_1 \cup S_2$, where $S_1$ consists of all vertices in $S$ that are empty to some part $T_i$, and $S_2$ consists of the remaining vertices $v$, namely those satisfying $d(v,T_i)<\zeta$ for at least two choices of $1 \leq i \leq p-1$.

Now, we pick an index $i \in [p-1]$ uniformly at random, and then pick a $k$-set $Q \subset V_i$ uniformly at random. By doing so, we obtain a (non-uniform) distribution on the set of $k$-cliques in $\ol \Gamma$. For a vertex $v \in V(\Gamma)$, let us say that $v$ \emph{extends} $Q$ if $Q \cup \{v\}$ is also a clique in $\ol \Gamma$, or equivalently if $v$ is not adjacent in $\Gamma$ to any vertex of $Q$. Note that if $v \in Q$, then we still say that $v$ extends $Q$, even though this is not really an extension per se. We observe that if $v \in T$, then the probability that $v$ extends $Q$ is at least $1/(p-1)$. Indeed, the probability that $v$ extends $Q$ is at least the probability that $v \in T_i$ for the randomly chosen index $i$, which is exactly $1/(p-1)$ since we pick the index $i$ uniformly at random.

Next, if $v \in S_1$, then we again have that the probability that $v$ extends $Q$ is at least $1/(p-1)$. Indeed, if $v \in S_1$, then $v$ has no edges to $T_j$ for at least one index $j$. The probability that $v$ extends $Q$ is then at least the probability that $j$ is the randomly chosen index, which equals $1/(p-1)$.

Finally, if $v \in S_2$, then without loss of generality, $d(v,T_1) < \zeta$ and $d(v,T_2)<\zeta$. If the randomly chosen index $i$ is $1$ or $2$, then the probability that $v$ has an edge to $Q$ is at most $k\zeta$, by the union bound. Therefore, if $v \in S_2$, then
\[
\pr(v\text{ extends }Q) \geq \frac{2}{p-1}\cdot (1-k\zeta) \geq \frac{1}{p-1},
\]
since we may pick $\alpha$ sufficiently small so that $k \zeta \leq 1/2$. By putting all of this together, we find that $\pr(v \text{ extends }Q) \geq 1/(p-1)$ for every vertex $v \in V(\Gamma)$. By linearity of expectation, this implies that
\[
\E[|\{v : v\text{ extends }Q\}|] = \sum_{v \in V(\Gamma)} \pr(v\text{ extends }Q) \geq \frac{N}{p-1}
\]
Therefore, there exists some clique $Q$ in $\ol \Gamma$ which has at least $\lceil N/(p-1)\rceil = n$ extensions. Since exactly $k$ of these extensions are the degenerate ones coming from vertices in $Q$ itself, we find that $\ol \Gamma$ contains a copy of $H=\book kn$. This completes the proof.
\end{proof}

\section{Concluding remarks}\label{sec:concluding}

In this section we collect a few of the tantalizing open questions remaining in this area.
\paragraph{Removing regularity.}
Note that the full Ramsey goodness results of Nikiforov and Rousseau~\cite{NR09} hold in greater generality than our results \cref{thm:book-book} and \cref{thm:multipartite}. However, due to the dependence of their arguments on Szemer\'edi's regularity lemma, the quantitative dependence between the graph sizes involved are tower-type. It would be interesting to find a direct proof of their goodness results without regularity, as this would likely lead to superior quantitative bounds.

\paragraph{Near Ramsey goodness.}
In \cref{thm:multipartite}, we study the Ramsey number $r(K_p(a_1,\dots,a_p), \book kn)$ for sufficiently large $n$, where $a_1,\dots,a_{p-1}$ are fixed and $a_p \leq \delta n$ for some absolute constant $\delta>0$. We are able to determine this Ramsey number in the case $a_1 = a_2 = 1$ (in which case the answer is given by the Ramsey goodness bound), but it is natural to ask what happens for larger values of $a_1$ and $a_2$. In this case, there is a natural lower bound, generalizing the proof of the ``only if'' direction of \cref{thm:multipartite}, and which shows a surprising connection to an analogue of the classical extremal problem for complete bipartite graphs. To explain this connection, we first define the following Dirac-type extremal function.
\begin{definition}
Given a graph $H$ and integers $k,n$, let $d_k(n,H)$ be the maximum $d$ for which there is an $(n+d-1)$-vertex $H$-free graph, at most $k-1$ vertices of which have degree less than $d$.
\end{definition}
Now let $d = d_k(n, K_{a_1,a_2})$, and let $\Gamma_0$ be a $K_{a_1,a_2}$-free graph on $n+d-1$ vertices, at most $k-1$ of which have degree less than $d$. Let $\Gamma$ be a graph with $N=(p-1)(n-1)+d$ vertices, whose vertex set is divided into $p-1$ parts $U_1,\dots,U_{p-1}$ with $\ab{U_1} = n+d-1$ and $\ab{U_2} = \dotsb = \ab{U_{p-1}} = n-1$, such that $\Gamma[U_1]$ is isomorphic to $\Gamma_0$, and such that all pairs of vertices in different parts are adjacent. Then $\Gamma$ is $K_p(a_1,\dots,a_p)$-free, since $U_2,\dots,U_{p-1}$ are independent sets, and $\Gamma[U_1]$ is $K_{a_1,a_2}$-free. Additionally, $\ol \Gamma$ is a disjoint union of $\ol{\Gamma_0}$ and $p-2$ cliques of order $n-1$. The cliques are too small to contain a copy of $\book kn$, and all but at most $k-1$ vertices of $\ol{\Gamma_0}$ have degree at most $(n+d-1)-1-d = n-2$. Since $\book kn$ has $k$ vertices of degree $n-1$, this shows that $\ol \Gamma$ is $\book kn$-free. Thus, we conclude that
\begin{equation}\label{generalextremal}
r(K_p(a_1,\dots,a_p), \book kn) > (p-1)(n-1) + d_k(n,K_{a_1,a_2}).
\end{equation}
Our proof of the ``only if'' direction of \cref{thm:multipartite} used the same argument, and we simply noted that if $a_2>1$, then for sufficiently large $n$, we have $d_k(n,K_{a_1,a_2})\geq a_1$ for all $k \geq 2$. We conjecture that the lower bound (\ref{generalextremal}) is tight for sufficiently large $n$, if $a_1,\dots,a_{p-1}$ are fixed, and $a_p \leq \delta n$.
\begin{conjecture}\label{conj:dirac-type}
    For all integers $k,p,t\geq 2$, there exists some $\delta>0$ such that the following holds for all $n \geq 1$. For positive integers $a_1 \leq \dotsb \leq a_{p-1} \leq t$ and $a_p \leq \delta n$, we have
    \[
    r(K_p(a_1,\dots,a_p), \book kn) = (p-1)(n-1) + d_k(n,K_{a_1,a_2})+1.
    \]
\end{conjecture}
\noindent Thus, \cref{thm:multipartite} verifies \cref{conj:dirac-type} in the case $a_1 = a_2 = 1$.
\paragraph{Disconnected graphs.}
Ramsey goodness results are some of the rare examples in graph Ramsey theory where exact values of Ramsey numbers are known. Another such example is an old result of Burr, Erd\H os, and Spencer~\cite{BuErSp}, recently improved by Buci\'c and Sudakov~\cite{BuSu}, which shows
\[
r(nG, nG) = 2(|G| - \alpha(G))n + c
\]
for $n$ sufficiently large and some constant $c=c(G)$. Here, $G$ is a fixed graph, $nG$ is a vertex disjoint union of $n$ copies of $G$, and $\alpha(G)$ is the independence number of $G$. Does there exist a theory of Ramsey goodness for disconnected graphs, giving a common generalization of the Burr--Erd\H os--Spencer result and our theorems?

\paragraph{Empty pairs in triangle-free graphs.}
Motivated by a well-studied approach to the famous Erd\H os--Hajnal conjecture, the following conjecture was proposed by Conlon, Fox, and Sudakov.
    \begin{conjecture}[{\cite[Conjecture 3.14]{CoFoSu15}}] \label{conj:triangle-free}
        There exists some $\eps>0$ such that every $N$-vertex triangle complete graph contains two vertex subsets $A,B$ with $\ab A \geq \eps N$, $\ab B \geq N^\eps$, and with no edges between $A$ and $B$.
    \end{conjecture}
    For more on this conjecture and its variants, see also \cite{CFSSS}. \cref{conj:triangle-free} remains open. The strongest result in this direction, due independently to Fox and Shapira (unpublished) says that one may take $\ab A \geq \eps N$ and $\ab B \geq \eps\log N/\log \log N$. One consequence of \cref{thm:main1} is that we may take $\ab A \geq \eps N$ and $\ab B \geq (\log N)^\eps$, for $\eps = 1/31$. Indeed, \cref{thm:main1} with $p=3$ says that if $n \geq 2^{k^{10p}} = 2^{k^{30}}$ and if $N = (p-1)(n-1)+1 = 2n-1$, then for every $N$-vertex triangle-free graph $\Gamma$, its complement $\ol \Gamma$ contains a copy of $\book kn$. Let $A$ be the set of leaves of this book and $B$ be its spine, so that $\ab A = n \geq N/31$ and $\ab B = k \geq (\log N)^{1/31}$. Since $A \cup B$ span a book in $\ol \Gamma$, there are no edges between $A$ and $B$ in $\Gamma$.

    By the same argument, we see that improving the bounds in \cref{thm:main1} could yield progress on \cref{conj:triangle-free}. For example, improving the bound $n \geq 2^{k^{10p}}$ in \cref{thm:main1} to a bound that is single-exponential in both $k$ and $p$ would allow one to take $\ab A \geq \eps N$ and $\ab B \geq \eps \log N$ in \cref{conj:triangle-free}.

\paragraph{Ramsey goodness threshold.}
 More generally, it is natural to ask what the ``Ramsey goodness threshold'' is in \cref{thm:main1}. That is, what is the smallest $n$ (in terms of $k$ and $p$) such that $r(K_p, \book kn) = (p-1)(n-1)+1$? A simple random construction shows that this threshold is at least $(k/\log p)^{cp}$, for an absolute constant $c>0$. Indeed, let $n = (k/\log p)^{cp}$ and $N = (p-1)(n-1)+1$, and let $\Gamma$ be an Erd\H os--R\'enyi random graph on $N$ vertices with edge probability\footnote{If the quantity $C(\log p)/k$ is greater than $1$, then the result we are trying to prove is vacuously true, since $(k/\log p)^{cp}$ is then less than $1$. Thus we may assume that this is a valid edge probability.} $C(\log p)/k$, for an absolute constant $C>0$. Then a first moment estimate shows that with positive probability, $\Gamma$ does not contain a copy of $K_p$ and its complement does not contain a copy of $\book kn$.

    However, there remains a rather large gap between the lower bound of $(k/\log p)^{cp}$ and the upper bound of $2^{k^{10p}}$ for this threshold. In particular, it would be interesting to determine if, for $p$ fixed, the correct behavior is polynomial or exponential in $k$.

Ramsey goodness of books revisited


All Prime Numbers Have Primitive Roots

Abstract: If p is a prime, then the numbers 1, 2, ..., p-1 form a group under multiplication modulo p. A number g that generates this group is called a primitive root of p; i.e., g is such that every number between 1 and p-1 can be written as a power of g modulo p. Building on prior work in the ACL2 community, this paper describes a constructive proof that every prime number has a primitive root.

\section{Introduction}
\label{sec:intro}

This paper describes a proof in ACL2 of the fact that all prime numbers
have primitive roots. A \emph{primitive root} of a prime number $p$ is a
number $g$ such that all the numbers $1, 2, \dots, p-1$ can be written
as $g^n \bmod p$ for some value of $n$. For example, if $p=5$, then $g=2$
is a primitive root of $p$ since $1=2^4 \bmod 5$, $2=2^1 \bmod 5$,
$3=2^3 \bmod 5$, and $4=2^2 \bmod 5$. However, for $p=7$, the number $2$
is not a primitive root of 7, because $2^n \bmod 7$ is always one of
$2$, $4$, or $1$. In particular, $2$ does not generate $3$ mod $7$. So not
all numbers in $1, 2, \dots, p-1$ are powers of $2$. The
reader can easily verify that $3$ is a primitive root of $7$, so the
theorem holds in this case.

More formally, if $p$ is a prime it is well known that the set of numbers
modulo $p$, written $\mathbb{Z}/p\mathbb{Z}$, forms a field. This occurs
because when $p$ is prime and for non-zero $a\in\mathbb{Z}/p\mathbb{Z}$, $a$ always has
a multiplicative inverse, i.e., a number $b\in\mathbb{Z}/p\mathbb{Z}$ such
that $ab\equiv1 \pmod p$. (Actually, inverses exist whenever $a$ and $p$
have no common factors, but this is guaranteed for all non-zero $a$ when $p$ is prime.)

The multiplicative group of this field, denoted by
$\left(\mathbb{Z}/p\mathbb{Z}\right)^*$, contains the elements
$1, 2, \dots, p-1$ and $g$ is a primitive root of $p$ precisely when
$g$ generates (in the sense of group theory) this group. So the fact that
prime numbers have primitive roots actually tells us something very interesting
about the structure of the group $\left(\mathbb{Z}/p\mathbb{Z}\right)^*$;
it is a cyclic group, so it has the simplest possible structure.
Primitive roots also have applications to fast arithmetic modulo $p$, similar
to the way logarithms can be used to turn multiplication to addition over
the reals~\cite{silverman2006friendly}.

The ACL2 formalization of this result follows the hand proof presented
in~\cite{youtube:primitive-roots}.
The proof itself builds on two significant forays into
number theory in ACL2. First is Russinoff's proof of quadratic reciprocity,
which also defined the foundational notions of \texttt{divides},
\texttt{primep}, and useful lemmas such as that prime fields are
integral domains (if $ab=0$ then either $a=0$ or $b=0$), and an important
lemma due to Euclid (if $p$ divides $ab$, then either $p$ divides $a$ or $p$
divides $b$)~\cite{Russ:quadratic-reciprocity,Russ:quadratic-reciprocity-books}. We also built on top of
Kestrel's formalization of prime fields, which includes definitions for the
various field operations and their various arithmetic
properties~\cite{kestrel:pfield}. As this brief list of prior results
indicates, many basic facts from number theory have already been formalized
in ACL2, but unfortunately the results are scattered in several places in
the community books. This is unfortunate, because number theory is a very
practical branch of mathematics, e.g., with applications to cryptography.
One thing we learned from this project is that it is time to collect these
various results under a common branch of the community books, so that future
projects can more easily build on top of the foundations that have already
been implemented.

The rest of this paper is organized as follows. In Sect.~\ref{sec:background},
we present some of the basic mathematical definitions in ACL2 of standard
concepts from number (and group) theory, like the order of a group element.
These are actually useful formalizations that could be used in other projects,
not just as part of this effort. Then in Sect.~\ref{sec:special-poly} we
discuss polynomial congruences, and in particular we prove that a special
family of polynomials have the greatest possible number of distinct roots.
This seemingly unrelated fact turns out to be a key technical lemma that is
used in Sect.~\ref{sec:order-construction} to construct elements
that have a desired order. The proof of the main theorem follows from these
constructions, and it is shown in Sect.~\ref{sec:primitive-root}. We conclude
the paper in Sect.~\ref{sec:conclusion} and give some ideas for future work.

\section{Mathematical Background}
\label{sec:background}

In this section, we discuss some mathematical foundations that are needed to
prove that all prime numbers have primitive roots. The definitions and proofs
in this section are very general and not solely for the purpose of our desired
theorem. In other words, these should be part of a global library of ACL2 books
formalizing number theory.

The first important concept is that of the order of an element of a group.
If $a\in\left(\mathbb{Z}/p\mathbb{Z}\right)^*$, the \emph{order} of $a$, denoted as
$\ord(a)$, is the least positive integer $k$ such that $a^{k}\equiv 1 \pmod p$.

The notion of order does not appear to be well-defined, since it seems possible that
$a^{k}\not\equiv 1 \pmod p$ for all positive integers $k$. But when $p$ is prime, an
important theorem of Fermat's says that this cannot be the case.
\begin{thm}[Fermat's Little Theorem]
	If $p$ is a prime number, and $a\in\left(\mathbb{Z}/p\mathbb{Z}\right)^*$,
	then $a^{p-1}\equiv 1 \pmod p$.
\end{thm}
This theorem, formalized in ACL2 as part of~\cite{Russ:quadratic-reciprocity,Russ:quadratic-reciprocity-books},
immediately shows that $\ord(a) \le p-1$. We used this to define \texttt{order} in
ACL2. First, the function \texttt{(all-powers a p)} generates the list
$[a^1 \bmod p, a^2 \bmod p, \dots, a^k \bmod p]$ such that $k\le p-1$ and if $1\le i<k$, then
$a^i \bmod p \ne 1$. Clearly, the length of \texttt{(all-powers a p)} is between $1$ and $p-1$,
inclusive, and when the length is less than $p-1$ the last element must be equal to 1.
Using Fermat's Little Theorem, it is easy to show that even when the length is exactly
equal to $p-1$, the last element is equal to 1. Then \texttt{(order a p)} is defined as
\texttt{(len (all-powers a p))}, and it follows that $a^{\ord(a)} \equiv 1 \pmod p$.

Another important theorem about order is that if $n$ is a positive integer such that
$a^n \pmod p = 1$ and there does not exist a smaller positive integer $m$ such that
$a^m \pmod p = 1$, then in fact $\ord(a)=n$. We capture this theorem in ACL2 as
\begin{lstlisting}
(defthmd smallest-pow-eq-1-is-order
  (implies (and (fep a p)
                (not (equal 0 a))
                (primep p)
                (posp n)
                (equal (pow a n p) 1)
                (not (exists-smaller-power-eq-1 a p n)))
           (equal (order a p) n))
  :hints ...)
\end{lstlisting}
We include the ACL2 source of that theorem here, only to familiarize the reader with the functions
\texttt{fep} which recognizes elements of the field $\mathbb{Z}/p\mathbb{Z}$, \texttt{primep} which
recognizes primes, \texttt{pow} which performs exponentiation in the field, and its friends
\texttt{add}, \texttt{mul}, \texttt{inv}, etc., which perform the other arithmetic operations in
the field---all of these were previously defined in the ACL2 Community Books.

An important fact about order is that for any element $a$, $\ord(a)$ divides $p-1$, which we will
write in the usual notation as $\ord(a) \mid p-1$. This follows because if $\ord(a)=n$, then the
list $L_n=[a^1 \bmod p, a^2 \bmod p, \dots, a^n \bmod p]$ ends in $1$, and $1$ does not appear
anywhere inside the list. But then $a^{n+k} \equiv a^n a^k \equiv a_k \pmod n$, so
$L_{2n}=[a^1 \bmod p, a^2 \bmod p, \dots, a^{2n} \bmod p]$ is
simply two copies of $L_n$; i.e., $L_{2n} = app(L_n, L_n)$. That means that $L_{2n}$ ends in $1$,
and the only ones are $a^{n} \bmod p$ and $a^{2n} \bmod p$. This is easily extended to any multiple
of $n$, and since we know that $a^{p-1} \bmod p = 1$, it follows (almost) immediately that $p-1$
must be a multiple of $\ord(a)$, i.e., $\ord(a) \mid p-1$. This is actually a special case of
Lagrange's theorem for groups, but specialized for $\left(\mathbb{Z}/p\mathbb{Z}\right)^*$.

Another fact about \texttt{order} that is important to our proof is that the order of an inverse
is the same as the order of the element. I.e., $\ord(a^{-1}) = \ord(a)$. We proved this equality by
showing that both inequalities hold, and we used Lagrange's theorem to establish the inequalities.
The end result in ACL2 is as follows
\begin{lstlisting}
(defthmd order-inv
  (implies (and (fep a p)
                (not (equal 0 a))
                (primep p))
           (equal (order (inv a p) p)
                  (order a p)))
  :hints ...)
\end{lstlisting}

We end this section by mentioning that the proof uses many facts about divides and the
greatest common divisor of two integers, formalized as \texttt{divides} and \texttt{g-c-d}
in~\cite{Russ:quadratic-reciprocity,Russ:quadratic-reciprocity-books}. And it also depends on many facts about the arithmetic
functions in $\mathbb{Z}/p\mathbb{Z}$, which were formalized in~\cite{kestrel:pfield}. While we
needed to prove a handful of additional properties about many of these
these functions, the existing formalizations had already established most of the foundational
results, so this was mostly a matter of engineering the lemmas needed for our proof.

\section{A Special Polynomial Congruence}
\label{sec:special-poly}

In this section, we take an aside to consider polynomials modulo $p$. That is, we explore the roots
of polynomial congruences, such as
$$a_0 + a_1 x + \cdots + a_{n-1} x^{n-1} + a_n x^n \equiv 0 \pmod p.$$
The reason that polynomials pop up on a paper about prime numbers, is that polynomials can be used
as an alternative language to describe properties of congruences. For example, Fermat's Little
Theorem can be restated by saying that the polynomial congruence
$$-1 + x^{p-1} \equiv 0 \pmod p$$
has exactly $p-1$ distinct roots in $\left(\mathbb{Z}/p\mathbb{Z}\right)^*$.

Polynomials in ACL2 were formalized in~\cite{DBLP:conf/itp/GamboaC12} (among possibly many others),
but there are significant differences between polynomials and polynomial congruences. For
example, the polynomial $x^2+2$ has no roots among the reals, but the similar polynomial congruence
$x^2+2$ does have a root in $\mathbb{Z}/11\mathbb{Z}$, because when $x=3$,
$x^2+2=3^2+2=11\equiv 0 \pmod {11}$. So many of the properties of polynomials could not be
trivially transferred to polynomial congruences, and they had to be reproved from first principles.

One important lemma is that if $x$ is a root of the product of polynomials \texttt{poly1} and
\texttt{poly2}, then $x$ must be a root of at least one of those polynomials. This result depends
crucially on the fact that $\left(\mathbb{Z}/p\mathbb{Z}\right)^*$ is an integral domain when
$p$ is prime; i.e., if $ab\equiv0 \pmod p$ then either $a\equiv0 \pmod p$ or $b\equiv0 \pmod p$.
This lemma has a (mostly) immediate corollary, that the number of distinct roots of
the product of \texttt{poly1} and \texttt{poly2} is at most the number of distinct roots of
\texttt{poly1} plus the number of distinct roots of \texttt{poly2}. Note that all the roots must
be in $\left(\mathbb{Z}/p\mathbb{Z}\right)^*$, so the number of distinct roots of any polynomial
is at most $p-1$. This also means that it is possible to find a root methodically, by testing if $1$
is a root, or $2$ is a root, an so on. So if we know that a polynomial has a root, finding
that root is guaranteed.

As a special case, consider a linear polynomial of the form $a_0 + a_1 x$, where $a_1 \not\equiv 0 \pmod p$.
Then $a$ is a root of this polynomial congruence if and only if $a = -a_0/a_1 \bmod p$, or in ACL2
\begin{lstlisting}
(defthm root-of-linear-pfield-polynomial
  (implies (and (primep p)
                (non-trivial-pfield-polynomial-p poly p)
                (equal (len poly) 2)
                (fep a p))
           (equal (pfield-polynomial-root-p poly a p)
                  (equal a (neg (div (first poly)
                                     (second poly)
                                     p)
                                p))))
  :hints ...)
\end{lstlisting}
In particular, since the arithmetic operations return a single value, this also shows that a non-trivial
linear polynomial has exactly one root, where by ``non-trivial'' we mean that $a_1 \not\equiv 0 \pmod p$.

Now consider a general polynomial $P(x) = a_0 + a_1 x + \cdots + a_{n-1} x^{n-1} + a_n x^n$ with
$a_n \not\equiv 0 \pmod p$. Suppose that $a$ is a root of this polynomial. Then using the long-division
algorithm for polynomials, we can factor $P(x)$ into $P(x) = (x-a) Q(x)$ where
$Q(x) = b_0 + b_1 x + \cdots + b_{n-2} x^{n-2} + b_{n-1} x^{n-1}$, for some suitable choice
of $b_i$.
\begin{lstlisting}
(defthm eval-poly-with-root
  (implies (and (integer-polynomial-p poly)
                (primep p)
                (integerp a)
                (fep x p)
                (pfield-polynomial-root-p poly a p))
           (equal (eval-pfield-polynomial poly x p)
                  (mul (eval-pfield-polynomial
                        `(,(- a) 1)
                        x p)
                       (eval-pfield-polynomial
                        (cdr (divide-polynomial-with-remainder-by-x+a
                              poly
                              (- a)))
                        x p)
                       p)))
  :hints ...)
\end{lstlisting}
This also shows that if $b$ is a root of $P(x)$, then either $b=a$ or $b$ is a root of $Q(x)$. In other
words, the number of distinct roots of $P(x)$ is at most 1 more than the number of distinct roots of
$Q(x)$.
If $n=1$, we've already seen that $P(x)$ has exactly one root in
$\left(\mathbb{Z}/p\mathbb{Z}\right)^*$. So by induction, the number of roots of $P(x)$ is at most $n$.
\begin{lstlisting}
(defthm num-roots-of-poly-upper-bound
  (implies (and (primep p)
                (non-trivial-pfield-polynomial-p poly p)
                (<= 2 (len poly)))
           (<= (pfield-polynomial-num-roots poly p)
               (len (cdr poly))))
  :hints ...)
\end{lstlisting}
This is, of course, a familiar and expected result for polynomials over the reals, but it is somewhat
surprising over $\mathbb{Z}/p\mathbb{Z}$, since it is possible that $P(a)\ne0$ but that $P(a)\equiv 0 \pmod p$.
I.e., it is possible that $a$ is a root of the congruence, but not of the polynomial over the reals. Nevertheless,
the total number of roots for the congruence is still bounded by $n$.

Now we introduce a special class of polynomials, which we call Fermat polynomials. The function
\texttt{(fermat-poly n)} constructs the polynomial $-1 + x^n$. Now suppose that $n=p-1$.
From Fermat's Little Theorem, it follows that this polynomial has exactly $n=p-1$ roots.
\begin{lstlisting}
(defthm num-roots-of-fermat-poly
  (implies (primep p)
           (equal (pfield-polynomial-num-roots (fermat-poly (1- p)) p)
                  (1- p)))
  :hints ...)
\end{lstlisting}

Now, suppose that $n$ is a composite that can be written as $n=cd$, and again consider the polynomial $-1 + x^n$.
We observe that this polynomial can always be factored as
\begin{equation}
-1 + x^n = -1 + x^{cd} = (-1 + x^d)(1+x^d+x^{2d}+\dots+x^{(c-1)d}).
\label{eqn-1}
\end{equation}
This result is easily proved on paper by expanding the right-hand side and matching up exponents. In ACL2, this is
a more technical proof that is really more about list manipulation. In particular, notice that the second polynomial
on the right-hand side consists of $c-1$ copies of the polynomial $x^d$ and that multiplying is by $x^d$ (and taking
into account the leading 1) results in $c$ copies of $x^d$ with a leading 0 consed in front. Summing the negated
polynomial then cancels all but the last copy of $x^d$, so the result is $-1+x^{cd}$. We found it convenient that
reasoning about exponents was reduced to reasoning about \texttt{cons} and \texttt{append} $k$ times, at which ACL2
excels.

Looking at Eqn.~\ref{eqn-1}, we see that the left-hand side has exactly $n$ roots when $n=p-1$ since $p$ is prime.
But the right-hand side has at most $d+(c-1)d$ distinct roots. Since $d+(c-1)d=d+cd-d=cd=n$, we conclude that both
polynomials in the product of the right-hand side must have the maximum number of distinct roots. In particular,
the polynomial $-1+x^d$ must have exactly $d$ distinct roots. Recall that the only thing special about $d$ is that
is divides $n$, so we have proved the following important technical lemma:
\begin{lstlisting}
(defthm num-roots-fermat-poly-divisor-implicit
  (implies (and (posp d)
                (primep p)
                (divides d (1- p)))
           (equal (pfield-polynomial-num-roots (fermat-poly d) p) d))
  :hints ...)
\end{lstlisting}
We will use this lemma in the next section.

\section{Constructing Elements of Given Order in $\left(\mathbb{Z}/p\mathbb{Z}\right)^*$}
\label{sec:order-construction}

In this section, we show how we can construct an element that has a desired order in
$\left(\mathbb{Z}/p\mathbb{Z}\right)^*$, possibly by using other elements with known smaller order.

For starters, suppose that $a$ has order $m$ and $b$ has order $n$. What is the order of $ab$? In general, there's
not much we can say; e.g., if $b=a^{-1}$ then $ab=1$ so its order is $1$. But when $m$ and $n$ are relatively prime,
that is $\gcd(m,n)=1$, it turns out that the order of $ab$ is equal to $mn$.

To see this, observe that $(ab)^{mn}\equiv1\pmod p$. This follows because
\begin{align*}
(ab)^{mn} &\equiv \left((ab)^m\right)^n \\
          &\equiv \left(a^m b^m\right)^n \\
          &\equiv \left(1 b^m\right)^n \\
          &\equiv \left(b^m\right)^n \\
          &\equiv b^{mn} \\
          &\equiv \left(b^n\right)^m \\
          &\equiv 1^m \\
          &\equiv 1 \pmod p .
\end{align*}
As seen in Sect.~\ref{sec:background}, this implies that $\ord(ab) \mid mn$, which means $\ord(ab) \le mn$.
That is, $\ord(ab) \le \ord(a)\ord(b)$.

Now, suppose that $k$ is such that $(ab)^k\equiv1 \pmod p$. It follows that
$a^k \equiv b^{-k} \equiv (b^{-1})^k \pmod p$. Raising both sides to the power $n$, we have that
$a^{nk} \equiv (b^{-1})^{nk}$. Since $\ord(b^{-1})=\ord(b)=n$, $(b^{-1})^{nk} \equiv 1 \pmod p$, so
$a^{nk} \equiv 1 \pmod p$ as well. This means that $\ord(a^k) \mid m$ and $\ord(a^k) \mid n$, and since
$\gcd(m,n)=1$ this means that the only possible value of $\ord(a^k)$ is 1.

All that is to show that $a^k \equiv b^k \equiv 1 \pmod p$. but that means that $m \mid k$ and $n \mid k$.
Again, since $\gcd(m,n)=1$ this means that $mn \mid k$. The only constraint on $k$ is that $(ab)^k\equiv1 \pmod p$,
so $\ord(ab)$ is such a $k$. This means that $\ord(a)\ord(b) \mid \ord(ab)$, so $\ord(a)\ord(b) \le \ord(ab)$.
Combined with the earlier inequality this shows that $\ord(ab)=\ord(a)\ord(b)$. In particular, given $a$ and $b$
with orders $m$ and $n$ that are relatively prime, this shows that we can construct an element with order $mn$:
\begin{lstlisting}
(defthm construct-product-order
  (implies (and (primep p)
                (fep a p)
                (not (equal 0 a))
                (fep b p)
                (not (equal 0 b))
                (relatively-primep (order a p) (order b p)))
           (equal (order (mul a b p) p)
                  (* (order a p) (order b p))))
  :hints ...)
\end{lstlisting}

We now show how to construct an element that has a different special order. In particular, we wish to show that if
$p$ and $q$ are primes and $q^k \mid n=p-1$, then there is some element
$g_{q^k}$ of $\left(\mathbb{Z}/p\mathbb{Z}\right)^*$ with order $q^k$.

We define the function \texttt{(number-of-powers x q)} which returns the largest power $k$ such that $q^k \mid x$.
For instance, \texttt{(number-of-powers 40 2)} is 3, since $40=2^3\cdot5$. Now suppose that $x$ divides a prime
power $q^n$. Then in fact, $x$ must be one of $1$, $q$, $q^2$, \dots, $q^n$. In particular, $x=q^k$ where $k$ is
the number of powers of $q$ in $x$ (and note that $k\le n$):
\begin{lstlisting}
(defthm factors-of-prime-powers
  (implies (and (primep q)
                (posp x)
                (natp n)
                (divides x (expt q n)))
           (equal x (expt q (number-of-powers x q))))
  :hints ...)
\end{lstlisting}

So suppose that $x$ is such that $x^{q^n}\equiv 1 \pmod p$, assuming for now that such an $x$ exists. Then clearly
$\ord(x) \mid q^n$, which means that $\ord(q^n)$ must be one of $1$, $q$, $q^2$, \dots, $q^n$. Now suppose also
that the order of $x$ is $q^i$ where $i<n$. Then $x^{q^i}\equiv 1 \pmod p$, so $x^{q^j}\equiv 1 \pmod p$ for any $j>i$. This follows because
\begin{align*}
x^{q^j} &\equiv x^{q^{i+j-i}} \\
        &\equiv x^{q^i q^{j-i}} \\
        &\equiv \left(x^{q^i}\right)^{q^{j-i}} \\
        &\equiv 1^{q^{j-i}} \\
        &\equiv 1 \pmod p
\end{align*}
In particular, if the order of $x$ is $q^i$ where $i<n$, it must be the case that $x^{q^{n-1}}\equiv 1 \pmod p$.
\begin{lstlisting}
(defthm order-is-prime-power-lemma
  (implies (and (primep p)
                (primep q)
                (<= q p)
                (fep a p)
                (not (= 0 a))
                (natp n)
                (= (pow a (expt q n) p) 1)
                (not (= (pow a (expt q (- n 1)) p) 1)))
           (equal (order a p) (expt q n)))
  :hints ...)
\end{lstlisting}
Now we address the question of whether such an $x$ exists. I.e., is there an $x$ such that both of these
equations hold:
\begin{align}
x^{q^n}& \equiv 1 \pmod p \label{eqn-2} \\
x^{q^{n-1}}& \not\equiv 1 \pmod p \label{eqn-3}
\end{align}
Note that for such an $x$, $\ord(x)$ is necessarily equal to $q^n$.

This is where the theorems about polynomials proved in Sect.~\ref{sec:special-poly} come into play. Eqn.~\ref{eqn-2}
holds precisely when $x$ is a root of the polynomial congruence $x^{q^n}-1\equiv 0 \pmod p$, and the theorem from
Sect.~\ref{sec:special-poly} guarantees that there are precisely $q^n$ distinct roots of this polynomial congruence,
as long as $q^n \mid p-1$. So there are $q^n$ values of $x$ that satisfy Eqn.~\ref{eqn-2}. Similarly, there are
$q^{n-1}$ values of $x$ that satisfy Eqn.~\ref{eqn-3}, again under the assumption that $q^{n-1} \mid p-1$, which is guaranteed when $q^n \mid p-1$. Since
$q^n > q^{n-1}$, there must be at least one $x$ that satisfies Eqn.~\ref{eqn-2} but not Eqn.~\ref{eqn-3}. It follows,
then that $\ord(x) = q^n$ for this particular $x$. Moreover, as observed earlier, the roots of any non-trivial
polynomial congruence must be one of $1$, $2$, \dots, $p-1$, so it is possible to \emph{find} an appropriate value
of $x$ by searching.
\begin{lstlisting}
(defthm order-is-prime-power
  (implies (and (primep p)
                (primep q)
                (natp n)
                (divides (expt q n) (1- p)))
           (and (fep (witness-with-order-q^n q n p) p)
                (not (= 0 (witness-with-order-q^n q n p)))
                (equal (order (witness-with-order-q^n q n p) p)
                       (expt q n))))
  :hints ...)
\end{lstlisting}
Using the two theorems proved in this section, we will show in the next how to find an element with order $p-1$, i.e.,
a primitive root of $p$.

\section{A Primitive Root of $p$}
\label{sec:primitive-root}

Using the results proved in Sect.~\ref{sec:order-construction}, it is straightforward to
prove that all prime numbers have primitive roots. The typical pen-and-paper proof goes
like this. Suppose that $p$ is an odd prime. (If $p=2$, it is obvious that $1$ is a primitive
root.) Factor the number $p-1$ as a product of prime powers, as in
$$p-1 = {q_1}^{k_1} \cdot {q_2}^{k_2} \cdot \dots  \cdot {q_m}^{k_m}.$$
Now, for each term ${q_i}^{k_i}$, there is an element $c_i$ of order  ${q_i}^{k_i}$.
Note that all the $q_i$ are primes distinct from one another, so the gcd of any ${q_i}^{k_i}$
and any product of other ${q_j}^{k_j}$ must be $1$. So the $c_i$ are numbers of order
${q_i}^{k_i}$ which are relatively prime. So $c=c_1\cdot c_2 \cdot \dots \cdot c_m$ must have
order ${q_1}^{k_1} \cdot {q_2}^{k_2} \cdot \dots  \cdot {q_m}^{k_m} = p-1$.  Thus $c$ is
a primitive root of $p$.

We could have followed this approach in ACL2, and in fact prime
factorization has been formalized in ACL2 and NQTHM numerous times,
e.g., in~\cite{DBLP:conf/acl2/CowlesG06}. But this turned out not to
be very helpful for two reasons. First, the formalization
in~\cite{DBLP:conf/acl2/CowlesG06} uses a different (albeit
equivalent) definition of ``prime.'' This is a common situation in
the ACL2 formalizations of number theory, and it is something that we
would like to see addressed. Second, the result about primitive roots
does not depend on the full Fundamental Theorem of Arithmetic; i.e.,
what we need is that the number $p-1$ can be decomposed into prime
powers, but we do not need that the decomposition is unique.
Naturally, the uniqueness property is the hardest part of the
proof. So simply proving a weak version of prime factorization would
be easy and effective for our purposes.

In fact, it's possible to decompose $p-1$ into powers of primes and
compute the primitive root $c$ at the same time. The first step is to
define the function \texttt{(primitive-root-aux k p)} that finds
an element of order \texttt{k}:
\begin{lstlisting}
(defun primitive-root-aux (k p)
  (if (or (zp k) (= 1 k))
      1
    (let* ((q (least-divisor 2 k))
           (n (number-of-powers k q))
           (k1 (/ k (expt q n))))
      (mul (witness-with-order-q^n q n p)
           (primitive-root-aux k1 p)
           p))))
\end{lstlisting}
The difficult part is helping ACL2 admit this function by proving
that it always terminates. The function \texttt{least-divisor},
defined in~\cite{Russ:quadratic-reciprocity,Russ:quadratic-reciprocity-books}, finds the smallest
divisor (starting at 2) of \texttt{k}. For $k\ge2$, it is shown
in~\cite{Russ:quadratic-reciprocity,Russ:quadratic-reciprocity-books} that this number is always a
prime less than or equal to $k$. Then the function
\texttt{number-of-powers} finds the corresponding exponent in the
prime decomposition of \texttt{k}. Using those facts, we proved that
$k/q^n$ is a natural number that is smaller than $k$, thus proving
the termination of the function.

By inspection, it is easy to see that this function does return the
primitive root $c$ described in the hand proof. We proved that in
ACL2 using an induction suggested by the function
\texttt{primitive-root-aux} to prove the following key theorem:
\begin{lstlisting}
(defthm primes-have-primitive-roots-aux
  (implies (and (primep p)
                (natp k)
                (divides k (1- p)))
           (equal (order (primitive-root-aux k p) p)
                  k))
  :hints ...)
\end{lstlisting}
In order for this to work, we had to prove a number of technical
lemmas. For starters, since we're using induction as suggested by
\texttt{primitive-root-aux} we have to show that $k/q^n$ is
a natural that divides $p-1$ whenever $k$ is a natural that divides
$p-1$. And we also had to show that the two terms multiplied in
\texttt{primitive-root-aux} satisfy the conditions of the theorems
\texttt{construct-product-order} and
\texttt{order-is-prime-power} that are used to create the element
of order $k$. The most interesting are
\begin{itemize}
\item  the functions return elements in the multiplicative group
$\left(\mathbb{Z}/p\mathbb{Z}\right)^*$,
\item in particular the result of those operations is never $0$,
\item the number $k/q^n$ divides $p-1$ if $k$ divides
$p-1$,
\item and the gcd of $q^n$ and $k/q^n$ is $1$.
\end{itemize}
Once that is done, the primitive root of $p$ can be defined and
shown to be a primitive root as follows:
\begin{lstlisting}
(defund primitive-root (p)
  (primitive-root-aux (1- p) p))

(defthm primes-have-primitive-roots
  (implies (primep p)
           (equal (order (primitive-root p) p)
                  (1- p)))
  :hints ...)
\end{lstlisting}

\section{Conclusion}
\label{sec:conclusion}

In this paper, we presented a proof that all prime numbers have at least one primitive root.
In fact, the number of primitive roots of $p$ can be shown to be $\phi(p-1)$ where $\phi$ is
Euler's totient function (the number of positive integers up to $n$ that are relatively prime
to $n$). Proving that would be a nice extension to this work that could happen in the future.

The proof relied on prior work on number theory, but our experience suggests that the prior
work is scattered across many directories in the community books. Moreover, many foundational
results needed to be proved to complement the existing foundations. This reflects the fact that
the development of number theory in ACL2 has been driven by specific results, so the foundations
developed in each project are tailored to support the needs of those specific projects. Given the
importance of number theory in areas such as cryptography, as well as the suitability of ACL2 to
reason effectively about this branch of mathematics, we think is would be a great time to consolidate
these formalizations in ACL2 under a common location in the community books.
Recent discussions in the ACL2 mailing list suggest that there is enough momentum to carry
out this project.

\nocite{*}
\bibliographystyle{eptcs}

Logic in Computer Science (cs.LO); Discrete Mathematics (cs.DM)

All Prime Numbers Have Primitive Roots