# Dynamic Programming. Prof. S.J. Soni

Size: px
Start display at page:

## Transcription

1 Dynamic Programming Prof. S.J. Soni

2 Idea is Very Simple.. Introduction void calculating the same thing twice, usually by keeping a table of known results that fills up as subinstances are solved. Dynamic Programming is a bottom-up technique. We usually start with the smallest, and hence the simplest, subinstances. y combining their solutions, we obtain the answers to subinstances of increasing size, until finally we arrive at the solution of the original instance.

3 alculating the binomial coefficient onsider the problem of calculating the binomial coefficient 3

4 alculating the binomial coefficient Function (n,k) if k= or k=n then return 1 else return (n-1,k-1) + (n-1,k) alculate for (5,3) (4,), (4,3) (3,1), (3,) (3,), (3,3) (,), (,1), (,1), (,) (,1), (,), (,), (,3) This algorithm calculates same values again & again, so that it is inefficient algorithm. 4

5 Solution using Dynamic Programming Pascal s Triangle : 1 1: 1 1 : 1 1 3: : : : : :

6 Solution using Dynamic Programming Pascal s Triangle k-1 k : 1 1: 1 1 : 1 1 3: : : n-1: (n-1,k-1) (n-1,k) n: (n,k) 6

7 Making hange () Problem There are several instances of problem in which greedy algorithm fails to generate answer. For example, There are coins for 1,4 and 6 units. If we have to make change for 8 units, the greedy algorithm will propose doing so using one 6-unit coin and two 1-unit coins, for a total of three coins. However it is clearly possible to do better than this: we can give the customer his change using just two 4-unit coins. lthough the greedy algorithm does not find this solution, it is easily obtained using dynamic programming. 7

8 Solution using Dynamic Programming To solve this problem using DP, we set up a table c [1 n, N], with one row for each available denomination and one column for each amount from to N units. In this table c [i,j] will be the minimum number of coins required to pay an amount of j units, <= j <=N. c [i,j] = min (c[i-1,j], 1+c[i, j - di]) 8

9 Solution using Dynamic Programming mount d1= d= d3= c [i,j] = min (c[i-1,j], 1+c[i, j - di]) c [,1] = min (c[1,1], 1+c[, 1-4]) = min(1,inf) =1 c [,] = min (c[1,], 1+c[, - 4]) = min(,inf) = c [,4] = min (c[1,4], 1+c[, 4-4]) = min(4,1) =1 c [3,8] = min (c[,8], 1+c[3, 8-6]) = min(,3) = 9

10 Solution using Dynamic Programming Which oins? mount d1= d= d3= If c[i,j]=c[i-1,j], we move up to c[i-1, j] c[3,8] = c[,8], we move on to c[,8] Otherwise check c[i,j]=1+c[i,j-di] so check c[,8]=1+c[,8-4] = 1+c[,4]= [select coin] gain check c[,4]<>c[1,4] so, c[,4]=1+c[,4-4]=1 [select coin] 1

11 Knapsack Problem () We are given number of objects and knapsack. This time, however, we suppose that the objects may not be broken into smaller pieces, we may decide either to take an object or to leave it behind, but we may not take a fraction of an object. So it s called Non-fractional knapsack problem or /1 knapsack problem. 11

12 Knapsack Problem () Example for which greedy method cannot work. Object 1 3 Weight Value W=1 Greedy method generates the value 8 which is not optimal. 1

13 Solution using Dynamic Programming To solve this problem using DP, we set up a table V [1 n, W], with one row for each available object and one column for each weight from to W. In this table V [i,j] will be the maximum value of the objects, <= j <=W & 1<= i <=n V [i,j] = max (V[i-1,j], V[i-1, j - wi]+vi) 13

14 Knapsack Problem () Example Objects Weight Value Vi/Wi W=11 14

15 Solution using Dynamic Programming Weight Limit w1=1, v1= w= v= w3=5, v3= w4=6, v4= w5=7, v5= V [i,j] = max (V[i-1,j], V[i-1, j - wi]+vi) V [5,11] = max (V[4,11], V[4,11-7] +8 )= max(4,35)=4 15

16 Solution using Dynamic Programming Weight Limit w1=1, v1= w= v= w3=5, v3= w4=6, v4= w5=7, v5= Which Objects? If V[i,j]=V[i-1,j], we move up to V[i-1, j] V[5,11] = V[4,11], we move on to V[4,11] Otherwise check V[i,j]=V[i-1,j-wi]+vi so check V[4,11]=V[3,11-6]+ = V[3,5]+=4 [select object 4] now check V[3,5]=V[,5-5]+18=V[,]+18=18 [select object 3] 16

17 Practice Examples Solve following knapsack problem using dynamic programming algorithm with given capacity W=5, Weight and Value are as follows : (,1),(1,1),(3,),(,15). Solve the following Knapsack Problem using Dynamic Programming Method. Write the equation for solving above problem. n = 5, W = 1 Object Weight (w) Value (v)

18 Shortest Paths Let G = <N,> be a directed graph; N is a set of nodes and is a set of edges. We want to calculate the length of the shortest path between each pair of nodes. Suppose the nodes of G are numbered from 1 to n, so N=[1,, n] and suppose matrix L gives the length of each edge, with L[i,j]= for i=1,,..n. L[i,j] >= for all i and j, and L[i,j]= if the edge (i,j) does not exist. 18

19 Shortest Paths The principle of optimality: If k is a node on the shortest path from i to j, then the part of the path from i to k, and the part from k to j, must also be optimal. We construct matrix D that gives the length of the shortest path between each pair of nodes. D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j]) 19

20 Floyd s lgorithm D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j]) D 1 = D = L = D 1 [3,] = min(d [3,], D [3,1]+D [1,]) = min(inf, 3+5) = 35

21 Floyd s lgorithm D = L = D 1 = D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j]) D 1 [1,1] = min(d [1,1], D [1,1]+D [1,1]) = min(, + ) = D 1 [1,] = min(d [1,], D [1,1]+D [1,]) = min(5, + 5) = 5 D 1 [,1] = min(d [,1], D [,1]+D [1,1]) = min(5, 5+ ) = 5 D 1 [3,] = min(d [3,], D [3,1]+D [1,]) = min(inf, 3+5) = 35 1

22 Floyd s lgorithm D = L = D 1 = D = D 3 = D 4 = D k [i,j] = min(d k-1 [i,j], D k-1 [i,k]+d k-1 [k,j])

23 Practice Example Write the equation for finding out shortest path using Floyd s algorithm. Use Floyd s method to find shortest path for below mentions all pairs

24 hained Matrix Multiplication Recall that the product of a p x q matrix and a q x r matrix is the p x r matrix given by q ij = a ik b kj k=1 1<=i<=p, 1<=j<=r It is clear that a total of pqr scalar multiplications are required to calculate the matrix product using this algorithm. e.g. (3X5) (5X4) => (3X4) [3X5X4=6 multiplications] 4

25 hained Matrix Multiplication Suppose now we want to calculate the product of more than two matrices. Matrix multiplication is associative, so we can compute the matrix product M = M 1 M. M n in a number of ways, which all gives the same answer. M = (((M 1 M ) M 3 ). M n ) = ((M 1 M ) (M 3. M n )) = ((M 1 (M M 3 )). M n )) and so on. However matrix multiplication is not commutative, so we are not allowed to change the order of the matrices in these arrangements. 5

26 MM - Example Suppose for example, we want to calculate the product D of four matrices, where is 13X5, is 5X89, is 89X3 and D is 3X34 Instances (())D => =5785, ()=3471, (())D=136 => = 158 scalar multpls ()(D) = 541 (()D) = 455 (())D = 856 ((D)) = 6418 The most efficient method is almost 19 times faster than the slowest. 6

27 MM Example Number of combinations N T(n) The values of T(n) are called the atalan numbers. 7

28 hained Matrix Multiplication Suppose the dimensions of the matrices are given by a vector d[..n] such that the matrix M i, 1<= i<=n, is of dimension d i-1 X d i We fill the table m ij using the following rules for s=,1,, n-1. s= : m ii = i=1,,.,n s=1 : m i,i+1 = d i-1 d i d i+1 i=1,,.,n-1 1<s<n: m i,i+s = min (m ik +m k+1,i+s +d i-1 d k d i+s ) i<= k < i+s i=1,,,n-s 8

29 hained Matrix Multiplication Suppose for example, we want to calculate the product D of four matrices, where is 13X5, is 5X89, is 89X3 and D is 3X34 do d1 d d3 d4 The vector d is therefore (13, 5, 89, 3, 34). s=1 : m i,i+1 = d i-1 d i d i+1 i=1,,.,n-1 For s=1, we find m1 = d d1 d = 13X5X89 = 5785 m3 = d1 d d3 = 5X89X3 = 1335 m34 = d d3 d4 = 89X3X34 = 978 9

30 hained Matrix Multiplication 1<s<n: m i,i+s = min (m ik +m k+1,i+s +d i-1 d k d i+s ) i<= k < i+s For s=, we obtain, [where 1<s<n=1<<4 and i = 1,..,4-, so i=1,] [ i<=k<i+s = 1<=k<3, so k=1 & k=] m13 = min (m11+m3+13x5x3, // for k=1 m1+m33+13x89x3) // for k= = min (153,956) = 153 [ i<=k<i+s = <=k<4, so k= & k=3] m4 = min (m+m34+5x89x34, // for k= m3+m44+5x3x34) // for k=3 = min (48,1845) = 1845 i=1,,,n-s 3

31 hained Matrix Multiplication 1<s<n: m i,i+s = min (m ik +m k+1,i+s +d i-1 d k d i+s ) i<= k < i+s Finally for s=3, we obtain, [where 1<s<n=1<3<4 and i = 1,..,4-3, so i=1 only] [ i<=k<i+s = 1<=k<4, so k=1, k=, k=3] m14 = min (m11+m4+13x5x34, // for k=1 m1+m34+13x89x34, // for k= m13+m14+13x3x34) // for k=3 = min (455,541,856) = 856 i=1,,,n-s 31

32 hained Matrix Multiplication j=1 3 4 i= s=3 s= s=1 s= Solution: (( ( )) D) is 13X5, is 5X89, is 89X3 and D is 3X34 [()= ()=195 + (())D=136] = 856 3

33 GTU Practice Examples Given the four matrix find out optimal sequence for multiplication D=<5,4,6,,7> Using algorithm find an optimal parenthesization of a matrix chain product whose sequence of dimension is 5,1,3,1,5,5,6 (use dynamic programming). 33

34 Longest ommon Subsequence Problem using Dynamic Programming 34

35 Dynamic programming It is used, when the solution can be recursively described in terms of solutions to subproblems (optimal substructure) lgorithm finds solutions to subproblems and stores them in memory for later use More efficient than brute-force methods, which solve the same subproblems over and over again 35

36 Longest ommon Subsequence (LS) pplication: comparison of two DN strings Ex: X= { D }, Y= { D } Longest ommon Subsequence: X = D Y = D rute force algorithm would compare each subsequence of X with the symbols in Y 36

37 LS lgorithm if X = m, Y = n, then there are m subsequences of x; we must compare each with Y (n comparisons) So the running time of the brute-force algorithm is O(n m ) Notice that the LS problem has optimal substructure: solutions of subproblems are parts of the final solution. Subproblems: find LS of pairs of prefixes of X and Y 37

38 LS lgorithm First we ll find the length of LS. Later we ll modify the algorithm to find LS itself. Define X i, Y j to be the prefixes of X and Y of length i and j respectively Define c[i,j] to be the length of LS of X i and Y j Then the length of LS of X and Y will be c[m,n] c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], 38

39 LS recursive solution c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], We start with i = j = (empty substrings of x and y) Since X and Y are empty strings, their LS is always empty (i.e. c[,] = ) LS of empty string and any other string is empty, so for every i and j: c[, j] = c[i,] = 39

40 LS recursive solution c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], When we calculate c[i,j], we consider two cases: First case: x[i]=y[j]: one more symbol in strings X and Y matches, so the length of LS X i and Y j equals to the length of LS of smaller strings X i-1 and Y i-1, plus 1 4

41 LS recursive solution c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], Second case: x[i]!= y[j] s symbols don t match, our solution is not improved, and the length of LS(X i, Y j ) is the same as before (i.e. maximum of LS(X i, Y j-1 ) and LS(X i-1,y j ) 41

42 LS Example We ll see how LS algorithm works on the following example: X = Y = D What is the Longest ommon Subsequence of X and Y? LS(X, Y) = X = Y = D 4

43 LS Example () j D i Yj D Xi X = ; m = X = 4 Y = D; n = Y = 5 llocate array c[4,5] 43

44 LS Example (1) j D i Yj D Xi for i = 1 to m c[i,] = for j = 1 to n c[,j] = 44

45 LS Example () j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 45

46 LS Example (3) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 46

47 LS Example (4) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 47

48 LS Example (5) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 48

49 LS Example (6) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 49

50 LS Example (7) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 5

51 LS Example (8) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 51

52 LS Example (1) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 5

53 LS Example (11) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 53

54 LS Example (1) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 54

55 LS Example (13) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 55

56 LS Example (14) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 56

57 LS Example (15) j D i Yj D Xi if ( X i == Y j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] ) 57

58 LS lgorithm Running Time LS algorithm calculates the values of each entry of the array c[m,n] So what is the running time? O(m*n) since each c[i,j] is calculated in constant time, and there are m*n elements in the array 58

59 How to find actual LS So far, we have just found the length of LS, but not LS itself. We want to modify this algorithm to make it output Longest ommon Subsequence of X and Y Each c[i,j] depends on c[i-1,j] and c[i,j-1] or c[i-1, j-1] For each c[i,j] we can say how it was acquired: 3 For example, here c[i,j] = c[i-1,j-1] +1 = +1=3 59

60 How to find actual LS - continued Remember that c[ i, j] c[ i 1, j 1] 1 max( c[ i, j 1], c[ i 1, j]) if x[ i] y[ otherwise j], So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-1, j-1]+1, remember x[i] (because x[i] is a part of LS) When i= or j= (i.e. we reached the beginning), output remembered letters in reverse order 6

61 Finding LS j i Yj D Xi

62 Finding LS () j i Yj D Xi LS (reversed order): LS (straight order): (this string turned Prof. S.J. Soni out - SPE, to Visnagar be a palindrome) 6

63 GTU Practice Examples Given two sequences of characters, P=<MLNOM> Q=<MNOM> Obtain the longest common subsequence. Find Longest ommon Subsequence using Dynamic Programming Technique with illustration X={,,,,D,,} Y={,D,,,,} Using algorithm determine an Longest ommon Sequence of,,,d,,,,d,f and,,,f (use dynamic programming). Explain how to find out Longest ommon Subsequence of two strings using Dynamic Programming method.find any one Longest ommon Subsequence of given two strings using Dynamic Programming. S1=abbacdcba S=bcdbbcaac 63

### Dynamic Programming. p. 1/43

Dynamic Programming Formalized by Richard Bellman Programming relates to planning/use of tables, rather than computer programming. Solve smaller problems first, record solutions in a table; use solutions

### Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 03 Dynamic Programming (Chapter 15) Stephen Scott and Vinodchandran N. Variyam sscott@cse.unl.edu 1/44 Introduction Dynamic

### Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of s Lecture 09 Dynamic Programming (Chapter 15) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 41 Spring 2010 Dynamic programming

### Dynamic Programming ACM Seminar in Algorithmics

Dynamic Programming ACM Seminar in Algorithmics Marko Genyk-Berezovskyj berezovs@fel.cvut.cz Tomáš Tunys tunystom@fel.cvut.cz CVUT FEL, K13133 February 27, 2013 Problem: Longest Increasing Subsequence

### Introduction. I Dynamic programming is a technique for solving optimization problems. I Key element: Decompose a problem into subproblems, solve them

ntroduction Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 03 Dynamic Programming (Chapter 15) Stephen Scott and Vinodchandran N. Variyam Dynamic programming is a technique

### CS473 - Algorithms I

CS473 - Algorithms I Lecture 10 Dynamic Programming View in slide-show mode CS 473 Lecture 10 Cevdet Aykanat and Mustafa Ozdal, Bilkent University 1 Introduction An algorithm design paradigm like divide-and-conquer

### Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Lecture 13 More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees. Announcements HW5 due Friday! HW6 released Friday! Last time Not coding in an action

### Partha Sarathi Mandal

MA 252: Data Structures and Algorithms Lecture 32 http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html Partha Sarathi Mandal Dept. of Mathematics, IIT Guwahati The All-Pairs Shortest Paths Problem

### Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Lecture 13 More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees. Announcements HW5 due Friday! HW6 released Friday! Last time Not coding in an action

### Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

Dynamic Programming Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. August 31, 2015 Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication

### CS 470/570 Dynamic Programming. Format of Dynamic Programming algorithms:

CS 470/570 Dynamic Programming Format of Dynamic Programming algorithms: Use a recursive formula But recursion can be inefficient because it might repeat the same computations multiple times So use an

### Matrices: 2.1 Operations with Matrices

Goals In this chapter and section we study matrix operations: Define matrix addition Define multiplication of matrix by a scalar, to be called scalar multiplication. Define multiplication of two matrices,

### DAA Unit- II Greedy and Dynamic Programming. By Mrs. B.A. Khivsara Asst. Professor Department of Computer Engineering SNJB s KBJ COE, Chandwad

DAA Unit- II Greedy and Dynamic Programming By Mrs. B.A. Khivsara Asst. Professor Department of Computer Engineering SNJB s KBJ COE, Chandwad 1 Greedy Method 2 Greedy Method Greedy Principal: are typically

### General Methods for Algorithm Design

General Methods for Algorithm Design 1. Dynamic Programming Multiplication of matrices Elements of the dynamic programming Optimal triangulation of polygons Longest common subsequence 2. Greedy Methods

### Dynamic Programming. Credits: Many of these slides were originally authored by Jeff Edmonds, York University. Thanks Jeff!

Dynamic Programming Credits: Many of these slides were originally authored by Jeff Edmonds, York University. Thanks Jeff! Optimization Problems For most, the best known algorithm runs in exponential time.

### CMPSCI 311: Introduction to Algorithms Second Midterm Exam

CMPSCI 311: Introduction to Algorithms Second Midterm Exam April 11, 2018. Name: ID: Instructions: Answer the questions directly on the exam pages. Show all your work for each question. Providing more

### Week 7 Solution. The two implementations are 1. Approach 1. int fib(int n) { if (n <= 1) return n; return fib(n 1) + fib(n 2); } 2.

Week 7 Solution 1.You are given two implementations for finding the nth Fibonacci number(f Fibonacci numbers are defined by F(n = F(n 1 + F(n 2 with F(0 = 0 and F(1 = 1 The two implementations are 1. Approach

### Algorithm Design and Analysis

Algorithm Design and Analysis LECTURE 18 Dynamic Programming (Segmented LS recap) Longest Common Subsequence Adam Smith Segmented Least Squares Least squares. Foundational problem in statistic and numerical

### Dynamic Programming. Weighted Interval Scheduling. Algorithmic Paradigms. Dynamic Programming

lgorithmic Paradigms Dynamic Programming reed Build up a solution incrementally, myopically optimizing some local criterion Divide-and-conquer Break up a problem into two sub-problems, solve each sub-problem

### CS483 Design and Analysis of Algorithms

CS483 Design and Analysis of Algorithms Lectures 15-16 Dynamic Programming Instructor: Fei Li lifei@cs.gmu.edu with subject: CS483 Office hours: STII, Room 443, Friday 4:00pm - 6:00pm or by appointments

### CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CSE 431/531: Analysis of Algorithms Dynamic Programming Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Paradigms for Designing Algorithms Greedy algorithm Make a

### CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms Professor Henry Carter Fall 2016 Recap Space-time tradeoffs allow for faster algorithms at the cost of space complexity overhead Dynamic

### Lecture 4: An FPTAS for Knapsack, and K-Center

Comp 260: Advanced Algorithms Tufts University, Spring 2016 Prof. Lenore Cowen Scribe: Eric Bailey Lecture 4: An FPTAS for Knapsack, and K-Center 1 Introduction Definition 1.0.1. The Knapsack problem (restated)

### Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

Greedy Algorithm 1 Introduction Similar to dynamic programming. Used for optimization problems. Not always yield an optimal solution. Make choice for the one looks best right now. Make a locally optimal

### Determining an Optimal Parenthesization of a Matrix Chain Product using Dynamic Programming

Determining an Optimal Parenthesization of a Matrix Chain Product using Dynamic Programming Vivian Brian Lobo 1, Flevina D souza 1, Pooja Gharat 1, Edwina Jacob 1, and Jeba Sangeetha Augestin 1 1 Department

### Algorithms Design & Analysis. Dynamic Programming

Algorithms Design & Analysis Dynamic Programming Recap Divide-and-conquer design paradigm Today s topics Dynamic programming Design paradigm Assembly-line scheduling Matrix-chain multiplication Elements

### CS60007 Algorithm Design and Analysis 2018 Assignment 1

CS60007 Algorithm Design and Analysis 2018 Assignment 1 Palash Dey and Swagato Sanyal Indian Institute of Technology, Kharagpur Please submit the solutions of the problems 6, 11, 12 and 13 (written in

### Dynamic Programming. Cormen et. al. IV 15

Dynamic Programming Cormen et. al. IV 5 Dynamic Programming Applications Areas. Bioinformatics. Control theory. Operations research. Some famous dynamic programming algorithms. Unix diff for comparing

### Chapter 6. Weighted Interval Scheduling. Dynamic Programming. Algorithmic Paradigms. Dynamic Programming Applications

lgorithmic Paradigms hapter Dynamic Programming reedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer. Break up a problem into sub-problems, solve each

### Dynamic Programming (CLRS )

Dynamic Programming (CLRS.-.) Today we discuss a technique called dynamic programming. It is neither especially dynamic nor especially programming related. We will discuss dynamic programming by looking

### Lecture 9. Greedy Algorithm

Lecture 9. Greedy Algorithm T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018

### Graduate Algorithms CS F-09 Dynamic Programming

Graduate Algorithms CS673-216F-9 Dynamic Programming David Galles Department of Computer Science University of San Francisco 9-: Recursive Solutions Divide a problem into smaller subproblems Recursively

### A Review of Matrix Analysis

Matrix Notation Part Matrix Operations Matrices are simply rectangular arrays of quantities Each quantity in the array is called an element of the matrix and an element can be either a numerical value

### Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

Aside: Golden Ratio Golden Ratio: A universal law. Golden ratio φ = lim n a n+b n a n = 1+ 5 2 a n+1 = a n + b n, b n = a n 1 Ruta (UIUC) CS473 1 Spring 2018 1 / 41 CS 473: Algorithms, Spring 2018 Dynamic

### INF4130: Dynamic Programming September 2, 2014 DRAFT version

INF4130: Dynamic Programming September 2, 2014 DRAFT version In the textbook: Ch. 9, and Section 20.5 Chapter 9 can also be found at the home page for INF4130 These slides were originally made by Petter

### Lecture 13: Dynamic Programming Part 2 10:00 AM, Feb 23, 2018

CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 13: Dynamic Programming Part 2 10:00 AM, Feb 23, 2018 Contents 1 Holidays 1 1.1 Halloween..........................................

### Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Algorithms and Theory of Computation Lecture 9: Dynamic Programming Xiaohui Bei MAS 714 September 10, 2018 Nanyang Technological University MAS 714 September 10, 2018 1 / 21 Recursion in Algorithm Design

### Dynamic programming. Curs 2015

Dynamic programming. Curs 2015 Fibonacci Recurrence. n-th Fibonacci Term INPUT: n nat QUESTION: Compute F n = F n 1 + F n 2 Recursive Fibonacci (n) if n = 0 then return 0 else if n = 1 then return 1 else

### Lecture 2: Pairwise Alignment. CG Ron Shamir

Lecture 2: Pairwise Alignment 1 Main source 2 Why compare sequences? Human hexosaminidase A vs Mouse hexosaminidase A 3 www.mathworks.com/.../jan04/bio_genome.html Sequence Alignment עימוד רצפים The problem:

### After linear functions, the next simplest functions are polynomials. Examples are

Mathematics 10 Fall 1999 Polynomials After linear functions, the next simplest functions are polynomials. Examples are 1+x, x +3x +, x 3. Here x is a variable. Recall that we have defined a function to

### CS325: Analysis of Algorithms, Fall Final Exam

CS: Analysis of Algorithms, Fall 0 Final Exam I don t know policy: you may write I don t know and nothing else to answer a question and receive percent of the total points for that problem whereas a completely

### Courses 12-13: Dynamic programming

Courses 12-13: Dynamic programming 1 Outline What is dynamic programming? Main steps in applying dyamic programming Recurrence relations: ascending versus descendent Applications 2 What is dynamic programming?

### Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

Dynamic Programming: Matrix chain multiplication (CLRS.) The problem Given a sequence of matrices A, A, A,..., A n, find the best way (using the minimal number of multiplications) to compute their product.

### Greedy Algorithms My T. UF

Introduction to Algorithms Greedy Algorithms @ UF Overview A greedy algorithm always makes the choice that looks best at the moment Make a locally optimal choice in hope of getting a globally optimal solution

### More Dynamic Programming

CS 374: Algorithms & Models of Computation, Spring 2017 More Dynamic Programming Lecture 14 March 9, 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 42 What is the running time of the following? Consider

### Stage-structured Populations

Department of Biology New Mexico State University Las Cruces, New Mexico 88003 brook@nmsu.edu Fall 2009 Age-Structured Populations All individuals are not equivalent to each other Rates of survivorship

### More Dynamic Programming

Algorithms & Models of Computation CS/ECE 374, Fall 2017 More Dynamic Programming Lecture 14 Tuesday, October 17, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 48 What is the running time of the following?

### Note that M i,j depends on two entries in row (i 1). If we proceed in a row major order, these two entries will be available when we are ready to comp

CSE 3500 Algorithms and Complexity Fall 2016 Lecture 18: October 27, 2016 Dynamic Programming Steps involved in a typical dynamic programming algorithm are: 1. Identify a function such that the solution

### ME751 Advanced Computational Multibody Dynamics

ME751 Advanced Computational Multibody Dynamics Review: Elements of Linear Algebra & Calculus September 9, 2016 Dan Negrut University of Wisconsin-Madison Quote of the day If you can't convince them, confuse

### Dynamic Programming. Shuang Zhao. Microsoft Research Asia September 5, Dynamic Programming. Shuang Zhao. Outline. Introduction.

Microsoft Research Asia September 5, 2005 1 2 3 4 Section I What is? Definition is a technique for efficiently recurrence computing by storing partial results. In this slides, I will NOT use too many formal

### CSE 421 Dynamic Programming

CSE Dynamic Programming Yin Tat Lee Weighted Interval Scheduling Interval Scheduling Job j starts at s(j) and finishes at f j and has weight w j Two jobs compatible if they don t overlap. Goal: find maximum

### Analysis of Algorithms I: All-Pairs Shortest Paths

Analysis of Algorithms I: All-Pairs Shortest Paths Xi Chen Columbia University The All-Pairs Shortest Paths Problem. Input: A directed weighted graph G = (V, E) with an edge-weight function w : E R. Output:

### Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 5311 Lecture 22 All-Pairs Shortest Paths Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 All Pairs

### Chapter 6. Properties of Regular Languages

Chapter 6 Properties of Regular Languages Regular Sets and Languages Claim(1). The family of languages accepted by FSAs consists of precisely the regular sets over a given alphabet. Every regular set is

### Vector, Matrix, and Tensor Derivatives

Vector, Matrix, and Tensor Derivatives Erik Learned-Miller The purpose of this document is to help you learn to take derivatives of vectors, matrices, and higher order tensors (arrays with three dimensions

### 1 Assembly Line Scheduling in Manufacturing Sector

Indian Institute of Information Technology Design and Manufacturing, Kancheepuram, Chennai 600 27, India An Autonomous Institute under MHRD, Govt of India http://www.iiitdm.ac.in COM 209T Design and Analysis

### Phys 201. Matrices and Determinants

Phys 201 Matrices and Determinants 1 1.1 Matrices 1.2 Operations of matrices 1.3 Types of matrices 1.4 Properties of matrices 1.5 Determinants 1.6 Inverse of a 3 3 matrix 2 1.1 Matrices A 2 3 7 =! " 1

### BSc (Hons) in Computer Games Development. vi Calculate the components a, b and c of a non-zero vector that is orthogonal to

1 APPLIED MATHEMATICS INSTRUCTIONS Full marks will be awarded for the correct solutions to ANY FIVE QUESTIONS. This paper will be marked out of a TOTAL MAXIMUM MARK OF 100. Credit will be given for clearly

### Complexity Theory of Polynomial-Time Problems

Complexity Theory of Polynomial-Time Problems Lecture 1: Introduction, Easy Examples Karl Bringmann and Sebastian Krinninger Audience no formal requirements, but: NP-hardness, satisfiability problem, how

### Lecture #8: We now take up the concept of dynamic programming again using examples.

Lecture #8: 0.0.1 Dynamic Programming:(Chapter 15) We now take up the concept of dynamic programming again using examples. Example 1 (Chapter 15.2) Matrix Chain Multiplication INPUT: An Ordered set of

### Areas. ! Bioinformatics. ! Control theory. ! Information theory. ! Operations research. ! Computer science: theory, graphics, AI, systems,.

lgorithmic Paradigms hapter Dynamic Programming reed Build up a solution incrementally, myopically optimizing some local criterion Divide-and-conquer Break up a problem into two sub-problems, solve each

### CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 20: Dynamic Programming

CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.ucsd.edu Office 4208 CSE Building Lecture 20: Dynamic Programming DYNAMIC PROGRAMMING Dynamic programming is an algorithmic paradigm in which

### Outline / Reading. Greedy Method as a fundamental algorithm design technique

Greedy Method Outline / Reading Greedy Method as a fundamental algorithm design technique Application to problems of: Making change Fractional Knapsack Problem (Ch. 5.1.1) Task Scheduling (Ch. 5.1.2) Minimum

### CS 580: Algorithm Design and Analysis

CS 58: Algorithm Design and Analysis Jeremiah Blocki Purdue University Spring 28 Announcement: Homework 3 due February 5 th at :59PM Midterm Exam: Wed, Feb 2 (8PM-PM) @ MTHW 2 Recap: Dynamic Programming

### Approximation: Theory and Algorithms

Approximation: Theory and Algorithms The String Edit Distance Nikolaus Augsten Free University of Bozen-Bolzano Faculty of Computer Science DIS Unit 2 March 6, 2009 Nikolaus Augsten (DIS) Approximation:

### A primer on matrices

A primer on matrices Stephen Boyd August 4, 2007 These notes describe the notation of matrices, the mechanics of matrix manipulation, and how to use matrices to formulate and solve sets of simultaneous

### Data Structures and Algorithms

Data Structures and Algorithms CS245-2017S-22 Dynamic Programming David Galles Department of Computer Science University of San Francisco 22-0: Dynamic Programming Simple, recursive solution to a problem

### CS173 Running Time and Big-O. Tandy Warnow

CS173 Running Time and Big-O Tandy Warnow CS 173 Running Times and Big-O analysis Tandy Warnow Today s material We will cover: Running time analysis Review of running time analysis of Bubblesort Review

### All-Pairs Shortest Paths

All-Pairs Shortest Paths Version of October 28, 2016 Version of October 28, 2016 All-Pairs Shortest Paths 1 / 26 Outline Another example of dynamic programming Will see two different dynamic programming

### Lecture 2: Divide and conquer and Dynamic programming

Chapter 2 Lecture 2: Divide and conquer and Dynamic programming 2.1 Divide and Conquer Idea: - divide the problem into subproblems in linear time - solve subproblems recursively - combine the results in

### Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

Assignment 4 CSci 3110: Introduction to Algorithms Sample Solutions Question 1 Denote the points by p 1, p 2,..., p n, ordered by increasing x-coordinates. We start with a few observations about the structure

### CSE 421 Weighted Interval Scheduling, Knapsack, RNA Secondary Structure

CSE Weighted Interval Scheduling, Knapsack, RNA Secondary Structure Shayan Oveis haran Weighted Interval Scheduling Interval Scheduling Job j starts at s(j) and finishes at f j and has weight w j Two jobs

### What is Dynamic Programming

What is Dynamic Programming Like DaC, Greedy algorithms, Dynamic Programming is another useful method for designing efficient algorithms. Why the name? Eye of the Hurricane: An Autobiography - A quote

### Question Paper Code :

www.vidyarthiplus.com Reg. No. : B.E./B.Tech. DEGREE EXAMINATION, NOVEMBER/DECEMBER 2011. Time : Three hours Fourth Semester Computer Science and Engineering CS 2251 DESIGN AND ANALYSIS OF ALGORITHMS (Regulation

### Notes on vectors and matrices

Notes on vectors and matrices EE103 Winter Quarter 2001-02 L Vandenberghe 1 Terminology and notation Matrices, vectors, and scalars A matrix is a rectangular array of numbers (also called scalars), written

### Similarity Search. The String Edit Distance. Nikolaus Augsten. Free University of Bozen-Bolzano Faculty of Computer Science DIS. Unit 2 March 8, 2012

Similarity Search The String Edit Distance Nikolaus Augsten Free University of Bozen-Bolzano Faculty of Computer Science DIS Unit 2 March 8, 2012 Nikolaus Augsten (DIS) Similarity Search Unit 2 March 8,

### Matrices. Chapter Definitions and Notations

Chapter 3 Matrices 3. Definitions and Notations Matrices are yet another mathematical object. Learning about matrices means learning what they are, how they are represented, the types of operations which

### Copyright 2000, Kevin Wayne 1

/9/ lgorithmic Paradigms hapter Dynamic Programming reed. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer. Break up a problem into two sub-problems, solve

### CSE 202 Homework 4 Matthias Springer, A

CSE 202 Homework 4 Matthias Springer, A99500782 1 Problem 2 Basic Idea PERFECT ASSEMBLY N P: a permutation P of s i S is a certificate that can be checked in polynomial time by ensuring that P = S, and

### Mathematics for Graphics and Vision

Mathematics for Graphics and Vision Steven Mills March 3, 06 Contents Introduction 5 Scalars 6. Visualising Scalars........................ 6. Operations on Scalars...................... 6.3 A Note on

### Linear Algebra. Matrices Operations. Consider, for example, a system of equations such as x + 2y z + 4w = 0, 3x 4y + 2z 6w = 0, x 3y 2z + w = 0.

Matrices Operations Linear Algebra Consider, for example, a system of equations such as x + 2y z + 4w = 0, 3x 4y + 2z 6w = 0, x 3y 2z + w = 0 The rectangular array 1 2 1 4 3 4 2 6 1 3 2 1 in which the

### Advanced Engineering Mathematics Prof. Pratima Panigrahi Department of Mathematics Indian Institute of Technology, Kharagpur

Advanced Engineering Mathematics Prof. Pratima Panigrahi Department of Mathematics Indian Institute of Technology, Kharagpur Lecture No. # 02 Vector Spaces, Subspaces, linearly Dependent/Independent of

### Data Structures in Java

Data Structures in Java Lecture 20: Algorithm Design Techniques 12/2/2015 Daniel Bauer 1 Algorithms and Problem Solving Purpose of algorithms: find solutions to problems. Data Structures provide ways of

### Similarity Search. The String Edit Distance. Nikolaus Augsten.

Similarity Search The String Edit Distance Nikolaus Augsten nikolaus.augsten@sbg.ac.at Dept. of Computer Sciences University of Salzburg http://dbresearch.uni-salzburg.at Version October 18, 2016 Wintersemester

### Find: a multiset M { 1,..., n } so that. i M w i W and. i M v i is maximized. Find: a set S { 1,..., n } so that. i S w i W and. i S v i is maximized.

Knapsack gain Slides for IS 675 PV hapter 6: ynamic Programming, Part 2 Jim Royer EES October 28, 2009 The Knapsack Problem (KP) knapsack with weight capacity W. Items 1,..., n where item i has weight

### Outline. Approximation: Theory and Algorithms. Motivation. Outline. The String Edit Distance. Nikolaus Augsten. Unit 2 March 6, 2009

Outline Approximation: Theory and Algorithms The Nikolaus Augsten Free University of Bozen-Bolzano Faculty of Computer Science DIS Unit 2 March 6, 2009 1 Nikolaus Augsten (DIS) Approximation: Theory and

### Regular Expressions and Language Properties

Regular Expressions and Language Properties Mridul Aanjaneya Stanford University July 3, 2012 Mridul Aanjaneya Automata Theory 1/ 47 Tentative Schedule HW #1: Out (07/03), Due (07/11) HW #2: Out (07/10),

### CS583 Lecture 11. Many slides here are based on D. Luebke slides. Review: Dynamic Programming

// CS8 Lecture Jana Kosecka Dynamic Programming Greedy Algorithms Many slides here are based on D. Luebke slides Review: Dynamic Programming A meta-technique, not an algorithm (like divide & conquer) Applicable

### 1 Matrices and matrix algebra

1 Matrices and matrix algebra 1.1 Examples of matrices A matrix is a rectangular array of numbers and/or variables. For instance 4 2 0 3 1 A = 5 1.2 0.7 x 3 π 3 4 6 27 is a matrix with 3 rows and 5 columns

### Chapter 6. Dynamic Programming. CS 350: Winter 2018

Chapter 6 Dynamic Programming CS 350: Winter 2018 1 Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer. Break up a problem into

### /633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17 12.1 Introduction Today we re going to do a couple more examples of dynamic programming. While

### CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

CS 470/570 Divide-and-Conquer Format of Divide-and-Conquer algorithms: Divide: Split the array or list into smaller pieces Conquer: Solve the same problem recursively on smaller pieces Combine: Build the

### 2.6 Complexity Theory for Map-Reduce. Star Joins 2.6. COMPLEXITY THEORY FOR MAP-REDUCE 51

2.6. COMPLEXITY THEORY FOR MAP-REDUCE 51 Star Joins A common structure for data mining of commercial data is the star join. For example, a chain store like Walmart keeps a fact table whose tuples each

### An Introduction to NeRDS (Nearly Rank Deficient Systems)

(Nearly Rank Deficient Systems) BY: PAUL W. HANSON Abstract I show that any full rank n n matrix may be decomposento the sum of a diagonal matrix and a matrix of rank m where m < n. This decomposition