Suppose we have m ordered sets and we want to find their intersection.
Which data structures should we use for the ordered sets and which algorithm would be the most efficient?
same question: Algorithm for N-way merge
It appears that the literature is huge. Thus a better question is this: Which good implementations are there?
You can create binary tree with link to parent node and implement classic algorithm of intersection/union:
iterA to left-most (smallest) node of the tree (i.e. descend over left-most branches to leaf).iterB to first (smallest) node of the ordered set (if it is implemented with ordered array or to the left-most node if as tree).iterA and iterB
iterAiterA and itemBiterBAdvance of binary tree iterator by:
Update:
If you know that your ordered set (walked by iterB) is much smaller than the tree you can use a bit more sophisticated algorithm for intersection:
iterB to beginning of ordered set (lower value).iterA to the node which is minimum upper bound of value iterB.iterA and iterB
itemB to the next value.iterA to the minimum upper bound of value at itemB starting from current itemA.itemB pass all items of ordered set.Where advance to the minimum upper bound from specific node is:
The main idea of searching bound is narrowing upper and minimum bound ("-" is ignored nodes, "..." is new search range):
for B < X < A
U
/ \-
L
-/ \...
for A < X < B
L
-/ \
U
.../ \-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With