On the maximum number of integer colourings with forbidden monochromatic sums

Let $f(n,r)$ denote the maximum number of colourings of $A \subseteq \lbrace 1,\ldots,n\rbrace$ with $r$ colours such that each colour class is sum-free. Here, a sum is a subset $\lbrace x,y,z\rbrace$ such that $x+y=z$. We show that $f(n,2) = 2^{\lceil n/2\rceil}$, and describe the extremal subsets. Further, using linear optimisation, we asymptotically determine the logarithm of $f(n,r)$ for $r \leq 5$. Similar results were obtained by H\`an and Jim\'enez in the setting of finite abelian groups.


Introduction and results
A recent trend in combinatorial number theory has been to consider versions of classical problems from extremal graph theory in the sum-free setting. We state some examples. The famous theorem of Mantel from 1907 [30] states that every n-vertex graph with more than n 2 /4 edges necessarily contains a triangle. On the other hand, it is not hard to show that every subset A of [n] := {1, . . . , n} of size more than n/2 necessarily contains a Schur triple, or sum; that is, a triple {x, y, z} of not necessarily distinct elements such that x + y = z. The name dates back to a result of Schur from 1916 which states that every r-colouring of [n] contains yields a monochromatic Schur triple when n is sufficiently large. Its graph-theoretic counterpart is Ramsey's theorem from 1928 which guarantees a monochromatic clique in any r-edge-colouring of a sufficiently large complete graph. The triangle removal lemma of Ruzsa and Szemerédi [33] states that every n-vertex graph containing o(n 3 ) triangles can be made triangle-free by removing o(n 2 ) edges. In the sum-free setting, the removal lemma of Green [15], and Král', Serra and Vena [27] states that every A ⊆ [n] containing o(n 2 ) Schur triples can be made sum-free by removing o(n) elements. Erdős, Kleitman and Rothschild [12] proved that the number of n-vertex triangle-free graphs is 2 n 2 /4+o(n 2 ) , that is, the obvious lower bound of taking every subgraph of a maximal triangle-free graph is, in a sense, tight. In the sum-free setting, resolving a conjecture of Cameron and Erdős [7], Green [14] and independently Sapozhenko [34] proved that, for every i = 0, 1, there exists a constant C i , depending only on the parity of n, such that [n] contains (C i + o(1))2 n/2 sum-free sets. So again, the obvious lower bound is tight.

The Erdős-Rothschild problem for cliques in graphs
Taking inspiration from the extremal graph theory literature, in this paper we consider another classical graph problem in the sum-free setting: the problem of Erdős and Rothschild [10,11], which is stated as follows. Given an n-vertex graph G and positive integers r, k, say that a colouring of its edges with r colours (an r-edge-colouring) is valid if there are no monochromatic copies of K k . Among all such graphs G, what is the maximum number F (n, r, k) of valid colourings? Ramsey's theorem implies that any graph containing a sufficiently large clique has no valid colourings. Clearly, any colouring of a K k -free graph is valid. Turán's theorem implies that the largest such graph is T k−1 (n), the complete balanced (k − 1)-partite graph. Thus we obtain the bound F (n, r, k) r t k−1 (n) , (1.1) where t k−1 (n) is the number of edges in T k−1 (n). Erdős and Rothschild conjectured that this trivial lower bound is tight when n is large and (r, k) = (2, 3) (i.e. one uses two colours and forbids monochromatic triangles), and further, that T 2 (n) is the unique extremal graph. This was confirmed for all n 6 by Yuster [38], who himself extended the conjecture to larger cliques in the case r = 2. This was in turn verified by Alon, Balogh, Keevash and Sudakov [1], showing that for r ∈ {2, 3}, equality holds in (1.1), and T k−1 (n) is the unique extremal graph. They further showed that for all r, k ∈ N such that k 3 and r 2, the limit lim n→∞ log r F (n, r, k)/ n 2 exists. In other words, there is some h(r, k) such that F (n, r, k) = r h(r,k)( n 2 )+o(n 2 ) . (

1.2)
However, for every other choice of (r, k), there exists a constant c = c(r, k) such that h(r, k) > (k − 2)/(k − 1) + c. Note that the edge density of T k−1 (n) tends to (k − 2)/(k − 1) with n, so this says that the trivial lower bound in (1.1) is not correct for all other choices Theorem 1.2. There exists n 0 > 0 such that for all integers n n 0 , we have f (n, 2) = 2 n/2 .
Note that in the sum-free setting with two colours, there is an extremal subset which is not sum-free, namely {n/2, . . . , n}, containing at most one sum. In the graph setting with two colours, there is a unique extremal graph, namely T 2 (n), which is K 3 -free.
As in the graph setting, our results decrease in strength as the number of colours increases. When r = 3 we can obtain the following stability theorem. Moreover, the following holds. For all ε > 0 there exist δ, n 0 > 0 such that for all integers n n 0 , whenever A ⊆ [n] satisfies f (A, 3) f (n, 3) · 2 −δn , we have that either |A O | εn; or |A I 2 | εn.
In particular, we are able to asymptotically solve the 5-colour case of the Erdős-Rothschild problem in the sum-free setting, in contrast to the graph setting in which it is wide open for triangles and larger cliques. (This turns out to be a consequence of the rigid structure of large maximal sum-free sets.) The (asymptotic) lower bounds in Theorems 1. .
The first assertion follows from the fact that every r-colouring of a sum-free set A is valid, so f (A, r) = r |A| . For the second, note that any colouring σ : O ∪I 2 → [r] with σ(x) ∈ {1, . . . , r/2 } whenever x ∈ O \ I 2 , and σ(y) ∈ { r/2 + 1, . . . , r} whenever y ∈ I 2 \ O (and O ∩ I 2 coloured arbitrarily), is valid, giving the claimed bound. Note that r 1/2 > (r r 2 /4 ) 1/4 for r ∈ {2, 3}, while this inequality becomes an equality for r = 4, and reverses for r 5. It is tempting to believe that the bound in Theorem 1.4 holds for all r 4. However, this is not true. Indeed, when r is large, the set A := O ∪I 2 ∪ {x ∈ [n] : x ≡ 1, 4 mod 5} contains exponentially more valid r-colourings than the electronic journal of combinatorics 28(1) (2021), #P1.59 O ∪I 2 . To see this, suppose for simplicity that 3|r, and partition the colours [r] into three sets C O , C I 2 , C 1,4 of equal size. Let any colour in C O be allowed for any x ∈ O, and do the same for C I 2 and I 2 , and for C 1,4 and {x ≡ 1, 4 mod 5}. So, for example the largest 10t + 1 which is less than n has all r colours allowed since it lies in all three sets. Any colouring which only uses allowed colours for each vertex is valid, since each of the three sets is sum-free. Calculating the sizes of the intersections, we see that It would also be interesting to see if for every fixed integer r 6, the limit lim n→∞ log f (n, r)/n exists.

Some remarks on the methods and proofs
An important tool in our proof is the Green's container theorem for finite abelian groups (Theorem 2.2). The special case that we need states that, for every positive integer n, there is a small family F of subsets of [n], called containers, each of which is almost sum-free, and such that every sum-free subset of [n] lies in some member of F. In the proof of the main result in [1] and other Erdős-Rothschild-type results for graphs, Szemerédi's regularity lemma [36] is used to approximate a large graph G by another graph of bounded size (the reduced graph). Then, for each valid r-edge-colouring σ of G, for each i ∈ [r] one can approximate the K k -free subgraph σ −1 (i) of G by a K k -free graph of bounded size. In our proofs, for each valid r-colouring of [n], we approximate the sum-free subset σ −1 (i) of [n] by a container.
We use Theorem 2.2 to reduce the problem of determining f (n, r) to solving an optimisation problem (Problem 2) whose maximum approaches log f (n, r)/n as n tends to infinity (Theorem 3.3). Roughly speaking, Problem 2 involves layering sum-free subsets A 1 , . . . , A r of [n] and measuring a weighted overlap g(A 1 , . . . , A r ).
To attack Problem 2, we require a second important tool, namely a very strong stability theorem of Deshoulliers, Freiman, Sós, and Temkin [9], which was recently strengthened by Tran [37]. This states that every sum-free subset of [n] is either 'small', or has a very rigid structure: either it contains only odd elements, or it somehow resembles the interval I 2 . Now it turns out that, if r is small and (A 1 , . . . , A r ) is a maximiser for Problem 2, then at most one of the A i can be small. The rigid structure of the others means that the feasible set for Problem 2 is not too large. In fact, for r ∈ {2, 3}, it can be easily solved at this stage, and we find that either all of A 1 , . . . , A r are close to O; or they are all close to I 2 . This proves Theorem 1.3 and completes the first step of the proof of Theorem 1.2.
To complete the proof of Theorem 1.2, we have two cases to consider. The solution to Problem 2 when r = 2 implies that any subset A ⊆ [n] with f (A, r) = f (n, r) satisfies either (1) |A O | = o(n); or (2) |A I 2 | = o(n). We use stability arguments, together with techniques from [5], to obtain the exact structure of A.
For r ∈ {4, 5}, we find a reduction of Problem 2 to a linear optimisation problem. First, for each i ∈ [r], and any feasible (A 1 , . . . , A r ) ⊆ [n] r , we obtain d i ∈ [0, 1] which are each functions of A 1 , . . . , A r and n and such that g(A 1 , . . . , A r ) is linear in d 1 , . . . , d r . Now, using the structural information returned from stability, we obtain constraints, linear in d 1 , . . . , d r , which every maximiser (A 1 , . . . , A r ) must satisfy. This gives rise to a linear program in the variables d 1 , . . . , d r . Now, this linear program is a relaxation of Problem 2, so its maximiser may not correspond to a feasible solution (A 1 , . . . , A r ) of Problem 2. But, if we can exhibit a feasible tuple (A 1 , . . . , A r ) such that the maximum M of this program satisfies M = g(A 1 , . . . , A r ), then (A 1 , . . . , A r ) is a maximiser of Problem 2. Thus our task is to find enough constraints (of sufficient strength) so that this is possible. In so doing, we will prove Theorem 1.4.

The Erdős-Rothschild problem in other settings
Erdős and Rothschild also considered the problem of counting monochromatic H-free colourings, for an arbitrary fixed graph H. In [1], it is shown that the analogue of their main result for cliques in fact holds when H is colour-critical. Further cases including matchings, stars, paths, trees were investigated in [20,21]. Other works have considered a fixed forbidden colour pattern of H, see [3,6,22,24].
An analogous problem for directed graphs was solved by Alon and Yuster [2], who determined, for each k-vertex tournament T , the maximum number of T -free orientations of an n-vertex graph, when n is sufficiently large. The hypergraph analogue was addressed in [19,28,29].
The authors of [8] and [19] considered the problem of counting the number of colourings of families of r-sets such that every colour class is -intersecting. A related result in the context of vector spaces over a finite field GF (q) is proved in [23]. These results are Erdős-Rothschild versions of the classical Erdős-Ko-Rado theorem.
During the preparation of this paper, we became aware of the results of Hàn and Jiménez [18] who recently studied similar questions in the setting of finite abelian groups. Given a finite abelian group (Γ, +), define an r-colouring of A ⊆ Γ to be valid if it has no monochromatic sum. Let f (Γ, r) be the maximum number of valid r-colourings among all subsets A of Γ. The results of Hàn and Jiménez show a close relationship between f (Γ, r) and the largest sum-free sets of Γ, and characterise for r 5 the extremal sets. As in [18], our proof begins with an application of the container method. But, in the setting of abelian groups, there is much more structure than in the integer setting, and thus one can obtain much stronger stability, leading to precise results for larger values of r than those obtained here. Most of the work in this paper goes into dealing with 'interval-like' sets, (an analogue of which) does not appear in the abelian group setting. We remark that Hàn and Jiménez's result and ours do not imply one another.

Organisation of the paper
Section 2 sets up the notation we will use and contains the statements of results on sumfree sets necessary for the proof. In Section 3 we define Problem 2, the optimisation problem whose maximum is a parameter g(n, r) which is closely related to f (n, r). Then in Sections 4, 5 and 6 we prove Theorems 1.3, 1.2 and 1.4 respectively. We make some concluding remarks in Section 7.

Notation and Preliminaries
In this section we define the notation that we will use, and some results on sum-free subsets which are needed in our proofs.

Notation
Given integers m, n such that m n, we write [m, n] to denote the set {m, . . . , n}, and write [n] := [1, n]. For a set A ⊆ [n], we define d(A) = |A|/n and min(A) to be the density and the minimum element of A, respectively. We also define E and O to be respectively the set of all even and odd integers in [n]. As we defined earlier, I 2 := [ n/2 + 1, n] and I 1 := [ n/2 ] (we suppress the dependence on n in the notation). Given A, B ⊆ Z, we write A + B := {a + b : a ∈ A, b ∈ B}, and A B := (A \ B) ∪ (B \ A) denotes the symmetric difference of A and B. For any x ∈ Z, we also write x · A := {xa : a ∈ A}. Logarithms will always be taken to the base 2.

Tools for sum-free subsets
The first result we state is a very strong stability theorem for sum-free subsets due to Deshouillers, Freiman, Temkin and Sós [9]. It states that every large sum-free set S ⊆ [n] either contains no even number, or is, in a certain sense, close to the interval I 2 . Throughout the rest of the paper, we refer to such sum-free sets as type (a), type (b), and type (c) respectively.
We use the following container theorem of Green [15] (see also Green-Ruzsa [16,17]), which, for large n, guarantees a small collection of subsets of [n] which somehow approximates the collection of sum-free sets. We should also mention that (hyper)graph containers have been used successfully in many contexts, see [4,25,26,35].
It is convenient for us to include property (iv) although it is not explicitly stated in [14], but follows immediately from (i), Theorem 2.1 and Theorem 2.3. Given a sumfree set S ⊆ [n], the set F ∈ F guaranteed by (ii) is called a container for S. We also need the following removal lemma of Green [14,15], and Král', Serra and Vena [27], which guarantees that a subset of [n] containing o(n 2 ) sums can be made sum-free by removing o(n) elements. This result comes from applying a regularity lemma and consequently the dependence of δ on ε is rather poor: 1/δ is at most a tower of twos of height logarithmic in 1/ε, which follows from [27] and [13]. Finally, we will use the following folklore inequality which bounds the size of the set A + B: Proposition 2.4. For all finite non-empty subsets A, B of Z, we have that |A + B| |A| + |B| − 1.

An equivalent covering problem
In this section, we define a new maximisation problem whose value g(n, r) is closely related to f (n, r). Then, for the rest of the paper, it suffices to consider this new problem. To motivate the problem, consider the following procedure for finding a subset A ⊆ [n] with many valid colourings. Let r ∈ N be the number of colours, as usual, and choose sum-free subsets A 1 , . . . , A r of [n]. Then the number of valid colourings of i∈[r] A i is at least the number of colourings σ which colours x with some i such that x ∈ A i . If x lies in many A i then the number of choices for σ(x) is large. So a choice of A 1 , . . . , A r with a large appropriately weighted overlap generates many valid colourings. We now make this precise. Equivalently, for each i ∈ [r] let D i := I∈2 [r] :|I|=i E I ; that is, the set of all elements that are in exactly i different A j 's. Let d i := |D i |/n and define (d 1 , . . . , d r ) to be the intersection vector of (A 1 , . . . , A r ).
Define also g(A, r) to be the maximum of g(A 1 , . . . , A r ) over all tuples of sum-free subsets Remark 3.2. It is not hard to see that, for every n and r, there is always some tuple (A 1 , . . . , A r ) of sum-free subsets of [n] which is extremal (that is, g(A 1 , . . . , A r ) = g(n, r)), and A i is a maximal sum-free subset for all i ∈ [r]. It will be useful to choose such an extremal tuple later, since if we know e.g. that A i contains no even element, then by Theorem 2.1 we can assume that A i = O.
The first step in the proofs of Theorems 1.2-1.4 is to show that these problems are, in a sense, equivalent.
For all ε > 0 and r ∈ N, there exists n 0 > 0 such that the following holds for all integers n n 0 .
implies that σ is valid. Thus the number of such σ is a lower bound for the total number of valid colourings, and so as required.
For the remainder of the proof we focus on the upper bound. Fix an integer r and let ε > 0. We may assume that ε 1/r. Choose η such that 0 < η ε. Apply Theorem 2.3 to obtain γ, n 0 > 0 such that, for all integers n n 0 , every A ⊆ [n] which contains at most γn 2 Schur triples may be made sum-free by removing at most ηn elements. Without loss of generality we may assume that γ η. Theorem 2.2 implies that, by increasing n 0 if necessary, for all integers n n 0 , there exists a family F = F n of containers such that (i) every F ∈ F contains at most γn 2 Schur triples; (ii) every sum-free subset of [n] lies in at least one F ∈ F; (iii) |F| 2 γn ; and Without loss of generality, we may assume that 1/n 0 γ and ε 1. We have the hierarchy 1/n 0 γ η ε 1.
Given any n n 0 and the family F of containers, for each F ∈ F, fix a largest sum-free subset F * of F (note that there may be more than one choice of F * , but we just pick one of these). Then (i) together with Theorem 2.3 implies that |F | − ηn |F * | |F |. Now let n n 0 be an integer, and A ⊆ [n] be arbitrary. Consider any fixed valid r-colouring σ of A.
Thus, for each valid colouring σ : A → [r], we obtain a tuple (F * 1 , . . . , F * r ). Observe that but F * i may contain many elements which do not lie in σ −1 (i). We now claim that the following procedure generates every valid colouring σ of A, and therefore the number of choices in this procedure is an upper bound on f (A, r). Each choice will generate a colouring τ : 1. For all i ∈ [r], choose a container G i ∈ F, and let G * i be the largest sum-free subset of G i we fixed earlier.

For each
3. For each I ∈ 2 [r] \ {∅} and x ∈ E I , choose i ∈ I and set τ (x) := i.
We need to show that there is a choice in (1)-(4) which will yield τ = σ. In (1), for each i ∈ [r], (ii) and the fact that σ is valid implies that we can choose (2) is fixed by our choices in (1). In (3), by construction, for every In (4) we are free to colour the uncoloured elements of A with σ. Since σ was an arbitrary valid colouring of A, we have proved the claim.
Thus it remains to count the number of colourings generated by (1)-(4). Given a tuple (G 1 , . . . , G r ) ∈ F r of containers, let C be the set of colourings τ : A → [r] generated by it, i.e. the set of τ which arise from the procedure after fixing the choice (G 1 , . . . , G r ) in (1). Observe that (G 1 , . . . , G r ) gives rise to a unique tuple (G * 1 , . . . , G * r ). Then, since the only choices are in (3) and (4), we have Taking logarithms, we have, bounding very loosely, that But, by (iii), the number of choices of (G 1 , . . . , G r ) ∈ F r is at most |F| r 2 rγn , so completing the proof of the upper bound. The second assertion is an obvious consequence of the first.
The discussion about lower bounds after the statement of Theorem 1.4 amounts to the following inequalities: For all integers n r 2, we have log f (n, r) n g(n, r) g(I 2 , . . . , I 2 r ) = n/2 n · log r; and (3.6) log f (n, r) n g(n, r) g(I 2 , . . . , I 2 (3.7) Figure 1 shows these three constructions in the case when r = 4, when they each give rise to roughly the same lower bound. Theorem 1.4) implies that each one is in fact an approximate optimal solution of Problem 1. Observe the following easy correspondence between feasible solutions of Problems 1 and 2. Given a feasible solution A ⊆ [n] of Problem 1 and a valid r-colouring σ of A, we have that (σ −1 (1), . . . , σ −1 (r)) is a feasible solution of Problem 2. Given a feasible solution Theorem 3.3 is essentially an analogue of the main result of [31]. Recall the parameters h(r, k) and F (n, r, k) related to the graph problem defined in the introduction, related by (1.2). Informally speaking, determining g(n, r) involves layering r sum-free subsets of [n] so that an appropriately weighted overlap is as large as possible, whereas determining h(r, k) involves layering r finite K k -free graphs so that their weighted overlap is as large as possible. Importantly and unfortunately, g(n, r) does of course depend on n. However, the cases in which F (n, r, k) has been determined (when r is small) give us some valuable intuition for determining g(n, r) (and hence approximately determining f (n, r)): namely that for an extremal tuple (A 1 , . . . , A r ) of sum-free sets, each A i should perhaps be a largest sum-free set: either O or I 2 . Unlike in the case of graphs, our ground set [n] comes with a fixed labelling. So there is only one way to layer, say, O and I 2 , whereas there are many ways to layer any two r-vertex graphs G and H.

The proof of Theorem 1.3
Given Theorem 3.3, it is now a fairly simple task to obtain stability in the case when r = 2, 3. Indeed, it suffices to prove stability for Problem 2.

Proof: Let
Clearly A is a sum-free subset of [n]. Suppose that A contains at least one even element.
Then Theorem 2.1 implies that A is a sum-free set of type (c), so min(A ) |A | (1/2 − ε/(3r))n, and so |A Finally, since every sum-free subset of [n] has size at most n/2 , we have Similarly, in the second case, we have |A O | εn.
First consider the case when r = 2. Then as required. Now let r = 3. Then Recall that each A i is sum-free with size at most n/2 , so , a contradiction to (4.1).
Note that, in the case r = 3, the proof proceeds by solving a linear program in variables d 1 , d 2 , d 3 (and similarly for r = 2, but the program is trivial here). The very same linear programs (approximately) yield F (n, r, 3) for r ∈ {2, 3} in the proof of the main result in [1], where the variables d i correspond to densities of overlapping triangle-free graphs. Proof. Choose n 0 > 0 sufficiently large so that the conclusion of Theorem 3.3 holds with parameter ε/(200r) and the conclusion of Lemma 4.1 holds with parameter ε/2. Let n n 0 be an integer and let . The first assertion f (n, 3) = 3 n/2+o(n) of Theorem 1.3 follows immediately from Corollary 4.3.

The proof of Theorem 1.2
In this section we use Corollary 4.3 to prove Theorem 1.2. Before starting the proof, we need the following useful notion. This notion is useful since each edge in a link graph represents some restriction of valid colourings, and so a large set of independent edges in a link graph limits the number of valid colourings.
Proof. We will bound f (A, r) by re-constructing valid colourings using the following procedure, in which every choice yields a colouring τ : A → [r] (which may or may not be valid).
To see that the procedure generates every valid r-colouring of A, we just need to check that every valid r-colouring σ has the property in Step 2 (since all other choices were arbitrary). That is, for all uv ∈ M x , σ does not assign u, v, x the same colour. But this is clear since {u, v, x} is a Schur triple.
Therefore the number of colourings generated by the procedure is an upper bound for f (A, r). Thus, using the fact that as required.
We are now ready to prove Theorem 1.2.
Proof of Theorem 1.2. First recall that 2 n/2 is a lower bound for f (n, 2) for all positive integers n, since we can always find a sum-free subset of [n] of size n/2 , namely O or I 2 , and colour it arbitrarily. Let 0 < ε < 1/200 and apply Corollary 4.3 to obtain n 0 . Now fix an integer n > n 0 , and let A ⊆ [n] be such that f (A, 2) = f (n, 2) 2 n/2 . By Corollary 4.3, we have that either |A O | εn; or |A I 2 | εn. Thus |A| n/2 + εn. We will use the following claim throughout the proof. To find large matchings in link graphs in the next two claims, we will use the fact that a graph G with e edges and maximum degree ∆ contains a matching of size at least e/(∆ + 1). This is an immediate consequence of Vizing's theorem on edge-colourings.
In this case, we will prove that A = O. It suffices to show that A ∩ E = ∅, since for any such set we have f (A, 2) = 2 |A| , and thus A = O is clearly the unique extremal subset among such sets. Arguing by contradiction, the following claim together with Claim 5.3 will complete the proof of this case. Now A has few elements from I 1 . We first show that the smallest element in A cannot be far from n/2. For all i ∈ N, denote by δ i the i th smallest element in A (denoted so in analogy with the minimum degree of a graph). We may suppose that δ 1 ∈ I 1 since otherwise A ⊆ I 2 is sum-free and we are done.
So n is even and we have equality if and only if (δ 1 , δ 2 ) = (n/2, n/2+1). Since I 2 \A ⊆ {n}, there are two candidates for extremal sets: [n/2, n − 1] and [n/2, n]. It is easy to see that both have 2 n/2 valid 2-colourings. This completes the proof of Theorem 1.2.
6 The proof of Theorem 1.4 To prove Theorem 1.4, by Theorem 3.3, it suffices to determine g(n, r) asymptotically.
Recall from the proof of Lemma 4.1 that we reduced the problem of asymptotically determining g(n, 3) to solving a linear program. Indeed, we let (A 1 , A 2 , A 3 ) be a tuple of sum-free subsets with intersection vector (d 1 , d 2 , d 3 ), and found a reduction of Problem 2 into a linear program in variables d 1 , d 2 , d 3 . Our task was then to maximise i∈ [3] d i log i subject to d 1 , d 2 , d 3 0 and i∈ [3] id i 3 n n 2 . So to prove Theorem 1.4, we will again reduce Problem 2 to a linear program in variables d 1 , . . . , d r and an additional slack variable a (defined below) for r ∈ {4, 5}.
Given n ∈ N, let A 1 , . . . , A r ∈ [n] be maximal sum-free sets. Throughout the rest of this section, define D 1 , . . . , D r , d 1 , . . . , d r as in Problem 2. So D i is the set of all elements that are in exactly i different A j 's, and d i := |D i |/n. Further, we let C be the subset of [r] such that A i is a type (a) set for every i ∈ C, and a type (b) or (c) set, for every i ∈ [r] \ C. Recall that a type (a) set S has |S| 2n/5 + 1, a type (b) set consists of odd numbers, and a type (c) set S has |S| min(S). We define where N (r) is a positive integer depending only on r; and α ij , β i , α i ∈ R for all i N (r) and j r. We say that this set C of inequalities is (ε, r)-sufficient if max i∈ [r] f i log i subject to C is at most max 1 2 log r, 1 4 log(r r 2 /4 ) + ε.
When (d 1 , . . . , d r ) playing the roles of (f 1 , . . . , f r ) satisfies C, we say that C is a family of constraints for A 1 , . . . , A r .
We will always use d i instead of f i since we are only interested in C which are a family of constraints for A 1 , . . . , A r . However, we would like these constraints to be independent of n, which of course A 1 , . . . , A r are not. As an example, when r = 3, we showed that, for every ε > 0, sufficiently large n ∈ N and sum-free subsets A 1 , A 2 , A 3 of [n], the family of constraints is (ε, 3)-sufficient. Indeed, the final constraint we actually derived was d 1 + 2d 2 + 3d 3 3/n · n/2 , but we can replace the right-hand side by the weaker 3/2 + ε/300 to obtain a family which is still a family of constraints for A 1 , A 2 , A 3 , but is independent of n.
The proof of Theorem 1.4 follows from the next lemma.
For each r = 4, 5, we split the proof of Lemma 6.2 into cases depending on the structure of A 1 , . . . , A r (obtained from Theorem 2.1). Then, in each case, we find a family of constraints which is (ε, r)-sufficient. Given a family C of inequalities, we must 1. show that it is a family of constraints for A 1 , . . . , A r , i.e. that each inequality holds; then 2. show that it is (ε, r)-sufficient, i.e. consider the linear program max i∈[r] d i log i subject to C, and show that its optimal solution is at most the required value.
Since it is only a series of tedious calculations, we defer the details of (2) to the appendix, and limit ourselves to some remarks here.

Achieving (2): Solving linear programs
Since there are many cases (depending on the structure of A 1 , . . . , A r ), and sometimes rather a lot of inequalities in each family of constraints C, where possible, we use Mathematica to solve the resulting linear program max i∈[r] d i log i subject to C. Suppose that r = 4 (when r = 5 the situation is similar). Given A 1 , . . . , A 4 , an (ε, 4)-sufficient family C is such that i∈ [4] d i log i 1 + ε.
There are two cases, depending on whether the 4-tuple (A 1 , A 2 , A 3 , A 4 ) is close to extremal or not, namely if g(A 1 , A 2 , A 3 , A 4 ) is close to the maximum possible value or not. Suppose that there is some specific value of ε, say ε = 1/1000, and a family C of constraints which is (1/1000, 4)-sufficient, for which Mathematica shows the output i∈ [4] d i log i 0.999. The level of accuracy of the program is enough for us to know that certainly g(A 1 , . . . , A 4 ) = i∈ [4] d i log i < 1 − 1/2000. So in this case, we are done, and in fact since this number is less than our lower bound (3.6) by some absolute constant, we see that A 1 , . . . , A 4 cannot be close to extremal.
If instead, given input ε = 1/1000, Mathematica shows an output 0.999 < i∈ [4] d i log i 1.001, say, we need to be more careful. In this case, we will write out the dual program of max i∈ [4] d i log i subject to C, which is a minimisation problem. We then exhibit a feasible solution to the dual which is at most 1 + ε. By the weak duality theorem, we see that max i∈ [4] d i log i 1 + ε, as required.

Linear constraints for general r
To achieve (1), we will first derive a set of linear constraints which apply for any number r of colours. Lemma 6.3. For all ε > 0 and integers r 4, there exists an n 0 > 0 such that the following holds. Let n, s, t ∈ N be such that n n 0 and s + t r. Also, let A 1 , . . . , A r be maximal sum-free subsets of [n] such that s of them are of type (b), t of them are of type (c), and (d 1 , . . . , d r ) is their intersection vector. Then Proof. Let n 0 := r/ε and let n n 0 be an integer. For the first inequality, by (6.1) and the definition of type (a) sets, we have where the last inequality follows from n r/ε. To prove the second part of the lemma, since ε r/n 1/n, it suffices to show that the set D r ∪ D r−1 is sum-free. Assume to the contrary that there exist we have |I i | 1 for every i ∈ [3], and therefore i∈ [3] I i r − i∈ [3] I i r − 3 1, where the last inequality follows from r 4. Therefore, there exists an i ∈ [r] such that x 1 , x 2 , x 3 ∈ A i , which contradicts A i being sum-free.
We will use the next two simple facts repeatedly. Recall that d(A) = |A|/n is the density of a set A.
Observation 6.4. Let k ∈ [r] and suppose that A 1 , . . . , A k are of type (c). Then The next lemma concerns the size of the intersection of type (c) sets. Lemma 6.5. For all ε > 0 and positive integers r, there exists an n 0 > 0, such that the following holds. For every integer n n 0 , let A 1 , . . . , A r be maximal sum-free subsets of [n] such that there is some k r for which A 1 , . . . , A k ⊆ [n] are type (c) sets. Then Proof. Let n 0 := k/ε and let n > n 0 be an integer and A 1 , . . . , A r be subsets of [n] as in the statement. Let A * = ∩ i∈[k] A i . It suffices to show that |A * | n/2 − (k − 1) − kan. By Observation 6.4(i), for all i ∈ [k], min(A i ) n/2 − an, and therefore A i ⊆ I a := [ n/2 − an, n]. Every integer x in the subset L := I a \ A * lies in at most k − 1 of the A i 's. Assume to the contrary that |A * | < n/2 − (k − 1) − kan. Then But Observation 6.4(ii) implies that i∈[k] |A i | k n/2 − an. Together with (6.2), we get 2(k − 1) n/2 < (k − 1)n, a contradiction.
Throughout the rest of the paper, given a defined by (6.1), we will let J 1 := 1, n 2 − an , J 2 := n 2 − an + 1, n , and J 3 := n 2 − an + 1, n 2 (6.3) and will refer to J 1 , J 2 , and J 3 as the first, second, and middle interval respectively. Note that by definition an is an integer, and the set J 2 \J 3 = I 2 is a sum-free set of maximum size n/2 . The following observation is a straightforward consequence of Observation 6.4(i) and the fact that the unique maximal sum-free subset of type (b) is O.
The final result in this subsection states some constraints involving the intervals J 2 and J 3 . Lemma 6.7. For all ε > 0 and positive integers r, there exists n 0 > 0 such that the following holds. Let k r and n n 0 be positive integers. Let A 1 , . . . , A k ⊆ [n] be type (c) maximal sum-free sets and define Proof. Let n 0 := r/ε and let n n 0 be an integer. Since A i has q i n elements in J 3 , we have that min(A i ) n 2 + 1 − q i n for all i ∈ [k]. Using Observation 6.4(ii) and the definition of type (c) sets, we have Thus i∈[k] q i k/n + a a + ε, proving the first inequality. To prove (ii), let B := I 2 \ ∩ i∈[k] A i . In other words, B is the set of all elements in I 2 = J 2 \ J 3 that are missing from at least one of the A 1 , . . . , A k . By Observation 6.6(i),

i∈[k]
q i + a + ε, and the second required inequality follows.

The 4 colour case
Proof of Lemma 6.2 when r = 4. Let ε > 0 and choose ε , n 0 > 0 such that 1/n 0 ε ε 1/100. (we may assume the last inequality without loss of generality). Let n n 0 be an integer and let A 1 , . . . , A 4 ⊆ [n] be maximal sum-free sets with intersection vector (d 1 , . . . , d 4 ) as defined in Problem 2. We need to obtain a family of (ε, 4)-sufficient constraints. We have the following set of basic constraints which will be used throughout the proof: where the last inequality follows from Lemma 6.3. Suppose first that there is an i ∈ [4] for which A i is of type (a). By Lemma 6.3, we have that i∈ [4] id i 19/10 + ε .
Thus we can assume that all of A 1 , . . . , A 4 are of type (b) or (c). Let s be the number of A i of type (b) and t the number of A i of type (c) (so s + t = 4). Define a as in (6.1). By Lemma 6.3 (with ε playing the role of ε), we have i∈ [4] id i 2 − a + ε . (C2) Suppose that a 1/10. Then (C2) implies that (C0 * ) holds. But, as we have seen, {(C0 * ), (C1)} is an (ε, 4)-sufficient family. Hence, throughout the rest of the proof, we can assume that a < 1/10.
Suppose first that t = 0. Then all of A 1 , . . . , A 4 are of type (b), so D 0 is the set of evens, D 4 is the set of odds, and all the other D i 's are empty. Therefore as required. Suppose instead that t = 1. Then D 0 ∪ D 1 contains every even integer, and therefore The family {(C1), (C2), (C3), (C4), (C7)} is (ε, 4)-sufficient. This completes the proof of Case 2. Therefore, the only remaining case is the following.
We will prove that the following constraints hold.
By Observation 6.6(ii), we have We work through some cases depending on the values of (s, t), in increasing order of complexity. Case 1. 5 − s 1 or 5 − t 1.
Note that Further, using Lemma 6.5 and Observation 6.6(i), we have proving (D7). To prove (D8),  We will assume that A 1 and A 2 are type (b) sets (so A 1 = A 2 = O), and A 3 , A 4 , A 5 are type (c) sets. Let q i := d(A i ∩ J 3 ), for every i ∈ {3, 4, 5}. First, we will prove that the following constraints hold.
where we used Lemma 6.5 for the final inequality. For (D17), we have The final inequality follows from (6.6), (6.11) and Observation 6.6(i). Finally we will prove (D18). Observe that E \ (A 4 ∪ A 5 ) ⊆ D 0 and O \(A 4 ∪ A 5 ) ⊆ D 3 . By Observation 6.6(i), we have that Combining these facts, we see that We will assume that A 1 , A 2 are of type (b); A 3 , A 4 are of type (c); and A 5 is of type (a). Our immediate aim is to prove that the following inequalities hold.

Concluding remarks
We determined f (n, r) exactly when r = 2 (Theorem 1.2). It would be interesting to proceed from our stability result (Theorem 1.3) and obtain an exact result for r = 3, and characterise the extremal sets. It seems possible to extract a statement about stability from the proof of Theorem 1.4 by more careful analysis of the linear programs. That is, the following may be obtainable. For all ε > 0, as long as n is a sufficiently large integer: if r = 4 and A ⊆ [n] is extremal, then one of A O, A I 2 and A (O ∪I 2 ) has size at most εn; and if r = 5 and A ⊆ [n] is extremal, then |A (O ∪I 2 )| εn.
It is also possible that the method used to prove Theorem 1.4 (namely finding sufficient linear constraints) can prove the analogous result for r = 6. The main obstacle is the fact that, among extremal A 1 , . . . , A 6 , one cannot a priori guarantee less than two type (a) sets. This leads to 18 different values of (s, t) to consider. Since the proof for r = 5 was already very involved, we did not pursue this further.
Finally, for large r, the value of f (n, r) and the structure of the extremal sets is completely open. The following families (depending on ε ) are (ε, 5)-sufficient. Proof. For 1), 3) and 4), taking ε = 1/100 in Mathematica yields i∈ [4] d i log i < 1 − 1/1000, so we are done in these cases. Given a linear maximisation (primal ) program: Maximise c d subject to Ad b and d 0, the dual minimisation program is: Minimise b y subject to A y c and y 0.
We are again done by the weak duality theorem.
Family 10) The family yields the following primal and dual linear programs.  So the family in 11) is (ε, 5)-sufficient.

Family 12)
The family yields the following primal and dual linear programs. So the family in 12) is (ε, 5)-sufficient. This completes the proof.