Almost all optimally coloured complete graphs contain a rainbow Hamilton path

A subgraph $H$ of an edge-coloured graph is called rainbow if all of the edges of $H$ have different colours. In 1989, Andersen conjectured that every proper edge-colouring of $K_{n}$ admits a rainbow path of length $n-2$. We show that almost all optimal edge-colourings of $K_{n}$ admit both (i) a rainbow Hamilton path and (ii) a rainbow cycle using all of the colours. This result demonstrates that Andersen's Conjecture holds for almost all optimal edge-colourings of $K_{n}$ and answers a recent question of Ferber, Jain, and Sudakov. Our result also has applications to the existence of transversals in random symmetric Latin squares.

1. Introduction 1.1. Extremal results on rainbow colourings. We say that a subgraph H of an edgecoloured graph is rainbow if all of the edges of H have different colours. An optimal edgecolouring of a graph is a proper edge-colouring using the minimum possible number of colours. In this paper we study the problem of finding a rainbow Hamilton path in large optimally edge-coloured complete graphs.
The study of finding rainbow structures within edge-coloured graphs has a rich history. For example, the problem posed by Euler on finding orthogonal n × n Latin squares can easily be seen to be equivalent to that of finding an optimal edge-colouring of the complete bipartite graph K n,n which decomposes into edge-disjoint rainbow perfect matchings. It transpires that there are optimal colourings of K n,n without even a single rainbow perfect matching, if n is even. However, an important conjecture, often referred to as the Ryser-Brualdi-Stein Conjecture, posits that one can always find an almost-perfect rainbow matching, as follows. Conjecture 1.1 (Ryser [36], Brualdi-Stein [9,37]). Every optimal edge-colouring of K n,n admits a rainbow matching of size n − 1 and, if n is odd, a rainbow perfect matching.
Currently, the strongest result towards this conjecture for arbitrary optimal edge-colourings is due to Keevash, Pokrovskiy, Sudakov, and Yepremyan [24], who showed that there is always a rainbow matching of size n − O(log n/ log log n). This result improved earlier bounds of Woolbright [38], Brouwer, de Vries, and Wieringa [7], and Hatami and Shor [21].
It is natural to search for spanning rainbow structures in the non-partite setting as well; that is, what spanning rainbow substructures can be found in properly edge-coloured complete graphs K n ? It is clear that one can always find a rainbow spanning tree -indeed, simply take the star rooted at any vertex. Kaneko, Kano, and Suzuki [22] conjectured that for n > 4, in any proper edge-colouring of K n , one can find n/2 edge-disjoint rainbow spanning trees, thus decomposing K n if n is even, and almost decomposing K n if n is odd. This conjecture was recently proved approximately by Montgomery, Pokrovskiy, and Sudakov [32], who showed that in any properly edge-coloured K n , one can find (1 − o(1))n/2 edge-disjoint rainbow spanning trees.
For optimal edge-colourings, even more is known. Note firstly that if n is even and K n is optimally edge-coloured, then the colour classes form a 1-factorization of K n ; that is, a decomposition of K n into perfect matchings. Throughout the paper, we will use the term 1factorization synonymously with an edge-colouring whose colour classes form a 1-factorization.
Date: 1st July 2020. This project has received partial funding from the European Research Council (ERC) under the European Union's Horizon 2020 research and innovation programme (grant agreement no. 786198, D. Kühn and D. Osthus). The research leading to these results was also partially supported by the EPSRC, grant nos. EP/N019504/1 (T. Kelly and D. Kühn) and EP/S00100X/1 (D. Osthus).

Random colourings.
It is natural to consider these problems in a probabilistic setting, that is to consider random edge-colourings as well as random Latin squares. However, the "rigidity" of the underlying structure makes these probability spaces very challenging to analyze. Recently significant progress was made by Kwan [26], who showed that almost all Latin squares contain a transversal, or equivalently, that almost all optimal edge-colourings of K n,n admit a rainbow perfect matching. His analysis was carried out in a hypergraph setting, which also yields the result that almost all Steiner triple systems contain a perfect matching. Recently, this latter result was strengthened by Ferber and Kwan [14], who showed that almost all Steiner triple systems have an approximate decomposition into edge-disjoint perfect matchings. Here we show that Hahn's original conjecture (and thus Andersen's Conjecture as well) holds for almost all 1-factorizations, answering a recent question of Ferber, Jain, and Sudakov [13]. In what follows, we say a property holds 'with high probability' if it holds with a probability that tends to 1 as the number of vertices n tends to infinity. Theorem 1.3. Let φ be a uniformly random optimal edge-colouring of K n . Then with high probability, (i) φ admits a rainbow Hamilton path, and (ii) φ admits a rainbow cycle F containing all of the colours. In particular, if n is odd, then F is a rainbow Hamilton cycle.
As discussed in Section 7, there is a well-known correspondence between rainbow 2-factors in n-edge-colourings of K n and transversals in symmetric Latin squares, as a transversal in a Latin square corresponds to a permutation σ of [n] such that the entries in positions (i, σ(i)) are distinct for all i ∈ [n]. Based on this, we use Theorem 1.3(ii) to show that random symmetric Latin squares of odd order contain a Hamilton transversal with high probability. Here we say a transversal is Hamilton if the underlying permutation σ is an n-cycle. Corollary 1.4. Let n be an odd integer and L a uniformly random symmetric n × n Latin square. Then with high probability L contains a Hamilton transversal.
Further results on random Latin squares were recently obtained by Kwan and Sudakov [27], who gave estimates on the number of intercalates in a random Latin square as well as their likely discrepancy.

Notation
In this section, we collect some definitions and notation that we will use throughout the paper. , where G is a |D|-regular graph on a vertex set V of size n, and φ G is a 1-factorization of G with colour set D. Often, we abuse notation and write G ∈ G col D , and in this case we let φ G denote the implicit 1factorization of G, sometimes simply writing φ when G is clear from the context. A subgraph H ⊆ G ∈ G col D inherits the colours of its edges from G. Observe that uniformly randomly choosing a 1-factorization φ of K n on vertex set V and colour set [n − 1] is equivalent to uniformly randomly choosing G ∈ G col [n −1] . For any D ⊆ [n − 1], G ∈ G col D , and sets V ⊆ V , D ⊆ D, we define E V ,D (G) := {e = xy ∈ E(G) : φ G (e) ∈ D , x, y ∈ V }, and we define e V ,D (G) := |E V ,D (G)|. For a hypergraph H, we write ∆ c (H) to denote the maximum codegree of H; that is, the maximum number of edges containing any two fixed vertices of H.
For a set D of size n and a partition P of D into m parts, we say that P is equitable to mean that all parts P of P satisfy |P | ∈ { n/m , n/m }, and when it does not affect the argument, we will assume all parts of an equitable partition have size precisely n/m. For a set S and a real number p ∈ [0, 1], a p-random subset T ⊆ S is a random subset in which each element of S is included in T independently with probability p. A β-random subgraph of a graph G is a spanning subgraph of G where the edge-set is a β-random subset of E(G). For an event E in any probability space, we write E to denote the complement of E. For real numbers a, b, c such that b > 0, we write a = (1 ± b)c to mean that the inequality (1 − b)c ≤ a ≤ (1 + b)c holds. For a natural number n ∈ N, we define [n] := {1, 2, . . . , n}, and [n] 0 := [n] ∪ {0}. We write x y to mean that for any y ∈ (0, 1] there exists an x 0 ∈ (0, 1) such that for all 0 < x ≤ x 0 the subsequent statement holds. Hierarchies with more constants are defined similarly and should be read from the right to the left. Constants in hierarchies will always be real numbers in (0, 1]. We assume large numbers to be integers if this does not affect the argument.

Proving Theorem 1.3
In this section, let φ be a 1-factorization of K n with vertex set V and colour set C = [n − 1]. We provide an overview of the proof of Theorem 1.3, and in fact, we prove Theorem 1.3 in the case when n is even in Section 3.2 assuming two key lemmas which we prove in later sections. In particular, we assume that n is even in Sections 3-6, so that the optimal edge-colouring φ we work with is a 1-factorization of K n . In Section 7 we derive Theorem 1.3 in the case when n is odd from the case when n is even. We will also deduce Corollary 1.4 from Theorem 1.3(ii) in Section 7. Throughout the proof we work with constants ε, γ, η, and µ satisfying the following hierarchy: Our proof uses the absorption method as well as switching techniques. Note that the latter is a significant difference to [14,26], which rely on the analysis of the random greedy triangle removal process, as well as modifications of arguments in [23,28] which bound the number of Steiner triple systems. Our main objective is to show that with high probability, in a random 1-factorization, we can find an absorbing structure inside a random subset of Θ(µn) reserved vertices, using a random subset of Θ(µn) reserved colours. A recent result [16,Lemma 16], based on hypergraph matchings, enables us to find a long rainbow path avoiding these reserved vertices and colours, and using our absorbing structure, we extend this path to a rainbow Hamilton path. More precisely, we randomly 'reserve' Θ(µn) vertices and colours and show that with high probability we can find an absorbing structure. This absorbing structure consists of a subgraph G abs containing only reserved vertices and colours and all but at most γn of them. Moreover G abs contains 'flexible' sets of vertices and colours V flex and C flex each of size ηn, with the following crucial property: ( †) for any pair of equal-sized subsets X ⊆ V flex and Y ⊆ C flex of size at most ηn/2, the graph G abs − X contains a spanning rainbow path whose colours avoid Y . Given this absorbing structure, we find a rainbow Hamilton path in the following three steps: (1) Long path step: Apply [16, Lemma 16] to obtain a long rainbow path P 1 containing only non-reserved vertices and colours and all but at most γn of them. (2) Covering step: 'Cover' the vertices and colours not in G abs or P 1 using the flexible sets, by greedily constructing a path P 2 containing them as well as sets X ⊆ V flex and Y ⊆ C flex of size at most ηn/2. (3) Absorbing step: 'Absorb' the remaining vertices and colours, by letting P 3 be the rainbow path guaranteed by ( †). In the covering step, we can ensure that P 2 shares one end with P 1 and one end with P 3 so that P 1 ∪ P 2 ∪ P 3 is a rainbow Hamilton path, as desired. These steps are fairly straightforward, so the majority of the paper is devoted to building the absorbing structure.
In Section 3.1, we present the details of this absorbing structure, and in Section 3.2, we prove Theorem 1.3 (in the case when n is even) subject to its existence.
3.1. Absorption. Our absorbing structure consists of many 'gadgets' pieced together in a particular way. First, we introduce these gadgets in the following definition (see also Figure 1). Definition 3.1. For every v ∈ V and c ∈ C, a (v, c)-absorbing gadget is a subgraph of K n of the form A = T ∪ Q such that the following holds: • T ∼ = K 3 and Q ∼ = C 4 , • T and Q are vertex-disjoint, • v ∈ V (T ) and there is a unique edge e ∈ E(Q) such that φ(e) = c, • if e 1 , e 2 ∈ E(T ) are the edges incident to v, then there is matching {e 1 , e 2 } in Q not containing e such that φ(e i ) = φ(e i ) for i ∈ {1, 2}, • if e 3 ∈ E(T ) is the edge not incident to v, then there is an edge e 3 ∈ E(Q) such that {e 3 , e} is a matching in Q and φ(e 3 ) = φ(e 3 ) = c. In this case, a pair P 1 , P 2 of paths completes the (v, c)-absorbing gadget A = T ∪ Q if • the ends of P 1 are non-adjacent vertices in Q, • one end of P 2 is in Q but not incident to e and the other end of P 2 is in V (T ) \ {v}, • P 1 and P 2 are vertex-disjoint and both P 1 and P 2 are internally vertex-disjoint from A, • P 1 ∪ P 2 is rainbow, and • φ(P 1 ∪ P 2 ) ∩ φ(A) = ∅, and we say A := A ∪ P 1 ∪ P 2 is a (v, c)-absorber. We also define the following.
• The path P with edge-set E(P 1 ) ∪ E(P 2 ) ∪ {e 1 , e 2 , e 3 } is the (v, c)-avoiding path in A , and the path P with edge-set E( Suppose A is a (v, c)-absorber that is part of the absorbing structure G abs . Importantly, the (v, c)-absorbing path is rainbow, spanning in A, and contains all the colours in φ(A), and the (v, c)-avoiding path is rainbow, spanning in A − v, and contains all the colours in φ(A) \ {c}. Thus, when proving ( †), if v or c is in X or Y , then the spanning rainbow path in G abs − X contains the (v, c)-avoiding path, and otherwise it may contain the (v, c)-absorbing path.
We build our absorbing structure out of (v, c)-absorbers, using a bipartite graph H as a template, where one part of H is a subset of V and the other part is a subset of C. For every edge vc ∈ E(H), we will have a (v, c)-absorber in the absorbing structure. It is convenient for us to distinguish between a (v, c)-absorbing gadget and a (v, c)-absorber, because when we build our absorbing structure, we first find a (v, c)-absorbing gadget for every vc ∈ E(H) and then find the paths completing each absorbing gadget, as in the following definition.
• If P is a collection of vertex-disjoint paths of length 4, then we say P completes A if the following holds: -P ∈P P is rainbow, no colour that is either in C or is used by a (v, c)-absorbing gadget A v,c ∈ A appears in a path P ∈ P, no vertex that is either in V or is used by a (v, c)-absorbing gadget A v,c ∈ A is an internal vertex of a path P ∈ P, for every (v, c)-absorbing gadget A v,c ∈ A there is a pair of paths P 1 , P 2 ∈ P such that P 1 and P 2 complete A v,c to a (v, c)-absorber A v,c , and the graph A∈A A ∪ P ∈P P \ V is connected and has maximum degree three, and P is minimal subject to this property. for every vc ∈ E(H), if vc ∈ E(M ), then P contains the (v, c)-absorbing path in the (v, c)-absorber A v,c and P contains the (v, c)-avoiding path otherwise (that is, Note that if P completes A, then some of the paths in P will complete absorbing gadgets in A to absorbers, while the remaining set of paths P ⊆ P will be used to connect all the absorbing gadgets in A. More precisely, there is an enumeration A 1 , . . . , A |A| of A and an enumeration P 1 , . . . , P |A|−1 of P such that each P i joins A i to A i+1 . In particular, for each i ∈ [|A|] \ {1, |A|}, each vertex in A i \ V is the endpoint of precisely one path in P (and thus has degree three in A∈A A ∪ P ∈P P ), while both A 1 \ V and A |A| \ V contain precisely one vertex which is not the endpoint of some path in P (and thus these two vertices have degree two in A∈A A ∪ P ∈P P ). Any tail T of an H-absorber (A, P) has to start at one of these two vertices. Altogether this means that, given any matching M of H, the path absorbing M in (A, P, T ) in the definition above actually exists.
Our absorbing structure is essentially an H-absorber (A, P) with a tail T and flexible sets V flex , C flex ⊆ V (H) for an appropriately chosen template H. If H − (X ∪ Y ) has a perfect matching M , then the path absorbing M in (A, P, T ) satisfies ( †). This fact motivates the property of H that we need in the next definition.
First, we briefly discuss the purpose of the tail T . Recall that we reserve Θ(µn) vertices and colours for our absorbing structure. Our H-absorber, however, contains Θ(ηn) vertices and colours, yet we need our absorbing structure to contain all but at most γn of the reserved vertices and colours. We need η µ in order to build the H-absorber greedily, i.e. by selecting (v, c)-absorbing gadgets arbitrarily in turn for each edge of H, so the purpose of the tail then is to extend the H-absorber to a structure containing almost all of the reserved vertices and colours. This concept was first introduced by Montgomery [31]. If H is robustly matchable with respect to V flex and C flex , then an H-absorber (A, P) with tail T satisfies ( †). The last property of our absorbing structure that we need is that the flexible sets allow us to execute the covering step, which we capture in the following definition.
Definition 3.4. Let V flex ⊆ V , let C flex ⊆ C, and let G flex be a spanning subgraph of K n .
• If u, v ∈ V and c ∈ C, and P ⊆ G flex is a rainbow path of length four such that u and v are the ends of P , u , w, v ∈ V flex , where uu , u w, wv , vv ∈ E(P ), φ(uu ), φ(wv ), φ(vv ) ∈ C flex , and φ(u w) = c, then P is a (V flex , C flex , G flex )-cover of u, v, and c.
• If P is a rainbow path such that P = k i=1 P i where P i is a (V flex , C flex , G flex )-cover of v i , v i+1 , and c i , then P is a (V flex , C flex , G flex )-cover of {v 1 , . . . , v k+1 } and {c 1 , . . . , c k }.
• If H is a regular bipartite graph with bipartition (V , C ) where V ⊆ V and C ⊆ C such that -H is robustly matchable with respect to V flex and C flex where |V flex |, |C flex | ≥ δn, and for every u, v ∈ V and c ∈ C, there are at least δn 2 (V flex , C flex , G flex )-covers of u, v, and c, then H is a δ-absorbing template with flexible sets (V flex , C flex , G flex ).
• If (A, P) is an H-absorber where H is a δ-absorbing template and T is a tail for (A, P), then (A, P, T, H) is a δ-absorber.
A 36γ-absorber has the properties we need to execute both the covering step and the absorbing step, which we make formal with the next proposition.
First, we introduce the following convenient convention.
, and • G is the complement of A∈A A ∪ P ∈P P ∪ T , then there is both a rainbow Hamilton path containing P and a rainbow cycle containing P and all of the colours in C.
Proof. Order the colours in C \ (φ(P ) ∪ C ) as c 1 , . . . , c k , and note that k ≤ δn/18. Order the vertices in V \ (P ∪ V ) as v 1 , . . . , v . Using that H is regular, it is easy to see that |V | = |C | + 1, and thus = k − 1. Let v 0 and v 0 be the ends of P , let u be the end of T not in V (A) for any A ∈ A, and let u be the unique vertex in A∈A V (A) \ V (T ) of degree two in A∈A A ∪ P ∈P P . Let (V flex , C flex , G flex ) be the flexible sets of H. First we show that there is a rainbow Hamilton path containing P . We claim there is a (V flex , C flex , G flex )-cover P of {v 0 , . . . , v k } and {c 1 , . . . , c k }, where v k := u. Suppose for j ∈ [k − 1] and i < j that P i is a (V flex , C flex , G flex )-cover of v i , v i+1 , and c i+1 such that i<j P i is a rainbow path. We show that there exists a (V flex , C flex , G flex )-cover P j of v j , v j+1 , and c j+1 that is internally-vertex-and colour-disjoint from i<j P i , which implies that i≤j P i is a rainbow path, and thus we can choose the path P greedily, proving the claim. Since each vertex in V flex and each colour in C flex is contained in at most 3n (V flex , C flex , G flex )-covers of v j , v j+1 , and c j+1 , and since H is a δ-absorbing template, there are at least δn 2 − 18n · j (V flex , C flex , G flex )-covers of v j , v j+1 , and c j+1 not containing a vertex or colour from i<j P i . Thus, since j < k ≤ δn/18, there exists a (V flex , C flex , G flex )-cover P j of v j , v j+1 , and c j+1 such that i≤j P i is a rainbow path, as desired, and consequently we can choose the path P greedily, as claimed. Now let X := V (P ) ∩ V flex , and let Y := φ(P ) ∩ C flex . Since |X| = |Y | = 3k ≤ |V flex |/2 and H is robustly matchable with respect to V flex and C flex , there is a perfect matching M in H − (X ∪ Y ). Let P be the path absorbing M in (A, P, T ). Then P ∪ P ∪ P is a rainbow Hamilton path, as desired. Now we show that there is a rainbow cycle containing P and all of the colours in C. By the same argument as before, there is a (V flex , C flex , G flex )-cover P 1 of {v 0 , . . . , v −1 , u} and {c 1 , . . . , c k−1 } as well as a (V flex , C flex , G flex )-cover P 2 of v 0 , u , and c k such that P 1 and P 2 are vertex-and colour-disjoint. Letting X := V (P 1 ∪ P 2 ) ∩ V flex and Y := φ(P 1 ∪ P 2 ) ∩ C flex , letting M be a perfect matching in H − (X ∪ Y ) and P be the path absorbing M in (A, P, T ) as before, P ∪ P 1 ∪ P 2 ∪ P is a rainbow cycle using all the colours in C, as desired.
3.2. The proof of Theorem 1.3 when n is even. In this subsection, we prove the n even case of Theorem 1.3 subject to two lemmas, Lemmas 3.8 and 3.9, which we prove in Sections 6 and 5, respectively. The first of these lemmas, Lemma 3.8, states that almost all 1-factorizations have two key properties, introduced in the next two definitions. Lemma 3.9 states that if a 1factorization has both of these properties, then we can build an absorber using the reserved vertices and colours with high probability.
Recall the hierarchy of constants ε, γ, η, µ from (3.1). Firstly, we will need to show that if G ∈ G col [n−1] is chosen uniformly at random, then with high probability, for any V ⊆ V , C ⊆ C that are not too small, G admits many edges with colour in C and both endpoints in V . This property will be used in the construction of the tail of our absorber.
is µ-robustly gadget-resilient if for all x ∈ V and all c ∈ C, there is a 5µn/4-well-spread collection of at least µ 4 n 2 /2 23 (x, c)-absorbing gadgets in G.
We prove Lemma 3.8 using a 'coloured version' of switching arguments that are commonly used to study random regular graphs. Unfortunately, 1-factorizations of the complete graph K n are 'rigid' structures, in the sense that it is difficult to make local changes without global ramifications on such a 1-factorization. Thus, instead of analysing switchings between graphs in G col [n−1] , we will analyse switchings between graphs in G col D for appropriately chosen D [n−1]. In the setting of random Latin squares, this approach was used by McKay and Wanless [30] and further developed by Kwan and Sudakov [27], and we build on their ideas. Finally, we use results on the number of 1-factorizations of dense regular graphs due to Kahn and Lovász (see Theorem 6.10) and Ferber, Jain, and Sudakov (see Theorem 6.11) to study the number of completions of a graph H ∈ G col D to a graph G ∈ G col [n−1] , and we use this information to compare the probability space corresponding to a uniform random choice of H ∈ G col D , with the probability space corresponding to a uniform random choice of G ∈ G col [n−1] . Lemma 3.9. Suppose 1/n ε γ η µ 1, and let p = q = β = 5µ + 26887η/2 + γ/3 − 26880ε. If φ is an ε-locally edge-resilient and µ-robustly gadget-resilient 1-factorization of K n with vertex set V and colour set C and (R1) V is a p-random subset of V , (R2) C is a q-random subset of C, and (R3) G is a β-random subgraph of K n , then with high probability there is a 36γ-absorber (A, P, T, H) such that A∈A A ∪ P ∈P P ∪ T is contained in (V , C , G ) with γ-bounded remainder.
The final ingredient in the proof of Theorem 1.3 is the following lemma which follows from [16,Lemma 16], that enables us to find the long rainbow path whose leftover we absorb using the absorber from Lemma 3.9.
Lemma 3.10. Suppose 1/n γ p, and let q = β = p. If φ is a 1-factorization of K n with vertex set V and colour set C and • V is a p-random subset of V , • C is a q-random subset of C, and • G is a β-random subgraph of K n , then with high probability there is a rainbow path contained in (V , C , G) with γ-bounded remainder.
We conclude this section with a proof of Theorem 1.3 in the case that n is even, assuming Lemmas 3.8 and 3.9.
Proof of Theorem 1.3, n even case. By Lemma 3.8, it suffices to prove that if φ is an ε-locally edge-resilient and µ-robustly gadget-resilient 1-factorization, then there is a rainbow Hamilton path and a rainbow cycle containing all of the colours. Let -random, and let G 1 and G 2 be β-random and (1 − β)-random subgraphs of K n such that E(G 1 ) and E(G 2 ) partition the edges of K n . By Lemma 3.9 applied with V = V 1 , C = C 1 , and G = G 1 , and by Lemma 3.10 applied with V = V 2 , C = C 2 , and G = G 2 , the following holds with high probability. There exists with γ-bounded remainder, and (ii) a rainbow path P contained in (V 2 , C 2 , G 2 ) with γ-bounded remainder. Now we fix an outcome of the random partitions (V 1 , V 2 ), (C 1 , C 2 ), and (G 1 , G 2 ) so that (i) and (ii) hold. By Proposition 3.5, there is both a rainbow Hamilton path containing P and a rainbow cycle containing P and all of the colours in C, as desired.

Tools
In this section, we collect some results that we will use throughout the paper. 4.1. Probabilistic tools. We will use the following standard probabilistic estimates. Lemma 4.1 (Chernoff Bound). Let X have binomial distribution with parameters n, p. Then for any 0 < t ≤ np, .
Let X 1 , . . . , X m be independent random variables taking values in X , and let f :

4.2.
Hypergraph matchings. When we build our absorber in the proof of Lemma 3.9, we seek to efficiently use the vertices, colours, and edges of our random subsets V ⊆ V , C ⊆ C, E ⊆ E, and to do this we make use of the existence of large matchings in almost-regular hypergraphs with small codegree. In fact, we will need the stronger property that there exists a large matching in such a hypergraph which is well-distributed with respect to a specified collection of vertex subsets. We make this precise in the following definition. Given a hypergraph H and a collection of subsets F of V (H), we say a matching M in H is (γ, F)-perfect if for each F ∈ F, at most γ · max{|F |, |V (H)| 2/5 } vertices of F are left uncovered by M. The following theorem is a consequence of Theorem 1.2 in [4], and is based on a result of Pippenger and Spencer [33].
If F is a collection of subsets of V (H) such that |F| ≤ n log n , then there exists a (γ, F)-perfect matching.
We will use Theorem 4.3 in the final step of constructing an absorber (see Lemma 5.5). We construct an auxiliary hypergraph H whose edges represent structures we wish to find, and a large well-distributed matching in H corresponds to an efficient allocation of vertices, colours, and edges of the 1-factorization to construct almost all of these desired structures. We remark that this is also a key strategy in the proof of Lemma 3.10, and was first used in [25].

4.3.
Robustly matchable bipartite graphs of constant degree. In this subsection, we prove that there exist large bipartite graphs which are robustly matchable as in Definition 3.3, and have constant maximum degree.
In this case, we say H is robustly matchable with respect to B 1 , and that B 1 is the identified flexible set.
By [31,Lemma 10.7], for all sufficiently large m there exists an RM BG(3m, 2m, 2m) with maximum degree at most 100. We use a one-sided (there is one flexible set) RM BG(3m, 2m, 2m) exhibited in [16,Corollary 10] in which each of the vertex classes are regular, to construct a 256-regular two-sided (in that we identify a flexible set on each side of the vertex bipartition) 2RM BG(7m, 2m). Lemma 4.5. For all sufficiently large m, there is a 2RM BG(7m, 2m) that is 256-regular.

Proof.
Suppose that m ∈ N is sufficiently large. By [16,Corollary 10], there exists an RM BG(3m, 2m, 2m) that is (256, 192)-regular (i.e. all vertices in the first vertex class have degree 256 and all vertices in the second vertex class of have degree 192). Let H and H be two vertex-disjoint isomorphic copies of a (192, 256)-regular RM BG(3m, 2m, 2m), and let (A, B 1 ∪ B 2 ) and (A , B 1 ∪ B 2 ) be the bipartitions of H and H respectively such that H is robustly matchable with respect to B 1 and H is robustly matchable with respect to B 1 .
Let H be a 64-regular bipartite graph with bipartition ( Throughout this section, let φ be an ε-locally edge-resilient and µ-robustly gadget resilient 1-factorization of K n with vertex set V and colour set C, let E := E(K n ), and recall LetH be a 256-regular 2RM BG(7m, 2m) where 2m = (η −2ε)n, which exists by Lemma 4.5. We define the following probabilities: 1. An absorber partition of V , C, and K n is defined as follows: where V main is p main -random, V flex is p flex -random etc, and the sets of colours are defined analogously. Let V := V \ V main , C := C \ C main , and let G be a β-random subgraph of K n .
Note that V , C , and G satisfy (R1)-(R3) in the statement of Lemma 3.9.

5.1.
Overview of the proof. We now overview our strategy for proving Lemma 3.9. First we need the following definitions. A link is a rainbow path of length 4 with internal vertices in V link ∪ V link , ends in V abs , and colours and edges in C link ∪ C link and G , respectively. A link with internal vertices in V link and colours in C link is a main link, and a link with internal vertices in V link and colours in C link is a reserve link. If M is a matching and P = {P e } e∈E(M ) is a collection of vertex-disjoint links such that P ∈P P is rainbow and P uv has ends u and v for every uv ∈ E(M ), then P links M .
We aim to build a 36γ-absorber (A, P, T, H) such that A∈A A ∪ P ∈P P ∪ T is contained in (V , C , G ) with γ-bounded remainder and H ∼ =H. First, we show (see Lemma 5.3) that with high probability there is a 36γ-absorbing template 6ε-bounded remainder. Then, we show that with high probability, there exists an H-absorber (A, P) where • for every vc ∈ E(H), the (v, c)-absorbing gadget A v,c ∈ A uses vertices, colours, and edges in V abs , C abs , and G , respectively, and • every P ∈ P is a link.
. Finally, letting V abs and C abs be the vertices and colours in V abs and C abs not used by any (v, c)-absorbing gadget in A, we show that with high probability there is a tail T for  We find these structures in the following steps. For Steps 1 and 2, see Lemma 5.4, and for Steps 3 and 4, see Lemma 5.5.
1) First, we find the collection A of absorbing gadgets greedily, using the robust gadgetresilience property of φ, 2) then we greedily construct the matching M , using the local edge-resilience property of φ. 3) Next, we construct an auxiliary hypergraph in which each hyperedge corresponds to a main link and apply Theorem 4.3 to choose most of the links in P, and 4) finally we greedily choose the remainder of the links in P from the reserve links.
Indeed, (a) and (b) follow from the Chernoff Bound (Lemma 4.1). To prove (c), for each u, v, and c, we apply McDiarmid's Inequality (Theorem 4.2). Consider the random variable f counting the number of (V flex , C flex , G )-covers of u, v, and c. Note that f is determined by the following independent binomial random variables: where X c indicates if c ∈ C flex , and for each edge e, the random variable X e which indicates if e ∈ E(G ). We claim there are at least 2(n/2 − 2)(n − 7) (V, C, K n )-covers of u, v, and c. To that end, let u w be a c-edge with u , w ∈ V \ {u, v}. There are at least n − 7 vertices v ∈ V \ {u, v, u , w} such that φ(vv ), φ(wv ) / ∈ {φ(uu ), c}, and for each such vertex v the path uu wv v is a (V, C, K n )-cover of u, v, and c. Similarly, there are at least n − 7 (V, C, K n )-covers of the form uwu v v. Altogether this gives at least 2(n/2 − 2)(n − 7) ≥ n 2 /2 (V, C, K n )-covers of u, v, and c, as claimed. Therefore E [f ] ≥ p 3 q 3 β 4 n 2 /2. For each z ∈ V , X z affects f by at most 3n, and X uz , and X vz each affect f by at most n, and for each c ∈ C, X c affects f by at most 3n. Since m = (η/2 − ε)n, by (a) and (b), there exists V flex ⊆ V flex , C flex ⊆ C flex , V buff ⊆ V buff , and C buff ⊆ C buff , such that |V flex |, |C flex | = 2m and |V buff |, |C buff | = 5m, which we choose arbitrarily, and moreover, |V flex \V flex |, |C flex \C flex | ≤ 3εn and |V buff \V buff |, |C buff \C buff | ≤ 6εn, as required. Choose bijections from V flex , C flex , V buff , and C buff to the flexible sets and the buffer sets ofH arbitrarily, and let H ∼ =H be the corresponding graph. Now H satisfies (5.3.1) and (5.3.2), as required, so it remains to show that H is a 36γ-absorbing template. Since each vertex or colour in V flex or C flex is in at most 3n (V flex , C flex , G )-covers of u, v, and c, (a) and (c) imply that there are at least p 3 q 3 β 4 n 2 /4 − 18εn 2 ≥ 36γn 2 (V flex , C flex , G )-covers of u, v, and c, so H is a 36γ-absorbing template, as desired.

Greedily building an H-absorber.
Lemma 5.4. Consider an absorber partition of V , C, and K n . The following holds with high probability. Suppose V res ⊆ V flex ∪ V buff and C res ⊆ C flex ∪ C buff . For every graph H ∼ =H with bipartition (V res , C res ), there exists (5.4.1) a collection A = {A vc : vc ∈ E(H)} such that A satisfies H and such that for all A vc ∈ A we have that A vc uses vertices, colours, and edges in V abs , C abs , and G respectively, and (5.4.2) a rainbow matching M contained in (V abs , C abs , G ) with 5ε-bounded remainder, where V abs and C abs are the sets of vertices and colours in V abs and C abs not used by any absorbing gadget in A.
Proof. For convenience, let p := p abs and q := q abs in this proof.
Since φ is µ-robustly gadget-resilient, for every v ∈ V , c ∈ C, there is a collection A v,c of precisely 2 −23 µ 4 n 2 (v, c)-absorbing gadgets such that every vertex, every colour, and every edge is used by at most 5µn/4 of the A ∈ A v,c . (Recall from Definition 3.1 that a (v, c)absorbing gadget does not use v and c.) Fix v ∈ V , c ∈ C. The expected number of the (v, c)-absorbing gadgets in A v,c using only vertices in V abs , colours in C abs , and edges in G is p 6 q 3 β 7 |A v,c |. Let E v,c be the event that fewer than p 6 q 3 β 7 |A v,c |/2 of the (v, c)-absorbing gadgets in A v,c use only vertices in V abs , colours in C abs and edges in G . We claim that P [E v,c ] ≤ exp(−2 −51 p 12 q 6 β 14 µ 6 n).
To see this, for each u ∈ V , d ∈ C, e ∈ E, let m u , m d , and m e denote the number of (v, c)-absorbing gadgets in A v,c using u, d, and e, respectively. We will apply McDiarmid's Inequality (Theorem 4.2) to the function f v,c which counts the number of A ∈ A v,c using only vertices in V abs , colours in C abs , and edges in G . We use independent indicator random variables {X u } u∈V ∪ {X d } d∈C ∪ {X e } e∈E which indicate whether or not a vertex u is in V abs , a colour d is in C abs , and an edge e is in G . Each random variable X u , X d , X e affects f v,c by at most m u , m d , m e , respectively. Since m u ≤ 5µn/4 for all u ∈ V and m d ≤ 5µn/4 for all d ∈ C, we have u∈V m 2 u , d∈C m 2 d ≤ 25µ 2 n 3 /16. Since e∈E m e = 7|A v,c | and m e ≤ 5µn/4 for all e ∈ E, it follows that e∈E m 2 e ≤ 35µn|A v,c |/4. Therefore, by McDiarmid's Inequality, we have as claimed. Thus, by a union bound, the probability that there exist v ∈ V , c ∈ C such that E v,c holds is at most exp(−2 −52 p 12 q 6 β 14 µ 6 n).
We claim the following holds with high probability: (a) |V abs | = (p ± ε)n and |C abs | = (q ± ε)n; there is a collection A abs v i ,c i of at least 2 −24 p 6 q 3 β 7 µ 4 n 2 (v i , c i )-absorbing gadgets each using only V abs -vertices, C abs -colours, and G -edges, and moreover, each vertex in V abs , colour in C abs , and edge in G is used by at most 5µn/4 of the A ∈ A abs v i ,c i . Thus, at most 20µn · i ≤ 20µn|E(H)| ≤ 17920ηµn 2 of the (v i , c i )-absorbing gadgets in A abs v i ,c i use a vertex, colour, or edge used by any of the A j for j < i. Since |A abs v i ,c i | ≥ 2 −24 p 6 q 3 β 7 µ 4 n 2 , we conclude that there is at least one (v i , c i )-absorbing gadget A ∈ A abs v i ,c i using vertices, colours, and edges which are disjoint from the vertices, colours, and edges used by A j , for all j < i. We arbitrarily choose such an A to be A i . Continuing in this way, it is clear that satisfies H, so (5.4.1) holds. Now we prove (5.4.2). Let V abs and C abs be the vertices, colours, and edges in V abs and C abs not used by any (v, c)-absorbing gadget in A. By (a) and (5.1), we have |V abs | = (2µ ± ε)n and |C abs | = (µ±ε)n. Thus, by (c), we can greedily choose a rainbow matching M in (V abs , C abs , G ) of size at least (µ − 2ε)n, and M satisfies (5.4.2).

5.4.
Linking. Lastly, we need the following lemma, inspired by [16,Lemma 20], which we use to both complete the set of absorbing gadgets obtained by Lemma 5.4 to an H-absorber and also construct its tail. Recall that links were defined at the beginning of Section 5.1.
Proof. We choose a new constant δ such that ε δ γ. For convenience, let p := p link and q := q link , let G 1 be the spanning subgraph of G consisting of edges with a colour in C link , and let G 2 be the spanning subgraph of G consisting of edges with a colour in C link . First we claim that with high probability the following holds: (a) |V link | = (p ± ε)n, |C link | = (q ± ε)n, |V link | = (γ/3 ± ε)n, and |C link | = (γ/3 ± ε)n, (b) |V abs | = (1 ± ε)p abs n = (1 ± ε)2pn/3, (c) for all v ∈ V , we have Indeed (a)-(d) follow from (5.1) and the Chernoff Bound. We prove (e) and (f) using Mc-Diarmid's Inequality. To prove (e), for each u, v ∈ V , we apply McDiarmid's Inequality to the random variable f counting |N G 1 (u) ∩ N G 1 (v) ∩ V link | with respect to independent binomial random variables {X w , X uw , X vw } w∈V and {X c } c∈C , where X w indicates if w ∈ V link , X uw and X vw indicate if the edges uw and vw respectively are in G , and X c indicates if c ∈ C link . For each w ∈ V , X w , X uw , and X vw affect f by at most one, and for each c ∈ C, X c affects f by at most two. Thus, by McDiarmid's Inequality with t = εE [f ] /2, we have |N G 1 (u) ∩ N G 1 (v) ∩ V link | = (1 ± ε)pβ 2 q 2 n with probability at least 1 − exp −(εpβ 2 q 2 n/2) 2 /7n . By a union bound, (e) also holds with high probability. The proof of (f) is similar, so we omit it.
Now we assume (a)-(f) hold, we suppose M is a matching such that V (M ) ⊆ V abs and |V abs \ V (M )| ≤ εn, and we show that (5.5.1) and (5.5.2) hold with respect to M . Since We apply Theorem 4.3 to the following 8-uniform hypergraph H: the vertex-set is E(M ) ∪ V link ∪ C link , and for every xy • P has ends x and y, • v 1 , v 2 , and v 3 are the internal vertices in P , and • φ(P ) = {c 1 , c 2 , c 3 , c 4 }.
Proof of claim: Let xy ∈ E(M ). By (a), there are (1 ± ε)pn vertices v 1 ∈ V link that can be in a link P with ends x and y corresponding to a hyperedge in H, where v 1 is not adjacent to x or y.
For each such v 1 ∈ V link , by (e), there are (1 ± ε)pβ 2 q 2 n choices for the vertex in V link adjacent to both x and v 1 in P , and for each such v 2 ∈ V link , again by (e), there are (1 ± 2ε)pβ 2 q 2 n choices for the vertex in V link adjacent to both v 1 and y in P such that P is a main link. Thus, d H (xy) = (1 ± 5ε)p 3 β 4 q 4 n 3 , as required. Now let v 1 ∈ V link . First, we count the number of hyperedges in H containing v 1 corresponding to a link P where v 1 is adjacent to one of the ends. By (c), and since |V abs \ V (M )| ≤ εn, there are (1 ± √ ε)2pβqn/3 choices of the vertex x ∈ V (M ) adjacent to v 1 in P . For each such x, again by (c), there are (1 ± 2ε)pβqn choices of the vertex v 2 ∈ V link adjacent to y in P where xy ∈ E(M ). For each such v 2 ∈ V link , by (e), there are (1 ± 2ε)pβ 2 q 2 n choices of the vertex v 3 ∈ V link adjacent to both v 1 and v 2 in P . Thus, the number of hyperedges in H containing v 1 corresponding to a link where v 1 is adjacent to one of the ends is (1 ± 2 √ ε)2p 3 β 4 q 4 n 3 /3. Next, we count the number of hyperedges in H containing v 1 corresponding to a link P where v 1 is not adjacent to one of the ends. By (5.3), there are (1 ± √ ε)pn/3 choices for the edge xy ∈ E(M ) where x and y are the ends of P . For each such xy ∈ E(M ), by (e), there are (1±ε)pβ 2 q 2 n choices of the vertex v 2 ∈ V link such that v 2 is adjacent to x and v 1 in P , and again by (e), for each such v 2 ∈ V link , there are (1±2ε)pβ 2 q 2 n choices of the vertex v 3 ∈ V link adjacent to both y and v 1 in P . Thus, the number of hyperedges in H containing v 1 corresponding to a link where v 1 is not adjacent to one of the ends is (1 ± 2 √ ε)p 3 β 4 q 4 n 3 /3, so as required. Now let c 1 ∈ C link . First we count the number of hyperedges in H containing c 1 corresponding to a link P where c 1 is the colour of one of the edges incident to an end of P . By (d), and since |V abs \ V (M )| ≤ εn, there are (1 ± √ ε)2p 2 βn/3 choices of the edge xv 1 in P where x ∈ V (M ) is an end of P and φ(xv 1 ) = c 1 . For each such edge xv 1 , by (c), there are (1 ± 2ε)pβqn choices of the vertex v 2 ∈ V link adjacent to y in P where xy ∈ E(M ). For each such vertex v 2 , by (e), there are (1 ± 2ε)pβ 2 q 2 n choices of the vertex v 3 adjacent to both v 1 and v 2 in P . Thus, the number of hyperedges in H containing c 1 corresponding to a link where c 1 is the colour of one of the edges incident to an end of P is (1 ± 2 √ ε)2p 4 β 4 q 3 n 3 /3. Next, we count the number of hyperedges in H containing c 1 corresponding to a link P where c 1 is the colour of one of the edges with both ends in V link . By (d), there are (1 ± ε)p 2 βn/2 choices for the edge v 1 v 2 in P such that φ(v 1 v 2 ) = c 1 , and thus (1 ± ε)p 2 βn choices for the edge if we assume v 1 is adjacent to an end in P . For each such edge v 1 v 2 , by (c), and since |V abs \ V (M )| ≤ εn, there are (1 ± √ ε)2pβqn/3 choices for the vertex x ∈ V (M ) adjacent to v 1 in P . For each such vertex x, by (e), there are (1 ± 2ε)pβ 2 q 2 n choices for the vertex v 3 adjacent to both y and v 2 in P , where xy ∈ E(M ). Thus, the number of hyperedges in H containing c 1 corresponding to a link where c 1 is the colour of one of the edges with both ends in V link is as required to prove Claim 1. − This can be proved similarly as above (with room to spare). Let Write E(M ) = {x 1 y 1 , . . . , x k y k }, and suppose P i is a reserve link with ends x i and y i for i < j, where j ∈ [k]. We show that that there is a reserve link P j that is vertex-and colour-disjoint from i<j P i , which implies that j i=1 P i links {x 1 y 1 , . . . , x j y j }, and thus we can choose P 2 greedily. Since k ≤ δn and each link has at most three vertices in V link , by (a), there is a vertex v ∈ V link \ i<j V (P i ). By (f), there are at least ∈ i<j φ(P i ), and since j/n ≤ δ γ, we may let v 1 be such a vertex. Similarly, by (f), there is a vertex Now there is a reserve link P j with ends x j and y j and internal vertices v, v 1 , and v 2 that is vertex-and colour-disjoint from i<j P i , as claimed, and therefore there exists a collection P 2 of reserve links that links M . Now P 1 ∪ P 2 links M , so (5.5.1) holds. By (a), and since M is (δ, F)-perfect, (5.5.2) holds, as required.

Proof.
We now have all the tools we need to prove Lemma 3.9. and P ∪ T satisfies (5.5.2), we have A∈A A ∪ P ∈P P ∪ T is contained in (V , C , G ) with γ-bounded remainder, as required.

Finding many well-spread absorbing gadgets
The aim of this section is to prove Lemma 3.8, which states that, for appropriate µ, ε, almost all 1-factorizations of K n are ε-locally edge-resilient and µ-robustly gadget-resilient. We will use switchings in G col D for appropriate D [n − 1] to analyse the probability that a uniformly random G ∈ G col D satisfies the necessary properties, and then use a 'weighting factor' (see Corollary 6.12) to make comparisons to the probability space corresponding to a uniform random choice of G ∈ G col [n−1] .
6.1. Switchings. We begin by analysing the property of ε-local edge-resilience.

Proof.
Note that if G ∈ G col D has at least ε 3 n 2 /100 edges with endpoints in V for all choices of V ⊆ V of size precisely εn, then G is ε-locally edge-resilient. Fix V ⊆ V of size precisely εn. For any G ∈ G col D , we say that a subgraph H ⊆ G together with a labelling of its vertices (Note that different labellings of a subgraph H ⊆ G that both satisfy these conditions will be considered to correspond to different spin systems of G.) We now define the spin switching operation. Suppose G ∈ G col D and H ⊆ G is a spin system. Then we define spin H (G) to be the coloured graph obtained from G by deleting the edges vw, xy, zu, and adding the edges uv, wx, yz, each with colour φ G (vw). To bound ∆ s+1 from above, we fix G ∈ M s+1 and bound the number of pairs (G, H), where G ∈ M s and H is a spin system of G such that spin H (G) = G . There are s + 1 choices for the edge e ∈ E V ,D (G ) created by a spin operation, and 2 choices for which endpoint of e played the role of u in a spin, and which played the role of v. Now there are at most (n/2) 2 choices for two edges with colour φ G (e) in G with both endpoints outside of V , and at most 8 choices for which endpoints of these edges played the roles of w, x, y, z in a spin operation yielding G . We deduce that ∆ s+1 ≤ 4(s + 1)n 2 .
Suppose that s ≤ ε 3 n 2 /80. To bound δ s from below, we fix G ∈ M s and find a lower bound for the number of spin systems , and suppose for a contradiction that |V * G | < 9εn/10. Then there are at least εn/10 vertices v ∈ V for which there are at least εn/10 colours c ∈ D such that the c-neighbour of v is in V , in G, whence s = e V ,D (G) ≥ ε 2 n 2 /200 > ε 3 n 2 /80 ≥ s, a contradiction. Note further that, since s ≤ ε 3 n 2 /80, there are at least 9εn/10 2 − ε 3 n 2 /80 ≥ ε 2 n 2 /4 pairs {a, b} ∈ V * G 2 such that ab / ∈ E(G). For each such choice of {a, b}, there are two choices of which vertex will play the role of u and which will play the role of v in a spin system. Since u, v ∈ V * G , there are at least 4εn/5 colours c ∈ D such that the c-neighbour z of u, and the c-neighbour w of v, are such that w, z ∈ V \ V , in G. Finally, there are at least n/2 − 3εn ≥ n/4 edges coloured c in G with neither endpoint in V ∪ N G (w) ∪ N G (z), and two choices of which endpoint of such an edge will play the role of x, and which will play the role of y. We deduce that δ s ≥ ε 3 n 4 /5. Altogether, we conclude that if s ≤ ε 3 n 2 /80 and M s is non-empty, then M s+1 is non-empty and |M s |/|M s+1 | ≤ 20(s + 1)n 2 /ε 3 n 4 ≤ 1/2. A union bound over all choices of V ⊆ V of size εn now completes the proof.
We now turn to showing that for suitable D ⊆ [n−1], almost all G ∈ G col D are robustly gadgetresilient, which turns out to be a much harder property to analyse than local edge-resilience, and we devote the rest of this section to it. We first need to show that almost all G ∈ G col D are 'quasirandom', in the sense that small sets of vertices do not have too many crossing edges.
. We say that G ∈ G col D is quasirandom if for all sets A, B ⊆ V , not necessarily distinct, such that |A| = |B| = |D|, we have that e G (A, B) < 8(|D| − 1) 3 /n. We define Q col D := {G ∈ G col D : G is quasirandom}. When we are analysing switchings to study the property of robust gadget-resilience (see Lemma 6.8), it will be important to condition on this quasirandomness. One can use another switching argument to show that almost all G ∈ G col D are quasirandom.
Proof. Fix A, B ⊆ V satisfying |A| = |B| = µn + 1. For any G ∈ G col D , we say that a subgraph H ⊆ G together with a labelling of its vertices V (H) = {a, b, v, w} is a rotation system of G if E(H) = {ab, vw}, where a ∈ A, b ∈ B, v, w / ∈ A ∪ B, aw, bv / ∈ E(G), and φ G (ab) = φ G (vw). We now define the rotate switching operation. Suppose G ∈ G col D and H ⊆ G is a rotation system. Then we define rot H (G) to be the coloured graph obtained from G by deleting the edges ab, vw, and adding the edges aw, bv, each with colour φ G (ab). Writing G := rot H (G), notice that G ∈ G col D and e G (A, B) = e G (A, B) − 1. Lemma 6.3 follows by analysing the degrees of auxiliary bipartite multigraphs B s in a similar way as in the proof of Lemma 6.1. We omit the details.
Next we will use a switching argument to find a large set of well-spread absorbing gadgets (cf. Definition 3.7). For this, we consider slightly more restrictive substructures than the absorbing gadgets defined in Definition 3.1. These additional restrictions (an extra edge f as well as an underlying partition P of the colours) give us better control over the switching process: they allow us to argue that we do not create more than one additional gadget per switch. Let is an (ordered) partition of D into four subsets, and let x ∈ V .
Definition 6.4. An (x, c, P)-gadget in G is a subgraph J = A ∪ {f } of G the following form (see Figure 4): (i) A is an (x, c)-absorbing gadget in G; (ii) there is an edge e 1 ∈ ∂ A (x) such that φ(e 1 ) ∈ D 1 , and the remaining edge e 2 ∈ ∂ A (x) satisfies φ(e 2 ) ∈ D 2 ; (iii) the edge e 3 of A which is not incident to x but shares an endvertex with e 1 and an endvertex with e 2 satisfies φ(e 3 ) ∈ D 3 ; (iv) f = xv is an edge of G, where v is the unique vertex of A such that φ(∂ A (v)) = {c, φ(e 1 )}; We now define some terminology that will be useful for analysing how many (x, c, P)-gadgets there are in a graph G ∈ G col D * , and how well-spread these gadgets are. Each of the terms we define here will have a dependence on the choice of the triple (x, c, P), but since this triple will always be clear from context, for presentation we omit the (x, c, P)-notation. Definition 6.5. We say that an (x, c, P)-gadget J in G is distinguishable in G if the edges e 3 , e 3 of J such that φ(e 3 ) = φ(e 3 ) ∈ D 3 are such that there is no other (x, c, P)-gadget J = J in G such that e 3 ∈ E(J ) or e 3 ∈ E(J ).
We will aim only to count distinguishable (x, c, P)-gadgets, which will ensure the collection of gadgets we find is well-spread across the set of edges in G ∈ G col D * that can play the roles of e 3 , e 3 . We also need to ensure that the collection of gadgets we find is well-spread across the c-edges of G. Definition 6.6.
• For each c-edge e of G ∈ G col D * , we define the saturation of e in G, denoted sat G (e), or simply sat(e) when G is clear from context, to be the number of distinguishable (x, c, P)gadgets of G which contain e. We say that e is unsaturated in G if sat(e) ≤ |D| − 1, saturated if sat(e) ≥ |D|, and supersaturated if sat(e) ≥ |D| + 6. We define Sat(G) to be the set of saturated c-edges of G, and Unsat(G) := E c (G) \ Sat(G). sat(e).
We simultaneously switch two edges into the positions u 5 u 6 and u 9 u 10 because it is much easier to find structures as in Figure 5 than it is to find such a structure with one of these edges already in place. Moreover, the two 'switching cycles' we use have three edges and three non-edges (rather than two of each, as in the rotation switching) essentially because of the extra freedom this gives us when choosing the edges u 1 u 2 and u 13 u 14 . This extra freedom allows us to ensure that in almost all twist systems, one avoids undesirable issues like inadvertently creating more than one new gadget when one performs the twist.
The proof of Lemma 6.8 proceeds with a similar strategy to those of Lemmas 6.1 and 6.3, but it is much more challenging this time to show that graphs with low r(G)-value admit many ways to switch to yield a graph G ∈ G col D * satisfying r(G ) = r(G) + 1. Thus, it follows from condition (b) that there are at most s+1 choices for the canonical (x, c, P)gadget of a twist yielding G for which we record an edge in B s . Fixing this (x, c, P)-gadget fixes the vertices of V which played the roles of x, u 5 , u 6 , . . . , u 10 in a twist yielding G . To determine all possible sets of vertices playing the roles of u 1 , u 2 , u 3 , u 4 , u 11 , u 12 , u 13 , u 14 (thus determining H and G such that twist H (G) = G ), it suffices to find all choices of four edges of G with colour φ G (u 5 u 6 ) satisfying the necessary non-adjacency conditions. There are at most (n/2) 4 choices for these four edges, and at most 4! · 2 4 choices for which endpoints of these edges play which role. We deduce that ∆ s+1 ≤ 24n 4 (s + 1).
Suppose that s ≤ k 4 /2 22 n 2 . To bound δ s from below, we fix G ∈ T D * s and find a lower bound for the number of twist systems H ⊆ G for which we record an edge between G and twist H (G) in B s . To do this, we will show that there are many choices for a set of four colours and two edges, such that each of these sets uniquely identifies a twist system in G for which we record an edge in B s . Note that since s ≤ k 4 /2 22 n 2 and G ∈ Q D * s , we have We begin by finding subsets of D 3 and D 4 with some useful properties in G.
(ii) there are at most 64k 3 /n 2 d-edges e in G with the property that e lies in some distinguishable (x, c, P)-gadget in G whose c-edge is not supersaturated.
Proof of claim: Observe that |N D 1 (x)| = |N D 2 (x)| = k/4. Then, by (arbitrarily extending N D 1 (x), N D 2 (x) and) applying (6.2), we see that e(N D 1 (x), N D 2 (x)) ≤ 10k 3 /n. Thus there is a setD 3 ⊆ D 3 of size |D 3 | ≥ 3k/16 such that each d ∈D 3 satisfies (i). Next, notice that, since r(G) = s, there are at most s/k ≤ k 3 /2 22 n 2 saturated c-edges in G. Suppose for a contradiction that at least k/16 colours d ∈ D 3 are such that there are at least 64k 3 /n 2 dedges e in G with the property that e lies in some distinguishable (x, c, P)-gadget in G whose c-edge is not supersaturated. Then, by considering the contribution of these distinguishable (x, c, P)-gadgets to r(G), and accounting for saturated c-edges, we obtain that r(G) ≥ (k/16) · 32k 3 /n 2 − 5k 3 /2 22 n 2 > s, a contradiction. Thus there is a setD 3 ⊆ D 3 of size |D 3 | ≥ 3k/16 such that each d ∈D 3 satisfies (ii). We define D good We now show that there are many choices of a vector (d 1 , together with an identification of which endpoints will play which role, such that each vector uniquely gives rise to a candidate of a twist system H ⊆ G. We can begin to construct such a candidate by choosing d 4 ∈ D good 4 and letting u 7 denote the d 4 -neighbour of x in G, and letting u 8 denote the c-neighbour of u 7 . Secondly, we choose d 1 ∈ D 1 , avoiding the colour of the edge xu 8 (if it is present), and let u 5 denote the d 1 -neighbour of u 7 , and let u 9 denote the d 1 -neighbour of x. Next, we choose d 2 ∈ D 2 , avoiding the colours of the edges u 5 u 8 , u 5 x, u 8 x, u 8 u 9 in G (if they are present), and let u 6 denote the d 2 -neighbour of u 8 , and let u 10 denote the d 2 -neighbour of x. Then, we choose d 3 ∈ D good 3 , avoiding the colours of all edges in E G ({x, u 5 , u 6 , . . . , u 10 }). We let u 3 , u 4 , u 11 , u 12 denote the d 3 -neighbours of u 5 , u 6 , u 9 , u 10 , respectively. Finally, we choose two distinct edges f 1 , f 2 ∈ E d 3 (G) which are not incident to any vertex in {x, u 3 , u 4 , . . . , u 12 }, and we choose which endpoint of f 1 will play the role of u 1 and which will play the role of u 2 , and choose which endpoint of f 2 will play the role of u 13 and which will play the role of u 14 . Let Λ denote the set of all possible vectors (d 1 , d 2 , d 3 , d 4 , that can be chosen in this way, so that |Λ| ≥ k 8 · 3k 16 · k 8 · k 16 · n 4 ·2· n 4 ·2 = 3k 4 n 2 /2 16 . Further, let H(λ) ⊆ G denote the labelled subgraph of G corresponding to λ ∈ Λ in the above way. If H(λ) is a twist system, then we sometimes say that we 'twist on λ' to mean that we perform the twist operation to obtain twist H(λ) (G) from G.

It is clear that H(λ)
is unique for all vectors λ ∈ Λ, and that H(λ) satisfies conditions (i)-(vi) of the definition of a twist system. However, some H(λ) may fail to satisfy (vii), and some may fail to satisfy condition (b) in the definition of adjacency in B s . We now show that only for a small proportion of λ ∈ Λ do either of these problems occur. We begin by ensuring that most λ ∈ Λ give rise to twist systems.
Claim 2: There is a subset Λ 1 ⊆ Λ such that |Λ 1 | ≥ 9|Λ|/10 and H(λ) is a twist system for all λ ∈ Λ 1 . and − → f 1 , − → f 2 appearing concurrently in some λ ∈ Λ, and note that there are at most (k/4) 2 · n 2 such choices. Here and throughout the remainder of the proof of Lemma 6.8, we write u 7 for the d 4 -neighbour of x, we write u 8 for the c-neighbour of u 7 , and so on, where the choice of → f 2 will always be clear from context. Note that fixing d 3 , d 4 only fixes the vertices x, u 7 , u 8 . There are at most 10k 3 /n pairs (d 1 , d 2 ) with each d i ∈ D i such that there is an edge u 5 u 6 ∈ E(G), since otherwise e(N D 1 (u 7 ), N D 2 (u 8 )) > 10k 3 /n, contradicting (6.2). Similarly, there are at most 10k 3 /n pairs (d 1 , d 2 ) with each d i ∈ D i such that u 9 u 10 is an edge of G. We deduce that there are at most (20k 3 /n) · (k/4) 2 · n 2 = 5k 5 n/4 vectors λ ∈ Λ for which H(λ) is such that either u 5 u 6 or u 9 u 10 is an edge of G.

Proof of claim:
be the set of d 3 -edges e in G with the property that e is in some distinguishable (x, c, P)-gadget in G whose c-edge is not supersaturated. Recall that |F d 3 (G)| ≤ 64k 3 /n 2 since d 3 ∈ D good 3 . Observe then that there are at most 128k 3 /n 2 colours d 2 ∈ D 2 such that u 10 is the endpoint of an edge in F d 3 (G). Thus for all but at most (k/4) 3 ·n 2 ·128k 3 /n 2 = 2k 6 , there are at most 128k 3 /n 2 choices of − → f 1 such that f 1 ∈ F d 3 (G), so that for all but at most (k/4) 4 ·n·128k 3 /n 2 = k 7 /2n vectors λ ∈ Λ 1 , H(λ) is such that f 1 / ∈ F d 3 (G). Similar analyses show that there are at most 8k 6 +k 7 /n ≤ 9k 6 ≤ |Λ 1 |/10 choices of λ ∈ Λ 1 such that {u 1 u 2 , u 3 u 5 , u 4 u 6 , u 9 u 11 , u 10 u 12 , u 13 u 14 }∩F d 3 (G) = ∅. By definition of F d 3 (G) and supersaturation of a c-edge, we deduce that for all remaining λ ∈ Λ 1 , H(λ) is such that deleting the edges u 1 u 2 , u 3 u 5 , u 4 u 6 , u 9 u 11 , u 10 u 12 , u 13 u 14 does not decrease r(G). − When we perform a twist operation on a twist system H in G, since the only new edges we add have some colour in D 3 , we have that for any new distinguishable (x, c, P)-gadget J we create in the twist, one of the new edges u 1 u 3 , u 2 u 4 , u 5 u 6 , u 9 u 10 , u 11 u 13 , u 12 u 14 of the twist is playing the role of either v 5 v 6 or v 9 v 10 in J. (Here and throughout the rest of the proof, we imagine completed (x, c, P)-gadgets J as having vertices labelled x, v 5 , . . . , v 10 , where the role of v i corresponds to the role of u i in Figure 5.) We now show that for most λ ∈ Λ 2 , H(λ) s is non-empty, then T D * s+1 is non-empty and |T D * s |/|T D * s+1 | ≤ 2 17 ·24n 4 (s+ 1)/3k 4 n 2 ≤ 1/2. Now, fix s ≤ µ 4 n 2 /2 23  which completes the proof of the lemma.
6.2. Weighting factor. We now state two results on the number of 1-factorizations in dense d-regular graphs G, where a 1-factorization of G consists of an ordered set of d perfect matchings in G. We will use these results to find a 'weighting factor' (see Corollary 6.12), which we will use to compare the probabilities of particular events occurring in different probability spaces. For any graph G, let M (G) denote the number of distinct 1-factorizations of G, and for any n, d ∈ N, let G n d denote the set of d-regular graphs on n vertices. Firstly, the Kahn-Lovász Theorem (see e.g. [2]) states that a graph with degree sequence r 1 , . . . , r n has at most n i=1 (r i !) 1/2r i perfect matchings. In particular, an n-vertex d-regular graph has at most (d!) n/2d perfect matchings. To determine an upper bound for the number of 1-factorizations of a d-regular graph G, one can simply apply the Kahn-Lovász Theorem repeatedly to obtain M (G) ≤ d r=1 (r!) n/2r . Using Stirling's approximation, we obtain the following result. On the other hand, Ferber, Jain, and Sudakov [13] proved the following lower bound for the number of distinct 1-factorizations in dense regular graphs. Theorem 6.11 ([13, Theorem 1.2]). Suppose C > 0 and n ∈ N is even with 1/n 1/C 1, and d ≥ (1/2 + n −1/C )n. Then every G ∈ G n d satisfies dn/2 . Theorems 6.10 and 6.11 immediately yield the following corollary: Corollary 6.12. Suppose C > 0 and n ∈ N is even with 1/n 1/C 1, and d ≥ (1/2 + n −1/C )n. Then and a set of colours D ⊆ [n − 1], we define the restriction of G to D, denoted G| D , to be the spanning subgraph of G containing precisely those edges of G which have colour in D. Observe that G| D ∈ G col D . We now have all the tools we need to prove Lemma 3.8.
Proof of Lemma 3.8. Let C > 0 be the constant given by Corollary 6.12 and suppose that 1/n 1/C, µ, ε. Let P denote the probability measure for the space corresponding to choosing G ∈ G col [n−1] uniformly at random. Fix D ⊆ [n − 1] such that |D| = εn, and let P D denote the probability measure for the space corresponding to choosing H ∈ G col D uniformly at random. Let G bad D denote the set of H ∈ G col D such that H is not ε-locally edge-resilient. For H ∈ G col D , write N H for the number of distinct completions of H to an element G ∈ G col Now, fix x ∈ V , and fix c ∈ [n − 1]. Choose F ⊆ [n − 1] \ {c} of size |F | = µn arbitrarily. Write F * := F ∪ {c}, and let P F * denote the probability measure for the space S corresponding to choosing H ∈ G col F * uniformly at random. Let P be an equitable (ordered) partition of F into four subsets. Let A (x,c) F * ⊆ G col F * be the set of H ∈ G col F * such that H has a 5µn/4-well-spread collection of at least µ 4 n 2 /2 23 (x, c)-absorbing gadgets. Then, considering A (x,c) F * , Q col F * , Q col F * as events in S, observe that Thus, applying Lemma 6.9, Lemma 6.3, and Lemma 6.8, we obtain P F * A (x,c) F * ≤ P F * r(H) ≤ µ 4 n 2 /2 23 H ∈ Q col F * + P F * H / ∈ Q col F * ≤ exp − µ 4 n 2 2 24 + exp −µ 3 n 2 ≤ exp − µ 4 n 2 2 25 .
The result now follows by combining (6.3) and (6.4).

Modifications and Corollaries
In this section we show how to derive the n odd case of Theorem 1.3 from the case when n is even. We also show how Theorem 1.3(ii) implies Corollary 1.4. 7.1. A rainbow Hamilton cycle for n odd. We actually derive the n odd case of Theorem 1.3 from the following slightly stronger version of Theorem 1.3(ii) in the case when n is even.
Theorem 7.1. If n is even and φ is a uniformly random 1-factorization of K n , then for every vertex v, with high probability, φ admits a rainbow cycle containing all of the colours and all of the vertices except v.
We now argue that our proof of Theorem 1.3 for n even is sufficiently robust to also obtain this strengthening. In particular, we can strengthen Lemma 3.9 so that the absorber does not contain v, since (a)-(c) in Lemma 5.3, (a)-(c) in Lemma 5.4, and (a)-(f) in Lemma 5.5 all hold after deleting v from any part in the absorber partition. The proof of Lemma 3.10 is also sufficiently robust to guarantee that the rainbow path from the lemma does not contain v, but we do not need this strengthening, since we can instead strengthen Proposition 3.5 to obtain a rainbow cycle containing P − v and all of the colours, as follows. If v ∈ V (P ), then we replace v in P with a (V flex , C flex , G flex )-cover by deleting v and adding a (V flex , C flex , G flex )-cover of w, w , and φ(vw), where w and w are the vertices adjacent to v in P . The remainder of the proof proceeds normally, letting v := v to ensure v / ∈ V (P 1 ). In this procedure, we need to assume that P is contained in (V \ V , C \ C , G ) with δ/19-bounded remainder (rather than δ/18), but in Lemma 3.9 we can find a 38γ-absorber, which completes the proof. Now we show how Theorem 7.1 implies the odd n case of Theorem 1.3.
Proof of Theorem 1.3, n odd case. When n is odd, any optimal edge-colouring of K n has n colour classes, each containing precisely (n − 1)/2 edges. For every colour c, there is a unique vertex which has no incident edges of colour c, and for every vertex v, there is a unique colour such that v has no incident edges of this colour. Thus, we can obtain a 1-factorization φ of K n+1 from an optimal edge-colouring φ of K n in the following way. We add a vertex z, and for every other vertex v, we add an edge zv, where φ (zv) is the unique colour c such that v is not incident to a c-edge in K n . Note that this operation produces a bijection from the set of n-edge-colourings of K n to the set of 1-factorizations of K n+1 . Thus, if n is odd and φ is a uniformly random optimal edge-colouring of K n , then φ is a uniformly random optimal edge-colouring of K n+1 . By Theorem 7.1, with high probability there is a rainbow cycle F in K n+1 containing all of the colours and all of the vertices except z, so F is a rainbow Hamilton cycle in K n , satisfying Theorem 1.3(ii). Deleting any edge from F gives a rainbow Hamilton path, as required in Theorem 1.3(i). Proof of Corollary 1.4. Suppose that n ∈ N is odd. Firstly, note that there is a one-to-one correspondence between the set L sym n of symmetric n × n Latin squares with symbols in [n] (say) and the set Φ n of optimal edge-colourings of K n on vertices [n] and with colours in [n]. Indeed, let φ ∈ Φ n . Then we can construct a unique symmetric Latin square L φ ∈ L sym n by putting the symbol φ(ij) in position (i, j) for all edges ij ∈ E(K n ), and for each position (i, i) on the leading diagonal we now enter the unique symbol still missing from row i. Conversely, let L ∈ L sym n . We can obtain a unique element φ L ∈ Φ n from L in the following way. Colour each edge ij of the complete graph K n on vertex set [n] with the symbol in position (i, j) of L. It is clear that φ L is proper, and thus φ L is optimal. Moreover, it is clear that we can uniquely recover L from φ L . Now, let K • n be the graph obtained from K n by adding a loop ii at every vertex i ∈ [n], and for every φ ∈ Φ n , let φ • be the unique proper n-edge-colouring of K • n such that the restriction of φ • to the underlying simple graph is φ. The rainbow 2-factors in K • n admitted by φ • correspond to transversals in L φ in the following way. If L ∈ L sym n and T is a transversal of L, then the subgraph of K • n induced by the edges ij where (i, j) ∈ T is a rainbow 2-factor. If σ is the underlying permutation of T , then the cycles of this rainbow 2-factor are precisely the cycles in the cycle decomposition of σ, up to orientation. Therefore a rainbow Hamilton cycle in K • n corresponds to two disjoint Hamilton transversals in L φ .
By these correspondences, for n odd, if L ∈ L sym n is a uniformly random symmetric n×n Latin square, then φ L is a uniformly random optimal edge-colouring of K n . By Theorem 1.3(ii), φ L admits a rainbow Hamilton cycle F with high probability. Since F is also a rainbow Hamilton cycle in K • n , the corresponding transversals in L are Hamilton, as desired.
Note that, if n is odd, the leading diagonal of any L ∈ L sym n is also a transversal, disjoint from any Hamilton transversal. Indeed, by symmetry all symbols appear an even number of times off of the leading diagonal, and therefore an odd number of times (and thus exactly once) on the leading diagonal.