Skip to content

Fix union-find weight updates#32

Merged
yomichi merged 1 commit intomainfrom
codex/バグを見つけて修正する
Jun 16, 2025

Hidden character warning

The head ref may contain hidden characters: "codex/\u30d0\u30b0\u3092\u898b\u3064\u3051\u3066\u4fee\u6b63\u3059\u308b"
Merged

Fix union-find weight updates#32
yomichi merged 1 commit intomainfrom
codex/バグを見つけて修正する

Conversation

@yomichi
Copy link
Owner

@yomichi yomichi commented Jun 16, 2025

Summary

  • clarify union by weight in unify! docs
  • merge weights into the new root when unifying
  • add regression test for unify!

Testing

  • julia --project=. -e 'using Pkg; Pkg.test()'

https://chatgpt.com/codex/tasks/task_e_684fa53e5fb883278235b3af6fdb0bb9

Summary by CodeRabbit

  • Documentation
    • Clarified the behavior of the union operation in the UnionFind structure to specify union by weight.
  • Bug Fixes
    • Corrected the logic for updating weights during union operations to ensure accurate cluster management.
  • Tests
    • Added comprehensive tests for the UnionFind functionality, verifying union operations, root identification, cluster counting, and weight tracking.
    • Included the new tests in the automated test suite.

@coderabbitai
Copy link

coderabbitai bot commented Jun 16, 2025

Walkthrough

The changes update the union-find data structure to correct the weight update logic in its union operation and clarify its documentation. Additionally, a new test file for union-find is added and integrated into the test suite to verify the correctness of union operations, root identification, and cluster counting.

Changes

File(s) Change Summary
src/model/common/union_find.jl Corrected union-by-weight logic in unify! and updated its documentation comment.
test/runtests.jl Added "union_find.jl" to the list of executed test files.
test/union_find.jl Introduced new tests for the UnionFind structure, covering union, root, and cluster count behavior.

Poem

In the warren, sets unite,
Rabbits hop from left to right.
With weights now swapped just so,
Their clusters merge and grow.
Tests ensure the roots align,
All is well in union time!
🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/model/common/union_find.jl (1)

61-71: Consider zeroing the absorbed root’s weight for safer downstream use

u.weights[r1] is updated correctly, but the weight stored in r2 (now a non-root) is left untouched.
If any downstream logic ever inspects weights[i] without first checking isroot(u,i), the stale size may lead to subtle bugs (e.g. double counting cluster sizes).

         u.parents[r2] = r1
         u.weights[r1] += u.weights[r2]
+        # Optional safety: clear the absorbed root’s weight
+        # so that only true roots carry meaningful sizes.
+        # This has zero algorithmic impact but prevents
+        # accidental misuse elsewhere.
+        # u.weights[r2] = 0

This is a non-functional suggestion; feel free to skip if the invariant “weights are meaningful only for roots” is already well-documented and consistently honoured.

test/runtests.jl (1)

15-16: Guard against duplicate test inclusions

"checkpoint.jl" is added alongside the new "union_find.jl".
If checkpoint.jl was already in the list before this change, it will now run twice, lengthening CI time and potentially producing duplicate output.

Consider de-duplicating:

filenames = unique([
    "observable.jl",
    "classical.jl",
    "quantum.jl",
    "checkpoint.jl",
    "union_find.jl"
])
test/union_find.jl (1)

18-22: Add a check on the absorbed root’s parent to tighten the regression test

After the final unify!, node r34 (the former root of the {3,4} set) should now have parents[r34] == r.
Verifying this guards against regressions where the parent pointer is not updated correctly.

     r = unify!(uf, 1, 3)
     @test r == SpinMonteCarlo.root!(uf, 2) == SpinMonteCarlo.root!(uf, 4)
     @test uf.weights[r] == 4
+    @test uf.parents[r34] == r   # ensure the old root now points to the new root
     @test uf.nclusters == 1
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0b6630b and 7ae78e7.

📒 Files selected for processing (3)
  • src/model/common/union_find.jl (2 hunks)
  • test/runtests.jl (1 hunks)
  • test/union_find.jl (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: tests (macos-latest, 1)
  • GitHub Check: tests (ubuntu-latest, 1)
  • GitHub Check: tests (ubuntu-latest, lts)
  • GitHub Check: tests (windows-latest, 1)
  • GitHub Check: Documentation
  • GitHub Check: code-style

@yomichi yomichi merged commit e953166 into main Jun 16, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant