Triangle Codes of Graphs: Exact Values on Seven and Eight Vertices, and the Dual Problem on n Vertices

AI-generated · No human mathematician has yet refereed the proof.

Draw graphs on n labelled points so that any two differ in a set of edges containing a triangle. Lean proves the most is 512 on 7 points and 4,096 on 8. If instead no difference may contain a triangle, Lean finds the largest number for every n.

A triangle code on n points is a family of graphs in which, for any two different members, the edges lying in exactly one of them form a graph with a triangle; an odd-cycle code asks for an odd cycle instead. Let Mₜ(n) and Mₒ(n) (t for triangle, o for odd cycle) be the largest sizes. For every n, Mₜ(n) ≤ Mₒ(n) ≤ 2^(C(n, 2) − ⌊n²/4⌋), and both equal the bound when an optimal linear triangle code exists: a set of graphs closed under such differences, of dimension C(n, 2) − ⌊n²/4⌋, whose nonempty members all contain a triangle. Such codes exist for n = 3, …, 8, so Mₜ(n) = Mₒ(n) = 2, 4, 16, 64, 512 and 4,096. In the dual problem no two different members may differ in a graph with a triangle, or with an odd cycle: for every n the largest such families, of sizes Dₜ(n) and Dₒ(n), both have exactly 2^⌊n²/4⌋ members. A linear set of graphs none of whose members contains a triangle has dimension at most ⌊n²/4⌋, and for n ≠ 3 it has dimension ⌊n²/4⌋ exactly when it is the set of all graphs on the cross pairs of a balanced split; on 3 points the graphs with an even number of edges form one more such set. For an optimal linear triangle code V and a balanced split P, every graph is in exactly one way a member of V plus a graph on the cross pairs of P, and V takes every pattern on the inside pairs. For a group H of permutations keeping V and the cross pairs of P, the number of orbits of all pairs equals the number of orbits of the cross pairs of P plus the dimension of the set of members of V that H fixes, and every set of pairs that H keeps together and that has no triangle has at most as many orbits as the cross pairs of P. V is invariant under no swap of two different points when n ≥ 5, no 3-cycle of three different points when n ≥ 6, and no double swap of four different points when n ≥ 7 is odd; on 7 points, under no permutation with such a power. On 7 points some optimal codes have symmetries of order 3, 5 and 6. On 8 points two optimal codes share a symmetry of order 7 and the complete graph, have 7 and 0 members with 4 edges, and are not relabellings of each other. Some optimal code on 8 points has all its members with an even number of edges, and its number of members with each number of edges is computed exactly.

How many different drawings of lines between 7 dots can you make, if any two drawings must differ in a set of lines containing a triangle? In 2023 Alon, Gujgiczer, Körner, Milojević and Simonyi proved a limit on the number and found the answer for up to 6 dots, leaving 7 open. Lean proves that the limit is reached on 7 dots and on 8.

Optimal codes on 7 and 8 points reach Alon et al.’s bound, and a counting proof finds the dual values for every n

Walkthrough: 5 of 5 main results in Lean

Lean re-proves each published result below only in the part this paper uses, and each line names that part. No published result is assumed outside Lean.

Published inputs

  • Alon, Gujgiczer, Körner, Milojević & Simonyi (2023) (Re-proved in Lean): Defined triangle codes, proved Mₜ(n) ≤ Mₒ(n) ≤ 2^(C(n, 2) − ⌊n²/4⌋), found Mₜ(n) = Mₒ(n) = 2, 4, 16, 64 for n = 3 to 6 and Mₒ(7) = 512, and asked whether the bound is reached always or for infinitely many n (their Problem 4). Lean re-proves the bound for triangles and odd cycles for every n, the four small values and Mₒ(7) = 512, and their Lemma 1 only for triangles and odd cycles.
  • Mantel (1907) (not fully read) (Re-proved in Lean): A graph on n points with no triangle has at most ⌊n²/4⌋ edges. Lean proves this for every n, and its equality case, that only the cross pairs of a balanced split reach ⌊n²/4⌋.
  • Pikhurko, Staden & Yilma (2017) (Re-proved in Lean): Use Zykov’s symmetrization in the proof of their Theorem 2; the proof of the paper’s Lemma 10 follows it. Lean proves only the form used here, that some complete multipartite graph on n points has the largest I(G), for every n.

What this paper adds

  • Codes that reach the bound: Optimal codes on 7 and 8 points; Optimal codes on 7 points with symmetries; Two different optimal codes on 8 points; Edge counts of one code on 8 points
  • Symmetries an optimal code cannot have: An optimal code takes every pattern on the inside pairs of a balanced split; Counting orbits of pairs; No swap (n ≥ 5), 3-cycle (n ≥ 6) or double swap (odd n ≥ 7); Forbidden powers on 7 points
  • The dual problem: no difference with a triangle: From families to a count; Deleting one point; Dₜ(n) = Dₒ(n) = 2^⌊n²/4⌋ for every n; Linear families without triangles

The bound is reached for 3 ≤ n ≤ 8, and the dual values hold for every n

Mₜ(n) = Mₒ(n) = 2^(C(n, 2) − ⌊n²/4⌋) for 3 ≤ n ≤ 8, with 512 on 7 points and 4,096 on 8; Dₜ(n) = Dₒ(n) = 2^⌊n²/4⌋ for every n; linear families without triangles have dimension at most ⌊n²/4⌋; no optimal linear triangle code is unchanged by a swap (n ≥ 5), a 3-cycle (n ≥ 6) or a double swap (odd n ≥ 7); and two optimal codes on 8 points are not relabellings of each other.

All 5 of 5 main results, and 16 of 17 numbered claims, proved in Lean with standard axioms; Proposition 17, which relabellings of 7 points (by cycle type) keep some optimal code invariant, rests on a computer search over 10,660 cases outside Lean.

Listed as open in the paper

  • Is Mₜ(9) = 2^(36 − 20), that is, is there a triangle code with 2¹⁶ = 65,536 members on 9 points?
  • Problem 4 of Alon et al. for all n, whether the bound is reached always or for infinitely many n.
  • For n ≥ 4, is every largest family with no difference containing a triangle the family of all graphs on the cross pairs of a balanced split, with one fixed graph added to every member?
  • Are the balanced complete bipartite graphs the only graphs with I(G) = 2^⌊n²/4⌋, for every n ≥ 4?
  • Can the 10,660-case exclusion of a 7-cycle symmetry on 7 points (Proposition 17) be moved into Lean?

Adjacent prior work (not used as inputs)

  • Alon (2026) (not fully read): A survey of graph codes, read in an author’s copy, saying that the exact size of the largest families whose differences all contain a copy of a fixed graph L “is open, even for relatively simple graphs L like a triangle”.
  • Bai, Gao, Ma & Wu (2023): Prove asymptotic results on structured codes of graphs.
  • Balogh (2006) (not fully read): Determined, for large n, the largest number of 2-colourings of a graph’s edges with no copy of a fixed 2-colouring of a complete graph that uses both colours; by the paper’s reading this gives I(G) ≤ 2^⌊n²/4⌋ for all large n. The proof here does not use it.
  • Beers & Botnan (2025): Bounded the Betti number b₁ (the number of independent loops not filled in by triangles) of the flag complex of a graph on n points by that of the balanced complete bipartite graph. The dimension bound for linear families also follows from their inequality; the paper proves it from Mantel’s theorem.
  • Versteegen (2023): Compares linear and general codes in which no difference is a copy of a given graph.
  • Norin & Sun (2016): Studied the largest edge set meeting each triangle in at most one edge, not the number of such sets.

Four graphs on four points

Take the points 0, 1, 2, 3 and four graphs: no edges; 01, 02, 03, 12; 02, 12, 13, 23; and 01, 03, 13, 23. Any two differ in a graph with a triangle: the second and third differ in 01, 03, 13, 23, which has the triangle on 0, 1 and 3. No five graphs can do this. Split the points into {0, 1} and {2, 3}: two graphs that agree on the inside pairs 01 and 23 differ only in cross pairs, which never make a triangle, and the two inside pairs can be filled in only 2 × 2 = 4 ways.

Graphs, codes and relabelling

A graph on n points says, for each of the C(n, 2) pairs of points, whether the pair is an edge. Adding two graphs keeps the edges that lie in exactly one of them. A triangle code is a family in which the sum of any two different members contains a triangle: Mₜ(n) (t for triangle) is the size of the largest one, and Mₒ(n) (o for odd cycle) is the same with odd cycles in place of triangles.

  • A pair of points
  • A graph on n points
  • The complete graph
  • A triangle code
  • An odd-cycle code
  • A linear triangle code
  • An optimal linear triangle code
  • The members of V as a family
  • Mₜ(n), the largest triangle code
  • Mₒ(n), the largest odd-cycle code
  • Relabelling a code
  • Members with w edges
  • V is invariant under π

Members must differ on the inside pairs

Split the points into two sides. If two members of an odd-cycle code agreed on every inside pair, their sum would use only cross pairs, so it would be bipartite and have no odd cycle. So a balanced split, with C(n, 2) − ⌊n²/4⌋ inside pairs, allows at most 2^(C(n, 2) − ⌊n²/4⌋) members. An optimal linear triangle code reaches this bound, and it takes every pattern of 0s and 1s on the inside pairs.

  • The cross pairs of a split
  • The inside pairs of a split
  • Graphs using only the pairs in S
  • A graph on S is a list of values on S
  • A balanced split
  • Reading the inside pairs
  • Mₒ(n) ≤ 2^(C(n, 2) − ⌊n²/4⌋)

    For every n, the largest odd-cycle code on n points has at most 2^(C(n, 2) − ⌊n²/4⌋) members.

    theorem TriangleFreeCodeCap.M_Codd_le_bipartiteBound (n : ℕ) :
      M_Codd n ≤ 2 ^ (n.choose 2 - n ^ 2 / 4)
  • One optimal code fixes both numbers

    If an optimal linear triangle code on n points exists, then Mₜ(n) and Mₒ(n) both equal 2^(C(n, 2) − ⌊n²/4⌋).

  • Code plus cross graphs is everything

    Let V be an optimal linear triangle code and P a balanced split. Every graph on n points is, in exactly one way, a member of V plus a graph that uses only cross pairs of P.

  • An optimal code takes every pattern

    Let V be an optimal linear triangle code and P a balanced split. For every choice of 0s and 1s on the inside pairs of P, some member of V has exactly those values there.

  • The upper bound, and what one optimal code gives

    For every n, Mₜ(n) ≤ Mₒ(n) ≤ 2^(C(n, 2) − ⌊n²/4⌋). If an optimal linear triangle code on n points exists, then Mₜ(n) and Mₒ(n) both equal 2^(C(n, 2) − ⌊n²/4⌋).

  • An optimal code fills in a balanced split

    Let V be an optimal linear triangle code and P a balanced split. Every graph is in exactly one way a member of V plus a graph that uses only cross pairs of P. For every choice of 0s and 1s on the inside pairs of P, some member of V has exactly those values there.

Checks that Lean's kernel runs

Graphs on at most 8 points are stored as 28 bits, one per pair, and adding graphs flips bits. One program tries all 2^k sums of k listed graphs on the n points, the basis, and checks that each sum is empty or contains a triangle; Lean proves that if it passes, the sums form a linear triangle code. A second check, that each basis graph has an edge that no other basis graph has (its pivot pair), gives the dimension k, and similar programs count members by edges and check symmetries.

  • A graph as 28 bits
  • The empty mask
  • Checking every sum for a triangle
  • Reading a count
  • The code spanned by B
  • Checking the pivots
  • Two checks make an optimal code

    If k = C(n, 2) − ⌊n²/4⌋, the pivot check passes and the sum check passes, then the code spanned by B is an optimal linear triangle code.

Optimal codes on 3 to 8 points

Lean writes out optimal codes on 3, 4, 5, 6, 7 and 8 points, with 2, 4, 16, 64, 512 and 4,096 members, so Mₜ(n) and Mₒ(n) both equal the bound for each of these n. On 7 points there are optimal codes with symmetries of order 3, 5 and 6. On 8 points the codes V and W both have the 7-cycle (0 1 2 3 4 5 6) as a symmetry, but V has 7 members with 4 edges and W has none, so no relabelling turns one into the other.

  • The values for 7 and 8 points

    Mₜ(7) = Mₒ(7) = 512 and Mₜ(8) = Mₒ(8) = 4,096. There is a linear triangle code of dimension 9 on 7 points and one of dimension 12 on 8 points.

    theorem TriangleFreeCodeCap.paper_values :
      (M_K3 7 = 512 ∧ M_Codd 7 = 512) ∧
      (M_K3 8 = 4096 ∧ M_Codd 8 = 4096) ∧
        (∃ V, IsLinearTriangleCode V ∧ Module.finrank (ZMod 2) ↥V = 9) ∧
          ∃ V, IsLinearTriangleCode V ∧ Module.finrank (ZMod 2) ↥V = 12
  • Two different optimal codes on 8 points with one symmetry

    There are a permutation π of the 8 points of order 7 and two optimal linear triangle codes V and W on 8 points, both invariant under π and both containing the complete graph, such that V has exactly 7 members with 4 edges and W has none. No permutation of the points turns V into W.

    theorem TriangleFreeCodeCap.paper_two_codes :
      ∃ π V W,
      orderOf π = 7 ∧
        (IsOptimalLinearTriangleCode V ∧ IsPermInvariant π V ∧ completeGraph 8 ∈ V ∧ weightCount V 4 = 7) ∧
          (IsOptimalLinearTriangleCode W ∧ IsPermInvariant π W ∧ completeGraph 8 ∈ W ∧ weightCount W 4 = 0) ∧
            ∀ (σ : Equiv.Perm (Fin 8)), permSubmodule σ V ≠ W
  • The code on 7 points
  • The code on 7 points is optimal

    It is a linear triangle code of dimension 9 = 21 − 12.

  • The code V on 8 points
  • V is optimal

    V is a linear triangle code of dimension 12 = 28 − 16.

  • The 7-cycle (0 1 2 3 4 5 6)
  • The table of edge counts of V
  • The edge counts of V

    For every w, the number of members of V with w edges is the entry for w in the table: 1 member with 0 edges, 7 with 4, 14 with 6, 105 with 8, 336 with 10, 847 with 12, 1,476 with 14, 847 with 16, 336 with 18, 105 with 20, 14 with 22, 7 with 24, 1 with 28, and none with any other number.

  • The code W on 8 points
  • No relabelling turns V into W

    For every permutation π of the 8 points, π(V) ≠ W.

Counting orbits of pairs

A group H of permutations moves the pairs around in orbits, and a graph fixed by H gives all pairs of one orbit the same value. If an optimal linear triangle code V is invariant under H and P is a balanced split whose cross pairs H keeps together, the fixed graphs of V and the fixed graphs on the cross pairs of P together make up all fixed graphs. Comparing dimensions shows that no set of pairs that H keeps together and that has no triangle, such as the cross pairs of another split that H keeps together, has more orbits than the cross pairs of P.

  • The orbit equation

    Let V be an optimal linear triangle code invariant under a group H, and P a balanced split whose cross pairs form a set invariant under H. Then the dimension of the fixed graphs in V plus orb(H, cross pairs of P) equals orb(H, all pairs).

  • Triangle-free sets have few orbits

    Let V be an optimal linear triangle code invariant under a group H, and P a balanced split whose cross pairs form a set invariant under H. Then every set S of pairs that is invariant under H and forms a graph without a triangle has orb(H, S) ≤ orb(H, cross pairs of P).

  • The orbit lemma, for a code invariant under H

    Let H be a group of permutations, V an optimal linear triangle code invariant under H, and P a balanced split whose cross pairs form a set invariant under H. Then the dimension of the graphs in V fixed by H, plus orb(H, cross pairs of P), equals orb(H, all pairs). And every set S of pairs invariant under H whose graph has no triangle has orb(H, S) ≤ orb(H, cross pairs of P).

Ruling out swaps, 3-cycles and double swaps

When n ≥ 5, no optimal linear triangle code is invariant under a swap of two points a and b: a member plus its swapped copy would be a bipartite member, so every member would be unchanged by the swap, yet the code takes a pattern that the swap changes. For a 3-cycle when n ≥ 6, and for a double swap when n ≥ 7 is odd, Lean finds two splits whose orbit counts break the orbit lemma. On 7 points this rules out every permutation with such a power.

  • No swap of two points when n ≥ 5

    Let n ≥ 5 and let V be an optimal linear triangle code on n points. For any two different points a and b, V is not invariant under the permutation that swaps a and b.

  • The 3-cycle (a b c)
  • No 3-cycle when n ≥ 6

    Let n ≥ 6 and let V be an optimal linear triangle code on n points. For any three different points a, b and c, V is not invariant under the 3-cycle (a b c).

  • The double swap (a b)(c d)
  • No double swap when n ≥ 7 is odd

    Let n ≥ 7 be odd and let V be an optimal linear triangle code on n points. For any four different points a, b, c, d, V is not invariant under (a b)(c d).

  • No swap (n ≥ 5), 3-cycle (n ≥ 6) or double swap (odd n ≥ 7)

    Let V be an optimal linear triangle code on n points. If n ≥ 5, V is not invariant under the swap of any two different points. If n ≥ 6, it is not invariant under any 3-cycle of three different points. If n ≥ 7 is odd, it is not invariant under any double swap (a b)(c d) of four different points.

    theorem TriangleFreeCodeCap.paper_symmetry {n : ℕ} {V : Submodule (ZMod 2) (Graph n)} (hopt : IsOptimalLinearTriangleCode V) :
      (5 ≤ n → ∀ (a b : Fin n), a ≠ b → ¬IsPermInvariant (Equiv.swap a b) V) ∧
      (6 ≤ n → ∀ (a b c : Fin n), a ≠ b → b ≠ c → a ≠ c → ¬IsPermInvariant (threeCycle a b c) V) ∧
        (7 ≤ n →
          n % 2 = 1 →
            ∀ (a b c d : Fin n),
              a ≠ b → a ≠ c → a ≠ d → b ≠ c → b ≠ d → c ≠ d → ¬IsPermInvariant (doubleTransposition a b c d) V)

The dual problem: differences without triangles

In the dual problem no two different members of a family may differ in a graph with a triangle, or with an odd cycle, and Dₜ(n) and Dₒ(n) are the largest sizes. The 2^⌊n²/4⌋ graphs using only the cross pairs of a balanced split form such a family. For the upper bound, compression shows that Dₜ(n) is the largest number I(G) of subgraphs of one graph G that take at most one edge from each triangle of G; symmetrization makes G complete multipartite, and deleting one point at a time gives I(G) ≤ 2^⌊n²/4⌋ by induction on n. For linear families, Lean proves Mantel's theorem, that a graph with no triangle has at most ⌊n²/4⌋ edges, with its equality case, and uses it to bound their dimension by ⌊n²/4⌋ and, for n ≠ 3, to find every linear family that reaches it.

  • Dₜ(n) = Dₒ(n) = 2^⌊n²/4⌋ for every n

    For every n, the largest family of graphs on n points in which no two different members differ in a graph with a triangle has exactly 2^⌊n²/4⌋ members, and so does the largest family in which no two differ in a graph with an odd cycle.

    theorem TriangleFreeCodeCap.paper_dual (n : ℕ) :
      D_K3 n = 2 ^ (n ^ 2 / 4) ∧ D_Codd n = 2 ^ (n ^ 2 / 4)
  • Linear families without triangles, and the largest ones

    Let S be a linear set of graphs on n points none of whose members contains a triangle. Then S has dimension at most ⌊n²/4⌋. If n ≠ 3, S has dimension exactly ⌊n²/4⌋ if and only if S is the set of all graphs using only the cross pairs of some balanced split.

  • Compression: Dₜ(n) = the largest I(G)

    For every n, Dₜ(n) equals the largest number of triangle-independent subgraphs of a graph on n points.

  • Symmetrization: a complete multipartite graph has the largest I

    For every n, some complete multipartite graph G on n points has I(H) ≤ I(G) for every graph H on n points.

  • Deletion: I(G) ≤ (1 + ∑ (2^|Q| − 1)) · I(G − v)

    For every complete multipartite graph G on m + 1 points and every point v, I(G) ≤ (1 + ∑ (2^|Q| − 1)) · I(G − v), where the sum runs over the parts Q of G other than the part of v.

  • Every graph has I(G) ≤ 2^⌊n²/4⌋

    For every n and every graph G on n points, G has at most 2^⌊n²/4⌋ triangle-independent subgraphs.

  • Mantel's theorem

    For every n, a graph on n points with no triangle has at most ⌊n²/4⌋ edges.

  • When Mantel's bound is reached

    For every n, a graph G on n points with no triangle has exactly ⌊n²/4⌋ edges if and only if its set of edges is the set of cross pairs of some balanced split.

  • Around a triangle the values add up to 0

    Let S be a linear triangle-free family and a, b, c three different points whose pairs ab, bc and ac are all used by S. Then every member G of S has G(ab) + G(bc) + G(ac) = 0 modulo 2.

  • Dₜ(n), the largest family with triangle-free differences
  • Dₒ(n), the largest family with odd-cycle-free differences
  • A linear triangle-free family
Short

Watch on YouTube ↗