I will ask this question using Scala syntax, although 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 testresult elements that overlap some element in groundtruth .
I can do it like this:
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.
Is there a faster algorithm for calculating this result or a data structure that can make the exists test more efficient?
PS The algorithm should run on groundtruth and testresult generated with an expression similar to the one below. In other words, there are no guarantees regarding the relationship between the ranges within the list; Range have an average size of 100 or more.
(1 to 1000).map{x => val midPt = r.nextInt(100000); ((midPt - r.nextInt(100)) to (midPt + r.nextInt(100))); }.toList
source share