Download presentation

Presentation is loading. Please wait.

Published byAlexandra O'Grady Modified over 3 years ago

1
Welcome to our presentation Presentation topic : Dynamic programming Presented by: 1. Shahin Pervin ( ) 2. Mostakima Yesmin ( ) 3. Jyotirmoyee Saha ( )

2
What is dynamic programming? Dynamic programming is a technique for solving problem and come up an algorithm. Dynamic programming divide the problem into subparts and then solve the subparts and use the solutions of the subparts to come to a solution. The main difference between dynamic programming and divide and conquer design technique is that the partial solutions are stored in dynamic programming but are not stored and used in divide and conquer technique.

3
Its history The term dynamic programming was originally used in the 1940s by Richard Bellman to describe the process of solving problems where one needs to find the best decisions one after another.Richard Bellman

4
Why does it apply? Dynamic programming is typically applied to optimization problems. In such problems there can be many possible solution. Each solution has a value, and we wish to find a solution with the optimal (minimum or maximum) value.

5
Its step The development of dynamic programming algorithm can be broken into a sequence of four steps- 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.

6
Example Matrix-chain multiplication---- If the chain of matrices is (A 1,A 2,A 3,A 4 ), the product A 1 A 2 A 3 A 4 can be fully parenthesized in five distinct ways: (A 1 (A 2 (A 3 A 4 ))) (A 1 ((A 2 A 3 )A 4 )) ((A 1 A 2 )(A 3 A 4 )) ((A 1 (A 2 A 3 ))A 4 ) (((A 1 A 2 )A 3 )A 4 )

7
Matrix-chain Multiplication Problem If columns A equal the numbers of rows of B For A=p*q and B=q*r. the resulting matrix C is a p*r. The matrix-chain multiplication problem- its not actually multiplying matrices.Its goal is only to determine an order for multiplying matrices that has the lowest or highest cost.

8
Matrix-chain Multiplication Apply We have many options because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B and C we would have: associative (A(BC))= ((AB)C) = ((AC)B) =.... However, the order in which we parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency. For example, suppose A is a (10*100) matrix, B is a (100*5) matrix, and C is a (5 × 50) matrix. Then, ((AB)C)= (10*5*100) + (10*50*5) = = 7500 operations (A(BC)) = (100*50*5) + (50*10*100) = = operations.

9
Structure of an Optimal parenthesization The structure of an optimal parenthesization Notation: A i..j = result from evaluating A i A i+1 … A j (i j) Any parenthesization of A i A i+1 … A j must split the product between A k and A k+1 for some integer k in the range i k < j Cost = cost of computing A i..k + cost of computing A k+1..j + cost of multiplying A i..k and A k+1..j together.

10
A recursive solution m[i, j ] = m[i, k] + m[k+1, j ] + p i-1 p k p j for i k < j m[i, i ] = 0 for i=1,2,…,n

11
11-11 A recursive solution But … optimal parenthesization occurs at one value of k among all possible i k < j Check all these and select the best one m[i, j ] = 0 if i=j min {m[i, k] + m[k+1, j ] + p i-1 p k p j } if i

12
11-12 Algorithm to Compute Optimal Cost First computes costs for chains of length l =1 Then for chains of length l =2,3, … and so on Computes the optimal cost bottom-up Input: Array p[0…n] containing matrix dimensions and n Result: Minimum-cost table m and split table s MATRIX-CHAIN-ORDER(p[ ], n) for i 1 to n m[i, i] 0 for l 2 to n for i 1 to n-l+1 j i+l-1 m[i, j] for k i to j-1 q m[i, k] + m[k+1, j] + p[i-1] p[k] p[j] if q < m[i, j] m[i, j] q s[i, j] k return m and s Takes O(n 3 ) time Requires O(n 2 ) space

13
l = 2 10*20*25 = *15*5= 2625 l =3 m[3,5] = min m[3,4]+m[5,5] + 15*10*20 = = 3750 m[3,3]+m[4,5] + 15*5*20 = = 2500

14
Constructing an optimal solution Each entry s[i, j] records the value of k such that the optimal parenthesization of A i A i+1 … A j splits the product between A k and A k+1 A 1..n A 1..s[1..n] A s[1..n]+1..n A 1..s[1..n] A 1..s[1, s[1..n]] A s[1, s[1..n]]+1..s[1..n] Recursive … ((A1(A2A3))((A4 A5)A6))Optimal parenthesization:

15
The end Thank You

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google