Partha Sarathi Manal

Similar documents
Dynamic Programming. Andreas Klappenecker. [partially based on slides by Prof. Welch] Monday, September 24, 2012

12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12

Lecture 4: Dynamic programming I

Dynamic Programming part 2

Lecture 13: Chain Matrix Multiplication

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS 231: Algorithmic Problem Solving

Data Structures and Algorithms Week 8

Lecture 8. Dynamic Programming

Dynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book)

Dynamic Programming II

Chain Matrix Multiplication

Dynamic Programming. Outline and Reading. Computing Fibonacci

ECE250: Algorithms and Data Structures Dynamic Programming Part B

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

Homework3: Dynamic Programming - Answers

Dynamic Programming (Part #2)

So far... Finished looking at lower bounds and linear sorts.

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

Lecture 5: Matrices. Dheeraj Kumar Singh 07CS1004 Teacher: Prof. Niloy Ganguly Department of Computer Science and Engineering IIT Kharagpur

D-BAUG Informatik I. Exercise session: week 5 HS 2018

We ve done. Now. Next

Partha Sarathi Mandal

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

CSE 101- Winter 18 Discussion Section Week 6

Unit-5 Dynamic Programming 2016

CSE 373 Analysis of Algorithms, Fall Homework #3 Solutions Due Monday, October 18, 2003

y j LCS-Length(X,Y) Running time: O(st) set c[i,0] s and c[0,j] s to 0 for i=1 to s for j=1 to t if x i =y j then else if

CMSC351 - Fall 2014, Homework #4

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

Chapter 3 Dynamic programming

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

5.1 The String reconstruction problem

Dynamic Programming. December 15, CMPE 250 Dynamic Programming December 15, / 60

Chapter 18 out of 37 from Discrete Mathematics for Neophytes: Number Theory, Probability, Algorithms, and Other Stuff by J. M. Cargal.

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Partha Sarathi Manal

Dynamic Programming Group Exercises

Introduction to Algorithms

Simplified Optimal Parenthesization Scheme for Matrix Chain Multiplication Problem using Bottom-up Practice in 2-Tree Structure

Parallelizing The Matrix Multiplication. 6/10/2013 LONI Parallel Programming Workshop

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming

Outline. CS38 Introduction to Algorithms. Fast Fourier Transform (FFT) Fast Fourier Transform (FFT) Fast Fourier Transform (FFT)

CE 601: Numerical Methods Lecture 5. Course Coordinator: Dr. Suresh A. Kartha, Associate Professor, Department of Civil Engineering, IIT Guwahati.

Dynamic Programming. Lecture Overview Introduction

/463 Algorithms - Fall 2013 Solution to Assignment 3

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

Partha Sarathi Manal

Dynamic Programming in Haskell

Dynamic Programming Matrix-chain Multiplication

F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)

Algorithm Design Techniques part I

CSED233: Data Structures (2017F) Lecture12: Strings and Dynamic Programming

Matrix Inverse 2 ( 2) 1 = 2 1 2

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

CSC-140 Assignment 5

Framework for Design of Dynamic Programming Algorithms

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

Unit 4: Dynamic Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

EDIT DISTANCE. Given two words (strings), how can we define a notion of closeness. For example: Is PELICAN closer to PENGUIN or POLITICIAN?

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Introduction to Engineering gii

Parallel Algorithms CSE /22/2015. Outline of this lecture: 1 Implementation of cilk for. 2. Parallel Matrix Multiplication

The Matrix-Tree Theorem and Its Applications to Complete and Complete Bipartite Graphs

Matrix Multiplication

Lecture 17: Array Algorithms

Dynamic programming II - The Recursion Strikes Back

PetShop (BYU Students, SIGGRAPH 2006)

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

Lesson 3.3. Multiplication of Matrices, Part 2

Handout 1: Viewing an Animation

15.Dynamic Programming

MATH 423 Linear Algebra II Lecture 17: Reduced row echelon form (continued). Determinant of a matrix.

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Dynamic Programming

Chapter 1: Number and Operations

Parallel Computing: Parallel Algorithm Design Examples Jin, Hai

Writing Parallel Programs; Cost Model.

Algorithms and Complexity. Exercise session 3+4

1 Non greedy algorithms (which we should have covered

(Refer Slide Time 04:53)

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Write an algorithm to find the maximum value that can be obtained by an appropriate placement of parentheses in the expression

agenda 10 MIN Go over benchmark 30 MIN Unit 2 Day 1 Notes 5 MIN Mastery Work

Advanced Algorithms Class Notes for Monday, November 10, 2014

Partha Sarathi Mandal

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Performing Matrix Operations on the TI-83/84

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

CSE 4/531 Solution 3

CSC 373: Algorithm Design and Analysis Lecture 8

Math 1526: Excel Lab 5 Summer 2004

Transcription:

MA 515: Introduction to Algorithms & MA353 : Design and Analysis of Algorithms [3-0-0-6] Lecture 29 http://www.iitg.ernet.in/psm/indexing_ma353/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept. of Mathematics, IIT Guwahati Mon 10:00-10:55 Tue 11:00-11:55 Fri 9:00-9:55 Class Room : 2101

Matrix Chain Order Problem Multiplying non-square matrices: A is n x m, B is m x p must be equal AB is n x p whose (i,j) entry is a ik b kj Computing AB takes nmp scalar multiplications and n(m-1)p scalar additions (using basic algorithm). Suppose we have a sequence of matrices to multiply. What is the best order?

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 mults. (A((BC)D)) : requires 1400 mults.

Matrix Chain Order Problem Given matrices A 1, A 2,, A n, where A i is d i-1 x d i : [1] What is minimum number of scalar mults required to compute A 1 A 2 A n? [2] What order of matrix multiplications achieves this minimum?

A Possible Solution Try all possibilities and choose the best one. Let P(n)= # of alternative parenthesizations of a seq of n matrices. Then P(n) = k=1 n-1 P(k)P(n-k) if n 2; P(n)=1 if n=1; Sol. (4 n /n 3/2 ) (Hint: Catalan number) Drawback is there are too many of them (exponential in the number of matrices to be multiplied) Need to be more clever - try dynamic programming!

Step 1: Develop a Recursive Solution Define M(i,j) to be the minimum number of mults. needed to compute A i A i+1 A j Goal: Find M(1,n). Basis: M(i,i) = 0. Recursion: How to 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 M(i,j) = M(i,k) + M(k+1,j) + d i-1 d k d j for a k k run from i to j-1 M(i,j) = min k (M(i,k) + M(k+1,j) + d i-1 d k d j )

Step 2: Find Dependencies Among Subproblems 5 1 0 2 n/a 0 3 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 1 0 5 2 n/a 0 3 n/a n/a 0 5 n/a n/a n/a n/a 0

Pseudocode j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal pay attention here to remember actual sequence of mults. running time O(n 3 )

1 0 1200 700 1400 2 n/a 0 400 650 3 n/a n/a 0 10,000

j := i + d // column corresponding to row i on d-th diagonal for k := i to j-1 to 1 2 n/a 3 n/a n/a 4 n/a n/a n/a

j := i + d // column corresponding to row i on d-th diagonal for k := i to j-1 to 1 0 2 n/a 0 3 n/a n/a 0

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal d=1 i= 1, 2, 3 j= 2, 3, 4 k=1, 2, 3 M[1,2] M[2,3] M[3,4] 1 0 1200 2 n/a 0 3 n/a n/a 0

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal d=1 i= 1, 2, 3 j= 2, 3, 4 k=1, 2, 3 M[1,2] M[2,3] M[3,4] 1 0 1200 2 n/a 0 400 3 n/a n/a 0

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal d=1 i= 1, 2, 3 j= 2, 3, 4 k=1, 2, 3 M[1,2] M[2,3] M[3,4] 1 0 1200 2 n/a 0 400 3 n/a n/a 0 10,000

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal d=1 i= 1, 2, 3 j= 2, 3, 4 k=1, 2, 3 M[1,2] M[2,3] M[3,4] 1 0 1200 2 n/a 0 400 3 n/a n/a 0 10,000

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal A(BC) d=2 i= 1, 2 j= 3, 4 k=1,2, 2,3 M[1,3] M[2,4] 1 0 1200 700 2 n/a 0 400 3 n/a n/a 0 10,000

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal (AB)C d=2 i= 1, 2 j= 3, 4 k=1,2, 2,3 M[1,3] M[2,4] 1 0 1200 700 2 n/a 0 400 3 n/a n/a 0 10,000 13200

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal A(BC) d=2 i= 1, 2 j= 3, 4 k=1,2, 2,3 M[1,3] M[2,4] 1 0 1200 700 2 n/a 0 400 3 n/a n/a 0 10,000

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal (BC)D d=2 i= 1, 2 j= 3, 4 k=1,2, 2,3 M[1,3] M[2,4] 1 0 1200 700 2 n/a 0 400 650 3 n/a n/a 0 10,000

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal A((BC)D) d=3 i= 1 j= 4 k=1, 2, 3 M[1,4] 1 0 1200 700 1400 2 n/a 0 400 650 3 n/a n/a 0 10,000

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal A((BC)D) d=3 i= 1 j= 4 k=1, 2, 3 M[1,4] 1 0 1200 700 2 n/a 0 400 650 3 n/a n/a 0 10,000 1400

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal (AB)(CD) d=3 i= 1 j= 4 k=1, 2, 3 M[1,4] 1 0 1200 700 1400 2 n/a 0 400 650 3 n/a n/a 0 10,000 41200

j := i + d for k := i to j-1 to // column corresponding to row i on d-th diagonal (A(BC))D d=3 i= 1 j= 4 k=1, 2, 3 M[1,4] 1 0 1200 700 1400 2 n/a 0 400 650 3 n/a n/a 0 10,000 8200

j := i + d // column corresponding to row i on d-th diagonal for k := i to j-1 to 1 0 1200 700 1400 2 n/a 0 400 650 A((BC)D) 3 n/a n/a 0 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 j := i + d for k := 1 to j -1 to // column corresponding to row i on d-th diagonal if previous line changed value of M[i,j] then S[i,j] := k keep track of cheapest split point found so far: between A k and A k+1 )

1 0 1200 700 1400 2 n/a 0 400 650 3 n/a n/a 0 10,000 S: 1 n/a 1 1 1 2 n/a n/a 2 3 3 n/a n/a n/a 3 4 n/a n/a n/a n/a

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 else k := S[i,j] output "(" Print(S,i,k) Print(S,k+1,j) ")" 1 0 1200 700 1400 S: 1 1 1 2 n/a 0 400 650 2 3 3 n/a n/a 0 10,000 3 A((BC)D) (A 1 ((A 2 A 3 )A 4 ))