diff --git a/doc/attr.xml b/doc/attr.xml index 0684df880..a18302d54 100644 --- a/doc/attr.xml +++ b/doc/attr.xml @@ -2091,6 +2091,28 @@ gap> D; <#/GAPDoc> +<#GAPDoc Label="DigraphRemoveAllEdges"> + + + + An empty digraph. + + This operation returns a digraph constructed from digraph. If digraph is mutable, + its list of neighbours will be replaced with an empty 2D list of the same length. +

+ + If digraph is immutable, then a new, empty immutable digraph with the same number of vertices is returned. + + D3 := Digraph(IsMutableDigraph, [[2], [1], [2]]); + +gap> DigraphRemoveAllEdges(D3); + +]]> + + +<#/GAPDoc> + <#GAPDoc Label="DigraphAddAllLoops"> diff --git a/doc/oper.xml b/doc/oper.xml index 92782fed8..16ded8109 100644 --- a/doc/oper.xml +++ b/doc/oper.xml @@ -2291,6 +2291,29 @@ gap> HomomorphicProduct(D1, D2); <#/GAPDoc> +<#GAPDoc Label="SwapDigraphs"> + + + + If D1 and D2 are mutable digraphs, + then SwapDigraphs will swap the contents of + D1 and D2. + + D1 := Digraph(IsMutableDigraph, [[4], [5], [1, 2], [], []]); + +gap> D2 := Digraph(IsMutableDigraph, [[2, 3, 4], [1, 3, 4, 5], [1, 2], [5], [4]]); + +gap> SwapDigraphs(D1, D2); +gap> OutNeighbours(D2); +[ [ 4 ], [ 5 ], [ 1, 2 ], [ ], [ ] ] +gap> OutNeighbours(D1); +[ [ 2, 3, 4 ], [ 1, 3, 4, 5 ], [ 1, 2 ], [ 5 ], [ 4 ] ] +]]> + + +<#/GAPDoc> + <#GAPDoc Label="LexicographicProduct"> diff --git a/gap/attr.gd b/gap/attr.gd index 7afe2359e..106b6e839 100644 --- a/gap/attr.gd +++ b/gap/attr.gd @@ -89,6 +89,7 @@ DeclareAttributeThatReturnsDigraph("DigraphReverse", IsDigraph); DeclareAttributeThatReturnsDigraph("DigraphDual", IsDigraph); DeclareAttributeThatReturnsDigraph("ReducedDigraph", IsDigraph); DeclareAttributeThatReturnsDigraph("DigraphRemoveAllMultipleEdges", IsDigraph); +DeclareAttributeThatReturnsDigraph("DigraphRemoveAllEdges", IsDigraph); # TODO replace all DeclareOperations below to # DeclareAttributeThatReturnsDigraph, and remove the *Attr versions. diff --git a/gap/attr.gi b/gap/attr.gi index ed325892c..04dc306d0 100644 --- a/gap/attr.gi +++ b/gap/attr.gi @@ -2537,6 +2537,19 @@ function(D) return D; end); +InstallMethodThatReturnsDigraph(DigraphRemoveAllEdges, +"for an immutable digraph", +[IsImmutableDigraph], +D -> NullDigraph(DigraphNrVertices(D))); + +InstallMethodThatReturnsDigraph(DigraphRemoveAllEdges, +"for a mutable digraph", +[IsMutableDigraph], +function(D) + D!.OutNeighbours := List(DigraphVertices(D), v -> []); + return D; +end); + InstallMethod(DigraphAddAllLoops, "for a digraph", [IsDigraph], function(D) local C, v; diff --git a/gap/oper.gd b/gap/oper.gd index 619de1db3..5c0e9ed0b 100644 --- a/gap/oper.gd +++ b/gap/oper.gd @@ -59,6 +59,8 @@ DeclareSynonym("DigraphLexicographicProduct", LexicographicProduct); DeclareGlobalFunction("DIGRAPHS_CombinationOperProcessArgs"); DeclareOperation("DIGRAPHS_GraphProduct", [IsDigraph, IsDigraph, IsFunction]); +DeclareOperation("SwapDigraphs", [IsMutableDigraph, IsMutableDigraph]); + # 4. Actions . . . DeclareOperation("OnDigraphs", [IsDigraph, IsPerm]); DeclareOperation("OnDigraphs", [IsDigraph, IsTransformation]); diff --git a/gap/oper.gi b/gap/oper.gi index 13a42a6b0..85b327d40 100644 --- a/gap/oper.gi +++ b/gap/oper.gi @@ -989,6 +989,17 @@ function(D1, D2, edge_function) return Digraph(edges); end); +InstallMethod(SwapDigraphs, +"for two mutable digraphs", +[IsMutableDigraph, IsMutableDigraph], +function(D1, D2) + local nb1, nb2; + nb1 := OutNeighbours(D1); + nb2 := OutNeighbours(D2); + D1!.OutNeighbours := nb2; + D2!.OutNeighbours := nb1; +end); + ############################################################################### # 4. Actions ############################################################################### diff --git a/tst/standard/attr.tst b/tst/standard/attr.tst index 55f047434..74b908532 100644 --- a/tst/standard/attr.tst +++ b/tst/standard/attr.tst @@ -2548,6 +2548,22 @@ true gap> IsChainDigraph(MaximalAntiSymmetricSubdigraph(D)); true +# DigraphRemoveAllEdges: for a digraph +gap> gr := Digraph(IsImmutableDigraph, [[2, 3], [3], [4], []]); + +gap> DigraphRemoveAllEdges(gr); + +gap> gr2 := Digraph(IsMutableDigraph, [[2, 3], [3], [4], []]); + +gap> DigraphRemoveAllEdges(gr2); + +gap> gr3 := Digraph(IsMutableDigraph, [[], [], [], []]); + +gap> DigraphRemoveAllEdges(gr3); + +gap> OutNeighbours(gr3); +[ [ ], [ ], [ ], [ ] ] + # CharacteristicPolynomial gap> gr := Digraph([ > [2, 2, 2], [1, 3, 6, 8, 9, 10], [4, 6, 8], diff --git a/tst/standard/oper.tst b/tst/standard/oper.tst index 29a963e24..48070e624 100644 --- a/tst/standard/oper.tst +++ b/tst/standard/oper.tst @@ -2523,6 +2523,36 @@ gap> OutNeighbours(last); gap> LexicographicProduct(ChainDigraph(3), CycleDigraph(7)); +# SwapDigraphs +gap> D2 := Digraph(IsMutableDigraph, [[4], [5], [1, 2], [], []]); + +gap> D1 := Digraph(IsMutableDigraph, [[2, 3, 4], [1, 3, 4, 5], [1, 2], [5], [4]]); + +gap> SwapDigraphs(D1, D2); +gap> OutNeighbours(D1); +[ [ 4 ], [ 5 ], [ 1, 2 ], [ ], [ ] ] +gap> OutNeighbours(D2); +[ [ 2, 3, 4 ], [ 1, 3, 4, 5 ], [ 1, 2 ], [ 5 ], [ 4 ] ] +gap> D3 := Digraph(IsMutableDigraph, [[2], [1], [2]]); + +gap> SwapDigraphs(D1, D3); +gap> OutNeighbours(D1); +[ [ 2 ], [ 1 ], [ 2 ] ] +gap> OutNeighbours(D3); +[ [ 4 ], [ 5 ], [ 1, 2 ], [ ], [ ] ] +gap> SwapDigraphs(D1, D3); +gap> OutNeighbours(D1); +[ [ 4 ], [ 5 ], [ 1, 2 ], [ ], [ ] ] +gap> OutNeighbours(D3); +[ [ 2 ], [ 1 ], [ 2 ] ] +gap> D2 := Digraph(IsMutableDigraph, [[], [], []]); + +gap> SwapDigraphs(D3, D2); +gap> OutNeighbours(D2); +[ [ 2 ], [ 1 ], [ 2 ] ] +gap> OutNeighbours(D3); +[ [ ], [ ], [ ] ] + # DigraphShortestPathSpanningTree gap> D := Digraph([[2, 3, 4], [1, 3, 4, 5], [1, 2], [5], [4]]); diff --git a/tst/testinstall.tst b/tst/testinstall.tst index 1891f21f9..8b03f22bb 100644 --- a/tst/testinstall.tst +++ b/tst/testinstall.tst @@ -258,6 +258,11 @@ gap> DigraphNrEdges(gr2); gap> DigraphNrAdjacencies(gr2); 21 +# DigraphRemoveAllEdges +gap> gr := Digraph(IsMutableDigraph, [[3], [4], [5], [1, 5], [1, 2]]);; +gap> DigraphRemoveAllEdges(gr); + + # Fix seg fault cause by wrong handling of no edges in # FuncDIGRAPH_SOURCE_RANGE gap> gr := Digraph([[]]); @@ -536,6 +541,15 @@ gap> AutomorphismGroup(D) > = Group([(1, 2, 3), (1, 2), (4, 5, 6), (4, 5), (1, 4)(2, 5)(3, 6)]); true +# SwapDigraphs +gap> C := Digraph(IsMutableDigraph, [[4], [5], [1, 2], [], []]);; +gap> D := Digraph(IsMutableDigraph, [[2, 3, 4], [1, 3, 4, 5], [1, 2], [5], [4]]);; +gap> SwapDigraphs(C, D); +gap> OutNeighbours(D); +[ [ 4 ], [ 5 ], [ 1, 2 ], [ ], [ ] ] +gap> OutNeighbours(C); +[ [ 2, 3, 4 ], [ 1, 3, 4, 5 ], [ 1, 2 ], [ 5 ], [ 4 ] ] + # gap> DIGRAPHS_StopTest(); gap> STOP_TEST("Digraphs package: testinstall.tst", 0);