Coin Change (Dynamic Programming) - algorithm

We usually the following recurrence relation for the coin change problem:
(P is the total money for which we need change and d_i is the coin available)
But can't we make it like this:
(V is the given sorted set of coins available, i and j are its subscripts with Vj being the highest value coin given)
C[p,Vi,j] = C[p,Vi,j-1] if Vj > p
= C[p-Vj,Vi,j] + 1 if Vj <=p
Is there anything wrong with what I wrote? Though the solution is not dynamic but isn't it more efficient?

Consider P = 6, V = {4, 3, 1}. You would pick 4, 1, 1 instead of 3, 3, so 3 coins instead of the optimal 2.

What you've written is similar to the greedy algorithm that works only under certain conditions. (See - How to tell if greedy algorithm suffices for finding minimum coin change?).
Also, in your version you aren't actually using Vi within the recurrence, so it's just a waste of memory

Related

divisibility algorithm on an array of integers

You are given an array of positive integers A. You need to create a subset of the array A with the maximum number of elements with the property that however we take any two numbers of the subset (we can call it x and y), we have that gcd(x,y) is higher than 1. Print the elements of the subset.
For example, if we have n = 4 and the array is {15, 7, 10, 6}, the output needs to be {15, 10, 6}.
Is there any faster solution than backtracking?
Yes, I think you have a better solution. Transform this to a graph problem: each integer is a node; two nodes i and j have an edge connecting them iff gcd(i, j) > 1.
Now, you need to find the largest fully-connected subgraph, (a.k.a. a clique). A little research will show you how to implement that. It's not efficient, but it's more tractable and reliable.
This is equivalent to the Clique problem. So no, there is no efficient solution for this (unless P = NP).

Algorithms for distributing natural numbers in to equal piles

I'm looking for an algorithm that can take in a set of natural numbers, for example:
S = {1, 3, 4, 2, 9, 34, 432, 43}
Then divide them into as equal piles as possible. The number of piles are predefined as n.
The goal is to have the sum of the difference between each pile and the lowest pile, to be the smallest.
Here comes an example.
Let's say you have:
S = { 1, 2, 2, 3, 1, 2, 3 }
n = 3
Then a solution could be
N1 = { 1, 2 }
N2 = { 2, 3 }
N3 = { 1, 2, 3 }
The sum of these piles would be 3, 5 and 6. The error would be: (5 - 3) + (6 - 3) = 5.
The algorithm needs to find the solution with the lowest error.
Any help is appreciated. Please comment if something is unclear.
I would argue that there is no efficient way to solve this problem because it is a NP-hard problem.
Proof:
Let's denote the problem you proposed as P*,
We can reduce the partition problem(known NP-hard) into P* by doing the following
Given a arbitrary partition problem P1, we ask the black box which solve P* to solve P1 with N=2(i.e, divide the set into 2 pile that minimize the different).
If the difference return by the black box is zero, -> there is a solution for P1
If the difference return by the black box is non-zero, -> there isn't a solution for P1
Therefore, P* is NP-hard
This sounds like a variation of the https://en.m.wikipedia.org/wiki/Bin_packing_problem. However, the size of the bins is not given thus it is at least as hard as Bin Packing. Thus the problem is NP-hard.
For an approximate solution you could for example calculate the average bin size and perform an adaptation of first-fit or best-fit in order to allow small overpacking.

Maximizing the minimum difference between elements in a set

I recently thought of this problem, and I thought of an "instinctive" greedy solution but I can't prove its optimality.
You are given N integers, V1, V2, ..., VN and K sets (K < N).
You need to find a way of partitioning the integers into the sets, so that the minimum difference between any two elements in the same set is maximized.
For example, when the integers are 1, 5, 6, 8, 8 and you have 2 sets, an optimal way of partitioning the integers would be
{1, 6, 8}
{5, 8}
So the minimum difference is between 6 and 8, which is 2.
This arrangement is not unique, for example
{1, 5, 8}
{6, 8}
Also gives a minimum difference of 2.
I was thinking, if I can use a greedy algorithm to solve this.
I would sort it first, and then put all V1, V1+K, V1+2K... together, and then all V2, V2+K, V2+2K... together, and so on.
Is there a proof for the optimality of this solution, or a counterexample where this does not work?
Thanks.
Yes, it's optimal. We'll show that if a difference D appears using your process, then for any arrangement of the numbers there's a pair of numbers in the same set which differ by at most D.
To prove it, consider adding the sorted numbers one by one to the K sets. Let's call the sorted numbers x[i]. Suppose we're adding x[n] to one of the sets. The largest value in that set is x[n-k], with x[n]-x[n-k] = D for some D.
Now, the set x[n-k], x[n-k+1], ..., x[n] is a set of k+1 numbers, all of which differ from each other by at most D (for x[n]-x[n-k] = D).
By the pigeon-hole principle, two of these k+1 numbers must fall in the same set no matter how you arrange them, so the maximum minimum distance must be at most D.
This proves that if a distance D appears in your process, then the maximum minimum distance achievable is at most D.
Let D_min be the smallest difference between two numbers in the same set using your process. Then we've shown that the maximum minimum distance achievable is <= D_min, but also D_min <= maximum minimum distance (since D_min is a minimum distance) which shows that D_min is the maximum minimum distance.

Knapsack variation in Dynamic Programming

I'm trying to solve this exercise: we are given n items, where each has a given nonnegative weigth w1,w2,...,wn and value v1,v2,...,vn, and a knapsack with max weigth capacity W. I have to find a subset S of maximum value, subject to two restricions: 1) the total weight of the set should not exceed W; 2) I can't take objects with consecutive index.
For example, with n = 10, possible solutions are {1, 4, 6, 9}, {2, 4, 10} o {1, 10}.
How can I build a correct recurrence?
Recall that the knapsack recursive formula used for the DP solution is:
D(i,w) = max { D(i-1,w) , D(i-1,w-weight[i]) + value[i] }
In your modified problem, if you chose to take i - you cannot take i-1, resulting in the modification of:
D(i,w) = max { D(i-1,w) , D(i-2,w-weight[i]) + value[i] }
^
note here
i-2 instead of i-1
Similar to classic knapsack, it is also an exhaustive search - and thus provides optimal solution for the same reasons.
The idea is given that you have decided to chose i - you cannot chose i-1, so find the optimal solution that uses at most the item i-2. (no change from the original if you decided to exclude i)

From a given number, determine three close numbers whose product is the original number

I have a number n, and I want to find three numbers whose product is n but are as close to each other as possible. That is, if n = 12 then I'd like to get 3, 2, 2 as a result, as opposed to 6, 1, 2.
Another way to think of it is that if n is the volume of a cuboid then I want to find the lengths of the sides so as to make the cuboid as much like a cube as possible (that is, the lengths as similar as possible). These numbers must be integers.
I know there is unlikely to be a perfect solution to this, and I'm happy to use something which gives a good answer most of the time, but I just can't think where to go with coming up with this algorithm. Any ideas?
Here's my first algorithm sketch, granted that n is relatively small:
Compute the prime factors of n.
Pick out the three largest and assign them to f1, f2, f3. If there are less than three factors, assign 1.
Loop over remaining factors in decreasing order, multiply them into the currently smallest partition.
Edit
Let's take n=60.
Its prime factors are 5 3 2 2.
Set f1=5, f2=3 and f3=2.
The remaining 2 is multiplied to f3, because it is the smallest.
We end up with 5 * 4 * 3 = 60.
Edit
This algorithm will not find optimum, notice btillys comment:
Consider 17550 = 2 * 3 * 3 * 3 * 5 * 5
* 13. Your algorithm would give 15, 30, 39 when the best is 25, 26, 27.
Edit
Ok, here's my second algorithm sketch with a slightly better heuristic:
Set the list L to the prime factors of n.
Set r to the cube root of n.
Create the set of three factors F, initially set to 1.
Iterate over the prime factors in descending order:
Try to multiply the current factor L[i] with each of the factors in descending order.
If the result is less than r, perform the multiplication and move on to the next
prime factor.
If not, try the next F. If out of Fs, multiply with the smallest one.
This will work for the case of 17550:
n=17550
L=13,5,5,3,3,3,2
r=25.98
F = { 1, 1, 1 }
Iteration 1:
F[0] * 13 is less than r, set F to {13,1,1}.
Iteration 2:
F[0] * 5 = 65 is greated than r.
F[1] * 5 = 5 is less than r, set F to {13,5,1}.
Iteration 3:
F[0] * 5 = 65 is greated than r.
F[1] * 5 = 25 is less than r, set F to {13,25,1}.
Iteration 4:
F[0] * 3 = 39 is greated than r.
F[1] * 3 = 75 is greated than r.
F[2] * 3 = 3 is less than r, set F to {13,25,3}.
Iteration 5:
F[0] * 3 = 39 is greated than r.
F[1] * 3 = 75 is greated than r.
F[2] * 3 = 9 is less than r, set F to {13,25,9}.
Iteration 6:
F[0] * 3 = 39 is greated than r.
F[1] * 3 = 75 is greated than r.
F[2] * 3 = 27 is greater than r, but it is the smallest F we can get. Set F to {13,25,27}.
Iteration 7:
F[0] * 2 = 26 is greated than r, but it is the smallest F we can get. Set F to {26,25,27}.
Here's a purely math based approach, that returns the optimal solution and does not involve any kind of sorting. Hell, it doesn't even need the prime factors.
Background:
1) Recall that for a polynomial
the sum and product of the roots are given by
where x_i are the roots.
2) Recall another elementary result from optimization theory:
i.e., given two variables such that their product is a constant, the sum is minimum when the two variables are equal to each other. The tilde variables denote the optimal values.
A corollary of this would be that if the sum of two variables whose product is constant, is a minimum, then the two variables are equal to each other.
Reformulate the original problem:
Your question above can now be reformulated as a polynomial root-finding exercise. We'll construct a polynomial that satisfies your conditions, and the roots of that polynomial will be your answer. If you need k numbers that are optimal, you'll have a polynomial of degree k. In this case, we can talk in terms of a cubic equation
We know that:
c is the negative of the input number (assume positive)
a is an integer and negative (since factors are all positive)
b is an integer (which is the sum of the roots taken two at a time) and is positive.
Roots of p must be real (and positive, but that has already been addressed).
To solve the problem, we simply need to maximize a subject to the above set of conditions. The only part not explicitly known right now, is condition 4, which we can easily enforce using the discriminant of the polynomial.
For a cubic polynomial p, the discriminant is
and p has real and distinct roots if ∆>0 and real and coincident (either two or all three) if ∆=0. So, constraint 4 now reads ∆>=0. This is now simple and easy to program.
Solution in Mathematica
Here's a solution in Mathematica that implements this.
And here's a test on some of the numbers used in other answers/comments.
The column on the left is the list and the corresponding row in the column on the right gives the optimal solution.
NOTE:
I just noticed that OP never mentioned that the 3 numbers needed to be integers although everyone (including myself until now) assumed that they were (probably because of his first example). Re-reading the question, and going by the cube example, it doesn't seem like OP was fixated on integers.
This is an important point which will decide which class of algorithms to pursue and needs to be defined. If they need not be integers, there are several polynomial based solutions that can be provided, one of which is mine (after relaxing the integer constraint). If they should be integers, then perhaps an approach using branch-n-bound/branch-n-cut/cutting plane might be more appropriate.
The following was written assuming the OP meant the three numbers to be integers.
The way I've implemented it right now, it can give a non-integer solution in certain cases.
The reason this gives non-integer solutions for x is because I had only maximized a, when actually, b also needs to be minimum (not only that, but also because I haven't placed a constraint on the x_i being integers. It is possible to use the integer root theorem, which would involve finding the prime factors, but makes things more complicated.)
Mathematica code in text
Clear[poly, disc, f]
poly = x^3 + a x^2 + b x + c;
disc = Discriminant[poly, x];
f[n_Integer] :=
Module[{p, \[CapitalDelta] = disc /. c -> -n},
p = poly /.
Maximize[{a, \[CapitalDelta] >= 0,
b > 0 && a < 0 && {a, b} \[Element] Integers}, {a, b}][[
2]] /. c -> -n;
Solve[p == 0]
]
There may be a clever way to find the tightest triplet, as Anders Lindahl is pursuing, but I will focus on a more basic approach.
If I generate all triplets, then I can filter them afterward however I want, so I will start there. The best way I know to generate these uses recursion:
f[n_, 1] := {{n}}
f[n_, k_] := Join ##
Table[
{q, ##} & ### Select[f[n/q, k - 1], #[[1]] >= q &],
{q, #[[2 ;; ⌈ Length##/k ⌉ ]] & # Divisors # n}
]
This function f takes two integer arguments, the number to factor n, and the number of factors to produce k.
The section #[[2 ;; ⌈ Length##/k ⌉ ]] & # Divisors # n uses Divisors to produce a list of all divisors of n (including 1), and then takes from these from the second (to drop the 1) to the Ceiling of the number of divisors divided by k.
For example, for {n = 240, k = 3} the output is {2, 3, 4, 5, 6, 8}
The Table command iterates over this list while accumulating results, assigning each element to q.
The body of the Table is Select[f[n/q, k - 1], #[[1]] >= q &]. This calls f recursively, and then selects from the result all lists that begin with a number >= q.
{q, ##} & ### (also in the body) then "prepends" q to each of these selected lists.
Finally, Join ## merges the lists of these selected lists that are produced by each loop of Table.
The result is all of the integer factors of n into k parts, in lexicographical order. Example:
In[]:= f[240, 3]
Out[]= {{2, 2, 60}, {2, 3, 40}, {2, 4, 30}, {2, 5, 24}, {2, 6, 20},
{2, 8, 15}, {2, 10, 12}, {3, 4, 20}, {3, 5, 16}, {3, 8, 10},
{4, 4, 15}, {4, 5, 12}, {4, 6, 10}, {5, 6, 8}}
With the output of the function/algorithm given above, one can then test triplets for quality however desired.
Notice that because of the ordering the last triplet in the output is the one with the greatest minimum factor. This will usually be the most "cubic" of the results, but occasionally it is not.
If the true optimum must be found, it makes sense to test starting from the right side of the list, abandoning the search if a better result is not found quickly, as the quality of the results decrease as you move left.
Obviously this method relies upon a fast Divisors function, but I presume that this is either a standard library function, or you can find a good implementation here on StackOverflow. With that in place, this should be quite fast. The code above finds all triplets for n from 1 to 10,000 in 1.26 seconds on my machine.
Instead of reinventing the wheel, one should recognize this as a variation of a well known NP-complete problem.
Compute the prime factors of n.
Compute the logarithms of these factors
The problem translates as partitioning these logs into three sums that are as close as possible.
This problem is known as a variation of the Bin Packing problem, known as Multiprocessor scheduling
Given the fact that the Multiprocessor scheduling problem is NP-complete, it's no wonder that it's hard to find an algorithm that does not search the whole problem space and finds the optimum solution.
But I guess there are already several algorithms that deal with either Bin-Packing or Multiprocessor-Scheduling and find near-optimum solutions in efficient manner.
Another related problem (generalization) is the Job shop scheduling. See the wikipedia description with many links to known algorithms.
What wikipedia describes as (the often-used LPT-Algorithm (Longest Processing Time) is exactly what Anders Lindahl came up with first.
EDIT
Here's a shorter explanation using more efficient code, KSetPartitions simplifies things considerably. So did some suggestions from Mr.W. The overall logic remains the same.
Assuming there a at least 3 prime factors of n,
Find the list of triplet KSetPartitions for the prime factors of n.
Multiply each of the elements (prime factors) within each subset to produce all possible combinations for three divisors of n (when multiplied they yield n). You can think of the divisors as the length, width and height of an orthogonal parallelepiped.
The parallelepiped closest to a cube will have the shortest space diagonal. Sum the squares of the three divisors for each case and pick the smallest.
Here's the code in Mathematica:
Needs["Combinatorica`"]
g[n_] := Module[{factors = Join ## ConstantArray ### FactorInteger[n]},
Sort[Union[Sort /# Apply[Times, Union[Sort /#
KSetPartitions[factors, 3]], {2}]]
/. {a_Integer, b_Integer, c_Integer} :>
{Total[Power[{a, b, c}, 2]], {a, b, c}}][[1, 2]]]
It can handle fairly large numbers, but slows down considerably as the number of factors of n grows. The examples below show timings for 240, 2400, ...24000000.
This could be sped up in principle by taking into account cases where a prime factor appears more than once in a divisor. I don't have the know-how to do it yet.
In[28]:= g[240]
Out[28]= {5, 6, 8}
In[27]:= t = Table[Timing[g[24*10^n]][[1]], {n, 6}]
Out[27]= {0.001868, 0.012734, 0.102968, 1.02469, 10.4816, 105.444}

Resources