5

我正在寻找一种算法来执行以下操作。我有一个事件时间表,这些事件跨越可以重叠的时间段。我想将这些事件折叠成一个时间线不重叠的时间段,每个时间段都由一个或多个事件的存在定义。

虽然概念上很简单,但捕捉所有可能的情况并适当地分割时间线可能有点混乱。

为了说明(这里的横轴是时间):

Event A   -----
Event B      ----

变成

Event A   ---
Event A+B    --
Event B        --   

另一个例子:

A    -----------
B       ---
C            --

变成:

A    ---
A+B     ---
A          --
A+C          --
A              -

是否有任何标准算法/数据结构可以做到这一点?

4

1 回答 1

5

Put the start and end times of every event in an array, and sort them in non-decreasing order of time (if a "start" and an "end" happen and the same time, break ties by placing the "end" first).

We'll do a sweep line algorithm.

Traverse the sorted array, while maintaining a set of "active" events: whenever you see a start/end time, respectively add or drop the corresponding event from the set, and add (if the active set is non-empty) an event to your solution.

The resulting set of events is disjoint, and can be labelled as needed.

于 2013-03-29T19:24:14.940 回答