Docsity

Chain Matrix Multiplication Problem Using Dynamic Programming-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Chain Matrix Multiplication Problem Using Dynamic Programming-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 16

Chain Matrix Multiplication Problem using

Dynamic Programming

  • Chain-Matrix Multiplication• Problem Analysis
    • Notations– Dynamic Algorithm– Time Complexity
      • Generalization and Applications• Conclusion

Today Covered

Dynamic Programming Solution

-^

Problem is of type optimization

-^

Sub-problems are dependant

-^

Optimal structure can be characterized and

-^

Can be defined recursively

-^

Solution for base cases exits

-^

Optimal solution can be constructed

-^

Hence here is dynamic programming Why Dynamic Programming in this problem?

Ai..j

= (A

. Ai

i+

….. A

). (Ak

k+

. A

k+

….. A

) = Aj

i..k

× A

k+

..^ j

i^ ≤

k < j

-^

Order of A

i..k

= p

i-^

x p

, and order of Ak^

k+1..j

= p

x pk^

,j

-^

m[i, k] = minimum number of multiplicationsneeded to compute A

i..k

-^

m[k+1, j] = minimum number of multiplicationsneeded to compute A

k+1..j

-^

Mathematical Model

Mathematical Model

-^

Problem: Compute optimal multiplication orderfor a series of matrices given below

20

.^50 50 .^5 5 .^100 100 10

4

3

2

1

A

A

A

A

m[1,1]

m[1,2]

m[1,3]

m[1,4]

m[2,2]

m[2,3]

m[2,4]

m[3,3]

m[3,4]m[4,4]

P^0

= 10 P^1

= 100 P^2

= 5 P^3

= 50 P^4

= 20

Example: Dynamic Programming

m[1, 2] = 0 + 0 + 10. 100. 5

= 5000

s[1, 2] = k = 1

]

[ ] , [ ( ] ,

[^

1

min

j k i

j ki

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

 

] 2

[

]

[

] 2

[^

2

0

2

min^1

p p p k m k m m k

k

^

 

] 2

[

] 1 ,

1 [

] 2

[^

2 1 0

min

p p p m m m

Computing m[1, 2], m[2, 3], m[3, 4]

m[2, 3] = 0 + 0 + 100. 5. 50

= 25000

s[2, 3] = k = 2

]

[ ] , [ ( ] ,

[^

1

min

j k i

j ki

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

 

] 3

[

]

[

] 3

[^

3

1

3

min^2

p p p k m k m m k

k

^

 

] 3

[

] 2

[

] 3

[^

2 1

min

p p p m m m

Computing m[2, 3]

m[1, 3] = min(0+25000+10.100.50, 5000+0+10.5.50)

= min(75000, 2500) = 2500

s[1, 3] = k = 2

]

[ ] , [ ( ] ,

[^

1

min

j k i

j ki

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

 

] 3

[

]

[

] 3

[^

3

0

3

min^1

p p p k m k m m k

k

^

 

] 3

[

] 2

[

] 3

[

] 1 ,

1 [

] 3

[

3 2 0

3 1 0

min

p p p

m

m

p p p m m m

Computing m[1, 3], m[2, 4]

m[2, 4] = min(0+5000+100.5.20, 25000+0+100.50.20)

= min(15000, 35000) = 15000

s[2, 4] = k = 2

]

[ ] , [ ( ] ,

[^

1

min

j k i

j ki

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

 

] 4

[

]

[

] 4

[^

4

1

4

min^2

p p p k m k m m k

k

^

 

] 4

[

] 3

[

] 4

[

] 2

[

] 4

[

4 3 1

4 2 1

min

p p p

m

m

p p p m m m

Computing m[2, 4]

-^

Final Cost Matrix

-^

Order of Computation

0

5000

2500

11000

0

25000

15000

0

50000

1

5

8

10

2

6

9

3

7 4

Final Cost Matrix and Its Order of Computation

0

1

2

2

0

2

2

0

3 0

K,s Values Leading Minimum m[i, j]

n^

length[p] – 1

for i

1 to n

do m[i, i]

0

for l

2 to n,

do for i

1 to n-l+

do j

i+l-

m[i, j]

 

for k

i to j-

do q

m[i, k] + m[k+1, j] + p

i-

. p . pk^

j

if q < m[i, j]

then m[i, j] = q

s[i, j]

k

  1. return m and s,

“l is chain length” m[1,1]

m[1,2]

m[1,3]

m[1,4]

m[2,2]

m[2,3]

m[2,4]

m[3,3]

m[3,4]m[4,4]

Chain-Matrix-Order(p)



 

 

^



 

 

n i

i n k

n i

n ij

k

i j

n n T^

1

1

1

1

)

(

) (



 

 

n i

i n i n n n

T^

1

2

) 1

)(

(

) (

)

2

( 1 2

) (^

2

2 1

i n i ni

n

n n T

n i

  

 ^



)

2

( 1 2

) (^

1

1

1

2

1

1

2

     

n i

n i

n i

n i

n i

i

n

i

ni

n

n n T

Computational Cost