Find the best set among the many sets based on it's item's cost - algorithm

I have items in sets as a below example. Each item contains particular cost.
I have a max budget. I need to do combination in such a way that in each combination I need at least one item from each set and sum of the costs should be equal to my budget.
Example
A = [a1, a2, a3, a4, ... , a10]
B = [b1, b2, b3, b4, ... , b10]
C = [c1, c2, c3, c4, ... , c10] may be upto G
Max budget = 10
cost of a1 = 2
a2 = 8
b1 = 1
b2 = 7
c1 = 3
c2 = 1
etc
Output can be
[a1, b2, c2] i,e 2+7+1 = 10
[a2, b1, c2] i,e 8+1+1 = 10
[a1, b1, c1] i,e 2+1+3 = 6 Eliminated (since 6 != 10)
goes on
I can have max of 7 sets and 10 items in each. So maximum combinations will be 10^7. Is there any algorithm to achieve this easily. I followed brute force method and it is too expensive.
Thank you.

Related

Avoid accuracy problems while computing the permanent using the Ryser formula

Task
I want to calculate the permanent P of a NxN matrix for N up to 100. I can make use of the fact that the matrix features only M=4 (or slightly more) different rows and cols. The matrix might look like
A1 ... A1 B1 ... B1 C1 ... C1 D1 ... D1 |
... | r1 identical rows
A1 ... A1 B1 ... B1 C1 ... C1 D1 ... D1 |
A2 ... A2 B2 ... B2 C2 ... C2 D2 ... D2
...
A2 ... A2 B2 ... B2 C2 ... C2 D2 ... D2
A3 ... A3 B3 ... B2 C2 ... C2 D2 ... D2
...
A3 ... A3 B3 ... B3 C3 ... C3 D3 ... D3
A4 ... A4 B4 ... B4 C4 ... C4 D4 ... D4
...
A4 ... A4 B4 ... B4 C4 ... C4 D4 ... D4
---------
c1 identical cols
and c and r are the multiplicities of cols and rows. All values in the matrix are laying between 0 and 1 and are encoded as double precision floating-point numbers.
Algorithm
I tried to use the Ryser formula to calculate the permanent. For the formula, one needs to first calculate the sum of each row and multiply all the row sums. For the matrix above this yields
S0 = (c1 * A1 + c2 * B1 + c3 * C1 + c4 * D1)^r1 * ...
* (c1 * A4 + c2 * B4 + c3 * C4 + c4 * D4)^r4
As a next step the same is done with col 1 deleted
S1 = ((c1-1) * A1 + c2 * B1 + c3 * C1 + c4 * D1)^r1 * ...
* ((c1-1) * A4 + c2 * B4 + c3 * C4 + c4 * D4)^r4
and this number is subtracted from S0.
The algorithm continues with all possible ways to delete single and group of cols and the products of the row sums of the remaining matrix are added (even number of cols deleted) and subtracted (odd number of cols deleted).
The task can be solved relative efficiently if one makes use of the identical cols (for example the result S1 will pop up exactly c1 times).
Problem
Even if the final result is small the values of the intermediate results S0, S1, ... can reach values up to N^N. A double can hold this number but the absolute precision for such big numbers is below or on the order of the expected overall result. The expected result P is on the order of c1!*c2!*c3!*c4! (actually I am interested in P/(c1!*c2!*c3!*c4!) which should lay between 0 and 1).
I tried to arrange the additions and subtractions of the values S in a way that the sums of the intermediate results are around 0. This helps in the sense that I can avoid intermediate results that are exceeding N^N, but this improves things only a little bit. I also thought about using logarithms for the intermediate results to keep the absolute numbers down - but the relative accuracy of the encoded numbers will be still bounded by the encoding as floating point number and I think I will run into the same problem. If possible, I want to avoid the usage of data types that are implementing a variable-precision arithmetic for performance reasons (currently I am using matlab).

Uniqueness in Permutation and Combination

I am trying to create some pseudocode to generate possible outcomes for this scenario:
There is a tournament taking place, where each round all players in the tournament are in a group with other players of different teams.
Given x amount of teams, each team has exactly n amount of players. What are the possible outcomes for groups of size r where you can only have one player of each team AND the player must have not played with any of the other players already in previous rounds.
Example: 4 teams (A-D), 4 players each team, 4 players each grouping.
Possible groupings are: (correct team constraint)
A1, B1, C1, D1
A1, B3, C1, D2
But not: (violates same team constraint)
A1, A3, C2, D2
B3, C2, D4, B1
However, the uniqueness constraint comes into play in this grouping
A1, B1, C1, D1
A1, B3, C1, D2
While it does follow the constraints of playing with different teams, it has broken the rule of uniqueness of playing with different players. In this case A1 is grouped up twice with C1
At the end of the day the pseudocode should be able to create something like the following
Round 1 Round 2 Round 3 Round 4
a1 b1 a1 d4 a1 c2 a1 c4
c1 d1 b2 c3 b4 d3 d2 b3
a2 b2 a2 d1 a2 c3 a2 c1
c2 d2 b3 c4 b1 d4 d3 b4
a3 b3 a3 d2 a3 c4 a3 c2
c3 d3 b4 c1 b2 d1 d4 b1
a4 b4 a4 d3 a4 c1 a4 c3
c4 d4 b1 c2 b3 d2 d1 b2
In the example you see that in each round no player has been grouped up with another previous player.
If the number of players on a team is a prime power (2, 3, 4, 5, 7, 8, 9, 11, 13, 16, 17, 19, etc.), then here's an algorithm that creates a schedule with the maximum number of rounds, based on a finite affine plane.
We work in the finite field GF(n), where n is the number of players on a team. GF(n) has its own notion of multiplication; when n is a prime, it's multiplication mod n, and when n is higher power of some prime, it's multiplication of univariate polynomials mod some irreducible polynomial of the appropriate degree. Each team is identified by a nonzero element of GF(n); let the set of team identifiers be T. Each team member is identified by a pair in T×GF(n). For each nonzero element r of GF(n), the groups for round r are
{{(t, r*t + c) | t in T} | c in GF(n)},
where * and + denote multiplication and addition respectively in GF(n).
Implementation in Python 3
This problem is very closely related to the Social Golfer Problem. The Social Golfer Problem asks, given n players who each play once a day in g groups of size s (n = g×s), how many days can they be scheduled such that no player plays with any other player more than once?
The algorithms for finding solutions to instances of Social Golfer problems are a patchwork of constraint solvers and mathematical constructions, which together don't address very many cases satisfactorily. If the number of players on a team is equal to the group size, then solutions to this problem can be derived by interpreting the first day's schedule as the team assignments and then using the rest of the schedule. There may be other constructions.

A feature ranking algorithm

if I have the following partitions or subsets with the corresponding scores as follows:
{X1,X2} with score C1
{X2,X3} with score C2
{X3,X4} with score C3
{X4,X1} with score C4
I want to write an algorithm that will rank the Xs based on the corresponding score of the subset they appeared in.
one way for example will be to do the following:
X1 = (C1 + C4)/2
X2 = (C1 + C2)/2
X3 = (C2 + C3)/2
X4 = (C3 + C4)/2
and then sort the results.
is there a more efficient or better ideas to do the ranking?
If you think that the score of a set is the sum of the scores of each object, you can write your equation in matrix form as :
C = M * X
where C is a vector of length 4 with components C1, C2, C3, C4, M is the matrix (in your case, as I understand this may vary)
1 1 0 0
0 1 1 0
0 0 1 1
1 0 0 1
and X is the unknown. You can then use Gaussian elimination to determine X and the get the ranking as you suggested.

Counting number of quadruples of integers

I saw this question today where we need to count the number of
quadruples of integers
(X1, X2, X3, X4), such that Li ≤ Xi ≤ Ri for i
= 1, 2, 3, 4 and X1 ≠ X2, X2 ≠ X3, X3 ≠ X4, X4 ≠ X1.
input:
Li Ri
1 4
1 3
1 2
4 4
output:
8
1 2 1 4
1 3 1 4
1 3 2 4
2 1 2 4
2 3 1 4
2 3 2 4
3 1 2 4
3 2 1 4
My initial thoughts were using
Principle of Inclusion Exclusion
I was able to find number if unrestricted quadruples but I am not able to figure out how can we find the remaining conditions to reach the final solution. Also I came to know this question can be done using DFS .
How can we do this question with Inclusion Exclusion/ DFS
Inclusion/Exclusion will give you the number of quadruples, but won't give you the quadruples themselves.
Let Ai be the set of quadruples satisfying Lj<=Xj<=Rj for all j, with Xi=X(i+1) (where the indices are cyclic, so X5 means X1). In the example you provided,
A1 = { (1114), (1124), (2214), (2224), (3314), (3324) }
A2 = { (1114), (2114), (3114), (4114), (1224), (2224), (3224), (4224) }
A3 = { } (empty set)
A4 = { (4114), (4214), (4314), (4124), (4224), (4324) }
We also need the intersections of pairs of sets:
A1 cap A2 = { (1114), (2224) } (note first three numbers identical)
A1 cap A3 = { }
A1 cap A4 = { } (can't have X4=X1=X2)
A2 cap A3 = { }
A2 cap A4 = { (4114), (4224) }
A3 cap A4 = { }
Intersections of triples of sets:
A1 cap A2 cap A3 = { }
A1 cap A2 cap A4 = { }
A1 cap A3 cap A4 = { }
A2 cap A3 cap A4 = { }
And the intersection of all the sets:
A1 cap A2 cap A3 cap A4 = { }
Inclusion/exclusion in its complementary form tells us that
|intersection of complements of Ai| = |unrestricted quadruples|
- sum of |Ai| + sum of |Ai cap Aj| - sum of |Ai cap Aj cap Ak|
+ sum of |Ai cap Aj cap Ak cap Al|
where none of the indices i,j,k,l are equal. In your example,
|intersection of complements of Ai| = 4x3x2x1 - (6+8+0+6) + (2+0+0+0+2+0) - (0+0+0+0) + 0
= 24 - 20 + 4 - 0 + 0 = 8
In order to find the |Ai| and their intersections, you have to find intersections of intervals [Li,Ri] and multiply the lengths of intersections by the lengths of unrestricted intervals. For example,
|A1| = |[1234] cap [123]| x |[12]| x |[4]| = 3 x 2 x 1 = 6
|A2 cap A4| = |[123] cap [12]| x |[4] cap [1234]| = |[12]| x |[4]| = 2 x 1 = 2
I don't see what depth first search has to do with it in this approach.
It depends if the sets are disjoint or share elements. For n = 4, meaning quadruples, as you asked about, I think I got it down to between 1 and 4 iterations if we commit the ends to four types describing if x_1 is a member of X2 and x_4 a member of X3.
Example with three iterations:
input = {1,2,3}{1,2}{1,2,3}{3,4}
2 * (1)(12)(123)(3) = (1)(2)(1)(3) = 2 * 1 // x_1 ∈ X2, x_4 ∈ X3
2 * (1)(12)(123)(4) = (1)(2)(13)(4) = 2 * 2 // x_1 ∈ X2, x_4 ∉ X3
1 * (3)(12)(123)(4) = (3)(12)(12,3)(4) = 1 * (2 + 2) // x_1 ∉ X2, x_4 ∉ X3
Total = 10
Example with one iteration:
input = {1,2,3,4}{1,2,3,4}{1,2,3,4}{1,2,3,4} // x_1 ∈ X2, x_4 ∈ X3
12 * (1)(1234)(1234)(2) = (1)(2,34)(134)(2) = 12 * (3 + 4)
Total = 84

Distrubution of a sum S over N different operands

I'm trying to figure out a way to distribute a sum S over N different operands (b1, b2, .., bn), where b1, b2, ... bn are in a fixed ratio, which is determined by another set of operands (a1,a2, .. an)
Consider a situation where:
Candidate A gets a total of Ta votes from N constituencies, with distribution: {a1, a2, a3 .. aN}
Candidate B gets a total of Tb votes (Ta and Tb are unrelated, which means Ta < Tb, Ta = Tb & Ta > Tb are all possible) from M constituencies (IMP: M <= N), distribution unknown.
What is the best approach to allot the Tb votes to the constituencies b1, b2, b3.. bM such that, they are distributed in the same ratio as a1, a2, a3.. aN.
Some Cases:
1.Ideal
Ta = 20 (8,6,4,2) Tb = 10
Then we get: Tb (4,3,2,1)
2.Somewhat less ideal
Ta = 20(8 ,6, 4, 1 , 1) Tb = 10
Then we get (4, 3, 2, 1, 0) which actually means (4,3,2,1) (M < N), and is still tolerable.
Is your a_i sorted always? Assuming that's the case, one way to start is to start assigning b_i from the first value of a_i.
One simple solution:
br = ar * (Tb / Ta)
Which doesn't really work for complex ranges or for a mis-matched Ta and Tb
Like, Ta = 22 (5, 5, 4, 2, 1, 1, 1, 1, 1, 1)
and Tb = 7
UPDATE:
I followed following rules to get to the best solution:
Keep the ratio as (Tb / Ta) and keep on distributing until you run out.
Whenever you round, round up i.e. 3.24 -> 4 and 3.68 also -> 4
e.g. Here: b1 = 5 * 7 / 22 => 2, b2 = 5*7/22 = 2, b3 = 4*7/22 = 2, b4 = 1 (Since just one remains)
So we have Tb = 7(2,2,2,1) Which is closest to (5, 5, 4, 2)

Resources