marzullo's algorithm

given several intervals, how to find a interval which is a intersect of the most number of the given intervals?

Method:

Step1: Represent each returned interval in the following form [start, +1] , [end, -1]
Step2: You now have a list of tuples, the first element of which is a time. Sort these tuples by the time, irrespective of whatever is the second element. So you would have [t(i), val(i)], [t(i+1), val(i+1)], ... where val(i) = ±1. 
Step3: initialize two vars count = 0 and max =0
Step4: Now run through the sorted tuples and set count = count + val(i)
            Compare if count > max then 
                                set max = count
                                set answer = [t(i), t(i+1)]
Step5: Return answer

posted @ 2014-11-14 11:10  zmiao  阅读(208)  评论(0编辑  收藏  举报