Docsity

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

Partial preview of the text

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

CS 334 Analysis of Algorithms

10/18/

CS 3343 – Fall 2007

Dynamic Programming

Carola Wenk

Slides courtesy of Charles Leiserson with changes

and additions by Carola Wenk

CS 334 Analysis of Algorithms

10/18/

Dynamic programming

Algorithm design technique (like divide and conquer)

  • Is 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 334 Analysis of Algorithms

4

10/18/

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 334 Analysis of Algorithms

10/18/

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 334 Analysis of Algorithms

10/18/

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 334 Analysis of Algorithms

10/18/

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 334 Analysis of Algorithms

10/18/

Recursive algorithm for LCS

LCS(

x

y

i

j

if

x

[

i

] =

y

[

j

]

then

c

[

i

j

]

LCS(

x

y

i

j

else

c

[

i

j

]

max

LCS(

x

y

i–

j

LCS(

x

y

i

j

Worst-case:

x

[

i

]

y

[

j

], in which case the

algorithm evaluates two subproblems, eachwith only one parameter decremented.

CS 334 Analysis of Algorithms

10/18/

same

subproblem

but we’re solving subproblems already solved!

Recursion tree

m

n

3, 3,

2, 2,

1, 1,

3, 3,

3, 3,

2, 2,

1, 1,

2, 2,

Height =

m

n

work potentially exponential.

2, 2,

1, 1,

2, 2,

m

n

CS 334 Analysis of Algorithms

10/18/

Dynamic-programming

There are two variants of dynamicprogramming:1. Memoization2. Bottom-up dynamic programming

(often referred to as “dynamicprogramming”)

CS 334 Analysis of Algorithms

10/18/

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)

sameas before

for all

i

,

j

:

c

[

i

,0]=

and

c

[0,

j

]=

CS 334 Analysis of Algorithms

10/18/

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 334 Analysis of Algorithms

10/18/

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