diff --git a/doc/examples.xml b/doc/examples.xml index 617ee181f..242215678 100644 --- a/doc/examples.xml +++ b/doc/examples.xml @@ -345,3 +345,47 @@ true <#/GAPDoc> + +<#GAPDoc Label="KnightsGraph"> + + + A digraph. + + If m and n are both positive integers, then this operation + returns the Knight's Graph for a m by n board.

+ + From + https://en.wikipedia.org/wiki/Knight%27s_graph: +

+ + In graph theory, a knight's graph, or a knight's tour graph, is a + graph that represents all legal moves of the knight chess piece on a + chessboard. Each vertex of this graph represents a square of the + chessboard, and each edge connects two squares that are a knight's move + apart from each other. More specifically, an m by n knight's + graph is a knight's graph of an m by n chessboard. +

+ + If the optional first argument filt is present, then this should + specify the category or representation the digraph being created will + belong to. For example, if filt is , + then the digraph being created will be mutable, if filt is , then the digraph will be immutable. + If the optional first argument filt is not present, then is used by default.

+ + D := KnightsGraph(8, 8); + +gap> IsConnectedDigraph(D); +true +gap> D := KnightsGraph(3, 3); + +gap> IsConnectedDigraph(D); +false +gap> KnightsGraph(IsMutable, 3, 9); + +]]> + + +<#/GAPDoc> diff --git a/doc/z-chap2.xml b/doc/z-chap2.xml index 673cd8495..7d4d28826 100644 --- a/doc/z-chap2.xml +++ b/doc/z-chap2.xml @@ -87,6 +87,7 @@ <#Include Label="JohnsonDigraph"> <#Include Label="PetersenGraph"> <#Include Label="GeneralisedPetersenGraph"> + <#Include Label="KnightsGraph"> <#Include Label="StarDigraph"> diff --git a/gap/examples.gd b/gap/examples.gd index 0d4ecb22a..e3a1e15d6 100644 --- a/gap/examples.gd +++ b/gap/examples.gd @@ -52,3 +52,7 @@ DeclareOperation("GeneralisedPetersenGraph", [IsFunction, IsInt, IsInt]); DeclareConstructor("StarDigraphCons", [IsDigraph, IsPosInt]); DeclareOperation("StarDigraph", [IsPosInt]); DeclareOperation("StarDigraph", [IsFunction, IsPosInt]); + +DeclareConstructor("KnightsGraphCons", [IsDigraph, IsPosInt, IsPosInt]); +DeclareOperation("KnightsGraph", [IsPosInt, IsPosInt]); +DeclareOperation("KnightsGraph", [IsFunction, IsPosInt, IsPosInt]); diff --git a/gap/examples.gi b/gap/examples.gi index 4e870b6c9..b829bd2af 100644 --- a/gap/examples.gi +++ b/gap/examples.gi @@ -400,3 +400,49 @@ function(filt, k) SetIsCompleteBipartiteDigraph(D, k > 1); return D; end); + +InstallMethod(KnightsGraphCons, +"for IsMutableDigraph and two positive integers", +[IsMutableDigraph, IsPosInt, IsPosInt], +function(filt, m, n) + local D, moveOffSets, coordinates, target, i, j, iPos; + D := EmptyDigraph(IsMutableDigraph, m * n); + moveOffSets := [[2, 1], [-2, 1], [2, -1], [-2, -1], + [1, 2], [-1, 2], [1, -2], [-1, -2]]; + coordinates := []; + for i in [1 .. n] do + for j in [1 .. m] do + Add(coordinates, [i, j]); + od; + od; + iPos := 0; + for i in coordinates do + iPos := iPos + 1; + for j in moveOffSets do + target := [i[1] + j[1], i[2] + j[2]]; + if target[1] in [1 .. n] and target[2] in [1 .. m] then + DigraphAddEdge(D, [iPos, (target[1] - 1) * m + target[2]]); + fi; + od; + od; + return D; +end); + +InstallMethod(KnightsGraphCons, +"for IsImmutableDigraph and two positive integers", +[IsImmutableDigraph, IsPosInt, IsPosInt], +function(filt, m, n) + local D; + D := MakeImmutable(KnightsGraphCons(IsMutableDigraph, m, n)); + SetIsMultiDigraph(D, false); + SetIsSymmetricDigraph(D, true); + SetIsConnectedDigraph(D, m > 2 and n > 2 and not (m = 3 and n = 3)); + return D; +end); + +InstallMethod(KnightsGraph, "for a function and two positive integers", +[IsFunction, IsPosInt, IsPosInt], +KnightsGraphCons); + +InstallMethod(KnightsGraph, "for two positive integers", [IsPosInt, IsPosInt], +{m, n} -> KnightsGraphCons(IsImmutableDigraph, m, n)); diff --git a/tst/standard/examples.tst b/tst/standard/examples.tst index 6ba367f9b..c281eb55e 100644 --- a/tst/standard/examples.tst +++ b/tst/standard/examples.tst @@ -240,6 +240,18 @@ true gap> IsMultiDigraph(StarDigraph(3)); false +# Knight's Graph +gap> D := KnightsGraph(8, 8); + +gap> IsConnectedDigraph(D); +true +gap> D := KnightsGraph(3, 3); + +gap> IsConnectedDigraph(D); +false +gap> KnightsGraph(IsMutable, 3, 9); + + # gap> DIGRAPHS_StopTest(); gap> STOP_TEST("Digraphs package: standard/examples.tst", 0);