-
Notifications
You must be signed in to change notification settings - Fork 61
Added a helper function and updated the existing methods to preserve edge weights when removing vertices, edges or copying digraphs (issue #683) #877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0c7642c
1af3160
b39a548
469f7a4
f004640
3aa73d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -167,23 +167,41 @@ InstallMethod(DigraphMutableCopy, "for a digraph by out-neighbours", | |||||
| [IsDigraphByOutNeighboursRep], | ||||||
| function(D) | ||||||
| local copy; | ||||||
|
|
||||||
| copy := ConvertToMutableDigraphNC(OutNeighboursMutableCopy(D)); | ||||||
| SetDigraphVertexLabels(copy, StructuralCopy(DigraphVertexLabels(D))); | ||||||
|
|
||||||
| if HaveEdgeLabelsBeenAssigned(D) then | ||||||
| SetDigraphEdgeLabelsNC(copy, StructuralCopy(DigraphEdgeLabelsNC(D))); | ||||||
| fi; | ||||||
|
|
||||||
| if IsImmutableDigraph(D) and HasEdgeWeights(D) then | ||||||
| copy!.edgeweights := EdgeWeightsMutableCopy(D); | ||||||
| elif IsMutableDigraph(D) and IsBound(D!.edgeweights) then | ||||||
| copy!.edgeweights := StructuralCopy(D!.edgeweights); | ||||||
| fi; | ||||||
|
|
||||||
| return copy; | ||||||
| end); | ||||||
|
|
||||||
| InstallMethod(DigraphImmutableCopy, "for a digraph by out-neighbours", | ||||||
| [IsDigraphByOutNeighboursRep], | ||||||
| function(D) | ||||||
| local copy; | ||||||
|
|
||||||
| copy := ConvertToImmutableDigraphNC(OutNeighboursMutableCopy(D)); | ||||||
| SetDigraphVertexLabels(copy, StructuralCopy(DigraphVertexLabels(D))); | ||||||
|
|
||||||
| if HaveEdgeLabelsBeenAssigned(D) then | ||||||
| SetDigraphEdgeLabelsNC(copy, StructuralCopy(DigraphEdgeLabelsNC(D))); | ||||||
| fi; | ||||||
|
|
||||||
| if IsImmutableDigraph(D) and HasEdgeWeights(D) then | ||||||
| SetEdgeWeights(copy, StructuralCopy(EdgeWeights(D))); | ||||||
| elif IsMutableDigraph(D) and IsBound(D!.edgeweights) then | ||||||
| SetEdgeWeights(copy, StructuralCopy(D!.edgeweights)); | ||||||
| fi; | ||||||
|
|
||||||
| return copy; | ||||||
| end); | ||||||
|
|
||||||
|
|
@@ -1837,3 +1855,32 @@ n -> RandomLatticeCons(IsImmutableDigraph, n)); | |||||
|
|
||||||
| InstallMethod(RandomLattice, "for a func and a pos int", [IsFunction, IsPosInt], | ||||||
| RandomLatticeCons); | ||||||
|
|
||||||
| InstallMethod(RemoveDigraphEdgeWeight, "for a mutable digraph, pos int, pos int", | ||||||
| [IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt, IsPosInt], | ||||||
| function(D, v, pos) | ||||||
| if IsBound(D!.edgeweights) and v <= Length(D!.edgeweights) and pos <= Length(D!.edgeweights[v]) then | ||||||
| Remove(D!.edgeweights[v], pos); | ||||||
| fi; | ||||||
| end); | ||||||
|
|
||||||
| InstallMethod(RemoveDigraphEdgeWeight, "for an immutable digraph, pos int, pos int", | ||||||
| [IsImmutableDigraph, IsPosInt, IsPosInt], | ||||||
| function(D, v, pos) | ||||||
| local newD, w; | ||||||
| newD := DigraphMutableCopy(D); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if HasEdgeWeights(D) then | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if v <= Length(newD!.edgeweights) | ||||||
| and pos <= Length(newD!.edgeweights[v]) then | ||||||
| Remove(newD!.edgeweights[v], pos); | ||||||
| fi; | ||||||
| fi; | ||||||
|
|
||||||
| MakeImmutable(newD); | ||||||
| if HasEdgeWeights(D) then | ||||||
| SetEdgeWeights(newD, StructuralCopy(newD!.edgeweights)); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| fi; | ||||||
|
|
||||||
| return newD; | ||||||
| end); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| #@local G, G1, L, TestPartialOrderDigraph | ||
| #@local TestPartialOrderDigraph2, TestUnion, a, adj, b, comps, copy, d, e | ||
| #@local edges, edges2, func, g, gr, gr1, gr2, gr3, gr4, gri, grrt, grt, h, i | ||
| #@local i1, i2, id, idom, in1, in2, in3, iter, j1, j2, m, m1, m2, mat, n, nbs | ||
| #@local i1, i2, id, idom, in1, in2, in3, iter, j1, j2, m, m1, m2, m3, m4, im3, im4, mat, n, nbs | ||
| #@local out, out1, out2, out3, p1, p2, path, preorder, qr, r, res, rtclosure, t | ||
| #@local tclosure, u1, u2, x | ||
| gap> START_TEST("Digraphs package: standard/oper.tst"); | ||
|
|
@@ -3299,6 +3299,46 @@ gap> DigraphEdges(D); | |
| gap> DigraphVertexLabels(D); | ||
| [ 1, 2, 3, 6, [ 4, 5 ] ] | ||
|
|
||
| # Tests for DigraphRemoveVertex, DigraphRemoveVertex (immutable), DigraphRemoveEdge, DigraphRemoveEdge (immutable). | ||
| gap> d := EdgeWeightedDigraph([[2,3],[3],[],[]], [[5,10],[15],[],[]]); | ||
| <immutable digraph with 4 vertices, 3 edges> | ||
| gap> m2 := DigraphMutableCopy(d); | ||
| <mutable digraph with 4 vertices, 3 edges> | ||
| gap> DigraphRemoveEdge(m2, 1, 3); | ||
| <mutable digraph with 4 vertices, 2 edges> | ||
| gap> OutNeighbours(m2); | ||
| [ [ 2 ], [ 3 ], [ ], [ ] ] | ||
| gap> m2!.edgeweights; | ||
| [ [ 5 ], [ 15 ], [ ], [ ] ] | ||
|
Comment on lines
+3311
to
+3312
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a design decision here! Either:
The second approach has been taken in this PR, and we should review whether it's a good idea when we finally merge. |
||
| gap> im3 := DigraphRemoveEdge(d, 1, 3); | ||
| <immutable digraph with 4 vertices, 2 edges> | ||
| gap> IsImmutableDigraph(im3); | ||
| true | ||
| gap> OutNeighbours(im3); | ||
| [ [ 2 ], [ 3 ], [ ], [ ] ] | ||
| gap> EdgeWeights(im3); | ||
| [ [ 5 ], [ 15 ], [ ], [ ] ] | ||
| gap> m4 := DigraphMutableCopy(d); | ||
| <mutable digraph with 4 vertices, 3 edges> | ||
| gap> DigraphRemoveVertex(m4, 1); | ||
| <mutable digraph with 3 vertices, 1 edge> | ||
| gap> OutNeighbours(m4); | ||
| [ [ 2 ], [ ], [ ] ] | ||
| gap> m4!.edgeweights; | ||
| [ [ 15 ], [ ], [ ] ] | ||
| gap> im4 := DigraphRemoveVertex(d, 1); | ||
| <immutable digraph with 3 vertices, 1 edge> | ||
| gap> IsImmutableDigraph(im4); | ||
| true | ||
| gap> OutNeighbours(im4); | ||
| [ [ 2 ], [ ], [ ] ] | ||
| gap> EdgeWeights(im4); | ||
| [ [ 15 ], [ ], [ ] ] | ||
| gap> OutNeighbours(d); | ||
| [ [ 2, 3 ], [ 3 ], [ ], [ ] ] | ||
| gap> EdgeWeights(d); | ||
| [ [ 5, 10 ], [ 15 ], [ ], [ ] ] | ||
|
|
||
| # | ||
| gap> DIGRAPHS_StopTest(); | ||
| gap> STOP_TEST("Digraphs package: standard/oper.tst", 0); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.