Skip to content

Conversation

@Raghu0703
Copy link

Description

This PR adds an implementation of Kruskal's Algorithm for finding the Minimum Spanning Tree (MST) in weighted undirected graphs. This addresses the feature request in issue #7067.

Kruskal's algorithm is a fundamental graph algorithm that builds the MST by sorting edges by weight and adding them one by one, using a Union-Find data structure to avoid creating cycles.

Changes Made

  • Added Kruskal.java - Complete implementation with Union-Find optimization
  • Added KruskalTest.java - Comprehensive test suite with 9 test cases covering various scenarios
  • Documentation - Included detailed JavaDoc comments explaining the algorithm
  • Optimizations - Implemented path compression and union by rank for efficiency

Algorithm Details

  • Time Complexity: O(E log E) where E is the number of edges
  • Space Complexity: O(V + E) where V is the number of vertices
  • Key Features:
    • Uses Disjoint Set (Union-Find) for efficient cycle detection
    • Sorts edges by weight using Java's built-in sorting
    • Handles edge cases like disconnected graphs and empty inputs

Testing

All tests pass successfully and cover:

  • ✅ Simple connected graphs
  • ✅ Disconnected components
  • ✅ Complete graphs
  • ✅ Graphs with equal-weight edges
  • ✅ Edge cases (empty graphs, single edges)
  • ✅ Input validation (null checks, invalid vertex counts)

References

Fixes #7067


Checklist

  • I have read CONTRIBUTING.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 with mvn spotless:apply

- Implements LU decomposition using Doolittle's method
- Decomposes square matrix A into L (lower) and U (upper) triangular matrices
- Includes comprehensive test cases with edge case handling
- Time complexity: O(n^3), Space complexity: O(n^2)
- Resolves TheAlgorithms#6833
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 Kruskal’s algorithm

1 participant