diff --git a/doc/attr.xml b/doc/attr.xml
index 4685d71ca..e62a45de1 100644
--- a/doc/attr.xml
+++ b/doc/attr.xml
@@ -1898,6 +1898,51 @@ gap> ChromaticNumber(CompleteDigraph(10) : byskov);
<#/GAPDoc>
+<#GAPDoc Label="ChromaticPolynomial">
+
+
+ A polynomial with integer coefficients.
+
+ If digraph is a digraph without loops (see ), then this attribute returns the chromatic
+ polynomial of digraph. Edge directions and multiple edges are
+ ignored, and an error is raised if digraph contains loops.
+
+
+ The chromatic polynomial of an undirected graph is a polynomial
+ whose value at each non-negative integer k is the number of
+ proper k-colourings of the graph. A proper k-colouring of a
+ graph is a labelling of its vertices with at most k colours in such
+ a way that adjacent vertices have different labels.
+
+
+ The result is a monic polynomial in the indeterminate k, with
+ degree equal to the number of vertices of digraph. The chromatic
+ number (see ) is the least non-negative
+ integer that is not a root of the chromatic polynomial.
+
+
+ For families of graphs where closed-form expressions are known, these
+ formulae are used to determine the polynomial. Otherwise, it is computed
+ recursively using the deletion-contraction formula.
+ ChromaticPolynomial(EmptyDigraph(10));
+k^10
+gap> ChromaticPolynomial(CompleteDigraph(5));
+k^5-10*k^4+35*k^3-50*k^2+24*k
+gap> ChromaticPolynomial(BinaryTree(3));
+k^7-6*k^6+15*k^5-20*k^4+15*k^3-6*k^2+k
+gap> ChromaticPolynomial(Digraph([[], [3], [5], [2, 3], [4]]));
+k^5-5*k^4+8*k^3-4*k^2
+gap> ChromaticPolynomial(EmptyDigraph(0));
+1
+gap> Value(ChromaticPolynomial(PetersenGraph()), 5);
+332880
+]]>
+
+
+<#/GAPDoc>
+
<#GAPDoc Label="DigraphBicomponents">
diff --git a/doc/z-chap6.xml b/doc/z-chap6.xml
index 26f5e23bc..21006229a 100644
--- a/doc/z-chap6.xml
+++ b/doc/z-chap6.xml
@@ -75,6 +75,7 @@ from} $E_a$ \emph{to} $E_b$. In this case we say that $E_a$ and $E_b$ are
<#Include Label="DigraphWelshPowellOrder">
<#Include Label="DigraphSmallestLastOrder">
<#Include Label="ChromaticNumber">
+ <#Include Label="ChromaticPolynomial">
<#Include Label="DigraphCore">
<#Include Label="LatticeDigraphEmbedding">
<#Include Label="IsLatticeHomomorphism">
diff --git a/gap/attr.gd b/gap/attr.gd
index ff0f5daec..617c39ebd 100644
--- a/gap/attr.gd
+++ b/gap/attr.gd
@@ -77,6 +77,7 @@ DeclareAttribute("DigraphPeriod", IsDigraph);
DeclareAttribute("DigraphLoops", IsDigraph);
DeclareAttribute("ChromaticNumber", IsDigraph);
+DeclareAttribute("ChromaticPolynomial", IsDigraph);
DeclareAttribute("DigraphBicomponents", IsDigraph);
DeclareAttribute("DIGRAPHS_Bipartite", IsDigraph);
DeclareAttribute("DigraphCore", IsDigraph);
diff --git a/gap/attr.gi b/gap/attr.gi
index 40e278a9f..88e9231ea 100644
--- a/gap/attr.gi
+++ b/gap/attr.gi
@@ -841,6 +841,125 @@ function(D)
return chrom;
end);
+InstallMethod(ChromaticPolynomial, "for a digraph", [IsDigraph],
+function(D)
+ local r, k, memo_dict, ChooseEdge, ConnectedPoly, ChromaticPoly, G;
+
+ r := PolynomialRing(Integers, ["x_1", "k"]);
+ k := IndeterminatesOfPolynomialRing(r)[2];
+ memo_dict := NewDictionary("", true);
+
+ # Helper for choosing best edge to minimise recursion depth.
+ ChooseEdge := function(C)
+ local nbs, u, v, best_edge, best_score, score;
+
+ nbs := List(OutNeighbours(C), Set);
+ best_edge := fail;
+ best_score := [-1, 1];
+
+ for u in DigraphVertices(C) do
+ for v in nbs[u] do
+ if u < v then
+ # Ideally: many triangles and/or dense neighbourhoods.
+ score := [Length(Intersection(nbs[u], nbs[v])),
+ Length(nbs[u]) + Length(nbs[v])];
+ if score > best_score then
+ best_score := score;
+ best_edge := [u, v];
+ fi;
+ fi;
+ od;
+ od;
+
+ return best_edge;
+ end;
+
+ # Helper for finding the chromatic polynomial of a given connected graph.
+ ConnectedPoly := function(C)
+ local n, m, degs, e, u, v, w, C_m, C_del, C_cont, key, val, poly;
+
+ n := DigraphNrVertices(C);
+ # Using symmetric closure: should divide by 2.
+ m := DigraphNrEdges(C) / 2;
+ degs := OutDegrees(C);
+
+ # Base cases, closed-form expressions.
+ if n = 1 then
+ return k;
+ elif m = (n * (n - 1)) / 2 then
+ return Product([0 .. n - 1], i -> k - i);
+ elif m = n and ForAll(degs, d -> d = 2) then
+ return (k - 1) ^ n + (-1) ^ n * (k - 1);
+ elif m = n - 1 then
+ return k * (k - 1) ^ (n - 1);
+ fi;
+
+ # Memoisation.
+ key := String(List(OutNeighbours(BlissCanonicalDigraph(C)), Set));
+ val := LookupDictionary(memo_dict, key);
+ if val <> fail then
+ return val;
+ fi;
+
+ # General case: deletion-contraction recurrence.
+ e := ChooseEdge(C);
+ u := e[1];
+ v := e[2];
+
+ C_m := DigraphMutableCopy(C);
+ DigraphRemoveEdge(C_m, u, v);
+ DigraphRemoveEdge(C_m, v, u);
+
+ C_del := DigraphImmutableCopy(C_m);
+
+ for w in OutNeighbours(C_del)[v] do
+ if w <> u and not IsDigraphEdge(C_m, u, w) then
+ DigraphAddEdge(C_m, u, w);
+ DigraphAddEdge(C_m, w, u);
+ fi;
+ od;
+ DigraphRemoveVertex(C_m, v);
+
+ C_cont := DigraphImmutableCopy(C_m);
+
+ poly := ChromaticPoly(C_del) - ChromaticPoly(C_cont);
+ AddDictionary(memo_dict, key, poly);
+ return poly;
+ end;
+
+ # Helper for computing the chromatic polynomial of a given graph
+ # (for recursion).
+ ChromaticPoly := function(H)
+ local res, comps, c;
+
+ comps := DigraphConnectedComponents(H).comps;
+ if Length(comps) = 1 then
+ return ConnectedPoly(H);
+ fi;
+
+ res := One(r);
+ for c in comps do
+ res := res * ConnectedPoly(InducedSubdigraph(H, c));
+ od;
+
+ return res;
+ end;
+
+ # Combining everything above, computing chromatic polynomial:
+ if DigraphHasLoops(D) then
+ ErrorNoReturn("the argument must be a digraph with no loops,");
+ elif DigraphNrEdges(D) = 0 then
+ return k ^ DigraphNrVertices(D);
+ fi;
+
+ # Algorithm requires a simple, undirected graph.
+ G := DigraphMutableCopy(D);
+ DigraphRemoveAllMultipleEdges(G);
+ DigraphSymmetricClosure(G);
+
+ return ChromaticPoly(DigraphImmutableCopy(G));
+end);
+
#
# The following method is currently useless, as the OutNeighbours are computed
# and set whenever a digraph is created. It could be reinstated later if we
diff --git a/tst/standard/attr.tst b/tst/standard/attr.tst
index 28844d651..4a9b08ccc 100644
--- a/tst/standard/attr.tst
+++ b/tst/standard/attr.tst
@@ -2017,6 +2017,81 @@ gap> ChromaticNumber(a : christofides);
gap> ChromaticNumber(b : christofides);
49
+# ChromaticPolynomial
+gap> D := EmptyDigraph(0);;
+gap> ChromaticPolynomial(D);
+1
+gap> D := EmptyDigraph(1);;
+gap> ChromaticPolynomial(D);
+k
+gap> D := Digraph([[1]]);;
+gap> ChromaticPolynomial(D);
+Error, the argument must be a digraph with no loops,
+gap> D := Digraph([[1, 2, 3], [3], [1, 2]]);;
+gap> ChromaticPolynomial(D);
+Error, the argument must be a digraph with no loops,
+gap> D := DigraphDisjointUnion(CompleteDigraph(3), Digraph([[1], [1]]));;
+gap> ChromaticPolynomial(D);
+Error, the argument must be a digraph with no loops,
+gap> D := CompleteDigraph(2);;
+gap> ChromaticPolynomial(D);
+k^2-k
+gap> D := Digraph([[2, 2, 2], []]);;
+gap> ChromaticPolynomial(D);
+k^2-k
+gap> D := EmptyDigraph(2);;
+gap> ChromaticPolynomial(D);
+k^2
+gap> D := EmptyDigraph(10);;
+gap> ChromaticPolynomial(D);
+k^10
+gap> D := CompleteDigraph(4);;
+gap> ChromaticPolynomial(D);
+k^4-6*k^3+11*k^2-6*k
+gap> D := BinaryTree(3);;
+gap> ChromaticPolynomial(D);
+k^7-6*k^6+15*k^5-20*k^4+15*k^3-6*k^2+k
+gap> D := ChainDigraph(7);;
+gap> ChromaticPolynomial(D);
+k^7-6*k^6+15*k^5-20*k^4+15*k^3-6*k^2+k
+gap> D := CycleDigraph(5);;
+gap> ChromaticPolynomial(D);
+k^5-5*k^4+10*k^3-10*k^2+4*k
+gap> D := Digraph([[2, 4], [3, 4], [4], []]);;
+gap> ChromaticPolynomial(D);
+k^4-5*k^3+8*k^2-4*k
+gap> D := Digraph([[2, 3], [3], [4, 5], [5], []]);;
+gap> ChromaticPolynomial(D);
+k^5-6*k^4+13*k^3-12*k^2+4*k
+gap> D := DigraphDisjointUnion(BinaryTree(3),
+> Digraph([[2, 4], [3, 4], [4], []]));;
+gap> ChromaticPolynomial(D);
+k^11-11*k^10+53*k^9-147*k^8+259*k^7-301*k^6+231*k^5-113*k^4+32*k^3-4*k^2
+gap> D := PetersenGraph();;
+gap> ChromaticPolynomial(D);
+k^10-15*k^9+105*k^8-455*k^7+1353*k^6-2861*k^5+4275*k^4-4305*k^3+2606*k^2-704*k
+gap> ChromaticPolynomial(D) = ChromaticPolynomial(OnDigraphs(D, (1, 3, 5, 7)));
+true
+gap> D := DigraphMutableCopy(PetersenGraph());;
+gap> D1 := DigraphCopy(D);;
+gap> ChromaticPolynomial(D);;
+gap> D = D1;
+true
+gap> D := DigraphDisjointUnion(EmptyDigraph(5), ChainDigraph(3));;
+gap> ChromaticPolynomial(D);
+k^8-2*k^7+k^6
+gap> D1 := Digraph([[2, 4], [3, 4], [4], []]);;
+gap> D := DigraphDisjointUnion(D1, D1, D1, D1);;
+gap> ChromaticPolynomial(D) = ChromaticPolynomial(D1) ^ 4;
+true
+gap> D1 := Digraph([[2, 3, 4, 5], [3, 5], [1, 4], [5], [1]]);;
+gap> D2 := CompleteDigraph(5);;
+gap> D3 := BinaryTree(4);;
+gap> D := DigraphDisjointUnion(D1, D2, D3);;
+gap> ChromaticPolynomial(D) = ChromaticPolynomial(D1) * ChromaticPolynomial(D2)
+> * ChromaticPolynomial(D3);
+true
+
# DegreeMatrix
gap> gr := Digraph([[2, 3, 4], [2, 5], [1, 5, 4], [1], [1, 1, 2, 4]]);;
gap> DegreeMatrix(gr);