From 1004ddbfcd7d1bde51774245ff7e69ac5a46fee0 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Wed, 4 Nov 2026 12:10:22 +0000 Subject: [PATCH 01/15] Not working --- gap/digraph.gd | 11 ++++++++++- gap/digraph.gi | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/gap/digraph.gd b/gap/digraph.gd index 46cb83d79..8045c9f5e 100644 --- a/gap/digraph.gd +++ b/gap/digraph.gd @@ -128,14 +128,23 @@ DeclareOperation("AsMonoid", [IsFunction, IsDigraph]); DeclareOperation("AsSemigroup", [IsFunction, IsDigraph, IsDenseList, IsDenseList]); -# 10. Random digraphs . . . +# 10. ``Random digraphs . . . +DeclareOperation("RandomDigraphOutNeighbours", [IsInt, IsFloat]); +DeclareOperation("RandomDigraphOutNeighbours", [IsRandomSource, IsInt, IsFloat]); + DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsRat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsFloat]); +#DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt]); +#DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsRat]); +#DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsInt]); DeclareOperation("RandomDigraph", [IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsInt, IsFloat]); +#DeclareOperation("RandomDigraph", [IsRandomSource, IsInt]); +#DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsRat]); +#DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsFunction, IsInt]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsFloat]); diff --git a/gap/digraph.gi b/gap/digraph.gi index 337bfef4a..263e04e4a 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1395,7 +1395,30 @@ end); ######################################################################## # 10. Random digraphs -######################################################################## +######################################################################### + + +InstallMethodWithRandomSource(RandomDigraphOutNeighbours, +"for a random source and an integer and a float", +[IsRandomSource, IsInt, IsFloat], +function(rs, n, p) + local adj, i, j, q; + if p < 0.0 or p > 1.0 then + ErrorNoReturn("the 3rd argument

must be between 0 and 1"); + fi; + adj := List([ 1 .. n], i -> []); + for i in [1 .. n] do + for j in [1 .. n] do + q := Float(Random(rs, 0, 2 ^ 52 - 1 + n ^ 2)) / Float(2 ^ 52 + n ^ 2); + #We think adding n^2 will increase resolution for large digraphs + if q < p then + Add(adj[i], j); + fi; + od; + od; + return adj; +end); + # Methods for probability unspecified or given as a rational for filter in ["IsMutableDigraph", @@ -1406,25 +1429,26 @@ for filter in ["IsMutableDigraph", "IsStronglyConnectedDigraph", "IsAcyclicDigraph", "IsSymmetricDigraph"] do + InstallMethod(RandomDigraphCons, Concatenation("for ", filter, " and an integer"), [EvalString(filter), IsInt], - {f, n} -> RandomDigraphCons(f, n, Float(Random(0, n ^ 2) / n ^ 2))); + {f, n} -> RandomDigraphCons(f, n, Float(Random(0, n ^ 2) / (n ^ 2)))); InstallMethod(RandomDigraphCons, Concatenation("for ", filter, ", an integer, and a rational"), [EvalString(filter), IsInt, IsRat], - {f, n, p} -> RandomDigraphCons(f, n, Float(p))); + {f, n, p} -> RandomDigraphCons(f, n , Float(p))); od; -InstallMethod(RandomDigraphCons, -"for IsMutableDigraph, a positive integer, and a float", -[IsMutableDigraph, IsPosInt, IsFloat], -function(_, n, p) +InstallMethodWithRandomSource(RandomDigraphCons, +"for a random source and IsMutableDigraph, a positive integer, and a float", +[IsRandomSource, IsMutableDigraph, IsPosInt, IsFloat], +function(rs, _, n, p) if p < 0.0 or 1.0 < p then ErrorNoReturn("the 2nd argument

must be between 0 and 1,"); fi; - return DigraphNC(IsMutableDigraph, RANDOM_DIGRAPH(n, p)); + return DigraphNC(IsMutableDigraph, RandomDigraphOutNeighbours(rs, n, p)); end); # This function takes an existing adjacency list after solely creating From a66788fde4a49a9ab280b0970f5c0b3a36f0f9ec Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Wed, 4 Nov 2026 14:37:30 +0000 Subject: [PATCH 02/15] Fixed InstallMethodWithRandomSource constructor error --- gap/digraph.gd | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gap/digraph.gd b/gap/digraph.gd index 8045c9f5e..0f64070be 100644 --- a/gap/digraph.gd +++ b/gap/digraph.gd @@ -135,16 +135,16 @@ DeclareOperation("RandomDigraphOutNeighbours", [IsRandomSource, IsInt, IsFloat]) DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsRat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsFloat]); -#DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt]); -#DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsRat]); -#DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsFloat]); +DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt]); +DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsRat]); +DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsInt]); DeclareOperation("RandomDigraph", [IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsInt, IsFloat]); -#DeclareOperation("RandomDigraph", [IsRandomSource, IsInt]); -#DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsRat]); -#DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsFloat]); +DeclareOperation("RandomDigraph", [IsRandomSource, IsInt]); +DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsRat]); +DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsFunction, IsInt]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsFloat]); From 55aedbcbb63068b320ce65c6fb58f0523937ed99 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Wed, 4 Nov 2026 14:46:53 +0000 Subject: [PATCH 03/15] Corrected version of digraph.gi and digraph.gd --- gap/digraph.gi | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/gap/digraph.gi b/gap/digraph.gi index 263e04e4a..274722264 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1441,16 +1441,27 @@ for filter in ["IsMutableDigraph", {f, n, p} -> RandomDigraphCons(f, n , Float(p))); od; -InstallMethodWithRandomSource(RandomDigraphCons, -"for a random source and IsMutableDigraph, a positive integer, and a float", -[IsRandomSource, IsMutableDigraph, IsPosInt, IsFloat], -function(rs, _, n, p) +InstallMethod(RandomDigraphCons, +"for a random source and IsMutableDigraph, a integer, and a float", +[IsMutableDigraph, IsRandomSource, IsInt, IsFloat], +function(_, rs, n, p) if p < 0.0 or 1.0 < p then ErrorNoReturn("the 2nd argument

must be between 0 and 1,"); fi; return DigraphNC(IsMutableDigraph, RandomDigraphOutNeighbours(rs, n, p)); end); +InstallMethod(RandomDigraphCons, +"IsMutableDigraph, an integer, and a float", +[IsMutableDigraph, IsInt, IsFloat], +function(_, n, p) + if p < 0.0 or 1.0 < p then + ErrorNoReturn("the 2nd argument

must be between 0 and 1,"); + fi; + return RandomDigraphCons(IsMutableDigraph, GlobalMersenneTwister, n, p); +end); + + # This function takes an existing adjacency list after solely creating # a Hamiltonian cycle or tree, and randomly adds edges between all # remaining vertices in the graph. From 1680f4ce5411c774661fc1385f38ab5f49569d21 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Tue, 10 Nov 2026 21:16:00 +0000 Subject: [PATCH 04/15] Documentation, testing and fixing errors --- doc/digraph.xml | 10 +++++-- gap/digraph.gd | 14 ++++++--- gap/digraph.gi | 61 +++++++++++++++++++++++++++++++++------- tst/standard/digraph.tst | 2 ++ 4 files changed, 71 insertions(+), 16 deletions(-) diff --git a/doc/digraph.xml b/doc/digraph.xml index 05e578e11..87caef211 100644 --- a/doc/digraph.xml +++ b/doc/digraph.xml @@ -761,7 +761,7 @@ gap> List(D, Size); <#GAPDoc Label="RandomDigraph"> - + A digraph. &STANDARD_FILT_TEXT; @@ -788,12 +788,18 @@ gap> List(D, Size); For and , edges are added between any pairs of vertices with probability approximately p.

+ If the optional second argument rs is a random source (see ) then it + is used to generate the random choices made when creating the digraph. If rs is not specified, then the + global random source GlobalMersenneTwister is used. Two calls with the same arguments and random sources + in the same state return the same digraph, which makes it possible to reproduce a random digraph by resetting the + random source.

+ If n is a positive integer, then this function returns a random digraph with n vertices and without multiple edges. The result may or may not have loops. If using , the resulting graph will not have any loops by definition.

- If the optional second argument p is a float with value + If the optional fourth argument p is a float with value 0 \leq p \leq 1, then an edge will exist between each pair of vertices with probability approximately p. If p is not specified, then a random probability will be assumed diff --git a/gap/digraph.gd b/gap/digraph.gd index 0f64070be..c2b4691b2 100644 --- a/gap/digraph.gd +++ b/gap/digraph.gd @@ -130,14 +130,17 @@ DeclareOperation("AsSemigroup", # 10. ``Random digraphs . . . DeclareOperation("RandomDigraphOutNeighbours", [IsInt, IsFloat]); -DeclareOperation("RandomDigraphOutNeighbours", [IsRandomSource, IsInt, IsFloat]); +DeclareOperation("RandomDigraphOutNeighbours", + [IsRandomSource, IsInt, IsFloat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsRat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsFloat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt]); -DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsRat]); -DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsFloat]); +DeclareConstructor("RandomDigraphCons", + [IsDigraph, IsRandomSource, IsInt, IsRat]); +DeclareConstructor("RandomDigraphCons", + [IsDigraph, IsRandomSource, IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsInt]); DeclareOperation("RandomDigraph", [IsInt, IsRat]); @@ -145,9 +148,12 @@ DeclareOperation("RandomDigraph", [IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsRandomSource, IsInt]); DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsFloat]); -DeclareOperation("RandomDigraph", [IsFunction, IsInt]); +DeclareOperation("RandomDigraph", [IsFunction, IsInt]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsFloat]); +DeclareOperation("RandomDigraph", [IsFunction, IsRandomSource, IsInt]); +DeclareOperation("RandomDigraph", [IsFunction, IsRandomSource, IsInt, IsRat]); +DeclareOperation("RandomDigraph", [IsFunction, IsRandomSource, IsInt, IsFloat]); DeclareConstructor("RandomTournamentCons", [IsDigraph, IsInt]); DeclareOperation("RandomTournament", [IsInt]); diff --git a/gap/digraph.gi b/gap/digraph.gi index 274722264..a44581dcd 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1396,7 +1396,6 @@ end); ######################################################################## # 10. Random digraphs ######################################################################### - InstallMethodWithRandomSource(RandomDigraphOutNeighbours, "for a random source and an integer and a float", @@ -1407,10 +1406,11 @@ function(rs, n, p) ErrorNoReturn("the 3rd argument

must be between 0 and 1"); fi; adj := List([ 1 .. n], i -> []); - for i in [1 .. n] do + for i in [1 .. n] do for j in [1 .. n] do - q := Float(Random(rs, 0, 2 ^ 52 - 1 + n ^ 2)) / Float(2 ^ 52 + n ^ 2); - #We think adding n^2 will increase resolution for large digraphs + q := Float(Random(rs, 0, 2 ^ 52 - 1 + n ^ 2)) / Float(2 ^ 52 + n ^ 2); + #We think adding n^2 will increase resolution for + #large digraphs if q < p then Add(adj[i], j); fi; @@ -1419,7 +1419,6 @@ function(rs, n, p) return adj; end); - # Methods for probability unspecified or given as a rational for filter in ["IsMutableDigraph", "IsImmutableDigraph", @@ -1438,15 +1437,27 @@ for filter in ["IsMutableDigraph", InstallMethod(RandomDigraphCons, Concatenation("for ", filter, ", an integer, and a rational"), [EvalString(filter), IsInt, IsRat], - {f, n, p} -> RandomDigraphCons(f, n , Float(p))); + {f, n, p} -> RandomDigraphCons(f, n, Float(p))); + + InstallMethod(RandomDigraphCons, + Concatenation("for ", filter, ", a random source, and an integer"), + [EvalString(filter), IsRandomSource, IsInt], + {f, rs, n} -> + RandomDigraphCons(f, rs, n, Float(Random(rs, 0, n ^ 2) / (n ^ 2)))); + + InstallMethod(RandomDigraphCons, + Concatenation("for ", filter, ", a random source, an integer, and a rational"), + [EvalString(filter), IsRandomSource, IsInt, IsRat], + {f, rs, n, p} -> RandomDigraphCons(f, rs, n, Float(p))); + od; InstallMethod(RandomDigraphCons, -"for a random source and IsMutableDigraph, a integer, and a float", +"for IsMutableDigraph, a random source, an integer, and a float", [IsMutableDigraph, IsRandomSource, IsInt, IsFloat], function(_, rs, n, p) if p < 0.0 or 1.0 < p then - ErrorNoReturn("the 2nd argument

must be between 0 and 1,"); + ErrorNoReturn("the probability

must be between 0 and 1,"); fi; return DigraphNC(IsMutableDigraph, RandomDigraphOutNeighbours(rs, n, p)); end); @@ -1456,12 +1467,11 @@ InstallMethod(RandomDigraphCons, [IsMutableDigraph, IsInt, IsFloat], function(_, n, p) if p < 0.0 or 1.0 < p then - ErrorNoReturn("the 2nd argument

must be between 0 and 1,"); + ErrorNoReturn("the probability

must be between 0 and 1,"); fi; return RandomDigraphCons(IsMutableDigraph, GlobalMersenneTwister, n, p); end); - # This function takes an existing adjacency list after solely creating # a Hamiltonian cycle or tree, and randomly adds edges between all # remaining vertices in the graph. @@ -1769,6 +1779,16 @@ function(_, n, p) return DigraphNC(adjacencyList); end); +InstallMethod(RandomDigraphCons, +"for IsImmutableDigraph, a random source, a positive integer, and a float", +[IsImmutableDigraph, IsRandomSource, IsPosInt, IsFloat], +function(_, rs, n, p) + local D; + D := MakeImmutable(RandomDigraphCons(IsMutableDigraph, rs, n, p)); + SetIsMultiDigraph(D, false); + return D; +end); + InstallMethod(RandomDigraphCons, "for IsImmutableDigraph, a positive integer, and a float", [IsImmutableDigraph, IsPosInt, IsFloat], @@ -1788,6 +1808,18 @@ InstallMethod(RandomDigraph, "for a pos int and a rational", [IsPosInt, IsRat], InstallMethod(RandomDigraph, "for a pos int and a float", [IsPosInt, IsFloat], {n, p} -> RandomDigraphCons(IsImmutableDigraph, n, p)); +InstallMethod(RandomDigraph, "for a random source and a pos int", +[IsRandomSource, IsPosInt], +{rs, n} -> RandomDigraphCons(IsImmutableDigraph, rs, n)); + +InstallMethod(RandomDigraph, "for a random source, a pos int and a rational", +[IsRandomSource, IsPosInt, IsRat], +{rs, n, p} -> RandomDigraphCons(IsImmutableDigraph, rs, n, p)); + +InstallMethod(RandomDigraph, "for a random source, a pos int and a float", +[IsRandomSource, IsPosInt, IsFloat], +{rs, n, p} -> RandomDigraphCons(IsImmutableDigraph, rs, n, p)); + InstallMethod(RandomDigraph, "for a func and a pos int", [IsFunction, IsPosInt], RandomDigraphCons); @@ -1797,6 +1829,15 @@ InstallMethod(RandomDigraph, "for a func, a pos int, and a rational", InstallMethod(RandomDigraph, "for a func, a pos int, and a float", [IsFunction, IsPosInt, IsFloat], RandomDigraphCons); +InstallMethod(RandomDigraph, "for a func, a random source, a pos int", +[IsFunction, IsRandomSource, IsPosInt], RandomDigraphCons); + +InstallMethod(RandomDigraph, "for a func, a random source, a pos int and a rational", +[IsFunction, IsRandomSource, IsPosInt, IsRat], RandomDigraphCons); + +InstallMethod(RandomDigraph, "for a func, a random source, a pos int and a float", +[IsFunction, IsRandomSource, IsPosInt, IsFloat], RandomDigraphCons); + InstallMethod(RandomMultiDigraph, "for a pos int", [IsPosInt], n -> RandomMultiDigraph(n, Random([1 .. (n * (n - 1)) / 2]))); diff --git a/tst/standard/digraph.tst b/tst/standard/digraph.tst index 4c458f29e..52d15bd43 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -2091,3 +2091,5 @@ gap> graph := RandomDigraph(IsEulerianDigraph, 10);; # gap> DIGRAPHS_StopTest(); gap> STOP_TEST("Digraphs package: standard/digraph.tst", 0); + +#Random Digraph with Random Source From ce375150e7a4199f5501d4f09ae1d51cc0ac0587 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Wed, 11 Nov 2026 16:35:11 +0000 Subject: [PATCH 05/15] Fixing error regarding filters, improving documentation and adding new testing --- doc/digraph.xml | 8 +++ gap/digraph.gi | 122 +++++++++++++++------------------------ tst/standard/digraph.tst | 68 ++++++++++++++++++++++ 3 files changed, 123 insertions(+), 75 deletions(-) diff --git a/doc/digraph.xml b/doc/digraph.xml index 87caef211..3ad4c0f33 100644 --- a/doc/digraph.xml +++ b/doc/digraph.xml @@ -809,6 +809,14 @@ gap> RandomDigraph(1000); gap> RandomDigraph(10000, 0.023); +gap> RandomDigraph(GlobalMersenneTwister, 1000); + +gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000); + +gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000, 1/2); + +gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000, 0.5); + gap> RandomDigraph(IsMutableDigraph, 1000, 1 / 2); gap> RandomDigraph(IsConnectedDigraph, 1000, 0.75); diff --git a/gap/digraph.gi b/gap/digraph.gi index a44581dcd..ad4faec40 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1429,16 +1429,6 @@ for filter in ["IsMutableDigraph", "IsAcyclicDigraph", "IsSymmetricDigraph"] do - InstallMethod(RandomDigraphCons, - Concatenation("for ", filter, " and an integer"), - [EvalString(filter), IsInt], - {f, n} -> RandomDigraphCons(f, n, Float(Random(0, n ^ 2) / (n ^ 2)))); - - InstallMethod(RandomDigraphCons, - Concatenation("for ", filter, ", an integer, and a rational"), - [EvalString(filter), IsInt, IsRat], - {f, n, p} -> RandomDigraphCons(f, n, Float(p))); - InstallMethod(RandomDigraphCons, Concatenation("for ", filter, ", a random source, and an integer"), [EvalString(filter), IsRandomSource, IsInt], @@ -1462,20 +1452,10 @@ function(_, rs, n, p) return DigraphNC(IsMutableDigraph, RandomDigraphOutNeighbours(rs, n, p)); end); -InstallMethod(RandomDigraphCons, -"IsMutableDigraph, an integer, and a float", -[IsMutableDigraph, IsInt, IsFloat], -function(_, n, p) - if p < 0.0 or 1.0 < p then - ErrorNoReturn("the probability

must be between 0 and 1,"); - fi; - return RandomDigraphCons(IsMutableDigraph, GlobalMersenneTwister, n, p); -end); - # This function takes an existing adjacency list after solely creating # a Hamiltonian cycle or tree, and randomly adds edges between all # remaining vertices in the graph. -BindGlobal("DIGRAPHS_FillOutGraph", function(n, p, adjacencyList) +BindGlobal("DIGRAPHS_FillOutGraph", function(rs, n, p, adjacencyList) local vertices, probability, i, j; vertices := [1 .. n]; @@ -1484,7 +1464,7 @@ BindGlobal("DIGRAPHS_FillOutGraph", function(n, p, adjacencyList) for i in vertices do for j in vertices do if (not (j in adjacencyList[i])) then - if Float(Random(probability) / 100) < p then + if Float(Random(rs, probability) / 100) < p then Add(adjacencyList[i], j); fi; fi; @@ -1495,9 +1475,9 @@ BindGlobal("DIGRAPHS_FillOutGraph", function(n, p, adjacencyList) end); InstallMethod(RandomDigraphCons, -"for IsHamiltonianDigraph, a positive integer, and a float", -[IsHamiltonianDigraph, IsPosInt, IsFloat], -function(_, n, p) +"for IsHamiltonianDigraph, a random source, a positive integer, and a float", +[IsHamiltonianDigraph, IsRandomSource, IsPosInt, IsFloat], +function(_, rs, n, p) local adjacencyList, vertices, i, startVertex, hamiltonianCycle, x, j; adjacencyList := EmptyPlist(n); @@ -1510,14 +1490,14 @@ function(_, n, p) # Edge Case if n = 1 then - if Float(Random([0 .. 99]) / 100) < p then + if Float(Random(rs, [0 .. 99]) / 100) < p then Add(adjacencyList[1], 1); fi; return DigraphNC(adjacencyList); fi; # Starting from a random vertex, we create a Hamiltonian cycle - startVertex := Remove(vertices, Random(vertices)); + startVertex := Remove(vertices, Random(rs, vertices)); hamiltonianCycle := EmptyPlist(n); hamiltonianCycle[1] := startVertex; @@ -1527,7 +1507,7 @@ function(_, n, p) # Create a random edge from the last vertex in the cycle # to a random vertex not in the cycle i := hamiltonianCycle[x]; - j := Remove(vertices, Random([1 .. Length(vertices)])); + j := Remove(vertices, Random(rs, [1 .. Length(vertices)])); Add(hamiltonianCycle, j); Add(adjacencyList[i], j); od; @@ -1536,15 +1516,15 @@ function(_, n, p) # Once we have created a Hamiltonian cycle, fill out the rest of the graph # with random edges according to p - adjacencyList := DIGRAPHS_FillOutGraph(n, p, adjacencyList); + adjacencyList := DIGRAPHS_FillOutGraph(rs, n, p, adjacencyList); return DigraphNC(adjacencyList); end); InstallMethod(RandomDigraphCons, -"for IsEulerianDigraph, a positive integer, and a float", -[IsEulerianDigraph, IsPosInt, IsFloat], -function(_, n, p) +"for IsEulerianDigraph, a random source, a positive integer, and a float", +[IsEulerianDigraph, IsRandomSource, IsPosInt, IsFloat], +function(_, rs, n, p) local adjacencyList, vertices, probability, startVertex, circuitVertex, i, continueCircuit, verticesToConsider, connectedVertices, verticesToCheck; @@ -1559,14 +1539,14 @@ function(_, n, p) # Edge Case if n = 1 then - if Float(Random(probability) / 100) < p then + if Float(Random(rs, probability) / 100) < p then Add(adjacencyList[1], 1); fi; return DigraphNC(adjacencyList); fi; # Starting from a random vertex, we create an Eulerian circuit - startVertex := Random(vertices); + startVertex := Random(rs, vertices); circuitVertex := startVertex; continueCircuit := true; @@ -1587,7 +1567,7 @@ function(_, n, p) while Length(verticesToConsider[circuitVertex]) > 0 do # From the remaining vertices to check, select a random one to see # if an edge will be added - i := Random(verticesToConsider[circuitVertex]); + i := Random(rs, verticesToConsider[circuitVertex]); Remove(verticesToConsider[circuitVertex], Position(verticesToConsider[circuitVertex], i)); @@ -1600,7 +1580,7 @@ function(_, n, p) Add(connectedVertices, i); circuitVertex := i; break; - elif Float(Random(probability) / 100) < p then + elif Float(Random(rs, probability) / 100) < p then Add(adjacencyList[circuitVertex], i); circuitVertex := i; break; @@ -1626,7 +1606,7 @@ function(_, n, p) verticesToCheck := [1 .. n]; while Length(verticesToCheck) > 0 do i := Remove(verticesToCheck, - Random([1 .. Length(verticesToCheck)])); + Random(rs, [1 .. Length(verticesToCheck)])); if (not (i in adjacencyList[circuitVertex])) then Add(adjacencyList[circuitVertex], i); circuitVertex := i; @@ -1641,9 +1621,9 @@ function(_, n, p) end); InstallMethod(RandomDigraphCons, -"for IsConnectedDigraph, a positive integer, and a float", -[IsConnectedDigraph, IsPosInt, IsFloat], -function(_, n, p) +"for IsConnectedDigraph, a random source, a positive integer, and a float", +[IsConnectedDigraph, IsRandomSource, IsPosInt, IsFloat], +function(_, rs, n, p) local adjacencyList, vertices, startVertex, tree, x, i, j; adjacencyList := EmptyPlist(n); @@ -1656,29 +1636,29 @@ function(_, n, p) # Starting from a random vertex, we first create a tree to guarantee # connectivity - startVertex := Remove(vertices, Random(vertices)); + startVertex := Remove(vertices, Random(rs, vertices)); tree := [startVertex]; # While there are n-1 remaining vertices to be added to the tree for x in [1 .. n - 1] do # Create an edge from a random vertex in the tree, to a random vertex # outside of it - i := Random(tree); - j := Remove(vertices, Random([1 .. Length(vertices)])); + i := Random(rs, tree); + j := Remove(vertices, Random(rs, [1 .. Length(vertices)])); Add(tree, j); Add(adjacencyList[i], j); od; # Once the tree has been created, we fill out the rest of the graph with # random edges according to p - adjacencyList := DIGRAPHS_FillOutGraph(n, p, adjacencyList); + adjacencyList := DIGRAPHS_FillOutGraph(rs, n, p, adjacencyList); return DigraphNC(adjacencyList); end); InstallMethod(RandomDigraphCons, -"for IsStronglyConnectedDigraph, a positive integer, and a float", -[IsStronglyConnectedDigraph, IsPosInt, IsFloat], -function(_, n, p) +"for IsStronglyConnectedDigraph, a random source, a positive integer, and a float", +[IsStronglyConnectedDigraph, IsRandomSource, IsPosInt, IsFloat], +function(_, rs, n, p) local d, adjMatrix, stronglyConnectedComponents, scc_a, scc_b, i, random_u, random_v; @@ -1693,8 +1673,8 @@ function(_, n, p) scc_b := stronglyConnectedComponents.comps[i + 1]; # add a connection from u to v - random_u := Random(scc_a); - random_v := Random(scc_b); + random_u := Random(rs, scc_a); + random_v := Random(rs, scc_b); adjMatrix[random_u][random_v] := 1; od; @@ -1705,8 +1685,8 @@ function(_, n, p) scc_b := stronglyConnectedComponents.comps[1]; # add a connection from u to v - random_u := Random(scc_a); - random_v := Random(scc_b); + random_u := Random(rs, scc_a); + random_v := Random(rs, scc_b); adjMatrix[random_u][random_v] := 1; @@ -1714,9 +1694,9 @@ function(_, n, p) end); InstallMethod(RandomDigraphCons, -"for IsAcyclicDigraph, a positive integer, and a float", -[IsAcyclicDigraph, IsPosInt, IsFloat], -function(_, n, p) +"for IsAcyclicDigraph, a random source, a positive integer, and a float", +[IsAcyclicDigraph, IsRandomSource, IsPosInt, IsFloat], +function(_, rs, n, p) local adjacencyList, vertices, probability, i, j; adjacencyList := EmptyPlist(n); @@ -1737,7 +1717,7 @@ function(_, n, p) # From position i in the list, we consider all vertices j to the right of i for i in vertices do for j in [i + 1 .. n] do - if Float(Random(probability) / 100) < p then + if Float(Random(rs, probability) / 100) < p then Add(adjacencyList[vertices[i]], vertices[j]); fi; od; @@ -1747,9 +1727,9 @@ function(_, n, p) end); InstallMethod(RandomDigraphCons, -"for IsSymmetricDigraph, a positive integer, and a float", -[IsSymmetricDigraph, IsPosInt, IsFloat], -function(_, n, p) +"for IsSymmetricDigraph, a random source, a positive integer, and a float", +[IsSymmetricDigraph, IsRandomSource, IsPosInt, IsFloat], +function(_, rs, n, p) local adjacencyList, vertices, probability, i, j; adjacencyList := EmptyPlist(n); @@ -1763,7 +1743,7 @@ function(_, n, p) for i in vertices do for j in [i .. n] do - if Float(Random(probability) / 100) < p then + if Float(Random(rs, probability) / 100) < p then # If it's a self-loop, only add one edge otherwise we would # have a multi-edge. if i = j then @@ -1789,24 +1769,14 @@ function(_, rs, n, p) return D; end); -InstallMethod(RandomDigraphCons, -"for IsImmutableDigraph, a positive integer, and a float", -[IsImmutableDigraph, IsPosInt, IsFloat], -function(_, n, p) - local D; - D := MakeImmutable(RandomDigraphCons(IsMutableDigraph, n, p)); - SetIsMultiDigraph(D, false); - return D; -end); - InstallMethod(RandomDigraph, "for a pos int", [IsPosInt], -n -> RandomDigraphCons(IsImmutableDigraph, n)); +n -> RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n)); InstallMethod(RandomDigraph, "for a pos int and a rational", [IsPosInt, IsRat], -{n, p} -> RandomDigraphCons(IsImmutableDigraph, n, p)); +{n, p} -> RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n, Float(p))); InstallMethod(RandomDigraph, "for a pos int and a float", [IsPosInt, IsFloat], -{n, p} -> RandomDigraphCons(IsImmutableDigraph, n, p)); +{n, p} -> RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n, p)); InstallMethod(RandomDigraph, "for a random source and a pos int", [IsRandomSource, IsPosInt], @@ -1821,13 +1791,15 @@ InstallMethod(RandomDigraph, "for a random source, a pos int and a float", {rs, n, p} -> RandomDigraphCons(IsImmutableDigraph, rs, n, p)); InstallMethod(RandomDigraph, "for a func and a pos int", [IsFunction, IsPosInt], -RandomDigraphCons); +{f, n} -> RandomDigraphCons(f, GlobalMersenneTwister, n)); InstallMethod(RandomDigraph, "for a func, a pos int, and a rational", -[IsFunction, IsPosInt, IsRat], RandomDigraphCons); +[IsFunction, IsPosInt, IsRat], +{f, n, p} -> RandomDigraphCons(f, GlobalMersenneTwister, n, Float(p))); InstallMethod(RandomDigraph, "for a func, a pos int, and a float", -[IsFunction, IsPosInt, IsFloat], RandomDigraphCons); +[IsFunction, IsPosInt, IsFloat], +{f, n, p} -> RandomDigraphCons(f, GlobalMersenneTwister, n, p)); InstallMethod(RandomDigraph, "for a func, a random source, a pos int", [IsFunction, IsRandomSource, IsPosInt], RandomDigraphCons); diff --git a/tst/standard/digraph.tst b/tst/standard/digraph.tst index 52d15bd43..61642c366 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -2093,3 +2093,71 @@ gap> DIGRAPHS_StopTest(); gap> STOP_TEST("Digraphs package: standard/digraph.tst", 0); #Random Digraph with Random Source + +gap> RandomDigraph(IsConnectedDigraph, 10); + +gap> RandomDigraph(IsStronglyConnectedDigraph, 10); + +gap> RandomDigraph(IsAcyclicDigraph, 10); + +gap> RandomDigraph(IsSymmetricDigraph, 10); + +gap> RandomDigraph(IsMutableDigraph, 10, 1/2); + +gap> RandomDigraph(IsImmutableDigraph, 10, 1/2); + +gap> RandomDigraph(IsHamiltonianDigraph, 10, 1/2); + +gap> RandomDigraph(IsEulerianDigraph, 10, 1/2); + +gap> RandomDigraph(IsConnectedDigraph, 10, 1/2); + +gap> RandomDigraph(IsStronglyConnectedDigraph, 10, 1/2); + +gap> RandomDigraph(IsAcyclicDigraph, 10, 1/2); + +gap> RandomDigraph(IsSymmetricDigraph, 10, 1/2); + +gap> RandomDigraph(IsSymmetricDigraph, 10, 0.5); + +gap> RandomDigraph(IsSymmetricDigraph, 10, 0.5); + +gap> RandomDigraph(IsMutableDigraph, 10, 0.5); + +gap> RandomDigraph(IsImmutableDigraph, 10, 0.5); + +gap> RandomDigraph(IsHamiltonianDigraph, 10, 0.5); + +gap> RandomDigraph(IsEulerianDigraph, 10, 0.5); + +gap> RandomDigraph(IsConnectedDigraph, 10, 0.5); + +gap> RandomDigraph(IsStronglyConnectedDigraph, 10, 0.5); + +gap> RandomDigraph(IsAcyclicDigraph, 10, 0.5); + +gap> RandomDigraph(IsSymmetricDigraph, 10, 0.5); + +gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(IsImmutableDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(IsHamiltonianDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(IsEulerianDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(IsConnectedDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(IsStronglyConnectedDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(IsAcyclicDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(IsSymmetricDigraph, GlobalMersenneTwister, 10, 0.5); + +gap> RandomDigraph(GlobalMersenneTwister, 10, 0.5); + +gap> Reset(GlobalMersenneTwister);; +gap> RandomDigraph(GlobalMersenneTwister, 10, 0.5); + +gap> last = last3; +true From e2f8eaef5f040975d6b20671e28900d6d54f0aa4 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Fri, 20 Nov 2026 14:51:54 +0000 Subject: [PATCH 06/15] Fixing minor syntax errors in .xml file and ensuring tests work --- doc/digraph.xml | 6 +- tst/standard/digraph.tst | 115 ++++++++++++++++++--------------------- 2 files changed, 54 insertions(+), 67 deletions(-) diff --git a/doc/digraph.xml b/doc/digraph.xml index 3ad4c0f33..629dfb3e2 100644 --- a/doc/digraph.xml +++ b/doc/digraph.xml @@ -789,8 +789,8 @@ gap> List(D, Size); pairs of vertices with probability approximately p.

If the optional second argument rs is a random source (see ) then it - is used to generate the random choices made when creating the digraph. If rs is not specified, then the - global random source GlobalMersenneTwister is used. Two calls with the same arguments and random sources + is used to generate the random choices made when creating the digraph. If rs is not specified, then the + global random source GlobalMersenneTwister is used. Two calls with the same arguments and random sources in the same state return the same digraph, which makes it possible to reproduce a random digraph by resetting the random source.

@@ -813,8 +813,6 @@ gap> RandomDigraph(GlobalMersenneTwister, 1000); gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000); -gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000, 1/2); - gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000, 0.5); gap> RandomDigraph(IsMutableDigraph, 1000, 1 / 2); diff --git a/tst/standard/digraph.tst b/tst/standard/digraph.tst index 61642c366..a23079a15 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -2094,70 +2094,59 @@ gap> STOP_TEST("Digraphs package: standard/digraph.tst", 0); #Random Digraph with Random Source -gap> RandomDigraph(IsConnectedDigraph, 10); - -gap> RandomDigraph(IsStronglyConnectedDigraph, 10); - -gap> RandomDigraph(IsAcyclicDigraph, 10); - -gap> RandomDigraph(IsSymmetricDigraph, 10); - -gap> RandomDigraph(IsMutableDigraph, 10, 1/2); - -gap> RandomDigraph(IsImmutableDigraph, 10, 1/2); - -gap> RandomDigraph(IsHamiltonianDigraph, 10, 1/2); - -gap> RandomDigraph(IsEulerianDigraph, 10, 1/2); - -gap> RandomDigraph(IsConnectedDigraph, 10, 1/2); - -gap> RandomDigraph(IsStronglyConnectedDigraph, 10, 1/2); - -gap> RandomDigraph(IsAcyclicDigraph, 10, 1/2); - -gap> RandomDigraph(IsSymmetricDigraph, 10, 1/2); - -gap> RandomDigraph(IsSymmetricDigraph, 10, 0.5); - -gap> RandomDigraph(IsSymmetricDigraph, 10, 0.5); - -gap> RandomDigraph(IsMutableDigraph, 10, 0.5); - -gap> RandomDigraph(IsImmutableDigraph, 10, 0.5); - -gap> RandomDigraph(IsHamiltonianDigraph, 10, 0.5); - -gap> RandomDigraph(IsEulerianDigraph, 10, 0.5); - -gap> RandomDigraph(IsConnectedDigraph, 10, 0.5); - -gap> RandomDigraph(IsStronglyConnectedDigraph, 10, 0.5); - -gap> RandomDigraph(IsAcyclicDigraph, 10, 0.5); - -gap> RandomDigraph(IsSymmetricDigraph, 10, 0.5); - -gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(IsImmutableDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(IsHamiltonianDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(IsEulerianDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(IsConnectedDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(IsStronglyConnectedDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(IsAcyclicDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(IsSymmetricDigraph, GlobalMersenneTwister, 10, 0.5); - -gap> RandomDigraph(GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsMutableDigraph(graph) then + Print("False"); + fi; +gap> graph := RandomDigraph(IsImmutableDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsImmutableDigraph(graph) then + Print("False"); + fi; +gap> graph := RandomDigraph(IsHamiltonianDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsHamiltonianDigraph(graph) then + Print("False"); + fi; +gap> graph := RandomDigraph(IsEulerianDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsEulerianDigraph(graph) then + Print("False"); + fi; +gap> graph := RandomDigraph(IsConnectedDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsConnectedDigraph(graph) then + Print("False"); + fi; +gap> graph := RandomDigraph(IsStronglyConnectedDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsStronglyConnectedDigraph(graph) then + Print("False"); + fi; +gap> graph := RandomDigraph(IsAcyclicDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsAcyclicDigraph(graph) then + Print("False"); + fi;gap> +gap> graph := RandomDigraph(IsSymmetricDigraph, GlobalMersenneTwister, 10, 0.5); + if not IsSymmetricDigraph(graph) then + Print("False"); + fi; +gap> Reset(GlobalMersenneTwister);; +gap> graph := RandomDigraph(GlobalMersenneTwister, 10, 0.5); gap> Reset(GlobalMersenneTwister);; -gap> RandomDigraph(GlobalMersenneTwister, 10, 0.5); +gap> graph2 := RandomDigraph(GlobalMersenneTwister, 10, 0.5); -gap> last = last3; +gap> graph = graph2; true +gap> Reset(GlobalMersenneTwister);; +gap> graph := RandomDigraph(IsAcyclicDigraph, 10, 0.3); + +gap> Reset(GlobalMersenneTwister);; +gap> graph2 := RandomDigraph(IsAcyclicDigraph, 10, 0.3); + +gap> graph = graph2; +true +gap> Reset(GlobalMersenneTwister);; +gap> graph := RandomDigraph(IsHamiltonianDigraph, 10); + +gap> Reset(GlobalMersenneTwister);; +gap> graph2 := RandomDigraph(IsHamiltonianDigraph, 10); + +gap> graph = graph2; +true \ No newline at end of file From 107cfd13d1651ee16b0da29feb5c70ae7729b134 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Fri, 20 Nov 2026 15:40:39 +0000 Subject: [PATCH 07/15] Adjusting test files, fixing errors due to my changes in weights.gi, and linting code --- gap/digraph.gd | 10 +++++----- gap/digraph.gi | 32 ++++++++++++++++++-------------- gap/weights.gi | 6 +++--- tst/standard/digraph.tst | 28 ++++++++++++++-------------- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/gap/digraph.gd b/gap/digraph.gd index c2b4691b2..19d39733c 100644 --- a/gap/digraph.gd +++ b/gap/digraph.gd @@ -128,18 +128,18 @@ DeclareOperation("AsMonoid", [IsFunction, IsDigraph]); DeclareOperation("AsSemigroup", [IsFunction, IsDigraph, IsDenseList, IsDenseList]); -# 10. ``Random digraphs . . . +# 10. ``Random digraphs . . . DeclareOperation("RandomDigraphOutNeighbours", [IsInt, IsFloat]); -DeclareOperation("RandomDigraphOutNeighbours", +DeclareOperation("RandomDigraphOutNeighbours", [IsRandomSource, IsInt, IsFloat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsRat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsInt, IsFloat]); DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt]); -DeclareConstructor("RandomDigraphCons", +DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsRat]); -DeclareConstructor("RandomDigraphCons", +DeclareConstructor("RandomDigraphCons", [IsDigraph, IsRandomSource, IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsInt]); @@ -148,7 +148,7 @@ DeclareOperation("RandomDigraph", [IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsRandomSource, IsInt]); DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsRandomSource, IsInt, IsFloat]); -DeclareOperation("RandomDigraph", [IsFunction, IsInt]); +DeclareOperation("RandomDigraph", [IsFunction, IsInt]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsRat]); DeclareOperation("RandomDigraph", [IsFunction, IsInt, IsFloat]); DeclareOperation("RandomDigraph", [IsFunction, IsRandomSource, IsInt]); diff --git a/gap/digraph.gi b/gap/digraph.gi index ad4faec40..e184eb0f8 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1395,8 +1395,8 @@ end); ######################################################################## # 10. Random digraphs -######################################################################### - +######################################################################### + InstallMethodWithRandomSource(RandomDigraphOutNeighbours, "for a random source and an integer and a float", [IsRandomSource, IsInt, IsFloat], @@ -1436,7 +1436,8 @@ for filter in ["IsMutableDigraph", RandomDigraphCons(f, rs, n, Float(Random(rs, 0, n ^ 2) / (n ^ 2)))); InstallMethod(RandomDigraphCons, - Concatenation("for ", filter, ", a random source, an integer, and a rational"), + Concatenation("for ", filter, + ", a random source, an integer, and a rational"), [EvalString(filter), IsRandomSource, IsInt, IsRat], {f, rs, n, p} -> RandomDigraphCons(f, rs, n, Float(p))); @@ -1655,8 +1656,8 @@ function(_, rs, n, p) return DigraphNC(adjacencyList); end); -InstallMethod(RandomDigraphCons, -"for IsStronglyConnectedDigraph, a random source, a positive integer, and a float", +InstallMethod(RandomDigraphCons, "for IsStronglyConnectedDigraph, +a random source, a positive integer, and a float", [IsStronglyConnectedDigraph, IsRandomSource, IsPosInt, IsFloat], function(_, rs, n, p) local d, adjMatrix, stronglyConnectedComponents, @@ -1773,20 +1774,21 @@ InstallMethod(RandomDigraph, "for a pos int", [IsPosInt], n -> RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n)); InstallMethod(RandomDigraph, "for a pos int and a rational", [IsPosInt, IsRat], -{n, p} -> RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n, Float(p))); +{n, p} -> +RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n, Float(p))); InstallMethod(RandomDigraph, "for a pos int and a float", [IsPosInt, IsFloat], {n, p} -> RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n, p)); -InstallMethod(RandomDigraph, "for a random source and a pos int", -[IsRandomSource, IsPosInt], +InstallMethod(RandomDigraph, "for a random source and a pos int", +[IsRandomSource, IsPosInt], {rs, n} -> RandomDigraphCons(IsImmutableDigraph, rs, n)); -InstallMethod(RandomDigraph, "for a random source, a pos int and a rational", +InstallMethod(RandomDigraph, "for a random source, a pos int and a rational", [IsRandomSource, IsPosInt, IsRat], {rs, n, p} -> RandomDigraphCons(IsImmutableDigraph, rs, n, p)); -InstallMethod(RandomDigraph, "for a random source, a pos int and a float", +InstallMethod(RandomDigraph, "for a random source, a pos int and a float", [IsRandomSource, IsPosInt, IsFloat], {rs, n, p} -> RandomDigraphCons(IsImmutableDigraph, rs, n, p)); @@ -1794,20 +1796,22 @@ InstallMethod(RandomDigraph, "for a func and a pos int", [IsFunction, IsPosInt], {f, n} -> RandomDigraphCons(f, GlobalMersenneTwister, n)); InstallMethod(RandomDigraph, "for a func, a pos int, and a rational", -[IsFunction, IsPosInt, IsRat], +[IsFunction, IsPosInt, IsRat], {f, n, p} -> RandomDigraphCons(f, GlobalMersenneTwister, n, Float(p))); InstallMethod(RandomDigraph, "for a func, a pos int, and a float", -[IsFunction, IsPosInt, IsFloat], +[IsFunction, IsPosInt, IsFloat], {f, n, p} -> RandomDigraphCons(f, GlobalMersenneTwister, n, p)); InstallMethod(RandomDigraph, "for a func, a random source, a pos int", [IsFunction, IsRandomSource, IsPosInt], RandomDigraphCons); -InstallMethod(RandomDigraph, "for a func, a random source, a pos int and a rational", +InstallMethod(RandomDigraph, +"for a func, a random source, a pos int and a rational", [IsFunction, IsRandomSource, IsPosInt, IsRat], RandomDigraphCons); -InstallMethod(RandomDigraph, "for a func, a random source, a pos int and a float", +InstallMethod(RandomDigraph, +"for a func, a random source, a pos int and a float", [IsFunction, IsRandomSource, IsPosInt, IsFloat], RandomDigraphCons); InstallMethod(RandomMultiDigraph, "for a pos int", [IsPosInt], diff --git a/gap/weights.gi b/gap/weights.gi index dc1081fcf..d93544467 100644 --- a/gap/weights.gi +++ b/gap/weights.gi @@ -879,15 +879,15 @@ end); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a pos int", [IsPosInt], -n -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n)); +n -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, GlobalMersenneTwister, n)); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a pos int and a float", [IsPosInt, IsFloat], -{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n, p)); +{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, GlobalMersenneTwister, n, p)); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a pos int and a rational", [IsPosInt, IsRat], -{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n, p)); +{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, GlobalMersenneTwister, n, p)); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a function and a pos int", [IsFunction, IsPosInt], diff --git a/tst/standard/digraph.tst b/tst/standard/digraph.tst index a23079a15..bd9989364 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -583,9 +583,9 @@ Error, no 1st choice method found for `RandomDigraph' on 1 arguments gap> RandomDigraph(4, 0); gap> RandomDigraph(10, 1.01); -Error, the 2nd argument

must be between 0 and 1, +Error, the probability

must be between 0 and 1, gap> RandomDigraph(10, -0.01); -Error, the 2nd argument

must be between 0 and 1, +Error, the probability

must be between 0 and 1, gap> RandomDigraph(10, 1 / 10);; # RandomDigraph(IsImmutableDigraph, ...) @@ -606,9 +606,9 @@ Error, no 1st choice method found for `RandomDigraph' on 2 arguments gap> RandomDigraph(IsImmutableDigraph, 4, 0); gap> RandomDigraph(IsImmutableDigraph, 10, 1.01); -Error, the 2nd argument

must be between 0 and 1, +Error, the probability

must be between 0 and 1, gap> RandomDigraph(IsImmutableDigraph, 10, -0.01); -Error, the 2nd argument

must be between 0 and 1, +Error, the probability

must be between 0 and 1, gap> RandomDigraph(IsImmutableDigraph, 10, 1 / 10);; # RandomDigraph(IsMutableDigraph, ...) @@ -629,9 +629,9 @@ Error, no 1st choice method found for `RandomDigraph' on 2 arguments gap> RandomDigraph(IsMutableDigraph, 4, 0); gap> RandomDigraph(IsMutableDigraph, 10, 1.01); -Error, the 2nd argument

must be between 0 and 1, +Error, the probability

must be between 0 and 1, gap> RandomDigraph(IsMutableDigraph, 10, -0.01); -Error, the 2nd argument

must be between 0 and 1, +Error, the probability

must be between 0 and 1, gap> RandomDigraph(IsMutableDigraph, 10, 1 / 10);; # RandomMultiDigraph @@ -2094,35 +2094,35 @@ gap> STOP_TEST("Digraphs package: standard/digraph.tst", 0); #Random Digraph with Random Source -gap> graph := RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsMutableDigraph(graph) then Print("False"); fi; -gap> graph := RandomDigraph(IsImmutableDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsImmutableDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsImmutableDigraph(graph) then Print("False"); fi; -gap> graph := RandomDigraph(IsHamiltonianDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsHamiltonianDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsHamiltonianDigraph(graph) then Print("False"); fi; -gap> graph := RandomDigraph(IsEulerianDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsEulerianDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsEulerianDigraph(graph) then Print("False"); fi; -gap> graph := RandomDigraph(IsConnectedDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsConnectedDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsConnectedDigraph(graph) then Print("False"); fi; -gap> graph := RandomDigraph(IsStronglyConnectedDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsStronglyConnectedDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsStronglyConnectedDigraph(graph) then Print("False"); fi; -gap> graph := RandomDigraph(IsAcyclicDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsAcyclicDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsAcyclicDigraph(graph) then Print("False"); fi;gap> -gap> graph := RandomDigraph(IsSymmetricDigraph, GlobalMersenneTwister, 10, 0.5); +gap> graph := RandomDigraph(IsSymmetricDigraph, GlobalMersenneTwister, 10, 0.5);; if not IsSymmetricDigraph(graph) then Print("False"); fi; From cbc6f5c986e4350edbc5f0fbdf1d5e1f9e47e71d Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sat, 21 Nov 2026 16:39:25 +0000 Subject: [PATCH 08/15] Installing methods for RandomDigraphCons without random source --- doc/digraph.xml | 2 +- gap/digraph.gi | 336 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 328 insertions(+), 10 deletions(-) diff --git a/doc/digraph.xml b/doc/digraph.xml index 629dfb3e2..94f7f0627 100644 --- a/doc/digraph.xml +++ b/doc/digraph.xml @@ -812,7 +812,7 @@ gap> RandomDigraph(10000, 0.023); gap> RandomDigraph(GlobalMersenneTwister, 1000); gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000); - + gap> RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 1000, 0.5); gap> RandomDigraph(IsMutableDigraph, 1000, 1 / 2); diff --git a/gap/digraph.gi b/gap/digraph.gi index e184eb0f8..ceb59d68a 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1397,10 +1397,7 @@ end); # 10. Random digraphs ######################################################################### -InstallMethodWithRandomSource(RandomDigraphOutNeighbours, -"for a random source and an integer and a float", -[IsRandomSource, IsInt, IsFloat], -function(rs, n, p) +BindGlobal("DIGRAPHS_RandomOutNeighbours", function(rs, n, p) local adj, i, j, q; if p < 0.0 or p > 1.0 then ErrorNoReturn("the 3rd argument

must be between 0 and 1"); @@ -1409,8 +1406,8 @@ function(rs, n, p) for i in [1 .. n] do for j in [1 .. n] do q := Float(Random(rs, 0, 2 ^ 52 - 1 + n ^ 2)) / Float(2 ^ 52 + n ^ 2); - #We think adding n^2 will increase resolution for - #large digraphs + # We think adding n^2 will increase resolution for + # large digraphs if q < p then Add(adj[i], j); fi; @@ -1420,6 +1417,9 @@ function(rs, n, p) end); # Methods for probability unspecified or given as a rational +# Installing methods for RandomDigraphCons both with and +# without random source, since a lot of code relies on the +# version of it without a random source argument for filter in ["IsMutableDigraph", "IsImmutableDigraph", "IsHamiltonianDigraph", @@ -1441,6 +1441,17 @@ for filter in ["IsMutableDigraph", [EvalString(filter), IsRandomSource, IsInt, IsRat], {f, rs, n, p} -> RandomDigraphCons(f, rs, n, Float(p))); + InstallMethod(RandomDigraphCons, + Concatenation("for ", filter, " and an integer"), + [EvalString(filter), IsInt], + {f, n} -> + RandomDigraphCons(f, n, Float(Random(0, n ^ 2) / (n ^ 2)))); + + InstallMethod(RandomDigraphCons, + Concatenation("for ", filter,", an integer, and a rational"), + [EvalString(filter), IsInt, IsRat], + {f, n, p} -> RandomDigraphCons(f, n, Float(p))); + od; InstallMethod(RandomDigraphCons, @@ -1450,7 +1461,18 @@ function(_, rs, n, p) if p < 0.0 or 1.0 < p then ErrorNoReturn("the probability

must be between 0 and 1,"); fi; - return DigraphNC(IsMutableDigraph, RandomDigraphOutNeighbours(rs, n, p)); + return DigraphNC(IsMutableDigraph, DIGRAPHS_RandomOutNeighbours(rs, n, p)); +end); + +InstallMethod(RandomDigraphCons, +"for IsMutableDigraph, an integer, and a float", +[IsMutableDigraph, IsInt, IsFloat], +function(_, n, p) + if p < 0.0 or 1.0 < p then + ErrorNoReturn("the probability

must be between 0 and 1,"); + fi; + return DigraphNC(IsMutableDigraph, + DIGRAPHS_RandomOutNeighbours(GlobalMersenneTwister, n, p)); end); # This function takes an existing adjacency list after solely creating @@ -1522,6 +1544,53 @@ function(_, rs, n, p) return DigraphNC(adjacencyList); end); +InstallMethod(RandomDigraphCons, +"for IsHamiltonianDigraph, a positive integer, and a float", +[IsHamiltonianDigraph, IsPosInt, IsFloat], +function(_, n, p) + local adjacencyList, vertices, i, startVertex, hamiltonianCycle, x, j; + + adjacencyList := EmptyPlist(n); + + vertices := [1 .. n]; + + for i in vertices do + Add(adjacencyList, []); + od; + + # Edge Case + if n = 1 then + if Float(Random([0 .. 99]) / 100) < p then + Add(adjacencyList[1], 1); + fi; + return DigraphNC(adjacencyList); + fi; + + # Starting from a random vertex, we create a Hamiltonian cycle + startVertex := Remove(vertices, Random(vertices)); + hamiltonianCycle := EmptyPlist(n); + hamiltonianCycle[1] := startVertex; + + # While there are remaining n-1 vertices to be added to the Hamiltonian + # cycle + for x in [1 .. n - 1] do + # Create a random edge from the last vertex in the cycle + # to a random vertex not in the cycle + i := hamiltonianCycle[x]; + j := Remove(vertices, Random([1 .. Length(vertices)])); + Add(hamiltonianCycle, j); + Add(adjacencyList[i], j); + od; + + Add(adjacencyList[hamiltonianCycle[n]], startVertex); + + # Once we have created a Hamiltonian cycle, fill out the rest of the graph + # with random edges according to p + adjacencyList := DIGRAPHS_FillOutGraph(n, p, adjacencyList); + + return DigraphNC(adjacencyList); +end); + InstallMethod(RandomDigraphCons, "for IsEulerianDigraph, a random source, a positive integer, and a float", [IsEulerianDigraph, IsRandomSource, IsPosInt, IsFloat], @@ -1621,6 +1690,105 @@ function(_, rs, n, p) return DigraphNC(adjacencyList); end); +InstallMethod(RandomDigraphCons, +"for IsEulerianDigraph, a positive integer, and a float", +[IsEulerianDigraph, IsPosInt, IsFloat], +function(_, n, p) + local adjacencyList, vertices, probability, startVertex, circuitVertex, i, + continueCircuit, verticesToConsider, connectedVertices, verticesToCheck; + + adjacencyList := EmptyPlist(n); + + vertices := [1 .. n]; + probability := [0 .. 99]; + + for i in vertices do + Add(adjacencyList, []); + od; + + # Edge Case + if n = 1 then + if Float(Random(probability) / 100) < p then + Add(adjacencyList[1], 1); + fi; + return DigraphNC(adjacencyList); + fi; + + # Starting from a random vertex, we create an Eulerian circuit + startVertex := Random(vertices); + circuitVertex := startVertex; + continueCircuit := true; + + # This will keep track of the vertices left to consider when deciding + # whether to extend the cycle. For example, if we want to extend from the + # current circuitVertex, we can check - verticesToConsider[circuitVertex] - + # to see which vertices it hasn't previously considered. + verticesToConsider := EmptyPlist(n); + for i in vertices do + Add(verticesToConsider, [1 .. n]); + od; + + # Eulerian graphs must be connected, so we store a list of connected + # vertices and check if a vertex isn't in the list + connectedVertices := [startVertex]; + + while continueCircuit do + while Length(verticesToConsider[circuitVertex]) > 0 do + # From the remaining vertices to check, select a random one to see + # if an edge will be added + i := Random(verticesToConsider[circuitVertex]); + Remove(verticesToConsider[circuitVertex], + Position(verticesToConsider[circuitVertex], i)); + + # Check that the edge doesn't already exist + if (not (i in adjacencyList[circuitVertex])) then + # First we guarantee that we get a connected graph by checking + # if the vertex isn't already connected + if (not (i in connectedVertices)) then + Add(adjacencyList[circuitVertex], i); + Add(connectedVertices, i); + circuitVertex := i; + break; + elif Float(Random(probability) / 100) < p then + Add(adjacencyList[circuitVertex], i); + circuitVertex := i; + break; + fi; + fi; + od; + + # If there are not more vertices to consider, we can end the circuit + if Length(verticesToConsider[circuitVertex]) = 0 then + continueCircuit := false; + fi; + od; + + # Finish the circuit by adding an edge back to the start vertex + # (if it isn't already at the start vertex) + if circuitVertex <> startVertex then + # If an edge already exists between the finish vertex and the start + # vertex, we must find another path to the start vertex to avoid + # generating a multidigraph. + while (startVertex in adjacencyList[circuitVertex]) do + # Here we consider all possible edges again, including previously + # rejected edges, to guarantee a path back to the start + verticesToCheck := [1 .. n]; + while Length(verticesToCheck) > 0 do + i := Remove(verticesToCheck, + Random([1 .. Length(verticesToCheck)])); + if (not (i in adjacencyList[circuitVertex])) then + Add(adjacencyList[circuitVertex], i); + circuitVertex := i; + break; + fi; + od; + od; + Add(adjacencyList[circuitVertex], startVertex); + fi; + + return DigraphNC(adjacencyList); +end); + InstallMethod(RandomDigraphCons, "for IsConnectedDigraph, a random source, a positive integer, and a float", [IsConnectedDigraph, IsRandomSource, IsPosInt, IsFloat], @@ -1656,8 +1824,43 @@ function(_, rs, n, p) return DigraphNC(adjacencyList); end); -InstallMethod(RandomDigraphCons, "for IsStronglyConnectedDigraph, -a random source, a positive integer, and a float", +InstallMethod(RandomDigraphCons, +"for IsConnectedDigraph, a positive integer, and a float", +[IsConnectedDigraph, IsPosInt, IsFloat], +function(_, n, p) + local adjacencyList, vertices, startVertex, tree, x, i, j; + + adjacencyList := EmptyPlist(n); + + vertices := [1 .. n]; + + for i in vertices do + Add(adjacencyList, []); + od; + + # Starting from a random vertex, we first create a tree to guarantee + # connectivity + startVertex := Remove(vertices, Random(vertices)); + tree := [startVertex]; + + # While there are n-1 remaining vertices to be added to the tree + for x in [1 .. n - 1] do + # Create an edge from a random vertex in the tree, to a random vertex + # outside of it + i := Random(tree); + j := Remove(vertices, Random([1 .. Length(vertices)])); + Add(tree, j); + Add(adjacencyList[i], j); + od; + + # Once the tree has been created, we fill out the rest of the graph with + # random edges according to p + adjacencyList := DIGRAPHS_FillOutGraph(GlobalMersenneTwister, n, p, adjacencyList); + return DigraphNC(adjacencyList); +end); + +InstallMethod(RandomDigraphCons, +"for IsStronglyConnectedDigraph, a random source, a positive integer, and a float", [IsStronglyConnectedDigraph, IsRandomSource, IsPosInt, IsFloat], function(_, rs, n, p) local d, adjMatrix, stronglyConnectedComponents, @@ -1694,6 +1897,44 @@ function(_, rs, n, p) return DigraphByAdjacencyMatrix(adjMatrix); end); +InstallMethod(RandomDigraphCons, +"for IsStronglyConnectedDigraph, a positive integer, and a float", +[IsStronglyConnectedDigraph, IsPosInt, IsFloat], +function(_, n, p) + local d, adjMatrix, stronglyConnectedComponents, + scc_a, scc_b, i, random_u, random_v; + + d := RandomDigraph(n, p); + + stronglyConnectedComponents := DigraphStronglyConnectedComponents(d); + + adjMatrix := AdjacencyMatrixMutableCopy(d); + + for i in [1 .. Size(stronglyConnectedComponents.comps) - 1] do + scc_a := stronglyConnectedComponents.comps[i]; + scc_b := stronglyConnectedComponents.comps[i + 1]; + + # add a connection from u to v + random_u := Random(scc_a); + random_v := Random(scc_b); + + adjMatrix[random_u][random_v] := 1; + od; + + # connect end scc to first scc + scc_a := stronglyConnectedComponents.comps[ + Size(stronglyConnectedComponents.comps)]; + scc_b := stronglyConnectedComponents.comps[1]; + + # add a connection from u to v + random_u := Random(scc_a); + random_v := Random(scc_b); + + adjMatrix[random_u][random_v] := 1; + + return DigraphByAdjacencyMatrix(adjMatrix); +end); + InstallMethod(RandomDigraphCons, "for IsAcyclicDigraph, a random source, a positive integer, and a float", [IsAcyclicDigraph, IsRandomSource, IsPosInt, IsFloat], @@ -1727,6 +1968,39 @@ function(_, rs, n, p) return DigraphNC(adjacencyList); end); +InstallMethod(RandomDigraphCons, +"for IsAcyclicDigraph, a positive integer, and a float", +[IsAcyclicDigraph, IsPosInt, IsFloat], +function(_, n, p) + local adjacencyList, vertices, probability, i, j; + + adjacencyList := EmptyPlist(n); + + vertices := [1 .. n]; + probability := [0 .. 99]; + + for i in vertices do + Add(adjacencyList, []); + od; + + # We shuffle the vertices and treat the order of the vertices as a + # hierarchy where all vertices to the right of a vertex in the list are + # potential edges. This idea is that the edges flow downwards in the + # hierarchy, avoiding cycles. + Shuffle(vertices); + + # From position i in the list, we consider all vertices j to the right of i + for i in vertices do + for j in [i + 1 .. n] do + if Float(Random(probability) / 100) < p then + Add(adjacencyList[vertices[i]], vertices[j]); + fi; + od; + od; + + return DigraphNC(adjacencyList); +end); + InstallMethod(RandomDigraphCons, "for IsSymmetricDigraph, a random source, a positive integer, and a float", [IsSymmetricDigraph, IsRandomSource, IsPosInt, IsFloat], @@ -1760,6 +2034,40 @@ function(_, rs, n, p) return DigraphNC(adjacencyList); end); +InstallMethod(RandomDigraphCons, +"for IsSymmetricDigraph, a positive integer, and a float", +[IsSymmetricDigraph, IsPosInt, IsFloat], +function(_, n, p) + local adjacencyList, vertices, probability, i, j; + + adjacencyList := EmptyPlist(n); + + vertices := [1 .. n]; + probability := [0 .. 99]; + + for i in vertices do + Add(adjacencyList, []); + od; + + for i in vertices do + for j in [i .. n] do + if Float(Random(probability) / 100) < p then + # If it's a self-loop, only add one edge otherwise we would + # have a multi-edge. + if i = j then + Add(adjacencyList[i], j); + else + Add(adjacencyList[i], j); + Add(adjacencyList[j], i); + fi; + fi; + od; + od; + + return DigraphNC(adjacencyList); +end); + + InstallMethod(RandomDigraphCons, "for IsImmutableDigraph, a random source, a positive integer, and a float", [IsImmutableDigraph, IsRandomSource, IsPosInt, IsFloat], @@ -1770,6 +2078,16 @@ function(_, rs, n, p) return D; end); +InstallMethod(RandomDigraphCons, +"for IsImmutableDigraph, a positive integer, and a float", +[IsImmutableDigraph, IsPosInt, IsFloat], +function(_, n, p) + local D; + D := MakeImmutable(RandomDigraphCons(IsMutableDigraph, n, p)); + SetIsMultiDigraph(D, false); + return D; +end); + InstallMethod(RandomDigraph, "for a pos int", [IsPosInt], n -> RandomDigraphCons(IsImmutableDigraph, GlobalMersenneTwister, n)); From 2bd6b389cd7d21b098325b219d771c79b21d8197 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sun, 22 Nov 2026 10:41:20 +0000 Subject: [PATCH 09/15] Installing methods with and without rs for each filter, cleaning up test files and linting --- gap/digraph.gi | 15 ++++++----- gap/weights.gi | 6 ++--- tst/standard/digraph.tst | 58 ++++++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/gap/digraph.gi b/gap/digraph.gi index ceb59d68a..65ea6cbce 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1402,7 +1402,7 @@ BindGlobal("DIGRAPHS_RandomOutNeighbours", function(rs, n, p) if p < 0.0 or p > 1.0 then ErrorNoReturn("the 3rd argument

must be between 0 and 1"); fi; - adj := List([ 1 .. n], i -> []); + adj := List([1 .. n], i -> []); for i in [1 .. n] do for j in [1 .. n] do q := Float(Random(rs, 0, 2 ^ 52 - 1 + n ^ 2)) / Float(2 ^ 52 + n ^ 2); @@ -1448,7 +1448,7 @@ for filter in ["IsMutableDigraph", RandomDigraphCons(f, n, Float(Random(0, n ^ 2) / (n ^ 2)))); InstallMethod(RandomDigraphCons, - Concatenation("for ", filter,", an integer, and a rational"), + Concatenation("for ", filter, ", an integer, and a rational"), [EvalString(filter), IsInt, IsRat], {f, n, p} -> RandomDigraphCons(f, n, Float(p))); @@ -1855,12 +1855,14 @@ function(_, n, p) # Once the tree has been created, we fill out the rest of the graph with # random edges according to p - adjacencyList := DIGRAPHS_FillOutGraph(GlobalMersenneTwister, n, p, adjacencyList); + adjacencyList := + DIGRAPHS_FillOutGraph(GlobalMersenneTwister, n, p, adjacencyList); return DigraphNC(adjacencyList); end); -InstallMethod(RandomDigraphCons, -"for IsStronglyConnectedDigraph, a random source, a positive integer, and a float", +InstallMethod(RandomDigraphCons, +Concatenation("for IsStronglyConnectedDigraph, a random source," +, "a positive integer, and a float"), [IsStronglyConnectedDigraph, IsRandomSource, IsPosInt, IsFloat], function(_, rs, n, p) local d, adjMatrix, stronglyConnectedComponents, @@ -1897,7 +1899,7 @@ function(_, rs, n, p) return DigraphByAdjacencyMatrix(adjMatrix); end); -InstallMethod(RandomDigraphCons, +InstallMethod(RandomDigraphCons, "for IsStronglyConnectedDigraph, a positive integer, and a float", [IsStronglyConnectedDigraph, IsPosInt, IsFloat], function(_, n, p) @@ -2067,7 +2069,6 @@ function(_, n, p) return DigraphNC(adjacencyList); end); - InstallMethod(RandomDigraphCons, "for IsImmutableDigraph, a random source, a positive integer, and a float", [IsImmutableDigraph, IsRandomSource, IsPosInt, IsFloat], diff --git a/gap/weights.gi b/gap/weights.gi index d93544467..dc1081fcf 100644 --- a/gap/weights.gi +++ b/gap/weights.gi @@ -879,15 +879,15 @@ end); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a pos int", [IsPosInt], -n -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, GlobalMersenneTwister, n)); +n -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n)); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a pos int and a float", [IsPosInt, IsFloat], -{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, GlobalMersenneTwister, n, p)); +{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n, p)); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a pos int and a rational", [IsPosInt, IsRat], -{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, GlobalMersenneTwister, n, p)); +{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n, p)); InstallMethod(RandomUniqueEdgeWeightedDigraph, "for a function and a pos int", [IsFunction, IsPosInt], diff --git a/tst/standard/digraph.tst b/tst/standard/digraph.tst index bd9989364..16c1d8e28 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -2088,44 +2088,40 @@ gap> graph := RandomDigraph(IsEulerianDigraph, 10);; > Print("False"); > fi; -# -gap> DIGRAPHS_StopTest(); -gap> STOP_TEST("Digraphs package: standard/digraph.tst", 0); - #Random Digraph with Random Source gap> graph := RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsMutableDigraph(graph) then - Print("False"); - fi; +> if not IsMutableDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsImmutableDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsImmutableDigraph(graph) then - Print("False"); - fi; +> if not IsImmutableDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsHamiltonianDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsHamiltonianDigraph(graph) then - Print("False"); - fi; +> if not IsHamiltonianDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsEulerianDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsEulerianDigraph(graph) then - Print("False"); - fi; +> if not IsEulerianDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsConnectedDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsConnectedDigraph(graph) then - Print("False"); - fi; +> if not IsConnectedDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsStronglyConnectedDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsStronglyConnectedDigraph(graph) then - Print("False"); - fi; +> if not IsStronglyConnectedDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsAcyclicDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsAcyclicDigraph(graph) then - Print("False"); - fi;gap> +> if not IsAcyclicDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsSymmetricDigraph, GlobalMersenneTwister, 10, 0.5);; - if not IsSymmetricDigraph(graph) then - Print("False"); - fi; +> if not IsSymmetricDigraph(graph) then +> Print("False"); +> fi; gap> Reset(GlobalMersenneTwister);; gap> graph := RandomDigraph(GlobalMersenneTwister, 10, 0.5); @@ -2149,4 +2145,8 @@ gap> Reset(GlobalMersenneTwister);; gap> graph2 := RandomDigraph(IsHamiltonianDigraph, 10); gap> graph = graph2; -true \ No newline at end of file +true + +# +gap> DIGRAPHS_StopTest(); +gap> STOP_TEST("Digraphs package: standard/digraph.tst", 0); \ No newline at end of file From a5ad80583a8a0322757f7ddd64e9b18d50a9ce51 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sun, 22 Nov 2026 10:53:24 +0000 Subject: [PATCH 10/15] Removing trailing whitespace --- gap/digraph.gi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gap/digraph.gi b/gap/digraph.gi index 65ea6cbce..45f385115 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1855,7 +1855,7 @@ function(_, n, p) # Once the tree has been created, we fill out the rest of the graph with # random edges according to p - adjacencyList := + adjacencyList := DIGRAPHS_FillOutGraph(GlobalMersenneTwister, n, p, adjacencyList); return DigraphNC(adjacencyList); end); From 337615ca249cdca04e595617c724a6041e0d8145 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sun, 22 Nov 2026 11:01:19 +0000 Subject: [PATCH 11/15] Trying to get last test to work --- tst/standard/digraph.tst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tst/standard/digraph.tst b/tst/standard/digraph.tst index 16c1d8e28..99feab278 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -2063,6 +2063,10 @@ gap> graph := RandomDigraph(IsAcyclicDigraph, 10);; > fi; # Random Eulerian Digraph +gap> graph := RandomDigraph(IsEulerianDigraph, 10);; +> if not IsEulerianDigraph(graph) then +> Print("False"); +> fi; gap> graph := RandomDigraph(IsEulerianDigraph, 1, 0);; > if DigraphNrEdges(graph) <> 0 then > Print("False"); @@ -2083,10 +2087,6 @@ gap> for p in [1 .. 9] do > Print("False"); > fi; > od; -gap> graph := RandomDigraph(IsEulerianDigraph, 10);; -> if not IsEulerianDigraph(graph) then -> Print("False"); -> fi; #Random Digraph with Random Source From 1a4e1e7fa9f296bde577d36e9c770e55d9d1e93a Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sun, 22 Nov 2026 11:27:17 +0000 Subject: [PATCH 12/15] removing empty line in test file --- tst/standard/digraph.tst | 1 - 1 file changed, 1 deletion(-) diff --git a/tst/standard/digraph.tst b/tst/standard/digraph.tst index 99feab278..7c8199067 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -2089,7 +2089,6 @@ gap> for p in [1 .. 9] do > od; #Random Digraph with Random Source - gap> graph := RandomDigraph(IsMutableDigraph, GlobalMersenneTwister, 10, 0.5);; > if not IsMutableDigraph(graph) then > Print("False"); From 829fc4c1b2711d9a70205bcf25cf7c2732549f1e Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sun, 22 Nov 2026 12:52:13 +0000 Subject: [PATCH 13/15] fixing documentation error --- doc/digraph.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/digraph.xml b/doc/digraph.xml index 94f7f0627..a2fb86c52 100644 --- a/doc/digraph.xml +++ b/doc/digraph.xml @@ -788,7 +788,7 @@ gap> List(D, Size); For and , edges are added between any pairs of vertices with probability approximately p.

- If the optional second argument rs is a random source (see ) then it + If the optional second argument rs is a random source (see ) then it is used to generate the random choices made when creating the digraph. If rs is not specified, then the global random source GlobalMersenneTwister is used. Two calls with the same arguments and random sources in the same state return the same digraph, which makes it possible to reproduce a random digraph by resetting the From 0fc1148c0eebf93cdba38122287adca18cd7b4d8 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sun, 22 Nov 2026 12:58:01 +0000 Subject: [PATCH 14/15] Fixing documentation syntax --- doc/digraph.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/digraph.xml b/doc/digraph.xml index a2fb86c52..d493f9b07 100644 --- a/doc/digraph.xml +++ b/doc/digraph.xml @@ -788,7 +788,7 @@ gap> List(D, Size); For and , edges are added between any pairs of vertices with probability approximately p.

- If the optional second argument rs is a random source (see ) then it + If the optional second argument rs is a random source (see ) then it is used to generate the random choices made when creating the digraph. If rs is not specified, then the global random source GlobalMersenneTwister is used. Two calls with the same arguments and random sources in the same state return the same digraph, which makes it possible to reproduce a random digraph by resetting the From 0dc54b24d1a63bebf035f5aa9bfa65263c0e2f28 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Sun, 22 Nov 2026 13:01:34 +0000 Subject: [PATCH 15/15] removing comma in xml file --- doc/digraph.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/digraph.xml b/doc/digraph.xml index d493f9b07..603f544e1 100644 --- a/doc/digraph.xml +++ b/doc/digraph.xml @@ -788,7 +788,7 @@ gap> List(D, Size); For and , edges are added between any pairs of vertices with probability approximately p.

- If the optional second argument rs is a random source (see ) then it + If the optional second argument rs is a random source (see ) then it is used to generate the random choices made when creating the digraph. If rs is not specified, then the global random source GlobalMersenneTwister is used. Two calls with the same arguments and random sources in the same state return the same digraph, which makes it possible to reproduce a random digraph by resetting the