Dynamic Programming 2 Neil Tang 4222010 CS 223

  • Slides: 9
Download presentation
Dynamic Programming 2 Neil Tang 4/22/2010 CS 223 Advanced Data Structures and Algorithms 1

Dynamic Programming 2 Neil Tang 4/22/2010 CS 223 Advanced Data Structures and Algorithms 1

Course Survey Please complete the course survey at: http: //www. cs. montana. edu/survey/ CS

Course Survey Please complete the course survey at: http: //www. cs. montana. edu/survey/ CS 223 Advanced Data Structures and Algorithms 2

Class Overview Ø Basic idea Ø Matrix multiplication Ø Ordering matrix multiplication CS 223

Class Overview Ø Basic idea Ø Matrix multiplication Ø Ordering matrix multiplication CS 223 Advanced Data Structures and Algorithms 3

Basic Idea Ø Mathematically express the problem in the recursive form. Ø Solve it

Basic Idea Ø Mathematically express the problem in the recursive form. Ø Solve it by a non-recursive algorithm that systematically records the answers to the subproblems in a table. CS 223 Advanced Data Structures and Algorithms 4

Matrix Multiplication Ø C=A B. Ø If the size of A is w y,

Matrix Multiplication Ø C=A B. Ø If the size of A is w y, then the size of B must be y z, i. e. , the number of columns of A must be equal to the number of rows of B. Ø The size of C is w z and CS 223 Advanced Data Structures and Algorithms 5

An Example Ø Compute A B C D with |A|=50 10, |B| = 10

An Example Ø Compute A B C D with |A|=50 10, |B| = 10 40, |C|=40 30 and |D|=30 5. Ø (((A B) C) D) involves 20, 000+60, 000+7, 500 =87, 500 multiplications. Ø (A ((B C) D)) involves 12, 000+1, 500+2, 500 =16, 000 multiplications. Ø (A (B (C D))) involves 6, 000+2, 500 =10, 500 multiplications. CS 223 Advanced Data Structures and Algorithms 6

Ordering Matrix Multiplications Ø Consider Aleft+1…Ai…Aright Ø mleft, right = min left i right

Ordering Matrix Multiplications Ø Consider Aleft+1…Ai…Aright Ø mleft, right = min left i right {mleft, i + mi+1, right+cleft-1 cicright} CS 223 Advanced Data Structures and Algorithms 7

Another Example right left right CS 223 Advanced Data Structures and Algorithms left 8

Another Example right left right CS 223 Advanced Data Structures and Algorithms left 8

Ordering Matrix Multiplications The time complexity is O(n 3) CS 223 Advanced Data Structures

Ordering Matrix Multiplications The time complexity is O(n 3) CS 223 Advanced Data Structures and Algorithms 9