Added Johnson's algorithm for all-pairs shortest paths#13340
Added Johnson's algorithm for all-pairs shortest paths#13340sangampaudel530 wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
|
Hi @cclauss @MaximSmolskiy Could you review this pr and give some feedback. Could you please merge this after reviewing it ? |
|
Hi @cclauss , I wanted to kindly ask if there’s any feedback or required changes for this PR. |
There was a problem hiding this comment.
Pull request overview
This PR adds a new graph algorithm implementation for Johnson’s all-pairs shortest paths, including support for negative edge weights and negative-cycle detection, plus pytest coverage.
Changes:
- Adds
graphs/johnson.pywith Bellman-Ford potentials and repeated Dijkstra runs. - Adds
graphs/tests/test_johnson.pycovering a basic graph and a negative-cycle case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
graphs/johnson.py |
Implements Johnson’s algorithm and helper routines. |
graphs/tests/test_johnson.py |
Adds tests for successful shortest-path computation and negative-cycle rejection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| new_dist = d_u + w_prime | ||
| if new_dist < dist[v]: | ||
| dist[v] = new_dist | ||
| heapq.heappush(heap, (new_dist, v)) |
| def johnson(graph: adjacency) -> dict[Node, dict[Node, float]]: | ||
| """ | ||
| Compute all-pairs shortest paths using Johnson's algorithm. | ||
|
|
| assert math.isclose(dist[0][3], 2.0, abs_tol=1e-9) | ||
| assert math.isclose(dist[3][2], -5.0, abs_tol=1e-9) | ||
|
|
||
|
|
| Example: | ||
| >>> g = { | ||
| ... 0: [(1, 3), (2, 8), (4, -4)], | ||
| ... 1: [(3, 1), (4, 7)], | ||
| ... 2: [(1, 4)], |
| Compute all-pairs shortest paths using Johnson's algorithm. | ||
|
|
||
| Args: | ||
| graph: adjacency list {u: [(v, weight), ...], ...} |
Describe your change:
This PR adds Johnson's algorithm for computing all-pairs shortest paths in a weighted graph. The implementation handles graphs with negative edge weights (but no negative cycles) by combining Bellman-Ford and Dijkstra algorithms. A corresponding test file
test_johnson.pyis also included to validate correctness.Checklist: