From d4c73bf4832967ff67f92ceb5b28f686aea24c79 Mon Sep 17 00:00:00 2001 From: Sumi Sera Date: Wed, 14 Oct 2026 14:44:05 +0100 Subject: [PATCH 1/2] 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/2] 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