May I ask if someone has already seen or faced the following problem?
I need to handle a list of cost/profit values c1/p1, c2/p2, c3/p3,... that satisfies:
- c1≤c2≤c3≤c4...
- p1≤p2≤p3≤p4...
This is an example: 2/3
, 4/5
, 9/15
, 12/19
If one tries
to insert 10/14
in above list, the operation is rejected because of the existing
cost/profit pair 9/12
: it is never useful to increase the cost (9->10
)
and decrease the profit (14->12
). Such lists can arise for instance in
(the states of) dynamic programming algorithms for knapsack problems, where
the costs can represent weights.
If one inserts 7/20
in above list, this should trigger the deletion of 9/15
and 12/19
.
I have written a solution using the C++
std::set
(often implemented with
red-black trees), but I needed to provide a comparison function that eventually
become a bit overly complex. Also, the insertion in such sets takes
logarithmic time and that can easily actually lead to linear time (in terms
of non-amortized complexity) for example when an insertion triggers the deletion of all other
elements.
I wonder if better solutions exist, given that there are countless solutions to implement (ordered) sets, e.g., priority queues, heaps, linked lists, hash tables, etc.
This is a Pareto front (obj1: min cost, obj2: max profit), but I still could not find the best structure to record it.