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/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"> 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..dcd502980 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); @@ -2602,3 +2608,58 @@ 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 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)); + od; + Append(result, "\n"); + od; + return result; +end); + +InstallMethod(DigraphFromLADString, "for a String", [IsString], +function(s) + # 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 + ErrorNoReturn("the argument must be a non-empty string"); + fi; + n := Int(NormalizedWhitespace(lines[1])); + 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 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 + od; + return ConvertToImmutableDigraphNC(nbs); +end); \ No newline at end of file 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