fix: bad-character shift in Boyer-Moore search has no effect - #15010
Open
bolverk wants to merge 1 commit into
Open
fix: bad-character shift in Boyer-Moore search has no effect#15010bolverk wants to merge 1 commit into
bolverk wants to merge 1 commit into
Conversation
Reassigning the for-loop variable in bad_character_heuristic() does not change Python iteration, so the bad-character shift was dead code and the search ran as brute force. Convert to a while loop so the shift advances the search, and compute the shift in the pattern's coordinate space. The revised algorithm is machine-verified with Dafny (soundness and completeness); it agrees with a brute-force scan on 30k random inputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix bad-character shift having no effect (dead loop variable)
Fixes #14844
Summary
bad_character_heuristic()instrings/boyer_moore_search.pyassigned thecomputed shift to the
for-loop variablei:In Python, reassigning a
for-loop variable inside the loop body has no effecton the iteration sequence, so the shift was dead code and the search silently ran
as a plain O(n·m) scan over every position.
The rewrite uses a
whileloop so the shift actually advances the search, andcomputes the shift in the pattern's coordinate space (the mismatch offset and
the alignment target are both pattern indices), instead of mixing a text index
with a pattern index as the old code did.
Correctness
The new algorithm was first written in Dafny and machine-verified (15 proof
obligations, 0 errors machine-checked by the SMT-based verifier) to establish:
pattern.The key safety invariant is documented in the docstring: a jump maps the
mismatching text position onto a pattern index that carries a character unequal
to it, so no skipped alignment can be a match.
In addition to the proof, the implementation agrees with a brute-force scan on
30,000 randomized inputs (empty text, single-character and repeated patterns).
Impact (the issue's own reproduction)
text = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP",pattern = "MNOP":The existing doctest
BoyerMooreSearch("ABAABA", "AB").bad_character_heuristic()still returns
[0, 3].Checklist
ruff check,ruff format --check, and the doctests