Dynamic Programming Algorithm (DPA) for Edit-Distance

python 经典算法之--动态规划算法(Dynamic Programming Algorithm) 它通过将原问题分解为若干子问题,逐个求解子问题的最优解,从而得到原问题的最优解。= s2[j-1]时,dp[i][j] = max(dp[i-1][j], dp[i][j-1])。其中,dp[i-1][j]表示不选第i个物品,dp[i-1][j-w[i]]+v[i]表示选择第i个物品。当s1[i-1] == s2[j-1]时,dp[i][j] = dp[i-1][j-1] + 1;初始状态为dp[0][j] = 0和dp[i][0] = 0。初始状态为dp[i][0] = 0和dp[0][j] = 0。 阅读详情

The words `computer' and `commuter' are very similar, and a change of just one letter, p->m will change the first word into the second. The word `sport' can be changed into `spot' by the deletion of the `p', or equivalently, `spot' can be changed into `sport' by the insertion of `p'.

The edit distance of two strings, s1 and s2, is defined as the minimum number of point mutations required to change s1 into s2, where a point mutation is one of:

change a letter,
insert a letter or
delete a letter
 

The following recurrence relations define the edit distance, d(s1,s2), of two strings s1 and s2:

d('', '') = 0
d(s, '')  = d('', s) = |s|   -- i.e. length of s
d(s1+ch1, s2+ch2)
= min( d(s1, s2) + if ch1=ch2 then 0 else 1 fi,
d(s1+ch1, s2) + 1,
d(s1, s2+ch2) + 1 )
The first two rules above are obviously true, so it is only necessary consider the last one. Here, neither string is the empty string, so each has a last character, ch1 and ch2 respectively. Somehow, ch1 and ch2 have to be explained in an edit of s1+ch1 into s2+ch2. If ch1 equals ch2, they can be matched for no penalty, i.e. 0, and the overall edit distance is d(s1,s2). If ch1 differs from ch2, then ch1 could be changed into ch2, i.e. 1, giving an overall cost d(s1,s2)+1. Another possibility is to delete ch1 and edit s1 into s2+ch2, d(s1,s2+ch2)+1. The last possibility is to edit s1+ch1 into s2 and then insert ch2, d(s1+ch1,s2)+1. There are no other alternatives. We take the least expensive, i.e. min, of these alternatives.
 

The recurrence relations imply an obvious ternary-recursive routine. This is not a good idea because it is exponentially slow, and impractical for strings of more than a very few characters.

Examination of the relations reveals that d(s1,s2) depends only on d(s1',s2') where s1' is shorter than s1, or s2' is shorter than s2, or both. This allows the dynamic programming technique to be used.

A two-dimensional matrix, m[0..|s1|,0..|s2|] is used to hold the edit distance values:

m[i,j] = d(s1[1..i], s2[1..j])
m[0, 0] = 0
m[i, 0] = i,  i=1..|s1|
m[0, j] = j,  j=1..|s2|
m[i,j] = min( m[i-1,j-1]
+ if s1[i]=s2[j] then 0 else 1 fi,
m[i-1, j] + 1,
m[i, j-1] + 1 ),    i=1..|s1|, j=1..|s2|
m[,] can be computed row by row. Row m[i,] depends only on row m[i-1,]. The time complexity of this algorithm is O(|s1|*|s2|). If s1 and s2 have a `similar' length, about `n' say, this complexity is O(n2), much better than exponential!

 

 

Complexity
The time-complexity of the algorithm is O(|s1|*|s2|), i.e. O(n2) if the lengths of both strings is about `n'. The space-complexity is also O(n2) if the whole of the matrix is kept for a trace-back to find an optimal alignment. If only the value of the edit distance is needed, only two rows of the matrix need be allocated; they can be "recycled", and the space complexity is then O(|s1|), i.e. O(n).

Variations
The costs of the point mutations can be varied to be numbers other than 0 or 1. Linear gap-costs are sometimes used where a run of insertions (or deletions) of length `x', has a cost of `ax+b', for constants `a' and `b'. If b>0, this penalises numerous short runs of insertions and deletions.

Longest Common Subsequence
The longest common subsequence (LCS) of two sequences, s1 and s2, is a subsequence of both s1 and of s2 of maximum possible length. The more alike that s1 and s2 are, the longer is their LCS.

Other Algorithms
There are faster algorithms for the edit distance problem, and for similar problems. Some of these algorithms are fast if certain conditions hold, e.g. the strings are similar, or dissimilar, or the alphabet is large, etc..

Ukkonen (1983) gave an algorithm with worst case time complexity O(n*d), and the average complexity is O(n+d2), where n is the length of the strings, and d is their edit distance. This is fast for similar strings where d is small, i.e. when d<<n.

Applications
File Revision
The Unix command diff f1 f2 finds the difference between files f1 and f2, producing an edit script to convert f1 into f2. If two (or more) computers share copies of a large file F, and someone on machine-1 edits F=F.bak, making a few changes, to give F.new, it might be very expensive and/or slow to transmit the whole revised file F.new to machine-2. However, diff F.bak F.new will give a small edit script which can be transmitted quickly to machine-2 where the local copy of the file can be updated to equal F.new.

diff treats a whole line as a "character" and uses a special edit-distance algorithm that is fast when the "alphabet" is large and there are few chance matches between elements of the two strings (files). In contrast, there are many chance character-matches in DNA where the alphabet size is just 4, {A,C,G,T}.

Try `man diff' to see the manual entry for diff.

Remote Screen Update Problem
If a computer program on machine-1 is being used by someone from a screen on (distant) machine-2, e.g. via rlogin etc., then machine-1 may need to update the screen on machine-2 as the computation proceeds. One approach is for the program (on machine-1) to keep a "picture" of what the screen currently is (on machine-2) and another picture of what it should become. The differences can be found (by an algorithm related to edit-distance) and the differences transmitted... saving on transmission band-width.

Spelling Correction
Algorithms related to the edit distance may be used in spelling correctors. If a text contains a word, w, that is not in the dictionary, a `close' word, i.e. one with a small edit distance to w, may be suggested as a correction.

Transposition errors are common in written text. A transposition can be treated as a deletion plus an insertion, but a simple variation on the algorithm can treat a transposition as a single point mutation.

Plagiarism Detection
The edit distance provides an indication of similarity that might be too close in some situations ... think about it.

Molecular Biology
Example
An example of a DNA sequence from `Genebank' can be found [here]. The simple edit distance algorithm would normally be run on sequences of at most a few thousand bases.
 

The edit distance gives an indication of how `close' two strings are. Similar measures are used to compute a distance between DNA sequences (strings over {A,C,G,T}, or protein sequences (over an alphabet of 20 amino acids), for various purposes, e.g.:

to find genes or proteins that may have shared functions or properties
to infer family relationships and evolutionary trees over different organisms
 

Speech Recognition
Algorithms similar to those for the edit-distance problem are used in some speech recognition systems: find a close match between a new utterance and one in a library of classified utterances.

Notes
V. I. Levenshtein. Binary codes capable of correcting deletions, insertions and reversals. Doklady Akademii Nauk SSSR 163(4) p845-848, 1965,
also Soviet Physics Doklady 10(8) p707-710, Feb 1966.
Discovered the basic DPA for edit distance.
S. B. Needleman and C. D. Wunsch. A general method applicable to the search for similarities in the amino acid sequence of two proteins. Jrnl Molec. Biol. 48 p443-453, 1970.
Defined a similarity score on molecular-biology sequences, with an O(n2) algorithm that is closely related to those discussed here.
Hirschberg (1975) presented a method of recovering an alignment (of an LCS) in O(n2) time but in only linear, O(n)-space; see [here].
E. Ukkonen On approximate string matching. Proc. Int. Conf. on Foundations of Comp. Theory, Springer-Verlag, LNCS 158 p487-495, 1983.
Worst case O(nd)-time, average case O(n+d2)-time algorithm for edit-distance, where d is the edit-distance between the two strings.
See also exact, as opposed to approximate, (sub-)string [matching].
More research information on "the" DPA and Bioinformatics [here].
If your programming language does not support 2-dimensional arrays, and requires arrays or strings to indexed from zero upwards, some home-grown address translation will be needed to program the DPA defined above.

四、Dynamic-programming algorithm Dynamic--LCS (学习笔记,无什参考价值!) 1 问题 2 算法 2.1 Brute-force LCS algorithm 检查每一个subsequence是否是yyy的子列时,遍历yyy的每一个元素,看是否依次可以全部覆盖subsequence所有元素,所以其复杂度为O(n)O(n)O(n); 2.2 Dynamic-programming hallmark #1 动态规划的第一特征–最优子结构,下面用定理的方式证明这种特征; 这个性质是说,一个规模稍大的最优解问题建立在一些规模较小的最优解问题基础 阅读详情

相关推荐

动态规划算法——背包问题(Dynamic Programming Algorithm - Knapsack Problem)

动态规划算法——背包问题(Dynamic Programming Algorithm - Knapsack Problem)背包问题(Knapsack Problem) (picture is from https://en.wikipedia.org/wiki/Knapsack_problem) The knapsack problem is a problem in combinator

UoM_XiaoShuaiShuai的博客 4963

Dynamic Programming(I)

Topics: Dynamic Programming(DP) paradigm Assembly-Line Scheduling Matrix-Chain Multiplication Rod Cutting Optimization Problems A design technique, like divide-and-conquer. Works bottom-up rather then top-down. Useful for optimization problems. Four-ste

weixin_45018859的博客 403

【ROS2】MOMO的鱼香ROS2(三)ROS2入门篇——ROS2第一个节点

【ROS2】MOMO的鱼香ROS2(三)ROS2入门篇——ROS2第一个节点

qq_44703886的博客 3628

【算法】动态规划(Dynamic Programming)

文章目录什么是动态规划算法数字三角形经典递归解法 什么是动态规划算法 总体思想 (1)动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题 (2)经分解得到的子问题往往不是互相独立的,有些子问题被重复计算多次 (3)如果能够保存已解决的子问题的答案,而在需要时再找出已求得的答案,就可以避免大量重复计算,从而得到多项式时间算法(备忘录法) (4)图解: 使用动态规划来求解的问题需要具备的基本要素包括: (1)重复子问题 递归算法求解问题时,每次产生的子问题并不总是新问题,有些子

高朗的博客 4万+

DVWA靶场实战:从搭建到渗透测试的完整指南

本文详细介绍了DVWA靶场的搭建与渗透测试全流程,涵盖LAMP环境搭建、Docker部署及集成环境安装三种方式。通过SQL注入、文件上传漏洞等实战案例,帮助读者掌握Web安全测试技巧,并提供防御方案与进阶调优建议,是网络安全学习的实用指南。

weixin_28312391的博客 491

2019年上交电院计算机直博保研经历

本人选报的是上交六月一日在武汉举办的直博选拔,五月三十一日到达酒店,地点在武汉大学旁边(PS:上交是不管住宿、来回车票的,~划重点~包括夏令营),入住后当然是紧张的准备英语面试等等内容了,当天我是排在了下午面试,感觉还很好,上午可以多睡一会,最好能够去踩踩点上午,然后下午面试一定要早去,说是一点开始,但是老师十二点半就开始让学生进去了,有一个同学排在第二个,但是当时叫到他时还不到一点,没到,最后据...

LifeMaker的博客 4903

动态规划算法(Dynamic Programming)

动态规划是一种用于解决具有重叠子问题和最优子结构性质的复杂问题的算法设计技巧。它将问题分解为更小的子问题,并通过保存这些子问题的解来避免重复计算。

Zaczc的博客 1743

RAG-Anything 实战指南:从零构建企业级多模态知识库

本文是RAG-Anything的实战指南,详细介绍了如何从零构建企业级多模态知识库。通过一体化框架,它能无差别解析文本、表格、图片、公式等多格式文档,并构建可深度理解和智能检索的知识图谱。指南涵盖了环境搭建、核心实战、智能查询及生产级部署优化,帮助企业将杂乱文档转化为智能知识大脑。

pandas7gardener的博客 243

算法专题:动态规划(Dynamic Programming)

算法专题:动态规划(Dynamic Programming)算法专题:动态规划(Dynamic Programming)算法专题:动态规划(Dynamic Programming)算法专题:动态规划(Dynamic Programming)算法专题:动态规划(Dynamic Programming)

weixin_49376454的博客 1723

蓝桥杯算法:打印图形——六芒星

【输入格式】 输入一行包含一个整数 n。 【输出格式】 输出一个六芒星,表示满足题意的图形。 【样例输入】 3 4 【样例输出】 【评测用例规模与约定】 对于所有评测用例,1 ≤ n ≤ 20。 总结: 第一次做图形题,做了蛮久,图形题有很多的方式去完成,它考验的是你能不能找到更简单的那一种方式. 我初看到这个图形,第一种想法是把这个六芒星当做六个小三角形来做,然后以某种规则排列一下,就能做出来,但是尝试了一下感觉太麻烦了。 然后我的第二种想法是,这个图形是对称图形,怎么能写出一半吗? 这样另一半想办

KeepCodeing 3843

结构光相移法-多频外差原理与实践

结构光相移法是一种常用的三维形貌测量技术,它通过对投射到目标物体上的结构光进行相位调制和解调,实现对物体表面的三维重建。其中,多频外差原理是结构光相移法的核心原理之一,通过使用不同频率的结构光来实现相位测量,并可以提高测量精度和抗干扰性能。结构光相移法的多频外差原理是一种常用的三维形貌测量技术,它通过使用多个不同频率的结构光进行相位调制和解调,实现对物体表面的三维重建。本文介绍了多频外差原理的原理和实践应用,并提供了相应的Python源代码,希望对读者理解和应用结构光相移法有所帮助。

CodeByte的博客 737

[转载]Dynamic Programming Algorithm (DPA) for Edit-Distance

转自:http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Dynamic/Edit/ The words `computer and `commuter are very similar, and a change of just one letter, p->m will change the first word into the sec

Faith Seeker 739

AI辅助Android开发:从传统到智能化的技术演进

AI辅助开发的核心在于利用算法模型自动化或优化传统任务。在Android开发中,AI可应用于需求分析、设计、编码、测试和维护各阶段。需求分析:AI工具如基于NLP的模型(例如GPT系列)可解析用户需求文档,自动生成用户故事和原型设计。假设产品需求为“开发一个航班查询应用”,AI能提取关键词,输出UML图或伪代码。代码生成:使用AI代码助手(如GitHub Copilot),工程师输入自然语言描述,AI生成Java/Kotlin代码片段。

郑伟强dev的专栏 138

编辑距离 算法 Dynamic Programming Algorithm (DPA) for Edit-Distance

关于两个字符串s1,s2的差别,可以通过计算他们的最小编辑距离来决定。        所谓的编辑距离: 让s1和s2变成相同字符串需要下面操作的最小次数。 1.         把某个字符ch1变成ch2 2.         删除某个字符 3.         插入某个字符 例如      s1 = “12433” 和s2=”1233”;                      则

1819

【WRF-Urban】使用 NUDAPT 数据集与 BEP/BEP_BEM 方案的 WRF 配置指南

【WRF-Urban】使用 NUDAPT 数据集与 BEP/BEP_BEM 方案的 WRF 配置指南

WW、forever的博客 1965

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Dynamic programming algorithm

Optimal computer folding of large RNA sequences using thermodynamics and auxiliary information Year: 1981 Authors: Michael Zuker and Patrick Stiegler Journal Name: Nucleic Acids Research Motivation 将动态规划算法与热动力学数据结合。 Research Objective 基于动态规划算法提出一种更有效、快速、并且

weixin_43269419的博客 482

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

算法-动态规划 Dynamic Programming--从菜鸟到老鸟

前言最近在牛客网上做了几套公司的真题,发现有关动态规划(Dynamic Programming)算法的题目很多。相对于我来说,算法里面遇到的问题里面感觉最难的也就是动态规划(Dynamic Programming)算法了,于是花了好长时间,查找了相关的文献和资料准备彻底的理解动态规划(Dynamic Programming)算法。一是帮助自己总结知识点,二是也能够帮助他人更好的理解这个算法。后面的参

有图有真相 48万+

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

基础算法总结(一)Algorithm(动态规划 Dynamic Programming)

动态规划(Dynamic Programming) DP定义:   动态规划是分治思想的延伸,通俗一点来说就是大事化小,小事化无的艺术。   在将大问题化解为小问题的分治过程中,保存对这些小问题已经处理好的结果,并供后面处理更大规模的问题时直接使用这些结果。 动态规划具备了以下三个特点:   1. 把原来的问题分解成了几个相似的子问题。   2. 所有的子问题都只需要解决一次。   3. 储存子问题的解。 动态规划的本质:   是对问题状态的定义和状态转移方程的定义(状态以及状态之间的递推关系)。 动态规

时光若止-Dale 937

动态规划(Dynamic Programming)与贪心算法(Greedy Algorithms)

动态规划动态规划是用来解决优化问题的,即要作出一组选择以达到最优解。动态规划有两个标志:一是可以划分最优子问题,二是子问题有重叠性(可以自底向上存储这些子问题的解,把算法效率从指数时间降到多项式时间)。例如,动态规划可以运用在矩阵相乘上。在矩阵连乘的时候,在中间增加括号是不影响最后结果的。也就是说 A * B * C = A * ( B * C )。假设A、B、C的维数为10 X 100,

無名黑洞 6859

Dynamic Programming Algorithms

There are two ways to implement a DP algorithm: Bottom-up, also known as tabulation Top-down, also known as memoization Bottom-up (Tabulation) Bottom-up is implemented with iteration and starts at the base cases. Using the Fibonacci sequence as an examp

CHNMSCS 526

算法之动态规划(Dynamic Programming)

1、介绍 (1)   动态规划是解决多阶段决策过程最优化的一种有效的数学方法,他是美国学者Richard.bellman在1951年提出的,1957年他的专著《动态规划》的问世标志着运筹学的一个重要分支—-动态规划的诞生。   所谓多阶段决策问题是指这样一类问题,该问题的决策过程时一种在多个相互联系的阶段分别作出决策以形成序列决策的过程,而这些决策均是根据总体最优化这一共同的目标而采取的。 ...

强子的博客 2万+

Introduction to Algorithm ( chapter 15: Dynamic Programming)

Dynamic programming is a very powerful algorithmic paradigm in which a problem is solved by identifying a collection of subproblems and tackling them one by one, smallest first, using the answers to

liucheng17的专栏 902

Dynamic Programming(动态规划算法题总结)

写在前面 暑假也是闲来无事,就想做做动态规划的题。想想在过去不久的第十届蓝桥杯国赛经历,几乎全是动态规划的题,虽然有个别题可以用深搜广搜解决,但是能过的样例不超过30%,都会超时的。因此我想总结一下。希望不会让我掉头发,这太烧脑了… 1.矩形覆盖 我们可以用2 * 1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2 * 1的小矩形无重叠地覆盖一个2 * n的大矩形,总共有多少种方法? 思路 这个...

小白的Coding之旅 342

动态规划(Dynamic Programming)算法与LC实例的理解

动态规划(Dynamic Programming)算法与LC实例的理解 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路。也欢迎大家来给我的Github的Leetcode算法项目点star呀~~ 动态规划(Dynamic Programming)算法与LC实例的理解 DP是什么 基本定义 帮助理解的经典问题:硬币问题 第二个经典问题:斐波那契数列 ...

MapleStory的博客 4632

动态规划(dynamic programming)

概念 动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法。20世纪50年代初美国数学家R.E.Bellman等人在研究多阶段决策过程(multistep decision process)的优化问题时,提出了著名的最优化原理(principle of optimality),把多阶段过程转化为一系列单阶段问题,...

浮华饰浮华的博客 419

经典算法之动态规划(Dynamic Programming)

1、动态规划的定义   动态规划,dynamic Programming,是一种高效解决问题的方法,使用与具有重复子问题和最优子结构的问题。 2、动态规划的思想   动态规划算法通常用于求解具有某种最优性质的问题。在这类问题中,可能会有许多可行解。每一个解都对应于一个值,我们希望找到具有最优值的解。动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题,先求解子问题,然后从这...

青萍之末的博客 3067
上一篇: 如何运行C++ STL程序
vector_conductor
博客等级 码龄20年 0粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值