Skip to content
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

Added Heavy-Light Decomposition (HLD) for Efficient Tree Queries #6169

Merged
merged 13 commits into from
Feb 17, 2025

Conversation

NithinU2802
Copy link
Contributor

Description:
This PR introduces an implementation of Heavy-Light Decomposition (HLD) in Java, enabling efficient path queries (such as maximum, sum, or updates) on a tree using segment trees. HLD reduces query time complexity from O(N) to O(log N) by decomposing the tree into heavy and light chains. Instead of traversing the whole tree (O(N)), we use HLD to jump chains and use segment trees for fast path queries in O(logN).

✅ Features:

  1. Supports path queries (e.g., max in path, sum in path).
  2. Uses segment trees for efficient range queries.
  3. Optimized tree decomposition and traversal.

Example:
Consider the below Tree

       1 (10)
      /   \
   2 (20)  3 (30)
   /   \
4 (40)  5 (50)

Path Queries and Expected Results

  1. Maximum in Path (4 → 5)
    Path: 4 → 2 → 5
    Values along the path: {40, 20, 50}
    Max Value: 50

  2. Maximum in Path (3 → 4)
    Path: 3 → 1 → 2 → 4
    Values along the path: {30, 10, 20, 40}
    Max Value: 40

Note: Edges are added with indices to define the tree's structure, while node values are initialized separately. This separation allows for flexible updates and queries without altering the tree's topology, ensuring greater efficiency in handling updates and queries.

I have a strong passion for algorithms, particularly their efficiency, and am eager to contribute to this project. Let me know if any changes are needed! 🚀😊

  • 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 clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented Feb 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.12%. Comparing base (63ce6b8) to head (da15d6d).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6169      +/-   ##
============================================
+ Coverage     73.97%   74.12%   +0.14%     
- Complexity     5157     5191      +34     
============================================
  Files           662      663       +1     
  Lines         17681    17771      +90     
  Branches       3407     3422      +15     
============================================
+ Hits          13079    13172      +93     
+ Misses         4097     4096       -1     
+ Partials        505      503       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

The code looks good, please fix PR checks so that I could merge it

@NithinU2802 NithinU2802 requested a review from siriak February 17, 2025 14:53
Copy link
Member

@siriak siriak 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, thanks a lot for adding this!

@siriak siriak merged commit 5be5e35 into TheAlgorithms:master Feb 17, 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.

3 participants