算法导论上机报告.docx

上传人:b****6 文档编号:3323888 上传时间:2022-11-21 格式:DOCX 页数:11 大小:19.98KB
下载 相关 举报
算法导论上机报告.docx_第1页
第1页 / 共11页
算法导论上机报告.docx_第2页
第2页 / 共11页
算法导论上机报告.docx_第3页
第3页 / 共11页
算法导论上机报告.docx_第4页
第4页 / 共11页
算法导论上机报告.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

算法导论上机报告.docx

《算法导论上机报告.docx》由会员分享,可在线阅读,更多相关《算法导论上机报告.docx(11页珍藏版)》请在冰豆网上搜索。

算法导论上机报告.docx

算法导论上机报告

编辑距离

编辑距离(EditDistance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。

许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。

一般来说,编辑距离越小,两个串的相似度越大。

例如将kitten一字转成sitting:

sitten(k→s)

sittin(e→i)

sitting(→g)

(a)

Solution:

OperationzCostTotal

initialstring00

right00

insertn33

inserta36

rightanalgorithm06

replacebyy410

replacebys414

replacebyi418

replacebys422

delete224

delete226

deleteanalysis228

(b)Solution:

WearguethatthereisasequenceSthattransformsxtoywithcostd(x,y)

withoutusingany“left”operationsbycontradiction.Supposethatnosuchsequence

Sexists.ConsiderasequenceS1thatdoestransformxtoywithcostd(x,y)that

uses“left”operations.ConsiderthecharactersinsertedbytheoperationsinS1.Ifa

characterisinsertedandthenlaterdeleted,thenbothoperationscanberemovedfrom

s1toproduceasequenceS2thatproducesthesameresultatlowercost.Ifacharacter

aisinsertedandthenlaterreplacedbyacharacterb,thentheinsertoperationcanbe

changedtoinsertbandthereplaceoperationcanberemovedtoproduceasequenceS2

thatproducesthesameresultatlowercost.Ifacharacterisreplacedbyacharactera

andthenreplacedagainbyacharacterb,thenbothoperationscanbereplacedby“re

placeb”.Thismeansthatallinsertedandreplacedcharactersareneverchangedafter

performingtheinsertionorreplacement.Noticethatafterremovingtheseoperations

thatintroducedependencies,anysequenceofinsert,delete,andreplaceoperationscan

bereorderedsothattheyoccurfromlefttorightwithoutaffectingtheoutcomeofthe

transformation.

(c)Weshowthatcomputingeditdistanceforstringsxandycanbedonebyfindingthe

editdistanceofsubproblems.Defineacostfunction

(i,j)=d(x,y[1..i]||x[j+1..m])

(1)

Thatis

(i,j)istheminimumcostoftransformingthefirstjcharactersofxinto

thefirsticharactersofy.Thend(x,y)=

(n,m).Nowconsiderasequenceof

operationsS=(o1,o2,...,ok)thattransformsxtoywithcostC(S)=d(x,y).

LetSibethesubsequenceofScontainingthefirstioperationsofS.Letzibethe

auxilliarystringafterperformingoperationsSi,wherez0=xandzk=y.

(d)Wecancalculatetheeditdistanced(x,y)usingthedefinitiono𝐜𝐱𝐲(i,j)fromEqua

tion1.Recallthatd(x,y)=

(m,n).Sinceweshowedinpart(a)thatthereexists

asequenceofoperationsthatachievesd(x,y)withoutusingthe“left”operation,we

onlyneedtoconsiderthefouroperations“right”,“replace”,“delete”,and“insert”.

Wecancalculate

(m,n)recursively.Thebasecaseiswhennotransformationop

erationshavebeenperformed,so𝐜𝐱𝐲(0,0)=0.

(e)ConstructatableTwhereeachentryT[i,j]=

(i,j).Sinceeachvalue

of

(i,j)onlydependson𝐜𝐱𝐲(i1,j2)wherei1<=iandj1<=j,wecancomputethe

entriesofTrowbyrowusingEquation2:

EDIT-DISTANCE(x[1..m],y[1..n])

1T[0,0]←0

2fori←1ton

3forj←1tom

4doT[i,j]←min𝐓𝐢−𝟏,𝐣−𝟏,𝐢𝐟𝐢>𝟎𝐚𝐧𝐝𝐣>𝟎𝐚𝐧𝐝𝐱[𝐢−𝟏]=𝐲[𝐣−𝟏]𝐓𝐢−𝟏,𝐣−𝟏+𝟒,𝐢𝐟𝐢>𝟎𝐚𝐧𝐝𝐣>𝟎𝐓𝐢−𝟏,𝐣+𝟐,𝐢𝐟𝐢>𝟎𝐓𝐢,𝐣−𝟏+𝟑,𝐢𝐟𝐣>𝟎

5returnT[n,m]

Therunningtimeofthisalgorithmis

(mn).Thisalgorithmrequires

(mn)space.

(f)

x:

electricalengineering

y:

computerscience

EditDistance:

54

Oper|c|Total|z

initial|0|0|*electricalengineering

delete|2|2|*lectricalengineering

delete|2|4|*ectricalengineering

delete|2|6|*ctricalengineering

right|0|6|c*tricalengineering

insert|3|9|co*tricalengineering

insert|3|12|com*tricalengineering

insert|3|15|comp*tricalengineering

insert|3|18|compu*tricalengineering

right|0|18|comput*ricalengineering

insert|3|21|compute*ricalengineering

right|0|21|computer*icalengineering

delete|2|23|computer*calengineering

delete|2|25|computer*alengineering

delete|2|27|computer*lengineering

delete|2|29|computer*engineering

right|0|29|computer*engineering

delete|2|31|computer*ngineering

replace|4|35|computers*gineering

replace|4|39|computersc*ineering

right|0|39|computersci*neering

delete|2|41|computersci*eering

delete|2|43|computersci*ering

right|0|43|computerscie*ring

delete|2|45|computerscie*ing

delete|2|47|computerscie*ng

right|0|47|computerscien*g

insert|3|50|computerscienc*g

replace|4|54|computerscience*

(g)InputFiled(x,y)

Input11816

Input21824

Input31829

(h)AllthetransformationoperationscanbeimplementedinO

(1)timeusing

twostacks,LandR.Initially,LisemptyandRcontainsallthecharactersofxin

order.

OperationImplementation

leftIfnotEMPTY(L),thenPUSH(R,POP(L))

rightIfnotEMPTY(R),thenPUSH(L,POP(R))

replacebycIfnotEMPTY(R),thenPOP(R),PUSH(L,c)

deleteIfnotEMPTY(R),thenPOP(R)

insertcPUSH(L,c)

EachstackoperationrequiresO

(1)time,andeachtransformationoperationrequires

onlyO

(1)stackoperations.ThereforeeachoperationrequiresO

(1)time.

代码部分:

#include

#include

#include

#include

//OperationCosts

#defineMOVE_COST0

#defineREPLACE_COST4

#defineINSERT_COST3

#defineDELETE_COST2

//OperationTypes

typedefenum{initial,right,insert,delete,replace,final}opType;

char*opStrings[]={"initial","right","insert","delete","replace","final"};

//Recoversandprintsouttheeditsnecessarytochangexintoy.

staticvoidrecoverEdits(char*x,intm,

char*y,intn,

int**d,opType**opPerformed);

//Computestheeditdistancebetweenxandy

staticvoidcomputeEditDistance(char*x,intm,

char*y,intn){

//Inthisdynamicprogram,d[i][j]storestheeditdistancebetween

//thestringx[i..m-1]andy[j..n-1].

//NOTE!

Carraysstartindexingat0.

int**d;

opType**opPerformed;//storeoperationsforreconstructingtheanswer

opTypeopDone;

inti,j;

d=(int**)malloc((m+1)*sizeof(int*));

assert(d!

=NULL);

opPerformed=(opType**)malloc((m+1)*sizeof(opType*));

assert(opPerformed!

=NULL);

for(i=0;i<=m;i++){

d[i]=(int*)malloc((n+1)*sizeof(int));

assert(d[i]!

=NULL);

opPerformed[i]=(opType*)malloc((n+1)*sizeof(opType));

assert(opPerformed[i]!

=NULL);

}

//Initializethebasecases.

//x[m]andy[n]arebothnullstrings.Editdistancebetweenthemis0.

d[m][n]=0;

opPerformed[m][n]=final;

for(i=0;i

//Toconvertx[i..m-1]tothenullstringy[n],deleteall(m-i)

//remainingcharactersinx.

d[i][n]=DELETE_COST*(m-i);

opPerformed[i][n]=delete;

}

for(j=0;j

//Toconvertx[m](thenullstring)intoy[j..n-1],insert

//themissingn-jcharacters.

d[m][j]=INSERT_COST*(n-j);

opPerformed[m][j]=insert;

}

//Startatd[m][n]andloopbackwards

for(i=m-1;i>=0;i--){

for(j=n-1;j>=0;j--){

intcostForReplaceOrMove;

intcostForInsert;

intcostForDelete;

intminValue;

//Computed[i][j]astheminimumof4terms:

//Ifx[i]==y[j],wecouldmoveright.

//Otherwise,wecanreplacex[i]withy[j]and

//incrementiandj.

costForReplaceOrMove=d[i+1][j+1]+REPLACE_COST*(x[i]!

=y[j])+MOVE_COST*(x[i]==y[j]);

//Ifweinsertacharacterintoxtomatchy[j],then

//weincrementjby1

costForInsert=d[i][j+1]+INSERT_COST;

//Ifwedeleteacharacterinx,thenwe

//incrementiby1.

costForDelete=d[i+1][j]+DELETE_COST;

//Oftheaboveoperations,findonethatgivesus

//aminimumcost.

minValue=costForReplaceOrMove;

if(x[i]!

=y[j]){

opDone=replace;

}

else{

opDone=right;

}

if(minValue>costForInsert){

minValue=costForInsert;

opDone=insert;

}

if(minValue>costForDelete){

minValue=costForDelete;

opDone=delete;

}

d[i][j]=minValue;

opPerformed[i][j]=opDone;

}

}

//Finalanswer

printf("EditDistance=%d\n",d[0][0]);

//Recoveroperations/printoutansweronlyifthestringsaren'ttoolong.

if((m<75)&&(n<75)){

recoverEdits(x,m,y,n,d,opPerformed);

}

for(i=0;i<=m;i++){

free(d[i]);

free(opPerformed[i]);

}

free(d);

free(opPerformed);

}

staticvoidrecoverEdits(char*x,intm,

char*y,intn,

int**d,opType**opPerformed){

inti=0;

intj=0;

intk=0;

inta;

intopDone;

intcostSoFar=0;

intstepCost=0;

char*newString;

newString=(char*)malloc(sizeof(char)*(m+n));

//Ratherthanimplementingastringobjectforxthatallowsus

//constant-timeinsertsanddeletesatthecursor,

//wearejustgoingtohavetwocopiesofthestring.

//newString[0..k]storeseverythingbeforethecursorthatwehave

//changed,andx[i..m-1]willstoreallthepartsafterthecursorthat

//arestillthesame.

strncpy(newString,x,m);

printf("\n");

printf("(i,j):

%8s|c|Total|z\n","Oper");

printf("---------------------------------------------------------------------\n");

printf("(%2d,%2d):

%8s|%4d|%4d|",i,j,"initial",costSoFar,stepCost);

printf("*%s\n",x);

while(opPerformed[i][j]!

=final){

opDone=opPerformed[i][j];

switch(opDone){

caseright:

newString[k]=x[i];

i++;

j++;

stepCost=MOVE_COST;

break;

casereplace:

newString[k]=y[j];

i++;

j++;

stepCost=REPLACE_COST;

break;

caseinsert:

newString[k]=y[j];

j++;

stepCost=INSERT_COST;

break;

casedelete:

i++;

stepCost=DELETE_COST;

break;

default:

//Weshouldneverafollowapointertoinitialorfinal.

printf("ERROR.\n");

exit

(1);

break;

}

if(opDone!

=delete){

k++;

}

costSoFar+=stepCost;

printf("(%2d,%2d):

%8s|

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1