dynamic-programming

Optimal substructure

家住魔仙堡 提交于 2019-12-05 20:52:45
I'm trying to get a fuller picture of the use of the optimal substructure property in dynamic programming, yet I've gone blind on why we have to prove that any optimal solution to the problem contains within it optimal solutions to the sub-problems. Wouldn't it be enough to show that some optimal solution to the problem has this property, and then use this to argue that since the solution built by our recursive algorithm is at least as good as an optimal solution, it will itself be optimal? In other words, I fail to spot where in the correctness argument for our algorithm we need that all

Is this Longest Common Subsequence Correct?

强颜欢笑 提交于 2019-12-05 20:43:52
I just wrote this implementation to find out the length of the longest increasing subsequence using dynamic programming. So for input as [10, 22, 9, 33, 21, 50, 41, 60, 80] the LIS is 6 and one of the set is [10, 22, 33, 50, 60, 80]. When I run the below code I get the correct answer as 6 with O(n) complexity. Is it correct? def lis(a): dp_lis = [] curr_index = 0 prev_index = 0 for i in range(len(a)): prev_index = curr_index curr_index = i print 'if: %d < %d and %d < %d' % (prev_index, curr_index, a[prev_index], a[curr_index]) if prev_index < curr_index and a[prev_index] < a[curr_index]: print

Algorithm to generate k element subsets in order of their sum

佐手、 提交于 2019-12-05 20:42:06
If I have an unsorted large set of n integers (say 2^20 of them) and would like to generate subsets with k elements each (where k is small, say 5 ) in increasing order of their sums, what is the most efficient way to do so? Why I need to generate these subsets in this fashion is that I would like to find the k-element subset with the smallest sum satisfying a certain condition, and I thus would apply the condition on each of the k-element subsets generated. Also, what would be the complexity of the algorithm? There is a similar question here: Algorithm to get every possible subset of a list,

Given a phrase without spaces add spaces to make proper sentence

£可爱£侵袭症+ 提交于 2019-12-05 20:31:33
This is what I've in mind, but it's O(n^2): For ex: Input is "Thisisawesome", we need to check if adding the current character makes the older found set any longer and meaningful. But in order to see till where we need to back up we'll have to traverse all the way to the beginning. For ex: "awe" and "some" make proper words but "awesome" makes the bigger word. Please suggest how can we improve the complexity. Here is the code: void update(string in) { int len= in.length(); int DS[len]; string word; for(int i=0; i<len; i++) DS[i]=0; for(int i=0; i<len; i++) for(int j=i+1; j<=len; j++) { word =

formulation of general dynamic programming problem

☆樱花仙子☆ 提交于 2019-12-05 20:24:14
I wonder if the objective function of a general dynamic programming problem can always be formulated as in dynamic programming on wiki , where the objective function is a sum of items for action and state at every stage? Or that is just a specical case and what is the general formulation? EDIT: By "dynamic programming problem", I mean a problem that can be solved by dynamic programming technique. Such kind of problems possess the property of optimal problem and optimal structure . But at lease for me it is sometimes not easy to identify such problems, perhaps because I have not become used to

kadane algorithm in java

让人想犯罪 __ 提交于 2019-12-05 18:23:39
问题 I have the following implementation of Kadane's algorithm in java. It is basically to find the maximum sum of contiguous subarray. String[] numbers = string.split(","); int max_so_far = 0; int max_ending_here = 0; for (int i = 0; i < numbers.length-1;i++){ max_ending_here = max_ending_here + Integer.parseInt(numbers[i]); if (max_ending_here < 0) max_ending_here = 0; if (max_so_far < max_ending_here) max_so_far = max_ending_here; } System.out.println(max_so_far); However this doesn't work if

Why does memoization not improve the runtime of Merge Sort?

允我心安 提交于 2019-12-05 17:30:36
Why does memoization not improve the runtime of Merge Sort? I have this question from Assignment task. But as far as I know, Merge Sort uses divide and conquer approach (no overlapping subproblems) but Memoization is based on dynamic programing (with overlapping subproblem). I know the runtime of Merge Sort is O(nlogn) . I even searched on web search engines and there is no result for this question. Is this question wrong? If it sounds wrong but why would a professor give a wrong question in assignment? If the question is not wrong or my understanding about the question, Merge Sort and

Faster edit distance algorithm [closed]

回眸只為那壹抹淺笑 提交于 2019-12-05 13:00:21
Closed . This question needs to be more focused . It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post . Closed 5 years ago . Problem: I know the trivial edit distance DP formulation and computation in O(mn) for 2 strings of size n and m respectively. But I recently came to know that if we only need to calculate the minimum value of edit distance f and it is bounded |f|<=s, then we can calculate it in O(min(m,n) + s^2) or O(s*min(m,n)) [wikipedia] time. Please explain the dp formulation behind it if

Dynamic programming: find the subset with product of all members is equals to given number

此生再无相见时 提交于 2019-12-05 11:09:48
I will find an algorithm for this problem. Input: array of n integers and number k We must find a set of numbers from array, that product of all this numbers in set equals to k is I think, I must use dynamic programming for this task. But I have no idea, how to use it. This is similar to the subset sum problem, where you are required to find a subset whom sum is a value k . Since there is a solution to your problem (you have a subset S whom multiplication is k ) if and only if you have a subset of log(x) for each x in the set, whom sum is log(k) (the same subset, with log on each element), the

Given a stock of integers 0-9, what is the last number I can write before I run out of some integer?

浪尽此生 提交于 2019-12-05 10:13:21
As the title says, given a stock of integers 0-9, what is the last number I can write before I run out of some integer? So if I'm given a stock of, say 10 for every number from 0 to 9, what is the last number I can write before I run out of some number. For example, with a stock of 2 I can write numbers 1 ... 10: 1 2 3 4 5 6 7 8 9 10 at this point my stock for ones is 0, and I cannot write 11. Also note that if I was given a stock of 3, I could still write only numbers 1 ... 10, because 11 would cost me 2 ones, which would leave my stock for ones at -1. What I have come up so far: public class