Docsity

Dynamic Programming: Solving Overlapping Subproblems with Optimal Substructure, Study notes of Algorithms and Programming

Toggle sidebar
bg1
1
CS 5633 Analysis of Algorithms 13/3/09
CS 5633 -- Spring 2009
Dynamic Programming
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 5633 Analysis of Algorithms 23/3/09
Dynamic programming
•Algorithm design technique
• A technique for solving problems that have
• overlapping subproblems
• and, when used for optimization, have an optimal
substructure property
•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 33/3/09
Example: Fibonacci numbers
•F(0)=0; F(1)=1; F(n)=F(n-1)+F(n-2) for n ≥2
• Implement this recursion naively:
F(n)
F(n-1)F(n-2)
F(n-2)F(n-3)F(n-3)F(n-4)
Solve same
subproblems
many times !
Runtime is
exponential in n.
•Store 1D DP-table and fill bottom-up in O(n) time:
F: 0112358
CS 5633 Analysis of Algorithms 43/3/09
Longest Common Subsequence
Example: Longest Common Subsequence (LCS)
•Given two sequences x[1 . . m]and y[1 . . n], find
a longest subsequence common to them both.
x:ABCBDAB
y:BDC ABA
“a” not “the”
BCBA =
LCS(x, y)
functional notation,
but not a function
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Programming: Solving Overlapping Subproblems with Optimal Substructure and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 5633 Analysis of Algorithms

1

3/3/

CS 5633 -- Spring 2009

Dynamic Programming

Carola Wenk

Slides courtesy of Charles Leiserson with small

changes by Carola Wenk

CS 5633 Analysis of Algorithms

2

3/3/

Dynamic programming

Algorithm design technique

  • A technique for solving problems that have
    • overlapping subproblems• and, when used for optimization, have an optimalsubstructure property

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/

Example: Fibonacci numbers

F(0)=0; F(1)=1; F(

n

)=F(

n

-1)+F(

n

-2) for

n

  • Implement this recursion naively:

F(

n

F(

n

F(

n

F(

n

F(

n

F(

n

F(

n

Solve samesubproblemsmany times !Runtime isexponential in

n

Store 1D DP-table and fill bottom-up in

O(n)

time:

F:

CS 5633 Analysis of Algorithms

4

3/3/

Longest Common Subsequence

Example:

Longest Common Subsequence (LCS)

  • Given two sequences

x

[..

m

] and

y

[..

n

], find

a longest subsequence common to them both.

x

A

B

C

B

D

A

B

y

B

D

C

A

B

A

“a”

not

“the”

BCBA =LCS(

x

y

functional notation,but not a function

CS 5633 Analysis of Algorithms

5

3/3/

Brute-force LCS algorithm

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

  • Hence, the runtime would be exponential!

CS 5633 Analysis of Algorithms

3/3/

Towards a better algorithm

Two-Step Approach: 1. Look at the

length

of a longest-common

subsequence.

  1. Extend the algorithm to find the LCS itself. Strategy:

Consider

prefixes

of

x

and

y

  • Define

c

[

i

j

] =

LCS(

x

[..

i

],

y

[..

j

])

  • Then,

c

[

m

n

] =

LCS(

x

y

Notation:

Denote the length of a sequence

s

by

s

CS 5633 Analysis of Algorithms

7

3/3/

Recursive formulation

Theorem.

c

[

i

j

] =

c

[

i

j

–1] + 1

if

x

[

i

] =

y

[

j

],

max

c

[

i

j

],

c

[

i

j

–1]

otherwise.

Let

z

[..

k

] = LCS(

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

–1].

Proof.

Case

x

[

i

] =

y

[

j

]:

...

1

2

i

m

...

1

2

j

n

x

y

CS 5633 Analysis of Algorithms

3/3/

Proof (continued)

Claim:

z

[..

k

–1] = LCS(

x

[..

i

–1],

y

[..

j

–1]).

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

–1] =

k

–1, which implies that

c

[

i

j

]

c

[

i

j

–1] + 1.

Other cases are similar.

CS 5633 Analysis of Algorithms

13

3/3/

Dynamic-programming

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 algorithm

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

D

nil nil

nil nil

nil nil

nil nil

nil nil

nil nil

C

nil nil

nil nil

nil nil

nil nil

nil nil

nil nil

nil nil

nil nil

A

nil nil

nil nil

nil nil

nil nil

nil nil

nil nil

B

nil nil

nil nil

nil nil

nil nil

nil nil

A

Memoization

A

B

C

B

D

B

B A

nil nil

nil nil

nil nil

LCS(

x

y

x

y

  • ••
  • ••
  • ••

CS 5633 Analysis of Algorithms

3/3/

D

C

A

B

A

Bottom-up dynamic-programming algorithm

I

DEA

Compute thetable bottom-up.

A

B

C

B

D

B

B A

Time =

mn

CS 5633 Analysis of Algorithms

3/3/

A

A

B

C

B

D

B

B D C A B A

A

B

C

B

B

D

D

C

A

A

B

B

A

Bottom-up dynamic-programming algorithm

I

DEA

Compute thetable bottom-up.

Time =

mn

ReconstructLCS by back-tracing.

Space =

mn

Exercise: O

(min{

m

n