Chapter 7
String, Sequence and Indexing Algorithms
Post Graduate Program in AI and ML
Advanced Algorithm
•
Post Graduate
String, Sequence and Indexing Algorithms
Introduction
String and sequence algorithms are central to modern artificial intelligence and machine learning systems. Search engines compare queries with documents; natural-language processing (NLP) systems tokenize, align, and retrieve text; bioinformatics pipelines compare DNA and protein sequences; recommendation systems identify similar item descriptions; and software tools detect duplicated or altered code. These applications differ in domain, but they share a computational problem: determine relationships among ordered symbols efficiently.
A string is a finite sequence of symbols drawn from an alphabet Σ. For a string S of length n, positions are commonly indexed from 0 to n−1. A substring is a contiguous portion of S, whereas a subsequence preserves order but need not preserve contiguity. This distinction explains why exact matching, edit distance, and sequence alignment require different algorithms.
At postgraduate level, the important design question is not simply which algorithm is fastest in theory. It is which representation, objective function, indexing strategy, and approximation policy are appropriate for the data and application constraints.
Learning Objectives
By the end of this chapter, a learner should be able to:
Distinguish strings, substrings, subsequences, prefixes, suffixes, and inverted indexes.
Explain and apply naïve matching, finite-automaton matching, KMP, and Boyer–Moore.
Construct and use tries, suffix arrays, suffix trees, and the Burrows–Wheeler transform.
Formulate edit distance and sequence alignment using dynamic programming.
Compare exact, approximate, and locality-sensitive retrieval methods.
Select algorithms using complexity, memory, latency, and domain constraints.
Evaluate practical and ethical implications of text and biological sequence processing.
1. Fundamental Concepts and Terminology
Let S = s₀s₁…sₙ₋₁ be a string of length n over alphabet Σ. A pattern P = p₀p₁…pₘ₋₁ has length m. Exact pattern matching asks whether P occurs beginning at some position i in S, that is, whether S[i+j] = P[j] for every 0 ≤ j < m.
A prefix begins at position 0; a suffix ends at position n−1. A substring S[i:j] usually denotes positions i through j−1. A subsequence is obtained by deleting zero or more symbols without changing the order of the remaining symbols. For example, “ace” is a subsequence of “abcde” but not a substring.
The alphabet may be small, such as DNA symbols {A, C, G, T}, or very large, such as Unicode text. Alphabet size affects transition tables, trie memory, hashing, and cache behaviour. In NLP, normalization decisions—case folding, punctuation handling, Unicode normalization, and tokenization—are part of the algorithmic specification, not merely preprocessing.
The principal costs are:
Preprocessing time: work performed before queries arrive.
Query time: work for one pattern or retrieval request.
Space: memory used by the representation.
Output sensitivity: time required to report k matches, often at least Ω(k).
Latency and throughput: important distinctions in interactive search and batch genomics.
2. Exact Pattern Matching
2.1 Naïve matching
The naïve algorithm aligns P at every possible position in S and compares characters from left to right. Its worst-case running time is O((n−m+1)m), conventionally written O(nm), and its extra space is O(1). It is nevertheless useful when patterns are short, data are small, or implementation simplicity is more valuable than worst-case guarantees.
For random text and a sufficiently large alphabet, mismatches often occur early, so observed performance can be substantially better than the worst case. However, repetitive data such as “aaaaaaaa…” and pattern “aaaab” exposes the quadratic behaviour.
2.2 Knuth–Morris–Pratt (KMP)
KMP avoids rechecking text characters after a mismatch. It preprocesses P into a failure function, also called the prefix-function or longest proper prefix-suffix (LPS) array. LPS[j] is the length of the longest proper prefix of P[0:j+1] that is also a suffix of that prefix.
If a mismatch occurs after j matched characters, KMP shifts the pattern so that the LPS[j−1] characters remain aligned. The pattern preprocessing costs O(m), and the search costs O(n), giving total O(n+m) time and O(m) auxiliary space.
The guarantee is independent of alphabet distribution. This makes KMP valuable in streaming systems, adversarial environments, and applications requiring predictable latency. Its main practical disadvantage is that its control flow can be less cache-friendly than simpler algorithms for ordinary text.
2.3 Boyer–Moore and its variants
Boyer–Moore compares the pattern from right to left and uses shifts based on two heuristics:
The bad-character rule shifts the pattern according to the last occurrence of the mismatching text symbol in P.
The good-suffix rule shifts the pattern to align a previously matched suffix with another occurrence or a suitable prefix.
With complete heuristics and appropriate implementation, Boyer–Moore has strong practical performance and can skip large portions of text. Simplified variants such as Horspool are easier to implement. Classical worst-case bounds depend on the exact variant and shift strategy; therefore, claims of universally linear performance should not be made without specifying the implementation. Boyer–Moore is especially effective for relatively long patterns and large alphabets, while KMP is preferable when a strict O(n+m) guarantee is required.
2.4 Finite automata
A deterministic finite automaton (DFA) for a pattern represents the state q as the length of the longest prefix of P matching the current text suffix. Each transition consumes one input symbol. Once constructed, scanning requires O(n) time. Explicit construction may require O(m|Σ|) space, which is unsuitable for very large alphabets unless transitions are sparse or computed lazily.
Finite automata provide a useful conceptual bridge to lexical analysis, tokenization, regular expressions, and streaming classification.
3. Indexing Structures for Large-Scale Retrieval
When many patterns must be searched over relatively stable text, preprocessing an index is preferable to rescanning the corpus.
3.1 Tries
A trie stores strings character by character from a root. Each root-to-node path represents a prefix. Search takes O(m) time for a pattern of length m, subject to transition lookup cost. A trie supports prefix completion naturally, making it suitable for autocomplete, dictionaries, and routing tables.
A naïve trie can consume substantial memory because each node may contain many pointers. Compressed tries merge non-branching paths; ternary search trees and succinct representations provide further trade-offs. For multilingual text, sparse maps or compact arrays are often preferable to a dense |Σ|-entry array at every node.
3.2 Suffix trees
A suffix tree is a compressed trie containing all suffixes of a text. It supports substring queries in O(m+k) time, where m is the pattern length and k is the number of reported occurrences, after linear-time construction for standard models of computation. Its space is O(n), but implementation complexity is high, and pointer-heavy structures may perform poorly on modern memory hierarchies.
Suffix trees support repeated-substring discovery, longest common substring computations, and many genome-analysis tasks.
3.3 Suffix arrays and the LCP array
A suffix array is an array of the starting positions of all suffixes of S in lexicographic order. For S = “banana$”, where $ is a unique sentinel smaller than ordinary symbols, the suffixes are ordered as shown conceptually below:
Rank
Suffix
Start
0
$
6
1
a$
5
2
ana$
3
3
anana$
1
4
banana$
0
5
na$
4
6
nana$
2
The suffix array requires O(log n) comparisons for a pattern search using binary search, with comparison cost potentially making the practical bound O(m log n). With the LCP (longest common prefix) array and specialized techniques, repeated comparisons can be reduced and many queries become efficient.
Suffix arrays are often preferred over suffix trees because they are simpler, compact, and cache-efficient. Construction algorithms range from O(n log n) doubling methods to linear-time algorithms, although linear asymptotic methods may have greater implementation complexity.
3.4 Burrows–Wheeler transform and FM-index
The Burrows–Wheeler transform (BWT) rearranges a string using the last column of its sorted cyclic rotations. For “banana$”, the transformed string is “annb$aa”. The BWT is reversible when the sentinel is unique and the associated ordering information is retained implicitly.
An FM-index augments the BWT with rank information. Its backward-search procedure extends a pattern one symbol at a time and maintains a suffix-array interval containing all suffixes prefixed by the current pattern. A standard query uses the C array and an Occ(c, i) function, which counts occurrences of symbol c in the BWT prefix ending before position i. The interval update is:
l' = C[c] + Occ(c, l) r' = C[c] + Occ(c, r)
Here [l, r) is the current suffix-array interval, c is the newly prepended symbol, C[c] is the number of symbols lexicographically smaller than c, and Occ(c, i) counts c in positions 0 through i−1. The resulting interval [l′, r′) represents matches for the extended pattern. Rank data are stored in compressed form, producing an index whose size can approach the compressed text size.
FM-indexes are important in short-read DNA alignment and large-scale text search because they combine compression with backward pattern search. Their performance depends on sampling, rank-support structures, and the cost of locating text positions from suffix-array intervals.
Comparison of string indexing structures
Figure: Comparison of string indexing structures
Figure: Three important indexing families and the design considerations connecting them.
4. Edit Distance and Sequence Alignment
Exact matching is too rigid when text contains spelling errors, transcription variation, mutations, or noisy observations. Edit distance measures the minimum cost of transforming one sequence into another.
4.1 Levenshtein distance
For strings X of length n and Y of length m, define D[i,j] as the minimum cost of transforming X[0:i] into Y[0:j]. With unit insertion, deletion, and substitution costs:
The boundary conditions are D[i,0] = i and D[0,j] = j. The indicator [condition] equals 1 when the condition is true and 0 otherwise. The final distance is D[n,m]. This recurrence assumes independent edit operations, additive costs, and no transposition operation. The dynamic-programming time is O(nm), and the full table uses O(nm) space. Since each row depends only on the previous row, the distance alone can be computed in O(min(n,m)) space, although reconstructing an alignment generally requires additional information.
Worked example. Transform “cat” into “cut”. The first and second characters match; the third characters differ. Substituting “a” with “u” costs 1, so the distance is 1. Transforming “cat” into “cats” costs one insertion and therefore has distance 1.
Weighted edit distance assigns costs wᵢ for insertion, w_d for deletion, and w_s(a,b) for substitution. The recurrence remains valid after replacing unit costs with these weights. Costs must be non-negative for the usual shortest-path interpretation; application-specific asymmetry is allowed, such as making deletion cheaper than insertion in noisy OCR.
4.2 Global and local sequence alignment
In bioinformatics, alignment often uses match rewards and mismatch and gap penalties. The Needleman–Wunsch algorithm performs global alignment, forcing the entire sequences into an alignment. The Smith–Waterman algorithm performs local alignment and identifies the highest-scoring corresponding regions.
Here F[i,j] is the best score for prefixes ending at i and j, s is a match/mismatch scoring function, and g is a linear gap penalty. Boundary values are F[i,0] = −ig and F[0,j] = −jg. For local alignment, negative scores are reset to zero:
The best local alignment is the maximum cell in the table, followed by traceback until a zero is reached. Both algorithms require O(nm) time and O(nm) space in their straightforward form. Affine gap penalties, which distinguish gap opening from gap extension, use additional states and better model biological insertions and deletions:
gap(k) = gopen + (k−1)gextend
Here k is the gap length, and the formula assumes the first gap symbol pays the opening cost while each subsequent symbol pays an extension cost.
4.3 Banded and bit-parallel dynamic programming
If a maximum plausible edit distance d is known, computation can be restricted to cells satisfying |i−j| ≤ d. The time becomes O(d·max(n,m)) rather than O(nm), assuming the true alignment lies within the band. This is a conditional optimization: an incorrectly narrow band can miss the correct answer.
Bit-parallel algorithms pack many dynamic-programming states into machine words and exploit bit operations. They can be extremely fast for short patterns, but their complexity depends on word size and implementation details. They are widely used in approximate dictionary lookup and read mapping.
5. Approximate Retrieval and Similarity Search
Approximate retrieval seeks items similar to a query rather than identical to it. The similarity measure must reflect the application.
Edit distance: appropriate for typos and biological mutations.
Jaccard similarity: compares sets of tokens or shingles. For sets A and B, J(A,B) = |A ∩ B| / |A ∪ B|.
Cosine similarity: compares vector representations. For nonzero vectors x and y, cos(θ) = (x · y)/(||x|| ||y||).
Hamming distance: counts unequal positions and requires equal-length strings.
Jaro–Winkler: often useful for short names, but its parameters and prefix boost must be validated for the target population.
An inverted index maps a term to a posting list of documents containing it. Boolean retrieval intersects, unions, or subtracts posting lists. Ranking systems commonly combine term statistics, field information, and learned signals. A classical term-frequency/inverse-document-frequency weight is:
idf(t) = log(N / df(t))
Here N is the number of documents and df(t) is the number containing term t. The formula assumes df(t) > 0; smoothing is needed when unseen terms are possible. TF–IDF is interpretable but does not understand synonyms or context. Dense embeddings improve semantic matching but can introduce representational bias and require approximate nearest-neighbour indexes.
Locality-sensitive hashing (LSH) hashes similar objects into the same bucket with higher probability than dissimilar objects. For MinHash, collision probability equals Jaccard similarity under the standard random-permutation model. LSH provides probabilistic retrieval rather than an exact guarantee. Graph-based methods such as HNSW often provide strong practical recall–latency trade-offs, but index construction, memory use, and recall must be measured on the actual workload.
A robust retrieval pipeline often uses several stages:
Normalize and tokenize input.
Retrieve a high-recall candidate set using an inverted or vector index.
Apply exact, edit-distance, or neural re-ranking.
Enforce business, safety, privacy, and access constraints.
Monitor recall, precision, latency, and drift.
6. Case Study: Approximate Clinical Search
This is an illustrative case. A hospital information system must retrieve medication and diagnosis records despite misspellings, abbreviations, and inconsistent punctuation. A patient query such as “amoxycillin” should retrieve records containing “amoxicillin,” but the system must avoid confusing medications with dangerously similar names.
Background and problem. Exact matching produces false negatives. Unrestricted fuzzy matching produces false positives and may create clinical risk. The system must support millions of records with interactive response time.
Analysis. A single similarity measure is inadequate. Character-level edit distance handles spelling variation, token-level matching handles multiword names, and domain terminology provides safety constraints. Rare terms should receive more retrieval weight than common terms, but frequency alone must not determine clinical equivalence.
Approach. The proposed architecture first applies Unicode normalization and controlled abbreviation expansion. An inverted index retrieves candidates by token and character n-gram. A trie supports prefix completion. Candidate pairs are filtered by a maximum weighted edit distance, with lower thresholds for short drug names because one character can be clinically significant. A domain dictionary and a pharmacist-approved synonym table constrain substitutions. A re-ranking model combines exact field matches, edit distance, token overlap, and contextual fields such as dosage form. Results are shown with explanations and never silently replace the user’s original term.
Evaluation. Measure recall at k, precision at k, false-positive rate for high-risk names, median and tail latency, and performance across languages and departments. Human review is required for safety-critical errors. Offline accuracy is insufficient if the index is stale or the user interface obscures uncertainty.
Outcome and learning points. A hybrid system is safer than a purely neural or purely fuzzy system. Thresholds should be field-specific, rare dangerous confusions need special treatment, and monitoring must include subgroup and drift analysis. The case demonstrates that algorithm selection is a governance decision as well as a performance decision.
7. Design Trade-offs, Limitations, and Best Practices
Choosing an algorithm
Requirement
Suitable starting point
Main caution
One short exact query
Naïve or Horspool
Weak worst-case guarantees
Guaranteed linear scan
KMP
More preprocessing and less intuitive skipping
Many prefix queries
Trie
Memory consumption
Many substring queries on fixed text
Suffix array or FM-index
Construction and update cost
Small-error comparison
Banded edit distance
Band must contain the true path
Semantic document retrieval
Inverted index plus embeddings
Drift, bias, and approximate recall
Biological alignment
Smith–Waterman or domain aligner
Scoring model strongly affects results
Important limitations
First, asymptotic complexity hides constants, cache locality, allocation cost, and parallelization. Second, preprocessing is valuable only when the corpus is sufficiently stable or updates can be propagated. Third, compressed indexes may reduce memory while increasing rank or locate costs. Fourth, approximate methods can produce plausible but incorrect matches. Finally, normalization can destroy distinctions: case folding, accent removal, or aggressive stemming may improve recall but harm precision.
Engineering and governance practices
Use representative, adversarial, and multilingual test data. Specify whether positions refer to bytes, Unicode code points, grapheme clusters, or tokens. Report both average and tail latency. Separate candidate generation from final ranking so recall and precision can be tuned independently. Version dictionaries, scoring parameters, and indexes. Protect sensitive text and genomic data through access control, encryption, retention limits, and audit logs. In clinical and employment contexts, provide human oversight and an appeal path. Do not treat similarity as identity or statistical relevance as truth.
8. Contemporary Developments and Research Directions
Neural retrieval uses transformer-derived embeddings to capture semantic relationships that lexical indexes miss. Hybrid retrieval combines lexical evidence with dense vectors, while late-interaction models preserve some token-level interpretability. Learned indexes attempt to predict key positions rather than relying solely on traditional search trees, but distribution shift can degrade their guarantees.
For long-context language systems, efficient indexing of passages, entities, and temporal versions is increasingly important. In bioinformatics, pan-genome indexes represent variation across many individuals rather than one reference genome. Research also addresses privacy-preserving approximate matching, federated retrieval, differentiable dynamic programming, and hardware acceleration using GPUs and specialized vector instructions.
These developments do not eliminate classical algorithms. They increase the need for them: exact verification, edit-distance constraints, compressed storage, and interpretable candidate generation remain essential components of reliable AI systems.
Chapter Summary
String algorithms operate on ordered symbols and support exact matching, indexing, approximate comparison, and alignment. Naïve matching is simple, KMP provides a linear worst-case guarantee, and Boyer–Moore often performs well in practical text search. Tries support prefix operations; suffix arrays and suffix trees support substring analysis; the BWT and FM-index combine compression with efficient backward search.
Dynamic programming gives principled solutions for Levenshtein distance, global alignment, and local alignment. Weighted and affine-gap models allow domain-specific costs. Banded and bit-parallel methods improve performance under appropriate assumptions. Approximate retrieval combines edit distance, token statistics, vector similarity, and probabilistic indexes, usually in a multi-stage architecture.
The correct design depends on query volume, corpus stability, alphabet, memory, latency, output size, and risk. In professional AI and ML systems, evaluation, explainability, privacy, safety, and governance are as important as asymptotic complexity.
Key Terms
String: An ordered finite sequence of symbols.
Alphabet: The set of symbols from which strings are formed.
Substring: A contiguous portion of a string.
Subsequence: An order-preserving selection that need not be contiguous.
Prefix: A substring beginning at the first position.
Suffix: A substring ending at the last position.
Pattern matching: Finding occurrences of a pattern in text.
LPS array: The longest proper prefix-suffix values used by KMP.
Trie: A tree representing strings by shared prefixes.
Suffix tree: A compressed trie of all suffixes of a text.
Suffix array: Lexicographically sorted starting positions of suffixes.
LCP array: Longest common-prefix lengths between adjacent suffixes.
BWT: A reversible permutation useful for text compression and indexing.
FM-index: A compressed index based on BWT and rank queries.
Edit distance: Minimum cost of transforming one string into another.
Dynamic programming: Solving overlapping subproblems through stored states.
Sequence alignment: Arranging sequences to maximize a defined score.
Inverted index: A mapping from terms to documents or positions.
Locality-sensitive hashing: Probabilistic hashing that preserves similarity.
Approximate retrieval: Retrieval based on similarity rather than exact equality.
Review Questions
Short Answer Questions
Distinguish a substring from a subsequence with an example.
What information does the LPS array store in KMP?
Why can a trie consume more memory than its asymptotic query time suggests?
State the boundary conditions for Levenshtein dynamic programming.
What is the purpose of the sentinel in a suffix-array or BWT construction?
Long Answer Questions
Compare naïve matching, KMP, and Boyer–Moore in terms of preprocessing, worst-case behaviour, and practical use.
Explain suffix arrays, LCP arrays, suffix trees, and FM-indexes as alternative indexing structures.
Derive the Levenshtein recurrence and discuss how weighted costs change its interpretation.
Compare global and local sequence alignment, including their boundary conditions and applications.
Explain why a multi-stage approximate retrieval architecture is preferable to relying on one similarity measure.
Analytical / Application Questions
Design an indexing strategy for a multilingual autocomplete service with frequent prefix queries and daily dictionary updates. Justify the representation and update policy.
A DNA-read pipeline knows that the true alignment has at most eight edits. Explain when banded dynamic programming is safe and what can happen if the band is too narrow.
Propose an evaluation and governance plan for a clinical fuzzy-search system. Include accuracy, latency, safety, privacy, and human-review measures.
Multiple Choice Questions with Answers
Question 1
Which algorithm guarantees O(n + m) search time for a pattern of length m in text of length n after linear preprocessing?
A. Naïve matching
B. Knuth–Morris–Pratt
C. Unindexed binary search
D. Basic trie construction
Correct Answer: B. Knuth–Morris–Pratt
Question 2
What does an LPS value in KMP represent?
A. The longest suffix of the text
B. The number of alphabet symbols
C. The longest proper prefix that is also a suffix
D. The number of pattern occurrences
Correct Answer: C. The longest proper prefix that is also a suffix
Question 3
Which structure is naturally suited to prefix completion?
A. Trie
B. LCP array alone
C. Edit-distance matrix
D. BWT sentinel
Correct Answer: A. Trie
Question 4
Under unit insertion, deletion, and substitution costs, what is the Levenshtein distance between “cat” and “cut”?
A. 0
B. 1
C. 2
D. 3
Correct Answer: B. 1
Question 5
Which alignment method resets negative scores to zero to find the best local match?
A. Needleman–Wunsch
B. KMP
C. Smith–Waterman
D. Boyer–Moore
Correct Answer: C. Smith–Waterman
Question 6
In standard FM-index backward search, what does the maintained suffix-array interval represent?
A. All suffixes containing no sentinel
B. Suffixes prefixed by the current pattern
C. All suffixes of equal length
D. Only the lexicographically largest suffix
Correct Answer: B. Suffixes prefixed by the current pattern
Question 7
What is the principal risk of using an excessively narrow band in banded dynamic programming?
A. The alphabet becomes larger
B. The true optimal alignment may be excluded
C. All substitutions become free
D. The algorithm always uses quadratic space
Correct Answer: B. The true optimal alignment may be excluded
Question 8
Which measure is defined as |A ∩ B| divided by |A ∪ B|?
A. Cosine similarity
B. Hamming distance
C. Jaccard similarity
D. Edit distance
Correct Answer: C. Jaccard similarity
Question 9
What does an inverted index map?
A. Documents to encryption keys only
B. Terms to documents or positions
C. Suffixes to alphabet size
D. Queries to neural-network layers
Correct Answer: B. Terms to documents or positions
Question 10
Which statement best describes an FM-index?
A. A dense prefix tree for autocomplete
B. A compressed index using the BWT and rank information
C. A dynamic-programming table for global alignment
D. A hashing scheme that requires equal-length strings
Correct Answer: B. A compressed index using the BWT and rank information