DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Cryptography Module in Mule 4
  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads

Trending

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. The Levenshtein Distance Algorithm

The Levenshtein Distance Algorithm

In this article, we take at look at an interesting algorithm that can be applied to the work of data scientists and data analysts.

By 
Nikhil Babar user avatar
Nikhil Babar
·
Oct. 02, 18 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
121.2K Views

Join the DZone community and get the full member experience.

Join For Free

The Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions, or substitutions) required to change one word into the other. It is named after Vladimir Levenshtein, who discovered this equation in 1965.

Levenshtein distance may also be referred to as edit distance, although it may also denote a larger family of distance metrics. It is closely related to pairwise string alignments.

Definition

Mathematically, the Levenshtein distance between two strings, a and b (of length |a| and |b| respectively), is given bylev a,b(|a|,|b|) where:

Levenshtein distance between two strings

Here, 1(ai≠bi) is the indicator function equal to 0 when ai≠bi and equal to 1 otherwise, and leva, b(i,j) is the distance between the first i characters of a and the first j characters of b.

Note that the first element in the minimum corresponds to deletion (from a to b), the second to insertion and the third to match or mismatch, depending on whether the respective symbols are the same.

Example

The Levenshtein distance between “FLOMAX” and “VOLMAX” is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:

 Levenshtein distance between FLOMAX & VOLMAX

Levenshtein distance between “GILY” and “GEELY” is 2.

THREE

Levenshtein distance between “HONDA” and “HYUNDAI” is 3.

Levenshtein distance between HONDA & HYUNDAI

Application

  • String Matching.

  • Spelling Checking.

Dynamic Programming Approach

The Levenshtein algorithm calculates the least number of edit operations that are necessary to modify one string to obtain another string. The most common way of calculating this is by the dynamic programming approach:

  1. A matrix is initialized measuring in the (m, n) cell the Levenshtein distance between the m-character prefix of one with the n-prefix of the other word.
  2. The matrix can be filled from the upper left to the lower right corner.
  3. Each jump horizontally or vertically corresponds to an insert or a delete, respectively.
  4. The cost is normally set to 1 for each of the operations.
  5. The diagonal jump can cost either one, if the two characters in the row and column do not match else 0, if they match. Each cell always minimizes the cost locally.
  6. This way the number in the lower right corner is the Levenshtein distance between both words.

An example that features the comparison of “HONDA” and “HYUNDAI”:

Vladimir_Levenshtein Matrix

The following is two representations, the Levenshtein distance between “HONDA” and “HYUNDAI” is 3.

Levenshtein distance between HONDA & HYUNDAI

Use Case

In approximate string matching, the objective is to find matches for short strings in many longer texts, in situations where a small number of differences are to be expected. The short strings could come from a dictionary, for instance. Here, one of the strings is typically short, while the other is arbitrarily long. This has a wide range of applications, for instance, spell checkers, correction systems for optical character recognition and software to assist natural language translation based on translation memory.

The Levenshtein distance can also be computed between two longer strings. But the cost to compute it, which is roughly proportional to the product of the two string lengths, makes this impractical. Thus, when used to aid in fuzzy string searching in applications such as record linkage, the compared strings are usually short to help improve the speed of comparisons.

Strings Data Types Algorithm

Published at DZone with permission of Nikhil Babar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Cryptography Module in Mule 4
  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!