Knuth-Morris-Pratt Algorithm

Maryann Gitonga
2 min readSep 4, 2023

--

How it works

Imagine you’re using a text editor, searching for a specific word or phrase within a lengthy document. You start at the beginning of the document (the text) with your cursor and the word or phrase you want to find (the pattern). Instead of manually scanning through the text character by character, the Knuth-Morris-Pratt Algorithm (KMP) algorithm acts as your helpful assistant.

You tell your assistant, “I’m looking for the word ‘search’ in this document.” Your assistant takes a peek at ‘search’ and realizes that there’s no ‘s’ in the first paragraph. Instead of making you read every character, it suggests, “Let’s skip this paragraph. There’s no ‘s’ here.” You both jump ahead without retracing your steps.

Moving on, in the middle of the document, your assistant spots the word ‘search.’ But rather than making you read it letter by letter, your assistant says, “Good news! We found ‘s’ in the middle, but this isn’t a match. However, we’ve got a clue now. We can skip some more text since we know ‘s’ doesn’t appear until the middle.”

You continue scrolling, and thanks to your assistant’s clever guidance, you don’t need to go back and reread sections or examine every word in detail. Eventually, you locate ‘search’ without reading every single character.

The KMP algorithm in your text editor efficiently helps you find a specific pattern, like ‘search,’ within a large body of text. It does so by leveraging insights from previous comparisons to avoid unnecessary searching, making your text editing tasks quicker and more efficient.

Illustration of the KMP Algorithm: Making the Longest Prefix Suffix (phi) table.
Use the LPS table to find the matching word.

Benefits

  1. It significantly reduces the time required for substring searches by eliminating redundant character comparisons.
  2. It offers a time complexity of O(n + m), which is notably faster compared to the naive approach’s worst-case time complexity of O(m * n).

Real-life application

In text editors, the KMP algorithm swiftly identifies all instances of a particular word or phrase within a document, ensuring efficient and speedy searches.

Simple Code Illustration

Where next?

--

--

Maryann Gitonga

A Software Engineer who loves trying out different tooling & engineering concepts in Back-end Engineering & DevOps. An explorer in the ML world too!