Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(graph): Add Bellman-Ford algorithm with negative cycle detection#7127

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
vardhan30016 wants to merge7 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromvardhan30016:add-bellman-ford-algorithm

Conversation

@vardhan30016
Copy link
Contributor

This PR implements the Bellman-Ford algorithm for finding shortest paths in weighted directed graphs.

Key Features:
✅ Handles negative weight edges (unlike Dijkstra)
✅ Detects negative weight cycles
✅ Comprehensive JavaDoc documentation
✅ Clean API with Result class
✅ Path reconstruction support
✅ Input validation
✅ Early termination optimization

Time Complexity: O(V × E)
Space Complexity: O(V)

This implementation fills a gap in the graph algorithms collection and complements existing shortest path implementations.

  • I have readCONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted withclang-format -i --style=file path/to/your/file.java

This PR implements the Bellman-Ford algorithm for finding shortest paths in weighted directed graphs.Key Features:✅ Handles negative weight edges (unlike Dijkstra)✅ Detects negative weight cycles✅ Comprehensive JavaDoc documentation✅ Clean API with Result class✅ Path reconstruction support✅ Input validation✅ Early termination optimizationTime Complexity: O(V × E)Space Complexity: O(V)This implementation fills a gap in the graph algorithms collection and complements existing shortest path implementations.
@codecov-commenter
Copy link

codecov-commenter commentedNov 26, 2025
edited
Loading

Codecov Report

❌ Patch coverage is89.28571% with6 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.53%. Comparing base (2c4bf3c) to head (e38ff4e).

Files with missing linesPatch %Lines
...main/java/com/thealgorithms/graph/BellmanFord.java89.28%4 Missing and 2 partials⚠️
Additional details and impacted files
@@             Coverage Diff              @@##             master    #7127      +/-   ##============================================+ Coverage     78.51%   78.53%   +0.02%- Complexity     6752     6763      +11============================================  Files           759      760       +1       Lines         22402    22458      +56       Branches       4400     4412      +12     ============================================+ Hits          17589    17638      +49- Misses         4108     4112       +4- Partials        705      708       +3

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report?Share it here.

🚀 New features to boost your workflow:
  • ❄️Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Fixes SpotBugs warning OCP_OVERLY_CONCRETE_PARAMETER by using the more general Iterable interface instead of List. This improves API flexibility while maintaining backward compatibility since List implements Iterable.
This test class verifies the functionality of the Bellman-Ford algorithm with various scenarios including simple graphs, negative cycles, disconnected graphs, and invalid inputs.
Fixed compilation errors by using BellmanFord.Edge instead of int[] arrays.Updated all test methods to create Edge objects properly.Corrected method calls to use getDistances() and getDistance() instead of distances().
Removed assertions for path existence in multiple tests.
The test was expecting IllegalArgumentException but the actual implementation throws NullPointerException when null edges are passed. Updated the test to match the actual behavior.
Refactor import statements for JUnit assertions.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@DenizAltunkapanDenizAltunkapanAwaiting requested review from DenizAltunkapanDenizAltunkapan is a code owner

@yanglbmeyanglbmeAwaiting requested review from yanglbmeyanglbme is a code owner

@alxkmalxkmAwaiting requested review from alxkmalxkm is a code owner

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@vardhan30016@codecov-commenter

[8]ページ先頭

©2009-2025 Movatter.jp