简体   繁体   English

如何使用树结构对对象进行分组(使用java)

[英]how to group objects using tree structure (with java)

Here's what I'm trying to do:这是我正在尝试做的事情:

I'm working on a program and part of what it is supposed to do is to take a large group of people and divide them into a number of groups (given a particular number of participants/groups).我正在开发一个程序,它应该做的一部分是把一大群人分成若干组(给定特定数量的参与者/组)。 However, each person has to travel through each group exactly once (the order doesn't matter) and I want to minimize the number of people who travel together through the different groups.但是,每个人必须恰好通过每个组一次(顺序无关紧要),我想尽量减少一起通过不同组的人数。

So far I've been able to create a tree structure, which has a root branching off into a node for each group, which each branch off into nodes for all the other groups (except the parent group), which each branch into nodes for the remaining groups (except the parent, or the parent's parent, etc).到目前为止,我已经能够创建一个树结构,它有一个根分支到每个组的一个节点,每个组都分支到所有其他组(父组除外)的节点,每个分支到节点其余组(除了父母,或父母的父母等)。

This can be visualized below (given 5 groups -- A,B,C,D,E):这可以在下面看到(给定 5 个组——A、B、C、D、E):

Level 1:    A         B         C           D          E  
Level 2: B C D E   A C D E   A B D E     A B C E    A B C D  
Level 3 (for node E only) :                     BCD ACD ABD ABC  
etc. (with n levels for n groups)

Each level represents one grouping of all the participants and at each level every participant must be in a group.每个级别代表所有参与者的一个分组,并且在每个级别,每个参与者都必须在一个组中。

In my program each person is currently represented as an integer, stored in an ArrayList.在我的程序中,每个人当前表示为 integer,存储在 ArrayList 中。 The whole ArrayList starts at the root, then is divided (randomly) into even groups at each node (in the example above, 5 groups at the first level, 4 at the second, etc) until the end of the tree (when every participant has gone through each group once).整个 ArrayList 从根开始,然后在每个节点(在上面的示例中,第一级 5 个组,第二级 4 个等)(随机)分成偶数组,直到树的末尾(当每个参与者已通过每组一次)。 I'm currently implementing this recursively.我目前正在递归地实现这一点。

What I am having trouble figuring out is how to make sure the number of participants in each group at each level are equivalent (or off by one).我无法弄清楚的是如何确保每个级别的每个组中的参与者数量相等(或相差一个)。 This is easy when the number of participants is equal to the total number of nodes at the last level (or some multiple thereof), but get more tricky when that is not the case (and at each node the participants do not divide evenly into the number of children nodes).当参与者的数量等于最后一层的节点总数(或其倍数)时,这很容易,但如果不是这种情况,则变得更加棘手(并且在每个节点上,参与者不会均匀地分成子节点的数量)。

I'm not looking for specific code to solve this problem, just ideas of how I could ensure evenly sized groups at each level (especially a solution that would work with a wide range of inputed number of participants and groups).我不是在寻找解决这个问题的特定代码,只是关于如何确保每个级别的组大小均匀的想法(特别是一个可以与广泛输入的参与者和组数量一起工作的解决方案)。

Thanks in advance and let me know if I'm unclear at all (as I found it rather hard to describe my problem)!提前感谢,如果我不清楚,请告诉我(因为我发现很难描述我的问题)!

I thought your description was quite good.我觉得你的描述很不错。 If I understand correctly, this could apply to a workshop or something where there are a number of group activities at different stations, and everyone needs to do every station (but ideally with a different mix of people).如果我理解正确,这可能适用于研讨会或在不同站点有许多团体活动的东西,每个人都需要做每个站点(但最好是与不同的人混合)。

I think you might be over complicating your architecture with a tree.我认为您可能过度使用树来复杂化您的架构。 Why not start by dividing the people by the number of groups to get the appropriate number of people per group (randomly decide who starts in what group), then just have each group maintain state about who has been there, then for each 'move', randomly select from the remaining people?为什么不首先将人数除以组数以获得每组的适当人数(随机决定谁从哪个组开始),然后让每个组维护 state 关于谁去过那里,然后是每个“移动” ,从剩下的人中随机 select? You'd always have the right number per group, and you wouldn't have any overlap, and randomness would ensure that people don't just move from group to group together.每个组总是有正确的数字,并且不会有任何重叠,随机性将确保人们不会只是从一个组移动到另一个组。

If your people didn't divide evenly by the number of groups, you could always just have one group that maintains a different number, or apply some additional logic to move the odd number of people to a random group on each move如果您的人员没有按组数平均分配,您总是可以只拥有一个保持不同数量的组,或者应用一些额外的逻辑在每次移动时将奇数人移动到随机组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM