Advanced Algorithms Analysis and Design

Slides:



Advertisements
Similar presentations
Multiplying Matrices Two matrices, A with (p x q) matrix and B with (q x r) matrix, can be multiplied to get C with dimensions p x r, using scalar multiplications.
Advertisements

Chapter 15 Dynamic Programming Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
Dynamic Programming Carrie Williams. What is Dynamic Programming? Method of breaking the problem into smaller, simpler sub-problems Method of breaking.
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming Code
1 Dynamic Programming Andreas Klappenecker [based on slides by Prof. Welch]
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
Analysis of Algorithms CS 477/677
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
Optimal binary search trees
11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product.
Catalan Numbers.
Dynamic Programming UNC Chapel Hill Z. Guo.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 4: Dynamic Programming Phan Th ị Hà D ươ ng 1.
Algorithmics - Lecture 121 LECTURE 11: Dynamic programming - II -
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Dynamic Programming Typically applied to optimization problems
Advanced Algorithms Analysis and Design
Properties and Applications of Matrices
Matrices Rules & Operations.
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Analysis of Algorithms
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Seminar on Dynamic Programming.
Chapter 8 Dynamic Programming
Advanced Design and Analysis Techniques
Matrix Chain Multiplication
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Dynamic programming techniques
Chapter 8 Dynamic Programming.
CSCE 411 Design and Analysis of Algorithms
Matrix Chain Multiplication
Advanced Algorithms Analysis and Design
ICS 353: Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Advanced Algorithms Analysis and Design
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Advanced Algorithms Analysis and Design
Dynamic Programming.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Data Structure and Algorithms
Chapter 15: Dynamic Programming II
Lecture 8. Paradigm #6 Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
Dynamic Programming-- Longest Common Subsequence
Longest increasing subsequence (LIS) Matrix chain multiplication
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Analysis of Algorithms CS 477/677
Advanced Analysis of Algorithms
Matrix Chain Multiplication
CSCI 235, Spring 2019, Lecture 25 Dynamic Programming
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
Analysis of Algorithms CS 477/677
Seminar on Dynamic Programming.
Data Structures and Algorithms Dynamic Programming
Presentation transcript:

Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Lecture No. 16 Chain Matrix Multiplication Problem using Lecture No. 16 Chain Matrix Multiplication Problem using Dynamic Programming Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Chain-Matrix Multiplication Problem Analysis Today Covered Chain-Matrix Multiplication Problem Analysis Notations Dynamic Algorithm Time Complexity Generalization and Applications Conclusion Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Problem Statement: Chain Matrix Multiplication Statement: The chain-matrix multiplication problem can be stated as below: Given a chain of [A1, A2, . . . , An] of n matrices for i = 1, 2, . . . , n, matrix Ai has dimension pi-1 x pi, find the order of multiplication which minimizes the number of scalar multiplications. Note: Order of A1 is p0 x p1, Order of A2 is p1 x p2, Order of A3 is p2 x p3, etc. Order of A1 x A2 x A3 is p0 x p3, Order of A1 x A2 x . . . x An is p0 x pn Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Dynamic Programming Solution Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Why Dynamic Programming in this problem? Problem is of type optimization Sub-problems are dependant Optimal structure can be characterized and Can be defined recursively Solution for base cases exits Optimal solution can be constructed Hence here is dynamic programming Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Dynamic Programming Formulation Let Ai..j = Ai . Ai+1 . . . Aj Order of Ai = pi-1 x pi, and Order of Aj = pj-1 x pj, Order of Ai..j = rows in Ai x columns in Aj = pi-1 × pj At the highest level of parenthesisation, Ai..j = Ai..k × Ak+1..j i ≤ k < j Let m[i, j] = minimum number of multiplications needed to compute Ai..j, for 1 ≤ i ≤ j ≤ n Objective function = finding minimum number of multiplications needed to compute A1..n i.e. to compute m[1, n] Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Mathematical Model Ai..j = (Ai. Ai+1….. Ak). (Ak+1. Ak+2….. Aj) = Ai..k × Ak+1..j i ≤ k < j Order of Ai..k = pi-1 x pk, and order of Ak+1..j = pk x pj, m[i, k] = minimum number of multiplications needed to compute Ai..k m[k+1, j] = minimum number of multiplications needed to compute Ak+1..j Mathematical Model Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Example: Dynamic Programming Problem: Compute optimal multiplication order for a series of matrices given below m[1,1] m[1,2] m[1,3] m[1,4] m[2,2] m[2,3] m[2,4] m[3,3] m[3,4] m[4,4] P0 = 10 P1 = 100 P2 = 5 P3 = 50 P4 = 20 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Main Diagonal Main Diagonal m[1, 1] = 0 m[2, 2] = 0 m[3, 3] = 0 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computing m[1, 2], m[2, 3], m[3, 4] m[1, 2] = 0 + 0 + 10 . 100 . 5 = 5000 s[1, 2] = k = 1 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computing m[2, 3] m[2, 3] = 0 + 0 + 100 . 5 . 50 = 25000 s[2, 3] = k = 2 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computing m[3, 4] m[3, 4] = 0 + 0 + 5 . 50 . 20 = 5000 s[3, 4] = k = 3 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computing m[1, 3], m[2, 4] m[1, 3] = min(0+25000+10.100.50, 5000+0+10.5.50) = min(75000, 2500) = 2500 s[1, 3] = k = 2 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computing m[2, 4] m[2, 4] = min(0+5000+100.5.20, 25000+0+100.50.20) s[2, 4] = k = 2 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computing m[1, 4] m[1, 4] = min(0+15000+10.100.20, 5000+5000+ 10.5.20, 2500+0+10.50.20) = min(35000, 11000, 35000) = 11000 s[1, 4] = k = 2 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Final Cost Matrix and Its Order of Computation 5000 2500 11000 25000 15000 Final Cost Matrix Order of Computation 1 5 8 10 2 6 9 3 7 4 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

K,s Values Leading Minimum m[i, j] 1 2 3 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Representing Order using Binary Tree The above computation shows that the minimum cost for multiplying those four matrices is 11000. The optimal order for multiplication is ((A1 . A2) . (A3 . A4)) For, m(1, 4) k = 2 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Chain-Matrix-Order(p) n  length[p] – 1 for i  1 to n do m[i, i]  0 for l  2 to n, do for i  1 to n-l+1 do j  i+l-1 m[i, j]   for k  i to j-1 do q  m[i, k] + m[k+1, j] + pi-1 . pk . pj if q < m[i, j] then m[i, j] = q s[i, j]  k return m and s, “l is chain length” m[1,1] m[1,2] m[1,3] m[1,4] m[2,2] m[2,3] m[2,4] m[3,3] m[3,4] m[4,4] Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computational Cost Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computational Cost Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Computational Cost Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Cost Comparison Brute Force Dynamic Programming There are three loop The most two loop for i, j, satisfy the condition: 1  i  j  n Cost = nC2 + n = n(n-1)/2 + n =  (n2) The third one most inner loop for k satisfies the condition, i  k < j, in worst case, it cost n and Hence total cost =  (n2 . n) =  (n3) Brute Force Approach P(n) = C(n - 1) C(n)  (4n/n3/2) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Generalization: Sequence of Objects Although this algorithm applies well to the problem of matrix chain multiplication Many researchers have noted that it generalizes well to solving a more abstract problem given a linear sequence of objects an associative binary operation on those objects hold the objective to find a way to compute the cost of performing that operation on any two given objects and finally computing the minimum cost for grouping these objects to apply the operation over the entire sequence. It is obvious that this problem can be solved using chain matrix multiplication, because there is a one to one correspondence between both problem Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Generalization: String Concatenation One common special case of chain matrix multiplication problem is string concatenation. For example, we are give a list of strings. The cost of concatenating two strings of length m and n is for example O(m + n) Since we need O(m) time to find the end of the first string and O(n) time to copy the second string onto the end of it. Using this cost function, we can write a dynamic programming algorithm to find the fastest way to concatenate a sequence of strings It is possible to concatenate all in time proportional to sum of their lengths, but here we are interested to link this problem with chain matrix multiplication Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Generalization: Parallel Processors Another generalization is to solve the problem when many parallel processors are available. In this case, instead of adding the costs of computing each subsequence, we just take the maximum, because we can do them both simultaneously. This can drastically affect both the minimum cost and the final optimal grouping But of course more balanced groupings that keep all the processors busy is more favorable solution There exists some more sophisticated approaches to solve this problem Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Conclusion Created some notations to describe mathematical model of the chain matrix multiplication problem A recursive model was described Based on this model dynamic programming algorithm was designed An example was taken for applying model to solve dynamically, constructing optimal solution based on the given information Time complexity was computed for the Algorithm Applications of chain matrix problem are discussed Dr Nazir A. Zafar Advanced Algorithms Analysis and Design