-
Notifications
You must be signed in to change notification settings - Fork 47
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
UnweightedBellmanFord #523
base: main
Are you sure you want to change the base?
Changes from all commits
ffdf33d
75795bf
367e18e
f22c463
620d5f3
bb363ed
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 |
---|---|---|
|
@@ -902,6 +902,37 @@ gap> DigraphShortestDistances(D); | |
</ManSection> | ||
<#/GAPDoc> | ||
|
||
<#GAPDoc Label="UnweightedBellmanFord"> | ||
<ManSection> | ||
<Attr Name="UnweightedBellmanFord" Arg="digraph","source"/> | ||
<Returns>A list of integers or <K>fail</K>.</Returns> | ||
<Description> | ||
If <A>digraph</A> is a digraph with <M>n</M> vertices, then this | ||
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. and |
||
function returns a list with two sublists of <M>n</M> entries, where each entry is | ||
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. "...a list |
||
either a non-negative integer, or <K>fail</K>. <P/> | ||
|
||
If there is a directed path from <A>source</A> to vertex <C>i</C>, then for each i-th entry the first sublist contains | ||
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.
|
||
the length of the shortest directed path to that i-th vertex and second sublist contains the vertex preceding that i-th | ||
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. This could be clearer - maybe something like "... vertex |
||
vertex. If no such directed path exists, then the value of i is <C>fail</C>. | ||
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. "the value of |
||
We use the convention that the distance from every vertex to | ||
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. "any" rather than "every", and no need for "for all vertices..." |
||
itself is <C>0</C> for all vertices <C>i</C>. | ||
<P/> | ||
|
||
<Example><![CDATA[ | ||
gap> D := Digraph([[1, 2], [3], [1, 2], [4]]); | ||
<immutable digraph with 4 vertices, 6 edges> | ||
gap> UnweightedBellmanFord(D, 2) | ||
[ [ 2, 0, 1, fail ], [ 3, fail, 2, fail ] ] | ||
gap> D := CycleDigraph(IsMutableDigraph, 3); | ||
<mutable digraph with 3 vertices, 3 edges> | ||
gap> UnweightedBellmanFord(D, 3); | ||
[ [ 1, 2, 0 ], [ 3, 1, fail ] ] | ||
]]></Example> | ||
</Description> | ||
</ManSection> | ||
<#/GAPDoc> | ||
|
||
|
||
<#GAPDoc Label="DigraphDiameter"> | ||
<ManSection> | ||
<Attr Name="DigraphDiameter" Arg="digraph"/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -700,6 +700,52 @@ end); | |
# returns the vertices (i.e. numbers) of <D> ordered so that there are no | ||
# edges from <out[j]> to <out[i]> for all <i> greater than <j>. | ||
|
||
InstallMethod(UnweightedBellmanFord, "for a digraph by out-neighbours", | ||
[IsDigraph, IsPosInt], | ||
function(digraph, source) | ||
local distance, n, predecessor, i, inf, u, v, edge, w; | ||
n := DigraphNrVertices(digraph); | ||
# wouldn't work for weighted digraphs | ||
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. This comment isn't necessary - the name of the function is enough. |
||
inf := n + 1; | ||
distance := List([1 .. n], x -> 0); | ||
predecessor := List([1 .. n], x -> 0); | ||
for i in DigraphVertices(digraph) do | ||
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. This loop is unnecessary - you should just do Better would be to use the descriptive function |
||
distance[i] := inf; | ||
predecessor[i] := 0; | ||
od; | ||
distance[source] := 0; | ||
for i in [1 .. n - 1] do | ||
for edge in DigraphEdges(digraph) do | ||
u := edge[1]; | ||
v := edge[2]; | ||
# only works for unweighted graphs, w needs to be changed into a variable | ||
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. But for this function which is only supposed to do the unweighted version, is there any reason to have this comment or use the variable |
||
w := 1; | ||
if distance[u] + w < distance[v] then | ||
distance[v] := distance[u] + w; | ||
predecessor[v] := u; | ||
fi; | ||
od; | ||
od; | ||
for edge in DigraphEdges(digraph) do | ||
u := edge[1]; | ||
v := edge[2]; | ||
# only works for unweighted graphs, w needs to be changed into a variable | ||
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. As above. |
||
w := 1; | ||
if distance[u] + w < distance[v] then | ||
Print("Graph contains a negative-weight cycle"); | ||
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. We almost never want our functions to |
||
fi; | ||
od; | ||
for i in DigraphVertices(digraph) do | ||
if distance[i] >= inf then | ||
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. Can this value really ever be greater than |
||
distance[i] := fail; | ||
fi; | ||
if predecessor[i] = 0 then | ||
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. I would be tempted to use
|
||
predecessor[i] := fail; | ||
fi; | ||
od; | ||
return [distance, predecessor]; | ||
end); | ||
|
||
InstallMethod(DigraphTopologicalSort, "for a digraph by out-neighbours", | ||
[IsDigraphByOutNeighboursRep], | ||
D -> DIGRAPH_TOPO_SORT(OutNeighbours(D))); | ||
|
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.
This is not what the rest of the documentation claims as the return value