3.8.8String Algorithms

Suffix tree (conceptual)

1,832 words8 min readdifficulty · medium3 backlinks

WHY does this thing exist?

Take S=S = banana. Its suffixes are:

start index suffix
0 banana
1 anana
2 nana
3 ana
4 na
5 a

A trie of these has one root-to-leaf path per suffix. The problem: it can have O(n2)O(n^2) nodes. We fix this with compression.


WHAT exactly is a suffix tree?

Figure — Suffix tree (conceptual)

HOW do we build the idea (derivation from a trie)


WHAT can you DO with it (the 80/20 payoff)



Recall Feynman: explain to a 12-year-old

Imagine you write a long word and you want a magic map of every smaller piece hiding inside it. You take the word and chop off the front letter again and again — banana, anana, nana, … — and feed all these tails into a tree where words that start the same way share the same branch. Now if a friend asks "is nan in there?", you just walk: n → a → n, and if you can finish the path, yes it's inside! Counting how many little leaf-tags hang below tells you how many times it appears. The $ at the end is a tiny flag that makes sure every tail gets its own leaf so nothing hides inside another.


Active Recall

A suffix tree is a compressed trie of what?
All suffixes of the string S\$$. Why append a unique terminal `? ::: So no suffix is a prefix of another; every suffix then ends at its own leaf, guaranteeing exactly $n+1$ leaves. How many leaves does a suffix tree of $S\$ have? ::: Exactly $n+1$ (one per suffix, including the suffix).Constraintoninternalnodes(besidesroot)?:::Eachhasatleast2children(theyarebranchingpoints).Whyistotalspace` suffix). Constraint on internal nodes (besides root)? ::: Each has at least 2 children (they are branching points). Why is total space O(n)andnotand notO(n^2)?:::Edgelabelsarestoredasindexpairs? ::: Edge labels are stored as index pairs (i,j)intointoS,notascopiedsubstrings;with, not as copied substrings; with O(n)edgesthatsedges that'sO(n)space.Timetotestifpatternspace. Time to test if patternPisasubstring?:::is a substring? :::O(\lvert P\rvert)justwalkdownmatching— just walk down matchingPscharacters.Howdoyoucountoccurrencesof's characters. How do you count occurrences of Paftermatchingit?:::Countthenumberofleavesinthesubtreebelowthematchpoint.Whatdoesthedeepestinternalnode(bychardepth)giveyou?:::Thelongestrepeatedsubstringofafter matching it? ::: Count the number of leaves in the subtree below the match point. What does the deepest internal node (by char depth) give you? ::: The longest repeated substring ofS.Whymustedgesoutofanodestartwithdistinctcharacters?:::Sowalkingmatchesapatterndeterministicallyatmostoneedgetofollowpernextcharacter.Substringof. Why must edges out of a node start with distinct characters? ::: So walking matches a pattern deterministically — at most one edge to follow per next character. Substring of Sequalsaprefixofwhat?:::Aprefixofsomesuffixofequals a prefix of what? ::: A prefix of some suffix ofS$ — this is why a tree of suffixes answers substring queries.

Connections

  • Trie — the uncompressed ancestor of the suffix tree.
  • Suffix Array — same info, less memory; often preferred in practice.
  • Ukkonen's Algorithm — linear-time online construction.
  • KMP Algorithm — alternative single-pattern matcher, O(n+m)O(n+m).
  • Longest Common Substring — solved via a generalized suffix tree of two strings.
  • Burrows-Wheeler Transform — related suffix-structure used in compression.

Concept Map

is prefix of

inserted into

has O of n squared nodes

produces

forces each suffix to a leaf

has n plus 1 leaves

keeps space linear

enables

bounds internal nodes

Substring of S

Suffix of S

Trie of all suffixes

Suffix tree

Terminal symbol dollar

Compress single-child chains

Edge labels as start end index pairs

Total nodes O of n

Search P in O of length P

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Suffix tree ka core idea simple hai: kisi string SS ke saare suffixes (peeche se chhote hote tails — banana, anana, nana...) ko ek tree me daal do, jaha same se shuru hone wale tails same branch share karte hain. Yaad rakho: koi bhi substring asal me kisi suffix ka prefix hi hota hai. Isliye agar saare suffixes tree me hain, to "kya P string ke andar hai?" ka jawaab bas root se P ke letters follow karke mil jaata hai — bilkul O(P)O(|P|) time me, chahe SS kitni badi ho.

Hum end me ek special $ lagate hain jo alphabet me nahi hai. Kyun? Taaki koi suffix doosre ka prefix na ban jaaye (jaise a aur ana). $ ke baad har suffix apne alag leaf pe khatam hota hai, to exactly n+1n+1 leaves milte hain — clean structure.

Space ki tension mat lo. Edge labels ko hum literal text ke roop me store nahi karte; sirf do integer (i,j)(i,j) rakhte hain jo bolte hain "S[i..j]S[i..j]". Isliye poora tree O(n)O(n) memory me aata hai, O(n2)O(n^2) nahi. Build karne ke liye Ukkonen's algorithm linear O(n)O(n) time leta hai.

Yeh kyun matter karta hai? Ek hi structure se bahut saare problems turant solve hote hain: pattern search, kitni baar P aaya (subtree ke leaves gino), aur longest repeated substring (sabse deep internal node ka path). Interviews aur real bioinformatics (DNA matching) dono me yeh power-tool hai. Aaj practice me log aksar suffix array prefer karte hain kyunki memory kam lagti hai, par concept yahi suffix tree wala hai.

Go deeper — visual, from zero

Test yourself — String Algorithms

Connections