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

  • SEARCH
  • Home
  • 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 7604389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:53:50+00:00 2026-05-30T23:53:50+00:00

I am trying to improve an existing javascript levenstein distance calculation source code to

  • 0

I am trying to improve an existing javascript levenstein distance calculation source code to generate a martix with not only value of the current setps, but also with actions taken (insert, replace, delete or match)

I get the wrong results in an “actions” matrix:

levenstein actions

in the algorithm we see that (not js, from wikipedia):

 d[i, j] := minimum
                    (
                      d[i-1, j] + 1,  // a deletion
                      d[i, j-1] + 1,  // an insertion
                      d[i-1, j-1] + 1 // a substitution
                    )

So in my JS code I do the following:

// Step 6
d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);

// a deletion
if(d[i][j] == d[i - 1][j] + 1) {
    actions[i][j] = 'D';
}

// a insertion
if(d[i][j] == d[i][j - 1] + 1) {
    actions[i][j] = 'I';
}

// a substitution
if(d[i][j] == d[i - 1][j - 1] + cost) {
    actions[i][j] = 'R';
}

A d matrix (two-dimensional array) is for values, and it get’s populated with correct values.
But why the corresponding actions matrix displays not what logically algorithm would do?

What am I doing wrong with respect to assigning them ‘I’, ‘R’, ‘D’? Or is it correct and just doesn’t seem logical to me, since I thought in the aforeshown scenario, an insertion would take place in the second step.

BTW, is it actually sensible to generate such “actions” matrix in case of a Levenstein algorithm?

  • 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-30T23:53:51+00:00Added an answer on May 30, 2026 at 11:53 pm

    There usually are many ways to generate a set of “actions” for any given Levensthein matrix. In your example you can trace back the resulting cost matrix always to the minimun and you’ll find quite a few paths.

    Here are some examples:

    (0,0)(0,1)(1,2)(1,3)(2,4)(3,5)
    
    (0,0)(1,1)(1,2)(1,3)(2,4)(3,5)
    
    (0,0)(0,1)(0,2)(1,3)(2,4)(3,5)
    

    So I could find at least three different interpretations of the same distance matrix. This means, unless you have some way to prefer directions (e.g. prefer substitutions over deletions over insertions), your matrix will be highly ambiguous.

    Now to the algorithm you proposed for filling up the action matrix: In your case you are implicitly prefering substituions (because they are checked last and will override previous choices) over insertions and insertions over deletions. That’s where all the Rs in your matrix come from. Let’s see what happens here:

    The proposed solution when we prefer substitutions is to insert A and N before anything else then replace M by N, A by A and X by S. If you check you can see that this will have a cost of four (two insertions and two “real” substitutions), which is exactly what the matrix determined (this is the last path in the paths I traced down).

    Now checking your action matrix again, what we find is, if we trace back from the final corner: R, R and R in places (3,5), (2,4) and (1,3). This corresponds to the final substituion of MAX to NAS. What is missing here however is the insertion of the leading AN which I traced out above. Looking at the matrix, one can see that there are numbers in the first row and colum, not actions. These however should be deletions and substitutions respectively in which case you can produce the final sequence SSRRR which has a cost of four for turning MAX into ANNAS.

    However you should be aware, that it is not really necessary to compute the actions in a matrix like you did, because all information will be available in the final cost matrix. You can always trace back the final cost matrix from the last corner to the first and you will be able to reconstruct all paths that can turn one word into another. However once you have fixed the actions in the action matrix, there will only be one path left of all the possibilities.

    This has to do a lot with the cost being well and uniquely defined whereas the paths may be highly ambiguous.

    EDIT

    Here is a full action matrix for the paths, which includes ambiguities:

    *   D     D     D 
    I   R    R/D    D
    I  R/I   R/I    R
    I  R/I   R/I   R/I
    I  R/I    R   R/I/D
    I  R/I    I     R
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to improve some existing code which originally took 3 minutes to prepare
I am trying to improve SQLite error handling in an existing C++ program. I
I was just trying to learn and understand jQuery source code (so far with
I'm trying to improve performance of an existing MySQL database. It's a database regarding
Im trying to improve the following CASE statement to calculate the difference only once.
I am trying to improve my SQL query so that i could get only
Trying to improve my code's agility and loading time, I thought of doing this,
I'm trying to optimize some existing code ( where speed is the primary criteria
I'm trying to improve my bucket sort for large number over 10000.I'm not quite
I am trying to add NOT NULL and a DEFAULT to an existing table.

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.