3.8.5 · HinglishString Algorithms

Boyer-Moore — bad character, good suffix heuristics

2,130 words10 min readRead in English

3.8.5 · Coding › String Algorithms


Setup & Notation

  • Text jiska length hai, pattern jiska length hai. Hum woh saari positions chahte hain jahan , mein aata hai.
  • Hum ko ke neeche kisi shift par align karte hain, phir vs compare karte hain, leftward chalte hue.
  • Mismatch par (ya full match ke baad) hum ko right ki taraf kuch amount se shift karte hain aur retry karte hain.

Heuristic 1 — Bad Character Rule

SHIFT DERIVE KAISE KARTE HAIN. Humne pattern index par text char ke saath mismatch kiya. Hum ko right move karna chahte hain taaki mein rightmost , position ke neeche align ho.

  • Agar : woh occurrence se left mein hai, toh shift .
  • Agar : mein koi nahi, poora pattern us se aage slide karo: .
  • Agar : use align karna pattern ko peeche move kar dega — illegal. Hum minimum shift tak clamp karte hain.

Heuristic 2 — Good Suffix Rule

ISKO KAISE COMPUTE KARTE HAIN ( array jo mismatch position se index hota hai).

Hum do helper arrays use karte hain. ko standard "Z/border-from-the-right" preprocessing se define karo:

Maano = ke longest suffix ki length jo index par khatam hoti hai aur ka bhi suffix hai:

se hum good-suffix shift array banate hain (shift jab mismatch index par hai, yaani matched suffix length ):

  • Init sabhi ko karo (Case 2 default, phir refine karo).
  • Case 2 (prefix = suffix-of-suffix): jin positions par ka prefix border hai, wahan shifts set karo.
  • Case 1 (internal reoccurrence): har ke liye jahan (yaani ek prefix match karta hai), ya jahan bhi reoccur kare, corresponding shift record karo.
Figure — Boyer-Moore — bad character, good suffix heuristics

Sab kuch ek saath (search loop)


Complexity


Common Mistakes


Flashcards

Boyer-Moore pattern ko right-to-left kyun compare karta hai?
Taaki mismatch ek matched suffix reveal kare, good-suffix rule se bade skips enable ho aur bad text char ko P mein uski rightmost copy ke saath align kar sakein.
last(c) (last-occurrence function) define karo.
Sabse bada index jahan ho, ya agar .
Mismatch index par text char ke saath bad-character shift formula?
.
Bad-character shift mein kyun?
Jab toh raw value ho jaati hai (backward/zero shift) → infinite loop; kam se kam 1 tak clamp karo.
"Good suffix" kya hai?
Pehle se matched suffix jab index par mismatch hoti hai.
Good-suffix rule ke do cases?
(1) Good suffix ki ek aur internal occurrence align karo jiske pehle wala char se alag ho; (2) ka ek prefix align karo jo good suffix ke suffix ke barabar ho.
Mismatch par final BM shift?
.
Dono shifts ka max lena safe kyun hai?
Har rule ek independent valid lower bound hai safe jump ke liye; do safe jumps mein se bada bhi safe hai.
Basic BM search ka best-case aur worst-case time?
Best (sublinear), worst ; Galil rule ise bana deta hai.
Preprocessing cost?
Bad char ke liye , good suffix ke liye .

Recall Feynman: 12-saal ke bachche ko samjhao

Socho ek word stamp ko ek lambi sentence par match karna hai. Letters ko left se right check karne ki jagah, tum stamp ko uske aakhiri letter se peeche ki taraf check karte ho. Jaise hi koi letter match nahi karta, tum do sawaal poochte ho: "Mere stamp mein yeh galat letter aur kahan aata hai?" (slide karo taaki woh line up ho jaaye) aur "Jo part match hua — kya woh mere stamp mein kahin pehle bhi aata hai?" (slide karo taaki woh bhi line up ho jaaye). Phir tum dono mein se bade jump se jump karte ho. Kyunki tum kabhi kabhi sentence ke poore chunks skip kar dete ho bina dekhe, yeh super fast hai.


Connections

  • Knuth-Morris-Pratt (KMP) — borders/prefix-function bhi use karta hai; good-suffix uska mirror image hai (suffix borders).
  • Z-Algorithm — alternative linear preprocessing; suff[] compute karne se related hai.
  • Rabin-Karp — hashing approach; alag tradeoff (avg , koi skipping nahi).
  • Borders and Prefix Function — good-suffix Case 2 ke peeche core theory.
  • Galil Rule — BM worst case ko tak patch karta hai.
  • Aho-Corasick — multi-pattern matching; BM variants (Commentz-Walter) BM ideas extend karte hain.

Concept Map

improved by

compares

enables

uses

uses

text char c

needs

gives

c absent so -1

when last c gt j

takes

feeds

feeds

Naive matching

Boyer-Moore

Right to left

Often sublinear

Bad character rule

Good suffix rule

Mismatch at index j

last c function

shift = max 1, j - last c

Slide past position

Clamp to min shift 1

Largest legal shift