Edit Distance Algorithm and Knapsack Problem, Study notes of Algorithms and Programming

Lecture notes on the edit distance algorithm and its application to spell checkers, as well as an introduction to the knapsack problem and its solution using dynamic programming. The edit distance algorithm calculates the minimum number of edits (insertions, deletions, and substitutions) required to transform one string into another. The knapsack problem involves selecting items with given weights and values to maximize the total value within a given capacity.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-17r
koofers-user-17r 🇺🇸

10 documents

1 / 61

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Design and analysis of algorithms
Lecture 25& 26
Edyta Szyma´
nska
CS3510 A, Fall 2005 p. 1/16
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d

Partial preview of the text

Download Edit Distance Algorithm and Knapsack Problem and more Study notes Algorithms and Programming in PDF only on Docsity!

Design and analysis of algorithmsLecture 25& 26

Edyta Szyma ´nska [email protected]

Edit distance

Application: Spell Checkers

Edit distance

Application: Spell Checkers ocurrance^? “Perhaps you mean

occurrence^ ?”

Edit distance

Application: Spell Checkers ocurrance^? “Perhaps you mean

occurrence^ ?” ocurrance^ and occurrence^ are^

close by

Edit distance

Application: Spell Checkers ocurrance^? “Perhaps you mean

occurrence^ ?” ocurrance^ and occurrence^ are^

close by How to define “closeness”? minimum number of “edits”-insertions, deletions and substitutions ofcharacters needed to transform string

x^ into string^ y. Example 1:^ d(to, fro

)= 2, to→^ fo (substitution)

→^ fro (insertion)

Edit distance

Solution? - dynamic programming?

Edit distance

Solution? - dynamic programming ?What are the subproblems? Example 2:^ d(exponential, polynomial

) =?^ CS3510 A, Fall 2005 – p. 3/

Edit distance

Solution? - dynamic programming ?What are the subproblems? Example 2:^ d(exponential, polynomial

possible subproblem: the smallest number of edits neededto transform some prefix of

x,^ say^ expo^ into some prefix of y,^ say^ pol.

Edit distance

Solution? - dynamic programming ?What are the subproblems? Example 2:^ d(exponential, polynomial

possible subproblem: the smallest number of edits neededto transform some prefix of

x,^ say^ expo^ into some prefix of y,^ say^ pol. How to transform

expo^ to^ pol^? left to right right to left middle-out?

Edit distance

INPUT:^ two words

x^ :^ |x|^ =^ m^ and

y^ :^ |y|^ =^ n insert y[j], substitute x[i]^ →

y[j]

Edit distance

INPUT:^ two words

x^ :^ |x|^ =^ m^ and

y^ :^ |y|^ =^ n OUTPUT:^ E(m, n)

,^ where^ E(i, j)^ is the edit distance between prefixes^ x[1^... i

]^ and^ y[1^... j] To figure out^ E(

i, j)^ check^ x[i] =

y[j]. If^ x[i]^6 =^ y[j]^ then we have one of the following operations:^ delete x[i],^ insert y[j],^ substitute x

[i]^ →^ y[j]

Edit distance

INPUT:^ two words

x^ :^ |x|^ =^ m^ and

y^ :^ |y|^ =^ n OUTPUT:^ E(m, n)

,^ where^ E(i, j)^ is the edit distance between prefixes^ x[1^... i

]^ and^ y[1^... j] To figure out^ E(

i, j)^ check^ x[i] =

y[j]. If^ x[i]^6 =^ y[j]^ then we have one of the following operations:^ delete x[i], E

(i, j) =^ E(i^ −^1 , j

insert y[j], E(i, j

) =^ E(i, j^ −^ 1) + 1 substitute x[i]^ →

y[j]^ E(i, j) =^ E

(i^ −^1 , j^ −^ 1) + 1^ CS3510 A, Fall 2005 – p. 4/

Edit distance

INPUT:^ two words

x^ :^ |x|^ =^ m^ and

y^ :^ |y|^ =^ n OUTPUT:^ E(m, n)

,^ where^ E(i, j)^ is the edit distance between prefixes^ x[1^... i

]^ and^ y[1^... j] To figure out^ E(

i, j)^ check^ x[i] =

y[j]. If^ x[i]^6 =^ y[j]^ then we have one of the following operations:^ delete x[i], E

(i, j) =^ E(i^ −^1 , j

insert y[j], E(i, j

) =^ E(i, j^ −^ 1) + 1 substitute x[i]^ →

y[j]^ E(i, j) =^ E

(i^ −^1 , j^ −^ 1) + 1 Otherwise (x[i] =

y[j]) we have^ E

(i, j) :=^ E(i^ −^1 , j

−^ 1)

Base case:^ E(

, j) =^ j^ and^ E(i,

  1. =^ i.

CS3510 A, Fall 2005 – p. 4/

Edit distance

INPUT:^ two words

x^ :^ |x|^ =^ m^ and

y^ :^ |y|^ =^ n OUTPUT:^ E(m, n)

,^ where^ E(i, j)^ is the edit distance between prefixes^ x[1^... i

]^ and^ y[1^... j] To figure out^ E(

i, j)^ check^ x[i] =

y[j]. If^ x[i]^6 =^ y[j]^ then we have one of the following operations:^ delete x[i], E

(i, j) =^ E(i^ −^1 , j

insert y[j], E(i, j

) =^ E(i, j^ −^ 1) + 1 substitute x[i]^ →

y[j]^ E(i, j) =^ E

(i^ −^1 , j^ −^ 1) + 1 Otherwise (x[i] =

y[j]) we have^ E

(i, j) :=^ E(i^ −^1 , j

−^ 1)

Base case:^ E(

, j) =^ j^ and^ E(i,

  1. =^ i.^ Let {^1 dif f (i, j) = if x[i]^6 =^ y[j]^

CS3510 A, Fall 2005 – p. 4/