lecture 5 dynamic programming. dynamic programming self-reducibility

33
Lecture 5 Dynamic Programming

Upload: marilyn-ella-bennett

Post on 27-Dec-2015

281 views

Category:

Documents


2 download

TRANSCRIPT

Lecture 5 Dynamic Programming

Dynamic Programming

times.

many repeated ssubproblemdistinct ofnumber small"" a

containssolution recursive a :ssubproblem gOverlappin

s.subproblem tosolutions optimal contains

problem a osolution t optimalan :resubstructu Optimal

:hallmark Two

Self-reducibility

Divide and Conquer

• Divide the problem into subproblems.

• Conquer the subproblems by solving them recursively.

• Combine the solutions to subproblems into the solution for original problem.

Dynamic Programming

• Divide the problem into subproblems.

• Conquer the subproblems by solving them recursively.

• Combine the solutions to subproblems into the solution for original problem.

Remark on Divide and Conquer

sizes).smaller with problems some (i.e., ssubproblem

some toproblem thereduces structure recursiveA

!!ty!reducibili-Self Key Point:

Divide-and-Conquer is a DP-type technique.

Algorithms with Self-Reducibility

Dynamic Programming

Divide and Conquer

Greedy

Local Ratio

Matrix-chain Multiplication

tion.multiplicascaler of

number theminimizes way that ain

product thezeparenthesifully ,

dimension has matrix ,,...,2,1for where

matrices, of },...,,{chain aGiven

21

1

21

n

ii

i

n

AAA

pp

Ani

nAAA

Fully Parenthesize

)))(((

)))(((

products. zedparenthesi

fully twoofproduct or thematrix single a

either isit if zedparenthesifully isproduct A

4321

4321

AAAA

AAAA

Scalar Multiplications

. isproduct matrix thisdo to

tionsmultiplicascalar ofnumber theThus,

)(

Then ).(matrix

and )(matrix Consider

1

pqr

baAB

bB

rqaAqp

q

jrpjkij

jk

ij

tions.multiplicascalar of numbersdifferent give

may products zedparenthesifully Different

# of scalar multiplications

320210321

321310321

))((

))((

ppppppAAA

ppppppAAA

e.g.,

ly.respective , and of

products zedparenthesifully optimal arethey

Then . and of products

zedparenthesifully twoofproduct theis

product zedparenthesifully optimal Suppose

11

11

nkk

nkk

AAAA

AAAA

Step 1. Find recursive structure of optimal solution

. if

)],1[],[(min],[

; if 0],[

Then . computingfor tion multiplica

scalar ofnumber minimum thebe ],[Let

1

ji

pppjkmkimjim

jijim

AA

jim

jkijki

ji

Step 2. Build recursive formula about optimal value

Step 3. Computing optimal value

;return

else-end

);],1[],[,min(

do 1 tofor

;

begin else

0;return en th

if

],[function

1

u

pppjkmkimuu

jik

u

ji

jim

jki

Step 3. Computing optimal value

sm

kjis

qjim

jimq

pppjkmkimq

jik

jim

lij

lni

nl

iimni

plengthn

p

jki

and return

],[

],[ then

],[ if

],1[],[ do

1 tofor

],[

1 do

1 to1for

do to2for

;0],[ do to1for

;1)(

)(Order-Chain-Matrix

1

Step 4. Constructing an optimal solution

].,[ of valuesFollow jis

151

15,125

11,875 10,500

9,375 7,125 5,375

7,875 4,375 2,500 3,500

15,700 2,625 750 1,000 5,000

000 0 0 0

6A5A4A3A2A1A

m

ij6

54

3

21

12

3

45

6

3530 1535 515 105 2010 2520

151

15,125

11,875 10,500

9,375 7,125 5,375

7,875 4,375 2,500 3,500

15,700 2,625 750 1,000 5,000

000 0 0 0

6A5A4A3A2A1A

)(s

ij6

54

3

21

12

3

45

6(3)

(3)

(3)(3)

(3)(3)(3)

(3)

(5)

(5)

(4)(3)(2)

(1)

(1)

Optimal solution )))))((((( 654321 AAAAAA

Running Time

sm

kjis

qjim

jimq

pppjkmkimq

jik

jim

lij

lni

nl

iimni

plengthn

p

jki

and return

],[

],[ then

],[ if

],1[],[ do

1 tofor

],[

1 do

1 to1for

do to2for

;0],[ do to1for

;1)(

)(Order-Chain-Matrix

1

)(nO

)(nO

)(nO

)(nO

)( 3nO

Running Time

;return

else-end

);],1[],[,min(

do 1 tofor

;

begin else

0;return en th

if

],[function

1

u

pppjkmkimuu

jik

u

ji

jim

jki

)(nO

How many recursive calls?How many m[I,j] will be computed?

# of Subproblems

)(

computed? bemay ],[many How2nO

jim

. if

)],1[],[(min],[

; if 0],[

Then . computingfor tion multiplica

scalar ofnumber minimum thebe ],[Let

1

ji

pppjkmkimjim

jijim

AA

jim

jkijki

ji

Running Time

Remark on Running Time

(1) Time for computing recursive formula.

(2)The number of subproblems.

(3) Multiplication of (1) and (2)

Longest Common Subsequence

Problem

. and of esubsequenc

common longest a find , and sequences Given two

YXZ

YX

Recursive Formula

. and 0, if ]),1[],1,[max(

, and 0, if 1]1,1[

,0or 0 if 0

],[

ji

ji

yxjijicjic

yxjijic

ji

jic

More Examples

A Rectangle with holes

cuts. oflength totalthe

minimize toinside hole without rectanglessmaller into

it partition inside, holes-point with rectangle aGiven

NP-Hard!!!

Guillotine cut

Guillotine Partition

A sequence of guillotine cuts

Canonical one: every cut passes a hole.

Minimum length Guillotine Partition

• Given a rectangle with holes, partition it into smaller rectangles without hole to minimize the total length of guillotine cuts.

Minimum Guillotine Partition

Dynamic programmingIn time O(n ):5

Each cut has at most 2nchoices.

There are O(n ) subproblems.4

Minimum guillotine partition can be a polynomial-timeapproximation.

What we learnt in this lecture?

• How to design dynamic programming.

• Two ways to implement.

• How to analyze running time.

Puzzle

it? with time

running improve can we holds,property monotone thisIf

?)','(),(

,'for that it true is tion,MultiplicaChain -MatrixIn

liisliis

ii