Skip to content

Commit

Permalink
weights.gi: correct whitespace (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtorpey authored May 29, 2024
1 parent cf1353a commit 71077ba
Showing 1 changed file with 84 additions and 90 deletions.
174 changes: 84 additions & 90 deletions gap/weights.gi
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,47 @@
InstallGlobalFunction(EdgeWeightedDigraph,
function(digraph, weights)
local digraphVertices, nrVertices, u, outNeighbours,
outNeighbourWeights, idx;

outNeighbourWeights, idx;
if IsDigraph(digraph) then
digraph := DigraphCopy(digraph);
digraph := DigraphCopy(digraph);
else
digraph := Digraph(digraph);
digraph := Digraph(digraph);
fi;

# check all elements of weights is a list
if not ForAll(weights, IsListOrCollection) then
ErrorNoReturn("the 2nd argument (list) must be a list of lists,");
ErrorNoReturn("the 2nd argument (list) must be a list of lists,");
fi;

digraphVertices := DigraphVertices(digraph);

nrVertices := Size(digraphVertices);

# check number there is an edge weight list for vertex u
if nrVertices <> Size(weights) then
ErrorNoReturn("the number of out neighbours and weights must be equal,");
ErrorNoReturn("the number of out neighbours and weights must be equal,");
fi;

# check all elements of weights is a list and size/shape is correct
for u in digraphVertices do
outNeighbours := OutNeighbors(digraph)[u];

outNeighbourWeights := weights[u];
outNeighbours := OutNeighbors(digraph)[u];
outNeighbourWeights := weights[u];

# check number of out neighbours for u
# and number of weights given is the same
if Size(outNeighbours) <> Size(outNeighbourWeights) then
ErrorNoReturn("the sizes of the out neighbours and weights for vertex ",
u, " must be equal,");
fi;

# check number of out neighbours for u
# and number of weights given is the same
if Size(outNeighbours) <> Size(outNeighbourWeights) then
ErrorNoReturn(
"the sizes of the out neighbours and weights for vertex ",
u, " must be equal,");
# check all elements of out neighbours are appropriate
for idx in [1 .. Size(outNeighbours)] do
if not (IsInt(outNeighbourWeights[idx])
or IsFloat(outNeighbourWeights[idx])
or IsRat(outNeighbourWeights[idx])) then
ErrorNoReturn("out neighbour weight must be ",
"an integer, float or rational,");
fi;

# check all elements of out neighbours are appropriate
for idx in [1 .. Size(outNeighbours)] do

if not (IsInt(outNeighbourWeights[idx])
or IsFloat(outNeighbourWeights[idx])
or IsRat(outNeighbourWeights[idx])) then
ErrorNoReturn(
"out neighbour weight must be an integer, float or rational,");
fi;
od;
od;
od;

SetEdgeWeights(digraph, weights);
Expand All @@ -71,15 +66,14 @@ InstallMethod(IsNegativeEdgeWeightedDigraph, "for a digraph with edge weights",
[IsDigraph and HasEdgeWeights],
function(digraph)
local weights, u, w;

weights := EdgeWeights(digraph);

for u in weights do
for w in u do
if Float(w) < Float(0) then
return true;
fi;
od;
for w in u do
if Float(w) < Float(0) then
return true;
fi;
od;
od;
return false;
end);
Expand All @@ -105,70 +99,70 @@ InstallMethod(EdgeWeightedDigraphMinimumSpanningTree,
"for a digraph with edge weights",
[IsDigraph and HasEdgeWeights],
function(digraph)
local weights, numberOfVertices, edgeList, u, outNeighbours, idx, v, w, mst,
mstWeights, partition, i, nrEdges, total, node, x, y, out;
local weights, numberOfVertices, edgeList, u, outNeighbours, idx, v, w, mst,
mstWeights, partition, i, nrEdges, total, node, x, y, out;

# check graph is connected
if not IsConnectedDigraph(digraph) then
ErrorNoReturn("the argument <digraph> must be a connected digraph,");
fi;
# check graph is connected
if not IsConnectedDigraph(digraph) then
ErrorNoReturn("the argument <digraph> must be a connected digraph,");
fi;

weights := EdgeWeights(digraph);

# create a list of edges containing u-v
# w: the weight of the edge
# u: the start vertex
# v: the finishing vertex of that edge
numberOfVertices := DigraphNrVertices(digraph);
edgeList := [];
for u in DigraphVertices(digraph) do
outNeighbours := OutNeighboursOfVertex(digraph, u);
for idx in [1 .. Size(outNeighbours)] do
v := outNeighbours[idx]; # the out neighbour
w := weights[u][idx]; # the weight to the out neighbour
Add(edgeList, [w, u, v]);
od;
weights := EdgeWeights(digraph);

# create a list of edges containing u-v
# w: the weight of the edge
# u: the start vertex
# v: the finishing vertex of that edge
numberOfVertices := DigraphNrVertices(digraph);
edgeList := [];
for u in DigraphVertices(digraph) do
outNeighbours := OutNeighboursOfVertex(digraph, u);
for idx in [1 .. Size(outNeighbours)] do
v := outNeighbours[idx]; # the out neighbour
w := weights[u][idx]; # the weight to the out neighbour
Add(edgeList, [w, u, v]);
od;
od;

# sort edge weights by their weight
StableSortBy(edgeList, x -> x[1]);
# sort edge weights by their weight
StableSortBy(edgeList, x -> x[1]);

mst := EmptyPlist(numberOfVertices);
mstWeights := EmptyPlist(numberOfVertices);
mst := EmptyPlist(numberOfVertices);
mstWeights := EmptyPlist(numberOfVertices);

partition := PartitionDS(IsPartitionDS, numberOfVertices);
partition := PartitionDS(IsPartitionDS, numberOfVertices);

for v in [1 .. numberOfVertices] do
Add(mst, []);
Add(mstWeights, []);
od;
for v in [1 .. numberOfVertices] do
Add(mst, []);
Add(mstWeights, []);
od;

i := 1;
nrEdges := 0;
total := 0;
while nrEdges < numberOfVertices - 1 do
node := edgeList[i];

w := node[1];
u := node[2];
v := node[3];

i := i + 1;

x := Representative(partition, u);
y := Representative(partition, v);

# if cycle doesn't exist
if x <> y then
Add(mst[u], v);
Add(mstWeights[u], w);
nrEdges := nrEdges + 1;
total := total + w;
Unite(partition, x, y);
fi;
od;
i := 1;
nrEdges := 0;
total := 0;
while nrEdges < numberOfVertices - 1 do
node := edgeList[i];

w := node[1];
u := node[2];
v := node[3];

i := i + 1;

x := Representative(partition, u);
y := Representative(partition, v);

# if cycle doesn't exist
if x <> y then
Add(mst[u], v);
Add(mstWeights[u], w);
nrEdges := nrEdges + 1;
total := total + w;
Unite(partition, x, y);
fi;
od;

out := EdgeWeightedDigraph(mst, mstWeights);
SetEdgeWeightedDigraphTotalWeight(out, total);
return out;
out := EdgeWeightedDigraph(mst, mstWeights);
SetEdgeWeightedDigraphTotalWeight(out, total);
return out;
end);

0 comments on commit 71077ba

Please sign in to comment.