From 1004ddbfcd7d1bde51774245ff7e69ac5a46fee0 Mon Sep 17 00:00:00 2001 From: Heloise Conolly Date: Wed, 4 Nov 2026 12:10:22 +0000 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 4/5] 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 5/5] 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