I need help with algorithm I'm working on, my problem have 3 main components:
Item: an object with 2 fields
{
Integer reward;
Boolean_2D_Array area;
}
Generator:a module that generates items according to certain logic.
Collection: where I keep generated Items
For every items that arrives from the Generator I need to decide one of the following:
- it goes into the collection
- I throw it away
- I throw other item from the collection and put it instead
The decision is made according to “preferable” criteria, item A is prefferable to item B iff:
- A.reward >= B.reward
B.area is subset of A.area - This rule also means that there is no True value in B that is False in A, it can be achive as:
XOR(A.area,OR(A.area,B.area)) == 0
in example:
A B
0011 0001
0011 0011
1111 0111
in the above example B.area is subset of A.area but A.area is not subset of B.area.
So if I get item from the generator, and there it is not preferable of any other item then I put it in the collection, In a preferable case - I'll keep only one of the items. for example, If the above A came from the generater and I have B in my collection, I'll throw B from the collection and keep A (assuming same reward for both).
My Problem: how to manage the collection?
As an initial implementation I hold a simple linkedlist, and for every item i get from the generator I iterate over all existing items and check the preferability according to the reward size and subset, but is there a better way? how can I store the Items in a way that will allow me to skip some of the subset checks? I mean, in my naiive implementation I do O(n^2) checks, is there a way to store the items in other data structure that will hold information regarding the area and by that minimize the checks needed?
P.S. All True values in the item's area are connected (you can travel from any true value to any other true value by moving [left,right,up,down] and tuching only true values all the way.
EDIT: as per Doc's request
The number of items can reach 100K easely, acutally it can be much bigger, 10M or even more -> this is part of problem solving mechanism where the problem size can be infinite so the better the impl. will be the bigger problems I can solve.
the 2D bit array represent an aspect of the problem so also the bigger the better, for now it's about less than 1000 bits.
the subset check implemented the best I can think (in c++, with xor & or of bytes) but when the collection gets big it takes a huge amount of time to progress.