Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions doc/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,51 @@ gap> ChromaticNumber(CompleteDigraph(10) : byskov);
</ManSection>
<#/GAPDoc>

<#GAPDoc Label="ChromaticPolynomial">
<ManSection>
<Attr Name="ChromaticPolynomial" Arg="digraph"/>
<Returns>A polynomial with integer coefficients.</Returns>
<Description>
If <A>digraph</A> is a digraph without loops (see <Ref
Prop="DigraphHasLoops"/>), then this attribute returns the chromatic
polynomial of <A>digraph</A>. Edge directions and multiple edges are
ignored, and an error is raised if <A>digraph</A> contains loops.
<P/>

The chromatic polynomial of an undirected graph is a polynomial
whose value at each non-negative integer <C>k</C> is the number of
<E>proper k-colourings</E> of the graph. A <E>proper k-colouring</E> of a
graph is a labelling of its vertices with at most <C>k</C> colours in such
a way that adjacent vertices have different labels.
<P/>

The result is a monic polynomial in the indeterminate <C>k</C>, with
degree equal to the number of vertices of <A>digraph</A>. The chromatic
number (see <Ref Oper="ChromaticNumber"/>) is the least non-negative
integer that is not a root of the chromatic polynomial.
<P/>

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.
<Example><![CDATA[
gap> 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
]]></Example>
</Description>
</ManSection>
<#/GAPDoc>

<#GAPDoc Label="DigraphBicomponents">
<ManSection>
<Attr Name="DigraphBicomponents" Arg="digraph"/>
Expand Down
1 change: 1 addition & 0 deletions doc/z-chap6.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
1 change: 1 addition & 0 deletions gap/attr.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
119 changes: 119 additions & 0 deletions gap/attr.gi
Original file line number Diff line number Diff line change
Expand Up @@ -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 <D> 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
Expand Down
75 changes: 75 additions & 0 deletions tst/standard/attr.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <D> must be a digraph with no loops,
gap> D := Digraph([[1, 2, 3], [3], [1, 2]]);;
gap> ChromaticPolynomial(D);
Error, the argument <D> must be a digraph with no loops,
gap> D := DigraphDisjointUnion(CompleteDigraph(3), Digraph([[1], [1]]));;
gap> ChromaticPolynomial(D);
Error, the argument <D> 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);
Expand Down
Loading