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

Add Kruskal's Algorithm implementation #7121

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

Open
RamsaiPolisetti wants to merge 1 commit into TheAlgorithms:master
base: master
Choose a base branch
Loading
from RamsaiPolisetti:add-kruskals-algorithm

Conversation

@RamsaiPolisetti
Copy link

@RamsaiPolisetti RamsaiPolisetti commented Nov 25, 2025

Summary

This PR adds an implementation of Kruskal's Minimum Spanning Tree (MST) algorithm to the graph package.
Kruskal’s algorithm is a classic greedy algorithm that finds the MST of a connected, undirected, weighted graph by selecting edges in increasing order of weight while avoiding cycles.

Implementation Details

  • Added KruskalsAlgorithm.java under src/main/java/com/thealgorithms/graph/
  • Includes:
    • Edge class for representing edges
    • DSU (Disjoint Set Union / Union–Find) for cycle detection
    • kruskalMST() method implementing the core algorithm
  • Clean, modular, and follows project style conventions.

Tests

  • Added a corresponding JUnit test under
    src/test/java/com/thealgorithms/graph/
  • Tests verify:
    • The MST has exactly V - 1 edges.
    • The smallest edges are correctly selected.
    • The algorithm avoids cycles.

All tests pass using ./gradlew test.

Checklist

  • Added implementation
  • Added tests
  • Code compiles successfully
  • Tests pass
  • Follows coding style and package structure

Copy link

codecov-commenter commented Nov 25, 2025
edited
Loading

Codecov Report

❌ Patch coverage is 0% with 59 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.29%. Comparing base (2c4bf3c) to head (8bbb886).
⚠️ Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
...ava/com/thealgorithms/graph/KruskalsAlgorithm.java 0.00% 59 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@
## master #7121 +/- ##
============================================
- Coverage 78.51% 78.29% -0.23% 
+ Complexity 6752 6751 -1 
============================================
 Files 759 760 +1 
 Lines 22402 22461 +59 
 Branches 4400 4410 +10 
============================================
- Hits 17589 17585 -4 
- Misses 4108 4169 +61 
- Partials 705 707 +2 

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@DenizAltunkapan DenizAltunkapan Awaiting requested review from DenizAltunkapan DenizAltunkapan is a code owner

@yanglbme yanglbme Awaiting requested review from yanglbme yanglbme is a code owner

@alxkm alxkm Awaiting requested review from alxkm alxkm 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.

AltStyle によって変換されたページ (->オリジナル) /