I have following problem e.g:
Given a bucket with symbols1 1 2 3 3 4
And book of recipes to create pairs e.g:12
13
24
Select from bucket optimal pairing, leaving as little as possible symbols in the bucket. So using the examplary values above the optimal pairing would be:13
13
24
Which would use all the symbols given.
Naive picking from the bucket could result in something like:12
13
Leaving the 3
and 4
unmatched. 3
and 4
cannot be matched because the book does not contain a recipe for that particular connection
Notes:
Real problem consits on average of: 500 elements in bucket in about 30 kind of symbols.
We've tried to implement the solution using the bruteforce algorithm, however I am afraid that even our grandchildren will not live long enough to see the result :).
There is no limit to the size of recipe book, it could even have every possible in the bucket. Pair made of the same element twice is not allowed.
The answer is not required to empty the bucket completely. Its just about getting the most pairs out of the bucket. Its okay to leave some in the bucket. It would be best to look for the optimal solution, however close approximation is also good enough.
I will appreciate an answer that proposes/gives hint to an algorithm to solve the problem.
Examples:
Bucket:1 1 2 2 2 2 3 3 3 4 5 6 7 8 8
Recipe book:12 34 15 68
Optimal result (one of possible):{1 2} {1 2} {3 4} {6 8}
Leftover:2
2
3
3
5
7
8
12
13
example, what is wrong with pairing34
? – Tim Biegeleisen1
symbol can be expressed asx+y=2
where 2 is the count of1
symbols, andx
&y
are the number of12
&13
respectively. Next you'll havex+z=1
,y=2
,z=1
and you could obviously solvex=0
from that to get your required result. Linear algebra will come in when no solution exists and you need an approximation. – Amita: 12, b: 34, c: 15, d: 68
, and the equations are:a+c=2
a=4
b=3
b=1
c=1
d=1
(nothing)=1
d=2
. Now, obviously there's no solution to this set of equations so you need an approximation, but you can see how your answer fits as well as 1 of each pair (a=b=c=d=1
). – Amit