3.8.6 · D1 · HinglishString Algorithms

FoundationsAho-Corasick — multiple pattern search, automaton

2,982 words14 min read↑ Read in English

3.8.6 · D1 · Coding › String Algorithms › Aho-Corasick — multiple pattern search, automaton

Pehle main note pe chadhne se pehle, tumhe woh vocabulary apni karni hogi jo woh bolti hai. Hum ise ek ek brick se build karte hain.


0 · Raw material — strings, characters, aur alphabet

Yahan sab kuch text ke baare mein hai: symbols ki ek row jo left to right padhi jaati hai.

symbol kyun? Kyunki baad mein, cost formulas count karte hain "ek slot per possible next letter", aur us slots ki sankhya hai hi . Alphabet ko naam dene se hum us cost ke baare mein baat kar sakte hain.

Figure — Aho-Corasick — multiple pattern search, automaton

Figure dekho: ek string bas ek line mein cells hain, har cell mein ek character hai. Cells ke neeche chhote index numbers tab matter karenge jab hum positions ki baat karenge — "match position 4 pe khatam hua".


1 · Prefix aur suffix — woh do halve jo sab kuch chalate hain

Yeh do words poore algorithm ka dil hain. Inhe galat samjha toh baaki kuch bhi nahi samjhega.

Figure — Aho-Corasick — multiple pattern search, automaton

Aho-Corasick inse itna obsessed kyun hai? Kyunki machine ka poora kaam yeh hai:

"Kisi search word ka sabse lamba prefix jo abhi tak padhe gaye text ka suffix bhi ho."

Woh ek sentence dono concepts ek saath use karta hai. Jab koi letter ek match extend karne mein fail karta hai, toh "shortcut rope" tumhe sabse lamba proper suffix pe jump kara deta hai jahan tum the — jo abhi bhi kisi word ka prefix ho. Dono halve, phir se. Ise pakad ke rakho — yeh parent note ke Step 2 mein wapas aata hai.


2 · Set, aur — ek poora dictionary measure karna

Hum ek word nahi dhundh rahe; hum ek saath words ka ek dictionary dhundh rahe hain.

Humein ki zaroorat kyun hai? Kyunki machine ki build cost (aur ) mein measure hoti hai, "number of patterns" mein nahi. Length 1000 ke do patterns build karne mein utna hi cost aata hai jitna length 1 ke 2000 patterns mein — jo matter karta hai woh total characters hain. ko naam dene se complexity line yeh precisely keh sakti hai.


3 · Graphs, nodes, edges — machine ki shape

Machine ek graph hai. Agar yeh word dara raha hai, toh yeh bas arrows se jude dots hain.

Figure — Aho-Corasick — multiple pattern search, automaton

Tree kyun na ki plain list? Kyunki jo words same start share karte hain (he aur hers dono he se start karte hain) woh apne front edges share kar sakte hain, toh machine he ek baar store karti hai. Yahi sharing exactly woh hai jo ek tree deta hai aur ek list nahi.


4 · Trie — prefixes ka ek tree

Ab "tree" ko "prefix" ke saath combine karo aur tumhe pehla real structure milega.

Trie ke baare mein hum Trie — prefix tree mein zyada detail mein jaate hain; yahan tumhe bas chahiye: node = ek prefix, edge = ek character, terminal = ek word yahan khatam hota hai.

Hum iske teen operations name karte hain, parent note ke saath match karte hue:

  • = woh string jo node represent karta hai.
  • ya = woh node jo tumhe se character labeled real edge follow karne pe milta hai (ho sakta hai exist na kare).
  • terminal: ek node jo flag ki gayi hai kyunki ek poora pattern wahan khatam hota hai.

Yeh genuinely naya idea hai; parent note ise fully derive karta hai. Yahan hum bas ensure karte hain ki symbol aur picture clear ho.

Yeh "hamesha shorter / hamesha upar" fact wahi hai kyun hum failure links ek specific order mein build karte hain — agla brick dekho.

Yeh idea directly single-pattern matching se liya gaya hai. Agar tumne KMP — single pattern matching dekha hai, toh failure link exactly KMP ka prefix function hai, lekin ek word ki jagah words ke poore tree pe spread kiya gaya hai.


6 · BFS — sabse pehle shortest strings process karna

Har failure link sahi se fill karne ke liye, humein nodes depth ke order mein visit karna hoga. Woh tool jo yeh karta hai woh BFS hai.


7 · Automaton aur uska transition

Finally, topic title mein woh word: automaton.

Suffixes pe built string machines ka ek poora family hai na ki ek fixed dictionary — Suffix Automaton aur Suffix Tree dekho — aur doosre matching engines jaise Z-algorithm and string matching. Aho-Corasick "ek saath kaafi fixed patterns" ka specialist hai.


8 · Cost symbols — , , , , aur big-

Aakhri brick: complexity line mein letters, taaki yeh English ki tarah padhe.


Prerequisite map

characters and alphabet

strings

prefix and suffix

pattern set and total length M

graph nodes and edges

tree and root and depth

Trie prefix tree

failure link

BFS by depth

automaton and transition delta

cost symbols n M Sigma z

Aho-Corasick machine

Ise upar ki taraf padho: characters se strings banti hain; strings prefixes/suffixes aur ek dictionary deti hain; graphs se trees milte hain, trees se trie milta hai; prefix + suffix + BFS se failure links milte hain; trie + failure links se automaton milta hai; cost symbols add karo aur tumhare paas poora Aho-Corasick machine hai.


Equipment checklist

Right side cover karo aur zor se jawaab do; agar koi ruk jaaye, toh main note se pehle woh section dobara padho.

Kya hota hai ek proper suffix of hers, aur sab list karo
Ek suffix jo poori string nahi hai: ers, rs, s, "".
Kaunsi single string har string ka ek prefix bhi aur suffix bhi hoti hai
Empty string "".
Trie mein, depth pe ek node kya represent karta hai
Ek string (kisi pattern ka prefix) jo exactly length ki hai.
kya measure karta hai
Sabhi patterns mein sab characters ki total sankhya jodi gayi.
kahan point karta hai — shorter ya longer string, aur exactly kaunsi
Strictly shorter string ki taraf: ka longest proper suffix jo abhi bhi ek trie node hai (ya root).
kya hai, aur kyun
Yeh root khud define kiya gaya hai (aur kabhi follow nahi hota), kyunki empty string ke paas fall back karne ke liye koi proper suffix nahi.
Failure links recursion se nahi BFS se kyun compute karein
ko shorter strings ke failure links chahiye; BFS pehle saari chhoti depths finish karta hai.
kya deta hai jab ke paas pe koi real edge nahi
Woh state jo failure rope(s) follow karne ke baad milti hai — yeh kabhi fail nahi hoti, worst case root.
Cost mein, kya hai
Us text ki length jo scan ki ja rahi hai.
Cost mein, kya hai
Scan ke dauran actually report ki gayi matches ki sankhya.
Scanning patterns ki sankhya se independent kyun hai
precomputed hone se, har text char ek lookup hai regardless of dictionary size.