LearnViz CompSci
Home / Learn / Algorithms / KMP Algorithm

Advanced Topic 43 of 51 Algorithms

KMP Algorithm

Linear-time string matching using a failure table.

How it works

The Knuth-Morris-Pratt algorithm finds a pattern in a text in O(n + m) time by precomputing a failure function, the longest proper prefix of the pattern that is also a suffix for each position. When a mismatch occurs, the pattern is shifted using this table instead of restarting comparison, so the text pointer never moves backward.

Step-by-step walkthrough

  1. The pattern ABABC has a failure table of how much of a prefix repeats at each position.
  2. Compare text and pattern left to right.
  3. A mismatch at C means ABAB was matched, and its longest border is AB.
  4. Jump the pattern forward and keep the text pointer where it is.
  5. The text is never moved backward so the total time is O(n + m).

Where it is used

Text search in editors and tools, bioinformatics sequence matching, plagiarism detection, network intrusion detection signatures and implementing substring search in libraries use KMP.

Common pitfall

Building the failure table is subtle, and off-by-one errors are very common. Using zero-based versus one-based definitions inconsistently breaks the shifting rule. For a single occasional search, a simple library routine may be enough. For multiple patterns use Aho-Corasick instead.

Where it fits in the course

KMP Algorithm is topic 43 of 51 in Algorithms, rated Advanced. It follows Maximum Flow. The next topic is Rabin-Karp Algorithm.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is KMP Algorithm?
Linear-time string matching using a failure table. It belongs to Algorithms and is an advanced-level topic.
Where do people use KMP Algorithm?
Text search in editors and tools, bioinformatics sequence matching, plagiarism detection, network intrusion detection signatures and implementing substring search in libraries use KMP.
What is the most common mistake with KMP Algorithm?
Building the failure table is subtle, and off-by-one errors are very common.
What should I learn before this topic?
Start with Maximum Flow, the topic just before it in Algorithms.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Algorithms quiz.
Is KMP Algorithm hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn KMP Algorithm?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to KMP Algorithm?
Maximum Flow, Rabin-Karp Algorithm, Strongly Connected Components are closely connected. The full list is in the related topics section of this page.
What should I study after KMP Algorithm?
Continue with Rabin-Karp Algorithm, the next topic in Algorithms.

Related topics