All Euler problems
Project Euler

Neighbourly Constraints

Count valid vertex-colourings of a graph (e.g., path or cycle) with k colours such that adjacent vertices have different colours. This is the chromatic polynomial.

Source sync May 21, 2026
Problem #0654
Level Level 25
Solved By 410
Languages C++, Python
Answer 815868280
Length 377 words
linear_algebragraphalgebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let $T(n, m)$ be the number of $m$-tuples of positive integers such that the sum of any two neighbouring elements of the tuple is $\le n$.

For example, $T(3, 4)=8$, via the following eight $4$-tuples:

$(1, 1, 1, 1)$

$(1, 1, 1, 2)$

$(1, 1, 2, 1)$

$(1, 2, 1, 1)$

$(1, 2, 1, 2)$

$(2, 1, 1, 1)$

$(2, 1, 1, 2)$

$(2, 1, 2, 1)$

You are also given that $T(5, 5)=246$, $T(10, 10^{2}) \equiv 862820094 \pmod{1\,000\,000\,007}$ and $T(10^2, 10) \equiv 782136797 \pmod{1\,000\,000\,007}$.

Find $T(5000, 10^{12}) \bmod 1\,000\,000\,007$.

Problem 654: Neighbourly Constraints

Mathematical Analysis

Chromatic Polynomial

For a graph GG, the chromatic polynomial P(G,k)P(G, k) counts proper colourings with kk colours.

Path PnP_n (n vertices):

P(Pn,k)=k(k−1)n−1(1)P(P_n, k) = k(k-1)^{n-1} \tag{1}

Cycle CnC_n:

P(Cn,k)=(k−1)n+(−1)n(k−1)(2)P(C_n, k) = (k-1)^n + (-1)^n(k-1) \tag{2}

Complete graph KnK_n:

P(Kn,k)=k(k−1)(k−2)⋯(k−n+1)=kn‾(3)P(K_n, k) = k(k-1)(k-2)\cdots(k-n+1) = k^{\underline{n}} \tag{3}

Transfer Matrix

For path/cycle graphs, the transfer matrix TT has entries Tij=[i≠j]T_{ij} = [i \ne j]:

T=Jk−IkT = J_k - I_k

where JkJ_k is the all-ones matrix. Its eigenvalues are k−1k-1 (once) and −1-1 (k−1k-1 times).

For the path: P(Pn,k)=1TTn−11⋅k/k=k(k−1)n−1P(P_n, k) = \mathbf{1}^T T^{n-1} \mathbf{1} \cdot k/k = k(k-1)^{n-1}.

For the cycle: P(Cn,k)=tr(Tn)=(k−1)n+(k−1)(−1)nP(C_n, k) = \text{tr}(T^n) = (k-1)^n + (k-1)(-1)^n.

Deletion-Contraction

P(G,k)=P(G−e,k)−P(G/e,k)(4)P(G, k) = P(G - e, k) - P(G/e, k) \tag{4}

where G−eG - e deletes edge ee and G/eG/e contracts it.

Derivation

For specific graphs: apply the appropriate formula. For general graphs: use deletion-contraction or transfer matrix.

Proof of Correctness

Theorem. P(Cn,k)=(k−1)n+(−1)n(k−1)P(C_n, k) = (k-1)^n + (-1)^n(k-1).

Proof. By inclusion-exclusion on the path colouring. Start with k(k−1)n−1k(k-1)^{n-1} colourings of the path. Subtract those where vertices 1 and nn have the same colour. Using the transfer matrix eigenvalues and computing tr(Tn)\text{tr}(T^n) gives the result. □\square

Complexity Analysis

  • Path/Cycle: O(log⁡n)O(\log n) via modular exponentiation of the formula.
  • General graph: Deletion-contraction is exponential; transfer matrix is O(k3log⁡n)O(k^3 \log n).

Additional Analysis

Deletion-contraction: P(G,k) = P(G-e,k) - P(G/e,k). Path: k(k-1)^{n-1}. Cycle: (k-1)^n + (-1)^n(k-1). Transfer matrix eigenvalues: k-1 and -1. Verification: P(C_3,3)=6.

Deletion-Contraction

P(G,k) = P(G-e,k) - P(G/e,k). Recursive with O(phi^m) time for m edges.

Transfer Matrix

For paths: T = J_k - I_k with eigenvalues k-1 (once) and -1 (k-1 times). Path: k(k-1)^{n-1}. Cycle: (k-1)^n + (-1)^n(k-1).

Verification

P(K_3, 3) = 321 = 6. P(C_4, 2) = 1+1 = 2 (two alternating colorings). P(C_4, 3) = 16+2 = 18.

Tree Theorem

Every tree on n vertices has P(T,k) = k*(k-1)^{n-1}, same as the path. This is because trees are contractible to a single vertex.

Whitney’s Theorem

The chromatic polynomial uniquely determines the graph among 2-connected graphs. This means the coloring structure encodes the graph’s topology.

Chromatic Roots

The roots of P(G, k) as a polynomial in k lie in specific regions of the complex plane. For planar graphs, no root exceeds 4 (by the four-color theorem, P(G, 4) > 0 for planar G).

Computational Hardness

Computing P(G, k) is #P-hard in general (Jaeger-Vertigan-Welsh). The deletion-contraction algorithm runs in O(phi^m) time where phi = (1+sqrt(5))/2 is the golden ratio.

Grid Graphs

For rectangular grid graphs, the chromatic polynomial can be computed by the transfer matrix method with state space 2^width, running in O(n * 2^{3w}) time for width w and length n.

Answer

815868280\boxed{815868280}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_654/solution.cpp
#include <iostream>

// Reference executable for problem_654.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "815868280";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}