如何编写程序来生成排序决策树?

发布于 2024-08-05 13:05:58 字数 513 浏览 6 评论 0 原文

在课堂上,我们得到了一个简单的决策树,用于对 3 个元素(a、b、c)进行排序。

替代文本
(来源:brpreiss.com

当我看到这一点时,我觉得这是有道理的。我能够跟上它。

然而,我现在必须为 4 个元素(a、b、c、d)制作一个决策树,并且叶子的数量刚刚增加到 24 个。

我正在努力以有条理的方式接近决策树,以帮助我跟踪以及我应该在每个分支进行比较的元素。

构建更大的决策树的系统方法是什么?如果我知道如何的话,我什至愿意编写一个程序来吐出可能的叶子结构。

In class we were given a simple decision tree for sorting 3 elements (a,b,c).

alt text
(source: brpreiss.com)

While looking at this, it makes sense to me. I was able to follow it.

However, I now have to make a decision tree for 4 elements (a,b,c,d) and the number of leafs just shot up to 24.

I'm struggling approaching the decision tree in a methodical way that helps me keep track and of the elements I'm suppose to be comparing at each branch.

What is a methodical way of approaching the construction of a larger decision tree? I'd even be willing to write a program to spit out the possible leafs-structure if I knew how to.

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

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

发布评论

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

评论(3

可爱咩 2024-08-12 13:05:58

您可能想研究排序网络。我认为应该可以将给定数量的输入的最佳排序网络转换为决策树。

或者,您可以采用给定的排序算法并逐步执行它,在每次比较时创建一个新分支。

最后,您可以反向执行此操作 - 例如,通过采用合并排序类型的方法:将所有 24 种可能的排序顺序放在树的底部。选择一个比较,并根据结果将叶子分为两组。对每个分支递归重复,直到每个分支只有一片叶子。

You might want to look into Sorting Networks. It should be possible to convert the optimal sorting network for a given number of inputs into a decision tree, I think.

Alternately, you could take a given sorting algorithm and step through it, creating a new branch at each comparison.

Finally, you could do this in reverse - for example, by taking a merge-sort type approach: Lay out all 24 possible sort orders at the bottom of the tree. Pick a comparison, and partition the leaves into two sets based on the outcome. Repeat recursively for each branch until you only have one leaf per branch.

唠甜嗑 2024-08-12 13:05:58

Charles Forgy 描述了这种算法:请参阅 Rete 算法。 (抱歉,WP 中的文章当然不是一个快速答案,但它可能是一个好的开始)

The kind of algorithm has been described by Charles Forgy: see the Rete algorithm. (I'm sorry, the article in WP is certainly not a quick answer but it might be a good start)

爱的故事 2024-08-12 13:05:58

在本例中,一种简单的方法是扩展现有的树。深度 3 树最多可以排序 2^3=8 个不同的结果,这足以对 3 个元素进行排序,因为 3! = 66 <= 8。要对 4 个元素进行排序,您至少需要深度 5:4! <= 2^5。考虑到 abcd code> 已按您现有的网络排序。

假设xyz已排序,因此x你可以使用这个网络在正确的位置添加新元素 d

// note: read from right to left
d<x<y<z -[yes]- (d<x)? -[yes]-- (d<y) -
x<d<y<z -[no]-/               /
x<y<d<z -[yes]- (d<z)? -[no]-/
x<y<z<d -[no]-/

因此,您基本上可以将现有的树复制 4 次,然后用上面的子树替换每个当前叶子,在每种情况下替换 xyz 的顺序为 abc 在当前叶子中。

请注意,虽然这适用于您的特定情况,并生成最小树,但附加子树以插入“下一个元素”将不会为其他情况生成最小高度排序树。例如,要对 a,b,c,d,e 进行排序,最小高度将为 7,如 5! = 120 和 7^2 = 128 。然而,将 e 放入已排序的 4 个元素列表中的子树本身至少需要深度 3(因为有 5 个可能的插入位置) - 因此我们可以轻松构建一个5+3 = 8 深度树,但需要另一种方法来构建有效的深度 7 树。

对于一般性讨论,Nick's answer 中有关排序网络的链接非常相​​关:您可以从网络构建排序树通过从左到右读取网络,为每个连接创建一个节点,然后将网络的两个变体视为子节点:其中一个已进行交换(例如,a是 false,所以现在 ab 被交换),以及另一个不需要的地方(因为 a)。排序决策树的深度是其排序网络的深度,根据该页面,虽然有一种生成对数深度树/网络(AKS)的算法,但它绝不是简单的。

A simple way, in this case, is to extend the existing tree. A depth-3 tree can sort for up to 2^3=8 different outcomes, which is enough to sort 3 elements, since 3! = 6 and 6 <= 8. To sort 4 elements you need at least depth 5: 4! <= 2^5. We can structure the two new, lowest levels to decide where to insert the d, given that a, b and c are already sorted by your existing net.

Assuming that x, y and z are sorted, so that x<y<z you can use this net to add a new element d at its correct position:

// note: read from right to left
d<x<y<z -[yes]- (d<x)? -[yes]-- (d<y) -
x<d<y<z -[no]-/               /
x<y<d<z -[yes]- (d<z)? -[no]-/
x<y<z<d -[no]-/

So you can essentially take your existing tree, replicate it 4 times, and replace each current leaf with the above sub-tree, in each case replacing x,y and z with the order of a, b and c in the current leaf.

Note that, while this works for your particular case, and yields a minimal tree, appending subtrees to insert "the next element" will not yield minimal-height sorting trees for other cases. For example, to sort a,b,c,d,e, the minimal height would be 7, as 5! = 120 and 7^2 = 128. However, a subtree to place an e into an already-sorted list of 4 elements would need, by itself, at least depth 3 (as there are 5 possible insertion positions) - so we can easily build a 5+3 = 8-depth tree, but would need another approach to build a valid depth-7 tree.

For a general discussion, the link on sorting networks in Nick's answer is very relevant: you can build a sorting tree from a network by reading the network from left to right, creating a node for each connection, and at that point, considering two variants of the network as children: one where the swap has been made (say, a<b is false, so now a and b get swapped), and another where it is not needed (because a<b). The depth of the sorting decision tree is that of its sorting network, and according to that page, while there is an algorithm to generate logarithmic-depth trees/networks (AKS), it is by no means simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文