使用 Perl 确定非重叠位置
我有一个位置集合 - 这是数据结构的示例。
my $locations =
{
loc_1 =>
{
start => 1,
end => 193,
},
loc_2 =>
{
start => 180,
end => 407,
},
loc_3 =>
{
start => 329,
end => 684,
},
loc_4 =>
{
start => 651,
end => 720,
},
};
确定非重叠位置的每种可能组合的最佳方法是什么?这个例子的答案看起来像这样。请记住,可能有一个或多个位置,并且这些位置可能重叠也可能不重叠。
my $non_overlapping_locations =
[
{
loc_1 =>
{
start => 1,
end => 193,
},
loc_3 =>
{
start => 329,
end => 684,
},
},
{
loc_1 =>
{
start => 1,
end => 193,
},
loc_4 =>
{
start => 651,
end => 720,
},
},
{
loc_2 =>
{
start => 180,
end => 407,
},
loc_4 =>
{
start => 651,
end => 720,
},
}
];
更新:ysth
的回复帮助我发现了措辞中的缺陷。我想我对//所有可能的//非重叠位置的组合不感兴趣,我只对不是其他解决方案子集的解决方案感兴趣。
I have a collection of locations--here is an example of the data structure.
my $locations =
{
loc_1 =>
{
start => 1,
end => 193,
},
loc_2 =>
{
start => 180,
end => 407,
},
loc_3 =>
{
start => 329,
end => 684,
},
loc_4 =>
{
start => 651,
end => 720,
},
};
What is the best way to determine every possible combination of non-overlapping locations? The answer for this example would look something like this. Keep in mind there may be one or more locations, and these locations may or may not overlap.
my $non_overlapping_locations =
[
{
loc_1 =>
{
start => 1,
end => 193,
},
loc_3 =>
{
start => 329,
end => 684,
},
},
{
loc_1 =>
{
start => 1,
end => 193,
},
loc_4 =>
{
start => 651,
end => 720,
},
},
{
loc_2 =>
{
start => 180,
end => 407,
},
loc_4 =>
{
start => 651,
end => 720,
},
}
];
Update: ysth
's response helped me see a flaw in my wording. I guess I'm not interested in //every possible// combination of non-overlapping locations, I'm only interested in the solutions that are not subsets of other solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个相对简单的优化是按开始和结束对 @locations 进行排序,并预先计算并存储在每个位置的哈希值中(或仅存储在 $locations->{foo} 中),以下位置中有多少个与该位置冲突。然后在 can_add... 的情况下,在递归之前将该数字与 @remaining 拼接起来。
或者为每个位置预先计算所有后续冲突位置的哈希值,并在递归之前使用 grep 将它们全部删除。 (尽管使用这种方法,让剩余的散列开始变得更有意义。)
更新:解决方案的另一种方法是构建要排除的位置树,其中叶子代表解决方案,内部节点代表仍然存在冲突的组合;顶部节点是所有位置,每个节点都有子节点,这些子节点表示删除剩余冲突位置之一,该位置(在某种任意排序方案中)大于父节点(如果有)删除的位置。
A relatively simple optimization would be to sort @locations by start and then end and pre-calculate and store in a hash (or just in $locations->{foo}) for each location how many of the following locations conflict with that location. Then in the can_add... case, splice that number off of @remaining before recursing.
Or pre-calculate for each location a hash of all following locations that conflict and strip them all out with a grep before recursing. (Though with that approach, having remaining be a hash begins to make more sense.)
Update: another approach to a solution would be to build up a tree of locations to exclude, where leaves represent solutions and inner nodes represent combinations that still have conflicts; the top node is all locations, and each node has children that represent removing one of the remaining conflicting locations that's greater (in some arbitrary ordering scheme) than the location removed by the parent node (if any).
首先,我会收集所有单独的点(每个位置的起点和终点),对它们进行排序并将它们保存在列表中。在您的情况下,这将是:
对于该列表中的每个间隔,找出有多少段与其重叠。在您的情况下,这将是:
并在哪些段超过 1 个(在本例中为 3 个)处循环。因此,案例总数为 2 x 2 x 2 = 8 个解决方案(您只能选择一个解决方案中匹配多区间的一段)。
我们找到了 2, 2, 2(或 2, 3, 4)。将它们放在一个数组中并从最后一个开始。递减它直到达到 0。当达到 1 时,递减前一个数字并将第一个数字设置为初始值减 1。
假设我们已经对初始段进行了编号:(在本例中
1,2,3,4 ,5,6
)。重叠的段将包含以下段[1,2]、[2,3]、[3,4]
。所以我们有 3 个重叠的部分。现在我们开始一个选择/消除的递归过程:在每一步中,我们都会查看一个具有多个片段的重叠片段。我们迭代选择,对于每个选择,我们做两件事:从每个后续重叠段中消除我们现在没有选择的段,并在有此选择可能性的每个后续重叠段中强制当前段选择。每一个不重叠的片段都将被视为一个新的选择。搜索下一个多项选择并递归。一旦我们找不到选择,我们就有部分解决方案。我们需要向其中添加不涉及任何重叠的段。打印出来。
在这种情况下,它会像这样: 第一步:
这应该可以正常工作。
现在实现这个的代码(我认为这不是最漂亮的 Perl 代码,但我真的不是一个 Perl 人):
First i would gather all the individual points (the start and end of each location), sort them and keep them in a list. In your case that would be:
For each interval in that list find out how many segments are overlapping it. In your case this would be:
and loop at what segments have more than 1 (in this case there are 3). So the total number of cases is 2 x 2 x 2 = 8 solutions (you can pick only one segment maching a multi interval in a solution).
we found 2, 2, 2 (or 2, 3, 4). Keep them in an array and start from the last. Decrement it until you reach 0. When you reach 1 decrement the previous number and set the first number at the initial value minus 1.
Let's assume we have numbered the initial segments: (in this case
1,2,3,4,5,6
). The overlapping segments will have the following segments in them[1,2], [2,3], [3,4]
. So we have 3 overlapping segments. Now we start a recursive process of choice/elimination:At each step we are looking at an overlapping segment that has multiple segments. We iterate the choices and for each choice we do two things: eliminate from each subsequent overlapping segment the segments we didn't choose now, and force the current segment choice in each subsequent overlapping segments that have this choice as a possibility. Every segment that becomes a non overlapping will be treated as a new choice. Search for next multiple choice and recurse. Once we can't find a choice we have partial solution. We need to add to it the segments that aren't involved in any overlapping. Print it.
In this case it would go like this: First step:
This should work properly.
Now the code implementing this (it's not the prettiest perl code around i assume but i'm really not a perl guy):
我不是 CS 人员,所以我并不反对所有最好的算法,但我想知道是否有比以下更好的方法:
使用
do_not_overlap
和add_to_output
合适定义的。如果您想检查是否重叠……这非常简单。
A 和 B 不会重叠,如果:
您可能需要根据共享边界是否构成重叠进行调整。另外,如果您知道 A 和 B 是否以某种方式排序(按开始或按结束),则可以简化此过程
I'm not a CS guy, so I'm not down with all of the best algorithms, but I wonder if there's a better approach than:
With
do_not_overlap
andadd_to_output
suitable defined.If you're wondering about checking for overlap... that's pretty straightforward.
A and B do not overlap if:
You may need to tweak depending on whether a shared boundary constitutes an overlap. Also, you can simplify this if you know whether A and B are sorted in some way (either by start or by end)
(现实生活侵入 - 抱歉,我会写一个解释 - 并摆脱那些空的 arrayrefs,尽管这是相当微不足道的 - 稍后!)
(Real life intrudes - Apologies, I'll write an explanation - and get ride of those empty arrayrefs, although that's fairly trivial - later!)