• Tidak ada hasil yang ditemukan

Design and Analysis of Algorithms

N/A
N/A
Protected

Academic year: 2024

Membagikan "Design and Analysis of Algorithms"

Copied!
8
0
0

Teks penuh

(1)

Design and Analysis of Algorithms

ผศ. ดร. สมชาย ประสิทธิ์จูตระกูล ภาควิชาวิศวกรรมคอมพิวเตอร

จุฬาลงกรณมหาวิทยาลัย 2542

http://www.cp.eng.chula.ac.th/faculty/spj

คําเตือน

เนื้อหาอันรวมถึงขอความ ตัวเลข สัญลักษณ รูปภาพ และ คําบรรยาย อาจมีขอผิดพลาดแฝงอยู ผูจัดทําจะไมรับผิด ชอบตอความเสียหายทั้งทางดานผลการเรียน สุขภาพกาย และสุขภาพจิตใดๆ อันเนื่องมาจากการใชสื่อการเรียนนี้

Dynamic Programming

Matrix-Chain Multiplication

http://www.cp.eng.chula.ac.th/faculty/spj

Outline

• Definition

• Optimal Substructures

• Recursive Solution to Subproblems

• Bottom-Up Dynamic Programming

• Examples

• Top-Down + Memoization

Matrix Multiplication

x x

x x

x x

x x

x x

x x

x x x

x x x

x x

x x

x x

x x 2 x 3 x 4 scalar multiplications [a x b] x [b x c] costs a b c scalar multiplications

Matrix-Chain Multiplication

A1 A2 A3 A4

• (A1(A2(A3A4)))

• (A1((A2A3)A4))

• ((A1A2)(A3A4))

• ((A1(A2A3)A4))

• (((A1A2)A3)A4)

Choose a full parenthesization which minimizes the number of

scalar multiplications

(2)

http://www.cp.eng.chula.ac.th/faculty/spj

( ) ( ) ( )

Example

A1 = a 10 × 100 matrix

A2 = a 100 × 5 matrix

A3 = a 5 × 50 matrix A1 A2 A3

10× 5 10× 100× 5 + 10× 5× 50 A1 ( ) A2 A3

100× 50 100× 5× 50 + 10× 100× 50

http://www.cp.eng.chula.ac.th/faculty/spj

( )( )

Brute Force

• How many full parenthesizations are there in a matrix chain of length n ?

( X ... X X ... X )

n

k n - k

P( n ) P( k ) × P( n-k ) P( n ) = P( k ) P( n-k ) k = 1Σn-1 = ( 4 n / n1.5 )

http://www.cp.eng.chula.ac.th/faculty/spj

Notation

A1 has dimension p0× p1

A2 has dimension p1× p2

Ai has dimension pi-1× pi

Ai ... Aj has dimension pi-1× pj

A1 A2 ... Ai ... Aj ... An p0× p1× p2 ... pi-1× pi ... pj-1× pj ... × pn

http://www.cp.eng.chula.ac.th/faculty/spj

Notation

m[ i, j ] = the min. number of scalar mults needed to compute the chain Ai ... Aj

m[ 1, n ] : solution

Optimal Substructure

( A1 A2 A3 A4 A5 A6 ) ( A1 A2 A3)( A4 A5 A6 )

m[1, 3 ] + m[ 4, 6 ] + p0 p3 p6 p0 ×p3 p3 ×p6 m[ 1, 3 ] m[ 4, 6 ]

Optimal Substructure

( A1 A2 A3 A4 A5 A6 ) m[1,6] = ?

( ( A1)( A2 A3 A4 A5 A6 ) ) m[1,1] + m[2,6] + p0 p1 p6 ( ( A1 A2)( A3 A4 A5 A6 ) ) m[1,2] + m[3,6] + p0 p2 p6 ( ( A1 A2 A3)( A4 A5 A6 ) ) m[1,3] + m[4,6] + p0 p3 p6 ( ( A1 A2 A3 A4 )(A5 A6 ) ) m[1,4] + m[5,6] + p0 p4 p6 ( ( A1 A2 A3 A4 A5)( A6 ) ) m[1,5] + m[6,6] + p0 p5 p6

(3)

http://www.cp.eng.chula.ac.th/faculty/spj

Recursive Solution

m[ i, j ] = m[ i, k ] + m[ k +1, j ] + pi-1 pk pj ( ( Ai .... Ak )(Ak+1 ... Aj ) )

min { }

i k < j

m[ i, j ] = 0 if i = j if i < j

http://www.cp.eng.chula.ac.th/faculty/spj

R_Matrix_Chain( p, i, j ) {

if i = j then return 0 m = INFINITY

for k = i to j-1

q = R_Matrix_Chain( p, i, k ) + R_matrix_Chain( p, k+1, j ) + p[i-1]*p[k]*p[j]

if q < m then m = q return m

}

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

Recursive Algorithm

http://www.cp.eng.chula.ac.th/faculty/spj

Recursive Algorithm : Analysis

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

T( n ) = ( T( k ) + T( n-k ) + O(1) ) k = 1Σn-1

= ( 2 n ) [ CLR p.311 ]

http://www.cp.eng.chula.ac.th/faculty/spj

Observation

• There are only n2 distinct m[ i, j ]’s

• Q : Why does it take ( 2n) time ?

• A : Overlapping subproblems

m[1,6] --> m[1,1], m[2,6], m[1,2], m[3,6], m[1,3], m[4,6], m[1,4], m[5,6], m[1,5], m[6,6]

m[2,6] --> m[2,2], m[3,6], m[2,3], m[4,6], m[2,4], m[5,6], m[2,5], m[6,6]

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

(4)

http://www.cp.eng.chula.ac.th/faculty/spj

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

x

http://www.cp.eng.chula.ac.th/faculty/spj

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

x x

http://www.cp.eng.chula.ac.th/faculty/spj

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

x x

x

http://www.cp.eng.chula.ac.th/faculty/spj

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

x x

x x

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

x x

x x

x

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x

x

x x

x x

x x

(5)

http://www.cp.eng.chula.ac.th/faculty/spj

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

x x

x x

x x

x

http://www.cp.eng.chula.ac.th/faculty/spj

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x

x

x x

x x

x x

x x

http://www.cp.eng.chula.ac.th/faculty/spj

Computing Optimal Cost : Bottom Up

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

1 2 3 4 5 6 1

2 3 4 5 6

0 0

0 0

0 0

Solution m[1, 6]

x

x x

x x x

x x

x x

x x

x x x

http://www.cp.eng.chula.ac.th/faculty/spj

Example

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[1,2]

= m[1,1] + m[2,2] + 10x5x1 = 0 + 0 + 50

= 50

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[2,3]

= m[2,2] + m[3,3] + 5x1x5 = 0 + 0 + 25

= 25

150

(6)

http://www.cp.eng.chula.ac.th/faculty/spj

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[3,4]

= m[3,3] + m[4,4] + 1x5x10 = 0 + 0 + 50

= 50

50 25 1

2

http://www.cp.eng.chula.ac.th/faculty/spj

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[4,5]

= m[4,4] + m[5,5] + 5x10x2 = 0 + 0 + 100

= 100

50 25

50 1

2 3

http://www.cp.eng.chula.ac.th/faculty/spj

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[1,3]

m[1,1] + m[2,3] + 10x5x5 = 0 + 25 + 250 = 275 m[1,2] + m[3,3] + 10x1x5 = 50 + 0 + 50 = 100

50 25

50 100 1

2 3

4

http://www.cp.eng.chula.ac.th/faculty/spj

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[2,4]

m[2,2] + m[3,4] + 5x1x10 = 0 + 50 + 50 = 100 m[2,3] + m[4,4] + 5x5x10 = 25 + 0 + 250 = 275

50 25

50 100 1 100

2 3

4 1

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[3,5]

m[3,3] + m[4,5] + 1x5x2 = 0 + 100 + 10 = 110 m[3,4] + m[5,5] + 1x10x2 = 50 + 0 + 20 = 70

50 25

50 100 100

100 1

2 3

4 1

2

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[1,4]

m[1,1] + m[2,4] + 10x5x10 = 0 + 100 + 500 = 600 m[1,2] + m[3,4] + 10x1x10 = 50 + 50 + 100 = 200 m[1,3] + m[4,4] + 10x5x10 = 100 + 0 + 500 = 600

50 25

50 100 100

100 70 1

2 3

4 1

2 4

(7)

http://www.cp.eng.chula.ac.th/faculty/spj

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[2,5]

m[2,2] + m[3,5] + 5x1x2 = 0 + 70 + 10 = 80 m[2,3] + m[4,5] + 5x5x2 = 25 + 100 + 50 = 175 m[2,4] + m[5,5] + 5x10x2 = 100 + 0 + 100 = 200

50 25

50 100 100

100 70 1 200

2 3

4 1

2 4 2

http://www.cp.eng.chula.ac.th/faculty/spj

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0

m[1,1] + m[2,5] + 10x5x2 = 0 + 80 + 100 = 180 m[1,2] + m[3,5] + 10x1x2 = 50 + 70 + 20 = 140 m[1,3] + m[4,5] + 10x5x2 = 100 + 100 + 100 = 300 m[1,4] + m[5,5] + 10x10x2 = 200 + 0 + 200 = 400

50 25

50 100 100

100 70 200

80 1

2 3

4 1

2 4 2

2

http://www.cp.eng.chula.ac.th/faculty/spj

Example

A1 A2 A3 A4 A5 10 × 5 × 1 × 5 × 10 × 2

1 2 3 4 5 1

2 3 4 5

m[ i, j ] = min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j 0

0 0

0 0 50

25 50

100 100

100 70 200

80 1 140

2 3

4 1

2 4 2

2

2 A1 A2 A3 A4 A5 (A1 A2 )(A3 A4 A5) (A1 A2 )((A3 A4 )A5)

http://www.cp.eng.chula.ac.th/faculty/spj

Matrix-Chain Mult. Dynamic Prog.

Matrix-Chain-Order( p, n ) {

for i = 1 to n m[i,i] = 0 for len = 2 to n

for i = 1 to n - len + 1 j = i + len - 1

return m[1,n]

}

m[ i, j ] =min { }m[ i, k ] + m[ k +1, j ] + pi-1 pk pj

i k < j

Matrix-Chain Mult. Dynamic Prog.

Matrix-Chain-Order( p, n ) {

for i = 1 to n m[i,i] = 0 for len = 2 to n

for i = 1 to n - len + 1 j = i + len - 1 m[i,j] = for k = i to j-1

q = m[i,k] + m[k+1,j] + p[i-1]*p[k]*p[j]

if q < m[i,j] then m[i,j] = q s[i,j] = k return s

}

Θ( n3 )

Matrix-Chain Multiplication

Matrix-Chain-Multiply( A, p, n ) {

s = Matrix-Chain-Order( p, n ) Matrix-Chain-Mult( A, s, 1, n ) }

Matrix-Chain-Mult( A, s, i, j ) {

if i < j

X = Matrix-Chain-Mult( A, s, i, s[i,j] ) Y = Matrix-Chain-Mult( A, s, s[i,j]+1, j ) return Matrix-Multiply( X, Y )

else

return A[i]

}

(8)

http://www.cp.eng.chula.ac.th/faculty/spj

R_Matrix_Chain( p, i, j ) {

if i = j then return 0 m = INFINITY

for k = i to j-1

q = R_Matrix_Chain( p, i, k ) + R_matrix_Chain( p, k+1, j ) + p[i-1]*p[k]*p[j]

if q < m then m = q return m

}

Top-Down Recursive Alg. : Revisited

( 2n )

http://www.cp.eng.chula.ac.th/faculty/spj

Lookup_Chain( p, i, j ) {

if i = j then return 0 m = INFINITY

for k = i to j-1

q = Lookup_Chain( p, i, k ) + Lookup_Chain( p, k+1, j ) + p[i-1]*p[k]*p[j]

if q < m then m = q return m

}

Top-Down Recursive Alg. : Revisited

http://www.cp.eng.chula.ac.th/faculty/spj

Lookup_Chain( p, i, j ) {

if i = j then return 0 m[i,j] = INFINITY for k = i to j-1

q = Lookup_Chain( p, i, k ) + Lookup_Chain( p, k+1, j ) + p[i-1]*p[k]*p[j]

if q < m[i,j] then m[i,j] = q return m[i,j]

}

Top-Down Recursive Alg. : Revisited

http://www.cp.eng.chula.ac.th/faculty/spj

Lookup_Chain( p, i, j ) {

if i = j then return 0 m[i,j] = INFINITY for k = i to j-1

q = Lookup_Chain( p, i, k ) + Lookup_Chain( p, k+1, j ) + p[i-1]*p[k]*p[j]

if q < m[i,j] then m[i,j] = q return m[i,j]

}

Top-Down + Memoization

if m[i,j] < INFINITY then return m[i,j]

Lookup_Chain( p, i, j ) {

if m[i,j] < INFINITY then return m[i,j]

if i = j then return 0 for k = i to j-1

q = Lookup_Chain( p, i, k ) + Lookup_Chain( p, k+1, j ) + p[i-1]*p[k]*p[j]

if q < m[i,j] then m[i,j] = q return m[i,j]

}

Top-Down + Memoization

Θ( n3 )

Referensi

Dokumen terkait

Using a user specified parameters, Eps , find the number points that have an SNN similarity of Eps or greater to each point. This is the SNN density of

Conclusion The implementation of the K-Means algorithm has resulted in the grouping of data according to the number of clusters as many as 3 and the process stops at the 3rd

Heat exchangers are needed to heat the DEB recycle and reactor section feed streams to have the reactors operate at a sufficiently high temperature, they are needed as reboilers and

!"# 2542 http://www.cp.eng.chula.ac.th/faculty/spj !" ## $%&'#* &$+!$ ,* "&'-"*%##++& %# .# .$'/0 1$ /-1 %#% Undecidable Problems Introduction

สมชาย ประสิทธิ์จูตระกูล ภาควิชาวิศวกรรมคอมพิวเตอร จุฬาลงกรณมหาวิทยาลัย 2542 http://www.cp.eng.chula.ac.th/faculty/spj คําเตือน เนื้อหาอันรวมถึงขอความ ตัวเลข สัญลักษณ รูปภาพ และ

http://www.cp.eng.chula.ac.th/faculty/spj Abu Abd-Allah ibn Musa al'Khwarizmi Al'Khwarizmi lived from 790 to 840 wrote on Hindu-Arabic numerals and was the first to use zero as a

Sri Chandrasekharendra Saraswathi Viswa Mahavidyalaya Department of Computer Science and Engineering Page 64 of 162 Syllabus B.E[CSE] Full Time L T P C 3 2 0 3 CS4T4 - DESIGN AND

Week No Catalog Topics Plan Weekly Coverage 1 Ch1: Introduction to algorithms 1p-41p Formal definition and characterization of algorithms, major problem areas, data structures