



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of dynamic programming, focusing on the fibonacci sequence and edit distance. The fibonacci sequence is analyzed for its runtime complexity, leading to the development of a more efficient dynamic programming solution. The document then introduces the edit distance problem and its recursive definition, followed by a dynamic programming algorithm to find the minimum number of editing steps required to transform one string into another.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Jared Saia
University of New Mexico
Intro Dynamic Programming
String Alignment
What is Dynamic Programming?tion and Reason in Common Sense (1905)it.” - George Santayana, The Life of Reason, Book I: Introduc- “Those who cannot remember the past are doomed to repeat
Dynamic
Programming
is
basically
“Divide
and
Conquer”
with memorization
Basic
Trick
is:
Don’t
solve
the
same
problem
more
than
once!
Consider
the
following
procedure
for
computing
the
n -th
Fi-
Fib(n){bonacci number:
if (n<2)
return n;
else
return Fib(n-1) + Fib(n-2);
Q: What is the runtime of Fib?
A: Except for recursive calls,
the entire algorithm takes a
constant number of steps.
If
n ) is the run time of the
algorithm on input
n , then we can say that:
n ) =
n (^) −
(^) 2) +
n (^) −
(^) 1) + 1
It’s easy to show by induction that
n ) = 2
n
This
is very bad!
Q: How can we solve
n ) exactly?
lecture to getA: We solved this recurrence using annihilaotrs in the last
n ) =
c 1 φ n
c 2 φˆ n
c 3 1 n
where
φ
=
1+
√ 5
2
and ˆ
φ
=
1 − √ 5
2
If we solve for constants, we get that:
c 1 (^) +
(^) c 2
c 3
c 1 φ (^) +
(^) c 2 φˆ (^) +
(^) c 3
c 1 φ 2 +^
(^) c 2 φˆ 2 +^
(^) c 3
ination) gives: Solving this system of linear equations (using Gaussian elim-
c 1
= 1 +
c 2
= 1
c 3
=
So our final solution is
n ) =
( 1 +
√ 5 ) φ n + ( 1
)
φ ˆ n
−^
(^) 1 = Θ(
φ n ) .
Formulate the problem recursively.
. Write down a formula
smaller subproblemsfor the whole problem as a simple combination of answers to
sidering the intermediate subproblems in the correct order.recurrence and works its way up to the final solution by con- Write an algorithm that starts with the base cases of yourBuild solutions to your recurrence from the bottom up.
problems. This is frequentlyNote: Dynamic Programs store the results of intermediate sub-
but not always
done with some type
of table.
The
edit distance
between two words is the minimum number
required to transform one word into another.of letter insertions, letter deletions, and letter substitutions
For example,
the edit distance between
and
is at most four:
∧ D
Better way to display this process:
Place the words one above the other in a table
the second word for every deletionPut a gap in the first word for every insertion and a gap in
tutionsColumns with two different characters correspond to substi-
Then
the
number
of
editing
steps
is
just
the
number
of
columns that don’t contain the same character twice
String Alignment for “FOOD” and “MONEY”: F
the edit distance between “Food” and “Money”It’s not too hard to see that we can’t do better than four for
distance exactly. Example:Unfortunately, it can be more difficult to compute the edit A L G O R I T H M A L T R U I S T I C
remaining alignment must also be optimalIf we remove the last column in an optimal alignment, the
subalignment of all but the last column.Easy to prove by contradiction: Assume there is some better
Then we can just
a better overall alignment.paste the last column onto this better subalignment to get
Note:
The last column can be either:
aligned with a character on bottomaligned with a blank on bottom or 3) a character on topaligned with a character on bottom, 2) a character on top
find a recursive definitionTo develop a DP algorithm for this problem, we first need to
Assume we have a
m
length string
and an
n
length string
Let
i, j
) be the edit distance between the first
i characters
of
and the first
j
characters of
Then what we want to find is
n, m
Say we want to compute
i, j
) for some
i
and
j
tion toFurther say that the “Recursion Fairy” can tell us the solu-
i ′ , j
), for all′
i ′ ≤
i ,
j ′ ≤
j ,
except
for
i ′ =
i
and
j ′ =
j
Q: Can we compute
i, j
) efficiently with help from the our
fairy friend?
We can build up a
m
n
table which contains all values of
i, j
in the 0-th row and 0-th columnWe start by filling in the base cases for this table: the entries
above, to the left and above and to the left.To fill in any other entry, we need to know the values directly
Thus we can fill in the table in the standard way:
left to
in each cell are always availableright and top down to ensure that the entries we need to fill
are equalBold numbers indicate places where characters in the strings
Arrows represent predecessors that define each entry:
hori-
substitution.zontal arrow is deletion, vertical is insertion and diagonal is
itselfBold diagonal arrows are “free” substitutions of a letter for
example table, so there are three optimal edit sequences).ner gives an optimal alignment (there are three paths in thisAny path of arrows from the top left to the bottom right cor-
Let
n
be the length of the first string and
m
the length of
the second string
Then there are Θ(
nm
) entries in the table, and it takes Θ(1)
time to fill each entry
This implies that the run time of the algorithm is Θ(
nm
Q: Can you find a faster algorithm?