Skip to content

Conversation

@Manan-09
Copy link
Owner

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

@Manan-09
Copy link
Owner Author

Here's a review of the pull request:

  1. Summary of code changes
    This PR renames the DisjointSets and Node classes to DisjointSetUnion and Node respectively, and moves them to a new package com.thealgorithms.datastructures.disjointsetunion. It also adds Javadoc comments to the classes and methods, renames methods to follow Java conventions (e.g., MakeSet to makeSet), and includes a new JUnit test class for the DisjointSetUnion functionality.

  2. Potential issues and suggested fixes

    • Regression in findSet optimization: The findSet method in the new DisjointSetUnion.java implementation has removed path compression. Path compression is a crucial optimization for Disjoint Set Union to achieve near-constant amortized time complexity. The Javadoc for findSet incorrectly states that it "uses path compression".
      • Suggestion: Revert the findSet implementation to include path compression, similar to the deleted version (recursive or iterative path compression). Ensure the Javadoc accurately reflects the implementation. For example, using the original recursive approach:
        public Node<T> findSet(Node<T> node) {
            if (node != node.parent) {
                node.parent = findSet(node.parent); // Path compression
            }
            return node.parent;
        }
    • Test brittleness in testUnionFindSet: The assertions directly on nodeX.parent (e.g., Assertions.assertEquals(node1, node1.parent);) are brittle. Parent pointers can change due to path compression during findSet operations, which means the direct parent might not always be the expected root before a findSet operation on that specific node. It's generally better to test the outcome of findSet itself.
      • Suggestion: Remove the assertions that check nodeX.parent directly. Focus on asserting that the roots returned by findSet for all connected nodes are the same, and potentially verify the ranks after unionSets if rank is a public field to test the union-by-rank optimization.
  3. Final verdict
    Needs changes. The regression in findSet's optimization and the brittle test assertions are critical issues that need to be addressed before approval.

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.

3 participants