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

[joss] Dijsktra functions do not throw error when encountering negative weights #525

Closed
szhorvat opened this issue Dec 5, 2021 · 1 comment · Fixed by #530
Closed

[joss] Dijsktra functions do not throw error when encountering negative weights #525

szhorvat opened this issue Dec 5, 2021 · 1 comment · Fixed by #530
Labels
bug Something isn't working
Milestone

Comments

@szhorvat
Copy link

szhorvat commented Dec 5, 2021

Functions that implement Dijsktra's algorithm should throw an error when encountering negative weights. Dijkstra's algorithm does not work with negative weights. Example:

g = rx.PyDiGraph()
g.extend_from_weighted_edge_list([(0,1, 3), (2,0, 2), (1,2, -2), (1,0, 4)])
print(rx.all_pairs_dijkstra_path_lengths(g, edge_cost_fn=lambda c: c))
AllPairsPathLengthMapping{1: PathLengthMapping{0: 0, 2: -2}, 2: PathLengthMapping{0: 2, 1: 5}, 0: PathLengthMapping{2: 1, 1: 3}}

The distance from 0 to 1 is returned as 3, but in fact it is 0. Thus the function is giving incorrect results without warnings.

There are other algorithms which can handle negative weights when computing graph distances, assuming the graph has no cycles along which the weights add up to negative numbers. But Dijkstra's can't.

@szhorvat
Copy link
Author

szhorvat commented Dec 5, 2021

For reference, here's a drawing of this graph, produced with another system:

image

I was unable to get a good drawing of this graph with mpl_draw() because it cannot handle multiple edges that connect the same vertices, even when the two edges are directed and running in opposite directions (i.e. not a multigraph). While it is possible to use `connectionstyle='arc3,rad=0.2', as in the documentation, to separate the edges, the labels will still be drawn on top of each other.

graphviz_draw(g) also seems to misrender this graph. I get this:

image

@mtreinish mtreinish added the bug Something isn't working label Dec 6, 2021
@mtreinish mtreinish added this to the 0.11.0 milestone Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants