15 views

Uploaded by tharunkutti

data structures

- Matrix Algebra
- Matrices
- UGC NET PAPER2015.docx
- Errata corrige Concepts and Applications of Finite Element Analysis,
- Modeling Transformations.
- Notes on MATLAB
- ejercicios linear models
- MMSE Detection
- Brahimi N.-mathemtical Models and Lagrangian Heuristics
- Mathematics Sample Paper-1-SOLUTION-Class 10-(SA-I)
- Mr 1 2018 Problems
- Multiple Stage Production
- Introduction to OpenGL - Matrices
- Maths Olympiad 2009
- CS70 Midterm
- Matrices and Determinant
- Matrix Multiplication
- l_5
- 301 ASMM English.pdf
- jawapan khawarizmi

You are on page 1of 16

Dynamic Programming

2

Introduction

Optimization problem: there can be many possible solution. Each solution has a

value, and we wish to find a solution with the optimal (minimum or maximum)

value

Dynamic Programming VS. Divide-and-Conquer

Solve problems by combining the solutions to sub-problems

The sub-problems of D-A-C are non-overlap

The sub-problems of D-P are overlap

Sub-problems share sub-sub-problems

D-A-C solves the common sub-sub-problems repeatedly

D-P solves every sub-sub-problems once and stores its answer in

a table

Programming refers to a tabular method

3

Development of A Dynamic-

Programming Algorithm

Characterize the structure of an optimal

solution

Recursively define the value of an

optimal solution

Compute the value of an optimal

solution in a bottom-up fashion

Construct an optimal solution from

computed information

Matrix-Chain Multiplication

5

Overview

Given a sequence (chain) <A

1

, A

2

,, A

n

> of n matrices to be multiplied, compute the

product A

1

A

2

A

n

in a way that minimizes the number of scalar multiplications

Matrix multiplication

Two matrices A and B can be multiplied only if they are compatible

The number of columns of A must equal the number of rows of B

A(p*q) * B(q*r) C(p*r)

The number of scalar multiplication is p*q*r

Example: <A

1

, A

2

, A

3

> (10*100, 100*5, 5*50)

((A

1

A

2

)A

3

) 10*100*5 + 10*5*50 = 5000 + 2500 = 7500

(A

1

(A

2

A

3

)) 100*5*50 + 10*100*50 = 25000 + 50000 = 75000

6

Matrix-Chain Multiplication

Problem

Given a sequence (chain) <A

1

, A

2

,, A

n

> of n matrices to be

multiplied, where i=1,2,, n, matrix A

i

has dimension

p

i-1

*p

i

, fully parenthesize the product A

1

A

2

A

n

in a way that minimizes

the number of scalar multiplications

Determine an order for multiplying matrices that has the lowest

cost

Counting the number of parenthesizations

>

=

=

=

1

1

2 ) ( ) (

1 1

) (

n

k

n if k n P k P

n if

n P

O(2

n

)

Impractical to check all possible parenthesizations

7

Matrix Multiplication

8

Step 1

The structure of an optimal parenthesization

Notation: A

i..j

= result from evaluating A

i

A

i+1

A

j

(i s j)

Any parenthesization of A

i

A

i+1

A

j

must split the product between A

k

and

A

k+1

for some integer k in the range i s k < j

Cost = cost of computing A

i..k

+ cost of computing A

k+1..j

+ cost of

multiplying A

i..k

and A

k+1..j

together

Suppose that an optimal parenthesization of A

i

A

i+1

A

j

splits the product

between A

k

and A

k+1

.

The parenthesization of the prefix sub-chain A

i

A

i+1

A

k

must be an

optimal parenthesization of A

i

A

i+1

A

k

The parenthesization of the prefix sub-chain A

k+1

A

i+1

A

j

must be an

optimal parenthesization of A

k+1

A

i+1

A

j

9

Illustration of Optimal

SubStructure

A

1

A

2

A

3

A

4

A

5

A

6

A

7

A

8

A

9

Suppose

((A

7

A

8

)A

9

)

is optimal

((A

1

A

2

)(A

3

((A

4

A

5

)A

6

)))

Minimal

Cost_A

1..6

+ Cost_A

7..9

+p

0

p

6

p

9

(A

3

((A

4

A

5

)A

6

)) (A

1

A

2

)

Then must be optimal for A

1

A

2

A

3

A

4

A

5

A

6

Otherwise, if

((A

4

A

5

)A

6

) (A

1

(A

2

A

3

))

is optimal for A

1

A

2

A

3

A

4

A

5

A

6

Then

((A

1

(A

2

A

3

)) ((A

4

A

5

)A

6

)) ((A

7

A

8

)A

9

)

will be better than

((A

7

A

8

)A

9

) ((A

1

A

2

)(A

3

((A

4

A

5

)A

6

)))

10

Step 2

A Recursive Solution

Sub-problem: determine the minimum cost of a parenthesization of

A

i

A

i+1

A

j

(1 s i s j s n)

m[i..j] = the minimum number of scalar multiplications needed to

compute the matrix A

i..j

s[i, j] = k, such that m[i, j] = m[i, k] + m[k+1, j] + p

i-1

p

k

p

j

We need to compute m[1..n]

A recursive solution takes exponential time

Encounter each sub-problem many times in different branches

of its recursion tree overlapping sub-problems

< + + +

=

=

< s

j i if p p p j k m k i m

j i if

j i m

j k i

j k

1

1

] , 1 [ ] , [ {

0

] , [

min

11

Step 3

Computing the optimal costs

How much sub-problems in total?

One for each choice of i and j satisfying 1 s i s j s n O(n

2

)

MATRIX-CHAIN-ORDER(p)

Input: a sequence p= <P

1

, P

2

,, P

n

> (length[p] = n+1)

Try to fill in the table m in a manner that corresponds to

solving the parenthesization problem on matrix chains of

increasing length

Lines 4-12: compute m[i, i+1], m[i, i+2], each time

12

O(n

3

), O (n

3

) O(n

3

) running time

O(n

2

) space

13

l = 2

10*20*25

=5000

35*15*5=

2625

l =3

m[3,5] = min

m[3,4]+m[5,5] + 15*10*20

=750 + 0 + 3000 = 3750

m[3,3]+m[4,5] + 15*5*20

=0 + 1000 + 1500 = 2500

14

Step 4

Constructing an optimal solution

Each entry s[i, j] records the value of k such that the optimal

parenthesization of A

i

A

i+1

A

j

splits the product between A

k

and A

k+1

A

1..n

A

1..s[1..n]

A

s[1..n]+1..n

A

1..s[1..n]

A

1..s[1, s[1..n]]

A

s[1, s[1..n]]+1..s[1..n]

Recursive

Elements of Dynamic

Programming

Optimal substructure

Overlapping subproblems

13 March 2013

Unit V Dynamic Programming and

String Matching 16

Example

1

a b a c a a b a c a b a c a b a a b b

7

8

19 18 17 15

a b a c a b

16 14

13

2 3 4 5 6

9

a b a c a b

a b a c a b

a b a c a b

a b a c a b

10 11 12

c

0

c

3

1

a

4 5 2 1 0 j

2 1 0 0

F(j)

b a b a P[j]

- Matrix AlgebraUploaded bySylvia Cheung
- MatricesUploaded byMhai Mabanta
- UGC NET PAPER2015.docxUploaded byawadhesh786
- Errata corrige Concepts and Applications of Finite Element Analysis,Uploaded byAccalappiacani87
- Modeling Transformations.Uploaded bytarun gehlot
- Notes on MATLABUploaded bySVR07
- ejercicios linear modelsUploaded byBarbievas
- MMSE DetectionUploaded byKrishna Kumar
- Mathematics Sample Paper-1-SOLUTION-Class 10-(SA-I)Uploaded byFirdosh Khan
- Brahimi N.-mathemtical Models and Lagrangian HeuristicsUploaded byMartin Vertiz
- Mr 1 2018 ProblemsUploaded byStephen
- Multiple Stage ProductionUploaded bydr_hsn57
- Introduction to OpenGL - MatricesUploaded bykorisnikkk
- Maths Olympiad 2009Uploaded bymgupta72
- CS70 MidtermUploaded byDan Goodman
- Matrices and DeterminantUploaded byAditya Nanda
- Matrix MultiplicationUploaded byNitant Panchal
- l_5Uploaded byEntropyPrinciple
- 301 ASMM English.pdfUploaded byIPutu Ellsa S
- jawapan khawarizmiUploaded byNorliyana Idris
- Design and Analysis of a Two Stage Traffic Light System Using Fuzzy Logic 2165 7866 1000162Uploaded byelektro
- 10.1.1.63.6662 (1)Uploaded byabhi
- Rtiwari Rd Book 12Uploaded bymghgol
- Chapter4.pdfUploaded byGrace Altea Heidi Cesar
- ALGTYPEUploaded byPuneeth Narayana
- Lecture 05Uploaded byRajneesh Singhaniya
- assesment 1Uploaded byASHUTOSH RAY 15BME0081
- Digital SystemsUploaded bycataice
- nsformations.pptUploaded bySuada Bőw Wéěžý
- Tough and Tricky Questions SolutionsUploaded bySatPrepVungTau

- observation lesson 2Uploaded byapi-289598938
- Facts About QuranUploaded byahmedziahaider
- chapter 5Uploaded byVikrant Narayan Vasudeva
- True Experimental DesignUploaded byAnn Sharmain Sta Rosa
- Unipolar Depression in Adults_ Management of Highly Resistant (Refractory) Depression - UpToDateUploaded byRenata Victoria
- ExercisesUploaded byBechara Haddad
- Optical Properties of Human Tissue Thesis_van Der ZeeUploaded bypiyush_kaviraj
- 5.6.2 UniverseUploaded byRyantyler13
- Space Transportation Concept of Operations Annex for NextGen-2Uploaded byseattlesailingfool
- Easy Hawaiian Lesson 2Uploaded bychrisbymeg
- 6th Grade Mind TeasersfUploaded byMadalena Tavares
- OGI Internship Open Recruitment FAQUploaded byAbubakar
- Translating Audio-Visual Humor--A Case StudyUploaded bylixiaoxiu
- Oriented Sample for XRDUploaded byAdarta Muh
- A Meeting With Imam KhomeiniUploaded byrazafarha
- LeukemiaUploaded byShashidhar K Bangera
- examen-ingles-selectividad-junio-2015-solucionUploaded byapi-356181352
- Bit MaskUploaded byAlexander Mejia
- Lovecraft RisingUploaded byPavle Đurić
- COGNITIVE RADIOUploaded bymsbajori
- Music Book: Piano Accordion Jazz Instruction by Ron PorterUploaded byRon Porter
- Chorus of MadmenUploaded byAmritaBanerjee
- Radioactivity Theory EUploaded bythinkiit
- Behavioral Approach to Leadership Boundary Spanning Transaction Relationship PatternUploaded byInternational Journal of Advanced Scientific Research and Development
- The Passion ExpoundedUploaded byapi-3695841
- PenguinsUploaded byPenélope Reyero Hernández
- Friendbook: A Semantic-based Friend Recommendation System for Social NetworksUploaded byJAYAPRAKASH
- Tech N9ne - See MEUploaded byRajat Verma
- MimeUploaded byIzzat Ismail
- Chapter 04Uploaded byohusman