Skip to content

Commit c8f001c

Browse files
authored
Edge weights #6: RandomUniqueEdgeWeightedDigraph (#755)
* Add RandomUniqueEdgeWeightedDigraph (contributed by @RaiyanC) * Tweak random edge-weighted algorithm * Final tweaks to RandomEdgeWeightedDigraph
1 parent 32fd72d commit c8f001c

5 files changed

Lines changed: 121 additions & 1 deletion

File tree

doc/weights.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,38 @@ gap> Sum(flow[1]);
271271
</Description>
272272
</ManSection>
273273
<#/GAPDoc>
274+
275+
<#GAPDoc Label="RandomUniqueEdgeWeightedDigraph">
276+
<ManSection>
277+
<Oper Name="RandomUniqueEdgeWeightedDigraph" Arg="[filt, ]n[, p]"/>
278+
<Returns>An edge-weighted digraph.</Returns>
279+
<Description>
280+
This operation returns a random edge-weighted digraph.<P/>
281+
282+
Its behaviour is the same as that of <Ref Oper="RandomDigraph"/> but the
283+
returned digraph will additionally have the <Ref Attr="EdgeWeights"/>
284+
attribute populated with random unique weights from the set
285+
<C>[1 .. m]</C> where <C>m</C> is the number of edges in the digraph.<P/>
286+
287+
&STANDARD_FILT_TEXT;
288+
289+
If <A>n</A> is a non-negative integer, then the returned digraph will have
290+
<A>n</A> vertices. If the optional second argument <A>p</A> is a float with
291+
value <M>0 \leq </M> <A> p </A> <M> \leq 1</M>, then an edge will exist
292+
between each pair of vertices with probability approximately <A>p</A>. If
293+
<A>p</A> is not specified, then a random probability will be assumed (chosen
294+
with uniform probability).<P/>
295+
296+
For more information on the arguments and behaviour of this operation, see
297+
<Ref Oper="RandomDigraph"/>.
298+
299+
<Log><![CDATA[
300+
gap> RandomUniqueEdgeWeightedDigraph(5);
301+
<immutable digraph with 5 vertices, 21 edges>
302+
gap> RandomUniqueEdgeWeightedDigraph(5, 1 / 2);
303+
<immutable digraph with 5 vertices, 14 edges>
304+
gap> RandomUniqueEdgeWeightedDigraph(IsEulerianDigraph, 5, 1 / 3);
305+
<immutable digraph with 5 vertices, 6 edges>]]></Log>
306+
</Description>
307+
</ManSection>
308+
<#/GAPDoc>

doc/z-chap5.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<#Include Label="EdgeWeightedDigraphShortestPaths">
3333
<#Include Label="EdgeWeightedDigraphShortestPath">
3434
<#Include Label="DigraphMaximumFlow">
35+
<#Include Label="RandomUniqueEdgeWeightedDigraph">
3536
</Section>
3637

3738
<Section><Heading>Orders</Heading>

gap/weights.gd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ DeclareGlobalFunction("DIGRAPHS_Edge_Weighted_Dijkstra");
3838
# 5. Maximum Flow
3939
DeclareOperation("DigraphMaximumFlow",
4040
[IsDigraph and HasEdgeWeights, IsPosInt, IsPosInt]);
41+
42+
# 6. Random edge weighted digraphs
43+
DeclareOperation("RandomUniqueEdgeWeightedDigraph", [IsPosInt]);
44+
DeclareOperation("RandomUniqueEdgeWeightedDigraph", [IsPosInt, IsFloat]);
45+
DeclareOperation("RandomUniqueEdgeWeightedDigraph", [IsPosInt, IsRat]);
46+
DeclareOperation("RandomUniqueEdgeWeightedDigraph", [IsFunction, IsPosInt]);
47+
DeclareOperation("RandomUniqueEdgeWeightedDigraph",
48+
[IsFunction, IsPosInt, IsFloat]);
49+
DeclareOperation("RandomUniqueEdgeWeightedDigraph",
50+
[IsFunction, IsPosInt, IsRat]);

gap/weights.gi

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,46 @@ function(digraph, start, destination)
780780

781781
return flows;
782782
end);
783+
784+
#############################################################################
785+
# 6. Random edge weighted digraphs
786+
#############################################################################
787+
788+
BindGlobal("DIGRAPHS_RandomEdgeWeightedDigraphFilt",
789+
function(arg...)
790+
local digraph, outs, weightsSource, weights;
791+
# Create random digraph
792+
digraph := CallFuncList(RandomDigraphCons, arg);
793+
outs := OutNeighbours(digraph);
794+
795+
# Unique weights are taken randomly from [1..nredges]
796+
weightsSource := Shuffle([1 .. DigraphNrEdges(digraph)]);
797+
weights := List(DigraphVertices(digraph),
798+
u -> List(outs[u], _ -> Remove(weightsSource)));
799+
800+
return EdgeWeightedDigraph(digraph, weights);
801+
end);
802+
803+
InstallMethod(RandomUniqueEdgeWeightedDigraph,
804+
"for a pos int", [IsPosInt],
805+
n -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n));
806+
807+
InstallMethod(RandomUniqueEdgeWeightedDigraph,
808+
"for a pos int and a float", [IsPosInt, IsFloat],
809+
{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n, p));
810+
811+
InstallMethod(RandomUniqueEdgeWeightedDigraph,
812+
"for a pos int and a rational", [IsPosInt, IsRat],
813+
{n, p} -> RandomUniqueEdgeWeightedDigraph(IsImmutableDigraph, n, p));
814+
815+
InstallMethod(RandomUniqueEdgeWeightedDigraph,
816+
"for a function and a pos int", [IsFunction, IsPosInt],
817+
DIGRAPHS_RandomEdgeWeightedDigraphFilt);
818+
819+
InstallMethod(RandomUniqueEdgeWeightedDigraph,
820+
"for a function, a pos int, and a float", [IsFunction, IsPosInt, IsFloat],
821+
DIGRAPHS_RandomEdgeWeightedDigraphFilt);
822+
823+
InstallMethod(RandomUniqueEdgeWeightedDigraph,
824+
"for a function, a pos int, and a rational", [IsFunction, IsPosInt, IsRat],
825+
DIGRAPHS_RandomEdgeWeightedDigraphFilt);

tst/standard/weights.tst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ gap> DIGRAPHS_StartTest();
1919
# 1. Edge Weights
2020
#############################################################################
2121

22-
# create edge weighted digraph
22+
# create edge-weighted digraph
2323
gap> d := EdgeWeightedDigraph([[2], []], [[5], []]);
2424
<immutable digraph with 2 vertices, 1 edge>
2525
gap> d := EdgeWeightedDigraph(Digraph([[2], []]), [[5], []]);
@@ -368,6 +368,37 @@ gap> gr := EdgeWeightedDigraph([[2], [3, 6], [4], [1, 6], [1, 3], []],
368368
gap> DigraphMaximumFlow(gr, 5, 6);
369369
[ [ 10 ], [ 3, 7 ], [ 7 ], [ 0, 7 ], [ 10, 4 ], [ ] ]
370370
371+
#############################################################################
372+
# 6. Random edge-weighted digraphs
373+
#############################################################################
374+
375+
# Random edge-weighted digraph with n
376+
gap> D := RandomUniqueEdgeWeightedDigraph(5);;
377+
gap> DigraphNrVertices(D);
378+
5
379+
gap> HasEdgeWeights(D);
380+
true
381+
382+
# Random edge-weighted digraph with n, p
383+
gap> D := RandomUniqueEdgeWeightedDigraph(10, 1 / 2);;
384+
gap> DigraphNrVertices(D);
385+
10
386+
gap> HasEdgeWeights(D);
387+
true
388+
gap> SortedList(Flat(EdgeWeights(D))) = [1 .. DigraphNrEdges(D)];
389+
true
390+
391+
# Random edge-weighted digraph with filt, n, p
392+
gap> D := RandomUniqueEdgeWeightedDigraph(IsEulerianDigraph, 5, 0.3);;
393+
gap> DigraphNrVertices(D);
394+
5
395+
gap> HasEdgeWeights(D);
396+
true
397+
gap> IsEulerianDigraph(D);
398+
true
399+
gap> SortedList(Flat(EdgeWeights(D))) = [1 .. DigraphNrEdges(D)];
400+
true
401+
371402
# DIGRAPHS_UnbindVariables
372403
gap> Unbind(d);
373404
gap> Unbind(tree);

0 commit comments

Comments
 (0)