From d4c73bf4832967ff67f92ceb5b28f686aea24c79 Mon Sep 17 00:00:00 2001 From: Sumi Sera Date: Wed, 14 Oct 2026 14:44:05 +0100 Subject: [PATCH 1/5] Created a LAD serialiser and deserialiser to convert to and from digraphs and LAD format --- gap/io.gd | 2 ++ gap/io.gi | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/gap/io.gd b/gap/io.gd index 66d6870e8..30235752a 100644 --- a/gap/io.gd +++ b/gap/io.gd @@ -62,3 +62,5 @@ DeclareOperation("DIMACSString", [IsDigraph]); DeclareGlobalFunction("WritePlainTextDigraph"); DeclareGlobalFunction("DreadnautString"); DeclareGlobalFunction("DigraphPlainTextLineEncoder"); +DeclareOperation("LADString", [IsDigraph]); +DeclareOperation("DigraphFromLADString", [IsString]); \ No newline at end of file diff --git a/gap/io.gi b/gap/io.gi index cbb2297de..460856bc8 100644 --- a/gap/io.gi +++ b/gap/io.gi @@ -2602,3 +2602,55 @@ end); InstallMethod(PlainTextString, "for a digraph", [IsDigraph], D -> DigraphPlainTextLineEncoder(" ", " ", -1)(D)); + +InstallMethod(LADString, "for a digraph", [IsDigraph], +function(D) + local result, nbs, v; + if IsMultiDigraph(D) then + ErrorNoReturn("the argument must not have multiple edges"); + elif DigraphHasLoops(D) then + ErrorNoReturn("the arguement must not have loops"); + fi; + result := StringFormatted("{}\n", DigraphNrVertices(D)); + for nbs in OutNeighbours(D) do + Append(result, String(Length(nbs))); + for v in SortedList(nbs) do + Append(result, " "); + Append(result, String(v-1)); + od; + Append(result, "\n"); + od; + return result; +end); + +InstallMethod(DigraphFromLADString, "for a String", [IsString], +function(s) + + #validating and parsing the string into seperate lines + local lines, n, nbs, line, i, v; + lines := Filtered(SplitString(s, "\n"), x -> not IsEmpty(x)); + if IsEmpty(lines) then + ErrorNoReturn("the argument must be a non-empty string"); + fi; + n := Int(NormalizedWhitespace(lines[1])); + if n = fail or n < 0 or Length(lines) <> n + 1 then + ErrorNoReturn("the first line of must be the number of vertices followed by one line per vertex"); + fi; + + #Convert LAD to Digraph + nbs := []; + for i in [1 .. n] do + line := List(SplitString(NormalizedWhitespace(lines[i+1]), " "), Int); + if IsEmpty(line) or line[1] <> Length(line) - 1 then + ErrorNoReturn("line ", i+1, " of is not valid"); + fi; + Remove(line, 1); + for v in line do + if v < 0 or v >= n then + ErrorNoReturn("line ", i+1, " of has a vertex out of range"); + fi; + od; + Add(nbs, line + 1); #converting from 0 indexed to 1 indexed + od; + return ConvertToImmutableDigraphNC(nbs); + end); \ No newline at end of file From 263a3ce6a2ba738d0026ef563c836d9971fbbf3c Mon Sep 17 00:00:00 2001 From: Sumi Sera Date: Wed, 14 Oct 2026 15:09:58 +0100 Subject: [PATCH 2/5] fix styling issues --- gap/io.gi | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/gap/io.gi b/gap/io.gi index 460856bc8..c3514e94e 100644 --- a/gap/io.gi +++ b/gap/io.gi @@ -2609,14 +2609,14 @@ function(D) if IsMultiDigraph(D) then ErrorNoReturn("the argument must not have multiple edges"); elif DigraphHasLoops(D) then - ErrorNoReturn("the arguement must not have loops"); + ErrorNoReturn("the argument must not have loops"); fi; result := StringFormatted("{}\n", DigraphNrVertices(D)); for nbs in OutNeighbours(D) do Append(result, String(Length(nbs))); for v in SortedList(nbs) do Append(result, " "); - Append(result, String(v-1)); + Append(result, String(v - 1)); od; Append(result, "\n"); od; @@ -2625,8 +2625,7 @@ end); InstallMethod(DigraphFromLADString, "for a String", [IsString], function(s) - - #validating and parsing the string into seperate lines + # validating and parsing the string into separate lines local lines, n, nbs, line, i, v; lines := Filtered(SplitString(s, "\n"), x -> not IsEmpty(x)); if IsEmpty(lines) then @@ -2634,23 +2633,24 @@ function(s) fi; n := Int(NormalizedWhitespace(lines[1])); if n = fail or n < 0 or Length(lines) <> n + 1 then - ErrorNoReturn("the first line of must be the number of vertices followed by one line per vertex"); + ErrorNoReturn("the first line of must be the number of vertices ", + "followed by one line per vertex"); fi; - #Convert LAD to Digraph + # Convert LAD to Digraph nbs := []; for i in [1 .. n] do - line := List(SplitString(NormalizedWhitespace(lines[i+1]), " "), Int); + line := List(SplitString(NormalizedWhitespace(lines[i + 1]), " "), Int); if IsEmpty(line) or line[1] <> Length(line) - 1 then - ErrorNoReturn("line ", i+1, " of is not valid"); + ErrorNoReturn("line ", i + 1, " of is not valid"); fi; Remove(line, 1); for v in line do if v < 0 or v >= n then - ErrorNoReturn("line ", i+1, " of has a vertex out of range"); + ErrorNoReturn("line ", i + 1, " of has a vertex out of range"); fi; od; - Add(nbs, line + 1); #converting from 0 indexed to 1 indexed + Add(nbs, line + 1); # converting from 0 indexed to 1 indexed od; return ConvertToImmutableDigraphNC(nbs); - end); \ No newline at end of file +end); \ No newline at end of file From 0d866190612068a2841e8c1d915d4242443cbc73 Mon Sep 17 00:00:00 2001 From: Sumi Sera Date: Wed, 28 Oct 2026 14:35:07 +0000 Subject: [PATCH 3/5] added tests and documentaion for LAD serialiser and deserialiser Fixed error formatting and adds validation for checking that the digraph does not contain a loop --- doc/io.xml | 33 ++++++++++++++++++++++++++++ gap/io.gi | 17 +++++++++++---- tst/standard/io.tst | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/doc/io.xml b/doc/io.xml index c1caf5156..6022f9fa7 100644 --- a/doc/io.xml +++ b/doc/io.xml @@ -987,4 +987,37 @@ gap> DigraphFromDIMACSString(last); ]]> +<#/GAPDoc> + +<#GAPDoc Label="LADString"> + + + A string. + + Returns a string representing the digraph D in the LAD format. + The first line contains the number of vertices n and it is followed by n lines, one per vertex. + Each of these lines consists of the out-degree of the vertex followed by its out-neighbours, all separated by spaces. + Vertices are numbered from 0. +

+ + The digraph D must not have multiple edges or loops. + LADString(CompleteDigraph(3)); +"3\n2 1 2\n2 0 2\n2 0 1\n"]]> + + +<#/GAPDoc> + +<#GAPDoc Label="DigraphFromLADString"> + + + A digraph. + + Returns the digraph represented by the string s in the LAD format (see ). + An error is raised if s is not a valid LAD string or if it describes a digraph with loops. + DigraphFromLADString("3\n2 1 2\n2 0 2\n2 0 1\n"); +]]> + + <#/GAPDoc> \ No newline at end of file diff --git a/gap/io.gi b/gap/io.gi index c3514e94e..7883bf50a 100644 --- a/gap/io.gi +++ b/gap/io.gi @@ -222,10 +222,12 @@ end); BindGlobal("WholeFileDecoders", HashSet()); AddSet(WholeFileDecoders, "DigraphFromDreadnautString"); AddSet(WholeFileDecoders, "DigraphFromDIMACSString"); +AddSet(WholeFileDecoders, "DigraphFromLADString"); BindGlobal("WholeFileEncoders", HashSet()); AddSet(WholeFileEncoders, "DreadnautString"); AddSet(WholeFileEncoders, "DIMACSString"); +AddSet(WholeFileEncoders, "LADString"); InstallGlobalFunction(IsWholeFileDecoder, decoder -> NameFunction(decoder) in WholeFileDecoders); @@ -297,6 +299,8 @@ function(filename) return DigraphFromDreadnautString; elif extension = "dimacs" then return DigraphFromDIMACSString; + elif extension = "lad" then + return DigraphFromLADString; fi; return fail; @@ -335,6 +339,8 @@ function(filename) return DreadnautString; elif extension = "dimacs" then return DIMACSString; + elif extension = "lad" then + return LADString; fi; return fail; end); @@ -2632,22 +2638,25 @@ function(s) ErrorNoReturn("the argument must be a non-empty string"); fi; n := Int(NormalizedWhitespace(lines[1])); - if n = fail or n < 0 or Length(lines) <> n + 1 then - ErrorNoReturn("the first line of must be the number of vertices ", - "followed by one line per vertex"); + if n = fail or n < 0 then + ErrorNoReturn("the first line of must be the number of vertices"); + elif Length(lines) <> n + 1 then + ErrorNoReturn(" must have exactly one line per vertex after the first"); fi; # Convert LAD to Digraph nbs := []; for i in [1 .. n] do line := List(SplitString(NormalizedWhitespace(lines[i + 1]), " "), Int); - if IsEmpty(line) or line[1] <> Length(line) - 1 then + if IsEmpty(line) or fail in line or line[1] <> Length(line) - 1 then ErrorNoReturn("line ", i + 1, " of is not valid"); fi; Remove(line, 1); for v in line do if v < 0 or v >= n then ErrorNoReturn("line ", i + 1, " of has a vertex out of range"); + elif v = i - 1 then + ErrorNoReturn("line ", i + 1, " of contains a loop"); fi; od; Add(nbs, line + 1); # converting from 0 indexed to 1 indexed diff --git a/tst/standard/io.tst b/tst/standard/io.tst index d1f09a17f..a9dd67381 100644 --- a/tst/standard/io.tst +++ b/tst/standard/io.tst @@ -1090,6 +1090,59 @@ gap> DigraphFromDreadnautString("$=1n3dg\n1 : 2 3.\nf=[1\nf=2"); Error, Unexpected character 'f' in partition specification (line 4) gap> IO_Close(file);; +# LADString and DigraphFromLADString +gap> D := CompleteDigraph(3);; +gap> str := LADString(D); +"3\n2 1 2\n2 0 2\n2 0 1\n" +gap> DigraphFromLADString(str) = D; +true +gap> D := EmptyDigraph(0);; +gap> LADString(D); +"0\n" +gap> DigraphFromLADString(LADString(D)) = D; +true +gap> D := EmptyDigraph(4);; +gap> DigraphFromLADString(LADString(D)) = D; +true +gap> D := Digraph([[2], [], [1]]);; +gap> LADString(D); +"3\n1 1\n0\n1 0\n" +gap> DigraphFromLADString(LADString(D)) = D; +true + +# LADString: bad input +gap> LADString(Digraph([[2, 2], []])); +Error, the argument must not have multiple edges +gap> LADString(Digraph([[1], []])); +Error, the argument must not have loops + +# DigraphFromLADString: bad input +gap> DigraphFromLADString(""); +Error, the argument must be a non-empty string +gap> DigraphFromLADString("abc\n"); +Error, the first line of must be the number of vertices +gap> DigraphFromLADString("2\n1 1\n"); +Error, must have exactly one line per vertex after the first +gap> DigraphFromLADString("2\n1 5\n0\n"); +Error, line 2 of has a vertex out of range +gap> DigraphFromLADString("2\n2 1\n0\n"); +Error, line 2 of is not valid +gap> DigraphFromLADString("2\n1 x\n0\n"); +Error, line 2 of is not valid + +# LAD files +gap> filename := Concatenation(DIGRAPHS_Dir(), "/tst/out/test.lad");; +gap> D := CompleteDigraph(3);; +gap> WriteDigraphs(filename, D, "w"); +IO_OK +gap> ReadDigraphs(filename)[1] = D; +true +gap> WriteDigraphs(filename, D); +Error, LADString is a whole file encoder and so the argument must be "w\ +". +gap> DigraphFromLADString("1\n1 0\n"); +Error, line 2 of contains a loop + # Ensure all files introduced by tests are closed gap> IO.OpenFiles = files; true From 1d8153ddb0535330cf27cc34b32d5b81577e1464 Mon Sep 17 00:00:00 2001 From: Sumi Sera Date: Wed, 4 Nov 2026 11:32:33 +0000 Subject: [PATCH 4/5] fixed gaplint issue --- gap/io.gi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gap/io.gi b/gap/io.gi index 7883bf50a..dcd502980 100644 --- a/gap/io.gi +++ b/gap/io.gi @@ -2656,7 +2656,7 @@ function(s) if v < 0 or v >= n then ErrorNoReturn("line ", i + 1, " of has a vertex out of range"); elif v = i - 1 then - ErrorNoReturn("line ", i + 1, " of contains a loop"); + ErrorNoReturn("line ", i + 1, " of contains a loop"); fi; od; Add(nbs, line + 1); # converting from 0 indexed to 1 indexed From 708c7350b8a695094d51c5f0fa1c0c5bc6237985 Mon Sep 17 00:00:00 2001 From: Sumi Sera Date: Mon, 16 Nov 2026 14:09:38 +0000 Subject: [PATCH 5/5] including LAD docs in manual --- doc/z-chap9.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/z-chap9.xml b/doc/z-chap9.xml index a3599c40a..9992a96a0 100644 --- a/doc/z-chap9.xml +++ b/doc/z-chap9.xml @@ -19,6 +19,8 @@ <#Include Label="Graph6String"> <#Include Label="DigraphFromDreadnautString"> <#Include Label="DIMACSString"> + <#Include Label="LADString"> + <#Include Label="DigraphFromLADString"> <#Include Label="DigraphFile"> <#Include Label="ReadDigraphs"> <#Include Label="WriteDigraphs">