Advanced Algorithms Analysis and Design

Slides:



Advertisements
Similar presentations
Welcome to our presentation
Advertisements

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.
Algorithm Design Methodologies Divide & Conquer Dynamic Programming Backtracking.
Dynamic Programming An algorithm design paradigm like divide-and-conquer “Programming”: A tabular method (not writing computer code) Divide-and-Conquer.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
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.
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.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Lecture 5 Dynamic Programming. Dynamic Programming Self-reducibility.
Catalan Numbers.
Dynamic Programming UNC Chapel Hill Z. Guo.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
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.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 4: Dynamic Programming Phan Th ị Hà D ươ ng 1.
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.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar 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.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Dynamic Programming Typically applied to optimization problems
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
Analysis of Algorithms
Advanced Algorithms Analysis and Design
Lecture 5 Dynamic Programming
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Seminar on Dynamic Programming.
Advanced Design and Analysis Techniques
Matrix Chain Multiplication
Lecture 5 Dynamic Programming
Dynamic Programming Several problems Principle of dynamic programming
Chapter 8 Dynamic Programming.
Objective of This Course
Matrix Chain Multiplication
Dynamic Programming General Idea
Advanced Algorithms Analysis and Design
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 1/15/ :41 PM
Dynamic Programming.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Comp 122, Fall 2004.
Dynamic Programming General Idea
DYNAMIC PROGRAMMING.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Matrix Chain Product 張智星 (Roger Jang)
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Matrix Chain Multiplication
CSCI 235, Spring 2019, Lecture 25 Dynamic Programming
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Divide-and-Conquer 7 2  9 4   2   4   7
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
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. 15. Dynamic Programming for Solving. Optimization Problems Lecture No. 15 Dynamic Programming for Solving Optimization Problems (Chain Matrix Multiplication Problem) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Today Covered Optimizations problem? Steps in Development of Dynamic Algorithms Why dynamic in optimization problem? Introduction to Catalan numbers Chain-Matrix Multiplication Problem Analysis Brute Force approach Time Complexity Conclusion Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Optimization Problems If a problem has only one correct solution, then optimization is not required For example, there is only one sorted sequence containing a given set of numbers. Optimization problems have many solutions. We want to compute an optimal solution e. g. with minimal cost and maximal gain. There could be many solutions having optimal value Dynamic programming is very effective technique Development of dynamic programming algorithms can be broken into a sequence steps as in the next. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Steps in Development of Dynamic Algorithms Characterize the structure of an optimal solution Recursively define the value of an optimal solution Compute the value of an optimal solution in a bottom-up fashion Construct an optimal solution from computed information Note: Steps 1-3 form the basis of a dynamic programming solution to a problem. Step 4 can be omitted only if the value of an optimal solution is required. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Why Dynamic Programming? Dynamic programming, like divide and conquer method, solves problems by combining the solutions to sub-problems. Divide and conquer algorithms: partition the problem into independent sub-problem Solve the sub-problem recursively and Combine their solutions to solve the original problem In contrast, dynamic programming is applicable when the sub-problems are not independent. Dynamic programming is typically applied to optimization problems. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Time Complexity in Dynamic Algorithms If there are polynomial number of sub-problems. If each sub-problem can be computed in polynomial time. Then the solution of whole problem can be found in polynomial time. Remark: Greedy also applies a top-down strategy but usually on one sub-problem so that the order of computation is clear Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Catalan Numbers Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Multiplying n Numbers n multiplication order 2 (x1 · x2) 3 (x1 · (x2 · x3)) ((x1 · x2) · x3) 4 (x1 · (x2 · (x3 · x4))) (x1 · ((x2 · x3) · x4)) ((x1 · x2) · (x3 · x4)) ((x1 · (x2 · x3)) · x4) (((x1 · x2) · x3) · x4) Objective: Find C(n), the number of ways to compute product x1 . x2 …. xn. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Multiplying n Numbers – small n C(n) 1 2 3 4 5 14 6 42 7 132 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Multiplying n Numbers - small n Recursive equation: where is the last multiplication? Catalan numbers: Asymptotic value: Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Chain-Matrix Multiplication 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 where 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

Objective is to find order not multiplication Given a sequence of matrices, we want to find a most efficient way to multiply these matrices It means that problem is not actually to perform the multiplications, but decide the order in which these must be multiplied to reduce the cost. This problem is an optimization type which can be solved using dynamic programming. The problem is not limited to find an efficient way of multiplication of matrices, but can be used to be applied in various purposes. But how to transform the original problem into chain matrix multiplication, this is another issue, which is common in systems modeling. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Why this problem is of Optimization Category? If these matrices are all square and of same size, the multiplication order will not affect the total cost. If matrices are of different sizes but compatible for multiplication, then order can make big difference. Brute Force approach The number of possible multiplication orders are exponential in n, and so trying all possible orders may take a very long time. Dynamic Programming To find an optimal solution, we will discuss it using dynamic programming to solve it efficiently. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Assumptions (Only Multiplications Considered) We really want is the minimum cost to multiply But we know that cost of an algorithm depends on how many number of operations are performed i.e. We must be interested to minimize number of operations, needed to multiply out the matrices. As in matrices multiplication, there will be addition as well multiplication operations in addition to other Since cost of multiplication is dominated over addition therefore we will minimize the number of multiplication operations in this problem. In case of two matrices, there is only one way to multiply them, so the cost fixed. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Chain Matrix Multiplication (Brute Force Approach) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Brute Force Chain Matrix Multiplication Algorithm If we wish to multiply two matrices: A = a[i, j]p, q and B = b[i, j]q, r Now if C = AB then order of C is p x r. Since in each entry c[i, j], there are q number of scalar of multiplications Total number of scalar multiplications in computing C = Total entries in C x Cost of computing a single entry = p . r . q Hence the computational cost of AB = p . q . r Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Brute Force Chain Matrix Multiplication Example Given a sequence [A1, A2, A3, A4] Order of A1 = 10 x 100 Order of A2 = 100 x 5 Order of A3 = 5x 50 Order of A4 = 50x 20 Compute the order of the product A1 . A2 . A3 . A4 in such a way that minimizes the total number of scalar multiplications. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Brute Force Chain Matrix Multiplication There are five ways to parenthesize this product Cost of computing the matrix product may vary, depending on order of parenthesis. All possible ways of parenthesizing (A1 · (A2 . (A3 . A4))) (A1 · ((A2 . A3). A4)) ((A1 · A2). (A3 . A4)) ((A1 · (A2 . A3)). A4) (((A1 · A2). A3). A4) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Kinds of problems solved by algorithms Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Second Chain : (A1 · ((A2 . A3). A4)) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Third Chain : ((A1 · A2). (A3 . A4)) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Fourth Chain : ((A1 · (A2 . A3)). A4) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Fifth Chain : (((A1 · A2). A3). A4) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Chain Matrix Cost ((A1 · A2). (A3 . A4)) First Chain 35,000 Second Chain 145,000 Third Chain 11,000 Fourth Chain 85,000 Fifth Chain 17,500 ((A1 · A2). (A3 . A4)) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Generalization of Brute Force Approach If there is sequence of n matrices, [A1, A2, . . . , An] Ai has dimension pi-1 x pi, where for i = 1, 2, . . . , n Find order of multiplication that minimizes number of scalar multiplications using brute force approach Recurrence Relation: After kth matrix, create two sub-lists, one with k and other with n - k matrices i.e. (A1 A2A3A4A5 . . . Ak) (Ak+1Ak+2…An) Let P(n) be the number of different ways of parenthesizing n items Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Generalization of Brute Force Approach If n = 2 P(2) = P(1).P(1) = 1.1 = 1 If n = 3 P(3) = P(1).P(2) + P(2).P(1) = 1.1 + 1.1 = 2 (A1 A2A3) = ((A1 . A2). A3) OR (A1 . (A2. A3)) If n = 4 P(4) = P(1).P(3) + P(2).P(2) + P(3).P(1) = 1.2 + 1.1 + 2.1 = 5 Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Why Brute Force Approach not Economical This is related to a famous function in combinatorics called the Catalan numbers. Catalan numbers are related with the number of different binary trees on n nodes. P(n)  (4n/n3/2) The dominating term is the exponential 4n thus P(n) will grow large very quickly. And hence this approach is not economical. Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Conclusion Introduction to optimization problem Dynamic Programming Comparison of Dynamic and Divide and conquer Chain matrix multiplication problem is introduced Analyzed that chain matrix is an optimization type problem Brute force solution is discussed Analysis is done and based on its analysis it was found that solution to chain matrix multiplication problem using brute force approach is not economical for large input Dr Nazir A. Zafar Advanced Algorithms Analysis and Design