Reputation: 692
I have two machines, and for each machine I have the intervals during which it was working as a list of the start points and list of the end points of the intervals (for example [0, 5, 9] and [3, 6, 13] would indicate that the machine was working from time 0 to 3, 5 to 6, and 9 to 13).
I would like to determine how to calculate the start and end points of intervals where exactly one machine was working and exactly two machines were working are the same time.
For instance if the start and end points of the second machine intervals were [0, 7] and [2, 10] then the start and end points of intervals where exactly one machine was working are [2, 5, 7, 10] and [3, 6, 9, 13] and the start and end points of intervals where exactly two machines were working are [0, 9] and [2, 10].
I would like to be able to generalize this to three or more machines as well.
This is what I have:
overlapping_interval_starts = {}
overlapping_interval_ends = {}
whole_list = [(start, 's') for start in interval_starts] + [(end, 'e') for end in interval_ends]
whole_list.sort()
count = 1
if whole_list:
previous, _ = whole_list.pop(0)
for elt, elt_type in whole_list:
current = elt
if (count != 0) and (previous != current):
try:
overlapping_interval_starts[count].append(previous)
overlapping_interval_ends[count].append(current)
except KeyError:
overlapping_interval_starts[count] = [previous]
overlapping_interval_ends[count] = [current]
if elt_type == 's':
count += 1
else:
count -= 1
previous = current
It works on the example problem I have, but I am not sure how to verify it is correct in general.
For instance running it on the following problem:
Machine 1: [2, 7], [6, 11]
Machine 2: [1, 5, 11], [3, 10, 13]
Machine 3: [2, 6], [5, 8]
yields:
overlapping_interval_starts = {1: [1, 10, 11],
2: [3, 5, 6, 8],
3: [2, 7]}
overlapping_interval_ends = {1: [2, 11, 13],
2: [5, 6, 7, 10],
3: [3, 8]}
which is technically correct, but there should really only be two intervals (3 to 7 and 8 to 10) where two machines overlap. So I am struggling to see how I can be sure it works in general.
Upvotes: 0
Views: 93
Reputation: 1644
Step through the whole timeline, beginning at the first time-stamp. In other words...
Get the minimum start time of all of the different lists of start times. During the interval beginning at this time, there is one machine working, call this machine M.
Of the remaining start times for all computers other than M, and of the end times for M, find the minimum time-stamp. If it's a start time, then increment the number of computers working at a time. If it's an end-time, decrement the number of computers working.
Continue doing this until you're out of time-stamps. Every time you find a start-time to be the minimum time, you switch to looking at that computer's end-times. Every time you find an end-time to be the minimum time, you switch to looking at that computer's start-times.
Here's a visualization:
Computer A: 0-----3 5-6 9-------13
Computer B: 0---2 7-----10
^
Minimum stamp: 0
Number working: 1
As of time: 0
##################################################
Computer A: X-----3 5-6 9-------13
Computer B: 0---2 7-----10
^
Minimum stamp: 0
Number working: 2
As of time: 0
##################################################
Computer A: X-----3 5-6 9-------13
Computer B: X---2 7-----10
^
Minimum stamp: 2
Number working: 2 1
As of time: 0 2
##################################################
Computer A: X-----3 5-6 9-------13
Computer B: X---X 7-----10
^
Minimum stamp: 3
Number working: 2 1 0
As of time: 0 2 3
##################################################
Computer A: X-----X 5-6 9-------13
Computer B: X---X 7-----10
^
Minimum stamp: 5
Number working: 2 1 0 1
As of time: 0 2 3 5
##################################################
Computer A: X-----X X-6 9-------13
Computer B: X---X 7-----10
^
Minimum stamp: 6
Number working: 2 1 0 1 0
As of time: 0 2 3 5 6
##################################################
Computer A: X-----X X-X 9-------13
Computer B: X---X 7-----10
^
Minimum stamp: 7
Number working: 2 1 0 1 0 1
As of time: 0 2 3 5 6 7
##################################################
Computer A: X-----X X-X 9-------13
Computer B: X---X X-----10
^
Minimum stamp: 9
Number working: 2 1 0 1 0 1 2
As of time: 0 2 3 5 6 7 9
##################################################
Computer A: X-----X X-X X-------13
Computer B: X---X X-----10
^
Minimum stamp: 10
Number working: 2 1 0 1 0 1 2 1
As of time: 0 2 3 5 6 7 9 10
##################################################
Computer A: X-----X X-X X-------13
Computer B: X---X X-----XX
^
Minimum stamp: 13
Number working: 2 1 0 1 0 1 2 1 0
As of time: 0 2 3 5 6 7 9 10 13
##################################################
Computer A: X-----X X-X X-------XX
Computer B: X---X X-----XX
Minimum stamp: Done
Number working: 2 1 0 1 0 1 2 1 0
As of time: 0 2 3 5 6 7 9 10 13
Upvotes: 1