Download presentation

Presentation is loading. Please wait.

Published byKendall Hannibal Modified over 2 years ago

1
Dynamic Programming II By Harry Wiggins

2
Summary DP is a confusing name for a programming technique that dramatically reduces the runtime from exponential to polynomial time. The trick is to find the subproblem within the problem and to come up with the recursive relationship. Then to figure out the right order to fill the table(especially 2D DP problems).

3
Example 1 : Longest increasing subsequence Subproblem : best[i] is the answer for the sequence s_i, … s_n. Recursive formula : best[i]=max{best[j] : j>i and s_j>s_i}. Order : best[n]=1. Then calculate best[n-1], …., best[1]. The answer is best[1].

4
Example 2 : Hidden DP Given a word what is the least number of letters you need to insert anywhere to make it a palindromic word? Q : BANANA A : 1 (add B at end) Did you know : aibohphobia is the fear of palindromes?

5
Solution Looking for longest palindromic subset. Note it’s a match between string and its reverse. Need to find longest common substring (did last time also) (IOI 2000 Day 1 Question 1)

6
Example 3 : Integer knapsack You are designing a contest which isn’t allowed to be longer than a certain predetermined length. You are also given a set of problems. Each problem has a point value and a certain length. Find the contest which has the maximum number of points but within the length constraint.

7
Need to be careful! Can’t re-use a problem. Subproblem is most points for length l contest after using m problems. best[l][m]=max(best[l][m-1],best[l- length(m)][m-1]) However we can be space efficient. If we update in the right order we only need ??? one dimensional arrays.

8
Example 4 : Breaking strings The cost of breaking a string is its length. You are given a string and posititions you want to break it, you need to calculate the least cost (ie the order) of doing that. Ie. n = 20, break at 3,8,10. If left to right it is 49. Best?

9
Solution Look it at differently. for each segment (i,j) let cost(i,j) be the least cost(i,j) = least{(cost(i,p) + cost(p,j) + length(i,j) : all p} This is 0(n^3). It turns out we can do better.

10
Improvement Let P(i,j) be the position where cost(i,j) is minimized. It can be shown (icky maths) that cost(i,j) = least{cost(i,k) + cost(k,j) : P(i,j-1) <= k <= P(i+1,j)} Complexity? O(n^2)

11
The End.

Similar presentations

OK

CSCI 256 Data Structures and Algorithm Analysis Lecture 16 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

CSCI 256 Data Structures and Algorithm Analysis Lecture 16 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on video teleconferencing jobs Ppt on economic reforms in india 1991 Ppt on geotextile Ppt on youth leadership development Ppt on airbag working principle Ppt on information and communication technology in education Ppt on obstructive jaundice Ppt on endangered species tiger Ppt on amelia earhart biography Ppt on percentage for class 6