



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS 5633 Analysis of Algorithms
1
3/3/
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 5633 Analysis of Algorithms
2
3/3/
Algorithm design technique
Idea:
Do not repeatedly solve the same subproblems,
but solve them only once and store the solutions in a dynamic programming table
CS 5633 Analysis of Algorithms
3
3/3/
n
n
n
-2) for
n
n
n
n
n
n
n
n
Solve samesubproblemsmany times !Runtime isexponential in
n
Store 1D DP-table and fill bottom-up in
O(n)
time:
CS 5633 Analysis of Algorithms
4
3/3/
Example:
Longest Common Subsequence (LCS)
x
m
] and
y
n
], find
a longest subsequence common to them both.
x
y
“a”
not
“the”
x
y
functional notation,but not a function
CS 5633 Analysis of Algorithms
5
3/3/
Check every subsequence of
x
m
] to see
if it is also a subsequence of
y
n
Analysis • 2
m
subsequences of
x
(each bit-vector of
length
m
determines a distinct subsequence
of
x
CS 5633 Analysis of Algorithms
3/3/
Two-Step Approach: 1. Look at the
length
of a longest-common
subsequence.
Consider
prefixes
of
x
and
y
c
i
j
x
i
y
j
c
m
n
x
y
Notation:
Denote the length of a sequence
s
by
s
CS 5633 Analysis of Algorithms
7
3/3/
Theorem.
c
i
j
c
i
j
if
x
i
y
j
max
c
i
j
c
i
j
otherwise.
Let
z
k
x
i
y
j
]), where
c
i
j
k
. Then,
z
k
x
i
], or else
z
could be extended.
Thus,
z
k
–1] is CS of
x
i
–1] and
y
j
Proof.
Case
x
i
y
j
...
1
2
i
m
...
1
2
j
n
x
y
CS 5633 Analysis of Algorithms
3/3/
Claim:
z
k
x
i
y
j
Suppose
w
is a longer CS of
x
i
–1] and
y
j
–1], that is,
w
k
–1. Then,
cut and
paste
w
z
k
w
concatenated with
z
k
]) is a
common subsequence of
x
i
] and
y
j
with
w
z
k
k
. Contradiction, proving the
claim.
Thus,
c
i
j
k
–1, which implies that
c
i
j
c
i
j
Other cases are similar.
CS 5633 Analysis of Algorithms
13
3/3/
There are two variants of dynamicprogramming:1. Memoization2. Bottom-up dynamic programming
(often referred to as “dynamicprogramming”)
CS 5633 Analysis of Algorithms
3/3/
Memoization:
After computing a solution to a
subproblem, store it in a table. Subsequent calls checkthe table to avoid redoing work.Time =
mn
) = constant work per table entry.
Space =
mn
LCS(
x
,
y
,
i
,
j
)
if
c
[
i
,
j
] =
NIL
then if
x
[
i
] =
y
[
j
]
then
c
[
i
,
j
]
←
LCS(
x
,
y
,
i
–1,
j
–1) + 1
else
c
[
i
,
j
]
←
max
LCS(
x
,
y
,
i
–1,
j
),
LCS(
x
,
y
,
i
,
j
–1)
sameasbefore
for all
i
,
j
:
c
[
i
,0]=
and
c
[0,
j
]=
CS 5633 Analysis of Algorithms
15
3/3/
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
nil nil
x
y
x
y
CS 5633 Analysis of Algorithms
3/3/
DEA
Compute thetable bottom-up.
Time =
mn
CS 5633 Analysis of Algorithms
3/3/
DEA
Compute thetable bottom-up.
Time =
mn
ReconstructLCS by back-tracing.
Space =
mn
Exercise: O
(min{
m
n