首页 > 其他分享 >[Algorithm] Dynamic programming - 01 - Drawing 2-d matrix

[Algorithm] Dynamic programming - 01 - Drawing 2-d matrix

时间:2023-03-28 14:33:59浏览次数:43  
标签:01 matrix Algorithm column many how edits need row

Problem: Levenshtein Distance

Write a function that takes in two strings and returns the minimum number of edit operations that need to be performed on the first string to obtain the second string. There are three edit operations: insertion of a character, deletion of a character, and substitution of a character for another.

str1 = "abc"
str2 = "yabd"
Sample Output
2 // insert "y"; substitute "c" for "d"

 

Code:

查看代码
 function minEditOps(str1, str2) {
  const m = str1.length;
  const n = str2.length;

  // Initialize a 2D array with zeros
  const dp = Array.from({ length: m + 1 }, () =>
    Array.from({ length: n + 1 }, () => 0)
  );

  // Fill in the first row and column with incremental values
  for (let i = 0; i <= m; i++) {
    dp[i][0] = i;
  }
  for (let j = 0; j <= n; j++) {
    dp[0][j] = j;
  }

  // Fill in the rest of the matrix using dynamic programming
  for (let i = 1; i <= m; i++) {
    for (let j = 1; j <= n; j++) {
      if (str1[i - 1] === str2[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        dp[i][j] =
          1 +
          Math.min(
            dp[i - 1][j - 1], // substitution
            dp[i - 1][j], // deletion
            dp[i][j - 1] // insertion
          );
      }
    }
  }

  // The minimum number of edit operations is at the bottom right corner of the matrix
  return dp[m][n];
}

 

Drawing a 2-d matrix

   ''  y a b d
''  0  1 2 3 4  -> insertion
a   1  1 1 2 3
b   2  2 2 1 2
c   3  3 3 2 2

|              \
deletion         substitution

 

Looking at first row:

   ''  y a b d
''  0  1 2 3 4  -> insertion

From left to right, we repeatly ask the question

from '' (column)to '' (row), how many edits we need? 0

from '' (column)to 'y', how many edits we need? insert 'y'

from '' (column)to 'a', how many edits we need? based on current 'y', to 'ya', insert 'a'

from '' (column)to 'b', how many edits we need? based on current 'ya', to 'yab', insert 'b'

from '' (column)to 'd', how many edits we need? based on current 'yab', to 'yabd', insert 'd'

Every time we move one column, we use the result based on previou column, and operation we do is insertion.

 

Looking at first column

   '' 
''  0  
a   1 
b   2
c   3 

|              
deletion

from '' (column)to '' (row), how many edits we need? 0

from 'a' (column)to ''(row), how many edits we need? delete 'a'

from 'b' (column)to ''(row), how many edits we need?  already deleted 'a', nowdelete 'b'

from 'c' (column)to ''(row), how many edits we need?  already deleted 'ab', nowdelete 'c'

Every time, we move down one row, we do deletion

 

When column(a) equals row(a)

   ''  y a b d
''  0  1 2 3 4  -> insertion
a   1  1 1
b   2   
c   3   

|              \
deletion         substitution

For example, we reach column(a) and row(a), because they are the same, so we do need to do anything, the same as column('') and row('y')case. This operation is substitution.

 

When column and row are different

''  y a b d
''  0  1 2 3 4  -> insertion
a   1  1 1 2 3
b   2  2 2 1 2
c   3  3 3 2 ?

|              \
deletion         substitution

For example the ?

 1 2
 2 ?

We check the values and get min value then +1. so ? = 1 + 1 = 2

 

标签:01,matrix,Algorithm,column,many,how,edits,need,row
From: https://www.cnblogs.com/Answer1215/p/17265040.html

相关文章

  • ubuntu12.04安装QQ2012教程
    Ubuntu(乌班图)是基于DebianGNU/Linux,支持x86、amd64(即x64)和ppc架构,由全球化的专业开发团队(CanonicalLtd)打造的开源GNU/Linux操作系统,为桌面虚拟化提供支持平台。Ubuntu系统......
  • Walkthrough-KIOPTRIX 2014
    0x01环境靶机地址:https://www.vulnhub.com/entry/kioptrix-2014-5,62/靶机默认网卡有点问题,移除网卡再新增网卡即可环境容易崩溃,崩溃了重启就好0x02过程1.信息收集......
  • [BZOJ3331][BeiJing2013]压力
    #include<iostream>#include<cmath>#include<algorithm>#include<cstring>#include<cstdio>#include<vector>usingnamespacestd;vector<int>temp[5211314],b......
  • 练习01_基本运算与程序结构
    通过之前的练习,我们掌握了Python的安装和环境配置,我们尝试了anaconda的jupyternotebook和百度飞桨PPAIStudio。两个都是非常方便的编程平台,使用者可以根据自己的要求和......
  • 【题解】[APIO2010] 信号覆盖
    题目分析:其实就是涉及四个点之间的位置关系,三个点形成圆判断是否包含另一个点。考虑四个点之间形成的多边形只可能是凸四边形或者是凹四边形,如下图所示:(上图为凸多边形)......
  • 百兆以太网PHY芯片,RPC8201F,瑞普康,替代瑞昱RTL8201
    国产以太网睿普康集成电路主要从事智能物联网通信芯片的设计、产品研发和销售,同时为客户提供技术服务支持。包括4G/5G蜂窝物联网芯片、以太网phy接口芯片、以太网供电芯......
  • Office2010安装错误1402问题
    http://blog.sina.com.cn/s/blog_555ea2470101831d.htmlsecedit/configure/cfg%windir%\inf\defltbase.inf/dbdefltbase.sdb/verbose......
  • python-01
    一:python2和python3的区别:1.python2中没有默认编码格式,如果遇到中英文的内容需要做声明coding=utf-8,python3已经默认系统中有coding=utf-8的编码格式print语句在python2......
  • 接口测试01
    一:接口的定义:统称为API,程序与程序之间的对接,又称为灰盒测试,偏逻辑测试为什么作接口测试:当界面功能没有出来时,测试人员可以尽早做接口测试,可以节省时间,可以突破前端的一些......
  • iPhone 5全新设计曝光,或于2012秋季发布
    来自BGR的消息,苹果将于明年秋季发布完全重新设计过的iPhone。新款手机将会采用塑料及橡胶材料,这些材料将用于新手机的外壳设计,环绕手机正面边缘部分(就像iPhone3GS那样),......