I'm going to ask this question using Scala syntax, even though the question is really language independent.
Suppose I have two lists
val groundtruth:List[Range]
val testresult:List[Range]
And I want to find all of the elements of testresult
that overlap some element in groundtruth
.
I can do this as follows:
def overlaps(x:Range,y:Range) = (x contains y.start) || (y contains x.start)
val result = testresult.filter{ tr => groundtruth.exists{gt => overlaps(gt,tr)}}
But this takes O(testresult.size * groundtruth.size)
time to run.
Is there a faster algorithm for computing this result, or a data structure that can make the exists
test more efficient?
P.S. The algorithm should work on groundtruth
and testresult
generated with an expression like the following. In other words, there are no guarantees about the relationships between the ranges within a list, the Range
s have an average size of 100 or larger.
(1 to 1000).map{x =>
val midPt = r.nextInt(100000);
((midPt - r.nextInt(100)) to (midPt + r.nextInt(100)));
}.toList