Dynamic Programming: The Matrix Chain Algorithm

Similar documents
Ranking teams in partially-disjoint tournaments

#7 Still more DP, Scoring Matrices 9/5/07

You are to develop a program that takes as input the scorecard filled out by Bob and that produces as output the correct scorecard.

BCB 444/544 Fall 07 Dobbs 1

Search I. Tuomas Sandholm Carnegie Mellon University Computer Science Department. [Read Russell & Norvig Chapter 3]

CS Lecture 5. Vidroha debroy. Material adapted courtesy of Prof. Xiangnan Kong and Prof. Carolina Ruiz at Worcester Polytechnic Institute

Retractable Pool Cover

GRAPH-BASED SPORTS RANKINGS

ORF 201 Computer Methods in Problem Solving. Final Project: Dynamic Programming Optimal Sailing Strategies

Problem Solving as Search - I

21st AMC (A) 1 (B) 2 (C) 3 (D) 4 (E) 5

CT4510: Computer Graphics. Transformation BOCHANG MOON

SIMULATION OF ENTRAPMENTS IN LCM PROCESSES

Name Date PD. Pythagorean Theorem

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

MATHCOUNTS. Raytheon National Competition Sprint Round Problems 1 30 DO NOT BEGIN UNTIL YOU ARE INSTRUCTED TO DO SO. Name.

2nd Grade Quarter Four Assessment Guide

Henry and Poppy. have fun with Multiplication. Year 2 maths. We had fun making these questions for you. Enjoy them.

8-1. The Pythagorean Theorem and Its Converse. Vocabulary. Review. Vocabulary Builder. Use Your Vocabulary

Simulating Major League Baseball Games

ECE 697B (667) Spring 2003

The system design must obey these constraints. The system is to have the minimum cost (capital plus operating) while meeting the constraints.

Massey Method. Introduction. The Process

Warm-up exercises with ball

Pythagorean Theorem Name:

Online Companion to Using Simulation to Help Manage the Pace of Play in Golf

The next criteria will apply to partial tournaments. Consider the following example:

Mixed Trig Problems. For each problem show a complete solution with diagrams that include all the pertinent facts and answers.

CMIMC 2018 Official Contest Information

(a) Monday (b) Wednesday (c) Thursday (d) Friday (e) Saturday

WHEN TECHNOLOGICAL COEFFICIENTS CHANGES NEED TO BE ENDOGENOUS

Measuring Length. Goals. You will be able to

Jasmin Smajic 1, Christian Hafner 2, Jürg Leuthold 2, March 16, 2015 Introduction to Finite Element Method (FEM) Part 1 (2-D FEM)

UAB MATH-BY-MAIL CONTEST, 2004

A B

Introduction to Parallelism in CASTEP

FIFA World Ranking. Semester Thesis. Lukas Affolter.

Mad Hatter 6-8. Math Field Day Mad Hatter 6-8. Part I

LLT Education Services

ABSTRACT PATTERNS USING 3D-DYNAMIC MODELING. Kaustav Nandy, Master of Science, Department of Electrical And Computer Engineering

Lecture 5. Optimisation. Regularisation

PREAMBLE. Why the Informatics Olympiad in Team were born

Cleveland State University MCE441: Intr. Linear Control Systems. Lecture 6: The Transfer Function Poles and Zeros Partial Fraction Expansions

Using Markov Chains to Analyze Volleyball Matches

HIGH RESOLUTION DEPTH IMAGE RECOVERY ALGORITHM USING GRAYSCALE IMAGE.

Math A Regents Exam 0806 Page 1

Introduction to Matlab for Engineers

Using March Madness in the first Linear Algebra course. Steve Hilbert Ithaca College

9.3 Altitude-on-Hypotenuse Theorems

Project 2 Discussion

Better Search Improved Uninformed Search CIS 32

Math Section 4.1 Special Triangles

Abstract In this paper, the author deals with the properties of circumscribed ellipses of convex quadrilaterals, using tools of parallel projective tr

Besides the reported poor performance of the candidates there were a number of mistakes observed on the assessment tool itself outlined as follows:

8-1. The Pythagorean Theorem and Its Converse. Vocabulary. Review. Vocabulary Builder. Use Your Vocabulary

Polynomial DC decompositions

N2-3 Rounding and Approximation

5.8 The Pythagorean Theorem

Let s go Fly a Kite Up, in the Atmosphere!!!

Lesson 21: Special Relationships within Right Triangles Dividing into Two Similar Sub-Triangles

Flight Dynamics II (Stability) Prof. Nandan Kumar Sinha Department of Aerospace Engineering Indian Institute of Technology, Madras

Journal of Quantitative Analysis in Sports

Real World Search Problems. CS 331: Artificial Intelligence Uninformed Search. Simpler Search Problems. Example: Oregon. Search Problem Formulation

The Beginner's Guide to Mathematica Version 4

CREATIVE ADVANCED CHOREOGRAPHY By Dottie Welch, for Charlotte, North Carolina, CALLERLAB Meeting, April 2006

In memory of Dr. Kevin P. Granata, my graduate advisor, who was killed protecting others on the morning of April 16, 2007.

Math 1070 Sample Final Exam Spring 2016

Decision Trees. Nicholas Ruozzi University of Texas at Dallas. Based on the slides of Vibhav Gogate and David Sontag

ECONOMICS DEPT NON - ASSESSED TEST. For Internal Students of Royal Holloway. Time Allowed: 1 hour. Instructions to candidates:

Example: sequence data set wit two loci [simula

Using Dividing Plates with the Rotary Table

Game Theory and Olympic Tae Kwon Do

March Madness A Math Primer

Mastering the Mechanical E6B in 20 minutes!

Problem Solving Agents

Grade 7 & 8 Math Circles Pair-O -Dice: The Game April 2/3, 2013

Mixture Models & EM. Nicholas Ruozzi University of Texas at Dallas. based on the slides of Vibhav Gogate

Exam Question 9: Hydrostatics. March 6, Applied Mathematics: Lecture 8. Brendan Williamson. Introduction. Density, Weight and Volume

Six Couple Squares. Presentation by Bob Elling

Week 7 One-way ANOVA

Navigate to the golf data folder and make it your working directory. Load the data by typing

Math 3 Plane Geometry Review Special Triangles

Advanced Hydraulics Prof. Dr. Suresh A. Kartha Department of Civil Engineering Indian Institute of Technology, Guwahati

Bachelor of Computer Applications (Semester-2) Subject Name: Project in C Subject Code: BCA 205. General Guidelines for Project in C

PHYS 102 Quiz Problems Chapter 19 : Kinetic Theory of Gases Dr. M. F. Al-Kuhaili

Lecture 18 10/09/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

Solving Problems by Searching chap3 1. Problem-Solving Agents

HOW FAST/FAR DOES FLY LINE FALL? N. Perkins of the University of Michigan, March 2003

Design Strategies for ARX with Provable Bounds: SPARX and LAX

2015 π Math Contest. Individual Round SOLUTIONS

Fun with Folding. led by Thomas Clark. October 7, Take a strip of paper, fold it in half, and make a good crease at the midpoint position.

Lab 11 Density and Buoyancy

Circle Left / Circle Right. Forward & Back. Right-Arm Turn Left-Arm Turn Allemande Left. Promenade. Right & Left Grand.

Advanced CRaMS: Controlling the Stations Barry Johnson May 21, 2011

Planning and Acting in Partially Observable Stochastic Domains

A Markov Model for Baseball with Applications

Experiences with Area Assessment Materials

FIRST Robotics Competition (FRC ) Game Descriptions Maize Craze

GLOBAL PREMIER SOCCER

Math 4. Unit 1: Conic Sections Lesson 1.1: What Is a Conic Section?

Transcription:

Dynamic Programming: The Matrix Chain Algorithm Andreas Klappenecker [partially based on slides by Prof. Welch]

Matrix Chain Problem Suppose that we want to multiply a sequence of rectangular matrices. In which order should we multiply? A x (BxC) or (AxB) x C

Matrices An n x m matrix A over the real numbers is a rectangular array of nm real numbers that are arranged in n rows and m columns. For example, a 3 x 2 matrix A has 6 entries a 11 a 12! A = a 21 a 22 a 31 a 32 where each of the entries a ij is e.g. a real number.

Matrix Multiplication Let A be an n x m matrix B an m x p matrix The product of A and B is n x p matrix AB whose (i,j)-th entry is! k=1 m a ik b kj In other words, we multiply the entries of the i-th row of A with the entries of the j-th column of B and add them up.

Matrix Multiplication

Complexity of Matrix Multiplication Let A be an n x m matrix, B an m x p matrix. Thus, AB is an n x p matrix. Computing the product AB takes nmp scalar multiplications n(m-1)p scalar additions for the standard matrix multiplication algorithm.

Matrix Chain Order Problem Matrix multiplication is associative, meaning that (AB)C = A(BC). Therefore, we have a choice in forming the product of several matrices. What is the least expensive way to form the product of several matrices if the naïve matrix multiplication algorithm is used? [We use the number of scalar multiplications as cost.]

Why Order Matters Suppose we have 4 matrices: A: 30 x 1 B: 1 x 40 C: 40 x 10 D: 10 x 25 ((AB)(CD)) : requires 41,200 scalar multiplications (A((BC)D)) : requires 1400 scalar multiplications

Matrix Chain Order Problem Given matrices A 1, A 2,, A n, where A i is a d i-1 x d i matrix. [1] What is minimum number of scalar multiplications required to compute the product A 1 A 2 A n? [2] What order of matrix multiplications achieves this minimum? We focus on question [1], and sketch an answer to [2].

A Possible Solution Try all possibilities and choose the best one. Drawback: There are too many of them (exponential in the number of matrices to be multiplied) We need to be smarter: Let s try dynamic programming!

Step 1: Develop a Recursive Solution Define M(i,j) to be the minimum number of multiplications needed to compute!! A i A i+1 A j Goal: Find M(1,n). Basis: M(i,i) = 0. Recursion: How can one define M(i,j) recursively?

Defining M(i,j) Recursively Consider all possible ways to split A i through A j into two pieces. Compare the costs of all these splits: best case cost for computing the product of the two pieces plus the cost of multiplying the two products Take the best one M(i,j) = min k (M(i,k) + M(k+1,j) + d i-1 d k d j )

Defining M(i,j) Recursively (A i A k ) (A k+1 A j ) P 1 P 2 minimum cost to compute P 1 is M(i,k) minimum cost to compute P 2 is M(k+1,j) cost to compute P 1 P 2 is d i-1 d k d j

Step 2: Find Dependencies Among Subproblems M: 1 2 3 4 5 1 0 2 n/a 0 3 n/a n/a 0 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Step 2: Find Dependencies Among Subproblems M: 1 2 3 4 5 1 0 GOAL! 2 n/a 0 3 n/a n/a 0 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Step 2: Find Dependencies Among Subproblems M: 1 2 3 4 5 1 0 GOAL! 2 n/a 0 3 n/a n/a 0 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Step 2: Find Dependencies Among Subproblems M: 1 2 3 4 5 1 0 GOAL! 2 n/a 0 3 n/a n/a 0 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Step 2: Find Dependencies Among Subproblems M: 1 2 3 4 5 1 0 2 n/a 0 3 n/a n/a 0 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0 GOAL! computing the pink square requires the purple ones: to the left and below.

Defining the Dependencies Computing M(i,j) uses everything in same row to the left:! M(i,i), M(i,i+1),, M(i,j-1) and everything in same column below:! M(i,j), M(i+1,j),,M(j,j)

Step 3: Identify Order for Solving Subproblems Recall the dependencies between subproblems just found Solve the subproblems (i.e., fill in the table entries) this way: - go along the diagonal - start just above the main diagonal - end in the upper right corner (goal)

Order for Solving Subproblems M: 1 2 3 4 5 1 0 2 n/a 0 3 n/a n/a 0 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Order for Solving Subproblems M: 1 2 3 4 5 1 0 2 n/a 0 3 n/a n/a 0 1 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Order for Solving Subproblems M: 1 2 3 4 5 1 0 2 n/a 0 3 n/a n/a 0 1 2 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Order for Solving Subproblems M: 1 2 3 4 5 1 0 2 n/a 0 3 n/a n/a 0 1 2 3 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Order for Solving Subproblems M: 1 2 3 4 5 1 0 2 n/a 0 3 n/a n/a 0 1 2 3 4 4 n/a n/a n/a 0 5 n/a n/a n/a n/a 0

Pseudocode for i := 1 to n do M[i,i] := 0 for d := 1 to n-1 do // diagonals for i := 1 to n-d to // rows w/ an entry on d-th diagonal j := i + d // column corresp. to row i on d-th diagonal M[i,j] := infinity for k := i to j-1 to M[i,j] := min(m[i,j], M[i,k]+M[k+1,j]+d i-1 d k d j ) endfor endfor endfor

Pseudocode for i := 1 to n do M[i,i] := 0 for d := 1 to n-1 do // diagonals for i := 1 to n-d to // rows w/ an entry on d-th diagonal j := i + d // column corresp. to row i on d-th diagonal M[i,j] := infinity for k := i to j-1 to M[i,j] := min(m[i,j], M[i,k]+M[k+1,j]+d i-1 d k d j ) endfor endfor endfor running time O(n 3 )

Pseudocode for i := 1 to n do M[i,i] := 0 for d := 1 to n-1 do // diagonals for i := 1 to n-d to // rows w/ an entry on d-th diagonal j := i + d // column corresp. to row i on d-th diagonal M[i,j] := infinity for k := i to j-1 to M[i,j] := min(m[i,j], M[i,k]+M[k+1,j]+d i-1 d k d j ) pay attention here to remember actual sequence of mults. endfor endfor endfor running time O(n 3 )

Example M: 1 2 3 4 1 0 1200 700 1400 2 n/a 0 400 650 3 n/a n/a 0 10,000 4 n/a n/a n/a 0 1: A is 30x1 2: B is 1x40 3: C is 40x10 4: D is 10x25 BxC: 1x40x10 (BxC)xD: 400 + 1x10x25 Bx(CxD):... + 10,000

Keeping Track of the Order It's fine to know the cost of the cheapest order, but what is that cheapest order? Keep another array S and update it when computing the minimum cost in the inner loop After M and S have been filled in, then call a recursive algorithm on S to print out the actual order

Modified Pseudocode for i := 1 to n do M[i,i] := 0 for d := 1 to n-1 do // diagonals for i := 1 to n-d to // rows w/ an entry on d-th diagonal j := i + d // column corresponding to row i on d-th diagonal M[i,j] := infinity for k := i to j-1 to M[i,j] := min(m[i,j], M[i,k]+M[k+1,j]+d i-1 d k d j ) if previous line changed value of M[i,j] then S[i,j] := k endfor endfor endfor keep track of cheapest split point found so far: between A k and A k+1

Example M: 1 2 3 4 S: 1 0 1200 700 1400 2 n/a 0 400 650 1 3 n/a n/a 0 10,000 4 n/a n/a n/a 0 1 2 1 3 3 1: A is 30x1 2: B is 1x40 3: C is 40x10 4: D is 10x25 A x (BCD) A x ((BC) x D) A x ((BxC) x D)

Using S to Print Best Ordering Call Print(S,1,n) to get the entire ordering. Print(S,i,j): if i = j then output "A" + i //+ is string concat else k := S[i,j] output "(" + Print(S,i,k) + Print(S,k+1,j) + ")"