Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gap/digraph.gd
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,5 @@ DeclareOperation("RandomLattice", [IsFunction, IsPosInt]);
# in the not-too-distant future!
DeclareOperation("RandomMultiDigraph", [IsPosInt]);
DeclareOperation("RandomMultiDigraph", [IsPosInt, IsPosInt]);

DeclareGlobalFunction("CopyEdgeWeightsForSubdigraph");
30 changes: 30 additions & 0 deletions gap/digraph.gi
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ function(D)
if HaveEdgeLabelsBeenAssigned(D) then
SetDigraphEdgeLabelsNC(copy, StructuralCopy(DigraphEdgeLabelsNC(D)));
fi;

if HasEdgeWeights(D) then
SetEdgeWeights(copy, StructuralCopy(EdgeWeights(D)));
fi;

return copy;
end);

Expand Down Expand Up @@ -1837,3 +1842,28 @@ n -> RandomLatticeCons(IsImmutableDigraph, n));

InstallMethod(RandomLattice, "for a func and a pos int", [IsFunction, IsPosInt],
RandomLatticeCons);

InstallGlobalFunction(CopyEdgeWeightsForSubdigraph,
function(oldDigraph, newDigraph, removedVertices)
local oldWeights, newWeights, i, j, weight, shiftVertices;

Comment thread
devansh2605 marked this conversation as resolved.
Outdated
if not HasEdgeWeights(oldDigraph) then
return;
fi;

oldWeights := EdgeWeights(oldDigraph);
newWeights := [];
for i in [1 .. DigraphNrVertices(newDigraph)] do
newWeights[i] := [];

for j in OutNeighbours(newDigraph, i) do
shiftVertices := function(x)
return x + Length(Filtered(removedVertices, v -> v <= x));
end;
Comment thread
devansh2605 marked this conversation as resolved.
Outdated
weight := oldWeights[shiftVertices(i)][shiftVertices(j)];
Add(newWeights[i], weight);
od;
od;

SetEdgeWeights(newDigraph, newWeights);
end);
29 changes: 26 additions & 3 deletions gap/oper.gi
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ InstallMethod(DigraphRemoveVertex,
"for a mutable digraph by out-neighbours and positive integer",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt],
function(D, u)
local pos, w, v;
local pos, w, v, oldD;
oldD := StructuralCopy(D);
if u > DigraphNrVertices(D) then
return D;
fi;
Expand All @@ -131,17 +132,30 @@ function(D, u)
fi;
od;
od;

if HasEdgeWeights(D) then
CopyEdgeWeightsForSubdigraph(oldD, D, [u]);
fi;

return D;
end);

InstallMethod(DigraphRemoveVertex,
"for an immutable digraph and positive integer",
[IsImmutableDigraph, IsPosInt],
function(D, u)
local newD;
if u > DigraphNrVertices(D) then
return D;
fi;
return MakeImmutable(DigraphRemoveVertex(DigraphMutableCopy(D), u));

newD := DigraphRemoveVertex(DigraphMutableCopy(D), u);
if HasEdgeWeights(D) then
CopyEdgeWeightsForSubdigraph(D, newD, [u]);
fi;
MakeImmutable(newD);

return newD;
end);

InstallMethod(DigraphRemoveVertices, "for a mutable digraph and a list",
Expand Down Expand Up @@ -227,7 +241,7 @@ InstallMethod(DigraphRemoveEdge,
"for a mutable digraph by out-neighbours and two positive integers",
[IsMutableDigraph and IsDigraphByOutNeighboursRep, IsPosInt, IsPosInt],
function(D, src, ran)
local pos;
local pos, weights;
if IsMultiDigraph(D) then
ErrorNoReturn("the 1st argument <D> must be a digraph with no multiple ",
"edges,");
Expand All @@ -243,6 +257,15 @@ function(D, src, ran)
Remove(D!.OutNeighbours[src], pos);
RemoveDigraphEdgeLabel(D, src, pos);
fi;

if HasEdgeWeights(D) then
weights := StructuralCopy(EdgeWeights(D));
if IsBound(weights[src]) and IsBound(weights[src][ran]) then
weights[src][ran] := fail;
fi;
SetEdgeWeights(D, weights);
fi;

return D;
end);

Expand Down
Loading