寻找列表所有可能分裂的算法

6

我正在使用一个我创建的包含一组数字数据的链表。我需要找到一种方法来测试这个列表的每一个可能的两个集合的分割情况,以便于做到这一点,我需要将列表分解为每一个可能的两个集合的组合。顺序不重要,可能会有重复。

For instance, for a list of numbers {1 4 3 1}, the possible splits are 

{1} and {4, 3, 1}
{4} and {1, 3, 1}
{3} and {1, 4, 1}
{1} and {1, 4, 3}
{1, 4} and {3, 1}
{1, 3} and {4, 1}
{1, 1} and {4, 3}

一个由4个数字组成的列表并不难,但随着列表变得更大,事情会变得更加复杂,我很难看出其中的规律。有人能帮我找到一个算法吗?
编辑:
抱歉,我没有看到问题。这是我迄今为止尝试过的。我的循环结构是错误的。当我在常规数组上尝试后弄清楚我在做什么时,我将扩展算法以适应我的链表。
public class TwoSubsets
{
    public static void main(String[] args)
    {
        int[] list = {1, 3, 5, 7, 8};
        int places = 1;

        int[] subsetA = new int[10];
        int[] subsetB = new int[10];


        for (int i = 0; i < list.length; i++)
        {
            subsetA[i] = list[i];       
            for (int current = 0; current < (5 - i ); current++)
            {
                subsetB[current] = list[places];        
                places++;

            }

            System.out.print("subsetA = ");
            for (int j = 0; j < subsetA.length; j++)
            {
                System.out.print(subsetA[j] + " ");
            }

            System.out.println();
            System.out.print("subsetB = ");
            for (int k = 0; k < subsetB.length; k++)
            {
                System.out.print(subsetB[k] + " ");
            }



        }
    }

}

我添加了一个编辑来展示我目前所做的工作。然而,我的问题似乎被“搁置”了。 - Kallaste
3个回答

1

因此,您正在寻找给定集合的所有(适当的)子集,除了互补的子集。如果您的列表有n个元素,则将有2^n个子集。但是,由于您不想要空子集,并且希望将分区(A,B)与(B,A)进行标识,因此您会得到2^(n-1)-1个分区。

要枚举它们,您可以使用具有n个数字的二进制数字将分区标识为其中一个数字,其中位置k上的数字0表示列表的第k个元素在分区的第一个集合中,数字1表示它在第二个集合中。您想要使用其互补数(交换另一个集合),并排除0(空子集)。

因此,您可以使用位运算符。异或运算符给出互补细分。因此,类似以下内容的内容应该可以工作:

int m = (1<<n)-1; // where n is the number of elements, m=111111...11 in binary
for (int i=0;i<m-1;++i) {
    if (i>(m^i)) continue; // this was already considered with 0 and 1 exchanged
    // here the binary digits of i represent the partition
    for (int j=0;j<n;++j) {
       if ((1<<j) & i) {
          // the j-th element of the list goes into the second set of the partition
       } else {
          // the j-th element of the list goes into the first set of the partition
       }
    }
}

只需要添加去重部分即可。 - justhalf
移除什么?if ... continue 会按子集的顺序删除重复项。 - Emanuele Paolini
对于{1,1,2}这个例子,你应该生成分区{[1,2],[1]}和{[1,1],[2]}。但是你的解决方案会给出{[1,2],[1]}两次,因为输入中有两个1。一个来自二进制数100,另一个来自010。 - justhalf

1

代码:

public static void main(String[] args) {
    for(String element : findSplits(list)) {
        System.out.println(element);
    }        
}

static ArrayList<String> findSplits(ArrayList<Integer> set) {
    ArrayList<String> output = new ArrayList();
    ArrayList<Integer> first = new ArrayList(), second = new ArrayList();
    String bitString;
    int bits = (int) Math.pow(2, set.size());
    while (bits-- > 0) {
        bitString = String.format("%" + set.size() + "s", Integer.toBinaryString(bits)).replace(' ', '0');
        for (int i = 0; i < set.size(); i++) {
            if (bitString.substring(i, i+1).equals("0")) {
                first.add(set.get(i));
            } else {
                second.add(set.get(i));
            }
        }
        if (first.size() < set.size() && second.size() < set.size()) {
            if (!output.contains(first + " " + second) && !output.contains(second + " " + first)) {
                output.add(first + " " + second);
            }
        }
        first.clear();
        second.clear();
    }
    return output;
}

输出:

[1] [1, 4, 3]
[3] [1, 4, 1]
[3, 1] [1, 4]
[4] [1, 3, 1]
[4, 1] [1, 3]
[4, 3] [1, 1]
[4, 3, 1] [1]

这符合您的要求吗?如果不是,请告诉我,我会进行调整或必要时添加评论。


0
使用链表来存储子集实际上非常理想--代码将比使用数组更容易组合。
编写一个递归函数来构建子集。它将采用以下参数:
  • 一个“输入”列表
  • 一个“输出”列表
  • 结果向量(这将是一个“收集参数”,如果你知道什么意思的话)
下面是Ruby的大致代码草图:
 # 'input', and 'output' are linked-list nodes
 # we'll assume they have 'value' and 'next' attributes
 # we'll further assume that a new node can be allocated with Node.new(value,next)
 # the lists are null-terminated

 def build_subsets(input, output, results)
   if input.nil?
     results << output
   else
     item = input.value
     input = input.next
     build_subsets(input, Node.new(item, output), results)
     build_subsets(input, output, results)
   end
 end

像这样调用它:

 results = []
 build_subsets(list, nil, results)

之后,所有的子集都将在results中。我知道你需要Java翻译,但这应该很容易转换成Java。我只是给你一个代码如何工作的想法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接