dynamic-programming

How does this C++ function use memoization?

#include <vector> std::vector<long int> as; long int a(size_t n){ if(n==1) return 1; if(n==2) return -2; if(as.size()<n+1) as.resize(n+1); if(as[n]<=0) { as[n]=-4*a(n-1)-4*a(n-2); } return mod(as[n], 65535); } The above code sample using memoization to calculate a recursive formula based on some input n. I know t...

When have you used dynamic programming in the field?

When have you ever directly applied the concepts of dynamic programming to solve a problem in the field? It's sometimes not evident how it can be applied when using it to solve a made-up instance of the knapsack problem. ...

Subset summing

I have a problem related to the subset sum problem and am wondering if the differences make it easier, i.e. solvable in a reasonable amount of time. Given a value V, a set size L, and a sequence of numbers [1,N] S, how many size L subsets of S sum to less than V? This is different than the subset sum problem in three ways: I care h...

Dynamic Programming Recursion and a sprinkle of Memoization

I have this massive array of ints from 0-4 in this triangle. I am trying to learn dynamic programming with Ruby and would like some assistance in calculating the number of paths in the triangle that meet three criterion: You must start at one of the zero points in the row with 70 elements. Your path can be directly above you one row (...

How to compute optimal paths for traveling salesman bitonic tour?

UPDATED After more reading, the solution can be given with the following recurrence relation: (a) When i = 1 and j = 2, l(i; j) = dist(pi; pj ) (b) When i < j - 1; l(i; j) = l(i; j - 1) + dist(pj-1; pj) (c) When i = j - 1 and j > 2, min 1<=k<i (l(k; i) + dist(pk; pj )) This is now starting to make sense, except for part C. How would ...

How to implement this equation in Java?

Ok, this is more of a follow-up question: http://stackoverflow.com/questions/874982/how-to-compute-optimal-paths-for-traveling-salesman-bitonic-tour First of all, for the bitonic tour of the traveling salesman problem I have the following recurrence relation: (a) When i = 1 and j = 2, l(i; j) = dist(pi; pj ) (b) When i < j - 1; l(i; j)...

Algorithm to Divide a list of numbers into 2 equal sum lists

There is a list of numbers. The list is to be divided into 2 equal sized lists, with a minimal difference in sum. The sums have to be printed. #Example: >>>que = [2,3,10,5,8,9,7,3,5,2] >>>make_teams(que) 27 27 Is there an error in the following code algorithm for some case? How do I optimize and/or pythonize this? def make_teams(q...

Has anyone solved a programming puzzle similar to this?

"Suppose you want to build a solid panel out of rows of 4×1 and 6×1 Lego blocks. For structural strength, the spaces between the blocks must never line up in adjacent rows. As an example, the 18×3 panel shown below is not acceptable, because the spaces between the blocks in the top two rows line up. There are 2 ways to build a 10×1 pane...

What is a good algorithm for getting the minimum vertex cover of a tree?

What is a good algorithm for getting the minimum vertex cover of a tree? INPUT: The node's neighbours. OUTPUT: The minimum number of vertices. ...

Dynamic Programming: Number of ways to get at least N bubble sort swaps?

Let's say I have an array of elements for which a total ordering exists. The bubble sort distance is the number of swaps that it would take to sort the array if I were using a bubble sort. What is an efficient (will probably involve dynamic programming) way to calculate the number of possible permutations of this array that will have a...

Help requested: what's a fast way to bounce between lists and tuples in python?

I'm working on a script that takes the elements from companies and pairs them up with the elements of people. The goal is to optimize the pairings such that the sum of all pair values is maximized (the value of each individual pairing is precomputed and stored in the dictionary ctrPairs). They're all paired in a 1:1, each company has...

Where Can I Find Online Examples of Cormen's Assembly-Line Scheduling?

This is a school-related question, although not exactly homework. I'm taking an algorithms course, currently working on Chapter 15 of Cormen's Introduction to Algorithms book. I've been successful at finding plenty of online examples of most of the algorithms in the book, and I can usually find some type of Java applet or other program...

What is dynamic programming?

This is a really general question. What is dynamic programming (how's it different from recursion, memoization, etc)? I've read the wikipedia article on it but I still don't really understand it. Help me 'get' dynamic programming. Thanks. ...

Parallel Dynamic Programming

Are there any good papers discussing how to take a dynamic program and parallelize it? ...

I want to add a singleton method with a closure to a Ruby object (Ruby)

I wish to add a singleton method to a particular object. I wish that when a instance method on a object is first called, it does some work, and then creates a singleton method for said object of the same name (that contains the work). On all subsequent calls on said object, the singleton method would shadow the instance method and would ...

What is dynamic programming algorithm for finding a Hamiltonian cycle?

What is dynamic programming algorithm for finding a Hamiltonian cycle in a undirected graph? I have seen somewhere that there exists a algorithm with O(n*2^n) time complextity ...

Sum of digits of a factorial

Link to the original problem It's not a homework question. I just thought that someone might know a real solution to this problem. I was on a programming contest back in 2004, and there was this problem: Given n, find sum of digits of n!. n can be from 0 to 10000. Time limit: 1 second. I think there was up to 100 numbers for each t...

Dynamic programming problems

I'm looking for some pointers about a dynamic programming problem. I cannot find any relevant information about how to solve this kind of problem. The only kind of problem I know how to solve using dynamic programming is when I have two sequences and create a matrix of those sequences. But I don't see how I can apply that to the followin...

A simple example for someone who wants to understand Dynamic Programming

I am looking for a manageably understandable example for someone who wants to learn Dynamic Programming. There are nice answers here about what is dynamic programming. The fibonacci sequence is a great example, but it is too small to scratch the surface. It looks a great subject to learn about although I haven't taken the algorithms clas...

Java Need help implementing an algorithm

This algorithm is so advanced for my basic programming skills that I just don't see how I could implement it. I'm posting this in a new question because I can't keep bothering the guy who gave me the algorithm alone about this in the comment section in the previous question. MaxSet(node) = 1 if "node" is a leaf MaxSet(node) = Max(1 + S...