darwinrlo / public

0 stars 0 forks source link

Merge Intervals #34

Open darwinrlo opened 4 years ago

darwinrlo commented 4 years ago

Claim: Sorting by start time puts overlapping intervals right next to each other.

If two intervals A and C overlap, then if there is an interval B whose start time falls between A and C, then B overlaps with both A and C.

B is between A and C in the sorted order, so one of the following situations holds:

Claim: If there is more than one interval between A and C in the sorted order, then they all overlap with A and C and with each other.

Conclusion: If the intervals are sorted by start time, then if two intervals overlap, any interval between them in the sorted order overlap with one or both of them. These intervening intervals can be merged with one of the delimiting intervals, which in turn can be merged with each other to form a single interval.

Claims

darwinrlo commented 4 years ago

If two intervals overlap, then any intervals between them overlap with both of them. They can all be merged into one interval. This is one case.

darwinrlo commented 4 years ago

Case: Two intervals do not overlap, but they're connected by a chain of overlapping intervals.

In the sorted order, the chain is not broken by a non-overlapping interval. In general, as we have shown previously, in the sorted order, overlapping intervals do not have intervening intervals that do not overlap with at least one of them.

The intervening intervals can be merged with one another. Then they can be merged with the original overlapping intervals, which can then be merged into one interval.

darwinrlo commented 4 years ago

Let a and b be the start and end of a chain of overlapping intervals. That is, a does not overlap with the interval that precedes it and b does not overlap with the interval that follows it. As we have established, this chain is contiguous in the sorted order. The whole chain can be merged into one interval.

Claim: There are no other intervals left to merge with. There are no other overlapping intervals.

This is not possible because, between two overlapping intervals in the sorted order, there are no intervening intervals that do not overlap with either of the overlapping intervals.

darwinrlo commented 4 years ago

Draft 2

(Better but still not well-written. Will need a third draft.)

darwinrlo commented 4 years ago

Consider two intervals a and b such that a.start <= b.start. Then a and b overlap iff b.start <= a.end.

Let a and c be two overlapping intervals such that a.start <= c.start. Let b be an interval between a and c in the sorted order. Then b.start >= a.start and b.start <= c.start.

Since a and c overlap, c.start <= a.end, which means b.start <= a.end too. Since b.start >= a.start and b.start <= a.end, b overlaps with a.

Conclusion: All intervals between a and c in the sorted order overlap with a.

darwinrlo commented 4 years ago

Let a[0], a[1], ..., a[n] be a maximal chain of overlapping intervals in the sorted order. Maximal means that a[0] does not overlap with the interval that precedes it and a[n] does not overlap with the interval that follows it. Merge them into a single interval A.

Claim: There are no other intervals that overlap with a[0] and no other intervals that overlap a[n]. This follows from there not being intervening intervals between two overlapping intervals, and we are given that a[0] does not overlap with the interval that precedes it and a[n] does not overlap with the interval that follows it.

darwinrlo commented 4 years ago

Say we have two intervals A and C with A appearing before C in the sorted order. They are not next to each other in the sorted order, which means there are intervals B[0], ..., B[n] between them. The intervals between them do not overlap with either of them. Then A and C do not overlap.

Rigorously:

darwinrlo commented 4 years ago

Say we have intervals A and C in the sorted order. They are not next to each other. In between them are intervals B[0], ..., B[n]. Somewhere between B[0] and B[n], B[i] does not overlap with B[i+1]. Then A and C do not overlap.

(We can prove this rigorously, but it would be pedantic to do so.)

darwinrlo commented 4 years ago

If A and C overlap, then they either overlap directly or there is an unbroken chain of overlapping intervals between them. If there were a broken chain, we can show that A and C do not overlap.

All intervals in a chain of overlapping intervals are in the same contiguous segment of the sorted order.