diff --git a/doc/digraph.xml b/doc/digraph.xml index 05e578e11..3ad4c0f33 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 @@ -803,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.gd b/gap/digraph.gd index 46cb83d79..c2b4691b2 100644 --- a/gap/digraph.gd +++ b/gap/digraph.gd @@ -128,17 +128,32 @@ 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", [IsFunction, IsInt]); +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]); +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 337bfef4a..ad4faec40 100644 --- a/gap/digraph.gi +++ b/gap/digraph.gi @@ -1395,7 +1395,29 @@ 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,31 +1428,34 @@ 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))); + 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, ", an integer, and a rational"), - [EvalString(filter), IsInt, IsRat], - {f, n, p} -> RandomDigraphCons(f, n, Float(p))); + 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 IsMutableDigraph, a positive integer, and a float", -[IsMutableDigraph, IsPosInt, IsFloat], -function(_, n, p) +"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, RANDOM_DIGRAPH(n, p)); + return DigraphNC(IsMutableDigraph, RandomDigraphOutNeighbours(rs, 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]; @@ -1439,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; @@ -1450,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); @@ -1465,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; @@ -1482,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; @@ -1491,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; @@ -1514,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; @@ -1542,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)); @@ -1555,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; @@ -1581,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; @@ -1596,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); @@ -1611,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; @@ -1648,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; @@ -1660,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; @@ -1669,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); @@ -1692,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; @@ -1702,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); @@ -1718,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 @@ -1735,32 +1760,55 @@ function(_, n, p) end); InstallMethod(RandomDigraphCons, -"for IsImmutableDigraph, a positive integer, and a float", -[IsImmutableDigraph, IsPosInt, IsFloat], -function(_, n, p) +"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, n, p)); + D := MakeImmutable(RandomDigraphCons(IsMutableDigraph, rs, 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], +{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); +{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); + +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..61642c366 100644 --- a/tst/standard/digraph.tst +++ b/tst/standard/digraph.tst @@ -2091,3 +2091,73 @@ gap> graph := RandomDigraph(IsEulerianDigraph, 10);; # 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