Skip to content

Conversation

@SamXop123
Copy link
Contributor

Yen’s K-shortest loopless paths with tests

Summary

  • What: Implement YensKShortestPaths to compute up to K loopless shortest paths in a directed, non-negative weighted graph.
  • Why: Enables alternative path analysis beyond the single shortest path, complementing existing graph algorithms.
  • How: Yen’s algorithm using Dijkstra to compute spur paths, a candidate min-heap ordered by total cost with deterministic tie-breaking, and loopless constraints.

Changes

  • [new] src/main/java/com/thealgorithms/graph/YensKShortestPaths.java
    • Public API: List<List<Integer>> kShortestPaths(int[][] weights, int src, int dst, int k).
    • Input contract: adjacency matrix with -1 for no edge; otherwise non-negative weights.
    • Validations: square matrix, valid indices, k >= 1, weights >= -1.
  • [new] src/test/java/com/thealgorithms/graph/YensKShortestPathsTest.java
    • Basic small-graph K-paths with deterministic ordering.
    • K larger than available paths.
    • No path returns empty list.
    • Source equals destination returns trivial path.
    • Negative weight rejection (values < -1).
  • [updated] DIRECTORY.md
    • Added YensKShortestPaths under the graph/ section.

Complexity

  • Approximately O(K · (V + E) log V) per spur iteration (depends on Dijkstra and graph density).

How to verify

mvn -q -DskipITs verify
  • Ensures compile, tests, and quality gates (Checkstyle, PMD, SpotBugs).
  • Code formatted via clang-format with --style=file.

References

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work.
  • Filenames are in PascalCase.
  • Functions and variable names follow Java naming conventions.
  • New algorithm has a URL in comments (will add if maintainers request).
  • Code formatted (clang-format) and mvn verify passes locally.

Issue linkage

Notes for reviewers

  • The implementation uses an adjacency matrix for consistency with many graph algorithms in this repo.
  • Tie-breaking is deterministic (cost, then lexicographic on node sequences) to stabilize test outputs.
  • Happy to add an adjacency-list variant in a follow-up if preferred.

@codecov-commenter
Copy link

codecov-commenter commented Oct 13, 2025

Codecov Report

❌ Patch coverage is 73.75000% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.40%. Comparing base (8b8434c) to head (31e8229).

Files with missing lines Patch % Lines
...va/com/thealgorithms/graph/YensKShortestPaths.java 81.42% 13 Missing and 13 partials ⚠️
...rithms/datastructures/bloomfilter/BloomFilter.java 20.00% 8 Missing and 8 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6773      +/-   ##
============================================
- Coverage     77.42%   77.40%   -0.02%     
- Complexity     6208     6240      +32     
============================================
  Files           722      723       +1     
  Lines         20680    20839     +159     
  Branches       4019     4064      +45     
============================================
+ Hits          16012    16131     +119     
- Misses         4055     4075      +20     
- Partials        613      633      +20     

☔ 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.

Copy link
Member

@alxkm alxkm left a comment

Choose a reason for hiding this comment

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

Looks good. Thank you for the contribution.

@alxkm alxkm merged commit 8726d40 into TheAlgorithms:master Oct 13, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Add Yen’s K-shortest loopless paths algorithm to Graphs

3 participants