dynamic-programming

How do I optimize these ocaml functions for dynamic interval scheduling?

I have a program that solves the weighted interval scheduling problem using dynamic programming (and believe it or not, it isn't for homework). I've profiled it, and I seem to be spending most of my time filling M with p(...). Here are the functions: let rec get_highest_nonconflicting prev count start = match prev with head :...

dynamic programming: finding largest non-overlapping squares

I really have no idea how to do this using dynamic programming: Problem: I need to find the 2 largest non overlapping squares of a table For example: 5 6 R F F R R F F F F F F F R R F F F F F F F F F F F F F F F F The numbers 5 and 6 are the number of rows and columns respectively, and “R” means reserved and “F” means free. In this ca...

Dynamic programming - Largest square block

I need to find the largest square of 1's in a giant file full of 1's and 0's. I know i have to use dynamic programming. I am storing it in a 2D array. Any help with the algorithm to find the largest square would be great, thanks! ex) 1 0 1 0 1 0 1 0 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 ans: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Patterns for functional, dynamic and aspect-oriented programming

We have a very nice GoF book (Design Patterns: Elements of Reusable Object-Oriented Software) about patterns in Object Oriented Programming, and plenty of articles and resources in the web on this subject. Are there any books (articles, resources) on patterns(best practices) for functional programming? For dynamic programming in langua...

Dynamic Programming: Sum-of-products

Let's say you have two lists, L1 and L2, of the same length, N. We define prodSum as: def prodSum(L1, L2) : ans = 0 for elem1, elem2 in zip(L1, L2) : ans += elem1 * elem2 return ans Is there an efficient algorithm to find, assuming L1 is sorted, the number of permutations of L2 such that prodSum(L1, L2) < some pr...

Effect on performance when using objects in c++

Hey, I have a dynamic programming algorithm for Knapsack in C++. When it was implemented as a function and accessing variables passed into it, it was taking 22 seconds to run on a particular instance. When I made it the member function of my class KnapsackInstance and had it use variables that were data members of that class, it started...

How can I compute the number of characters required to turn a string into a palindrome?

I recently found a contest problem that asks you to compute the minimum number of characters that must be inserted (anywhere) in a string to turn it into a palindrome. For example, given the string: "abcbd" we can turn it into a palindrome by inserting just two characters: one after "a" and another after "d": "adbcbda". This seems to b...

Dynamic programming algorithm N, K problem

An algorithm which will take two positive numbers N and K and calculate the biggest possible number we can get by transforming N into another number via removing K digits from N. For ex, let say we have N=12345 and K=3 so the biggest possible number we can get by removing 3 digits from N is 45 (other transformations would be 12, 15, 35...

formulation of general dynamic programming problem

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...

Finding the minimum length RLE

The classical RLE algorithm compresses data by using numbers to represent how many times the character following a number appears in the text at that position. For example: AAABBAAABBCECE => 3A2B3A2B1C1E1C1E However, in the above example, that method results in even more space being used by the compressed text. A better idea would be t...

dynamic programming pseudocode for Travelling Salesman

hi all. this is a dynamic programming pseudocode for TSP (Travelling Salesman Problem). i understood its optimal substructure but i can't figure out what the code in red brackets do. i am not asking anyone to write the actual code, i just need explanation on what is happening so i can write my own.... thanks:) here is a link for the ps...

Can I find all multisets of given size more efficiently?

Given a set of possible values and a number of "digits," I want to find every unique, unordered grouping of values. For example, say you have an alphabet of A, B, C. All the combinations of 3 digits would be: AAA AAB ABB BBB BBC BCC CCC CCA CAA ABC The specific problem I'm trying to solve is a bit simpler. I'm doing a BlackJack game a...

Box stacking problem

I found this famous dp problem in many places, but I can not figure out how to solve. You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as possible, but you can only stack a box o...

Help with a dynamic-programming problem

The problem is this: There is an array of M binary numbers and each of them is in state '0' or '1'. You can perform several steps in changing the state of the numbers and in each step you are allowed to change the state of exactly N sequential numbers. Given the numbers M, N and the array with the members of course, you are about to calc...

Array: mathematical sequence

An array of integers A[i] (i > 1) is defined in the following way: an element A[k] ( k > 1) is the smallest number greater than A[k-1] such that the sum of its digits is equal to the sum of the digits of the number 4* A[k-1] . You need to write a program that calculates the N th number in this array based on the given first eleme...

Polygon packing 2D

Hi! I have problem of packing 2 arbitrary polygons. I.e. we have 2 arbitrary polygons. We are to find such placement of this polygons (we could make rotations and movements), when rectangle, which circumscribes this polygons has minimal area. I know, that this is a NP-complete problem. I want to choose an efficient algorithm for solvin...

Is there a good book specialized in exercises in dynamic programming?

Is there a good book specialized in exercises on finding recurrence cause i want to train on (dynamic programming) cause i find myself a lot knowing that a problem can be solved dynamically but i can't find the recurrence function . ...

How to determine the longest increasing subsequence using dynamic programming?

Let's say I have a set of integers. I want to find the longest increasing subsequence of that set using dynamic programming. This is simply out of practice, reviewing my old notes from my algorithms course, and I don't seem to understand how this works. Thanks ...

Dynamic programming - Coin change decision problem?

I'm reviewing some old notes from my algorithms course and the dynamic programming problems are seeming a bit tricky to me. I have a problem where we have an unlimited supply of coins, with some denominations x1, x2, ... xn and we want to make change for some value X. We are trying to design a dynamic program to decide whether change f...

Getting the submatrix with maximum sum?

With the help of the Algorithmist and Larry and a modification of Kadane's Algorithm, here is my solution: int dim = matrix.length; //computing the vertical prefix sum for columns int[][] ps = new int[dim][dim]; for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { if (j == 0) { ...