Ad distribution problem: an optimal solution? - algorithm

I'm asked to find a 2 approximate solution to this problem:
You’re consulting for an e-commerce site that receives a large number
of visitors each day. For each visitor i, where i € {1, 2 ..... n}, the site
has assigned a value v[i], representing the expected revenue that can be
obtained from this customer.
Each visitor i is shown one of m possible ads A1, A2 ..... Am as they
enter the site. The site wants a selection of one ad for each customer so
that each ad is seen, overall, by a set of customers of reasonably large
total weight.
Thus, given a selection of one ad for each customer, we will
define the spread of this selection to be the minimum, over j = 1, 2 ..... m,
of the total weight of all customers who were shown ad Aj.
Example: Suppose there are six customers with values 3, 4, 12, 2, 4, 6, and
there are m = 3 ads. Then, in this instance, one could achieve a spread of
9 by showing ad A1 to customers 1, 2, 4, ad A2 to customer 3, and ad A3 to
customers 5 and 6.
The ultimate goal is to find a selection of an ad for each customer
that maximizes the spread.
Unfortunately, this optimization problem
is NP-hard (you don’t have to prove this).
So instead give a polynomial-time algorithm that approximates the maximum spread within a factor of 2.
The solution I found is the following:
Order visitors values in descending order
Add the next visitor value (i.e. assign the visitor) to
the Ad with the current lowest total value
Repeat
This solution actually seems to always find the optimal solution, or I simply can't find a counterexample.
Can you find it? Is this a non-polinomial solution and I just can't see it?

With:
v = [7, 6, 5, 3, 3]
m = 2
The optimal solution is:
A1: 6 + 3 + 3 = 12
A2: 5 + 7 = 12
Your solution gives:
A1: 7 + 3 + 3 = 13
A2: 6 + 5 = 11

Related

N groups have a list of numbers that it can pick. Determine if all groups will be able to choose a number if one group chooses number i

Say that there is a group, each with numbers that it can pick.
Group 1: [1, 2, 3, 4],
Group 2: [2, 3, 4],
Group 3: [2, 3, 4],
Group 4: [4]
Say that it initially Group 4 chooses 4 in its list. Now, other groups can't use a 4. One possible arrangement is group 1 chooses the 1, group 3 chooses the 2, and group 2 chooses the 3. However, if group 1 initially chooses 4, there is no valid arrangement, since the only number group 4 can use is 4. Since 4 was used by group 1, group 4 can't choose any numbers, and thus not every group can choose a number. Return true or false if given that group N chooses a number i that they can pick, all the rest of the groups can choose a number. Is there an efficient O(n) or O(n^2) algorithm?
My solution attempt/thoughts:
Originally, I thought that maybe converting this into a directed graph representation and determining if there is a way for all nodes to be traversed to from another node. However, this would mean there are loops involved, and also this is polynomial.

Algorithm for searching the combination with lowest price [duplicate]

I have items with ID (1001, 1002, 1003, 1004, 1005, 1006). There respective quantities are (2, 5, 1, 1, 5, 2): Now I have data like following.There is an offerId for each row.
offerId :{[Item_Id, Item_quantity_on_which_offer_Applied, Discount per quantity]}
1 :{[1001, 2, 21]}
4 :{[1002, 5, 5]}
6 :{[1003, 1, 25] [1004, 1, 25]}
5 :{[1004, 1, 20]}
3 :{[1005, 5, 17.5] [1002, 5, 17.5]}
2 :{[1005, 2, 18.33] [1001, 2, 26] [1006, 2, 21.67]}
Explaination When offer Id 1 is applied, I get Item 2 quantities of Item Id 1002 at 21 rs. discount per quantity i.e. I am getting 21 rs. discount on 1 quantity of 1002.
Objective I want to get the best offer combination. For example in above case best offer combination will be:
OfferId : 2 (discount = 132 (i.e. (18.33+26+21.67)*2))
OfferId : 3 (note: for 3 quantities of item 1005 and 3 quantities of item 1002 since 2 quantities of item 1005 is already in offer Id 2). (discount = 105(i.e. (17.5+17.5)*3))
Now item 1002 has 2 quantity remaining , so:
offerId : 4 (applied on 2 quantities of item 1002)(discount = 10(i.e 5*2))
offerId : 6 (discount = (25+25)*1 = 50)
So in a nutshell, offerids 2, 3 , 4 , 6 will give me best combination of offers where offer 4 is applied on 2 quantities of item 1002,
offer 3 for 3 quantities of item 1005 and 3 quantities of item 1002.
Above is the result I desire to compute best offer combination depending on quantity.
So far, I had been able to find best offer combination without considering quantity. But now my requirement is to consider quantities of Items and then find best offer combination.
It would be really helpful if anyone can provide me with a pseudocode. Any suggestions are greatly appreciated.
P.S. I am writing my code in Golang but solutions in any language are welcomed.
I hope I framed my question correctly. Comment below if any more information regarding question is required.
Thanks in advance.
Even if there is only a single item of each type and every offer gives the same total discount (say, $1), this is NP-hard, since the NP-hard problem Set Packing can be reduced to it: for each set, make an offer for the same elements with total discount $1. Since all offers provide the same benefit, the optimal solution to this constructed instance of your problem is the one that uses the largest number of offers, and this solution corresponds directly to the optimal solution to the original Set Packing problem.
Thus there's no hope for a polynomial-time solution to your problem.

Algorithm to find best offer combination that gives maximum discount on a given set Of Items

I have items with ID (1001, 1002, 1003, 1004, 1005, 1006). There respective quantities are (2, 5, 1, 1, 5, 2): Now I have data like following.There is an offerId for each row.
offerId :{[Item_Id, Item_quantity_on_which_offer_Applied, Discount per quantity]}
1 :{[1001, 2, 21]}
4 :{[1002, 5, 5]}
6 :{[1003, 1, 25] [1004, 1, 25]}
5 :{[1004, 1, 20]}
3 :{[1005, 5, 17.5] [1002, 5, 17.5]}
2 :{[1005, 2, 18.33] [1001, 2, 26] [1006, 2, 21.67]}
Explaination When offer Id 1 is applied, I get Item 2 quantities of Item Id 1002 at 21 rs. discount per quantity i.e. I am getting 21 rs. discount on 1 quantity of 1002.
Objective I want to get the best offer combination. For example in above case best offer combination will be:
OfferId : 2 (discount = 132 (i.e. (18.33+26+21.67)*2))
OfferId : 3 (note: for 3 quantities of item 1005 and 3 quantities of item 1002 since 2 quantities of item 1005 is already in offer Id 2). (discount = 105(i.e. (17.5+17.5)*3))
Now item 1002 has 2 quantity remaining , so:
offerId : 4 (applied on 2 quantities of item 1002)(discount = 10(i.e 5*2))
offerId : 6 (discount = (25+25)*1 = 50)
So in a nutshell, offerids 2, 3 , 4 , 6 will give me best combination of offers where offer 4 is applied on 2 quantities of item 1002,
offer 3 for 3 quantities of item 1005 and 3 quantities of item 1002.
Above is the result I desire to compute best offer combination depending on quantity.
So far, I had been able to find best offer combination without considering quantity. But now my requirement is to consider quantities of Items and then find best offer combination.
It would be really helpful if anyone can provide me with a pseudocode. Any suggestions are greatly appreciated.
P.S. I am writing my code in Golang but solutions in any language are welcomed.
I hope I framed my question correctly. Comment below if any more information regarding question is required.
Thanks in advance.
Even if there is only a single item of each type and every offer gives the same total discount (say, $1), this is NP-hard, since the NP-hard problem Set Packing can be reduced to it: for each set, make an offer for the same elements with total discount $1. Since all offers provide the same benefit, the optimal solution to this constructed instance of your problem is the one that uses the largest number of offers, and this solution corresponds directly to the optimal solution to the original Set Packing problem.
Thus there's no hope for a polynomial-time solution to your problem.

Strategy with regard to how to approach this algorithm?

I was asked this question in a test and I need help with regards to how I should approach the solution, not the actual answer. The question is
You have been given a 7 digit number(with each digit being distinct and 0-9). The number has this property
product of first 3 digits = product of last 3 digits = product of central 3 digits
Identify the middle digit.
Now, I can do this on paper by brute force(trial and error), the product is 72 and digits being
8,1,9,2,4,3,6
Now how do I approach the problem in a no brute force way?
Let the number is: a b c d e f g
So as per the rule(1):
axbxc = cxdxe = exfxg
more over we have(2):
axb = dxe and
cxd = fxg
This question can be solved with factorization and little bit of hit/trial.
Out of the digits from 1 to 9, 5 and 7 can rejected straight-away since these are prime numbers and would not fit in the above two equations.
The digits 1 to 9 can be factored as:
1 = 1, 2 = 2, 3 = 3, 4 = 2X2, 6 = 2X3, 8 = 2X2X2, 9 = 3X3
After factorization we are now left with total 7 - 2's, 4 - 3's and the number 1.
As for rule 2 we are left with only 4 possibilities, these 4 equations can be computed by factorization logic since we know we have overall 7 2's and 4 3's with us.
1: 1X8(2x2x2) = 2X4(2x2)
2: 1X6(3x2) = 3X2
3: 4(2x2)X3 = 6(3x2)X2
4: 9(3x3)X2 = 6(3x2)X3
Skipping 5 and 7 we are left with 7 digits.
With above equations we have 4 digits with us and are left with remaining 3 digits which can be tested through hit and trial. For example, if we consider the first case we have:
1X8 = 2X4 and are left with 3,6,9.
we have axbxc = cxdxe we can opt c with these 3 options in that case the products would be 24, 48 and 72.
24 cant be correct since for last three digits we are left with are 6,9,4(=216)
48 cant be correct since for last three digits we are left with 3,9,4(=108)
72 could be a valid option since the last three digits in that case would be 3,6,4 (=72)
This question is good to solve with Relational Programming. I think it very clearly lets the programmer see what's going on and how the problem is solved. While it may not be the most efficient way to solve problems, it can still bring desired clarity and handle problems up to a certain size. Consider this small example from Oz:
fun {FindDigits}
D1 = {Digit}
D2 = {Digit}
D3 = {Digit}
D4 = {Digit}
D5 = {Digit}
D6 = {Digit}
D7 = {Digit}
L = [D1 D2 D3] M = [D3 D4 D5] E= [D5 D6 D7] TotL in
TotL = [D1 D2 D3 D4 D5 D6 D7]
{Unique TotL} = true
{ProductList L} = {ProductList M} = {ProductList E}
TotL
end
(Now this would be possible to parameterize furthermore, but non-optimized to illustrate the point).
Here you first pick 7 digits with a function Digit/0. Then you create three lists, L, M and E consisting of the segments, as well as a total list to return (you could also return the concatenation, but I found this better for illustration).
Then comes the point, you specify relations that have to be intact. First, that the TotL is unique (distinct in your tasks wording). Then the next one, that the segment products have to be equal.
What now happens is that a search is conducted for your answers. This is a depth-first search strategy, but could also be breadth-first, and a solver is called to bring out all solutions. The search strategy is found inside the SolveAll/1 function.
{Browse {SolveAll FindDigits}}
Which in turns returns this list of answers:
[[1 8 9 2 4 3 6] [1 8 9 2 4 6 3] [3 6 4 2 9 1 8]
[3 6 4 2 9 8 1] [6 3 4 2 9 1 8] [6 3 4 2 9 8 1]
[8 1 9 2 4 3 6] [8 1 9 2 4 6 3]]
At least this way forward is not using brute force. Essentially you are searching for answers here. There might be heuristics that let you find the correct answer sooner (some mathematical magic, perhaps), or you can use genetic algorithms to search the space or other well-known strategies.
Prime factor of distinct digit (if possible)
0 = 0
1 = 1
2 = 2
3 = 3
4 = 2 x 2
5 = 5
6 = 2 x 3
7 = 7
8 = 2 x 2 x 2
9 = 3 x 3
In total:
7 2's + 4 3's + 1 5's + 1 7's
With the fact that When A=B=C, composition of prime factor of A must be same as composition of prime factor of B and that of C, 0 , 5 and 7 are excluded since they have unique prime factor that can never match with the fact.
Hence, 7 2's + 4 3's are left and we have 7 digit (1,2,3,4,6,8,9). As there are 7 digits only, the number is formed by these digits only.
Recall the fact, A, B and C must have same composition of prime factors. This implies that A, B and C have same number of 2's and 3's in their composition. So, we should try to achieve (in total for A and B and C):
9 OR 12 2's AND
6 3's
(Must be product of 3, lower bound is total number of prime factor of all digits, upper bound is lower bound * 2)
Consider point 2 (as it has one possibility), A has 2 3's and same for B and C. To have more number of prime factor in total, we need to put digit in connection digit between two product (third or fifth digit). Extract digits with prime factor 3 into two groups {3,6} and {9} and put digit into connection digit. The only possible way is to put 9 in connection digit and 3,6 on unconnected product. That mean xx9xx36 or 36xx9xx (order of 3,6 is not important)
With this result, we get 9 x middle x connection digit = connection digit x 3 x 6. Thus, middle = (3 x 6) / 9 = 2
My answer actually extends #Ansh's answer.
Let abcdefg be the digits of the number. Then
ab=de
cd=fg
From these relations we can exclude 0, 5 and 7 because there are no other multipliers of these numbers between 0 and 9. So we are left with seven numbers and each number is included once in each answer. We are going to examine how we can pair the numbers (ab, de, cd, fg).
What happens with 9? It can't be combined with 3 or 6 since then their product will have three times the factor 3 and we have at total 4 factors of 3. Similarly, 3 and 6 must be combined at least one time together in response to the two factors of 9. This gives a product of 18 and so 9 must be combined at least once with 2.
Now if 9x2 is in a corner then 3x6 must be in the middle. Meaning in the other corner there must be another multiplier of 3. So 9 and 2 are in the middle.
Let's suppose ab=3x6 (The other case is symmetric). Then d must be 9 or 2. But if d is 9 then f or g must be multiplier of 3. So d is 2 and e is 9. We can stop here and answer the middle digit is
2
Now we have 2c = fg and the remaining choices are 1, 4, 8. We see that the only solutions are c = 4, f = 1, g = 8 and c = 4, f = 8, g = 1.
So if is 3x6 is in the left corner we have the following solutions:
3642918, 3642981, 6342918, 6342981
If 3x6 is in the right corner we have the following solutions which are the reverse of the above:
8192463, 1892463, 8192436, 1892436
Here is how you can consider the problem:
Let's note the final solution N1 N2 N3 N4 N5 N6 N7 for the 3 numbers N1N2N3, N3N4N5 and N5N6N7
0, 5 and 7 are to exclude because they are prime and no other ciphers is a multiple of them. So if they had divided one of the 3 numbers, no other number could have divided the others.
So we get the 7 remaining ciphers : 1234689
where the product of the ciphers is 2^7*3^4
(N1*N2*N3) and (N5*N6*N7) are equals so their product is a square number. We can then remove, one of the number (N4) from the product of the previous point to find a square number (i.e. even exponents on both numbers)
N4 can't be 1, 3, 4, 6, 9.
We conclude N4 is 2 or 8
If N4 is 8 and it divides (N3*N4*N5), we can't use the remaining even numbers (2, 4, 6) to divides
both (N1*N2*N3) and (N6*N7*N8) by 8. So N4 is 2 and 8 does not belong to the second group (let's put it in N1).
Now, we have: 1st grp: 8XX, 2nd group: X2X 3rd group: XXX
Note: at this point we know that the product is 72 because it is 2^3*3^2 (the square root of 2^6*3^4) but the result is not really important. We have made the difficult part knowing the 7 numbers and the middle position.
Then, we know that we have to distribute 2^3 on (N1*N2*N3), (N3*N4*N5), (N5*N6*N7) because 2^3*2*2^3=2^7
We already gave 8 to N1, 2 to N4 and we place 6 to N6, and 4 to N5 position, resulting in each of the 3 numbers being a multiple of 8.
Now, we have: 1st grp: 8XX, 2nd group: X24 3rd group: 46X
We have the same way of thinking considering the odd number, we distribute 3^2, on each part knowing that we already have a 6 in the last group.
Last group will then get the 3. And first and second ones the 9.
Now, we have: 1st grp: 8X9, 2nd group: 924 3rd group: 463
And, then 1 at N2, which is the remaining position.
This problem is pretty easy if you look at the number 72 more carefully.
We have our number with this form abcdefg
and abc = cde = efg, with those digits 8,1,9,2,4,3,6
So, first, we can conclude that 8,1,9 must be one of the triple, because, there is no way 1 can go with other two numbers to form 72.
We can also conclude that 1 must be in the start/end of the whole number or middle of the triple.
So now we have 819defg or 918defg ...
Using some calculations with the rest of those digits, we can see that only 819defg is possible, because, we need 72/9 = 8,so only 2,4 is valid, while we cannot create 72/8 = 9 from those 2,4,3,6 digits, so -> 81924fg or 81942fg and 819 must be the triple that start or end our number.
So the rest of the job is easy, we need either 72/4 = 18 or 72/2 = 36, now, we can have our answers: 8192436 or 8192463.
7 digits: 8,1,9,2,4,3,6
say XxYxZ = 72
1) pick any two from above 7 digits. say X,Y
2) divide 72 by X and then Y.. you will get the 3rd number i.e Z.
we found XYZ set of 3-digits which gives result 72.
now repeat 1) and 2) with remaining 4 digits.
this time we found ABC which multiplies to 72.
lets say, 7th digit left out is I.
3) divide 72 by I. result R
4) divide R by one of XYZ. check if result is in ABC.
if No, repeat the step 3)
if yes, found the third pair.(assume you divided R by Y and the result is B)
YIB is the third pair.
so... solution will be.
XZYIBAC
You have your 7 numbers - instead of looking at it in groups of 3 divide up the number as such:
AB | C | D | E | FG
Get the value of AB and use it to get the value of C like so: C = ABC/AB
Next you want to do the same thing with the trailing 2 digits to find E using FG. E = EFG/FG
Now that you have C & E you can solve for D
Since CDE = ABC then D = ABC/CE
Remember your formulas - instead of looking at numbers create a formula aka an algorithm that you know will work every time.
ABC = CDE = EFG However, you have to remember that your = signs have to balance. You can see that D = ABC/CE = EFG/CE Once you know that, you can figure out what you need in order to solve the problem.
Made a quick example in a fiddle of the code:
http://jsfiddle.net/4ykxx9ve/1/
var findMidNum = function() {
var num = [8, 1, 9, 2, 4, 3, 6];
var ab = num[0] * num[1];
var fg = num[5] * num[6];
var abc = num[0] * num[1] * num[2];
var cde = num[2] * num[3] * num[4];
var efg = num[4] * num[5] * num[6];
var c = abc/ab;
var e = efg/fg;
var ce = c * e
var d = abc/ce;
console.log(d); //2
}();
You have been given a 7 digit number(with each digit being distinct and 0-9). The number has this property
product of first 3 digits = product of last 3 digits = product of central 3 digits
Identify the middle digit.
Now, I can do this on paper by brute force(trial and error), the product is 72 and digits being
8,1,9,2,4,3,6
Now how do I approach the problem in a no brute force way?
use linq and substring functions
example var item = array.Skip(3).Take(3) in such a way that you have a loop
for(f =0;f<charlen.length;f++){
var xItemSum = charlen[f].Skip(f).Take(f).Sum(f => f.Value);
}
// untested code

Project Euler - 68

I have already read What is an "external node" of a "magic" 3-gon ring? and I have solved problems up until 90 but this n-gon thing totally baffles me as I don't understand the question at all.
So I take this ring and I understand that the external circles are 4, 5, 6 as they are outside the inner circle. Now he says there are eight solutions. And the eight solutions are without much explanation listed below. Let me take
9 4,2,3; 5,3,1; 6,1,2
9 4,3,2; 6,2,1; 5,1,3
So how do we arrive at the 2 solutions? I understand 4, 3, 2, is in straight line and 6,2,1 is in straight line and 5, 1, 3 are in a straight line and they are in clockwise so the second solution makes sense.
Questions
Why does the first solution 4,2,3; 5,3,1; 6,1,2 go anti clock wise? Should it not be 423 612 and then 531?
How do we arrive at 8 solutions. Is it just randomly picking three numbers? What exactly does it mean to solve a "N-gon"?
The first doesn't go anti-clockwise. It's what you get from the configuration
4
\
2
/ \
1---3---5
/
6
when you go clockwise, starting with the smallest number in the outer ring.
How do we arrive at 8 solutions. Is it just randomly picking three numbers? What exactly does it mean to solve a "N-gon"?
For an N-gon, you have an inner N-gon, and for each side of the N-gon one spike, like
X
|
X---X---X
| |
X---X---X
|
X
so that the spike together with the side of the inner N-gon connects a group of three places. A "solution" of the N-gon is a configuration where you placed the numbers from 1 to 2*N so that each of the N groups sums to the same value.
The places at the end of the spikes appear in only one group each, the places on the vertices of the inner N-gon in two. So the sum of the sums of all groups is
N
∑ k + ∑{ numbers on vertices }
k=1
The sum of the numbers on the vertices of the inner N-gon is at least 1 + 2 + ... + N = N*(N+1)/2 and at most (N+1) + (N+2) + ... + 2*N = N² + N*(N+1)/2 = N*(3*N+1)/2.
Hence the sum of the sums of all groups is between
N*(2*N+1) + N*(N+1)/2 = N*(5*N+3)/2
and
N*(2*N+1) + N*(3*N+1)/2 = N*(7*N+3)/2
inclusive, and the sum per group must be between
(5*N+3)/2
and
(7*N+3)/2
again inclusive.
For the triangle - N = 3 - the bounds are (5*3+3)/2 = 9 and (7*3+3)/2 = 12. For a square - N = 4 - the bounds are (5*4+3)/2 = 11.5 and (7*4+3)/2 = 15.5 - since the sum must be an integer, the possible sums are 12, 13, 14, 15.
Going back to the triangle, if the sum of each group is 9, the sum of the sums is 27, and the sum of the numbers on the vertices must be 27 - (1+2+3+4+5+6) = 27 - 21 = 6 = 1+2+3, so the numbers on the vertices are 1, 2 and 3.
For the sum to be 9, the value at the end of the spike for the side connecting 1 and 2 must be 6, for the side connecting 1 and 3, the spike value must be 5, and 4 for the side connecting 2 and 3.
If you start with the smallest value on the spikes - 4 - you know you have to place 2 and 3 on the vertices of the side that spike protrudes from. There are two ways to arrange the two numbers there, leading to the two solutions for sum 9.
If the sum of each group is 10, the sum of the sums is 30, and the sum of the numbers on the vertices must be 9. To represent 9 as the sum of three distinct numbers from 1 to 6, you have the possibilities
1 + 2 + 6
1 + 3 + 5
2 + 3 + 4
For the first group, you have one side connecting 1 and 2, so you'd need a 7 on the end of the spike to make 10 - no solution.
For the third group, the minimal sum of two of the numbers is 5, but 5+6 = 11 > 10, so there's no place for the 6 - no solution.
For the second group, the sums of the sides are
1 + 3 = 4 -- 6 on the spike
1 + 5 = 6 -- 4 on the spike
3 + 5 = 8 -- 2 on the spike
and you have two ways to arrange 3 and 5, so that the group is either 2-3-5 or 2-5-3, the rest follows again.
The solutions for the sums 11 and 12 can be obtained similarly, or by replacing k with 7-k in the solutions for the sums 9 resp. 10.
To solve the problem, you must now find out
what it means to obtain a 16-digit string or a 17-digit string
which sum for the groups gives rise to the largest value when the numbers are concatenated in the prescribed way.
(And use pencil and paper for the fastest solution.)

Resources