Skip to content

Conversation

@prasanth-30011
Copy link

This PR adds an implementation of Kruskal’s Algorithm for computing the Minimum
Spanning Tree (MST) of a weighted undirected graph, addressing Issue #7067.

✔ What’s Included

  • Added KruskalMST.java under com.thealgorithms.greedyalgorithms
  • Added a clean and efficient implementation using:
    • Greedy approach
    • Disjoint Set Union (Union-Find) with path compression
    • Edge class with comparable interface for sorting
  • Added unit tests KruskalMSTTest.java that verify:
    • Correct MST weight for a sample connected graph
    • Correct handling of disconnected graphs (returns -1)

✔ Why This is Useful

  • Kruskal’s Algorithm is a core greedy technique frequently used in:
    • Competitive programming
    • Graph theory education
    • Interview preparation
  • Adds missing MST support to the greedy algorithms library
  • Completes the request made in Issue [FEATURE REQUEST] Add Kruskal’s algorithm #7067

🧪 Tests

All tests pass successfully using:

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 96.49123% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.52%. Comparing base (e6c576c) to head (bf8b2d3).

Files with missing lines Patch % Lines
...com/thealgorithms/greedyalgorithms/KruskalMST.java 95.45% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7088      +/-   ##
============================================
+ Coverage     78.46%   78.52%   +0.05%     
+ Complexity     6746     6744       -2     
============================================
  Files           758      759       +1     
  Lines         22339    22368      +29     
  Branches       4384     4389       +5     
============================================
+ Hits          17528    17564      +36     
+ Misses         4107     4099       -8     
- Partials        704      705       +1     

☔ 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants