生成重叠范围的子数组

发布于 2024-10-19 23:32:58 字数 433 浏览 2 评论 0原文

我的名字是蒂莫西·萨松。我正在用 C# 开发一些调度软件,但遇到了一些麻烦。该程序的最后一部分旨在获取大量每周事件(存储为一周中的一天以及开始和结束时间),并将它们分类到包含彼此重叠的事件的子列表中(这样就没有人可能参加了子列表中的两项活动)。

目前,它通过在列表中查找“最长”事件 ((endTime-startTime)*numDays) 并将其及其重叠的每个课程添加到子列表中来实现此目的。然后,它会找到所有“冲突”(不重叠的事件)并解决它们,同时删除尽可能少的课程。我有这么多,但是由于我必须处理的范围数量,我最终得到了相当多的子列表。有没有更好的方法来分割列表,这样我最终会得到更少的子列表?

我考虑过一种蛮力方法,简单地尝试每种可能性并选择最好的,但范围的数量足够高(平均为 100-500),这样做可能会相当慢。任何建议或指示将不胜感激。

感谢您抽出宝贵时间,

蒂莫西·萨松

my name is Timothy Sassone. I am working on developing some scheduling software of a sort in C#, but have run into some trouble. This last part of the program is intended to take a large list of weekly events (stored as day(s) of the week and a start and end time), and sort them into sub-lists containing events that overlap with one-another (such that no one could possibly have attended two of the events in the sublist).

At the moment, it does this by finding the "longest" event in the list ((endTime-startTime)*numDays), and adding it and every course it overlaps with to a sub-list. It then finds all the "conflicts" (events which do not overlap) and resolves them while removing the fewest number of courses possible. This much I have, but with the number of ranges I have to deal with, I end up with a rather high number of sub-lists. Is there any better way to split the list, such that I end up with fewer sub-lists?

I have considered a brute-force method, simply trying every possibility and going with the best, but the number of ranges is high enough (anywhere from 100-500 on average) that doing so could be rather slow. Any suggestions or pointers would be most appreciated.

Thank you for your time,

Timothy Sassone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-10-26 23:32:58

.NET 时间段库 http://www.codeproject .com/KB/datetime/TimePeriod.aspx 包含用于搜索重叠时间段的 TimePeriodIntersector

通过对时间线上的所有时刻进行计数/排序,使用线性快速算法计算重叠。

TimePeriodIntersector 的用法如下所示:

// ----------------------------------------------------------------------
public void TimePeriodCombinerSample()
{
  TimePeriodCollection periods = new TimePeriodCollection();

  periods.Add( new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 04 ), new DateTime( 2011, 3, 08 ) ) );

  periods.Add( new TimeRange( new DateTime( 2011, 3, 15 ), new DateTime( 2011, 3, 18 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 18 ), new DateTime( 2011, 3, 22 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 20 ), new DateTime( 2011, 3, 24 ) ) );

  periods.Add( new TimeRange( new DateTime( 2011, 3, 26 ), new DateTime( 2011, 3, 30 ) ) );

  TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>();
  ITimePeriodCollection combinedPeriods = periodCombiner.CombinePeriods( periods );

  foreach ( ITimePeriod combinedPeriod in combinedPeriods )
  {
    Console.WriteLine( "Combined Period: " + combinedPeriod );
  }
  // > Combined Period: 01.03.2011 - 10.03.2011 | 9.00:00
  // > Combined Period: 15.03.2011 - 24.03.2011 | 9.00:00
  // > Combined Period: 26.03.2011 - 30.03.2011 | 4.00:00
} // TimePeriodCombinerSample

The Time Period Library for .NET http://www.codeproject.com/KB/datetime/TimePeriod.aspx includes the TimePeriodIntersector to search for overlapping time periods.

The overlaps are calculated with a linear and fast algorithm by counting/sorting all moments on the time line.

And the usage of TimePeriodIntersector looks like:

// ----------------------------------------------------------------------
public void TimePeriodCombinerSample()
{
  TimePeriodCollection periods = new TimePeriodCollection();

  periods.Add( new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 04 ), new DateTime( 2011, 3, 08 ) ) );

  periods.Add( new TimeRange( new DateTime( 2011, 3, 15 ), new DateTime( 2011, 3, 18 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 18 ), new DateTime( 2011, 3, 22 ) ) );
  periods.Add( new TimeRange( new DateTime( 2011, 3, 20 ), new DateTime( 2011, 3, 24 ) ) );

  periods.Add( new TimeRange( new DateTime( 2011, 3, 26 ), new DateTime( 2011, 3, 30 ) ) );

  TimePeriodCombiner<TimeRange> periodCombiner = new TimePeriodCombiner<TimeRange>();
  ITimePeriodCollection combinedPeriods = periodCombiner.CombinePeriods( periods );

  foreach ( ITimePeriod combinedPeriod in combinedPeriods )
  {
    Console.WriteLine( "Combined Period: " + combinedPeriod );
  }
  // > Combined Period: 01.03.2011 - 10.03.2011 | 9.00:00
  // > Combined Period: 15.03.2011 - 24.03.2011 | 9.00:00
  // > Combined Period: 26.03.2011 - 30.03.2011 | 4.00:00
} // TimePeriodCombinerSample
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文