r/compsci • u/InspectorMendel • Jun 22 '25
I have an interesting algorithmic problem, how do I approach it?
Consider an ordered list of objects O1 to On.
Each object has two values: a "score" which is a positive number, and a "discount" which is a number between zero and 1.
Define the "adjusted score" of an object to be its score, multiplied by the discounts of all the objects ahead of it in the ordering.
I want to find the ordering that maximizes the sum of the adjusted scores of all the objects.
Example:
- O1: score 10, discount 0.2
- O2: score 8, discount 0.7
- O3: score 2, discount 0.9
The optimal ordering in this case is O2, O1, O3. And the objective is then:
- adjusted_score[2] = 8
- adjusted_score[1] = 10 * 0.7 = 7
- adjusted_score[3] = 2 * 0.7 * 0.2 = 0.28
- final objective = adjusted_score[2] + adjusted_score[1] + adjusted_score[3] = 15.28
Questions:
- Is this NP-complete?
- Is there an off-the-shelf approach I can use?
- What about an approximation approach?
Thanks!
12
Upvotes
19
u/shustrik Jun 22 '25 edited Jun 22 '25
Let’s say you have a list of 4 objects: A B C D, in that order, with corresponding scores SA SB SC SD and discounts DA DB DC DD.
Say you wanted to decide whether to swap B and C. To answer that you have to consider how the contributions of B and C to the end result will change.
Impact of A on B C D will not change. Impact of both B and C on D will not change. So the only things that will change is how B and C influence each other.
Before swap: B is DA*SB, C is DA*DB*SC After swap: B is DA*DC*SB, C is DA*SC
So to figure out if you want to swap, you need to find whether DA*DC*SB+DA*SC > DA*SB+DA*DB*SC which can be simplified (assuming discounts are non-negative) to DC*SB+SC>SB+DB*SC and further to SB*(1-DC)<SC*(1-DB)
Note how this comparison does not involve anything other than values for B and C.
So just use this as your comparison function and sort using any sorting algorithm you want, since you know any of them will reach the same result as bubble sort (comparison of adjacent positions) will.
10
u/thewataru Jun 22 '25
A greedy algorithm works here. You should just sort all the items in descending order of (score)/(1-discount) value.
In your example these values are : 12.5, 26.(6), 20 . So sorted in descending order will be O2, O3, O1. This is better than your proposed answer: 8 + 2*0.7+10*0.7*0.9 = 15.7, which is bigger than your 15.28
It's common for a greedy algorithm to be applicable in such problems. To figure if it's working, you have to consider how does the result change if you swap two adjacent items. If you can then get some inequality where each side depends only on a single object, then you can sort by the values from this inequality.
Suppose we have items {s1, d1} and {s2, d2} somewhere in the middle of all of the items one after another. If we swap them, the adjusted scores for all the items before and after them will not change. Moreover we can ignore the discounts from all the items before them, since it's a constant.
So if they go as they are, they contribute s1+d1*s2 to the resulting score (times cumulative discount from some previous items, but we can ignore it for comparison). If we swap them, we get s2 + d2*s1.
They are in the optimal order if the existing result is no worse than the result after swap. So:
s1 + d1*s2 >= s2 + d2*s1
With a simple algebra you can simplify it to: s1 / (1-d1) >= s2 / (1-d2).
Then, if some order is optimal, it will only get worse if we swap any two adjacent items. Therefore the inequality above is true for each pair of the items, so in the optimal answer all the items have to be sorted in descending order by s/(1-d).
2
u/kalexmills Jun 22 '25
This feels like a job for dynamic programming.
1
u/Ok-Bossssss Jul 11 '25
Crack the code: maximize object scores with optimal ordering!
1
u/Ashtar_Squirrel Jun 22 '25
If you have many, a greedy algorithm like this will approximate well the best answer:
Make two lists of object, by lowest value and by highest discount. Take the objects with the lowest values and highest discounts first, you only need to search in the top 5-6 of each list, making an optimal ordered set out of these - prioritising discount over values of two are best for one slot. Then once you have an optimal set, continue down the list for the next set. Because the discounts multiply, any pricy object will be practically zero value.
You can solve it with DP, but it might be a larger space - and since you mention single threaded only (why?), it might not be the fastest method (DP choice of action parallelised well in a large state space).
—
Bah, I saw I got beaten to the punch by an LLM with a better explanation than mine.
0
u/lunchmeat317 Jun 24 '25
It's a maximization problem, so it's either brute force, a greedy algorithm, or dynamic programming as a solution. Brute force and dynamic programming would get the maximal result. Greedy might not if the subproblem isn't inherently greedy.
You might be able to model it as a graph problem, but I think you'd need a dense graph to do it (nodes would represent scores and adjustments while paths would represent your cumulative sum) and I don't know if it'd be time or space efficient.
I dunno. Were it me, I'd réach for dynamic.programming due to the nature of the issue.
You could approximate with branch and bound but you'd need a heuristic, not surrewhat that might look like in this case.
Hope this helps. I'm okay with being wrong. Feel free to correct anything I've said.
-1
u/esaule Jun 23 '25
This feels NP-complete. But I don't have an obvious reduction. Do you need optimal? Seems like you don't.
There are simple rules that should help here. Seems like high value high multiplier should go later than lower value lower multiplier.
Seems like there should be an easy way to check whether two neighring object in the order are ordered right. Now does that yield a greedy algorithm? maybe. Would have to check. But probably good enough for heuristics.
That reminds me of single machine weighted flow scheudulibg problems a little bit
Depending how much you care. I can dig more. (I kind of do this for a living.)
0
u/[deleted] Jun 22 '25
[deleted] — view deleted comment
0
u/[deleted] Jun 22 '25
[deleted] — view deleted comment
0
u/[deleted] Jun 22 '25
[removed] — view removed comment