Skip to content
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

Added Dijkstra version for digraph with edges weights #319

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion gap/oper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ DeclareOperation("DigraphDijkstra",
[IsDigraph, IsPosInt]);
DeclareOperation("DigraphDijkstra",
[IsDigraph, IsPosInt, IsPosInt]);

DeclareOperation("DigraphDijkstraSTWeights",
[IsDigraph, IsPosInt, IsPosInt, IsList]);
DeclareOperation("DigraphConnectedComponent", [IsDigraph, IsPosInt]);
DeclareOperation("DigraphStronglyConnectedComponent", [IsDigraph, IsPosInt]);
DeclareOperation("DigraphPath", [IsDigraph, IsPosInt, IsPosInt]);
Expand Down
31 changes: 31 additions & 0 deletions gap/oper.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,37 @@ InstallMethod(DigraphDijkstra, "for a digraph, and a vertex",
[IsDigraph, IsPosInt],
{digraph, source} -> DIGRAPHS_DijkstraST(digraph, source, fail));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{digraph, source} -> DIGRAPHS_DijkstraST(digraph, source, fail))

would become

{digraph, source} -> DIGRAPHS_DijkstraST(digraph, source, fail, {u, v} -> 1))


InstallMethod(DigraphDijkstraSTWeights, "for a digraph, a vertex, a vertex, and a list of weights",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using your new version DigraphDijkstraSTWeights as the code of DIGRAPHS_DijkstraST, with one change: the argument weights could be a function like:

weights := function(u, v)
  return list_of_weights[u][v];
end;

if a list of weights is given, and if no list of weights is given then we could do:

weights := function(u, v)
  return 1;
end;

[IsDigraph, IsPosInt, IsPosInt, IsList],
function(digraph, source, target, weights)
local dist, prev, queue, u, v, alt;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that the weights have the same "shape" as OutNeighbours(digraph)


dist := [];
prev := [];
queue := BinaryHeap({x, y} -> x[1] < y[1]);

for v in DigraphVertices(digraph) do
dist[v] := infinity;
prev[v] := -1;
od;

dist[source] := 0;
Push(queue, [0, source]);

while not IsEmpty(queue) do
u := Pop(queue);
u := u[2];
for v in OutNeighbours(digraph)[u] do
alt := dist[u] + weights[u][v];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would become weights(u, v) instead of weights[u][v]

if alt < dist[v] then
dist[v] := alt; prev[v] := u;
Push(queue, [dist[v], v]);
fi;
od;
od;
return [dist, prev];
end);

InstallMethod(IteratorOfPaths,
"for a digraph by out-neighbours and two pos ints",
[IsDigraphByOutNeighboursRep, IsPosInt, IsPosInt],
Expand Down