Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 909439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:52:42+00:00 2026-05-15T16:52:42+00:00

I recently posted a question about optimizing the algorithm to compute the Levenshtein Distance,

  • 0

I recently posted a question about optimizing the algorithm to compute the Levenshtein Distance, and the replies lead me to the Wikipedia article on Levenshtein Distance.

The article mentioned that if there is a bound k on the maximum distance a possible result can be from the given query, then the running time can be reduced from O(mn) to O(kn), m and n being the lengths of the strings. I looked up the algorithm, but I couldn’t really figure out how to implement it. I was hoping to get some leads on that here.

The optimization is #4 under “Possible Improvements”.

The part that confused me was the one that said that we only need to compute a diagonal stripe of width 2k+1, centered on the main diagonal (the main diagonal is defined as coordinates (i,i)).

If someone could offer some help/insight, I would really appreciate it. If needed, I can post the complete description of the algorithm in the book as an answer here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:52:43+00:00Added an answer on May 15, 2026 at 4:52 pm

    I’ve done it a number of times. The way I do it is with a recursive depth-first tree-walk of the game tree of possible changes. There is a budget k of changes, that I use to prune the tree. With that routine in hand, first I run it with k=0, then k=1, then k=2 until I either get a hit or I don’t want to go any higher.

    char* a = /* string 1 */;
    char* b = /* string 2 */;
    int na = strlen(a);
    int nb = strlen(b);
    bool walk(int ia, int ib, int k){
      /* if the budget is exhausted, prune the search */
      if (k < 0) return false;
      /* if at end of both strings we have a match */
      if (ia == na && ib == nb) return true;
      /* if the first characters match, continue walking with no reduction in budget */
      if (ia < na && ib < nb && a[ia] == b[ib] && walk(ia+1, ib+1, k)) return true;
      /* if the first characters don't match, assume there is a 1-character replacement */
      if (ia < na && ib < nb && a[ia] != b[ib] && walk(ia+1, ib+1, k-1)) return true;
      /* try assuming there is an extra character in a */
      if (ia < na && walk(ia+1, ib, k-1)) return true;
      /* try assuming there is an extra character in b */
      if (ib < nb && walk(ia, ib+1, k-1)) return true;
      /* if none of those worked, I give up */
      return false;
    }
    

    Added to explain trie-search:

    // definition of trie-node:
    struct TNode {
      TNode* pa[128]; // for each possible character, pointer to subnode
    };
    
    // simple trie-walk of a node
    // key is the input word, answer is the output word,
    // i is the character position, and hdis is the hamming distance.
    void walk(TNode* p, char key[], char answer[], int i, int hdis){
      // If this is the end of a word in the trie, it is marked as
      // having something non-null under the '\0' entry of the trie.
      if (p->pa[0] != null){
        if (key[i] == '\0') printf("answer = %s, hdis = %d\n", answer, hdis);
      }
      // for every actual subnode of the trie
      for(char c = 1; c < 128; c++){
        // if it is a real subnode
        if (p->pa[c] != null){
          // keep track of the answer word represented by the trie
          answer[i] = c; answer[i+1] = '\0';
          // and walk that subnode
          // If the answer disagrees with the key, increment the hamming distance
          walk(p->pa[c], key, answer, i+1, (answer[i]==key[i] ? hdis : hdis+1));
        }
      }
    }
    // Note: you have to edit this to handle short keys.
    // Simplest is to just append a lot of '\0' bytes to the key.
    

    Now, to limit it to a budget, just refuse to descend if hdis is too large.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently posted a question about a WCF Restful web service that I am
I recently posted this question about codes for a gift-card-like voucher that users can
I recently posted a question about screen rotation in my live wallpaper. To test
I recently posted a question about centering a page with CSS. I figured out
I recently posted a question about getting a NullPointerException whenever I called an array
From another question I recently posted, it seems that Classic ASP might not be
I recently posted a question about getting last 3 results in table in the
Recently i posted a question about what the variable is of a file which
I recently posted a question here about some memory issues I was having. I've
Relatively new to python. I recently posted a question in regards to validating that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.