Skip to main content

Algorithms · Graph optimization and traversal algorithms

Let T[0..n-1] be a text and P[0..m-1] a pattern over the same alphabet, where 1≤ m≤ n

Problem

Let \(T[0..n-1]\) be a text and \(P[0..m-1]\) a pattern over the same alphabet, where \(1\le m\le n\). The naive matcher tries every shift \(s\in\{0,\ldots,n-m\}\), comparing pattern positions from left to right until the first mismatch or a complete match. 1. Trace the algorithm on \(T=\texttt{AABAACAADAABAABA}\) and \(P=\texttt{AABA}\), listing the compared pattern positions and result at every shift. 2. Prove that exactly the occurrence shifts are reported. 3. Give a tight worst-case character-comparison bound in \(n,m\), and provide a two-character-alphabet family attaining it up to a constant factor for every valid \(n,m\).

Hint

** At one shift, comparisons stop at the first mismatch.

Check your work

Work the problem yourself first. Then open it in Training to check your answer and read the full worked solution.

The answer check and full solution for this problem come with ProofAnvil Practice membership ($19 USD monthly). See membership. Or start with the free Algorithms sample problem: Try the free sample problem.

More Algorithms practice problems

Back to Algorithms

An original ProofAnvil practice problem, written for this course. ProofAnvil is a practice course, not a homework-answer service.