Dynamic Programming 2 Neil Tang 4/22/2008 CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms Course Survey Please complete the course survey by May 3 (Sat) at: http://www.cs.montana.edu/survey/ CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms Class Overview Basic idea Matrix multiplication Ordering matrix multiplication CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms 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. CS223 Advanced Data Structures and Algorithms
Matrix Multiplication C=AB. If the size of A is wy, then the size of B must be yz, i.e., the number of columns of A must be equal to the number of rows of B. The size of C is wz and CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms An Example Compute ABCD with |A|=5010, |B| = 1040, |C|=4030 and |D|=305. (((AB)C)D) involves 20,000+60,000+7,500 =87,500 multiplications. (A((BC)D)) involves 12,000+1,500+2,500 =16,000 multiplications. (A(B(CD))) involves 6,000+2,000+2,500 =10,500 multiplications. CS223 Advanced Data Structures and Algorithms
Ordering Matrix Multiplications Consider AleftAleft+1…Ai…Aright mleft,right = min leftiright {mleft,i + mi+1,right+cleft-1cicright} CS223 Advanced Data Structures and Algorithms
Ordering Matrix Multiplications The time complexity is O(n3) CS223 Advanced Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms Another Example right left right left CS223 Advanced Data Structures and Algorithms